forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProblemTDEM.py
1128 lines (847 loc) · 35.6 KB
/
ProblemTDEM.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import division, print_function
import scipy.sparse as sp
import numpy as np
from SimPEG import Problem, Utils, Solver as SimpegSolver
from SimPEG.EM.Base import BaseEMProblem
from SimPEG.EM.TDEM.SurveyTDEM import Survey as SurveyTDEM
from SimPEG.EM.TDEM.FieldsTDEM import (
FieldsTDEM, Fields3D_b, Fields3D_e, Fields3D_h, Fields3D_j, Fields_Derivs
)
from scipy.constants import mu_0
import time
class BaseTDEMProblem(Problem.BaseTimeProblem, BaseEMProblem):
"""
We start with the first order form of Maxwell's equations, eliminate and
solve the second order form. For the time discretization, we use backward
Euler.
"""
surveyPair = SurveyTDEM #: A SimPEG.EM.TDEM.SurveyTDEM Class
fieldsPair = FieldsTDEM #: A SimPEG.EM.TDEM.FieldsTDEM Class
def __init__(self, mesh, **kwargs):
BaseEMProblem.__init__(self, mesh, **kwargs)
# def fields_nostore(self, m):
# """
# Solve the forward problem without storing fields
# :param numpy.array m: inversion model (nP,)
# :rtype: numpy.array
# :return numpy.array: numpy.array (nD,)
# """
def fields(self, m):
"""
Solve the forward problem for the fields.
:param numpy.array m: inversion model (nP,)
:rtype: SimPEG.EM.TDEM.FieldsTDEM
:return F: fields object
"""
tic = time.time()
self.model = m
F = self.fieldsPair(self.mesh, self.survey)
# set initial fields
F[:, self._fieldType+'Solution', 0] = self.getInitialFields()
# timestep to solve forward
if self.verbose:
print('{}\nCalculating fields(m)\n{}'.format('*'*50, '*'*50))
Ainv = None
for tInd, dt in enumerate(self.timeSteps):
# keep factors if dt is the same as previous step b/c A will be the
# same
if Ainv is not None and (
tInd > 0 and dt != self.timeSteps[tInd - 1]
):
Ainv.clean()
Ainv = None
if Ainv is None:
A = self.getAdiag(tInd)
if self.verbose:
print('Factoring... (dt = {:e})'.format(dt))
Ainv = self.Solver(A, **self.solverOpts)
if self.verbose:
print('Done')
rhs = self.getRHS(tInd+1) # this is on the nodes of the time mesh
Asubdiag = self.getAsubdiag(tInd)
if self.verbose:
print(' Solving... (tInd = {:d})'.format(tInd+1))
# taking a step
sol = Ainv * (rhs - Asubdiag * F[:, (self._fieldType + 'Solution'),
tInd])
if self.verbose:
print(' Done...')
if sol.ndim == 1:
sol.shape = (sol.size, 1)
F[:, self._fieldType+'Solution', tInd+1] = sol
if self.verbose:
print('{}\nDone calculating fields(m)\n{}'.format('*'*50, '*'*50))
Ainv.clean()
return F
def Jvec(self, m, v, f=None):
"""
Jvec computes the sensitivity times a vector
.. math::
\mathbf{J} \mathbf{v} = \\frac{d\mathbf{P}}{d\mathbf{F}} \left(
\\frac{d\mathbf{F}}{d\mathbf{u}} \\frac{d\mathbf{u}}{d\mathbf{m}} +
\\frac{\partial\mathbf{F}}{\partial\mathbf{m}} \\right) \mathbf{v}
where
.. math::
\mathbf{A} \\frac{d\mathbf{u}}{d\mathbf{m}} +
\\frac{\partial \mathbf{A}(\mathbf{u}, \mathbf{m})}
{\partial\mathbf{m}} =
\\frac{d \mathbf{RHS}}{d \mathbf{m}}
"""
if f is None:
f = self.fields(m)
ftype = self._fieldType + 'Solution' # the thing we solved for
self.model = m
# mat to store previous time-step's solution deriv times a vector for
# each source
# size: nu x nSrc
# this is a bit silly
# if self._fieldType is 'b' or self._fieldType is 'j':
# ifields = np.zeros((self.mesh.nF, len(Srcs)))
# elif self._fieldType is 'e' or self._fieldType is 'h':
# ifields = np.zeros((self.mesh.nE, len(Srcs)))
# for i, src in enumerate(self.survey.srcList):
dun_dm_v = np.hstack([
Utils.mkvc(
self.getInitialFieldsDeriv(src, v, f=f), 2
)
for src in self.survey.srcList
])
# can over-write this at each timestep
# store the field derivs we need to project to calc full deriv
df_dm_v = Fields_Derivs(self.mesh, self.survey)
Adiaginv = None
for tInd, dt in zip(range(self.nT), self.timeSteps):
# keep factors if dt is the same as previous step b/c A will be the
# same
if Adiaginv is not None and (tInd > 0 and dt !=
self.timeSteps[tInd - 1]):
Adiaginv.clean()
Adiaginv = None
if Adiaginv is None:
A = self.getAdiag(tInd)
Adiaginv = self.Solver(A, **self.solverOpts)
Asubdiag = self.getAsubdiag(tInd)
for i, src in enumerate(self.survey.srcList):
# here, we are lagging by a timestep, so filling in as we go
for projField in set([rx.projField for rx in src.rxList]):
df_dmFun = getattr(f, '_%sDeriv' % projField, None)
# df_dm_v is dense, but we only need the times at
# (rx.P.T * ones > 0)
# This should be called rx.footprint
df_dm_v[src, '{}Deriv'.format(projField), tInd] = df_dmFun(
tInd, src, dun_dm_v[:, i], v
)
un_src = f[src, ftype, tInd+1]
# cell centered on time mesh
dA_dm_v = self.getAdiagDeriv(tInd, un_src, v)
# on nodes of time mesh
dRHS_dm_v = self.getRHSDeriv(tInd+1, src, v)
dAsubdiag_dm_v = self.getAsubdiagDeriv(
tInd, f[src, ftype, tInd], v
)
JRHS = dRHS_dm_v - dAsubdiag_dm_v - dA_dm_v
# step in time and overwrite
if tInd != len(self.timeSteps+1):
dun_dm_v[:, i] = Adiaginv * (
JRHS - Asubdiag * dun_dm_v[:, i]
)
Jv = []
for src in self.survey.srcList:
for rx in src.rxList:
Jv.append(
rx.evalDeriv(src, self.mesh, self.timeMesh, f, Utils.mkvc(
df_dm_v[src, '%sDeriv' % rx.projField, :]
)
)
)
Adiaginv.clean()
# del df_dm_v, dun_dm_v, Asubdiag
# return Utils.mkvc(Jv)
return np.hstack(Jv)
def Jtvec(self, m, v, f=None):
"""
Jvec computes the adjoint of the sensitivity times a vector
.. math::
\mathbf{J}^\\top \mathbf{v} = \left(
\\frac{d\mathbf{u}}{d\mathbf{m}} ^ \\top
\\frac{d\mathbf{F}}{d\mathbf{u}} ^ \\top +
\\frac{\partial\mathbf{F}}{\partial\mathbf{m}} ^ \\top \\right)
\\frac{d\mathbf{P}}{d\mathbf{F}} ^ \\top \mathbf{v}
where
.. math::
\\frac{d\mathbf{u}}{d\mathbf{m}} ^\\top \mathbf{A}^\\top +
\\frac{d\mathbf{A}(\mathbf{u})}{d\mathbf{m}} ^ \\top =
\\frac{d \mathbf{RHS}}{d \mathbf{m}} ^ \\top
"""
if f is None:
f = self.fields(m)
self.model = m
ftype = self._fieldType + 'Solution' # the thing we solved for
# Ensure v is a data object.
if not isinstance(v, self.dataPair):
v = self.dataPair(self.survey, v)
df_duT_v = Fields_Derivs(self.mesh, self.survey)
# same size as fields at a single timestep
ATinv_df_duT_v = np.zeros(
(
len(self.survey.srcList),
len(f[self.survey.srcList[0], ftype, 0])
),
dtype=float
)
JTv = np.zeros(m.shape, dtype=float)
# Loop over sources and receivers to create a fields object:
# PT_v, df_duT_v, df_dmT_v
# initialize storage for PT_v (don't need to preserve over sources)
PT_v = Fields_Derivs(self.mesh, self.survey)
for src in self.survey.srcList:
# Looping over initializing field class is appending memory!
# PT_v = Fields_Derivs(self.mesh, self.survey) # initialize storage
# #for PT_v (don't need to preserve over sources)
# initialize size
df_duT_v[src, '{}Deriv'.format(self._fieldType), :] = (
np.zeros_like(f[src, self._fieldType, :])
)
for rx in src.rxList:
PT_v[src, '{}Deriv'.format(rx.projField), :] = rx.evalDeriv(
src, self.mesh, self.timeMesh, f, Utils.mkvc(v[src, rx]),
adjoint=True
) # this is +=
# PT_v = np.reshape(curPT_v,(len(curPT_v)/self.timeMesh.nN,
# self.timeMesh.nN), order='F')
df_duTFun = getattr(f, '_{}Deriv'.format(rx.projField), None)
for tInd in range(self.nT+1):
cur = df_duTFun(
tInd, src, None, Utils.mkvc(
PT_v[src, '{}Deriv'.format(rx.projField), tInd]
),
adjoint=True
)
df_duT_v[src, '{}Deriv'.format(self._fieldType), tInd] = (
df_duT_v[src, '{}Deriv'.format(self._fieldType), tInd] +
Utils.mkvc(cur[0], 2))
JTv = cur[1] + JTv
del PT_v # no longer need this
AdiagTinv = None
# Do the back-solve through time
# if the previous timestep is the same: no need to refactor the matrix
# for tInd, dt in zip(range(self.nT), self.timeSteps):
for tInd in reversed(range(self.nT)):
# tInd = tIndP - 1
if AdiagTinv is not None and (
tInd <= self.nT and
self.timeSteps[tInd] != self.timeSteps[tInd+1]
):
AdiagTinv.clean()
AdiagTinv = None
# refactor if we need to
if AdiagTinv is None: # and tInd > -1:
Adiag = self.getAdiag(tInd)
AdiagTinv = self.Solver(Adiag.T, **self.solverOpts)
if tInd < self.nT - 1:
Asubdiag = self.getAsubdiag(tInd+1)
for isrc, src in enumerate(self.survey.srcList):
# solve against df_duT_v
if tInd >= self.nT-1:
# last timestep (first to be solved)
ATinv_df_duT_v[isrc, :] = AdiagTinv * df_duT_v[
src, '{}Deriv'.format(self._fieldType), tInd+1]
elif tInd > -1:
ATinv_df_duT_v[isrc, :] = AdiagTinv * (
Utils.mkvc(df_duT_v[
src, '{}Deriv'.format(self._fieldType), tInd+1
]
) - Asubdiag.T * Utils.mkvc(ATinv_df_duT_v[isrc, :]))
if tInd < self.nT:
dAsubdiagT_dm_v = self.getAsubdiagDeriv(
tInd, f[src, ftype, tInd], ATinv_df_duT_v[isrc, :],
adjoint=True)
else:
dAsubdiagT_dm_v = Utils.Zero()
dRHST_dm_v = self.getRHSDeriv(
tInd+1, src, ATinv_df_duT_v[isrc, :], adjoint=True
) # on nodes of time mesh
un_src = f[src, ftype, tInd+1]
# cell centered on time mesh
dAT_dm_v = self.getAdiagDeriv(
tInd, un_src, ATinv_df_duT_v[isrc, :], adjoint=True
)
JTv = JTv + Utils.mkvc(
-dAT_dm_v - dAsubdiagT_dm_v + dRHST_dm_v
)
# del df_duT_v, ATinv_df_duT_v, A, Asubdiag
if AdiagTinv is not None:
AdiagTinv.clean()
return Utils.mkvc(JTv).astype(float)
def getSourceTerm(self, tInd):
"""
Assemble the source term. This ensures that the RHS is a vector / array
of the correct size
"""
Srcs = self.survey.srcList
if self._formulation == 'EB':
s_m = np.zeros((self.mesh.nF, len(Srcs)))
s_e = np.zeros((self.mesh.nE, len(Srcs)))
elif self._formulation == 'HJ':
s_m = np.zeros((self.mesh.nE, len(Srcs)))
s_e = np.zeros((self.mesh.nF, len(Srcs)))
for i, src in enumerate(Srcs):
smi, sei = src.eval(self, self.times[tInd])
s_m[:, i] = s_m[:, i] + smi
s_e[:, i] = s_e[:, i] + sei
return s_m, s_e
def getInitialFields(self):
"""
Ask the sources for initial fields
"""
Srcs = self.survey.srcList
if self._fieldType in ['b', 'j']:
ifields = np.zeros((self.mesh.nF, len(Srcs)))
elif self._fieldType in ['e', 'h']:
ifields = np.zeros((self.mesh.nE, len(Srcs)))
for i, src in enumerate(Srcs):
ifields[:, i] = (
ifields[:, i] + getattr(
src, '{}Initial'.format(self._fieldType), None
)(self)
)
return ifields
def getInitialFieldsDeriv(self, src, v, adjoint=False, f=None):
if adjoint is False:
if self._fieldType in ['b', 'j']:
ifieldsDeriv = np.zeros(self.mesh.nF)
elif self._fieldType in ['e', 'h']:
ifieldsDeriv = np.zeros(self.mesh.nE)
elif adjoint is True:
ifieldsDeriv = np.zeros(self.mapping.nP)
ifieldsDeriv = (Utils.mkvc(
getattr(src, '{}InitialDeriv'.format(self._fieldType),
None)(self, v, adjoint, f)) + ifieldsDeriv
)
return ifieldsDeriv
###############################################################################
# #
# E-B Formulation #
# #
###############################################################################
# ------------------------------- Problem3D_b ------------------------------- #
class Problem3D_b(BaseTDEMProblem):
"""
Starting from the quasi-static E-B formulation of Maxwell's equations
(semi-discretized)
.. math::
\mathbf{C} \mathbf{e} + \\frac{\partial \mathbf{b}}{\partial t} =
\mathbf{s_m} \\\\
\mathbf{C}^{\\top} \mathbf{M_{\mu^{-1}}^f} \mathbf{b} -
\mathbf{M_{\sigma}^e} \mathbf{e} = \mathbf{s_e}
where :math:`\mathbf{s_e}` is an integrated quantity, we eliminate
:math:`\mathbf{e}` using
.. math::
\mathbf{e} = \mathbf{M_{\sigma}^e}^{-1} \mathbf{C}^{\\top}
\mathbf{M_{\mu^{-1}}^f} \mathbf{b} -
\mathbf{M_{\sigma}^e}^{-1} \mathbf{s_e}
to obtain a second order semi-discretized system in :math:`\mathbf{b}`
.. math::
\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{C}^{\\top}
\mathbf{M_{\mu^{-1}}^f} \mathbf{b} +
\\frac{\partial \mathbf{b}}{\partial t} =
\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{s_e} + \mathbf{s_m}
and moving everything except the time derivative to the rhs gives
.. math::
\\frac{\partial \mathbf{b}}{\partial t} =
-\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{C}^{\\top}
\mathbf{M_{\mu^{-1}}^f} \mathbf{b} +
\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{s_e} + \mathbf{s_m}
For the time discretization, we use backward euler. To solve for the
:math:`n+1` th time step, we have
.. math::
\\frac{\mathbf{b}^{n+1} - \mathbf{b}^{n}}{\mathbf{dt}} =
-\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{C}^{\\top}
\mathbf{M_{\mu^{-1}}^f} \mathbf{b}^{n+1} +
\mathbf{C} \mathbf{M_{\sigma}^e}^{-1} \mathbf{s_e}^{n+1} +
\mathbf{s_m}^{n+1}
re-arranging to put :math:`\mathbf{b}^{n+1}` on the left hand side gives
.. math::
(\mathbf{I} + \mathbf{dt} \mathbf{C} \mathbf{M_{\sigma}^e}^{-1}
\mathbf{C}^{\\top} \mathbf{M_{\mu^{-1}}^f}) \mathbf{b}^{n+1} =
\mathbf{b}^{n} + \mathbf{dt}(\mathbf{C} \mathbf{M_{\sigma}^e}^{-1}
\mathbf{s_e}^{n+1} + \mathbf{s_m}^{n+1})
"""
_fieldType = 'b'
_formulation = 'EB'
fieldsPair = Fields3D_b #: A SimPEG.EM.TDEM.Fields3D_b object
surveyPair = SurveyTDEM
def __init__(self, mesh, **kwargs):
BaseTDEMProblem.__init__(self, mesh, **kwargs)
def getAdiag(self, tInd):
"""
System matrix at a given time index
.. math::
(\mathbf{I} + \mathbf{dt} \mathbf{C} \mathbf{M_{\sigma}^e}^{-1}
\mathbf{C}^{\\top} \mathbf{M_{\mu^{-1}}^f})
"""
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]
C = self.mesh.edgeCurl
MeSigmaI = self.MeSigmaI
MfMui = self.MfMui
I = Utils.speye(self.mesh.nF)
A = 1./dt * I + (C * (MeSigmaI * (C.T * MfMui)))
if self._makeASymmetric is True:
return MfMui.T * A
return A
def getAdiagDeriv(self, tInd, u, v, adjoint=False):
"""
Derivative of ADiag
"""
C = self.mesh.edgeCurl
def MeSigmaIDeriv(x):
return self.MeSigmaIDeriv(x)
MfMui = self.MfMui
if adjoint:
if self._makeASymmetric is True:
v = MfMui * v
return MeSigmaIDeriv(C.T * (MfMui * u)).T * (C.T * v)
ADeriv = (C * (MeSigmaIDeriv(C.T * (MfMui * u)) * v))
if self._makeASymmetric is True:
return MfMui.T * ADeriv
return ADeriv
def getAsubdiag(self, tInd):
"""
Matrix below the diagonal
"""
dt = self.timeSteps[tInd]
MfMui = self.MfMui
Asubdiag = - 1./dt * sp.eye(self.mesh.nF)
if self._makeASymmetric is True:
return MfMui.T * Asubdiag
return Asubdiag
def getAsubdiagDeriv(self, tInd, u, v, adjoint=False):
return Utils.Zero() * v
def getRHS(self, tInd):
"""
Assemble the RHS
"""
C = self.mesh.edgeCurl
MeSigmaI = self.MeSigmaI
MfMui = self.MfMui
s_m, s_e = self.getSourceTerm(tInd)
rhs = (C * (MeSigmaI * s_e) + s_m)
if self._makeASymmetric is True:
return MfMui.T * rhs
return rhs
def getRHSDeriv(self, tInd, src, v, adjoint=False):
"""
Derivative of the RHS
"""
C = self.mesh.edgeCurl
MeSigmaI = self.MeSigmaI
def MeSigmaIDeriv(u):
return self.MeSigmaIDeriv(u)
MfMui = self.MfMui
_, s_e = src.eval(self, self.times[tInd])
s_mDeriv, s_eDeriv = src.evalDeriv(
self, self.times[tInd], adjoint=adjoint
)
if adjoint:
if self._makeASymmetric is True:
v = self.MfMui * v
if isinstance(s_e, Utils.Zero):
MeSigmaIDerivT_v = Utils.Zero()
else:
MeSigmaIDerivT_v = MeSigmaIDeriv(s_e).T * C.T * v
RHSDeriv = (
MeSigmaIDerivT_v + s_eDeriv( MeSigmaI.T * (C.T * v)) +
s_mDeriv(v)
)
return RHSDeriv
if isinstance(s_e, Utils.Zero):
MeSigmaIDeriv_v = Utils.Zero()
else:
MeSigmaIDeriv_v = MeSigmaIDeriv(s_e) * v
RHSDeriv = (
C * MeSigmaIDeriv_v + C * MeSigmaI * s_eDeriv(v) + s_mDeriv(v)
)
if self._makeASymmetric is True:
return self.MfMui.T * RHSDeriv
return RHSDeriv
# ------------------------------- Problem3D_e ------------------------------- #
class Problem3D_e(BaseTDEMProblem):
"""
Solve the EB-formulation of Maxwell's equations for the electric field, e.
Starting with
.. math::
\\nabla \\times \\mathbf{e} + \\frac{\\partial \\mathbf{b}}{\\partial t} = \\mathbf{s_m} \\
\\nabla \\times \mu^{-1} \\mathbf{b} - \sigma \\mathbf{e} = \\mathbf{s_e}
we eliminate :math:`\\frac{\\partial b}{\\partial t}` using
.. math::
\\frac{\\partial \\mathbf{b}}{\\partial t} = - \\nabla \\times \\mathbf{e} + \\mathbf{s_m}
taking the time-derivative of Ampere's law, we see
.. math::
\\frac{\\partial}{\\partial t}\left( \\nabla \\times \mu^{-1} \\mathbf{b} - \\sigma \\mathbf{e} \\right) = \\frac{\\partial \\mathbf{s_e}}{\\partial t} \\
\\nabla \\times \\mu^{-1} \\frac{\\partial \\mathbf{b}}{\\partial t} - \\sigma \\frac{\\partial\\mathbf{e}}{\\partial t} = \\frac{\\partial \\mathbf{s_e}}{\\partial t}
which gives us
.. math::
\\nabla \\times \\mu^{-1} \\nabla \\times \\mathbf{e} + \\sigma \\frac{\\partial\\mathbf{e}}{\\partial t} = \\nabla \\times \\mu^{-1} \\mathbf{s_m} + \\frac{\\partial \\mathbf{s_e}}{\\partial t}
"""
_fieldType = 'e'
_formulation = 'EB'
fieldsPair = Fields3D_e #: A Fields3D_e
surveyPair = SurveyTDEM
Adcinv = None
def __init__(self, mesh, **kwargs):
BaseTDEMProblem.__init__(self, mesh, **kwargs)
def Jtvec(self, m, v, f=None):
"""
Jvec computes the adjoint of the sensitivity times a vector
"""
if f is None:
f = self.fields(m)
self.model = m
ftype = self._fieldType + 'Solution' # the thing we solved for
# Ensure v is a data object.
if not isinstance(v, self.dataPair):
v = self.dataPair(self.survey, v)
df_duT_v = Fields_Derivs(self.mesh, self.survey)
# same size as fields at a single timestep
ATinv_df_duT_v = np.zeros(
(
len(self.survey.srcList),
len(f[self.survey.srcList[0], ftype, 0])
),
dtype=float
)
JTv = np.zeros(m.shape, dtype=float)
# Loop over sources and receivers to create a fields object:
# PT_v, df_duT_v, df_dmT_v
# initialize storage for PT_v (don't need to preserve over sources)
PT_v = Fields_Derivs(self.mesh, self.survey)
for src in self.survey.srcList:
# Looping over initializing field class is appending memory!
# PT_v = Fields_Derivs(self.mesh, self.survey) # initialize storage
# #for PT_v (don't need to preserve over sources)
# initialize size
df_duT_v[src, '{}Deriv'.format(self._fieldType), :] = (
np.zeros_like(f[src, self._fieldType, :])
)
for rx in src.rxList:
PT_v[src, '{}Deriv'.format(rx.projField), :] = rx.evalDeriv(
src, self.mesh, self.timeMesh, f, Utils.mkvc(v[src, rx]),
adjoint=True
)
# this is +=
# PT_v = np.reshape(curPT_v,(len(curPT_v)/self.timeMesh.nN,
# self.timeMesh.nN), order='F')
df_duTFun = getattr(f, '_{}Deriv'.format(rx.projField), None)
for tInd in range(self.nT+1):
cur = df_duTFun(
tInd, src, None, Utils.mkvc(
PT_v[src, '{}Deriv'.format(rx.projField), tInd]
),
adjoint=True
)
df_duT_v[src, '{}Deriv'.format(self._fieldType), tInd] = (
df_duT_v[src, '{}Deriv'.format(self._fieldType), tInd]
+ Utils.mkvc(cur[0], 2)
)
JTv = cur[1] + JTv
# no longer need this
del PT_v
AdiagTinv = None
# Do the back-solve through time
# if the previous timestep is the same: no need to refactor the matrix
# for tInd, dt in zip(range(self.nT), self.timeSteps):
for tInd in reversed(range(self.nT)):
# tInd = tIndP - 1
if AdiagTinv is not None and (
tInd <= self.nT and
self.timeSteps[tInd] != self.timeSteps[tInd+1]
):
AdiagTinv.clean()
AdiagTinv = None
# refactor if we need to
if AdiagTinv is None: # and tInd > -1:
Adiag = self.getAdiag(tInd)
AdiagTinv = self.Solver(Adiag.T, **self.solverOpts)
if tInd < self.nT - 1:
Asubdiag = self.getAsubdiag(tInd+1)
for isrc, src in enumerate(self.survey.srcList):
# solve against df_duT_v
if tInd >= self.nT-1:
# last timestep (first to be solved)
ATinv_df_duT_v[isrc, :] = AdiagTinv * df_duT_v[
src, '{}Deriv'.format(self._fieldType), tInd+1]
elif tInd > -1:
ATinv_df_duT_v[isrc, :] = AdiagTinv * (
Utils.mkvc(df_duT_v[
src, '{}Deriv'.format(self._fieldType), tInd+1
]
) - Asubdiag.T * Utils.mkvc(ATinv_df_duT_v[isrc, :]))
dAsubdiagT_dm_v = self.getAsubdiagDeriv(
tInd, f[src, ftype, tInd], ATinv_df_duT_v[isrc, :],
adjoint=True)
dRHST_dm_v = self.getRHSDeriv(
tInd+1, src, ATinv_df_duT_v[isrc, :], adjoint=True
) # on nodes of time mesh
un_src = f[src, ftype, tInd+1]
# cell centered on time mesh
dAT_dm_v = self.getAdiagDeriv(
tInd, un_src, ATinv_df_duT_v[isrc, :], adjoint=True
)
JTv = JTv + Utils.mkvc(
-dAT_dm_v - dAsubdiagT_dm_v + dRHST_dm_v
)
# Treating initial condition when a galvanic source is included
tInd = -1
Grad = self.mesh.nodalGrad
for isrc, src in enumerate(self.survey.srcList):
if src.srcType == "Galvanic":
ATinv_df_duT_v[isrc, :] = Grad*(self.Adcinv*(Grad.T*(
Utils.mkvc(df_duT_v[
src, '{}Deriv'.format(self._fieldType), tInd+1
]
) - Asubdiag.T * Utils.mkvc(ATinv_df_duT_v[isrc, :]))
))
dRHST_dm_v = self.getRHSDeriv(
tInd+1, src, ATinv_df_duT_v[isrc, :], adjoint=True
) # on nodes of time mesh
un_src = f[src, ftype, tInd+1]
# cell centered on time mesh
dAT_dm_v = (
self.MeSigmaDeriv(un_src).T * ATinv_df_duT_v[isrc, :]
)
JTv = JTv + Utils.mkvc(
-dAT_dm_v + dRHST_dm_v
)
# del df_duT_v, ATinv_df_duT_v, A, Asubdiag
if AdiagTinv is not None:
AdiagTinv.clean()
return Utils.mkvc(JTv).astype(float)
def getAdiag(self, tInd):
"""
Diagonal of the system matrix at a given time index
"""
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]
C = self.mesh.edgeCurl
MfMui = self.MfMui
MeSigma = self.MeSigma
return C.T * (MfMui * C)+1./dt * MeSigma
def getAdiagDeriv(self, tInd, u, v, adjoint=False):
"""
Deriv of ADiag with respect to electrical conductivity
"""
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]
MeSigmaDeriv = self.MeSigmaDeriv(u)
if adjoint:
return 1./dt * MeSigmaDeriv.T * v
return 1./dt * MeSigmaDeriv * v
def getAsubdiag(self, tInd):
"""
Matrix below the diagonal
"""
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]
return - 1./dt * self.MeSigma
def getAsubdiagDeriv(self, tInd, u, v, adjoint=False):
"""
Derivative of the matrix below the diagonal with respect to electrical
conductivity
"""
dt = self.timeSteps[tInd]
if adjoint:
return - 1./dt * self.MeSigmaDeriv(u).T * v
return - 1./dt * self.MeSigmaDeriv(u) * v
def getRHS(self, tInd):
"""
right hand side
"""
# Omit this: Note input was tInd+1
# if tInd == len(self.timeSteps):
# tInd = tInd - 1
dt = self.timeSteps[tInd-1]
s_m, s_e = self.getSourceTerm(tInd)
_, s_en1 = self.getSourceTerm(tInd-1)
return (-1./dt * (s_e - s_en1) +
self.mesh.edgeCurl.T * self.MfMui * s_m)
def getRHSDeriv(self, tInd, src, v, adjoint=False):
# right now, we are assuming that s_e, s_m do not depend on the model.
return Utils.Zero()
def getInitialFields(self):
"""
Ask the sources for initial fields
"""
Srcs = self.survey.srcList
if self._fieldType in ['b', 'j']:
ifields = np.zeros((self.mesh.nF, len(Srcs)))
elif self._fieldType in ['e', 'h']:
ifields = np.zeros((self.mesh.nE, len(Srcs)))
if self.verbose:
print ("Calculating Initial fields")
for i, src in enumerate(Srcs):
# Check if the source is grounded
if src.srcType == "Galvanic" and src.waveform.hasInitialFields:
# Check self.Adcinv and clean
if self.Adcinv is not None:
self.Adcinv.clean()
# Factorize Adc matrix
if self.verbose:
print ("Factorize system matrix for DC problem")
Adc = self.getAdc()
self.Adcinv = self.Solver(Adc)
ifields[:, i] = (
ifields[:, i] + getattr(
src, '{}Initial'.format(self._fieldType), None
)(self)
)
return ifields
def getAdc(self):
MeSigma = self.MeSigma
Grad = self.mesh.nodalGrad
Adc = Grad.T * MeSigma * Grad
# Handling Null space of A
Adc[0, 0] = Adc[0, 0] + 1.
return Adc
def getAdcDeriv(self, u, v, adjoint=False):
Grad = self.mesh.nodalGrad
if not adjoint:
return Grad.T*(self.MeSigmaDeriv(-u)*v)
elif adjoint:
return self.MeSigmaDeriv(-u).T * (Grad*v)
return Adc
def clean(self):
"""
Clean factors
"""
if self.Adcinv is not None:
self.Adcinv.clean()
###############################################################################
# #
# H-J Formulation #
# #
###############################################################################
# ------------------------------- Problem3D_h ------------------------------- #
class Problem3D_h(BaseTDEMProblem):
"""
Solve the H-J formulation of Maxwell's equations for the magnetic field h.
We start with Maxwell's equations in terms of the magnetic field and
current density
.. math::
\\nabla \\times \\rho \\mathbf{j} + \\mu \\frac{\\partial h}{\\partial t} = \\mathbf{s_m} \\
\\nabla \\times \\mathbf{h} - \\mathbf{j} = \\mathbf{s_e}
and eliminate :math:`\\mathbf{j}` using
.. math::
\\mathbf{j} = \\nabla \\times \\mathbf{h} - \\mathbf{s_e}
giving
.. math::
\\nabla \\times \\rho \\nabla \\times \\mathbf{h} + \\mu \\frac{\\partial h}{\\partial t} = \\nabla \\times \\rho \\mathbf{s_e} + \\mathbf{s_m}
"""
_fieldType = 'h'
_formulation = 'HJ'
fieldsPair = Fields3D_h #: Fields object pair
surveyPair = SurveyTDEM
def __init__(self, mesh, **kwargs):
BaseTDEMProblem.__init__(self, mesh, **kwargs)
def getAdiag(self, tInd):
"""
System matrix at a given time index
"""
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]
C = self.mesh.edgeCurl
MfRho = self.MfRho
MeMu = self.MeMu
return C.T * ( MfRho * C ) + 1./dt * MeMu
def getAdiagDeriv(self, tInd, u, v, adjoint=False):
assert tInd >= 0 and tInd < self.nT
dt = self.timeSteps[tInd]