In [1]:
import SimPEG as simpeg
import simpegMT as simpegmt
import numpy as np
import matplotlib.pyplot as plt

In [ ]:


In [2]:
## Setup the modelling
# Setting up 1D mesh and conductivity models to forward model data.

# Frequency
nFreq = 31
freqs = np.logspace(3,-3,nFreq)
# Set mesh parameters
ct = 20
air = simpeg.Utils.meshTensor([(ct,16,1.4)])
core = np.concatenate( (  np.kron(simpeg.Utils.meshTensor([(ct,10,-1.3)]),np.ones((5,))) , simpeg.Utils.meshTensor([(ct,5)]) ) )
bot = simpeg.Utils.meshTensor([(core[0],10,-1.4)])
x0 = -np.array([np.sum(np.concatenate((core,bot)))])
# Make the model
m1d = simpeg.Mesh.TensorMesh([np.concatenate((bot,core,air))], x0=x0)

# Setup model varibles
active = m1d.vectorCCx<0.
layer1 = (m1d.vectorCCx<-500.) & (m1d.vectorCCx>=-800.)
layer2 = (m1d.vectorCCx<-3500.) & (m1d.vectorCCx>=-5000.)
# Set the conductivity values
sig_half = 2e-3
sig_air = 1e-8
sig_layer1 = .2
sig_layer2 = .2
# Make the true model
sigma_true = np.ones(m1d.nCx)*sig_air
sigma_true[active] = sig_half
sigma_true[layer1] = sig_layer1
sigma_true[layer2] = sig_layer2
# Extract the model 
m_true = np.log(sigma_true[active])
# Make the background model
sigma_0 = np.ones(m1d.nCx)*sig_air
sigma_0[active] = sig_half
m_0 = np.log(sigma_0[active])

# Set the mapping
actMap = simpeg.Maps.ActiveCells(m1d, active, np.log(1e-8), nC=m1d.nCx)
mappingExpAct = simpeg.Maps.ExpMap(m1d) * actMap

In [3]:
## Setup the layout of the survey, set the sources and the connected receivers

# Receivers 
rxList = []
for rxType in ['z1dr','z1di']:
    rxList.append(simpegmt.SurveyMT.RxMT(simpeg.mkvc(np.array([0.0]),2).T,rxType))
# Source list
srcList =[]
for freq in freqs:
        srcList.append(simpegmt.SurveyMT.srcMT_polxy_1Dprimary(rxList,freq))
# Make the survey
survey = simpegmt.SurveyMT.SurveyMT(srcList)
survey.mtrue = m_true
# Set the problem
problem = simpegmt.ProblemMT1D.eForm_psField(m1d,sigmaPrimary=sigma_0,mapping=mappingExpAct)
from pymatsolver import MumpsSolver
problem.solver = MumpsSolver
problem.pair(survey)

In [4]:
## Read the data
std = 0.05 # 5% std
# Load the files if they exist
if os.path.isfile('MT1D_dtrue.npy') and os.path.isfile('MT1D_dobs.npy'):
    d_true = np.load('MT1D_dtrue.npy')
    d_obs = np.load('MT1D_dobs.npy')
else:
    # Forward model
    d_true = survey.dpred(m_true)
    np.save('MT1D_dtrue.npy',d_true)
    d_obs = d_true + (std*abs(d_true)*np.random.randn(*d_true.shape))
    np.save('MT1D_dobs.npy',d_obs)
# Assign the datas to the survey object
survey.dtrue = d_true
survey.dobs = d_obs
survey.std = np.abs(survey.dobs*std) + 0.01*np.linalg.norm(survey.dobs) #survey.dobs*0 + std
# Assign the data weight
survey.Wd = 1/survey.std #(abs(survey.dobs)*survey.std)

In [5]:
## Setup the inversion proceedure

