/[escript]/trunk/speckley/test/python/run_SpeckleyRipleyCoupler.py
ViewVC logotype

Contents of /trunk/speckley/test/python/run_SpeckleyRipleyCoupler.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5707 - (show annotations)
Mon Jun 29 03:59:06 2015 UTC (3 years, 8 months ago) by sshaw
File MIME type: text/x-python
File size: 11690 byte(s)
adding copyright headers to files without copyright info, moved header to top of file in some cases where it wasn't
1 ##############################################################################
2 #
3 # Copyright (c) 2003-2015 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Open Software License version 3.0
8 # http://www.opensource.org/licenses/osl-3.0.php
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15 from __future__ import division, print_function
16
17 __copyright__="""Copyright (c) 2003-2015 by The 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 os
25 import esys.escriptcore.utestselect as unittest
26 from esys.escriptcore.testing import *
27 from esys.escript import *
28 from esys.speckley import Rectangle as sRectangle, Brick as sBrick
29
30 #domain families may not be present
31 HAS_RIPLEY = True
32 HAS_FINLEY = True
33 try:
34 from esys.ripley import Rectangle as rRectangle, Brick as rBrick
35 except ImportError as e:
36 HAS_RIPLEY = False
37
38 try:
39 from esys.finley import Rectangle as fRectangle
40 except ImportError as e:
41 HAS_FINLEY = False
42
43 if HAS_RIPLEY:
44 from esys.escriptcore.domainCouplers import SpeckleyToRipley
45
46
47 @unittest.skipIf(not HAS_RIPLEY, "Ripley domains not present")
48 class Test_ripleyCoupler(unittest.TestCase):
49
50 def calculateVariance(self, s, r):
51 actual = r.getX()
52 sinput = s.getX()
53 sX = interpolate(sinput, Function(s))
54 return actual - interpolate(sX, Function(r)) #actual - interpo...
55
56 def badInterpolations(self, speckley, ripley):
57 FS = Function(speckley)
58 FR = Function(ripley)
59 #bad speck -> good rip
60 with self.assertRaises(RuntimeError):
61 interpolate(speckley.getX(), FR)
62 with self.assertRaises(RuntimeError):
63 interpolate(Data(5, ReducedFunction(speckley)), FR)
64 #good speck -> bad rip
65 with self.assertRaises(RuntimeError):
66 interpolate(Data(5, FS), ReducedFunction(ripley))
67 with self.assertRaises(RuntimeError):
68 interpolate(Data(5, FS), ContinuousFunction(ripley))
69
70 def test_Rectangle_non_Function(self):
71 for order in range(2, 11):
72 coupler = SpeckleyToRipley(2, (2*getMPISizeWorld()*order,order),
73 order=order, lengths=[3.*getMPISizeWorld(),2.])
74 self.badInterpolations(coupler.getSpeckley(), coupler.getRipley())
75
76 def test_Brick_non_Function(self):
77 for order in range(2, 11):
78 #values here are arbitrary, just has to be Bricks
79 coupler = SpeckleyToRipley(3, (2*getMPISizeWorld()*order,order,order),
80 order=order, lengths=[3.*getMPISizeWorld(),2.,2.])
81 self.badInterpolations(coupler.getSpeckley(), coupler.getRipley())
82
83
84 def test_Rectangle(self):
85 for order in range(2,11):
86 coupler = SpeckleyToRipley(2, (2*getMPISizeWorld()*order,order),
87 order=order, lengths=[3.*getMPISizeWorld(),2.])
88 d = self.calculateVariance(coupler.getSpeckley(), coupler.getRipley())
89 res = Lsup(d)
90 self.assertLess(res, 1e-10,
91 "Rectangles of order %d failed to interpolate correctly"%order +\
92 ", variance of %g"%res)
93
94 coupler = SpeckleyToRipley(2, (order, 2*getMPISizeWorld()*order),
95 order=order, lengths=[2.,3.*getMPISizeWorld()])
96 a = self.calculateVariance(coupler.getSpeckley(), coupler.getRipley())
97 self.assertLess(Lsup(a), 1e-10,
98 "Rectangles of order %d failed to interpolate correctly"%order +\
99 ", variance of %g"%Lsup(a))
100
101 @unittest.skipIf(getMPISizeWorld() < 4, "requires at least 4 ranks")
102 def test_MultiDimSplit_Rectangle(self):
103 ranks = getMPISizeWorld()
104 squares = [4,9,16,25,36] #could do more, unlikely to test though
105 subdivs = [0,0]
106 loop = 0
107 if ranks not in squares:
108 if ranks % 3 == 0:
109 subdivs[0] = 3
110 subdivs[1] = ranks//3
111 loop = 2
112 elif ranks % 2 == 0:
113 subdivs[0] = 2
114 subdivs[1] = ranks//2
115 loop = 1
116 else:
117 raise unittest.SkipTest("inappropriate number of ranks")
118 else:
119 subdivs = [squares.index(ranks)+2]*2
120 loop = 2
121
122 for order in range(2,11):
123 divs = subdivs[:]
124 for i in range(loop):
125 r = rRectangle(2*divs[0]*order-1, 2*divs[1]*order-1,
126 d0=divs[0], d1=divs[1])
127 s = sRectangle(order, divs[0]*2, divs[1]*2,
128 d0=divs[0], d1=divs[1])
129 d = self.calculateVariance(s, r)
130 res = Lsup(d)
131 self.assertLess(res, 1e-10,
132 "".join(["Rectangles of order %d with "%order,
133 "subdivisons %s "%divs,
134 "failed to interpolate correctly",
135 ", variance of %g"%res]))
136 divs.append(divs.pop(0))
137
138 @unittest.skipIf(getMPISizeWorld() < 4, "requires at least 4 ranks")
139 def test_BiaxialSplits_Brick(self):
140 ranks = getMPISizeWorld()
141 squares = [4,9,16,25,36] #could do more, unlikely to test though
142 error_msg = "Brick of order {0} and subdivisions {1} failed to"+\
143 " interpolate correctly, variance of {2}"
144 subdivs = [0,0,0]
145 loop = 3
146 if ranks not in squares:
147 if ranks % 3 == 0:
148 subdivs[0] = 3
149 subdivs[1] = ranks//3
150 subdivs[2] = 1
151 elif ranks % 2 == 0:
152 subdivs[0] = 2
153 subdivs[1] = ranks//2
154 subdivs[2] = 1
155 else:
156 raise unittest.SkipTest("inappropriate number of ranks")
157 else:
158 subdivs = [squares.index(ranks)+2]*2 + [1]
159 loop = 1
160
161 for order in range(2,11):
162 for i in range(loop):
163 divs = subdivs[i:] + subdivs[:i]
164 r = rBrick(2*divs[0]*order-1, 2*divs[1]*order-1, 2*divs[2]*order-1,
165 d0=divs[0], d1=divs[1], d2=divs[2])
166 s = sBrick(order, divs[0]*2, divs[1]*2, divs[2]*2,
167 d0=divs[0], d1=divs[1], d2=divs[2])
168 d = self.calculateVariance(s, r)
169 res = Lsup(d)
170 self.assertLess(res, 1e-10, error_msg.format(order, divs, res))
171
172 @unittest.skipIf(getMPISizeWorld() < 8, "requires at least 8 ranks")
173 def test_TriaxialSplits_Brick(self):
174 ranks = getMPISizeWorld()
175 cubes = [8,27,64] #could do more, unlikely to test though
176 error_msg = "Brick of order {0} and subdivisions {1} failed to"+\
177 " interpolate correctly, variance of {2}"
178 subdivs = [0,0,0]
179 loop = 3
180 if ranks not in cubes:
181 if ranks % 2 == 0:
182 subdivs[0] = 2
183 if (ranks // 2) % 2 == 0:
184 subdivs[1] = 2
185 subdivs[2] = ranks // 4
186 elif (ranks // 2) % 3 == 0:
187 subdivs[1] = 3
188 subdivs[2] = ranks // 6
189 else:
190 raise unittest.SkipTest("inappropriate number of ranks")
191 elif ranks % 3 == 0:
192 subdivs[0] = 3
193 if (ranks // 2) % 2 == 0:
194 subdivs[1] = 2
195 subdivs[2] = ranks // 4
196 elif (ranks // 2) % 3 == 0:
197 subdivs[1] = 3
198 subdivs[2] = ranks // 6
199 else:
200 raise unittest.SkipTest("inappropriate number of ranks")
201 else:
202 raise unittest.SkipTest("inappropriate number of ranks")
203 else:
204 subdivs = [cubes.index(ranks)+2]*3
205 loop = 1
206
207 for order in range(2,11):
208 for i in range(loop):
209 divs = subdivs[i:] + subdivs[:i]
210 r = rBrick(2*divs[0]*order-1, 2*divs[1]*order-1, 2*divs[2]*order-1,
211 d0=divs[0], d1=divs[1], d2=divs[2])
212 s = sBrick(order, divs[0]*2, divs[1]*2, divs[2]*2,
213 d0=divs[0], d1=divs[1], d2=divs[2])
214 res = Lsup(self.calculateVariance(s, r))
215 self.assertLess(res, 1e-10, error_msg.format(order, divs, res))
216
217 def test_Brick(self):
218 elements = [3*getMPISizeWorld(), 2, 1]
219 lengths = [3.*getMPISizeWorld(), 4., 5.]
220 labels = ["x", "y", "z"]
221 error_message = "Brick {0}-split of order {1} failed to interpolate " +\
222 "correctly, variance of {2}"
223 for i in range(len(labels)):
224 e = tuple(elements[i:] + elements[:i])
225 l = tuple(lengths[i:] + lengths[:i])
226 for order in range(2,11):
227 ele = tuple([j*order for j in e])
228 coupler = SpeckleyToRipley(3, ele, order=order, lengths=l)
229 a = self.calculateVariance(coupler.getSpeckley(), coupler.getRipley())
230 self.assertLess(Lsup(a), 1e-10,
231 error_message.format(labels[i], order, Lsup(a)))
232
233 def test_mismatch_errors(self):
234 ranks = getMPISizeWorld()
235 r = rRectangle(2*ranks - 1, 2, l0=1., d0=ranks)
236 s = sRectangle(2, 2*ranks, 2, l0=2., d0=ranks)
237 with self.assertRaises(RuntimeError): #length mismatch
238 self.calculateVariance(r, s)
239 r = rBrick(2*ranks - 1, 2, 2, l1=2., d0=ranks)
240 s = sBrick(2, 2*ranks, 2, 2, l1=1., d0=ranks)
241 with self.assertRaises(RuntimeError): #length mismatch
242 self.calculateVariance(r, s)
243 r = rRectangle(2*ranks - 1, 2, l0=1., l1=2., d0=ranks)
244 s = sBrick(2, 2*ranks, 2, 2, l0=2., l1=1., d0=ranks)
245 with self.assertRaises(RuntimeError): #dimensionality mismatch
246 self.calculateVariance(r, s)
247 if getMPISizeWorld() > 1:
248 r = rRectangle(2*ranks - 1, 2, l0=1., d0=ranks)
249 s = sRectangle(2, 2, 2*ranks, l0=2., d1=ranks)
250 with self.assertRaises(RuntimeError): #subdivision mismatch
251 self.calculateVariance(r, s)
252
253 @unittest.skipIf(not HAS_FINLEY, "Finley domains not present")
254 def test_bad_domains(self):
255 ranks = getMPISizeWorld()
256 f = fRectangle(2*ranks - 1, 2, l0=1., l1=2., d0=ranks)
257 s = sRectangle(2, 2*ranks, 2, l0=2., d0=ranks)
258 with self.assertRaises(RuntimeError): #finley is not ripley
259 self.calculateVariance(f, s)
260
261 def test_creation(self):
262 with self.assertRaises(RuntimeError): #invalid order
263 coupler = SpeckleyToRipley(2, pointsPerDim=(4,2*getMPISizeWorld()), order=1)
264 with self.assertRaises(RuntimeError): #invalid order
265 coupler = SpeckleyToRipley(2, pointsPerDim=(40,20*getMPISizeWorld()), order=11)
266 with self.assertRaises(ValueError): #invalid dimension
267 coupler = SpeckleyToRipley(1, pointsPerDim=(4,2*getMPISizeWorld()))
268 with self.assertRaises(ValueError): #incalid dimension
269 coupler = SpeckleyToRipley(4, pointsPerDim=(4,2*getMPISizeWorld()))
270
271
272 if __name__ == '__main__':
273 run_tests(__name__, exit_on_failure=True)
274

  ViewVC Help
Powered by ViewVC 1.1.26