Changeset 5208


Ignore:
Timestamp:
Apr 11, 2008, 4:20:45 PM (16 years ago)
Author:
duncan
Message:

Add averaging to set region.

Location:
anuga_core/source/anuga/abstract_2d_finite_volumes
Files:
2 edited

Legend:

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

    r2491 r5208  
    1 """boundary.py - Classes for implementing region conditions
     1"""region.py - Classes for implementing region conditions
    22
    33NOTE: I've only set it up for testing X values of constants and functions.
     
    88# FIXME (DSG-DSG) add better comments
    99
     10from Numeric import average
    1011class Region:
    1112    """Base class for modifying quantities based on a region.
     
    6162    """
    6263    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.
    6371    """
    6472   
    65     def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None):
     73    def __init__(self, tag, quantity, X, location='vertices', initial_quantity=None, average=False):
    6674        #I have to get this going!
    6775        #Region.__init__(self)
     
    7078        self.location = location
    7179        self.X = X
     80        self.average = average
    7281        if initial_quantity is None:
    7382            self.quantity_initial_value = quantity
     
    8796            #              indices=self.build_indices(elements, domain),
    8897            #              location=self.location) + self.X
    89 
    9098            Q = domain.get_quantity(self.quantity_initial_value)
    91             new_values = Q.get_values(indices=self.build_indices(elements, domain),
    92                                       location=self.location) + self.X           
     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   
    93110            domain.set_quantity(self.quantity_answer, new_values,
    94111                                indices=self.build_indices(elements, domain),
     
    97114class Add_quantities(Region):
    98115    """
    99     Will add a value to the current quantity value.
     116    Will add a quantity to the current quantity value.
    100117    """
    101118   
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_region.py

    r3514 r5208  
    77from region import *
    88#from anuga.config import epsilon
    9 from Numeric import allclose #, array, ones, Float
     9from Numeric import allclose, average #, array, ones, Float
    1010
    1111
     
    174174                         [ 0.07,  0.07,  0.07],
    175175                         [ 0.07,  0.07,  0.07]])
     176                         
     177    def test_unique_vertices_average_loc_vert(self):
     178        """
     179        get values based on triangle lists.
     180        """
     181        from mesh_factory import rectangular
     182        from shallow_water import Domain
     183        from Numeric import zeros, Float
     184
     185        #Create basic mesh
     186        points, vertices, boundary = rectangular(1, 3)
     187        #Create shallow water domain
     188        domain = Domain(points, vertices, boundary)
     189        domain.build_tagged_elements_dictionary({'bottom':[0,1],
     190                                                 'top':[4,5],
     191                                                 'not_bottom':[2,3,4,5]})
     192
     193        #Set friction
     194        domain.set_quantity('friction', add_x_y)
     195        av_bottom = 2.0/3.0
     196        add = 60.0
     197        calc_frict = av_bottom + add
     198        domain.set_region(Add_value_to_region('bottom', 'friction', add,
     199                          initial_quantity='friction',
     200                           #location='unique vertices',
     201                           location='vertices',
     202                           average=True
     203                          ))
     204
     205        #print domain.quantities['friction'].get_values()
     206        frict_points = domain.quantities['friction'].get_values()
     207        assert allclose(frict_points[0],\
     208                        [ calc_frict, calc_frict, calc_frict])
     209        assert allclose(frict_points[1],\
     210                        [ calc_frict, calc_frict, calc_frict])
     211 
     212    def test_unique_vertices_average_loc_unique_vert(self):
     213        """
     214        get values based on triangle lists.
     215        """
     216        from mesh_factory import rectangular
     217        from shallow_water import Domain
     218        from Numeric import zeros, Float
     219
     220        #Create basic mesh
     221        points, vertices, boundary = rectangular(1, 3)
     222        #Create shallow water domain
     223        domain = Domain(points, vertices, boundary)
     224        domain.build_tagged_elements_dictionary({'bottom':[0,1],
     225                                                 'top':[4,5],
     226                                                 'not_bottom':[2,3,4,5]})
     227
     228        #Set friction
     229        domain.set_quantity('friction', add_x_y)
     230        av_bottom = 2.0/3.0
     231        add = 60.0
     232        calc_frict = av_bottom + add
     233        domain.set_region(Add_value_to_region('bottom', 'friction', add,
     234                          initial_quantity='friction',
     235                           location='unique vertices',
     236                           average=True
     237                          ))
     238
     239        #print domain.quantities['friction'].get_values()
     240        frict_points = domain.quantities['friction'].get_values()
     241        assert allclose(frict_points[0],\
     242                        [ calc_frict, calc_frict, calc_frict])
     243        assert allclose(frict_points[1],\
     244                        [ calc_frict, calc_frict, calc_frict])
     245        assert allclose(frict_points[2],\
     246                        [ calc_frict, 1.0 + 2.0/3.0, calc_frict])
     247        assert allclose(frict_points[3],\
     248                        [ 2.0/3.0,calc_frict, 1.0 + 2.0/3.0])
     249                                               
     250                         
    176251#-------------------------------------------------------------
    177252if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.