In [1]:
from SimPEG import *
import simpegDCIP as DC
from numpy.polynomial import polynomial

In [2]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['linalg']
`%matplotlib` prevents importing * from pylab and numpy

In [3]:
cs = 0.5
mesh = Mesh.TensorMesh([np.ones(100)*cs, np.ones(50)*cs], "CN")
x = mesh.vectorCCx

In [4]:
actx = (x>-15.)&(x<15.)

In [5]:
order = 3
Vobs = polynomial.polyvander(x, order)
dobs = 0.05*x-4
H = np.dot(Vobs.T, Vobs)
g = np.dot(Vobs.T, dobs)
mest = np.linalg.solve(H, g)

In [6]:
dpred = Vobs.dot(mest)

In [7]:
V = polynomial.polyvander(x, order)
m1D = Mesh.TensorMesh([V.shape[1]+2])
weight = (1./(V**2).sum(axis=0))**0.5
weight = weight / weight.max()
weightmap = Maps.Weighting(m1D, weights=np.r_[1., 1., weight])
m0_poly = np.r_[np.log(1e-3), np.log(1e-3), np.r_[-3., np.zeros(V.shape[1]-1)] / weight]
mtrue_poly = np.r_[np.log(1e-3), np.log(1e0), mest / weight]

In [8]:
polymap = Maps.PolyMap(mesh, V.shape[1]-1, logSigma=True, normal='Y')
mappingfwd = polymap*weightmap
mapping = Maps.ExpMap(mesh)

In [9]:
sigma = mappingfwd*mtrue_poly

In [10]:
xr = np.linspace(-15, 15, 20)
xz_A = Utils.ndgrid(xr, np.r_[-0.25])
xz_B = Utils.ndgrid(np.ones_like(xr)*22, np.r_[-0.25])
xz_M = Utils.ndgrid(xr, np.r_[-0.25])
xz_N = Utils.ndgrid(np.ones_like(xr)*-22, np.r_[-0.25])

ntx = xz_A.shape[0]
txList = []
for i in range(ntx):
    offset = abs(xz_A[i,0]-xz_M[:,0])
    actrx = offset > 5.
    rx = DC.RxDipole(xz_M[actrx,:], xz_N[actrx,:])
    src = DC.SrcDipole([rx], xz_A[i,:], xz_B[i,:])
    txList.append(src)

In [11]:
dat = mesh.plotImage(np.log10(sigma), clim=(-2, 0))
plt.colorbar(dat[0])
plot(xz_A[:,0], xz_A[:,1], 'w.')
plot(xz_B[:,0], xz_B[:,1], 'y.')
plot(xz_N[:,0], xz_N[:,1], 'r.')


Out[11]:
[<matplotlib.lines.Line2D at 0x1078d47d0>]
/Users/sgkang/anaconda/lib/python2.7/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  if self._edgecolors == str('face'):

In [12]:
dat = mesh.plotImage(np.log10(mappingfwd*m0_poly), clim=(-2, 0))
plt.colorbar(dat[0])
plot(xz_A[:,0], xz_A[:,1], 'w.')
plot(xz_B[:,0], xz_B[:,1], 'y.')
plot(xz_N[:,0], xz_N[:,1], 'r.')


Out[12]:
[<matplotlib.lines.Line2D at 0x107a11410>]

In [13]:
from SimPEG import SolverLU

In [14]:
mtrue = np.log(sigma)
m0 = np.log(np.ones(mesh.nC)*1e-3)
survey = DC.SurveyDC(txList)
problem = DC.ProblemDC_CC(mesh, mapping = mapping)
problem.pair(survey)
problem.Solver = SolverLU
dtrue  = survey.dpred(mtrue)
d0  = survey.dpred(m0)
j = problem.fields
plot(dtrue)
plot(d0)


Out[14]:
[<matplotlib.lines.Line2D at 0x10797cc90>]

In [15]:
abs(dtrue).min()


Out[15]:
0.0046885259085681129

In [16]:
hist(np.log10(abs(dtrue)), bins = 20)


Out[16]:
(array([  1.,   2.,   3.,   4.,   5.,   4.,   6.,   7.,   8.,   9.,   8.,
         10.,  17.,  17.,  18.,  33.,  41.,  30.,  32.,  17.]),
 array([-2.32896368, -2.11250236, -1.89604103, -1.67957971, -1.46311839,
        -1.24665706, -1.03019574, -0.81373441, -0.59727309, -0.38081177,
        -0.16435044,  0.05211088,  0.2685722 ,  0.48503353,  0.70149485,
         0.91795617,  1.1344175 ,  1.35087882,  1.56734014,  1.78380147,
         2.00026279]),
 <a list of 20 Patch objects>)

