source: inundation/ga/storm_surge/pyvolution-1d/domain.py @ 643

Last change on this file since 643 was 335, checked in by steve, 20 years ago

Limit and gradient aded to quantity

File size: 2.7 KB
RevLine 
[214]1"""Class Domain - 1D domains for finite-volume computations of
2   the shallow water wave equation
3
4
5   Copyright 2004
6   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
7   Geoscience Australia
8"""
9
10class Domain:
11
[256]12    def __init__(self, coordinates, conserved_quantities = None,
13                 other_quantities = None):
[214]14        """
15        Build 1D elements from x coordinates
16        """
17
18        from Numeric import array, zeros, Float, Int
[335]19
20        self.beta = 1.0
[214]21        #Store Points
22        self.coordinates = array(coordinates)
23
24        #Register number of Elements
25        self.number_of_elements = N = len(self.coordinates)-1
26       
27
28        #Allocate space for geometric quantities
[279]29        self.vertices  = zeros((N, 2), Float)
30        self.centroids = zeros(N, Float)
31        self.areas     = zeros(N, Float)
[214]32        for i in range(N):
33            xl = self.coordinates[i]
34            xr = self.coordinates[i+1]
[279]35            self.vertices[i,0] = xl
36            self.vertices[i,1] = xr
[214]37           
38            centroid = (xl+xr)/2
[279]39            self.centroids[i] = centroid
[214]40
41            msg = 'Coordinates should be ordered, smallest to largest'
42            assert xr>xl, msg
[256]43
[214]44            self.areas[i] = (xr-xl)
45
[279]46##         print 'N', N
47##         print 'Centroid', self.centroids
48##         print 'Areas', self.areas
49##         print 'Vertex_Coordinates', self.vertices
[214]50       
[279]51    def get_centroids(self):
[256]52        """Return all coordinates of centroids
53        Return x coordinate of centroid for each element as a N array
54        """
[279]55       
56        return self.centroids
[256]57
[279]58    def get_vertices(self):
59        """Return all coordinates of centroids
60        Return x coordinate of centroid for each element as a N array
61        """
[256]62
[279]63        return self.vertices
64
[256]65    def get_coordinate(self, elem_id, vertex=None):
66        """Return coordinate of centroid,
67        or left or right vertex.
68        Left vertex (vertex=0). Right vertex (vertex=1)
69        """
70
71        if vertex is None:
[279]72            return self.centroids[elem_id]
[256]73        else:
[279]74            return self.vertices[elem_id,vertex]
[214]75
[279]76    def get_area(self, elem_id):
77        """Return area of element id
78        """
79
80        return self.areas[elem_id]
81   
82
[214]83if __name__ == "__main__":
[256]84
[214]85    points1 = [0.0, 1.0, 2.0, 3.0]
86    D1 = Domain(points1)
87
[256]88    print D1.get_coordinate(0)
89    print D1.get_coordinate(0,1)
[214]90
[256]91    try:
92        print D1.get_coordinate(3)
93    except:
94        pass
95    else:
96        msg =  'Should have raised an out of bounds exception'
97    raise msg
98   
99    #points2 = [0.0, 1.0, 2.0, 3.0, 2.5]
100    #D2 = Domain(points2)
[214]101
[256]102   
[214]103
104
105
[256]106
107
Note: See TracBrowser for help on using the repository browser.