source: trunk/anuga_core/source/anuga/structures/inlet.py @ 8007

Last change on this file since 8007 was 8003, checked in by habili, 12 years ago

fix for is_inside_polygon

File size: 7.3 KB
Line 
1from anuga.geometry.polygon import inside_polygon, is_inside_polygon
2from anuga.config import velocity_protection, g
3import math
4
5import numpy as num
6
7class Inlet:
8    """Contains information associated with each inlet
9    """
10
11    def __init__(self, domain, polygon, enquiry_pt,  outward_culvert_vector=None, verbose=False):
12
13        self.domain = domain
14        self.domain_bounding_polygon = self.domain.get_boundary_polygon()
15        self.polygon = polygon
16        self.enquiry_pt = enquiry_pt
17        self.outward_culvert_vector = outward_culvert_vector
18        self.verbose = verbose
19
20        self.compute_indices()
21        self.compute_area()
22
23
24    def compute_indices(self):
25
26        # Get boundary (in absolute coordinates)
27        bounding_polygon = self.domain_bounding_polygon
28        domain_centroids = self.domain.get_centroid_coordinates(absolute=True)
29
30
31        # Check that polygon lies within the mesh.
32        for point in self.polygon:
33                msg = 'Point %s ' %  str(point)
34                msg += ' did not fall within the domain boundary.'
35                assert is_inside_polygon(point, bounding_polygon), msg
36               
37        point = self.enquiry_pt
38        msg = 'Enquiry Point %s ' %  str(point)
39        msg += ' did not fall within the domain boundary.'
40        assert is_inside_polygon(point, bounding_polygon), msg
41
42
43        self.triangle_indices = inside_polygon(domain_centroids, self.polygon, verbose=self.verbose)
44
45        if len(self.triangle_indices) == 0:
46            region = 'Inlet polygon=%s' % (self.polygon)
47            msg = 'No triangles have been identified in region '
48            raise Exception, msg
49
50        self.enquiry_index = self.domain.get_triangle_containing_point(self.enquiry_pt)
51
52
53
54    def compute_area(self):
55       
56        # Compute inlet area as the sum of areas of triangles identified
57        # by polygon. Must be called after compute_inlet_triangle_indices().
58        if len(self.triangle_indices) == 0:
59            region = 'Inlet polygon=%s' % (self.inlet_polygon)
60            msg = 'No triangles have been identified in region '
61            raise Exception, msg
62       
63        self.area = 0.0
64        for j in self.triangle_indices:
65            self.area += self.domain.areas[j]
66
67        msg = 'Inlet exchange area has area = %f' % self.area
68        assert self.area > 0.0
69
70
71    def get_area(self):
72
73        return self.area
74
75   
76    def get_areas(self):
77       
78        # Must be called after compute_inlet_triangle_indices().
79        return self.domain.areas.take(self.triangle_indices)
80   
81       
82    def get_stages(self):
83       
84        return self.domain.quantities['stage'].centroid_values.take(self.triangle_indices)
85       
86       
87    def get_average_stage(self):
88
89        return num.sum(self.get_stages()*self.get_areas())/self.area
90       
91    def get_elevations(self):   
92       
93        return self.domain.quantities['elevation'].centroid_values.take(self.triangle_indices)
94       
95    def get_average_elevation(self):
96       
97
98        return num.sum(self.get_elevations()*self.get_areas())/self.area
99   
100   
101    def get_xmoms(self):
102   
103        return self.domain.quantities['xmomentum'].centroid_values.take(self.triangle_indices)
104       
105       
106    def get_average_xmom(self):
107
108        return num.sum(self.get_xmoms()*self.get_areas())/self.area
109       
110   
111    def get_ymoms(self):
112       
113        return self.domain.quantities['ymomentum'].centroid_values.take(self.triangle_indices)
114 
115 
116    def get_average_ymom(self):
117       
118        return num.sum(self.get_ymoms()*self.get_areas())/self.area
119   
120
121    def get_heights(self):
122   
123        return self.get_stages() - self.get_elevations()
124   
125   
126    def get_total_water_volume(self):
127       
128       return num.sum(self.get_heights()*self.get_areas())
129 
130
131    def get_average_height(self):
132   
133        return self.get_total_water_volume()/self.area
134       
135       
136    def get_velocities(self):
137       
138            heights = self.get_heights()
139            u = self.get_xmoms()/(heights + velocity_protection/heights)
140            v = self.get_ymoms()/(heights + velocity_protection/heights)
141           
142            return u, v
143
144
145    def get_xvelocities(self):
146
147            heights = self.get_heights()
148            return self.get_xmoms()/(heights + velocity_protection/heights)
149
150    def get_yvelocities(self):
151
152            heights = self.get_heights()
153            return self.get_ymoms()/(heights + velocity_protection/heights)
154           
155           
156    def get_average_speed(self):
157 
158            u, v = self.get_velocities()
159           
160            average_u = num.sum(u*self.get_areas())/self.area
161            average_v = num.sum(v*self.get_areas())/self.area
162           
163            return math.sqrt(average_u**2 + average_v**2)
164
165
166    def get_average_velocity_head(self):
167
168        return 0.5*self.get_average_speed()**2/g
169
170
171    def get_average_total_energy(self):
172       
173        return self.get_average_velocity_head() + self.get_average_stage()
174       
175   
176    def get_average_specific_energy(self):
177       
178        return self.get_average_velocity_head() + self.get_average_height()
179
180
181    def get_enquiry_stage(self):
182
183        return self.domain.quantities['stage'].centroid_values[self.enquiry_index]
184
185
186    def get_enquiry_xmom(self):
187
188        return self.domain.quantities['xmomentum'].centroid_values[self.enquiry_index]
189
190    def get_enquiry_ymom(self):
191
192        return self.domain.quantities['ymomentum'].centroid_values[self.enquiry_index]
193
194
195    def get_enquiry_elevation(self):
196
197        return self.domain.quantities['elevation'].centroid_values[self.enquiry_index]
198
199    def get_enquiry_height(self):
200
201        return self.get_enquiry_stage() - self.get_enquiry_elevation()
202
203
204    def get_enquiry_velocity(self):
205
206            height = self.get_enquiry_height()
207            u = self.get_enquiry_xmom()/(height + velocity_protection/height)
208            v = self.get_enquiry_ymom()/(height + velocity_protection/height)
209
210            return u, v
211
212
213    def get_enquiry_xvelocity(self):
214
215            height = self.get_enquiry_height()
216            return self.get_enquiry_xmom()/(height + velocity_protection/height)
217
218    def get_enquiry_yvelocity(self):
219
220            height = self.get_enquiry_height()
221            return self.get_enquiry_ymom()/(height + velocity_protection/height)
222
223
224    def get_enquiry_speed(self):
225
226            u, v = self.get_enquiry_velocity()
227
228            return math.sqrt(u**2 + v**2)
229
230
231    def get_enquiry_velocity_head(self):
232
233        return 0.5*self.get_enquiry_speed()**2/g
234
235
236    def get_enquiry_total_energy(self):
237
238        return self.get_enquiry_velocity_head() + self.get_enquiry_stage()
239
240
241    def get_enquiry_specific_energy(self):
242
243        return self.get_enquiry_velocity_head() + self.get_enquiry_height()
244
245
246    def set_heights(self,height):
247
248        self.domain.quantities['stage'].centroid_values.put(self.triangle_indices, self.get_elevations() + height)
249
250
251    def set_stages(self,stage):
252
253        self.domain.quantities['stage'].centroid_values.put(self.triangle_indices, stage)
254
255
256    def set_xmoms(self,xmom):
257
258        self.domain.quantities['xmomentum'].centroid_values.put(self.triangle_indices, xmom)
259
260
261    def set_ymoms(self,ymom):
262
263        self.domain.quantities['ymomentum'].centroid_values.put(self.triangle_indices, ymom)
264
265
266    def set_elevations(self,elevation):
267
268        self.domain.quantities['elevation'].centroid_values.put(self.triangle_indices, elevation)
Note: See TracBrowser for help on using the repository browser.