/[escript]/trunk/finley/test/python/run_darcy.py
ViewVC logotype

Contents of /trunk/finley/test/python/run_darcy.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4938 - (show annotations)
Wed May 14 01:13:23 2014 UTC (5 years, 6 months ago) by jfenwick
File MIME type: text/x-python
File size: 14399 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 # -*- coding: utf-8 -*-
2
3 ##############################################################################
4 #
5 # Copyright (c) 2003-2014 by University of Queensland
6 # http://www.uq.edu.au
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
13 # Development 2012-2013 by School of Earth Sciences
14 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
15 #
16 ##############################################################################
17
18 __copyright__="""Copyright (c) 2003-2014 by University of Queensland
19 http://www.uq.edu.au
20 Primary Business: Queensland, Australia"""
21 __license__="""Licensed under the Open Software License version 3.0
22 http://www.opensource.org/licenses/osl-3.0.php"""
23 __url__="https://launchpad.net/escript-finley"
24
25 import esys.escriptcore.utestselect as unittest
26 import tempfile
27
28
29
30 VERBOSE=False and True
31
32 from esys.escript import *
33 from esys.escript.models import DarcyFlow
34 from esys.finley import Rectangle, Brick
35
36 from math import pi
37 import numpy
38 import sys
39 import os
40 #====================================================================================================================
41 try:
42 FINLEY_WORKDIR=os.environ['FINLEY_WORKDIR']
43 except KeyError:
44 FINLEY_WORKDIR='.'
45
46 class Test_Darcy(unittest.TestCase):
47 # this is a simple test for the darcy flux problem
48 #
49 #
50 # p = 1/k * ( 1/2* (fb-f0)/xb* x **2 + f0 * x - ub*x ) + p0
51 #
52 # with f = (fb-f0)/xb* x + f0
53 #
54 # u = f - k * p,x = ub
55 #
56 # we prescribe pressure at x=x0=0 to p0
57 #
58 # if we prescribe the pressure on the bottom x=xb we set
59 #
60 # pb= 1/k * ( 1/2* (fb-f0)* xb + f0 * xb - ub*xb ) + p0 = 1/k * ((fb+f0)/2 - ub ) * xb + p0
61 #
62 # which leads to ub = (fb+f0)/2-k*(pb-p0)/xb
63 #
64 def rescaleDomain(self):
65 x=self.dom.getX().copy()
66 for i in range(self.dom.getDim()):
67 x_inf=inf(x[i])
68 x_sup=sup(x[i])
69 if i == self.dom.getDim()-1:
70 x[i]=-self.WIDTH*(x[i]-x_sup)/(x_inf-x_sup)
71 else:
72 x[i]=self.WIDTH*(x[i]-x_inf)/(x_sup-x_inf)
73 self.dom.setX(x)
74 def getScalarMask(self,include_bottom=True):
75 x=self.dom.getX().copy()
76 x_inf=inf(x[self.dom.getDim()-1])
77 x_sup=sup(x[self.dom.getDim()-1])
78 out=whereZero(x[self.dom.getDim()-1]-x_sup)
79 if include_bottom: out+=whereZero(x[self.dom.getDim()-1]-x_inf)
80 return wherePositive(out)
81 def getVectorMask(self,include_bottom=True):
82 x=self.dom.getX().copy()
83 out=Vector(0.,Solution(self.dom))
84 for i in range(self.dom.getDim()):
85 x_inf=inf(x[i])
86 x_sup=sup(x[i])
87 if i != self.dom.getDim()-1: out[i]+=whereZero(x[i]-x_sup)
88 if i != self.dom.getDim()-1 or include_bottom: out[i]+=whereZero(x[i]-x_inf)
89 return wherePositive(out)
90
91 def setSolutionFixedBottom(self, p0, pb, f0, fb, k):
92 d=self.dom.getDim()
93 x=self.dom.getX()[d-1]
94 xb=inf(x)
95 u=Vector(0.,Solution(self.dom))+kronecker(d)[d-1]*((f0+fb)/2.-k*(pb-p0)/xb)
96 p=1./k*((fb-f0)/(xb*2.)* x**2 - (fb-f0)/2.*x)+(pb-p0)/xb*x + p0
97 f= ((fb-f0)/xb* x + f0)*kronecker(Function(self.dom))[d-1]
98 return u,p,f
99
100 def testConstF_FixedBottom_smallK(self):
101 k=1.e-8
102 mp=self.getScalarMask(include_bottom=True)
103 mv=self.getVectorMask(include_bottom=False)
104 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
105 p=p_ref*mp
106 u=u_ref*mv
107 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
108 df.setValue(g=f,
109 location_of_fixed_pressure=mp,
110 location_of_fixed_flux=mv,
111 permeability=Scalar(k,Function(self.dom)))
112 v,p=df.solve(u_ref,p)
113
114
115 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
116 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
117 def testConstF_FixedBottom_mediumK(self):
118 k=1.
119 mp=self.getScalarMask(include_bottom=True)
120 mv=self.getVectorMask(include_bottom=False)
121 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
122 p=p_ref*mp
123 u=u_ref*mv
124 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
125 df.setValue(g=f,
126 location_of_fixed_pressure=mp,
127 location_of_fixed_flux=mv,
128 permeability=Scalar(k,Function(self.dom)))
129 v,p=df.solve(u,p )
130
131
132 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
133 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
134
135 def testConstF_FixedBottom_largeK(self):
136 k=1.e8
137 mp=self.getScalarMask(include_bottom=True)
138 mv=self.getVectorMask(include_bottom=False)
139 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
140 p=p_ref*mp
141 u=u_ref*mv
142 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
143 df.setValue(g=f,
144 location_of_fixed_pressure=mp,
145 location_of_fixed_flux=mv,
146 permeability=Scalar(k,Function(self.dom)))
147 v,p=df.solve(u,p)
148 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
149 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
150
151 def testVarioF_FixedBottom_smallK(self):
152 k=1.e-8
153 mp=self.getScalarMask(include_bottom=True)
154 mv=self.getVectorMask(include_bottom=False)
155 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
156 p=p_ref*mp
157 u=u_ref*mv
158 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
159 #df.getSolverOptionsPressure().setVerbosityOn()
160 df.setValue(g=f,
161 location_of_fixed_pressure=mp,
162 location_of_fixed_flux=mv,
163 permeability=Scalar(k,Function(self.dom)))
164 v,p=df.solve(u,p)
165
166 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
167 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
168
169 def testVarioF_FixedBottom_mediumK(self):
170 k=1.
171 mp=self.getScalarMask(include_bottom=True)
172 mv=self.getVectorMask(include_bottom=False)
173 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
174 p=p_ref *mp
175 u=u_ref *mv
176 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
177 df.setValue(g=f,
178 location_of_fixed_pressure=mp,
179 location_of_fixed_flux=mv,
180 permeability=Scalar(k,Function(self.dom)))
181 v,p=df.solve(u,p)
182
183 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
184 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
185
186 def testVarioF_FixedBottom_largeK(self):
187 k=1.e8
188 mp=self.getScalarMask(include_bottom=True)
189 mv=self.getVectorMask(include_bottom=False)
190 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
191 p=p_ref*mp
192
193 u=u_ref*mv
194 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
195 df.setValue(g=f,
196 location_of_fixed_pressure=mp,
197 location_of_fixed_flux=mv,
198 permeability=Scalar(k,Function(self.dom)))
199 v,p=df.solve(u,p)
200
201 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
202 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
203
204 def testConstF_FreeBottom_smallK(self):
205 k=1.e-8
206 mp=self.getScalarMask(include_bottom=False)
207 mv=self.getVectorMask(include_bottom=True)
208 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
209 p=p_ref # *mp
210 u=u_ref*mv
211 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
212 df.setValue(g=f,
213 location_of_fixed_pressure=mp,
214 location_of_fixed_flux=mv,
215 permeability=Scalar(k,Function(self.dom)))
216
217 v,p=df.solve(u,p)
218
219 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
220 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
221
222 def testConstF_FreeBottom_mediumK(self):
223 k=1.
224 mp=self.getScalarMask(include_bottom=False)
225 mv=self.getVectorMask(include_bottom=True)
226 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
227 p=p_ref *mp
228 u=u_ref*mv
229 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
230 df.setValue(g=f,
231 location_of_fixed_pressure=mp,
232 location_of_fixed_flux=mv,
233 permeability=Scalar(k,Function(self.dom)))
234 v,p=df.solve(u,p)
235
236
237 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
238 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
239
240 def testConstF_FreeBottom_largeK(self):
241 k=1.e8
242 mp=self.getScalarMask(include_bottom=False)
243 mv=self.getVectorMask(include_bottom=True)
244 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=10.,k=k)
245 p=p_ref*mp
246 u=u_ref*mv
247 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
248 #df.getSolverOptionsPressure().setVerbosityOn()
249 df.setValue(g=f,
250 location_of_fixed_pressure=mp,
251 location_of_fixed_flux=mv,
252 permeability=Scalar(k,Function(self.dom)))
253 v,p=df.solve(u,p)
254 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
255 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
256
257 def testVarioF_FreeBottom_smallK(self):
258 k=1.e-8
259 mp=self.getScalarMask(include_bottom=False)
260 mv=self.getVectorMask(include_bottom=True)
261 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
262 p=p_ref*mp
263 u=u_ref*mv
264 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
265 df.setValue(g=f,
266 location_of_fixed_pressure=mp,
267 location_of_fixed_flux=mv,
268 permeability=Scalar(k,Function(self.dom)))
269 v,p=df.solve(u,p)
270
271 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
272 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
273
274 def testVarioF_FreeBottom_mediumK(self):
275 k=1.
276 mp=self.getScalarMask(include_bottom=False)
277 mv=self.getVectorMask(include_bottom=True)
278 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
279 p=p_ref*mp
280 u=u_ref*mv
281 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
282 df.setValue(g=f,
283 location_of_fixed_pressure=mp,
284 location_of_fixed_flux=mv,
285 permeability=Scalar(k,Function(self.dom)))
286 v,p=df.solve(u,p)
287 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
288 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
289
290 def testVarioF_FreeBottom_largeK(self):
291 k=1.e8
292 mp=self.getScalarMask(include_bottom=False)
293 mv=self.getVectorMask(include_bottom=True)
294 u_ref,p_ref,f=self.setSolutionFixedBottom(p0=2500,pb=4000.,f0=10.,fb=30.,k=k)
295 p=p_ref*mp
296 u=u_ref*mv
297 df=DarcyFlow(self.dom, verbose=VERBOSE, solver=self.SOLVER)
298 df.setValue(g=f,
299 location_of_fixed_pressure=mp,
300 location_of_fixed_flux=mv,
301 permeability=Scalar(k,Function(self.dom)))
302 v,p=df.solve(u,p)
303 self.assertTrue(Lsup(v-u_ref)<self.TEST_TOL*Lsup(u_ref), "flux error too big.")
304 self.assertTrue(Lsup(p-p_ref)<self.TEST_TOL*Lsup(p_ref), "pressure error too big.")
305
306 class Test_Darcy2D(Test_Darcy):
307 TOL=1e-4
308 WIDTH=1.
309 SOLVER=DarcyFlow.POST
310 def setUp(self):
311 NE=40 # wrning smaller NE may case a failure for VarioF tests due to discretization errors.
312 self.dom = Rectangle(NE,NE)
313 self.rescaleDomain()
314 def tearDown(self):
315 del self.dom
316
317 class Test_Darcy2D_EVAL(Test_Darcy2D):
318 TEST_TOL=0.01
319 SOLVER=DarcyFlow.EVAL
320
321 class Test_Darcy2D_POST(Test_Darcy2D):
322 TEST_TOL=1.e-3
323 SOLVER=DarcyFlow.POST
324
325 class Test_Darcy2D_SMOOTH(Test_Darcy2D):
326 TEST_TOL=0.01
327 SOLVER=DarcyFlow.SMOOTH
328
329 class Test_Darcy3D(Test_Darcy):
330 WIDTH=1.
331 SOLVER=DarcyFlow.POST
332 def setUp(self):
333 NE=29 # wrning smaller NE may case a failure for VarioF tests due to discretization errors.
334 self.dom = Brick(NE,NE,NE)
335 self.rescaleDomain()
336 def tearDown(self):
337 del self.dom
338
339 class Test_Darcy3D_EVAL(Test_Darcy3D):
340 TEST_TOL=0.01
341 SOLVER=DarcyFlow.EVAL
342
343 class Test_Darcy3D_POST(Test_Darcy3D):
344 TEST_TOL=1.e-3
345 SOLVER=DarcyFlow.POST
346
347 class Test_Darcy3D_SMOOTH(Test_Darcy3D):
348 TEST_TOL=0.01
349 SOLVER=DarcyFlow.SMOOTH
350
351
352 if __name__ == '__main__':
353 suite = unittest.TestSuite()
354 suite.addTest(unittest.makeSuite(Test_Darcy2D_SMOOTH))
355 suite.addTest(unittest.makeSuite(Test_Darcy2D_POST))
356 suite.addTest(unittest.makeSuite(Test_Darcy2D_EVAL))
357
358 suite.addTest(unittest.makeSuite(Test_Darcy3D_SMOOTH))
359 suite.addTest(unittest.makeSuite(Test_Darcy3D_POST))
360 suite.addTest(unittest.makeSuite(Test_Darcy3D_EVAL))
361 #suite.addTest(Test_Darcy2D_POST("testConstF_FixedBottom_largeK"))
362 s=unittest.TextTestRunner(verbosity=2).run(suite)
363 if not s.wasSuccessful(): sys.exit(1)
364
365

  ViewVC Help
Powered by ViewVC 1.1.26