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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (4 years, 10 months ago) by sshaw
File MIME type: text/x-python
File size: 12315 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) 2003-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) 2003-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
25
26 import logging
27 import esys.escriptcore.utestselect as unittest
28 from esys.escriptcore.testing import *
29 from esys.downunder.coordinates import *
30 from esys.ripley import Brick, Rectangle
31 import esys.escript
32 from esys.escript import Lsup, sin, cos
33 import esys.escript.unitsSI as U
34 import os
35 import sys
36 from math import pi
37 # this is mainly to avoid warning messages
38 logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
39
40 try:
41 WORKDIR=os.environ['DOWNUNDER_WORKDIR']
42 except KeyError:
43 WORKDIR='.'
44
45 NE=20
46 RTOL=1e-8
47
48 class TestCoordinates(unittest.TestCase):
49 def test_CartesianReferenceSystem(self):
50 cs=CartesianReferenceSystem()
51
52 self.assertEqual(cs.getName(), "CARTESIAN", "wrong name")
53 self.assertTrue(str(cs).startswith("CARTESIAN (id"), "wrong str")
54 self.assertTrue(cs == cs, "wrong cs == cs")
55 self.assertFalse(cs != cs, "wrong cs == cs")
56 self.assertFalse(cs == None, "wrong cs == None")
57 self.assertTrue(cs != None, "wrong cs == None")
58
59 cs2=CartesianReferenceSystem()
60 self.assertTrue(cs == cs2, "wrong cs == cs2")
61 self.assertFalse(cs != cs2, "wrong cs == cs2")
62 self.assertTrue(cs2 == cs, "wrong cs2 == cs")
63 self.assertFalse(cs2 != cs, "wrong cs2 == cs")
64
65 def test_GeodeticReferenceSystem(self):
66
67 cs = GeodeticReferenceSystem(a= 4*U.m, f=0.75, angular_unit=0.1, name="A1")
68 self.assertEqual(cs.getName(), "A1", "wrong name")
69 self.assertEqual(cs.getAngularUnit(), 0.1, "wrong angular_unit")
70 self.assertEqual(cs.getFlattening(), 0.75, "wrong flattening")
71 self.assertEqual(cs.getSemiMajorAxis(), 4., "wrong SemiMajorAxis")
72 self.assertEqual(cs.getSemiMinorAxis(), 1., "wrong SemiMinorAxis")
73 self.assertTrue(cs == cs, "wrong cs == cs")
74 self.assertFalse(cs != cs, "wrong cs == cs")
75 self.assertFalse(cs == None, "wrong cs == None")
76 self.assertTrue(cs != None, "wrong cs == None")
77
78 # lets try a few stupid things:
79 self.assertRaises(ValueError, GeodeticReferenceSystem, a= -4)
80 self.assertRaises(ValueError, GeodeticReferenceSystem, f=-0.75)
81 self.assertRaises(ValueError, GeodeticReferenceSystem, f=1.)
82 self.assertRaises(ValueError, GeodeticReferenceSystem, angular_unit=-0.1)
83
84 # cs == CartesianReferenceSystem() ?
85 cs2=CartesianReferenceSystem()
86 self.assertFalse(cs == cs2, "wrong cs == cs2")
87 self.assertTrue(cs != cs2, "wrong cs == cs2")
88 self.assertFalse(cs2 == cs, "wrong cs2 == cs")
89 self.assertTrue(cs2 != cs, "wrong cs2 == cs")
90
91 cs2 = GeodeticReferenceSystem(a= 4*U.m, f=0.75, angular_unit=0.1, name="A2")
92 self.assertTrue(cs == cs2, "wrong cs == cs2")
93 self.assertFalse(cs != cs2, "wrong cs == cs2")
94 self.assertTrue(cs2 == cs, "wrong cs2 == cs")
95 self.assertFalse(cs2 != cs, "wrong cs2 == cs")
96
97 # different semi majr axis?
98 cs2 = GeodeticReferenceSystem(a= 2*U.m, f=0.75, angular_unit=0.1, name="A2")
99 self.assertFalse(cs == cs2, "wrong cs == cs2")
100 self.assertTrue(cs != cs2, "wrong cs == cs2")
101 self.assertFalse(cs2 == cs, "wrong cs2 == cs")
102 self.assertTrue(cs2 != cs, "wrong cs2 == cs")
103
104 # different flattening
105 cs2 = GeodeticReferenceSystem(a= 4*U.m, f=0.7, angular_unit=0.1, name="A2")
106 self.assertFalse(cs == cs2, "wrong cs == cs2")
107 self.assertTrue(cs != cs2, "wrong cs == cs2")
108 self.assertFalse(cs2 == cs, "wrong cs2 == cs")
109 self.assertTrue(cs2 != cs, "wrong cs2 == cs")
110
111 # different angular_unit
112 cs2 = GeodeticReferenceSystem(a= 4*U.m, f=0.75, angular_unit=0.2, name="A2")
113 self.assertFalse(cs == cs2, "wrong cs == cs2")
114 self.assertTrue(cs != cs2, "wrong cs == cs2")
115 self.assertFalse(cs2 == cs, "wrong cs2 == cs")
116 self.assertTrue(cs2 != cs, "wrong cs2 == cs")
117
118 sp=SphericalReferenceSystem(R=1*U.km)
119 self.assertEqual(sp.getName(), "SPHERE", "wrong name")
120 self.assertAlmostEqual(sp.getAngularUnit(), 1*U.DEG, msg="wrong angular_unit")
121 self.assertAlmostEqual(sp.getFlattening(), 0., msg="wrong flattening")
122 self.assertAlmostEqual(sp.getSemiMajorAxis(), 1000.,msg= "wrong SemiMajorAxis")
123
124 sp=SphericalReferenceSystem()
125 self.assertEqual(sp.getName(), "SPHERE", msg= "wrong name")
126 self.assertAlmostEqual(sp.getAngularUnit(), 1*U.DEG, msg="wrong angular_unit")
127 self.assertAlmostEqual(sp.getFlattening(), 0., msg="wrong flattening")
128 self.assertAlmostEqual(sp.getSemiMajorAxis(), 6378137, msg="wrong SemiMajorAxis")
129
130
131 sp=WGS84ReferenceSystem()
132 self.assertEqual(sp.getName(), "WGS84", msg= "wrong name")
133 self.assertAlmostEqual(sp.getAngularUnit(), 1*U.DEG, msg="wrong angular_unit")
134 self.assertAlmostEqual(sp.getFlattening(), 0.0033528106647474805, msg="wrong flattening")
135 self.assertAlmostEqual(sp.getSemiMajorAxis(), 6378137, msg="wrong SemiMajorAxis")
136
137 sp=GRS80ReferenceSystem()
138 self.assertEqual(sp.getName(), "GRS80", msg= "wrong name")
139 self.assertAlmostEqual(sp.getAngularUnit(), 1*U.DEG, msg="wrong angular_unit")
140 self.assertAlmostEqual(sp.getFlattening(), 0.003352810681182319, msg="wrong flattening")
141 self.assertAlmostEqual(sp.getSemiMajorAxis(), 6378137, msg="wrong SemiMajorAxis")
142
143 def test_CartesianTransformation3D(self):
144
145 dom=Brick(NE,NE,NE, l0=10, l1=10, l2=10)
146
147 cs=CartesianReferenceSystem()
148 tf=cs.createTransformation(dom)
149
150 self.assertEqual(tf.getReferenceSystem(),cs , "wrong reference")
151 self.assertEqual(tf.getDomain(),dom , "wrong reference")
152 self.assertTrue(tf.isCartesian(), "wrong isCartesian check")
153
154
155 v=tf.getVolumeFactor()
156 self.assertTrue(isinstance(v, esys.escript.Data), "wrong volume factor type")
157 self.assertEqual(v.getFunctionSpace(), esys.escript.Function(dom), "wrong volume factor type")
158 error=Lsup(v-1.)
159 self.assertTrue(error<=RTOL, "volume factor")
160
161 s=tf.getScalingFactors()
162 self.assertTrue(isinstance(s, esys.escript.Data), "scaling factor type")
163 self.assertEqual(s.getShape(), (dom.getDim(),), "scaling factor length")
164 self.assertEqual(s.getFunctionSpace(), esys.escript.Function(dom), "wrong 0-th scaling factor type")
165
166 error=Lsup(s[0]-1.)
167 self.assertTrue(error<=RTOL, "0-th scaling factor")
168 error=Lsup(s[1]-1.)
169 self.assertTrue(error<=RTOL, "1-th scaling factor")
170 error=Lsup(s[2]-1.)
171 self.assertTrue(error<=RTOL, "2-th scaling factor")
172
173 def test_CartesianTransformation2D(self):
174
175 dom=Rectangle(NE,NE, l0=10, l1=10)
176
177 cs=CartesianReferenceSystem()
178 tf=cs.createTransformation(dom)
179
180 self.assertEqual(tf.getReferenceSystem(),cs , "wrong reference")
181 self.assertEqual(tf.getDomain(),dom , "wrong reference")
182 self.assertTrue(tf.isCartesian(), "wrong isCartesian check")
183
184
185 v=tf.getVolumeFactor()
186 self.assertTrue(isinstance(v, esys.escript.Data), "wrong volume factor type")
187 self.assertEqual(v.getFunctionSpace(), esys.escript.Function(dom), "wrong volume factor type")
188 error=Lsup(v-1.)
189 self.assertTrue(error<=RTOL, "volume factor")
190
191 s=tf.getScalingFactors()
192 self.assertTrue(isinstance(s, esys.escript.Data), "scaling factor type")
193 self.assertEqual(s.getShape(), (dom.getDim(),), "scaling factor length")
194 self.assertEqual(s.getFunctionSpace(), esys.escript.Function(dom), "wrong 0-th scaling factor type")
195
196 error=Lsup(s[0]-1.)
197 self.assertTrue(error<=RTOL, "0-th scaling factor")
198
199 error=Lsup(s[1]-1.)
200 self.assertTrue(error<=RTOL, "1-th scaling factor")
201
202
203
204 def test_SphericalTransformation3D(self):
205
206 dom=Brick(NE,NE,NE, l0=90, l1=45, l2=10.)
207
208 cs=SphericalReferenceSystem()
209 tf=cs.createTransformation(dom)
210
211 self.assertEqual(tf.getReferenceSystem(),cs , "wrong reference")
212 self.assertEqual(tf.getDomain(),dom , "wrong reference")
213 self.assertFalse(tf.isCartesian(), "wrong isCartesian check")
214
215 R=6378137.0
216 x=esys.escript.Function(dom).getX()
217 phi=(90.-x[1])/180.*pi
218 lam=x[0]/180.*pi
219 h=x[2]*1000.
220 r=h+R
221
222 v=tf.getVolumeFactor()
223 self.assertTrue(isinstance(v, esys.escript.Data), "wrong volume factor type")
224 self.assertEqual(v.getFunctionSpace(), esys.escript.Function(dom), "wrong volume factor type")
225 error=Lsup(v- r**2*sin(phi)*(pi/180.)**2*1000. )
226 self.assertTrue(error<=RTOL * R*R*(pi/180.)**2*1000., "volume factor")
227
228 s=tf.getScalingFactors()
229 self.assertTrue(isinstance(s, esys.escript.Data), "scaling factor type")
230 self.assertEqual(s.getShape(), (dom.getDim(),), "scaling factor length")
231 self.assertEqual(s.getFunctionSpace(), esys.escript.Function(dom), "wrong 0-th scaling factor type")
232
233 error=Lsup(s[1]-1/r/pi*180.)
234 self.assertTrue(error<=RTOL/R/pi*180., "0-th scaling factor")
235
236 error=Lsup(s[0]-1/(r*sin(phi))/pi*180.)
237 self.assertTrue(error<=RTOL/R/pi*180., "1-th scaling factor")
238
239 error=Lsup(s[2]-1./1000.)
240 self.assertTrue(error<=RTOL/1000., "2-th scaling factor")
241
242 def test_SphericalTransformation2D(self):
243
244 dom=Rectangle(NE,NE, l0=45., l1=10.)
245
246 cs=SphericalReferenceSystem()
247 tf=cs.createTransformation(dom)
248
249 self.assertEqual(tf.getReferenceSystem(),cs , "wrong reference")
250 self.assertEqual(tf.getDomain(),dom , "wrong reference")
251 self.assertFalse(tf.isCartesian(), "wrong isCartesian check")
252
253 R=6378137.0
254 x=esys.escript.Function(dom).getX()
255 phi=(90.-x[0])/180.*pi
256 h=x[1]*1000.
257 r=h+R
258
259 v=tf.getVolumeFactor()
260 self.assertTrue(isinstance(v, esys.escript.Data), "wrong volume factor type")
261 self.assertEqual(v.getFunctionSpace(), esys.escript.Function(dom), "wrong volume factor type")
262 error=Lsup(v-r*pi/180.*1000.)
263 self.assertTrue(error<=RTOL*R, "volume factor")
264
265 s=tf.getScalingFactors()
266 self.assertTrue(isinstance(s, esys.escript.Data), "scaling factor type")
267 self.assertEqual(s.getShape(), (dom.getDim(),), "scaling factor length")
268 self.assertEqual(s.getFunctionSpace(), esys.escript.Function(dom), "wrong 0-th scaling factor type")
269
270 error=Lsup(s[0]-1./r/pi*180.)
271 self.assertTrue(error<=RTOL/R/pi*180., "0-th scaling factor")
272 error=Lsup(s[1]-1./1000)
273 self.assertTrue(error<=RTOL/1000., "1-th scaling factor")
274
275
276 if __name__ == '__main__':
277 run_tests(__name__, exit_on_failure=True)
278

  ViewVC Help
Powered by ViewVC 1.1.26