Changeset 715 for inundation/ga/storm_surge
- Timestamp:
- Dec 22, 2004, 2:20:59 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/general_mesh.py
r653 r715 195 195 196 196 if (indexes == None): 197 indexes = range(len(self)) 197 indexes = range(len(self)) #len(self)=number of elements 198 198 199 199 return take(self.triangles, indexes) 200 200 201 def get_unique_vertices(self, indexes=None): 202 triangles = self.get_vertices(indexes=indexes) 203 unique_verts = {} 204 for triangle in triangles: 205 unique_verts[triangle[0]] = 0 206 unique_verts[triangle[1]] = 0 207 unique_verts[triangle[2]] = 0 208 return unique_verts.keys() 209 201 210 def build_vertexlist(self): 202 211 """Build vertexlist index by vertex ids and for each entry (point id) -
inundation/ga/storm_surge/pyvolution/quantity.py
r700 r715 94 94 internal ordering. 95 95 96 If values are described a function, it will be evaluated at specified points 97 Indexes is the set of element ids that the operation applies to. 96 If values are described a function, it will be evaluated at 97 specified points 98 99 If indexex is not 'unique vertices' Indexes is the set of element ids 100 that the operation applies to. 101 If indexex is 'unique vertices' Indexes is the set of vertex ids 102 that the operation applies to. 103 98 104 99 105 If selected location is vertices, values for centroid and edges … … 103 109 """ 104 110 105 if location not in ['vertices', 'centroids', 'edges' ]:111 if location not in ['vertices', 'centroids', 'edges', 'unique vertices']: 106 112 msg = 'Invalid location: %s' %location 107 113 raise msg … … 136 142 for i in indexes: 137 143 self.edge_values[i,:] = X 144 145 elif location == 'unique vertices': 146 if (indexes == None): 147 self.edge_values[:] = X 148 else: 149 150 #Go through list of unique vertices 151 for unique_vert_id in indexes: 152 triangles = self.domain.vertexlist[unique_vert_id] 153 154 #In case there are unused points 155 if triangles is None: continue 156 157 #Go through all triangle, vertex pairs 158 #and set corresponding vertex value 159 for triangle_id, vertex_id in triangles: 160 self.vertex_values[triangle_id, vertex_id] = X 161 162 #Intialise centroid and edge_values 163 self.interpolate() 138 164 else: 139 165 if (indexes == None): … … 148 174 self.set_array_values(X, location, indexes = indexes) 149 175 150 if location == 'vertices' :176 if location == 'vertices' or location == 'unique vertices': 151 177 #Intialise centroid and edge_values 152 178 self.interpolate() … … 177 203 from Numeric import take 178 204 179 if location not in ['vertices', 'centroids', 'edges' ]:205 if location not in ['vertices', 'centroids', 'edges', 'unique vertices']: 180 206 msg = 'Invalid location: %s' %location 181 207 raise msg … … 185 211 Numeric.ArrayType],\ 186 212 'Indices must be a list or None' 187 188 if (indexes == None):189 indexes = range(len(self))190 191 213 192 214 if location == 'centroids': 215 if (indexes == None): 216 indexes = range(len(self)) 193 217 return take(self.centroid_values,indexes) 194 218 elif location == 'edges': 195 return take(self.edge_values,indexes) 196 else: 219 if (indexes == None): 220 indexes = range(len(self)) 221 return take(self.edge_values,indexes) 222 elif location == 'unique vertices': 223 if (indexes == None): 224 indexes=range(self.domain.coordinates.shape[0]) 225 vert_values = [] 226 #Go through list of unique vertices 227 for unique_vert_id in indexes: 228 triangles = self.domain.vertexlist[unique_vert_id] 229 230 #In case there are unused points 231 if triangles is None: 232 msg = 'Unique vertex not associated with triangles' 233 raise msg 234 235 # Go through all triangle, vertex pairs 236 # Average the values 237 sum = 0 238 for triangle_id, vertex_id in triangles: 239 sum += self.vertex_values[triangle_id, vertex_id] 240 vert_values.append(sum/len(triangles)) 241 return Numeric.array(vert_values) 242 else: 243 if (indexes == None): 244 indexes = range(len(self)) 197 245 return take(self.vertex_values,indexes) 198 246 … … 238 286 values: Numeric array 239 287 location: Where values are to be stored. 240 Permissible options are: vertices, edges, centroid 241 Default is "vertices" 242 243 indexes - if this action is carried out on a subset of elements 244 The element indexes are specified here. 288 Permissible options are: vertices, edges, centroid, unique vertices 289 Default is "vertices" 290 291 indexes - if this action is carried out on a subset of 292 elements or unique vertices 293 The element/unique vertex indexes are specified here. 245 294 246 295 In case of location == 'centroid' the dimension values must … … 296 345 297 346 self.edge_values = values 347 348 elif location == 'unique vertices': 349 assert len(values.shape) == 1, 'Values array must be 1d' 350 self.set_vertex_values(values, indexes = indexes) 298 351 else: 299 352 if len(values.shape) == 1: 300 301 if indexes == None:353 self.set_vertex_values(values, indexes = indexes) 354 #if indexes == None: 302 355 #Values are being specified once for each unique vertex 303 msg = 'Number of values must match number of vertices'304 assert values.shape[0] == self.domain.coordinates.shape[0], msg305 self.set_vertex_values(values)306 else:307 for element_index, value in map(None, indexes, values):308 self.vertex_values[element_index, :] = value356 # msg = 'Number of values must match number of vertices' 357 # assert values.shape[0] == self.domain.coordinates.shape[0], msg 358 # self.set_vertex_values(values) 359 #else: 360 # for element_index, value in map(None, indexes, values): 361 # self.vertex_values[element_index, :] = value 309 362 310 363 elif len(values.shape) == 2: … … 325 378 326 379 # FIXME have a get_vertex_values as well, so the 'level' quantity can be 327 # set, based on the elevation 328 def set_vertex_values(self, A): 329 """Set vertex values for all triangles based on input array A 330 which is assumed to have one entry per (unique) vertex, i.e. 331 one value for each row in array self.domain.coordinates. 380 # set, based on the elevation 381 def set_vertex_values(self, A, indexes = None): 382 """Set vertex values for all unique vertices based on input array A 383 which has one entry per unique vertex, i.e. 384 one value for each row in array self.domain.coordinates or 385 one value for each row in vertexlist. 386 387 indexes is the list of vertex_id's that will be set. 388 389 Note: Functions not allowed 332 390 """ 333 391 … … 338 396 339 397 assert len(A.shape) == 1 340 assert A.shape[0] == self.domain.coordinates.shape[0] 341 342 N = A.shape[0] 343 398 399 if indexes == None: 400 assert A.shape[0] == self.domain.coordinates.shape[0] 401 vertex_list = range(A.shape[0]) 402 else: 403 assert A.shape[0] == len(indexes) 404 vertex_list = indexes 344 405 #Go through list of unique vertices 345 for k in range(N):346 L = self.domain.vertexlist[k]347 348 if Lis None: continue #In case there are unused points406 for i_index,unique_vert_id in enumerate(vertex_list): 407 triangles = self.domain.vertexlist[unique_vert_id] 408 409 if triangles is None: continue #In case there are unused points 349 410 350 411 #Go through all triangle, vertex pairs 351 #touching vertex kand set corresponding vertex value352 for triangle_id, vertex_id in L:353 self.vertex_values[triangle_id, vertex_id] = A[ k]412 #touching vertex unique_vert_id and set corresponding vertex value 413 for triangle_id, vertex_id in triangles: 414 self.vertex_values[triangle_id, vertex_id] = A[i_index] 354 415 355 416 #Intialise centroid and edge_values 356 417 self.interpolate() 357 358 418 359 419 def smooth_vertex_values(self, value_array='field_values', -
inundation/ga/storm_surge/pyvolution/region.py
r592 r715 12 12 """ 13 13 14 def __init__(self ):15 pass14 def __init__(self, location='vertices'): 15 self.location = location 16 16 17 17 def __call__(self, tag, elements, domain): … … 20 20 21 21 22 class Set_Region(Region): 22 def build_indexes(self, elements, domain): 23 """ 24 Return a list of triangle_id or vertex_id, depending on the location 25 """ 26 if self.location == 'unique vertices': 27 return domain.get_unique_vertices(elements) 28 else: 29 return elements 30 31 class Set_region(Region): 23 32 24 33 def __init__(self, tag, quantity, X, location='vertices'): … … 43 52 """ 44 53 if tag == self.tag: 45 domain.set_quantity(self.quantity, self.X, indexes = elements) 54 domain.set_quantity(self.quantity, 55 self.X, 56 location=self.location, 57 indexes=self.build_indexes(elements, domain)) 46 58 47 48 class Add_ Value_To_Region(Region):59 60 class Add_value_to_region(Region): 49 61 """ 50 62 Will add a value to the current quantity value. 51 63 """ 52 64 53 def __init__(self, tag, quantity, X, location='vertex'): 54 Region.__init__(self) 65 def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None): 66 #I have to get this going! 67 #Region.__init__(self) 55 68 self.tag = tag 56 self.quantity = quantity69 self.quantity_answer = quantity 57 70 self.location = location 58 71 self.X = X 72 if initial_quantity is None: 73 self.quantity_initial_value = quantity 74 else: 75 self.quantity_initial_value = initial_quantity 59 76 if callable(X): 60 77 raise 'This class does not work with functions' … … 67 84 """ 68 85 if tag == self.tag: 69 new_values = domain.get_quantity(self.quantity, 70 indexes = elements) + self.X 71 domain.set_quantity(self.quantity, new_values, indexes = elements) 86 new_values = domain.get_quantity(self.quantity_initial_value, 87 indexes=self.build_indexes(elements, domain), 88 location=self.location) + self.X 89 domain.set_quantity(self.quantity_answer, new_values, 90 indexes=self.build_indexes(elements, domain), 91 location=self.location) 92 93 94 class Stage_no_less_than_elevation(Region): 95 """ 96 Will set the stage to not be less than the elevation. 97 This would be good, but it's not region dependent. 98 Wait for it to become a default for pyvolution. 99 """ 100 101 def __init__(self): 102 pass -
inundation/ga/storm_surge/pyvolution/shallow_water.py
r659 r715 18 18 19 19 from domain import * 20 from region import * 20 21 Generic_domain = Domain #Rename 21 22 -
inundation/ga/storm_surge/pyvolution/test_general_mesh.py
r530 r715 17 17 pass 18 18 19 def test_get ting_some_vertex_values(self):19 def test_get_vertex_values(self): 20 20 """ 21 set valuesbased on triangle lists.21 get connectivity based on triangle lists. 22 22 """ 23 23 from mesh_factory import rectangular … … 28 28 points, vertices, boundary = rectangular(1, 3) 29 29 domain = Domain(points, vertices, boundary) 30 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 31 [4,4,4],[5,5,5],[6,6,6]]) 30 32 31 value = [7] 33 32 indexes = [1] … … 36 35 domain.triangles[4]] 37 36 37 def test_get_unique_vertex_values(self): 38 """ 39 get unique_vertex based on triangle lists. 40 """ 41 from mesh_factory import rectangular 42 from shallow_water import Domain 43 from Numeric import zeros, Float 44 45 #Create basic mesh 46 points, vertices, boundary = rectangular(1, 3) 47 domain = Domain(points, vertices, boundary) 48 49 assert domain.get_unique_vertices() == [0,1,2,3,4,5,6,7] 50 unique_vertices = domain.get_unique_vertices([0,1,4]) 51 unique_vertices.sort() 52 assert unique_vertices == [0,1,2,4,5,6,7] 53 54 unique_vertices = domain.get_unique_vertices([0,4]) 55 unique_vertices.sort() 56 assert unique_vertices == [0,2,4,5,6,7] 57 38 58 #------------------------------------------------------------- 39 59 if __name__ == "__main__": -
inundation/ga/storm_surge/pyvolution/test_quantity.py
r590 r715 195 195 quantity.set_values(f, location = 'centroids') 196 196 assert allclose(quantity.centroid_values, 197 [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 198 199 197 [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 200 198 201 199 202 200 def test_set_vertex_values(self): 203 201 quantity = Quantity(self.mesh4) 204 205 206 202 quantity.set_vertex_values([0,1,2,3,4,5]) 207 208 203 209 204 assert allclose(quantity.vertex_values, 210 205 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 211 212 assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) #Centroid 213 206 assert allclose(quantity.centroid_values, 207 [1., 7./3, 11./3, 8./3]) #Centroid 214 208 assert allclose(quantity.edge_values, [[1., 1.5, 0.5], 215 209 [3., 2.5, 1.5], … … 218 212 219 213 220 214 def test_set_vertex_values_subset(self): 215 quantity = Quantity(self.mesh4) 216 quantity.set_vertex_values([0,1,2,3,4,5]) 217 quantity.set_vertex_values([0,20,30,50], indexes = [0,2,3,5]) 218 219 assert allclose(quantity.vertex_values, 220 [[1,0,20], [1,20,4], [4,20,50], [30,1,4]]) 221 221 222 222 def test_set_vertex_values_using_general_interface(self): … … 229 229 assert allclose(quantity.vertex_values, 230 230 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 231 232 assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) #Centroid 231 232 #Centroid 233 assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) 233 234 234 235 assert allclose(quantity.edge_values, [[1., 1.5, 0.5], … … 659 660 #Create basic mesh 660 661 points, vertices, boundary = rectangular(1, 3) 661 662 #print "vertices",vertices 662 663 #Create shallow water domain 663 664 domain = Domain(points, vertices, boundary) … … 678 679 assert allclose(quantity.vertex_values[1], value[0]) 679 680 680 # FIXMEDSG this seems wrong -DSG 681 # It's right. The values is an array, indexed by the element id's 682 # given in indexes. It sets values per triangle. 681 682 #print "quantity",quantity.vertex_values 683 683 values = [10,100,50] 684 quantity.set_values(values, indexes = [0,1,5] ) # , location = 'centroids'684 quantity.set_values(values, indexes = [0,1,5], location = 'centroids') 685 685 #print "2 quantity.vertex_values",quantity.vertex_values 686 686 assert allclose(quantity.vertex_values[0], [10,10,10]) … … 690 690 assert allclose(quantity.centroid_values, [10,100,3,4,5,50]) 691 691 692 693 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 694 [4,4,4],[5,5,5],[6,6,6]]) 695 values = [10,100,50] 696 #this will be per unique vertex, indexing the vertices 697 #print "quantity.vertex_values",quantity.vertex_values 698 quantity.set_values(values, indexes = [0,1,5]) 699 #print "quantity.vertex_values",quantity.vertex_values 700 assert allclose(quantity.vertex_values[0], [1,50,10]) 701 assert allclose(quantity.vertex_values[5], [6,6,6]) 702 assert allclose(quantity.vertex_values[1], [100,10,50]) 703 704 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 705 [4,4,4],[5,5,5],[6,6,6]]) 692 706 values = [[31,30,29],[400,400,400],[1000,999,998]] 693 707 quantity.set_values(values, indexes = [3,3,5]) 694 708 quantity.interpolate() 695 assert allclose(quantity.centroid_values, [1 0,100,3,400,5,999])709 assert allclose(quantity.centroid_values, [1,2,3,400,5,999]) 696 710 697 711 values = [[1,1,1],[2,2,2],[3,3,3], … … 711 725 [ 6., 7., 2.], 712 726 [ 3., 2., 7.]]) 727 728 def test_setting_unique_vertex_values(self): 729 """ 730 set values based on unique_vertex lists. 731 """ 732 from mesh_factory import rectangular 733 from shallow_water import Domain 734 from Numeric import zeros, Float 735 736 #Create basic mesh 737 points, vertices, boundary = rectangular(1, 3) 738 #print "vertices",vertices 739 #Create shallow water domain 740 domain = Domain(points, vertices, boundary) 741 #print "domain.number_of_elements ",domain.number_of_elements 742 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 743 [4,4,4],[5,5,5]]) 744 value = 7 745 indexes = [1,5] 746 quantity.set_values(value, 747 location = 'unique vertices', 748 indexes = indexes) 749 #print "quantity.centroid_values",quantity.centroid_values 750 assert allclose(quantity.vertex_values[0], [0,7,0]) 751 assert allclose(quantity.vertex_values[1], [7,1,7]) 752 assert allclose(quantity.vertex_values[2], [7,2,7]) 753 754 755 def test_get_values(self): 756 """ 757 get values based on triangle lists. 758 """ 759 from mesh_factory import rectangular 760 from shallow_water import Domain 761 from Numeric import zeros, Float 762 763 #Create basic mesh 764 points, vertices, boundary = rectangular(1, 3) 765 766 #print "points",points 767 #print "vertices",vertices 768 #print "boundary",boundary 769 770 #Create shallow water domain 771 domain = Domain(points, vertices, boundary) 772 #print "domain.number_of_elements ",domain.number_of_elements 773 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 774 [4,4,4],[5,5,5]]) 775 776 #print "quantity.get_values(location = 'unique vertices')", \ 777 # quantity.get_values(location = 'unique vertices') 778 779 #print "quantity.get_values(location = 'unique vertices')", \ 780 # quantity.get_values(indexes=[0,1,2,3,4,5,6,7], \ 781 # location = 'unique vertices') 782 783 answer = [0.5,2,4,5,0,1,3,4.5] 784 assert allclose(answer, 785 quantity.get_values(location = 'unique vertices')) 786 787 indexes = [0,5,3] 788 answer = [0.5,1,5] 789 assert allclose(answer, 790 quantity.get_values(indexes=indexes, \ 791 location = 'unique vertices')) 792 #print "quantity.centroid_values",quantity.centroid_values 793 #print "quantity.get_values(location = 'centroids') ",\ 794 # quantity.get_values(location = 'centroids') 713 795 714 796 def test_getting_some_vertex_values(self): … … 775 857 if __name__ == "__main__": 776 858 suite = unittest.makeSuite(TestCase,'test') 859 #print "restricted test" 860 #suite = unittest.makeSuite(TestCase,'test_set_vertex_values_subset') 777 861 runner = unittest.TextTestRunner() 778 862 runner.run(suite) -
inundation/ga/storm_surge/pyvolution/test_region.py
r700 r715 44 44 domain.set_quantity('friction', manning) 45 45 46 a = Set_ Region('bottom', 'friction', 0.09)47 b = Set_ Region('top', 'friction', 1.0)46 a = Set_region('bottom', 'friction', 0.09) 47 b = Set_region('top', 'friction', 1.0) 48 48 domain.set_region([a, b]) 49 49 #print domain.quantities['friction'].get_values() … … 56 56 [ 1.0, 1.0, 1.0]]) 57 57 58 #c = Add_Value_To_ Region('all', 'friction', 10.0)59 domain.set_region(Add_ Value_To_Region('all', 'friction', 10.0))58 #c = Add_Value_To_region('all', 'friction', 10.0) 59 domain.set_region(Add_value_to_region('all', 'friction', 10.0)) 60 60 #print domain.quantities['friction'].get_values() 61 61 assert allclose(domain.quantities['friction'].get_values(), … … 68 68 69 69 # trying a function 70 domain.set_region(Set_ Region('top', 'friction', add_x_y))70 domain.set_region(Set_region('top', 'friction', add_x_y)) 71 71 #print domain.quantities['friction'].get_values() 72 72 assert allclose(domain.quantities['friction'].get_values(), … … 77 77 [ 5./3, 2.0, 2./3], 78 78 [ 1.0, 2./3, 2.0]]) 79 79 80 domain.set_quantity('elevation', 10.0) 81 domain.set_quantity('level', 10.0) 82 domain.set_region(Add_value_to_region('top', 'level', 1.0,initial_quantity='elevation')) 83 #print domain.quantities['level'].get_values() 84 assert allclose(domain.quantities['level'].get_values(), 85 [[ 10., 10., 10.], 86 [ 10., 10., 10.], 87 [ 10., 10., 10.], 88 [ 10., 10., 10.], 89 [ 11.0, 11.0, 11.0], 90 [ 11.0, 11.0, 11.0]]) 91 92 def test_unique_vertices(self): 93 """ 94 get values based on triangle lists. 95 """ 96 from mesh_factory import rectangular 97 from shallow_water import Domain 98 from Numeric import zeros, Float 99 100 #Create basic mesh 101 points, vertices, boundary = rectangular(1, 3) 102 103 #Create shallow water domain 104 domain = Domain(points, vertices, boundary) 105 domain.build_tagged_elements_dictionary({'bottom':[0,1], 106 'top':[4,5], 107 'all':[0,1,2,3,4,5]}) 108 109 #Set friction 110 manning = 0.07 111 domain.set_quantity('friction', manning) 112 113 a = Set_region('bottom', 'friction', 0.09, location = 'unique vertices') 114 domain.set_region(a) 115 #print domain.quantities['friction'].get_values() 116 assert allclose(domain.quantities['friction'].get_values(),\ 117 [[ 0.09, 0.09, 0.09], 118 [ 0.09, 0.09, 0.09], 119 [ 0.09, 0.07, 0.09], 120 [ 0.07, 0.09, 0.07], 121 [ 0.07, 0.07, 0.07], 122 [ 0.07, 0.07, 0.07]]) 123 124 125 def test_unique_verticesII(self): 126 """ 127 get values based on triangle lists. 128 """ 129 from mesh_factory import rectangular 130 from shallow_water import Domain 131 from Numeric import zeros, Float 132 133 #Create basic mesh 134 points, vertices, boundary = rectangular(1, 3) 135 136 #Create shallow water domain 137 domain = Domain(points, vertices, boundary) 138 domain.build_tagged_elements_dictionary({'bottom':[0,1], 139 'top':[4,5], 140 'all':[0,1,2,3,4,5]}) 141 142 #Set friction 143 manning = 0.07 144 domain.set_quantity('friction', manning) 145 146 domain.set_region(Add_value_to_region('bottom', 'friction', 1.0,initial_quantity='friction', location = 'unique vertices')) 147 148 #print domain.quantities['friction'].get_values() 149 assert allclose(domain.quantities['friction'].get_values(),\ 150 [[ 1.07, 1.07, 1.07], 151 [ 1.07, 1.07, 1.07], 152 [ 1.07, 0.07, 1.07], 153 [ 0.07, 1.07, 0.07], 154 [ 0.07, 0.07, 0.07], 155 [ 0.07, 0.07, 0.07]]) 80 156 #------------------------------------------------------------- 81 157 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.