# Define a counter
C =  simpeg.Utils.Counter()
# Set the optimization
opt = simpeg.Optimization.InexactGaussNewton(maxIter = 30)
opt.counter = C
opt.LSshorten = 0.5
opt.remember('xc')
# Data misfit
dmis = simpeg.DataMisfit.l2_DataMisfit(survey)
# Regularization
# Note: We want you use a mesh the corresponds to the domain we want to solve, the active cells.
if True:
    regMesh = simpeg.Mesh.TensorMesh([m1d.hx[problem.mapping.sigmaMap.maps[-1].indActive]],m1d.x0)
    reg = simpeg.Regularization.Tikhonov(regMesh)
else:
    reg = simpeg.Regularization.Tikhonov(m1d,mapping=mappingExpAct)
reg.smoothModel = True
reg.alpha_s = 1e-7
reg.alpha_x = 1.
# reg.alpha_xx = 0.001
# Inversion problem
invProb = simpeg.InvProblem.BaseInvProblem(dmis, reg, opt)
invProb.counter = C
# Beta cooling
beta = simpeg.Directives.BetaSchedule()
betaest = simpeg.Directives.BetaEstimate_ByEig(beta0_ratio=0.75)
saveModel = simpeg.Directives.SaveModelEveryIteration()
saveModel.fileName = 'Inversion_NoStoppingregMesh_smoothTrue'
# Create an inversion object
inv = simpeg.Inversion.BaseInversion(invProb, directiveList=[beta,betaest,saveModel])

In [6]:
# Run the inversion, given the background model as a start.
mopt = inv.run(m_0)


SimPEG.InvProblem will set Regularization.mref to m0.
SimPEG.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv.
                    ***Done using same solver as the problem***
SimPEG.l2_DataMisfit is creating default weightings for Wd.
SimPEG.SaveModelEveryIteration will save your models as: '###-Inversion_NoStoppingregMesh_smoothTrue.npy'
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment   
-----------------------------------------------------------------------------
   0  2.55e+05  2.18e+05  0.00e+00  2.18e+05    4.01e+04      0              
   1  2.55e+05  2.50e+04  2.21e-03  2.55e+04    5.64e+03      0              
   2  2.55e+05  3.37e+03  4.74e-03  4.58e+03    9.91e+02      0   Skip BFGS  
   3  3.19e+04  1.78e+03  4.90e-03  1.93e+03    2.83e+02      0   Skip BFGS  
   4  3.19e+04  9.92e+02  1.67e-02  1.52e+03    2.15e+02      0   Skip BFGS  
   5  3.19e+04  7.50e+02  1.83e-02  1.33e+03    1.06e+02      0              
   6  3.99e+03  6.23e+02  2.13e-02  7.08e+02    1.40e+02      0   Skip BFGS  
   7  3.99e+03  3.26e+02  5.92e-02  5.62e+02    2.61e+02      0              
   8  3.99e+03  3.58e+02  3.99e-02  5.17e+02    1.18e+02      0              
   9  4.98e+02  3.33e+02  4.07e-02  3.53e+02    1.14e+02      1              
  10  4.98e+02  2.51e+02  1.43e-01  3.22e+02    3.78e+02      0              
  11  4.98e+02  1.75e+02  1.17e-01  2.34e+02    2.27e+02      1              
  12  6.23e+01  7.99e+01  1.45e-01  8.89e+01    5.71e+01      0   Skip BFGS  
  13  6.23e+01  7.36e+01  1.92e-01  8.56e+01    1.16e+02      0   Skip BFGS  
  14  6.23e+01  6.72e+01  1.99e-01  7.96e+01    7.08e+01      0              
  15  7.79e+00  6.04e+01  2.08e-01  6.21e+01    2.97e+01      0              
  16  7.79e+00  5.60e+01  2.35e-01  5.78e+01    4.30e+01      0              
  17  7.79e+00  4.85e+01  3.72e-01  5.14e+01    3.44e+01      0              
  18  9.73e-01  4.34e+01  3.81e-01  4.38e+01    7.57e+00      0              
  19  9.73e-01  4.30e+01  3.60e-01  4.33e+01    1.87e+01      0   Skip BFGS  
  20  9.73e-01  4.20e+01  3.74e-01  4.24e+01    1.23e+01      0              
  21  1.22e-01  4.03e+01  4.12e-01  4.04e+01    1.79e+01      2              
  22  1.22e-01  4.00e+01  4.83e-01  4.01e+01    2.70e+01      0   Skip BFGS  
  23  1.22e-01  3.92e+01  4.79e-01  3.92e+01    2.06e+01      0              
  24  1.52e-02  3.86e+01  5.04e-01  3.86e+01    1.97e+01      2   Skip BFGS  
  25  1.52e-02  3.82e+01  5.02e-01  3.82e+01    3.32e+01      0              
  26  1.52e-02  3.80e+01  4.74e-01  3.80e+01    2.44e+01      0              
  27  1.90e-03  3.76e+01  4.57e-01  3.76e+01    3.62e+01      1   Skip BFGS  
  28  1.90e-03  3.74e+01  4.77e-01  3.74e+01    3.13e+01      1              
  29  1.90e-03  3.74e+01  4.55e-01  3.74e+01    2.68e+01      0              
  30  2.38e-04  3.71e+01  4.30e-01  3.71e+01    2.24e+01      1              
