/[escript]/trunk/downunder/test/python/run_mappings.py
ViewVC logotype

Contents of /trunk/downunder/test/python/run_mappings.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (5 years, 2 months ago) by sshaw
File MIME type: text/x-python
File size: 5117 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) 2012-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) 2012-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 import esys.escriptcore.utestselect as unittest
25 from esys.escriptcore.testing import *
26 import sys
27 from esys.escript import *
28 from esys.downunder.mappings import *
29 from esys.ripley import Rectangle
30 import numpy
31
32 class TestLinearMapping(unittest.TestCase):
33 def setUp(self):
34 self.alpha = 4.2
35 self.p0 = 1.75
36 self.mapping=LinearMapping(self.alpha, self.p0)
37
38 def test_invalid_alpha(self):
39 self.assertRaises(Exception, LinearMapping, 0)
40
41 def test_value(self):
42 v=self.mapping.getValue(1.23)
43 ref=self.alpha*1.23+self.p0
44 self.assertAlmostEqual(v, ref)
45
46 def test_derivative(self):
47 v=self.mapping.getDerivative(1.23)
48 ref=self.alpha
49 self.assertAlmostEqual(v, ref)
50
51 def test_inverse(self):
52 v=self.mapping.getInverse(1.23)
53 ref=(1.23-self.p0)/self.alpha
54 self.assertAlmostEqual(v, ref)
55
56 class TestAcousticVelocityMapping(unittest.TestCase):
57 def setUp(self):
58 self.fs=Function(Rectangle(40,40))
59 def tearDown(self):
60 del self.fs
61
62 def test_case1(self):
63 Q0=100.
64 V0=3000
65 QQ=Scalar(Q0, self.fs)
66 VV=Scalar(V0, self.fs)
67 mp=AcousticVelocityMapping(VV, QQ)
68
69 # get Value test
70 s0=mp.getValue( (0,0))
71 s0_ref=1/(V0*complex(1,-1/(2*Q0)))**2
72 self.assertLess( Lsup(s0[0]-s0_ref.real), 1e-8 * abs(s0_ref.real))
73 self.assertLess( Lsup(s0[1]-s0_ref.imag), 1e-8 * abs(s0_ref.imag))
74
75 M0=numpy.log(s0_ref)
76 s1=mp.getValue( (1.,0))
77 s1_ref=numpy.exp(complex(1.,0)+M0)
78 self.assertLess( Lsup(s1[0]-s1_ref.real), 1e-8 * abs(s1_ref.real))
79 self.assertLess( Lsup(s1[1]-s1_ref.imag), 1e-8 * abs(s1_ref.imag))
80
81 s2=mp.getValue( (0,1.))
82 s2_ref=numpy.exp(complex(0,1,)+M0)
83 self.assertLess( Lsup(s2[0]-s2_ref.real), 1e-8 * abs(s2_ref.real))
84 self.assertLess( Lsup(s2[1]-s2_ref.imag), 1e-8 * abs(s2_ref.imag))
85
86 # get derivative
87 m0=0.5
88 m1=0.01
89 STEP=0.5e-4
90
91 DS=mp.getDerivative((m0, m1))
92 s0=mp.getValue((m0, m1))
93 s1=mp.getValue((m0+STEP, m1))
94 DS_ref = (s1-s0)/STEP
95 self.assertLess( Lsup(DS[0,0]-DS_ref[0]), 1e-2 * Lsup(DS_ref[0]))
96 self.assertLess( Lsup(DS[1,0]-DS_ref[1]), 1e-2 * Lsup(DS_ref[1]))
97
98 s1=mp.getValue((m0, m1+STEP))
99 DS_ref= (s1-s0)/STEP
100 self.assertTrue( Lsup(DS[1,1]-DS_ref[1]), 1e-2 * Lsup(DS_ref[1]))
101 self.assertLess( Lsup(DS[0,1]-DS_ref[0]), 1e-2 * Lsup(DS_ref[0]))
102 # get inverse
103 s0_ref=1/(V0*complex(1,-1/(2*Q0)))**2
104 m0=mp.getInverse( (s0_ref.real,s0_ref.imag))
105 self.assertLess( Lsup(m0[0]), 1e-14)
106 self.assertLess( Lsup(m0[1]), 1e-14)
107
108 class TestBoundedRangeMapping(unittest.TestCase):
109 def setUp(self):
110 self.alpha=4.2
111 self.mapping=BoundedRangeMapping(1.5, 3.5)
112
113 def test_invalid_range(self):
114 self.assertRaises(ValueError, BoundedRangeMapping, 1, 0)
115
116 def test_min_value(self):
117 v=self.mapping.getValue(-10000)
118 ref=1.5
119 self.assertAlmostEqual(v, ref)
120
121 def test_max_value(self):
122 v=self.mapping.getValue(10000)
123 ref=3.5
124 self.assertAlmostEqual(v, ref)
125
126 def test_zero_value(self):
127 v=self.mapping.getValue(0)
128 ref=2.5
129 self.assertAlmostEqual(v, ref)
130
131 def test_min_derivative(self):
132 v=self.mapping.getDerivative(-10000)
133 ref=0.
134 self.assertAlmostEqual(v, ref)
135
136 def test_max_derivative(self):
137 v=self.mapping.getDerivative(10000)
138 ref=0.
139 self.assertAlmostEqual(v, ref)
140
141 def test_zero_derivative(self):
142 v=self.mapping.getDerivative(0)
143 ref=1.
144 self.assertAlmostEqual(v, ref)
145
146 def test_inverse_toolow(self):
147 self.assertRaises(ValueError, self.mapping.getInverse, 1.5)
148
149 def test_inverse_toohigh(self):
150 self.assertRaises(ValueError, self.mapping.getInverse, 3.5)
151
152 def test_inverse(self):
153 v=self.mapping.getInverse(2.5)
154 ref=0.
155 self.assertAlmostEqual(v, ref)
156
157 if __name__ == '__main__':
158 run_tests(__name__, exit_on_failure=True)
159

  ViewVC Help
Powered by ViewVC 1.1.26