# 3D DC inversion of Dipole Dipole arrayΒΆ

This is an example for 3D DC Inversion. The model consists of 2 spheres, one conductive, the other one resistive compared to the background.

We restrain the inversion to the Core Mesh through the use an Active Cells mapping that we combine with an exponetial mapping to invert in log conductivity space. Here mapping, $$\mathcal{M}$$, indicates transformation of our model to a different space:

$\sigma = \mathcal{M}(\mathbf{m})$

Following example will show you how user can implement a 3D DC inversion.

from SimPEG import (
Mesh, Maps, Utils,
DataMisfit, Regularization, Optimization,
InvProblem, Directives, Inversion
)
from SimPEG.EM.Static import DC, Utils as DCUtils
import numpy as np
import matplotlib.pyplot as plt
try:
from pymatsolver import Pardiso as Solver
except ImportError:
from SimPEG import SolverLU as Solver

np.random.seed(12345)

# 3D Mesh
#########

# Cell sizes
csx, csy, csz = 1., 1., 0.5
# Number of core cells in each direction
ncx, ncy, ncz = 41, 31, 21
# Vectors of cell lengths in each direction with padding
hz = [(csz, npad, -1.5), (csz, ncz)]
# Create mesh and center it
mesh = Mesh.TensorMesh([hx, hy, hz], x0="CCN")

# 2-spheres Model Creation

# Spheres parameters
x0, y0, z0, r0 = -6., 0., -3.5, 3.
x1, y1, z1, r1 = 6., 0., -3.5, 3.

# ln conductivity
ln_sigback = -5.
ln_sigc = -3.
ln_sigr = -6.

# Define model
# Background
mtrue = ln_sigback * np.ones(mesh.nC)

# Conductive sphere
csph = (np.sqrt((mesh.gridCC[:, 0] - x0)**2. + (mesh.gridCC[:, 1] - y0)**2. +
(mesh.gridCC[:, 2] - z0)**2.)) < r0
mtrue[csph] = ln_sigc * np.ones_like(mtrue[csph])

# Resistive Sphere
rsph = (np.sqrt((mesh.gridCC[:, 0] - x1)**2. + (mesh.gridCC[:, 1] - y1)**2. +
(mesh.gridCC[:, 2] - z1)**2.)) < r1
mtrue[rsph] = ln_sigr * np.ones_like(mtrue[rsph])

# Extract Core Mesh
xmin, xmax = -20., 20.
ymin, ymax = -15., 15.
zmin, zmax = -10., 0.
xyzlim = np.r_[[[xmin, xmax], [ymin, ymax], [zmin, zmax]]]
actind, meshCore = Utils.meshutils.ExtractCoreMesh(xyzlim, mesh)

# Function to plot cylinder border
def getCylinderPoints(xc, zc, r):
xLocOrig1 = np.arange(-r, r + r / 10., r / 10.)
xLocOrig2 = np.arange(r, -r - r / 10., -r / 10.)
# Top half of cylinder
zLoc1 = np.sqrt(-xLocOrig1**2. + r**2.) + zc
# Bottom half of cylinder
zLoc2 = -np.sqrt(-xLocOrig2**2. + r**2.) + zc
# Shift from x = 0 to xc
xLoc1 = xLocOrig1 + xc * np.ones_like(xLocOrig1)
xLoc2 = xLocOrig2 + xc * np.ones_like(xLocOrig2)

topHalf = np.vstack([xLoc1, zLoc1]).T
topHalf = topHalf[0:-1, :]
bottomHalf = np.vstack([xLoc2, zLoc2]).T
bottomHalf = bottomHalf[0:-1, :]

cylinderPoints = np.vstack([topHalf, bottomHalf])
cylinderPoints = np.vstack([cylinderPoints, topHalf[0, :]])
return cylinderPoints

# Setup a synthetic Dipole-Dipole Survey
# Line 1
xmin, xmax = -15., 15.
ymin, ymax = 0., 0.
zmin, zmax = 0, 0
endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
survey1 = DCUtils.gen_DCIPsurvey(endl, "dipole-dipole", dim=mesh.dim,
a=3, b=3, n=8)

# Line 2
xmin, xmax = -15., 15.
ymin, ymax = 5., 5.
zmin, zmax = 0, 0
endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
survey2 = DCUtils.gen_DCIPsurvey(endl, "dipole-dipole", dim=mesh.dim,
a=3, b=3, n=8)

# Line 3
xmin, xmax = -15., 15.
ymin, ymax = -5., -5.
zmin, zmax = 0, 0
endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
survey3 = DCUtils.gen_DCIPsurvey(endl, "dipole-dipole", dim=mesh.dim,
a=3, b=3, n=8)

# Concatenate lines
survey = DC.Survey(survey1.srcList + survey2.srcList + survey3.srcList)

# Setup Problem with exponential mapping and Active cells only in the core mesh
expmap = Maps.ExpMap(mesh)
mapactive = Maps.InjectActiveCells(mesh=mesh, indActive=actind,
valInactive=-5.)
mapping = expmap * mapactive
problem = DC.Problem3D_CC(mesh, sigmaMap=mapping)
problem.pair(survey)
problem.Solver = Solver

survey.dpred(mtrue[actind])
survey.makeSyntheticData(mtrue[actind], std=0.05, force=True)

# Tikhonov Inversion


Out:

SimPEG.Survey assigned new std of 5.00%