------------------------- STOP! -------------------------
1 : |fc-fOld| = 3.3322e-01 <= tolF*(1+|f0|) = 2.1833e+04
1 : |xc-x_last| = 1.0086e+00 <= tolX*(1+|x0|) = 5.1104e+00
0 : |proj(x-g)-x|    = 2.2396e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 2.2396e+01 <= 1e3*eps       = 1.0000e-02
1 : maxIter   =      30    <= iter          =     30
------------------------- DONE! -------------------------

In [7]:
modList = []
modFiles = glob('*Inversion_NoStoppingregMesh_smoothTrue.npy')
modFiles.sort()
for f in modFiles:
    modList.append(np.load(f))

In [8]:
# simpegmt.Utils.dataUtils.plotMT1DModelData(problem,modList)
# plt.show()

In [9]:
%matplotlib inline
fig = simpegmt.Utils.dataUtils.plotMT1DModelData(problem,[m_0,mopt])
fig.suptitle('No stopping-useMref')
plt.show()


/home/gudni/anaconda/lib/python2.7/site-packages/numpy/ma/core.py:2834: FutureWarning: Numpy has detected that you (may be) writing to an array returned
by numpy.diagonal or by selecting multiple fields in a record
array. This code will likely break in a future numpy release --
see numpy.diagonal or arrays.indexing reference docs for details.
The quick fix is to make an explicit copy (e.g., do
arr.diagonal().copy() or arr[['f0','f1']].copy()).
  if (obj.__array_interface__["data"][0]
/home/gudni/anaconda/lib/python2.7/site-packages/numpy/ma/core.py:2835: FutureWarning: Numpy has detected that you (may be) writing to an array returned
by numpy.diagonal or by selecting multiple fields in a record
array. This code will likely break in a future numpy release --
see numpy.diagonal or arrays.indexing reference docs for details.
The quick fix is to make an explicit copy (e.g., do
arr.diagonal().copy() or arr[['f0','f1']].copy()).
  != self.__array_interface__["data"][0]):

In [12]:
reg.alpha_xx = 0.001
saveModel.fileName = 'Inversion_NoStoppingregMesh_smoothTrueWxx'

In [13]:
moptWxx = inv.run(m_0)


SimPEG.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv.
                    ***Done using same solver as the problem***
