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

  ViewVC Help
Powered by ViewVC 1.1.26