/[escript]/trunk/downunder/test/python/run_forward.py
ViewVC logotype

Annotation of /trunk/downunder/test/python/run_forward.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5057 - (hide annotations)
Fri Jun 20 01:53:38 2014 UTC (4 years, 10 months ago) by gross
File MIME type: text/x-python
File size: 14061 byte(s)
some fixes for MT-TE
1 gross 4688
2     ##############################################################################
3     #
4     # Copyright (c) 2003-2014 by University of Queensland
5     # http://www.uq.edu.au
6     #
7     # Primary Business: Queensland, Australia
8     # Licensed under the Open Software License version 3.0
9     # http://www.opensource.org/licenses/osl-3.0.php
10     #
11     # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12     # Development 2012-2013 by School of Earth Sciences
13     # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14     #
15     ##############################################################################
16    
17     __copyright__="""Copyright (c) 2003-2014 by University of Queensland
18     http://www.uq.edu.au
19     Primary Business: Queensland, Australia"""
20     __license__="""Licensed under the Open Software License version 3.0
21     http://www.opensource.org/licenses/osl-3.0.php"""
22     __url__="https://launchpad.net/escript-finley"
23    
24     import logging
25 jfenwick 4938 import esys.escriptcore.utestselect as unittest
26 sshaw 4984 from esys.escriptcore.testing import *
27 gross 4688 import numpy as np
28     import os
29     import sys
30 gross 4979 import cmath
31 gross 4688 from esys.downunder import *
32     from esys.escript import unitsSI as U
33     from esys.escript import *
34     from esys.weipa import saveSilo
35     from esys.escript.linearPDEs import LinearSinglePDE, LinearPDE
36 jfenwick 4713 from esys.escript import getEscriptParamInt
37 gross 4688
38     mpisize = getMPISizeWorld()
39     # this is mainly to avoid warning messages
40     logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
41    
42     try:
43     TEST_DATA_ROOT=os.environ['DOWNUNDER_TEST_DATA_ROOT']
44     except KeyError:
45     TEST_DATA_ROOT='ref_data'
46    
47 gross 5057 print TEST_DATA_ROOT
48 gross 4688 try:
49     WORKDIR=os.environ['DOWNUNDER_WORKDIR']
50     except KeyError:
51     WORKDIR='.'
52 jfenwick 4713
53 gross 4688
54 jfenwick 4713 have_direct=getEscriptParamInt("PASO_DIRECT")
55 gross 4688
56    
57 jfenwick 4713 @unittest.skipIf(mpisize>1 or have_direct!=1, "more than 1 MPI rank or missing direct solver")
58 gross 4688 class TestAcousticInversion(unittest.TestCase):
59     def test_API(self):
60     from esys.ripley import Rectangle
61     domain=Rectangle(20,20, diracPoints=[(0.5,1.)], diracTags=['sss'])
62     omega=2.
63    
64    
65     data=Data([1,2], FunctionOnBoundary(domain))
66     F=Data([2,3], Function(domain))
67     w=1.
68     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, data, 1.) # F is a scalar
69     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, [1,2], F) # data is not Data
70     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, Data([1,2], Function(domain)), F) # data is not on boundary
71     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, Scalar(1, Function(domain)), F) # data is not of shape (2,)
72     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, [1,2], data, F) # w is not a scalar
73     self.assertRaises(ValueError, AcousticWaveForm, domain, omega, Scalar(1, Function(domain)), data, F) # w is not a scalar
74    
75     # now we do a real one
76     acw=AcousticWaveForm(domain, omega, w, data, F)
77     self.assertEqual(acw.getDomain(), domain)
78     pde=acw.setUpPDE()
79     self.assertIsInstance(pde, LinearPDE)
80     self.assertEqual(pde.getNumEquations(), 2)
81     self.assertEqual(pde.getNumSolutions(), 2)
82     self.assertEqual(pde.getDomain(), domain)
83    
84    
85     def test_numeric2DscaleF(self):
86    
87     from esys.ripley import Rectangle
88     domain=Rectangle(100,100, diracPoints=[(0.5,1.)], diracTags=['sss'])
89     omega=2.
90    
91     # test solution is u = a * z where a is complex
92     a=complex(3.45, 0.56)
93     sigma=complex(1e-3, 0.056)
94    
95    
96     data=Data([a.real, a.imag], FunctionOnBoundary(domain))
97     mydata=data.copy()
98    
99     z=FunctionOnBoundary(domain).getX()[1]
100     w=whereZero(z-1.)
101     # source:
102     F=Data( [1,0],Function(domain))
103     #
104     acw=AcousticWaveForm(domain, omega, w, data, F, coordinates=None, fixAtBottom=False, tol=1e-8, saveMemory=True, scaleF=True)
105     # check rescaled data
106     surv=acw.getSurvey()
107     self.assertAlmostEqual( integrate(length(surv[0])**2 * surv[1]), 1.)
108    
109     mydata_scale=sqrt( integrate(w*length(mydata)**2) )
110     self.assertAlmostEqual( acw.getSourceScaling(z*[1, 0.]) , a/mydata_scale )
111     self.assertAlmostEqual( acw.getSourceScaling(mydata) , 1./mydata_scale )
112    
113     # this should be zero:
114     sigma_comps=[sigma.real, sigma.imag]
115     args=acw.getArguments(sigma_comps)
116     d=acw.getDefect(sigma_comps, *args)
117     self.assertTrue(isinstance(d, float))
118     self.assertTrue(abs(d) < 1e-10)
119    
120     dg=acw.getGradient(sigma_comps, *args)
121     self.assertTrue(isinstance(dg, Data))
122     self.assertTrue(dg.getShape()==(2,))
123     self.assertTrue(dg.getFunctionSpace()==Solution(domain))
124     self.assertTrue(Lsup(dg) < 1e-10)
125    
126     # this shuld be zero' too
127     sigma_comps=[2*sigma.real, sigma.imag/2.]
128     args=acw.getArguments(sigma_comps)
129     d=acw.getDefect(sigma_comps, *args)
130     self.assertTrue(isinstance(d, float))
131     self.assertTrue(abs(d)< 1e-10)
132    
133     dg=acw.getGradient(sigma_comps, *args)
134     self.assertTrue(isinstance(dg, Data))
135     self.assertTrue(dg.getShape()==(2,))
136     self.assertTrue(dg.getFunctionSpace()==Solution(domain))
137     self.assertTrue(Lsup(dg) < 1e-10)
138    
139     # this shouldn't be zero:
140     sigma0=[2*sigma.real, 10*a.imag]*(27*Function(domain).getX()[0]-Function(domain).getX()[1])
141     args=acw.getArguments(sigma0)
142     d0=acw.getDefect(sigma0, *args)
143     self.assertTrue(isinstance(d0, float))
144     self.assertTrue(d0 >= 0)
145     self.assertTrue(d0 > 1e-10)
146    
147     dg0=acw.getGradient(sigma0, *args)
148     self.assertTrue(isinstance(dg0, Data))
149     self.assertTrue(dg0.getShape()==(2,))
150     self.assertTrue(dg0.getFunctionSpace()==Solution(domain))
151     self.assertTrue(Lsup(dg0) > 1e-10)
152    
153     # test the gradient numerrically:
154     h=0.002
155     X=Function(domain).getX()
156     # .. increment:
157     p=h*exp(-(length(X-[0.6,0.6])/10)**2)*Lsup(length(sigma0))
158    
159    
160     sigma1=sigma0+p*[1,0]
161     args=acw.getArguments(sigma1)
162     d1=acw.getDefect(sigma1, *args)
163     self.assertTrue( abs( d1-d0-integrate(dg0[0]*p) ) < 1e-2 * abs(d1-d0) )
164    
165     sigma2=sigma0+p*[0,1]
166     args=acw.getArguments(sigma2)
167     d2=acw.getDefect(sigma2, *args)
168     self.assertTrue( abs(d2-d0-integrate(dg0[1]*p)) < 1e-2 * abs(d2-d0) )
169    
170     def test_numeric2DnoscaleF(self):
171    
172     from esys.ripley import Rectangle
173     domain=Rectangle(10,20, diracPoints=[(0.5,1.)], diracTags=['sss'])
174     omega=1.5
175    
176     # test solution is u = a * z where a is complex
177     a=complex(3.45, 0.56)
178     sigma=complex(1e-3, 0.056)
179    
180    
181     data=Data([a.real, a.imag], FunctionOnBoundary(domain))
182     z=FunctionOnBoundary(domain).getX()[1]
183     w=whereZero(z-1.)
184     # F = - a*omega* sigma
185     F=Data( [-(a*omega**2*sigma).real, -(a*omega**2*sigma).imag ],Function(domain))
186    
187     acw=AcousticWaveForm(domain, omega, w, data, F, coordinates=None, fixAtBottom=False, tol=1e-8, saveMemory=True, scaleF=False)
188     # this should be zero:
189     sigma_comps=[sigma.real, sigma.imag]
190     args=acw.getArguments(sigma_comps)
191     d=acw.getDefect(sigma_comps, *args)
192     self.assertTrue(isinstance(d, float))
193 jfenwick 4719 self.assertTrue(Lsup(d) < 1e-10)
194     #self.assertTrue(d >= 0)
195     #self.assertTrue(d < 1e-10)
196 gross 4688
197     dg=acw.getGradient(sigma_comps, *args)
198    
199     self.assertTrue(isinstance(dg, Data))
200     self.assertTrue(dg.getShape()==(2,))
201     self.assertTrue(dg.getFunctionSpace()==Solution(domain))
202     self.assertTrue(Lsup(dg) < 5e-10)
203     # this shouldn't be zero:
204     sigma0=Data([2*sigma.real, sigma.imag/2], Function(domain) )
205     args=acw.getArguments(sigma0)
206     d0=acw.getDefect(sigma0, *args)
207     self.assertTrue(isinstance(d0, float))
208     self.assertTrue(d0 >= 0)
209     self.assertTrue(d0 > 1e-10)
210    
211     dg0=acw.getGradient(sigma0, *args)
212     self.assertTrue(isinstance(dg0, Data))
213     self.assertTrue(dg0.getShape()==(2,))
214     self.assertTrue(dg0.getFunctionSpace()==Solution(domain))
215     self.assertTrue(Lsup(dg0) > 1e-10)
216     # test the gradient numerrically:
217     h=0.001
218     X=Function(domain).getX()
219     p=h*sin(length(X)*np.pi)*Lsup(length(sigma0))
220    
221     sigma1=sigma0+p*[1,0]
222     args=acw.getArguments(sigma1)
223     d1=acw.getDefect(sigma1, *args)
224    
225     self.assertTrue( abs( d1-d0-integrate(dg0[0]*p) ) < 1e-2 * abs(d1-d0) )
226    
227     sigma2=sigma0+p*[0,1]
228     args=acw.getArguments(sigma2)
229     d2=acw.getDefect(sigma2, *args)
230     self.assertTrue( abs(d2-d0-integrate(dg0[1]*p)) < 1e-2 * abs(d2-d0) )
231    
232 gross 4979 class TestMT2DModelTEMode(unittest.TestCase):
233     def test_API(self):
234     from esys.ripley import Rectangle
235     domain=Rectangle(20,20)
236     omega=2.
237     x=[ [0.2,0.5], [0.3,0.5] ]
238     Z_XY=[ complex(1.2,1.5), complex(1.3,2.5) ]
239     eta=1.
240     w0=1.
241     E_x0=1.
242     # now we do a real one
243     acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta, w0=w0, E_x0=E_x0)
244     self.assertEqual(acw.getDomain(), domain)
245     pde=acw.setUpPDE()
246     self.assertIsInstance(pde, LinearPDE)
247     self.assertEqual(pde.getNumEquations(), 2)
248     self.assertEqual(pde.getNumSolutions(), 2)
249     self.assertEqual(pde.getDomain(), domain)
250    
251     # other things that should work
252     acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta=[1.,1.], w0=[2.,3.], E_x0=complex(4.5,6) )
253    
254     # these shouldn't work
255     self.assertRaises(ValueError, MT2DModelTEMode, domain, omega, x, [3.], eta=[1.,1.], w0=[2.,3.], E_x0=complex(4.5,6) )
256     self.assertRaises(ValueError, MT2DModelTEMode, domain, omega, x, Z_XY, eta=[1.], w0=[2.,3.], E_x0=complex(4.5,6) )
257     self.assertRaises(ValueError, MT2DModelTEMode, domain, omega, [(6.7,5)], Z_XY, eta=[1.,1.], w0=[2.,3.], E_x0=complex(4.5,6) )
258    
259     def test_PDE(self):
260    
261     omega=2.
262     mu0=0.123
263     SIGMA=15.
264     k=cmath.sqrt(1j*omega*mu0*SIGMA) # Ex=exp(k*z)
265    
266     from esys.ripley import Rectangle
267     domain=Rectangle(200,200)
268    
269    
270 gross 5057 IMP=cmath.sqrt(1j*omega*mu0/SIGMA)
271 gross 4980 Z_XY=[ IMP, IMP ]
272 gross 4979 x=[ [0.3,0.5], [0.6,0.5] ]
273 gross 4980 eta=0.005
274 gross 4979 z=domain.getX()[1]
275     Ex0_ex=exp(-k.real*z)*cos(-k.imag*z)
276     Ex0_ex_z=(-k.real*cos(-k.imag*z)+k.imag*sin(-k.imag*z)) * exp(-k.real*z)
277     Ex1_ex=exp(-k.real*z)*sin(-k.imag*z)
278     Ex1_ex_z=(-k.real*sin(-k.imag*z)-k.imag*cos(-k.imag*z)) * exp(-k.real*z)
279    
280     acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta, mu=mu0, fixAtBottom=True, E_x0=Ex0_ex*[1.,0]+ Ex1_ex*[0,1.] )
281    
282     args=acw.getArguments(SIGMA)
283     Ex=args[0]
284     Exz=args[1]
285     self.assertTrue(Lsup(Ex[0]-Ex0_ex) <= 1e-4 * Lsup(Ex0_ex))
286     self.assertTrue(Lsup(Ex[1]-Ex1_ex) <= 1e-4 * Lsup(Ex1_ex))
287     self.assertTrue(Lsup(Exz[0]-Ex0_ex_z) <= 1e-2 * Lsup(Ex0_ex_z))
288     self.assertTrue(Lsup(Exz[1]-Ex1_ex_z) <= 1e-2 * Lsup(Ex1_ex_z))
289    
290 gross 4980 argsr=acw.getArguments(0.)
291     ref=acw.getDefect(0., *argsr)
292    
293     # this should be almost zero:
294 gross 4979 args=acw.getArguments(SIGMA)
295     d=acw.getDefect(SIGMA, *args)
296    
297     self.assertTrue( d > 0.)
298     self.assertTrue( ref > 0.)
299     self.assertTrue( d <= 1e-4 * ref ) # d should be zero (some sort of)
300 gross 4980
301     # and this should be zero
302     d0=acw.getDefect(SIGMA, Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], Ex0_ex_z*[1.,0]+ Ex1_ex_z*[0,1.])
303     self.assertTrue( d0 <= 1e-8 * ref ) # d should be zero (some sort of)
304 gross 4979
305 gross 4980
306     # and this too
307     dg=acw.getGradient(SIGMA, Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], Ex0_ex_z*[1.,0]+ Ex1_ex_z*[0,1.])
308     self.assertTrue(isinstance(dg, Data))
309     self.assertTrue(dg.getShape()==())
310     self.assertTrue(Lsup(dg) < 1e-10)
311 gross 4979
312 gross 4981 def test_Differential(self):
313 gross 4980
314 gross 4981 INC=0.01
315 gross 4980
316     omega=2.
317     mu0=0.123
318     SIGMA=15.
319     k=cmath.sqrt(1j*omega*mu0*SIGMA) # Ex=exp(k*z)
320    
321     from esys.ripley import Rectangle
322     domain=Rectangle(200,200)
323    
324    
325     IMP=-cmath.sqrt(1j*omega*mu0/SIGMA)
326     Z_XY=[ IMP, IMP ]
327     x=[ [0.3,0.5], [0.6,0.5] ]
328     eta=0.005
329     z=domain.getX()[1]
330     Ex0_ex=exp(-k.real*z)*cos(-k.imag*z)
331     Ex0_ex_z=(-k.real*cos(-k.imag*z)+k.imag*sin(-k.imag*z)) * exp(-k.real*z)
332     Ex1_ex=exp(-k.real*z)*sin(-k.imag*z)
333     Ex1_ex_z=(-k.real*sin(-k.imag*z)-k.imag*cos(-k.imag*z)) * exp(-k.real*z)
334    
335     acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta, mu=mu0, fixAtBottom=True, E_x0=Ex0_ex*[1.,0]+ Ex1_ex*[0,1.] )
336 gross 4981
337     # this is the base line:
338     SIGMA0=10.
339 gross 4980 args0=acw.getArguments(SIGMA0)
340     d0=acw.getDefect(SIGMA0, *args0)
341    
342 gross 4981 dg0=acw.getGradient(SIGMA0, *args0)
343     self.assertTrue(isinstance(dg0, Data))
344     self.assertTrue(dg0.getShape()==())
345 gross 4980
346 gross 4981
347 gross 4980 X=Function(domain).getX()
348 gross 4981
349     # test 1
350     p=INC
351     SIGMA1=SIGMA0+p
352 gross 4980 args1=acw.getArguments(SIGMA1)
353     d1=acw.getDefect(SIGMA1, *args1)
354 gross 4981 self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
355    
356     # test 2
357     p=exp(-length(X-(0.2,0.2))**2/10)*INC
358     SIGMA1=SIGMA0+p
359     args1=acw.getArguments(SIGMA1)
360     d1=acw.getDefect(SIGMA1, *args1)
361     self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
362 gross 4980
363 gross 4981 # test 3
364     p=sin(length(X)*3*3.14)*INC
365     SIGMA1=SIGMA0+p
366     args1=acw.getArguments(SIGMA1)
367     d1=acw.getDefect(SIGMA1, *args1)
368     self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
369 gross 4979
370 gross 4688
371 sshaw 4984 if __name__ == '__main__':
372     run_tests(__name__, exit_on_failure=True)
373 gross 4688

  ViewVC Help
Powered by ViewVC 1.1.26