Changeset 7730
- Timestamp:
- May 17, 2010, 3:57:07 PM (15 years ago)
- 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 Domain1 from anuga.shallow_water.shallow_water_domain import Domain 2 2 3 # The different states the model can be in 4 STATE_SETUP, STATE_BUILT, STATE_RUNNING = range(0, 3) 3 5 4 class Model: 5 def __init__(self ):6 class Model: 7 def __init__(self, name): 6 8 self.domain = None # Domain class will be removed as we refactor the API 7 9 self.geometry_data = None 8 10 self.quantities = [] 11 self.boundaries = [] 12 self._state = STATE_SETUP 13 self.name = name 14 9 15 10 16 def set_geometry(self, *args, **kwargs): 11 17 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().' 13 21 else: 14 22 self.geometry_data = (args, kwargs) 15 16 def set_name(self, name): 17 self.name = name 23 18 24 19 25 def set_quantity(self, *args, **kwargs): 20 26 """Set values for named quantity 21 27 """ 28 if self._state != STATE_SETUP: 29 raise RuntimeError, 'Model already built - call this before build().' 22 30 23 31 self.quantities.append((args, kwargs)) 24 32 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) 29 38 30 args, kwargs = self.geometry_data31 39 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() 34 54 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.' 40 58 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.' 54 61 55 62 if len(self.geometry_data[0]) == 1: 56 raise InputError, 'Load mesh not implemented.'63 raise RuntimeError, 'Load mesh not implemented.' 57 64 else: 58 65 args, kwargs = self.geometry_data 59 66 67 self.domain = Domain(*args, **kwargs) 68 self.domain.set_name(self.name) 60 69 61 points, vertices, boundary = rectangular_cross(10, 5,62 len1=10.0, len2=5.0) # Mesh63 64 domain = Domain(points, vertices, boundary) # Create domain65 66 # domain = Domain(*args, **kwargs)67 domain.set_name(self.name)68 69 def topography(x, y):70 return -x/1071 domain.set_quantity('elevation', topography) # Use function for elevation72 domain.set_quantity('friction', 0.01) # Constant friction73 domain.set_quantity('stage', # Dry bed74 expression='elevation')75 76 print self.quantities77 78 70 for args, kwargs in self.quantities: 79 71 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 80 79 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 81 88 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 11 11 class modelTestCase(unittest.TestCase): 12 12 def setUp(self): 13 self.model = Model() 13 # construct and name model 14 self.model = Model('test_model') 14 15 pass 15 16 … … 38 39 from anuga.shallow_water import Dirichlet_boundary 39 40 40 self.model.set_geometry(*rectangular_cross(10, 5, len1=10.0, len2=5.0)) # Create domain41 self.model.set_name('channel1') # Output name41 def topography(x, y): 42 return -x/10 42 43 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') 48 48 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 51 64 self.model.set_quantity('stage', # Dry bed 52 expression='elevation') 65 expression='elevation') 53 66 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)) 54 94 self.model.build() 55 95 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 61 103 62 # domain.set_boundary({'left': Bi, 'right': Bi, 'top': Bi, 'bottom': Bi})63 64 #------------------------------------------------------------------------------65 # Evolve system through time66 #------------------------------------------------------------------------------67 # for t in domain.evolve(yieldstep=0.2, finaltime=40.0):68 # print domain.timestepping_statistics()69 70 104 71 105
Note: See TracChangeset
for help on using the changeset viewer.