1 | """Functions for geometries related to culvert flows |
---|
2 | """ |
---|
3 | |
---|
4 | # Import necessary modules |
---|
5 | from math import sqrt |
---|
6 | import numpy |
---|
7 | |
---|
8 | |
---|
9 | def create_culvert_polygons(end_point0, |
---|
10 | end_point1, |
---|
11 | width, height=None, |
---|
12 | enquiry_gap_factor=1.0, |
---|
13 | enquiry_shape_factor=2.0, |
---|
14 | number_of_barrels=1): |
---|
15 | """Create polygons at the end of a culvert inlet and outlet. |
---|
16 | At either end two polygons will be created; one for the actual flow to pass through and one a little further away |
---|
17 | for enquiring the total energy at both ends of the culvert and transferring flow. |
---|
18 | |
---|
19 | Input (mandatory): |
---|
20 | end_point0 - one end of the culvert (x,y) |
---|
21 | end_point1 - other end of the culvert (x,y) |
---|
22 | width - culvert width |
---|
23 | |
---|
24 | Input (optional): |
---|
25 | height - culvert height, defaults to width making a square culvert |
---|
26 | enquiry_gap_factor - sets the distance to the enquiry polygon |
---|
27 | enquiry_shape_factor - sets the shape of the enquiry polygon |
---|
28 | (large value widens polygon but reduces height |
---|
29 | to preserve same area as exchange polygon) |
---|
30 | number_of_barrels - number of identical pipes. |
---|
31 | |
---|
32 | Output: |
---|
33 | |
---|
34 | Dictionary of four polygons. The dictionary keys are: |
---|
35 | 'exchange_polygon0' - polygon defining the flow area at end_point0 |
---|
36 | 'exchange_polygon1' - polygon defining the flow area at end_point1 |
---|
37 | 'enquiry_polygon0' - polygon defining the enquiry field near end_point0 |
---|
38 | 'enquiry_polygon1' - polygon defining the enquiry field near end_point1 |
---|
39 | """ |
---|
40 | |
---|
41 | |
---|
42 | # Input check |
---|
43 | if height is None: |
---|
44 | height = width |
---|
45 | |
---|
46 | # Dictionary for calculated polygons |
---|
47 | culvert_polygons = {} |
---|
48 | |
---|
49 | |
---|
50 | # Calculate geometry |
---|
51 | x0, y0 = end_point0 |
---|
52 | x1, y1 = end_point1 |
---|
53 | |
---|
54 | dx = x1-x0 |
---|
55 | dy = y1-y0 |
---|
56 | |
---|
57 | vector = numpy.array([dx, dy]) |
---|
58 | length = sqrt(numpy.sum(vector**2)) |
---|
59 | |
---|
60 | # Adjust polygon width to number of barrels in this culvert |
---|
61 | width *= number_of_barrels |
---|
62 | |
---|
63 | |
---|
64 | # Unit direction vector and normal |
---|
65 | vector /= length |
---|
66 | normal = numpy.array([-dy, dx])/length |
---|
67 | |
---|
68 | |
---|
69 | # Short hands |
---|
70 | w = width/2*normal # Perpendicular vector of 1/2 width |
---|
71 | h = height*vector # Vector of length=height in the |
---|
72 | # direction of the culvert |
---|
73 | |
---|
74 | # Build exchange polygon 0 |
---|
75 | p0 = end_point0 + w |
---|
76 | p1 = end_point0 - w |
---|
77 | p2 = p1 - h |
---|
78 | p3 = p0 - h |
---|
79 | culvert_polygons['exchange_polygon0'] = numpy.array([p0,p1,p2,p3]) |
---|
80 | |
---|
81 | |
---|
82 | # Build exchange polygon 1 |
---|
83 | p0 = end_point1 + w |
---|
84 | p1 = end_point1 - w |
---|
85 | p2 = p1 + h |
---|
86 | p3 = p0 + h |
---|
87 | culvert_polygons['exchange_polygon1'] = numpy.array([p0,p1,p2,p3]) |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | # Redefine shorthands for enquiry polygons |
---|
92 | w = w*enquiry_shape_factor |
---|
93 | h = h/enquiry_shape_factor |
---|
94 | gap = (enquiry_gap_factor + h)*vector |
---|
95 | |
---|
96 | # Build enquiry polygon 0 |
---|
97 | p0 = end_point0 + w - gap |
---|
98 | p1 = end_point0 - w - gap |
---|
99 | p2 = p1 - h |
---|
100 | p3 = p0 - h |
---|
101 | culvert_polygons['enquiry_polygon0'] = numpy.array([p0,p1,p2,p3]) |
---|
102 | |
---|
103 | # Build enquiry polygon 1 |
---|
104 | p0 = end_point1 + w + gap |
---|
105 | p1 = end_point1 - w + gap |
---|
106 | p2 = p1 + h |
---|
107 | p3 = p0 + h |
---|
108 | culvert_polygons['enquiry_polygon1'] = numpy.array([p0,p1,p2,p3]) |
---|
109 | |
---|
110 | # Return results |
---|
111 | culvert_polygons['vector'] = vector |
---|
112 | culvert_polygons['length'] = length |
---|
113 | culvert_polygons['normal'] = normal |
---|
114 | return culvert_polygons |
---|