/[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 4688 - (show annotations)
Wed Feb 19 03:18:58 2014 UTC (5 years, 2 months ago) by gross
File MIME type: text/x-python
File size: 8872 byte(s)
acoustic forward problem + testing add
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 unittest
26 import numpy as np
27 import os
28 import sys
29 from esys.downunder import *
30 from esys.escript import unitsSI as U
31 from esys.escript import *
32 from esys.weipa import saveSilo
33 from esys.escript.linearPDEs import LinearSinglePDE, LinearPDE
34
35 mpisize = getMPISizeWorld()
36 # this is mainly to avoid warning messages
37 logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
38
39 try:
40 TEST_DATA_ROOT=os.environ['DOWNUNDER_TEST_DATA_ROOT']
41 except KeyError:
42 TEST_DATA_ROOT='ref_data'
43
44 try:
45 WORKDIR=os.environ['DOWNUNDER_WORKDIR']
46 except KeyError:
47 WORKDIR='.'
48
49
50
51 @unittest.skipIf(mpisize>1, "more than 1 MPI rank")
52 class TestAcousticInversion(unittest.TestCase):
53 def test_API(self):
54 from esys.ripley import Rectangle
55 domain=Rectangle(20,20, diracPoints=[(0.5,1.)], diracTags=['sss'])
56 omega=2.
57
58
59 data=Data([1,2], FunctionOnBoundary(domain))
60 F=Data([2,3], Function(domain))
61 w=1.
62 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, data, 1.) # F is a scalar
63 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, [1,2], F) # data is not Data
64 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, Data([1,2], Function(domain)), F) # data is not on boundary
65 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, w, Scalar(1, Function(domain)), F) # data is not of shape (2,)
66 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, [1,2], data, F) # w is not a scalar
67 self.assertRaises(ValueError, AcousticWaveForm, domain, omega, Scalar(1, Function(domain)), data, F) # w is not a scalar
68
69 # now we do a real one
70 acw=AcousticWaveForm(domain, omega, w, data, F)
71 self.assertEqual(acw.getDomain(), domain)
72 pde=acw.setUpPDE()
73 self.assertIsInstance(pde, LinearPDE)
74 self.assertEqual(pde.getNumEquations(), 2)
75 self.assertEqual(pde.getNumSolutions(), 2)
76 self.assertEqual(pde.getDomain(), domain)
77
78
79 def test_numeric2DscaleF(self):
80
81 from esys.ripley import Rectangle
82 domain=Rectangle(100,100, diracPoints=[(0.5,1.)], diracTags=['sss'])
83 omega=2.
84
85 # test solution is u = a * z where a is complex
86 a=complex(3.45, 0.56)
87 sigma=complex(1e-3, 0.056)
88
89
90 data=Data([a.real, a.imag], FunctionOnBoundary(domain))
91 mydata=data.copy()
92
93 z=FunctionOnBoundary(domain).getX()[1]
94 w=whereZero(z-1.)
95 # source:
96 F=Data( [1,0],Function(domain))
97 #
98 acw=AcousticWaveForm(domain, omega, w, data, F, coordinates=None, fixAtBottom=False, tol=1e-8, saveMemory=True, scaleF=True)
99 # check rescaled data
100 surv=acw.getSurvey()
101 self.assertAlmostEqual( integrate(length(surv[0])**2 * surv[1]), 1.)
102
103 mydata_scale=sqrt( integrate(w*length(mydata)**2) )
104 self.assertAlmostEqual( acw.getSourceScaling(z*[1, 0.]) , a/mydata_scale )
105 self.assertAlmostEqual( acw.getSourceScaling(mydata) , 1./mydata_scale )
106
107 # this should be zero:
108 sigma_comps=[sigma.real, sigma.imag]
109 args=acw.getArguments(sigma_comps)
110 d=acw.getDefect(sigma_comps, *args)
111 self.assertTrue(isinstance(d, float))
112 self.assertTrue(abs(d) < 1e-10)
113
114 dg=acw.getGradient(sigma_comps, *args)
115 self.assertTrue(isinstance(dg, Data))
116 self.assertTrue(dg.getShape()==(2,))
117 self.assertTrue(dg.getFunctionSpace()==Solution(domain))
118 self.assertTrue(Lsup(dg) < 1e-10)
119
120 # this shuld be zero' too
121 sigma_comps=[2*sigma.real, sigma.imag/2.]
122 args=acw.getArguments(sigma_comps)
123 d=acw.getDefect(sigma_comps, *args)
124 self.assertTrue(isinstance(d, float))
125 self.assertTrue(abs(d)< 1e-10)
126
127 dg=acw.getGradient(sigma_comps, *args)
128 self.assertTrue(isinstance(dg, Data))
129 self.assertTrue(dg.getShape()==(2,))
130 self.assertTrue(dg.getFunctionSpace()==Solution(domain))
131 self.assertTrue(Lsup(dg) < 1e-10)
132
133 # this shouldn't be zero:
134 sigma0=[2*sigma.real, 10*a.imag]*(27*Function(domain).getX()[0]-Function(domain).getX()[1])
135 args=acw.getArguments(sigma0)
136 d0=acw.getDefect(sigma0, *args)
137 self.assertTrue(isinstance(d0, float))
138 self.assertTrue(d0 >= 0)
139 self.assertTrue(d0 > 1e-10)
140
141 dg0=acw.getGradient(sigma0, *args)
142 self.assertTrue(isinstance(dg0, Data))
143 self.assertTrue(dg0.getShape()==(2,))
144 self.assertTrue(dg0.getFunctionSpace()==Solution(domain))
145 self.assertTrue(Lsup(dg0) > 1e-10)
146
147 # test the gradient numerrically:
148 h=0.002
149 X=Function(domain).getX()
150 # .. increment:
151 p=h*exp(-(length(X-[0.6,0.6])/10)**2)*Lsup(length(sigma0))
152
153
154 sigma1=sigma0+p*[1,0]
155 args=acw.getArguments(sigma1)
156 d1=acw.getDefect(sigma1, *args)
157 self.assertTrue( abs( d1-d0-integrate(dg0[0]*p) ) < 1e-2 * abs(d1-d0) )
158
159 sigma2=sigma0+p*[0,1]
160 args=acw.getArguments(sigma2)
161 d2=acw.getDefect(sigma2, *args)
162 self.assertTrue( abs(d2-d0-integrate(dg0[1]*p)) < 1e-2 * abs(d2-d0) )
163
164 def test_numeric2DnoscaleF(self):
165
166 from esys.ripley import Rectangle
167 domain=Rectangle(10,20, diracPoints=[(0.5,1.)], diracTags=['sss'])
168 omega=1.5
169
170 # test solution is u = a * z where a is complex
171 a=complex(3.45, 0.56)
172 sigma=complex(1e-3, 0.056)
173
174
175 data=Data([a.real, a.imag], FunctionOnBoundary(domain))
176 z=FunctionOnBoundary(domain).getX()[1]
177 w=whereZero(z-1.)
178 # F = - a*omega* sigma
179 F=Data( [-(a*omega**2*sigma).real, -(a*omega**2*sigma).imag ],Function(domain))
180
181 acw=AcousticWaveForm(domain, omega, w, data, F, coordinates=None, fixAtBottom=False, tol=1e-8, saveMemory=True, scaleF=False)
182 # this should be zero:
183 sigma_comps=[sigma.real, sigma.imag]
184 args=acw.getArguments(sigma_comps)
185 d=acw.getDefect(sigma_comps, *args)
186 self.assertTrue(isinstance(d, float))
187 self.assertTrue(d >= 0)
188 self.assertTrue(d < 1e-10)
189
190 dg=acw.getGradient(sigma_comps, *args)
191
192 self.assertTrue(isinstance(dg, Data))
193 self.assertTrue(dg.getShape()==(2,))
194 self.assertTrue(dg.getFunctionSpace()==Solution(domain))
195 self.assertTrue(Lsup(dg) < 5e-10)
196 # this shouldn't be zero:
197 sigma0=Data([2*sigma.real, sigma.imag/2], Function(domain) )
198 args=acw.getArguments(sigma0)
199 d0=acw.getDefect(sigma0, *args)
200 self.assertTrue(isinstance(d0, float))
201 self.assertTrue(d0 >= 0)
202 self.assertTrue(d0 > 1e-10)
203
204 dg0=acw.getGradient(sigma0, *args)
205 self.assertTrue(isinstance(dg0, Data))
206 self.assertTrue(dg0.getShape()==(2,))
207 self.assertTrue(dg0.getFunctionSpace()==Solution(domain))
208 self.assertTrue(Lsup(dg0) > 1e-10)
209 # test the gradient numerrically:
210 h=0.001
211 X=Function(domain).getX()
212 p=h*sin(length(X)*np.pi)*Lsup(length(sigma0))
213
214 sigma1=sigma0+p*[1,0]
215 args=acw.getArguments(sigma1)
216 d1=acw.getDefect(sigma1, *args)
217
218 self.assertTrue( abs( d1-d0-integrate(dg0[0]*p) ) < 1e-2 * abs(d1-d0) )
219
220 sigma2=sigma0+p*[0,1]
221 args=acw.getArguments(sigma2)
222 d2=acw.getDefect(sigma2, *args)
223 self.assertTrue( abs(d2-d0-integrate(dg0[1]*p)) < 1e-2 * abs(d2-d0) )
224
225
226 if __name__ == "__main__":
227 suite = unittest.TestSuite()
228 suite.addTest(unittest.makeSuite(TestAcousticInversion))
229 s=unittest.TextTestRunner(verbosity=2).run(suite)
230 if not s.wasSuccessful(): sys.exit(1)
231

  ViewVC Help
Powered by ViewVC 1.1.26