/[escript]/trunk/ripley/test/python/run_simplesolve.py
ViewVC logotype

Contents of /trunk/ripley/test/python/run_simplesolve.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4938 - (show annotations)
Wed May 14 01:13:23 2014 UTC (5 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 18151 byte(s)
Modify unit tests to read their classes from
esys.escriptcore.utestselect

Change the line in that file to switch between unittest and unittest2


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 """
25 Test suite for the linearPDE and pdetools test on ripley
26
27 :remark:
28
29 :var __author__: name of author
30 :var __licence__: licence agreement
31 :var __url__: url entry point on documentation
32 :var __version__: version
33 :var __date__: date of the version
34 """
35
36 __author__="Lutz Gross, l.gross@uq.edu.au"
37
38 import esys.escriptcore.utestselect as unittest, sys
39 from esys.escript import *
40 from esys.ripley import Rectangle,Brick
41 from esys.escript.linearPDEs import LinearPDE, SolverOptions
42 import numpy
43 SOLVER_VERBOSE=False
44
45 try:
46 RIPLEY_TEST_DATA=os.environ['RIPLEY_TEST_DATA']
47 except KeyError:
48 RIPLEY_TEST_DATA='.'
49
50 SOLVER_TOL=1.e-8
51 REL_TOL=1.e-6
52
53 FAC_DIAG=1.
54 FAC_OFFDIAG=-0.4
55
56 # number of elements in the spatial directions
57 NE0=12
58 NE1=12
59 NE2=8
60 mpiSize=getMPISizeWorld()
61 for x in [int(sqrt(mpiSize)),2,3,5,7,1]:
62 NX=x
63 NY=mpiSize//x
64 if NX*NY == mpiSize:
65 break
66
67 for x in [(int(mpiSize**(1/3.)),int(mpiSize**(1/3.))),(2,3),(2,2),(1,2),(1,1)]:
68 NXb=x[0]
69 NYb=x[1]
70 NZb=mpiSize//(x[0]*x[1])
71 if NXb*NYb*NZb == mpiSize:
72 break
73
74 class SimpleSolve_Rectangle_SinglePDE_Paso_BICGSTAB_Jacobi(unittest.TestCase):
75 def test_solve(self):
76 # Tell about how many MPI CPUs and OpenMP threads
77 domain=Rectangle(n0=NE0*NX-1, n1=NE1*NY-1, d0=NX, d1=NY)
78 x=Solution(domain).getX()
79 # --- set exact solution ----
80 u_ex=Scalar(0,Solution(domain))
81 u_ex=1.+2.*x[0]+3.*x[1]
82 # --- set exact gradient -----------
83 g_ex=Data(0.,(2,),Solution(domain))
84 g_ex[0]=2.
85 g_ex[1]=3.
86 # -------- test gradient --------------------------------
87 g=grad(u_ex)
88 self.assertTrue(Lsup(g_ex-g)<REL_TOL*Lsup(g_ex))
89 # -------- set-up PDE -----------------------------------
90 pde=LinearPDE(domain,numEquations=1)
91 mask=whereZero(x[0])
92 pde.setValue(r=u_ex,q=mask)
93 pde.setValue(A=kronecker(2),y=inner(g_ex,domain.getNormal()))
94 # -------- get the solution ---------------------------
95 pde.getSolverOptions().setTolerance(SOLVER_TOL)
96 pde.getSolverOptions().setSolverMethod(SolverOptions.BICGSTAB)
97 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
98 pde.getSolverOptions().setPackage(SolverOptions.PASO)
99 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
100 u=pde.getSolution()
101 # -------- test the solution ---------------------------
102 error=Lsup(u-u_ex)
103 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
104 class SimpleSolve_Rectangle_SinglePDE_Paso_PCG_Jacobi(unittest.TestCase):
105 def test_solve(self):
106 domain=Rectangle(n0=NE0*NX-1, n1=NE1*NY-1, d0=NX, d1=NY)
107 x=Solution(domain).getX()
108 # --- set exact solution ----
109 u_ex=Scalar(0,Solution(domain))
110 u_ex=1.+2.*x[0]+3.*x[1]
111 # --- set exact gradient -----------
112 g_ex=Data(0.,(2,),Solution(domain))
113 g_ex[0]=2.
114 g_ex[1]=3.
115 # -------- test gradient --------------------------------
116 g=grad(u_ex)
117 self.assertTrue(Lsup(g_ex-g)<REL_TOL*Lsup(g_ex))
118 # -------- set-up PDE -----------------------------------
119 pde=LinearPDE(domain,numEquations=1)
120 mask=whereZero(x[0])
121 pde.setValue(r=u_ex,q=mask)
122 pde.setValue(A=kronecker(2),y=inner(g_ex,domain.getNormal()))
123 # -------- get the solution ---------------------------
124 pde.getSolverOptions().setTolerance(SOLVER_TOL)
125 pde.getSolverOptions().setSolverMethod(SolverOptions.PCG)
126 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
127 pde.getSolverOptions().setPackage(SolverOptions.PASO)
128 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
129 u=pde.getSolution()
130 # -------- test the solution ---------------------------
131 error=Lsup(u-u_ex)
132 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
133 class SimpleSolve_Rectangle_SystemPDE_Paso_PCG_Jacobi(unittest.TestCase):
134 def test_solve(self):
135 domain=Rectangle(n0=NE0*NX-1, n1=NE1*NY-1, d0=NX, d1=NY)
136 x=Solution(domain).getX()
137 # --- set exact solution ----
138 u_ex=Vector(0,Solution(domain))
139 u_ex[0]=1.+2.*x[0]+3.*x[1]
140 u_ex[1]=-1.+3.*x[0]+2.*x[1]
141 # --- set exact gradient -----------
142 g_ex=Data(0.,(2,2),Solution(domain))
143 g_ex[0,0]=2.
144 g_ex[0,1]=3.
145 g_ex[1,0]=3.
146 g_ex[1,1]=2.
147 # -------- test gradient --------------------------------
148 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
149 # -------- set-up PDE -----------------------------------
150 pde=LinearPDE(domain,numEquations=2)
151 mask=whereZero(x[0])
152 pde.setValue(r=u_ex,q=mask*numpy.ones(2,))
153 A=Tensor4(0,Function(domain))
154 A[0,:,0,:]=kronecker(2)
155 A[1,:,1,:]=kronecker(2)
156 Y=Vector(0.,Function(domain))
157 Y[0]=u_ex[0]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG
158 Y[1]=u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG
159 pde.setValue(A=A,
160 D=kronecker(2)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((2,2))*FAC_OFFDIAG,
161 Y=Y,
162 y=matrixmult(g_ex,domain.getNormal()))
163 # -------- get the solution ---------------------------
164 pde.getSolverOptions().setTolerance(SOLVER_TOL)
165 pde.getSolverOptions().setSolverMethod(SolverOptions.PCG)
166 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
167 pde.getSolverOptions().setPackage(SolverOptions.PASO)
168 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
169 u=pde.getSolution()
170 # -------- test the solution ---------------------------
171 error=Lsup(u-u_ex)
172 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
173 class SimpleSolve_Brick_SinglePDE_Paso_PCG_Jacobi(unittest.TestCase):
174 def test_solve(self):
175 domain=Brick(n0=NE0*NXb-1, n1=NE1*NYb-1, n2=NE2*NZb-1, d0=NXb, d1=NYb, d2=NZb)
176 x=Solution(domain).getX()
177 u_ex=1.+2.*x[0]+3.*x[1]+4.*x[2]
178 # --- set exact gradient -----------
179 g_ex=Data(0.,(3,),Solution(domain))
180 g_ex[0]=2.
181 g_ex[1]=3.
182 g_ex[2]=4.
183 # -------- test gradient --------------------------------
184 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
185 # -------- set-up PDE -----------------------------------
186 pde=LinearPDE(domain,numEquations=1)
187 mask=whereZero(x[0])
188 pde.setValue(r=u_ex,q=mask)
189 pde.setValue(A=kronecker(3),y=inner(g_ex,domain.getNormal()))
190 # -------- get the solution ---------------------------
191 pde.getSolverOptions().setTolerance(SOLVER_TOL)
192 pde.getSolverOptions().setSolverMethod(SolverOptions.PCG)
193 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
194 pde.getSolverOptions().setPackage(SolverOptions.PASO)
195 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
196 u=pde.getSolution()
197 # -------- test the solution ---------------------------
198 error=Lsup(u-u_ex)
199 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
200 class SimpleSolve_Brick_SystemPDE_Paso_PCG_Jacobi(unittest.TestCase):
201 def test_solve(self):
202 domain=Brick(n0=NE0*NXb-1, n1=NE1*NYb-1, n2=NE2*NZb-1, d0=NXb, d1=NYb, d2=NZb)
203 x=Solution(domain).getX()
204 # --- set exact solution ----
205 u_ex=Vector(0,Solution(domain))
206 u_ex[0]=1.+2.*x[0]+3.*x[1]+4.*x[2]
207 u_ex[1]=-1.+4.*x[0]+1.*x[1]-2.*x[2]
208 u_ex[2]=5.+8.*x[0]+4.*x[1]+5.*x[2]
209 # --- set exact gradient -----------
210 g_ex=Data(0.,(3,3),Solution(domain))
211 g_ex[0,0]=2.
212 g_ex[0,1]=3.
213 g_ex[0,2]=4.
214 g_ex[1,0]=4.
215 g_ex[1,1]=1.
216 g_ex[1,2]=-2.
217 g_ex[2,0]=8.
218 g_ex[2,1]=4.
219 g_ex[2,2]=5.
220 # -------- test gradient --------------------------------
221 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
222 # -------- set-up PDE -----------------------------------
223 pde=LinearPDE(domain,numEquations=3)
224 mask=whereZero(x[0])
225 pde.setValue(r=u_ex,q=mask*numpy.ones(3,))
226 A=Tensor4(0,Function(domain))
227 A[0,:,0,:]=kronecker(3)
228 A[1,:,1,:]=kronecker(3)
229 A[2,:,2,:]=kronecker(3)
230 Y=Vector(0.,Function(domain))
231 Y[0]=u_ex[0]*FAC_DIAG+u_ex[2]*FAC_OFFDIAG+u_ex[1]*FAC_OFFDIAG
232 Y[1]=u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG+u_ex[2]*FAC_OFFDIAG
233 Y[2]=u_ex[2]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG+u_ex[0]*FAC_OFFDIAG
234 pde.setValue(A=A,
235 D=kronecker(3)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((3,3))*FAC_OFFDIAG,
236 Y=Y,
237 y=matrixmult(g_ex,domain.getNormal()))
238 # -------- get the solution ---------------------------
239 pde.getSolverOptions().setTolerance(SOLVER_TOL)
240 pde.getSolverOptions().setSolverMethod(SolverOptions.PCG)
241 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
242 pde.getSolverOptions().setPackage(SolverOptions.PASO)
243 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
244 u=pde.getSolution()
245 # -------- test the solution ---------------------------
246 error=Lsup(u-u_ex)
247 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
248
249 class SimpleSolve_Rectangle_SinglePDE_Paso_MINRES_Jacobi(unittest.TestCase):
250 def test_solve(self):
251 domain=Rectangle(n0=NE0*NX-1, n1=NE1*NY-1, d0=NX, d1=NY)
252 x=Solution(domain).getX()
253 # --- set exact solution ----
254 u_ex=Scalar(0,Solution(domain))
255 u_ex=1.+2.*x[0]+3.*x[1]
256 # --- set exact gradient -----------
257 g_ex=Data(0.,(2,),Solution(domain))
258 g_ex[0]=2.
259 g_ex[1]=3.
260 # -------- test gradient --------------------------------
261 g=grad(u_ex)
262 self.assertTrue(Lsup(g_ex-g)<REL_TOL*Lsup(g_ex))
263 # -------- set-up PDE -----------------------------------
264 pde=LinearPDE(domain,numEquations=1)
265 mask=whereZero(x[0])
266 pde.setValue(r=u_ex,q=mask)
267 pde.setValue(A=kronecker(2),y=inner(g_ex,domain.getNormal()))
268 # -------- get the solution ---------------------------
269 pde.getSolverOptions().setTolerance(SOLVER_TOL)
270 pde.getSolverOptions().setSolverMethod(SolverOptions.MINRES)
271 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
272 pde.getSolverOptions().setPackage(SolverOptions.PASO)
273
274 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
275 u=pde.getSolution()
276 # -------- test the solution ---------------------------
277 error=Lsup(u-u_ex)
278 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
279
280 class SimpleSolve_Rectangle_SystemPDE_Paso_MINRES_Jacobi(unittest.TestCase):
281 def test_solve(self):
282 domain=Rectangle(n0=NE0*NX-1, n1=NE1*NY-1, d0=NX, d1=NY)
283 x=Solution(domain).getX()
284 # --- set exact solution ----
285 u_ex=Vector(0,Solution(domain))
286 u_ex[0]=1.+2.*x[0]+3.*x[1]
287 u_ex[1]=-1.+3.*x[0]+2.*x[1]
288 # --- set exact gradient -----------
289 g_ex=Data(0.,(2,2),Solution(domain))
290 g_ex[0,0]=2.
291 g_ex[0,1]=3.
292 g_ex[1,0]=3.
293 g_ex[1,1]=2.
294 # -------- test gradient --------------------------------
295 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
296 # -------- set-up PDE -----------------------------------
297 pde=LinearPDE(domain,numEquations=2)
298 mask=whereZero(x[0])
299 pde.setValue(r=u_ex,q=mask*numpy.ones(2,))
300 A=Tensor4(0,Function(domain))
301 A[0,:,0,:]=kronecker(2)
302 A[1,:,1,:]=kronecker(2)
303 Y=Vector(0.,Function(domain))
304 Y[0]=u_ex[0]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG
305 Y[1]=u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG
306 pde.setValue(A=A,
307 D=kronecker(2)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((2,2))*FAC_OFFDIAG,
308 Y=Y,
309 y=matrixmult(g_ex,domain.getNormal()))
310 # -------- get the solution ---------------------------
311 pde.getSolverOptions().setTolerance(SOLVER_TOL)
312 pde.getSolverOptions().setSolverMethod(SolverOptions.MINRES)
313 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
314 pde.getSolverOptions().setPackage(SolverOptions.PASO)
315 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
316 u=pde.getSolution()
317 # -------- test the solution ---------------------------
318 error=Lsup(u-u_ex)
319 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
320 class SimpleSolve_Brick_SinglePDE_Paso_MINRES_Jacobi(unittest.TestCase):
321 def test_solve(self):
322 domain=Brick(n0=NE0*NXb-1, n1=NE1*NYb-1, n2=NE2*NZb-1, d0=NXb, d1=NYb, d2=NZb)
323 x=Solution(domain).getX()
324 u_ex=1.+2.*x[0]+3.*x[1]+4.*x[2]
325 # --- set exact gradient -----------
326 g_ex=Data(0.,(3,),Solution(domain))
327 g_ex[0]=2.
328 g_ex[1]=3.
329 g_ex[2]=4.
330 # -------- test gradient --------------------------------
331 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
332 # -------- set-up PDE -----------------------------------
333 pde=LinearPDE(domain,numEquations=1)
334 mask=whereZero(x[0])
335 pde.setValue(r=u_ex,q=mask)
336 pde.setValue(A=kronecker(3),y=inner(g_ex,domain.getNormal()))
337 # -------- get the solution ---------------------------
338 pde.getSolverOptions().setTolerance(SOLVER_TOL)
339 pde.getSolverOptions().setSolverMethod(SolverOptions.MINRES)
340 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
341 pde.getSolverOptions().setPackage(SolverOptions.PASO)
342 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
343 u=pde.getSolution()
344 # -------- test the solution ---------------------------
345 error=Lsup(u-u_ex)
346 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
347
348 class SimpleSolve_Brick_SystemPDE_Paso_MINRES_Jacobi(unittest.TestCase):
349 def test_solve(self):
350 domain=Brick(n0=NE0*NXb-1, n1=NE1*NYb-1, n2=NE2*NZb-1, d0=NXb, d1=NYb, d2=NZb)
351 x=Solution(domain).getX()
352 # --- set exact solution ----
353 u_ex=Vector(0,Solution(domain))
354 u_ex[0]=1.+2.*x[0]+3.*x[1]+4.*x[2]
355 u_ex[1]=-1.+4.*x[0]+1.*x[1]-2.*x[2]
356 u_ex[2]=5.+8.*x[0]+4.*x[1]+5.*x[2]
357 # --- set exact gradient -----------
358 g_ex=Data(0.,(3,3),Solution(domain))
359 g_ex[0,0]=2.
360 g_ex[0,1]=3.
361 g_ex[0,2]=4.
362 g_ex[1,0]=4.
363 g_ex[1,1]=1.
364 g_ex[1,2]=-2.
365 g_ex[2,0]=8.
366 g_ex[2,1]=4.
367 g_ex[2,2]=5.
368 # -------- test gradient --------------------------------
369 self.assertTrue(Lsup(g_ex-grad(u_ex))<REL_TOL*Lsup(g_ex))
370 # -------- set-up PDE -----------------------------------
371 pde=LinearPDE(domain,numEquations=3)
372 mask=whereZero(x[0])
373 pde.setValue(r=u_ex,q=mask*numpy.ones(3,))
374 A=Tensor4(0,Function(domain))
375 A[0,:,0,:]=kronecker(3)
376 A[1,:,1,:]=kronecker(3)
377 A[2,:,2,:]=kronecker(3)
378 Y=Vector(0.,Function(domain))
379 Y[0]=u_ex[0]*FAC_DIAG+u_ex[2]*FAC_OFFDIAG+u_ex[1]*FAC_OFFDIAG
380 Y[1]=u_ex[1]*FAC_DIAG+u_ex[0]*FAC_OFFDIAG+u_ex[2]*FAC_OFFDIAG
381 Y[2]=u_ex[2]*FAC_DIAG+u_ex[1]*FAC_OFFDIAG+u_ex[0]*FAC_OFFDIAG
382 pde.setValue(A=A,
383 D=kronecker(3)*(FAC_DIAG-FAC_OFFDIAG)+numpy.ones((3,3))*FAC_OFFDIAG,
384 Y=Y,
385 y=matrixmult(g_ex,domain.getNormal()))
386 # -------- get the solution ---------------------------
387 pde.getSolverOptions().setTolerance(SOLVER_TOL)
388 pde.getSolverOptions().setSolverMethod(SolverOptions.MINRES)
389 pde.getSolverOptions().setPreconditioner(SolverOptions.JACOBI)
390 pde.getSolverOptions().setPackage(SolverOptions.PASO)
391 pde.getSolverOptions().setVerbosity(SOLVER_VERBOSE)
392 u=pde.getSolution()
393 # -------- test the solution ---------------------------
394 error=Lsup(u-u_ex)
395 self.assertTrue(error<REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
396
397
398 if __name__ == '__main__':
399 suite = unittest.TestSuite()
400 suite.addTest(unittest.makeSuite(SimpleSolve_Rectangle_SinglePDE_Paso_BICGSTAB_Jacobi))
401 suite.addTest(unittest.makeSuite(SimpleSolve_Rectangle_SinglePDE_Paso_PCG_Jacobi))
402 suite.addTest(unittest.makeSuite(SimpleSolve_Rectangle_SystemPDE_Paso_PCG_Jacobi))
403 suite.addTest(unittest.makeSuite(SimpleSolve_Brick_SinglePDE_Paso_PCG_Jacobi))
404 suite.addTest(unittest.makeSuite(SimpleSolve_Brick_SystemPDE_Paso_PCG_Jacobi))
405 suite.addTest(unittest.makeSuite(SimpleSolve_Rectangle_SinglePDE_Paso_MINRES_Jacobi))
406 suite.addTest(unittest.makeSuite(SimpleSolve_Rectangle_SinglePDE_Paso_MINRES_Jacobi))
407 suite.addTest(unittest.makeSuite(SimpleSolve_Brick_SinglePDE_Paso_MINRES_Jacobi))
408 suite.addTest(unittest.makeSuite(SimpleSolve_Brick_SystemPDE_Paso_MINRES_Jacobi))
409
410 s=unittest.TextTestRunner(verbosity=2).run(suite)
411 if not s.wasSuccessful(): sys.exit(1)

  ViewVC Help
Powered by ViewVC 1.1.26