Changeset 835


Ignore:
Timestamp:
Feb 7, 2005, 10:02:41 AM (20 years ago)
Author:
ole
Message:

Worked on file boundary and conversions

Location:
inundation/ga/storm_surge/pyvolution
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/generic_boundary_conditions.py

    r623 r835  
    116116
    117117
    118 class File_boundary(Boundary):
     118class File_boundary_time(Boundary):
    119119    """Boundary values obtained from file and interpolated.
    120120    conserved quantities as a function of time.
     
    123123
    124124    No spatial info assumed.
     125    FIXME: Soon to be Obsolete
    125126    """
    126127
     
    156157        t = self.domain.time
    157158        return self.F(t)
     159
     160
     161
     162
     163class File_boundary(Boundary):
     164    """Boundary values obtained from file and interpolated.
     165    conserved quantities as a function of time.
     166
     167    Assumes that file contains a time series and possibly
     168    also spatial info.
     169    See docstring for File_function in util.py for details about
     170    admissible file formats
     171    """
     172
     173
     174    def __init__(self, filename, domain):
     175        import time
     176        from Numeric import array
     177        from config import time_format
     178        from util import File_function
     179       
     180        Boundary.__init__(self)
     181
     182        self.F = File_function(filename, domain)
     183        self.domain = domain
     184
     185        #Get x,y vertex coordinates for all triangles
     186        V = domain.vertex_coordinates
     187       
     188        #Compute midpoint coordinates for all boundary elements
     189        midpoint_coordinates = {}
     190        for vol_id, edge_id in domain.boundary.keys():
     191           
     192            x0 = V[vol_id, 0]; y0 = V[vol_id, 1]
     193            x1 = V[vol_id, 2]; y1 = V[vol_id, 3]
     194            x2 = V[vol_id, 4]; y2 = V[vol_id, 5]           
     195
     196            #Midpoints       
     197            if edge_id == 0: m = array([(x1 + x2)/2, (y1 + y2)/2])
     198            if edge_id == 1: m = array([(x0 + x2)/2, (y0 + y2)/2])
     199            if edge_id == 2: m = array([(x1 + x0)/2, (y1 + y0)/2])           
     200            midpoint_coordinates[(vol_id, edge_id)] = m
     201       
     202        self.midpoint_coordinates = midpoint_coordinates   
     203
     204        #Test
     205        q = self.F(0, 0, 0)
     206
     207        d = len(domain.conserved_quantities)
     208        msg = 'Values specified in file must be a list or an array of length %d' %d
     209        assert len(q) == d, msg
     210
     211       
     212    def __repr__(self):
     213        return 'File boundary'
     214
     215    def evaluate(self, vol_id=None, edge_id=None):
     216        """Return linearly interpolated values based on domain.time
     217
     218        vol_id and edge_id are ignored
     219        """
     220
     221        t = self.domain.time
     222
     223        if vol_id is not None and edge_id is not None:
     224            x, y = self.midpoint_coordinates[ vol_id, edge_id ]
     225            return self.F(t, x, y)
     226        else:
     227            return self.F(t)
     228
     229
     230
     231
    158232
    159233
  • inundation/ga/storm_surge/pyvolution/test_data_manager.py

    r834 r835  
    560560        fid.close()
    561561       
    562         os.remove(filename)
    563         os.remove(root + '.xya')       
    564 
     562
     563        os.remove(root + '.xya')
     564        os.remove(root + '.dem')               
     565        os.remove(root + '.asc')       
    565566       
    566567       
  • inundation/ga/storm_surge/pyvolution/test_generic_boundary_conditions.py

    r773 r835  
    101101
    102102
    103     def test_fileboundary(self):
     103    def test_fileboundary_time_only(self):
    104104        """Test that boundary values can be read from file and interpolated
    105105        """
     
    113113        a = [0.0, 0.0]
    114114        b = [0.0, 2.0]
    115         c = [2.0,0.0]
     115        c = [2.0, 0.0]
    116116        d = [0.0, 4.0]
    117117        e = [2.0, 2.0]
    118         f = [4.0,0.0]
     118        f = [4.0, 0.0]
    119119
    120120        points = [a, b, c, d, e, f]
     
    135135        domain.check_integrity()
    136136       
    137         domain.check_integrity()
    138137
    139138        #Write file
     
    151150       
    152151        F = File_boundary(filename, domain)
    153         os.remove(filename)       
    154 
     152        os.remove(filename)
     153       
     154
     155        #Check that midpoint coordinates at boundary are correctly computed
     156        #print domain.boundary
     157        #print F.midpoint_coordinates
     158        assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0])
     159        assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0])
     160        assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0])
     161        assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0])
     162        assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0])
     163        assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0])         
     164
     165
     166        #Check time interpolation
    155167        from config import default_boundary_tag
    156168        domain.set_boundary( {default_boundary_tag: F} )
     
    167179
    168180
    169 
     181    def test_fileboundary_time_and_space(self):
     182        """Test that boundary values can be read from file and interpolated
     183        Include both time and space
     184        """
     185
     186        from domain import Domain
     187        from quantity import Quantity
     188
     189        raise 'Not yet implemented'
    170190
    171191    def test_fileboundary_exception(self):
  • inundation/ga/storm_surge/pyvolution/test_least_squares.py

    r834 r835  
    512512        assert allclose(attributes, ref)
    513513
    514        
     514        os.remove(FN)
    515515       
    516516
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r834 r835  
    192192        assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )
    193193       
    194 
     194        os.remove(filename)
    195195       
    196196
     
    268268        assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )
    269269       
    270 
     270        os.remove(filename)
     271       
    271272
    272273    def test_file_function_time_with_domain_different_start(self):
     
    336337        assert allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )
    337338       
     339        os.remove(filename)       
    338340
    339341    def test_xya_ascii(self):
  • inundation/ga/storm_surge/pyvolution/util.py

    r834 r835  
    109109    NOTE: At this stage the function is assumed to depend on
    110110    time only, i.e  no spatial dependency!!!!!
    111     When that is need we can use the least_squares interpolation.
     111    When that is needed we can use the least_squares interpolation.
    112112    """
    113113
     
    189189            self.read_times() #Now read all times in.
    190190        else:
    191             raise 'x,y dependecy not yet implemented'
     191            raise 'x,y dependency not yet implemented'
    192192
    193193       
     
    237237        FIXME: x, y dependency not yet implemented except that
    238238        result is a vector of same length as x and y
     239        FIXME: Naaaa
    239240        """
    240241
     
    275276            return q
    276277        else:
    277             from Numeric import ones, Float
    278             #Create one constant column for each value
    279             N = len(x)
    280             assert len(y) == N, 'x and y must have same length'
    281 
    282             res = []
    283             for col in q:
    284                 res.append(col*ones(N, Float))
     278            try:
     279                N = len(x)
     280            except:
     281                return q
     282            else:
     283                from Numeric import ones, Float
     284                #x is a vector - Create one constant column for each value
     285                N = len(x)
     286                assert len(y) == N, 'x and y must have same length'
     287             
     288                res = []
     289                for col in q:
     290                    res.append(col*ones(N, Float))
    285291           
    286             return res
     292                return res
     293           
    287294
    288295def read_xya(filename, format = 'netcdf'):
Note: See TracChangeset for help on using the changeset viewer.