Changeset 7730


Ignore:
Timestamp:
May 17, 2010, 3:57:07 PM (14 years ago)
Author:
hudson
Message:

Widened scope of new ANUGA API test.

Location:
anuga_core/source/anuga/interface
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/interface/model.py

    r7728 r7730  
    1 from anuga.shallow_water.domain import Domain
     1from anuga.shallow_water.shallow_water_domain import Domain
    22
     3# The different states the model can be in
     4STATE_SETUP, STATE_BUILT, STATE_RUNNING = range(0, 3)
    35
    4 class Model:
    5     def __init__(self):
     6class Model:   
     7    def __init__(self, name):
    68        self.domain = None   # Domain class will be removed as we refactor the API
    79        self.geometry_data = None
    810        self.quantities = []
     11        self.boundaries = []
     12        self._state = STATE_SETUP
     13        self.name = name
     14
    915   
    1016    def set_geometry(self, *args, **kwargs):
    1117        if self.geometry_data:
    12             raise InputError, 'geometry already defined in model.'
     18            raise RuntimeError, 'geometry already defined in model.'
     19        elif self._state != STATE_SETUP:
     20            raise RuntimeError, 'Model already built - call this before build().'
    1321        else:
    1422            self.geometry_data = (args, kwargs)
    15        
    16     def set_name(self, name):
    17         self.name = name
     23
    1824       
    1925    def set_quantity(self, *args, **kwargs):
    2026        """Set values for named quantity
    2127        """
     28        if self._state != STATE_SETUP:
     29            raise RuntimeError, 'Model already built - call this before build().'
    2230
    2331        self.quantities.append((args, kwargs))
    2432
    25        
    26     def build(self):
    27         from anuga.shallow_water import Reflective_boundary
    28         from anuga.shallow_water import Dirichlet_boundary
     33    def get_quantity(self, name):
     34        if self._state != STATE_RUNNING:
     35            raise RuntimeError, 'Model needs to be running - call this after run().'
     36           
     37        return self.domain.get_quantity(name)
    2938
    30         args, kwargs = self.geometry_data
    3139
    32         self.domain = Domain(*args, **kwargs)  # Create domain
    33         self.domain.set_name('channel1')                  # Output name
     40    def set_boundary(self, *args, **kwargs):
     41        """Set the boundary properties.
     42        """
     43        if self._state == STATE_SETUP:
     44            # dont apply boundaries until model is built
     45            self.boundaries.append((args, kwargs))
     46        else:
     47            # model is running, apply boundaries right away
     48            self.domain.set_boundary(*args, **kwargs)
     49   
     50    def get_normals(self):
     51        # FIXME: This is a wrapper to allow reflective boundary to work.
     52        # Should be refactored and removed.
     53        return domain.get_normals()
    3454
    35         #------------------------------------------------------------------------------
    36         # Setup initial conditions
    37         #------------------------------------------------------------------------------
    38         def topography(x, y):
    39             return -x/10                             # linear bed slope
     55    def build(self):       
     56        if not self.geometry_data:
     57            raise RuntimeError, 'Model needs geometry. Set mesh or geometry.'
    4058
    41         self.domain.set_quantity('elevation', topography) # Use function for elevation
    42         self.domain.set_quantity('friction', 0.01)        # Constant friction
    43         self.domain.set_quantity('stage',                 # Dry bed
    44                             expression='elevation')         
    45         return
    46        
    47        
    48         from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross
    49         if not self.name:
    50             raise InputError, 'Model needs a name: use model.set_name().'
    51        
    52         if not self.geometry_data:
    53             raise InputError, 'Model needs geometry. Set mesh or geometry.'
     59        if self._state != STATE_SETUP:
     60            raise RuntimeError, 'Model already built.'
    5461
    5562        if len(self.geometry_data[0]) == 1:
    56             raise InputError, 'Load mesh not implemented.'
     63            raise RuntimeError, 'Load mesh not implemented.'
    5764        else:
    5865            args, kwargs = self.geometry_data
    5966
     67            self.domain = Domain(*args, **kwargs)
     68            self.domain.set_name(self.name)
    6069
    61             points, vertices, boundary = rectangular_cross(10, 5,
    62                                                            len1=10.0, len2=5.0) # Mesh
    63 
    64             domain = Domain(points, vertices, boundary)  # Create domain
    65 
    66         #    domain = Domain(*args, **kwargs)
    67             domain.set_name(self.name)
    68 
    69             def topography(x, y):
    70                 return -x/10   
    71             domain.set_quantity('elevation', topography) # Use function for elevation
    72             domain.set_quantity('friction', 0.01)        # Constant friction
    73             domain.set_quantity('stage',                 # Dry bed
    74                                 expression='elevation') 
    75            
    76             print self.quantities
    77            
    7870            for args, kwargs in self.quantities:
    7971                self.domain.set_quantity(*args, **kwargs)
     72
     73            for args, kwargs in self.boundaries:
     74                self.domain.set_boundary(*args, **kwargs)
     75
     76            self._state = STATE_BUILT
     77            self.geometry_data = None
     78            self.quantities = None
    8079       
     80
     81    def run(self, yieldstep, finaltime, verbose = False, timestep_function = None):
     82        if self._state != STATE_BUILT:
     83            msg = 'Model is not built, cannot run yet.'
     84            msg += ' Call build() before run().'
     85            raise RuntimeError, msg
     86 
     87        self._state = STATE_RUNNING
    8188       
     89        for t in self.domain.evolve(yieldstep, finaltime):
     90            if verbose == True:
     91                print self.domain.timestepping_statistics()
     92               
     93            # call the user's function at every timestep
     94            if timestep_function:
     95                timestep_function(self, t)
  • anuga_core/source/anuga/interface/test_model.py

    r7728 r7730  
    1111class modelTestCase(unittest.TestCase):
    1212    def setUp(self):
    13         self.model = Model()     
     13        # construct and name model
     14        self.model = Model('test_model')
    1415        pass
    1516
     
    3839        from anuga.shallow_water import Dirichlet_boundary
    3940       
    40         self.model.set_geometry(*rectangular_cross(10, 5, len1=10.0, len2=5.0))  # Create domain       
    41         self.model.set_name('channel1')                  # Output name
     41        def topography(x, y):
     42            return -x/10                         
    4243
    43         #------------------------------------------------------------------------------
    44         # Setup initial conditions
    45         #------------------------------------------------------------------------------
    46         def topography(x, y):
    47             return -x/10                             # linear bed slope
     44        # set quantities
     45        self.model.set_quantity('elevation', topography)
     46        self.model.set_quantity('friction', 0.01)         
     47        self.model.set_quantity('stage', expression='elevation') 
    4848
    49         self.model.set_quantity('elevation', topography) # Use function for elevation
    50         self.model.set_quantity('friction', 0.01)        # Constant friction
     49        # set properties of boundaries (reflective, flow in, etc.)
     50        Bi = Dirichlet_boundary([0.4, 0, 0])         # Inflow
     51        self.model.set_boundary({'left': Bi, 'right': Bi, 'top': Bi, 'bottom': Bi})
     52
     53        # set the geometry to use (may be a mesh file, or points/vertices tuple)
     54        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
     55
     56        # build, then run the simulation
     57        self.model.build()
     58        self.model.run(0.5, 1.0)
     59
     60
     61    def test_wrong_input_order(self):
     62        """The user tries to build before model is defined. """
     63       
    5164        self.model.set_quantity('stage',                 # Dry bed
    52                             expression='elevation') 
     65                            expression='elevation')         
    5366
     67        try:
     68            self.model.build()           
     69        except:
     70            pass
     71        else:
     72            msg = 'Should have raised exception for missing mesh'
     73            raise Exception, msg
     74
     75
     76    def test_duplicate_geometry(self):
     77        """The user tries to assign geometry twice. """
     78
     79        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
     80
     81        try:
     82            self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0)) 
     83        except:
     84            pass
     85        else:
     86            msg = 'Should have raised bad input exception'
     87            raise Exception, msg
     88
     89
     90    def test_input_after_build(self):
     91        """The user tries to change built model. """
     92
     93        self.model.set_geometry(*rectangular_cross(2, 2, len1=10.0, len2=5.0))
    5494        self.model.build()
    5595       
    56         #------------------------------------------------------------------------------
    57         # Setup boundary conditions
    58         #------------------------------------------------------------------------------
    59         Bi = Dirichlet_boundary([0.4, 0, 0])         # Inflow
    60     #    Br = Reflective_boundary(domain)             # Solid reflective wall
     96        try:
     97            self.model.set_quantity('friction', 0.01) 
     98        except:
     99            pass
     100        else:
     101            msg = 'Should have raised exception because model already built'
     102            raise Exception, msg
    61103
    62      #   domain.set_boundary({'left': Bi, 'right': Bi, 'top': Bi, 'bottom': Bi})
    63 
    64         #------------------------------------------------------------------------------
    65         # Evolve system through time
    66         #------------------------------------------------------------------------------
    67    #     for t in domain.evolve(yieldstep=0.2, finaltime=40.0):
    68    #         print domain.timestepping_statistics()       
    69        
    70104
    71105
Note: See TracChangeset for help on using the changeset viewer.