1 | """region.py - Classes for implementing region conditions |
---|
2 | |
---|
3 | NOTE: I've only set it up for testing X values of constants and functions. |
---|
4 | Although it should work for vectors/arrays of values, that implies |
---|
5 | knowing info about the actual triangles, and that is not how the user should |
---|
6 | operate. |
---|
7 | """ |
---|
8 | # FIXME (DSG-DSG) add better comments |
---|
9 | |
---|
10 | from Numeric import average |
---|
11 | class Region: |
---|
12 | """Base class for modifying quantities based on a region. |
---|
13 | """ |
---|
14 | |
---|
15 | def __init__(self, location='vertices'): |
---|
16 | self.location = location |
---|
17 | |
---|
18 | def __call__(self, tag, elements, domain): |
---|
19 | msg = 'Generic class Boundary must be subclassed' |
---|
20 | raise msg |
---|
21 | |
---|
22 | |
---|
23 | def build_indices(self, elements, domain): |
---|
24 | """ |
---|
25 | Return a list of triangle_id or vertex_id, depending on the location |
---|
26 | """ |
---|
27 | if self.location == 'unique vertices': |
---|
28 | return domain.get_unique_vertices(elements) |
---|
29 | else: |
---|
30 | return elements |
---|
31 | |
---|
32 | class Set_region(Region): |
---|
33 | |
---|
34 | def __init__(self, tag, quantity, X, location='vertices'): |
---|
35 | """ |
---|
36 | name: Name of quantity |
---|
37 | X: const or function |
---|
38 | location: Where values are to be stored. |
---|
39 | Permissible options are: vertices, centroid |
---|
40 | """ |
---|
41 | |
---|
42 | Region.__init__(self) |
---|
43 | self.tag = tag |
---|
44 | self.quantity = quantity |
---|
45 | self.location = location |
---|
46 | self.X = X |
---|
47 | |
---|
48 | def __repr__(self): |
---|
49 | pass |
---|
50 | |
---|
51 | def __call__(self, tag, elements, domain): |
---|
52 | """ |
---|
53 | """ |
---|
54 | if tag == self.tag: |
---|
55 | domain.set_quantity(self.quantity, |
---|
56 | self.X, |
---|
57 | location=self.location, |
---|
58 | indices=self.build_indices(elements, domain)) |
---|
59 | |
---|
60 | |
---|
61 | class Add_value_to_region(Region): |
---|
62 | """ |
---|
63 | Will add a value to the current quantity value. |
---|
64 | |
---|
65 | quantity = initial_quantity + X. |
---|
66 | |
---|
67 | This method does not work with functions |
---|
68 | |
---|
69 | Use average to add X to a mean average of the quantity values. |
---|
70 | eg if applying this to elevation this will give a flat roof. |
---|
71 | """ |
---|
72 | |
---|
73 | def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None, average=False): |
---|
74 | #I have to get this going! |
---|
75 | #Region.__init__(self) |
---|
76 | self.tag = tag |
---|
77 | self.quantity_answer = quantity |
---|
78 | self.location = location |
---|
79 | self.X = X |
---|
80 | self.average = average |
---|
81 | if initial_quantity is None: |
---|
82 | self.quantity_initial_value = quantity |
---|
83 | else: |
---|
84 | self.quantity_initial_value = initial_quantity |
---|
85 | if callable(X): |
---|
86 | raise 'This class does not work with functions' |
---|
87 | |
---|
88 | def __repr__(self): |
---|
89 | pass |
---|
90 | |
---|
91 | def __call__(self, tag, elements, domain): |
---|
92 | """ |
---|
93 | """ |
---|
94 | if tag == self.tag: |
---|
95 | #new_values = domain.get_quantity(self.quantity_initial_value, |
---|
96 | # indices=self.build_indices(elements, domain), |
---|
97 | # location=self.location) + self.X |
---|
98 | Q = domain.get_quantity(self.quantity_initial_value) |
---|
99 | if self.average is True: |
---|
100 | # Average the points, and then the verts in the averaged point. |
---|
101 | values = Q.get_values(indices=self.build_indices(elements, domain), |
---|
102 | location=self.location) |
---|
103 | av = average(values) |
---|
104 | if self.location == "vertices": |
---|
105 | av = average(av) |
---|
106 | new_values = av + self.X |
---|
107 | else: |
---|
108 | new_values = Q.get_values(indices=self.build_indices(elements, domain), |
---|
109 | location=self.location) + self.X |
---|
110 | domain.set_quantity(self.quantity_answer, new_values, |
---|
111 | indices=self.build_indices(elements, domain), |
---|
112 | location=self.location) |
---|
113 | |
---|
114 | class Add_quantities(Region): |
---|
115 | """ |
---|
116 | Will add a quantity to the current quantity value. |
---|
117 | """ |
---|
118 | |
---|
119 | def __init__(self, tag, quantity_answer, adding_quantity, location='vertices'): |
---|
120 | #I have to get this going! |
---|
121 | #Region.__init__(self) |
---|
122 | self.tag = tag |
---|
123 | self.quantity_answer = quantity_answer |
---|
124 | self.adding_quantity = adding_quantity |
---|
125 | self.location = location |
---|
126 | |
---|
127 | def __repr__(self): |
---|
128 | pass |
---|
129 | |
---|
130 | def __call__(self, tag, elements, domain): |
---|
131 | """ |
---|
132 | """ |
---|
133 | if tag == self.tag: |
---|
134 | |
---|
135 | #new_values = domain.get_quantity(self.quantity_answer, |
---|
136 | # indices=self.build_indices(elements, domain), |
---|
137 | # location=self.location) \ |
---|
138 | # + domain.get_quantity(self.adding_quantity, |
---|
139 | # indices=self.build_indices(elements, domain), |
---|
140 | # location=self.location) |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | indices = self.build_indices(elements, domain) |
---|
145 | location = self.location |
---|
146 | Q1 = domain.get_quantity(self.quantity_answer) |
---|
147 | Q2 = domain.get_quantity(self.adding_quantity) |
---|
148 | new_values = Q1.get_values(indices=indices, location=location) +\ |
---|
149 | Q2.get_values(indices=indices, location=location) |
---|
150 | |
---|
151 | |
---|
152 | domain.set_quantity(self.quantity_answer, new_values, |
---|
153 | indices=self.build_indices(elements, domain), |
---|
154 | location=self.location) |
---|
155 | |
---|
156 | |
---|
157 | class Stage_no_less_than_elevation(Region): |
---|
158 | """ |
---|
159 | Will set the stage to not be less than the elevation. |
---|
160 | This would be good, but it's not region dependent. |
---|
161 | Wait for it to become a default for pyvolution. |
---|
162 | """ |
---|
163 | |
---|
164 | def __init__(self): |
---|
165 | pass |
---|