1 | import anuga.geometry.polygon |
---|
2 | from anuga.geometry.polygon import inside_polygon, is_inside_polygon, line_intersect |
---|
3 | from anuga.config import velocity_protection, g |
---|
4 | import math |
---|
5 | |
---|
6 | import numpy as num |
---|
7 | |
---|
8 | class Inlet: |
---|
9 | """Contains information associated with each inlet |
---|
10 | """ |
---|
11 | |
---|
12 | def __init__(self, domain, line, verbose=False): |
---|
13 | |
---|
14 | self.domain = domain |
---|
15 | self.domain_bounding_polygon = self.domain.get_boundary_polygon() |
---|
16 | self.line = num.asarray(line, dtype=num.float64) |
---|
17 | self.verbose = verbose |
---|
18 | |
---|
19 | self.compute_triangle_indices() |
---|
20 | self.compute_area() |
---|
21 | self.compute_inlet_length() |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | def compute_triangle_indices(self): |
---|
26 | |
---|
27 | # Get boundary (in absolute coordinates) |
---|
28 | bounding_polygon = self.domain_bounding_polygon |
---|
29 | domain_centroids = self.domain.get_centroid_coordinates(absolute=True) |
---|
30 | vertex_coordinates = self.domain.get_vertex_coordinates(absolute=True) |
---|
31 | |
---|
32 | # Check that line lies within the mesh. |
---|
33 | for point in self.line: |
---|
34 | msg = 'Point %s ' % str(point) |
---|
35 | msg += ' did not fall within the domain boundary.' |
---|
36 | assert is_inside_polygon(point, bounding_polygon), msg |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | self.triangle_indices = line_intersect(vertex_coordinates, self.line) |
---|
41 | |
---|
42 | if len(self.triangle_indices) == 0: |
---|
43 | msg = 'Inlet line=%s ' % (self.line) |
---|
44 | msg += 'No triangles intersecting line ' |
---|
45 | raise Exception, msg |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | def compute_area(self): |
---|
50 | |
---|
51 | # Compute inlet area as the sum of areas of triangles identified |
---|
52 | # by line. Must be called after compute_inlet_triangle_indices(). |
---|
53 | if len(self.triangle_indices) == 0: |
---|
54 | region = 'Inlet line=%s' % (self.inlet_line) |
---|
55 | msg = 'No triangles have been identified in region ' |
---|
56 | raise Exception, msg |
---|
57 | |
---|
58 | self.area = 0.0 |
---|
59 | for j in self.triangle_indices: |
---|
60 | self.area += self.domain.areas[j] |
---|
61 | |
---|
62 | msg = 'Inlet exchange area has area = %f' % self.area |
---|
63 | assert self.area > 0.0 |
---|
64 | |
---|
65 | |
---|
66 | def compute_inlet_length(self): |
---|
67 | """ Compute the length of the inlet (as |
---|
68 | defined by the input line |
---|
69 | """ |
---|
70 | |
---|
71 | point0 = self.line[0] |
---|
72 | point1 = self.line[1] |
---|
73 | |
---|
74 | self.inlet_length = anuga.geometry.polygon.line_length(self.line) |
---|
75 | |
---|
76 | |
---|
77 | def get_inlet_length(self): |
---|
78 | |
---|
79 | return self.inlet_length |
---|
80 | |
---|
81 | def get_line(self): |
---|
82 | |
---|
83 | return self.line |
---|
84 | |
---|
85 | def get_area(self): |
---|
86 | |
---|
87 | return self.area |
---|
88 | |
---|
89 | |
---|
90 | def get_areas(self): |
---|
91 | |
---|
92 | # Must be called after compute_inlet_triangle_indices(). |
---|
93 | return self.domain.areas.take(self.triangle_indices) |
---|
94 | |
---|
95 | |
---|
96 | def get_stages(self): |
---|
97 | |
---|
98 | return self.domain.quantities['stage'].centroid_values.take(self.triangle_indices) |
---|
99 | |
---|
100 | |
---|
101 | def get_average_stage(self): |
---|
102 | |
---|
103 | return num.sum(self.get_stages()*self.get_areas())/self.area |
---|
104 | |
---|
105 | def get_elevations(self): |
---|
106 | |
---|
107 | return self.domain.quantities['elevation'].centroid_values.take(self.triangle_indices) |
---|
108 | |
---|
109 | def get_average_elevation(self): |
---|
110 | |
---|
111 | return num.sum(self.get_elevations()*self.get_areas())/self.area |
---|
112 | |
---|
113 | |
---|
114 | def get_xmoms(self): |
---|
115 | |
---|
116 | return self.domain.quantities['xmomentum'].centroid_values.take(self.triangle_indices) |
---|
117 | |
---|
118 | |
---|
119 | def get_average_xmom(self): |
---|
120 | |
---|
121 | return num.sum(self.get_xmoms()*self.get_areas())/self.area |
---|
122 | |
---|
123 | |
---|
124 | def get_ymoms(self): |
---|
125 | |
---|
126 | return self.domain.quantities['ymomentum'].centroid_values.take(self.triangle_indices) |
---|
127 | |
---|
128 | |
---|
129 | def get_average_ymom(self): |
---|
130 | |
---|
131 | return num.sum(self.get_ymoms()*self.get_areas())/self.area |
---|
132 | |
---|
133 | |
---|
134 | def get_depths(self): |
---|
135 | |
---|
136 | return self.get_stages() - self.get_elevations() |
---|
137 | |
---|
138 | |
---|
139 | def get_total_water_volume(self): |
---|
140 | |
---|
141 | return num.sum(self.get_depths()*self.get_areas()) |
---|
142 | |
---|
143 | |
---|
144 | def get_average_depth(self): |
---|
145 | |
---|
146 | return self.get_total_water_volume()/self.area |
---|
147 | |
---|
148 | |
---|
149 | def get_velocities(self): |
---|
150 | |
---|
151 | depths = self.get_depths() |
---|
152 | u = self.get_xmoms()/(depths + velocity_protection/depths) |
---|
153 | v = self.get_ymoms()/(depths + velocity_protection/depths) |
---|
154 | |
---|
155 | return u, v |
---|
156 | |
---|
157 | |
---|
158 | def get_xvelocities(self): |
---|
159 | |
---|
160 | depths = self.get_depths() |
---|
161 | return self.get_xmoms()/(depths + velocity_protection/depths) |
---|
162 | |
---|
163 | def get_yvelocities(self): |
---|
164 | |
---|
165 | depths = self.get_depths() |
---|
166 | return self.get_ymoms()/(depths + velocity_protection/depths) |
---|
167 | |
---|
168 | |
---|
169 | def get_average_speed(self): |
---|
170 | |
---|
171 | u, v = self.get_velocities() |
---|
172 | |
---|
173 | average_u = num.sum(u*self.get_areas())/self.area |
---|
174 | average_v = num.sum(v*self.get_areas())/self.area |
---|
175 | |
---|
176 | return math.sqrt(average_u**2 + average_v**2) |
---|
177 | |
---|
178 | |
---|
179 | def get_average_velocity_head(self): |
---|
180 | |
---|
181 | return 0.5*self.get_average_speed()**2/g |
---|
182 | |
---|
183 | |
---|
184 | def get_average_total_energy(self): |
---|
185 | |
---|
186 | return self.get_average_velocity_head() + self.get_average_stage() |
---|
187 | |
---|
188 | |
---|
189 | def get_average_specific_energy(self): |
---|
190 | |
---|
191 | return self.get_average_velocity_head() + self.get_average_depth() |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | def set_depths(self,depth): |
---|
196 | |
---|
197 | self.domain.quantities['stage'].centroid_values.put(self.triangle_indices, self.get_elevations() + depth) |
---|
198 | |
---|
199 | |
---|
200 | def set_stages(self,stage): |
---|
201 | |
---|
202 | self.domain.quantities['stage'].centroid_values.put(self.triangle_indices, stage) |
---|
203 | |
---|
204 | |
---|
205 | def set_xmoms(self,xmom): |
---|
206 | |
---|
207 | self.domain.quantities['xmomentum'].centroid_values.put(self.triangle_indices, xmom) |
---|
208 | |
---|
209 | |
---|
210 | def set_ymoms(self,ymom): |
---|
211 | |
---|
212 | self.domain.quantities['ymomentum'].centroid_values.put(self.triangle_indices, ymom) |
---|
213 | |
---|
214 | |
---|
215 | def set_elevations(self,elevation): |
---|
216 | |
---|
217 | self.domain.quantities['elevation'].centroid_values.put(self.triangle_indices, elevation) |
---|
218 | |
---|
219 | def set_stages_evenly(self,volume): |
---|
220 | """ Distribute volume of water over |
---|
221 | inlet exchange region so that stage is level |
---|
222 | """ |
---|
223 | |
---|
224 | areas = self.get_areas() |
---|
225 | stages = self.get_stages() |
---|
226 | |
---|
227 | stages_order = stages.argsort() |
---|
228 | |
---|
229 | # accumulate areas of cells ordered by stage |
---|
230 | summed_areas = num.cumsum(areas[stages_order]) |
---|
231 | |
---|
232 | # accumulate the volume need to fill cells |
---|
233 | summed_volume = num.zeros_like(areas) |
---|
234 | summed_volume[1:] = num.cumsum(summed_areas[:-1]*num.diff(stages[stages_order])) |
---|
235 | |
---|
236 | # find the number of cells which will be filled |
---|
237 | index = num.nonzero(summed_volume<=volume)[0][-1] |
---|
238 | |
---|
239 | # calculate stage needed to fill chosen cells with given volume of water |
---|
240 | depth = (volume - summed_volume[index])/summed_areas[index] |
---|
241 | stages[stages_order[0:index+1]] = stages[stages_order[index]]+depth |
---|
242 | |
---|
243 | self.set_stages(stages) |
---|
244 | |
---|
245 | def set_depths_evenly(self,volume): |
---|
246 | """ Distribute volume over all exchange |
---|
247 | cells with equal depth of water |
---|
248 | """ |
---|
249 | |
---|
250 | new_depth = self.get_average_depth() + (volume/self.get_area()) |
---|
251 | self.set_depths(new_depth) |
---|
252 | |
---|