Changeset 322


Ignore:
Timestamp:
Sep 20, 2004, 10:18:06 AM (20 years ago)
Author:
duncan
Message:

mesh inherits from general_mesh

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/general_mesh.py

    r321 r322  
    6969       
    7070        self.normals = zeros((N, 6), Float)
     71        self.areas = zeros(N, Float)
     72        self.edgelengths = zeros((N, 3), Float)
    7173
    7274        #Get x,y coordinates for all triangles and store
     
    8284
    8385            #Area
    84             area = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2
     86            self.areas[i] = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2
    8587
    8688            msg = 'Triangle (%f,%f), (%f,%f), (%f, %f)' %(x0,y0,x1,y1,x2,y2)
    87             msg += ' is degenerate:  area == %f' %area
    88             assert area > 0.0, msg
     89            msg += ' is degenerate:  area == %f' %self.areas[i]
     90            assert self.areas[i] > 0.0, msg
    8991       
    9092
     
    117119                                  n1[1], -n1[0],
    118120                                  n2[1], -n2[0]]
     121           
     122            #Edgelengths
     123            self.edgelengths[i, :] = [l0, l1, l2]
    119124           
    120125    def __len__(self):
  • inundation/ga/storm_surge/pyvolution/mesh.py

    r320 r322  
    55"""
    66
    7 
    8 class Mesh:
     7from general_mesh import General_Mesh
     8
     9class Mesh(General_Mesh):
    910    """Collection of triangular elements (purely geometric)
    1011
     
    6566        from Numeric import array, zeros, Int, Float, maximum, sqrt, sum
    6667
    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       
    8472        #Allocate space for geometric quantities
    8573        self.centroid_coordinates = zeros((N, 2), Float)
    8674
    87        
    88         self.areas = zeros(N, Float)
    8975        self.radii = zeros(N, Float)
    90         self.edgelengths = zeros((N, 3), Float)
    9176
    9277        self.neighbours = zeros((N, 3), Int)
     
    9580        self.surrogate_neighbours = zeros((N, 3), Int)       
    9681       
    97         self.normals = zeros((N, 6), Float)
    98 
    9982        #Get x,y coordinates for all triangles and store
    100         self.vertex_coordinates = V = self.compute_vertex_coordinates()
     83        V = self.vertex_coordinates
    10184       
    10285        #Initialise each triangle
     
    11295            self.centroid_coordinates[i] = centroid
    11396
    114             #Area
    115             self.areas[i] = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2
    116 
    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, msg
    120        
    12197            #Midpoints       
    12298            m0 = array([(x1 + x2)/2, (y1 + y2)/2])
     
    131107            d2 = sqrt(sum( (centroid-m2)**2 ))
    132108       
    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)               
    166110
    167111            #Initialise Neighbours (-1 means that it is a boundary neighbour)
     
    189133
    190134       
    191     def __len__(self):
    192         return self.number_of_elements
    193 
    194135    def __repr__(self):
    195136        return 'Mesh: %d triangles, %d elements, %d boundary segments'\
     
    498439        #for id, edge in self.boundary:
    499440        #    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 array
    507         (ordered as x0, y0, x1, y1, x2, y2 for each triangle)       
    508         """
    509         return self.normals
    510 
    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 array
    523         (ordered as x0, y0, x1, y1, x2, y2 for each triangle)       
    524         """
    525         return self.vertex_coordinates
    526 
    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 array
    537         (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_values
    542        
    543         from Numeric import zeros, Float
    544 
    545         N = self.number_of_elements
    546         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 0
    551                 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_coordinates
    556  
    557     def get_triangles(self, unique=False):
    558         """Get connectivity
    559         If unique is True give them only once as stored internally.
    560         If unique is False return
    561         """
    562 
    563         if unique is True:
    564             return self.triangles
    565         else:
    566             from Numeric import reshape, array, Int
    567                
    568             m = len(self)  #Number of volumes
    569             M = 3*m        #Total number of unique vertices
    570             return reshape(array(range(M)).astype(Int), (m,3))
    571 
    572 
    573441
    574442#FIXME: May get rid of
  • inundation/ga/storm_surge/pyvolution/test_mesh.py

    r305 r322  
    11#!/usr/bin/env python
     2
     3 #FIXME: Seperate the tests for mesh and general_mesh
    24
    35import unittest
     
    4850        #Area
    4951        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]
    5153
    5254        #Normals
Note: See TracChangeset for help on using the changeset viewer.