Changeset 459
- Timestamp:
- Oct 28, 2004, 4:20:00 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/domain.py
r458 r459 209 209 raise msg 210 210 211 211 def set_region(self, functions): 212 for tag in self.tagged_elements.keys(): 213 for function in functions: 214 print "tag",tag 215 print "function",function 216 function(tag, self.tagged_elements[tag], self) 217 218 #Do we need to do this sort of thing? 219 #self = function(tag, self.tagged_elements[tag], self) 220 212 221 #MISC 213 222 def check_integrity(self): -
inundation/ga/storm_surge/pyvolution/quantity.py
r458 r459 154 154 155 155 156 def set_array_values(self, values, location='vertices' ):156 def set_array_values(self, values, location='vertices', indexes = None): 157 157 """Set values for quantity 158 158 … … 175 175 """ 176 176 177 from Numeric import array, Float 177 from Numeric import array, Float, Int 178 178 179 179 values = array(values).astype(Float) 180 180 181 if (indexes <> None): 182 indexes = array(indexes).astype(Int) 183 msg = 'Number of values must match number of indexes' 184 assert values.shape[0] == indexes.shape[0], msg 185 181 186 N = self.centroid_values.shape[0] 182 187 … … 184 189 assert len(values.shape) == 1, 'Values array must be 1d' 185 190 186 msg = 'Number of values must match number of elements' 187 assert values.shape[0] == N, msg 188 189 self.centroid_values = values 191 if indexes == None: 192 msg = 'Number of values must match number of elements' 193 assert values.shape[0] == N, msg 194 195 self.centroid_values = values 196 else: 197 msg = 'Number of values must match number of indexes' 198 assert values.shape[0] == indexes.shape[0], msg 199 200 #Brute force 201 for i in range(len(indexes)): 202 self.centroid_values[indexes[i]] = values[i] 203 190 204 elif location == 'edges': 191 205 assert len(values.shape) == 2, 'Values array must be 2d' … … 200 214 else: 201 215 if len(values.shape) == 1: 202 #Values are being specified once for each unique vertex 203 msg = 'Number of values must match number of vertices' 204 assert values.shape[0] == self.domain.coordinates.shape[0], msg 205 206 self.set_vertex_values(values) 216 217 if indexes == None: 218 #Values are being specified once for each unique vertex 219 msg = 'Number of values must match number of vertices' 220 assert values.shape[0] == self.domain.coordinates.shape[0], msg 221 self.set_vertex_values(values) 222 else: 223 for element_index, value in map(None, indexes, values): 224 self.vertex_values[element_index, 0] = value 225 self.vertex_values[element_index, 1] = value 226 self.vertex_values[element_index, 2] = value 207 227 elif len(values.shape) == 2: 208 228 #Vertex values are given as a triplet for each triangle 209 msg = 'Number of values must match number of elements'210 assert values.shape[0] == N, msg211 229 212 230 msg = 'Array must be N x 3' 213 231 assert values.shape[1] == 3, msg 214 self.vertex_values = values 232 233 if indexes == None: 234 self.vertex_values = values 235 else: 236 for element_index, value in map(None, indexes, values): 237 self.vertex_values[element_index] = value 215 238 else: 216 239 msg = 'Values array must be 1d or 2d' 217 240 raise msg 241 242 #FIXME - roll this back into set_array_values, if possible 243 def set_array_values_by_index(self, values, location='vertices', indexes = None): 244 from Numeric import array, Float, Int 245 246 values = array(values).astype(Float) 247 248 if not (indexes == None): 249 indexes = array(indexes).astype(Int) 250 251 N = self.centroid_values.shape[0] 252 253 if location == 'centroids': 254 assert len(values.shape) == 1, 'Values array must be 1d' 255 256 if indexes == None: 257 msg = 'Number of values must match number of elements' 258 assert values.shape[0] == N, msg 259 260 self.centroid_values = values 261 else: 262 msg = 'Number of values must match number of indexes' 263 assert values.shape[0] == indexes.shape[0], msg 264 265 #Brute force 266 for i in range(len(indexes)): 267 self.centroid_values[indexes[i]] = values[i] 218 268 219 269 … … 246 296 for triangle_id, vertex_id in L: 247 297 self.vertex_values[triangle_id, vertex_id] = A[k] 298 299 #Intialise centroid and edge_values 300 self.interpolate() 301 302 def set_some_vertex_values(self, A, indexes): 303 """Set vertex values for some triangles based on input array A 304 which is assumed to have one entry per (unique) vertex, i.e. 305 one value for each row in array self.domain.coordinates. 306 """ 307 308 from Numeric import array, Float 309 310 #Assert that A can be converted to a Numeric array of appropriate dim 311 A = array(A, Float) 312 msg = 'Number of values must match number of indexes' 313 assert A.shape[0] == len(indexes), msg 314 if (len(A.shape) == 2): 315 assert A.shape[0] == len(indexes), msg 316 msg = 'Each vertex must have a list of three values, or one value' 317 assert A.shape[1] == 3, msg 318 elif (len(A.shape) <> 1): 319 msg = 'vertex values must be 1D or 2D.' 320 raise msg 321 322 #Go through list of triangles 323 #FIXME This is brute force. Can it be optimised? 324 for element_index, value in map(None, indexes, A): 325 if len(A.shape) == 2: 326 self.vertex_values[element_index] = value 327 else: 328 self.vertex_values[element_index, 0] = value 329 self.vertex_values[element_index, 1] = value 330 self.vertex_values[element_index, 2] = value 248 331 249 332 #Intialise centroid and edge_values -
inundation/ga/storm_surge/pyvolution/test_domain.py
r458 r459 7 7 from config import epsilon 8 8 from Numeric import allclose, array 9 10 11 def add_to_verts(tag, elements, domain): 12 if tag == "mound": 13 print "Selected" 14 15 9 16 10 17 class TestCase(unittest.TestCase): … … 267 274 # assert allclose(domain.quantities[name].centroid_values, x) 268 275 269 276 277 def testz_set_region(self): 278 """Domain implements a default first order gradient limiter 279 """ 280 281 a = [0.0, 0.0] 282 b = [0.0, 2.0] 283 c = [2.0,0.0] 284 d = [0.0, 4.0] 285 e = [2.0, 2.0] 286 f = [4.0,0.0] 287 288 points = [a, b, c, d, e, f] 289 #bac, bce, ecf, dbe 290 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 291 boundary = { (0, 0): 'Third', 292 (0, 2): 'First', 293 (2, 0): 'Second', 294 (2, 1): 'Second', 295 (3, 1): 'Second', 296 (3, 2): 'Third'} 297 298 domain = Domain(points, vertices, boundary, 299 conserved_quantities =\ 300 ['level', 'xmomentum', 'ymomentum']) 301 domain.check_integrity() 302 303 domain.set_quantity('level', [[1,2,3], [5,5,5], 304 [0,0,9], [-6, 3, 3]]) 305 306 assert allclose( domain.quantities['level'].centroid_values, 307 [2,5,3,0] ) 308 309 domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], 310 [3,3,3], [4, 4, 4]]) 311 312 domain.set_quantity('ymomentum', [[10,10,10], [20,20,20], 313 [30,30,30], [40, 40, 40]]) 314 315 316 domain.distribute_to_vertices_and_edges() 317 318 #First order extrapolation 319 assert allclose( domain.quantities['level'].vertex_values, 320 [[ 2., 2., 2.], 321 [ 5., 5., 5.], 322 [ 3., 3., 3.], 323 [ 0., 0., 0.]]) 324 325 domain.build_tagged_elements_dictionary({'mound':[0,1]}) 326 domain.set_region([add_to_verts]) 270 327 271 328 #------------------------------------------------------------- 272 329 if __name__ == "__main__": 273 suite = unittest.makeSuite(TestCase,'test ')330 suite = unittest.makeSuite(TestCase,'testz') 274 331 runner = unittest.TextTestRunner() 275 332 runner.run(suite) -
inundation/ga/storm_surge/pyvolution/test_quantity.py
r458 r459 611 611 612 612 613 614 615 613 614 def set_array_values_by_index(self): 615 616 from mesh_factory import rectangular 617 from shallow_water import Domain 618 from Numeric import zeros, Float 619 620 #Create basic mesh 621 points, vertices, boundary = rectangular(1, 1) 622 623 #Create shallow water domain 624 domain = Domain(points, vertices, boundary) 625 #print "domain.number_of_elements ",domain.number_of_elements 626 quantity = Quantity(domain,[[1,1,1],[2,2,2]]) 627 value = [7] 628 indexes = [1] 629 quantity.set_array_values_by_index(value, 630 location = 'centroids', 631 indexes = indexes) 632 #print "quantity.centroid_values",quantity.centroid_values 633 634 assert allclose(quantity.centroid_values, [1,7]) 635 636 quantity.set_array_values([15,20,25], indexes = indexes) 637 assert allclose(quantity.centroid_values, [1,20]) 638 639 quantity.set_array_values([15,20,25], indexes = indexes) 640 assert allclose(quantity.centroid_values, [1,20]) 641 642 def test_setting_some_vertex_values(self): 643 644 from mesh_factory import rectangular 645 from shallow_water import Domain 646 from Numeric import zeros, Float 647 648 #Create basic mesh 649 points, vertices, boundary = rectangular(1, 3) 650 651 #Create shallow water domain 652 domain = Domain(points, vertices, boundary) 653 #print "domain.number_of_elements ",domain.number_of_elements 654 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 655 [4,4,4],[5,5,5],[6,6,6]]) 656 value = [7] 657 indexes = [1] 658 quantity.set_array_values(value, 659 location = 'centroids', 660 indexes = indexes) 661 #print "quantity.centroid_values",quantity.centroid_values 662 assert allclose(quantity.centroid_values, [1,7,3,4,5,6]) 663 664 value = [[15,20,25]] 665 quantity.set_array_values(value, indexes = indexes) 666 #print "1 quantity.vertex_values",quantity.vertex_values 667 assert allclose(quantity.vertex_values[1], value[0]) 668 669 values = [10,100,50] 670 quantity.set_array_values(values, indexes = [0,1,5]) 671 #print "2 quantity.vertex_values",quantity.vertex_values 672 assert allclose(quantity.vertex_values[0], [10,10,10]) 673 assert allclose(quantity.vertex_values[5], [50,50,50]) 674 quantity.interpolate() 675 #print "quantity.centroid_values",quantity.centroid_values 676 assert allclose(quantity.centroid_values, [10,100,3,4,5,50]) 677 678 values = [[31,30,29],[400,400,400],[1000,999,998]] 679 quantity.set_array_values(values, indexes = [3,3,5]) 680 quantity.interpolate() 681 assert allclose(quantity.centroid_values, [10,100,3,400,5,999]) 616 682 #------------------------------------------------------------- 617 683 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.