Changeset 3689
- Timestamp:
- Oct 4, 2006, 11:00:56 AM (17 years ago)
- Location:
- anuga_core/source/anuga
- Files:
-
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/domain.py
r3678 r3689 318 318 319 319 See methods inside the quantity object for more options 320 321 FIXME: clean input args 320 322 """ 321 323 … … 498 500 print self.timestepping_statistics() 499 501 500 #Old version501 #if self.min_timestep == self.max_timestep:502 # print 'Time = %.4f, delta t = %.8f, steps=%d (%d)'\503 # %(self.time, self.min_timestep, self.number_of_steps,504 # self.number_of_first_order_steps)505 #elif self.min_timestep > self.max_timestep:506 # print 'Time = %.4f, steps=%d (%d)'\507 # %(self.time, self.number_of_steps,508 # self.number_of_first_order_steps)509 #else:510 # print 'Time = %.4f, delta t in [%.8f, %.8f], steps=%d (%d)'\511 # %(self.time, self.min_timestep,512 # self.max_timestep, self.number_of_steps,513 # self.number_of_first_order_steps)514 502 515 503 def timestepping_statistics(self): -
anuga_core/source/anuga/abstract_2d_finite_volumes/general_mesh.py
r3684 r3689 178 178 179 179 def __repr__(self): 180 return 'Mesh: %d vert ex coordinates, %d triangles'\180 return 'Mesh: %d vertices, %d triangles'\ 181 181 %(self.coordinates.shape[0], len(self)) 182 182 … … 197 197 198 198 199 def get_vertex_coordinates(self, obj=False, absolute=False):199 def get_vertex_coordinates(self, unique=False, obj=False, absolute=False): 200 200 """Return all vertex coordinates. 201 201 Return all vertex coordinates for all triangles as an Nx6 array … … 212 212 (To see which, switch to default absolute=True and run tests). 213 213 """ 214 215 if unique is True: 216 V = self.coordinates 217 if absolute is True: 218 if not self.geo_reference.is_absolute(): 219 V = self.geo_reference.get_absolute(V) 220 221 return V 222 223 214 224 215 225 V = self.vertex_coordinates … … 226 236 227 237 if obj is True: 228 229 238 N = V.shape[0] 230 239 return reshape(V, (3*N, 2)) -
anuga_core/source/anuga/abstract_2d_finite_volumes/neighbour_mesh.py
r3688 r3689 175 175 176 176 def __repr__(self): 177 return 'Mesh: %d vertex_coordinates, %d triangles, %d boundary segments'\178 %( self.coordinates.shape[0], len(self),len(self.boundary))177 return General_mesh.__repr__(self) + ', %d boundary segments'\ 178 %(len(self.boundary)) 179 179 180 180 -
anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py
r3678 r3689 15 15 """ 16 16 17 from Numeric import array, zeros, Float, concatenate, NewAxis, argmax, allclose 18 from anuga.utilities.numerical_tools import ensure_numeric 17 19 18 20 class Quantity: … … 21 23 22 24 from anuga.abstract_2d_finite_volumes.neighbour_mesh import Mesh 23 from Numeric import array, zeros, Float24 25 25 26 msg = 'First argument in Quantity.__init__ ' … … 619 620 620 621 621 from Numeric import Float622 from anuga.utilities.numerical_tools import ensure_numeric623 #from anuga.abstract_2d_finite_volumes.least_squares import fit_to_mesh624 622 from anuga.fit_interpolate.fit import fit_to_mesh 625 623 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 754 752 755 753 756 def get_values(self, location='vertices', indices = None): 754 755 756 def get_maximum_index(self, indices=None): 757 """Return index for maximum value of quantity (on centroids) 758 759 Optional argument: 760 indices is the set of element ids that the operation applies to. 761 762 Usage: 763 i = get_maximum_index() 764 765 Notes: 766 We do not seek the maximum at vertices as each vertex can 767 have multiple values - one for each triangle sharing it. 768 769 If there are multiple cells with same maximum value, the first cell 770 encountered in the triangle array is returned. 771 """ 772 773 V = self.get_values(location='centroids', indices=indices) 774 775 # Always return absolute indices 776 i = argmax(V) 777 778 if indices is None: 779 return i 780 else: 781 return indices[i] 782 783 784 def get_maximum_value(self, indices=None): 785 """Return maximum value of quantity (on centroids) 786 787 Optional argument: 788 indices is the set of element ids that the operation applies to. 789 790 Usage: 791 v = get_maximum_value() 792 793 Note, we do not seek the maximum at vertices as each vertex can 794 have multiple values - one for each triangle sharing it 795 """ 796 797 798 i = self.get_maximum_index(indices) 799 V = self.get_values(location='centroids') #, indices=indices) 800 801 return V[i] 802 803 804 def get_maximum_location(self, indices=None): 805 """Return location of maximum value of quantity (on centroids) 806 807 Optional argument: 808 indices is the set of element ids that the operation applies to. 809 810 Usage: 811 x, y = get_maximum_location() 812 813 814 Notes: 815 We do not seek the maximum at vertices as each vertex can 816 have multiple values - one for each triangle sharing it. 817 818 If there are multiple cells with same maximum value, the first cell 819 encountered in the triangle array is returned. 820 """ 821 822 i = self.get_maximum_index(indices) 823 x, y = self.domain.get_centroid_coordinates()[i] 824 825 return x, y 826 827 828 829 830 def get_interpolated_values(self, interpolation_points): 831 832 # Interpolation object based on internal (discontinuous triangles) 833 x, y, vertex_values, triangles = self.get_vertex_values(xy=True, smooth=False) 834 # FIXME: This concat should roll into get_vertex_values 835 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 ) 836 837 can_reuse = False 838 if hasattr(self, 'interpolation_object'): 839 # Reuse to save time 840 I = self.interpolation_object 841 842 if allclose(interpolation_points, I._point_coordinates): 843 can_reuse = True 844 845 846 if can_reuse is True: 847 result = I.interpolate(vertex_values) # Use absence to indicate reuse 848 else: 849 from anuga.fit_interpolate.interpolate import Interpolate 850 851 # Create interpolation object with matrix 852 I = Interpolate(vertex_coordinates, triangles) 853 self.interpolation_object = I 854 855 # Call interpolate with points the first time 856 interpolation_points = ensure_numeric(interpolation_points, Float) 857 result = I.interpolate(vertex_values, interpolation_points) 858 859 return result 860 861 862 def get_values(self, interpolation_points=None, location='vertices', indices = None): 757 863 """get values for quantity 758 864 759 865 return X, Compatible list, Numeric array (see below) 866 interpolation_points: List of x, y coordinates where value is sought (using interpolation) 867 If points are given, values of location and indices are ignored 760 868 location: Where values are to be stored. 761 869 Permissible options are: vertices, edges, centroid 762 870 and unique vertices. Default is 'vertices' 763 871 764 In case of location == 'centroids' the dimension values must765 be a list of a Numerical array of length N, N being the number766 of elements. Otherwise it must be of dimension Nx3767 872 768 873 The returned values with be a list the length of indices 769 (N if indices = None). Each value will be a list of the three 770 vertex values for this quantity. 874 (N if indices = None). 875 876 In case of location == 'centroids' the dimension of returned values will 877 be a list or a Numerical array of length N, N being the number 878 of elements. 879 880 In case of location == 'vertices' or 'edges' the dimension of returned values will 881 be of dimension Nx3 882 883 In case of location == 'unique vertices' the average value at each vertex will be 884 returned and the dimension of returned values will be a 1d array of length "number of vertices" 885 771 886 772 887 Indices is the set of element ids that the operation applies to. … … 777 892 """ 778 893 from Numeric import take 894 895 if interpolation_points is not None: 896 return self.get_interpolated_values(interpolation_points) 897 898 779 899 780 900 if location not in ['vertices', 'centroids', 'edges', 'unique vertices']: … … 810 930 # Go through all triangle, vertex pairs 811 931 # Average the values 932 933 # FIXME (Ole): Should we merge this with get_vertex_values 934 # and use the concept of a reduction operator? 812 935 sum = 0 813 936 for triangle_id, vertex_id in triangles: … … 940 1063 precision = Float 941 1064 942 if reduction is None:943 reduction = self.domain.reduction944 945 1065 #Create connectivity 946 1066 947 1067 if smooth == True: 1068 1069 if reduction is None: 1070 reduction = self.domain.reduction 948 1071 949 1072 V = self.domain.get_vertices() -
anuga_core/source/anuga/abstract_2d_finite_volumes/test_general_mesh.py
r3560 r3689 18 18 def tearDown(self): 19 19 pass 20 21 22 def test_get_vertex_coordinates(self): 23 from mesh_factory import rectangular 24 from Numeric import zeros, Float 25 26 #Create basic mesh 27 points, vertices, boundary = rectangular(1, 3) 28 domain = General_mesh(points, vertices, boundary) 29 30 assert allclose(domain.get_vertex_coordinates(unique=True), domain.coordinates) 31 32 #assert allclose(domain.get_vertex_coordinates(), ...TODO 33 #assert allclose(domain.get_vertex_coordinates(absolute=True), ...TODO 34 35 20 36 21 37 def test_get_vertex_values(self): … … 67 83 assert unique_vertices == [0,2,4,5,6,7] 68 84 85 86 87 69 88 #------------------------------------------------------------- 70 89 if __name__ == "__main__": -
anuga_core/source/anuga/abstract_2d_finite_volumes/test_quantity.py
r3678 r3689 110 110 [4.5, 4.5, 0.], 111 111 [3.0, -1.5, -1.5]]) 112 113 def test_get_maximum_1(self): 114 quantity = Conserved_quantity(self.mesh4, 115 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 116 assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroids 117 118 v = quantity.get_maximum_value() 119 assert v == 5 120 121 i = quantity.get_maximum_index() 122 assert i == 1 123 124 x,y = quantity.get_maximum_location() 125 xref, yref = 4.0/3, 4.0/3 126 assert x == xref 127 assert y == yref 128 129 v = quantity.get_values(interpolation_points = [[x,y]]) 130 assert allclose(v, 5) 131 132 def test_get_maximum_2(self): 133 134 a = [0.0, 0.0] 135 b = [0.0, 2.0] 136 c = [2.0,0.0] 137 d = [0.0, 4.0] 138 e = [2.0, 2.0] 139 f = [4.0,0.0] 140 141 points = [a, b, c, d, e, f] 142 #bac, bce, ecf, dbe 143 vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 144 145 domain = Domain(points, vertices) 146 147 quantity = Quantity(domain) 148 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 149 150 v = quantity.get_maximum_value() 151 assert v == 6 152 153 i = quantity.get_maximum_index() 154 assert i == 3 155 156 x,y = quantity.get_maximum_location() 157 xref, yref = 2.0/3, 8.0/3 158 assert x == xref 159 assert y == yref 160 161 v = quantity.get_values(interpolation_points = [[x,y]]) 162 assert allclose(v, 6) 163 164 165 #Multiple locations for maximum - 166 #Test that the algorithm picks the first occurrence 167 v = quantity.get_maximum_value(indices=[0,1,2]) 168 assert allclose(v, 4) 169 170 i = quantity.get_maximum_index(indices=[0,1,2]) 171 assert i == 1 172 173 x,y = quantity.get_maximum_location(indices=[0,1,2]) 174 xref, yref = 4.0/3, 4.0/3 175 assert x == xref 176 assert y == yref 177 178 v = quantity.get_values(interpolation_points = [[x,y]]) 179 assert allclose(v, 4) 180 181 # More test of indices...... 182 v = quantity.get_maximum_value(indices=[2,3]) 183 assert allclose(v, 6) 184 185 i = quantity.get_maximum_index(indices=[2,3]) 186 assert i == 3 187 188 x,y = quantity.get_maximum_location(indices=[2,3]) 189 xref, yref = 2.0/3, 8.0/3 190 assert x == xref 191 assert y == yref 192 193 v = quantity.get_values(interpolation_points = [[x,y]]) 194 assert allclose(v, 6) 195 196 112 197 113 198 def test_boundary_allocation(self): … … 1389 1474 # quantity.get_values(location = 'centroids') 1390 1475 1476 1477 1478 1479 def test_get_values_2(self): 1480 """Different mesh (working with domain object) - also check centroids. 1481 """ 1482 1483 1484 a = [0.0, 0.0] 1485 b = [0.0, 2.0] 1486 c = [2.0,0.0] 1487 d = [0.0, 4.0] 1488 e = [2.0, 2.0] 1489 f = [4.0,0.0] 1490 1491 points = [a, b, c, d, e, f] 1492 #bac, bce, ecf, dbe 1493 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1494 1495 domain = Domain(points, vertices) 1496 1497 quantity = Quantity(domain) 1498 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 1499 1500 assert allclose(quantity.get_values(location='centroids'), [2,4,4,6]) 1501 assert allclose(quantity.get_values(location='centroids', indices=[1,3]), [4,6]) 1502 1503 1504 assert allclose(quantity.get_values(location='vertices'), [[4,0,2], 1505 [4,2,6], 1506 [6,2,4], 1507 [8,4,6]]) 1508 1509 assert allclose(quantity.get_values(location='vertices', indices=[1,3]), [[4,2,6], 1510 [8,4,6]]) 1511 1512 1513 assert allclose(quantity.get_values(location='edges'), [[1,3,2], 1514 [4,5,3], 1515 [3,5,4], 1516 [5,7,6]]) 1517 assert allclose(quantity.get_values(location='edges', indices=[1,3]), 1518 [[4,5,3], 1519 [5,7,6]]) 1520 1521 # Check averaging over vertices 1522 #a: 0 1523 #b: (4+4+4)/3 1524 #c: (2+2+2)/3 1525 #d: 8 1526 #e: (6+6+6)/3 1527 #f: 4 1528 assert(quantity.get_values(location='unique vertices'), [0, 4, 2, 8, 6, 4]) 1529 1530 1531 1532 1533 1534 1535 def test_get_interpolated_values(self): 1536 1537 from mesh_factory import rectangular 1538 from shallow_water import Domain 1539 from Numeric import zeros, Float 1540 1541 #Create basic mesh 1542 points, vertices, boundary = rectangular(1, 3) 1543 domain = Domain(points, vertices, boundary) 1544 1545 #Constant values 1546 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 1547 [4,4,4],[5,5,5]]) 1548 1549 1550 1551 # Get interpolated values at centroids 1552 interpolation_points = domain.get_centroid_coordinates() 1553 answer = quantity.get_values(location='centroids') 1554 1555 1556 #print quantity.get_values(points=interpolation_points) 1557 assert allclose(answer, quantity.get_values(interpolation_points=interpolation_points)) 1558 1559 1560 #Arbitrary values 1561 quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], 1562 [1,4,-9],[2,5,0]]) 1563 1564 1565 # Get interpolated values at centroids 1566 interpolation_points = domain.get_centroid_coordinates() 1567 answer = quantity.get_values(location='centroids') 1568 #print answer 1569 #print quantity.get_values(points=interpolation_points) 1570 assert allclose(answer, quantity.get_values(interpolation_points=interpolation_points)) 1571 1572 1573 #FIXME TODO 1574 #indices = [0,5,3] 1575 #answer = [0.5,1,5] 1576 #assert allclose(answer, 1577 # quantity.get_values(indices=indices, \ 1578 # location = 'unique vertices')) 1579 1580 1581 1582 1583 def test_get_interpolated_values_2(self): 1584 a = [0.0, 0.0] 1585 b = [0.0, 2.0] 1586 c = [2.0,0.0] 1587 d = [0.0, 4.0] 1588 e = [2.0, 2.0] 1589 f = [4.0,0.0] 1590 1591 points = [a, b, c, d, e, f] 1592 #bac, bce, ecf, dbe 1593 vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 1594 1595 domain = Domain(points, vertices) 1596 1597 quantity = Quantity(domain) 1598 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 1599 1600 #First pick one point 1601 x, y = 2.0/3, 8.0/3 1602 v = quantity.get_values(interpolation_points = [[x,y]]) 1603 assert allclose(v, 6) 1604 1605 # Then another to test that algorithm won't blindly 1606 # reuse interpolation matrix 1607 x, y = 4.0/3, 4.0/3 1608 v = quantity.get_values(interpolation_points = [[x,y]]) 1609 assert allclose(v, 4) 1610 1611 1612 1613 1391 1614 def test_getting_some_vertex_values(self): 1392 1615 """ -
anuga_core/source/anuga/config.py
r3678 r3689 117 117 maximum_allowed_speed = 100.0 #Maximal particle speed of water 118 118 119 minimum_storable_height = 0.001#Water depth below which it is *stored* as 0119 minimum_storable_height = minimum_allowed_height #Water depth below which it is *stored* as 0 -
anuga_core/source/anuga/damage_modelling/test_inundation_damage.py
r3567 r3689 142 142 def tearDown(self): 143 143 #print "***** tearDown ********" 144 145 # FIXME (Ole): Sometimes this fails - is the file open or is it sometimes not created? 144 146 os.remove(self.sww.filename) 145 147 os.remove(self.csv_file) -
anuga_core/source/anuga/examples/netherlands.py
r3678 r3689 166 166 # 167 167 print 'Initial condition' 168 domain.set_quantity('stage', Constant_height(Z, 0.0)) 169 #domain.set_quantity('stage', Constant_stage(inflow_stage/2.0)) 168 domain.set_quantity('stage', expression='Z + 0.0') 170 169 171 170 #Evolve -
anuga_core/source/anuga/examples/runup.py
r3563 r3689 9 9 # Import necessary modules 10 10 #------------------------------------------------------------------------------ 11 12 11 from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross 13 12 from anuga.shallow_water import Domain … … 15 14 from anuga.shallow_water import Dirichlet_boundary 16 15 from anuga.shallow_water import Time_boundary 17 from anuga.shallow_water import Transmissive_ boundary16 from anuga.shallow_water import Transmissive_Momentum_Set_Stage_boundary 18 17 19 18 … … 21 20 # Setup computational domain 22 21 #------------------------------------------------------------------------------ 23 24 points, vertices, boundary = rectangular_cross( 10, 10) # Basic mesh22 N=10 23 points, vertices, boundary = rectangular_cross(N, N) # Basic mesh 25 24 26 25 domain = Domain(points, vertices, boundary) # Create domain … … 31 30 # Setup initial conditions 32 31 #------------------------------------------------------------------------------ 33 34 32 def topography(x,y): 35 33 return -x/2 # linear bed slope 36 #return x*(-(2.0-x)*.5) # curved bed slope34 37 35 38 36 domain.set_quantity('elevation', topography) # Use function for elevation 39 domain.set_quantity('friction', 0. 1) # Constant friction37 domain.set_quantity('friction', 0.0) # Constant friction 40 38 domain.set_quantity('stage', -.4) # Constant negative initial stage 41 39 … … 44 42 # Setup boundary conditions 45 43 #------------------------------------------------------------------------------ 44 from math import sin, pi, exp 46 45 47 from math import sin, pi, exp 46 def waveform(t): 47 return (0.1*sin(t*2*pi)-0.8) * exp(-2*t) 48 49 Bw = Transmissive_Momentum_Set_Stage_boundary(domain, waveform) 48 50 Br = Reflective_boundary(domain) # Solid reflective wall 49 Bt = Transmissive_boundary(domain) # Continue all values on boundary 50 Bd = Dirichlet_boundary([-0.2,0.,0.]) # Constant boundary values 51 Bw = Time_boundary(domain=domain, # Time dependent boundary 52 f=lambda t: [(.1*sin(t*2*pi)-0.3) * exp(-2*t), 0.0, 0.0]) 51 Bd = Dirichlet_boundary([-0.3,0,0]) 53 52 54 53 # Associate boundary tags with boundary objects … … 59 58 # Evolve system through time 60 59 #------------------------------------------------------------------------------ 61 62 for t in domain.evolve(yieldstep = 0.1, finaltime = 10.0): 60 for t in domain.evolve(yieldstep = 0.1, finaltime = 5.0): 63 61 domain.write_time() 64 62 -
anuga_core/source/anuga/examples/runup_convergence.py
r3630 r3689 34 34 sea_level = 0 # Mean sea level 35 35 min_elevation = -20 # Lowest elevation (elevation of offshore flat part) 36 offshore_depth =sea_level-min_elevation # offshore water depth36 offshore_depth = sea_level-min_elevation # offshore water depth 37 37 amplitude = 0.5 # Solitary wave height H 38 38 normalized_amplitude = amplitude/offshore_depth … … 52 52 53 53 # Structured mesh 54 dx = 20 # Resolution: Length of subdivisions on x axis (length)55 dy = 20 # Resolution: Length of subdivisions on y axis (width)54 dx = 30 # Resolution: Length of subdivisions on x axis (length) 55 dy = 30 # Resolution: Length of subdivisions on y axis (width) 56 56 57 57 length = east-west … … 74 74 interior_regions=[[interior_polygon,dx*dy/32]]) 75 75 domain = Domain(meshname, use_cache=True, verbose = True) 76 76 domain.set_minimum_storable_height(0.01) 77 77 78 78 … … 134 134 135 135 136 w0 = domain.get_maximum_inundation_elevation() 137 x0, y0 = domain.get_maximum_inundation_location() 138 print 'Coastline elevation = %.2f at (%.2f, %.2f)' %(w0, x0, y0) 139 w_i = domain.get_quantity('stage').get_values(interpolation_points=[[x0,y0]]) 140 print 'Interpolated elevation at (%.2f, %.2f) is %.2f' %(x0, y0, w_i) 141 136 142 #------------------------------------------------------------------------------ 137 143 # Evolve system through time 138 144 #------------------------------------------------------------------------------ 139 145 146 w_max = w0 140 147 stagestep = [] 141 148 for t in domain.evolve(yieldstep = 1, finaltime = 300): 142 149 domain.write_time() 150 151 w = domain.get_maximum_inundation_elevation() 152 x, y = domain.get_maximum_inundation_location() 153 print ' Coastline elevation = %.2f at (%.2f, %.2f)' %(w, x, y) 154 #w_i = domain.get_quantity('stage').get_values(interpolation_points=[[x,y]]) 155 #print ' Interpolated elevation at (%.2f, %.2f) is %.2f' %(x, y, w_i) 156 157 158 if w > w_max: 159 w_max = w 160 x_max = x 161 y_max = y 143 162 144 163 # Let's find the maximum runup here working directly with the quantities, … … 149 168 # 3 Find min x where h>0 over all t. 150 169 # 4 Perhaps do this across a number of ys 151 170 171 print 'Max coastline elevation = %.2f at (%.2f, %.2f)' %(w_max, x_max, y_max) 172 173 print 'Run up distance - %.2f' %sqrt( (x-x0)**2 + (y-y0)**2 ) 174 175 152 176 153 177 #----------------------------------------------------------------------------- -
anuga_core/source/anuga/fit_interpolate/general_fit_interpolate.py
r3566 r3689 77 77 triangles = ensure_numeric(triangles, Int) 78 78 vertex_coordinates = ensure_absolute(vertex_coordinates, 79 geo_reference = mesh_origin)79 geo_reference = mesh_origin) 80 80 81 81 #Don't pass geo_reference to mesh. It doesn't work. … … 83 83 self.mesh.check_integrity() 84 84 self.root = build_quadtree(self.mesh, 85 max_points_per_cell = max_vertices_per_cell)85 max_points_per_cell = max_vertices_per_cell) 86 86 #print "self.root",self.root.show() 87 87 88 88 89 def __repr__(self): 90 return 'Interpolation object based on: ' + repr(self.mesh) -
anuga_core/source/anuga/fit_interpolate/interpolate.py
r3686 r3689 76 76 a mesh origin, since geospatial has its own mesh origin. 77 77 """ 78 79 # FIXME (Ole): Need an input check 78 80 79 81 # Initialise variabels … … 82 84 83 85 FitInterpolate.__init__(self, 84 vertex_coordinates,85 triangles,86 mesh_origin,87 verbose,88 max_vertices_per_cell)89 90 86 vertex_coordinates, 87 triangles, 88 mesh_origin, 89 verbose, 90 max_vertices_per_cell) 91 92 # FIXME: What is a good start_blocking_len value? 91 93 def interpolate(self, f, point_coordinates = None, 92 94 start_blocking_len = 500000, verbose=False): … … 114 116 Interpolated values at inputted points (z). 115 117 """ 116 #print "point_coordinates interpolate.interpolate",point_coordinates 117 if isinstance(point_coordinates,Geospatial_data): 118 119 120 # FIXME (Ole): Need an input check that dimensions are compatible 121 122 # FIXME (Ole): Why is the interpolation matrix rebuilt everytime the method is called 123 # even if interpolation points are unchanged. 124 125 #print "point_coordinates interpolate.interpolate", point_coordinates 126 if isinstance(point_coordinates, Geospatial_data): 118 127 point_coordinates = point_coordinates.get_data_points( \ 119 128 absolute = True) 120 129 121 130 # Can I interpolate, based on previous point_coordinates? 122 131 if point_coordinates is None: 123 132 if self._A_can_be_reused is True and \ 124 len(self._point_coordinates) < start_blocking_len:133 len(self._point_coordinates) < start_blocking_len: 125 134 z = self._get_point_data_z(f, 126 135 verbose=verbose) … … 134 143 msg = 'ERROR (interpolate.py): No point_coordinates inputted' 135 144 raise Exception(msg) 136 137 138 if point_coordinates is not None: 145 146 if point_coordinates is not None: 139 147 self._point_coordinates = point_coordinates 140 148 if len(point_coordinates) < start_blocking_len or \ … … 144 152 verbose=verbose) 145 153 else: 154 #print 'BLOCKING' 146 155 #Handle blocking 147 156 self._A_can_be_reused = False 148 start =0157 start = 0 149 158 # creating a dummy array to concatenate to. 150 159 … … 156 165 z = zeros((0,)) 157 166 158 for end in range(start_blocking_len 159 ,len(point_coordinates)160 ,start_blocking_len):167 for end in range(start_blocking_len, 168 len(point_coordinates), 169 start_blocking_len): 161 170 t = self.interpolate_block(f, point_coordinates[start:end], 162 171 verbose=verbose) … … 171 180 return z 172 181 182 173 183 def interpolate_block(self, f, point_coordinates = None, verbose=False): 174 184 """ … … 184 194 absolute = True) 185 195 if point_coordinates is not None: 186 self._A = self._build_interpolation_matrix_A(point_coordinates,187 verbose=verbose)196 self._A = self._build_interpolation_matrix_A(point_coordinates, 197 verbose=verbose) 188 198 return self._get_point_data_z(f) 189 199 … … 206 216 207 217 def _build_interpolation_matrix_A(self, 208 point_coordinates,209 verbose = False):218 point_coordinates, 219 verbose = False): 210 220 """Build n x m interpolation matrix, where 211 221 n is the number of data points and … … 224 234 """ 225 235 226 236 #print 'Building interpolation matrix' 227 237 228 238 #Convert point_coordinates to Numeric arrays, in case it was a list. … … 486 496 #Build interpolator 487 497 interpol = Interpolate(vertex_coordinates, 488 489 490 491 492 498 triangles, 499 #point_coordinates = \ 500 #self.interpolation_points, 501 #alpha = 0, 502 verbose = verbose) 493 503 494 504 if verbose: print 'Interpolate' … … 501 511 if len(quantities[name].shape) == 2: 502 512 result = interpol.interpolate(quantities[name][i,:], 503 point_coordinates = \504 self.interpolation_points)513 point_coordinates = \ 514 self.interpolation_points) 505 515 else: 506 516 #Assume no time dependency -
anuga_core/source/anuga/fit_interpolate/test_interpolate.py
r3563 r3689 105 105 assert allclose(interp._build_interpolation_matrix_A(data).todense(), 106 106 [[1./3, 1./3, 1./3]]) 107 108 109 110 def test_simple_interpolation_example(self): 111 112 from mesh_factory import rectangular 113 from shallow_water import Domain 114 from Numeric import zeros, Float 115 from abstract_2d_finite_volumes.quantity import Quantity 116 117 #Create basic mesh 118 points, vertices, boundary = rectangular(1, 3) 119 120 #Create shallow water domain 121 domain = Domain(points, vertices, boundary) 122 123 #--------------- 124 #Constant values 125 #--------------- 126 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 127 [4,4,4],[5,5,5]]) 128 129 130 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 131 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 ) 132 # FIXME: This concat should roll into get_vertex_values 133 134 135 # Get interpolated values at centroids 136 interpolation_points = domain.get_centroid_coordinates() 137 answer = quantity.get_values(location='centroids') 138 139 I = Interpolate(vertex_coordinates, triangles) 140 result = I.interpolate(vertex_values, interpolation_points) 141 assert allclose(result, answer) 142 143 144 #--------------- 145 #Variable values 146 #--------------- 147 quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], 148 [1,4,-9],[2,5,0]]) 149 150 x, y, vertex_values, triangles = quantity.get_vertex_values(xy=True, smooth=False) 151 vertex_coordinates = concatenate( (x[:, NewAxis], y[:, NewAxis]), axis=1 ) 152 # FIXME: This concat should roll into get_vertex_values 153 154 155 # Get interpolated values at centroids 156 interpolation_points = domain.get_centroid_coordinates() 157 answer = quantity.get_values(location='centroids') 158 159 I = Interpolate(vertex_coordinates, triangles) 160 result = I.interpolate(vertex_values, interpolation_points) 161 assert allclose(result, answer) 162 107 163 108 164 def test_quad_tree(self): … … 770 826 771 827 772 def test_interpolate_reuse (self):828 def test_interpolate_reuse_if_None(self): 773 829 a = [-1.0, 0.0] 774 830 b = [3.0, 4.0] … … 827 883 except: 828 884 pass 885 886 def xxtest_interpolate_reuse_if_same(self): 887 888 # This on tests that repeated identical interpolation 889 # points makes use of precomputed matrix (Ole) 890 # This is not really a test and is disabled for now 891 892 a = [-1.0, 0.0] 893 b = [3.0, 4.0] 894 c = [4.0, 1.0] 895 d = [-3.0, 2.0] #3 896 e = [-1.0, -2.0] 897 f = [1.0, -2.0] #5 898 899 vertices = [a, b, c, d,e,f] 900 triangles = [[0,1,3], [1,0,2], [0,4,5], [0,5,2]] #abd bac aef afc 901 902 903 point_coords = [[-2.0, 2.0], 904 [-1.0, 1.0], 905 [0.0, 2.0], 906 [1.0, 1.0], 907 [2.0, 1.0], 908 [0.0, 0.0], 909 [1.0, 0.0], 910 [0.0, -1.0], 911 [-0.2, -0.5], 912 [-0.9, -1.5], 913 [0.5, -1.9], 914 [3.0, 1.0]] 915 916 interp = Interpolate(vertices, triangles) 917 f = array([linear_function(vertices),2*linear_function(vertices) ]) 918 f = transpose(f) 919 z = interp.interpolate(f, point_coords) 920 answer = [linear_function(point_coords), 921 2*linear_function(point_coords) ] 922 answer = transpose(answer) 923 924 assert allclose(z, answer) 925 assert allclose(interp._A_can_be_reused, True) 926 927 928 z = interp.interpolate(f) # None 929 assert allclose(z, answer) 930 z = interp.interpolate(f, point_coords) # Repeated (not really a test) 931 assert allclose(z, answer) 829 932 830 933 … … 1469 1572 1470 1573 suite = unittest.makeSuite(Test_Interpolate,'test') 1471 #suite = unittest.makeSuite(Test_Interpolate,'test_interpolat e_sww2csv')1574 #suite = unittest.makeSuite(Test_Interpolate,'test_interpolation_function_outside_point') 1472 1575 runner = unittest.TextTestRunner(verbosity=1) 1473 1576 runner.run(suite) -
anuga_core/source/anuga/pmesh/mesh_interface.py
r3624 r3689 28 28 mesh_geo_reference=None, 29 29 minimum_triangle_angle=28.0, 30 use_cache=False, 30 31 verbose=True): 31 32 """Create mesh from bounding polygons, and resolutions. -
anuga_core/source/anuga/shallow_water/shallow_water_domain.py
r3681 r3689 70 70 #$LastChangedRevision$ 71 71 #$LastChangedBy$ 72 73 from Numeric import zeros, ones, Float, array, sum, size 74 from Numeric import compress, arange 72 75 73 76 … … 217 220 218 221 222 223 def get_wet_elements(self, indices=None): 224 """Return indices for elements where h > minimum_allowed_height 225 226 Optional argument: 227 indices is the set of element ids that the operation applies to. 228 229 Usage: 230 indices = get_wet_elements() 231 232 Note, centroid values are used for this operation 233 """ 234 235 # Water depth below which it is considered to be 0 in the model 236 # FIXME (Ole): Allow this to be specified as a keyword argument as well 237 from anuga.config import minimum_allowed_height 238 239 240 elevation = self.get_quantity('elevation').get_values(location='centroids', indices=indices) 241 stage = self.get_quantity('stage').get_values(location='centroids', indices=indices) 242 depth = stage - elevation 243 244 # Select indices for which depth > 0 245 wet_indices = compress(depth > minimum_allowed_height, arange(len(depth))) 246 return wet_indices 247 248 249 def get_maximum_inundation_elevation(self, indices=None): 250 """Return highest elevation where h > 0 251 252 Optional argument: 253 indices is the set of element ids that the operation applies to. 254 255 Usage: 256 q = get_maximum_inundation_elevation() 257 258 Note, centroid values are used for this operation 259 """ 260 261 wet_elements = self.get_wet_elements(indices) 262 return self.get_quantity('elevation').get_maximum_value(indices=wet_elements) 263 264 265 def get_maximum_inundation_location(self, indices=None): 266 """Return highest elevation where h > 0 267 268 Optional argument: 269 indices is the set of element ids that the operation applies to. 270 271 Usage: 272 q = get_maximum_inundation_elevation() 273 274 Note, centroid values are used for this operation 275 """ 276 277 wet_elements = self.get_wet_elements(indices) 278 return self.get_quantity('elevation').get_maximum_location(indices=wet_elements) 279 280 281 282 219 283 def initialise_visualiser(self,scale_z=1.0,rect=None): 220 284 #Realtime visualisation … … 400 464 """ 401 465 402 from Numeric import zeros, Float403 404 466 assert len(q) == 3,\ 405 467 'Vector of conserved quantities must have length 3'\ … … 451 513 from anuga.config import g, epsilon 452 514 from math import sqrt 453 from Numeric import array454 515 455 516 #Align momentums with x-axis … … 539 600 540 601 import sys 541 from Numeric import zeros, Float542 602 543 603 N = domain.number_of_elements … … 614 674 """ 615 675 import sys 616 from Numeric import zeros, Float617 676 618 677 N = domain.number_of_elements … … 643 702 644 703 import sys 645 from Numeric import zeros, Float646 704 647 705 N = domain.number_of_elements … … 794 852 """ 795 853 796 from Numeric import zeros, Float797 798 854 N = domain.number_of_elements 799 855 beta_h = domain.beta_h … … 865 921 Wrapper for c-extension 866 922 """ 867 868 from Numeric import zeros, Float869 923 870 924 N = domain.number_of_elements … … 1046 1100 self.normals = domain.normals 1047 1101 1048 from Numeric import zeros, Float1049 1102 self.conserved_quantities = zeros(3, Float) 1050 1103 … … 1204 1257 """ 1205 1258 1206 from Numeric import zeros, Float, array, sum1207 1208 1259 xmom = domain.quantities['xmomentum'].explicit_update 1209 1260 ymom = domain.quantities['ymomentum'].explicit_update … … 1380 1431 2: a scalar 1381 1432 """ 1382 1383 from Numeric import ones, Float, array1384 1385 1433 1386 1434 if callable(f): -
anuga_core/source/anuga/shallow_water/shallow_water_ext.c
r3678 r3689 342 342 else 343 343 alpha = 1.0; //Flat bed 344 344 345 346 //alpha = 1.0; //Always deep FIXME: This actually looks good now 345 347 346 348 //printf("dz = %.3f, alpha = %.8f\n", dz, alpha); -
anuga_core/source/anuga/shallow_water/test_shallow_water_domain.py
r3678 r3689 5 5 6 6 from anuga.config import g, epsilon 7 from Numeric import allclose, a rray, zeros, ones, Float, take7 from Numeric import allclose, alltrue, array, zeros, ones, Float, take 8 8 from anuga.utilities.numerical_tools import mean 9 9 … … 668 668 669 669 670 def test_catching_negative_heights(self): 671 670 def xtest_catching_negative_heights(self): 671 672 #OBSOLETE 673 672 674 a = [0.0, 0.0] 673 675 b = [0.0, 2.0] … … 701 703 702 704 705 706 def test_get_wet_elements(self): 707 708 a = [0.0, 0.0] 709 b = [0.0, 2.0] 710 c = [2.0,0.0] 711 d = [0.0, 4.0] 712 e = [2.0, 2.0] 713 f = [4.0,0.0] 714 715 points = [a, b, c, d, e, f] 716 #bac, bce, ecf, dbe 717 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 718 719 domain = Domain(points, vertices) 720 val0 = 2.+2.0/3 721 val1 = 4.+4.0/3 722 val2 = 8.+2.0/3 723 val3 = 2.+8.0/3 724 725 zl=zr=5 726 domain.set_quantity('elevation', zl*ones( (4,3) )) 727 domain.set_quantity('stage', [[val0, val0-1, val0-2], 728 [val1, val1+1, val1], 729 [val2, val2-2, val2], 730 [val3-0.5, val3, val3]]) 731 732 733 734 #print domain.get_quantity('elevation').get_values(location='centroids') 735 #print domain.get_quantity('stage').get_values(location='centroids') 736 domain.check_integrity() 737 738 indices = domain.get_wet_elements() 739 assert allclose(indices, [1,2]) 740 741 indices = domain.get_wet_elements(indices=[0,1,3]) 742 assert allclose(indices, [1]) 743 744 745 746 def test_get_maximum_inundation_1(self): 747 748 a = [0.0, 0.0] 749 b = [0.0, 2.0] 750 c = [2.0,0.0] 751 d = [0.0, 4.0] 752 e = [2.0, 2.0] 753 f = [4.0,0.0] 754 755 points = [a, b, c, d, e, f] 756 #bac, bce, ecf, dbe 757 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 758 759 domain = Domain(points, vertices) 760 761 domain.set_quantity('elevation', lambda x, y: x+2*y) #2 4 4 6 762 domain.set_quantity('stage', 3) 763 764 domain.check_integrity() 765 766 indices = domain.get_wet_elements() 767 assert allclose(indices, [0]) 768 769 q = domain.get_maximum_inundation_elevation() 770 assert allclose(q, domain.get_quantity('elevation').get_values(location='centroids')[0]) 771 772 x, y = domain.get_maximum_inundation_location() 773 assert allclose([x, y], domain.get_centroid_coordinates()[0]) 774 775 776 def test_get_maximum_inundation_2(self): 777 """test_get_maximum_inundation_2(self) 778 779 Test multiple wet cells with same elevation 780 """ 781 782 a = [0.0, 0.0] 783 b = [0.0, 2.0] 784 c = [2.0,0.0] 785 d = [0.0, 4.0] 786 e = [2.0, 2.0] 787 f = [4.0,0.0] 788 789 points = [a, b, c, d, e, f] 790 #bac, bce, ecf, dbe 791 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 792 793 domain = Domain(points, vertices) 794 795 domain.set_quantity('elevation', lambda x, y: x+2*y) #2 4 4 6 796 domain.set_quantity('stage', 4.1) 797 798 domain.check_integrity() 799 800 indices = domain.get_wet_elements() 801 assert allclose(indices, [0,1,2]) 802 803 q = domain.get_maximum_inundation_elevation() 804 assert allclose(q, 4) 805 806 x, y = domain.get_maximum_inundation_location() 807 assert allclose([x, y], domain.get_centroid_coordinates()[1]) 808 809 810 def test_get_maximum_inundation_3(self): 811 """test_get_maximum_inundation_3(self) 812 813 Test real runup example 814 """ 815 816 from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross 817 818 initial_runup_height = -0.4 819 final_runup_height = -0.3 820 821 822 #-------------------------------------------------------------- 823 # Setup computational domain 824 #-------------------------------------------------------------- 825 N = 5 826 points, vertices, boundary = rectangular_cross(N, N) 827 domain = Domain(points, vertices, boundary) 828 829 #-------------------------------------------------------------- 830 # Setup initial conditions 831 #-------------------------------------------------------------- 832 def topography(x,y): 833 return -x/2 # linear bed slope 834 835 836 domain.set_quantity('elevation', topography) # Use function for elevation 837 domain.set_quantity('friction', 0.) # Zero friction 838 domain.set_quantity('stage', initial_runup_height) # Constant negative initial stage 839 840 841 #-------------------------------------------------------------- 842 # Setup boundary conditions 843 #-------------------------------------------------------------- 844 Br = Reflective_boundary(domain) # Reflective wall 845 Bd = Dirichlet_boundary([final_runup_height, # Constant inflow 846 0, 847 0]) 848 849 # All reflective to begin with (still water) 850 domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br}) 851 852 853 #-------------------------------------------------------------- 854 # Test initial inundation height 855 #-------------------------------------------------------------- 856 857 indices = domain.get_wet_elements() 858 z = domain.get_quantity('elevation').\ 859 get_values(location='centroids', indices=indices) 860 assert alltrue(z<initial_runup_height) 861 862 q = domain.get_maximum_inundation_elevation() 863 assert allclose(q, initial_runup_height, rtol = 1.0/N) # First order accuracy 864 865 x, y = domain.get_maximum_inundation_location() 866 867 qref = domain.get_quantity('elevation').get_values(interpolation_points = [[x, y]]) 868 assert allclose(q, qref) 869 870 871 wet_elements = domain.get_wet_elements() 872 wet_elevations = domain.get_quantity('elevation').get_values(location='centroids', 873 indices=wet_elements) 874 assert alltrue(wet_elevations<initial_runup_height) 875 assert allclose(wet_elevations, z) 876 877 878 #print domain.get_quantity('elevation').get_maximum_value(indices=wet_elements) 879 #print domain.get_quantity('elevation').get_maximum_location(indices=wet_elements) 880 #print domain.get_quantity('elevation').get_maximum_index(indices=wet_elements) 881 882 883 #-------------------------------------------------------------- 884 # Let triangles adjust 885 #-------------------------------------------------------------- 886 for t in domain.evolve(yieldstep = 0.1, finaltime = 1.0): 887 pass 888 889 890 #-------------------------------------------------------------- 891 # Test inundation height again 892 #-------------------------------------------------------------- 893 894 indices = domain.get_wet_elements() 895 z = domain.get_quantity('elevation').\ 896 get_values(location='centroids', indices=indices) 897 898 assert alltrue(z<initial_runup_height) 899 900 q = domain.get_maximum_inundation_elevation() 901 assert allclose(q, initial_runup_height, rtol = 1.0/N) # First order accuracy 902 903 x, y = domain.get_maximum_inundation_location() 904 qref = domain.get_quantity('elevation').get_values(interpolation_points = [[x, y]]) 905 assert allclose(q, qref) 906 907 908 #-------------------------------------------------------------- 909 # Update boundary to allow inflow 910 #-------------------------------------------------------------- 911 domain.modify_boundary({'right': Bd}) 912 913 914 #-------------------------------------------------------------- 915 # Evolve system through time 916 #-------------------------------------------------------------- 917 for t in domain.evolve(yieldstep = 0.1, finaltime = 3.0): 918 #domain.write_time() 919 pass 920 921 #-------------------------------------------------------------- 922 # Test inundation height again 923 #-------------------------------------------------------------- 924 925 indices = domain.get_wet_elements() 926 z = domain.get_quantity('elevation').\ 927 get_values(location='centroids', indices=indices) 928 929 assert alltrue(z<final_runup_height) 930 931 q = domain.get_maximum_inundation_elevation() 932 assert allclose(q, final_runup_height, rtol = 1.0/N) # First order accuracy 933 934 x, y = domain.get_maximum_inundation_location() 935 qref = domain.get_quantity('elevation').get_values(interpolation_points = [[x, y]]) 936 assert allclose(q, qref) 937 938 939 wet_elements = domain.get_wet_elements() 940 wet_elevations = domain.get_quantity('elevation').get_values(location='centroids', 941 indices=wet_elements) 942 assert alltrue(wet_elevations<final_runup_height) 943 assert allclose(wet_elevations, z) 944 703 945 704 946 … … 2757 2999 2758 3000 #Initial condition 2759 domain.set_quantity('stage', Constant_height(x_slope, 0.05))3001 domain.set_quantity('stage', expression = 'elevation + 0.05') 2760 3002 domain.check_integrity() 2761 3003 … … 3354 3596 self.failUnless(domain.geo_reference.xllcorner == 140.0, 3355 3597 "bad geo_referece") 3356 #************ 3598 3599 3600 #************ 3357 3601 3358 3602 … … 3398 3642 self.failUnless(domain.geo_reference.xllcorner == 140.0, 3399 3643 "bad geo_referece") 3400 #************3644 #************ 3401 3645 os.remove(fileName) 3402 3646 3403 3647 #------------------------------------------------------------- 3648 3404 3649 if __name__ == "__main__": 3405 3650 suite = unittest.makeSuite(Test_Shallow_Water,'test') 3651 #suite = unittest.makeSuite(Test_Shallow_Water,'test_get_maximum_inundation_3') 3406 3652 runner = unittest.TextTestRunner(verbosity=1) 3407 3653 runner.run(suite)
Note: See TracChangeset
for help on using the changeset viewer.