forked from simpeg/simpeg
/
GravityDriver.py
286 lines (228 loc) · 8.18 KB
/
GravityDriver.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
import re
import os
from SimPEG import Mesh, Utils
import numpy as np
from . import BaseGrav, Gravity
class GravityDriver_Inv(object):
"""docstring for GravityDriver_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 GRAV data with integral form
INPUT:
input_file: File name containing the forward parameter
OUTPUT:
mshfile
obsfile
topofile
start model
ref model
active cells model
weightfile
chi_target
as, ax ,ay, az
upper, lower bounds
lp, lqx, lqy, lqz
eps_p, eps_q
# 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: Cell weights
line = fid.readline()
l_input = re.split('[!\s]', line)
if l_input[0] == 'DEFAULT':
wgtfile = None
elif l_input[0] == 'FILE':
wgtfile = l_input[1].rstrip()
# Line 7: 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 8: 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 9: 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 10: 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 11: 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.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.readUBCgravityObservations(
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.asarray(
[
inds for inds, elem in enumerate(active, 1) if elem
],
dtype=int
) - 1
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.asarray(
[
inds for inds, elem in enumerate(staticCells, 1) if elem
],
dtype=int
) - 1
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.asarray(
[inds for inds, elem in enumerate(
dynamicCells, 1) if elem], dtype=int
) - 1
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
)
self._mref = self._mref[self.activeCells]
return self._mref
@property
def activeModel(self):
if getattr(self, '_activeModel', None) is None:
if isinstance(self._staticInput, str):
# 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