Changeset 7793 for anuga_work/development/anuga_1d/quantity.py
- Timestamp:
- Jun 6, 2010, 10:48:41 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_work/development/anuga_1d/quantity.py
r6453 r7793 1 """Class Quantity - Implements values at each 1d element 1 """ 2 Class Quantity - Implements values at each 1d element 2 3 3 4 To create: … … 85 86 """ 86 87 return self.centroid_values.shape[0] 87 88 89 def __neg__(self): 90 """Negate all values in this quantity giving meaning to the 91 expression -Q where Q is an instance of class Quantity 92 """ 93 94 Q = Quantity(self.domain) 95 Q.set_values_from_numeric(-self.vertex_values) 96 return Q 97 98 99 100 def __add__(self, other): 101 """Add to self anything that could populate a quantity 102 103 E.g other can be a constant, an array, a function, another quantity 104 (except for a filename or points, attributes (for now)) 105 - see set_values_from_numeric for details 106 """ 107 108 Q = Quantity(self.domain) 109 Q.set_values_from_numeric(other) 110 111 result = Quantity(self.domain) 112 result.set_values_from_numeric(self.vertex_values + Q.vertex_values) 113 return result 114 115 def __radd__(self, other): 116 """Handle cases like 7+Q, where Q is an instance of class Quantity 117 """ 118 119 return self + other 120 121 def __sub__(self, other): 122 return self + -other # Invoke self.__neg__() 123 124 def __mul__(self, other): 125 """Multiply self with anything that could populate a quantity 126 127 E.g other can be a constant, an array, a function, another quantity 128 (except for a filename or points, attributes (for now)) 129 - see set_values_from_numeric for details 130 """ 131 132 if isinstance(other, Quantity): 133 Q = other 134 else: 135 Q = Quantity(self.domain) 136 Q.set_values_from_numeric(other) 137 138 result = Quantity(self.domain) 139 140 # The product of vertex_values, edge_values and centroid_values 141 # are calculated and assigned directly without using 142 # set_values_from_numeric (which calls interpolate). Otherwise 143 # centroid values wouldn't be products from q1 and q2 144 result.vertex_values = self.vertex_values * Q.vertex_values 145 result.centroid_values = self.centroid_values * Q.centroid_values 146 147 return result 148 149 def __rmul__(self, other): 150 """Handle cases like 3*Q, where Q is an instance of class Quantity 151 """ 152 153 return self * other 154 155 def __div__(self, other): 156 """Divide self with anything that could populate a quantity 157 158 E.g other can be a constant, an array, a function, another quantity 159 (except for a filename or points, attributes (for now)) 160 - see set_values_from_numeric for details 161 162 Zero division is dealt with by adding an epsilon to the divisore 163 FIXME (Ole): Replace this with native INF once we migrate to NumPy 164 """ 165 166 if isinstance(other, Quantity): 167 Q = other 168 else: 169 Q = Quantity(self.domain) 170 Q.set_values_from_numeric(other) 171 172 result = Quantity(self.domain) 173 174 # The quotient of vertex_values, edge_values and centroid_values 175 # are calculated and assigned directly without using 176 # set_values_from_numeric (which calls interpolate). Otherwise 177 # centroid values wouldn't be quotient of q1 and q2 178 result.vertex_values = self.vertex_values/(Q.vertex_values + epsilon) 179 result.centroid_values = self.centroid_values/(Q.centroid_values + epsilon) 180 181 return result 182 183 def __rdiv__(self, other): 184 """Handle cases like 3/Q, where Q is an instance of class Quantity 185 """ 186 187 return self / other 188 189 def __pow__(self, other): 190 """Raise quantity to (numerical) power 191 192 As with __mul__ vertex values are processed entry by entry 193 while centroid and edge values are re-interpolated. 194 195 Example using __pow__: 196 Q = (Q1**2 + Q2**2)**0.5 197 """ 198 199 if isinstance(other, Quantity): 200 Q = other 201 else: 202 Q = Quantity(self.domain) 203 Q.set_values_from_numeric(other) 204 205 result = Quantity(self.domain) 206 207 # The power of vertex_values, edge_values and centroid_values 208 # are calculated and assigned directly without using 209 # set_values_from_numeric (which calls interpolate). Otherwise 210 # centroid values wouldn't be correct 211 result.vertex_values = self.vertex_values ** other 212 result.centroid_values = self.centroid_values ** other 213 214 return result 215 216 def set_values_from_numeric(self, numeric): 217 import Numeric 218 x = Numeric.array([1.0,2.0]) 219 y = [1.0,2.0] 220 221 if type(numeric) == type(y): 222 self.set_values_from_array(numeric) 223 elif type(numeric) == type(x): 224 self.set_values_from_array(numeric) 225 elif callable(numeric): 226 self.set_values_from_function(numeric) 227 elif isinstance(numeric, Quantity): 228 self.set_values_from_quantity(numeric) 229 else: # see if it's coercible to a float (float, int or long, etc) 230 try: 231 numeric = float(numeric) 232 except ValueError: 233 msg = ("Illegal type for variable 'numeric': %s" % type(numeric)) 234 raise Exception, msg 235 self.set_values_from_constant(numeric) 236 237 def set_values_from_constant(self,numeric): 238 239 self.vertex_values[:] = numeric 240 self.centroid_values[:] = numeric 241 242 243 def set_values_from_array(self,numeric): 244 245 self.vertex_values[:] = numeric 246 self.interpolate() 247 248 249 def set_values_from_quantity(self,quantity): 250 251 self.vertex_values[:] = quantity.vertex_values 252 self.centroid_values[:] = quantity.centroid_values 253 254 def set_values_from_function(self,function): 255 256 self.vertex_values[:] = map(function, self.domain.vertices) 257 self.centroid_values[:] = map(function, self.domain.centroids) 258 259 88 260 def interpolate(self): 89 261 """ … … 218 390 assert values.shape[1] == 2, msg 219 391 220 self.vertex_values = values392 self.vertex_values[:] = values 221 393 222 394 … … 428 600 """ 429 601 430 #print 'compute_gradient'431 602 432 603 from Numeric import array, zeros, Float … … 908 1079 self.centroid_values[:] = (a*self.centroid_values + b*self.centroid_backup_values).astype('f') 909 1080 910 class Conserved_quantity(Quantity): 911 """Class conserved quantity adds to Quantity: 912 913 storage and method for updating, and 914 methods for extrapolation from centropid to vertices inluding 915 gradients and limiters 916 """ 917 918 def __init__(self, domain, vertex_values=None): 919 Quantity.__init__(self, domain, vertex_values) 920 921 print "Use Quantity instead of Conserved_quantity" 922 923 924 ## 925 ##def newLinePlot(title='Simple Plot'): 926 ## import Gnuplot 927 ## g = Gnuplot.Gnuplot() 928 ## g.title(title) 929 ## g('set data style linespoints') 930 ## g.xlabel('x') 931 ## g.ylabel('y') 932 ## return g 933 ## 934 ##def linePlot(g,x,y): 935 ## import Gnuplot 936 ## g.plot(Gnuplot.PlotItems.Data(x.flat,y.flat)) 1081 937 1082 938 1083 def newLinePlot(title='Simple Plot'):
Note: See TracChangeset
for help on using the changeset viewer.