In [1]:
from SimPEG import Mesh, Utils, Maps
import simpegSP as SP
from pymatsolver import PardisoSolver
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
dx = 10.
hxind = [(dx, 3, -1.3), (dx, 20), (dx, 3, 1.3)]
hyind = [(dx, 3, -1.3), (dx, 20), (dx, 3, 1.3)]
hzind = [(dx, 3, -1.3), (dx, 15), (dx/2., 5)]
mesh = Mesh.TensorMesh([hxind, hyind, hzind], "CCN")
sigma = np.ones(mesh.nC)*1./100.
actind = mesh.gridCC[:,2] < -0.2
actMap = Maps.InjectActiveCells(mesh, actind, 0.)

In [3]:
x = mesh.vectorCCx[np.logical_and(mesh.vectorCCx>-80, mesh.vectorCCx<80.)]
y = mesh.vectorCCy[np.logical_and(mesh.vectorCCy>-80, mesh.vectorCCy<80.)]
# xyzM = Utils.ndgrid(np.ones_like(x)*-80, np.ones_like(y)*-80, np.r_[0.])
xyzM = Utils.ndgrid(x+dx, y, np.r_[0.])
xyzN = Utils.ndgrid(x, y, np.r_[0.])

In [4]:
prb = SP.Problem_CC(mesh, sigma=sigma, qMap=Maps.IdentityMap(mesh), Solver=PardisoSolver)
rx = SP.Rx.Dipole(xyzN, xyzM)
src = SP.Src.StreamingCurrents([rx], L=np.ones(mesh.nC), mesh=mesh, modelType="CurrentSource")
survey = SP.Survey([src])
survey.pair(prb)

In [5]:
q = np.zeros(mesh.nC)
inda = Utils.closestPoints(mesh, np.r_[-80, 0., -40])
indb = Utils.closestPoints(mesh, np.r_[80, 0.,  -40])
indc = Utils.closestPoints(mesh, np.r_[0, -80., -40])
indd = Utils.closestPoints(mesh, np.r_[0, 80,  -40])

q[inda] = 1.
q[indb] = -1.
q[indc] = 1.
q[indd] = -1.
f = prb.fields(q)
# out = survey.dpred(q[actind], f=f)

In [6]:
# v = np.ones(mesh.nC)[actind]
# jvec = prb.Jvec(q[actind], q[actind])
# jtvec = prb.Jtvec(q[actind], np.ones_like(out))

In [7]:
# mesh.plotSlice(actMap*jtvec, normal="Z", ind=5)

In [8]:
mesh.plotSlice(f[src, 'phi'], normal="Z", ind=19)


Out[8]:
(<matplotlib.collections.QuadMesh at 0x1153ee550>,)

In [9]:
# Utils.plot2Ddata(xyzN[:,:2], dobs, scale="linear")
# # plt.plot(mesh.gridCC[inda,0], mesh.gridCC[inda,1], 'ko')
# # plt.plot(mesh.gridCC[indb,0], mesh.gridCC[indb,1], 'ro')

In [10]:
# hout = hist(np.log10(abs(dobs)),bins=100)

In [11]:
# print abs(dobs).min()
# print abs(dobs).max()*0.01

In [12]:
dobs = survey.dpred(q)
dobs+= np.random.randn(survey.nD)*abs(dobs).max() * 0.05

In [13]:
depthweight = 1./ ((abs(mesh.gridCC[:,2])+2.5)**3.)
depthweight /= depthweight.max()

In [14]:
# Generate Full sensitivity
I = np.diag(np.ones_like(dobs))
J = np.zeros((dobs.size, mesh.nC))
for i in range(dobs.size):
    J[i,:] = prb.Jtvec(sigma, I[:,i])
    JtJ = (J**2).sum(axis=0)
JtJ /= JtJ.max()
prb.G = J

In [15]:
# out = mesh.plotSlice(JtJ, normal="Y", ind=12)
# plt.colorbar(out[0])

In [16]:
# plt.plot(1./dmisfit.Wd.diagonal())

In [ ]:


In [17]:
from SimPEG import (Mesh, Maps, Utils, DataMisfit, Regularization,
                    Optimization, Inversion, InvProblem, Directives)
survey.std = 0.
survey.eps = abs(dobs).max() * 0.05
survey.dobs = dobs
 
