-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBatch_Mag_Inversion_L2.py
207 lines (159 loc) · 6.78 KB
/
Batch_Mag_Inversion_L2.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import os
import sys
sys.path.insert(0,'/tera_raid/mmitchel/Programs/simpeg/simpeg-main-0152')
from SimPEG import dask
import SimPEG
print(SimPEG.__path__)
print(SimPEG.__version__)
import discretize as ds
print(ds.__path__)
print(ds.__version__)
import SimPEG.potential_fields as pf
from SimPEG import (
maps, utils, simulation, inverse_problem, inversion, optimization, regularization, data_misfit, directives
)
from SimPEG.utils import io_utils
import numpy as np
#Reproducible science
np.random.seed(518936)
mesh = ds.TreeMesh.read_UBC('mesh_CaMP.ubc')
data_mag = io_utils.read_mag3d_ubc('magnetic_data.obs')
print('TA mag dataset')
print("maximum mag data {} nT".format(data_mag.dobs.max()))
data_grav = io_utils.read_grav3d_ubc('grav_data.obs')
actvMap = maps.IdentityMap(mesh)
# mag problem
simulation_mag = pf.magnetics.simulation.Simulation3DIntegral(
survey=data_mag.survey,
mesh=mesh,
chiMap=actvMap,
)
# Grav problem
simulation_grav = pf.gravity.simulation.Simulation3DIntegral(
survey=data_grav.survey,
mesh=mesh,
rhoMap=actvMap,
)
# ## Create simulations and data misfits
def run_grav_inversion(directory='',dw=True, regtik=True, alpha_s=1., name='CaMP_gravity_synthetic_inversion_model'):
# Grav problem
dmis_grav = data_misfit.L2DataMisfit(data=data_grav, simulation=simulation_grav)
# Initial Model
m0 = np.zeros(mesh.n_cells)
# Define the regularization (model objective function).
if regtik:
print("Tikhonv regularization")
reg = regularization.Tikhonov(mesh, mapping=actvMap, indActive=np.ones(mesh.n_cells, dtype=bool))
else:
print("Simple regularization")
reg = regularization.Simple(mesh, mapping=actvMap, indActive=np.ones(mesh.n_cells, dtype=bool))
reg.alpha_s = alpha_s
if dw:
wr = utils.depth_weighting(
mesh, data_grav.survey.receiver_locations,
indActive=np.ones(mesh.n_cells, dtype=bool),
exponent=2
)
reg.cell_weights = wr
directives_list = []
else:
# Add sensitivity weights
sensitivity_weights = directives.UpdateSensitivityWeights(everyIter=False)
directives_list = [sensitivity_weights]
# Define how the optimization problem is solved. Here we will use a projected
# Gauss-Newton approach that employs the conjugate gradient solver.
opt = optimization.ProjectedGNCG(
maxIter=20, lower=-1.0, upper=1.0, maxIterLS=20, maxIterCG=100, tolCG=1e-4
)
# Here we define the inverse problem that is to be solved
inv_prob = inverse_problem.BaseInvProblem(dmis_grav, reg, opt)
# Defining a starting value for the trade-off parameter (beta) between the data
# misfit and the regularization.
starting_beta = directives.BetaEstimate_ByEig(beta0_ratio=1e0)
beta_schedule = directives.BetaSchedule(coolingFactor=5, coolingRate=1)
update_jacobi = directives.UpdatePreconditioner()
target_misfit = directives.TargetMisfit(chifact=1)
# save every iteration
save_dict = directives.SaveOutputDictEveryIteration(saveOnDisk=True)
every_iter_folder = directory + os.path.sep + 'EveryIteration_'+ name + os.path.sep
os.makedirs(every_iter_folder, exist_ok=True)
save_dict.directory = every_iter_folder
# The directives are defined as a list.
directives_list = directives_list + [
starting_beta,
beta_schedule,
update_jacobi,
target_misfit,
save_dict,
]
inv3 = inversion.BaseInversion(inv_prob, directives_list)
recovered_model_grav = inv3.run(m0)
os.makedirs(directory, exist_ok=True)
mesh.write_model_UBC(directory + os.path.sep + name + ".den", recovered_model_grav)
def run_mag_inversion(directory='',dw=True, regtik=True, alpha_s=1., name='CaMP_magnetic_synthetic_inversion_model'):
dmis_mag = data_misfit.L2DataMisfit(data=data_mag, simulation=simulation_mag)
# Initial Model
m0 = 1e-4 * np.ones(mesh.nC)
# Define the regularization (model objective function).
if regtik:
print("Tikhonv regularization")
reg = regularization.Tikhonov(mesh, mapping=actvMap, indActive=np.ones(mesh.n_cells, dtype=bool))
else:
print("Simple regularization")
reg = regularization.Simple(mesh, mapping=actvMap, indActive=np.ones(mesh.n_cells, dtype=bool))
reg.alpha_s = alpha_s
if dw:
wr = utils.depth_weighting(
mesh, data_mag.survey.receiver_locations,
indActive=np.ones(mesh.n_cells, dtype=bool),
exponent=3
)
reg.cell_weights = wr
directives_list = []
else:
# Add sensitivity weights
sensitivity_weights = directives.UpdateSensitivityWeights(everyIter=False)
directives_list = [sensitivity_weights]
opt = optimization.ProjectedGNCG(
maxIter=20, lower=0.0, upper=1.0, maxIterLS=20, maxIterCG=100, tolCG=1e-4
)
inv_prob = inverse_problem.BaseInvProblem(dmis_mag, reg, opt)
# Defining a starting value for the trade-off parameter (beta) between the data
# misfit and the regularization.
starting_beta = directives.BetaEstimate_ByEig(beta0_ratio=1e-2)
beta_schedule = directives.BetaSchedule(coolingFactor=5, coolingRate=1)
update_jacobi = directives.UpdatePreconditioner()
target_misfit = directives.TargetMisfit(chifact=1)
# save every iteration
save_dict = directives.SaveOutputDictEveryIteration(saveOnDisk=True)
every_iter_folder = directory + os.path.sep + 'EveryIteration_'+ name + os.path.sep
os.makedirs(every_iter_folder, exist_ok=True)
save_dict.directory = every_iter_folder
# The directives are defined as a list.
directives_list = directives_list + [
starting_beta,
beta_schedule,
update_jacobi,
target_misfit,
save_dict,
]
inv = inversion.BaseInversion(inv_prob, directives_list)
# Run inversion
recovered_model_mag = inv.run(m0)
os.makedirs(directory, exist_ok=True)
mesh.write_model_UBC(directory + os.path.sep + name + ".sus", recovered_model_mag)
################################################################
alphaslist = np.r_[1/(250**2), 1/(100**2), 1]
directory_mag='magnetic_inversions'
directory_grav='gravity_inversions'
for alphas in alphaslist:
for regtik,namereg in zip([True, False], ['Tik_','Simple_']):
for dw, namew in zip([True,False], ['dw_','sw_']):
name = 'minv_'+namereg+namew+'as{}'.format(alphas)
#print("gravity: ", name)
#run_grav_inversion(directory=directory_grav, dw=dw, regtik=regtik, alpha_s=alphas, name=name)
print("magnetic: ", name)
run_mag_inversion(directory=directory_mag, dw=dw, regtik=regtik, alpha_s=alphas, name=name)