Files
simpeg/notebooks/2DinvDC.ipynb
T
D Fournier 015099e72e Create import functions for UBCDC2D model and mesh.
Start driver function for forward model data from example.
2015-11-13 12:47:53 -08:00

126 KiB

In [1]:
from SimPEG import *
import simpegDCIP as DC
from numpy.polynomial import polynomial
Efficiency Warning: Interpolation will be slow, use setup.py!

            python setup.py build_ext --inplace
    
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 0x15be0278>]
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 0x15e72518>]
In [14]:
from SimPEG import SolverLU
In [15]:
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 [15]:
[<matplotlib.lines.Line2D at 0x160b5eb8>]
In [16]:
abs(dtrue).min()
Out [16]:
0.0046885259087758868
In [17]:
hist(np.log10(abs(dtrue)), bins = 20)
Out [17]:
(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 [18]:
noise = 0.05*abs(dtrue)*np.random.randn(dtrue.shape[0])
In [19]:
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 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.49e+05  5.43e-02  3.49e+05    2.17e+04      0   Skip BFGS  
   4  1.25e-01  6.57e+04  9.31e-02  6.57e+04    3.25e+03      0   Skip BFGS  
   5  1.25e-01  1.53e+04  7.19e+00  1.53e+04    4.83e+02      1   Skip BFGS  
   6  1.56e-02  4.40e+03  1.81e+01  4.40e+03    5.23e+02      1              
   7  1.56e-02  2.56e+03  4.02e+01  2.56e+03    2.82e+03      0   Skip BFGS  
   8  1.56e-02  5.47e+02  3.69e+01  5.47e+02    2.89e+02      0              
   9  1.95e-03  3.14e+02  3.71e+01  3.14e+02    3.22e+02      0   Skip BFGS  
  10  1.95e-03  2.18e+02  3.92e+01  2.18e+02    1.07e+02      0              
  11  1.95e-03  1.77e+02  3.86e+01  1.77e+02    8.57e+01      0              
  12  2.44e-04  1.44e+02  4.09e+01  1.44e+02    9.30e+01      0              
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 1.3960e+07
1 : |xc-x_last| = 3.1882e+00 <= tolX*(1+|x0|) = 4.8945e+01
0 : |proj(x-g)-x|    = 9.3043e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 9.3043e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      30    <= iter          =     13
------------------------- DONE! -------------------------
In [20]:
XC = opt.recall('xc')
In [21]:
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 [22]:
interact(viewInv, iteration = IntSlider(min=0, max=opt.iter-1,step=1, value=0))
True
In [23]:
plt.plot(survey.dobs)
plt.plot(invProb.dpred)
Out [23]:
[<matplotlib.lines.Line2D at 0x1ba516d8>]
In [ ]: