Changeset 6228
- Timestamp:
- Jan 22, 2009, 8:59:49 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py
r6226 r6228 21 21 from anuga.config import points_file_block_line_size as default_block_line_size 22 22 from anuga.config import epsilon 23 from anuga.caching import cache 23 24 24 25 import Numeric as num … … 33 34 # @param domain ?? 34 35 # @param vertex_values ?? 35 def __init__(self, domain, 36 vertex_values=None): 36 def __init__(self, domain, vertex_values=None): 37 37 from anuga.abstract_2d_finite_volumes.domain import Domain 38 38 39 msg = 'First argument in Quantity.__init__ ' 40 msg += 'must be of class Domain (or a subclass thereof). ' 41 msg += 'I got %s.' % str(domain.__class__) 39 msg = ('First argument in Quantity.__init__() must be of class Domain ' 40 '(or a subclass thereof). I got %s.' % str(domain.__class__)) 42 41 assert isinstance(domain, Domain), msg 43 42 … … 85 84 self.set_beta(1.0) 86 85 87 # 86 ############################################################################ 88 87 # Methods for operator overloading 89 # 90 91 ## 92 # @brief Get length of object. 93 # @return Return axis length of array. 88 ############################################################################ 89 94 90 def __len__(self): 95 91 return self.centroid_values.shape[0] 96 92 97 ##98 # @brief Negate all values in this quantity.99 93 def __neg__(self): 100 94 """Negate all values in this quantity giving meaning to the … … 106 100 return Q 107 101 108 ##109 # @brief Add two quantities.110 # @param other The other quantity (to the right).111 102 def __add__(self, other): 112 103 """Add to self anything that could populate a quantity … … 124 115 return result 125 116 126 ##127 # @brief Add two quantities.128 # @param other The other quantity (to the left).129 117 def __radd__(self, other): 130 118 """Handle cases like 7+Q, where Q is an instance of class Quantity … … 133 121 return self + other 134 122 135 ##136 # @brief Subtract two quantities.137 # @param other The other quantity (to the right).138 123 def __sub__(self, other): 139 124 return self + -other # Invoke self.__neg__() 140 125 141 ##142 # @brief Multiply two quantities.143 # @param other The other quantity (to the right).144 126 def __mul__(self, other): 145 127 """Multiply self with anything that could populate a quantity … … 168 150 return result 169 151 170 ##171 # @brief Multiply two quantities.172 # @param other The other quantity (to the left).173 152 def __rmul__(self, other): 174 153 """Handle cases like 3*Q, where Q is an instance of class Quantity … … 177 156 return self * other 178 157 179 ##180 # @brief Divide two quantities.181 # @param other The other quantity (to the right).182 158 def __div__(self, other): 183 159 """Divide self with anything that could populate a quantity … … 209 185 return result 210 186 211 ##212 # @brief Divide two quantities.213 # @param other The other quantity (to the left).214 187 def __rdiv__(self, other): 215 188 """Handle cases like 3/Q, where Q is an instance of class Quantity … … 218 191 return self / other 219 192 220 ##221 # @brief Raise a quaintity to some power.222 # @param other The power to raise quantity.223 193 def __pow__(self, other): 224 194 """Raise quantity to (numerical) power … … 248 218 249 219 return result 220 221 ############################################################################ 222 # Setters/Getters 223 ############################################################################ 250 224 251 225 ## … … 464 438 465 439 if smooth: 466 self.smooth_vertex_values() 440 self.smooth_vertex_values(use_cache=use_cache, 441 verbose=verbose) 467 442 468 443 return … … 491 466 indices, verbose) 492 467 elif type(numeric) in [num.ArrayType, ListType]: 493 self.set_values_from_array(numeric, location, indices, verbose) 468 self.set_values_from_array(numeric, location, indices, 469 use_cache=use_cache, verbose=verbose) 494 470 elif callable(numeric): 495 self.set_values_from_function(numeric, location, 496 indices, verbose) 471 self.set_values_from_function(numeric, location, indices, 472 use_cache=use_cache, 473 verbose=verbose) 497 474 elif isinstance(numeric, Quantity): 498 self.set_values_from_quantity(numeric, location, 499 indices,verbose)475 self.set_values_from_quantity(numeric, location, indices, 476 verbose=verbose) 500 477 elif isinstance(numeric, Geospatial_data): 501 478 self.set_values_from_geospatial_data(numeric, alpha, location, … … 510 487 msg = 'Argument function must be callable' 511 488 assert callable(function), msg 512 self.set_values_from_function(function, location, indices, verbose) 489 self.set_values_from_function(function, location, indices, 490 use_cache=use_cache, verbose=verbose) 513 491 elif geospatial_data is not None: 514 492 self.set_values_from_geospatial_data(geospatial_data, alpha, … … 539 517 540 518 541 # ---------------------------------------------------------------------------519 ############################################################################ 542 520 # Specific internal functions for setting values based on type 543 # ---------------------------------------------------------------------------521 ############################################################################ 544 522 545 523 ## … … 596 574 # @param location Where values are to be stored. 597 575 # @param indices Limit update to these indices. 576 # @param use_cache ?? 598 577 # @param verbose True if this method is to be verbose. 599 578 def set_values_from_array(self, values, 600 579 location='vertices', 601 580 indices=None, 581 use_cache=False, 602 582 verbose=False): 603 583 """Set values for quantity … … 657 637 'Values array must be 1d') 658 638 659 self.set_vertex_values(values.flat, indices=indices) 639 self.set_vertex_values(values.flat, indices=indices, 640 use_cache=use_cache, verbose=verbose) 660 641 else: 661 642 # Location vertices 662 643 if len(values.shape) == 1: 663 self.set_vertex_values(values, indices=indices) 644 # This is the common case arising from fitted 645 # values (e.g. from pts file). 646 self.set_vertex_values(values, indices=indices, 647 use_cache=use_cache, verbose=verbose) 664 648 elif len(values.shape) == 2: 665 649 # Vertex values are given as a triplet for each triangle … … 704 688 # @param location Where values are to be stored. 705 689 # @param indices ?? 690 # @param use_cache ?? 706 691 # @param verbose True if this method is to be verbose. 707 692 def set_values_from_function(self, f, 708 693 location='vertices', 709 694 indices=None, 695 use_cache=False, 710 696 verbose=False): 711 697 """Set values for quantity using specified function … … 731 717 732 718 V = num.take(self.domain.get_centroid_coordinates(), indices) 733 self.set_values(f(V[:,0], V[:,1]), 734 location=location, indices=indices) 719 x = V[:,0]; y = V[:,1] 720 if use_cache is True: 721 res = cache(f, (x, y), verbose=verbose) 722 else: 723 res = f(x, y) 724 725 self.set_values(res, location=location, indices=indices) 735 726 elif location == 'vertices': 727 # This is the default branch taken by set_quantity 736 728 M = self.domain.number_of_triangles 737 729 V = self.domain.get_vertex_coordinates() 738 730 739 731 x = V[:,0]; 740 y = V[:,1]; 741 values = f(x, y) 732 y = V[:,1] 733 if use_cache is True: 734 #print 'Caching function' 735 values = cache(f, (x, y), verbose=verbose) 736 else: 737 if verbose is True: 738 print 'Evaluating function in set_values' 739 values = f(x, y) 742 740 743 741 # FIXME (Ole): This code should replace all the … … 788 786 data_georef = geospatial_data.get_geo_reference() 789 787 788 from anuga.coordinate_transforms.geo_reference import Geo_reference 789 790 790 points = ensure_numeric(points, num.Float) 791 791 values = ensure_numeric(values, num.Float) … … 816 816 817 817 # Call underlying method using array values 818 self.set_values_from_array(vertex_attributes, location, 819 indices,verbose)818 self.set_values_from_array(vertex_attributes, location, indices, 819 use_cache=use_cache, verbose=verbose) 820 820 821 821 ## … … 902 902 903 903 # Call underlying method using array values 904 if verbose: 905 print 'Applying fitted data to domain' 904 906 self.set_values_from_array(vertex_attributes, location, 905 indices, verbose) 907 indices, use_cache=use_cache, 908 verbose=verbose) 906 909 907 910 ## … … 1213 1216 # @param A Array to set values with. 1214 1217 # @param indices Set of IDs of elements to work on. 1215 def set_vertex_values(self, A, indices=None): 1218 # @param use_cache ?? 1219 # @param verbose?? 1220 def set_vertex_values(self, A, 1221 indices=None, 1222 use_cache=False, 1223 verbose=False): 1216 1224 """Set vertex values for all unique vertices based on input array A 1217 1225 which has one entry per unique vertex, i.e. one value for each row in … … 1234 1242 vertex_list = indices 1235 1243 1244 #FIXME(Ole): This function ought to be faster. 1245 # We need to get the triangles_and_vertices list 1246 # from domain in one hit, then cache the computation of the 1247 # Nx3 array of vertex values that can then be assigned using 1248 # set_values_from_array. 1249 # 1250 # Alternatively, some C code would be handy 1251 # 1252 self._set_vertex_values(vertex_list, A) 1253 1254 ## 1255 # @brief Go through list of unique vertices. 1256 # @param vertex_list ?? 1257 # @param A ?? 1258 def _set_vertex_values(self, vertex_list, A): 1259 """Go through list of unique vertices 1260 This is the common case e.g. when values 1261 are obtained from a pts file through fitting 1262 """ 1263 1236 1264 # Go through list of unique vertices 1237 1265 for i_index, unique_vert_id in enumerate(vertex_list): … … 1252 1280 ## 1253 1281 # @brief Smooth vertex values. 1254 def smooth_vertex_values(self ):1282 def smooth_vertex_values(self, use_cache=False, verbose=False): 1255 1283 """Smooths vertex values.""" 1256 1284 1257 1285 A, V = self.get_vertex_values(xy=False, smooth=True) 1258 self.set_vertex_values(A )1286 self.set_vertex_values(A, use_cache=use_cache, verbose=verbose) 1259 1287 1260 1288 ############################################################################ … … 1267 1295 # @param smooth True if vertex values are to be smoothed. 1268 1296 # @param precision The type of the result values (default float). 1269 def get_vertex_values(self, xy=True, 1270 smooth=None, 1271 precision=None): 1297 def get_vertex_values(self, xy=True, smooth=None, precision=None): 1272 1298 """Return vertex values like an OBJ format i.e. one value per node. 1273 1299
Note: See TracChangeset
for help on using the changeset viewer.