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

  ViewVC Help
Powered by ViewVC 1.1.26