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

Last change on this file since 296 was 279, checked in by steve, 21 years ago

Working on Quantities

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        #Store Points
21        self.coordinates = array(coordinates)
22
23        #Register number of Elements
24        self.number_of_elements = N = len(self.coordinates)-1
25       
26
27        #Allocate space for geometric quantities
28        self.vertices  = zeros((N, 2), Float)
29        self.centroids = zeros(N, Float)
30        self.areas     = zeros(N, Float)
31        for i in range(N):
32            xl = self.coordinates[i]
33            xr = self.coordinates[i+1]
34            self.vertices[i,0] = xl
35            self.vertices[i,1] = xr
36           
37            centroid = (xl+xr)/2
38            self.centroids[i] = centroid
39
40            msg = 'Coordinates should be ordered, smallest to largest'
41            assert xr>xl, msg
42
43            self.areas[i] = (xr-xl)
44
45##         print 'N', N
46##         print 'Centroid', self.centroids
47##         print 'Areas', self.areas
48##         print 'Vertex_Coordinates', self.vertices
49       
50    def get_centroids(self):
51        """Return all coordinates of centroids
52        Return x coordinate of centroid for each element as a N array
53        """
54       
55        return self.centroids
56
57    def get_vertices(self):
58        """Return all coordinates of centroids
59        Return x coordinate of centroid for each element as a N array
60        """
61
62        return self.vertices
63
64    def get_coordinate(self, elem_id, vertex=None):
65        """Return coordinate of centroid,
66        or left or right vertex.
67        Left vertex (vertex=0). Right vertex (vertex=1)
68        """
69
70        if vertex is None:
71            return self.centroids[elem_id]
72        else:
73            return self.vertices[elem_id,vertex]
74
75    def get_area(self, elem_id):
76        """Return area of element id
77        """
78
79        return self.areas[elem_id]
80   
81
82if __name__ == "__main__":
83
84    points1 = [0.0, 1.0, 2.0, 3.0]
85    D1 = Domain(points1)
86
87    print D1.get_coordinate(0)
88    print D1.get_coordinate(0,1)
89
90    try:
91        print D1.get_coordinate(3)
92    except:
93        pass
94    else:
95        msg =  'Should have raised an out of bounds exception'
96    raise msg
97   
98    #points2 = [0.0, 1.0, 2.0, 3.0, 2.5]
99    #D2 = Domain(points2)
100
101   
102
103
104
105
106
Note: See TracBrowser for help on using the repository browser.