In [1]:
from SimPEG import Utils, Maps, Mesh
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
# temp = np.loadtxt("../data/pohang/dem.dat")
# inds = np.logical_and(temp[:,0]>230000, temp[:,0]<233000) & np.logical_and(temp[:,1]>286500, temp[:,1]<289500)
# topo = temp[inds,:]
# np.save("../data/pohang/topo", topo)

In [3]:
topo = np.load("../data/pohang/topo.npy")

In [4]:
# temp = np.loadtxt("../data/pohang/SP_cor_allf.dat")
fid = open("../data/pohang/finalSPfield.dat")
lines = fid.readlines()
temp = []
temp_skip = []
for line in lines:
    if len(line.split()) == 4:
        temp.append(map(float, line.split()[1:]))
    else:
        print "skip data"
#         temp_skip.append(map(float, line.split()[1:]))
        print line
data = np.vstack(temp)        
# "refe1"	231829.19	287921.942
# "refe2"	231785.361	287857.776


skip data
No	X	Y	

skip data
bb39	230920.919	287935.966	


In [5]:
plt.plot(topo[:,2]-topo[:,2].min())


Out[5]:
[<matplotlib.lines.Line2D at 0x109075f10>]

In [6]:
zmax = (topo[:,2]-topo[:,2].min()).max()

In [7]:
actdatinds = (data[:,0] < 233000) & (data[:,0] > 230281) & (data[:,1] > 286500) & (data[:,1] < 289500) & (data[:,2] < 40.)
xmax, xmin = data[actdatinds,0].max(), data[actdatinds,0].min()
ymax, ymin = data[actdatinds,1].max(), data[actdatinds,1].min()
xmed, ymed = 0.5*(xmax-xmin) + xmin, 0.5*(ymax-ymin) + ymin
xref, yref = 231829.19, 287921.942
xref_Inv, yref_Inv = 231829.19-xmed, 287921.942-ymed
topoInv = np.c_[topo[:,0]-xmed, topo[:,1]-ymed, topo[:,2]-topo[:,2].min()]
dataInv = np.c_[data[actdatinds,0]-xmed, data[actdatinds,1]-ymed, data[actdatinds,2]]
dataInv = np.vstack((dataInv, np.r_[xref_Inv, yref_Inv, 0.]))

In [8]:
Utils.plot2Ddata(topo[:,:2], topo[:,2], ncontour=100, contourOpts={"cmap":"viridis"})
plt.plot(data[actdatinds,0], data[actdatinds,1], '.')
plt.plot(xref, yref, 'ro')


Out[8]:
[<matplotlib.lines.Line2D at 0x1091c5a10>]

In [9]:
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
Utils.plot2Ddata(topoInv[:,:2], topoInv[:,2], ncontour=50, ax=ax, contourOpts={"cmap":"binary_r", "alpha":0.2})
Utils.plot2Ddata(dataInv[:,:2], dataInv[:,2], ncontour=100, contourOpts={"cmap":"jet", "alpha":0.6}, dataloc=True, ax=ax)


Out[9]:
(<matplotlib.contour.QuadContourSet at 0x10bdf7310>,
 <matplotlib.axes._subplots.AxesSubplot at 0x1093a2510>)

In [10]:
plt.plot(dataInv[:,2])


Out[10]:
[<matplotlib.lines.Line2D at 0x10d24ca50>]

In [11]:
dx = 50.
dz = 20.
npad = 5
hxind = [(dx, npad, -1.3), (dx, 60), (dx, npad, 1.3)]
hyind = [(dx, npad, -1.3), (dx, 60), (dx, npad, 1.3)]
hzind = [(dz, npad, -1.3), (dz, 10), (dz*0.8, 16)]
mesh = Mesh.TensorMesh([hxind, hyind, hzind], "CCN")
mesh._x0 = np.r_[mesh.x0[0], mesh.x0[1], mesh.x0[2]+zmax]

In [12]:
mesh.hz[:3].sum()


Out[12]:
175.32060000000001

In [13]:
from SimPEG.EM.Static.Utils import StaticUtils