dmisfit = DataMisfit.l2_DataMisfit(survey)
regmap = Maps.IdentityMap(nP = mesh.nC)
reg = Regularization.Sparse(mesh, mapping=regmap)
reg.cell_weights = JtJ
reg.alpha_s = 1.
reg.alpha_x = 0.
reg.alpha_y = 0.
reg.alpha_z = 0.
opt = Optimization.ProjectedGNCG(maxIter=100, tolX=1e-20, tolF=1e-20)
opt.maxIterLS = 20
IRLS = Directives.Update_IRLS(norms=([0.,1.,1.,1.]),
                                     eps=None, f_min_change=1e-3,
                                     minGNiter=3)
senseweight = Directives.Update_Wj()
invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt)
target = Directives.TargetMisfit()
# Create an inversion object
beta = Directives.BetaSchedule(coolingFactor=5, coolingRate=3)
betaest = Directives.BetaEstimate_ByEig()
betaest.beta0_ratio = 2.
updateprecond = Directives.Update_lin_PreCond()
inv = Inversion.BaseInversion(invProb, directiveList=[betaest, updateprecond, IRLS])
# inv = Inversion.BaseInversion(invProb, directiveList=[betaest, beta, updateprecond, target])
# inv = Inversion.BaseInversion(invProb, directiveList=[betaest, IRLS])
prb.counter = opt.counter = Utils.Counter()
opt.LSshorten = 0.5
opt.remember('xc')
m0 = np.ones(mesh.nC)*0.
reg.mref = m0
mopt = inv.run(m0)


SimPEG.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv.
                    ***Done using same Solver and solverOpts as the problem***
model has any nan: 0
=============================== Projected GNCG ===============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment   
-----------------------------------------------------------------------------
x0 has any nan: 0
   0  5.51e+09  1.86e+04  0.00e+00  1.86e+04    1.01e+06      0              
   1  2.76e+09  2.01e+03  2.63e-07  2.73e+03    1.25e+05      0              
   2  1.38e+09  1.08e+03  4.53e-07  1.70e+03    6.22e+04      0   Skip BFGS  
   3  6.89e+08  6.47e+02  6.74e-07  1.11e+03    4.65e+04      0   Skip BFGS  
   4  3.45e+08  3.88e+02  9.45e-07  7.13e+02    3.66e+04      0   Skip BFGS  
   5  1.72e+08  2.25e+02  1.24e-06  4.39e+02    2.71e+04      0   Skip BFGS  
   6  8.62e+07  1.50e+02  1.53e-06  2.82e+02    2.22e+04      0   Skip BFGS  
Convergence with smooth l2-norm regularization: Start IRLS steps...
L[p qx qy qz]-norm : [0.0, 1.0, 1.0, 1.0]
eps_p: [0.0024545764933138511, 0.1, 0.1] eps_q: [0.00097234760370370154, 0.1, 0.1]
Regularization decrease: 0.000e+00
   7  9.13e+07  1.21e+02  1.53e-06  2.61e+02    1.84e+04      0              
   8  9.13e+07  1.11e+02  1.55e-06  2.52e+02    1.31e+04      0              
   9  9.13e+07  1.04e+02  1.56e-06  2.47e+02    1.17e+04      0              
Regularization decrease: 6.129e-02
  10  1.20e+08  9.77e+01  1.56e-06  2.85e+02    1.20e+04      0              
  11  1.20e+08  9.85e+01  1.50e-06  2.78e+02    1.21e+04      0              
  12  1.20e+08  9.78e+01  1.50e-06  2.78e+02    8.87e+03      0              
Regularization decrease: 5.395e-02
  13  1.59e+08  9.61e+01  1.50e-06  3.36e+02    1.52e+04      0              
  14  1.59e+08  1.00e+02  1.41e-06  3.25e+02    1.23e+04      0              
  15  1.59e+08  9.67e+01  1.38e-06  3.16e+02    1.18e+04      0              
Regularization decrease: 9.318e-02
  16  2.11e+08  9.66e+01  1.38e-06  3.87e+02    1.81e+04      0              
  17  2.11e+08  1.23e+02  1.17e-06  3.70e+02    1.70e+04      0              
  18  2.11e+08  1.10e+02  1.18e-06  3.60e+02    1.07e+04      0              
