Changeset 8879


Ignore:
Timestamp:
Jun 2, 2013, 2:40:28 PM (12 years ago)
Author:
steve
Message:

added in some utilitiy functons to deal with evaluating functions of t , x,y
and x,y,t

Location:
trunk/anuga_core/source/anuga
Files:
2 added
1 edited

Legend:

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

    r8871 r8879  
    2222from anuga.fit_interpolate.interpolate import Modeltime_too_early, \
    2323                                              Modeltime_too_late
     24from anuga.utilities.function_utils import evaluate_temporal_function
    2425
    2526
     
    126127
    127128
    128         if  self.rate_callable:
    129             try:
    130                 rate = self.rate(t)
    131             except Modeltime_too_early, e:
    132                 raise Modeltime_too_early(e)
    133             except Modeltime_too_late, e:
    134                 if self.default_rate is None:
    135                     msg = '%s: ANUGA is trying to run longer than specified data.\n' %str(e)
    136                     msg += 'You can specify keyword argument default_rate in the '
    137                     msg += 'rate operator to tell it what to do in the absence of time data.'
    138                     raise Modeltime_too_late(msg)
    139                 else:
    140                     # Pass control to default rate function
    141                     rate = self.default_rate(t)
    142 
    143                     if self.default_rate_invoked is False:
    144                         # Issue warning the first time
    145                         msg = ('\n%s'
    146                            'Instead I will use the default rate: %s\n'
    147                            'Note: Further warnings will be supressed'
    148                            % (str(e), self.default_rate(t)))
    149                         warn(msg)
    150 
    151                         # FIXME (Ole): Replace this crude flag with
    152                         # Python's ability to print warnings only once.
    153                         # See http://docs.python.org/lib/warning-filter.html
    154                         self.default_rate_invoked = True
    155         else:
    156             rate = self.rate
     129        assert not self.rate_spatial
     130
     131
     132        rate = evaluate_temporal_function(self.rate, t, default_right_value=self.default_rate)
     133#        if  self.rate_callable:
     134#            try:
     135#                rate = self.rate(t)
     136#            except Modeltime_too_early, e:
     137#                raise Modeltime_too_early(e)
     138#            except Modeltime_too_late, e:
     139#                if self.default_rate is None:
     140#                    msg = '%s: ANUGA is trying to run longer than specified data.\n' %str(e)
     141#                    msg += 'You can specify keyword argument default_rate in the '
     142#                    msg += 'rate operator to tell it what to do in the absence of time data.'
     143#                    raise Modeltime_too_late(msg)
     144#                else:
     145#                    # Pass control to default rate function
     146#                    rate = self.default_rate(t)
     147#
     148#                    if self.default_rate_invoked is False:
     149#                        # Issue warning the first time
     150#                        msg = ('\n%s'
     151#                           'Instead I will use the default rate: %s\n'
     152#                           'Note: Further warnings will be supressed'
     153#                           % (str(e), self.default_rate(t)))
     154#                        warn(msg)
     155#
     156#                        # FIXME (Ole): Replace this crude flag with
     157#                        # Python's ability to print warnings only once.
     158#                        # See http://docs.python.org/lib/warning-filter.html
     159#                        self.default_rate_invoked = True
     160#        else:
     161#            rate = self.rate
    157162
    158163
     
    191196        #print xy
    192197        #print t
    193         rate = self.rate(x,y,t)
     198
     199        #print self.rate_type, self.rate_type == 'x,y,t'
     200        if self.rate_type == 'x,y,t':
     201            rate = self.rate(x,y,t)
     202        else:
     203            rate = self.rate(x,y)
    194204
    195205        return rate
     
    197207
    198208    def set_rate(self, rate):
    199         """Ability to change rate while running
    200         """
    201 
    202         #------------------------------------------
    203         # Check and store rate
    204         #------------------------------------------
    205         msg = ('Keyword argument rate must be a'
    206                'scalar, or a function of time.')
    207         assert (isinstance(rate, (int, float)) or
    208                 callable(rate)), msg
    209 
    210         self.rate_callable = False
    211         self.rate_spatial = False
     209        """Set rate (function)
     210        Can change rate while running
     211        """
     212
     213        from anuga.utilities.function_utils import determine_function_type
     214
     215        # Possible types are 'scalar', 't', 'x,y' and 'x,y,t'
     216        self.rate_type = determine_function_type(rate)
    212217
    213218        self.rate = rate
    214219
    215         if callable(rate):
     220        if self.rate_type == 'scalar':
     221            self.rate_callable = False
     222            self.rate_spatial = False
     223        elif self.rate_type == 't':
    216224            self.rate_callable = True
    217 
    218             x = num.array([0.0, 1.0])
    219             y = num.array([0.0, 2.0])
    220             t =0.0
    221 
    222             try:
    223                 self.rate(x,y,t)
    224             except:
    225                 #print 'Problem calling with two arguments'
    226                 self.rate_spatial = False
    227                 self.rate_callable = False
    228             else:
    229                 self.rate_spatial = True
    230                 self.rate_callable = True
    231 
    232                 #print self.rate_spatial , self.rate_callable
    233                 return
    234 
    235             try:
    236                 self.rate(t)
    237             except:
    238                 self.rate_callable = False
    239                 #print 'Problem calling 1 argument'
    240             else:
    241                 self.rate_callable = True
    242                 self.rate_spatial = False
    243 
    244         #print self.rate
    245         #print self.rate_spatial , self.rate_callable
     225            self.rate_spatial = False
     226        else:
     227            self.rate_callable = True
     228            self.rate_spatial = True
     229
     230
     231
     232
    246233
    247234
Note: See TracChangeset for help on using the changeset viewer.