/[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 5235 - (show annotations)
Wed Oct 29 01:01:44 2014 UTC (4 years, 5 months ago) by caltinay
File MIME type: text/x-python
File size: 16770 byte(s)
Tweaks to the forward model tests and finally silenced all but rank 0
for unit test output. I don't think this will hide useful info as all
ranks should output the same thing by the TextTestRunner...

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.assertLess(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(19, 19, d1=mpisize)
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 omega=2.
260 mu0=0.123
261 SIGMA=15.
262 k=cmath.sqrt(1j*omega*mu0*SIGMA) # Ex=exp(k*z)
263
264 from esys.ripley import Rectangle
265 domain=Rectangle(199,199, d1=mpisize)
266
267
268 IMP=cmath.sqrt(1j*omega*mu0/SIGMA)
269 Z_XY=[ IMP, IMP ]
270 x=[ [0.3,0.5], [0.6,0.5] ]
271 eta=0.005
272 z=domain.getX()[1]
273 Ex0_ex=exp(-k.real*z)*cos(-k.imag*z)
274 Ex0_ex_z=(-k.real*cos(-k.imag*z)+k.imag*sin(-k.imag*z)) * exp(-k.real*z)
275 Ex1_ex=exp(-k.real*z)*sin(-k.imag*z)
276 Ex1_ex_z=(-k.real*sin(-k.imag*z)-k.imag*cos(-k.imag*z)) * exp(-k.real*z)
277
278 acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta, mu=mu0, fixAtBottom=True, E_x0=Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], tol=1e-9)
279
280 args=acw.getArguments(SIGMA)
281 Ex=args[0]
282 Exz=args[1]
283 self.assertTrue(Lsup(Ex[0]-Ex0_ex) <= 1e-4 * Lsup(Ex0_ex))
284 self.assertTrue(Lsup(Ex[1]-Ex1_ex) <= 1e-4 * Lsup(Ex1_ex))
285 self.assertTrue(Lsup(Exz[0]-Ex0_ex_z) <= 1e-2 * Lsup(Ex0_ex_z))
286 self.assertTrue(Lsup(Exz[1]-Ex1_ex_z) <= 1e-2 * Lsup(Ex1_ex_z))
287
288 argsr=acw.getArguments(0.)
289 ref=acw.getDefect(0., *argsr)
290
291 # this should be almost zero:
292 args=acw.getArguments(SIGMA)
293 d=acw.getDefect(SIGMA, *args)
294
295 self.assertTrue( d > 0.)
296 self.assertTrue( ref > 0.)
297 self.assertTrue( d <= 1e-4 * ref ) # d should be zero (some sort of)
298
299 # and this should be zero
300 d0=acw.getDefect(SIGMA, Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], Ex0_ex_z*[1.,0]+ Ex1_ex_z*[0,1.])
301 self.assertTrue( d0 <= 1e-8 * ref ) # d should be zero (some sort of)
302
303
304 # and this too
305 dg=acw.getGradient(SIGMA, Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], Ex0_ex_z*[1.,0]+ Ex1_ex_z*[0,1.])
306 self.assertTrue(isinstance(dg, Data))
307 self.assertTrue(dg.getShape()==())
308 self.assertTrue(Lsup(dg) < 1e-10)
309
310 def test_Differential(self):
311
312 INC=0.01
313
314 omega=2.
315 mu0=0.123
316 SIGMA=15.
317 k=cmath.sqrt(1j*omega*mu0*SIGMA) # Ex=exp(k*z)
318
319 from esys.ripley import Rectangle
320 domain=Rectangle(99,99, d1=mpisize)
321
322
323 IMP=-cmath.sqrt(1j*omega*mu0/SIGMA)
324 Z_XY=[ IMP, IMP ]
325 x=[ [0.3,0.5], [0.6,0.5] ]
326 eta=0.005
327 z=domain.getX()[1]
328 Ex0_ex=exp(-k.real*z)*cos(-k.imag*z)
329 Ex0_ex_z=(-k.real*cos(-k.imag*z)+k.imag*sin(-k.imag*z)) * exp(-k.real*z)
330 Ex1_ex=exp(-k.real*z)*sin(-k.imag*z)
331 Ex1_ex_z=(-k.real*sin(-k.imag*z)-k.imag*cos(-k.imag*z)) * exp(-k.real*z)
332
333 acw=MT2DModelTEMode(domain, omega, x, Z_XY, eta, mu=mu0, fixAtBottom=True, E_x0=Ex0_ex*[1.,0]+ Ex1_ex*[0,1.], tol=1e-9 )
334
335 # this is the base line:
336 SIGMA0=10.
337 args0=acw.getArguments(SIGMA0)
338 d0=acw.getDefect(SIGMA0, *args0)
339
340 dg0=acw.getGradient(SIGMA0, *args0)
341 self.assertTrue(isinstance(dg0, Data))
342 self.assertTrue(dg0.getShape()==())
343
344
345 X=Function(domain).getX()
346
347 # test 1
348 p=INC
349 SIGMA1=SIGMA0+p
350 args1=acw.getArguments(SIGMA1)
351 d1=acw.getDefect(SIGMA1, *args1)
352 self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
353
354 # test 2
355 p=exp(-length(X-(0.2,0.2))**2/10)*INC
356 SIGMA1=SIGMA0+p
357 args1=acw.getArguments(SIGMA1)
358 d1=acw.getDefect(SIGMA1, *args1)
359 self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
360
361 # test 3
362 p=sin(length(X)*3*3.14)*INC
363 SIGMA1=SIGMA0+p
364 args1=acw.getArguments(SIGMA1)
365 d1=acw.getDefect(SIGMA1, *args1)
366 self.assertTrue( abs( d1-d0-integrate(dg0*p) ) < 1e-2 * abs(d1-d0) )
367
368 class TestSubsidence(unittest.TestCase):
369 def test_PDE(self):
370
371 lam=2.
372 mu=1.
373
374 from esys.ripley import Brick
375 domain=Brick(20,20,19, d2=mpisize)
376
377 xb=FunctionOnBoundary(domain).getX()
378 m=whereZero(xb[2]-1)
379 w=m*[0,0,1]
380 d=m*2.5
381 acw=Subsidence(domain, w,d, lam, mu )
382
383 P0=10.
384 args0=acw.getArguments(P0)
385 u=args0[0]
386 self.assertTrue(Lsup(u[0]) < 1.e-8)
387 self.assertTrue(Lsup(u[1]) < 1.e-8)
388 self.assertTrue(Lsup(u[2]-2.5*domain.getX()[2]) < 1.e-8)
389
390 dd=acw.getDefect(P0, *args0)
391
392 self.assertTrue( dd >= 0.)
393 self.assertTrue( dd <= 1e-7 * 2.5 )
394 def test_Differential(self):
395
396 lam=2.
397 mu=1.
398
399 INC=0.01
400 from esys.ripley import Brick
401 domain=Brick(20,20,20*mpisize-1 , d2=mpisize)
402
403 xb=FunctionOnBoundary(domain).getX()
404 m=whereZero(xb[2]-1)
405 w=m*[0,0,1]
406 d=m*2.5
407 acw=Subsidence(domain, w,d, lam, mu )
408
409
410 x=Function(domain).getX()
411 P0=x[0]*x[1]
412 args0=acw.getArguments(P0)
413 d0=acw.getDefect(P0, *args0)
414 grad_d=acw.getGradient(P0, *args0)
415
416
417 dP=exp(-(length(x-[0.5,0.5,0.5])/0.06)**2)
418 P1=P0+INC*dP
419 args1=acw.getArguments(P1)
420 d1=acw.getDefect(P1, *args1)
421 ref=abs((d1-d0)/INC)
422 self.assertTrue(abs((d1-d0)/INC-integrate(grad_d* dP)) < ref * 1.e-5)
423
424 dP=exp(-(length(x-[0.3,0.3,0.5])/0.06)**2)
425 P2=P0-INC*dP
426 args2=acw.getArguments(P2)
427 d2=acw.getDefect(P2, *args2)
428 ref=abs((d2-d0)/INC)
429 self.assertTrue(abs((d2-d0)/INC+integrate(grad_d* dP)) < ref * 1.e-5)
430
431
432
433 class TestIsostaticPressure(unittest.TestCase):
434 def test_all(self):
435 from esys.ripley import Brick
436 domain=Brick(50,50,20*mpisize-1, d2=mpisize)
437
438 ps=IsostaticPressure(domain, level0=1., coordinates=None)
439
440 g=Vector(0., Function(domain))
441 rho=Scalar(100, Function(domain))
442 p0=ps.getPressure(g, rho)
443 p_ref=-(1.-domain.getX()[2])*981.
444 self.assertTrue(Lsup(p0-p_ref) < 1e-6 * Lsup(p_ref))
445
446 g=Vector([0,0,-10], Function(domain))
447 rho=Scalar(0, Function(domain))
448 p0=ps.getPressure(g, rho)
449 p_ref=-(1.-domain.getX()[2])*26700
450 self.assertTrue(Lsup(p0-p_ref) < 1e-6 * Lsup(p_ref))
451
452 g=Vector([0,0,-10], Function(domain))
453 rho=Scalar(100, Function(domain))
454 p0=ps.getPressure(g, rho)
455 p_ref=-(1.-domain.getX()[2])*(981.+26700+1000)
456 self.assertTrue(Lsup(p0-p_ref) < 1e-6 * Lsup(p_ref))
457
458 if __name__ == '__main__':
459 run_tests(__name__, exit_on_failure=True)
460

  ViewVC Help
Powered by ViewVC 1.1.26