Changeset 6221 for anuga_core/source


Ignore:
Timestamp:
Jan 21, 2009, 4:32:32 PM (16 years ago)
Author:
ole
Message:

Added more keyword arguments and enabled caching of set_values_from_function.
There may be a problem if the function is a callable object, so this needs to be tested.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/abstract_2d_finite_volumes/quantity.py

    r6197 r6221  
    2222from anuga.config import points_file_block_line_size as default_block_line_size
    2323from anuga.config import epsilon
    24 
     24from anuga.caching import cache
    2525
    2626
     
    424424
    425425            if smooth:
    426                 self.smooth_vertex_values()
     426                self.smooth_vertex_values(use_cache=use_cache,
     427                                          verbose=verbose)
    427428
    428429               
     
    464465            elif type(numeric) in [num.ArrayType, ListType]:
    465466                self.set_values_from_array(numeric,
    466                                            location, indices, verbose)
     467                                           location, indices,
     468                                           use_cache=use_cache,
     469                                           verbose=verbose)
    467470            elif callable(numeric):
    468471                self.set_values_from_function(numeric,
    469                                               location, indices, verbose)
     472                                              location, indices,
     473                                              use_cache=use_cache,
     474                                              verbose=verbose)
    470475            elif isinstance(numeric, Quantity):
    471476                self.set_values_from_quantity(numeric,
    472                                               location, indices, verbose)
     477                                              location, indices,
     478                                              verbose=verbose)
    473479            elif isinstance(numeric, Geospatial_data):
    474480                self.set_values_from_geospatial_data(numeric,
     
    488494            assert callable(function), msg
    489495            self.set_values_from_function(function,
    490                                           location, indices, verbose)
     496                                          location,
     497                                          indices,
     498                                          use_cache=use_cache,
     499                                          verbose=verbose)
    491500        elif geospatial_data is not None:
    492501                self.set_values_from_geospatial_data(geospatial_data,
     
    587596                              location='vertices',
    588597                              indices=None,
     598                              use_cache=False,
    589599                              verbose=False):
    590600        """Set values for quantity
     
    645655                   'Values array must be 1d'
    646656
    647             self.set_vertex_values(values.flat, indices=indices)
     657            self.set_vertex_values(values.flat,
     658                                   indices=indices,
     659                                   use_cache=use_cache,
     660                                   verbose=verbose)
    648661           
    649662        else:
    650663            # Location vertices
    651664            if len(values.shape) == 1:
    652                 self.set_vertex_values(values, indices=indices)
     665                # This is the common case arising from fitted
     666                # values (e.g. from pts file).
     667                self.set_vertex_values(values,
     668                                       indices=indices,
     669                                       use_cache=use_cache,
     670                                       verbose=verbose)
    653671
    654672            elif len(values.shape) == 2:
     
    691709                                 location='vertices',
    692710                                 indices=None,
     711                                 use_cache=False,
    693712                                 verbose=False):
    694713        """Set values for quantity using specified function
     
    718737               
    719738            V = num.take(self.domain.get_centroid_coordinates(), indices)
    720             self.set_values(f(V[:,0], V[:,1]),
     739
     740            x = V[:,0]; y = V[:,1]
     741            if use_cache is True:
     742                res = cache(f, (x, y),
     743                            verbose=verbose)
     744            else:
     745                res = f(x, y)
     746
     747            self.set_values(res,
    721748                            location=location,
    722749                            indices=indices)
    723750           
    724751        elif location == 'vertices':
    725 
     752            # This is the default branch taken by set_quantity
     753           
    726754            M = self.domain.number_of_triangles
    727755            V = self.domain.get_vertex_coordinates()
    728756
    729             x = V[:,0]; y = V[:,1];                     
    730             values = f(x, y)
    731 
     757            x = V[:,0]; y = V[:,1]
     758            if use_cache is True:
     759                #print 'Caching function'
     760                values = cache(f, (x, y),
     761                               verbose=verbose)               
     762            else:
     763                if verbose is True:
     764                    print 'Evaluating function in set_values'
     765                values = f(x, y)
     766
     767            #print 'value', min(x), max(x), min(y), max(y), max(values), len(values)
     768               
    732769
    733770            # FIXME (Ole): This code should replace all the
     
    818855        # Call underlying method using array values
    819856        self.set_values_from_array(vertex_attributes,
    820                                    location, indices, verbose)
     857                                   location, indices,
     858                                   use_cache=use_cache,
     859                                   verbose=verbose)
    821860
    822861
     
    886925                                           
    887926        # Call underlying method using array values
     927        if verbose:
     928            print 'Applying fitted data to domain'
    888929        self.set_values_from_array(vertex_attributes,
    889                                    location, indices, verbose)
     930                                   location, indices,
     931                                   use_cache=use_cache,
     932                                   verbose=verbose)
    890933
    891934   
     
    11861229
    11871230
    1188     def set_vertex_values(self, A, indices = None):
     1231    def set_vertex_values(self, A,
     1232                          indices=None,
     1233                          use_cache=False,
     1234                          verbose=False):
    11891235        """Set vertex values for all unique vertices based on input array A
    11901236        which has one entry per unique vertex, i.e.
     
    12101256            vertex_list = indices
    12111257
    1212         # Go through list of unique vertices
     1258
     1259        #FIXME(Ole): This function ought to be faster.
     1260        # We need to get the triangles_and_vertices list
     1261        # from domain in one hit, then cache the computation of the
     1262        # Nx3 array of vertex values that can then be assigned using
     1263        # set_values_from_array.
     1264        #
     1265        # Alternatively, some C code would be handy
     1266        #
     1267        self._set_vertex_values(vertex_list, A)
     1268           
     1269
     1270    def _set_vertex_values(self, vertex_list, A):
     1271        """Go through list of unique vertices
     1272        This is the common case e.g. when values
     1273        are obtained from a pts file through fitting
     1274        """
     1275       
    12131276        for i_index, unique_vert_id in enumerate(vertex_list):
    1214 
    12151277
    12161278            triangles = self.domain.get_triangles_and_vertices_per_node(node=unique_vert_id)
     
    12281290
    12291291
    1230     def smooth_vertex_values(self):
     1292    def smooth_vertex_values(self,
     1293                             use_cache=False,
     1294                             verbose=False):
    12311295        """ Smooths vertex values.
    12321296        """
    12331297
    12341298        A,V = self.get_vertex_values(xy=False, smooth=True)
    1235         self.set_vertex_values(A)
     1299        self.set_vertex_values(A,
     1300                               use_cache=use_cache,
     1301                               verbose=verbose)                               
     1302                               
    12361303
    12371304
Note: See TracChangeset for help on using the changeset viewer.