In [14]:
print mesh


  ---- 3-D TensorMesh ----  
   x0: -2087.80
   y0: -2087.80
   z0: -605.12
  nCx: 70
  nCy: 70
  nCz: 31
   hx: 185.65,  142.81,  109.85,  84.50,  65.00,  60*50.00,  65.00,  84.50,  109.85,  142.81,  185.65,
   hy: 185.65,  142.81,  109.85,  84.50,  65.00,  60*50.00,  65.00,  84.50,  109.85,  142.81,  185.65,
   hz: 74.26,  57.12,  43.94,  33.80,  26.00,  10*20.00,  16*16.00,

In [15]:
from scipy.sparse import block_diag

In [16]:
actind = Utils.surface2ind_topo(mesh, topoInv, gridLoc="N")
actMap = Maps.InjectActiveCells(mesh, actind, 0.)
mesh2D, topoCC = StaticUtils.gettopoCC(mesh, ~actind)

In [17]:
mesh2D.plotImage(topoCC)


Out[17]:
(<matplotlib.collections.QuadMesh at 0x10ba92d10>,)

In [19]:
xyzlocInv = StaticUtils.drapeTopotoLoc(mesh, topoInv, dataInv[:,:2], actind=actind)
xyzlocInv = np.c_[xyzlocInv[:,:2], xyzlocInv[:,2]]

In [20]:
sigma = np.ones(mesh.nC)*1e-3
sigma[~actind] = 1e-8

In [21]:
indy = 40
indstemp = abs(xyzlocInv[:,1]-mesh.vectorCCy[indy]) < 10.
mesh.plotSlice(actind, normal="Z", ind=27, grid=True)
plt.plot(dataInv[:,0], dataInv[:,1], 'w.')
plt.plot(dataInv[indstemp,0], dataInv[indstemp,1], 'ro')

mesh.plotSlice(sigma, normal="Y", grid=True, ind = indy)
plt.plot(xyzlocInv[indstemp,0], xyzlocInv[indstemp,2], 'w.')


Out[21]:
[<matplotlib.lines.Line2D at 0x10d3da690>]

In [22]:
mesh.plotSlice(actind, normal="Z", ind=20, grid=True)
plt.plot(dataInv[:,0], dataInv[:,1], 'w.')
mesh.plotSlice(actind, normal="Y", grid=True)
plt.plot(dataInv[:,0], dataInv[:,2], 'w.')


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

In [23]:
zlocCC = Utils.mkvc(topoCC.reshape([-1,1]).repeat(mesh.nCz, axis=1))
depthweight = 1./ ((abs(mesh.gridCC[:,2] - zlocCC)+2.5)**1.)
depthweight /= depthweight.max()
depthweight[~actind] = np.nan
zlocCC[~actind] = np.nan

In [24]:
mesh.plotSlice(np.log10(depthweight), normal="Y", grid=True, clim=(-3, 0), ind=26)


Out[24]:
(<matplotlib.collections.QuadMesh at 0x10e966910>,
 <matplotlib.lines.Line2D at 0x10e966e50>)

In [25]:
import simpegSP as SP
from pymatsolver import PardisoSolver
from SimPEG.EM.Static.SIP.Regularization import MultiRegularization

xyzM = xyzlocInv[:-1,:]
xyzN = np.atleast_2d(xyzlocInv[-1,:]).repeat(xyzM.shape[0], axis=0)
wires = Maps.Wires(('jsx', actMap.nP), ('jsy', actMap.nP), ('jsz', actMap.nP))
prb = SP.Problem_CC(mesh, sigma=sigma, jsxMap=actMap*wires.jsx, jsyMap=actMap*wires.jsy, jszMap=actMap*wires.jsz, Solver=PardisoSolver)
rx = SP.Rx.Dipole(xyzN, xyzM)
src = SP.Src.StreamingCurrents([rx], L=np.ones(mesh.nC)*1e-6, mesh=mesh, modelType="CurrentDensity")
survey = SP.Survey([src])
survey.pair(prb)

