forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_dipoledipole_2Dinversion_twocylinders.py
178 lines (138 loc) · 5.27 KB
/
plot_dipoledipole_2Dinversion_twocylinders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
2D DC inversion of Dipole Dipole array
======================================
This is an example for 2D DC Inversion. The model consists of 2 cylinders,
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, :math:`\\mathcal{M}`,
indicates transformation of our model to a different space:
.. math::
\\sigma = \\mathcal{M}(\\mathbf{m})
Following example will show you how user can implement a 2D 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
# Reproducible science
np.random.seed(12345)
# 2D Mesh
#########
# Cells size
csx, csz = 0.25, 0.25
# Number of core cells in each direction
ncx, ncz = 123, 41
# Number of padding cells to add in each direction
npad = 12
# Vectors of cell lengthts in each direction
hx = [(csx, npad, -1.5), (csx, ncx), (csx, npad, 1.5)]
hz = [(csz, npad, -1.5), (csz, ncz)]
# Create mesh
mesh = Mesh.TensorMesh([hx, hz], x0="CN")
mesh.x0[1] = mesh.x0[1] + csz / 2.
# 2-cylinders Model Creation
############################
# Spheres parameters
x0, z0, r0 = -6., -5., 3.
x1, z1, r1 = 6., -5., 3.
ln_sigback = -5.
ln_sigc = -3.
ln_sigr = -6.
mtrue = ln_sigback * np.ones(mesh.nC)
csph = (np.sqrt((mesh.gridCC[:, 1] - z0) **
2. + (mesh.gridCC[:, 0] - x0)**2.)) < r0
mtrue[csph] = ln_sigc * np.ones_like(mtrue[csph])
# Define the sphere limit
rsph = (np.sqrt((mesh.gridCC[:, 1] - z1) **
2. + (mesh.gridCC[:, 0] - x1)**2.)) < r1
mtrue[rsph] = ln_sigr * np.ones_like(mtrue[rsph])
mtrue = Utils.mkvc(mtrue)
xmin, xmax = -15., 15
ymin, ymax = -15., 0.
xyzlim = np.r_[[[xmin, xmax], [ymin, ymax]]]
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 Dipole-Dipole Survey
xmin, xmax = -15., 15.
ymin, ymax = 0., 0.
zmin, zmax = mesh.vectorCCy[-1], mesh.vectorCCy[-1]
endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
survey = DCUtils.gen_DCIPsurvey(endl, "dipole-dipole", dim=mesh.dim,
a=1, b=1, n=10, d2flag='2D')
# 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
####################
m0 = np.median(ln_sigback) * np.ones(mapping.nP)
dmis = DataMisfit.l2_DataMisfit(survey)
regT = Regularization.Simple(mesh, indActive=actind)
# Personal preference for this solver with a Jacobi preconditioner
opt = Optimization.ProjectedGNCG(maxIter=20, lower=-10, upper=10,
maxIterLS=20, maxIterCG=30, tolCG=1e-4)
opt.remember('xc')
invProb = InvProblem.BaseInvProblem(dmis, regT, opt)
beta = Directives.BetaEstimate_ByEig(beta0_ratio=1.)
Target = Directives.TargetMisfit()
betaSched = Directives.BetaSchedule(coolingFactor=5., coolingRate=2)
updateSensW = Directives.UpdateSensitivityWeights(threshold=1e-3)
update_Jacobi = Directives.UpdatePreconditioner()
inv = Inversion.BaseInversion(invProb, directiveList=[beta, Target,
betaSched, updateSensW,
update_Jacobi])
minv = inv.run(m0)
# Final Plot
############
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax = Utils.mkvc(ax)
cyl0v = getCylinderPoints(x0, z0, r0)
cyl1v = getCylinderPoints(x1, z1, r1)
clim = [(mtrue[actind]).min(), (mtrue[actind]).max()]
dat = meshCore.plotImage(((mtrue[actind])), ax=ax[0], clim=clim)
ax[0].set_title('Ground Truth')
ax[0].set_aspect('equal')
meshCore.plotImage((minv), ax=ax[1], clim=clim)
ax[1].set_aspect('equal')
ax[1].set_title('Inverted Model')
for i in range(2):
ax[i].plot(cyl0v[:, 0], cyl0v[:, 1], 'k--')
ax[i].plot(cyl1v[:, 0], cyl1v[:, 1], 'k--')
fig.subplots_adjust(right=0.8)
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()