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

Contents of /trunk/escript/test/python/test_condEval.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3892 - (show annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 6203 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



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

  ViewVC Help
Powered by ViewVC 1.1.26