/[escript]/trunk/escriptcore/test/python/test_condEval.py
ViewVC logotype

Contents of /trunk/escriptcore/test/python/test_condEval.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: 6267 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) 2010-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) 2010-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 __author__="Joel Fenwick, joelfenwick@uq.edu.au"
25
26 import esys.escriptcore.utestselect as unittest
27 from esys.escriptcore.testing import *
28 import os
29 import numpy
30 import sys
31 from esys.escript import *
32
33 class Test_CondEval(unittest.TestCase):
34 RES_TOL=1.e-7 # RES_TOLerance to compare results
35 r=getTestDomainFunctionSpace(3,3)
36 x=r.getDomain().getX()
37
38 def test_Constant(self):
39 d1=Data((1,3),self.x.getFunctionSpace())
40 d2=Data((2,4),self.x.getFunctionSpace())
41 m1=Data(1,self.x.getFunctionSpace())
42 mm1=Data(-1,self.x.getFunctionSpace())
43 self.assertTrue(Lsup(condEval(m1,d1,d2)-(1,3))<self.RES_TOL)
44 self.assertTrue(Lsup(condEval(mm1,d1,d2)-(2,4))<self.RES_TOL)
45 #Now we try lazy
46 d1=delay(d1)
47 d2=delay(d2)
48 m1=delay(m1)
49 mm1=delay(mm1)
50 self.assertTrue(Lsup(condEval(m1,d1,d2)-(1,3))<self.RES_TOL)
51 self.assertTrue(Lsup(condEval(mm1,d1,d2)-(2,4))<self.RES_TOL)
52
53 def test_Tagged(self):
54 t1=Data((1,3,5),self.x.getFunctionSpace())
55 t2=Data((2,4,6),self.x.getFunctionSpace())
56
57 t1.tag()
58 t2.tag()
59
60 t1.setTaggedValue(1, (9,8,7))
61 t1.setTaggedValue(2, (0,0,0))
62
63 t2.setTaggedValue(1, (-1,-1,-1))
64 t2.setTaggedValue(3, (-2,-2,-2))
65
66 mt1=Data(1,self.x.getFunctionSpace())
67 mt1.tag()
68 mt1.setTaggedValue(1,1)
69 mt1.setTaggedValue(4,0)
70 mt2=Data(-1,self.x.getFunctionSpace())
71 mt2.tag()
72 mt2.setTaggedValue(1,0)
73 mt2.setTaggedValue(4,-1)
74 z=condEval(mt1,t1,t2)
75 res=Data((1,3,5), self.x.getFunctionSpace())
76 res.setTaggedValue(1,(9,8,7))
77 res.setTaggedValue(4,(2,4,6))
78 self.assertTrue(Lsup(z-res)<self.RES_TOL)
79 z=condEval(mt2,t1,t2)
80 res=Data((2,4,6), self.x.getFunctionSpace())
81 res.setTaggedValue(1,(-1,-1,-1))
82 res.setTaggedValue(4,(2,4,6))
83 self.assertTrue(Lsup(z-res)<self.RES_TOL)
84
85 #Now we try the same but lazy
86 mt1=delay(mt1)
87 mt2=delay(mt2)
88 t1=delay(t1)
89 t2=delay(t2)
90 res=Data((1,3,5), self.x.getFunctionSpace())
91 res.setTaggedValue(1,(9,8,7))
92 res.setTaggedValue(4,(2,4,6))
93 res=delay(res)
94
95 z=condEval(mt1,t1,t2)
96 y=z-res
97 Lsup(y)
98 self.assertTrue(Lsup(condEval(mt1,t1,t2)-res)<self.RES_TOL)
99 res=Data((2,4,6), self.x.getFunctionSpace())
100 res.setTaggedValue(1,(-1,-1,-1))
101 res.setTaggedValue(4,(2,4,6))
102 self.assertTrue(Lsup(condEval(mt2,t1,t2)-res)<self.RES_TOL)
103
104 def test_Expanded(self):
105 e1=Data((1,3,5),self.x.getFunctionSpace(),True)
106 e2=Data((2,4,6),self.x.getFunctionSpace(),False)
107
108 me1=Data(1,self.x.getFunctionSpace())*wherePositive(self.x-2)
109 me2=Data(1,self.x.getFunctionSpace())*(1-wherePositive(self.x-2))
110
111 self.assertTrue(Lsup(condEval(me1, e1,e2)+condEval(me2, e1,e2)-(3,7,11))<self.RES_TOL)
112
113 le1=delay(e1)
114 le2=delay(e2)
115
116 ml1=delay(me1)
117 ml2=delay(me2)
118
119 z=condEval(ml1, le1,le2)
120
121 self.assertTrue(Lsup(condEval(ml1, le1,le2)+condEval(ml2, le1,le2)-(3,7,11))<self.RES_TOL)
122
123 def test_Errors(self):
124 d1=Data((1,3),self.x.getFunctionSpace())
125 d2=Data((2,4),self.x.getFunctionSpace())
126 d3=Data((2,4,5),self.x.getFunctionSpace())
127 m=Data(1,self.x.getFunctionSpace())
128 mS1=Data((1,0),self.x.getFunctionSpace())
129 #Non-scalar mask
130 self.assertRaises(RuntimeError,condEval, mS1,d1,d2)
131 #shape mismatch
132 self.assertRaises(RuntimeError,condEval, m, d1, d3)
133
134 def test_promote(self):
135 #This is not an exhaustive test of all possible promotion combinataions
136 for v in [False, True]:
137 mt1=Data(2,self.x.getFunctionSpace())
138 mt1.tag()
139 if v:
140 mt1=delay(mt1)
141 d1=Data((1,3),self.x.getFunctionSpace())
142 d2=Data((67,89), self.x.getFunctionSpace(),True)
143 self.assertTrue(Lsup(condEval(mt1,d1,d2)-(1,3))<self.RES_TOL)
144
145 me1=Data(2,self.x.getFunctionSpace(),True)
146 d1=Data((1,3),self.x.getFunctionSpace())
147 if v:
148 d1=delay(d1)
149 d2=Data((7,19),self.x.getFunctionSpace())
150 d2.tag()
151 self.assertTrue(Lsup(condEval(me1,d1,d2)-(1,3))<self.RES_TOL)
152
153 mc1=Data(0,self.x.getFunctionSpace())
154 d1=Data((1,3),self.x.getFunctionSpace())
155 d1.tag()
156 d2=Data((7,19),self.x.getFunctionSpace())
157 d2.tag()
158 if v:
159 d2=delay(d2)
160 self.assertTrue(Lsup(condEval(mc1,d1,d2)-(7,19))<self.RES_TOL)
161
162 mt1=Data(2,self.x.getFunctionSpace())
163 mt1.tag()
164 if v:
165 mt1=delay(mt1)
166 d1=Data((1,3),self.x.getFunctionSpace())
167 d2=Data((67,89), self.x.getFunctionSpace())
168 self.assertTrue(Lsup(condEval(mt1,d1,d2)-(1,3))<self.RES_TOL)
169
170
171 if __name__ == '__main__':
172 run_tests(__name__, exit_on_failure=True)

  ViewVC Help
Powered by ViewVC 1.1.26