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

Contents of /trunk/escriptcore/test/python/run_symbolic.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3491 - (show annotations)
Wed Mar 30 06:13:34 2011 UTC (8 years, 5 months ago) by caltinay
Original Path: branches/symbolic_from_3470/escript/test/python/run_symbolic.py
File MIME type: text/x-python
File size: 6183 byte(s)
Added basic tests for added symbolic functions.

1
2 ########################################################
3 #
4 # Copyright (c) 2003-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) 2003-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 """
23 Test suite for the escript.symbolic module
24
25 :var __author__: name of author
26 :var __copyright__: copyrights
27 :var __license__: licence agreement
28 :var __url__: url entry point on documentation
29 :var __version__: version
30 :var __date__: date of the version
31 """
32
33 __author__="Cihan Altinay"
34
35 from esys.escript import Data, RandomData, FunctionSpace
36 from esys.escript.symbolic import *
37 import unittest
38
39 class SymbolicTestCase(unittest.TestCase):
40
41 def test_Evaluator(self):
42 e=Evaluator()
43 self.assertEqual(len(e), 0, "empty evaluator returns wrong length")
44 self.assertEqual(e.evaluate(), (), "result of evaluate() not empty")
45
46 x=Symbol('x')
47 e=Evaluator(x*x, x**3)
48 self.assertEqual(len(e), 2, "evaluator returns wrong length")
49 self.assertEqual(e[0], x*x, "first expression wrong")
50 self.assertEqual(e[1], x**3, "second expression wrong")
51
52 f=e.addExpression(x**4)
53 self.assertEqual(len(e), 3, "wrong length after addExpression()")
54 self.assertEqual(e, f, "addExpression() did not return self")
55 self.assertEqual(e[2], x**4, "third expression wrong")
56
57 e+=x**5
58 self.assertEqual(len(e), 4, "wrong length after += operator")
59 self.assertEqual(e[3], x**5, "fourth expression wrong")
60
61 self.assertRaises(RuntimeError, e.evaluate)
62 f=e.subs(x=2)
63 self.assertEqual(e, f, "subs() did not return self")
64 self.assertEqual(e.evaluate(), (4,8,16,32), "wrong result after subs()")
65 self.assertEqual(e(x=3), (9,27,81,243), "wrong result after __call__")
66
67 xx=RandomData((4,), FunctionSpace())
68 ref=[d.toListOfTuples() for d in (xx**2, xx**3, xx**4, xx**5)]
69 res=e(x=xx)
70 for d in res:
71 self.assertTrue(isinstance(d, Data), "substituted expression not a Data object")
72 res=[x.toListOfTuples() for x in res]
73 self.assertEqual(res, ref, "wrong result after substitution with Data object")
74
75 #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
76
77 def test_Functions(self):
78 import esys.escript as es
79 x=Symbol('x')
80 xx=RandomData((4,4), FunctionSpace())
81 TOL_DIGITS=8
82
83 y=wherePositive(x)
84 ref=es.wherePositive(xx).toListOfTuples()
85 res=Evaluator(y)(x=xx)[0].toListOfTuples()
86 self.assertEqual(res, ref, "wrong result for wherePositive()")
87
88 y=whereNonPositive(x)
89 ref=es.whereNonPositive(xx).toListOfTuples()
90 res=Evaluator(y)(x=xx)[0].toListOfTuples()
91 self.assertEqual(res, ref, "wrong result for whereNonPositive()")
92
93 y=whereNegative(x)
94 ref=es.whereNegative(xx).toListOfTuples()
95 res=Evaluator(y)(x=xx)[0].toListOfTuples()
96 self.assertEqual(res, ref, "wrong result for whereNegative()")
97
98 y=whereNonNegative(x)
99 ref=es.whereNonNegative(xx).toListOfTuples()
100 res=Evaluator(y)(x=xx)[0].toListOfTuples()
101 self.assertEqual(res, ref, "wrong result for whereNonNegative()")
102
103 y=whereZero(x)
104 ref=es.whereZero(xx).toListOfTuples()
105 res=Evaluator(y)(x=xx)[0].toListOfTuples()
106 self.assertEqual(res, ref, "wrong result for whereZero()")
107
108 y=whereNonZero(x)
109 ref=es.whereNonZero(xx).toListOfTuples()
110 res=Evaluator(y)(x=xx)[0].toListOfTuples()
111 self.assertEqual(res, ref, "wrong result for whereNonZero()")
112
113 y=log10(x)
114 ref=es.log10(xx)
115 res=Evaluator(y)(x=xx)[0]
116 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for log10()")
117
118 y=inverse(x)
119 ref=es.inverse(xx)
120 res=Evaluator(y)(x=xx)[0]
121 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for inverse()")
122
123 y=minval(x)
124 ref=es.minval(xx)
125 res=Evaluator(y)(x=xx)[0]
126 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for minval()")
127
128 y=maxval(x)
129 ref=es.maxval(xx)
130 res=Evaluator(y)(x=xx)[0]
131 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for maxval()")
132
133 y=trace(x)
134 ref=es.trace(xx)
135 res=Evaluator(y)(x=xx)[0]
136 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for trace()")
137
138 y=transpose(x)
139 ref=es.transpose(xx)
140 res=Evaluator(y)(x=xx)[0]
141 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for transpose()")
142
143 y=symmetric(x)
144 ref=es.symmetric(xx)
145 res=Evaluator(y)(x=xx)[0]
146 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for symmetric()")
147
148 y=nonsymmetric(x)
149 ref=es.nonsymmetric(xx)
150 res=Evaluator(y)(x=xx)[0]
151 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for nonsymmetric()")
152
153 y=swap_axes(x)
154 ref=es.swap_axes(xx)
155 res=Evaluator(y)(x=xx)[0]
156 self.assertAlmostEqual(es.Lsup(res-ref), 0.0, TOL_DIGITS, "wrong result for swap_axes()")
157
158 #y=grad(x)
159 #ref=es.grad(xx).toListOfTuples()
160 #res=Evaluator(y)(x=xx)[0].toListOfTuples()
161 #self.assertEqual(res, ref, "wrong result for grad()")
162
163
164 if __name__ == "__main__":
165 suite = unittest.TestSuite()
166 suite.addTest(unittest.makeSuite(SymbolicTestCase))
167 s=unittest.TextTestRunner(verbosity=2).run(suite)
168 if not s.wasSuccessful(): sys.exit(1)
169

  ViewVC Help
Powered by ViewVC 1.1.26