Changeset 322
- Timestamp:
- Sep 20, 2004, 10:18:06 AM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/general_mesh.py
r321 r322 69 69 70 70 self.normals = zeros((N, 6), Float) 71 self.areas = zeros(N, Float) 72 self.edgelengths = zeros((N, 3), Float) 71 73 72 74 #Get x,y coordinates for all triangles and store … … 82 84 83 85 #Area 84 area= abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/286 self.areas[i] = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2 85 87 86 88 msg = 'Triangle (%f,%f), (%f,%f), (%f, %f)' %(x0,y0,x1,y1,x2,y2) 87 msg += ' is degenerate: area == %f' % area88 assert area> 0.0, msg89 msg += ' is degenerate: area == %f' %self.areas[i] 90 assert self.areas[i] > 0.0, msg 89 91 90 92 … … 117 119 n1[1], -n1[0], 118 120 n2[1], -n2[0]] 121 122 #Edgelengths 123 self.edgelengths[i, :] = [l0, l1, l2] 119 124 120 125 def __len__(self): -
inundation/ga/storm_surge/pyvolution/mesh.py
r320 r322 5 5 """ 6 6 7 8 class Mesh: 7 from general_mesh import General_Mesh 8 9 class Mesh(General_Mesh): 9 10 """Collection of triangular elements (purely geometric) 10 11 … … 65 66 from Numeric import array, zeros, Int, Float, maximum, sqrt, sum 66 67 67 self.triangles = array(triangles).astype(Int) 68 self.coordinates = array(coordinates) 69 70 #Input checks 71 msg = 'Triangles must an Nx2 Numeric array or a sequence of 2-tuples' 72 assert len(self.triangles.shape) == 2, msg 73 74 msg = 'Coordinates must an Mx2 Numeric array or a sequence of 2-tuples' 75 assert len(self.coordinates.shape) == 2, msg 76 77 msg = 'Vertex indices reference non-existing coordinate sets' 78 assert max(max(self.triangles)) <= self.coordinates.shape[0], msg 79 80 81 #Register number of elements (N) 82 self.number_of_elements = N = self.triangles.shape[0] 83 68 General_Mesh.__init__(self,coordinates, triangles) 69 70 N = self.number_of_elements 71 84 72 #Allocate space for geometric quantities 85 73 self.centroid_coordinates = zeros((N, 2), Float) 86 74 87 88 self.areas = zeros(N, Float)89 75 self.radii = zeros(N, Float) 90 self.edgelengths = zeros((N, 3), Float)91 76 92 77 self.neighbours = zeros((N, 3), Int) … … 95 80 self.surrogate_neighbours = zeros((N, 3), Int) 96 81 97 self.normals = zeros((N, 6), Float)98 99 82 #Get x,y coordinates for all triangles and store 100 self.vertex_coordinates = V = self.compute_vertex_coordinates()83 V = self.vertex_coordinates 101 84 102 85 #Initialise each triangle … … 112 95 self.centroid_coordinates[i] = centroid 113 96 114 #Area115 self.areas[i] = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2116 117 msg = 'Triangle (%f,%f), (%f,%f), (%f, %f)' %(x0,y0,x1,y1,x2,y2)118 msg += ' is degenerate: area == %f' %self.areas[i]119 assert self.areas[i] > 0.0, msg120 121 97 #Midpoints 122 98 m0 = array([(x1 + x2)/2, (y1 + y2)/2]) … … 131 107 d2 = sqrt(sum( (centroid-m2)**2 )) 132 108 133 self.radii[i] = min(d0, d1, d2) 134 135 #Normals 136 #The normal vectors 137 # - point outward from each edge 138 # - are orthogonal to the edge 139 # - have unit length 140 # - Are enumerated according to the opposite corner: 141 # (First normal is associated with the edge opposite 142 # the first vertex, etc) 143 # - Stored as six floats n0x,n0y,n1x,n1y,n2x,n2y per triangle 144 145 n0 = array([x2 - x1, y2 - y1]) 146 l0 = sqrt(sum(n0**2)) 147 148 n1 = array([x0 - x2, y0 - y2]) 149 l1 = sqrt(sum(n1**2)) 150 151 n2 = array([x1 - x0, y1 - y0]) 152 l2 = sqrt(sum(n2**2)) 153 154 #Normalise 155 n0 /= l0 156 n1 /= l1 157 n2 /= l2 158 159 #Compute and store 160 self.normals[i, :] = [n0[1], -n0[0], 161 n1[1], -n1[0], 162 n2[1], -n2[0]] 163 164 #Edgelengths 165 self.edgelengths[i, :] = [l0, l1, l2] 109 self.radii[i] = min(d0, d1, d2) 166 110 167 111 #Initialise Neighbours (-1 means that it is a boundary neighbour) … … 189 133 190 134 191 def __len__(self):192 return self.number_of_elements193 194 135 def __repr__(self): 195 136 return 'Mesh: %d triangles, %d elements, %d boundary segments'\ … … 498 439 #for id, edge in self.boundary: 499 440 # assert self.neighbours[id,edge] < 0 500 501 502 503 504 def get_normals(self):505 """Return all normal vectors.506 Return normal vectors for all triangles as an Nx6 array507 (ordered as x0, y0, x1, y1, x2, y2 for each triangle)508 """509 return self.normals510 511 512 def get_normal(self, i, j):513 """Return normal vector j of the i'th triangle.514 Return value is the numeric array slice [x, y]515 """516 return self.normals[i, 2*j:2*j+2]517 518 519 520 def get_vertex_coordinates(self):521 """Return all vertex coordinates.522 Return all vertex coordinates for all triangles as an Nx6 array523 (ordered as x0, y0, x1, y1, x2, y2 for each triangle)524 """525 return self.vertex_coordinates526 527 528 def get_vertex_coordinate(self, i, j):529 """Return coordinates for vertex j of the i'th triangle.530 Return value is the numeric array slice [x, y]531 """532 return self.vertex_coordinates[i, 2*j:2*j+2]533 534 535 def compute_vertex_coordinates(self):536 """Return vertex coordinates for all triangles as an Nx6 array537 (ordered as x0, y0, x1, y1, x2, y2 for each triangle)538 """539 540 #FIXME: Perhaps they should be ordered as in obj files??541 #See quantity.get_vertex_values542 543 from Numeric import zeros, Float544 545 N = self.number_of_elements546 vertex_coordinates = zeros((N, 6), Float)547 548 for i in range(N):549 for j in range(3):550 k = self.triangles[i,j] #Index of vertex 0551 v_k = self.coordinates[k]552 vertex_coordinates[i, 2*j+0] = v_k[0]553 vertex_coordinates[i, 2*j+1] = v_k[1]554 555 return vertex_coordinates556 557 def get_triangles(self, unique=False):558 """Get connectivity559 If unique is True give them only once as stored internally.560 If unique is False return561 """562 563 if unique is True:564 return self.triangles565 else:566 from Numeric import reshape, array, Int567 568 m = len(self) #Number of volumes569 M = 3*m #Total number of unique vertices570 return reshape(array(range(M)).astype(Int), (m,3))571 572 573 441 574 442 #FIXME: May get rid of -
inundation/ga/storm_surge/pyvolution/test_mesh.py
r305 r322 1 1 #!/usr/bin/env python 2 3 #FIXME: Seperate the tests for mesh and general_mesh 2 4 3 5 import unittest … … 48 50 #Area 49 51 assert mesh.areas[0] == 6.0,\ 50 'Area was %f, should have been 6.0' % triangle.get_area()52 'Area was %f, should have been 6.0' %mesh.areas[0] 51 53 52 54 #Normals
Note: See TracChangeset
for help on using the changeset viewer.