1 | from anuga.geometry.polygon import inside_polygon, polygon_area |
---|
2 | from anuga.config import g |
---|
3 | import anuga.utilities.log as log |
---|
4 | import box_culvert |
---|
5 | import culvert_routines |
---|
6 | |
---|
7 | class Culvert_operator: |
---|
8 | """Culvert flow - transfer water from one rectangular box to another. |
---|
9 | Sets up the geometry of problem |
---|
10 | |
---|
11 | This is the base class for culverts. Inherit from this class (and overwrite |
---|
12 | compute_discharge method for specific subclasses) |
---|
13 | |
---|
14 | Input: Two points, pipe_size (either diameter or width, height), |
---|
15 | mannings_rougness, |
---|
16 | """ |
---|
17 | |
---|
18 | def __init__(self, |
---|
19 | domain, |
---|
20 | end_point0, |
---|
21 | end_point1, |
---|
22 | width, |
---|
23 | height=None, |
---|
24 | verbose=False): |
---|
25 | |
---|
26 | self.domain = domain |
---|
27 | self.domain.set_fractional_step_operator(self) |
---|
28 | end_points = [end_point0, end_point1] |
---|
29 | |
---|
30 | if height is None: |
---|
31 | height = width |
---|
32 | |
---|
33 | self.width = width |
---|
34 | self.height = height |
---|
35 | |
---|
36 | self.culvert = box_culvert.Box_culvert(self.domain, end_points, self.width, self.height) |
---|
37 | self.inlets = self.culvert.get_inlets() |
---|
38 | |
---|
39 | self.print_stats() |
---|
40 | |
---|
41 | |
---|
42 | def __call__(self): |
---|
43 | |
---|
44 | timestep = self.domain.get_timestep() |
---|
45 | |
---|
46 | from culvert_routines import Culvert_routines |
---|
47 | culvert_routine = culvert_routines.Culvert_routines(self.culvert) |
---|
48 | |
---|
49 | Q, barrel_velocity, culvert_outlet_depth = culvert_routine.boyd_circle() |
---|
50 | |
---|
51 | transfer_water = Q*timestep |
---|
52 | |
---|
53 | inflow = culvert_routine.get_inflow() |
---|
54 | outflow = culvert_routine.get_outflow() |
---|
55 | |
---|
56 | inflow.set_heights(inflow.get_average_height() - transfer_water) |
---|
57 | inflow.set_xmoms(0.0) |
---|
58 | inflow.set_ymoms(0.0) |
---|
59 | |
---|
60 | outflow.set_heights(outflow.get_average_height() + transfer_water) |
---|
61 | outflow.set_xmoms(0.0) |
---|
62 | outflow.set_ymoms(0.0) |
---|
63 | |
---|
64 | def print_stats(self): |
---|
65 | |
---|
66 | print '=====================================' |
---|
67 | print 'Generic Culvert Operator' |
---|
68 | print '=====================================' |
---|
69 | |
---|
70 | for i, inlet in enumerate(self.inlets): |
---|
71 | print '-------------------------------------' |
---|
72 | print 'Inlet %i' % i |
---|
73 | print '-------------------------------------' |
---|
74 | |
---|
75 | print 'inlet triangle indices and centres' |
---|
76 | print inlet.triangle_indices[i] |
---|
77 | print self.domain.get_centroid_coordinates()[inlet.triangle_indices[i]] |
---|
78 | |
---|
79 | print 'polygon' |
---|
80 | print inlet.polygon |
---|
81 | |
---|
82 | print '=====================================' |
---|
83 | |
---|
84 | |
---|
85 | # FIXME(Ole): Write in C and reuse this function by similar code |
---|
86 | # in interpolate.py |
---|
87 | def interpolate_linearly(x, xvec, yvec): |
---|
88 | |
---|
89 | msg = 'Input to function interpolate_linearly could not be converted ' |
---|
90 | msg += 'to numerical scalar: x = %s' % str(x) |
---|
91 | try: |
---|
92 | x = float(x) |
---|
93 | except: |
---|
94 | raise Exception, msg |
---|
95 | |
---|
96 | |
---|
97 | # Check bounds |
---|
98 | if x < xvec[0]: |
---|
99 | msg = 'Value provided = %.2f, interpolation minimum = %.2f.'\ |
---|
100 | % (x, xvec[0]) |
---|
101 | raise Below_interval, msg |
---|
102 | |
---|
103 | if x > xvec[-1]: |
---|
104 | msg = 'Value provided = %.2f, interpolation maximum = %.2f.'\ |
---|
105 | %(x, xvec[-1]) |
---|
106 | raise Above_interval, msg |
---|
107 | |
---|
108 | |
---|
109 | # Find appropriate slot within bounds |
---|
110 | i = 0 |
---|
111 | while x > xvec[i]: i += 1 |
---|
112 | |
---|
113 | |
---|
114 | x0 = xvec[i-1] |
---|
115 | x1 = xvec[i] |
---|
116 | alpha = (x - x0)/(x1 - x0) |
---|
117 | |
---|
118 | y0 = yvec[i-1] |
---|
119 | y1 = yvec[i] |
---|
120 | y = alpha*y1 + (1-alpha)*y0 |
---|
121 | |
---|
122 | return y |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | def read_culvert_description(culvert_description_filename): |
---|
127 | |
---|
128 | # Read description file |
---|
129 | fid = open(culvert_description_filename) |
---|
130 | |
---|
131 | read_rating_curve_data = False |
---|
132 | rating_curve = [] |
---|
133 | for i, line in enumerate(fid.readlines()): |
---|
134 | |
---|
135 | if read_rating_curve_data is True: |
---|
136 | fields = line.split(',') |
---|
137 | head_difference = float(fields[0].strip()) |
---|
138 | flow_rate = float(fields[1].strip()) |
---|
139 | barrel_velocity = float(fields[2].strip()) |
---|
140 | |
---|
141 | rating_curve.append([head_difference, flow_rate, barrel_velocity]) |
---|
142 | |
---|
143 | if i == 0: |
---|
144 | # Header |
---|
145 | continue |
---|
146 | if i == 1: |
---|
147 | # Metadata |
---|
148 | fields = line.split(',') |
---|
149 | label=fields[0].strip() |
---|
150 | type=fields[1].strip().lower() |
---|
151 | assert type in ['box', 'pipe'] |
---|
152 | |
---|
153 | width=float(fields[2].strip()) |
---|
154 | height=float(fields[3].strip()) |
---|
155 | length=float(fields[4].strip()) |
---|
156 | number_of_barrels=int(fields[5].strip()) |
---|
157 | #fields[6] refers to losses |
---|
158 | description=fields[7].strip() |
---|
159 | |
---|
160 | if line.strip() == '': continue # Skip blanks |
---|
161 | |
---|
162 | if line.startswith('Rating'): |
---|
163 | read_rating_curve_data = True |
---|
164 | # Flow data follows |
---|
165 | |
---|
166 | fid.close() |
---|
167 | |
---|
168 | return label, type, width, height, length, number_of_barrels, description, rating_curve |
---|
169 | |
---|