Changeset 5903
- Timestamp:
- Nov 6, 2008, 3:37:26 PM (16 years ago)
- Location:
- anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes
- Files:
-
- 21 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/domain.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Class Domain - 2D triangular domains for finite-volume computations of 4 2 conservation laws. … … 10 8 """ 11 9 12 ##from numpy.oldnumeric import allclose, argmax, zeros, Float 13 from numpy import allclose, argmax, zeros, float 10 import numpy 11 14 12 from anuga.config import epsilon 15 13 from anuga.config import beta_euler, beta_rk2 … … 108 106 109 107 if verbose: print 'Initialising Domain' 110 ## from numpy.oldnumeric import zeros, Float, Int, ones111 from numpy import zeros, float, int, ones112 108 from quantity import Quantity 113 109 … … 159 155 for key in self.full_send_dict: 160 156 buffer_shape = self.full_send_dict[key][0].shape[0] 161 self.full_send_dict[key].append( zeros( (buffer_shape,self.nsys) ,float))157 self.full_send_dict[key].append(numpy.zeros( (buffer_shape,self.nsys), numpy.float)) 162 158 163 159 164 160 for key in self.ghost_recv_dict: 165 161 buffer_shape = self.ghost_recv_dict[key][0].shape[0] 166 self.ghost_recv_dict[key].append( zeros( (buffer_shape,self.nsys) ,float))162 self.ghost_recv_dict[key].append(numpy.zeros( (buffer_shape,self.nsys), numpy.float)) 167 163 168 164 … … 172 168 N = len(self) #number_of_elements 173 169 self.number_of_elements = N 174 self.tri_full_flag = ones(N,int)170 self.tri_full_flag = numpy.ones(N, numpy.int) 175 171 for i in self.ghost_recv_dict.keys(): 176 172 for id in self.ghost_recv_dict[i][0]: … … 179 175 # Test the assumption that all full triangles are store before 180 176 # the ghost triangles. 181 if not allclose(self.tri_full_flag[:self.number_of_full_nodes],1):177 if not numpy.allclose(self.tri_full_flag[:self.number_of_full_nodes],1): 182 178 print 'WARNING: Not all full triangles are store before ghost triangles' 183 179 … … 234 230 # calculation 235 231 N = len(self) # Number_of_triangles 236 self.already_computed_flux = zeros((N, 3),int)232 self.already_computed_flux = numpy.zeros((N, 3), numpy.int) 237 233 238 234 # Storage for maximal speeds computed for each triangle by 239 235 # compute_fluxes 240 236 # This is used for diagnostics only (reset at every yieldstep) 241 self.max_speed = zeros(N,float)237 self.max_speed = numpy.zeros(N, numpy.float) 242 238 243 239 if mesh_filename is not None: … … 270 266 """ 271 267 272 ## from numpy.oldnumeric import zeros, Float273 from numpy import zeros, float274 275 268 if not (vertex is None or edge is None): 276 269 msg = 'Values for both vertex and edge was specified.' … … 278 271 raise msg 279 272 280 q = zeros( len(self.conserved_quantities),float)273 q = numpy.zeros( len(self.conserved_quantities), numpy.float) 281 274 282 275 for i, name in enumerate(self.conserved_quantities): … … 774 767 # Find index of largest computed flux speed 775 768 if triangle_id is None: 776 k = self.k = argmax(self.max_speed)769 k = self.k = numpy.argmax(self.max_speed) 777 770 else: 778 771 errmsg = 'Triangle_id %d does not exist in mesh: %s' %(triangle_id, … … 1091 1084 1092 1085 """ 1086 print '.evolve: 0' 1093 1087 1094 1088 from anuga.config import min_timestep, max_timestep, epsilon … … 1101 1095 %self.get_boundary_tags() 1102 1096 assert hasattr(self, 'boundary_objects'), msg 1097 print '.evolve: 1' 1103 1098 1104 1099 … … 1109 1104 1110 1105 self._order_ = self.default_order 1106 print '.evolve: 2' 1111 1107 1112 1108 … … 1132 1128 self.number_of_first_order_steps = 0 1133 1129 1134 1130 print '.evolve: 3' 1135 1131 # Update ghosts 1136 1132 self.update_ghosts() 1137 1133 1134 print '.evolve: 4' 1138 1135 # Initial update of vertex and edge values 1139 1136 self.distribute_to_vertices_and_edges() 1140 1137 1138 print '.evolve: 5' 1141 1139 # Update extrema if necessary (for reporting) 1142 1140 self.update_extrema() 1143 1141 1142 print '.evolve: 6' 1144 1143 # Initial update boundary values 1145 1144 self.update_boundary() 1146 1145 1146 print '.evolve: 7' 1147 1147 # Or maybe restore from latest checkpoint 1148 1148 if self.checkpoint is True: 1149 1149 self.goto_latest_checkpoint() 1150 print '.evolve: 8' 1150 1151 1151 1152 if skip_initial_step is False: … … 1207 1208 self.number_of_steps = 0 1208 1209 self.number_of_first_order_steps = 0 1209 self.max_speed = zeros(N,float)1210 self.max_speed = numpy.zeros(N, numpy.float) 1210 1211 1211 1212 def evolve_one_euler_step(self, yieldstep, finaltime): … … 1573 1574 """ 1574 1575 1575 ## from numpy.oldnumeric import ones, sum, equal, Float1576 from numpy import ones, sum, equal, float1577 1578 1576 N = len(self) # Number_of_triangles 1579 1577 d = len(self.conserved_quantities) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/ermapper_grids.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 5 3 # from os import open, write, read 6 ##import numpy.oldnumeric as Numeric7 4 import numpy 8 5 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/general_mesh.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py 2 3 4 ##from numpy.oldnumeric import concatenate, reshape, take, allclose 5 ##from numpy.oldnumeric import array, zeros, Int, Float, sqrt, sum, arange 6 from numpy import concatenate, reshape, take, allclose 7 from numpy import array, zeros, int, float, sqrt, sum, arange 1 import numpy 8 2 9 3 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 96 90 ## self.triangles = array(triangles, Int) 97 91 ## self.nodes = array(nodes, Float) 98 self.triangles = array(triangles,int)99 self.nodes = array(nodes,float)92 self.triangles = numpy.array(triangles, numpy.int) 93 self.nodes = numpy.array(nodes, numpy.float) 100 94 101 95 … … 153 147 154 148 ## self.xy_extent = array(xy_extent, Float) 155 self.xy_extent = array(xy_extent,float)149 self.xy_extent = numpy.array(xy_extent, numpy.float) 156 150 157 151 … … 160 154 ## self.areas = zeros(N, Float) 161 155 ## self.edgelengths = zeros((N, 3), Float) 162 self.normals = zeros((N, 6),float)163 self.areas = zeros(N,float)164 self.edgelengths = zeros((N, 3),float)156 self.normals = numpy.zeros((N, 6), numpy.float) 157 self.areas = numpy.zeros(N, numpy.float) 158 self.edgelengths = numpy.zeros((N, 3), numpy.float) 165 159 166 160 # Get x,y coordinates for all triangles and store … … 197 191 # - Stored as six floats n0x,n0y,n1x,n1y,n2x,n2y per triangle 198 192 199 n0 = array([x2 - x1, y2 - y1])200 l0 = sqrt(sum(n0**2))201 202 n1 = array([x0 - x2, y0 - y2])203 l1 = sqrt(sum(n1**2))204 205 n2 = array([x1 - x0, y1 - y0])206 l2 = sqrt(sum(n2**2))193 n0 = numpy.array([x2 - x1, y2 - y1]) 194 l0 = numpy.sqrt(numpy.sum(n0**2)) 195 196 n1 = numpy.array([x0 - x2, y0 - y2]) 197 l1 = numpy.sqrt(numpy.sum(n1**2)) 198 199 n2 = numpy.array([x1 - x0, y1 - y0]) 200 l2 = numpy.sqrt(numpy.sum(n2**2)) 207 201 208 202 # Normalise … … 284 278 if absolute is True: 285 279 if not self.geo_reference.is_absolute(): 286 return V + array([self.geo_reference.get_xllcorner(),287 self.geo_reference.get_yllcorner()])280 return V + numpy.array([self.geo_reference.get_xllcorner(), 281 self.geo_reference.get_yllcorner()]) 288 282 else: 289 283 return V … … 321 315 i = triangle_id 322 316 msg = 'triangle_id must be an integer' 323 print 'type(triangle_id)=%s. triangle_id=%s' % (type(triangle_id), str(triangle_id))324 317 assert int(i) == i, msg 325 318 assert 0 <= i < self.number_of_triangles … … 327 320 i3 = 3*i 328 321 if absolute is True and not self.geo_reference.is_absolute(): 329 offset =array([self.geo_reference.get_xllcorner(),330 self.geo_reference.get_yllcorner()])331 return array([V[i3,:]+offset,332 V[i3+1,:]+offset,333 V[i3+2,:]+offset])322 offset = numpy.array([self.geo_reference.get_xllcorner(), 323 self.geo_reference.get_yllcorner()]) 324 return numpy.array([V[i3,:]+offset, 325 V[i3+1,:]+offset, 326 V[i3+2,:]+offset]) 334 327 else: 335 return array([V[i3,:], V[i3+1,:], V[i3+2,:]])328 return numpy.array([V[i3,:], V[i3+1,:], V[i3+2,:]]) 336 329 337 330 … … 361 354 M = self.number_of_triangles 362 355 ## vertex_coordinates = zeros((3*M, 2), Float) 363 vertex_coordinates = zeros((3*M, 2),float)356 vertex_coordinates = numpy.zeros((3*M, 2), numpy.float) 364 357 365 358 for i in range(M): … … 388 381 indices = range(M) 389 382 390 return take(self.triangles, indices, axis=0)383 return numpy.take(self.triangles, indices, axis=0) 391 384 392 385 … … 422 415 #T = reshape(array(range(K)).astype(Int), (M,3)) 423 416 ## T = reshape(arange(K).astype(Int), (M,3)) # Faster 424 T = reshape(arange(K).astype(int), (M,3)) # Faster417 T = numpy.reshape(numpy.arange(K).astype(numpy.int), (M,3)) # Faster 425 418 426 419 return T … … 452 445 if node is not None: 453 446 # Get index for this node 454 first = sum(self.number_of_triangles_per_node[:node])447 first = numpy.sum(self.number_of_triangles_per_node[:node]) 455 448 456 449 # Get number of triangles for this node … … 465 458 triangle_list.append( (volume_id, vertex_id) ) 466 459 467 triangle_list = array(triangle_list)460 triangle_list = numpy.array(triangle_list) 468 461 else: 469 462 # Get info for all nodes recursively. … … 542 535 543 536 # Count number of triangles per node 544 number_of_triangles_per_node = zeros(self.number_of_full_nodes)537 number_of_triangles_per_node = numpy.zeros(self.number_of_full_nodes) 545 538 for volume_id, triangle in enumerate(self.get_triangles()): 546 539 for vertex_id in triangle: … … 548 541 549 542 # Allocate space for inverted structure 550 number_of_entries = sum(number_of_triangles_per_node)551 vertex_value_indices = zeros(number_of_entries)543 number_of_entries = numpy.sum(number_of_triangles_per_node) 544 vertex_value_indices = numpy.zeros(number_of_entries, numpy.int) 552 545 553 546 # Register (triangle, vertex) indices for each node … … 611 604 """ 612 605 613 return sum(self.areas) 614 615 616 606 return numpy.sum(self.areas) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/generic_boundary_conditions.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """boundary.py - Classes for implementing boundary conditions 4 2 """ … … 9 7 from anuga.fit_interpolate.interpolate import Modeltime_too_late 10 8 from anuga.fit_interpolate.interpolate import Modeltime_too_early 9 10 import numpy 11 11 12 12 … … 69 69 raise Exception, msg 70 70 71 # from numpy.oldnumeric import array, Float 72 from numpy import array, float 73 self.conserved_quantities=array(conserved_quantities).astype(float) 71 self.conserved_quantities=numpy.array(conserved_quantities).astype(numpy.float) 74 72 75 73 def __repr__(self): … … 99 97 raise msg 100 98 101 102 # from numpy.oldnumeric import array, Float103 from numpy import array, float104 99 try: 105 q = array(q).astype(float)100 q = numpy.array(q).astype(numpy.float) 106 101 except: 107 102 msg = 'Return value from time boundary function could ' … … 140 135 def __init__(self, filename, domain): 141 136 import time 142 # from numpy.oldnumeric import array143 from numpy import array144 137 from anuga.config import time_format 145 138 from anuga.abstract_2d_finite_volumes.util import File_function … … 211 204 212 205 import time 213 # from numpy.oldnumeric import array, zeros, Float214 from numpy import array, zeros, float215 206 from anuga.config import time_format 216 207 from anuga.abstract_2d_finite_volumes.util import file_function … … 228 219 229 220 if verbose: print 'Find midpoint coordinates of entire boundary' 230 self.midpoint_coordinates = zeros( (len(domain.boundary), 2),float)221 self.midpoint_coordinates = numpy.zeros( (len(domain.boundary), 2), numpy.float) 231 222 boundary_keys = domain.boundary.keys() 232 223 … … 248 239 249 240 # Compute midpoints 250 if edge_id == 0: m = array([(x1 + x2)/2, (y1 + y2)/2])251 if edge_id == 1: m = array([(x0 + x2)/2, (y0 + y2)/2])252 if edge_id == 2: m = array([(x1 + x0)/2, (y1 + y0)/2])241 if edge_id == 0: m = numpy.array([(x1 + x2)/2, (y1 + y2)/2]) 242 if edge_id == 1: m = numpy.array([(x0 + x2)/2, (y0 + y2)/2]) 243 if edge_id == 2: m = numpy.array([(x1 + x0)/2, (y1 + y0)/2]) 253 244 254 245 # Convert to absolute UTM coordinates -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/mesh_factory.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Library of standard meshes and facilities for reading various 4 2 mesh file formats 5 3 """ 4 5 import numpy 6 6 7 7 … … 75 75 76 76 from anuga.config import epsilon 77 # from numpy.oldnumeric import zeros, Float, Int78 from numpy import zeros, float, int79 77 80 78 delta1 = float(len1)/m … … 96 94 index = Index(n,m) 97 95 98 points = zeros( (Np,2),float)96 points = numpy.zeros( (Np,2), numpy.float) 99 97 100 98 for i in range(m+1): … … 108 106 109 107 110 elements = zeros( (Nt,3),int)108 elements = numpy.zeros( (Nt,3), numpy.int) 111 109 boundary = {} 112 110 nt = -1 … … 152 150 153 151 from anuga.config import epsilon 154 # from numpy.oldnumeric import zeros, Float, Int155 from numpy import zeros, float, int156 152 157 153 delta1 = float(len1)/m … … 212 208 """ 213 209 214 # from numpy.oldnumeric import array215 from numpy import array216 210 import math 217 211 … … 516 510 517 511 # from numpy.oldnumeric import array 518 from numpy import array519 512 import math 520 513 … … 598 591 """ 599 592 600 # from numpy.oldnumeric import array601 from numpy import array602 593 import math 603 594 … … 693 684 """ 694 685 695 # from numpy.oldnumeric import array696 from numpy import array697 686 import math 698 699 687 from anuga.config import epsilon 700 701 688 702 689 deltax = lenx/float(m) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/neighbour_mesh.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Classes implementing general 2D triangular mesh with neighbour structure. 4 2 … … 13 11 from anuga.caching import cache 14 12 from math import pi, sqrt 15 #from numpy.oldnumeric import array, allclose 16 from numpy import array, allclose 17 13 import numpy 18 14 19 15 class Mesh(General_mesh): … … 82 78 """ 83 79 84 85 86 # from numpy.oldnumeric import array, zeros, Int, Float, maximum, sqrt, sum87 from numpy import array, zeros, int, float, maximum, sqrt, sum88 89 80 General_mesh.__init__(self, coordinates, triangles, 90 81 number_of_full_nodes=\ … … 102 93 103 94 #Allocate space for geometric quantities 104 self.centroid_coordinates = zeros((N, 2),float)105 106 self.radii = zeros(N,float)107 108 self.neighbours = zeros((N, 3),int)109 self.neighbour_edges = zeros((N, 3),int)110 self.number_of_boundaries = zeros(N,int)111 self.surrogate_neighbours = zeros((N, 3),int)95 self.centroid_coordinates = numpy.zeros((N, 2), numpy.float) 96 97 self.radii = numpy.zeros(N, numpy.float) 98 99 self.neighbours = numpy.zeros((N, 3), numpy.int) 100 self.neighbour_edges = numpy.zeros((N, 3), numpy.int) 101 self.number_of_boundaries = numpy.zeros(N, numpy.int) 102 self.surrogate_neighbours = numpy.zeros((N, 3), numpy.int) 112 103 113 104 #Get x,y coordinates for all triangles and store … … 128 119 129 120 #Compute centroid 130 centroid = array([(x0 + x1 + x2)/3, (y0 + y1 + y2)/3])121 centroid = numpy.array([(x0 + x1 + x2)/3, (y0 + y1 + y2)/3]) 131 122 self.centroid_coordinates[i] = centroid 132 123 … … 137 128 138 129 #Midpoints 139 m0 = array([(x1 + x2)/2, (y1 + y2)/2])140 m1 = array([(x0 + x2)/2, (y0 + y2)/2])141 m2 = array([(x1 + x0)/2, (y1 + y0)/2])130 m0 = numpy.array([(x1 + x2)/2, (y1 + y2)/2]) 131 m1 = numpy.array([(x0 + x2)/2, (y0 + y2)/2]) 132 m2 = numpy.array([(x1 + x0)/2, (y1 + y0)/2]) 142 133 143 134 #The radius is the distance from the centroid of 144 135 #a triangle to the midpoint of the side of the triangle 145 136 #closest to the centroid 146 d0 = sqrt(sum( (centroid-m0)**2 ))147 d1 = sqrt(sum( (centroid-m1)**2 ))148 d2 = sqrt(sum( (centroid-m2)**2 ))137 d0 = numpy.sqrt(numpy.sum( (centroid-m0)**2 )) 138 d1 = numpy.sqrt(numpy.sum( (centroid-m1)**2 )) 139 d2 = numpy.sqrt(numpy.sum( (centroid-m2)**2 )) 149 140 150 141 self.radii[i] = min(d0, d1, d2) … … 154 145 #of inscribed circle is computed 155 146 156 a = sqrt((x0-x1)**2+(y0-y1)**2)157 b = sqrt((x1-x2)**2+(y1-y2)**2)158 c = sqrt((x2-x0)**2+(y2-y0)**2)147 a = numpy.sqrt((x0-x1)**2+(y0-y1)**2) 148 b = numpy.sqrt((x1-x2)**2+(y1-y2)**2) 149 c = numpy.sqrt((x2-x0)**2+(y2-y0)**2) 159 150 160 151 self.radii[i]=2.0*self.areas[i]/(a+b+c) … … 392 383 self.element_tag is defined 393 384 """ 394 # from numpy.oldnumeric import array, Int395 from numpy import array, int396 385 397 386 if tagged_elements is None: … … 400 389 #Check that all keys in given boundary exist 401 390 for tag in tagged_elements.keys(): 402 tagged_elements[tag] = array(tagged_elements[tag]).astype(int)391 tagged_elements[tag] = numpy.array(tagged_elements[tag]).astype(numpy.int) 403 392 404 393 msg = 'Not all elements exist. ' … … 468 457 """ 469 458 470 # from numpy.oldnumeric import allclose, sqrt, array, minimum, maximum471 from numpy import allclose, sqrt, array, minimum, maximum472 459 from anuga.utilities.numerical_tools import angle, ensure_numeric 473 460 … … 483 470 inverse_segments = {} 484 471 p0 = None 485 mindist = sqrt(sum((pmax-pmin)**2)) # Start value across entire mesh472 mindist = numpy.sqrt(numpy.sum((pmax-pmin)**2)) # Start value across entire mesh 486 473 for i, edge_id in self.boundary.keys(): 487 474 # Find vertex ids for boundary segment … … 493 480 B = self.get_vertex_coordinate(i, b, absolute=True) # End 494 481 495 496 482 # Take the point closest to pmin as starting point 497 483 # Note: Could be arbitrary, but nice to have 498 484 # a unique way of selecting 499 dist_A = sqrt(sum((A-pmin)**2))500 dist_B = sqrt(sum((B-pmin)**2))485 dist_A = numpy.sqrt(numpy.sum((A-pmin)**2)) 486 dist_B = numpy.sqrt(numpy.sum((B-pmin)**2)) 501 487 502 488 # Find lower leftmost point … … 599 585 # We have reached a point already visited. 600 586 601 if allclose(p1, polygon[0]):587 if numpy.allclose(p1, polygon[0]): 602 588 # If it is the initial point, the polygon is complete. 603 589 … … 635 621 from anuga.utilities.numerical_tools import anglediff 636 622 637 # from numpy.oldnumeric import sort, allclose638 from numpy import sort, allclose639 640 623 N = len(self) 641 624 … … 679 662 680 663 # Normalise 681 l_u = sqrt(u[0]*u[0] + u[1]*u[1])682 l_v = sqrt(v[0]*v[0] + v[1]*v[1])664 l_u = numpy.sqrt(u[0]*u[0] + u[1]*u[1]) 665 l_v = numpy.sqrt(v[0]*v[0] + v[1]*v[1]) 683 666 684 667 msg = 'Normal vector in triangle %d does not have unit length' %i 685 assert allclose(l_v, 1), msg668 assert numpy.allclose(l_v, 1), msg 686 669 687 670 x = (u[0]*v[0] + u[1]*v[1])/l_u # Inner product … … 737 720 738 721 V = self.vertex_value_indices[:] #Take a copy 739 V = sort(V)740 assert allclose(V, range(3*N))722 V = numpy.sort(V) 723 assert numpy.allclose(V, range(3*N)) 741 724 742 725 assert sum(self.number_of_triangles_per_node) ==\ … … 749 732 count[i] += 1 750 733 751 assert allclose(count, self.number_of_triangles_per_node)734 assert numpy.allclose(count, self.number_of_triangles_per_node) 752 735 753 736 … … 812 795 """ 813 796 814 # from numpy.oldnumeric import arange815 from numpy import arange816 797 from anuga.utilities.numerical_tools import histogram, create_bins 817 798 … … 1149 1130 1150 1131 # Distances from line origin to the two intersections 1151 z0 = array([x0 - xi0, y0 - eta0])1152 z1 = array([x1 - xi0, y1 - eta0])1153 d0 = sqrt(sum(z0**2))1154 d1 = sqrt(sum(z1**2))1132 z0 = numpy.array([x0 - xi0, y0 - eta0]) 1133 z1 = numpy.array([x1 - xi0, y1 - eta0]) 1134 d0 = numpy.sqrt(numpy.sum(z0**2)) 1135 d1 = numpy.sqrt(numpy.sum(z1**2)) 1155 1136 1156 1137 if d1 < d0: … … 1165 1146 # Normal direction: 1166 1147 # Right hand side relative to line direction 1167 vector = array([x1 - x0, y1 - y0]) # Segment vector1168 length = sqrt(sum(vector**2)) # Segment length1169 normal = array([vector[1], -vector[0]])/length1148 vector = numpy.array([x1 - x0, y1 - y0]) # Segment vector 1149 length = numpy.sqrt(numpy.sum(vector**2)) # Segment length 1150 normal = numpy.array([vector[1], -vector[0]])/length 1170 1151 1171 1152 … … 1247 1228 assert isinstance(segment, Triangle_intersection), msg 1248 1229 1249 midpoint = sum(array(segment.segment))/21230 midpoint = numpy.sum(numpy.array(segment.segment))/2 1250 1231 midpoints.append(midpoint) 1251 1232 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/pmesh2domain.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Class pmesh2domain - Converting .tsh files to doamains 4 2 … … 11 9 import sys 12 10 11 import numpy 13 12 14 13 … … 163 162 """ 164 163 165 # from numpy.oldnumeric import transpose166 from numpy import transpose167 164 from load_mesh.loadASCII import import_mesh_file 168 165 … … 175 172 volumes = mesh_dict['triangles'] 176 173 vertex_quantity_dict = {} 177 point_atts = transpose(mesh_dict['vertex_attributes'])174 point_atts = numpy.transpose(mesh_dict['vertex_attributes']) 178 175 point_titles = mesh_dict['vertex_attribute_titles'] 179 176 geo_reference = mesh_dict['geo_reference'] 180 177 if point_atts != None: 181 print 'type(point_atts)=%s' % type(point_atts)182 178 for quantity, value_vector in map(None, point_titles, point_atts): 183 179 vertex_quantity_dict[quantity] = value_vector -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/quantity.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Class Quantity - Implements values at each triangular element 4 2 … … 17 15 """ 18 16 19 #from numpy.oldnumeric import array, zeros, Float, less, concatenate, NewAxis,\ 20 # argmax, argmin, allclose, take, reshape, alltrue, Int 21 from numpy import array, zeros, float, less, concatenate, \ 22 argmax, argmin, allclose, take, reshape, alltrue, int 17 import numpy 23 18 24 19 from anuga.utilities.numerical_tools import ensure_numeric, is_scalar … … 42 37 if vertex_values is None: 43 38 N = len(domain) # number_of_elements 44 self.vertex_values = zeros((N, 3),float)39 self.vertex_values = numpy.zeros((N, 3), numpy.float) 45 40 else: 46 self.vertex_values = array(vertex_values).astype(float)41 self.vertex_values = numpy.array(vertex_values).astype(numpy.float) 47 42 48 43 N, V = self.vertex_values.shape … … 61 56 62 57 # Allocate space for other quantities 63 self.centroid_values = zeros(N,float)64 self.edge_values = zeros((N, 3),float)58 self.centroid_values = numpy.zeros(N, numpy.float) 59 self.edge_values = numpy.zeros((N, 3), numpy.float) 65 60 66 61 # Allocate space for Gradient 67 self.x_gradient = zeros(N,float)68 self.y_gradient = zeros(N,float)62 self.x_gradient = numpy.zeros(N, numpy.float) 63 self.y_gradient = numpy.zeros(N, numpy.float) 69 64 70 65 # Allocate space for Limiter Phi 71 self.phi = zeros(N,float)66 self.phi = numpy.zeros(N, numpy.float) 72 67 73 68 # Intialise centroid and edge_values … … 76 71 # Allocate space for boundary values 77 72 L = len(domain.boundary) 78 self.boundary_values = zeros(L,float)73 self.boundary_values = numpy.zeros(L, numpy.float) 79 74 80 75 # Allocate space for updates of conserved quantities by … … 82 77 83 78 # Allocate space for update fields 84 self.explicit_update = zeros(N,float )85 self.semi_implicit_update = zeros(N,float )86 self.centroid_backup_values = zeros(N,float)79 self.explicit_update = numpy.zeros(N, numpy.float ) 80 self.semi_implicit_update = numpy.zeros(N, numpy.float ) 81 self.centroid_backup_values = numpy.zeros(N, numpy.float) 87 82 88 83 self.set_beta(1.0) … … 386 381 from anuga.geospatial_data.geospatial_data import Geospatial_data 387 382 from types import FloatType, IntType, LongType, ListType, NoneType 388 ##NumPy from numpy.oldnumeric import ArrayType389 from numpy import ndarray, float390 383 391 384 # Treat special case: Polygon situation … … 453 446 454 447 msg = 'Indices must be a list or None' 455 ##NumPy assert type(indices) in [ListType, NoneType, ArrayType], msg 456 assert type(indices) in [ListType, NoneType, ndarray], msg 448 assert type(indices) in [ListType, NoneType, numpy.ndarray], msg 457 449 458 450 … … 464 456 self.set_values_from_constant(numeric, 465 457 location, indices, verbose) 466 ##NumPy elif type(numeric) in [ArrayType, ListType]: 467 elif type(numeric) in [ndarray, ListType]: 458 elif type(numeric) in [numpy.ndarray, ListType]: 468 459 self.set_values_from_array(numeric, 469 460 location, indices, verbose) … … 617 608 """ 618 609 619 # from numpy.oldnumeric import array, Float, Int, allclose 620 from numpy import array, float, int, allclose 621 622 values = array(values).astype(float) 610 values = numpy.array(values).astype(numpy.float) 623 611 624 612 if indices is not None: 625 indices = array(indices).astype(int)613 indices = numpy.array(indices).astype(numpy.int) 626 614 msg = 'Number of values must match number of indices:' 627 615 msg += ' You specified %d values and %d indices'\ … … 648 636 649 637 elif location == 'unique vertices': 650 assert len(values.shape) == 1 or allclose(values.shape[1:], 1),\638 assert len(values.shape) == 1 or numpy.allclose(values.shape[1:], 1),\ 651 639 'Values array must be 1d' 652 640 … … 684 672 A = q.vertex_values 685 673 686 # from numpy.oldnumeric import allclose687 from numpy import allclose688 674 msg = 'Quantities are defined on different meshes. '+\ 689 675 'This might be a case for implementing interpolation '+\ 690 676 'between different meshes.' 691 assert allclose(A.shape, self.vertex_values.shape), msg677 assert numpy.allclose(A.shape, self.vertex_values.shape), msg 692 678 693 679 self.set_values(A, location='vertices', … … 725 711 indices = range(len(self)) 726 712 727 V = take(self.domain.get_centroid_coordinates(), indices) 728 print 'V=%s' % str(V) 713 V = numpy.take(self.domain.get_centroid_coordinates(), indices, axis=0) 729 714 self.set_values(f(V[:,0], V[:,1]), 730 715 location=location, … … 790 775 791 776 792 points = ensure_numeric(points, float)793 values = ensure_numeric(values, float)777 points = ensure_numeric(points, numpy.float) 778 values = ensure_numeric(values, numpy.float) 794 779 795 780 if location != 'vertices': … … 923 908 # Always return absolute indices 924 909 if mode is None or mode == 'max': 925 i = argmax(V)910 i = numpy.argmax(V) 926 911 elif mode == 'min': 927 i = argmin(V)912 i = numpy.argmin(V) 928 913 929 914 … … 1128 1113 """ 1129 1114 1130 # from numpy.oldnumeric import take1131 from numpy import take1132 1133 1115 # FIXME (Ole): I reckon we should have the option of passing a 1134 1116 # polygon into get_values. The question becomes how … … 1154 1136 raise msg 1155 1137 1156 # import types, numpy.oldnumeric as Numeric1157 1138 import types 1158 from numpy import ndarray 1159 1160 assert type(indices) in [types.ListType, types.NoneType, ndarray], \ 1139 1140 assert type(indices) in [types.ListType, types.NoneType, numpy.ndarray], \ 1161 1141 'Indices must be a list or None' 1162 1142 … … 1164 1144 if (indices == None): 1165 1145 indices = range(len(self)) 1166 return take(self.centroid_values,indices)1146 return numpy.take(self.centroid_values, indices, axis=0) 1167 1147 elif location == 'edges': 1168 1148 if (indices == None): 1169 1149 indices = range(len(self)) 1170 return take(self.edge_values,indices)1150 return numpy.take(self.edge_values, indices, axis=0) 1171 1151 elif location == 'unique vertices': 1172 1152 if (indices == None): … … 1191 1171 sum += self.vertex_values[triangle_id, vertex_id] 1192 1172 vert_values.append(sum/len(triangles)) 1193 return Numeric.array(vert_values)1173 return numpy.array(vert_values) 1194 1174 else: 1195 1175 if (indices is None): 1196 1176 indices = range(len(self)) 1197 return take(self.vertex_values, indices)1177 return numpy.take(self.vertex_values, indices, axis=0) 1198 1178 1199 1179 … … 1209 1189 """ 1210 1190 1211 # from numpy.oldnumeric import array, Float1212 from numpy import array, float1213 1214 1191 # Assert that A can be converted to a Numeric array of appropriate dim 1215 A = ensure_numeric(A, float)1192 A = ensure_numeric(A, numpy.float) 1216 1193 1217 1194 # print 'SHAPE A', A.shape … … 1287 1264 """ 1288 1265 1289 # from numpy.oldnumeric import concatenate, zeros, Float, Int, array, reshape1290 from numpy import concatenate, zeros, float, int, array, reshape1291 1292 1293 1266 if smooth is None: 1294 1267 # Take default from domain … … 1299 1272 1300 1273 if precision is None: 1301 precision = float1274 precision = numpy.float 1302 1275 1303 1276 … … 1308 1281 V = self.domain.get_triangles() 1309 1282 N = self.domain.number_of_full_nodes # Ignore ghost nodes if any 1310 A = zeros(N,float)1283 A = numpy.zeros(N, numpy.float) 1311 1284 points = self.domain.get_nodes() 1312 1285 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/region.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """region.py - Classes for implementing region conditions 4 2 … … 10 8 # FIXME (DSG-DSG) add better comments 11 9 12 from numpy.oldnumeric import average 10 import numpy 11 12 13 13 class Region: 14 14 """Base class for modifying quantities based on a region. … … 103 103 values = Q.get_values(indices=self.build_indices(elements, domain), 104 104 location=self.location) 105 av = average(values)105 av = numpy.average(values) 106 106 if self.location == "vertices": 107 av = average(av)107 av = numpy.average(av) 108 108 new_values = av + self.X 109 109 else: -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/show_balanced_limiters.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """Example of shallow water wave equation. 4 2 … … 20 18 21 19 from mesh_factory import rectangular 22 from numpy.oldnumeric import array 23 20 24 21 25 22 ###################### … … 80 77 domain.set_quantity('stage', Z) 81 78 82 from numpy.oldnumeric import allclose83 84 79 #Evolve 85 80 for t in domain.evolve(yieldstep = 0.1, finaltime = 30): -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_domain.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 8 6 from domain import * 9 7 from anuga.config import epsilon 10 ##from numpy.oldnumeric import allclose, array, ones, Float, alltrue 11 from numpy import allclose, array, ones, float, alltrue 8 import numpy 12 9 13 10 … … 67 64 68 65 69 assert alltrue(domain.get_conserved_quantities(0, edge=1) == 0.)66 assert numpy.alltrue(domain.get_conserved_quantities(0, edge=1) == 0.) 70 67 71 68 … … 98 95 #Centroids 99 96 q = domain.get_conserved_quantities(0) 100 assert allclose(q, [2., 2., 0.])97 assert numpy.allclose(q, [2., 2., 0.]) 101 98 102 99 q = domain.get_conserved_quantities(1) 103 assert allclose(q, [5., 5., 0.])100 assert numpy.allclose(q, [5., 5., 0.]) 104 101 105 102 q = domain.get_conserved_quantities(2) 106 assert allclose(q, [3., 3., 0.])103 assert numpy.allclose(q, [3., 3., 0.]) 107 104 108 105 q = domain.get_conserved_quantities(3) 109 assert allclose(q, [0., 0., 0.])106 assert numpy.allclose(q, [0., 0., 0.]) 110 107 111 108 112 109 #Edges 113 110 q = domain.get_conserved_quantities(0, edge=0) 114 assert allclose(q, [2.5, 2.5, 0.])111 assert numpy.allclose(q, [2.5, 2.5, 0.]) 115 112 q = domain.get_conserved_quantities(0, edge=1) 116 assert allclose(q, [2., 2., 0.])113 assert numpy.allclose(q, [2., 2., 0.]) 117 114 q = domain.get_conserved_quantities(0, edge=2) 118 assert allclose(q, [1.5, 1.5, 0.])115 assert numpy.allclose(q, [1.5, 1.5, 0.]) 119 116 120 117 for i in range(3): 121 118 q = domain.get_conserved_quantities(1, edge=i) 122 assert allclose(q, [5, 5, 0.])119 assert numpy.allclose(q, [5, 5, 0.]) 123 120 124 121 125 122 q = domain.get_conserved_quantities(2, edge=0) 126 assert allclose(q, [4.5, 4.5, 0.])123 assert numpy.allclose(q, [4.5, 4.5, 0.]) 127 124 q = domain.get_conserved_quantities(2, edge=1) 128 assert allclose(q, [4.5, 4.5, 0.])125 assert numpy.allclose(q, [4.5, 4.5, 0.]) 129 126 q = domain.get_conserved_quantities(2, edge=2) 130 assert allclose(q, [0., 0., 0.])127 assert numpy.allclose(q, [0., 0., 0.]) 131 128 132 129 133 130 q = domain.get_conserved_quantities(3, edge=0) 134 assert allclose(q, [3., 3., 0.])131 assert numpy.allclose(q, [3., 3., 0.]) 135 132 q = domain.get_conserved_quantities(3, edge=1) 136 assert allclose(q, [-1.5, -1.5, 0.])133 assert numpy.allclose(q, [-1.5, -1.5, 0.]) 137 134 q = domain.get_conserved_quantities(3, edge=2) 138 assert allclose(q, [-1.5, -1.5, 0.])135 assert numpy.allclose(q, [-1.5, -1.5, 0.]) 139 136 140 137 … … 182 179 Q = domain.create_quantity_from_expression(expression) 183 180 184 assert allclose(Q.vertex_values, [[2,3,4], [6,6,6],185 [1,1,10], [-5, 4, 4]])181 assert numpy.allclose(Q.vertex_values, [[2,3,4], [6,6,6], 182 [1,1,10], [-5, 4, 4]]) 186 183 187 184 expression = '(xmomentum*xmomentum + ymomentum*ymomentum)**0.5' … … 191 188 Y = domain.quantities['ymomentum'].vertex_values 192 189 193 assert allclose(Q.vertex_values, (X**2 + Y**2)**0.5)190 assert numpy.allclose(Q.vertex_values, (X**2 + Y**2)**0.5) 194 191 195 192 … … 317 314 Q = domain.quantities['depth'] 318 315 319 assert allclose(Q.vertex_values, [[2,3,4], [6,6,6],320 [1,1,10], [-5, 4, 4]])316 assert numpy.allclose(Q.vertex_values, [[2,3,4], [6,6,6], 317 [1,1,10], [-5, 4, 4]]) 321 318 322 319 … … 385 382 domain.check_integrity() 386 383 387 assert allclose(domain.neighbours, [[-1,-2,-3]])384 assert numpy.allclose(domain.neighbours, [[-1,-2,-3]]) 388 385 389 386 … … 474 471 [0,0,9], [-6, 3, 3]]) 475 472 476 assert allclose( domain.quantities['stage'].centroid_values,477 [2,5,3,0] )473 assert numpy.allclose( domain.quantities['stage'].centroid_values, 474 [2,5,3,0] ) 478 475 479 476 domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], … … 487 484 488 485 #First order extrapolation 489 assert allclose( domain.quantities['stage'].vertex_values,490 [[ 2., 2., 2.],491 [ 5., 5., 5.],492 [ 3., 3., 3.],493 [ 0., 0., 0.]])486 assert numpy.allclose( domain.quantities['stage'].vertex_values, 487 [[ 2., 2., 2.], 488 [ 5., 5., 5.], 489 [ 3., 3., 3.], 490 [ 0., 0., 0.]]) 494 491 495 492 … … 530 527 531 528 for name in domain.conserved_quantities: 532 domain.quantities[name].explicit_update = array([4.,3.,2.,1.])533 domain.quantities[name].semi_implicit_update = array([1.,1.,1.,1.])529 domain.quantities[name].explicit_update = numpy.array([4.,3.,2.,1.]) 530 domain.quantities[name].semi_implicit_update = numpy.array([1.,1.,1.,1.]) 534 531 535 532 … … 538 535 domain.update_conserved_quantities() 539 536 540 sem = array([1.,1.,1.,1.])/array([1, 2, 3, 4])541 denom = ones(4,float)-domain.timestep*sem537 sem = numpy.array([1.,1.,1.,1.])/numpy.array([1, 2, 3, 4]) 538 denom = numpy.ones(4, numpy.float)-domain.timestep*sem 542 539 543 540 # x = array([1, 2, 3, 4]) + array( [.4,.3,.2,.1] ) 544 541 # x /= denom 545 542 546 x = array([1., 2., 3., 4.])543 x = numpy.array([1., 2., 3., 4.]) 547 544 x /= denom 548 x += domain.timestep* array( [4,3,2,1] )545 x += domain.timestep*numpy.array( [4,3,2,1] ) 549 546 550 547 for name in domain.conserved_quantities: 551 assert allclose(domain.quantities[name].centroid_values, x)548 assert numpy.allclose(domain.quantities[name].centroid_values, x) 552 549 553 550 … … 581 578 [0,0,9], [-6, 3, 3]]) 582 579 583 assert allclose( domain.quantities['stage'].centroid_values,584 [2,5,3,0] )580 assert numpy.allclose( domain.quantities['stage'].centroid_values, 581 [2,5,3,0] ) 585 582 586 583 domain.set_quantity('xmomentum', [[1,1,1], [2,2,2], … … 594 591 595 592 #First order extrapolation 596 assert allclose( domain.quantities['stage'].vertex_values,597 [[ 2., 2., 2.],598 [ 5., 5., 5.],599 [ 3., 3., 3.],600 [ 0., 0., 0.]])593 assert numpy.allclose( domain.quantities['stage'].vertex_values, 594 [[ 2., 2., 2.], 595 [ 5., 5., 5.], 596 [ 3., 3., 3.], 597 [ 0., 0., 0.]]) 601 598 602 599 domain.build_tagged_elements_dictionary({'mound':[0,1]}) … … 613 610 from mesh_factory import rectangular 614 611 from shallow_water import Domain 615 ## from numpy.oldnumeric import zeros, Float616 from numpy import zeros, float617 612 618 613 #Create basic mesh … … 631 626 from mesh_factory import rectangular 632 627 from shallow_water import Domain 633 ## from numpy.oldnumeric import zeros, Float634 from numpy import zeros, float635 628 636 629 #Create basic mesh … … 650 643 domain.set_region([set_bottom_friction, set_top_friction]) 651 644 #print domain.quantities['friction'].get_values() 652 assert allclose(domain.quantities['friction'].get_values(),\653 [[ 0.09, 0.09, 0.09],654 [ 0.09, 0.09, 0.09],655 [ 0.07, 0.07, 0.07],656 [ 0.07, 0.07, 0.07],657 [ 1.0, 1.0, 1.0],658 [ 1.0, 1.0, 1.0]])645 assert numpy.allclose(domain.quantities['friction'].get_values(),\ 646 [[ 0.09, 0.09, 0.09], 647 [ 0.09, 0.09, 0.09], 648 [ 0.07, 0.07, 0.07], 649 [ 0.07, 0.07, 0.07], 650 [ 1.0, 1.0, 1.0], 651 [ 1.0, 1.0, 1.0]]) 659 652 660 653 domain.set_region([set_all_friction]) 661 654 #print domain.quantities['friction'].get_values() 662 assert allclose(domain.quantities['friction'].get_values(),663 [[ 10.09, 10.09, 10.09],664 [ 10.09, 10.09, 10.09],665 [ 10.07, 10.07, 10.07],666 [ 10.07, 10.07, 10.07],667 [ 11.0, 11.0, 11.0],668 [ 11.0, 11.0, 11.0]])655 assert numpy.allclose(domain.quantities['friction'].get_values(), 656 [[ 10.09, 10.09, 10.09], 657 [ 10.09, 10.09, 10.09], 658 [ 10.07, 10.07, 10.07], 659 [ 10.07, 10.07, 10.07], 660 [ 11.0, 11.0, 11.0], 661 [ 11.0, 11.0, 11.0]]) 669 662 670 663 … … 675 668 from mesh_factory import rectangular 676 669 from shallow_water import Domain 677 ## from numpy.oldnumeric import zeros, Float678 from numpy import zeros, float679 670 680 671 #Create basic mesh … … 696 687 697 688 #print domain.quantities['friction'].get_values() 698 assert allclose(domain.quantities['friction'].get_values(),\699 [[ 0.09, 0.09, 0.09],700 [ 0.09, 0.09, 0.09],701 [ 0.07, 0.07, 0.07],702 [ 0.07, 0.07, 0.07],703 [ 1.0, 1.0, 1.0],704 [ 1.0, 1.0, 1.0]])689 assert numpy.allclose(domain.quantities['friction'].get_values(), 690 [[ 0.09, 0.09, 0.09], 691 [ 0.09, 0.09, 0.09], 692 [ 0.07, 0.07, 0.07], 693 [ 0.07, 0.07, 0.07], 694 [ 1.0, 1.0, 1.0], 695 [ 1.0, 1.0, 1.0]]) 705 696 706 697 domain.set_region([set_bottom_friction, set_top_friction]) 707 698 #print domain.quantities['friction'].get_values() 708 assert allclose(domain.quantities['friction'].get_values(),\709 [[ 0.09, 0.09, 0.09],710 [ 0.09, 0.09, 0.09],711 [ 0.07, 0.07, 0.07],712 [ 0.07, 0.07, 0.07],713 [ 1.0, 1.0, 1.0],714 [ 1.0, 1.0, 1.0]])699 assert numpy.allclose(domain.quantities['friction'].get_values(), 700 [[ 0.09, 0.09, 0.09], 701 [ 0.09, 0.09, 0.09], 702 [ 0.07, 0.07, 0.07], 703 [ 0.07, 0.07, 0.07], 704 [ 1.0, 1.0, 1.0], 705 [ 1.0, 1.0, 1.0]]) 715 706 716 707 domain.set_region([set_all_friction]) 717 708 #print domain.quantities['friction'].get_values() 718 assert allclose(domain.quantities['friction'].get_values(),719 [[ 10.09, 10.09, 10.09],720 [ 10.09, 10.09, 10.09],721 [ 10.07, 10.07, 10.07],722 [ 10.07, 10.07, 10.07],723 [ 11.0, 11.0, 11.0],724 [ 11.0, 11.0, 11.0]])709 assert numpy.allclose(domain.quantities['friction'].get_values(), 710 [[ 10.09, 10.09, 10.09], 711 [ 10.09, 10.09, 10.09], 712 [ 10.07, 10.07, 10.07], 713 [ 10.07, 10.07, 10.07], 714 [ 11.0, 11.0, 11.0], 715 [ 11.0, 11.0, 11.0]]) 725 716 726 717 #------------------------------------------------------------- -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_ermapper.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 7 5 8 6 import ermapper_grids 9 ##import numpy.oldnumeric as Numeric10 7 import numpy 11 8 from os import remove -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_general_mesh.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 9 7 10 8 from anuga.config import epsilon 11 ##from numpy.oldnumeric import allclose, array, ones, Float, alltrue 12 from numpy import allclose, array, ones, float, alltrue 9 import numpy 13 10 from general_mesh import General_mesh 14 11 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 26 23 def test_get_vertex_coordinates(self): 27 24 from mesh_factory import rectangular 28 ## from numpy.oldnumeric import zeros, Float29 from numpy import zeros, float30 25 31 26 #Create basic mesh … … 34 29 35 30 36 assert allclose(domain.get_nodes(), nodes)31 assert numpy.allclose(domain.get_nodes(), nodes) 37 32 38 33 … … 45 40 for j in range(3): 46 41 k = triangles[i,j] #Index of vertex j in triangle i 47 assert allclose(V[3*i+j,:], nodes[k])42 assert numpy.allclose(V[3*i+j,:], nodes[k]) 48 43 49 44 def test_get_vertex_coordinates_with_geo_ref(self): … … 59 54 f = [4.0, 0.0] 60 55 61 nodes = array([a, b, c, d, e, f])56 nodes = numpy.array([a, b, c, d, e, f]) 62 57 63 58 nodes_absolute = geo.get_absolute(nodes) 64 59 65 60 #bac, bce, ecf, dbe, daf, dae 66 triangles = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]])61 triangles = numpy.array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 67 62 68 63 domain = General_mesh(nodes, triangles, 69 64 geo_reference = geo) 70 65 verts = domain.get_vertex_coordinates(triangle_id=0) 71 self.assert_( allclose(array([b,a,c]), verts))66 self.assert_(numpy.allclose(numpy.array([b,a,c]), verts)) 72 67 verts = domain.get_vertex_coordinates(triangle_id=0) 73 self.assert_( allclose(array([b,a,c]), verts))68 self.assert_(numpy.allclose(numpy.array([b,a,c]), verts)) 74 69 verts = domain.get_vertex_coordinates(triangle_id=0, 75 70 absolute=True) 76 self.assert_( allclose(array([nodes_absolute[1],77 nodes_absolute[0],78 nodes_absolute[2]]), verts))71 self.assert_(numpy.allclose(numpy.array([nodes_absolute[1], 72 nodes_absolute[0], 73 nodes_absolute[2]]), verts)) 79 74 verts = domain.get_vertex_coordinates(triangle_id=0, 80 75 absolute=True) 81 self.assert_( allclose(array([nodes_absolute[1],82 nodes_absolute[0],83 nodes_absolute[2]]), verts))76 self.assert_(numpy.allclose(numpy.array([nodes_absolute[1], 77 nodes_absolute[0], 78 nodes_absolute[2]]), verts)) 84 79 85 80 … … 90 85 """ 91 86 from mesh_factory import rectangular 92 ## from numpy.oldnumeric import zeros, Float93 from numpy import zeros, float94 87 95 88 #Create basic mesh … … 98 91 99 92 100 assert allclose(domain.get_nodes(), nodes)93 assert numpy.allclose(domain.get_nodes(), nodes) 101 94 102 95 … … 109 102 for j in range(3): 110 103 k = triangles[i,j] #Index of vertex j in triangle i 111 assert allclose(V[j,:], nodes[k])104 assert numpy.allclose(V[j,:], nodes[k]) 112 105 113 106 … … 119 112 """ 120 113 from mesh_factory import rectangular 121 ## from numpy.oldnumeric import zeros, Float122 from numpy import zeros, float123 114 124 115 #Create basic mesh … … 127 118 128 119 value = [7] 129 assert allclose(domain.get_triangles(), triangles)130 assert allclose(domain.get_triangles([0,4]),131 [triangles[0], triangles[4]])120 assert numpy.allclose(domain.get_triangles(), triangles) 121 assert numpy.allclose(domain.get_triangles([0,4]), 122 [triangles[0], triangles[4]]) 132 123 133 124 … … 136 127 """ 137 128 from mesh_factory import rectangular 138 ## from numpy.oldnumeric import zeros, Float, array 139 from numpy import zeros, float, array 140 141 a = [0.0, 0.0] 142 b = [0.0, 2.0] 143 c = [2.0, 0.0] 144 d = [0.0, 4.0] 145 e = [2.0, 2.0] 146 f = [4.0, 0.0] 147 148 nodes = array([a, b, c, d, e, f]) 129 130 a = [0.0, 0.0] 131 b = [0.0, 2.0] 132 c = [2.0, 0.0] 133 d = [0.0, 4.0] 134 e = [2.0, 2.0] 135 f = [4.0, 0.0] 136 137 nodes = numpy.array([a, b, c, d, e, f]) 149 138 #bac, bce, ecf, dbe, daf, dae 150 triangles = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]])139 triangles = numpy.array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 151 140 152 141 domain1 = General_mesh(nodes, triangles) … … 169 158 #print count 170 159 # 171 assert allclose(count, domain.number_of_triangles_per_node)160 assert numpy.allclose(count, domain.number_of_triangles_per_node) 172 161 173 162 # Check indices … … 203 192 f = [4.0, 0.0] 204 193 205 nodes = array([a, b, c, d, e, f])194 nodes = numpy.array([a, b, c, d, e, f]) 206 195 #bac, bce, ecf, dbe, daf, dae 207 triangles = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]])196 triangles = numpy.array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 208 197 209 198 domain = General_mesh(nodes, triangles) … … 211 200 # One node 212 201 L = domain.get_triangles_and_vertices_per_node(node=2) 213 print 'L=%s' % str(L) 214 assert allclose(L[0], [0, 2]) 215 assert allclose(L[1], [1, 1]) 216 assert allclose(L[2], [2, 1]) 202 assert numpy.allclose(L[0], [0, 2]) 203 assert numpy.allclose(L[1], [1, 1]) 204 assert numpy.allclose(L[2], [2, 1]) 217 205 218 206 # All nodes … … 221 209 for i, Lref in enumerate(ALL): 222 210 L = domain.get_triangles_and_vertices_per_node(node=i) 223 assert allclose(L, Lref)211 assert numpy.allclose(L, Lref) 224 212 225 213 … … 232 220 from mesh_factory import rectangular 233 221 from shallow_water import Domain 234 ## from numpy.oldnumeric import zeros, Float235 from numpy import zeros, float236 222 237 223 #Create basic mesh … … 248 234 from mesh_factory import rectangular 249 235 from shallow_water import Domain 250 ## from numpy.oldnumeric import zeros, Float251 from numpy import zeros, float252 236 253 237 #Create basic mesh … … 283 267 f = [4.0, 0.0] 284 268 285 nodes = array([a, b, c, d, e, f])269 nodes = numpy.array([a, b, c, d, e, f]) 286 270 287 271 nodes_absolute = geo.get_absolute(nodes) 288 272 289 273 #bac, bce, ecf, dbe, daf, dae 290 triangles = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]])274 triangles = numpy.array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 291 275 292 276 domain = General_mesh(nodes, triangles, 293 277 geo_reference = geo) 294 278 node = domain.get_node(2) 295 self.assertTrue( alltrue(c == node))279 self.assertTrue(numpy.alltrue(c == node)) 296 280 297 281 node = domain.get_node(2, absolute=True) 298 self.assertTrue( alltrue(nodes_absolute[2] == node))282 self.assertTrue(numpy.alltrue(nodes_absolute[2] == node)) 299 283 300 284 node = domain.get_node(2, absolute=True) 301 self.assertTrue( alltrue(nodes_absolute[2] == node))285 self.assertTrue(numpy.alltrue(nodes_absolute[2] == node)) 302 286 303 287 … … 320 304 f = [4.0, 0.0] 321 305 322 nodes = array([a, b, c, d, e, f])306 nodes = numpy.array([a, b, c, d, e, f]) 323 307 324 308 nodes_absolute = geo.get_absolute(nodes) 325 309 326 310 # max index is 5, use 5, expect success 327 triangles = array([[1,5,2], [1,2,4], [4,2,5], [3,1,4]])311 triangles = numpy.array([[1,5,2], [1,2,4], [4,2,5], [3,1,4]]) 328 312 General_mesh(nodes, triangles, geo_reference=geo) 329 313 330 314 # max index is 5, use 6, expect assert failure 331 triangles = array([[1,6,2], [1,2,4], [4,2,5], [3,1,4]])315 triangles = numpy.array([[1,6,2], [1,2,4], [4,2,5], [3,1,4]]) 332 316 self.failUnlessRaises(AssertionError, General_mesh, 333 317 nodes, triangles, geo_reference=geo) 334 318 335 319 # max index is 5, use 10, expect assert failure 336 triangles = array([[1,10,2], [1,2,4], [4,2,5], [3,1,4]])320 triangles = numpy.array([[1,10,2], [1,2,4], [4,2,5], [3,1,4]]) 337 321 self.failUnlessRaises(AssertionError, General_mesh, 338 322 nodes, triangles, geo_reference=geo) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_generic_boundary_conditions.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 8 6 from generic_boundary_conditions import * 9 7 from anuga.config import epsilon 10 #from numpy.oldnumeric import allclose, array 11 from numpy import allclose, array 8 import numpy 12 9 13 10 … … 47 44 48 45 q = Bd.evaluate() 49 assert allclose(q, x)46 assert numpy.allclose(q, x) 50 47 51 48 … … 101 98 q = T.evaluate(0, 2) #Vol=0, edge=2 102 99 103 assert allclose(q, [1.5, 2.5])100 assert numpy.allclose(q, [1.5, 2.5]) 104 101 105 102 … … 178 175 179 176 #Check that midpoint coordinates at boundary are correctly computed 180 assert allclose( F.midpoint_coordinates,181 [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0],182 [3.0, 1.0], [1.0, 3.0], [0.0, 3.0]])183 184 #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0])185 #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0])186 #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0])187 #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0])188 #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0])189 #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0])177 assert numpy.allclose( F.midpoint_coordinates, 178 [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0], 179 [3.0, 1.0], [1.0, 3.0], [0.0, 3.0]]) 180 181 #assert numpy.allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0]) 182 #assert numpy.allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0]) 183 #assert numpy.allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0]) 184 #assert numpy.allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0]) 185 #assert numpy.allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0]) 186 #assert numpy.allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0]) 190 187 191 188 … … 196 193 domain.time = 5*30/2 #A quarter way through first step 197 194 q = F.evaluate() 198 assert allclose(q, [1.0/4, sin(2*pi/10)/4])195 assert numpy.allclose(q, [1.0/4, sin(2*pi/10)/4]) 199 196 200 197 201 198 domain.time = 2.5*5*60 #Half way between steps 2 and 3 202 199 q = F.evaluate() 203 assert allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2])200 assert numpy.allclose(q, [2.5, (sin(2*2*pi/10) + sin(3*2*pi/10))/2]) 204 201 205 202 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_ghost.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 8 6 from domain import * 9 7 from anuga.config import epsilon 10 from numpy.oldnumeric import allclose, array, ones, Float, alltrue 11 12 8 import numpy 13 9 14 10 … … 45 41 46 42 47 assert alltrue(domain.get_conserved_quantities(0, edge=1) == 0.)43 assert numpy.alltrue(domain.get_conserved_quantities(0, edge=1) == 0.) 48 44 49 45 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_neighbour_mesh.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 5 6 2 7 3 #FIXME: Seperate the tests for mesh and general_mesh … … 15 11 from mesh_factory import rectangular 16 12 from anuga.config import epsilon 17 #from numpy.oldnumeric import allclose, array, Int 18 from numpy import allclose, array, int 13 import numpy 19 14 20 15 from anuga.coordinate_transforms.geo_reference import Geo_reference … … 23 18 24 19 def distance(x, y): 25 return sqrt( sum( ( array(x)-array(y))**2 ))20 return sqrt( sum( (numpy.array(x)-numpy.array(y))**2 )) 26 21 27 22 class Test_Mesh(unittest.TestCase): … … 66 61 #Normals 67 62 normals = mesh.get_normals() 68 assert allclose(normals[0, 0:2], [3.0/5, 4.0/5])69 assert allclose(normals[0, 2:4], [-1.0, 0.0])70 assert allclose(normals[0, 4:6], [0.0, -1.0])71 72 assert allclose(mesh.get_normal(0,0), [3.0/5, 4.0/5])73 assert allclose(mesh.get_normal(0,1), [-1.0, 0.0])74 assert allclose(mesh.get_normal(0,2), [0.0, -1.0])63 assert numpy.allclose(normals[0, 0:2], [3.0/5, 4.0/5]) 64 assert numpy.allclose(normals[0, 2:4], [-1.0, 0.0]) 65 assert numpy.allclose(normals[0, 4:6], [0.0, -1.0]) 66 67 assert numpy.allclose(mesh.get_normal(0,0), [3.0/5, 4.0/5]) 68 assert numpy.allclose(mesh.get_normal(0,1), [-1.0, 0.0]) 69 assert numpy.allclose(mesh.get_normal(0,2), [0.0, -1.0]) 75 70 76 71 #Edge lengths 77 assert allclose(mesh.edgelengths[0], [5.0, 3.0, 4.0])72 assert numpy.allclose(mesh.edgelengths[0], [5.0, 3.0, 4.0]) 78 73 79 74 … … 84 79 85 80 V = mesh.get_vertex_coordinates() 86 assert allclose(V, [ [0.0, 0.0],87 [4.0, 0.0],88 [0.0, 3.0] ])81 assert numpy.allclose(V, [ [0.0, 0.0], 82 [4.0, 0.0], 83 [0.0, 3.0] ]) 89 84 90 85 V0 = mesh.get_vertex_coordinate(0, 0) 91 assert allclose(V0, [0.0, 0.0])86 assert numpy.allclose(V0, [0.0, 0.0]) 92 87 93 88 V1 = mesh.get_vertex_coordinate(0, 1) 94 assert allclose(V1, [4.0, 0.0])89 assert numpy.allclose(V1, [4.0, 0.0]) 95 90 96 91 V2 = mesh.get_vertex_coordinate(0, 2) 97 assert allclose(V2, [0.0, 3.0])92 assert numpy.allclose(V2, [0.0, 3.0]) 98 93 99 94 … … 240 235 241 236 mesh = Mesh(points, vertices,use_inscribed_circle=False) 242 assert allclose(mesh.radii[0],sqrt(3.0)/3),'Steve''s doesn''t work'237 assert numpy.allclose(mesh.radii[0],sqrt(3.0)/3),'Steve''s doesn''t work' 243 238 244 239 mesh = Mesh(points, vertices,use_inscribed_circle=True) 245 assert allclose(mesh.radii[0],sqrt(3.0)/3),'inscribed circle doesn''t work'240 assert numpy.allclose(mesh.radii[0],sqrt(3.0)/3),'inscribed circle doesn''t work' 246 241 247 242 def test_inscribed_circle_rightangle_triangle(self): … … 255 250 256 251 mesh = Mesh(points, vertices,use_inscribed_circle=False) 257 assert allclose(mesh.radii[0],5.0/6),'Steve''s doesn''t work'252 assert numpy.allclose(mesh.radii[0],5.0/6),'Steve''s doesn''t work' 258 253 259 254 mesh = Mesh(points, vertices,use_inscribed_circle=True) 260 assert allclose(mesh.radii[0],1.0),'inscribed circle doesn''t work'255 assert numpy.allclose(mesh.radii[0],1.0),'inscribed circle doesn''t work' 261 256 262 257 … … 272 267 assert mesh.areas[0] == 2.0 273 268 274 assert allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3])269 assert numpy.allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3]) 275 270 276 271 … … 306 301 assert mesh.edgelengths[1,2] == sqrt(8.0) 307 302 308 assert allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3])309 assert allclose(mesh.centroid_coordinates[1], [4.0/3, 4.0/3])310 assert allclose(mesh.centroid_coordinates[2], [8.0/3, 2.0/3])311 assert allclose(mesh.centroid_coordinates[3], [2.0/3, 8.0/3])303 assert numpy.allclose(mesh.centroid_coordinates[0], [2.0/3, 2.0/3]) 304 assert numpy.allclose(mesh.centroid_coordinates[1], [4.0/3, 4.0/3]) 305 assert numpy.allclose(mesh.centroid_coordinates[2], [8.0/3, 2.0/3]) 306 assert numpy.allclose(mesh.centroid_coordinates[3], [2.0/3, 8.0/3]) 312 307 313 308 def test_mesh_and_neighbours(self): … … 700 695 get values based on triangle lists. 701 696 """ 697 702 698 from mesh_factory import rectangular 703 # from numpy.oldnumeric import zeros, Float704 from numpy import zeros, float705 699 706 700 #Create basic mesh … … 720 714 def test_boundary_polygon(self): 721 715 from mesh_factory import rectangular 722 #from mesh import Mesh723 # from numpy.oldnumeric import zeros, Float724 from numpy import zeros, float725 716 726 717 #Create basic mesh … … 732 723 733 724 assert len(P) == 8 734 assert allclose(P, [[0.0, 0.0], [0.5, 0.0], [1.0, 0.0],735 [1.0, 0.5], [1.0, 1.0], [0.5, 1.0],736 [0.0, 1.0], [0.0, 0.5]])725 assert numpy.allclose(P, [[0.0, 0.0], [0.5, 0.0], [1.0, 0.0], 726 [1.0, 0.5], [1.0, 1.0], [0.5, 1.0], 727 [0.0, 1.0], [0.0, 0.5]]) 737 728 for p in points: 738 729 #print p, P … … 741 732 742 733 def test_boundary_polygon_II(self): 743 # from numpy.oldnumeric import zeros, Float744 from numpy import zeros, float745 746 747 734 #Points 748 735 a = [0.0, 0.0] #0 … … 770 757 771 758 assert len(P) == 8 772 assert allclose(P, [a, d, f, i, h, e, c, b])759 assert numpy.allclose(P, [a, d, f, i, h, e, c, b]) 773 760 774 761 for p in points: … … 780 767 """Same as II but vertices ordered differently 781 768 """ 782 783 # from numpy.oldnumeric import zeros, Float784 from numpy import zeros, float785 786 769 787 770 #Points … … 811 794 812 795 assert len(P) == 8 813 assert allclose(P, [a, d, f, i, h, e, c, b])796 assert numpy.allclose(P, [a, d, f, i, h, e, c, b]) 814 797 815 798 for p in points: … … 822 805 is partitioned using pymetis. 823 806 """ 824 825 # from numpy.oldnumeric import zeros, Float826 from numpy import zeros, float827 828 807 829 808 #Points … … 855 834 856 835 # Note that point e appears twice! 857 assert allclose(P, [a, d, f, e, g, h, e, c, b])836 assert numpy.allclose(P, [a, d, f, e, g, h, e, c, b]) 858 837 859 838 for p in points: … … 872 851 """ 873 852 874 # from numpy.oldnumeric import zeros, Float875 from numpy import zeros, float876 853 from mesh_factory import rectangular 877 854 … … 916 893 917 894 """ 918 # from numpy.oldnumeric import zeros, Float919 from numpy import zeros, float920 921 895 922 896 #Points … … 965 939 966 940 assert len(P) == 8 967 assert allclose(P, [a, d, f, i, h, e, c, b])968 assert allclose(P, [(0.0, 0.0), (0.5, 0.0), (1.0, 0.0), (1.5, 0.5), (1.0, 1.0), (0.5, 0.5), (0.0, 1.0), (0.0, 0.5)])941 assert numpy.allclose(P, [a, d, f, i, h, e, c, b]) 942 assert numpy.allclose(P, [(0.0, 0.0), (0.5, 0.0), (1.0, 0.0), (1.5, 0.5), (1.0, 1.0), (0.5, 0.5), (0.0, 1.0), (0.0, 0.5)]) 969 943 970 944 … … 1152 1126 assert is_inside_polygon(p, P) 1153 1127 1154 assert allclose(P, Pref)1128 assert numpy.allclose(P, Pref) 1155 1129 1156 1130 def test_lone_vertices(self): … … 1204 1178 boundary_polygon = mesh.get_boundary_polygon() 1205 1179 1206 assert allclose(absolute_points, boundary_polygon)1180 assert numpy.allclose(absolute_points, boundary_polygon) 1207 1181 1208 1182 def test_get_triangle_containing_point(self): … … 1251 1225 1252 1226 neighbours = mesh.get_triangle_neighbours(0) 1253 assert allclose(neighbours, [-1,1,-1])1227 assert numpy.allclose(neighbours, [-1,1,-1]) 1254 1228 neighbours = mesh.get_triangle_neighbours(-10) 1255 1229 assert neighbours == [] … … 1300 1274 for x in L: 1301 1275 if x.triangle_id % 2 == 0: 1302 assert allclose(x.length, ceiling-y_line)1276 assert numpy.allclose(x.length, ceiling-y_line) 1303 1277 else: 1304 assert allclose(x.length, y_line-floor)1278 assert numpy.allclose(x.length, y_line-floor) 1305 1279 1306 1280 1307 assert allclose(x.normal, [0,-1])1308 1309 assert allclose(x.segment[1][0], x.segment[0][0] + x.length)1310 assert allclose(x.segment[0][1], y_line)1311 assert allclose(x.segment[1][1], y_line)1281 assert numpy.allclose(x.normal, [0,-1]) 1282 1283 assert numpy.allclose(x.segment[1][0], x.segment[0][0] + x.length) 1284 assert numpy.allclose(x.segment[0][1], y_line) 1285 assert numpy.allclose(x.segment[1][1], y_line) 1312 1286 1313 1287 assert x.triangle_id in intersected_triangles … … 1316 1290 1317 1291 msg = 'Segments do not add up' 1318 assert allclose(total_length, 2), msg1292 assert numpy.allclose(total_length, 2), msg 1319 1293 1320 1294 … … 1351 1325 total_length = 0 1352 1326 for x in L: 1353 assert allclose(x.length, 1.0)1354 assert allclose(x.normal, [0,-1])1355 1356 assert allclose(x.segment[1][0], x.segment[0][0] + x.length)1357 assert allclose(x.segment[0][1], y_line)1358 assert allclose(x.segment[1][1], y_line)1327 assert numpy.allclose(x.length, 1.0) 1328 assert numpy.allclose(x.normal, [0,-1]) 1329 1330 assert numpy.allclose(x.segment[1][0], x.segment[0][0] + x.length) 1331 assert numpy.allclose(x.segment[0][1], y_line) 1332 assert numpy.allclose(x.segment[1][1], y_line) 1359 1333 1360 1334 … … 1365 1339 1366 1340 msg = 'Segments do not add up' 1367 assert allclose(total_length, 2), msg1341 assert numpy.allclose(total_length, 2), msg 1368 1342 1369 1343 … … 1405 1379 for x in L: 1406 1380 if x.triangle_id == 1: 1407 assert allclose(x.length, 1)1408 assert allclose(x.normal, [0, -1])1381 assert numpy.allclose(x.length, 1) 1382 assert numpy.allclose(x.normal, [0, -1]) 1409 1383 1410 1384 if x.triangle_id == 5: 1411 assert allclose(x.length, 0.5)1412 assert allclose(x.normal, [0, -1])1385 assert numpy.allclose(x.length, 0.5) 1386 assert numpy.allclose(x.normal, [0, -1]) 1413 1387 1414 1388 … … 1418 1392 1419 1393 msg = 'Segments do not add up' 1420 assert allclose(total_length, 1.5), msg1394 assert numpy.allclose(total_length, 1.5), msg 1421 1395 1422 1396 … … 1452 1426 total_length = 0 1453 1427 for i, x in enumerate(L): 1454 assert allclose(x.length, s2)1455 assert allclose(x.normal, [-s2, -s2])1456 assert allclose(sum(x.normal**2), 1)1428 assert numpy.allclose(x.length, s2) 1429 assert numpy.allclose(x.normal, [-s2, -s2]) 1430 assert numpy.allclose(sum(x.normal**2), 1) 1457 1431 1458 1432 assert x.triangle_id in intersected_triangles … … 1461 1435 1462 1436 msg = 'Segments do not add up' 1463 assert allclose(total_length, 4*s2), msg1437 assert numpy.allclose(total_length, 4*s2), msg 1464 1438 1465 1439 … … 1476 1450 total_length = 0 1477 1451 for i, x in enumerate(L): 1478 assert allclose(x.length, s2)1479 assert allclose(x.normal, [s2, s2])1480 assert allclose(sum(x.normal**2), 1)1452 assert numpy.allclose(x.length, s2) 1453 assert numpy.allclose(x.normal, [s2, s2]) 1454 assert numpy.allclose(sum(x.normal**2), 1) 1481 1455 1482 1456 assert x.triangle_id in intersected_triangles … … 1485 1459 1486 1460 msg = 'Segments do not add up' 1487 assert allclose(total_length, 4*s2), msg1461 assert numpy.allclose(total_length, 4*s2), msg 1488 1462 1489 1463 … … 1501 1475 total_length = 0 1502 1476 for i, x in enumerate(L): 1503 assert allclose(x.length, 2*s2)1504 assert allclose(x.normal, [-s2, s2])1505 assert allclose(sum(x.normal**2), 1)1477 assert numpy.allclose(x.length, 2*s2) 1478 assert numpy.allclose(x.normal, [-s2, s2]) 1479 assert numpy.allclose(sum(x.normal**2), 1) 1506 1480 1507 1481 assert x.triangle_id in intersected_triangles … … 1510 1484 1511 1485 msg = 'Segments do not add up' 1512 assert allclose(total_length, 4*s2), msg1486 assert numpy.allclose(total_length, 4*s2), msg 1513 1487 1514 1488 … … 1525 1499 total_length = 0 1526 1500 for i, x in enumerate(L): 1527 assert allclose(x.length, 2*s2)1528 assert allclose(x.normal, [s2, -s2])1529 assert allclose(sum(x.normal**2), 1)1501 assert numpy.allclose(x.length, 2*s2) 1502 assert numpy.allclose(x.normal, [s2, -s2]) 1503 assert numpy.allclose(sum(x.normal**2), 1) 1530 1504 1531 1505 assert x.triangle_id in intersected_triangles … … 1534 1508 1535 1509 msg = 'Segments do not add up' 1536 assert allclose(total_length, 4*s2), msg1510 assert numpy.allclose(total_length, 4*s2), msg 1537 1511 1538 1512 … … 1550 1524 total_length = 0 1551 1525 for i, x in enumerate(L): 1552 assert allclose(x.length, s2)1553 assert allclose(x.normal, [-s2, -s2])1554 assert allclose(sum(x.normal**2), 1)1526 assert numpy.allclose(x.length, s2) 1527 assert numpy.allclose(x.normal, [-s2, -s2]) 1528 assert numpy.allclose(sum(x.normal**2), 1) 1555 1529 1556 1530 assert x.triangle_id in intersected_triangles … … 1559 1533 1560 1534 msg = 'Segments do not add up' 1561 assert allclose(total_length, 2*s2), msg1535 assert numpy.allclose(total_length, 2*s2), msg 1562 1536 1563 1537 … … 1572 1546 total_length = 0 1573 1547 for i, x in enumerate(L): 1574 assert allclose(x.normal, [-s2, -s2])1575 assert allclose(sum(x.normal**2), 1)1548 assert numpy.allclose(x.normal, [-s2, -s2]) 1549 assert numpy.allclose(sum(x.normal**2), 1) 1576 1550 1577 1551 msg = 'Triangle %d' %x.triangle_id + ' is not in %s' %(intersected_triangles) … … 1608 1582 assert len(L) == 1 1609 1583 assert L[0].triangle_id == 3 1610 assert allclose(L[0].length, 0.5)1611 assert allclose(L[0].normal, [-1,0])1584 assert numpy.allclose(L[0].length, 0.5) 1585 assert numpy.allclose(L[0].normal, [-1,0]) 1612 1586 1613 1587 … … 1620 1594 assert len(L) == 1 1621 1595 assert L[0].triangle_id == 3 1622 assert allclose(L[0].length, 0.4)1623 assert allclose(L[0].normal, [-1,0])1596 assert numpy.allclose(L[0].length, 0.4) 1597 assert numpy.allclose(L[0].normal, [-1,0]) 1624 1598 1625 1599 intersected_triangles = [3] … … 1633 1607 assert len(L) == 1 1634 1608 assert L[0].triangle_id == 3 1635 assert allclose(L[0].length, 0.4)1636 assert allclose(L[0].normal, [1,0])1609 assert numpy.allclose(L[0].length, 0.4) 1610 assert numpy.allclose(L[0].normal, [1,0]) 1637 1611 1638 1612 … … 1668 1642 for x in L: 1669 1643 if x.triangle_id == 3: 1670 assert allclose(x.length, 0.5)1671 assert allclose(x.normal, [-1,0])1644 assert numpy.allclose(x.length, 0.5) 1645 assert numpy.allclose(x.normal, [-1,0]) 1672 1646 1673 1647 if x.triangle_id == 2: 1674 assert allclose(x.length, s2)1675 assert allclose(x.normal, [-s2,-s2])1648 assert numpy.allclose(x.length, s2) 1649 assert numpy.allclose(x.normal, [-s2,-s2]) 1676 1650 1677 1651 … … 1706 1680 for x in L: 1707 1681 if x.triangle_id == 3: 1708 assert allclose(x.length, 0.5)1709 assert allclose(x.normal, [-1,0])1682 assert numpy.allclose(x.length, 0.5) 1683 assert numpy.allclose(x.normal, [-1,0]) 1710 1684 1711 1685 if x.triangle_id == 2: 1712 1686 msg = str(x.length) 1713 assert allclose(x.length, s2), msg1714 assert allclose(x.normal, [-s2,-s2])1687 assert numpy.allclose(x.length, s2), msg 1688 assert numpy.allclose(x.normal, [-s2,-s2]) 1715 1689 1716 1690 if x.triangle_id == 5: 1717 segvec = array([line[2][0]-1,1718 line[2][1]-1])1691 segvec = numpy.array([line[2][0]-1, 1692 line[2][1]-1]) 1719 1693 msg = str(x.length) 1720 assert allclose(x.length, sqrt(sum(segvec**2))), msg1721 assert allclose(x.normal, [-s2,-s2])1694 assert numpy.allclose(x.length, sqrt(sum(segvec**2))), msg 1695 assert numpy.allclose(x.normal, [-s2,-s2]) 1722 1696 1723 1697 … … 1757 1731 for x in L: 1758 1732 if x.triangle_id == 3: 1759 assert allclose(x.length, 0.5)1760 assert allclose(x.normal, [-1,0])1733 assert numpy.allclose(x.length, 0.5) 1734 assert numpy.allclose(x.normal, [-1,0]) 1761 1735 1762 1736 if x.triangle_id == 2: 1763 1737 msg = str(x.length) 1764 assert allclose(x.length, s2), msg1765 assert allclose(x.normal, [-s2,-s2])1738 assert numpy.allclose(x.length, s2), msg 1739 assert numpy.allclose(x.normal, [-s2,-s2]) 1766 1740 1767 1741 if x.triangle_id == 5: 1768 1742 if x.segment == ((1.0, 1.0), (1.25, 0.75)): 1769 segvec = array([line[2][0]-1,1770 line[2][1]-1])1743 segvec = numpy.array([line[2][0]-1, 1744 line[2][1]-1]) 1771 1745 msg = str(x.length) 1772 assert allclose(x.length, sqrt(sum(segvec**2))), msg1773 assert allclose(x.normal, [-s2,-s2])1746 assert numpy.allclose(x.length, sqrt(sum(segvec**2))), msg 1747 assert numpy.allclose(x.normal, [-s2,-s2]) 1774 1748 elif x.segment == ((1.25, 0.75), (1.5, 1.0)): 1775 segvec = array([1.5-line[2][0],1776 1.0-line[2][1]])1749 segvec = numpy.array([1.5-line[2][0], 1750 1.0-line[2][1]]) 1777 1751 1778 assert allclose(x.length, sqrt(sum(segvec**2))), msg1779 assert allclose(x.normal, [s2,-s2])1752 assert numpy.allclose(x.length, sqrt(sum(segvec**2))), msg 1753 assert numpy.allclose(x.normal, [s2,-s2]) 1780 1754 else: 1781 1755 msg = 'Unknow segment: %s' %x.segment … … 1785 1759 1786 1760 if x.triangle_id == 6: 1787 assert allclose(x.normal, [s2,-s2])1788 assert allclose(x.segment, ((1.5, 1.0), (2, 1.5)))1761 assert numpy.allclose(x.normal, [s2,-s2]) 1762 assert numpy.allclose(x.segment, ((1.5, 1.0), (2, 1.5))) 1789 1763 1790 1764 … … 1852 1826 ref_length = line[1][1] - line[0][1] 1853 1827 #print ref_length, total_length 1854 assert allclose(total_length, ref_length)1828 assert numpy.allclose(total_length, ref_length) 1855 1829 1856 1830 -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_pmesh2domain.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 # 5 3 6 4 import unittest 7 8 #from numpy.oldnumeric import allclose, array 9 from numpy import allclose, array 10 5 import numpy 11 6 12 7 #from anuga.pyvolution.pmesh2domain import * … … 69 64 70 65 tags = {} 71 b1 = Dirichlet_boundary(conserved_quantities = array([0.0]))72 b2 = Dirichlet_boundary(conserved_quantities = array([1.0]))73 b3 = Dirichlet_boundary(conserved_quantities = array([2.0]))66 b1 = Dirichlet_boundary(conserved_quantities = numpy.array([0.0])) 67 b2 = Dirichlet_boundary(conserved_quantities = numpy.array([1.0])) 68 b3 = Dirichlet_boundary(conserved_quantities = numpy.array([2.0])) 74 69 tags["1"] = b1 75 70 tags["2"] = b2 … … 83 78 answer = [[0., 8., 0.], 84 79 [0., 10., 8.]] 85 assert allclose(domain.quantities['elevation'].vertex_values,86 answer)80 assert numpy.allclose(domain.quantities['elevation'].vertex_values, 81 answer) 87 82 88 83 #print domain.quantities['stage'].vertex_values 89 84 answer = [[0., 12., 10.], 90 85 [0., 10., 12.]] 91 assert allclose(domain.quantities['stage'].vertex_values,92 answer)86 assert numpy.allclose(domain.quantities['stage'].vertex_values, 87 answer) 93 88 94 89 #print domain.quantities['friction'].vertex_values 95 90 answer = [[0.01, 0.04, 0.03], 96 91 [0.01, 0.02, 0.04]] 97 assert allclose(domain.quantities['friction'].vertex_values,98 answer)99 100 #print domain.quantities['friction'].vertex_values 101 assert allclose(domain.tagged_elements['dsg'][0],0)102 assert allclose(domain.tagged_elements['ole nielsen'][0],1)92 assert numpy.allclose(domain.quantities['friction'].vertex_values, 93 answer) 94 95 #print domain.quantities['friction'].vertex_values 96 assert numpy.allclose(domain.tagged_elements['dsg'][0],0) 97 assert numpy.allclose(domain.tagged_elements['ole nielsen'][0],1) 103 98 104 99 self.failUnless( domain.boundary[(1, 0)] == '1', … … 162 157 163 158 tags = {} 164 b1 = Dirichlet_boundary(conserved_quantities = array([0.0]))165 b2 = Dirichlet_boundary(conserved_quantities = array([1.0]))166 b3 = Dirichlet_boundary(conserved_quantities = array([2.0]))159 b1 = Dirichlet_boundary(conserved_quantities = numpy.array([0.0])) 160 b2 = Dirichlet_boundary(conserved_quantities = numpy.array([1.0])) 161 b3 = Dirichlet_boundary(conserved_quantities = numpy.array([2.0])) 167 162 tags["1"] = b1 168 163 tags["2"] = b2 … … 177 172 answer = [[0., 8., 0.], 178 173 [0., 10., 8.]] 179 assert allclose(domain.quantities['elevation'].vertex_values,180 answer)174 assert numpy.allclose(domain.quantities['elevation'].vertex_values, 175 answer) 181 176 182 177 #print domain.quantities['stage'].vertex_values 183 178 answer = [[0., 12., 10.], 184 179 [0., 10., 12.]] 185 assert allclose(domain.quantities['stage'].vertex_values,186 answer)180 assert numpy.allclose(domain.quantities['stage'].vertex_values, 181 answer) 187 182 188 183 #print domain.quantities['friction'].vertex_values 189 184 answer = [[0.01, 0.04, 0.03], 190 185 [0.01, 0.02, 0.04]] 191 assert allclose(domain.quantities['friction'].vertex_values,192 answer)193 194 #print domain.quantities['friction'].vertex_values 195 assert allclose(domain.tagged_elements['dsg'][0],0)196 assert allclose(domain.tagged_elements['ole nielsen'][0],1)186 assert numpy.allclose(domain.quantities['friction'].vertex_values, 187 answer) 188 189 #print domain.quantities['friction'].vertex_values 190 assert numpy.allclose(domain.tagged_elements['dsg'][0],0) 191 assert numpy.allclose(domain.tagged_elements['ole nielsen'][0],1) 197 192 198 193 self.failUnless( domain.boundary[(1, 0)] == '1', … … 231 226 #domain.set_tag_dict(tag_dict) 232 227 #Boundary tests 233 b1 = Dirichlet_boundary(conserved_quantities = array([0.0]))234 b2 = Dirichlet_boundary(conserved_quantities = array([1.0]))235 b3 = Dirichlet_boundary(conserved_quantities = array([1.0]))228 b1 = Dirichlet_boundary(conserved_quantities = numpy.array([0.0])) 229 b2 = Dirichlet_boundary(conserved_quantities = numpy.array([1.0])) 230 b3 = Dirichlet_boundary(conserved_quantities = numpy.array([1.0])) 236 231 #test adding a boundary 237 232 tags = {} -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_quantity.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 9 7 from quantity import * 10 8 from anuga.config import epsilon 11 #from numpy.oldnumeric import allclose, array, ones, Float 12 from numpy import allclose, array, ones, float 9 import numpy 13 10 14 11 from anuga.fit_interpolate.fit import fit_to_mesh … … 21 18 #Aux for fit_interpolate.fit example 22 19 def linear_function(point): 23 point = array(point)20 point = numpy.array(point) 24 21 return point[:,0]+point[:,1] 25 22 … … 63 60 64 61 65 ##def test_creation(self):66 ## 67 ##quantity = Quantity(self.mesh1, [[1,2,3]])68 ## assertallclose(quantity.vertex_values, [[1.,2.,3.]])69 ## 70 ##try:71 ##quantity = Quantity()72 ##except:73 ##pass74 ##else:75 ##raise 'Should have raised empty quantity exception'76 ## 77 ## 78 ##try:79 ##quantity = Quantity([1,2,3])80 ##except AssertionError:81 ##pass82 ##except:83 ##raise 'Should have raised "mising mesh object" error'84 ## 85 ## 86 ##def test_creation_zeros(self):87 ## 88 ##quantity = Quantity(self.mesh1)89 ## assertallclose(quantity.vertex_values, [[0.,0.,0.]])90 ## 91 ## 92 ##quantity = Quantity(self.mesh4)93 ## assertallclose(quantity.vertex_values, [[0.,0.,0.], [0.,0.,0.],94 ##[0.,0.,0.], [0.,0.,0.]])95 ## 96 ## 97 ##def test_interpolation(self):98 ##quantity = Quantity(self.mesh1, [[1,2,3]])99 ## assertallclose(quantity.centroid_values, [2.0]) #Centroid100 ## 101 ## assertallclose(quantity.edge_values, [[2.5, 2.0, 1.5]])102 ## 103 ## 104 ##def test_interpolation2(self):105 ##quantity = Quantity(self.mesh4,106 ##[[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]])107 ## assertallclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid108 ## 109 ## 110 ##quantity.extrapolate_second_order()111 ## 112 ###print quantity.vertex_values113 ## assertallclose(quantity.vertex_values, [[3.5, -1.0, 3.5],114 ##[3.+2./3, 6.+2./3, 4.+2./3],115 ##[4.6, 3.4, 1.],116 ##[-5.0, 1.0, 4.0]])117 ## 118 ###print quantity.edge_values119 ## assertallclose(quantity.edge_values, [[1.25, 3.5, 1.25],120 ##[5. + 2/3.0, 4.0 + 1.0/6, 5.0 + 1.0/6],121 ##[2.2, 2.8, 4.0],122 ##[2.5, -0.5, -2.0]])123 ## 124 ## 125 ## #assertallclose(quantity.edge_values, [[2.5, 2.0, 1.5],126 ## #[5., 5., 5.],127 ## #[4.5, 4.5, 0.],128 ## #[3.0, -1.5, -1.5]])129 ## 62 def test_creation(self): 63 64 quantity = Quantity(self.mesh1, [[1,2,3]]) 65 assert numpy.allclose(quantity.vertex_values, [[1.,2.,3.]]) 66 67 try: 68 quantity = Quantity() 69 except: 70 pass 71 else: 72 raise 'Should have raised empty quantity exception' 73 74 75 try: 76 quantity = Quantity([1,2,3]) 77 except AssertionError: 78 pass 79 except: 80 raise 'Should have raised "mising mesh object" error' 81 82 83 def test_creation_zeros(self): 84 85 quantity = Quantity(self.mesh1) 86 assert numpy.allclose(quantity.vertex_values, [[0.,0.,0.]]) 87 88 89 quantity = Quantity(self.mesh4) 90 assert numpy.allclose(quantity.vertex_values, [[0.,0.,0.], [0.,0.,0.], 91 [0.,0.,0.], [0.,0.,0.]]) 92 93 94 def test_interpolation(self): 95 quantity = Quantity(self.mesh1, [[1,2,3]]) 96 assert numpy.allclose(quantity.centroid_values, [2.0]) #Centroid 97 98 assert numpy.allclose(quantity.edge_values, [[2.5, 2.0, 1.5]]) 99 100 101 def test_interpolation2(self): 102 quantity = Quantity(self.mesh4, 103 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 104 assert numpy.allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 105 106 107 quantity.extrapolate_second_order() 108 109 #print quantity.vertex_values 110 assert numpy.allclose(quantity.vertex_values, [[3.5, -1.0, 3.5], 111 [3.+2./3, 6.+2./3, 4.+2./3], 112 [4.6, 3.4, 1.], 113 [-5.0, 1.0, 4.0]]) 114 115 #print quantity.edge_values 116 assert numpy.allclose(quantity.edge_values, [[1.25, 3.5, 1.25], 117 [5. + 2/3.0, 4.0 + 1.0/6, 5.0 + 1.0/6], 118 [2.2, 2.8, 4.0], 119 [2.5, -0.5, -2.0]]) 120 121 122 #assert numpy.allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 123 # [5., 5., 5.], 124 # [4.5, 4.5, 0.], 125 # [3.0, -1.5, -1.5]]) 126 130 127 def test_get_extrema_1(self): 131 128 quantity = Quantity(self.mesh4, 132 129 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 133 assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroids130 assert numpy.allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroids 134 131 135 132 v = quantity.get_maximum_value() … … 151 148 152 149 v = quantity.get_values(interpolation_points = [[x,y]]) 153 assert allclose(v, 5)150 assert numpy.allclose(v, 5) 154 151 155 152 156 153 x,y = quantity.get_minimum_location() 157 154 v = quantity.get_values(interpolation_points = [[x,y]]) 158 assert allclose(v, 0) 159 160 161 ## def test_get_maximum_2(self): 162 ## 163 ## a = [0.0, 0.0] 164 ## b = [0.0, 2.0] 165 ## c = [2.0,0.0] 166 ## d = [0.0, 4.0] 167 ## e = [2.0, 2.0] 168 ## f = [4.0,0.0] 169 ## 170 ## points = [a, b, c, d, e, f] 171 ## #bac, bce, ecf, dbe 172 ## vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 173 ## 174 ## domain = Domain(points, vertices) 175 ## 176 ## quantity = Quantity(domain) 177 ## quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 178 ## 179 ## v = quantity.get_maximum_value() 180 ## assert v == 6 181 ## 182 ## v = quantity.get_minimum_value() 183 ## assert v == 2 184 ## 185 ## i = quantity.get_maximum_index() 186 ## assert i == 3 187 ## 188 ## i = quantity.get_minimum_index() 189 ## assert i == 0 190 ## 191 ## x,y = quantity.get_maximum_location() 192 ## xref, yref = 2.0/3, 8.0/3 193 ## assert x == xref 194 ## assert y == yref 195 ## 196 ## v = quantity.get_values(interpolation_points = [[x,y]]) 197 ## assert allclose(v, 6) 198 ## 199 ## x,y = quantity.get_minimum_location() 200 ## v = quantity.get_values(interpolation_points = [[x,y]]) 201 ## assert allclose(v, 2) 202 ## 203 ## #Multiple locations for maximum - 204 ## #Test that the algorithm picks the first occurrence 205 ## v = quantity.get_maximum_value(indices=[0,1,2]) 206 ## assert allclose(v, 4) 207 ## 208 ## i = quantity.get_maximum_index(indices=[0,1,2]) 209 ## assert i == 1 210 ## 211 ## x,y = quantity.get_maximum_location(indices=[0,1,2]) 212 ## xref, yref = 4.0/3, 4.0/3 213 ## assert x == xref 214 ## assert y == yref 215 ## 216 ## v = quantity.get_values(interpolation_points = [[x,y]]) 217 ## assert allclose(v, 4) 218 ## 219 ## # More test of indices...... 220 ## v = quantity.get_maximum_value(indices=[2,3]) 221 ## assert allclose(v, 6) 222 ## 223 ## i = quantity.get_maximum_index(indices=[2,3]) 224 ## assert i == 3 225 ## 226 ## x,y = quantity.get_maximum_location(indices=[2,3]) 227 ## xref, yref = 2.0/3, 8.0/3 228 ## assert x == xref 229 ## assert y == yref 230 ## 231 ## v = quantity.get_values(interpolation_points = [[x,y]]) 232 ## assert allclose(v, 6) 233 ## 234 ## 235 ## 236 ## def test_boundary_allocation(self): 237 ## quantity = Quantity(self.mesh4, 238 ## [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 239 ## 240 ## assert quantity.boundary_values.shape[0] == len(self.mesh4.boundary) 241 ## 242 ## 243 ## def test_set_values(self): 244 ## quantity = Quantity(self.mesh4) 245 ## 246 ## 247 ## quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 248 ## location = 'vertices') 249 ## assert allclose(quantity.vertex_values, 250 ## [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 251 ## assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 252 ## assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 253 ## [5., 5., 5.], 254 ## [4.5, 4.5, 0.], 255 ## [3.0, -1.5, -1.5]]) 256 ## 257 ## 258 ## # Test default 259 ## quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 260 ## assert allclose(quantity.vertex_values, 261 ## [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 262 ## assert allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 263 ## assert allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 264 ## [5., 5., 5.], 265 ## [4.5, 4.5, 0.], 266 ## [3.0, -1.5, -1.5]]) 267 ## 268 ## # Test centroids 269 ## quantity.set_values([1,2,3,4], location = 'centroids') 270 ## assert allclose(quantity.centroid_values, [1., 2., 3., 4.]) #Centroid 271 ## 272 ## # Test exceptions 273 ## try: 274 ## quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 275 ## location = 'bas kamel tuba') 276 ## except: 277 ## pass 278 ## 279 ## 280 ## try: 281 ## quantity.set_values([[1,2,3], [0,0,9]]) 282 ## except AssertionError: 283 ## pass 284 ## except: 285 ## raise 'should have raised Assertionerror' 286 ## 287 ## 288 ## 289 ## def test_set_values_const(self): 290 ## quantity = Quantity(self.mesh4) 291 ## 292 ## quantity.set_values(1.0, location = 'vertices') 293 ## assert allclose(quantity.vertex_values, 294 ## [[1,1,1], [1,1,1], [1,1,1], [1, 1, 1]]) 295 ## 296 ## assert allclose(quantity.centroid_values, [1, 1, 1, 1]) #Centroid 297 ## assert allclose(quantity.edge_values, [[1, 1, 1], 298 ## [1, 1, 1], 299 ## [1, 1, 1], 300 ## [1, 1, 1]]) 301 ## 302 ## 303 ## quantity.set_values(2.0, location = 'centroids') 304 ## assert allclose(quantity.centroid_values, [2, 2, 2, 2]) 305 ## 306 ## 307 ## def test_set_values_func(self): 308 ## quantity = Quantity(self.mesh4) 309 ## 310 ## def f(x, y): 311 ## return x+y 312 ## 313 ## quantity.set_values(f, location = 'vertices') 314 ## #print "quantity.vertex_values",quantity.vertex_values 315 ## assert allclose(quantity.vertex_values, 316 ## [[2,0,2], [2,2,4], [4,2,4], [4,2,4]]) 317 ## assert allclose(quantity.centroid_values, 318 ## [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 319 ## assert allclose(quantity.edge_values, 320 ## [[1,2,1], [3,3,2], [3,4,3], [3,4,3]]) 321 ## 322 ## 323 ## quantity.set_values(f, location = 'centroids') 324 ## assert allclose(quantity.centroid_values, 325 ## [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 326 ## 327 ## 328 ## def test_integral(self): 329 ## quantity = Quantity(self.mesh4) 330 ## 331 ## #Try constants first 332 ## const = 5 333 ## quantity.set_values(const, location = 'vertices') 334 ## #print 'Q', quantity.get_integral() 335 ## 336 ## assert allclose(quantity.get_integral(), self.mesh4.get_area() * const) 337 ## 338 ## #Try with a linear function 339 ## def f(x, y): 340 ## return x+y 341 ## 342 ## quantity.set_values(f, location = 'vertices') 343 ## 344 ## 345 ## ref_integral = (4.0/3 + 8.0/3 + 10.0/3 + 10.0/3) * 2 346 ## 347 ## assert allclose (quantity.get_integral(), ref_integral) 348 ## 349 ## 350 ## 351 ## def test_set_vertex_values(self): 352 ## quantity = Quantity(self.mesh4) 353 ## quantity.set_vertex_values([0,1,2,3,4,5]) 354 ## 355 ## assert allclose(quantity.vertex_values, 356 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 357 ## assert allclose(quantity.centroid_values, 358 ## [1., 7./3, 11./3, 8./3]) #Centroid 359 ## assert allclose(quantity.edge_values, [[1., 1.5, 0.5], 360 ## [3., 2.5, 1.5], 361 ## [3.5, 4.5, 3.], 362 ## [2.5, 3.5, 2]]) 363 ## 364 ## 365 ## def test_set_vertex_values_subset(self): 366 ## quantity = Quantity(self.mesh4) 367 ## quantity.set_vertex_values([0,1,2,3,4,5]) 368 ## quantity.set_vertex_values([0,20,30,50], indices = [0,2,3,5]) 369 ## 370 ## assert allclose(quantity.vertex_values, 371 ## [[1,0,20], [1,20,4], [4,20,50], [30,1,4]]) 372 ## 373 ## 374 ## def test_set_vertex_values_using_general_interface(self): 375 ## quantity = Quantity(self.mesh4) 376 ## 377 ## 378 ## quantity.set_values([0,1,2,3,4,5]) 379 ## 380 ## 381 ## assert allclose(quantity.vertex_values, 382 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 383 ## 384 ## #Centroid 385 ## assert allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) 386 ## 387 ## assert allclose(quantity.edge_values, [[1., 1.5, 0.5], 388 ## [3., 2.5, 1.5], 389 ## [3.5, 4.5, 3.], 390 ## [2.5, 3.5, 2]]) 391 ## 392 ## 393 ## 394 ## def test_set_vertex_values_using_general_interface_with_subset(self): 395 ## """test_set_vertex_values_using_general_interface_with_subset(self): 396 ## 397 ## Test that indices and polygon works (for constants values) 398 ## """ 399 ## 400 ## quantity = Quantity(self.mesh4) 401 ## 402 ## 403 ## quantity.set_values([0,2,3,5], indices=[0,2,3,5]) 404 ## assert allclose(quantity.vertex_values, 405 ## [[0,0,2], [0,2,0], [0,2,5], [3,0,0]]) 406 ## 407 ## 408 ## # Constant 409 ## quantity.set_values(0.0) 410 ## quantity.set_values(3.14, indices=[0,2], location='vertices') 411 ## 412 ## # Indices refer to triangle numbers 413 ## assert allclose(quantity.vertex_values, 414 ## [[3.14,3.14,3.14], [0,0,0], 415 ## [3.14,3.14,3.14], [0,0,0]]) 416 ## 417 ## 418 ## 419 ## # Now try with polygon (pick points where y>2) 420 ## polygon = [[0,2.1], [4,2.1], [4,7], [0,7]] 421 ## quantity.set_values(0.0) 422 ## quantity.set_values(3.14, polygon=polygon) 423 ## 424 ## assert allclose(quantity.vertex_values, 425 ## [[0,0,0], [0,0,0], [0,0,0], 426 ## [3.14,3.14,3.14]]) 427 ## 428 ## 429 ## # Another polygon (pick triangle 1 and 2 (rightmost triangles) 430 ## # using centroids 431 ## polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 432 ## quantity.set_values(0.0) 433 ## quantity.set_values(3.14, location='centroids', polygon=polygon) 434 ## assert allclose(quantity.vertex_values, 435 ## [[0,0,0], 436 ## [3.14,3.14,3.14], 437 ## [3.14,3.14,3.14], 438 ## [0,0,0]]) 439 ## 440 ## 441 ## # Same polygon now use vertices (default) 442 ## polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 443 ## quantity.set_values(0.0) 444 ## #print 'Here 2' 445 ## quantity.set_values(3.14, polygon=polygon) 446 ## assert allclose(quantity.vertex_values, 447 ## [[0,0,0], 448 ## [3.14,3.14,3.14], 449 ## [3.14,3.14,3.14], 450 ## [0,0,0]]) 451 ## 452 ## 453 ## # Test input checking 454 ## try: 455 ## quantity.set_values(3.14, polygon=polygon, indices = [0,2]) 456 ## except: 457 ## pass 458 ## else: 459 ## msg = 'Should have caught this' 460 ## raise msg 461 ## 462 ## 463 ## 464 ## 465 ## 466 ## def test_set_vertex_values_using_general_interface_subset_and_geo(self): 467 ## """test_set_vertex_values_using_general_interface_with_subset(self): 468 ## Test that indices and polygon works using georeferencing 469 ## """ 470 ## 471 ## quantity = Quantity(self.mesh4) 472 ## G = Geo_reference(56, 10, 100) 473 ## quantity.domain.geo_reference = G 474 ## 475 ## #print quantity.domain.get_nodes(absolute=True) 476 ## 477 ## 478 ## # Constant 479 ## quantity.set_values(0.0) 480 ## quantity.set_values(3.14, indices=[0,2], location='vertices') 481 ## 482 ## # Indices refer to triangle numbers here - not vertices (why?) 483 ## assert allclose(quantity.vertex_values, 484 ## [[3.14,3.14,3.14], [0,0,0], 485 ## [3.14,3.14,3.14], [0,0,0]]) 486 ## 487 ## 488 ## 489 ## # Now try with polygon (pick points where y>2) 490 ## polygon = array([[0,2.1], [4,2.1], [4,7], [0,7]]) 491 ## polygon += [G.xllcorner, G.yllcorner] 492 ## 493 ## quantity.set_values(0.0) 494 ## quantity.set_values(3.14, polygon=polygon, location='centroids') 495 ## 496 ## assert allclose(quantity.vertex_values, 497 ## [[0,0,0], [0,0,0], [0,0,0], 498 ## [3.14,3.14,3.14]]) 499 ## 500 ## 501 ## # Another polygon (pick triangle 1 and 2 (rightmost triangles) 502 ## polygon = array([[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]]) 503 ## polygon += [G.xllcorner, G.yllcorner] 504 ## 505 ## quantity.set_values(0.0) 506 ## quantity.set_values(3.14, polygon=polygon) 507 ## 508 ## assert allclose(quantity.vertex_values, 509 ## [[0,0,0], 510 ## [3.14,3.14,3.14], 511 ## [3.14,3.14,3.14], 512 ## [0,0,0]]) 513 ## 514 ## 515 ## 516 ## def test_set_values_using_fit(self): 517 ## 518 ## 519 ## quantity = Quantity(self.mesh4) 520 ## 521 ## #Get (enough) datapoints 522 ## data_points = [[ 0.66666667, 0.66666667], 523 ## [ 1.33333333, 1.33333333], 524 ## [ 2.66666667, 0.66666667], 525 ## [ 0.66666667, 2.66666667], 526 ## [ 0.0, 1.0], 527 ## [ 0.0, 3.0], 528 ## [ 1.0, 0.0], 529 ## [ 1.0, 1.0], 530 ## [ 1.0, 2.0], 531 ## [ 1.0, 3.0], 532 ## [ 2.0, 1.0], 533 ## [ 3.0, 0.0], 534 ## [ 3.0, 1.0]] 535 ## 536 ## z = linear_function(data_points) 537 ## 538 ## #Use built-in fit_interpolate.fit 539 ## quantity.set_values( Geospatial_data(data_points, z), alpha = 0 ) 540 ## #quantity.set_values(points = data_points, values = z, alpha = 0) 541 ## 542 ## 543 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 544 ## #print quantity.vertex_values, answer 545 ## assert allclose(quantity.vertex_values.ravel(), answer) 546 ## 547 ## 548 ## #Now try by setting the same values directly 549 ## vertex_attributes = fit_to_mesh(data_points, 550 ## quantity.domain.get_nodes(), 551 ## quantity.domain.triangles, #FIXME 552 ## point_attributes=z, 553 ## alpha = 0, 554 ## verbose=False) 555 ## 556 ## #print vertex_attributes 557 ## quantity.set_values(vertex_attributes) 558 ## assert allclose(quantity.vertex_values.ravel(), answer) 559 ## 560 ## 561 ## 562 ## 563 ## 564 ## def test_test_set_values_using_fit_w_geo(self): 565 ## 566 ## 567 ## #Mesh 568 ## vertex_coordinates = [[0.76, 0.76], 569 ## [0.76, 5.76], 570 ## [5.76, 0.76]] 571 ## triangles = [[0,2,1]] 572 ## 573 ## mesh_georef = Geo_reference(56,-0.76,-0.76) 574 ## mesh1 = Domain(vertex_coordinates, triangles, 575 ## geo_reference = mesh_georef) 576 ## mesh1.check_integrity() 577 ## 578 ## #Quantity 579 ## quantity = Quantity(mesh1) 580 ## 581 ## #Data 582 ## data_points = [[ 201.0, 401.0], 583 ## [ 201.0, 403.0], 584 ## [ 203.0, 401.0]] 585 ## 586 ## z = [2, 4, 4] 587 ## 588 ## data_georef = Geo_reference(56,-200,-400) 589 ## 590 ## 591 ## #Reference 592 ## ref = fit_to_mesh(data_points, vertex_coordinates, triangles, 593 ## point_attributes=z, 594 ## data_origin = data_georef.get_origin(), 595 ## mesh_origin = mesh_georef.get_origin(), 596 ## alpha = 0) 597 ## 598 ## assert allclose( ref, [0,5,5] ) 599 ## 600 ## 601 ## #Test set_values 602 ## 603 ## quantity.set_values( Geospatial_data(data_points, z, data_georef), alpha = 0 ) 604 ## 605 ## #quantity.set_values(points = data_points, 606 ## # values = z, 607 ## # data_georef = data_georef, 608 ## # alpha = 0) 609 ## 610 ## 611 ## #quantity.set_values(points = data_points, 612 ## # values = z, 613 ## # data_georef = data_georef, 614 ## # alpha = 0) 615 ## assert allclose(quantity.vertex_values.ravel(), ref) 616 ## 617 ## 618 ## 619 ## #Test set_values using geospatial data object 620 ## quantity.vertex_values[:] = 0.0 621 ## 622 ## geo = Geospatial_data(data_points, z, data_georef) 623 ## 624 ## 625 ## quantity.set_values(geospatial_data = geo, alpha = 0) 626 ## assert allclose(quantity.vertex_values.ravel(), ref) 627 ## 628 ## 629 ## 630 ## def test_set_values_from_file1(self): 631 ## quantity = Quantity(self.mesh4) 632 ## 633 ## #Get (enough) datapoints 634 ## data_points = [[ 0.66666667, 0.66666667], 635 ## [ 1.33333333, 1.33333333], 636 ## [ 2.66666667, 0.66666667], 637 ## [ 0.66666667, 2.66666667], 638 ## [ 0.0, 1.0], 639 ## [ 0.0, 3.0], 640 ## [ 1.0, 0.0], 641 ## [ 1.0, 1.0], 642 ## [ 1.0, 2.0], 643 ## [ 1.0, 3.0], 644 ## [ 2.0, 1.0], 645 ## [ 3.0, 0.0], 646 ## [ 3.0, 1.0]] 647 ## 648 ## data_geo_spatial = Geospatial_data(data_points, 649 ## geo_reference = Geo_reference(56, 0, 0)) 650 ## data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 651 ## attributes = linear_function(data_points_absolute) 652 ## att = 'spam_and_eggs' 653 ## 654 ## #Create .txt file 655 ## ptsfile = tempfile.mktemp(".txt") 656 ## file = open(ptsfile,"w") 657 ## file.write(" x,y," + att + " \n") 658 ## for data_point, attribute in map(None, data_points_absolute 659 ## ,attributes): 660 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 661 ## + ',' + str(attribute) 662 ## file.write(row + "\n") 663 ## file.close() 664 ## 665 ## 666 ## #Check that values can be set from file 667 ## quantity.set_values(filename = ptsfile, 668 ## attribute_name = att, alpha = 0) 669 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 670 ## 671 ## #print quantity.vertex_values.ravel() 672 ## #print answer 673 ## 674 ## 675 ## assert allclose(quantity.vertex_values.ravel(), answer) 676 ## 677 ## 678 ## #Check that values can be set from file using default attribute 679 ## quantity.set_values(filename = ptsfile, alpha = 0) 680 ## assert allclose(quantity.vertex_values.ravel(), answer) 681 ## 682 ## #Cleanup 683 ## import os 684 ## os.remove(ptsfile) 685 ## 686 ## 687 ## 688 ## def Xtest_set_values_from_file_using_polygon(self): 689 ## """test_set_values_from_file_using_polygon(self): 690 ## 691 ## Test that polygon restriction works for general points data 692 ## """ 693 ## 694 ## quantity = Quantity(self.mesh4) 695 ## 696 ## #Get (enough) datapoints 697 ## data_points = [[ 0.66666667, 0.66666667], 698 ## [ 1.33333333, 1.33333333], 699 ## [ 2.66666667, 0.66666667], 700 ## [ 0.66666667, 2.66666667], 701 ## [ 0.0, 1.0], 702 ## [ 0.0, 3.0], 703 ## [ 1.0, 0.0], 704 ## [ 1.0, 1.0], 705 ## [ 1.0, 2.0], 706 ## [ 1.0, 3.0], 707 ## [ 2.0, 1.0], 708 ## [ 3.0, 0.0], 709 ## [ 3.0, 1.0]] 710 ## 711 ## data_geo_spatial = Geospatial_data(data_points, 712 ## geo_reference = Geo_reference(56, 0, 0)) 713 ## data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 714 ## attributes = linear_function(data_points_absolute) 715 ## att = 'spam_and_eggs' 716 ## 717 ## #Create .txt file 718 ## ptsfile = tempfile.mktemp(".txt") 719 ## file = open(ptsfile,"w") 720 ## file.write(" x,y," + att + " \n") 721 ## for data_point, attribute in map(None, data_points_absolute 722 ## ,attributes): 723 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 724 ## + ',' + str(attribute) 725 ## file.write(row + "\n") 726 ## file.close() 727 ## 728 ## # Create restricting polygon (containing node #4 (2,2) and 729 ## # centroid of triangle #1 (bce) 730 ## polygon = [[1.0, 1.0], [4.0, 1.0], 731 ## [4.0, 4.0], [1.0, 4.0]] 732 ## 733 ## #print self.mesh4.nodes 734 ## #print inside_polygon(self.mesh4.nodes, polygon) 735 ## assert allclose(inside_polygon(self.mesh4.nodes, polygon), 4) 736 ## 737 ## #print quantity.domain.get_vertex_coordinates() 738 ## #print quantity.domain.get_nodes() 739 ## 740 ## # Check that values can be set from file 741 ## quantity.set_values(filename=ptsfile, 742 ## polygon=polygon, 743 ## location='unique vertices', 744 ## alpha=0) 745 ## 746 ## # Get indices for vertex coordinates in polygon 747 ## indices = inside_polygon(quantity.domain.get_vertex_coordinates(), 748 ## polygon) 749 ## points = take(quantity.domain.get_vertex_coordinates(), indices) 750 ## 751 ## answer = linear_function(points) 752 ## 753 ## #print quantity.vertex_values.ravel() 754 ## #print answer 755 ## 756 ## # Check vertices in polygon have been set 757 ## assert allclose(take(quantity.vertex_values.ravel(), indices), 758 ## answer) 759 ## 760 ## # Check vertices outside polygon are zero 761 ## indices = outside_polygon(quantity.domain.get_vertex_coordinates(), 762 ## polygon) 763 ## assert allclose(take(quantity.vertex_values.ravel(), indices), 764 ## 0.0) 765 ## 766 ## #Cleanup 767 ## import os 768 ## os.remove(ptsfile) 769 ## 770 ## 771 ## 772 ## 773 ## def test_cache_test_set_values_from_file(self): 774 ## # FIXME (Ole): What is this about? 775 ## # I don't think it checks anything new 776 ## quantity = Quantity(self.mesh4) 777 ## 778 ## #Get (enough) datapoints 779 ## data_points = [[ 0.66666667, 0.66666667], 780 ## [ 1.33333333, 1.33333333], 781 ## [ 2.66666667, 0.66666667], 782 ## [ 0.66666667, 2.66666667], 783 ## [ 0.0, 1.0], 784 ## [ 0.0, 3.0], 785 ## [ 1.0, 0.0], 786 ## [ 1.0, 1.0], 787 ## [ 1.0, 2.0], 788 ## [ 1.0, 3.0], 789 ## [ 2.0, 1.0], 790 ## [ 3.0, 0.0], 791 ## [ 3.0, 1.0]] 792 ## 793 ## georef = Geo_reference(56, 0, 0) 794 ## data_geo_spatial = Geospatial_data(data_points, 795 ## geo_reference=georef) 796 ## 797 ## data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 798 ## attributes = linear_function(data_points_absolute) 799 ## att = 'spam_and_eggs' 800 ## 801 ## # Create .txt file 802 ## ptsfile = tempfile.mktemp(".txt") 803 ## file = open(ptsfile,"w") 804 ## file.write(" x,y," + att + " \n") 805 ## for data_point, attribute in map(None, data_points_absolute 806 ## ,attributes): 807 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 808 ## + ',' + str(attribute) 809 ## file.write(row + "\n") 810 ## file.close() 811 ## 812 ## 813 ## # Check that values can be set from file 814 ## quantity.set_values(filename=ptsfile, 815 ## attribute_name=att, 816 ## alpha=0, 817 ## use_cache=True, 818 ## verbose=False) 819 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 820 ## assert allclose(quantity.vertex_values.ravel(), answer) 821 ## 822 ## 823 ## # Check that values can be set from file using default attribute 824 ## quantity.set_values(filename=ptsfile, 825 ## alpha=0) 826 ## assert allclose(quantity.vertex_values.ravel(), answer) 827 ## 828 ## # Check cache 829 ## quantity.set_values(filename=ptsfile, 830 ## attribute_name=att, 831 ## alpha=0, 832 ## use_cache=True, 833 ## verbose=False) 834 ## 835 ## 836 ## #Cleanup 837 ## import os 838 ## os.remove(ptsfile) 839 ## 840 ## def test_set_values_from_lat_long(self): 841 ## quantity = Quantity(self.mesh_onslow) 842 ## 843 ## #Get (enough) datapoints 844 ## data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 845 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 846 ## 847 ## data_geo_spatial = Geospatial_data(data_points, 848 ## points_are_lats_longs=True) 849 ## points_UTM = data_geo_spatial.get_data_points(absolute=True) 850 ## attributes = linear_function(points_UTM) 851 ## att = 'elevation' 852 ## 853 ## #Create .txt file 854 ## txt_file = tempfile.mktemp(".txt") 855 ## file = open(txt_file,"w") 856 ## file.write(" lat,long," + att + " \n") 857 ## for data_point, attribute in map(None, data_points, attributes): 858 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 859 ## + ',' + str(attribute) 860 ## #print "row", row 861 ## file.write(row + "\n") 862 ## file.close() 863 ## 864 ## 865 ## #Check that values can be set from file 866 ## quantity.set_values(filename=txt_file, 867 ## attribute_name=att, 868 ## alpha=0) 869 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 870 ## 871 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 872 ## #print "answer",answer 873 ## 874 ## assert allclose(quantity.vertex_values.ravel(), answer) 875 ## 876 ## 877 ## #Check that values can be set from file using default attribute 878 ## quantity.set_values(filename=txt_file, alpha=0) 879 ## assert allclose(quantity.vertex_values.ravel(), answer) 880 ## 881 ## #Cleanup 882 ## import os 883 ## os.remove(txt_file) 884 ## 885 ## def test_set_values_from_lat_long(self): 886 ## quantity = Quantity(self.mesh_onslow) 887 ## 888 ## #Get (enough) datapoints 889 ## data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 890 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 891 ## 892 ## data_geo_spatial = Geospatial_data(data_points, 893 ## points_are_lats_longs=True) 894 ## points_UTM = data_geo_spatial.get_data_points(absolute=True) 895 ## attributes = linear_function(points_UTM) 896 ## att = 'elevation' 897 ## 898 ## #Create .txt file 899 ## txt_file = tempfile.mktemp(".txt") 900 ## file = open(txt_file,"w") 901 ## file.write(" lat,long," + att + " \n") 902 ## for data_point, attribute in map(None, data_points, attributes): 903 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 904 ## + ',' + str(attribute) 905 ## #print "row", row 906 ## file.write(row + "\n") 907 ## file.close() 908 ## 909 ## 910 ## #Check that values can be set from file 911 ## quantity.set_values(filename=txt_file, 912 ## attribute_name=att, alpha=0) 913 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 914 ## 915 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 916 ## #print "answer",answer 917 ## 918 ## assert allclose(quantity.vertex_values.ravel(), answer) 919 ## 920 ## 921 ## #Check that values can be set from file using default attribute 922 ## quantity.set_values(filename=txt_file, alpha=0) 923 ## assert allclose(quantity.vertex_values.ravel(), answer) 924 ## 925 ## #Cleanup 926 ## import os 927 ## os.remove(txt_file) 928 ## 929 ## def test_set_values_from_UTM_pts(self): 930 ## quantity = Quantity(self.mesh_onslow) 931 ## 932 ## #Get (enough) datapoints 933 ## data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 934 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 935 ## 936 ## data_geo_spatial = Geospatial_data(data_points, 937 ## points_are_lats_longs=True) 938 ## points_UTM = data_geo_spatial.get_data_points(absolute=True) 939 ## attributes = linear_function(points_UTM) 940 ## att = 'elevation' 941 ## 942 ## #Create .txt file 943 ## txt_file = tempfile.mktemp(".txt") 944 ## file = open(txt_file,"w") 945 ## file.write(" x,y," + att + " \n") 946 ## for data_point, attribute in map(None, points_UTM, attributes): 947 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 948 ## + ',' + str(attribute) 949 ## #print "row", row 950 ## file.write(row + "\n") 951 ## file.close() 952 ## 953 ## 954 ## pts_file = tempfile.mktemp(".pts") 955 ## convert = Geospatial_data(txt_file) 956 ## convert.export_points_file(pts_file) 957 ## 958 ## #Check that values can be set from file 959 ## quantity.set_values_from_file(pts_file, att, 0, 960 ## 'vertices', None) 961 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 962 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 963 ## #print "answer",answer 964 ## assert allclose(quantity.vertex_values.ravel(), answer) 965 ## 966 ## #Check that values can be set from file 967 ## quantity.set_values(filename=pts_file, 968 ## attribute_name=att, alpha=0) 969 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 970 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 971 ## #print "answer",answer 972 ## assert allclose(quantity.vertex_values.ravel(), answer) 973 ## 974 ## 975 ## #Check that values can be set from file using default attribute 976 ## quantity.set_values(filename=txt_file, alpha=0) 977 ## assert allclose(quantity.vertex_values.ravel(), answer) 978 ## 979 ## #Cleanup 980 ## import os 981 ## os.remove(txt_file) 982 ## os.remove(pts_file) 983 ## 984 ## def verbose_test_set_values_from_UTM_pts(self): 985 ## quantity = Quantity(self.mesh_onslow) 986 ## 987 ## #Get (enough) datapoints 988 ## data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 989 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 990 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 991 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 992 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 993 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 994 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 995 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 996 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 997 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 998 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 999 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1000 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1001 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1002 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1003 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1004 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1005 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1006 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1007 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1008 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1009 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1010 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1011 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1012 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1013 ## [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1014 ## [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1015 ## ] 1016 ## 1017 ## data_geo_spatial = Geospatial_data(data_points, 1018 ## points_are_lats_longs=True) 1019 ## points_UTM = data_geo_spatial.get_data_points(absolute=True) 1020 ## attributes = linear_function(points_UTM) 1021 ## att = 'elevation' 1022 ## 1023 ## #Create .txt file 1024 ## txt_file = tempfile.mktemp(".txt") 1025 ## file = open(txt_file,"w") 1026 ## file.write(" x,y," + att + " \n") 1027 ## for data_point, attribute in map(None, points_UTM, attributes): 1028 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 1029 ## + ',' + str(attribute) 1030 ## #print "row", row 1031 ## file.write(row + "\n") 1032 ## file.close() 1033 ## 1034 ## 1035 ## pts_file = tempfile.mktemp(".pts") 1036 ## convert = Geospatial_data(txt_file) 1037 ## convert.export_points_file(pts_file) 1038 ## 1039 ## #Check that values can be set from file 1040 ## quantity.set_values_from_file(pts_file, att, 0, 1041 ## 'vertices', None, verbose = True, 1042 ## max_read_lines=2) 1043 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 1044 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 1045 ## #print "answer",answer 1046 ## assert allclose(quantity.vertex_values.ravel(), answer) 1047 ## 1048 ## #Check that values can be set from file 1049 ## quantity.set_values(filename=pts_file, 1050 ## attribute_name=att, alpha=0) 1051 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 1052 ## #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 1053 ## #print "answer",answer 1054 ## assert allclose(quantity.vertex_values.ravel(), answer) 1055 ## 1056 ## 1057 ## #Check that values can be set from file using default attribute 1058 ## quantity.set_values(filename=txt_file, alpha=0) 1059 ## assert allclose(quantity.vertex_values.ravel(), answer) 1060 ## 1061 ## #Cleanup 1062 ## import os 1063 ## os.remove(txt_file) 1064 ## os.remove(pts_file) 1065 ## 1066 ## def test_set_values_from_file_with_georef1(self): 1067 ## 1068 ## #Mesh in zone 56 (absolute coords) 1069 ## 1070 ## x0 = 314036.58727982 1071 ## y0 = 6224951.2960092 1072 ## 1073 ## a = [x0+0.0, y0+0.0] 1074 ## b = [x0+0.0, y0+2.0] 1075 ## c = [x0+2.0, y0+0.0] 1076 ## d = [x0+0.0, y0+4.0] 1077 ## e = [x0+2.0, y0+2.0] 1078 ## f = [x0+4.0, y0+0.0] 1079 ## 1080 ## points = [a, b, c, d, e, f] 1081 ## 1082 ## #bac, bce, ecf, dbe 1083 ## elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 1084 ## 1085 ## #absolute going in .. 1086 ## mesh4 = Domain(points, elements, 1087 ## geo_reference = Geo_reference(56, 0, 0)) 1088 ## mesh4.check_integrity() 1089 ## quantity = Quantity(mesh4) 1090 ## 1091 ## #Get (enough) datapoints (relative to georef) 1092 ## data_points_rel = [[ 0.66666667, 0.66666667], 1093 ## [ 1.33333333, 1.33333333], 1094 ## [ 2.66666667, 0.66666667], 1095 ## [ 0.66666667, 2.66666667], 1096 ## [ 0.0, 1.0], 1097 ## [ 0.0, 3.0], 1098 ## [ 1.0, 0.0], 1099 ## [ 1.0, 1.0], 1100 ## [ 1.0, 2.0], 1101 ## [ 1.0, 3.0], 1102 ## [ 2.0, 1.0], 1103 ## [ 3.0, 0.0], 1104 ## [ 3.0, 1.0]] 1105 ## 1106 ## data_geo_spatial = Geospatial_data(data_points_rel, 1107 ## geo_reference = Geo_reference(56, x0, y0)) 1108 ## data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 1109 ## attributes = linear_function(data_points_absolute) 1110 ## att = 'spam_and_eggs' 1111 ## 1112 ## #Create .txt file 1113 ## ptsfile = tempfile.mktemp(".txt") 1114 ## file = open(ptsfile,"w") 1115 ## file.write(" x,y," + att + " \n") 1116 ## for data_point, attribute in map(None, data_points_absolute 1117 ## ,attributes): 1118 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 1119 ## + ',' + str(attribute) 1120 ## file.write(row + "\n") 1121 ## file.close() 1122 ## 1123 ## #file = open(ptsfile, 'r') 1124 ## #lines = file.readlines() 1125 ## #file.close() 1126 ## 1127 ## 1128 ## #Check that values can be set from file 1129 ## quantity.set_values(filename=ptsfile, 1130 ## attribute_name=att, alpha=0) 1131 ## answer = linear_function(quantity.domain.get_vertex_coordinates()) 1132 ## 1133 ## assert allclose(quantity.vertex_values.ravel(), answer) 1134 ## 1135 ## 1136 ## #Check that values can be set from file using default attribute 1137 ## quantity.set_values(filename=ptsfile, alpha=0) 1138 ## assert allclose(quantity.vertex_values.ravel(), answer) 1139 ## 1140 ## #Cleanup 1141 ## import os 1142 ## os.remove(ptsfile) 1143 ## 1144 ## 1145 ## def test_set_values_from_file_with_georef2(self): 1146 ## 1147 ## #Mesh in zone 56 (relative coords) 1148 ## 1149 ## x0 = 314036.58727982 1150 ## y0 = 6224951.2960092 1151 ## #x0 = 0.0 1152 ## #y0 = 0.0 1153 ## 1154 ## a = [0.0, 0.0] 1155 ## b = [0.0, 2.0] 1156 ## c = [2.0, 0.0] 1157 ## d = [0.0, 4.0] 1158 ## e = [2.0, 2.0] 1159 ## f = [4.0, 0.0] 1160 ## 1161 ## points = [a, b, c, d, e, f] 1162 ## 1163 ## #bac, bce, ecf, dbe 1164 ## elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 1165 ## 1166 ## mesh4 = Domain(points, elements, 1167 ## geo_reference = Geo_reference(56, x0, y0)) 1168 ## mesh4.check_integrity() 1169 ## quantity = Quantity(mesh4) 1170 ## 1171 ## #Get (enough) datapoints 1172 ## data_points = [[ x0+0.66666667, y0+0.66666667], 1173 ## [ x0+1.33333333, y0+1.33333333], 1174 ## [ x0+2.66666667, y0+0.66666667], 1175 ## [ x0+0.66666667, y0+2.66666667], 1176 ## [ x0+0.0, y0+1.0], 1177 ## [ x0+0.0, y0+3.0], 1178 ## [ x0+1.0, y0+0.0], 1179 ## [ x0+1.0, y0+1.0], 1180 ## [ x0+1.0, y0+2.0], 1181 ## [ x0+1.0, y0+3.0], 1182 ## [ x0+2.0, y0+1.0], 1183 ## [ x0+3.0, y0+0.0], 1184 ## [ x0+3.0, y0+1.0]] 1185 ## 1186 ## 1187 ## data_geo_spatial = Geospatial_data(data_points, 1188 ## geo_reference = Geo_reference(56, 0, 0)) 1189 ## data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 1190 ## attributes = linear_function(data_points_absolute) 1191 ## att = 'spam_and_eggs' 1192 ## 1193 ## #Create .txt file 1194 ## ptsfile = tempfile.mktemp(".txt") 1195 ## file = open(ptsfile,"w") 1196 ## file.write(" x,y," + att + " \n") 1197 ## for data_point, attribute in map(None, data_points_absolute 1198 ## ,attributes): 1199 ## row = str(data_point[0]) + ',' + str(data_point[1]) \ 1200 ## + ',' + str(attribute) 1201 ## file.write(row + "\n") 1202 ## file.close() 1203 ## 1204 ## 1205 ## #Check that values can be set from file 1206 ## quantity.set_values(filename=ptsfile, 1207 ## attribute_name=att, alpha=0) 1208 ## answer = linear_function(quantity.domain. \ 1209 ## get_vertex_coordinates(absolute=True)) 1210 ## 1211 ## 1212 ## assert allclose(quantity.vertex_values.ravel(), answer) 1213 ## 1214 ## 1215 ## #Check that values can be set from file using default attribute 1216 ## quantity.set_values(filename=ptsfile, alpha=0) 1217 ## assert allclose(quantity.vertex_values.ravel(), answer) 1218 ## 1219 ## #Cleanup 1220 ## import os 1221 ## os.remove(ptsfile) 1222 ## 1223 ## 1224 ## 1225 ## 1226 ## def test_set_values_from_quantity(self): 1227 ## 1228 ## quantity1 = Quantity(self.mesh4) 1229 ## quantity1.set_vertex_values([0,1,2,3,4,5]) 1230 ## 1231 ## assert allclose(quantity1.vertex_values, 1232 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1233 ## 1234 ## 1235 ## quantity2 = Quantity(self.mesh4) 1236 ## quantity2.set_values(quantity=quantity1) 1237 ## assert allclose(quantity2.vertex_values, 1238 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1239 ## 1240 ## quantity2.set_values(quantity = 2*quantity1) 1241 ## assert allclose(quantity2.vertex_values, 1242 ## [[2,0,4], [2,4,8], [8,4,10], [6,2,8]]) 1243 ## 1244 ## quantity2.set_values(quantity = 2*quantity1 + 3) 1245 ## assert allclose(quantity2.vertex_values, 1246 ## [[5,3,7], [5,7,11], [11,7,13], [9,5,11]]) 1247 ## 1248 ## 1249 ## #Check detection of quantity as first orgument 1250 ## quantity2.set_values(2*quantity1 + 3) 1251 ## assert allclose(quantity2.vertex_values, 1252 ## [[5,3,7], [5,7,11], [11,7,13], [9,5,11]]) 1253 ## 1254 ## 1255 ## 1256 ## def Xtest_set_values_from_quantity_using_polygon(self): 1257 ## """test_set_values_from_quantity_using_polygon(self): 1258 ## 1259 ## Check that polygon can be used to restrict set_values when 1260 ## using another quantity as argument. 1261 ## """ 1262 ## 1263 ## # Create restricting polygon (containing node #4 (2,2) and 1264 ## # centroid of triangle #1 (bce) 1265 ## polygon = [[1.0, 1.0], [4.0, 1.0], 1266 ## [4.0, 4.0], [1.0, 4.0]] 1267 ## assert allclose(inside_polygon(self.mesh4.nodes, polygon), 4) 1268 ## 1269 ## quantity1 = Quantity(self.mesh4) 1270 ## quantity1.set_vertex_values([0,1,2,3,4,5]) 1271 ## 1272 ## assert allclose(quantity1.vertex_values, 1273 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1274 ## 1275 ## 1276 ## quantity2 = Quantity(self.mesh4) 1277 ## quantity2.set_values(quantity=quantity1, 1278 ## polygon=polygon) 1279 ## 1280 ## msg = 'Only node #4(e) at (2,2) should have values applied ' 1281 ## assert allclose(quantity2.vertex_values, 1282 ## [[0,0,0], [0,0,4], [4,0,0], [0,0,4]]), msg 1283 ## #bac, bce, ecf, dbe 1284 ## 1285 ## 1286 ## 1287 ## def test_overloading(self): 1288 ## 1289 ## quantity1 = Quantity(self.mesh4) 1290 ## quantity1.set_vertex_values([0,1,2,3,4,5]) 1291 ## 1292 ## assert allclose(quantity1.vertex_values, 1293 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1294 ## 1295 ## 1296 ## quantity2 = Quantity(self.mesh4) 1297 ## quantity2.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 1298 ## location = 'vertices') 1299 ## 1300 ## 1301 ## 1302 ## quantity3 = Quantity(self.mesh4) 1303 ## quantity3.set_values([[2,2,2], [7,8,9], [7,6,3], [3, 8, -8]], 1304 ## location = 'vertices') 1305 ## 1306 ## 1307 ## # Negation 1308 ## Q = -quantity1 1309 ## assert allclose(Q.vertex_values, -quantity1.vertex_values) 1310 ## assert allclose(Q.centroid_values, -quantity1.centroid_values) 1311 ## assert allclose(Q.edge_values, -quantity1.edge_values) 1312 ## 1313 ## # Addition 1314 ## Q = quantity1 + 7 1315 ## assert allclose(Q.vertex_values, quantity1.vertex_values + 7) 1316 ## assert allclose(Q.centroid_values, quantity1.centroid_values + 7) 1317 ## assert allclose(Q.edge_values, quantity1.edge_values + 7) 1318 ## 1319 ## Q = 7 + quantity1 1320 ## assert allclose(Q.vertex_values, quantity1.vertex_values + 7) 1321 ## assert allclose(Q.centroid_values, quantity1.centroid_values + 7) 1322 ## assert allclose(Q.edge_values, quantity1.edge_values + 7) 1323 ## 1324 ## Q = quantity1 + quantity2 1325 ## assert allclose(Q.vertex_values, 1326 ## quantity1.vertex_values + quantity2.vertex_values) 1327 ## assert allclose(Q.centroid_values, 1328 ## quantity1.centroid_values + quantity2.centroid_values) 1329 ## assert allclose(Q.edge_values, 1330 ## quantity1.edge_values + quantity2.edge_values) 1331 ## 1332 ## 1333 ## Q = quantity1 + quantity2 - 3 1334 ## assert allclose(Q.vertex_values, 1335 ## quantity1.vertex_values + quantity2.vertex_values - 3) 1336 ## 1337 ## Q = quantity1 - quantity2 1338 ## assert allclose(Q.vertex_values, 1339 ## quantity1.vertex_values - quantity2.vertex_values) 1340 ## 1341 ## #Scaling 1342 ## Q = quantity1*3 1343 ## assert allclose(Q.vertex_values, quantity1.vertex_values*3) 1344 ## assert allclose(Q.centroid_values, quantity1.centroid_values*3) 1345 ## assert allclose(Q.edge_values, quantity1.edge_values*3) 1346 ## Q = 3*quantity1 1347 ## assert allclose(Q.vertex_values, quantity1.vertex_values*3) 1348 ## 1349 ## #Multiplication 1350 ## Q = quantity1 * quantity2 1351 ## #print Q.vertex_values 1352 ## #print Q.centroid_values 1353 ## #print quantity1.centroid_values 1354 ## #print quantity2.centroid_values 1355 ## 1356 ## assert allclose(Q.vertex_values, 1357 ## quantity1.vertex_values * quantity2.vertex_values) 1358 ## 1359 ## #Linear combinations 1360 ## Q = 4*quantity1 + 2 1361 ## assert allclose(Q.vertex_values, 1362 ## 4*quantity1.vertex_values + 2) 1363 ## 1364 ## Q = quantity1*quantity2 + 2 1365 ## assert allclose(Q.vertex_values, 1366 ## quantity1.vertex_values * quantity2.vertex_values + 2) 1367 ## 1368 ## Q = quantity1*quantity2 + quantity3 1369 ## assert allclose(Q.vertex_values, 1370 ## quantity1.vertex_values * quantity2.vertex_values + 1371 ## quantity3.vertex_values) 1372 ## Q = quantity1*quantity2 + 3*quantity3 1373 ## assert allclose(Q.vertex_values, 1374 ## quantity1.vertex_values * quantity2.vertex_values + 1375 ## 3*quantity3.vertex_values) 1376 ## Q = quantity1*quantity2 + 3*quantity3 + 5.0 1377 ## assert allclose(Q.vertex_values, 1378 ## quantity1.vertex_values * quantity2.vertex_values + 1379 ## 3*quantity3.vertex_values + 5) 1380 ## 1381 ## Q = quantity1*quantity2 - quantity3 1382 ## assert allclose(Q.vertex_values, 1383 ## quantity1.vertex_values * quantity2.vertex_values - 1384 ## quantity3.vertex_values) 1385 ## Q = 1.5*quantity1*quantity2 - 3*quantity3 + 5.0 1386 ## assert allclose(Q.vertex_values, 1387 ## 1.5*quantity1.vertex_values * quantity2.vertex_values - 1388 ## 3*quantity3.vertex_values + 5) 1389 ## 1390 ## #Try combining quantities and arrays and scalars 1391 ## Q = 1.5*quantity1*quantity2.vertex_values -\ 1392 ## 3*quantity3.vertex_values + 5.0 1393 ## assert allclose(Q.vertex_values, 1394 ## 1.5*quantity1.vertex_values * quantity2.vertex_values - 1395 ## 3*quantity3.vertex_values + 5) 1396 ## 1397 ## 1398 ## #Powers 1399 ## Q = quantity1**2 1400 ## assert allclose(Q.vertex_values, quantity1.vertex_values**2) 1401 ## 1402 ## Q = quantity1**2 +quantity2**2 1403 ## assert allclose(Q.vertex_values, 1404 ## quantity1.vertex_values**2 + \ 1405 ## quantity2.vertex_values**2) 1406 ## 1407 ## Q = (quantity1**2 +quantity2**2)**0.5 1408 ## assert allclose(Q.vertex_values, 1409 ## (quantity1.vertex_values**2 + \ 1410 ## quantity2.vertex_values**2)**0.5) 1411 ## 1412 ## 1413 ## 1414 ## 1415 ## 1416 ## 1417 ## 1418 ## def test_compute_gradient(self): 1419 ## quantity = Quantity(self.mesh4) 1420 ## 1421 ## #Set up for a gradient of (2,0) at mid triangle 1422 ## quantity.set_values([2.0, 4.0, 6.0, 2.0], 1423 ## location = 'centroids') 1424 ## 1425 ## 1426 ## #Gradients 1427 ## quantity.compute_gradients() 1428 ## 1429 ## a = quantity.x_gradient 1430 ## b = quantity.y_gradient 1431 ## #print self.mesh4.centroid_coordinates 1432 ## #print a, b 1433 ## 1434 ## #The central triangle (1) 1435 ## #(using standard gradient based on neigbours controid values) 1436 ## assert allclose(a[1], 2.0) 1437 ## assert allclose(b[1], 0.0) 1438 ## 1439 ## 1440 ## #Left triangle (0) using two point gradient 1441 ## #q0 = q1 + a*(x0-x1) + b*(y0-y1) <=> 1442 ## #2 = 4 + a*(-2/3) + b*(-2/3) 1443 ## assert allclose(a[0] + b[0], 3) 1444 ## #From orthogonality (a*(y0-y1) + b*(x0-x1) == 0) 1445 ## assert allclose(a[0] - b[0], 0) 1446 ## 1447 ## 1448 ## #Right triangle (2) using two point gradient 1449 ## #q2 = q1 + a*(x2-x1) + b*(y2-y1) <=> 1450 ## #6 = 4 + a*(4/3) + b*(-2/3) 1451 ## assert allclose(2*a[2] - b[2], 3) 1452 ## #From orthogonality (a*(y1-y2) + b*(x2-x1) == 0) 1453 ## assert allclose(a[2] + 2*b[2], 0) 1454 ## 1455 ## 1456 ## #Top triangle (3) using two point gradient 1457 ## #q3 = q1 + a*(x3-x1) + b*(y3-y1) <=> 1458 ## #2 = 4 + a*(-2/3) + b*(4/3) 1459 ## assert allclose(a[3] - 2*b[3], 3) 1460 ## #From orthogonality (a*(y1-y3) + b*(x3-x1) == 0) 1461 ## assert allclose(2*a[3] + b[3], 0) 1462 ## 1463 ## 1464 ## 1465 ## #print a, b 1466 ## quantity.extrapolate_second_order() 1467 ## 1468 ## #Apply q(x,y) = qc + a*(x-xc) + b*(y-yc) 1469 ## assert allclose(quantity.vertex_values[0,:], [3., 0., 3.]) 1470 ## assert allclose(quantity.vertex_values[1,:], [4./3, 16./3, 16./3]) 1471 ## 1472 ## 1473 ## #a = 1.2, b=-0.6 1474 ## #q(4,0) = 6 + a*(4 - 8/3) + b*(-2/3) 1475 ## assert allclose(quantity.vertex_values[2,2], 8) 1476 ## 1477 ## def test_get_gradients(self): 1478 ## quantity = Quantity(self.mesh4) 1479 ## 1480 ## #Set up for a gradient of (2,0) at mid triangle 1481 ## quantity.set_values([2.0, 4.0, 6.0, 2.0], 1482 ## location = 'centroids') 1483 ## 1484 ## 1485 ## #Gradients 1486 ## quantity.compute_gradients() 1487 ## 1488 ## a, b = quantity.get_gradients() 1489 ## #print self.mesh4.centroid_coordinates 1490 ## #print a, b 1491 ## 1492 ## #The central triangle (1) 1493 ## #(using standard gradient based on neigbours controid values) 1494 ## assert allclose(a[1], 2.0) 1495 ## assert allclose(b[1], 0.0) 1496 ## 1497 ## 1498 ## #Left triangle (0) using two point gradient 1499 ## #q0 = q1 + a*(x0-x1) + b*(y0-y1) <=> 1500 ## #2 = 4 + a*(-2/3) + b*(-2/3) 1501 ## assert allclose(a[0] + b[0], 3) 1502 ## #From orthogonality (a*(y0-y1) + b*(x0-x1) == 0) 1503 ## assert allclose(a[0] - b[0], 0) 1504 ## 1505 ## 1506 ## #Right triangle (2) using two point gradient 1507 ## #q2 = q1 + a*(x2-x1) + b*(y2-y1) <=> 1508 ## #6 = 4 + a*(4/3) + b*(-2/3) 1509 ## assert allclose(2*a[2] - b[2], 3) 1510 ## #From orthogonality (a*(y1-y2) + b*(x2-x1) == 0) 1511 ## assert allclose(a[2] + 2*b[2], 0) 1512 ## 1513 ## 1514 ## #Top triangle (3) using two point gradient 1515 ## #q3 = q1 + a*(x3-x1) + b*(y3-y1) <=> 1516 ## #2 = 4 + a*(-2/3) + b*(4/3) 1517 ## assert allclose(a[3] - 2*b[3], 3) 1518 ## #From orthogonality (a*(y1-y3) + b*(x3-x1) == 0) 1519 ## assert allclose(2*a[3] + b[3], 0) 1520 ## 1521 ## 1522 ## def test_second_order_extrapolation2(self): 1523 ## quantity = Quantity(self.mesh4) 1524 ## 1525 ## #Set up for a gradient of (3,1), f(x) = 3x+y 1526 ## quantity.set_values([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3], 1527 ## location = 'centroids') 1528 ## 1529 ## #Gradients 1530 ## quantity.compute_gradients() 1531 ## 1532 ## a = quantity.x_gradient 1533 ## b = quantity.y_gradient 1534 ## 1535 ## #print a, b 1536 ## 1537 ## assert allclose(a[1], 3.0) 1538 ## assert allclose(b[1], 1.0) 1539 ## 1540 ## #Work out the others 1541 ## 1542 ## quantity.extrapolate_second_order() 1543 ## 1544 ## #print quantity.vertex_values 1545 ## assert allclose(quantity.vertex_values[1,0], 2.0) 1546 ## assert allclose(quantity.vertex_values[1,1], 6.0) 1547 ## assert allclose(quantity.vertex_values[1,2], 8.0) 1548 ## 1549 ## 1550 ## 1551 ## def test_backup_saxpy_centroid_values(self): 1552 ## quantity = Quantity(self.mesh4) 1553 ## 1554 ## #Set up for a gradient of (3,1), f(x) = 3x+y 1555 ## c_values = array([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3]) 1556 ## d_values = array([1.0, 2.0, 3.0, 4.0]) 1557 ## quantity.set_values(c_values, location = 'centroids') 1558 ## 1559 ## #Backup 1560 ## quantity.backup_centroid_values() 1561 ## 1562 ## #print quantity.vertex_values 1563 ## assert allclose(quantity.centroid_values, quantity.centroid_backup_values) 1564 ## 1565 ## 1566 ## quantity.set_values(d_values, location = 'centroids') 1567 ## 1568 ## quantity.saxpy_centroid_values(2.0, 3.0) 1569 ## 1570 ## assert(quantity.centroid_values, 2.0*d_values + 3.0*c_values) 1571 ## 1572 ## 1573 ## 1574 ## def test_first_order_extrapolator(self): 1575 ## quantity = Quantity(self.mesh4) 1576 ## 1577 ## #Test centroids 1578 ## quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1579 ## assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1580 ## 1581 ## #Extrapolate 1582 ## quantity.extrapolate_first_order() 1583 ## 1584 ## #Check that gradient is zero 1585 ## a,b = quantity.get_gradients() 1586 ## assert allclose(a, [0,0,0,0]) 1587 ## assert allclose(b, [0,0,0,0]) 1588 ## 1589 ## #Check vertices but not edge values 1590 ## assert allclose(quantity.vertex_values, 1591 ## [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) 1592 ## 1593 ## 1594 ## def test_second_order_extrapolator(self): 1595 ## quantity = Quantity(self.mesh4) 1596 ## 1597 ## #Set up for a gradient of (3,0) at mid triangle 1598 ## quantity.set_values([2.0, 4.0, 8.0, 2.0], 1599 ## location = 'centroids') 1600 ## 1601 ## 1602 ## 1603 ## quantity.extrapolate_second_order() 1604 ## quantity.limit() 1605 ## 1606 ## 1607 ## #Assert that central triangle is limited by neighbours 1608 ## assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] 1609 ## assert quantity.vertex_values[1,0] >= quantity.vertex_values[3,1] 1610 ## 1611 ## assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] 1612 ## assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] 1613 ## 1614 ## assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] 1615 ## assert quantity.vertex_values[1,2] >= quantity.vertex_values[3,1] 1616 ## 1617 ## 1618 ## #Assert that quantities are conserved 1619 ### from numpy.oldnumeric import sum 1620 ## from numpy import sum 1621 ## for k in range(quantity.centroid_values.shape[0]): 1622 ## assert allclose (quantity.centroid_values[k], 1623 ## sum(quantity.vertex_values[k,:])/3) 1624 ## 1625 ## 1626 ## 1627 ## 1628 ## 1629 ## def test_limit_vertices_by_all_neighbours(self): 1630 ## quantity = Quantity(self.mesh4) 1631 ## 1632 ## #Create a deliberate overshoot (e.g. from gradient computation) 1633 ## quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1634 ## 1635 ## 1636 ## #Limit 1637 ## quantity.limit_vertices_by_all_neighbours() 1638 ## 1639 ## #Assert that central triangle is limited by neighbours 1640 ## assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] 1641 ## assert quantity.vertex_values[1,0] <= quantity.vertex_values[3,1] 1642 ## 1643 ## assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] 1644 ## assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] 1645 ## 1646 ## assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] 1647 ## assert quantity.vertex_values[1,2] <= quantity.vertex_values[3,1] 1648 ## 1649 ## 1650 ## 1651 ## #Assert that quantities are conserved 1652 ### from numpy.oldnumeric import sum 1653 ## from numpy import sum 1654 ## for k in range(quantity.centroid_values.shape[0]): 1655 ## assert allclose (quantity.centroid_values[k], 1656 ## sum(quantity.vertex_values[k,:])/3) 1657 ## 1658 ## 1659 ## 1660 ## def test_limit_edges_by_all_neighbours(self): 1661 ## quantity = Quantity(self.mesh4) 1662 ## 1663 ## #Create a deliberate overshoot (e.g. from gradient computation) 1664 ## quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1665 ## 1666 ## 1667 ## #Limit 1668 ## quantity.limit_edges_by_all_neighbours() 1669 ## 1670 ## #Assert that central triangle is limited by neighbours 1671 ## assert quantity.edge_values[1,0] <= quantity.centroid_values[2] 1672 ## assert quantity.edge_values[1,0] >= quantity.centroid_values[0] 1673 ## 1674 ## assert quantity.edge_values[1,1] <= quantity.centroid_values[2] 1675 ## assert quantity.edge_values[1,1] >= quantity.centroid_values[0] 1676 ## 1677 ## assert quantity.edge_values[1,2] <= quantity.centroid_values[2] 1678 ## assert quantity.edge_values[1,2] >= quantity.centroid_values[0] 1679 ## 1680 ## 1681 ## 1682 ## #Assert that quantities are conserved 1683 ### from numpy.oldnumeric import sum 1684 ## from numpy import sum 1685 ## for k in range(quantity.centroid_values.shape[0]): 1686 ## assert allclose (quantity.centroid_values[k], 1687 ## sum(quantity.vertex_values[k,:])/3) 1688 ## 1689 ## 1690 ## def test_limit_edges_by_neighbour(self): 1691 ## quantity = Quantity(self.mesh4) 1692 ## 1693 ## #Create a deliberate overshoot (e.g. from gradient computation) 1694 ## quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1695 ## 1696 ## 1697 ## #Limit 1698 ## quantity.limit_edges_by_neighbour() 1699 ## 1700 ## #Assert that central triangle is limited by neighbours 1701 ## assert quantity.edge_values[1,0] <= quantity.centroid_values[3] 1702 ## assert quantity.edge_values[1,0] >= quantity.centroid_values[1] 1703 ## 1704 ## assert quantity.edge_values[1,1] <= quantity.centroid_values[2] 1705 ## assert quantity.edge_values[1,1] >= quantity.centroid_values[1] 1706 ## 1707 ## assert quantity.edge_values[1,2] <= quantity.centroid_values[1] 1708 ## assert quantity.edge_values[1,2] >= quantity.centroid_values[0] 1709 ## 1710 ## 1711 ## 1712 ## #Assert that quantities are conserved 1713 ### from numpy.oldnumeric import sum 1714 ## from numpy import sum 1715 ## for k in range(quantity.centroid_values.shape[0]): 1716 ## assert allclose (quantity.centroid_values[k], 1717 ## sum(quantity.vertex_values[k,:])/3) 1718 ## 1719 ## def test_limiter2(self): 1720 ## """Taken from test_shallow_water 1721 ## """ 1722 ## quantity = Quantity(self.mesh4) 1723 ## quantity.domain.beta_w = 0.9 1724 ## 1725 ## #Test centroids 1726 ## quantity.set_values([2.,4.,8.,2.], location = 'centroids') 1727 ## assert allclose(quantity.centroid_values, [2, 4, 8, 2]) #Centroid 1728 ## 1729 ## 1730 ## #Extrapolate 1731 ## quantity.extrapolate_second_order() 1732 ## 1733 ## assert allclose(quantity.vertex_values[1,:], [0.0, 6, 6]) 1734 ## 1735 ## #Limit 1736 ## quantity.limit() 1737 ## 1738 ## # limited value for beta_w = 0.9 1739 ## 1740 ## assert allclose(quantity.vertex_values[1,:], [2.2, 4.9, 4.9]) 1741 ## # limited values for beta_w = 0.5 1742 ## #assert allclose(quantity.vertex_values[1,:], [3.0, 4.5, 4.5]) 1743 ## 1744 ## 1745 ## #Assert that quantities are conserved 1746 ### from numpy.oldnumeric import sum 1747 ## from numpy import sum 1748 ## for k in range(quantity.centroid_values.shape[0]): 1749 ## assert allclose (quantity.centroid_values[k], 1750 ## sum(quantity.vertex_values[k,:])/3) 1751 ## 1752 ## 1753 ## 1754 ## 1755 ## 1756 ## def test_distribute_first_order(self): 1757 ## quantity = Quantity(self.mesh4) 1758 ## 1759 ## #Test centroids 1760 ## quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1761 ## assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1762 ## 1763 ## 1764 ## #Extrapolate from centroid to vertices and edges 1765 ## quantity.extrapolate_first_order() 1766 ## 1767 ## #Interpolate 1768 ## #quantity.interpolate_from_vertices_to_edges() 1769 ## 1770 ## assert allclose(quantity.vertex_values, 1771 ## [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) 1772 ## assert allclose(quantity.edge_values, [[1,1,1], [2,2,2], 1773 ## [3,3,3], [4, 4, 4]]) 1774 ## 1775 ## 1776 ## def test_interpolate_from_vertices_to_edges(self): 1777 ## quantity = Quantity(self.mesh4) 1778 ## 1779 ## quantity.vertex_values = array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]],float) 1780 ## 1781 ## quantity.interpolate_from_vertices_to_edges() 1782 ## 1783 ## assert allclose(quantity.edge_values, [[1., 1.5, 0.5], 1784 ## [3., 2.5, 1.5], 1785 ## [3.5, 4.5, 3.], 1786 ## [2.5, 3.5, 2]]) 1787 ## 1788 ## 1789 ## def test_interpolate_from_edges_to_vertices(self): 1790 ## quantity = Quantity(self.mesh4) 1791 ## 1792 ## quantity.edge_values = array([[1., 1.5, 0.5], 1793 ## [3., 2.5, 1.5], 1794 ## [3.5, 4.5, 3.], 1795 ## [2.5, 3.5, 2]],float) 1796 ## 1797 ## quantity.interpolate_from_edges_to_vertices() 1798 ## 1799 ## assert allclose(quantity.vertex_values, 1800 ## [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1801 ## 1802 ## 1803 ## 1804 ## def test_distribute_second_order(self): 1805 ## quantity = Quantity(self.mesh4) 1806 ## 1807 ## #Test centroids 1808 ## quantity.set_values([2.,4.,8.,2.], location = 'centroids') 1809 ## assert allclose(quantity.centroid_values, [2, 4, 8, 2]) #Centroid 1810 ## 1811 ## 1812 ## #Extrapolate 1813 ## quantity.extrapolate_second_order() 1814 ## 1815 ## assert allclose(quantity.vertex_values[1,:], [0.0, 6, 6]) 1816 ## 1817 ## 1818 ## def test_update_explicit(self): 1819 ## quantity = Quantity(self.mesh4) 1820 ## 1821 ## #Test centroids 1822 ## quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1823 ## assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1824 ## 1825 ## #Set explicit_update 1826 ## quantity.explicit_update = array( [1.,1.,1.,1.] ) 1827 ## 1828 ## #Update with given timestep 1829 ## quantity.update(0.1) 1830 ## 1831 ## x = array([1, 2, 3, 4]) + array( [.1,.1,.1,.1] ) 1832 ## assert allclose( quantity.centroid_values, x) 1833 ## 1834 ## def test_update_semi_implicit(self): 1835 ## quantity = Quantity(self.mesh4) 1836 ## 1837 ## #Test centroids 1838 ## quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1839 ## assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1840 ## 1841 ## #Set semi implicit update 1842 ## quantity.semi_implicit_update = array([1.,1.,1.,1.]) 1843 ## 1844 ## #Update with given timestep 1845 ## timestep = 0.1 1846 ## quantity.update(timestep) 1847 ## 1848 ## sem = array([1.,1.,1.,1.])/array([1, 2, 3, 4]) 1849 ## denom = ones(4, float)-timestep*sem 1850 ## 1851 ## x = array([1, 2, 3, 4])/denom 1852 ## assert allclose( quantity.centroid_values, x) 1853 ## 1854 ## 1855 ## def test_both_updates(self): 1856 ## quantity = Quantity(self.mesh4) 1857 ## 1858 ## #Test centroids 1859 ## quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1860 ## assert allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1861 ## 1862 ## #Set explicit_update 1863 ## quantity.explicit_update = array( [4.,3.,2.,1.] ) 1864 ## 1865 ## #Set semi implicit update 1866 ## quantity.semi_implicit_update = array( [1.,1.,1.,1.] ) 1867 ## 1868 ## #Update with given timestep 1869 ## timestep = 0.1 1870 ## quantity.update(0.1) 1871 ## 1872 ## sem = array([1.,1.,1.,1.])/array([1, 2, 3, 4]) 1873 ## denom = ones(4, float)-timestep*sem 1874 ## 1875 ## x = array([1., 2., 3., 4.]) 1876 ## x /= denom 1877 ## x += timestep*array( [4.0, 3.0, 2.0, 1.0] ) 1878 ## 1879 ## assert allclose( quantity.centroid_values, x) 1880 ## 1881 ## 1882 ## 1883 ## 1884 ## #Test smoothing 1885 ## def test_smoothing(self): 1886 ## 1887 ## from mesh_factory import rectangular 1888 ## from shallow_water import Domain, Transmissive_boundary 1889 ### from numpy.oldnumeric import zeros, Float 1890 ## from numpy import zeros, float 1891 ## from anuga.utilities.numerical_tools import mean 1892 ## 1893 ## #Create basic mesh 1894 ## points, vertices, boundary = rectangular(2, 2) 1895 ## 1896 ## #Create shallow water domain 1897 ## domain = Domain(points, vertices, boundary) 1898 ## domain.default_order=2 1899 ## domain.reduction = mean 1900 ## 1901 ## 1902 ## #Set some field values 1903 ## domain.set_quantity('elevation', lambda x,y: x) 1904 ## domain.set_quantity('friction', 0.03) 1905 ## 1906 ## 1907 ## ###################### 1908 ## # Boundary conditions 1909 ## B = Transmissive_boundary(domain) 1910 ## domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) 1911 ## 1912 ## 1913 ## ###################### 1914 ## #Initial condition - with jumps 1915 ## 1916 ## bed = domain.quantities['elevation'].vertex_values 1917 ## stage = zeros(bed.shape, float) 1918 ## 1919 ## h = 0.03 1920 ## for i in range(stage.shape[0]): 1921 ## if i % 2 == 0: 1922 ## stage[i,:] = bed[i,:] + h 1923 ## else: 1924 ## stage[i,:] = bed[i,:] 1925 ## 1926 ## domain.set_quantity('stage', stage) 1927 ## 1928 ## stage = domain.quantities['stage'] 1929 ## 1930 ## #Get smoothed stage 1931 ## A, V = stage.get_vertex_values(xy=False, smooth=True) 1932 ## Q = stage.vertex_values 1933 ## 1934 ## 1935 ## assert A.shape[0] == 9 1936 ## assert V.shape[0] == 8 1937 ## assert V.shape[1] == 3 1938 ## 1939 ## #First four points 1940 ## assert allclose(A[0], (Q[0,2] + Q[1,1])/2) 1941 ## assert allclose(A[1], (Q[1,0] + Q[3,1] + Q[2,2])/3) 1942 ## assert allclose(A[2], Q[3,0]) 1943 ## assert allclose(A[3], (Q[0,0] + Q[5,1] + Q[4,2])/3) 1944 ## 1945 ## #Center point 1946 ## assert allclose(A[4], (Q[0,1] + Q[1,2] + Q[2,0] +\ 1947 ## Q[5,0] + Q[6,2] + Q[7,1])/6) 1948 ## 1949 ## 1950 ## #Check V 1951 ## assert allclose(V[0,:], [3,4,0]) 1952 ## assert allclose(V[1,:], [1,0,4]) 1953 ## assert allclose(V[2,:], [4,5,1]) 1954 ## assert allclose(V[3,:], [2,1,5]) 1955 ## assert allclose(V[4,:], [6,7,3]) 1956 ## assert allclose(V[5,:], [4,3,7]) 1957 ## assert allclose(V[6,:], [7,8,4]) 1958 ## assert allclose(V[7,:], [5,4,8]) 1959 ## 1960 ## #Get smoothed stage with XY 1961 ## X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=True) 1962 ## 1963 ## assert allclose(A, A1) 1964 ## assert allclose(V, V1) 1965 ## 1966 ## #Check XY 1967 ## assert allclose(X[4], 0.5) 1968 ## assert allclose(Y[4], 0.5) 1969 ## 1970 ## assert allclose(X[7], 1.0) 1971 ## assert allclose(Y[7], 0.5) 1972 ## 1973 ## 1974 ## 1975 ## 1976 ## def test_vertex_values_no_smoothing(self): 1977 ## 1978 ## from mesh_factory import rectangular 1979 ## from shallow_water import Domain, Transmissive_boundary 1980 ### from numpy.oldnumeric import zeros, Float 1981 ## from numpy import zeros, float 1982 ## from anuga.utilities.numerical_tools import mean 1983 ## 1984 ## 1985 ## #Create basic mesh 1986 ## points, vertices, boundary = rectangular(2, 2) 1987 ## 1988 ## #Create shallow water domain 1989 ## domain = Domain(points, vertices, boundary) 1990 ## domain.default_order=2 1991 ## domain.reduction = mean 1992 ## 1993 ## 1994 ## #Set some field values 1995 ## domain.set_quantity('elevation', lambda x,y: x) 1996 ## domain.set_quantity('friction', 0.03) 1997 ## 1998 ## 1999 ## ###################### 2000 ## #Initial condition - with jumps 2001 ## 2002 ## bed = domain.quantities['elevation'].vertex_values 2003 ## stage = zeros(bed.shape, float) 2004 ## 2005 ## h = 0.03 2006 ## for i in range(stage.shape[0]): 2007 ## if i % 2 == 0: 2008 ## stage[i,:] = bed[i,:] + h 2009 ## else: 2010 ## stage[i,:] = bed[i,:] 2011 ## 2012 ## domain.set_quantity('stage', stage) 2013 ## 2014 ## #Get stage 2015 ## stage = domain.quantities['stage'] 2016 ## A, V = stage.get_vertex_values(xy=False, smooth=False) 2017 ## Q = stage.vertex_values.ravel() 2018 ## 2019 ## for k in range(8): 2020 ## assert allclose(A[k], Q[k]) 2021 ## 2022 ## 2023 ## for k in range(8): 2024 ## assert V[k, 0] == 3*k 2025 ## assert V[k, 1] == 3*k+1 2026 ## assert V[k, 2] == 3*k+2 2027 ## 2028 ## 2029 ## 2030 ## X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=False) 2031 ## 2032 ## 2033 ## assert allclose(A, A1) 2034 ## assert allclose(V, V1) 2035 ## 2036 ## #Check XY 2037 ## assert allclose(X[1], 0.5) 2038 ## assert allclose(Y[1], 0.5) 2039 ## assert allclose(X[4], 0.0) 2040 ## assert allclose(Y[4], 0.0) 2041 ## assert allclose(X[12], 1.0) 2042 ## assert allclose(Y[12], 0.0) 2043 ## 2044 ## 2045 ## 2046 ## def set_array_values_by_index(self): 2047 ## 2048 ## from mesh_factory import rectangular 2049 ## from shallow_water import Domain 2050 ### from numpy.oldnumeric import zeros, Float 2051 ## from numpy import zeros, float 2052 ## 2053 ## #Create basic mesh 2054 ## points, vertices, boundary = rectangular(1, 1) 2055 ## 2056 ## #Create shallow water domain 2057 ## domain = Domain(points, vertices, boundary) 2058 ## #print "domain.number_of_elements ",domain.number_of_elements 2059 ## quantity = Quantity(domain,[[1,1,1],[2,2,2]]) 2060 ## value = [7] 2061 ## indices = [1] 2062 ## quantity.set_array_values_by_index(value, 2063 ## location = 'centroids', 2064 ## indices = indices) 2065 ## #print "quantity.centroid_values",quantity.centroid_values 2066 ## 2067 ## assert allclose(quantity.centroid_values, [1,7]) 2068 ## 2069 ## quantity.set_array_values([15,20,25], indices = indices) 2070 ## assert allclose(quantity.centroid_values, [1,20]) 2071 ## 2072 ## quantity.set_array_values([15,20,25], indices = indices) 2073 ## assert allclose(quantity.centroid_values, [1,20]) 2074 ## 2075 ## def test_setting_some_vertex_values(self): 2076 ## """ 2077 ## set values based on triangle lists. 2078 ## """ 2079 ## from mesh_factory import rectangular 2080 ## from shallow_water import Domain 2081 ### from numpy.oldnumeric import zeros, Float 2082 ## from numpy import zeros, float 2083 ## 2084 ## #Create basic mesh 2085 ## points, vertices, boundary = rectangular(1, 3) 2086 ## #print "vertices",vertices 2087 ## #Create shallow water domain 2088 ## domain = Domain(points, vertices, boundary) 2089 ## #print "domain.number_of_elements ",domain.number_of_elements 2090 ## quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2091 ## [4,4,4],[5,5,5],[6,6,6]]) 2092 ## 2093 ## 2094 ## # Check that constants work 2095 ## value = 7 2096 ## indices = [1] 2097 ## quantity.set_values(value, 2098 ## location = 'centroids', 2099 ## indices = indices) 2100 ## #print "quantity.centroid_values",quantity.centroid_values 2101 ## assert allclose(quantity.centroid_values, [1,7,3,4,5,6]) 2102 ## 2103 ## value = [7] 2104 ## indices = [1] 2105 ## quantity.set_values(value, 2106 ## location = 'centroids', 2107 ## indices = indices) 2108 ## #print "quantity.centroid_values",quantity.centroid_values 2109 ## assert allclose(quantity.centroid_values, [1,7,3,4,5,6]) 2110 ## 2111 ## value = [[15,20,25]] 2112 ## quantity.set_values(value, indices = indices) 2113 ## #print "1 quantity.vertex_values",quantity.vertex_values 2114 ## assert allclose(quantity.vertex_values[1], value[0]) 2115 ## 2116 ## 2117 ## #print "quantity",quantity.vertex_values 2118 ## values = [10,100,50] 2119 ## quantity.set_values(values, indices = [0,1,5], location = 'centroids') 2120 ## #print "2 quantity.vertex_values",quantity.vertex_values 2121 ## assert allclose(quantity.vertex_values[0], [10,10,10]) 2122 ## assert allclose(quantity.vertex_values[5], [50,50,50]) 2123 ## #quantity.interpolate() 2124 ## #print "quantity.centroid_values",quantity.centroid_values 2125 ## assert allclose(quantity.centroid_values, [10,100,3,4,5,50]) 2126 ## 2127 ## 2128 ## quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2129 ## [4,4,4],[5,5,5],[6,6,6]]) 2130 ## values = [10,100,50] 2131 ## #this will be per unique vertex, indexing the vertices 2132 ## #print "quantity.vertex_values",quantity.vertex_values 2133 ## quantity.set_values(values, indices = [0,1,5]) 2134 ## #print "quantity.vertex_values",quantity.vertex_values 2135 ## assert allclose(quantity.vertex_values[0], [1,50,10]) 2136 ## assert allclose(quantity.vertex_values[5], [6,6,6]) 2137 ## assert allclose(quantity.vertex_values[1], [100,10,50]) 2138 ## 2139 ## quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2140 ## [4,4,4],[5,5,5],[6,6,6]]) 2141 ## values = [[31,30,29],[400,400,400],[1000,999,998]] 2142 ## quantity.set_values(values, indices = [3,3,5]) 2143 ## quantity.interpolate() 2144 ## assert allclose(quantity.centroid_values, [1,2,3,400,5,999]) 2145 ## 2146 ## values = [[1,1,1],[2,2,2],[3,3,3], 2147 ## [4,4,4],[5,5,5],[6,6,6]] 2148 ## quantity.set_values(values) 2149 ## 2150 ## # testing the standard set values by vertex 2151 ## # indexed by vertex_id in general_mesh.coordinates 2152 ## values = [0,1,2,3,4,5,6,7] 2153 ## 2154 ## quantity.set_values(values) 2155 ## #print "1 quantity.vertex_values",quantity.vertex_values 2156 ## assert allclose(quantity.vertex_values,[[ 4., 5., 0.], 2157 ## [ 1., 0., 5.], 2158 ## [ 5., 6., 1.], 2159 ## [ 2., 1., 6.], 2160 ## [ 6., 7., 2.], 2161 ## [ 3., 2., 7.]]) 2162 ## 2163 ## def test_setting_unique_vertex_values(self): 2164 ## """ 2165 ## set values based on unique_vertex lists. 2166 ## """ 2167 ## from mesh_factory import rectangular 2168 ## from shallow_water import Domain 2169 ### from numpy.oldnumeric import zeros, Float 2170 ## from numpy import zeros, float 2171 ## 2172 ## #Create basic mesh 2173 ## points, vertices, boundary = rectangular(1, 3) 2174 ## #print "vertices",vertices 2175 ## #Create shallow water domain 2176 ## domain = Domain(points, vertices, boundary) 2177 ## #print "domain.number_of_elements ",domain.number_of_elements 2178 ## quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2179 ## [4,4,4],[5,5,5]]) 2180 ## value = 7 2181 ## indices = [1,5] 2182 ## quantity.set_values(value, 2183 ## location = 'unique vertices', 2184 ## indices = indices) 2185 ## #print "quantity.centroid_values",quantity.centroid_values 2186 ## assert allclose(quantity.vertex_values[0], [0,7,0]) 2187 ## assert allclose(quantity.vertex_values[1], [7,1,7]) 2188 ## assert allclose(quantity.vertex_values[2], [7,2,7]) 2189 ## 2190 ## 2191 ## def test_get_values(self): 2192 ## """ 2193 ## get values based on triangle lists. 2194 ## """ 2195 ## from mesh_factory import rectangular 2196 ## from shallow_water import Domain 2197 ### from numpy.oldnumeric import zeros, Float 2198 ## from numpy import zeros, float 2199 ## 2200 ## #Create basic mesh 2201 ## points, vertices, boundary = rectangular(1, 3) 2202 ## 2203 ## #print "points",points 2204 ## #print "vertices",vertices 2205 ## #print "boundary",boundary 2206 ## 2207 ## #Create shallow water domain 2208 ## domain = Domain(points, vertices, boundary) 2209 ## #print "domain.number_of_elements ",domain.number_of_elements 2210 ## quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2211 ## [4,4,4],[5,5,5]]) 2212 ## 2213 ## #print "quantity.get_values(location = 'unique vertices')", \ 2214 ## # quantity.get_values(location = 'unique vertices') 2215 ## 2216 ## #print "quantity.get_values(location = 'unique vertices')", \ 2217 ## # quantity.get_values(indices=[0,1,2,3,4,5,6,7], \ 2218 ## # location = 'unique vertices') 2219 ## 2220 ## answer = [0.5,2,4,5,0,1,3,4.5] 2221 ## assert allclose(answer, 2222 ## quantity.get_values(location = 'unique vertices')) 2223 ## 2224 ## indices = [0,5,3] 2225 ## answer = [0.5,1,5] 2226 ## assert allclose(answer, 2227 ## quantity.get_values(indices=indices, \ 2228 ## location = 'unique vertices')) 2229 ## #print "quantity.centroid_values",quantity.centroid_values 2230 ## #print "quantity.get_values(location = 'centroids') ",\ 2231 ## # quantity.get_values(location = 'centroids') 2232 ## 2233 ## 2234 ## 2235 ## 2236 ## def test_get_values_2(self): 2237 ## """Different mesh (working with domain object) - also check centroids. 2238 ## """ 2239 ## 2240 ## 2241 ## a = [0.0, 0.0] 2242 ## b = [0.0, 2.0] 2243 ## c = [2.0,0.0] 2244 ## d = [0.0, 4.0] 2245 ## e = [2.0, 2.0] 2246 ## f = [4.0,0.0] 2247 ## 2248 ## points = [a, b, c, d, e, f] 2249 ## #bac, bce, ecf, dbe 2250 ## vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2251 ## 2252 ## domain = Domain(points, vertices) 2253 ## 2254 ## quantity = Quantity(domain) 2255 ## quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2256 ## 2257 ## assert allclose(quantity.get_values(location='centroids'), [2,4,4,6]) 2258 ## assert allclose(quantity.get_values(location='centroids', indices=[1,3]), [4,6]) 2259 ## 2260 ## 2261 ## assert allclose(quantity.get_values(location='vertices'), [[4,0,2], 2262 ## [4,2,6], 2263 ## [6,2,4], 2264 ## [8,4,6]]) 2265 ## 2266 ## assert allclose(quantity.get_values(location='vertices', indices=[1,3]), [[4,2,6], 2267 ## [8,4,6]]) 2268 ## 2269 ## 2270 ## assert allclose(quantity.get_values(location='edges'), [[1,3,2], 2271 ## [4,5,3], 2272 ## [3,5,4], 2273 ## [5,7,6]]) 2274 ## assert allclose(quantity.get_values(location='edges', indices=[1,3]), 2275 ## [[4,5,3], 2276 ## [5,7,6]]) 2277 ## 2278 ## # Check averaging over vertices 2279 ## #a: 0 2280 ## #b: (4+4+4)/3 2281 ## #c: (2+2+2)/3 2282 ## #d: 8 2283 ## #e: (6+6+6)/3 2284 ## #f: 4 2285 ## assert(quantity.get_values(location='unique vertices'), [0, 4, 2, 8, 6, 4]) 2286 ## 2287 ## 2288 ## 2289 ## 2290 ## 2291 ## 2292 ## def test_get_interpolated_values(self): 2293 ## 2294 ## from mesh_factory import rectangular 2295 ## from shallow_water import Domain 2296 ### from numpy.oldnumeric import zeros, Float 2297 ## from numpy import zeros, float 2298 ## 2299 ## #Create basic mesh 2300 ## points, vertices, boundary = rectangular(1, 3) 2301 ## domain = Domain(points, vertices, boundary) 2302 ## 2303 ## #Constant values 2304 ## quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2305 ## [4,4,4],[5,5,5]]) 2306 ## 2307 ## 2308 ## 2309 ## # Get interpolated values at centroids 2310 ## interpolation_points = domain.get_centroid_coordinates() 2311 ## answer = quantity.get_values(location='centroids') 2312 ## 2313 ## 2314 ## #print quantity.get_values(points=interpolation_points) 2315 ## assert allclose(answer, quantity.get_values(interpolation_points=interpolation_points)) 2316 ## 2317 ## 2318 ## #Arbitrary values 2319 ## quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], 2320 ## [1,4,-9],[2,5,0]]) 2321 ## 2322 ## 2323 ## # Get interpolated values at centroids 2324 ## interpolation_points = domain.get_centroid_coordinates() 2325 ## answer = quantity.get_values(location='centroids') 2326 ## #print answer 2327 ## #print quantity.get_values(interpolation_points=interpolation_points) 2328 ## assert allclose(answer, quantity.get_values(interpolation_points=interpolation_points, 2329 ## verbose=False)) 2330 ## 2331 ## 2332 ## #FIXME TODO 2333 ## #indices = [0,5,3] 2334 ## #answer = [0.5,1,5] 2335 ## #assert allclose(answer, 2336 ## # quantity.get_values(indices=indices, \ 2337 ## # location = 'unique vertices')) 2338 ## 2339 ## 2340 ## 2341 ## 2342 ## def test_get_interpolated_values_2(self): 2343 ## a = [0.0, 0.0] 2344 ## b = [0.0, 2.0] 2345 ## c = [2.0,0.0] 2346 ## d = [0.0, 4.0] 2347 ## e = [2.0, 2.0] 2348 ## f = [4.0,0.0] 2349 ## 2350 ## points = [a, b, c, d, e, f] 2351 ## #bac, bce, ecf, dbe 2352 ## vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2353 ## 2354 ## domain = Domain(points, vertices) 2355 ## 2356 ## quantity = Quantity(domain) 2357 ## quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2358 ## 2359 ## #First pick one point 2360 ## x, y = 2.0/3, 8.0/3 2361 ## v = quantity.get_values(interpolation_points = [[x,y]]) 2362 ## assert allclose(v, 6) 2363 ## 2364 ## # Then another to test that algorithm won't blindly 2365 ## # reuse interpolation matrix 2366 ## x, y = 4.0/3, 4.0/3 2367 ## v = quantity.get_values(interpolation_points = [[x,y]]) 2368 ## assert allclose(v, 4) 2369 ## 2370 ## 2371 ## 2372 ## def test_get_interpolated_values_with_georef(self): 2373 ## 2374 ## zone = 56 2375 ## xllcorner = 308500 2376 ## yllcorner = 6189000 2377 ## a = [0.0, 0.0] 2378 ## b = [0.0, 2.0] 2379 ## c = [2.0,0.0] 2380 ## d = [0.0, 4.0] 2381 ## e = [2.0, 2.0] 2382 ## f = [4.0,0.0] 2383 ## 2384 ## points = [a, b, c, d, e, f] 2385 ## #bac, bce, ecf, dbe 2386 ## vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2387 ## 2388 ## domain = Domain(points, vertices, 2389 ## geo_reference=Geo_reference(zone,xllcorner,yllcorner)) 2390 ## 2391 ## quantity = Quantity(domain) 2392 ## quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2393 ## 2394 ## #First pick one point (and turn it into absolute coordinates) 2395 ## x, y = 2.0/3, 8.0/3 2396 ## v = quantity.get_values(interpolation_points = [[x+xllcorner,y+yllcorner]]) 2397 ## assert allclose(v, 6) 2398 ## 2399 ## 2400 ## # Then another to test that algorithm won't blindly 2401 ## # reuse interpolation matrix 2402 ## x, y = 4.0/3, 4.0/3 2403 ## v = quantity.get_values(interpolation_points = [[x+xllcorner,y+yllcorner]]) 2404 ## assert allclose(v, 4) 2405 ## 2406 ## # Try two points 2407 ## pts = [[2.0/3 + xllcorner, 8.0/3 + yllcorner], 2408 ## [4.0/3 + xllcorner, 4.0/3 + yllcorner]] 2409 ## v = quantity.get_values(interpolation_points=pts) 2410 ## assert allclose(v, [6, 4]) 2411 ## 2412 ## # Test it using the geospatial data format with absolute input points and default georef 2413 ## pts = Geospatial_data(data_points=pts) 2414 ## v = quantity.get_values(interpolation_points=pts) 2415 ## assert allclose(v, [6, 4]) 2416 ## 2417 ## 2418 ## # Test it using the geospatial data format with relative input points 2419 ## pts = Geospatial_data(data_points=[[2.0/3, 8.0/3], [4.0/3, 4.0/3]], 2420 ## geo_reference=Geo_reference(zone,xllcorner,yllcorner)) 2421 ## v = quantity.get_values(interpolation_points=pts) 2422 ## assert allclose(v, [6, 4]) 2423 ## 2424 ## 2425 ## 2426 ## 2427 ## def test_getting_some_vertex_values(self): 2428 ## """ 2429 ## get values based on triangle lists. 2430 ## """ 2431 ## from mesh_factory import rectangular 2432 ## from shallow_water import Domain 2433 ### from numpy.oldnumeric import zeros, Float 2434 ## from numpy import zeros, float 2435 ## 2436 ## #Create basic mesh 2437 ## points, vertices, boundary = rectangular(1, 3) 2438 ## 2439 ## #print "points",points 2440 ## #print "vertices",vertices 2441 ## #print "boundary",boundary 2442 ## 2443 ## #Create shallow water domain 2444 ## domain = Domain(points, vertices, boundary) 2445 ## #print "domain.number_of_elements ",domain.number_of_elements 2446 ## quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2447 ## [4,4,4],[5,5,5],[6,6,6]]) 2448 ## value = [7] 2449 ## indices = [1] 2450 ## quantity.set_values(value, 2451 ## location = 'centroids', 2452 ## indices = indices) 2453 ## #print "quantity.centroid_values",quantity.centroid_values 2454 ## #print "quantity.get_values(location = 'centroids') ",\ 2455 ## # quantity.get_values(location = 'centroids') 2456 ## assert allclose(quantity.centroid_values, 2457 ## quantity.get_values(location = 'centroids')) 2458 ## 2459 ## 2460 ## value = [[15,20,25]] 2461 ## quantity.set_values(value, indices = indices) 2462 ## #print "1 quantity.vertex_values",quantity.vertex_values 2463 ## assert allclose(quantity.vertex_values, quantity.get_values()) 2464 ## 2465 ## assert allclose(quantity.edge_values, 2466 ## quantity.get_values(location = 'edges')) 2467 ## 2468 ## # get a subset of elements 2469 ## subset = quantity.get_values(location='centroids', indices=[0,5]) 2470 ## answer = [quantity.centroid_values[0],quantity.centroid_values[5]] 2471 ## assert allclose(subset, answer) 2472 ## 2473 ## 2474 ## subset = quantity.get_values(location='edges', indices=[0,5]) 2475 ## answer = [quantity.edge_values[0],quantity.edge_values[5]] 2476 ## #print "subset",subset 2477 ## #print "answer",answer 2478 ## assert allclose(subset, answer) 2479 ## 2480 ## subset = quantity.get_values( indices=[1,5]) 2481 ## answer = [quantity.vertex_values[1],quantity.vertex_values[5]] 2482 ## #print "subset",subset 2483 ## #print "answer",answer 2484 ## assert allclose(subset, answer) 2485 ## 2486 ## def test_smooth_vertex_values(self): 2487 ## """ 2488 ## get values based on triangle lists. 2489 ## """ 2490 ## from mesh_factory import rectangular 2491 ## from shallow_water import Domain 2492 ### from numpy.oldnumeric import zeros, Float 2493 ## from numpy import zeros, float 2494 ## 2495 ## #Create basic mesh 2496 ## points, vertices, boundary = rectangular(2, 2) 2497 ## 2498 ## #print "points",points 2499 ## #print "vertices",vertices 2500 ## #print "boundary",boundary 2501 ## 2502 ## #Create shallow water domain 2503 ## domain = Domain(points, vertices, boundary) 2504 ## #print "domain.number_of_elements ",domain.number_of_elements 2505 ## quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2506 ## [4,4,4],[5,5,5],[6,6,6],[7,7,7]]) 2507 ## 2508 ## #print "quantity.get_values(location = 'unique vertices')", \ 2509 ## # quantity.get_values(location = 'unique vertices') 2510 ## 2511 ## #print "quantity.get_values(location = 'unique vertices')", \ 2512 ## # quantity.get_values(indices=[0,1,2,3,4,5,6,7], \ 2513 ## # location = 'unique vertices') 2514 ## 2515 ## #print quantity.get_values(location = 'unique vertices') 2516 ## #print quantity.domain.number_of_triangles_per_node 2517 ## #print quantity.vertex_values 2518 ## 2519 ## #answer = [0.5, 2, 3, 3, 3.5, 4, 4, 5, 6.5] 2520 ## #assert allclose(answer, 2521 ## # quantity.get_values(location = 'unique vertices')) 2522 ## 2523 ## quantity.smooth_vertex_values() 2524 ## 2525 ## #print quantity.vertex_values 2526 ## 2527 ## 2528 ## answer_vertex_values = [[3,3.5,0.5],[2,0.5,3.5],[3.5,4,2],[3,2,4], 2529 ## [4,5,3],[3.5,3,5],[5,6.5,3.5],[4,3.5,6.5]] 2530 ## 2531 ## assert allclose(answer_vertex_values, 2532 ## quantity.vertex_values) 2533 ## #print "quantity.centroid_values",quantity.centroid_values 2534 ## #print "quantity.get_values(location = 'centroids') ",\ 2535 ## # quantity.get_values(location = 'centroids') 155 assert numpy.allclose(v, 0) 156 157 158 def test_get_maximum_2(self): 159 160 a = [0.0, 0.0] 161 b = [0.0, 2.0] 162 c = [2.0,0.0] 163 d = [0.0, 4.0] 164 e = [2.0, 2.0] 165 f = [4.0,0.0] 166 167 points = [a, b, c, d, e, f] 168 #bac, bce, ecf, dbe 169 vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 170 171 domain = Domain(points, vertices) 172 173 quantity = Quantity(domain) 174 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 175 176 v = quantity.get_maximum_value() 177 assert v == 6 178 179 v = quantity.get_minimum_value() 180 assert v == 2 181 182 i = quantity.get_maximum_index() 183 assert i == 3 184 185 i = quantity.get_minimum_index() 186 assert i == 0 187 188 x,y = quantity.get_maximum_location() 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 numpy.allclose(v, 6) 195 196 x,y = quantity.get_minimum_location() 197 v = quantity.get_values(interpolation_points = [[x,y]]) 198 assert numpy.allclose(v, 2) 199 200 #Multiple locations for maximum - 201 #Test that the algorithm picks the first occurrence 202 v = quantity.get_maximum_value(indices=[0,1,2]) 203 assert numpy.allclose(v, 4) 204 205 i = quantity.get_maximum_index(indices=[0,1,2]) 206 assert i == 1 207 208 x,y = quantity.get_maximum_location(indices=[0,1,2]) 209 xref, yref = 4.0/3, 4.0/3 210 assert x == xref 211 assert y == yref 212 213 v = quantity.get_values(interpolation_points = [[x,y]]) 214 assert numpy.allclose(v, 4) 215 216 # More test of indices...... 217 v = quantity.get_maximum_value(indices=[2,3]) 218 assert numpy.allclose(v, 6) 219 220 i = quantity.get_maximum_index(indices=[2,3]) 221 assert i == 3 222 223 x,y = quantity.get_maximum_location(indices=[2,3]) 224 xref, yref = 2.0/3, 8.0/3 225 assert x == xref 226 assert y == yref 227 228 v = quantity.get_values(interpolation_points = [[x,y]]) 229 assert numpy.allclose(v, 6) 230 231 232 233 def test_boundary_allocation(self): 234 quantity = Quantity(self.mesh4, 235 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 236 237 assert quantity.boundary_values.shape[0] == len(self.mesh4.boundary) 238 239 240 def test_set_values(self): 241 quantity = Quantity(self.mesh4) 242 243 244 quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 245 location = 'vertices') 246 assert numpy.allclose(quantity.vertex_values, 247 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 248 assert numpy.allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 249 assert numpy.allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 250 [5., 5., 5.], 251 [4.5, 4.5, 0.], 252 [3.0, -1.5, -1.5]]) 253 254 255 # Test default 256 quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 257 assert numpy.allclose(quantity.vertex_values, 258 [[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]]) 259 assert numpy.allclose(quantity.centroid_values, [2., 5., 3., 0.]) #Centroid 260 assert numpy.allclose(quantity.edge_values, [[2.5, 2.0, 1.5], 261 [5., 5., 5.], 262 [4.5, 4.5, 0.], 263 [3.0, -1.5, -1.5]]) 264 265 # Test centroids 266 quantity.set_values([1,2,3,4], location = 'centroids') 267 assert numpy.allclose(quantity.centroid_values, [1., 2., 3., 4.]) #Centroid 268 269 # Test exceptions 270 try: 271 quantity.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 272 location = 'bas kamel tuba') 273 except: 274 pass 275 276 277 try: 278 quantity.set_values([[1,2,3], [0,0,9]]) 279 except AssertionError: 280 pass 281 except: 282 raise 'should have raised Assertionerror' 283 284 285 286 def test_set_values_const(self): 287 quantity = Quantity(self.mesh4) 288 289 quantity.set_values(1.0, location = 'vertices') 290 assert numpy.allclose(quantity.vertex_values, 291 [[1,1,1], [1,1,1], [1,1,1], [1, 1, 1]]) 292 293 assert numpy.allclose(quantity.centroid_values, [1, 1, 1, 1]) #Centroid 294 assert numpy.allclose(quantity.edge_values, [[1, 1, 1], 295 [1, 1, 1], 296 [1, 1, 1], 297 [1, 1, 1]]) 298 299 300 quantity.set_values(2.0, location = 'centroids') 301 assert numpy.allclose(quantity.centroid_values, [2, 2, 2, 2]) 302 303 304 def test_set_values_func(self): 305 quantity = Quantity(self.mesh4) 306 307 def f(x, y): 308 return x+y 309 310 quantity.set_values(f, location = 'vertices') 311 #print "quantity.vertex_values",quantity.vertex_values 312 assert numpy.allclose(quantity.vertex_values, 313 [[2,0,2], [2,2,4], [4,2,4], [4,2,4]]) 314 assert numpy.allclose(quantity.centroid_values, 315 [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 316 assert numpy.allclose(quantity.edge_values, 317 [[1,2,1], [3,3,2], [3,4,3], [3,4,3]]) 318 319 320 quantity.set_values(f, location = 'centroids') 321 assert numpy.allclose(quantity.centroid_values, 322 [4.0/3, 8.0/3, 10.0/3, 10.0/3]) 323 324 325 def test_integral(self): 326 quantity = Quantity(self.mesh4) 327 328 #Try constants first 329 const = 5 330 quantity.set_values(const, location = 'vertices') 331 #print 'Q', quantity.get_integral() 332 333 assert numpy.allclose(quantity.get_integral(), self.mesh4.get_area() * const) 334 335 #Try with a linear function 336 def f(x, y): 337 return x+y 338 339 quantity.set_values(f, location = 'vertices') 340 341 342 ref_integral = (4.0/3 + 8.0/3 + 10.0/3 + 10.0/3) * 2 343 344 assert numpy.allclose (quantity.get_integral(), ref_integral) 345 346 347 348 def test_set_vertex_values(self): 349 quantity = Quantity(self.mesh4) 350 quantity.set_vertex_values([0,1,2,3,4,5]) 351 352 assert numpy.allclose(quantity.vertex_values, 353 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 354 assert numpy.allclose(quantity.centroid_values, 355 [1., 7./3, 11./3, 8./3]) #Centroid 356 assert numpy.allclose(quantity.edge_values, [[1., 1.5, 0.5], 357 [3., 2.5, 1.5], 358 [3.5, 4.5, 3.], 359 [2.5, 3.5, 2]]) 360 361 362 def test_set_vertex_values_subset(self): 363 quantity = Quantity(self.mesh4) 364 quantity.set_vertex_values([0,1,2,3,4,5]) 365 quantity.set_vertex_values([0,20,30,50], indices = [0,2,3,5]) 366 367 assert numpy.allclose(quantity.vertex_values, 368 [[1,0,20], [1,20,4], [4,20,50], [30,1,4]]) 369 370 371 def test_set_vertex_values_using_general_interface(self): 372 quantity = Quantity(self.mesh4) 373 374 375 quantity.set_values([0,1,2,3,4,5]) 376 377 378 assert numpy.allclose(quantity.vertex_values, 379 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 380 381 #Centroid 382 assert numpy.allclose(quantity.centroid_values, [1., 7./3, 11./3, 8./3]) 383 384 assert numpy.allclose(quantity.edge_values, [[1., 1.5, 0.5], 385 [3., 2.5, 1.5], 386 [3.5, 4.5, 3.], 387 [2.5, 3.5, 2]]) 388 389 390 391 def test_set_vertex_values_using_general_interface_with_subset(self): 392 """test_set_vertex_values_using_general_interface_with_subset(self): 393 394 Test that indices and polygon works (for constants values) 395 """ 396 397 quantity = Quantity(self.mesh4) 398 399 400 quantity.set_values([0,2,3,5], indices=[0,2,3,5]) 401 assert numpy.allclose(quantity.vertex_values, 402 [[0,0,2], [0,2,0], [0,2,5], [3,0,0]]) 403 404 405 # Constant 406 quantity.set_values(0.0) 407 quantity.set_values(3.14, indices=[0,2], location='vertices') 408 409 # Indices refer to triangle numbers 410 assert numpy.allclose(quantity.vertex_values, 411 [[3.14,3.14,3.14], [0,0,0], 412 [3.14,3.14,3.14], [0,0,0]]) 413 414 415 416 # Now try with polygon (pick points where y>2) 417 polygon = [[0,2.1], [4,2.1], [4,7], [0,7]] 418 quantity.set_values(0.0) 419 quantity.set_values(3.14, polygon=polygon) 420 421 assert numpy.allclose(quantity.vertex_values, 422 [[0,0,0], [0,0,0], [0,0,0], 423 [3.14,3.14,3.14]]) 424 425 426 # Another polygon (pick triangle 1 and 2 (rightmost triangles) 427 # using centroids 428 polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 429 quantity.set_values(0.0) 430 quantity.set_values(3.14, location='centroids', polygon=polygon) 431 assert numpy.allclose(quantity.vertex_values, 432 [[0,0,0], 433 [3.14,3.14,3.14], 434 [3.14,3.14,3.14], 435 [0,0,0]]) 436 437 438 # Same polygon now use vertices (default) 439 polygon = [[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]] 440 quantity.set_values(0.0) 441 #print 'Here 2' 442 quantity.set_values(3.14, polygon=polygon) 443 assert numpy.allclose(quantity.vertex_values, 444 [[0,0,0], 445 [3.14,3.14,3.14], 446 [3.14,3.14,3.14], 447 [0,0,0]]) 448 449 450 # Test input checking 451 try: 452 quantity.set_values(3.14, polygon=polygon, indices = [0,2]) 453 except: 454 pass 455 else: 456 msg = 'Should have caught this' 457 raise msg 458 459 460 461 462 463 def test_set_vertex_values_using_general_interface_subset_and_geo(self): 464 """test_set_vertex_values_using_general_interface_with_subset(self): 465 Test that indices and polygon works using georeferencing 466 """ 467 468 quantity = Quantity(self.mesh4) 469 G = Geo_reference(56, 10, 100) 470 quantity.domain.geo_reference = G 471 472 #print quantity.domain.get_nodes(absolute=True) 473 474 475 # Constant 476 quantity.set_values(0.0) 477 quantity.set_values(3.14, indices=[0,2], location='vertices') 478 479 # Indices refer to triangle numbers here - not vertices (why?) 480 assert numpy.allclose(quantity.vertex_values, 481 [[3.14,3.14,3.14], [0,0,0], 482 [3.14,3.14,3.14], [0,0,0]]) 483 484 485 486 # Now try with polygon (pick points where y>2) 487 polygon = numpy.array([[0,2.1], [4,2.1], [4,7], [0,7]]) 488 polygon += [G.xllcorner, G.yllcorner] 489 490 quantity.set_values(0.0) 491 quantity.set_values(3.14, polygon=polygon, location='centroids') 492 493 assert numpy.allclose(quantity.vertex_values, 494 [[0,0,0], [0,0,0], [0,0,0], 495 [3.14,3.14,3.14]]) 496 497 498 # Another polygon (pick triangle 1 and 2 (rightmost triangles) 499 polygon = numpy.array([[2.1, 0.0], [3.5,0.1], [2,2.2], [0.2,2]]) 500 polygon += [G.xllcorner, G.yllcorner] 501 502 quantity.set_values(0.0) 503 quantity.set_values(3.14, polygon=polygon) 504 505 assert numpy.allclose(quantity.vertex_values, 506 [[0,0,0], 507 [3.14,3.14,3.14], 508 [3.14,3.14,3.14], 509 [0,0,0]]) 510 511 512 513 def test_set_values_using_fit(self): 514 515 516 quantity = Quantity(self.mesh4) 517 518 #Get (enough) datapoints 519 data_points = [[ 0.66666667, 0.66666667], 520 [ 1.33333333, 1.33333333], 521 [ 2.66666667, 0.66666667], 522 [ 0.66666667, 2.66666667], 523 [ 0.0, 1.0], 524 [ 0.0, 3.0], 525 [ 1.0, 0.0], 526 [ 1.0, 1.0], 527 [ 1.0, 2.0], 528 [ 1.0, 3.0], 529 [ 2.0, 1.0], 530 [ 3.0, 0.0], 531 [ 3.0, 1.0]] 532 533 z = linear_function(data_points) 534 535 #Use built-in fit_interpolate.fit 536 quantity.set_values( Geospatial_data(data_points, z), alpha = 0 ) 537 #quantity.set_values(points = data_points, values = z, alpha = 0) 538 539 540 answer = linear_function(quantity.domain.get_vertex_coordinates()) 541 #print quantity.vertex_values, answer 542 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 543 544 545 #Now try by setting the same values directly 546 vertex_attributes = fit_to_mesh(data_points, 547 quantity.domain.get_nodes(), 548 quantity.domain.triangles, #FIXME 549 point_attributes=z, 550 alpha = 0, 551 verbose=False) 552 553 #print vertex_attributes 554 quantity.set_values(vertex_attributes) 555 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 556 557 558 559 560 561 def test_test_set_values_using_fit_w_geo(self): 562 563 564 #Mesh 565 vertex_coordinates = [[0.76, 0.76], 566 [0.76, 5.76], 567 [5.76, 0.76]] 568 triangles = [[0,2,1]] 569 570 mesh_georef = Geo_reference(56,-0.76,-0.76) 571 mesh1 = Domain(vertex_coordinates, triangles, 572 geo_reference = mesh_georef) 573 mesh1.check_integrity() 574 575 #Quantity 576 quantity = Quantity(mesh1) 577 578 #Data 579 data_points = [[ 201.0, 401.0], 580 [ 201.0, 403.0], 581 [ 203.0, 401.0]] 582 583 z = [2, 4, 4] 584 585 data_georef = Geo_reference(56,-200,-400) 586 587 588 #Reference 589 ref = fit_to_mesh(data_points, vertex_coordinates, triangles, 590 point_attributes=z, 591 data_origin = data_georef.get_origin(), 592 mesh_origin = mesh_georef.get_origin(), 593 alpha = 0) 594 595 assert numpy.allclose( ref, [0,5,5] ) 596 597 598 #Test set_values 599 600 quantity.set_values( Geospatial_data(data_points, z, data_georef), alpha = 0 ) 601 602 #quantity.set_values(points = data_points, 603 # values = z, 604 # data_georef = data_georef, 605 # alpha = 0) 606 607 608 #quantity.set_values(points = data_points, 609 # values = z, 610 # data_georef = data_georef, 611 # alpha = 0) 612 assert numpy.allclose(quantity.vertex_values.ravel(), ref) 613 614 615 616 #Test set_values using geospatial data object 617 quantity.vertex_values[:] = 0.0 618 619 geo = Geospatial_data(data_points, z, data_georef) 620 621 622 quantity.set_values(geospatial_data = geo, alpha = 0) 623 assert numpy.allclose(quantity.vertex_values.ravel(), ref) 624 625 626 627 def test_set_values_from_file1(self): 628 quantity = Quantity(self.mesh4) 629 630 #Get (enough) datapoints 631 data_points = [[ 0.66666667, 0.66666667], 632 [ 1.33333333, 1.33333333], 633 [ 2.66666667, 0.66666667], 634 [ 0.66666667, 2.66666667], 635 [ 0.0, 1.0], 636 [ 0.0, 3.0], 637 [ 1.0, 0.0], 638 [ 1.0, 1.0], 639 [ 1.0, 2.0], 640 [ 1.0, 3.0], 641 [ 2.0, 1.0], 642 [ 3.0, 0.0], 643 [ 3.0, 1.0]] 644 645 data_geo_spatial = Geospatial_data(data_points, 646 geo_reference = Geo_reference(56, 0, 0)) 647 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 648 attributes = linear_function(data_points_absolute) 649 att = 'spam_and_eggs' 650 651 #Create .txt file 652 ptsfile = tempfile.mktemp(".txt") 653 file = open(ptsfile,"w") 654 file.write(" x,y," + att + " \n") 655 for data_point, attribute in map(None, data_points_absolute 656 ,attributes): 657 row = str(data_point[0]) + ',' + str(data_point[1]) \ 658 + ',' + str(attribute) 659 file.write(row + "\n") 660 file.close() 661 662 663 #Check that values can be set from file 664 quantity.set_values(filename = ptsfile, 665 attribute_name = att, alpha = 0) 666 answer = linear_function(quantity.domain.get_vertex_coordinates()) 667 668 #print quantity.vertex_values.ravel() 669 #print answer 670 671 672 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 673 674 675 #Check that values can be set from file using default attribute 676 quantity.set_values(filename = ptsfile, alpha = 0) 677 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 678 679 #Cleanup 680 import os 681 os.remove(ptsfile) 682 683 684 685 def Xtest_set_values_from_file_using_polygon(self): 686 """test_set_values_from_file_using_polygon(self): 687 688 Test that polygon restriction works for general points data 689 """ 690 691 quantity = Quantity(self.mesh4) 692 693 #Get (enough) datapoints 694 data_points = [[ 0.66666667, 0.66666667], 695 [ 1.33333333, 1.33333333], 696 [ 2.66666667, 0.66666667], 697 [ 0.66666667, 2.66666667], 698 [ 0.0, 1.0], 699 [ 0.0, 3.0], 700 [ 1.0, 0.0], 701 [ 1.0, 1.0], 702 [ 1.0, 2.0], 703 [ 1.0, 3.0], 704 [ 2.0, 1.0], 705 [ 3.0, 0.0], 706 [ 3.0, 1.0]] 707 708 data_geo_spatial = Geospatial_data(data_points, 709 geo_reference = Geo_reference(56, 0, 0)) 710 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 711 attributes = linear_function(data_points_absolute) 712 att = 'spam_and_eggs' 713 714 #Create .txt file 715 ptsfile = tempfile.mktemp(".txt") 716 file = open(ptsfile,"w") 717 file.write(" x,y," + att + " \n") 718 for data_point, attribute in map(None, data_points_absolute 719 ,attributes): 720 row = str(data_point[0]) + ',' + str(data_point[1]) \ 721 + ',' + str(attribute) 722 file.write(row + "\n") 723 file.close() 724 725 # Create restricting polygon (containing node #4 (2,2) and 726 # centroid of triangle #1 (bce) 727 polygon = [[1.0, 1.0], [4.0, 1.0], 728 [4.0, 4.0], [1.0, 4.0]] 729 730 #print self.mesh4.nodes 731 #print inside_polygon(self.mesh4.nodes, polygon) 732 assert numpy.allclose(inside_polygon(self.mesh4.nodes, polygon), 4) 733 734 #print quantity.domain.get_vertex_coordinates() 735 #print quantity.domain.get_nodes() 736 737 # Check that values can be set from file 738 quantity.set_values(filename=ptsfile, 739 polygon=polygon, 740 location='unique vertices', 741 alpha=0) 742 743 # Get indices for vertex coordinates in polygon 744 indices = inside_polygon(quantity.domain.get_vertex_coordinates(), 745 polygon) 746 points = take(quantity.domain.get_vertex_coordinates(), indices) 747 748 answer = linear_function(points) 749 750 #print quantity.vertex_values.ravel() 751 #print answer 752 753 # Check vertices in polygon have been set 754 assert numpy.allclose(take(quantity.vertex_values.ravel(), indices), 755 answer) 756 757 # Check vertices outside polygon are zero 758 indices = outside_polygon(quantity.domain.get_vertex_coordinates(), 759 polygon) 760 assert numpy.allclose(take(quantity.vertex_values.ravel(), indices), 761 0.0) 762 763 #Cleanup 764 import os 765 os.remove(ptsfile) 766 767 768 769 770 def test_cache_test_set_values_from_file(self): 771 # FIXME (Ole): What is this about? 772 # I don't think it checks anything new 773 quantity = Quantity(self.mesh4) 774 775 #Get (enough) datapoints 776 data_points = [[ 0.66666667, 0.66666667], 777 [ 1.33333333, 1.33333333], 778 [ 2.66666667, 0.66666667], 779 [ 0.66666667, 2.66666667], 780 [ 0.0, 1.0], 781 [ 0.0, 3.0], 782 [ 1.0, 0.0], 783 [ 1.0, 1.0], 784 [ 1.0, 2.0], 785 [ 1.0, 3.0], 786 [ 2.0, 1.0], 787 [ 3.0, 0.0], 788 [ 3.0, 1.0]] 789 790 georef = Geo_reference(56, 0, 0) 791 data_geo_spatial = Geospatial_data(data_points, 792 geo_reference=georef) 793 794 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 795 attributes = linear_function(data_points_absolute) 796 att = 'spam_and_eggs' 797 798 # Create .txt file 799 ptsfile = tempfile.mktemp(".txt") 800 file = open(ptsfile,"w") 801 file.write(" x,y," + att + " \n") 802 for data_point, attribute in map(None, data_points_absolute 803 ,attributes): 804 row = str(data_point[0]) + ',' + str(data_point[1]) \ 805 + ',' + str(attribute) 806 file.write(row + "\n") 807 file.close() 808 809 810 # Check that values can be set from file 811 quantity.set_values(filename=ptsfile, 812 attribute_name=att, 813 alpha=0, 814 use_cache=True, 815 verbose=False) 816 answer = linear_function(quantity.domain.get_vertex_coordinates()) 817 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 818 819 820 # Check that values can be set from file using default attribute 821 quantity.set_values(filename=ptsfile, 822 alpha=0) 823 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 824 825 # Check cache 826 quantity.set_values(filename=ptsfile, 827 attribute_name=att, 828 alpha=0, 829 use_cache=True, 830 verbose=False) 831 832 833 #Cleanup 834 import os 835 os.remove(ptsfile) 836 837 def test_set_values_from_lat_long(self): 838 quantity = Quantity(self.mesh_onslow) 839 840 #Get (enough) datapoints 841 data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 842 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 843 844 data_geo_spatial = Geospatial_data(data_points, 845 points_are_lats_longs=True) 846 points_UTM = data_geo_spatial.get_data_points(absolute=True) 847 attributes = linear_function(points_UTM) 848 att = 'elevation' 849 850 #Create .txt file 851 txt_file = tempfile.mktemp(".txt") 852 file = open(txt_file,"w") 853 file.write(" lat,long," + att + " \n") 854 for data_point, attribute in map(None, data_points, attributes): 855 row = str(data_point[0]) + ',' + str(data_point[1]) \ 856 + ',' + str(attribute) 857 #print "row", row 858 file.write(row + "\n") 859 file.close() 860 861 862 #Check that values can be set from file 863 quantity.set_values(filename=txt_file, 864 attribute_name=att, 865 alpha=0) 866 answer = linear_function(quantity.domain.get_vertex_coordinates()) 867 868 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 869 #print "answer",answer 870 871 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 872 873 874 #Check that values can be set from file using default attribute 875 quantity.set_values(filename=txt_file, alpha=0) 876 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 877 878 #Cleanup 879 import os 880 os.remove(txt_file) 881 882 def test_set_values_from_lat_long(self): 883 quantity = Quantity(self.mesh_onslow) 884 885 #Get (enough) datapoints 886 data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 887 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 888 889 data_geo_spatial = Geospatial_data(data_points, 890 points_are_lats_longs=True) 891 points_UTM = data_geo_spatial.get_data_points(absolute=True) 892 attributes = linear_function(points_UTM) 893 att = 'elevation' 894 895 #Create .txt file 896 txt_file = tempfile.mktemp(".txt") 897 file = open(txt_file,"w") 898 file.write(" lat,long," + att + " \n") 899 for data_point, attribute in map(None, data_points, attributes): 900 row = str(data_point[0]) + ',' + str(data_point[1]) \ 901 + ',' + str(attribute) 902 #print "row", row 903 file.write(row + "\n") 904 file.close() 905 906 907 #Check that values can be set from file 908 quantity.set_values(filename=txt_file, 909 attribute_name=att, alpha=0) 910 answer = linear_function(quantity.domain.get_vertex_coordinates()) 911 912 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 913 #print "answer",answer 914 915 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 916 917 918 #Check that values can be set from file using default attribute 919 quantity.set_values(filename=txt_file, alpha=0) 920 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 921 922 #Cleanup 923 import os 924 os.remove(txt_file) 925 926 def test_set_values_from_UTM_pts(self): 927 quantity = Quantity(self.mesh_onslow) 928 929 #Get (enough) datapoints 930 data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 931 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6]] 932 933 data_geo_spatial = Geospatial_data(data_points, 934 points_are_lats_longs=True) 935 points_UTM = data_geo_spatial.get_data_points(absolute=True) 936 attributes = linear_function(points_UTM) 937 att = 'elevation' 938 939 #Create .txt file 940 txt_file = tempfile.mktemp(".txt") 941 file = open(txt_file,"w") 942 file.write(" x,y," + att + " \n") 943 for data_point, attribute in map(None, points_UTM, attributes): 944 row = str(data_point[0]) + ',' + str(data_point[1]) \ 945 + ',' + str(attribute) 946 #print "row", row 947 file.write(row + "\n") 948 file.close() 949 950 951 pts_file = tempfile.mktemp(".pts") 952 convert = Geospatial_data(txt_file) 953 convert.export_points_file(pts_file) 954 955 #Check that values can be set from file 956 quantity.set_values_from_file(pts_file, att, 0, 957 'vertices', None) 958 answer = linear_function(quantity.domain.get_vertex_coordinates()) 959 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 960 #print "answer",answer 961 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 962 963 #Check that values can be set from file 964 quantity.set_values(filename=pts_file, 965 attribute_name=att, alpha=0) 966 answer = linear_function(quantity.domain.get_vertex_coordinates()) 967 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 968 #print "answer",answer 969 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 970 971 972 #Check that values can be set from file using default attribute 973 quantity.set_values(filename=txt_file, alpha=0) 974 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 975 976 #Cleanup 977 import os 978 os.remove(txt_file) 979 os.remove(pts_file) 980 981 def verbose_test_set_values_from_UTM_pts(self): 982 quantity = Quantity(self.mesh_onslow) 983 984 #Get (enough) datapoints 985 data_points = [[-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 986 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 987 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 988 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 989 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 990 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 991 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 992 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 993 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 994 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 995 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 996 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 997 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 998 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 999 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1000 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1001 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1002 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1003 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1004 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1005 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1006 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1007 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1008 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1009 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1010 [-21.5, 114.5],[-21.4, 114.6],[-21.45,114.65], 1011 [-21.35, 114.65],[-21.45, 114.55],[-21.45,114.6], 1012 ] 1013 1014 data_geo_spatial = Geospatial_data(data_points, 1015 points_are_lats_longs=True) 1016 points_UTM = data_geo_spatial.get_data_points(absolute=True) 1017 attributes = linear_function(points_UTM) 1018 att = 'elevation' 1019 1020 #Create .txt file 1021 txt_file = tempfile.mktemp(".txt") 1022 file = open(txt_file,"w") 1023 file.write(" x,y," + att + " \n") 1024 for data_point, attribute in map(None, points_UTM, attributes): 1025 row = str(data_point[0]) + ',' + str(data_point[1]) \ 1026 + ',' + str(attribute) 1027 #print "row", row 1028 file.write(row + "\n") 1029 file.close() 1030 1031 1032 pts_file = tempfile.mktemp(".pts") 1033 convert = Geospatial_data(txt_file) 1034 convert.export_points_file(pts_file) 1035 1036 #Check that values can be set from file 1037 quantity.set_values_from_file(pts_file, att, 0, 1038 'vertices', None, verbose = True, 1039 max_read_lines=2) 1040 answer = linear_function(quantity.domain.get_vertex_coordinates()) 1041 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 1042 #print "answer",answer 1043 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1044 1045 #Check that values can be set from file 1046 quantity.set_values(filename=pts_file, 1047 attribute_name=att, alpha=0) 1048 answer = linear_function(quantity.domain.get_vertex_coordinates()) 1049 #print "quantity.vertex_values.ravel()", quantity.vertex_values.ravel() 1050 #print "answer",answer 1051 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1052 1053 1054 #Check that values can be set from file using default attribute 1055 quantity.set_values(filename=txt_file, alpha=0) 1056 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1057 1058 #Cleanup 1059 import os 1060 os.remove(txt_file) 1061 os.remove(pts_file) 1062 1063 def test_set_values_from_file_with_georef1(self): 1064 1065 #Mesh in zone 56 (absolute coords) 1066 1067 x0 = 314036.58727982 1068 y0 = 6224951.2960092 1069 1070 a = [x0+0.0, y0+0.0] 1071 b = [x0+0.0, y0+2.0] 1072 c = [x0+2.0, y0+0.0] 1073 d = [x0+0.0, y0+4.0] 1074 e = [x0+2.0, y0+2.0] 1075 f = [x0+4.0, y0+0.0] 1076 1077 points = [a, b, c, d, e, f] 1078 1079 #bac, bce, ecf, dbe 1080 elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 1081 1082 #absolute going in .. 1083 mesh4 = Domain(points, elements, 1084 geo_reference = Geo_reference(56, 0, 0)) 1085 mesh4.check_integrity() 1086 quantity = Quantity(mesh4) 1087 1088 #Get (enough) datapoints (relative to georef) 1089 data_points_rel = [[ 0.66666667, 0.66666667], 1090 [ 1.33333333, 1.33333333], 1091 [ 2.66666667, 0.66666667], 1092 [ 0.66666667, 2.66666667], 1093 [ 0.0, 1.0], 1094 [ 0.0, 3.0], 1095 [ 1.0, 0.0], 1096 [ 1.0, 1.0], 1097 [ 1.0, 2.0], 1098 [ 1.0, 3.0], 1099 [ 2.0, 1.0], 1100 [ 3.0, 0.0], 1101 [ 3.0, 1.0]] 1102 1103 data_geo_spatial = Geospatial_data(data_points_rel, 1104 geo_reference = Geo_reference(56, x0, y0)) 1105 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 1106 attributes = linear_function(data_points_absolute) 1107 att = 'spam_and_eggs' 1108 1109 #Create .txt file 1110 ptsfile = tempfile.mktemp(".txt") 1111 file = open(ptsfile,"w") 1112 file.write(" x,y," + att + " \n") 1113 for data_point, attribute in map(None, data_points_absolute 1114 ,attributes): 1115 row = str(data_point[0]) + ',' + str(data_point[1]) \ 1116 + ',' + str(attribute) 1117 file.write(row + "\n") 1118 file.close() 1119 1120 #file = open(ptsfile, 'r') 1121 #lines = file.readlines() 1122 #file.close() 1123 1124 1125 #Check that values can be set from file 1126 quantity.set_values(filename=ptsfile, 1127 attribute_name=att, alpha=0) 1128 answer = linear_function(quantity.domain.get_vertex_coordinates()) 1129 1130 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1131 1132 1133 #Check that values can be set from file using default attribute 1134 quantity.set_values(filename=ptsfile, alpha=0) 1135 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1136 1137 #Cleanup 1138 import os 1139 os.remove(ptsfile) 1140 1141 1142 def test_set_values_from_file_with_georef2(self): 1143 1144 #Mesh in zone 56 (relative coords) 1145 1146 x0 = 314036.58727982 1147 y0 = 6224951.2960092 1148 #x0 = 0.0 1149 #y0 = 0.0 1150 1151 a = [0.0, 0.0] 1152 b = [0.0, 2.0] 1153 c = [2.0, 0.0] 1154 d = [0.0, 4.0] 1155 e = [2.0, 2.0] 1156 f = [4.0, 0.0] 1157 1158 points = [a, b, c, d, e, f] 1159 1160 #bac, bce, ecf, dbe 1161 elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ] 1162 1163 mesh4 = Domain(points, elements, 1164 geo_reference = Geo_reference(56, x0, y0)) 1165 mesh4.check_integrity() 1166 quantity = Quantity(mesh4) 1167 1168 #Get (enough) datapoints 1169 data_points = [[ x0+0.66666667, y0+0.66666667], 1170 [ x0+1.33333333, y0+1.33333333], 1171 [ x0+2.66666667, y0+0.66666667], 1172 [ x0+0.66666667, y0+2.66666667], 1173 [ x0+0.0, y0+1.0], 1174 [ x0+0.0, y0+3.0], 1175 [ x0+1.0, y0+0.0], 1176 [ x0+1.0, y0+1.0], 1177 [ x0+1.0, y0+2.0], 1178 [ x0+1.0, y0+3.0], 1179 [ x0+2.0, y0+1.0], 1180 [ x0+3.0, y0+0.0], 1181 [ x0+3.0, y0+1.0]] 1182 1183 1184 data_geo_spatial = Geospatial_data(data_points, 1185 geo_reference = Geo_reference(56, 0, 0)) 1186 data_points_absolute = data_geo_spatial.get_data_points(absolute=True) 1187 attributes = linear_function(data_points_absolute) 1188 att = 'spam_and_eggs' 1189 1190 #Create .txt file 1191 ptsfile = tempfile.mktemp(".txt") 1192 file = open(ptsfile,"w") 1193 file.write(" x,y," + att + " \n") 1194 for data_point, attribute in map(None, data_points_absolute 1195 ,attributes): 1196 row = str(data_point[0]) + ',' + str(data_point[1]) \ 1197 + ',' + str(attribute) 1198 file.write(row + "\n") 1199 file.close() 1200 1201 1202 #Check that values can be set from file 1203 quantity.set_values(filename=ptsfile, 1204 attribute_name=att, alpha=0) 1205 answer = linear_function(quantity.domain. \ 1206 get_vertex_coordinates(absolute=True)) 1207 1208 1209 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1210 1211 1212 #Check that values can be set from file using default attribute 1213 quantity.set_values(filename=ptsfile, alpha=0) 1214 assert numpy.allclose(quantity.vertex_values.ravel(), answer) 1215 1216 #Cleanup 1217 import os 1218 os.remove(ptsfile) 1219 1220 1221 1222 1223 def test_set_values_from_quantity(self): 1224 1225 quantity1 = Quantity(self.mesh4) 1226 quantity1.set_vertex_values([0,1,2,3,4,5]) 1227 1228 assert numpy.allclose(quantity1.vertex_values, 1229 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1230 1231 1232 quantity2 = Quantity(self.mesh4) 1233 quantity2.set_values(quantity=quantity1) 1234 assert numpy.allclose(quantity2.vertex_values, 1235 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1236 1237 quantity2.set_values(quantity = 2*quantity1) 1238 assert numpy.allclose(quantity2.vertex_values, 1239 [[2,0,4], [2,4,8], [8,4,10], [6,2,8]]) 1240 1241 quantity2.set_values(quantity = 2*quantity1 + 3) 1242 assert numpy.allclose(quantity2.vertex_values, 1243 [[5,3,7], [5,7,11], [11,7,13], [9,5,11]]) 1244 1245 1246 #Check detection of quantity as first orgument 1247 quantity2.set_values(2*quantity1 + 3) 1248 assert numpy.allclose(quantity2.vertex_values, 1249 [[5,3,7], [5,7,11], [11,7,13], [9,5,11]]) 1250 1251 1252 1253 def Xtest_set_values_from_quantity_using_polygon(self): 1254 """test_set_values_from_quantity_using_polygon(self): 1255 1256 Check that polygon can be used to restrict set_values when 1257 using another quantity as argument. 1258 """ 1259 1260 # Create restricting polygon (containing node #4 (2,2) and 1261 # centroid of triangle #1 (bce) 1262 polygon = [[1.0, 1.0], [4.0, 1.0], 1263 [4.0, 4.0], [1.0, 4.0]] 1264 assert numpy.allclose(inside_polygon(self.mesh4.nodes, polygon), 4) 1265 1266 quantity1 = Quantity(self.mesh4) 1267 quantity1.set_vertex_values([0,1,2,3,4,5]) 1268 1269 assert numpy.allclose(quantity1.vertex_values, 1270 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1271 1272 1273 quantity2 = Quantity(self.mesh4) 1274 quantity2.set_values(quantity=quantity1, 1275 polygon=polygon) 1276 1277 msg = 'Only node #4(e) at (2,2) should have values applied ' 1278 assert numpy.allclose(quantity2.vertex_values, 1279 [[0,0,0], [0,0,4], [4,0,0], [0,0,4]]), msg 1280 #bac, bce, ecf, dbe 1281 1282 1283 1284 def test_overloading(self): 1285 1286 quantity1 = Quantity(self.mesh4) 1287 quantity1.set_vertex_values([0,1,2,3,4,5]) 1288 1289 assert numpy.allclose(quantity1.vertex_values, 1290 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1291 1292 1293 quantity2 = Quantity(self.mesh4) 1294 quantity2.set_values([[1,2,3], [5,5,5], [0,0,9], [-6, 3, 3]], 1295 location = 'vertices') 1296 1297 1298 1299 quantity3 = Quantity(self.mesh4) 1300 quantity3.set_values([[2,2,2], [7,8,9], [7,6,3], [3, 8, -8]], 1301 location = 'vertices') 1302 1303 1304 # Negation 1305 Q = -quantity1 1306 assert numpy.allclose(Q.vertex_values, -quantity1.vertex_values) 1307 assert numpy.allclose(Q.centroid_values, -quantity1.centroid_values) 1308 assert numpy.allclose(Q.edge_values, -quantity1.edge_values) 1309 1310 # Addition 1311 Q = quantity1 + 7 1312 assert numpy.allclose(Q.vertex_values, quantity1.vertex_values + 7) 1313 assert numpy.allclose(Q.centroid_values, quantity1.centroid_values + 7) 1314 assert numpy.allclose(Q.edge_values, quantity1.edge_values + 7) 1315 1316 Q = 7 + quantity1 1317 assert numpy.allclose(Q.vertex_values, quantity1.vertex_values + 7) 1318 assert numpy.allclose(Q.centroid_values, quantity1.centroid_values + 7) 1319 assert numpy.allclose(Q.edge_values, quantity1.edge_values + 7) 1320 1321 Q = quantity1 + quantity2 1322 assert numpy.allclose(Q.vertex_values, 1323 quantity1.vertex_values + quantity2.vertex_values) 1324 assert numpy.allclose(Q.centroid_values, 1325 quantity1.centroid_values + quantity2.centroid_values) 1326 assert numpy.allclose(Q.edge_values, 1327 quantity1.edge_values + quantity2.edge_values) 1328 1329 1330 Q = quantity1 + quantity2 - 3 1331 assert numpy.allclose(Q.vertex_values, 1332 quantity1.vertex_values + quantity2.vertex_values - 3) 1333 1334 Q = quantity1 - quantity2 1335 assert numpy.allclose(Q.vertex_values, 1336 quantity1.vertex_values - quantity2.vertex_values) 1337 1338 #Scaling 1339 Q = quantity1*3 1340 assert numpy.allclose(Q.vertex_values, quantity1.vertex_values*3) 1341 assert numpy.allclose(Q.centroid_values, quantity1.centroid_values*3) 1342 assert numpy.allclose(Q.edge_values, quantity1.edge_values*3) 1343 Q = 3*quantity1 1344 assert numpy.allclose(Q.vertex_values, quantity1.vertex_values*3) 1345 1346 #Multiplication 1347 Q = quantity1 * quantity2 1348 #print Q.vertex_values 1349 #print Q.centroid_values 1350 #print quantity1.centroid_values 1351 #print quantity2.centroid_values 1352 1353 assert numpy.allclose(Q.vertex_values, 1354 quantity1.vertex_values * quantity2.vertex_values) 1355 1356 #Linear combinations 1357 Q = 4*quantity1 + 2 1358 assert numpy.allclose(Q.vertex_values, 1359 4*quantity1.vertex_values + 2) 1360 1361 Q = quantity1*quantity2 + 2 1362 assert numpy.allclose(Q.vertex_values, 1363 quantity1.vertex_values * quantity2.vertex_values + 2) 1364 1365 Q = quantity1*quantity2 + quantity3 1366 assert numpy.allclose(Q.vertex_values, 1367 quantity1.vertex_values * quantity2.vertex_values + 1368 quantity3.vertex_values) 1369 Q = quantity1*quantity2 + 3*quantity3 1370 assert numpy.allclose(Q.vertex_values, 1371 quantity1.vertex_values * quantity2.vertex_values + 1372 3*quantity3.vertex_values) 1373 Q = quantity1*quantity2 + 3*quantity3 + 5.0 1374 assert numpy.allclose(Q.vertex_values, 1375 quantity1.vertex_values * quantity2.vertex_values + 1376 3*quantity3.vertex_values + 5) 1377 1378 Q = quantity1*quantity2 - quantity3 1379 assert numpy.allclose(Q.vertex_values, 1380 quantity1.vertex_values * quantity2.vertex_values - 1381 quantity3.vertex_values) 1382 Q = 1.5*quantity1*quantity2 - 3*quantity3 + 5.0 1383 assert numpy.allclose(Q.vertex_values, 1384 1.5*quantity1.vertex_values * quantity2.vertex_values - 1385 3*quantity3.vertex_values + 5) 1386 1387 #Try combining quantities and arrays and scalars 1388 Q = 1.5*quantity1*quantity2.vertex_values - \ 1389 3*quantity3.vertex_values + 5.0 1390 assert numpy.allclose(Q.vertex_values, 1391 1.5*quantity1.vertex_values * quantity2.vertex_values - 1392 3*quantity3.vertex_values + 5) 1393 1394 1395 #Powers 1396 Q = quantity1**2 1397 assert numpy.allclose(Q.vertex_values, quantity1.vertex_values**2) 1398 1399 Q = quantity1**2 +quantity2**2 1400 assert numpy.allclose(Q.vertex_values, 1401 quantity1.vertex_values**2 + 1402 quantity2.vertex_values**2) 1403 1404 Q = (quantity1**2 +quantity2**2)**0.5 1405 assert numpy.allclose(Q.vertex_values, 1406 (quantity1.vertex_values**2 + 1407 quantity2.vertex_values**2)**0.5) 1408 1409 1410 1411 1412 1413 1414 1415 def test_compute_gradient(self): 1416 quantity = Quantity(self.mesh4) 1417 1418 #Set up for a gradient of (2,0) at mid triangle 1419 quantity.set_values([2.0, 4.0, 6.0, 2.0], 1420 location = 'centroids') 1421 1422 1423 #Gradients 1424 quantity.compute_gradients() 1425 1426 a = quantity.x_gradient 1427 b = quantity.y_gradient 1428 #print self.mesh4.centroid_coordinates 1429 #print a, b 1430 1431 #The central triangle (1) 1432 #(using standard gradient based on neigbours controid values) 1433 assert numpy.allclose(a[1], 2.0) 1434 assert numpy.allclose(b[1], 0.0) 1435 1436 1437 #Left triangle (0) using two point gradient 1438 #q0 = q1 + a*(x0-x1) + b*(y0-y1) <=> 1439 #2 = 4 + a*(-2/3) + b*(-2/3) 1440 assert numpy.allclose(a[0] + b[0], 3) 1441 #From orthogonality (a*(y0-y1) + b*(x0-x1) == 0) 1442 assert numpy.allclose(a[0] - b[0], 0) 1443 1444 1445 #Right triangle (2) using two point gradient 1446 #q2 = q1 + a*(x2-x1) + b*(y2-y1) <=> 1447 #6 = 4 + a*(4/3) + b*(-2/3) 1448 assert numpy.allclose(2*a[2] - b[2], 3) 1449 #From orthogonality (a*(y1-y2) + b*(x2-x1) == 0) 1450 assert numpy.allclose(a[2] + 2*b[2], 0) 1451 1452 1453 #Top triangle (3) using two point gradient 1454 #q3 = q1 + a*(x3-x1) + b*(y3-y1) <=> 1455 #2 = 4 + a*(-2/3) + b*(4/3) 1456 assert numpy.allclose(a[3] - 2*b[3], 3) 1457 #From orthogonality (a*(y1-y3) + b*(x3-x1) == 0) 1458 assert numpy.allclose(2*a[3] + b[3], 0) 1459 1460 1461 1462 #print a, b 1463 quantity.extrapolate_second_order() 1464 1465 #Apply q(x,y) = qc + a*(x-xc) + b*(y-yc) 1466 assert numpy.allclose(quantity.vertex_values[0,:], [3., 0., 3.]) 1467 assert numpy.allclose(quantity.vertex_values[1,:], [4./3, 16./3, 16./3]) 1468 1469 1470 #a = 1.2, b=-0.6 1471 #q(4,0) = 6 + a*(4 - 8/3) + b*(-2/3) 1472 assert numpy.allclose(quantity.vertex_values[2,2], 8) 1473 1474 def test_get_gradients(self): 1475 quantity = Quantity(self.mesh4) 1476 1477 #Set up for a gradient of (2,0) at mid triangle 1478 quantity.set_values([2.0, 4.0, 6.0, 2.0], 1479 location = 'centroids') 1480 1481 1482 #Gradients 1483 quantity.compute_gradients() 1484 1485 a, b = quantity.get_gradients() 1486 #print self.mesh4.centroid_coordinates 1487 #print a, b 1488 1489 #The central triangle (1) 1490 #(using standard gradient based on neigbours controid values) 1491 assert numpy.allclose(a[1], 2.0) 1492 assert numpy.allclose(b[1], 0.0) 1493 1494 1495 #Left triangle (0) using two point gradient 1496 #q0 = q1 + a*(x0-x1) + b*(y0-y1) <=> 1497 #2 = 4 + a*(-2/3) + b*(-2/3) 1498 assert numpy.allclose(a[0] + b[0], 3) 1499 #From orthogonality (a*(y0-y1) + b*(x0-x1) == 0) 1500 assert numpy.allclose(a[0] - b[0], 0) 1501 1502 1503 #Right triangle (2) using two point gradient 1504 #q2 = q1 + a*(x2-x1) + b*(y2-y1) <=> 1505 #6 = 4 + a*(4/3) + b*(-2/3) 1506 assert numpy.allclose(2*a[2] - b[2], 3) 1507 #From orthogonality (a*(y1-y2) + b*(x2-x1) == 0) 1508 assert numpy.allclose(a[2] + 2*b[2], 0) 1509 1510 1511 #Top triangle (3) using two point gradient 1512 #q3 = q1 + a*(x3-x1) + b*(y3-y1) <=> 1513 #2 = 4 + a*(-2/3) + b*(4/3) 1514 assert numpy.allclose(a[3] - 2*b[3], 3) 1515 #From orthogonality (a*(y1-y3) + b*(x3-x1) == 0) 1516 assert numpy.allclose(2*a[3] + b[3], 0) 1517 1518 1519 def test_second_order_extrapolation2(self): 1520 quantity = Quantity(self.mesh4) 1521 1522 #Set up for a gradient of (3,1), f(x) = 3x+y 1523 quantity.set_values([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3], 1524 location = 'centroids') 1525 1526 #Gradients 1527 quantity.compute_gradients() 1528 1529 a = quantity.x_gradient 1530 b = quantity.y_gradient 1531 1532 #print a, b 1533 1534 assert numpy.allclose(a[1], 3.0) 1535 assert numpy.allclose(b[1], 1.0) 1536 1537 #Work out the others 1538 1539 quantity.extrapolate_second_order() 1540 1541 #print quantity.vertex_values 1542 assert numpy.allclose(quantity.vertex_values[1,0], 2.0) 1543 assert numpy.allclose(quantity.vertex_values[1,1], 6.0) 1544 assert numpy.allclose(quantity.vertex_values[1,2], 8.0) 1545 1546 1547 1548 def test_backup_saxpy_centroid_values(self): 1549 quantity = Quantity(self.mesh4) 1550 1551 #Set up for a gradient of (3,1), f(x) = 3x+y 1552 c_values = numpy.array([2.0+2.0/3, 4.0+4.0/3, 8.0+2.0/3, 2.0+8.0/3]) 1553 d_values = numpy.array([1.0, 2.0, 3.0, 4.0]) 1554 quantity.set_values(c_values, location = 'centroids') 1555 1556 #Backup 1557 quantity.backup_centroid_values() 1558 1559 #print quantity.vertex_values 1560 assert numpy.allclose(quantity.centroid_values, quantity.centroid_backup_values) 1561 1562 1563 quantity.set_values(d_values, location = 'centroids') 1564 1565 quantity.saxpy_centroid_values(2.0, 3.0) 1566 1567 assert(quantity.centroid_values, 2.0*d_values + 3.0*c_values) 1568 1569 1570 1571 def test_first_order_extrapolator(self): 1572 quantity = Quantity(self.mesh4) 1573 1574 #Test centroids 1575 quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1576 assert numpy.allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1577 1578 #Extrapolate 1579 quantity.extrapolate_first_order() 1580 1581 #Check that gradient is zero 1582 a,b = quantity.get_gradients() 1583 assert numpy.allclose(a, [0,0,0,0]) 1584 assert numpy.allclose(b, [0,0,0,0]) 1585 1586 #Check vertices but not edge values 1587 assert numpy.allclose(quantity.vertex_values, 1588 [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) 1589 1590 1591 def test_second_order_extrapolator(self): 1592 quantity = Quantity(self.mesh4) 1593 1594 #Set up for a gradient of (3,0) at mid triangle 1595 quantity.set_values([2.0, 4.0, 8.0, 2.0], 1596 location = 'centroids') 1597 1598 1599 1600 quantity.extrapolate_second_order() 1601 quantity.limit() 1602 1603 1604 #Assert that central triangle is limited by neighbours 1605 assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] 1606 assert quantity.vertex_values[1,0] >= quantity.vertex_values[3,1] 1607 1608 assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] 1609 assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] 1610 1611 assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] 1612 assert quantity.vertex_values[1,2] >= quantity.vertex_values[3,1] 1613 1614 1615 #Assert that quantities are conserved 1616 for k in range(quantity.centroid_values.shape[0]): 1617 assert numpy.allclose (quantity.centroid_values[k], 1618 numpy.sum(quantity.vertex_values[k,:])/3) 1619 1620 1621 1622 1623 1624 def test_limit_vertices_by_all_neighbours(self): 1625 quantity = Quantity(self.mesh4) 1626 1627 #Create a deliberate overshoot (e.g. from gradient computation) 1628 quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1629 1630 1631 #Limit 1632 quantity.limit_vertices_by_all_neighbours() 1633 1634 #Assert that central triangle is limited by neighbours 1635 assert quantity.vertex_values[1,0] >= quantity.vertex_values[0,0] 1636 assert quantity.vertex_values[1,0] <= quantity.vertex_values[3,1] 1637 1638 assert quantity.vertex_values[1,1] <= quantity.vertex_values[2,1] 1639 assert quantity.vertex_values[1,1] >= quantity.vertex_values[0,2] 1640 1641 assert quantity.vertex_values[1,2] <= quantity.vertex_values[2,0] 1642 assert quantity.vertex_values[1,2] <= quantity.vertex_values[3,1] 1643 1644 1645 1646 #Assert that quantities are conserved 1647 for k in range(quantity.centroid_values.shape[0]): 1648 assert numpy.allclose (quantity.centroid_values[k], 1649 numpy.sum(quantity.vertex_values[k,:])/3) 1650 1651 1652 1653 def test_limit_edges_by_all_neighbours(self): 1654 quantity = Quantity(self.mesh4) 1655 1656 #Create a deliberate overshoot (e.g. from gradient computation) 1657 quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1658 1659 1660 #Limit 1661 quantity.limit_edges_by_all_neighbours() 1662 1663 #Assert that central triangle is limited by neighbours 1664 assert quantity.edge_values[1,0] <= quantity.centroid_values[2] 1665 assert quantity.edge_values[1,0] >= quantity.centroid_values[0] 1666 1667 assert quantity.edge_values[1,1] <= quantity.centroid_values[2] 1668 assert quantity.edge_values[1,1] >= quantity.centroid_values[0] 1669 1670 assert quantity.edge_values[1,2] <= quantity.centroid_values[2] 1671 assert quantity.edge_values[1,2] >= quantity.centroid_values[0] 1672 1673 1674 1675 #Assert that quantities are conserved 1676 for k in range(quantity.centroid_values.shape[0]): 1677 assert numpy.allclose (quantity.centroid_values[k], 1678 numpy.sum(quantity.vertex_values[k,:])/3) 1679 1680 1681 def test_limit_edges_by_neighbour(self): 1682 quantity = Quantity(self.mesh4) 1683 1684 #Create a deliberate overshoot (e.g. from gradient computation) 1685 quantity.set_values([[3,0,3], [2,2,6], [5,3,8], [8,3,5]]) 1686 1687 1688 #Limit 1689 quantity.limit_edges_by_neighbour() 1690 1691 #Assert that central triangle is limited by neighbours 1692 assert quantity.edge_values[1,0] <= quantity.centroid_values[3] 1693 assert quantity.edge_values[1,0] >= quantity.centroid_values[1] 1694 1695 assert quantity.edge_values[1,1] <= quantity.centroid_values[2] 1696 assert quantity.edge_values[1,1] >= quantity.centroid_values[1] 1697 1698 assert quantity.edge_values[1,2] <= quantity.centroid_values[1] 1699 assert quantity.edge_values[1,2] >= quantity.centroid_values[0] 1700 1701 1702 1703 #Assert that quantities are conserved 1704 for k in range(quantity.centroid_values.shape[0]): 1705 assert numpy.allclose (quantity.centroid_values[k], 1706 numpy.sum(quantity.vertex_values[k,:])/3) 1707 1708 def test_limiter2(self): 1709 """Taken from test_shallow_water 1710 """ 1711 quantity = Quantity(self.mesh4) 1712 quantity.domain.beta_w = 0.9 1713 1714 #Test centroids 1715 quantity.set_values([2.,4.,8.,2.], location = 'centroids') 1716 assert numpy.allclose(quantity.centroid_values, [2, 4, 8, 2]) #Centroid 1717 1718 1719 #Extrapolate 1720 quantity.extrapolate_second_order() 1721 1722 assert numpy.allclose(quantity.vertex_values[1,:], [0.0, 6, 6]) 1723 1724 #Limit 1725 quantity.limit() 1726 1727 # limited value for beta_w = 0.9 1728 1729 assert numpy.allclose(quantity.vertex_values[1,:], [2.2, 4.9, 4.9]) 1730 # limited values for beta_w = 0.5 1731 #assert numpy.allclose(quantity.vertex_values[1,:], [3.0, 4.5, 4.5]) 1732 1733 1734 #Assert that quantities are conserved 1735 for k in range(quantity.centroid_values.shape[0]): 1736 assert numpy.allclose (quantity.centroid_values[k], 1737 numpy.sum(quantity.vertex_values[k,:])/3) 1738 1739 1740 1741 1742 1743 def test_distribute_first_order(self): 1744 quantity = Quantity(self.mesh4) 1745 1746 #Test centroids 1747 quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1748 assert numpy.allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1749 1750 1751 #Extrapolate from centroid to vertices and edges 1752 quantity.extrapolate_first_order() 1753 1754 #Interpolate 1755 #quantity.interpolate_from_vertices_to_edges() 1756 1757 assert numpy.allclose(quantity.vertex_values, 1758 [[1,1,1], [2,2,2], [3,3,3], [4, 4, 4]]) 1759 assert numpy.allclose(quantity.edge_values, [[1,1,1], [2,2,2], 1760 [3,3,3], [4, 4, 4]]) 1761 1762 1763 def test_interpolate_from_vertices_to_edges(self): 1764 quantity = Quantity(self.mesh4) 1765 1766 quantity.vertex_values = numpy.array([[1,0,2], [1,2,4], [4,2,5], [3,1,4]], numpy.float) 1767 1768 quantity.interpolate_from_vertices_to_edges() 1769 1770 assert numpy.allclose(quantity.edge_values, [[1., 1.5, 0.5], 1771 [3., 2.5, 1.5], 1772 [3.5, 4.5, 3.], 1773 [2.5, 3.5, 2]]) 1774 1775 1776 def test_interpolate_from_edges_to_vertices(self): 1777 quantity = Quantity(self.mesh4) 1778 1779 quantity.edge_values = numpy.array([[1., 1.5, 0.5], 1780 [3., 2.5, 1.5], 1781 [3.5, 4.5, 3.], 1782 [2.5, 3.5, 2]], numpy.float) 1783 1784 quantity.interpolate_from_edges_to_vertices() 1785 1786 assert numpy.allclose(quantity.vertex_values, 1787 [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]) 1788 1789 1790 1791 def test_distribute_second_order(self): 1792 quantity = Quantity(self.mesh4) 1793 1794 #Test centroids 1795 quantity.set_values([2.,4.,8.,2.], location = 'centroids') 1796 assert numpy.allclose(quantity.centroid_values, [2, 4, 8, 2]) #Centroid 1797 1798 1799 #Extrapolate 1800 quantity.extrapolate_second_order() 1801 1802 assert numpy.allclose(quantity.vertex_values[1,:], [0.0, 6, 6]) 1803 1804 1805 def test_update_explicit(self): 1806 quantity = Quantity(self.mesh4) 1807 1808 #Test centroids 1809 quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1810 assert numpy.allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1811 1812 #Set explicit_update 1813 quantity.explicit_update = numpy.array( [1.,1.,1.,1.] ) 1814 1815 #Update with given timestep 1816 quantity.update(0.1) 1817 1818 x = numpy.array([1, 2, 3, 4]) + numpy.array( [.1,.1,.1,.1] ) 1819 assert numpy.allclose( quantity.centroid_values, x) 1820 1821 def test_update_semi_implicit(self): 1822 quantity = Quantity(self.mesh4) 1823 1824 #Test centroids 1825 quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1826 assert numpy.allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1827 1828 #Set semi implicit update 1829 quantity.semi_implicit_update = numpy.array([1.,1.,1.,1.]) 1830 1831 #Update with given timestep 1832 timestep = 0.1 1833 quantity.update(timestep) 1834 1835 sem = numpy.array([1.,1.,1.,1.])/numpy.array([1, 2, 3, 4]) 1836 denom = numpy.ones(4, numpy.float)-timestep*sem 1837 1838 x = numpy.array([1, 2, 3, 4])/denom 1839 assert numpy.allclose( quantity.centroid_values, x) 1840 1841 1842 def test_both_updates(self): 1843 quantity = Quantity(self.mesh4) 1844 1845 #Test centroids 1846 quantity.set_values([1.,2.,3.,4.], location = 'centroids') 1847 assert numpy.allclose(quantity.centroid_values, [1, 2, 3, 4]) #Centroid 1848 1849 #Set explicit_update 1850 quantity.explicit_update = numpy.array( [4.,3.,2.,1.] ) 1851 1852 #Set semi implicit update 1853 quantity.semi_implicit_update = numpy.array( [1.,1.,1.,1.] ) 1854 1855 #Update with given timestep 1856 timestep = 0.1 1857 quantity.update(0.1) 1858 1859 sem = numpy.array([1.,1.,1.,1.])/numpy.array([1, 2, 3, 4]) 1860 denom = numpy.ones(4, numpy.float)-timestep*sem 1861 1862 x = numpy.array([1., 2., 3., 4.]) 1863 x /= denom 1864 x += timestep*numpy.array( [4.0, 3.0, 2.0, 1.0] ) 1865 1866 assert numpy.allclose( quantity.centroid_values, x) 1867 1868 1869 1870 1871 #Test smoothing 1872 def test_smoothing(self): 1873 1874 from mesh_factory import rectangular 1875 from shallow_water import Domain, Transmissive_boundary 1876 from anuga.utilities.numerical_tools import mean 1877 1878 #Create basic mesh 1879 points, vertices, boundary = rectangular(2, 2) 1880 1881 #Create shallow water domain 1882 domain = Domain(points, vertices, boundary) 1883 domain.default_order=2 1884 domain.reduction = mean 1885 1886 1887 #Set some field values 1888 domain.set_quantity('elevation', lambda x,y: x) 1889 domain.set_quantity('friction', 0.03) 1890 1891 1892 ###################### 1893 # Boundary conditions 1894 B = Transmissive_boundary(domain) 1895 domain.set_boundary( {'left': B, 'right': B, 'top': B, 'bottom': B}) 1896 1897 1898 ###################### 1899 #Initial condition - with jumps 1900 1901 bed = domain.quantities['elevation'].vertex_values 1902 stage = numpy.zeros(bed.shape, numpy.float) 1903 1904 h = 0.03 1905 for i in range(stage.shape[0]): 1906 if i % 2 == 0: 1907 stage[i,:] = bed[i,:] + h 1908 else: 1909 stage[i,:] = bed[i,:] 1910 1911 domain.set_quantity('stage', stage) 1912 1913 stage = domain.quantities['stage'] 1914 1915 #Get smoothed stage 1916 A, V = stage.get_vertex_values(xy=False, smooth=True) 1917 Q = stage.vertex_values 1918 1919 1920 assert A.shape[0] == 9 1921 assert V.shape[0] == 8 1922 assert V.shape[1] == 3 1923 1924 #First four points 1925 assert numpy.allclose(A[0], (Q[0,2] + Q[1,1])/2) 1926 assert numpy.allclose(A[1], (Q[1,0] + Q[3,1] + Q[2,2])/3) 1927 assert numpy.allclose(A[2], Q[3,0]) 1928 assert numpy.allclose(A[3], (Q[0,0] + Q[5,1] + Q[4,2])/3) 1929 1930 #Center point 1931 assert numpy.allclose(A[4], (Q[0,1] + Q[1,2] + Q[2,0] +\ 1932 Q[5,0] + Q[6,2] + Q[7,1])/6) 1933 1934 1935 #Check V 1936 assert numpy.allclose(V[0,:], [3,4,0]) 1937 assert numpy.allclose(V[1,:], [1,0,4]) 1938 assert numpy.allclose(V[2,:], [4,5,1]) 1939 assert numpy.allclose(V[3,:], [2,1,5]) 1940 assert numpy.allclose(V[4,:], [6,7,3]) 1941 assert numpy.allclose(V[5,:], [4,3,7]) 1942 assert numpy.allclose(V[6,:], [7,8,4]) 1943 assert numpy.allclose(V[7,:], [5,4,8]) 1944 1945 #Get smoothed stage with XY 1946 X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=True) 1947 1948 assert numpy.allclose(A, A1) 1949 assert numpy.allclose(V, V1) 1950 1951 #Check XY 1952 assert numpy.allclose(X[4], 0.5) 1953 assert numpy.allclose(Y[4], 0.5) 1954 1955 assert numpy.allclose(X[7], 1.0) 1956 assert numpy.allclose(Y[7], 0.5) 1957 1958 1959 1960 1961 def test_vertex_values_no_smoothing(self): 1962 1963 from mesh_factory import rectangular 1964 from shallow_water import Domain, Transmissive_boundary 1965 from anuga.utilities.numerical_tools import mean 1966 1967 1968 #Create basic mesh 1969 points, vertices, boundary = rectangular(2, 2) 1970 1971 #Create shallow water domain 1972 domain = Domain(points, vertices, boundary) 1973 domain.default_order=2 1974 domain.reduction = mean 1975 1976 1977 #Set some field values 1978 domain.set_quantity('elevation', lambda x,y: x) 1979 domain.set_quantity('friction', 0.03) 1980 1981 1982 ###################### 1983 #Initial condition - with jumps 1984 1985 bed = domain.quantities['elevation'].vertex_values 1986 stage = numpy.zeros(bed.shape, numpy.float) 1987 1988 h = 0.03 1989 for i in range(stage.shape[0]): 1990 if i % 2 == 0: 1991 stage[i,:] = bed[i,:] + h 1992 else: 1993 stage[i,:] = bed[i,:] 1994 1995 domain.set_quantity('stage', stage) 1996 1997 #Get stage 1998 stage = domain.quantities['stage'] 1999 A, V = stage.get_vertex_values(xy=False, smooth=False) 2000 Q = stage.vertex_values.ravel() 2001 2002 for k in range(8): 2003 assert numpy.allclose(A[k], Q[k]) 2004 2005 2006 for k in range(8): 2007 assert V[k, 0] == 3*k 2008 assert V[k, 1] == 3*k+1 2009 assert V[k, 2] == 3*k+2 2010 2011 2012 2013 X, Y, A1, V1 = stage.get_vertex_values(xy=True, smooth=False) 2014 2015 2016 assert numpy.allclose(A, A1) 2017 assert numpy.allclose(V, V1) 2018 2019 #Check XY 2020 assert numpy.allclose(X[1], 0.5) 2021 assert numpy.allclose(Y[1], 0.5) 2022 assert numpy.allclose(X[4], 0.0) 2023 assert numpy.allclose(Y[4], 0.0) 2024 assert numpy.allclose(X[12], 1.0) 2025 assert numpy.allclose(Y[12], 0.0) 2026 2027 2028 2029 def set_array_values_by_index(self): 2030 2031 from mesh_factory import rectangular 2032 from shallow_water import Domain 2033 2034 #Create basic mesh 2035 points, vertices, boundary = rectangular(1, 1) 2036 2037 #Create shallow water domain 2038 domain = Domain(points, vertices, boundary) 2039 #print "domain.number_of_elements ",domain.number_of_elements 2040 quantity = Quantity(domain,[[1,1,1],[2,2,2]]) 2041 value = [7] 2042 indices = [1] 2043 quantity.set_array_values_by_index(value, 2044 location = 'centroids', 2045 indices = indices) 2046 #print "quantity.centroid_values",quantity.centroid_values 2047 2048 assert numpy.allclose(quantity.centroid_values, [1,7]) 2049 2050 quantity.set_array_values([15,20,25], indices = indices) 2051 assert numpy.allclose(quantity.centroid_values, [1,20]) 2052 2053 quantity.set_array_values([15,20,25], indices = indices) 2054 assert numpy.allclose(quantity.centroid_values, [1,20]) 2055 2056 def test_setting_some_vertex_values(self): 2057 """ 2058 set values based on triangle lists. 2059 """ 2060 from mesh_factory import rectangular 2061 from shallow_water import Domain 2062 2063 #Create basic mesh 2064 points, vertices, boundary = rectangular(1, 3) 2065 #print "vertices",vertices 2066 #Create shallow water domain 2067 domain = Domain(points, vertices, boundary) 2068 #print "domain.number_of_elements ",domain.number_of_elements 2069 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2070 [4,4,4],[5,5,5],[6,6,6]]) 2071 2072 2073 # Check that constants work 2074 value = 7 2075 indices = [1] 2076 quantity.set_values(value, 2077 location = 'centroids', 2078 indices = indices) 2079 #print "quantity.centroid_values",quantity.centroid_values 2080 assert numpy.allclose(quantity.centroid_values, [1,7,3,4,5,6]) 2081 2082 value = [7] 2083 indices = [1] 2084 quantity.set_values(value, 2085 location = 'centroids', 2086 indices = indices) 2087 #print "quantity.centroid_values",quantity.centroid_values 2088 assert numpy.allclose(quantity.centroid_values, [1,7,3,4,5,6]) 2089 2090 value = [[15,20,25]] 2091 quantity.set_values(value, indices = indices) 2092 #print "1 quantity.vertex_values",quantity.vertex_values 2093 assert numpy.allclose(quantity.vertex_values[1], value[0]) 2094 2095 2096 #print "quantity",quantity.vertex_values 2097 values = [10,100,50] 2098 quantity.set_values(values, indices = [0,1,5], location = 'centroids') 2099 #print "2 quantity.vertex_values",quantity.vertex_values 2100 assert numpy.allclose(quantity.vertex_values[0], [10,10,10]) 2101 assert numpy.allclose(quantity.vertex_values[5], [50,50,50]) 2102 #quantity.interpolate() 2103 #print "quantity.centroid_values",quantity.centroid_values 2104 assert numpy.allclose(quantity.centroid_values, [10,100,3,4,5,50]) 2105 2106 2107 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2108 [4,4,4],[5,5,5],[6,6,6]]) 2109 values = [10,100,50] 2110 #this will be per unique vertex, indexing the vertices 2111 #print "quantity.vertex_values",quantity.vertex_values 2112 quantity.set_values(values, indices = [0,1,5]) 2113 #print "quantity.vertex_values",quantity.vertex_values 2114 assert numpy.allclose(quantity.vertex_values[0], [1,50,10]) 2115 assert numpy.allclose(quantity.vertex_values[5], [6,6,6]) 2116 assert numpy.allclose(quantity.vertex_values[1], [100,10,50]) 2117 2118 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2119 [4,4,4],[5,5,5],[6,6,6]]) 2120 values = [[31,30,29],[400,400,400],[1000,999,998]] 2121 quantity.set_values(values, indices = [3,3,5]) 2122 quantity.interpolate() 2123 assert numpy.allclose(quantity.centroid_values, [1,2,3,400,5,999]) 2124 2125 values = [[1,1,1],[2,2,2],[3,3,3], 2126 [4,4,4],[5,5,5],[6,6,6]] 2127 quantity.set_values(values) 2128 2129 # testing the standard set values by vertex 2130 # indexed by vertex_id in general_mesh.coordinates 2131 values = [0,1,2,3,4,5,6,7] 2132 2133 quantity.set_values(values) 2134 #print "1 quantity.vertex_values",quantity.vertex_values 2135 assert numpy.allclose(quantity.vertex_values,[[ 4., 5., 0.], 2136 [ 1., 0., 5.], 2137 [ 5., 6., 1.], 2138 [ 2., 1., 6.], 2139 [ 6., 7., 2.], 2140 [ 3., 2., 7.]]) 2141 2142 def test_setting_unique_vertex_values(self): 2143 """ 2144 set values based on unique_vertex lists. 2145 """ 2146 from mesh_factory import rectangular 2147 from shallow_water import Domain 2148 2149 #Create basic mesh 2150 points, vertices, boundary = rectangular(1, 3) 2151 #print "vertices",vertices 2152 #Create shallow water domain 2153 domain = Domain(points, vertices, boundary) 2154 #print "domain.number_of_elements ",domain.number_of_elements 2155 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2156 [4,4,4],[5,5,5]]) 2157 value = 7 2158 indices = [1,5] 2159 quantity.set_values(value, 2160 location = 'unique vertices', 2161 indices = indices) 2162 #print "quantity.centroid_values",quantity.centroid_values 2163 assert numpy.allclose(quantity.vertex_values[0], [0,7,0]) 2164 assert numpy.allclose(quantity.vertex_values[1], [7,1,7]) 2165 assert numpy.allclose(quantity.vertex_values[2], [7,2,7]) 2166 2167 2168 def test_get_values(self): 2169 """ 2170 get values based on triangle lists. 2171 """ 2172 from mesh_factory import rectangular 2173 from shallow_water import Domain 2174 2175 #Create basic mesh 2176 points, vertices, boundary = rectangular(1, 3) 2177 2178 #print "points",points 2179 #print "vertices",vertices 2180 #print "boundary",boundary 2181 2182 #Create shallow water domain 2183 domain = Domain(points, vertices, boundary) 2184 #print "domain.number_of_elements ",domain.number_of_elements 2185 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2186 [4,4,4],[5,5,5]]) 2187 2188 #print "quantity.get_values(location = 'unique vertices')", \ 2189 # quantity.get_values(location = 'unique vertices') 2190 2191 #print "quantity.get_values(location = 'unique vertices')", \ 2192 # quantity.get_values(indices=[0,1,2,3,4,5,6,7], \ 2193 # location = 'unique vertices') 2194 2195 answer = [0.5,2,4,5,0,1,3,4.5] 2196 assert numpy.allclose(answer, 2197 quantity.get_values(location = 'unique vertices')) 2198 2199 indices = [0,5,3] 2200 answer = [0.5,1,5] 2201 assert numpy.allclose(answer, 2202 quantity.get_values(indices=indices, 2203 location = 'unique vertices')) 2204 #print "quantity.centroid_values",quantity.centroid_values 2205 #print "quantity.get_values(location = 'centroids') ",\ 2206 # quantity.get_values(location = 'centroids') 2207 2208 2209 2210 2211 def test_get_values_2(self): 2212 """Different mesh (working with domain object) - also check centroids. 2213 """ 2214 2215 2216 a = [0.0, 0.0] 2217 b = [0.0, 2.0] 2218 c = [2.0,0.0] 2219 d = [0.0, 4.0] 2220 e = [2.0, 2.0] 2221 f = [4.0,0.0] 2222 2223 points = [a, b, c, d, e, f] 2224 #bac, bce, ecf, dbe 2225 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2226 2227 domain = Domain(points, vertices) 2228 2229 quantity = Quantity(domain) 2230 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2231 2232 assert numpy.allclose(quantity.get_values(location='centroids'), [2,4,4,6]) 2233 assert numpy.allclose(quantity.get_values(location='centroids', indices=[1,3]), [4,6]) 2234 2235 2236 assert numpy.allclose(quantity.get_values(location='vertices'), [[4,0,2], 2237 [4,2,6], 2238 [6,2,4], 2239 [8,4,6]]) 2240 2241 assert numpy.allclose(quantity.get_values(location='vertices', indices=[1,3]), [[4,2,6], 2242 [8,4,6]]) 2243 2244 2245 assert numpy.allclose(quantity.get_values(location='edges'), [[1,3,2], 2246 [4,5,3], 2247 [3,5,4], 2248 [5,7,6]]) 2249 assert numpy.allclose(quantity.get_values(location='edges', indices=[1,3]), 2250 [[4,5,3], 2251 [5,7,6]]) 2252 2253 # Check averaging over vertices 2254 #a: 0 2255 #b: (4+4+4)/3 2256 #c: (2+2+2)/3 2257 #d: 8 2258 #e: (6+6+6)/3 2259 #f: 4 2260 assert(quantity.get_values(location='unique vertices'), [0, 4, 2, 8, 6, 4]) 2261 2262 2263 2264 2265 2266 2267 def test_get_interpolated_values(self): 2268 2269 from mesh_factory import rectangular 2270 from shallow_water import Domain 2271 2272 #Create basic mesh 2273 points, vertices, boundary = rectangular(1, 3) 2274 domain = Domain(points, vertices, boundary) 2275 2276 #Constant values 2277 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2278 [4,4,4],[5,5,5]]) 2279 2280 2281 2282 # Get interpolated values at centroids 2283 interpolation_points = domain.get_centroid_coordinates() 2284 answer = quantity.get_values(location='centroids') 2285 2286 2287 #print quantity.get_values(points=interpolation_points) 2288 assert numpy.allclose(answer, quantity.get_values(interpolation_points=interpolation_points)) 2289 2290 2291 #Arbitrary values 2292 quantity = Quantity(domain,[[0,1,2],[3,1,7],[2,1,2],[3,3,7], 2293 [1,4,-9],[2,5,0]]) 2294 2295 2296 # Get interpolated values at centroids 2297 interpolation_points = domain.get_centroid_coordinates() 2298 answer = quantity.get_values(location='centroids') 2299 #print answer 2300 #print quantity.get_values(interpolation_points=interpolation_points) 2301 assert numpy.allclose(answer, quantity.get_values(interpolation_points=interpolation_points, 2302 verbose=False)) 2303 2304 2305 #FIXME TODO 2306 #indices = [0,5,3] 2307 #answer = [0.5,1,5] 2308 #assert numpy.allclose(answer, 2309 # quantity.get_values(indices=indices, 2310 # location = 'unique vertices')) 2311 2312 2313 2314 2315 def test_get_interpolated_values_2(self): 2316 a = [0.0, 0.0] 2317 b = [0.0, 2.0] 2318 c = [2.0,0.0] 2319 d = [0.0, 4.0] 2320 e = [2.0, 2.0] 2321 f = [4.0,0.0] 2322 2323 points = [a, b, c, d, e, f] 2324 #bac, bce, ecf, dbe 2325 vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2326 2327 domain = Domain(points, vertices) 2328 2329 quantity = Quantity(domain) 2330 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2331 2332 #First pick one point 2333 x, y = 2.0/3, 8.0/3 2334 v = quantity.get_values(interpolation_points = [[x,y]]) 2335 assert numpy.allclose(v, 6) 2336 2337 # Then another to test that algorithm won't blindly 2338 # reuse interpolation matrix 2339 x, y = 4.0/3, 4.0/3 2340 v = quantity.get_values(interpolation_points = [[x,y]]) 2341 assert numpy.allclose(v, 4) 2342 2343 2344 2345 def test_get_interpolated_values_with_georef(self): 2346 2347 zone = 56 2348 xllcorner = 308500 2349 yllcorner = 6189000 2350 a = [0.0, 0.0] 2351 b = [0.0, 2.0] 2352 c = [2.0,0.0] 2353 d = [0.0, 4.0] 2354 e = [2.0, 2.0] 2355 f = [4.0,0.0] 2356 2357 points = [a, b, c, d, e, f] 2358 #bac, bce, ecf, dbe 2359 vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]] 2360 2361 domain = Domain(points, vertices, 2362 geo_reference=Geo_reference(zone,xllcorner,yllcorner)) 2363 2364 quantity = Quantity(domain) 2365 quantity.set_values(lambda x, y: x+2*y) #2 4 4 6 2366 2367 #First pick one point (and turn it into absolute coordinates) 2368 x, y = 2.0/3, 8.0/3 2369 v = quantity.get_values(interpolation_points = [[x+xllcorner,y+yllcorner]]) 2370 assert numpy.allclose(v, 6) 2371 2372 2373 # Then another to test that algorithm won't blindly 2374 # reuse interpolation matrix 2375 x, y = 4.0/3, 4.0/3 2376 v = quantity.get_values(interpolation_points = [[x+xllcorner,y+yllcorner]]) 2377 assert numpy.allclose(v, 4) 2378 2379 # Try two points 2380 pts = [[2.0/3 + xllcorner, 8.0/3 + yllcorner], 2381 [4.0/3 + xllcorner, 4.0/3 + yllcorner]] 2382 v = quantity.get_values(interpolation_points=pts) 2383 assert numpy.allclose(v, [6, 4]) 2384 2385 # Test it using the geospatial data format with absolute input points and default georef 2386 pts = Geospatial_data(data_points=pts) 2387 v = quantity.get_values(interpolation_points=pts) 2388 assert numpy.allclose(v, [6, 4]) 2389 2390 2391 # Test it using the geospatial data format with relative input points 2392 pts = Geospatial_data(data_points=[[2.0/3, 8.0/3], [4.0/3, 4.0/3]], 2393 geo_reference=Geo_reference(zone,xllcorner,yllcorner)) 2394 v = quantity.get_values(interpolation_points=pts) 2395 assert numpy.allclose(v, [6, 4]) 2396 2397 2398 2399 2400 def test_getting_some_vertex_values(self): 2401 """ 2402 get values based on triangle lists. 2403 """ 2404 from mesh_factory import rectangular 2405 from shallow_water import Domain 2406 2407 #Create basic mesh 2408 points, vertices, boundary = rectangular(1, 3) 2409 2410 #print "points",points 2411 #print "vertices",vertices 2412 #print "boundary",boundary 2413 2414 #Create shallow water domain 2415 domain = Domain(points, vertices, boundary) 2416 #print "domain.number_of_elements ",domain.number_of_elements 2417 quantity = Quantity(domain,[[1,1,1],[2,2,2],[3,3,3], 2418 [4,4,4],[5,5,5],[6,6,6]]) 2419 value = [7] 2420 indices = [1] 2421 quantity.set_values(value, 2422 location = 'centroids', 2423 indices = indices) 2424 #print "quantity.centroid_values",quantity.centroid_values 2425 #print "quantity.get_values(location = 'centroids') ",\ 2426 # quantity.get_values(location = 'centroids') 2427 assert numpy.allclose(quantity.centroid_values, 2428 quantity.get_values(location = 'centroids')) 2429 2430 2431 value = [[15,20,25]] 2432 quantity.set_values(value, indices = indices) 2433 #print "1 quantity.vertex_values",quantity.vertex_values 2434 assert numpy.allclose(quantity.vertex_values, quantity.get_values()) 2435 2436 assert numpy.allclose(quantity.edge_values, 2437 quantity.get_values(location = 'edges')) 2438 2439 # get a subset of elements 2440 subset = quantity.get_values(location='centroids', indices=[0,5]) 2441 answer = [quantity.centroid_values[0],quantity.centroid_values[5]] 2442 assert numpy.allclose(subset, answer) 2443 2444 2445 subset = quantity.get_values(location='edges', indices=[0,5]) 2446 answer = [quantity.edge_values[0],quantity.edge_values[5]] 2447 #print "subset",subset 2448 #print "answer",answer 2449 assert numpy.allclose(subset, answer) 2450 2451 subset = quantity.get_values( indices=[1,5]) 2452 answer = [quantity.vertex_values[1],quantity.vertex_values[5]] 2453 #print "subset",subset 2454 #print "answer",answer 2455 assert numpy.allclose(subset, answer) 2456 2457 def test_smooth_vertex_values(self): 2458 """ 2459 get values based on triangle lists. 2460 """ 2461 from mesh_factory import rectangular 2462 from shallow_water import Domain 2463 2464 #Create basic mesh 2465 points, vertices, boundary = rectangular(2, 2) 2466 2467 #print "points",points 2468 #print "vertices",vertices 2469 #print "boundary",boundary 2470 2471 #Create shallow water domain 2472 domain = Domain(points, vertices, boundary) 2473 #print "domain.number_of_elements ",domain.number_of_elements 2474 quantity = Quantity(domain,[[0,0,0],[1,1,1],[2,2,2],[3,3,3], 2475 [4,4,4],[5,5,5],[6,6,6],[7,7,7]]) 2476 2477 #print "quantity.get_values(location = 'unique vertices')", \ 2478 # quantity.get_values(location = 'unique vertices') 2479 2480 #print "quantity.get_values(location = 'unique vertices')", \ 2481 # quantity.get_values(indices=[0,1,2,3,4,5,6,7], \ 2482 # location = 'unique vertices') 2483 2484 #print quantity.get_values(location = 'unique vertices') 2485 #print quantity.domain.number_of_triangles_per_node 2486 #print quantity.vertex_values 2487 2488 #answer = [0.5, 2, 3, 3, 3.5, 4, 4, 5, 6.5] 2489 #assert numpy.allclose(answer, 2490 # quantity.get_values(location = 'unique vertices')) 2491 2492 quantity.smooth_vertex_values() 2493 2494 #print quantity.vertex_values 2495 2496 2497 answer_vertex_values = [[3,3.5,0.5],[2,0.5,3.5],[3.5,4,2],[3,2,4], 2498 [4,5,3],[3.5,3,5],[5,6.5,3.5],[4,3.5,6.5]] 2499 2500 assert numpy.allclose(answer_vertex_values, 2501 quantity.vertex_values) 2502 #print "quantity.centroid_values",quantity.centroid_values 2503 #print "quantity.get_values(location = 'centroids') ",\ 2504 # quantity.get_values(location = 'centroids') 2536 2505 2537 2506 … … 2540 2509 if __name__ == "__main__": 2541 2510 suite = unittest.makeSuite(Test_Quantity, 'test') 2542 #suite = unittest.makeSuite(Test_Quantity, 'test_set_values_from_file_using_polygon') 2543 2544 #suite = unittest.makeSuite(Test_Quantity, 'test_set_vertex_values_using_general_interface_with_subset') 2545 #print "restricted test" 2546 #suite = unittest.makeSuite(Test_Quantity,'verbose_test_set_values_from_UTM_pts') 2511 # suite = unittest.makeSuite(Test_Quantity, 'test_get_extrema_1') 2547 2512 runner = unittest.TextTestRunner() 2548 2513 runner.run(suite) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_region.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 … … 8 6 from domain import * 9 7 from region import * 10 #from anuga.config import epsilon 11 from numpy.oldnumeric import allclose, average #, array, ones, Float 8 import numpy 9 12 10 """ 13 11 This is what the mesh in these tests look like; … … 46 44 from mesh_factory import rectangular 47 45 from shallow_water import Domain 48 from numpy.oldnumeric import zeros, Float49 46 50 47 #Create basic mesh … … 66 63 domain.set_region([a, b]) 67 64 #print domain.quantities['friction'].get_values() 68 assert allclose(domain.quantities['friction'].get_values(),\69 [[ 0.09, 0.09, 0.09],70 [ 0.09, 0.09, 0.09],71 [ 0.07, 0.07, 0.07],72 [ 0.07, 0.07, 0.07],73 [ 1.0, 1.0, 1.0],74 [ 1.0, 1.0, 1.0]])65 assert numpy.allclose(domain.quantities['friction'].get_values(), 66 [[ 0.09, 0.09, 0.09], 67 [ 0.09, 0.09, 0.09], 68 [ 0.07, 0.07, 0.07], 69 [ 0.07, 0.07, 0.07], 70 [ 1.0, 1.0, 1.0], 71 [ 1.0, 1.0, 1.0]]) 75 72 76 73 #c = Add_Value_To_region('all', 'friction', 10.0) 77 74 domain.set_region(Add_value_to_region('all', 'friction', 10.0)) 78 75 #print domain.quantities['friction'].get_values() 79 assert allclose(domain.quantities['friction'].get_values(),80 [[ 10.09, 10.09, 10.09],81 [ 10.09, 10.09, 10.09],82 [ 10.07, 10.07, 10.07],83 [ 10.07, 10.07, 10.07],84 [ 11.0, 11.0, 11.0],85 [ 11.0, 11.0, 11.0]])76 assert numpy.allclose(domain.quantities['friction'].get_values(), 77 [[ 10.09, 10.09, 10.09], 78 [ 10.09, 10.09, 10.09], 79 [ 10.07, 10.07, 10.07], 80 [ 10.07, 10.07, 10.07], 81 [ 11.0, 11.0, 11.0], 82 [ 11.0, 11.0, 11.0]]) 86 83 87 84 # trying a function 88 85 domain.set_region(Set_region('top', 'friction', add_x_y)) 89 86 #print domain.quantities['friction'].get_values() 90 assert allclose(domain.quantities['friction'].get_values(),91 [[ 10.09, 10.09, 10.09],92 [ 10.09, 10.09, 10.09],93 [ 10.07, 10.07, 10.07],94 [ 10.07, 10.07, 10.07],95 [ 5./3, 2.0, 2./3],96 [ 1.0, 2./3, 2.0]])87 assert numpy.allclose(domain.quantities['friction'].get_values(), 88 [[ 10.09, 10.09, 10.09], 89 [ 10.09, 10.09, 10.09], 90 [ 10.07, 10.07, 10.07], 91 [ 10.07, 10.07, 10.07], 92 [ 5./3, 2.0, 2./3], 93 [ 1.0, 2./3, 2.0]]) 97 94 98 95 domain.set_quantity('elevation', 10.0) … … 100 97 domain.set_region(Add_value_to_region('top', 'stage', 1.0,initial_quantity='elevation')) 101 98 #print domain.quantities['stage'].get_values() 102 assert allclose(domain.quantities['stage'].get_values(),103 [[ 10., 10., 10.],104 [ 10., 10., 10.],105 [ 10., 10., 10.],106 [ 10., 10., 10.],107 [ 11.0, 11.0, 11.0],108 [ 11.0, 11.0, 11.0]])99 assert numpy.allclose(domain.quantities['stage'].get_values(), 100 [[ 10., 10., 10.], 101 [ 10., 10., 10.], 102 [ 10., 10., 10.], 103 [ 10., 10., 10.], 104 [ 11.0, 11.0, 11.0], 105 [ 11.0, 11.0, 11.0]]) 109 106 110 107 … … 117 114 domain.set_region(Add_quantities('top', 'elevation','stage')) 118 115 #print domain.quantities['stage'].get_values() 119 assert allclose(domain.quantities['elevation'].get_values(),120 [[ 10., 10., 10.],121 [ 10., 10., 10.],122 [ 10., 10., 10.],123 [ 10., 10., 10.],124 [ 33., 33.0, 33.],125 [ 33.0, 33., 33.]])116 assert numpy.allclose(domain.quantities['elevation'].get_values(), 117 [[ 10., 10., 10.], 118 [ 10., 10., 10.], 119 [ 10., 10., 10.], 120 [ 10., 10., 10.], 121 [ 33., 33.0, 33.], 122 [ 33.0, 33., 33.]]) 126 123 127 124 def test_unique_vertices(self): … … 131 128 from mesh_factory import rectangular 132 129 from shallow_water import Domain 133 from numpy.oldnumeric import zeros, Float134 130 135 131 #Create basic mesh … … 149 145 domain.set_region(a) 150 146 #print domain.quantities['friction'].get_values() 151 assert allclose(domain.quantities['friction'].get_values(),\152 [[ 0.09, 0.09, 0.09],153 [ 0.09, 0.09, 0.09],154 [ 0.09, 0.07, 0.09],155 [ 0.07, 0.09, 0.07],156 [ 0.07, 0.07, 0.07],157 [ 0.07, 0.07, 0.07]])147 assert numpy.allclose(domain.quantities['friction'].get_values(),\ 148 [[ 0.09, 0.09, 0.09], 149 [ 0.09, 0.09, 0.09], 150 [ 0.09, 0.07, 0.09], 151 [ 0.07, 0.09, 0.07], 152 [ 0.07, 0.07, 0.07], 153 [ 0.07, 0.07, 0.07]]) 158 154 159 155 … … 164 160 from mesh_factory import rectangular 165 161 from shallow_water import Domain 166 from numpy.oldnumeric import zeros, Float167 162 168 163 #Create basic mesh … … 182 177 183 178 #print domain.quantities['friction'].get_values() 184 assert allclose(domain.quantities['friction'].get_values(),\185 [[ 1.07, 1.07, 1.07],186 [ 1.07, 1.07, 1.07],187 [ 1.07, 0.07, 1.07],188 [ 0.07, 1.07, 0.07],189 [ 0.07, 0.07, 0.07],190 [ 0.07, 0.07, 0.07]])179 assert numpy.allclose(domain.quantities['friction'].get_values(),\ 180 [[ 1.07, 1.07, 1.07], 181 [ 1.07, 1.07, 1.07], 182 [ 1.07, 0.07, 1.07], 183 [ 0.07, 1.07, 0.07], 184 [ 0.07, 0.07, 0.07], 185 [ 0.07, 0.07, 0.07]]) 191 186 192 187 def test_unique_vertices_average_loc_vert(self): … … 196 191 from mesh_factory import rectangular 197 192 from shallow_water import Domain 198 from numpy.oldnumeric import zeros, Float199 193 200 194 #Create basic mesh … … 220 214 #print domain.quantities['friction'].get_values() 221 215 frict_points = domain.quantities['friction'].get_values() 222 assert allclose(frict_points[0],\223 [ calc_frict, calc_frict, calc_frict])224 assert allclose(frict_points[1],\225 [ calc_frict, calc_frict, calc_frict])216 assert numpy.allclose(frict_points[0], 217 [ calc_frict, calc_frict, calc_frict]) 218 assert numpy.allclose(frict_points[1], 219 [ calc_frict, calc_frict, calc_frict]) 226 220 227 221 def test_unique_vertices_average_loc_unique_vert(self): … … 231 225 from mesh_factory import rectangular 232 226 from shallow_water import Domain 233 from numpy.oldnumeric import zeros, Float234 227 235 228 #Create basic mesh … … 254 247 #print domain.quantities['friction'].get_values() 255 248 frict_points = domain.quantities['friction'].get_values() 256 assert allclose(frict_points[0],\257 [ calc_frict, calc_frict, calc_frict])258 assert allclose(frict_points[1],\259 [ calc_frict, calc_frict, calc_frict])260 assert allclose(frict_points[2],\261 [ calc_frict, 1.0 + 2.0/3.0, calc_frict])262 assert allclose(frict_points[3],\263 [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0])249 assert numpy.allclose(frict_points[0], 250 [ calc_frict, calc_frict, calc_frict]) 251 assert numpy.allclose(frict_points[1], 252 [ calc_frict, calc_frict, calc_frict]) 253 assert numpy.allclose(frict_points[2], 254 [ calc_frict, 1.0 + 2.0/3.0, calc_frict]) 255 assert numpy.allclose(frict_points[3], 256 [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0]) 264 257 265 258 266 259 #------------------------------------------------------------- 267 260 if __name__ == "__main__": 268 ##suite = unittest.makeSuite(Test_Region,'test')269 suite = unittest.makeSuite(Test_Region,'test_unique_vertices_average_loc_unique_vert')261 suite = unittest.makeSuite(Test_Region,'test') 262 ## suite = unittest.makeSuite(Test_Region,'test_unique_vertices_average_loc_unique_vert') 270 263 runner = unittest.TextTestRunner() 271 264 runner.run(suite) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/test_util.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 #!/usr/bin/env python 4 2 5 6 3 import unittest 7 ##from numpy.oldnumeric import zeros, array, allclose, Float, alltrue 8 from numpy import zeros, array, allclose, float, alltrue 4 import numpy 5 9 6 from math import sqrt, pi 10 7 import tempfile, os … … 94 91 95 92 #Exact linear intpolation 96 assert allclose(q[0], 2*t)93 assert numpy.allclose(q[0], 2*t) 97 94 if i%6 == 0: 98 assert allclose(q[1], t**2)99 assert allclose(q[2], sin(t*pi/600))95 assert numpy.allclose(q[1], t**2) 96 assert numpy.allclose(q[2], sin(t*pi/600)) 100 97 101 98 #Check non-exact … … 103 100 t = 90 #Halfway between 60 and 120 104 101 q = F(t) 105 assert allclose( (120**2 + 60**2)/2, q[1] )106 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] )102 assert numpy.allclose( (120**2 + 60**2)/2, q[1] ) 103 assert numpy.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 107 104 108 105 109 106 t = 100 #Two thirds of the way between between 60 and 120 110 107 q = F(t) 111 assert allclose( 2*120**2/3 + 60**2/3, q[1] )112 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )108 assert numpy.allclose( 2*120**2/3 + 60**2/3, q[1] ) 109 assert numpy.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 113 110 114 111 os.remove(filename + '.txt') … … 128 125 from shallow_water import Domain, Dirichlet_boundary 129 126 from mesh_factory import rectangular 130 from numpy.oldnumeric import take, concatenate, reshape131 127 132 128 #Create basic mesh and shallow water domain … … 159 155 #Evolution 160 156 t0 = -1 157 # crash at following line 161 158 for t in domain1.evolve(yieldstep = 0.1, finaltime = finaltime): 162 159 #print 'Timesteps: %.16f, %.16f' %(t0, t) … … 185 182 186 183 last_time_index = len(time)-1 #Last last_time_index 187 d_stage = reshape(take(stage[last_time_index, :], [0,5,10,15]), (4,1))188 d_uh = reshape(take(xmomentum[last_time_index, :], [0,5,10,15]), (4,1))189 d_vh = reshape(take(ymomentum[last_time_index, :], [0,5,10,15]), (4,1))190 D = concatenate( (d_stage, d_uh, d_vh), axis=1)184 d_stage = numpy.reshape(numpy.take(stage[last_time_index, :], [0,5,10,15], axis=0), (4,1)) 185 d_uh = numpy.reshape(numpy.take(xmomentum[last_time_index, :], [0,5,10,15], axis=0), (4,1)) 186 d_vh = numpy.reshape(numpy.take(ymomentum[last_time_index, :], [0,5,10,15], axis=0), (4,1)) 187 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 191 188 192 189 #Reference interpolated values at midpoints on diagonal at … … 197 194 198 195 #And the midpoints are found now 199 Dx = take(reshape(x, (16,1)), [0,5,10,15])200 Dy = take(reshape(y, (16,1)), [0,5,10,15])201 202 diag = concatenate( (Dx, Dy), axis=1)196 Dx = numpy.take(numpy.reshape(x, (16,1)), [0,5,10,15], axis=0) 197 Dy = numpy.take(numpy.reshape(y, (16,1)), [0,5,10,15], axis=0) 198 199 diag = numpy.concatenate( (Dx, Dy), axis=1) 203 200 d_midpoints = (diag[1:] + diag[:-1])/2 204 201 … … 212 209 assert not T[-1] == T[-2], msg 213 210 t = time[last_time_index] 214 q = f(t, point_id=0); assert allclose(r0, q)215 q = f(t, point_id=1); assert allclose(r1, q)216 q = f(t, point_id=2); assert allclose(r2, q)211 q = f(t, point_id=0); assert numpy.allclose(r0, q) 212 q = f(t, point_id=1); assert numpy.allclose(r1, q) 213 q = f(t, point_id=2); assert numpy.allclose(r2, q) 217 214 218 215 … … 221 218 222 219 timestep = 0 #First timestep 223 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))224 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))225 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))226 D = concatenate( (d_stage, d_uh, d_vh), axis=1)220 d_stage = numpy.reshape(numpy.take(stage[timestep, :], [0,5,10,15], axis=0), (4,1)) 221 d_uh = numpy.reshape(numpy.take(xmomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 222 d_vh = numpy.reshape(numpy.take(ymomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 223 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 227 224 228 225 #Reference interpolated values at midpoints on diagonal at … … 234 231 #Let us see if the file function can find the correct 235 232 #values 236 q = f(0, point_id=0); assert allclose(r0, q)237 q = f(0, point_id=1); assert allclose(r1, q)238 q = f(0, point_id=2); assert allclose(r2, q)233 q = f(0, point_id=0); assert numpy.allclose(r0, q) 234 q = f(0, point_id=1); assert numpy.allclose(r1, q) 235 q = f(0, point_id=2); assert numpy.allclose(r2, q) 239 236 240 237 … … 243 240 244 241 timestep = 33 245 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))246 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))247 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))248 D = concatenate( (d_stage, d_uh, d_vh), axis=1)242 d_stage = numpy.reshape(numpy.take(stage[timestep, :], [0,5,10,15], axis=0), (4,1)) 243 d_uh = numpy.reshape(numpy.take(xmomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 244 d_vh = numpy.reshape(numpy.take(ymomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 245 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 249 246 250 247 #Reference interpolated values at midpoints on diagonal at … … 254 251 r2 = (D[2] + D[3])/2 255 252 256 q = f(timestep/10., point_id=0); assert allclose(r0, q)257 q = f(timestep/10., point_id=1); assert allclose(r1, q)258 q = f(timestep/10., point_id=2); assert allclose(r2, q)253 q = f(timestep/10., point_id=0); assert numpy.allclose(r0, q) 254 q = f(timestep/10., point_id=1); assert numpy.allclose(r1, q) 255 q = f(timestep/10., point_id=2); assert numpy.allclose(r2, q) 259 256 260 257 … … 264 261 265 262 timestep = 15 266 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))267 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))268 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))269 D = concatenate( (d_stage, d_uh, d_vh), axis=1)263 d_stage = numpy.reshape(numpy.take(stage[timestep, :], [0,5,10,15], axis=0), (4,1)) 264 d_uh = numpy.reshape(numpy.take(xmomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 265 d_vh = numpy.reshape(numpy.take(ymomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 266 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 270 267 271 268 #Reference interpolated values at midpoints on diagonal at … … 277 274 # 278 275 timestep = 16 279 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))280 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))281 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))282 D = concatenate( (d_stage, d_uh, d_vh), axis=1)276 d_stage = numpy.reshape(numpy.take(stage[timestep, :], [0,5,10,15], axis=0), (4,1)) 277 d_uh = numpy.reshape(numpy.take(xmomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 278 d_vh = numpy.reshape(numpy.take(ymomentum[timestep, :], [0,5,10,15], axis=0), (4,1)) 279 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 283 280 284 281 #Reference interpolated values at midpoints on diagonal at … … 293 290 r2 = (r2_0 + r2_1)/2 294 291 295 q = f((timestep - 0.5)/10., point_id=0); assert allclose(r0, q)296 q = f((timestep - 0.5)/10., point_id=1); assert allclose(r1, q)297 q = f((timestep - 0.5)/10., point_id=2); assert allclose(r2, q)292 q = f((timestep - 0.5)/10., point_id=0); assert numpy.allclose(r0, q) 293 q = f((timestep - 0.5)/10., point_id=1); assert numpy.allclose(r1, q) 294 q = f((timestep - 0.5)/10., point_id=2); assert numpy.allclose(r2, q) 298 295 299 296 ################## … … 307 304 308 305 #And the file function gives 309 q = f((timestep - 1.0/3)/10., point_id=0); assert allclose(r0, q)310 q = f((timestep - 1.0/3)/10., point_id=1); assert allclose(r1, q)311 q = f((timestep - 1.0/3)/10., point_id=2); assert allclose(r2, q)306 q = f((timestep - 1.0/3)/10., point_id=0); assert numpy.allclose(r0, q) 307 q = f((timestep - 1.0/3)/10., point_id=1); assert numpy.allclose(r1, q) 308 q = f((timestep - 1.0/3)/10., point_id=2); assert numpy.allclose(r2, q) 312 309 313 310 fid.close() … … 329 326 from shallow_water import Domain, Dirichlet_boundary 330 327 from mesh_factory import rectangular 331 from numpy.oldnumeric import take, concatenate, reshape332 328 333 329 … … 389 385 390 386 last_time_index = len(time)-1 #Last last_time_index 391 d_stage = reshape(take(stage[last_time_index, :], [0,5,10,15]), (4,1))392 d_uh = reshape(take(xmomentum[last_time_index, :], [0,5,10,15]), (4,1))393 d_vh = reshape(take(ymomentum[last_time_index, :], [0,5,10,15]), (4,1))394 D = concatenate( (d_stage, d_uh, d_vh), axis=1)387 d_stage = numpy.reshape(take(stage[last_time_index, :], [0,5,10,15]), (4,1)) 388 d_uh = numpy.reshape(take(xmomentum[last_time_index, :], [0,5,10,15]), (4,1)) 389 d_vh = numpy.reshape(take(ymomentum[last_time_index, :], [0,5,10,15]), (4,1)) 390 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 395 391 396 392 #Reference interpolated values at midpoints on diagonal at … … 401 397 402 398 #And the midpoints are found now 403 Dx = take( reshape(x, (16,1)), [0,5,10,15])404 Dy = take( reshape(y, (16,1)), [0,5,10,15])405 406 diag = concatenate( (Dx, Dy), axis=1)399 Dx = take(numpy.reshape(x, (16,1)), [0,5,10,15]) 400 Dy = take(numpy.reshape(y, (16,1)), [0,5,10,15]) 401 402 diag = numpy.concatenate( (Dx, Dy), axis=1) 407 403 d_midpoints = (diag[1:] + diag[:-1])/2 408 404 … … 418 414 419 415 t = time[last_time_index] 420 q = f(t, point_id=0); assert allclose(r0, q)421 q = f(t, point_id=1); assert allclose(r1, q)422 q = f(t, point_id=2); assert allclose(r2, q)416 q = f(t, point_id=0); assert numpy.allclose(r0, q) 417 q = f(t, point_id=1); assert numpy.allclose(r1, q) 418 q = f(t, point_id=2); assert numpy.allclose(r2, q) 423 419 424 420 … … 427 423 428 424 timestep = 0 #First timestep 429 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))430 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))431 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))432 D = concatenate( (d_stage, d_uh, d_vh), axis=1)425 d_stage = numpy.reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) 426 d_uh = numpy.reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) 427 d_vh = numpy.reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) 428 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 433 429 434 430 #Reference interpolated values at midpoints on diagonal at … … 440 436 #Let us see if the file function can find the correct 441 437 #values 442 q = f(0, point_id=0); assert allclose(r0, q)443 q = f(0, point_id=1); assert allclose(r1, q)444 q = f(0, point_id=2); assert allclose(r2, q)438 q = f(0, point_id=0); assert numpy.allclose(r0, q) 439 q = f(0, point_id=1); assert numpy.allclose(r1, q) 440 q = f(0, point_id=2); assert numpy.allclose(r2, q) 445 441 446 442 … … 449 445 450 446 timestep = 33 451 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))452 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))453 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))454 D = concatenate( (d_stage, d_uh, d_vh), axis=1)447 d_stage = numpy.reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) 448 d_uh = numpy.reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) 449 d_vh = numpy.reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) 450 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 455 451 456 452 #Reference interpolated values at midpoints on diagonal at … … 460 456 r2 = (D[2] + D[3])/2 461 457 462 q = f(timestep/10., point_id=0); assert allclose(r0, q)463 q = f(timestep/10., point_id=1); assert allclose(r1, q)464 q = f(timestep/10., point_id=2); assert allclose(r2, q)458 q = f(timestep/10., point_id=0); assert numpy.allclose(r0, q) 459 q = f(timestep/10., point_id=1); assert numpy.allclose(r1, q) 460 q = f(timestep/10., point_id=2); assert numpy.allclose(r2, q) 465 461 466 462 … … 470 466 471 467 timestep = 15 472 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))473 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))474 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))475 D = concatenate( (d_stage, d_uh, d_vh), axis=1)468 d_stage = numpy.reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) 469 d_uh = numpy.reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) 470 d_vh = numpy.reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) 471 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 476 472 477 473 #Reference interpolated values at midpoints on diagonal at … … 483 479 # 484 480 timestep = 16 485 d_stage = reshape(take(stage[timestep, :], [0,5,10,15]), (4,1))486 d_uh = reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1))487 d_vh = reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1))488 D = concatenate( (d_stage, d_uh, d_vh), axis=1)481 d_stage = numpy.reshape(take(stage[timestep, :], [0,5,10,15]), (4,1)) 482 d_uh = numpy.reshape(take(xmomentum[timestep, :], [0,5,10,15]), (4,1)) 483 d_vh = numpy.reshape(take(ymomentum[timestep, :], [0,5,10,15]), (4,1)) 484 D = numpy.concatenate( (d_stage, d_uh, d_vh), axis=1) 489 485 490 486 #Reference interpolated values at midpoints on diagonal at … … 499 495 r2 = (r2_0 + r2_1)/2 500 496 501 q = f((timestep - 0.5)/10., point_id=0); assert allclose(r0, q)502 q = f((timestep - 0.5)/10., point_id=1); assert allclose(r1, q)503 q = f((timestep - 0.5)/10., point_id=2); assert allclose(r2, q)497 q = f((timestep - 0.5)/10., point_id=0); assert numpy.allclose(r0, q) 498 q = f((timestep - 0.5)/10., point_id=1); assert numpy.allclose(r1, q) 499 q = f((timestep - 0.5)/10., point_id=2); assert numpy.allclose(r2, q) 504 500 505 501 ################## … … 513 509 514 510 #And the file function gives 515 q = f((timestep - 1.0/3)/10., point_id=0); assert allclose(r0, q)516 q = f((timestep - 1.0/3)/10., point_id=1); assert allclose(r1, q)517 q = f((timestep - 1.0/3)/10., point_id=2); assert allclose(r2, q)511 q = f((timestep - 1.0/3)/10., point_id=0); assert numpy.allclose(r0, q) 512 q = f((timestep - 1.0/3)/10., point_id=1); assert numpy.allclose(r1, q) 513 q = f((timestep - 1.0/3)/10., point_id=2); assert numpy.allclose(r2, q) 518 514 519 515 fid.close() … … 545 541 import os, time 546 542 from anuga.config import time_format 547 from numpy.oldnumeric import sin, pi, exp548 543 from mesh_factory import rectangular 549 544 from shallow_water import Domain … … 587 582 domain.set_quantity('xmomentum', f2) 588 583 589 f3 = lambda x,y: x**2 + y**2 * sin(t*pi/600)584 f3 = lambda x,y: x**2 + y**2 * numpy.sin(t*numpy.pi/600) 590 585 domain.set_quantity('ymomentum', f3) 591 586 … … 607 602 608 603 #Check that FF updates fixes domain starttime 609 assert allclose(domain.starttime, start)604 assert numpy.allclose(domain.starttime, start) 610 605 611 606 #Check that domain.starttime isn't updated if later … … 614 609 quantities = domain.conserved_quantities, 615 610 interpolation_points = interpolation_points) 616 assert allclose(domain.starttime, start+1)611 assert numpy.allclose(domain.starttime, start+1) 617 612 domain.starttime = start 618 613 … … 648 643 self.failUnless( q == actual, 'Fail!') 649 644 else: 650 assert allclose(q, actual)645 assert numpy.allclose(q, actual) 651 646 652 647 … … 658 653 t = 90 #Halfway between 60 and 120 659 654 q = F(t, point_id=id) 660 assert allclose( (q120+q60)/2, q )655 assert numpy.allclose( (q120+q60)/2, q ) 661 656 662 657 t = 100 #Two thirds of the way between between 60 and 120 663 658 q = F(t, point_id=id) 664 assert allclose(q60/3 + 2*q120/3, q)659 assert numpy.allclose(q60/3 + 2*q120/3, q) 665 660 666 661 … … 673 668 quantities = domain.conserved_quantities, 674 669 interpolation_points = interpolation_points) 675 assert allclose(domain.starttime, start+delta)670 assert numpy.allclose(domain.starttime, start+delta) 676 671 677 672 … … 692 687 693 688 q = F(t-delta, point_id=id) 694 assert allclose(q, (k*q1 + (6-k)*q0)/6)689 assert numpy.allclose(q, (k*q1 + (6-k)*q0)/6) 695 690 696 691 … … 706 701 import os, time 707 702 from anuga.config import time_format 708 from numpy.oldnumeric import sin, pi, exp709 703 from mesh_factory import rectangular 710 704 from shallow_water import Domain … … 754 748 domain.set_quantity('xmomentum', f2) 755 749 756 f3 = lambda x,y: x**2 + y**2 * sin(t*pi/600)750 f3 = lambda x,y: x**2 + y**2 * numpy.sin(t*numpy.pi/600) 757 751 domain.set_quantity('ymomentum', f3) 758 752 … … 777 771 778 772 #Check that FF updates fixes domain starttime 779 assert allclose(domain.starttime, start)773 assert numpy.allclose(domain.starttime, start) 780 774 781 775 #Check that domain.starttime isn't updated if later … … 784 778 quantities = domain.conserved_quantities, 785 779 interpolation_points = interpolation_points) 786 assert allclose(domain.starttime, start+1)780 assert numpy.allclose(domain.starttime, start+1) 787 781 domain.starttime = start 788 782 … … 820 814 self.failUnless( q == actual, 'Fail!') 821 815 else: 822 assert allclose(q, actual)816 assert numpy.allclose(q, actual) 823 817 824 818 # now lets check points inside the mesh … … 866 860 self.failUnless( q == actual, 'Fail!') 867 861 else: 868 assert allclose(q, actual)862 assert numpy.allclose(q, actual) 869 863 870 864 … … 876 870 t = 90 #Halfway between 60 and 120 877 871 q = F(t, point_id=id) 878 assert allclose( (q120+q60)/2, q )872 assert numpy.allclose( (q120+q60)/2, q ) 879 873 880 874 t = 100 #Two thirds of the way between between 60 and 120 881 875 q = F(t, point_id=id) 882 assert allclose(q60/3 + 2*q120/3, q)876 assert numpy.allclose(q60/3 + 2*q120/3, q) 883 877 884 878 … … 891 885 quantities = domain.conserved_quantities, 892 886 interpolation_points = interpolation_points) 893 assert allclose(domain.starttime, start+delta)887 assert numpy.allclose(domain.starttime, start+delta) 894 888 895 889 … … 910 904 911 905 q = F(t-delta, point_id=id) 912 assert allclose(q, (k*q1 + (6-k)*q0)/6)906 assert numpy.allclose(q, (k*q1 + (6-k)*q0)/6) 913 907 914 908 … … 958 952 domain, 959 953 quantities = ['Attribute0', 'Attribute1', 'Attribute2']) 960 assert allclose(domain.starttime, start)954 assert numpy.allclose(domain.starttime, start) 961 955 962 956 # Check that domain.starttime is updated if too early … … 965 959 domain, 966 960 quantities = ['Attribute0', 'Attribute1', 'Attribute2']) 967 assert allclose(domain.starttime, start)961 assert numpy.allclose(domain.starttime, start) 968 962 969 963 # Check that domain.starttime isn't updated if later … … 972 966 domain, 973 967 quantities = ['Attribute0', 'Attribute1', 'Attribute2']) 974 assert allclose(domain.starttime, start+1)968 assert numpy.allclose(domain.starttime, start+1) 975 969 976 970 domain.starttime = start … … 990 984 991 985 #Exact linear intpolation 992 assert allclose(q[0], 2*t)986 assert numpy.allclose(q[0], 2*t) 993 987 if i%6 == 0: 994 assert allclose(q[1], t**2)995 assert allclose(q[2], sin(t*pi/600))988 assert numpy.allclose(q[1], t**2) 989 assert numpy.allclose(q[2], sin(t*pi/600)) 996 990 997 991 #Check non-exact … … 999 993 t = 90 #Halfway between 60 and 120 1000 994 q = F(t) 1001 assert allclose( (120**2 + 60**2)/2, q[1] )1002 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] )995 assert numpy.allclose( (120**2 + 60**2)/2, q[1] ) 996 assert numpy.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 1003 997 1004 998 1005 999 t = 100 #Two thirds of the way between between 60 and 120 1006 1000 q = F(t) 1007 assert allclose( 2*120**2/3 + 60**2/3, q[1] )1008 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )1001 assert numpy.allclose( 2*120**2/3 + 60**2/3, q[1] ) 1002 assert numpy.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 1009 1003 1010 1004 os.remove(filename + '.tms') … … 1055 1049 F = file_function(filename + '.tms', domain, 1056 1050 quantities = ['Attribute0', 'Attribute1', 'Attribute2']) 1057 assert allclose(domain.starttime, start+delta)1051 assert numpy.allclose(domain.starttime, start+delta) 1058 1052 1059 1053 … … 1066 1060 1067 1061 #Exact linear intpolation 1068 assert allclose(q[0], 2*t)1062 assert numpy.allclose(q[0], 2*t) 1069 1063 if i%6 == 0: 1070 assert allclose(q[1], t**2)1071 assert allclose(q[2], sin(t*pi/600))1064 assert numpy.allclose(q[1], t**2) 1065 assert numpy.allclose(q[2], sin(t*pi/600)) 1072 1066 1073 1067 #Check non-exact … … 1075 1069 t = 90 #Halfway between 60 and 120 1076 1070 q = F(t-delta) 1077 assert allclose( (120**2 + 60**2)/2, q[1] )1078 assert allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] )1071 assert numpy.allclose( (120**2 + 60**2)/2, q[1] ) 1072 assert numpy.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] ) 1079 1073 1080 1074 1081 1075 t = 100 #Two thirds of the way between between 60 and 120 1082 1076 q = F(t-delta) 1083 assert allclose( 2*120**2/3 + 60**2/3, q[1] )1084 assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )1077 assert numpy.allclose( 2*120**2/3 + 60**2/3, q[1] ) 1078 assert numpy.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] ) 1085 1079 1086 1080 … … 1094 1088 #FIXME: Division is not expected to work for integers. 1095 1089 #This must be caught. 1096 foo = array([[1,2,3], 1097 [4,5,6]], float) 1098 1099 bar = array([[-1,0,5], 1100 [6,1,1]], float) 1090 foo = numpy.array([[1,2,3], [4,5,6]], numpy.float) 1091 1092 bar = numpy.array([[-1,0,5], [6,1,1]], numpy.float) 1101 1093 1102 1094 D = {'X': foo, 'Y': bar} 1103 1095 1104 1096 Z = apply_expression_to_dictionary('X+Y', D) 1105 assert allclose(Z, foo+bar)1097 assert numpy.allclose(Z, foo+bar) 1106 1098 1107 1099 Z = apply_expression_to_dictionary('X*Y', D) 1108 assert allclose(Z, foo*bar)1100 assert numpy.allclose(Z, foo*bar) 1109 1101 1110 1102 Z = apply_expression_to_dictionary('4*X+Y', D) 1111 assert allclose(Z, 4*foo+bar)1103 assert numpy.allclose(Z, 4*foo+bar) 1112 1104 1113 1105 # test zero division is OK 1114 1106 Z = apply_expression_to_dictionary('X/Y', D) 1115 assert allclose(1/Z, 1/(foo/bar)) # can't compare inf to inf1107 assert numpy.allclose(1/Z, 1/(foo/bar)) # can't compare inf to inf 1116 1108 1117 1109 # make an error for zero on zero … … 1271 1263 tris = [[0,1,2]] 1272 1264 new_verts, new_tris = remove_lone_verts(verts, tris) 1273 assert alltrue(new_verts == verts)1274 assert alltrue(new_tris == tris)1265 assert numpy.alltrue(new_verts == verts) 1266 assert numpy.alltrue(new_tris == tris) 1275 1267 1276 1268 … … 1287 1279 new_verts, new_tris = remove_lone_verts(verts, tris) 1288 1280 #print "new_verts", new_verts 1289 assert alltrue(new_verts == [[0,0],[1,0],[0,1]])1290 assert alltrue(new_tris == [[0,1,2]])1281 assert numpy.alltrue(new_verts == [[0,0],[1,0],[0,1]]) 1282 assert numpy.alltrue(new_tris == [[0,1,2]]) 1291 1283 1292 1284 def test_remove_lone_verts_c(self): … … 1294 1286 tris = [[0,1,3]] 1295 1287 new_verts, new_tris = remove_lone_verts(verts, tris) 1296 print "new_verts", new_verts 1297 assert alltrue(new_verts == [[0,0],[1,0],[0,1]]) 1298 assert alltrue(new_tris == [[0,1,2]]) 1288 assert numpy.alltrue(new_verts == [[0,0],[1,0],[0,1]]) 1289 assert numpy.alltrue(new_tris == [[0,1,2]]) 1299 1290 1300 1291 def test_remove_lone_verts_b(self): … … 1302 1293 tris = [[0,1,2]] 1303 1294 new_verts, new_tris = remove_lone_verts(verts, tris) 1304 assert alltrue(new_verts == verts[0:3])1305 assert alltrue(new_tris == tris)1295 assert numpy.alltrue(new_verts == verts[0:3]) 1296 assert numpy.alltrue(new_tris == tris) 1306 1297 1307 1298 … … 1310 1301 tris = [[0,1,2]] 1311 1302 new_verts, new_tris = remove_lone_verts(verts, tris) 1312 assert alltrue(new_verts == verts[0:3])1313 assert alltrue(new_tris == tris)1303 assert numpy.alltrue(new_verts == verts[0:3]) 1304 assert numpy.alltrue(new_tris == tris) 1314 1305 1315 1306 def test_get_min_max_values(self): … … 1499 1490 line.append([float(row[0]),float(row[1]),float(row[2]),float(row[3]),float(row[4]),float(row[5])]) 1500 1491 #print 'assert line',line[i],'point1',point1_answers_array[i] 1501 assert allclose(line[i], point1_answers_array[i])1492 assert numpy.allclose(line[i], point1_answers_array[i]) 1502 1493 1503 1494 point2_answers_array = [[0.0,1.0,1.5,-0.5,3.0,4.0], [2.0,10.0,10.5,-0.5,3.0,4.0]] … … 1512 1503 line.append([float(row[0]),float(row[1]),float(row[2]),float(row[3]),float(row[4]),float(row[5])]) 1513 1504 #print 'assert line',line[i],'point1',point1_answers_array[i] 1514 assert allclose(line[i], point2_answers_array[i])1505 assert numpy.allclose(line[i], point2_answers_array[i]) 1515 1506 1516 1507 # clean up … … 1618 1609 line.append([float(row[0]),float(row[1]),float(row[2])]) 1619 1610 #print 'line',line[i],'point1',point1_answers_array[i] 1620 assert allclose(line[i], point1_answers_array[i])1611 assert numpy.allclose(line[i], point1_answers_array[i]) 1621 1612 1622 1613 point2_answers_array = [[0.0,1.0,-0.5], [2.0,10.0,-0.5]] … … 1631 1622 line.append([float(row[0]),float(row[1]),float(row[2])]) 1632 1623 # print 'line',line[i],'point1',point1_answers_array[i] 1633 assert allclose(line[i], point2_answers_array[i])1624 assert numpy.allclose(line[i], point2_answers_array[i]) 1634 1625 1635 1626 # clean up … … 1737 1728 line.append([float(row[0]),float(row[1]),float(row[2]),float(row[3]),float(row[4]),float(row[5])]) 1738 1729 #print 'assert line',line[i],'point1',point1_answers_array[i] 1739 assert allclose(line[i], point1_answers_array[i])1730 assert numpy.allclose(line[i], point1_answers_array[i]) 1740 1731 1741 1732 point2_answers_array = [[5.0,1.0,1.5,-0.5,3.0,4.0], [7.0,10.0,10.5,-0.5,3.0,4.0]] … … 1750 1741 line.append([float(row[0]),float(row[1]),float(row[2]),float(row[3]),float(row[4]),float(row[5])]) 1751 1742 #print 'assert line',line[i],'point1',point1_answers_array[i] 1752 assert allclose(line[i], point2_answers_array[i])1743 assert numpy.allclose(line[i], point2_answers_array[i]) 1753 1744 1754 1745 # clean up … … 1833 1824 if __name__ == "__main__": 1834 1825 suite = unittest.makeSuite(Test_Util,'test') 1835 # suite = unittest.makeSuite(Test_Util,'test_s ww2csv')1826 # suite = unittest.makeSuite(Test_Util,'test_spatio_temporal_file_function_basic') 1836 1827 # runner = unittest.TextTestRunner(verbosity=2) 1837 1828 runner = unittest.TextTestRunner(verbosity=1) -
anuga_core/source_numpy_conversion/anuga/abstract_2d_finite_volumes/util.py
r5899 r5903 1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py2 3 1 """This module contains various auxiliary function used by pyvolution. 4 2 … … 17 15 18 16 from anuga.utilities.numerical_tools import ensure_numeric 19 from numpy.oldnumeric import arange, choose, zeros, Float, array, allclose, take, compress 17 import numpy 20 18 21 19 from anuga.geospatial_data.geospatial_data import ensure_absolute … … 239 237 from anuga.config import time_format 240 238 from Scientific.IO.NetCDF import NetCDFFile 241 from numpy.oldnumeric import array, zeros, Float, alltrue, concatenate, reshape239 ## from numpy.oldnumeric import array, zeros, Float, alltrue, concatenate, reshape 242 240 243 241 # Open NetCDF file … … 305 303 # Get variables 306 304 # if verbose: print 'Get variables' 307 time = array(fid.variables['time'][:] )305 time = numpy.array(fid.variables['time'][:] ) 308 306 309 307 # Get time independent stuff … … 319 317 triangles = fid.variables['volumes'][:] 320 318 321 x = reshape(x, (len(x),1))322 y = reshape(y, (len(y),1))323 vertex_coordinates = concatenate((x,y), axis=1) #m x 2 array319 x = numpy.reshape(x, (len(x),1)) 320 y = numpy.reshape(y, (len(y),1)) 321 vertex_coordinates = numpy.concatenate((x,y), axis=1) #m x 2 array 324 322 325 323 if boundary_polygon is not None: … … 333 331 for i in range(len(boundary_polygon)): 334 332 for j in range(len(x)): 335 if allclose(vertex_coordinates[j],boundary_polygon[i],1e-4):333 if numpy.allclose(vertex_coordinates[j],boundary_polygon[i],1e-4): 336 334 #FIX ME: 337 335 #currently gauges lat and long is stored as float and … … 353 351 gauge_neighbour_id.append(-1) 354 352 gauge_neighbour_id=ensure_numeric(gauge_neighbour_id) 355 if len( compress(gauge_neighbour_id>=0,gauge_neighbour_id))!=len(temp)-1:353 if len(numpy.compress(gauge_neighbour_id>=0,gauge_neighbour_id))!=len(temp)-1: 356 354 msg='incorrect number of segments' 357 355 raise msg … … 404 402 if boundary_polygon is not None: 405 403 #removes sts points that do not lie on boundary 406 quantities[name] = take(quantities[name],gauge_id,1)404 quantities[name] = numpy.take(quantities[name], gauge_id, 1) 407 405 408 406 # Close sww, tms or sts netcdf file … … 526 524 raise 'Illegal input to get_textual_float:', value 527 525 else: 528 return format % float(value)526 return format % float(value) 529 527 530 528 … … 1042 1040 """ 1043 1041 # from math import sqrt, atan, degrees 1044 from numpy.oldnumeric import ones, allclose, zeros, Float, ravel1042 ## from numpy.oldnumeric import ones, allclose, zeros, Float, ravel 1045 1043 from os import sep, altsep, getcwd, mkdir, access, F_OK, environ 1046 1044 … … 1083 1081 n0 = int(n0) 1084 1082 m = len(locations) 1085 model_time = zeros((n0,m,p), Float)1086 stages = zeros((n0,m,p), Float)1087 elevations = zeros((n0,m,p), Float)1088 momenta = zeros((n0,m,p), Float)1089 xmom = zeros((n0,m,p), Float)1090 ymom = zeros((n0,m,p), Float)1091 speed = zeros((n0,m,p), Float)1092 bearings = zeros((n0,m,p), Float)1093 due_east = 90.0* ones((n0,1), Float)1094 due_west = 270.0* ones((n0,1), Float)1095 depths = zeros((n0,m,p), Float)1096 eastings = zeros((n0,m,p), Float)1083 model_time = numpy.zeros((n0,m,p), numpy.float) 1084 stages = numpy.zeros((n0,m,p), numpy.float) 1085 elevations = numpy.zeros((n0,m,p), numpy.float) 1086 momenta = numpy.zeros((n0,m,p), numpy.float) 1087 xmom = numpy.zeros((n0,m,p), numpy.float) 1088 ymom = numpy.zeros((n0,m,p), numpy.float) 1089 speed = numpy.zeros((n0,m,p), numpy.float) 1090 bearings = numpy.zeros((n0,m,p), numpy.float) 1091 due_east = 90.0*numpy.ones((n0,1), numpy.float) 1092 due_west = 270.0*numpy.ones((n0,1), numpy.float) 1093 depths = numpy.zeros((n0,m,p), numpy.float) 1094 eastings = numpy.zeros((n0,m,p), numpy.float) 1097 1095 min_stages = [] 1098 1096 max_stages = [] … … 1106 1104 min_speeds = [] 1107 1105 max_depths = [] 1108 model_time_plot3d = zeros((n0,m), Float)1109 stages_plot3d = zeros((n0,m), Float)1110 eastings_plot3d = zeros((n0,m),Float)1106 model_time_plot3d = numpy.zeros((n0,m), numpy.float) 1107 stages_plot3d = numpy.zeros((n0,m), numpy.float) 1108 eastings_plot3d = numpy.zeros((n0,m),numpy.float) 1111 1109 if time_unit is 'mins': scale = 60.0 1112 1110 if time_unit is 'hours': scale = 3600.0 … … 1208 1206 else: 1209 1207 #ax.plot_wireframe(model_time[:,:,j],eastings[:,:,j],stages[:,:,j]) 1210 ax.plot3D( ravel(eastings[:,:,j]),ravel(model_time[:,:,j]),ravel(stages[:,:,j]))1208 ax.plot3D(numpy.ravel(eastings[:,:,j]),numpy.ravel(model_time[:,:,j]),numpy.ravel(stages[:,:,j])) 1211 1209 ax.set_xlabel('time') 1212 1210 ax.set_ylabel('x') … … 1507 1505 1508 1506 # initialise the array to easily find the index of the first loner 1509 loners= arange(2*N, N, -1) # if N=3 [6,5,4]1507 loners=numpy.arange(2*N, N, -1) # if N=3 [6,5,4] 1510 1508 for t in triangles: 1511 1509 for vert in t: … … 1541 1539 #print "loners", loners 1542 1540 #print "triangles before", triangles 1543 triangles = choose(triangles,loners)1541 triangles = numpy.choose(triangles,loners) 1544 1542 #print "triangles after", triangles 1545 1543 return verts, triangles … … 1556 1554 1557 1555 1558 xc = zeros(triangles.shape[0], Float) # Space for centroid info1556 xc = numpy.zeros(triangles.shape[0], numpy.float) # Space for centroid info 1559 1557 1560 1558 for k in range(triangles.shape[0]): … … 1838 1836 #add tide to stage if provided 1839 1837 if quantity == 'stage': 1840 quantity_value[quantity]= array(quantity_value[quantity])+directory_add_tide1838 quantity_value[quantity]=numpy.array(quantity_value[quantity])+directory_add_tide 1841 1839 1842 1840 #condition to find max and mins for all the plots … … 1949 1947 #get data from dict in to list 1950 1948 #do maths to list by changing to array 1951 t=( array(directory_quantity_value[directory][filename]['time'])+directory_start_time)/seconds_in_minutes1949 t=(numpy.array(directory_quantity_value[directory][filename]['time'])+directory_start_time)/seconds_in_minutes 1952 1950 1953 1951 #finds the maximum elevation, used only as a test … … 2130 2128 from csv import reader,writer 2131 2129 from anuga.utilities.numerical_tools import ensure_numeric, mean, NAN 2132 from numpy.oldnumeric import array, resize, shape, Float, zeros, take, argsort, argmin2130 ## from numpy.oldnumeric import array, resize, shape, Float, zeros, take, argsort, argmin 2133 2131 import string 2134 2132 from anuga.shallow_water.data_manager import get_all_swwfiles … … 2171 2169 2172 2170 #convert to array for file_function 2173 points_array = array(points,Float)2171 points_array = numpy.array(points, numpy.float) 2174 2172 2175 2173 points_array = ensure_absolute(points_array)
Note: See TracChangeset
for help on using the changeset viewer.