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 0x11546d2d0>,)

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.46e+08  1.86e+04  0.00e+00  1.86e+04    1.01e+06      0              
   1  2.73e+08  3.75e+03  4.29e-06  4.93e+03    2.10e+05      0              
   2  1.36e+08  1.71e+03  6.62e-06  2.62e+03    8.71e+04      0   Skip BFGS  
   3  6.82e+07  8.79e+02  9.44e-06  1.52e+03    5.92e+04      0   Skip BFGS  
   4  3.41e+07  4.61e+02  1.29e-05  9.00e+02    3.64e+04      0   Skip BFGS  
   5  1.71e+07  2.46e+02  1.66e-05  5.29e+02    2.69e+04      0   Skip BFGS  
   6  8.53e+06  1.32e+02  2.08e-05  3.09e+02    1.57e+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.00125289177341 eps_q: 0.00045211455046
Regularization decrease: 0.000e+00
   7  1.39e+07  7.85e+01  2.08e-05  3.68e+02    2.91e+04      0   Skip BFGS  
   8  1.39e+07  1.14e+02  1.48e-05  3.20e+02    1.15e+04      0              
   9  1.39e+07  8.36e+01  1.57e-05  3.03e+02    1.13e+04      0              
Regularization decrease: 4.393e-01
  10  1.83e+07  9.76e+01  1.57e-05  3.85e+02    1.73e+04      0              
  11  1.83e+07  1.21e+02  1.31e-05  3.59e+02    1.38e+04      0              
  12  1.83e+07  1.01e+02  1.36e-05  3.48e+02    1.00e+04      0              
Regularization decrease: 9.627e-02
  13  2.11e+07  1.10e+02  1.36e-05  3.97e+02    1.40e+04      0              
  14  2.11e+07  1.22e+02  1.20e-05  3.76e+02    1.28e+04      0              
  15  2.11e+07  1.10e+02  1.21e-05  3.67e+02    7.87e+03      0              
Regularization decrease: 7.210e-02
  16  2.43e+07  1.12e+02  1.21e-05  4.06e+02    1.35e+04      0              
  17  2.43e+07  1.25e+02  1.11e-05  3.95e+02    1.00e+04      0              
  18  2.43e+07  1.17e+02  1.11e-05  3.87e+02    9.24e+03      0              
Regularization decrease: 7.726e-02
  19  2.58e+07  1.21e+02  1.11e-05  4.07e+02    9.14e+03      0              
  20  2.58e+07  1.25e+02  1.05e-05  3.96e+02    1.03e+04      0              
  21  2.58e+07  1.23e+02  1.04e-05  3.92e+02    6.99e+03      0              
Regularization decrease: 4.738e-02
  22  2.73e+07  1.21e+02  1.04e-05  4.05e+02    1.01e+04      0              
  23  2.73e+07  1.27e+02  9.92e-06  3.97e+02    8.60e+03      0              
  24  2.73e+07  1.22e+02  9.89e-06  3.92e+02    7.44e+03      0              
Regularization decrease: 5.863e-02
  25  2.73e+07  1.24e+02  9.89e-06  3.94e+02    7.85e+03      0              
  26  2.73e+07  1.22e+02  9.69e-06  3.87e+02    8.70e+03      0              
  27  2.73e+07  1.23e+02  9.52e-06  3.83e+02    6.59e+03      0              
Regularization decrease: 2.079e-02
  28  2.92e+07  1.20e+02  9.52e-06  3.97e+02    8.37e+03      0              
  29  2.92e+07  1.27e+02  9.10e-06  3.92e+02    9.24e+03      0              
  30  2.92e+07  1.22e+02  9.12e-06  3.88e+02    7.01e+03      0              
Regularization decrease: 6.216e-02
  31  2.92e+07  1.24e+02  9.12e-06  3.90e+02    8.54e+03      0              
  32  2.92e+07  1.23e+02  9.00e-06  3.86e+02    7.84e+03      0              
  33  2.92e+07  1.25e+02  8.80e-06  3.82e+02    7.05e+03      0              
Regularization decrease: 1.197e-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.6241e-03 <= tolX*(1+|x0|) = 1.0000e-20
0 : |proj(x-g)-x|    = 7.0537e+03 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 7.0537e+03 <= 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 0x1154dc490>]

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 0x117f44d10>

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


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

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=19, clim=(-0.01, 0.01))
plt.colorbar(out[0])


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

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


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

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 0x126117d10>,
 <matplotlib.axes._subplots.AxesSubplot at 0x125fb9d50>)

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


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

In [ ]: