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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (4 years, 10 months ago) by sshaw
File MIME type: text/x-python
File size: 14041 byte(s)
revamping testrunners, now uses automated discovery and allows running specific tests without modifying files (see escriptcore/py_src/testing.py for more info/examples)

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

  ViewVC Help
Powered by ViewVC 1.1.26