1 | import os.path |
---|
2 | import sys |
---|
3 | |
---|
4 | from anuga.utilities.system_tools import get_pathname_from_package |
---|
5 | from anuga.geometry.polygon_function import Polygon_function |
---|
6 | |
---|
7 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross |
---|
8 | from anuga.abstract_2d_finite_volumes.quantity import Quantity |
---|
9 | from anuga.abstract_2d_finite_volumes.util import file_function |
---|
10 | |
---|
11 | import anuga |
---|
12 | |
---|
13 | from anuga.structures.boyd_box_operator import Boyd_box_operator |
---|
14 | from anuga.structures.inlet_operator import Inlet_operator |
---|
15 | |
---|
16 | #from anuga.culvert_flows.culvert_routines import boyd_generalised_culvert_model |
---|
17 | |
---|
18 | from math import pi, pow, sqrt |
---|
19 | |
---|
20 | import numpy as num |
---|
21 | #from parallel_inlet_operator import Parallel_Inlet_operator |
---|
22 | from anuga_parallel import distribute, myid, numprocs, finalize |
---|
23 | from anuga.geometry.polygon import inside_polygon, is_inside_polygon, line_intersect |
---|
24 | |
---|
25 | #from parallel_operator_factory import Inlet_operator, Boyd_box_operator |
---|
26 | import pypar |
---|
27 | import random |
---|
28 | |
---|
29 | |
---|
30 | """test_that_culvert_runs_rating |
---|
31 | |
---|
32 | This test exercises the culvert and checks values outside rating curve |
---|
33 | are dealt with |
---|
34 | """ |
---|
35 | verbose = True |
---|
36 | path = get_pathname_from_package('anuga.culvert_flows') |
---|
37 | |
---|
38 | length = 40. |
---|
39 | width = 15. |
---|
40 | |
---|
41 | dx = dy = 0.5 # Resolution: Length of subdivisions on both axes |
---|
42 | |
---|
43 | #---------------------------------------------------------------------- |
---|
44 | # Setup initial conditions |
---|
45 | #---------------------------------------------------------------------- |
---|
46 | |
---|
47 | def topography(x, y): |
---|
48 | """Set up a weir |
---|
49 | |
---|
50 | A culvert will connect either side |
---|
51 | """ |
---|
52 | # General Slope of Topography |
---|
53 | z=-x/1000 |
---|
54 | |
---|
55 | N = len(x) |
---|
56 | for i in range(N): |
---|
57 | |
---|
58 | # Sloping Embankment Across Channel |
---|
59 | if 5.0 < x[i] < 10.1: |
---|
60 | # Cut Out Segment for Culvert face |
---|
61 | if 1.0+(x[i]-5.0)/5.0 < y[i] < 4.0 - (x[i]-5.0)/5.0: |
---|
62 | z[i]=z[i] |
---|
63 | else: |
---|
64 | z[i] += 0.5*(x[i] -5.0) # Sloping Segment U/S Face |
---|
65 | if 10.0 < x[i] < 12.1: |
---|
66 | z[i] += 2.5 # Flat Crest of Embankment |
---|
67 | if 12.0 < x[i] < 14.5: |
---|
68 | # Cut Out Segment for Culvert face |
---|
69 | if 2.0-(x[i]-12.0)/2.5 < y[i] < 3.0 + (x[i]-12.0)/2.5: |
---|
70 | z[i]=z[i] |
---|
71 | else: |
---|
72 | z[i] += 2.5-1.0*(x[i] -12.0) # Sloping D/S Face |
---|
73 | |
---|
74 | |
---|
75 | return z |
---|
76 | |
---|
77 | filename=os.path.join(path, 'example_rating_curve.csv') |
---|
78 | |
---|
79 | line0 = [[10.0, 10.0], [30.0, 10.0]] |
---|
80 | #line0 = [[29.0, 10.0], [30.0, 10.0]] |
---|
81 | line1 = [[0.0, 10.0], [0.0, 15.0]] |
---|
82 | Q0 = file_function('test_hydrograph.tms', quantities=['hydrograph']) |
---|
83 | Q1 = 5.0 |
---|
84 | |
---|
85 | samples = 50 |
---|
86 | |
---|
87 | def run_test(parallel = False, control_data = None, test_points = None, verbose = False): |
---|
88 | success = True |
---|
89 | |
---|
90 | ##----------------------------------------------------------------------- |
---|
91 | ## Setup domain |
---|
92 | ##----------------------------------------------------------------------- |
---|
93 | |
---|
94 | points, vertices, boundary = rectangular_cross(int(length/dx), |
---|
95 | int(width/dy), |
---|
96 | len1=length, |
---|
97 | len2=width) |
---|
98 | |
---|
99 | domain = anuga.Domain(points, vertices, boundary) |
---|
100 | domain.set_name('Test_Parallel_Frac_Op') # Output name |
---|
101 | domain.set_default_order(2) |
---|
102 | |
---|
103 | ##----------------------------------------------------------------------- |
---|
104 | ## Distribute domain |
---|
105 | ##----------------------------------------------------------------------- |
---|
106 | |
---|
107 | if parallel: domain = distribute(domain) |
---|
108 | |
---|
109 | |
---|
110 | ##----------------------------------------------------------------------- |
---|
111 | ## Setup boundary conditions |
---|
112 | ##----------------------------------------------------------------------- |
---|
113 | |
---|
114 | domain.set_quantity('elevation', topography) |
---|
115 | domain.set_quantity('friction', 0.01) # Constant friction |
---|
116 | domain.set_quantity('stage', |
---|
117 | expression='elevation') # Dry initial condition |
---|
118 | |
---|
119 | |
---|
120 | Bi = anuga.Dirichlet_boundary([5.0, 0.0, 0.0]) |
---|
121 | Br = anuga.Reflective_boundary(domain) # Solid reflective wall |
---|
122 | domain.set_boundary({'left': Bi, 'right': Br, 'top': Br, 'bottom': Br}) |
---|
123 | |
---|
124 | |
---|
125 | ##----------------------------------------------------------------------- |
---|
126 | ## Determine triangle index coinciding with test points |
---|
127 | ##----------------------------------------------------------------------- |
---|
128 | |
---|
129 | assert(test_points is not None) |
---|
130 | assert(len(test_points) == samples) |
---|
131 | |
---|
132 | tri_ids = [] |
---|
133 | |
---|
134 | for point in test_points: |
---|
135 | try: |
---|
136 | k = domain.get_triangle_containing_point(point) |
---|
137 | if domain.tri_full_flag[k] == 1: |
---|
138 | tri_ids.append(k) |
---|
139 | else: |
---|
140 | tri_ids.append(-1) |
---|
141 | except: |
---|
142 | tri_ids.append(-2) |
---|
143 | |
---|
144 | if verbose: print 'P%d has points = %s' %(myid, tri_ids) |
---|
145 | |
---|
146 | if not parallel: control_data = [] |
---|
147 | |
---|
148 | ################ Define Fractional Operators ########################## |
---|
149 | |
---|
150 | inlet0 = None |
---|
151 | inlet1 = None |
---|
152 | boyd_box0 = None |
---|
153 | |
---|
154 | #inlet0 = Inlet_operator(domain, line0, Q0, debug = True) |
---|
155 | #inlet1 = Inlet_operator(domain, line1, Q1, debug = True) |
---|
156 | |
---|
157 | ## # Enquiry point [ 19. 2.5] is contained in two domains in 4 proc case |
---|
158 | ## if myid == 5 and parallel: |
---|
159 | ## boyd_box0 = Boyd_box_operator(domain, |
---|
160 | ## end_points=[[9.0, 2.5],[13.0, 2.5]], |
---|
161 | ## losses=1.5, |
---|
162 | ## width=1.0, |
---|
163 | ## apron=0.5, |
---|
164 | ## use_momentum_jet=True, |
---|
165 | ## use_velocity_head=False, |
---|
166 | ## manning=0.013, |
---|
167 | ## verbose=False) |
---|
168 | ## elif not parallel: |
---|
169 | ## boyd_box0 = Boyd_box_operator(domain, |
---|
170 | ## end_points=[[9.0, 2.5],[13.0, 2.5]], |
---|
171 | ## losses=1.5, |
---|
172 | ## width=1.0, |
---|
173 | ## apron=0.5, |
---|
174 | ## use_momentum_jet=True, |
---|
175 | ## use_velocity_head=False, |
---|
176 | ## manning=0.013, |
---|
177 | ## verbose=False) |
---|
178 | |
---|
179 | # if parallel: |
---|
180 | # factory = Parallel_operator_factory(domain, debug = True) |
---|
181 | # |
---|
182 | # inlet0 = factory.inlet_operator_factory(line0, Q0) |
---|
183 | # inlet1 = factory.inlet_operator_factory(line1, Q1) |
---|
184 | # |
---|
185 | # boyd_box0 = factory.boyd_box_operator_factory(end_points=[[9.0, 2.5],[19.0, 2.5]], |
---|
186 | # losses=1.5, |
---|
187 | # width=1.5, |
---|
188 | # apron=5.0, |
---|
189 | # use_momentum_jet=True, |
---|
190 | # use_velocity_head=False, |
---|
191 | # manning=0.013, |
---|
192 | # verbose=False) |
---|
193 | # |
---|
194 | # else: |
---|
195 | # inlet0 = Inlet_operator(domain, line0, Q0) |
---|
196 | # inlet1 = Inlet_operator(domain, line1, Q1) |
---|
197 | # |
---|
198 | # # Enquiry point [ 19. 2.5] is contained in two domains in 4 proc case |
---|
199 | # boyd_box0 = Boyd_box_operator(domain, |
---|
200 | # end_points=[[9.0, 2.5],[19.0, 2.5]], |
---|
201 | # losses=1.5, |
---|
202 | # width=1.5, |
---|
203 | # apron=5.0, |
---|
204 | # use_momentum_jet=True, |
---|
205 | # use_velocity_head=False, |
---|
206 | # manning=0.013, |
---|
207 | # verbose=False) |
---|
208 | |
---|
209 | ####################################################################### |
---|
210 | |
---|
211 | ##----------------------------------------------------------------------- |
---|
212 | ## Evolve system through time |
---|
213 | ##----------------------------------------------------------------------- |
---|
214 | |
---|
215 | for t in domain.evolve(yieldstep = 1.0, finaltime = 4): |
---|
216 | domain.write_time() |
---|
217 | |
---|
218 | #print domain.volumetric_balance_statistics() |
---|
219 | |
---|
220 | stage = domain.get_quantity('stage') |
---|
221 | |
---|
222 | #for i in range(samples): |
---|
223 | # if tri_ids[i] >= 0: |
---|
224 | # if verbose: print 'P%d tri %d, value = %s' %(myid, i, stage.centroid_values[tri_ids[i]]) |
---|
225 | |
---|
226 | sys.stdout.flush() |
---|
227 | |
---|
228 | pass |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | success = True |
---|
233 | |
---|
234 | ##----------------------------------------------------------------------- |
---|
235 | ## Assign/Test Control data |
---|
236 | ##----------------------------------------------------------------------- |
---|
237 | |
---|
238 | if not parallel: |
---|
239 | stage = domain.get_quantity('stage') |
---|
240 | |
---|
241 | for i in range(samples): |
---|
242 | assert(tri_ids[i] >= 0) |
---|
243 | control_data.append(stage.centroid_values[tri_ids[i]]) |
---|
244 | |
---|
245 | if inlet0 is not None: |
---|
246 | control_data.append(inlet0.inlet.get_average_stage()) |
---|
247 | control_data.append(inlet0.inlet.get_average_xmom()) |
---|
248 | control_data.append(inlet0.inlet.get_average_ymom()) |
---|
249 | control_data.append(inlet0.inlet.get_total_water_volume()) |
---|
250 | control_data.append(inlet0.inlet.get_average_depth()) |
---|
251 | |
---|
252 | if verbose: print 'P%d control_data = %s' %(myid, control_data) |
---|
253 | else: |
---|
254 | stage = domain.get_quantity('stage') |
---|
255 | |
---|
256 | for i in range(samples): |
---|
257 | if tri_ids[i] >= 0: |
---|
258 | local_success = num.allclose(control_data[i], stage.centroid_values[tri_ids[i]]) |
---|
259 | success = success and local_success |
---|
260 | if verbose and not local_success: |
---|
261 | print 'P%d tri %d, control = %s, actual = %s, Success = %s' %(myid, i, control_data[i], stage.centroid_values[tri_ids[i]], local_success) |
---|
262 | sys.stdout.flush() |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | if inlet0 is not None: |
---|
267 | inlet_master_proc = inlet0.inlet.get_master_proc() |
---|
268 | average_stage = inlet0.inlet.get_global_average_stage() |
---|
269 | average_xmom = inlet0.inlet.get_global_average_xmom() |
---|
270 | average_ymom = inlet0.inlet.get_global_average_ymom() |
---|
271 | average_volume = inlet0.inlet.get_global_total_water_volume() |
---|
272 | average_depth = inlet0.inlet.get_global_average_depth() |
---|
273 | |
---|
274 | if myid == inlet_master_proc: |
---|
275 | if verbose: |
---|
276 | print 'P%d average stage, control = %s, actual = %s' %(myid, control_data[samples], average_stage) |
---|
277 | |
---|
278 | print 'P%d average xmom, control = %s, actual = %s' %(myid, control_data[samples+1], average_xmom) |
---|
279 | |
---|
280 | print 'P%d average ymom, control = %s, actual = %s' %(myid, control_data[samples+2], average_ymom) |
---|
281 | |
---|
282 | print 'P%d average volume, control = %s, actual = %s' %(myid, control_data[samples+3], average_volume) |
---|
283 | |
---|
284 | print 'P%d average depth, control = %s, actual = %s' %(myid, control_data[samples+4], average_depth) |
---|
285 | |
---|
286 | |
---|
287 | #assert(success) |
---|
288 | |
---|
289 | return control_data |
---|
290 | |
---|
291 | |
---|
292 | if __name__=="__main__": |
---|
293 | |
---|
294 | test_points = [] |
---|
295 | |
---|
296 | if myid == 0: |
---|
297 | |
---|
298 | for i in range(samples): |
---|
299 | x = random.randrange(0,1000)/1000.0 * length |
---|
300 | y = random.randrange(0,1000)/1000.0 * width |
---|
301 | point = [x, y] |
---|
302 | test_points.append(point) |
---|
303 | |
---|
304 | for i in range(1,numprocs): |
---|
305 | pypar.send(test_points, i) |
---|
306 | else: |
---|
307 | test_points = pypar.receive(0) |
---|
308 | |
---|
309 | print "Test Points::" |
---|
310 | print test_points |
---|
311 | |
---|
312 | if myid == 0: |
---|
313 | control_data = run_test(parallel=False, test_points = test_points, verbose = True) |
---|
314 | |
---|
315 | for proc in range(1,numprocs): |
---|
316 | pypar.send(control_data, proc) |
---|
317 | else: |
---|
318 | control_data = pypar.receive(0) |
---|
319 | |
---|
320 | pypar.barrier() |
---|
321 | run_test(parallel=True, control_data = control_data, test_points = test_points, verbose = True) |
---|
322 | |
---|
323 | |
---|
324 | finalize() |
---|