In [26]:
survey.dobs = dataInv[:-1,2]

In [29]:
from SimPEG import (Mesh, Maps, Utils, DataMisfit, Regularization,
                    Optimization, Inversion, InvProblem, Directives)
survey.std = 0.
survey.eps = abs(survey.dobs).max() * 0.05

dmisfit = DataMisfit.l2_DataMisfit(survey)
regmap = Maps.IdentityMap(nP = actMap.nP*3)
reg = MultiRegularization(mesh, mapping=regmap ,nModels=3, indActive=actind)
reg.cell_weights = depthweight[actind] 
reg.alpha_s = 1.
reg.alpha_x = 1.
reg.alpha_y = 1.
reg.alpha_z = 1.
reg.ratios = [1., 1., 0.1]
opt = Optimization.InexactGaussNewton(maxIter=15, tolX=1e-20, tolF=1e-20)
opt.maxIterCG = 3
opt.maxIterLS = 30
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.
invProb.beta = 1e0
inv = Inversion.BaseInversion(invProb, directiveList=[beta, target])
prb.counter = opt.counter = Utils.Counter()
opt.LSshorten = 0.5
opt.remember('xc')
m0 = np.ones(actMap.nP*3)*0.
reg.mref = m0
opt.maxIterCG = 10
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
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment   
-----------------------------------------------------------------------------
x0 has any nan: 0
   0  1.00e+00  1.19e+04  0.00e+00  1.19e+04    2.85e+04      0              
   1  1.00e+00  4.38e+03  1.30e+02  4.51e+03    3.53e+03      0              
   2  1.00e+00  3.34e+03  5.32e+02  3.87e+03    1.55e+03      0              
   3  2.00e-01  3.21e+03  6.12e+02  3.33e+03    2.18e+03      0   Skip BFGS  
   4  2.00e-01  2.54e+03  2.19e+03  2.97e+03    3.17e+03      0              
   5  2.00e-01  2.49e+03  2.12e+03  2.91e+03    1.97e+03      0              
   6  4.00e-02  2.31e+03  2.73e+03  2.42e+03    2.41e+03      0              
   7  4.00e-02  1.84e+03  7.81e+03  2.15e+03    3.31e+03      0              
   8  4.00e-02  1.70e+03  9.19e+03  2.07e+03    2.85e+03      1              
   9  8.00e-03  1.60e+03  1.14e+04  1.69e+03    3.00e+03      0              
  10  8.00e-03  1.41e+03  1.73e+04  1.55e+03    3.94e+03      0              
  11  8.00e-03  1.31e+03  1.89e+04  1.46e+03    3.21e+03      0              
  12  1.60e-03  1.18e+03  2.38e+04  1.22e+03    1.83e+03      0              
  13  1.60e-03  1.04e+03  3.16e+04  1.09e+03    1.56e+03      1              
  14  1.60e-03  9.46e+02  3.93e+04  1.01e+03    2.04e+03      1   Skip BFGS  
  15  3.20e-04  9.15e+02  4.20e+04  9.29e+02    1.54e+03      2              
------------------------- STOP! -------------------------
0 : |fc-fOld| = 8.0086e+01 <= tolF*(1+|f0|) = 1.1880e-16
0 : |xc-x_last| = 6.6167e-01 <= tolX*(1+|x0|) = 1.0000e-20
0 : |proj(x-g)-x|    = 1.5413e+03 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.5413e+03 <= 1e3*eps       = 1.0000e-02
1 : maxIter   =      15    <= iter          =     15
------------------------- DONE! -------------------------

In [30]:
target.target


Out[30]:
172.0

In [31]:
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
vmin, vmax = invProb.dpred.min(), invProb.dpred.max()*1.3
Utils.plot2Ddata(xyzM[:,:2], invProb.dpred, ncontour=100, contourOpts={"cmap":"jet", "alpha":0.6, "vmin":vmin, "vmax":vmax}, dataloc=True, ax=ax)


Out[31]:
(<matplotlib.contour.QuadContourSet at 0x10b8a9310>,
 <matplotlib.axes._subplots.AxesSubplot at 0x11b43e7d0>)

