/[escript]/trunk/ripley/test/python/run_diracOnRipley.py
ViewVC logotype

Contents of /trunk/ripley/test/python/run_diracOnRipley.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: 10517 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 from __future__ import print_function, division
18
19 __copyright__="""Copyright (c) 2003-2014 by University of Queensland
20 http://www.uq.edu.au
21 Primary Business: Queensland, Australia"""
22 __license__="""Licensed under the Open Software License version 3.0
23 http://www.opensource.org/licenses/osl-3.0.php"""
24 __url__="https://launchpad.net/escript-finley"
25
26 import os, sys
27 import esys.escriptcore.utestselect as unittest
28 from esys.escriptcore.testing import *
29 from esys.escript import *
30 from esys.escript.linearPDEs import LameEquation
31 from esys.ripley import Rectangle, Brick
32
33 try:
34 RIPLEY_WORKDIR=os.environ['RIPLEY_WORKDIR']
35 except KeyError:
36 RIPLEY_WORKDIR='.'
37
38 class Test_DiracPoints(unittest.TestCase):
39 def getRectRefs(self, xLong):
40 Ex = self.longEdge+1
41 Ey = self.shortEdge+1
42 if not xLong:
43 Ex, Ey = Ey, Ex
44 result = [[-1 for j in range(Ex)] for i in range(Ey)]
45 ref = 0
46 if xLong:
47 for rankx in range(self.numRanks):
48 for y in range(Ey):
49 for x in range(3):
50 result[y][x+3*rankx] = ref
51 ref += 1
52 else:
53 for y in range(Ey):
54 for x in range(Ex):
55 result[y][x] = ref
56 ref += 1
57 return result
58
59 def getBrickRefs(self, splitAxis, dims):
60 dims = [i+1 for i in dims]
61 results = [[[-1 for z in range(dims[2])] for y in range(dims[1])] for x in range(dims[0])]
62 ref = 0
63 rankDim = [i for i in dims]
64 rankDim[splitAxis] = dims[splitAxis]//self.numRanks
65 rc = [0, 0, 0] #rank counters
66 for rank in range(self.numRanks):
67 for z in range(rankDim[2]):
68 for y in range(rankDim[1]):
69 for x in range(rankDim[0]):
70 results[x+rc[0]][y+rc[1]][z+rc[2]] = ref
71 ref += 1
72 rc[splitAxis] += rankDim[splitAxis]
73 return results
74
75 def generateRects(self, a, b):
76 rectX = Rectangle(self.longEdge, self.shortEdge, l0=self.longEdge,
77 l1=self.shortEdge, d0=self.numRanks, diracPoints=[(a,b)], diracTags=["test"])
78 rectY = Rectangle(self.shortEdge, self.longEdge, l0=self.shortEdge,
79 l1=self.longEdge, d1=self.numRanks, diracPoints=[(b,a)], diracTags=["test"])
80 return [rectX, rectY]
81
82 def generateBricks(self, a, b, c):
83 brickX = Brick(self.longEdge, 5, 5,
84 l0=self.longEdge, l1=5, l2=5,
85 d0=self.numRanks,
86 diracPoints=[(a,b,c)], diracTags=["test"])
87 brickY = Brick(5, self.longEdge, 5,
88 l0=5, l1=self.longEdge, l2=self.shortEdge,
89 d1=self.numRanks,
90 diracPoints=[(c,a,b)], diracTags=["test"])
91 brickZ = Brick(5, 5, self.longEdge,
92 l0=5, l1=5, l2=self.longEdge,
93 d2=self.numRanks,
94 diracPoints=[(b,c,a)], diracTags=["test"])
95 dims = [ [self.longEdge, 5, 5], [5, self.longEdge, 5],
96 [5, 5, self.longEdge]]
97 return [brickX, brickY, brickZ], dims
98
99 def owns(self, refs, y, x, xLong):
100 if xLong and self.rank*3 - 1 <= x <= self.rank*3 + 3:
101 return True
102 elif not xLong and self.rank*3 - 1 <= y <= self.rank*3 + 3:
103 return True
104 return False
105
106 def rectMessage(self, loc, xLong):
107 if xLong:
108 if not (0 <= loc[0] <= self.longEdge and 0 <= loc[1] <= self.shortEdge):
109 return self.empty
110 else:
111 if not (0 <= loc[1] <= self.longEdge and 0 <= loc[0] <= self.shortEdge):
112 return self.empty
113 x = int(round(loc[0] - 0.01))
114 y = int(round(loc[1] - 0.01))
115 refs = self.getRectRefs(xLong)
116 if self.owns(refs, y, x, xLong):
117 return "( id: 0, ref: {0}, pnt: 0) (0) {1}\n( id: 0, ref: {0}, pnt: 0) (1) {2}".format(refs[y][x], x, y)
118 return self.empty
119
120 def brickMessage(self, loc, splitAxis, refs, dims):
121 dims = [i + 1 for i in dims[splitAxis]]
122 for n in range(3):
123 if not 0 <= loc[n] <= dims[n]:
124 return self.empty
125 loc = [int(round(i - 1e-8)) for i in loc]
126 if self.rank*3 - 1 <= loc[splitAxis] <= self.rank*3 + 3:
127 x, y, z = loc
128 return "( id: 0, ref: {0}, pnt: 0) (0) {1}\n( id: 0, ref: {0}, pnt: 0) (1) {2}\n( id: 0, ref: {0}, pnt: 0) (2) {3}".format(refs[x][y][z], x, y, z)
129 return self.empty
130
131 def setUp(self):
132 self.numRanks = getMPISizeWorld()
133 self.rank = getMPIRankWorld()
134 self.shortEdge = 5
135 self.longEdge = 3*self.numRanks-1
136 self.empty = "(data contains no samples)\n"
137
138 def tearDown(self):
139 del self.numRanks
140 del self.rank
141 del self.shortEdge
142 del self.longEdge
143 del self.empty
144
145 def test_Creation(self):
146 r = self.numRanks
147 el = self.numRanks*3-1
148
149 #test bad types
150 with self.assertRaises(TypeError):
151 Rectangle(5, el, d1=r, diracPoints=(.0,0.), diracTags=["test"])
152 with self.assertRaises(TypeError):
153 Rectangle(5, el, d1=r, diracPoints=[(.0,0.)], diracTags=("test"))
154 with self.assertRaises(TypeError):
155 Rectangle(5, el, d1=r, diracPoints=[.0], diracTags=["test"])
156 with self.assertRaises(TypeError):
157 Rectangle(5, el, d1=r, diracPoints=[.0,.0], diracTags=["test"])
158
159 with self.assertRaises(TypeError):
160 Brick(5, el, 5, d1=r, diracPoints=(.0,0.,0.), diracTags=["test"])
161 with self.assertRaises(TypeError):
162 Brick(5, el, 5, d1=r, diracPoints=[(.0,0.,0.)], diracTags=("test"))
163 with self.assertRaises(TypeError):
164 Brick(5, el, 5, d1=r, diracPoints=[.0,0.], diracTags=["test"])
165
166 #test bad arg lengths
167 with self.assertRaises(RuntimeError):
168 Rectangle(5, el, d1=r, diracPoints=[(.0,)], diracTags=["test"])
169 with self.assertRaises(RuntimeError):
170 Rectangle(5, el, d1=r, diracPoints=[(.0,1.)], diracTags=[])
171 with self.assertRaises(RuntimeError):
172 Rectangle(5, el, d1=r, diracPoints=[(.0,0.)], diracTags=["test", "break"])
173 with self.assertRaises(RuntimeError):
174 Rectangle(5, el, d1=r, diracPoints=[(.0,0.,0.)], diracTags=["test"])
175
176 with self.assertRaises(RuntimeError):
177 Brick(5, el, 5, d1=r, diracPoints=[(.0,0.,0.,0.)], diracTags=["test"])
178 with self.assertRaises(RuntimeError):
179 Brick(5, el, 5, d1=r, diracPoints=[(.0,0.,)], diracTags=["test"])
180 with self.assertRaises(RuntimeError):
181 Brick(5, el, 5, d1=r, diracPoints=[(.0,)], diracTags=["test"])
182
183 def test_BrickInterpolation(self):
184 for a in range(-1, (self.longEdge+self.numRanks)*2, self.numRanks*2):
185 a = a//2.
186 c = 2.5
187 for b in range(self.shortEdge):
188 doms, dims = self.generateBricks(a,b,c)
189 for n, dom in enumerate(doms):
190 points = [a,b,c]
191 for i in range(n): #rotate points to match
192 points = [points.pop()] + points
193 refs = self.getBrickRefs(n, dims[n])
194 i = interpolate(dom.getX(), DiracDeltaFunctions(dom))
195 expected = self.brickMessage(tuple(points), n, refs, dims)
196 got = str(i)
197 global_result = getMPIWorldSum(1 if got != expected else 0)
198 self.assertEqual(got, expected,
199 "{0} not mapped correctly on rank {1} for d{2} splits".format(tuple(points), self.rank, n) + \
200 "\nexpected === \n{0}\ngot === \n{1}".format(expected,got)+ \
201 "\nlongedge=%d, shortedge=%d\n"%(self.longEdge, self.shortEdge))
202 #remaining ranks must also exit, otherwise we'll lock up
203 self.assertEqual(global_result, 0, "One or more ranks failed")
204
205 def test_RectangleInterpolation(self):
206 for a in range(-1, (self.longEdge+self.numRanks)*2, self.numRanks*2):
207 a = a//2.
208 for b in range(self.shortEdge):
209 rectX, rectY = self.generateRects(a,b)
210 i = interpolate(rectX.getX(), DiracDeltaFunctions(rectX))
211 got = str(i)
212 refs = self.getRectRefs(False)
213 expected = self.rectMessage((a,b),True)
214 global_result = getMPIWorldSum(1 if got != expected else 0)
215 self.assertEqual(got, expected,
216 "({0},{1}) not mapped correctly on rank {2} for d0 splits".format(a,b, self.rank) + \
217 "\nexpected === \n{0}\ngot === \n{1}".format(expected, got)+\
218 "\nlongedge=%d, shortedge=%d\n"%(self.longEdge, self.shortEdge))
219 #remaining ranks must also exit, otherwise we'll lock up
220 self.assertEqual(global_result, 0, "One or more ranks failed")
221 i = interpolate(rectY.getX(), DiracDeltaFunctions(rectY))
222 expected = self.rectMessage((b,a),False)
223 got = str(i)
224 global_result = getMPIWorldSum(1 if got != expected else 0)
225 self.assertEqual(got, expected,
226 "({0},{1}) not mapped correctly on rank {2} for d1 splits".format(b,a, self.rank) + \
227 "\nexpected === \n{0}\ngot === \n{1}".format(expected, got)+\
228 "\nlongedge={0}, shortedge={1}\n".format(self.longEdge, self.shortEdge))
229 #remaining ranks must also exit, otherwise we'll lock up
230 self.assertEqual(global_result, 0, "One or more ranks failed")
231
232 if __name__ == '__main__':
233 run_tests(__name__, exit_on_failure=True)
234
235

  ViewVC Help
Powered by ViewVC 1.1.26