Changeset 8506


Ignore:
Timestamp:
Aug 13, 2012, 3:47:51 PM (13 years ago)
Author:
steve
Message:

Adding in default argument

Location:
trunk/anuga_core/source/anuga
Files:
4 edited

Legend:

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

    r8486 r8506  
    149149
    150150
     151
     152
    151153#-------------------------------------------------------------------------------
    152154# Evolve system through time
  • trunk/anuga_core/source/anuga/operators/set_stage_operators.py

    r8475 r8506  
    9393                msg = '%s: ANUGA is trying to run longer than specified data.\n' %str(e)
    9494                msg += 'You can specify keyword argument default_rate in the '
    95                 msg += 'rate operator to tell it what to do in the absence of time data.'
     95                msg += 'stage function to tell it what to do in the absence of time data.'
    9696                raise Modeltime_too_late(msg)
    9797        else:
  • trunk/anuga_core/source/anuga/structures/inlet_operator.py

    r8488 r8506  
    33import inlet
    44
     5from warnings import warn
    56
    67class Inlet_operator(anuga.Operator):
     
    1920                 line,
    2021                 Q = 0.0,
     22                 default = None,
    2123                 description = None,
    2224                 label = None,
     
    4042        self.applied_Q = 0.0
    4143
     44        self.set_default(default)
     45
     46
    4247    def __call__(self):
    4348
     
    7176        """Allowing local modifications of Q
    7277        """
     78        from anuga.fit_interpolate.interpolate import Modeltime_too_early, Modeltime_too_late
    7379       
    7480        if callable(self.Q):
    75             Q = self.Q(t)[0]
     81            try:
     82                Q = self.Q(t)[0]
     83            except Modeltime_too_early, e:
     84                raise Modeltime_too_early(e)
     85            except Modeltime_too_late, e:
     86                Q = self.get_default(t,err_msg=e)
    7687        else:
    7788            Q = self.Q
     
    122133
    123134
     135    def set_default(self, default=None):
     136       
     137        """ Either leave default as None or change it into a function"""
     138
     139        if default is not None:
     140            # If it is a constant, make it a function
     141            if not callable(default):
     142                tmp = default
     143                default = lambda t: tmp
     144
     145            # Check that default_rate is a function of one argument
     146            try:
     147                default(0.0)
     148            except:
     149                raise Exception(msg)
     150
     151        self.default = default
     152        self.default_invoked = False
     153
     154
     155
     156    def get_default(self,t, err_msg=' '):
     157        """ Call get_default only if exception
     158        Modeltime_too_late(msg) has been raised
     159        """
     160
     161        from anuga.fit_interpolate.interpolate import Modeltime_too_early, Modeltime_too_late
     162
     163
     164        if self.default is None:
     165            msg = '%s: ANUGA is trying to run longer than specified data.\n' %str(err_msg)
     166            msg += 'You can specify keyword argument default in the '
     167            msg += 'operator to tell it what to do in the absence of time data.'
     168            raise Modeltime_too_late(msg)
     169        else:
     170            # Pass control to default rate function
     171            value = self.default(t)
     172
     173            if self.default_invoked is False:
     174                # Issue warning the first time
     175                msg = ('%s\n'
     176                       'Instead I will use the default rate: %s\n'
     177                       'Note: Further warnings will be supressed'
     178                       % (str(err_msg), str(self.default(t))))
     179                warn(msg)
     180
     181                # FIXME (Ole): Replace this crude flag with
     182                # Python's ability to print warnings only once.
     183                # See http://docs.python.org/lib/warning-filter.html
     184                self.default_invoked = True
     185
     186        return value
     187
     188
    124189    def set_Q(self, Q):
    125190
  • trunk/anuga_core/source/anuga/structures/test_inlet_operator.py

    r8198 r8506  
    183183
    184184        vol1 = domain.compute_total_volume()
     185
     186        #print vol1-vol0
    185187       
    186188        assert numpy.allclose(13.5, vol1-vol0, rtol=1.0e-8)
    187189               
    188 
     190    def test_inlet_variable_Q_default(self):
     191        """test_inlet_Q
     192
     193        This tests that the inlet operator adds the correct amount of water
     194        """
     195
     196        stage_0 = 11.0
     197        stage_1 = 10.0
     198        elevation_0 = 10.0
     199        elevation_1 = 10.0
     200
     201        domain_length = 200.0
     202        domain_width = 200.0
     203
     204
     205        domain = self._create_domain(d_length=domain_length,
     206                                     d_width=domain_width,
     207                                     dx = 10.0,
     208                                     dy = 10.0,
     209                                     elevation_0 = elevation_0,
     210                                     elevation_1 = elevation_1,
     211                                     stage_0 = stage_0,
     212                                     stage_1 = stage_1)
     213
     214        vol0 = domain.compute_total_volume()
     215
     216        finaltime = 5.0
     217
     218        #Make sure we are inthe right directory to find the
     219        #time series data for the inlets
     220        import os
     221        baseDir = os.getcwd()
     222
     223        try:
     224            os.chdir('structures')
     225        except:
     226            pass
     227
     228        line1 = [[95.0, 10.0], [105.0, 10.0]]
     229        Q1 = file_function(filename='inlet_operator_test1.tms', quantities=['hydrograph'])
     230
     231        line2 = [[10.0, 90.0], [20.0, 90.0]]
     232        Q2 = file_function(filename='inlet_operator_test2.tms', quantities=['hydrograph'])
     233
     234        os.chdir(baseDir)
     235
     236        Inlet_operator(domain, line1, Q1, default=6)
     237        Inlet_operator(domain, line2, Q2, default=3)
     238
     239        for t in domain.evolve(yieldstep = 1.0, finaltime = finaltime):
     240            #domain.write_time()
     241            #print domain.volumetric_balance_statistics()
     242            pass
     243
     244
     245        vol1 = domain.compute_total_volume()
     246
     247        #print vol1-vol0
     248
     249        assert numpy.allclose(31.5, vol1-vol0, rtol=1.0e-8)
    189250
    190251# =========================================================================
Note: See TracChangeset for help on using the changeset viewer.