SimPEG.SaveModelEveryIteration will save your models as: '###-Inversion_NoStoppingregMesh_smoothTrueWxx.npy'
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment   
-----------------------------------------------------------------------------
   0  2.54e+05  2.18e+05  0.00e+00  2.18e+05    4.01e+04      0              
   1  2.54e+05  2.50e+04  2.21e-03  2.55e+04    5.64e+03      0              
   2  2.54e+05  3.37e+03  4.74e-03  4.57e+03    9.91e+02      0   Skip BFGS  
   3  3.17e+04  1.77e+03  4.92e-03  1.93e+03    2.83e+02      0   Skip BFGS  
   4  3.17e+04  9.89e+02  1.68e-02  1.52e+03    2.15e+02      0   Skip BFGS  
   5  3.17e+04  7.47e+02  1.84e-02  1.33e+03    1.06e+02      0              
   6  3.96e+03  6.19e+02  2.14e-02  7.04e+02    1.39e+02      0   Skip BFGS  
   7  3.96e+03  3.25e+02  5.92e-02  5.60e+02    2.61e+02      0              
   8  3.96e+03  3.55e+02  4.00e-02  5.14e+02    1.17e+02      0              
   9  4.95e+02  3.29e+02  4.10e-02  3.50e+02    1.13e+02      1              
  10  4.95e+02  2.48e+02  1.43e-01  3.19e+02    3.78e+02      0              
  11  4.95e+02  1.75e+02  1.18e-01  2.33e+02    2.31e+02      1              
  12  6.19e+01  7.93e+01  1.45e-01  8.83e+01    5.76e+01      0   Skip BFGS  
  13  6.19e+01  6.97e+01  1.87e-01  8.13e+01    9.12e+01      0   Skip BFGS  
  14  6.19e+01  6.36e+01  1.88e-01  7.53e+01    2.36e+01      0              
  15  7.74e+00  6.22e+01  1.94e-01  6.37e+01    2.16e+01      0              
  16  7.74e+00  5.72e+01  2.23e-01  5.89e+01    3.88e+01      1              
  17  7.74e+00  5.15e+01  3.19e-01  5.40e+01    4.78e+01      1   Skip BFGS  
  18  9.68e-01  4.36e+01  3.51e-01  4.39e+01    1.50e+01      0              
  19  9.68e-01  4.22e+01  4.45e-01  4.27e+01    2.01e+01      0   Skip BFGS  
  20  9.68e-01  4.13e+01  4.20e-01  4.17e+01    9.91e+00      0              
  21  1.21e-01  4.02e+01  4.47e-01  4.02e+01    2.47e+01      1              
  22  1.21e-01  3.96e+01  4.82e-01  3.96e+01    2.93e+01      0   Skip BFGS  
  23  1.21e-01  3.92e+01  4.94e-01  3.93e+01    2.75e+01      0              
  24  1.51e-02  3.90e+01  5.51e-01  3.90e+01    2.27e+01      0   Skip BFGS  
  25  1.51e-02  3.82e+01  5.19e-01  3.82e+01    1.94e+01      0              
  26  1.51e-02  3.79e+01  5.52e-01  3.79e+01    1.71e+01      1   Skip BFGS  
  27  1.89e-03  3.76e+01  5.39e-01  3.76e+01    2.16e+01      1              
  28  1.89e-03  3.76e+01  6.14e-01  3.76e+01    3.82e+01      1   Skip BFGS  
  29  1.89e-03  3.69e+01  5.53e-01  3.69e+01    3.71e+01      0              
  30  2.36e-04  3.57e+01  5.65e-01  3.57e+01    1.91e+01      0              
------------------------- STOP! -------------------------
1 : |fc-fOld| = 1.1797e+00 <= tolF*(1+|f0|) = 2.1833e+04
1 : |xc-x_last| = 1.0384e+00 <= tolX*(1+|x0|) = 5.1104e+00
0 : |proj(x-g)-x|    = 1.9125e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.9125e+01 <= 1e3*eps       = 1.0000e-02
1 : maxIter   =      30    <= iter          =     30
------------------------- DONE! -------------------------

In [14]:
%matplotlib inline
fig = simpegmt.Utils.dataUtils.plotMT1DModelData(problem,[mopt,moptWxx])
fig.suptitle('No stopping-useMref - Wxx')
plt.show()



In [ ]: