File: MTModels.py

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (286 lines) | stat: -rw-r--r-- 10,729 bytes parent folder | download | duplicates (3)
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
__copyright__ = "Copyright (c) 2020 by University of Queensland http://www.uq.edu.au"
__license__   = "Licensed under the Apache License, version 2.0 http://www.apache.org/licenses/LICENSE-2.0"
__credits__   = "Lutz Gross, Andrea Codd"

from esys.escript import *
from esys.escript.linearPDEs import LinearSinglePDE, SolverOptions
from esys.escript.pdetools import Locator
import cmath
import numpy as np

class MT2DTEModel(object):
    """
    This class is a simple wrapper for 2D MT PDE model in the TE mode.
    MT  
        curl ((1/sigma) curl H) + i omega H = 0
        curl ((1/mu) curl E) + i omega sigma E = 0
    
    2D reduces to 
        -div (1/mu grad u) + i omega sigma u = 0
    where 
        u = Ex is transverse component of electric field 
        mu is magnetic permeability
        sigma is electrical conductivity
        omega is angular frequency
        i = sqrt(-1)   
 
    Domain typically includes air and ground layers.
    Conductivity sigma = 0 in the air layer.
        
    Boundary conditions included in the class are  
       - Ex is set to one at the top of the domain, typically at the top of an air layer.
       - At the bottom of the domain Ex=0 (set `fixBottom`=True) 
         or radiation condition dEx/dn+k*Ex=0 with k^2=2*pi*f*mu*sigma is set

    It has a function to set ground property
       - setConductivity
    and functions to output solutions
       - getImpedance
       - getApparentResitivity
       - getPhase. 

    """

    def __init__(self, domain, fixBottom=False, useFastSolver=False,  mu=4*np.pi*1e-7):
        """
        :param domain: the domain 
        :type domain: `Domain`
        :param fixBottom: if true the electric field at the bottom is set to zero. 
            Otherwise radiation condition is set.
        :type fixBottom: `bool`
        :param useFastSolver: use multigrid solver. (not supported yet)
        :type useFastSolver: `bool`
        """
        self.domain=domain
        self.mu=mu
        self.sigma=None
        self.sigma_boundary=None
        self.fixBottom=fixBottom
        self.useFastSolver=False
        self.pde=self.__createPDE()

    def __createPDE(self):
        """
        Create the PDE and set boundary conditions.
        """
        pde=LinearSinglePDE(self.domain, isComplex=True,)
        optionsG=pde.getSolverOptions()
        optionsG.setSolverMethod(SolverOptions.DIRECT)
        pde.setSymmetryOn()
        if self.useFastSolver and hasFeature('trilinos'): # ignored for now!
            optionsG.setPackage(SolverOptions.TRILINOS)
            optionsG.setPreconditioner(SolverOptions.AMG)
        pde.setValue(A=kronecker(self.domain.getDim()))
        z=self.domain.getX()[self.domain.getDim()-1]
        t=sup(z)
        if self.fixBottom:
            b=inf(z)
            pde.setValue(q=whereZero(z-t)+whereZero(z-b), r=(z-b)/(t-b))
        else:
            pde.setValue(q=whereZero(z-t), r=1.)
        return pde
    
    def setConductivity(self, sigma, sigma_boundary=None):
        """
        sets the conductivity `sigma`. 
        
        :param sigma: conductivity distribution. 
        :type sigma: `Data` or `float`
        :param sigma_boundary: conductivity distribution on bottom boundary. 
           Only required if fixBottom is not set and `sigma` cannot be 
           interpolated to boundary.
        :type sigma: `Data` or `float`
        """
        self.sigma=interpolate(sigma, Function(self.domain))
        if not self.fixBottom:
            if sigma_boundary:
                self.sigma_boundary=interpolate(sigma_boundary, FunctionOnBoundary(self.domain))
            else:
                self.sigma_boundary=interpolate(sigma, FunctionOnBoundary(self.domain))
        return self
        
    def getImpedance(self, f=1.):
        """
        return the impedance Zxy for frequency `f` in [Hz]. The electric field
        and magnetic field cane be accessed as attributes `Ex` and `Hy` after 
        completion.
        
        :param f: frequency in [Hz]
        :type f: `float`
        :returns: Zxy
        """
        o=2*np.pi*f
        self.pde.setValue(D=1j*o*self.mu*self.sigma)
        if not self.fixBottom:
            z=FunctionOnBoundary(self.domain).getX()[self.domain.getDim()-1]
            b=inf(z)
            k=(1+1j)*sqrt(o*self.mu*self.sigma_boundary/2)
            self.pde.setValue(d=k*whereZero(z-b))
        Ex=self.pde.getSolution()
        g=grad(Ex, ReducedFunction(self.domain))
        self.Hy=-1./(1j*o*self.mu)*g[self.domain.getDim()-1]
        self.Ex=interpolate(Ex, self.Hy.getFunctionSpace())
        Zxy=self.Ex/self.Hy
        return Zxy
    
    def getApparentResitivity(self, f, Zxy):
        """
        return the apparent resistivity from a given frequency `f` and impedance `Zxy`

        :param f: frequency in [Hz]
        :type f: `float`
        :param Zxy: impedance
        :type Zxy: `Data` or `np.array`
        """
        o=2*np.pi*f
        return abs(Zxy)**2/(self.mu*o)

    def getPhase(self, f, Zxy):
        """
        return the phase in [deg] from a given frequency `f` and impedance `Zxy`
        
        :param f: frequency in [Hz]
        :type f: `float`
        :param Zxy: impedance
        :type Zxy: `Data` or `np.array`
        """
        return atan2(Zxy.imag(),Zxy.real())/np.pi*180

