Changeset 8850


Ignore:
Timestamp:
Apr 26, 2013, 8:33:25 AM (12 years ago)
Author:
steve
Message:

Added in possibility for spatially dependent rate in rate_operators.py

Location:
trunk/anuga_core/source/anuga/operators
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/operators/base_operator.py

    r8824 r8850  
    2727        self.ymom_c  = self.domain.quantities['ymomentum'].centroid_values
    2828        self.elev_c  = self.domain.quantities['elevation'].centroid_values
     29        self.coord_c = self.domain.centroid_coordinates
    2930        self.areas = self.domain.areas
    3031
  • trunk/anuga_core/source/anuga/operators/rate_operators.py

    r8824 r8850  
    5757        self.factor = factor
    5858
     59        self.rate_callable = False
     60        self.rate_spatial = False
     61       
    5962        self.set_rate(rate)
    6063        self.set_default_rate(default_rate)
     
    7982        indices = self.indices
    8083
    81         rate = self.get_rate(t)
     84
     85        if self.rate_spatial:
     86            if self.indices is None:
     87                x = self.coord_c[:,0]
     88                y = self.coord_c[:,1]
     89            else:
     90                x = self.coord_c[indices,0]
     91                y = self.coord_c[indices,1]
     92            rate = self.get_spatial_rate(x,y,t)
     93        else:
     94            rate = self.get_rate(t)
    8295
    8396        if self.verbose is True:
     
    8598                         % (self.quantity_name, domain.get_time(), rate))
    8699
    87         if rate >= 0.0:
     100        if num.all(rate >= 0.0):
    88101            if self.indices is None:
    89102                self.stage_c[:] = self.stage_c[:]  \
     
    107120            t = self.get_time()
    108121
    109            
    110         if callable(self.rate):
     122
     123        if  self.rate_callable:
    111124            try:
    112125                rate = self.rate(t)
     
    146159        return rate
    147160
     161    def get_spatial_rate(self, x=None, y=None, t=None):
     162        """Provide a rate to calculate added volume
     163        only call if self.rate_spatial = True
     164        """
     165
     166        assert self.rate_spatial
     167
     168        if t is None:
     169            t = self.get_time()
     170
     171        if x is None:
     172            x = self.coord_c[:,0]
     173
     174        if y is None:
     175            y = self.coord_c[:,1]
     176
     177        #print xy
     178        #print t
     179        rate = self.rate(x,y,t)
     180
     181        return rate
     182
    148183
    149184    def set_rate(self, rate):
     
    158193        assert (isinstance(rate, (int, float)) or
    159194                callable(rate)), msg
     195
     196        self.rate_callable = False
     197        self.rate_spatial = False
     198
    160199        self.rate = rate
    161200
     201        if callable(rate):
     202            self.rate_callable = True
     203
     204            x = num.array([0.0, 1.0])
     205            y = num.array([0.0, 2.0])
     206            t =0.0
     207
     208            try:
     209                self.rate(x,y,t)
     210            except:
     211                #print 'Problem calling with two arguments'
     212                self.rate_spatial = False
     213                self.rate_callable = False
     214            else:
     215                self.rate_spatial = True
     216                self.rate_callable = True
     217
     218                #print self.rate_spatial , self.rate_callable
     219                return
     220
     221            try:
     222                self.rate(t)
     223            except:
     224                self.rate_callable = False
     225                #print 'Problem calling 1 argument'
     226            else:
     227                self.rate_callable = True
     228                self.rate_spatial = False
     229
     230        #print self.rate
     231        #print self.rate_spatial , self.rate_callable
    162232
    163233    def set_default_rate(self, default_rate):
  • trunk/anuga_core/source/anuga/operators/test_rate_operators.py

    r8488 r8850  
    11"""  Test environmental forcing - rain, wind, etc.
    22"""
     3import operator
    34
    45import unittest, os
     
    188189                                        'Attribute2'])
    189190
     191
    190192        #Now try interpolation
    191193        for i in range(20):
     
    383385        assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
    384386
     387
     388    def test_rate_operator_functions_spatial(self):
     389        from anuga.config import rho_a, rho_w, eta_w
     390        from math import pi, cos, sin
     391
     392        a = [0.0, 0.0]
     393        b = [0.0, 2.0]
     394        c = [2.0, 0.0]
     395        d = [0.0, 4.0]
     396        e = [2.0, 2.0]
     397        f = [4.0, 0.0]
     398
     399        points = [a, b, c, d, e, f]
     400        #             bac,     bce,     ecf,     dbe
     401        vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     402
     403        domain = Domain(points, vertices)
     404
     405        #Flat surface with 1m of water
     406        domain.set_quantity('elevation', 0.0)
     407        domain.set_quantity('stage', 1.0)
     408        domain.set_quantity('friction', 0.0)
     409
     410        Br = Reflective_boundary(domain)
     411        domain.set_boundary({'exterior': Br})
     412
     413        verbose = False
     414
     415        if verbose:
     416            print domain.quantities['elevation'].centroid_values
     417            print domain.quantities['stage'].centroid_values
     418            print domain.quantities['xmomentum'].centroid_values
     419            print domain.quantities['ymomentum'].centroid_values
     420
     421        # Apply operator to these triangles
     422        indices = [0,1,3]
     423        factor = 10.0
     424
     425
     426        def main_spatial_rate(x,y,t):
     427            # x and y should be an n by 1 array
     428            return x + y
     429
     430        default_rate = 0.0
     431
     432
     433        operator = Rate_operator(domain, rate=main_spatial_rate, factor=factor, \
     434                      indices=indices, default_rate = default_rate)
     435
     436
     437        # Apply Operator
     438        domain.timestep = 2.0
     439        operator()
     440
     441        t = operator.get_time()
     442        x = operator.coord_c[indices,0]
     443        y = operator.coord_c[indices,1]
     444        d = operator.get_timestep()*main_spatial_rate(x,y,t)*factor + 1
     445
     446        #print "d"
     447        #print d
     448        stage_ex = num.array([ 1.0,  1.0,   1.0,  1.0])
     449        stage_ex[indices] = d
     450
     451        if verbose:
     452            print domain.quantities['elevation'].centroid_values
     453            print domain.quantities['stage'].centroid_values
     454            print domain.quantities['xmomentum'].centroid_values
     455            print domain.quantities['ymomentum'].centroid_values
     456
     457        assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex)
     458        assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0)
     459        assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
     460
    385461           
    386462if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.