forked from simpeg/simpeg
/
MagneticsDriver.py
332 lines (255 loc) · 9.2 KB
/
MagneticsDriver.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import re
import os
from SimPEG import Mesh, Utils
import numpy as np
from . import BaseMag
from . import Magnetics
class MagneticsDriver_Inv(object):
"""docstring for MagneticsDriver_Inv"""
def __init__(self, input_file=None):
if input_file is not None:
self.basePath = os.path.sep.join(
input_file.split(os.path.sep)[:-1]
)
if len(self.basePath) > 0:
self.basePath += os.path.sep
self.readDriverFile(input_file.split(os.path.sep)[-1])
def readDriverFile(self, input_file):
"""
Read input files for forward modeling MAG data with integral form
INPUT:
input_file: File name containing the forward parameter
OUTPUT:
mshfile
obsfile
topofile
start model
ref model
mag model
weightfile
chi_target
as, ax ,ay, az
upper, lower bounds
lp, lqx, lqy, lqz
# All files should be in the working directory,
# otherwise the path must be specified.
"""
fid = open(self.basePath + input_file, 'r')
# Line 1: Mesh
line = fid.readline()
l_input = re.split('[!\s]', line)
mshfile = l_input[1].rstrip()
# Line 2: Observation file
line = fid.readline()
l_input = re.split('[!\s]', line)
obsfile = l_input[1].rstrip()
# Line 3: Topo, active-dyn, active-static
topofile = None
staticInput = None
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'TOPO':
topofile = l_input[1].rstrip()
elif l_input[0] == 'VALUE':
staticInput = float(l_input[1])
elif l_input[0] == 'FILE':
staticInput = l_input[1].rstrip()
# Line 4: Starting model
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
mstart = float(l_input[1])
elif l_input[0] == 'FILE':
mstart = l_input[1].rstrip()
# Line 5: Reference model
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
mref = float(l_input[1])
elif l_input[0] == 'FILE':
mref = l_input[1].rstrip()
# Line 6: Magnetization model
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'DEFAULT':
magfile = None
elif l_input[0] == 'FILE':
magfile = l_input[1].rstrip()
# Line 7: Cell weights
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'DEFAULT':
wgtfile = []
elif l_input[0] == 'FILE':
wgtfile = l_input[1].rstrip()
# Line 8: Target chi-factor
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'DEFAULT':
chi = 1.
elif l_input[0] == 'VALUE':
chi = float(l_input[1])
# Line 9: Alpha values
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
val = np.array(l_input[1:5])
alphas = val.astype(np.float)
elif l_input[0] == 'DEFAULT':
alphas = np.ones(4)
# Line 10: Bounds
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
val = np.array(l_input[1:3])
bounds = val.astype(np.float)
elif l_input[0] == 'FILE':
bounds = l_input[1].rstrip()
else:
bounds = [-np.inf, np.inf]
# Line 11: Norms
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
val = np.array(l_input[1:6])
lpnorms = val.astype(np.float)
elif l_input[0] == 'FILE':
lpnorms = l_input[1].rstrip()
# Line 12: Treshold values
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'VALUE':
val = np.array(l_input[1:3])
eps = val.astype(np.float)
elif l_input[0] == 'DEFAULT':
eps = None
self.mshfile = mshfile
self.obsfile = obsfile
self.topofile = topofile
self.mstart = mstart
self._mrefInput = mref
self._staticInput = staticInput
self.magfile = magfile
self.wgtfile = wgtfile
self.chi = chi
self.alphas = alphas
self.bounds = bounds
self.lpnorms = lpnorms
self.eps = eps
@property
def mesh(self):
if getattr(self, '_mesh', None) is None:
self._mesh = Mesh.TensorMesh.readUBC(self.basePath + self.mshfile)
return self._mesh
@property
def survey(self):
if getattr(self, '_survey', None) is None:
self._survey, _ = Utils.io_utils.readUBCmagneticsObservations(
self.basePath + self.obsfile
)
return self._survey
@property
def activeCells(self):
if getattr(self, '_activeCells', None) is None:
if getattr(self, 'topofile', None) is not None:
topo = np.genfromtxt(
self.basePath + self.topofile, skip_header=1
)
# Find the active cells
active = Utils.surface2ind_topo(self.mesh, topo, 'N')
elif isinstance(self._staticInput, float):
active = self.m0 != self._staticInput
else:
# Read from file active cells with 0:air, 1:dynamic, -1 static
active = self.activeModel != 0
inds = np.where(active)[0]
self._activeCells = inds
# Reduce m0 to active space
if len(self.m0) > len(self._activeCells):
self._m0 = self.m0[self._activeCells]
return self._activeCells
@property
def staticCells(self):
if getattr(self, '_staticCells', None) is None:
# Cells with value 1 in active model are dynamic
staticCells = self.activeModel[self.activeCells] == -1
inds = np.where(staticCells)[0]
self._staticCells = inds
return self._staticCells
@property
def dynamicCells(self):
if getattr(self, '_dynamicCells', None) is None:
# Cells with value 1 in active model are dynamic
dynamicCells = self.activeModel[self.activeCells] == 1
inds = np.where(dynamicCells)[0]
self._dynamicCells = inds
return self._dynamicCells
@property
def nC(self):
if getattr(self, '_nC', None) is None:
self._nC = len(self.activeCells)
return self._nC
@property
def m0(self):
if getattr(self, '_m0', None) is None:
if isinstance(self.mstart, float):
self._m0 = np.ones(self.nC) * self.mstart
else:
self._m0 = Mesh.TensorMesh.readModelUBC(
self.mesh, self.basePath + self.mstart
)
return self._m0
@property
def mref(self):
if getattr(self, '_mref', None) is None:
if isinstance(self._mrefInput, float):
self._mref = np.ones(self.nC) * self._mrefInput
else:
self._mref = Mesh.TensorMesh.readModelUBC(
self.mesh, self.basePath + self._mrefInput
)
# Reduce to active space
self._mref = self._mref[self.activeCells]
return self._mref
@property
def activeModel(self):
if getattr(self, '_activeModel', None) is None:
if self._staticInput == 'FILE':
# Read from file active cells with 0:air, 1:dynamic, -1 static
self._activeModel = Mesh.TensorMesh.readModelUBC(
self.mesh, self.basePath + self._staticInput
)
else:
self._activeModel = np.ones(self._mesh.nC)
return self._activeModel
@property
def magnetizationModel(self):
"""
magnetization vector
"""
if getattr(self, 'magfile', None) is None:
M = Magnetics.dip_azimuth2cartesian(
np.ones(self.nC) * self.survey.srcField.param[1],
np.ones(self.nC) * self.survey.srcField.param[2]
)
else:
with open(self.basePath + self.magfile) as f:
magmodel = f.read()
magmodel = magmodel.splitlines()
M = []
for line in magmodel:
M.append(map(float, line.split()))
# Convert list to 2d array
M = np.vstack(M)
# Cycle through three components and permute from UBC to SimPEG
for ii in range(3):
m = np.reshape(
M[:, ii],
(self.mesh.nCz, self.mesh.nCx, self.mesh.nCy),
order='F'
)
m = m[::-1, :, :]
m = np.transpose(m, (1, 2, 0))
M[:, ii] = Utils.mkvc(m)
self._M = M
return self._M