Changeset 256
- Timestamp:
- Aug 31, 2004, 5:51:05 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution-1d
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution-1d/domain.py
r214 r256 10 10 class Domain: 11 11 12 def __init__(self, coordinates): 12 def __init__(self, coordinates, conserved_quantities = None, 13 other_quantities = None): 13 14 """ 14 15 Build 1D elements from x coordinates … … 25 26 26 27 #Allocate space for geometric quantities 27 self. edge_coordinates= zeros((N, 2), Float)28 self.centroid s = zeros(N, Float)29 self.areas = zeros(N, Float)28 self.vertex_coordinates = zeros((N, 2), Float) 29 self.centroid_coordinates = zeros(N, Float) 30 self.areas = zeros(N, Float) 30 31 for i in range(N): 31 32 xl = self.coordinates[i] 32 33 xr = self.coordinates[i+1] 33 self. edge_coordinates[i,0] = xl34 self. edge_coordinates[i,1] = xr34 self.vertex_coordinates[i,0] = xl 35 self.vertex_coordinates[i,1] = xr 35 36 36 37 centroid = (xl+xr)/2 37 self.centroid s[i] = centroid38 self.centroid_coordinates[i] = centroid 38 39 39 40 msg = 'Coordinates should be ordered, smallest to largest' 40 41 assert xr>xl, msg 42 41 43 self.areas[i] = (xr-xl) 42 43 44 44 45 45 46 print 'N', N 46 47 print 'Coordinates', self.coordinates 47 print 'Centroid', self.centroid s48 print 'Centroid', self.centroid_coordinates 48 49 print 'Areas', self.areas 49 print ' Edge_Coordinates', self.edge_coordinates50 print 'Vertex_Coordinates', self.vertex_coordinates 50 51 52 def get_centroid_coordinates(self): 53 """Return all coordinates of centroids 54 Return x coordinate of centroid for each element as a N array 55 """ 56 57 return self.centroid_coordinates 58 59 def get_coordinate(self, elem_id, vertex=None): 60 """Return coordinate of centroid, 61 or left or right vertex. 62 Left vertex (vertex=0). Right vertex (vertex=1) 63 """ 64 65 if vertex is None: 66 return self.centroid_coordinates[elem_id] 67 else: 68 return self.vertex_coordinates[elem_id,vertex] 51 69 52 def get_centroids()53 70 54 71 if __name__ == "__main__": 72 55 73 points1 = [0.0, 1.0, 2.0, 3.0] 56 74 D1 = Domain(points1) 57 75 58 points2 = [0.0, 1.0, 2.0, 3.0, 2.5] 59 D2 = Domain(points2) 76 print D1.get_coordinate(0) 77 print D1.get_coordinate(0,1) 78 79 try: 80 print D1.get_coordinate(3) 81 except: 82 pass 83 else: 84 msg = 'Should have raised an out of bounds exception' 85 raise msg 86 87 #points2 = [0.0, 1.0, 2.0, 3.0, 2.5] 88 #D2 = Domain(points2) 89 90 60 91 61 92 -
inundation/ga/storm_surge/pyvolution-1d/test_domain.py
r214 r256 10 10 class TestCase(unittest.TestCase): 11 11 def setUp(self): 12 pass13 14 12 15 def tearDown(self):16 pass17 18 def test_construct(self):19 13 a = 0.0 20 14 b = 2.0 … … 26 20 points = [a, b, c, d, e, f] 27 21 28 domain = Domain(points) 22 self.points = [a, b, c, d, e, f] 23 self.domain = Domain(self.points) 29 24 25 26 def tearDown(self): 27 pass 28 29 30 def test_construct(self): 31 32 domain = self.domain 30 33 centroids = domain.get_centroids() 31 34 assert allclose(centroids, [1.0, 2.25, 2.75, 6.5, 11.0]) … … 48 51 msg = 'Should have raised exception' 49 52 raise msg 53 54 def test_point(self): 55 a = 0.0 56 b = 2.0 57 c = 2.5 58 d = 3.0 59 60 points = [a, b, c, d] 61 62 domain = Domain(points) 63 64 assert allclose(domain.get_point(0), 0.5*(a+b)) 65 assert allclose(domain.get_point(1), 0.5*(b+c)) 66 assert allclose(domain.get_point(2), 0.5*(c+d)) 50 67 68 try: 69 allclose(domain.get_point(3), 0.5*(a+b)) 70 except: 71 pass 72 else: 73 msg = 'Should have raised exception' 74 raise msg 75 76 assert allclose(domain.get_point(0,0), a) 77 assert allclose(domain.get_point(1,0), b) 78 assert allclose(domain.get_point(2,0), c) 79 80 assert allclose(domain.get_point(0,1), b) 81 assert allclose(domain.get_point(1,1), c) 82 assert allclose(domain.get_point(2,1), d) 83 84 51 85 52 86
Note: See TracChangeset
for help on using the changeset viewer.