Changeset 8361


Ignore:
Timestamp:
Mar 18, 2012, 9:07:07 PM (13 years ago)
Author:
steve
Message:

Working on rate operators (rain) to work in parallel

Location:
trunk/anuga_core/source/anuga
Files:
5 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/geometry/polygon.py

    r8257 r8361  
    229229
    230230def line_intersect(triangles, line, verbose=False):
    231     """Determine if a polygon and triangle overlap
     231    """Determine which of a list of trianglee intersect a line
    232232
    233233    """
     
    242242
    243243    if verbose:
    244         log.critical('Found %d triangles (out of %d) that overlap the polygon' % (count, M))
     244        log.critical('Found %d triangles (out of %d) that intersect line' % (count, M))
    245245
    246246    return indices[:count]
  • trunk/anuga_core/source/anuga/geometry/polygon_ext.c

    r8247 r8361  
    514514    if (A >= 1 && B >= 1)
    515515    {
    516         return 1; //polygon sits completely inside a triangle
     516        return 1; //line sits completely inside a triangle
    517517    }
    518518   
  • trunk/anuga_core/source/anuga/operators/base_operator.py

    r8357 r8361  
    66    """
    77
    8     def __init__(self, domain):
     8    counter = 0
     9
     10    def __init__(self,
     11                 domain,
     12                 description = None,
     13                 label = None,
     14                 logging = False,
     15                 verbose = False):
    916       
    1017        self.domain = domain
     
    1522            assert self.__parallel_safe(), msg
    1623
    17         self.logging = False
    18         self.logging_file = ''
     24        if description == None:
     25            self.description = ' '
     26        else:
     27            self.description = description
     28
     29
     30        if label == None:
     31            self.label = "inlet_%g" % Operator.counter
     32        else:
     33            self.label = label + '_%g' % Operator.counter
     34
     35
     36        self.verbose = verbose
     37
     38        # Keep count of inlet operator
     39        Operator.counter += 1
     40
     41        self.set_logging(logging)
     42
    1943
    2044    def __call__(self):
     
    3761        return message
    3862   
     63    def timestepping_statistics(self):
     64
     65        message  = 'You need to implement timestepping statistics for your operator'
     66        return message
     67
    3968
    4069    def print_statistics(self):
    4170
    4271        print self.statistics()
    43 
    44 
    45     def timestepping_statistics(self):
    46 
    47         message  = 'You need to implement timestepping statistics for your operator'
    48         return message
    4972
    5073    def print_timestepping_statistics(self):
     
    5881        if self.logging:
    5982            log_to_file(self.log_filename, self.timestepping_statistics())
    60            
     83
     84
     85
     86    def set_logging(self, flag=True):
     87
     88        self.logging = flag
     89
     90        # If flag is true open file with mode = "w" to form a clean file for logging
     91        if self.logging:
     92            self.log_filename = self.label + '.log'
     93            log_to_file(self.log_filename, self.statistics(), mode='w')
     94            log_to_file(self.log_filename, 'time,Q')
     95
     96            #log_to_file(self.log_filename, self.culvert_type)
     97
     98
  • trunk/anuga_core/source/anuga/operators/rate_operators.py

    r8360 r8361  
    1414import numpy as num
    1515import anuga.utilities.log as log
     16
     17from anuga.geometry.polygon import inside_polygon
    1618
    1719from anuga.operators.base_operator import Operator
     
    3638                 indices=None,
    3739                 default_rate=None,
    38                  verbose=False):
    39 
    40         Operator.__init__(self,domain)
     40                 description = None,
     41                 label = None,
     42                 logging = False,
     43                 verbose = False):
     44
     45
     46        anuga.Operator.__init__(self, domain, description, label, logging, verbose)
    4147
    4248        #------------------------------------------
     
    8187
    8288    def __call__(self):
     89        """
     90        Apply rate to those triangles defined in indices
     91
     92        indices == [] don't apply anywhere
     93        indices == None apply everywhere
     94        otherwise applyfor the specific indices
     95        """
    8396
    8497        if self.indices is []:
     
    103116
    104117    def update_rate(self, t):
    105         """Virtual method allowing local modifications by writing an
    106         overriding version in descendant
     118        """Provide a rate to calculate added volume
    107119        """
    108120
     
    146158
    147159    def __parallel_safe(self):
    148         """Operator is just applied independently to each cell and
     160        """Operator is applied independently to each cell and
    149161        so is parallel safe.
    150162        """
     
    166178
    167179#===============================================================================
    168 # Specific Rate Operators for special regions.
     180# Specific Rate Operators for circular region.
    169181#===============================================================================
    170182class Circular_rate_operator(Rate_operator):
     
    179191    def __init__(self, domain,
    180192                 rate=0.0,
    181                  center = None,
    182                  radius = None,
     193                 center=None,
     194                 radius=None,
    183195                 default_rate=None,
    184196                 verbose=False):
    185 
    186         if verbose: log.critical(self.__name__+': Beginning Initialisation')
    187 
    188197
    189198        assert center is not None
     
    205214
    206215            if ((x-c[0])**2+(y-c[1])**2) < r**2:
    207                 self.indices.append(k)
     216                indices.append(k)
    208217
    209218
     
    224233
    225234
     235#===============================================================================
     236# Specific Rate Operators for polygonal region.
     237#===============================================================================
     238class Polygonal_rate_operator(Rate_operator):
     239    """
     240    Add water at certain rate (ms^{-1} = vol/Area/sec) over a
     241    polygonal region
     242
     243    rate can be a function of time.
     244
     245    """
     246
     247    def __init__(self, domain,
     248                 rate=0.0,
     249                 polygon=None,
     250                 default_rate=None,
     251                 verbose=False):
     252
     253        assert center is not None
     254        assert radius is not None
     255
     256
     257        # Determine indices in update region
     258        N = domain.get_number_of_triangles()
     259        points = domain.get_centroid_coordinates(absolute=True)
     260
     261
     262        indices = inside_polygon(points, polygon)
     263
     264
     265        # It is possible that circle doesn't intersect with mesh (as can happen
     266        # for parallel runs
     267
     268
     269        Rate_operator.__init__(self,
     270                               domain,
     271                               rate=rate,
     272                               indices=indices,
     273                               default_rate=default_rate,
     274                               verbose=verbose)
     275
     276
     277        self.polygon = polygon
     278       
     279
     280
     281
     282
  • trunk/anuga_core/source/anuga/structures/inlet_operator.py

    r8228 r8361  
    11import anuga
    22import numpy
    3 import math
    43import inlet
    54
    6 from anuga.utilities.system_tools import log_to_file
    75
    8 
    9 class Inlet_operator:
     6class Inlet_operator(anuga.Operator):
    107    """Inlet Operator - add water to an inlet.
    118    Sets up the geometry of problem
     
    1714    """
    1815
    19     counter = 0
    2016
    2117    def __init__(self,
     
    2723                 logging = False,
    2824                 verbose = False):
     25
     26
     27        anuga.Operator.__init__(self, domain, description, label, logging, verbose)
     28
    2929       
    30         self.domain = domain
    31         self.domain.set_fractional_step_operator(self)
    3230        self.line = numpy.array(line, dtype='d')
    3331
     
    3533        self.Q = Q
    3634
    37         if description == None:
    38             self.description = ' '
    39         else:
    40             self.description = description
    41        
    42 
    43         if label == None:
    44             self.label = "inlet_%g" % Inlet_operator.counter
    45         else:
    46             self.label = label + '_%g' % Inlet_operator.counter
    47 
    48 
    49         self.verbose = verbose
    50 
    51         # Keep count of inlet operator
    52         Inlet_operator.counter += 1
    5335
    5436        self.enquiry_point = 0.5*(self.line[0] + self.line[1])
     
    5638        self.inlet = inlet.Inlet(self.domain, self.line, verbose= verbose)
    5739
    58         self.set_logging(logging)
     40        self.applied_Q = 0.0
    5941
    6042    def __call__(self):
     
    6345
    6446        t = self.domain.get_time()
     47
    6548        Q1 = self.update_Q(t)
    6649        Q2 = self.update_Q(t + timestep)
     50
     51        Q = 0.5*(Q1+Q2)
     52        volume = Q*timestep
    6753       
    68         volume = 0.5*(Q1+Q2)*timestep
    69        
    70         assert 0.5*(Q1+Q2) >= 0.0, 'Q < 0: Water to be removed from an inlet!'
    71        
     54        assert volume >= 0.0, 'Q < 0: Water to be removed from an inlet!'
     55
     56        # store last discharge
     57        self.applied_Q = Q
     58
    7259        # Distribute volume so as to obtain flat surface
    7360        self.inlet.set_stages_evenly(volume)
     
    7764       
    7865    def update_Q(self, t):
    79         """Virtual method allowing local modifications by writing an
    80         overriding version in descendant
     66        """Allowing local modifications of Q
    8167        """
    8268       
     
    121107
    122108
    123     def print_statistics(self):
    124 
    125         print self.statistics()
    126 
    127 
    128     def print_timestepping_statistics(self):
     109    def timestepping_statistics(self):
    129110
    130111        message = '---------------------------\n'
    131112        message += 'Inlet report for %s:\n' % self.label
    132113        message += '--------------------------\n'
    133         message += 'Q [m^3/s]: %.2f\n' % self.Q
    134        
    135 
    136         print message
    137 
    138 
    139     def set_logging(self, flag=True):
    140 
    141         self.logging = flag
    142 
    143         # If flag is true open file with mode = "w" to form a clean file for logging
    144         if self.logging:
    145             self.log_filename = self.label + '.log'
    146             log_to_file(self.log_filename, self.statistics(), mode='w')
    147             log_to_file(self.log_filename, 'time,Q')
    148 
    149             #log_to_file(self.log_filename, self.culvert_type)
    150 
    151 
    152     def timestepping_statistics(self):
    153 
    154         message  = '%.5f, ' % self.domain.get_time()
    155         message += '%.5f, ' % self.Q
     114        message += 'Q [m^3/s]: %.2f\n' % self.applied_Q
    156115
    157116        return message
    158 
    159     def log_timestepping_statistics(self):
    160 
    161          if self.logging:
    162              log_to_file(self.log_filename, self.timestepping_statistics())
    163 
    164117
    165118
  • trunk/anuga_core/source/anuga/structures/run_inlet_operator.py

    r8360 r8361  
    102102
    103103
    104 line = [[0.0, 5.0], [0.0, 10.0]]
     104line0 = [[0.0, 5.0], [0.0, 10.0]]
    105105
    106106Q = file_function('test_hydrograph.tms', quantities=['hydrograph'])
    107107
    108 Inlet_operator(domain, line, Q)
     108inlet0 = Inlet_operator(domain, line0, Q, label='first inlet')
    109109
     110
     111line1 = [[1.0, 5.0], [2.0, 10.0]]
     112inlet1 = Inlet_operator(domain, line1, 2.0)
    110113
    111114
     
    150153
    151154    print domain.volumetric_balance_statistics()
    152    
     155
     156    inlet0.print_timestepping_statistics()
     157    print inlet1.timestepping_statistics()
    153158    pass
    154159
Note: See TracChangeset for help on using the changeset viewer.