Changeset 1916
- Timestamp:
- Oct 14, 2005, 9:46:38 AM (19 years ago)
- Location:
- inundation/pyvolution
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/domain.py
r1862 r1916 187 187 """ 188 188 189 #FIXME (Ole): Allow new quantities 190 189 191 #from quantity import Quantity, Conserved_quantity 190 192 … … 219 221 220 222 return self.quantities[name].get_values( location, indices = indices) 223 224 225 221 226 222 227 … … 724 729 725 730 731 732 def create_quantity_from_expression(domain, expression): 733 """Create new quantity from other quantities using arbitrary expression 734 735 Combine existing quantities in domain using expression and return 736 result as a new quantity. 737 738 Note, the new quantity could e.g. be used in set_quantity 739 740 Valid expressions are limited to operators defined in class Quantity 741 742 Example: 743 744 745 """ 746 747 #Replace quantity names with actual quantities 748 quantities = domain.quantities 749 for quantity_name in quantities.keys(): 750 expression = expression.replace(quantity_name, 751 'quantities["%s"]' %quantity_name) 752 753 #Evaluate and return 754 return eval(expression) 755 756 757 726 758 ############################################## 727 759 #Initialise module -
inundation/pyvolution/quantity.py
r1903 r1916 72 72 73 73 def __add__(self, other): 74 """Add to self anything that could populate a quantity: 74 """Add to self anything that could populate a quantity 75 75 76 E.g other can be a constant, an array, a function, another quantity 76 77 (except for a filename or points, attributes (for now)) … … 95 96 96 97 def __mul__(self, other): 97 """Multiply self with anything that could populate a quantity: 98 """Multiply self with anything that could populate a quantity 99 98 100 E.g other can be a constant, an array, a function, another quantity 99 101 (except for a filename or points, attributes (for now)) … … 118 120 """ 119 121 return self * other 122 123 def __pow__(self, other): 124 """Raise quantity to (numerical) power 125 126 As with __mul__ vertex values are processed entry by entry 127 while centroid and edge values are re-interpolated. 128 129 Example using __pow__: 130 Q = (Q1**2 + Q2**2)**0.5 131 132 """ 133 134 result = Quantity(self.domain) 135 result.set_values(self.vertex_values**other) 136 return result 137 120 138 121 139 -
inundation/pyvolution/test_domain.py
r1753 r1916 134 134 q = domain.get_conserved_quantities(3, edge=2) 135 135 assert allclose(q, [-1.5, -1.5, 0.]) 136 137 138 139 def test_create_quantity_from_expression(self): 140 """Quantity created from other quantities using arbitrary expression 141 142 """ 143 144 145 a = [0.0, 0.0] 146 b = [0.0, 2.0] 147 c = [2.0,0.0] 148 d = [0.0, 4.0] 149 e = [2.0, 2.0] 150 f = [4.0,0.0] 151 152 points = [a, b, c, d, e, f] 153 #bac, bce, ecf, dbe, daf, dae 154 vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]] 155 156 domain = Domain(points, vertices, boundary=None, 157 conserved_quantities =\ 158 ['stage', 'xmomentum', 'ymomentum'], 159 other_quantities = ['elevation', 'friction']) 160 161 162 domain.set_quantity('elevation', -1) 163 164 165 domain.set_quantity('stage', [[1,2,3], [5,5,5], 166 [0,0,9], [-6, 3, 3]]) 167 168 domain.set_quantity('xmomentum', [[1,2,3], [5,5,5], 169 [0,0,9], [-6, 3, 3]]) 170 171 domain.set_quantity('ymomentum', [[3,3,3], [4,2,1], 172 [2,4,-1], [1, 0, 1]]) 173 174 domain.check_integrity() 175 176 177 178 expression = 'stage - elevation' 179 Q = create_quantity_from_expression(domain, expression) 180 181 assert allclose(Q.vertex_values, [[2,3,4], [6,6,6], 182 [1,1,10], [-5, 4, 4]]) 183 184 expression = '(xmomentum*xmomentum + ymomentum*ymomentum)**0.5' 185 Q = create_quantity_from_expression(domain, expression) 186 187 X = domain.quantities['xmomentum'].vertex_values 188 Y = domain.quantities['ymomentum'].vertex_values 189 190 assert allclose(Q.vertex_values, (X**2 + Y**2)**0.5) 191 192 193 194 136 195 137 196 -
inundation/pyvolution/test_quantity.py
r1754 r1916 522 522 3*quantity3.vertex_values + 5) 523 523 524 524 525 #Powers 526 Q = quantity1**2 527 assert allclose(Q.vertex_values, quantity1.vertex_values**2) 528 529 Q = quantity1**2 +quantity2**2 530 assert allclose(Q.vertex_values, 531 quantity1.vertex_values**2 + \ 532 quantity2.vertex_values**2) 533 534 Q = (quantity1**2 +quantity2**2)**0.5 535 assert allclose(Q.vertex_values, 536 (quantity1.vertex_values**2 + \ 537 quantity2.vertex_values**2)**0.5) 538 539 540 541 542 543 525 544 526 545 def test_gradient(self): … … 1208 1227 1209 1228 1229 1210 1230 #------------------------------------------------------------- 1211 1231 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.