Changeset 256


Ignore:
Timestamp:
Aug 31, 2004, 5:51:05 PM (20 years ago)
Author:
steve
Message:
 
Location:
inundation/ga/storm_surge/pyvolution-1d
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution-1d/domain.py

    r214 r256  
    1010class Domain:
    1111
    12     def __init__(self, coordinates):
     12    def __init__(self, coordinates, conserved_quantities = None,
     13                 other_quantities = None):
    1314        """
    1415        Build 1D elements from x coordinates
     
    2526
    2627        #Allocate space for geometric quantities
    27         self.edge_coordinates = zeros((N, 2), Float)
    28         self.centroids = zeros(N, Float)
    29         self.areas = zeros(N, Float)
     28        self.vertex_coordinates  = zeros((N, 2), Float)
     29        self.centroid_coordinates = zeros(N, Float)
     30        self.areas                = zeros(N, Float)
    3031        for i in range(N):
    3132            xl = self.coordinates[i]
    3233            xr = self.coordinates[i+1]
    33             self.edge_coordinates[i,0] = xl
    34             self.edge_coordinates[i,1] = xr
     34            self.vertex_coordinates[i,0] = xl
     35            self.vertex_coordinates[i,1] = xr
    3536           
    3637            centroid = (xl+xr)/2
    37             self.centroids[i] = centroid
     38            self.centroid_coordinates[i] = centroid
    3839
    3940            msg = 'Coordinates should be ordered, smallest to largest'
    4041            assert xr>xl, msg
     42
    4143            self.areas[i] = (xr-xl)
    42 
    4344
    4445
    4546        print 'N', N
    4647        print 'Coordinates', self.coordinates
    47         print 'Centroid', self.centroids
     48        print 'Centroid', self.centroid_coordinates
    4849        print 'Areas', self.areas
    49         print 'Edge_Coordinates', self.edge_coordinates
     50        print 'Vertex_Coordinates', self.vertex_coordinates
    5051       
     52    def get_centroid_coordinates(self):
     53        """Return all coordinates of centroids
     54        Return x coordinate of centroid for each element as a N array
     55        """
     56
     57        return self.centroid_coordinates
     58
     59    def get_coordinate(self, elem_id, vertex=None):
     60        """Return coordinate of centroid,
     61        or left or right vertex.
     62        Left vertex (vertex=0). Right vertex (vertex=1)
     63        """
     64
     65        if vertex is None:
     66            return self.centroid_coordinates[elem_id]
     67        else:
     68            return self.vertex_coordinates[elem_id,vertex]
    5169       
    52     def get_centroids()
    5370
    5471if __name__ == "__main__":
     72
    5573    points1 = [0.0, 1.0, 2.0, 3.0]
    5674    D1 = Domain(points1)
    5775
    58     points2 = [0.0, 1.0, 2.0, 3.0, 2.5]
    59     D2 = Domain(points2)
     76    print D1.get_coordinate(0)
     77    print D1.get_coordinate(0,1)
     78
     79    try:
     80        print D1.get_coordinate(3)
     81    except:
     82        pass
     83    else:
     84        msg =  'Should have raised an out of bounds exception'
     85    raise msg
     86   
     87    #points2 = [0.0, 1.0, 2.0, 3.0, 2.5]
     88    #D2 = Domain(points2)
     89
     90   
    6091
    6192
  • inundation/ga/storm_surge/pyvolution-1d/test_domain.py

    r214 r256  
    1010class TestCase(unittest.TestCase):
    1111    def setUp(self):
    12         pass
    13 
    1412       
    15     def tearDown(self):
    16         pass
    17 
    18     def test_construct(self):
    1913        a = 0.0
    2014        b = 2.0
     
    2620        points = [a, b, c, d, e, f]
    2721
    28         domain = Domain(points)
     22        self.points = [a, b, c, d, e, f]
     23        self.domain = Domain(self.points)
    2924
     25       
     26    def tearDown(self):
     27        pass
     28
     29
     30    def test_construct(self):
     31
     32        domain = self.domain
    3033        centroids = domain.get_centroids()
    3134        assert allclose(centroids, [1.0, 2.25, 2.75, 6.5, 11.0])
     
    4851            msg = 'Should have raised exception'
    4952            raise msg
     53
     54    def test_point(self):
     55        a = 0.0
     56        b = 2.0
     57        c = 2.5
     58        d = 3.0
     59
     60        points = [a, b, c, d]
     61
     62        domain = Domain(points)
     63
     64        assert allclose(domain.get_point(0), 0.5*(a+b))
     65        assert allclose(domain.get_point(1), 0.5*(b+c))
     66        assert allclose(domain.get_point(2), 0.5*(c+d))
    5067       
     68        try:
     69            allclose(domain.get_point(3), 0.5*(a+b))
     70        except:
     71            pass
     72        else:
     73            msg = 'Should have raised exception'
     74            raise msg
     75       
     76        assert allclose(domain.get_point(0,0), a)
     77        assert allclose(domain.get_point(1,0), b)
     78        assert allclose(domain.get_point(2,0), c)
     79
     80        assert allclose(domain.get_point(0,1), b)
     81        assert allclose(domain.get_point(1,1), c)
     82        assert allclose(domain.get_point(2,1), d)
     83
     84
    5185       
    5286       
Note: See TracChangeset for help on using the changeset viewer.