array([-2.51410791, -0.29576192, -0.26296652, -0.14435083, -0.08530996,
-0.05807052, -0.03079626, -0.02072721, -0.99841015, -0.48587714,
-0.23637571, -0.16404824, -0.09549052, -0.04530608, -0.02801386,
-0.98901092, -0.32287323, -0.15187638, -0.07930286, -0.03920691,
-0.02463379, -2.38653015, -0.50575349, -0.28168043, -0.09926345,
-0.05109834, -2.89979132, -0.8796768 , -0.2699441 , -0.11906281,
-3.90861604, -0.73192822, -0.28597353, -4.10510359, -0.89097248,
-2.75933569, -2.88158097, -0.5909837 , -0.23757963, -0.11530429,
-0.07968417, -0.04686213, -0.03198705, -0.02077632, -2.77367342,
-0.5673827 , -0.22115663, -0.12176635, -0.06906947, -0.03974126,
-0.02715362, -2.56958624, -0.59259651, -0.21972704, -0.11061309,
-0.06856099, -0.04200801, -2.69908326, -0.62803486, -0.27636241,
-0.12496709, -0.07080875, -2.72563769, -0.67557772, -0.2698489 ,
-0.14279963, -2.78317596, -0.78752462, -0.28888659, -2.94340233,
-0.73749625, -2.5682623 , -2.61198904, -0.57403395, -0.21428745,
-0.12020541, -0.07349336, -0.04853628, -0.03077572, -0.01959376,
-2.59684872, -0.60894476, -0.21621185, -0.11479912, -0.06668348,
-0.03872881, -0.02860789, -2.49102646, -0.59614157, -0.22079585,
-0.11736015, -0.05843798, -0.03921221, -2.43137291, -0.65182089,
-0.25688942, -0.12982847, -0.07342205, -2.65254952, -0.69366576,
-0.30393578, -0.14011937, -2.78445066, -0.73896767, -0.28651008,
-2.77943003, -0.65355712, -2.58097705])

# Initial Model
m0 = np.median(ln_sigback) * np.ones(mapping.nP)
# Data Misfit
dmis = DataMisfit.l2_DataMisfit(survey)
# Regularization
regT = Regularization.Simple(mesh, indActive=actind, alpha_s=1e-6,
alpha_x=1., alpha_y=1., alpha_z=1.)

# Optimization Scheme
opt = Optimization.InexactGaussNewton(maxIter=10)

# Form the problem
opt.remember('xc')
invProb = InvProblem.BaseInvProblem(dmis, regT, opt)

# Directives for Inversions
beta = Directives.BetaEstimate_ByEig(beta0_ratio=1e+1)
Target = Directives.TargetMisfit()
betaSched = Directives.BetaSchedule(coolingFactor=5., coolingRate=2)

inv = Inversion.BaseInversion(invProb, directiveList=[beta, Target,
betaSched])
# Run Inversion
minv = inv.run(m0)

# Final Plot
############

fig, ax = plt.subplots(2, 2, figsize=(12, 6))
ax = Utils.mkvc(ax)

cyl0v = getCylinderPoints(x0, z0, r0)
cyl1v = getCylinderPoints(x1, z1, r1)

cyl0h = getCylinderPoints(x0, y0, r0)
cyl1h = getCylinderPoints(x1, y1, r1)

clim = [(mtrue[actind]).min(), (mtrue[actind]).max()]

dat = meshCore.plotSlice(((mtrue[actind])), ax=ax[0], normal='Y', clim=clim,
ind=int(ncy / 2))
ax[0].set_title('Ground Truth, Vertical')
ax[0].set_aspect('equal')

meshCore.plotSlice((minv), ax=ax[1], normal='Y', clim=clim, ind=int(ncy / 2))
ax[1].set_aspect('equal')
ax[1].set_title('Inverted Model, Vertical')

meshCore.plotSlice(((mtrue[actind])), ax=ax[2], normal='Z', clim=clim,
ind=int(ncz / 2))
ax[2].set_title('Ground Truth, Horizontal')
ax[2].set_aspect('equal')

meshCore.plotSlice((minv), ax=ax[3], normal='Z', clim=clim, ind=int(ncz / 2))
ax[3].set_title('Inverted Model, Horizontal')
ax[3].set_aspect('equal')

for i in range(2):
ax[i].plot(cyl0v[:, 0], cyl0v[:, 1], 'k--')
ax[i].plot(cyl1v[:, 0], cyl1v[:, 1], 'k--')
for i in range(2, 4):
ax[i].plot(cyl1h[:, 0], cyl1h[:, 1], 'k--')
ax[i].plot(cyl0h[:, 0], cyl0h[:, 1], 'k--')

cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
cb = plt.colorbar(dat[0], ax=cbar_ax)
cb.set_label('ln conductivity')

cbar_ax.axis('off')

plt.show()


Out:

SimPEG.DataMisfit.l2_DataMisfit assigning default eps of 1e-5 * ||dobs||
SimPEG.InvProblem will set Regularization.mref to 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  2.54e+01  2.45e+03  0.00e+00  2.45e+03    5.54e+02      0
1  2.54e+01  3.37e+02  5.85e+00  4.86e+02    5.70e+01      0
2  5.08e+00  2.22e+02  8.63e+00  2.66e+02    4.53e+01      0   Skip BFGS
3  5.08e+00  9.36e+01  1.88e+01  1.89e+02    2.34e+01      0
4  1.02e+00  7.55e+01  2.02e+01  9.61e+01    1.97e+01      0
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 2.4556e+02
1 : |xc-x_last| = 3.7764e+00 <= tolX*(1+|x0|) = 7.5300e+01
0 : |proj(x-g)-x|    = 1.9714e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.9714e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      10    <= iter          =      5
------------------------- DONE! -------------------------
/Users/lindseyjh/git/simpeg/simpeg/examples/06-dc/plot_dipoledipole_3Dinversion_twospheres.py:221: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()


Total running time of the script: ( 1 minutes 8.762 seconds)

Gallery generated by Sphinx-Gallery