In [32]:
xc = opt.recall("xc")

In [44]:
dpred10 = survey.dpred(xc[10])

In [45]:
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
vmin, vmax = dpred10.min(), dpred10.max()*1.3
Utils.plot2Ddata(xyzM[:,:2], dpred10, ncontour=100, contourOpts={"cmap":"jet", "alpha":0.6, "vmin":vmin, "vmax":vmax}, dataloc=True, ax=ax)


Out[45]:
(<matplotlib.contour.QuadContourSet at 0x1228cbb90>,
 <matplotlib.axes._subplots.AxesSubplot at 0x130b51250>)

In [35]:
fig = plt.figure(figsize = (5,5))
ax = plt.subplot(111)
Utils.plot2Ddata(xyzM[:,:2], survey.dobs, ncontour=100, contourOpts={"cmap":"jet", "alpha":0.6, "vmin":vmin, "vmax":vmax}, dataloc=True, ax=ax)


Out[35]:
(<matplotlib.contour.QuadContourSet at 0x128060350>,
 <matplotlib.axes._subplots.AxesSubplot at 0x125ce1990>)

In [36]:
plt.plot(dpred10)
plt.plot(survey.dobs)


Out[36]:
[<matplotlib.lines.Line2D at 0x11b43e790>]

In [46]:
iteration = 10
xc = opt.recall("xc")
temp = xc[iteration].reshape((actMap.nP, 3), order="F")
tempx = actMap * temp[:,0]
tempy = actMap * temp[:,1]
tempz = actMap * temp[:,2]
temp_amp = np.sqrt(temp[:,0]**2 + temp[:,1]**2 + temp[:,2]**2)
tempx[~actind] = np.nan
tempy[~actind] = np.nan
tempz[~actind] = np.nan
temp = np.c_[tempx, tempy, tempz]

In [47]:
temp_amp.min()
temp_amp.max()


Out[47]:
0.49726580531277359

In [52]:
actMap_plot = Maps.InjectActiveCells(mesh, actind, np.nan)

In [53]:
indz, indy = 17, 31
mesh.plotSlice((actMap_plot*temp_amp), ind = indz, clim=(0, temp_amp.max()), normal="Z")
plt.ylim(-1500, 1500)
plt.xlim(-1500, 1500)
plt.gca().set_aspect("equal")



In [54]:
mesh.plotSlice((actMap_plot*temp_amp), clim=(0, temp_amp.max()), normal="Y", ind=indy)
plt.xlim(-1500, 1500)
plt.ylim(-300, 90)
plt.gca().set_aspect(2)
print mesh.vectorCCz[indz]
print mesh.vectorCCy[indy]


-130.0
-175.0

In [59]:
mesh.plotSlice(temp, clim=(0, temp_amp.max()), normal="Y", vType='CCv', view='vec', ind=32, streamOpts={"color":'w'})
print mesh.vectorCCz[indz]
plt.xlim(-1500, 1500)
plt.ylim(-300, 90)
plt.gca().set_aspect(2)


-130.0

In [60]:
mesh.plotSlice(tempx, normal="X", grid=True, ind=19)


Out[60]:
(<matplotlib.collections.QuadMesh at 0x140c52050>,
 <matplotlib.lines.Line2D at 0x140c52590>)

In [61]:
mesh.writeVTK("pohang1108", models={"sigma":sigma, "amp":actMap_plot*temp_amp, "j":np.c_[tempx, tempy, tempz]})
# np.savetxt("topoxyz.csv", topoInv, delimiter=",", header="xcoord, ycoord, zcoord")
# np.savetxt("dataxyz.csv", out, delimiter=",", header="xcoord, ycoord, zcoord")

In [62]:
actMap_plot*temp_amp


Out[62]:
array([ 0.00335093,  0.00256119,  0.001272  , ...,         nan,
               nan,         nan])

In [63]:
temp_amp.min()


Out[63]:
4.2358441927361655e-07

In [64]:
temp_amp.max()


Out[64]:
0.49726580531277359