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

  ViewVC Help
Powered by ViewVC 1.1.26