1 | """ |
---|
2 | Set elevation operators |
---|
3 | |
---|
4 | |
---|
5 | """ |
---|
6 | |
---|
7 | __author__="steve" |
---|
8 | __date__ ="$09/03/2012 4:46:39 PM$" |
---|
9 | |
---|
10 | import numpy as num |
---|
11 | |
---|
12 | from anuga import Domain |
---|
13 | from anuga import Quantity |
---|
14 | |
---|
15 | import anuga.utilities.log as log |
---|
16 | from anuga.geometry.polygon import inside_polygon |
---|
17 | from anuga.utilities.function_utils import determine_function_type |
---|
18 | from anuga import Region |
---|
19 | from anuga.config import indent |
---|
20 | |
---|
21 | class Set_quantity(Region): |
---|
22 | """ |
---|
23 | Helper class to setup calculation of quantity |
---|
24 | associated with a region (defined by indices, polygon or center/radius |
---|
25 | """ |
---|
26 | |
---|
27 | def __init__(self, |
---|
28 | domain, |
---|
29 | quantity, |
---|
30 | value=None, |
---|
31 | indices=None, |
---|
32 | polygon=None, |
---|
33 | center=None, |
---|
34 | radius=None, |
---|
35 | line=None, |
---|
36 | verbose = False, |
---|
37 | test_elevation=True, |
---|
38 | test_stage=True): |
---|
39 | |
---|
40 | |
---|
41 | Region.__init__(self, domain, |
---|
42 | indices=indices, |
---|
43 | polygon=polygon, |
---|
44 | center=center, |
---|
45 | radius=radius, |
---|
46 | line=line, |
---|
47 | verbose=verbose) |
---|
48 | |
---|
49 | |
---|
50 | self.set_value(value) |
---|
51 | |
---|
52 | #------------------------------------------- |
---|
53 | # Test quantity |
---|
54 | #------------------------------------------- |
---|
55 | self.quantity = quantity |
---|
56 | msg = 'quantity not found in domain' |
---|
57 | assert quantity in domain.quantities, msg |
---|
58 | |
---|
59 | if test_elevation: |
---|
60 | msg ='Use Set_elevation to maintain continuity' |
---|
61 | assert quantity is not 'elevation', msg |
---|
62 | |
---|
63 | if test_stage: |
---|
64 | msg ='Use Set_stage to maintain non-negative water depth' |
---|
65 | assert quantity is not 'stage', msg |
---|
66 | |
---|
67 | #------------------------------------------- |
---|
68 | # Useful quantity alias |
---|
69 | #------------------------------------------ |
---|
70 | self.quantity_c = self.domain.quantities[quantity].centroid_values |
---|
71 | |
---|
72 | self.coord_c = self.domain.centroid_coordinates |
---|
73 | self.areas = self.domain.areas |
---|
74 | |
---|
75 | |
---|
76 | def __call__(self): |
---|
77 | """ |
---|
78 | Apply value to those triangles defined by indices |
---|
79 | |
---|
80 | indices == [], don't apply anywhere |
---|
81 | indices == None, apply everywhere |
---|
82 | otherwise apply for the specific indices |
---|
83 | """ |
---|
84 | |
---|
85 | if self.indices is []: |
---|
86 | return |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | #value = self.get_value() |
---|
91 | |
---|
92 | from pprint import pprint |
---|
93 | #print 'value' |
---|
94 | #pprint(value) |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | if self.indices is None: |
---|
99 | |
---|
100 | #-------------------------------------- |
---|
101 | # Update centroid values |
---|
102 | #-------------------------------------- |
---|
103 | try: |
---|
104 | value = self.get_value(x=self.coord_c[:,0], y=self.coord_c[:,1]) |
---|
105 | #print value |
---|
106 | self.quantity_c[:] = value |
---|
107 | except ValueError: |
---|
108 | pass |
---|
109 | |
---|
110 | else: |
---|
111 | |
---|
112 | #-------------------------------------- |
---|
113 | # Update centroid values |
---|
114 | #-------------------------------------- |
---|
115 | ids = self.indices |
---|
116 | x = self.coord_c[ids,0] |
---|
117 | y = self.coord_c[ids,1] |
---|
118 | try: |
---|
119 | value = self.get_value(x=x,y=y) |
---|
120 | self.quantity_c[ids] = value |
---|
121 | except ValueError: |
---|
122 | pass |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | def set_value(self, value = None): |
---|
127 | |
---|
128 | self.value = value |
---|
129 | self.value_type = determine_function_type(value) |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | def get_value(self, x = None, y = None, t = None): |
---|
134 | """Get value of quantity at time t. |
---|
135 | If t not specified, return value at current domain time |
---|
136 | """ |
---|
137 | |
---|
138 | #from anuga.fit_interpolate.interpolate import Modeltime_too_early, \ |
---|
139 | # Modeltime_too_late |
---|
140 | |
---|
141 | |
---|
142 | #print 'x,y,t' |
---|
143 | #print x,y,t |
---|
144 | |
---|
145 | if t is None: |
---|
146 | t = self.domain.get_time() |
---|
147 | |
---|
148 | #try: |
---|
149 | if self.value_type == 't': |
---|
150 | value = self.value(t) |
---|
151 | elif self.value_type == 'x,y': |
---|
152 | value = self.value(x,y) |
---|
153 | elif self.value_type == 'x,y,t': |
---|
154 | value = self.value(x,y,t) |
---|
155 | else: |
---|
156 | #print self.value |
---|
157 | value = float(self.value) |
---|
158 | |
---|
159 | #except Modeltime_too_early, e: |
---|
160 | # raise Modeltime_too_early(e) |
---|
161 | #except Modeltime_too_late, e: |
---|
162 | # raise Modeltime_too_late(e) |
---|
163 | |
---|
164 | |
---|
165 | return value |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|