class MT2DTMModel(object):
    """
    This a class for solving the 2D MT model in the TM mode.
    MT  
        curl ((1/sigma)curl H) + i omega H = 0
        curl ((1/mu)curl E) + i omega sigma E = 0
    
    2D 
        -div (1/sigma grad u) + i omega mu u = 0
    where 
        u = Hx is transverse component of magnetic field
        mu is magnetic permeability
        sigma is electrical conductivity
        omega is angular frequency
        i = sqrt(-1)    
    
    Hx is set to one in the air layer and the interface of the air layer and the subsurface.
    At the bottom of the domain Ex=0 (set `fixBottom`=True) 
       or radiation condition dEx/dn+k*Ex=0 with k^2=2*pi*f*mu*sigma is set.
    
    """
    def __init__(self, domain, fixBottom=False, airLayer=None, useFastSolver=False,  mu=4*np.pi*1e-7):
        """
        :param domain: the domain
        :type domain: `Domain`
        :param fixBottom: if true the potential at all faces except the top is set to zero. 
            Otherwise on the the bottom is set to zero.
        :type fixBottom: `bool`
        :param airLayer: defines the air layer including the interface between air layer and subsurface. 
             If set to `None` then just the (plane) top surface is used. 
             If set to a `float` then the region above `airlayer` (including the interface) 
                is defined as air layer. 
             Otherwise `airlayer` needs to be defined as `Data` with value `1` marking 
                 the air layer and its interface.  
        :type airLayer: `None`, `float`, `Data`
        :param useFastSolver: use multigrid solver. This may fail.
        :type useFastSolver: `bool`
        
        :note: the attribute `airLayer` gives the mask for the air layer 
               including the interface between the air layer and the subsurface.
        """
        self.domain=domain
        self.mu=mu
        self.rho=None
        self.rho_boundary=None

        self.fixBottom=fixBottom
        self.useFastSolver=False
        self.pde=self.__createPDE(airLayer)

    def __createPDE(self, airLayer=None):
        pde=LinearSinglePDE(self.domain, isComplex=True,)
        optionsG=pde.getSolverOptions()
        optionsG.setSolverMethod(SolverOptions.DIRECT)
        pde.setSymmetryOn()
        if self.useFastSolver and hasFeature('trilinos'): # ignored for now!
            optionsG.setPackage(SolverOptions.TRILINOS)
            optionsG.setPreconditioner(SolverOptions.AMG)
            optionsG.setTrilinosParameter("problem: symmetric", True)
        z=self.domain.getX()[self.domain.getDim()-1]
        b=inf(z)        
        if airLayer is None:
            self.airLayer=whereNonNegative(z-sup(z))
        elif isinstance(airLayer, float) or  isinstance(airLayer, int):
            self.airLayer=whereNonNegative(z-airLayer)
        else:
            self.airLayer=wherePositive(interpolate(airLayer, Solution(self.domain)))
        if self.fixBottom:
            pde.setValue(q=self.airLayer+whereZero(z-b), r=self.airLayer)
        else:
            pde.setValue(q=self.airLayer, r=self.airLayer)
        return pde
    
    def setResistivity(self, rho, rho_boundary=None):
        """
        sets the resistivity. 
        :param rho: resistivity distribution. 
        :type rho: `Data`
        :param rho_boundary: rho distribution on bottom boundary. Only required if fixBottom is set and
                               `rho` cannot be interpolated to boundary.
        :type sigma: `Data`
        """
        self.rho=interpolate(rho, Function(self.domain))
        if not self.fixBottom:
            if rho_boundary:
                self.rho_boundary=interpolate(rho_boundary, FunctionOnBoundary(self.domain))
            else:
                self.rho_boundary=interpolate(rho, FunctionOnBoundary(self.domain))

        self.pde.setValue(A=self.rho*kronecker(self.domain.getDim()))        
        return self
        
    def getImpedance(self, f=1.):
        """
        return the impedance Zyx and the electric field Ex for frequency `f` 
        
        :param f: frequency in [Hz]
        :type f: `float`
        :returns: Zyx
        """
        o=2*np.pi*f
        self.pde.setValue(D=1j*o*self.mu)
        if not self.fixBottom:
            z=FunctionOnBoundary(self.domain).getX()[self.domain.getDim()-1]
            b=inf(z)
            k=(1+1j)*sqrt(o*self.mu*self.rho_boundary/2)
            self.pde.setValue(d=k*whereZero(z-b))
        Hx=self.pde.getSolution()
        g=grad(Hx, ReducedFunction(self.domain))
        
        self.Ey=self.rho*g[self.domain.getDim()-1]
        self.Hxi=interpolate(Hx, self.Ey.getFunctionSpace())
        Zyx=self.Ey/self.Hxi
        return Zyx
    
    def getApparentResitivity(self, f, Zyx):
        """
        return the apparent resistivity from a given frequency `f` and impedance `Zyx`

        :param f: frequency in Hz
        :type f: `float`
        :param Zyx: impedance
        :type Zyx: `Data` or `np.array`
        """
        o=2*np.pi*f
        return abs(Zyx)**2/(self.mu*o)

    def getPhase(self, f, Zyx):
        """
        return the phase in [deg] from a given frequency f [Hz] and impedance Zyx

        :param f: frequency in Hz
        :type f: `float`
        :param Zyx: impedance
        :type Zyx: `Data` or `np.array`
        """
        return atan2(Zyx.imag(),Zyx.real())/np.pi*180