-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBatch_Mag_Inversion_LpLq.py
246 lines (196 loc) · 7.83 KB
/
Batch_Mag_Inversion_LpLq.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
#!/usr/bin/env python
# coding: utf-8
# In[2]:
import os
#from mpi4py import MPI
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__)
#comm = MPI.COMM_WORLD
#size = comm.Get_size()
#rank = comm.Get_rank()
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('1block_magnetic_data.obs')
print("maximum mag data {} nT".format(data_mag.dobs.max()))
data_grav = io_utils.read_grav3d_ubc('1block_gravity_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, alpha_s=1., alphasmoothdefault=True, 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)
if alphasmoothdefault:
alpha_x, alpha_y, alpha_z = 1.0, 1.0, 1.0
else:
alpha_x = mesh.hx.min()**2
alpha_y = mesh.hy.min()**2
alpha_z = mesh.hz.min()**2
# Define the regularization (model objective function).
reg = regularization.Sparse(
mesh,
indActive=np.ones(mesh.n_cells, dtype=bool),
mapping=actvMap,
alpha_s=alpha_s,
alpha_x=alpha_x,
alpha_y=alpha_y,
alpha_z=alpha_z,
gradientType='total'
)
reg.norms = [[0, 1, 1, 1]]
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=100, 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()
IRLS = directives.Update_IRLS(f_min_change=1e-4, max_irls_iterations=50, beta_tol=1e-2,)
# 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,
IRLS,
update_jacobi,
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, alpha_s=1., alphasmoothdefault=True, 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)
if alphasmoothdefault:
alpha_x, alpha_y, alpha_z = 1.0, 1.0, 1.0
else:
alpha_x = mesh.hx.min()**2
alpha_y = mesh.hy.min()**2
alpha_z = mesh.hz.min()**2
# Define the regularization (model objective function).
reg = regularization.Sparse(
mesh,
indActive=np.ones(mesh.n_cells, dtype=bool),
mapping=actvMap,
alpha_s=alpha_s,
alpha_x=alpha_x,
alpha_y=alpha_y,
alpha_z=alpha_z,
gradientType='total'
)
reg.norms = [[0, 1, 1, 1]]
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=100, 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()
IRLS = directives.Update_IRLS(f_min_change=1e-4, max_irls_iterations=50, beta_tol=1e-2,)
# 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,
IRLS,
update_jacobi,
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]
#alphasmoothdefault = [True, False]
#dw = [True,False]
#namealpha = ['as{}'.format(alphas) for alphas in alphaslist]
#namereg = ['defaultSmooth1_','defaultSmooth0_']
#namew = ['dw_','sw_']
#d1s, d2s = np.meshgrid(alphaslist, alphasmoothdefault, dw)
#d1s = d1s.reshape(-1)
#d2s = d2s.reshape(-1)
#for i in range(rank, n_params, size):
directory_mag='magnetic_inversions_LpLq'
directory_grav='gravity_inversions_LpLq'
for alphas in alphaslist:
for alphasmoothdefault,namereg in zip([True], ['defaultSmooth1_']):
for dw, namew in zip([True], ['dw_']):
name = 'minv_'+namereg+namew+'as{}'.format(alphas)
print("magnetic: ", name)
run_mag_inversion(directory=directory_mag, dw=dw, alphasmoothdefault=alphasmoothdefault, alpha_s=alphas, name=name)
#for alphas in alphaslist:
# for alphasmoothdefault,namereg in zip([False, True], ['defaultSmooth0_','defaultSmooth1_']):
# 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, alphasmoothdefault=alphasmoothdefault, alpha_s=alphas, name=name)