Regularization decrease: 1.499e-01
  19  2.48e+08  1.09e+02  1.18e-06  4.03e+02    1.80e+04      0              
  20  2.48e+08  1.29e+02  1.04e-06  3.86e+02    2.14e+04      0              
  21  2.48e+08  1.18e+02  1.01e-06  3.69e+02    1.38e+04      0              
Regularization decrease: 1.444e-01
  22  2.71e+08  1.17e+02  1.01e-06  3.92e+02    1.79e+04      0              
  23  2.71e+08  1.28e+02  8.90e-07  3.69e+02    2.08e+04      0              
  24  2.71e+08  1.16e+02  8.73e-07  3.53e+02    1.54e+04      0              
Regularization decrease: 1.714e-01
  25  2.86e+08  1.21e+02  8.73e-07  3.71e+02    1.61e+04      0              
  26  2.86e+08  1.26e+02  7.81e-07  3.49e+02    2.33e+04      0              
  27  2.86e+08  1.21e+02  7.42e-07  3.34e+02    1.45e+04      0              
Regularization decrease: 1.383e-01
  28  2.86e+08  1.22e+02  7.42e-07  3.35e+02    1.60e+04      0              
  29  2.86e+08  1.24e+02  6.90e-07  3.22e+02    1.70e+04      0              
  30  2.86e+08  1.21e+02  6.69e-07  3.13e+02    1.60e+04      0              
Regularization decrease: 9.891e-02
  31  2.86e+08  1.23e+02  6.69e-07  3.15e+02    1.61e+04      0              
  32  2.86e+08  1.22e+02  6.42e-07  3.06e+02    1.10e+04      0              
  33  2.86e+08  1.21e+02  6.27e-07  3.01e+02    1.37e+04      0              
Regularization decrease: 3.995e-02
Reach maximum number of IRLS cycles: 10
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 1.8630e-16
0 : |xc-x_last| = 1.2669e-01 <= tolX*(1+|x0|) = 1.0000e-20
0 : |proj(x-g)-x|    = 1.3684e+04 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.3684e+04 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =     100    <= iter          =     34
------------------------- DONE! -------------------------

In [18]:
plt.plot(dobs)
plt.plot(invProb.dpred)


Out[18]:
[<matplotlib.lines.Line2D at 0x1152793d0>]

In [19]:
# out = mesh.plotSlice(reg.l2model, normal="Y", ind=12, clim=(-0.01, 0.01))
# plt.colorbar(out[0])

In [20]:
out = mesh.plotSlice(mopt, normal="Y", ind=12, clim=(-0.01, 0.01))
plt.colorbar(out[0])


Out[20]:
<matplotlib.colorbar.Colorbar at 0x117ecd110>

In [21]:
mesh.plotSlice(q, normal="Y", ind=12, clim=(-0.01, 0.01))
plt.colorbar(out[0])


Out[21]:
<matplotlib.colorbar.Colorbar at 0x1194347d0>

In [22]:
# out = mesh.plotSlice(reg.l2model, normal="Z", ind=19, clim=(-0.01, 0.01))
# plt.colorbar(out[0])

In [23]:
out = mesh.plotSlice(mopt, normal="Z", ind=16, clim=(-0.01, 0.01))
plt.colorbar(out[0])


Out[23]:
<matplotlib.colorbar.Colorbar at 0x11acfb710>

In [24]:
out = mesh.plotSlice(q, normal="Z", ind=16)
plt.colorbar(out[0])


Out[24]:
<matplotlib.colorbar.Colorbar at 0x123babc10>

In [25]:
# hout = hist(abs(reg.l2model), bins=200)
# plt.xlim(0, 0.008)
# plt.xscale("linear")

In [26]:
hout = hist(abs(mopt), bins=200)
plt.xlim(0, 0.008)
plt.xscale("linear")



In [27]:
hout = hist(abs(mesh.cellGradx*mopt), bins=100)
plt.xscale("linear")
plt.xlim(0, 0.0001)


Out[27]:
(0, 0.0001)

In [28]:
Utils.plot2Ddata(xyzN[:,:2], survey.dobs, scale="linear")


Out[28]:
(<matplotlib.contour.QuadContourSet at 0x12535bcd0>,
 <matplotlib.axes._subplots.AxesSubplot at 0x125206450>)

In [29]:
Utils.plot2Ddata(xyzN[:,:2], invProb.dpred, scale="linear")


Out[29]:
(<matplotlib.contour.QuadContourSet at 0x125611310>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1253a7a10>)

In [ ]: