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

Last change on this file since 1 was 1, checked in by duncan, 20 years ago

initial import

File size: 2.7 KB
Line 
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
12    def __init__(self, coordinates, conserved_quantities = None,
13                 other_quantities = None):
14        """
15        Build 1D elements from x coordinates
16        """
17
18        from Numeric import array, zeros, Float, Int
19
20        self.beta = 1.0
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
29        self.vertices  = zeros((N, 2), Float)
30        self.centroids = zeros(N, Float)
31        self.areas     = zeros(N, Float)
32        for i in range(N):
33            xl = self.coordinates[i]
34            xr = self.coordinates[i+1]
35            self.vertices[i,0] = xl
36            self.vertices[i,1] = xr
37           
38            centroid = (xl+xr)/2
39            self.centroids[i] = centroid
40
41            msg = 'Coordinates should be ordered, smallest to largest'
42            assert xr>xl, msg
43
44            self.areas[i] = (xr-xl)
45
46##         print 'N', N
47##         print 'Centroid', self.centroids
48##         print 'Areas', self.areas
49##         print 'Vertex_Coordinates', self.vertices
50       
51    def get_centroids(self):
52        """Return all coordinates of centroids
53        Return x coordinate of centroid for each element as a N array
54        """
55       
56        return self.centroids
57
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        """
62
63        return self.vertices
64
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:
72            return self.centroids[elem_id]
73        else:
74            return self.vertices[elem_id,vertex]
75
76    def get_area(self, elem_id):
77        """Return area of element id
78        """
79
80        return self.areas[elem_id]
81   
82
83if __name__ == "__main__":
84
85    points1 = [0.0, 1.0, 2.0, 3.0]
86    D1 = Domain(points1)
87
88    print D1.get_coordinate(0)
89    print D1.get_coordinate(0,1)
90
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)
101
102   
103
104
105
106
107
Note: See TracBrowser for help on using the repository browser.