Changeset 659
- Timestamp:
- Dec 2, 2004, 2:11:36 PM (20 years ago)
- Location:
- inundation/ga/storm_surge
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/examples/beach.py
r653 r659 21 21 from pmesh2domain import pmesh_to_domain_instance 22 22 from config import data_dir 23 from util import read_polygon, Polygon_function 23 24 24 25 ###################### … … 49 50 50 51 51 def inside(p,polygon):52 return True53 52 54 def friction(x, y):55 """Read friction from polygon file and set accordingly56 """57 53 58 from Numeric import array 59 x = array(x).astype('f') 60 y = array(y).astype('f') 54 p0 = [[20,20], [40,20], [40,40], [20,40]] 61 55 62 #Get polygon 63 fid = open('beach.poly') 64 lines = fid.readlines() 65 fid.close() 66 polygon = [] 67 for line in lines: 68 fields = line.split(',') 69 polygon.append( [float(fields[0]), float(fields[1])] ) 70 71 print polygon 72 73 z = 0*x 74 for i in range(len(x)): 75 if inside( (x[i], y[i]), polygon): 76 z[i] = 1 77 else: 78 z[i] = 0 79 80 friction([0],[1]) 81 82 domain.set_quantity('elevation', x_slope) 83 domain.set_quantity('level', x_slope) 56 domain.set_quantity('elevation', Polygon_function( [(p0, 50)]), 'centroids' ) 57 domain.set_quantity('level', Polygon_function( [(p0, 50)] ), 'centroids' ) 84 58 domain.set_quantity('friction', 0.07) 85 59 86 60 print domain.get_extent() 87 import sys; sys.exit()88 61 89 62 ###################### -
inundation/ga/storm_surge/pyvolution/domain.py
r648 r659 121 121 self.set_quantity(key, quantity_dict[key], location='vertices') 122 122 123 123 124 def set_quantity(self, name, X, location='vertices', indexes = None): 124 125 """Set values for named quantity -
inundation/ga/storm_surge/pyvolution/flatbed.py
r505 r659 10 10 from mesh_factory import rectangular 11 11 from shallow_water import Domain, Reflective_boundary, Dirichlet_boundary,\ 12 Constant_height , wind_Stress12 Constant_height 13 13 from Numeric import array 14 from util import Polygon_function, read_polygon 14 15 15 16 #Create basic mesh 16 N = 15017 points, vertices, boundary = rectangular(N, N, 100 0, 1000)17 N = 20 18 points, vertices, boundary = rectangular(N, N, 100, 100) 18 19 19 20 #Create shallow water domain 20 21 domain = Domain(points, vertices, boundary) 21 domain.smooth = True22 domain.visualise = False23 22 domain.store = True 24 domain.default_order=1 23 domain.set_name('polygons') 24 domain.default_order=2 25 25 26 26 #Set driving forces … … 29 29 inflow_level = 10.0 30 30 domain.set_quantity('friction', manning) 31 32 33 34 35 #Define polynomials 36 p0 = [[20,27], [30,25], [40,40], [20,40]] 37 p1 = [[80,19], [90,20], [85,50], [80,55], [75,58], [70,60], [60,24]] 38 p2 = read_polygon('testpoly.txt') 39 40 41 #Set elevation 42 domain.set_quantity('elevation', 43 Polygon_function([(p0, 23), (p1,10), (p2,15)])) 44 45 46 31 47 32 48 … … 43 59 ###################### 44 60 #Evolution 45 for t in domain.evolve(yieldstep = 1 0, finaltime = 500):61 for t in domain.evolve(yieldstep = 1, finaltime = 1): 46 62 domain.write_time() 47 63 -
inundation/ga/storm_surge/pyvolution/quantity.py
r593 r659 147 147 #Intialise centroid and edge_values 148 148 self.interpolate() 149 149 150 if location == 'centroids': 151 #Extrapolate 1st order - to capture notion of area being specified 152 self.extrapolate_first_order() 150 153 151 154 def get_values(self, location='vertices', indexes = None): … … 479 482 480 483 484 def extrapolate_first_order(self): 485 """Extrapolate conserved quantities from centroid to 486 vertices for each volume using 487 first order scheme. 488 """ 489 490 qc = self.centroid_values 491 qv = self.vertex_values 492 493 for i in range(3): 494 qv[:,i] = qc 481 495 482 496 … … 486 500 487 501 boundary values, storage and method for updating, and 488 methods for extrapolation from centropid to vertices inluding502 methods for (second order) extrapolation from centroid to vertices inluding 489 503 gradients and limiters 490 504 """ … … 525 539 526 540 527 def extrapolate_first_order(self):528 """Extrapolate conserved quantities from centroid to529 vertices for each volume using530 first order scheme.531 """532 533 qc = self.centroid_values534 qv = self.vertex_values535 536 for i in range(3):537 qv[:,i] = qc538 539 540 541 def extrapolate_second_order(self): 541 542 #Call correct module function -
inundation/ga/storm_surge/pyvolution/shallow_water.py
r623 r659 81 81 bed = self.quantities['elevation'] 82 82 83 msg = 'All water levels must be greater than the bed elevation'84 assert alltrue( greater_equal(85 level.vertex_values, bed.vertex_values )), msg86 87 assert alltrue( greater_equal(88 level.edge_values, bed.edge_values )), msg89 90 assert alltrue( greater_equal(91 level.centroid_values, bed.centroid_values )), msg83 #msg = 'All water levels must be greater than the bed elevation' 84 #assert alltrue( greater_equal( 85 # level.vertex_values, bed.vertex_values )), msg 86 # 87 #assert alltrue( greater_equal( 88 # level.edge_values, bed.edge_values )), msg 89 # 90 #assert alltrue( greater_equal( 91 # level.centroid_values, bed.centroid_values )), msg 92 92 93 93 -
inundation/ga/storm_surge/pyvolution/test_util.py
r656 r659 356 356 357 357 def test_point_on_line(self): 358 358 359 359 #Endpoints first 360 360 assert point_on_line( (0, 0), [(0,0), (1,0)] ) … … 371 371 assert not point_on_line( (0, 0.5), [(0,0), (1,1)] ) 372 372 373 #From real example that failed 374 assert not point_on_line( (40,50), [(40,20), (40,40)] ) 375 376 377 #From real example that failed 378 assert not point_on_line( (40,19), [(40,20), (40,40)] ) 379 380 373 381 374 382 375 383 def test_inside_polygon(self): 376 384 385 377 386 #Simplest case: Polygon is the unit square 378 387 polygon = [[0,0], [1,0], [1,1], [0,1]] … … 394 403 assert not inside_polygon( (0.5, 0.), polygon, closed=False) 395 404 assert not inside_polygon( (1., 0.5), polygon, closed=False) 396 405 406 407 408 #From real example (that failed) 409 polygon = [[20,20], [40,20], [40,40], [20,40]] 410 points = [ [40, 50] ] 411 res = inside_polygon(points, polygon) 412 assert len(res) == 0 413 414 polygon = [[20,20], [40,20], [40,40], [20,40]] 415 points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ] 416 res = inside_polygon(points, polygon) 417 assert len(res) == 2 418 assert allclose(res, [0,1]) 419 420 397 421 398 422 #More convoluted and non convex polygon … … 455 479 #assert not inside_polygon( (0.5, 0.5), polygon ) 456 480 457 points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]458 459 481 460 482 -
inundation/ga/storm_surge/pyvolution/util.py
r656 r659 70 70 y1 = line[1][1] 71 71 72 73 a = array([x - x0, y - y0]) 72 a = array([x - x0, y - y0]) 73 a_normal = array([a[1], -a[0]]) 74 74 75 b = array([x1 - x0, y1 - y0]) 75 76 76 len_a = sqrt(sum(a**2)) 77 len_b = sqrt(sum(b**2)) 78 79 if len_a == 0 or len_a == len_b: 80 return True 77 if dot(a_normal, b) == 0: 78 #Point is somewhere on the infinite extension of the line 79 80 len_a = sqrt(sum(a**2)) 81 len_b = sqrt(sum(b**2)) 82 if dot(a, b) >= 0 and len_a <= len_b: 83 return True 84 else: 85 return False 81 86 else: 82 x = dot(a, b)/len_b 83 return allclose(x, len_a) 84 85 86 87 return False 88 89 87 90 def inside_polygon(point, polygon, closed = True): 88 91 """Determine whether points are inside or outside a polygon … … 155 158 py = polygon[:,1] 156 159 157 160 158 161 #Begin algorithm 159 162 indices = [] … … 162 165 x = points[k, 0] 163 166 y = points[k, 1] 167 164 168 165 169 inside = False … … 257 261 for polygon, value in self.regions: 258 262 indices = inside_polygon(points, polygon) 259 260 263 for i in indices: 261 264 z[i] = value 262 265 266 #from sys import exit; exit() 263 267 return z 268 269 def read_polygon(filename): 270 """Read points assumed to form a polygon 271 There must be exactly two numbers in each line 272 """ 273 274 #Get polygon 275 fid = open(filename) 276 lines = fid.readlines() 277 fid.close() 278 polygon = [] 279 for line in lines: 280 fields = line.split(',') 281 polygon.append( [float(fields[0]), float(fields[1])] ) 282 283 return polygon 284 264 285 265 286 class File_function:
Note: See TracChangeset
for help on using the changeset viewer.