In [17]:
noise = 0.05*abs(dtrue)*np.random.randn(dtrue.shape[0])

In [18]:
survey.dobs = dtrue +noise
dmis = DataMisfit.l2_DataMisfit(survey)
dmis.Wd = 1./(0.05*abs(dtrue)+ 0.1)
reg = Regularization.Tikhonov(mesh)
opt = Optimization.InexactGaussNewton(maxIter=30,maxIterLS=20)
opt.remember('xc')
invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
betaSched = Directives.BetaSchedule(coolingFactor=8, coolingRate=3)
targetmis = Directives.TargetMisfit()
savemodel = Directives.SaveModelEveryIteration()
inv = Inversion.BaseInversion(invProb, directiveList=[betaSched,targetmis])
reg.alpha_s = 1e-5
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***
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment   
-----------------------------------------------------------------------------
   0  1.00e+00  1.40e+08  0.00e+00  1.40e+08    8.32e+06      0              
   1  1.00e+00  1.88e+07  6.18e-03  1.88e+07    1.13e+06      0              
   2  1.00e+00  2.52e+06  2.45e-02  2.52e+06    1.55e+05      0   Skip BFGS  
   3  1.25e-01  3.50e+05  5.43e-02  3.50e+05    2.18e+04      0   Skip BFGS  
   4  1.25e-01  6.61e+04  9.31e-02  6.61e+04    3.26e+03      0   Skip BFGS  
   5  1.25e-01  1.57e+04  7.18e+00  1.57e+04    4.85e+02      1   Skip BFGS  
   6  1.56e-02  4.44e+03  1.84e+01  4.44e+03    5.43e+02      1              
   7  1.56e-02  2.74e+03  4.09e+01  2.74e+03    2.92e+03      0   Skip BFGS  
   8  1.56e-02  5.47e+02  3.77e+01  5.48e+02    2.94e+02      0              
   9  1.95e-03  3.27e+02  3.71e+01  3.27e+02    2.70e+02      0   Skip BFGS  
  10  1.95e-03  2.42e+02  3.97e+01  2.42e+02    8.87e+01      0              
  11  1.95e-03  1.96e+02  3.88e+01  1.96e+02    1.22e+02      0              
  12  2.44e-04  1.56e+02  4.13e+01  1.56e+02    9.85e+01      0              
  13  2.44e-04  1.37e+02  4.19e+01  1.37e+02    1.23e+02      0              
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 1.3959e+07
1 : |xc-x_last| = 7.6786e-01 <= tolX*(1+|x0|) = 4.8945e+01
0 : |proj(x-g)-x|    = 1.2300e+02 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.2300e+02 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      30    <= iter          =     14
------------------------- DONE! -------------------------

In [19]:
XC = opt.recall('xc')

In [20]:
from ipywidgets import interact, IntSlider
def viewInv(iteration):
    fig  = plt.figure(num=0,figsize = (10,5))
    ax = plt.subplot(111)
    dat = mesh.plotImage(np.log10(mapping*XC[iteration]), grid=True, ax=ax, clim=(-3, 0), gridOpts={'alpha':0.2}, pcolorOpts={'cmap':cm.RdPu})
#     ax.set_xlim(mesh.vectorNx.min(), mesh.vectorNx.max())
#     ax.set_ylim(mesh.vectorNy.min(), mesh.vectorNy.max())
    ax.plot(mesh.vectorCCx, dpred, 'r--')
    ax.plot(xz_A[:,0], xz_A[:,1], 'k.')
    ax.plot(xz_B[:,0], xz_B[:,1], 'r.')
    ax.plot(xz_N[:,0], xz_N[:,1], 'b.')    
#     plt.colorbar(dat[0], ax=ax)
#     ax.set_ylim (-15, 0.)
#     ax.set_xlim (-15, 15.)
    plt.show()
    return True

In [21]:
interact(viewInv, iteration = IntSlider(min=0, max=opt.iter-1,step=1, value=0))


True

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


Out[22]:
[<matplotlib.lines.Line2D at 0x10d44d590>]

In [ ]: