forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_inversion_linear.py
253 lines (207 loc) · 8.33 KB
/
plot_inversion_linear.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""
PF: Gravity: Inversion Linear
=============================
Create a synthetic block model and invert
with a compact norm
"""
import numpy as np
import matplotlib.pyplot as plt
from SimPEG import Mesh
from SimPEG import Utils
from SimPEG import Maps
from SimPEG import Regularization
from SimPEG import DataMisfit
from SimPEG import Optimization
from SimPEG import InvProblem
from SimPEG import Directives
from SimPEG import Inversion
from SimPEG import PF
def run(plotIt=True):
# Create a mesh
dx = 5.
hxind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)]
hyind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)]
hzind = [(dx, 5, -1.3), (dx, 7), (3.5, 1), (2, 5)]
mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC')
# Get index of the center
midx = int(mesh.nCx/2)
midy = int(mesh.nCy/2)
# Lets create a simple Gaussian topo and set the active cells
[xx, yy] = np.meshgrid(mesh.vectorNx, mesh.vectorNy)
zz = -np.exp((xx**2 + yy**2) / 75**2) + mesh.vectorNz[-1]
# We would usually load a topofile
topo = np.c_[Utils.mkvc(xx), Utils.mkvc(yy), Utils.mkvc(zz)]
# Go from topo to actv cells
actv = Utils.surface2ind_topo(mesh, topo, 'N')
actv = np.asarray([inds for inds, elem in enumerate(actv, 1) if elem],
dtype=int) - 1
# Create active map to go from reduce space to full
actvMap = Maps.InjectActiveCells(mesh, actv, -100)
nC = len(actv)
# Create and array of observation points
xr = np.linspace(-30., 30., 20)
yr = np.linspace(-30., 30., 20)
X, Y = np.meshgrid(xr, yr)
# Move the observation points 5m above the topo
Z = -np.exp((X**2 + Y**2) / 75**2) + mesh.vectorNz[-1] + 0.1
# Create a MAGsurvey
rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)]
rxLoc = PF.BaseGrav.RxObs(rxLoc)
srcField = PF.BaseGrav.SrcField([rxLoc])
survey = PF.BaseGrav.LinearSurvey(srcField)
# We can now create a susceptibility model and generate data
# Here a simple block in half-space
model = np.zeros((mesh.nCx, mesh.nCy, mesh.nCz))
model[(midx-5):(midx-1), (midy-2):(midy+2), -10:-6] = 0.75
model[(midx+1):(midx+5), (midy-2):(midy+2), -10:-6] = -0.75
model = Utils.mkvc(model)
model = model[actv]
# Create active map to go from reduce set to full
actvMap = Maps.InjectActiveCells(mesh, actv, -100)
# Create reduced identity map
idenMap = Maps.IdentityMap(nP=nC)
# Create the forward model operator
prob = PF.Gravity.GravityIntegral(mesh, rhoMap=idenMap, actInd=actv)
# Pair the survey and problem
survey.pair(prob)
# Compute linear forward operator and compute some data
d = prob.fields(model)
# Add noise and uncertainties
# We add some random Gaussian noise (1nT)
data = d + np.random.randn(len(d))*1e-3
wd = np.ones(len(data))*1e-3 # Assign flat uncertainties
survey.dobs = data
survey.std = wd
survey.mtrue = model
# Create sensitivity weights from our linear forward operator
rxLoc = survey.srcField.rxList[0].locs
wr = np.sum(prob.G**2., axis=0)**0.5
wr = (wr/np.max(wr))
# Create a regularization
reg = Regularization.Sparse(mesh, indActive=actv, mapping=idenMap)
reg.cell_weights = wr
reg.norms = np.c_[1, 0, 0, 0]
# Data misfit function
dmis = DataMisfit.l2_DataMisfit(survey)
dmis.W = Utils.sdiag(1/wd)
# Add directives to the inversion
opt = Optimization.ProjectedGNCG(maxIter=100, lower=-1., upper=1.,
maxIterLS=20, maxIterCG=10,
tolCG=1e-3)
invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
betaest = Directives.BetaEstimate_ByEig()
# Here is where the norms are applied
# Use pick a treshold parameter empirically based on the distribution of
# model parameters
IRLS = Directives.Update_IRLS(
f_min_change=1e-4, maxIRLSiter=30, coolEpsFact=1.5, beta_tol=1e-1,
)
saveDict = Directives.SaveOutputEveryIteration(save_txt=False)
update_Jacobi = Directives.UpdatePreconditioner()
inv = Inversion.BaseInversion(
invProb, directiveList=[IRLS, betaest, update_Jacobi, saveDict]
)
# Run the inversion
m0 = np.ones(nC)*1e-4 # Starting model
mrec = inv.run(m0)
if plotIt:
# Here is the recovered susceptibility model
ypanel = midx
zpanel = -7
m_l2 = actvMap * invProb.l2model
m_l2[m_l2 == -100] = np.nan
m_lp = actvMap * mrec
m_lp[m_lp == -100] = np.nan
m_true = actvMap * model
m_true[m_true == -100] = np.nan
vmin, vmax = mrec.min(), mrec.max()
# Plot the data
PF.Gravity.plot_obs_2D(rxLoc, d=data)
plt.figure()
# Plot L2 model
ax = plt.subplot(321)
mesh.plotSlice(m_l2, ax=ax, normal='Z', ind=zpanel,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
plt.title('Plan l2-model.')
plt.gca().set_aspect('equal')
plt.ylabel('y')
ax.xaxis.set_visible(False)
plt.gca().set_aspect('equal', adjustable='box')
# Vertica section
ax = plt.subplot(322)
mesh.plotSlice(m_l2, ax=ax, normal='Y', ind=midx,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
plt.title('E-W l2-model.')
plt.gca().set_aspect('equal')
ax.xaxis.set_visible(False)
plt.ylabel('z')
plt.gca().set_aspect('equal', adjustable='box')
# Plot Lp model
ax = plt.subplot(323)
mesh.plotSlice(m_lp, ax=ax, normal='Z', ind=zpanel,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
plt.title('Plan lp-model.')
plt.gca().set_aspect('equal')
ax.xaxis.set_visible(False)
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')
# Vertical section
ax = plt.subplot(324)
mesh.plotSlice(m_lp, ax=ax, normal='Y', ind=midx,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
plt.title('E-W lp-model.')
plt.gca().set_aspect('equal')
ax.xaxis.set_visible(False)
plt.ylabel('z')
plt.gca().set_aspect('equal', adjustable='box')
# Plot True model
ax = plt.subplot(325)
mesh.plotSlice(m_true, ax=ax, normal='Z', ind=zpanel,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
plt.title('Plan true model.')
plt.gca().set_aspect('equal')
plt.xlabel('x')
plt.ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')
# Vertical section
ax = plt.subplot(326)
mesh.plotSlice(m_true, ax=ax, normal='Y', ind=midx,
grid=True, clim=(vmin, vmax))
plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
plt.title('E-W true model.')
plt.gca().set_aspect('equal')
plt.xlabel('x')
plt.ylabel('z')
plt.gca().set_aspect('equal', adjustable='box')
# Plot convergence curves
fig, axs = plt.figure(), plt.subplot()
axs.plot(saveDict.phi_d, 'k', lw=2)
axs.plot(
np.r_[IRLS.iterStart, IRLS.iterStart],
np.r_[0, np.max(saveDict.phi_d)], 'k:'
)
twin = axs.twinx()
twin.plot(saveDict.phi_m, 'k--', lw=2)
axs.text(
IRLS.iterStart, np.max(saveDict.phi_d)/2.,
'IRLS Steps', va='bottom', ha='center',
rotation='vertical', size=12,
bbox={'facecolor': 'white'}
)
axs.set_ylabel('$\phi_d$', size=16, rotation=0)
axs.set_xlabel('Iterations', size=14)
twin.set_ylabel('$\phi_m$', size=16, rotation=0)
if __name__ == '__main__':
run()
plt.show()