Changeset 664
- Timestamp:
- Dec 2, 2004, 3:28:33 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/flatbed.py
r660 r664 17 17 18 18 #Create basic mesh 19 N = 2019 N = 50 20 20 points, vertices, boundary = rectangular(N, N, 100, 100) 21 21 … … 25 25 domain.set_name('polygons') 26 26 print "Output being written to " + data_dir + sep + \ 27 domain.filename + " ."+ domain.format27 domain.filename + "_size%d." %len(domain) + domain.format 28 28 29 29 … … 36 36 domain.set_quantity('friction', manning) 37 37 38 def wiggle(x, y): 39 from Numeric import sin 40 from math import pi 41 return 10 + sin(2*pi*x/10) 38 42 39 43 def slope(x, y): 44 return 20*(x/100+y/100) 40 45 41 46 #Define polynomials … … 47 52 #Set elevation 48 53 domain.set_quantity('elevation', 49 Polygon_function([(p0, 23), (p1,10), (p2,15)])) 54 Polygon_function([(p0,slope), (p1,wiggle), (p2,15)])) 55 #domain.set_quantity('level', 56 # Polygon_function([(p0,slope), (p1,wiggle), (p2,15)])) 50 57 51 58 -
inundation/ga/storm_surge/pyvolution/test_util.py
r659 r664 8 8 from util import * 9 9 from config import epsilon 10 11 12 def test_function(x, y): 13 return x+y 10 14 11 15 class TestCase(unittest.TestCase): … … 334 338 335 339 336 def test_polygon_function (self):340 def test_polygon_function_constants(self): 337 341 p1 = [[0,0], [10,0], [10,10], [0,10]] 338 342 p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] … … 353 357 assert allclose(z, [2,1,0,2]) 354 358 359 360 def test_polygon_function_callable(self): 361 """Check that values passed into Polygon_function can be callable 362 themselves. 363 """ 364 p1 = [[0,0], [10,0], [10,10], [0,10]] 365 p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] 366 367 f = Polygon_function( [(p1, test_function)] ) 368 z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1 369 assert allclose(z, [10,14,0,0]) 370 371 #Combined 372 f = Polygon_function( [(p1, test_function), (p2, 2.0)] ) 373 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 374 assert allclose(z, [2,14,0,2]) 375 376 377 #Combined w default 378 f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14) 379 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 380 assert allclose(z, [2,14,3.14,2]) 381 382 383 #Combined w default func 384 f = Polygon_function( [(p1, test_function), (p2, 2.0)], 385 default = test_function) 386 z = f([5, 5, 27, 35], [5, 9, 8, -5]) 387 assert allclose(z, [2,14,35,2]) 355 388 356 389 … … 467 500 assert inside_polygon( (10.5, 10.5), polygon ) 468 501 469 #FIXME: Fails 470 #assert not inside_polygon( (5.5, 5.5), polygon )502 #FIXME: Fails if point is 5.5, 5.5 503 assert not inside_polygon( (0, 5.5), polygon ) 471 504 472 505 #Polygon with a hole … … 475 508 476 509 assert inside_polygon( (0, -0.5), polygon ) 477 478 #FIXME: Fails 479 #assert not inside_polygon( (0.5, 0.5), polygon ) 510 assert not inside_polygon( (0.5, 0.5), polygon ) 480 511 481 512 -
inundation/ga/storm_surge/pyvolution/util.py
r659 r664 57 57 """ 58 58 59 #FIXME: Could do with some optimisation59 #FIXME: Could do with some C-optimisation 60 60 61 61 from Numeric import array, dot, allclose … … 94 94 point - Tuple of (x, y) coordinates, or list of tuples 95 95 polygon - list of vertices of polygon 96 closed - (optional) determine whether points on boundary should be 97 regarded as belonging to the polygon (closed = True) 98 or not (closed = False) 96 99 97 100 Output: … … 109 112 110 113 Remarks: 111 <Copy from source Franklins code > 114 The vertices may be listed clockwise or counterclockwise and 115 the first point may optionally be repeated. 116 Polygons do not need to be convex. 117 Polygons can have holes in them and points inside a hole is 118 regarded as being outside the polygon. 112 119 113 114 points at a vertex are considered to be inside115 120 116 Algorithm due to Darel Finley, http://www.alienryderflex.com/polygon/ 117 118 119 FIXME: More comments about algoritm 120 FIXME: Deal with case where point on border of polygon (closed) 121 121 Algorithm is based on work by Darel Finley, 122 http://www.alienryderflex.com/polygon/ 123 122 124 """ 123 125 … … 166 168 y = points[k, 1] 167 169 168 169 170 inside = False 170 171 for i in range(N): … … 187 188 if inside: indices.append(k) 188 189 189 #print x, y, polygon, inside190 191 192 190 if one_point: 193 191 return inside … … 222 220 Note: If two polygons overlap, the one last in the list takes precedence 223 221 224 Note: default can only be a constant.225 222 """ 226 223 … … 253 250 x = array(x).astype(Float) 254 251 y = array(y).astype(Float) 255 x = reshape(x, (len(x), 1)) 256 y = reshape(y, (len(y), 1)) 257 258 points = concatenate( (x,y), axis=1 ) 259 260 z = ones(x.shape[0], Float) * self.default 252 253 N = len(x) 254 assert len(y) == N 255 256 points = concatenate( (reshape(x, (N, 1)), 257 reshape(y, (N, 1))), axis=1 ) 258 259 if callable(self.default): 260 z = self.default(x,y) 261 else: 262 z = ones(N, Float) * self.default 263 261 264 for polygon, value in self.regions: 262 265 indices = inside_polygon(points, polygon) 263 for i in indices: 264 z[i] = value 265 266 #from sys import exit; exit() 266 267 #FIXME: This needs to be vectorised 268 if callable(value): 269 for i in indices: 270 xx = array([x[i]]) 271 yy = array([y[i]]) 272 z[i] = value(xx, yy)[0] 273 else: 274 for i in indices: 275 z[i] = value 276 267 277 return z 268 278
Note: See TracChangeset
for help on using the changeset viewer.