Changeset 7505


Ignore:
Timestamp:
Sep 10, 2009, 2:14:40 PM (15 years ago)
Author:
steve
Message:

Added a new Time_space_boundary. Just provide the domain and a function of

t,x,y

Location:
anuga_core
Files:
5 edited

Legend:

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

    r7498 r7505  
    110110        self.verbose = verbose
    111111
    112        
     112        if domain is None:
     113            raise Exception, 'You must specify a domain to Time_boundary'
     114
    113115        # FIXME: Temporary code to deal with both f and function
    114116        if function is not None and f is not None:
     
    142144
    143145        d = len(domain.conserved_quantities)
    144         msg = 'Return value for f must be a list or an array of length %d' %d
     146        msg = 'Return value for function must be a list or an array of length %d' %d
    145147        assert len(q) == d, msg
    146148
     
    186188        return res
    187189
    188 
     190class Time_space_boundary(Boundary):
     191    """Time and spatially dependent boundary returns values for the
     192    conserved quantities as a function of time and space.
     193    Must specify domain to get access to model time and a function of t,x,y
     194    which must return conserved quantities at specified time and location.
     195   
     196    Example:
     197      B = Time_space_boundary(domain,
     198                              function=lambda t,x,y: [(60<t<3660)*2, 0, 0])
     199     
     200      This will produce a boundary condition with is a 2m high square wave
     201      starting 60 seconds into the simulation and lasting one hour.
     202      Momentum applied will be 0 at all times.
     203                       
     204    """
     205
     206
     207    def __init__(self, domain=None,
     208                 function=None,
     209                 default_boundary=None,
     210                 verbose=False):
     211        Boundary.__init__(self)
     212
     213        self.default_boundary = default_boundary
     214        self.default_boundary_invoked = False    # Flag
     215        self.domain = domain
     216        self.verbose = verbose
     217
     218       
     219        if function is None:
     220            raise Exception, 'You must specify a function to Time_space_boundary'
     221           
     222        try:
     223            q = function(0.0, 0.0, 0.0)
     224        except Exception, e:
     225            msg = 'Function for time_space_boundary could not be executed:\n%s' %e
     226            raise msg
     227
     228
     229        try:
     230            q = num.array(q, num.float)
     231        except:
     232            msg = 'Return value from time_space_boundary function could '
     233            msg += 'not be converted into a numeric array of floats.\n'
     234            msg += 'Specified function should return either list or array.\n'
     235            msg += 'I got %s' %str(q)
     236            raise msg
     237
     238        msg = 'ERROR: Time_space_boundary function must return a 1d list or array '
     239        assert len(q.shape) == 1, msg
     240
     241        d = len(domain.conserved_quantities)
     242        msg = 'Return value for function must be a list or an array of length %d' %d
     243        assert len(q) == d, msg
     244
     245        self.function = function
     246        self.domain = domain
     247
     248    def __repr__(self):
     249        return 'Time space boundary'
     250
     251    def evaluate(self, vol_id=None, edge_id=None):
     252        # FIXME (Ole): I think this should be get_time(), see ticket:306
     253
     254        [x,y] = self.domain.get_edge_midpoint_coordinate(vol_id, edge_id)
     255
     256        try:
     257            res = self.function(self.domain.get_time(), x, y)
     258        except Modeltime_too_early, e:
     259            raise Modeltime_too_early, e
     260        except Modeltime_too_late, e:
     261            if self.default_boundary is None:
     262                raise Exception, e # Reraise exception
     263            else:
     264                # Pass control to default boundary
     265                res = self.default_boundary.evaluate(vol_id, edge_id)
     266               
     267                # Ensure that result cannot be manipulated
     268                # This is a real danger in case the
     269                # default_boundary is a Dirichlet type
     270                # for instance.
     271                res = res.copy()
     272               
     273                if self.default_boundary_invoked is False:
     274                    if self.verbose:               
     275                        # Issue warning the first time
     276                        msg = '%s' %str(e)
     277                        msg += 'Instead I will use the default boundary: %s\n'\
     278                            %str(self.default_boundary)
     279                        msg += 'Note: Further warnings will be supressed'
     280                        log.critical(msg)
     281               
     282                    # FIXME (Ole): Replace this crude flag with
     283                    # Python's ability to print warnings only once.
     284                    # See http://docs.python.org/lib/warning-filter.html
     285                    self.default_boundary_invoked = True
     286
     287        return res
    189288
    190289class File_boundary(Boundary):
  • anuga_core/source/anuga/abstract_2d_finite_volumes/test_generic_boundary_conditions.py

    r7276 r7505  
    4747        q = Bd.evaluate()
    4848        assert num.allclose(q, x)
     49
     50
     51    def test_time(self):
     52        from quantity import Quantity
     53
     54        a = [0.0, 0.0]
     55        b = [0.0, 2.0]
     56        c = [2.0,0.0]
     57        d = [0.0, 4.0]
     58        e = [2.0, 2.0]
     59        f = [4.0,0.0]
     60
     61        points = [a, b, c, d, e, f]
     62
     63        #bac, bce, ecf, dbe
     64        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
     65
     66        domain = Domain(points, elements)
     67        domain.check_integrity()
     68
     69        domain.conserved_quantities = ['stage', 'ymomentum']
     70        domain.quantities['stage'] =\
     71                                   Quantity(domain, [[1,2,3], [5,5,5],
     72                                                     [0,0,9], [-6, 3, 3]])
     73
     74        domain.quantities['ymomentum'] =\
     75                                   Quantity(domain, [[2,3,4], [5,5,5],
     76                                                     [0,0,9], [-6, 3, 3]])
     77
     78
     79        domain.check_integrity()
     80
     81        #Test time bdry, you need to provide a domain and function
     82        try:
     83            T = Time_boundary(domain)
     84        except:
     85            pass
     86        else:
     87            raise 'Should have raised exception'
     88
     89        #Test time bdry, you need to provide a function
     90        try:
     91            T = Time_boundary()
     92        except:
     93            pass
     94        else:
     95            raise 'Should have raised exception'
     96
     97
     98        def function(t):
     99            return [1.0, 0.0]
     100       
     101        T = Time_boundary(domain, function)
     102
     103        from anuga.config import default_boundary_tag
     104        domain.set_boundary( {default_boundary_tag: T} )
     105
     106
     107        #FIXME: should not necessarily be true always.
     108        #E.g. with None as a boundary object.
     109        assert len(domain.boundary) == len(domain.boundary_objects)
     110
     111        q = T.evaluate(0, 2)  #Vol=0, edge=2
     112
     113        assert num.allclose(q, [1.0, 0.0])
     114
     115
     116    def test_time_space_boundary(self):
     117        from quantity import Quantity
     118
     119        a = [0.0, 0.0]
     120        b = [0.0, 2.0]
     121        c = [2.0,0.0]
     122        d = [0.0, 4.0]
     123        e = [2.0, 2.0]
     124        f = [4.0,0.0]
     125
     126        points = [a, b, c, d, e, f]
     127
     128        #bac, bce, ecf, dbe
     129        elements = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4] ]
     130
     131        domain = Domain(points, elements)
     132        domain.check_integrity()
     133
     134        domain.conserved_quantities = ['stage', 'ymomentum']
     135        domain.quantities['stage'] =\
     136                                   Quantity(domain, [[1,2,3], [5,5,5],
     137                                                     [0,0,9], [-6, 3, 3]])
     138
     139        domain.quantities['ymomentum'] =\
     140                                   Quantity(domain, [[2,3,4], [5,5,5],
     141                                                     [0,0,9], [-6, 3, 3]])
     142
     143
     144        domain.check_integrity()
     145
     146        #Test time space bdry, you need to provide a domain and function
     147        try:
     148            T = Time_space_boundary(domain)
     149        except:
     150            pass
     151        else:
     152            raise 'Should have raised exception'
     153
     154        #Test time bdry, you need to provide a function
     155        try:
     156            T = Time_space_boundary()
     157        except:
     158            pass
     159        else:
     160            raise 'Should have raised exception'
     161
     162
     163        def function(t,x,y):
     164            return [x,y]
     165       
     166        T = Time_space_boundary(domain, function)
     167
     168        from anuga.config import default_boundary_tag
     169        domain.set_boundary( {default_boundary_tag: T} )
     170
     171
     172        #FIXME: should not necessarily be true always.
     173        #E.g. with None as a boundary object.
     174        assert len(domain.boundary) == len(domain.boundary_objects)
     175
     176        q = T.evaluate(0, 2)  #Vol=0, edge=2
     177        assert num.allclose(q, domain.get_edge_midpoint_coordinate(0,2))
     178
     179
     180        q = T.evaluate(1, 1)  #Vol=0, edge=2
     181        assert num.allclose(q, domain.get_edge_midpoint_coordinate(1,1))       
     182
     183
     184
    49185
    50186
  • anuga_core/source/anuga_parallel/test_parallel_distribute_domain.py

    r7459 r7505  
    4848quantity = 'stage'
    4949nprocs = 4
    50 verbose = True
     50verbose = False
    5151
    5252#--------------------------------------------------------------------------
  • anuga_core/source/anuga_parallel/test_parallel_sw_flow.py

    r7452 r7505  
    3737N = 25
    3838M = 25
    39 verbose =  True
     39verbose = False
    4040
    4141#---------------------------------
  • anuga_core/test_all.py

    r7493 r7505  
    88os.chdir('source')
    99os.chdir('anuga')
    10    
     10print
     11print '======================= anuga tests ================================='   
    1112print 'Changing to', os.getcwd() # This is now different from buildroot   
    1213execfile('test_all.py')
     
    1617os.chdir('pymetis')
    1718print
    18 print '*********************************************************************'
     19print '===================== pymetis tests ================================='
    1920print 'Changing to', os.getcwd()
    2021execfile('test_all.py')
    2122
    22 #os.chdir('..')
    23 #os.chdir('anuga_parallel')
    24 #print
    25 #print '*********************************************************************'
    26 #print 'Changing to', os.getcwd()
    27 #execfile('test_all.py')
     23os.chdir('..')
     24os.chdir('anuga_parallel')
     25print
     26print '===================== anuga_parallel tests =========================='
     27print 'Changing to', os.getcwd()
     28execfile('test_all.py')
    2829
    2930print
Note: See TracChangeset for help on using the changeset viewer.