forked from simpeg/simpeg
/
Gravity.py
411 lines (297 loc) · 11.2 KB
/
Gravity.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from __future__ import print_function
from SimPEG import Problem, Mesh
from SimPEG import Utils
from SimPEG.Utils import mkvc
from SimPEG import Props
import scipy as sp
import scipy.constants as constants
import os
import time
import numpy as np
class GravityIntegral(Problem.LinearProblem):
rho, rhoMap, rhoDeriv = Props.Invertible(
"Specific density (g/cc)",
default=1.
)
# surveyPair = Survey.LinearSurvey
forwardOnly = False # Is TRUE, forward matrix not stored to memory
actInd = None #: Active cell indices provided
rx_type = 'z'
silent = False
memory_saving_mode = False
parallelized = False
n_cpu = None
progress_index = -1
gtgdiag = None
aa = []
def __init__(self, mesh, **kwargs):
Problem.BaseProblem.__init__(self, mesh, **kwargs)
def fields(self, m):
model = self.rhoMap*m
if self.forwardOnly:
# Compute the linear operation without forming the full dense G
fields = self.Intrgl_Fwr_Op(m=m)
return mkvc(fields)
else:
vec = np.dot(self.G, model.astype(np.float32))
return vec.astype(np.float64)
def getJtJdiag(self, m, W=None):
"""
Return the diagonal of JtJ
"""
if self.gtgdiag is None:
if W is None:
w = np.ones(self.G.shape[1])
else:
w = W.diagonal()
dmudm = self.rhoMap.deriv(m)
self.gtgdiag = np.zeros(dmudm.shape[1])
for ii in range(self.G.shape[0]):
self.gtgdiag += (w[ii]*self.G[ii, :]*dmudm)**2.
return self.gtgdiag
def getJ(self, m, f=None):
"""
Sensitivity matrix
"""
return self.G
def Jvec(self, m, v, f=None):
dmudm = self.rhoMap.deriv(m)
return self.G.dot(dmudm*v)
def Jtvec(self, m, v, f=None):
dmudm = self.rhoMap.deriv(m)
return dmudm.T * (self.G.T.dot(v))
@property
def G(self):
if not self.ispaired:
raise Exception('Need to pair!')
if getattr(self, '_G', None) is None:
print("Begin linear forward calculation: " + self.rx_type)
start = time.time()
self._G = self.Intrgl_Fwr_Op()
print("Linear forward calculation ended in: " + str(time.time()-start) + " sec")
return self._G
def Intrgl_Fwr_Op(self, m=None, rx_type='z'):
"""
Gravity forward operator in integral form
flag = 'z' | 'xyz'
Return
_G = Linear forward modeling operation
Created on March, 15th 2016
@author: dominiquef
"""
if m is not None:
self.model = self.rhoMap*m
if getattr(self, 'actInd', None) is not None:
if self.actInd.dtype == 'bool':
inds = np.where(self.actInd)[0]
else:
inds = self.actInd
else:
inds = np.asarray(range(self.mesh.nC))
self.nC = len(inds)
# Create active cell projector
P = sp.sparse.csr_matrix(
(np.ones(self.nC), (inds, range(self.nC))),
shape=(self.mesh.nC, self.nC)
)
# Create vectors of nodal location
# (lower and upper corners for each cell)
if isinstance(self.mesh, Mesh.TreeMesh):
# Get upper and lower corners of each cell
bsw = (self.mesh.gridCC -
np.kron(self.mesh.vol.T**(1/3)/2,
np.ones(3)).reshape((self.mesh.nC, 3)))
tne = (self.mesh.gridCC +
np.kron(self.mesh.vol.T**(1/3)/2,
np.ones(3)).reshape((self.mesh.nC, 3)))
xn1, xn2 = bsw[:, 0], tne[:, 0]
yn1, yn2 = bsw[:, 1], tne[:, 1]
zn1, zn2 = bsw[:, 2], tne[:, 2]
else:
xn = self.mesh.vectorNx
yn = self.mesh.vectorNy
zn = self.mesh.vectorNz
yn2, xn2, zn2 = np.meshgrid(yn[1:], xn[1:], zn[1:])
yn1, xn1, zn1 = np.meshgrid(yn[:-1], xn[:-1], zn[:-1])
self.Yn = P.T*np.c_[Utils.mkvc(yn1), Utils.mkvc(yn2)]
self.Xn = P.T*np.c_[Utils.mkvc(xn1), Utils.mkvc(xn2)]
self.Zn = P.T*np.c_[Utils.mkvc(zn1), Utils.mkvc(zn2)]
self.rxLoc = self.survey.srcField.rxList[0].locs
self.nD = int(self.rxLoc.shape[0])
# if self.n_cpu is None:
# self.n_cpu = multiprocessing.cpu_count()
# Switch to determine if the process has to be run in parallel
job = Forward(
rxLoc=self.rxLoc, Xn=self.Xn, Yn=self.Yn, Zn=self.Zn,
n_cpu=self.n_cpu, forwardOnly=self.forwardOnly,
model=self.model, rx_type=self.rx_type,
parallelized=self.parallelized
)
G = job.calculate()
return G
@property
def modelMap(self):
"""
Call for general mapping of the problem
"""
return self.rhoMap
class Forward(object):
"""
Add docstring once it works
"""
progress_index = -1
parallelized = False
rxLoc = None
Xn, Yn, Zn = None, None, None
n_cpu = None
forwardOnly = False
model = None
rx_type = 'z'
def __init__(self, **kwargs):
super(Forward, self).__init__()
Utils.setKwargs(self, **kwargs)
def calculate(self):
self.nD = self.rxLoc.shape[0]
if self.parallelized:
if self.n_cpu is None:
# By default take half the cores, turns out be faster
# than running full threads
self.n_cpu = int(multiprocessing.cpu_count()/2)
pool = multiprocessing.Pool(self.n_cpu)
result = pool.map(self.calcTrow, [self.rxLoc[ii, :] for ii in range(self.nD)])
pool.close()
pool.join()
else:
result = []
for ii in range(self.nD):
result += [self.calcTrow(self.rxLoc[ii, :])]
self.progress(ii, self.nD)
if self.forwardOnly:
return mkvc(np.vstack(result))
else:
return np.vstack(result)
def calcTrow(self, xyzLoc):
"""
Load in the active nodes of a tensor mesh and computes the gravity tensor
for a given observation location xyzLoc[obsx, obsy, obsz]
INPUT:
Xn, Yn, Zn: Node location matrix for the lower and upper most corners of
all cells in the mesh shape[nC,2]
M
OUTPUT:
Tx = [Txx Txy Txz]
Ty = [Tyx Tyy Tyz]
Tz = [Tzx Tzy Tzz]
where each elements have dimension 1-by-nC.
Only the upper half 5 elements have to be computed since symetric.
Currently done as for-loops but will eventually be changed to vector
indexing, once the topography has been figured out.
"""
NewtG = constants.G*1e+8 # Convertion from mGal (1e-5) and g/cc (1e-3)
eps = 1e-8 # add a small value to the locations to avoid
# Pre-allocate space for 1D array
row = np.zeros((1, self.Xn.shape[0]))
dz = xyzLoc[2] - self.Zn
dy = self.Yn - xyzLoc[1]
dx = self.Xn - xyzLoc[0]
# Compute contribution from each corners
for aa in range(2):
for bb in range(2):
for cc in range(2):
r = (
mkvc(dx[:, aa]) ** 2 +
mkvc(dy[:, bb]) ** 2 +
mkvc(dz[:, cc]) ** 2
) ** (0.50)
if self.rx_type == 'x':
row -= NewtG * (-1) ** aa * (-1) ** bb * (-1) ** cc * (
dy[:, bb] * np.log(dz[:, cc] + r + eps) +
dz[:, cc] * np.log(dy[:, bb] + r + eps) -
dx[:, aa] * np.arctan(dy[:, bb] * dz[:, cc] /
(dx[:, aa] * r + eps)))
elif self.rx_type == 'y':
row -= NewtG * (-1) ** aa * (-1) ** bb * (-1) ** cc * (
dx[:, aa] * np.log(dz[:, cc] + r + eps) +
dz[:, cc] * np.log(dx[:, aa] + r + eps) -
dy[:, bb] * np.arctan(dx[:, aa] * dz[:, cc] /
(dy[:, bb] * r + eps)))
else:
row -= NewtG * (-1) ** aa * (-1) ** bb * (-1) ** cc * (
dx[:, aa] * np.log(dy[:, bb] + r + eps) +
dy[:, bb] * np.log(dx[:, aa] + r + eps) -
dz[:, cc] * np.arctan(dx[:, aa] * dy[:, bb] /
(dz[:, cc] * r + eps)))
if self.forwardOnly:
return np.dot(row, self.model)
else:
return row
def progress(self, ind, total):
"""
progress(ind,prog,final)
Function measuring the progress of a process and print to screen the %.
Useful to estimate the remaining runtime of a large problem.
Created on Dec, 20th 2015
@author: dominiquef
"""
arg = np.floor(ind/total*10.)
if arg > self.progress_index:
print("Done " + str(arg*10) + " %")
self.progress_index = arg
class Problem3D_Diff(Problem.BaseProblem):
"""
Gravity in differential equations!
"""
_depreciate_main_map = 'rhoMap'
rho, rhoMap, rhoDeriv = Props.Invertible(
"Specific density (g/cc)",
default=1.
)
solver = None
def __init__(self, mesh, **kwargs):
Problem.BaseProblem.__init__(self, mesh, **kwargs)
self.mesh.setCellGradBC('dirichlet')
self._Div = self.mesh.cellGrad
@property
def MfI(self): return self._MfI
@property
def Mfi(self): return self._Mfi
def makeMassMatrices(self, m):
self.model = m
self._Mfi = self.mesh.getFaceInnerProduct()
self._MfI = Utils.sdiag(1. / self._Mfi.diagonal())
def getRHS(self, m):
"""
"""
Mc = Utils.sdiag(self.mesh.vol)
self.model = m
rho = self.rho
return Mc * rho
def getA(self, m):
"""
GetA creates and returns the A matrix for the Gravity nodal problem
The A matrix has the form:
.. math ::
\mathbf{A} = \Div(\MfMui)^{-1}\Div^{T}
"""
return -self._Div.T * self.Mfi * self._Div
def fields(self, m):
"""
Return gravity potential (u) and field (g)
u: defined on the cell nodes [nC x 1]
gField: defined on the cell faces [nF x 1]
"""
from scipy.constants import G as NewtG
self.makeMassMatrices(m)
A = self.getA(m)
RHS = self.getRHS(m)
if self.solver is None:
m1 = sp.linalg.interface.aslinearoperator(
Utils.sdiag(1 / A.diagonal())
)
u, info = sp.linalg.bicgstab(A, RHS, tol=1e-6, maxiter=1000, M=m1)
else:
print("Solving with Paradiso")
Ainv = self.solver(A)
u = Ainv * RHS
gField = 4. * np.pi * NewtG * 1e+8 * self._Div * u
return {'G': gField, 'u': u}