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=[x.toListOfTuples() for x 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 |
if __name__ == "__main__": |
78 |
suite = unittest.TestSuite() |
79 |
suite.addTest(unittest.makeSuite(SymbolicTestCase)) |
80 |
s=unittest.TextTestRunner(verbosity=2).run(suite) |
81 |
if not s.wasSuccessful(): sys.exit(1) |
82 |
|