Changeset 209


Ignore:
Timestamp:
Aug 24, 2004, 12:10:48 PM (21 years ago)
Author:
ole
Message:

testing of set_value and a bit of name changing

Location:
inundation/ga/storm_surge/pyvolution
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/domain.py

    r205 r209  
    133133        return q
    134134
    135     def set_quantity(self, name, values, location='vertices'):
     135    def set_quantity(self, name, X, location='vertices'):
    136136        """Set values for named quantity
    137137
    138138        name: Name of quantity
    139         values: Compatible list or Numeric array (see below)
     139        X: Compatible list, Numeric array, const or function (see below)
    140140        location: Where values are to be stored.
    141141                  Permissible options are: vertices, edges, centroid
     
    149149        """
    150150
    151         self.quantities[name].set_values(values, location)
     151        self.quantities[name].set_values(X, location)
    152152       
    153153
     
    461461            Q = self.quantities[name]
    462462            if self.order == 1:
    463                 Q.first_order_extrapolator()
     463                Q.extrapolate_first_order()
    464464            elif self.order == 2:
    465                 Q.second_order_extrapolator()
    466                 Q.limiter()                               
     465                Q.extrapolate_second_order()
     466                Q.limit()                               
    467467            else:
    468468                raise 'Unknown order'
  • inundation/ga/storm_surge/pyvolution/quantity.py

    r206 r209  
    8484            self.edge_values[i, 2] = 0.5*(v0 + v1)
    8585
     86
    8687    def update(self, timestep):
    8788        """Update centroid values based on values stored in
     
    185186       
    186187
    187     def limiter(self):
     188    def limit(self):
    188189        """Limit slopes for each volume to eliminate artificial variance
    189190        introduced by e.g. second order extrapolator
     
    245246
    246247       
    247     def first_order_extrapolator(self):
     248    def extrapolate_first_order(self):
    248249        """Extrapolate conserved quantities from centroid to
    249250        vertices for each volume using
     
    258259           
    259260           
    260     def second_order_extrapolator(self):
     261    def extrapolate_second_order(self):
    261262        """Extrapolate conserved quantities from centroid to
    262263        vertices for each volume using
     
    322323
    323324
    324         #FIXME: Should do location erro check here only
    325        
    326 
     325        #FIXME: Should do location error check here only
     326       
     327        if location not in ['vertices', 'centroids', 'edges']:
     328            msg = 'Invalid location: %s' %location
     329            raise msg
     330       
    327331        import types
    328332       
     
    331335            self.set_function_values(X, location)           
    332336        elif type(X) in [types.FloatType, types.IntType, types.LongType]:
    333             if location == 'vertices':
    334                 self.vertex_values[:] = X
     337            if location == 'centroids':
     338                self.centroid_values[:] = X
    335339            elif location == 'edges':
    336340                self.edge_values[:] = X
    337             elif location == 'centroids':
    338                 self.centroid_values[:] = X
    339341            else:
    340                 raise 'Invalid location: %s' %location               
     342                self.vertex_values[:] = X               
     343
    341344        else:
    342345            #Use array specific method
    343346            self.set_array_values(X, location)
     347
     348        if location == 'vertices':
     349            #Intialise centroid and edge_values
     350            self.interpolate()
     351           
    344352
    345353
     
    353361        """
    354362
    355 
    356         if location == 'vertices':
    357             V = self.domain.get_vertex_coordinates()
    358 
     363        if location == 'centroids':
     364            P = self.domain.centroids
     365            self.set_values(f(P[:,0], P[:,1]), location)
     366        elif location == 'edges':
     367            raise 'Not yet implemented: %s' %location
     368        else:
     369            #Vertices
     370            P = self.domain.get_vertex_coordinates()
    359371            for i in range(3):
    360                 self.set_values(f(V[:,2*i], V[:,2*i+1]), location)
    361         elif location == 'edges':
    362             raise 'Not yet implemented'
    363         elif location == 'centroids':
    364             V = self.domain.centroids
    365             self.set_values(f(V[:,0], V[:,1]), location)           
    366         else:
    367             raise 'Invalid location: %s' %location                           
     372                self.vertex_values[:,i] = f(P[:,2*i], P[:,2*i+1])
    368373
    369374           
     
    398403        assert values.shape[0] == N, msg
    399404       
    400         if location == 'vertices':
    401             assert len(values.shape) == 2, 'Values array must be 2d'
    402             msg = 'Array must be N x 3'           
    403             assert values.shape[1] == 3, msg
    404            
    405             self.vertex_values = values
    406            
    407             #Intialise centroid and edge_values
    408             self.interpolate()
     405        if location == 'centroids':
     406            assert len(values.shape) == 1, 'Values array must be 1d'
     407            self.centroid_values = values                       
    409408        elif location == 'edges':
    410409            assert len(values.shape) == 2, 'Values array must be 2d'
     
    413412           
    414413            self.edge_values = values
    415            
    416         elif location == 'centroids':
    417             assert len(values.shape) == 1, 'Values array must be 1d'
    418             self.centroid_values = values                       
    419414        else:
    420             raise 'Invalid location: %s' %location
     415            assert len(values.shape) == 2, 'Values array must be 2d'
     416            msg = 'Array must be N x 3'           
     417            assert values.shape[1] == 3, msg
     418           
     419            self.vertex_values = values
     420
  • inundation/ga/storm_surge/pyvolution/shallow_water.py

    r205 r209  
    274274    protect_against_infinitesimal_heights_centroid(domain)
    275275    if domain.order == 1:
    276         first_order_extrapolator(domain)
     276        extrapolate_first_order(domain)
    277277    elif domain.order == 2:
    278         second_order_extrapolator(domain)       
     278        extrapolate_second_order(domain)       
    279279    else:
    280280        raise 'Unknown order'
     
    312312
    313313
    314 def first_order_extrapolator(domain):
     314def extrapolate_first_order(domain):
    315315    """First order extrapolator function, specific
    316316    to the shallow water wave equation.
     
    332332    for name in domain.conserved_quantities:
    333333        Q = domain.quantities[name]
    334         Q.first_order_extrapolator()
     334        Q.extrapolate_first_order()
    335335        Q.interpolate_from_vertices_to_edges()       
    336336     
     
    396396
    397397       
    398 def second_order_extrapolator(domain):
     398def extrapolate_second_order(domain):
    399399    """Second order limiter function, specific to the shallow water wave
    400400    equation.
     
    427427        Q = domain.quantities[name]
    428428
    429         Q.second_order_extrapolator()
     429        Q.extrapolate_second_order()
    430430        Q.limiter()
    431431        Q.interpolate_from_vertices_to_edges()               
  • inundation/ga/storm_surge/pyvolution/test_quantity.py

    r205 r209  
    145145
    146146
     147    def test_set_values_const(self):
     148        quantity = Quantity(self.mesh4)
     149
     150        quantity.set_values(1.0, location = 'vertices')
     151        assert allclose(quantity.vertex_values,
     152                        [[1,1,1], [1,1,1], [1,1,1], [1, 1, 1]])
     153        assert allclose(quantity.centroid_values, [1, 1, 1, 1]) #Centroid
     154        assert allclose(quantity.edge_values, [[1, 1, 1],
     155                                               [1, 1, 1],
     156                                               [1, 1, 1],
     157                                               [1, 1, 1]])
     158
     159
     160        quantity.set_values(2.0, location = 'centroids')
     161        assert allclose(quantity.centroid_values, [2, 2, 2, 2])
     162
     163        quantity.set_values(3.0, location = 'edges')
     164        assert allclose(quantity.edge_values, [[3, 3, 3],
     165                                               [3, 3, 3],
     166                                               [3, 3, 3],
     167                                               [3, 3, 3]])       
     168
     169
     170    def test_set_values_func(self):
     171        quantity = Quantity(self.mesh4)
     172
     173        def f(x, y):
     174            return x+y
     175
     176        quantity.set_values(f, location = 'vertices')
     177        assert allclose(quantity.vertex_values,
     178                        [[2,0,2], [2,2,4], [4,2,4], [4,2,4]])       
     179        assert allclose(quantity.centroid_values,
     180                        [4.0/3, 8.0/3, 10.0/3, 10.0/3])
     181        assert allclose(quantity.edge_values,
     182                        [[1,2,1], [3,3,2], [3,4,3], [3,4,3]])               
     183
     184       
     185        quantity.set_values(f, location = 'centroids')
     186        assert allclose(quantity.centroid_values,
     187                        [4.0/3, 8.0/3, 10.0/3, 10.0/3])       
     188
     189
    147190    def test_gradient(self):
    148191        quantity = Quantity(self.mesh4)
     
    165208
    166209
    167         quantity.second_order_extrapolator()
     210        quantity.extrapolate_second_order()
    168211       
    169212        assert allclose(quantity.vertex_values, [[2., 2.,  2.],
     
    359402
    360403        #Extrapolate
    361         quantity.first_order_extrapolator()
     404        quantity.extrapolate_first_order()
    362405
    363406        #Check vertices but not edge values
     
    375418
    376419
    377         quantity.second_order_extrapolator()
    378         quantity.limiter()
     420        quantity.extrapolate_second_order()
     421        quantity.limit()
    379422
    380423
     
    408451
    409452        #Limit
    410         quantity.limiter()
     453        quantity.limit()
    411454
    412455        #Assert that central triangle is limited by neighbours
     
    439482
    440483        #Extrapolate
    441         quantity.first_order_extrapolator()
     484        quantity.extrapolate_first_order()
    442485
    443486        #Interpolate
Note: See TracChangeset for help on using the changeset viewer.