Changeset 750


Ignore:
Timestamp:
Jan 20, 2005, 11:18:13 AM (19 years ago)
Author:
ole
Message:

Test that initial values are indeed output at t == 0 (just to make sure).
Also a check that bounsry is created before starting evolve.

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

Legend:

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

    r659 r750  
    262262            msg = 'Conserved quantities must be a subset of all quantities'
    263263            assert quantity in self.quantities, msg
     264
     265        ##assert hasattr(self, 'boundary_objects')   
    264266   
    265267    def write_time(self):
     
    313315        from config import min_timestep, max_timestep, epsilon
    314316
    315 
     317        #FIXME: Maybe lump into a larger check prior to evolving
     318        msg = 'Boundary tags must be bound to boundary objects before evolving system, '
     319        msg += 'e.g. using the method set_boundary.\n'       
     320        msg += 'This system has the boundary tags %s ' %self.get_boundary_tags()
     321        assert hasattr(self, 'boundary_objects'), msg
     322       
    316323        ##self.set_defaults()
    317324
  • inundation/ga/storm_surge/pyvolution/shallow_water.py

    r715 r750  
    196196
    197197        #Store vertices and connectivity
    198         self.writer.store_connectivity()                   
     198        self.writer.store_connectivity()
     199
    199200
    200201    def store_timestep(self, name):
     
    206207        self.writer.store_timestep(name)
    207208       
     209
    208210#Rotation of momentum vector
    209211def rotate(q, normal, direction = 1):
     
    949951    """
    950952
    951     def __init__(self, s, phi):
     953    def __init__(self, *args, **kwargs):
    952954        """Initialise windfield from wind speed s [m/s]
    953955        and wind direction phi [degrees]
     
    983985        Alternatively, one vector valued function for (speed, angle)
    984986        can be applied, providing both quantities simultaneously.
    985         FIXME: Not yet implemented
     987        As in
     988        W = Wind_stress(F), where returns (speed, angle) for each t.         
    986989
    987990        domain.forcing_terms.append(W)
     
    991994        from Numeric import array, Float               
    992995
     996        if len(args) == 2:
     997            s = args[0]
     998            phi = args[1]
     999        elif len(args) == 1:
     1000            #Assume vector function returning (s, phi)(t,x,y)
     1001            vector_function = args[0]
     1002            s = lambda t,x,y: vector_function(t,x,y)[0]
     1003            phi = lambda t,x,y: vector_function(t,x,y)[1]
     1004        else:
     1005           #Assume info is in 2 keyword arguments
     1006       
     1007           if len(kwargs) == 2:
     1008               s = kwargs['s']
     1009               phi = kwargs['phi']
     1010           else:
     1011               raise 'Assumes two keyword arguments: s=..., phi=....'   
     1012           
    9931013        self.speed = check_forcefield(s)
    9941014        self.phi = check_forcefield(phi)
    995 
     1015           
    9961016        self.const = eta_w*rho_a/rho_w
    9971017       
  • inundation/ga/storm_surge/pyvolution/test_data_manager.py

    r353 r750  
    33
    44import unittest
     5import copy
    56from Numeric import zeros, array, allclose, Float
    67from util import mean
     
    5051               
    5152        domain.set_quantity('level', level)
     53        self.initial_level = copy.copy(domain.quantities['level'].vertex_values)
    5254
    5355        domain.distribute_to_vertices_and_edges()   
     
    366368        #Check contents
    367369        #Get NetCDF
    368         fid = NetCDFFile(sww.filename, 'r')  #Open existing file for append
     370        fid = NetCDFFile(sww.filename, 'r')
    369371     
    370372
     
    400402
    401403
     404    def test_sync(self):
     405        """Test info stored at each timestep is as expected (incl initial condition)
     406        """
     407
     408        import time, os, config
     409        from Numeric import array, zeros, allclose, Float, concatenate
     410        from Scientific.IO.NetCDF import NetCDFFile       
     411
     412        self.domain.filename = 'synctest'
     413        self.domain.format = 'sww'
     414        self.domain.smooth = False
     415        self.domain.store = True
     416
     417        #Evolution
     418        print
     419        for t in self.domain.evolve(yieldstep = 1.0, finaltime = 4.0):
     420            level = self.domain.quantities['level'].vertex_values
     421
     422            #Get NetCDF
     423            fid = NetCDFFile(self.domain.writer.filename, 'r')
     424            stage = fid.variables['stage']       
     425
     426            if t == 0.0:
     427                assert allclose(level, self.initial_level)
     428                assert allclose(stage[:], level.flat)
     429            else:
     430                assert not allclose(level, self.initial_level)
     431                assert not allclose(stage[:], level.flat)                 
     432
     433
     434
     435
    402436    def test_sww_DSG(self):
    403437        """Not a test, rather a look at the sww format
  • inundation/ga/storm_surge/pyvolution/test_shallow_water.py

    r648 r750  
    11#!/usr/bin/env python
    22
    3 import unittest
     3import unittest, os
    44from math import sqrt, pi
    55
     
    1818    from math import sqrt, exp, cos, pi
    1919
     20    x = array(x)
     21    y = array(y)   
     22
    2023    N = len(x)
    2124    s = 0*x  #New array
     
    4548    from math import sqrt, atan, pi
    4649
     50    x = array(x)
     51    y = array(y)   
    4752
    4853    N = len(x)
     
    649654            pass
    650655
     656
     657
     658
     659    #####################################################   
     660    def test_initial_condition(self):
     661        """Test that initial condition is output at time == 0
     662        """
     663
     664        from config import g
     665        import copy
     666       
     667        a = [0.0, 0.0]
     668        b = [0.0, 2.0]
     669        c = [2.0, 0.0]
     670        d = [0.0, 4.0]
     671        e = [2.0, 2.0]
     672        f = [4.0, 0.0]
     673
     674        points = [a, b, c, d, e, f]
     675        #bac, bce, ecf, dbe
     676        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     677       
     678        domain = Domain(points, vertices)
     679
     680        #Set up for a gradient of (3,0) at mid triangle         
     681        def slope(x, y):
     682            return 3*x
     683
     684        h = 0.1
     685        def level(x,y):
     686            return slope(x,y)+h
     687
     688        domain.set_quantity('elevation', slope)
     689        domain.set_quantity('level', level)
     690
     691        initial_level = copy.copy(domain.quantities['level'].vertex_values)
     692
     693        domain.set_boundary({'exterior': Reflective_boundary(domain)})
     694
     695        #Evolution
     696        for t in domain.evolve(yieldstep = 1.0, finaltime = 2.0):
     697            level = domain.quantities['level'].vertex_values
     698
     699            if t == 0.0:
     700                assert allclose(level, initial_level)
     701            else:
     702                assert not allclose(level, initial_level)
     703               
     704     
     705       
     706
    651707    #####################################################   
    652708    def test_gravity(self):
     
    879935
    880936
     937
     938    def test_windfield_from_file(self):
     939        from config import rho_a, rho_w, eta_w
     940        from math import pi, cos, sin, sqrt
     941        from config import time_format
     942        from util import File_function
     943        import time
     944       
     945
     946        a = [0.0, 0.0]
     947        b = [0.0, 2.0]
     948        c = [2.0, 0.0]
     949        d = [0.0, 4.0]
     950        e = [2.0, 2.0]
     951        f = [4.0, 0.0]
     952
     953        points = [a, b, c, d, e, f]
     954        #bac, bce, ecf, dbe
     955        vertices = [ [1,0,2], [1,2,4], [4,2,5], [3,1,4]]
     956
     957        domain = Domain(points, vertices)
     958
     959        #Flat surface with 1m of water
     960        domain.set_quantity('elevation', 0)
     961        domain.set_quantity('level', 1.0)
     962        domain.set_quantity('friction', 0)       
     963
     964        Br = Reflective_boundary(domain)
     965        domain.set_boundary({'exterior': Br})
     966
     967
     968        domain.time = 7 #Take a time that is represented in file (not zero)
     969
     970        #Write wind stress file (ensure that domain.time is covered)
     971        #Take x=1 and y=0
     972        filename = 'test_windstress_from_file.txt'
     973        start = time.mktime(time.strptime('2000', '%Y'))       
     974        fid = open(filename, 'w')
     975        dt = 1  #One second interval
     976        t = 0.0
     977        while t <= 10.0:
     978            t_string = time.strftime(time_format, time.gmtime(t+start))
     979
     980            fid.write('%s, %f %f\n' %(t_string,
     981                                      speed(t,[1],[0])[0],
     982                                      angle(t,[1],[0])[0]))
     983            t += dt
     984   
     985        fid.close()
     986
     987        #Setup wind stress
     988        F = File_function(filename)
     989        W = Wind_stress(F)
     990        domain.forcing_terms = []
     991        domain.forcing_terms.append(W)
     992       
     993        domain.compute_forcing_terms()
     994
     995        #Compute reference solution
     996        const = eta_w*rho_a/rho_w
     997       
     998        N = domain.number_of_elements
     999
     1000        t = domain.time
     1001
     1002        s = speed(t,[1],[0])[0]
     1003        phi = angle(t,[1],[0])[0]
     1004       
     1005        #Convert to radians
     1006        phi = phi*pi/180
     1007
     1008       
     1009        #Compute velocity vector (u, v)
     1010        u = s*cos(phi)
     1011        v = s*sin(phi)
     1012
     1013        #Compute wind stress
     1014        S = const * sqrt(u**2 + v**2)
     1015
     1016        for k in range(N):
     1017            assert allclose(domain.quantities['level'].explicit_update[k], 0)
     1018            assert allclose(domain.quantities['xmomentum'].explicit_update[k], S*u)
     1019            assert allclose(domain.quantities['ymomentum'].explicit_update[k], S*v)
     1020
     1021        os.remove(filename)
    8811022
    8821023    def test_wind_stress_error_condition(self):
  • inundation/ga/storm_surge/pyvolution/util.py

    r671 r750  
    301301        time [DD/MM/YY hh:mm:ss], x, y, value0 value1 value2 ...
    302302
    303     In either case, the callable obejct will return a tuple of
     303    In either case, the callable object will return a tuple of
    304304    interpolated values, one each value stated in the file.
    305305
     
    442442        """Evaluate f(t,x,y)
    443443
    444         FIXME: x, y dependency not yet implemented
     444        FIXME: x, y dependency not yet implemented except that
     445        result is a vector of same length as x and y
    445446        """
    446447
     
    473474                (self.T[self.index+1] - self.T[self.index])
    474475
    475         #Compute interpolated values for values
     476        #Compute interpolated values
    476477        q = self.Q[self.index,:] +\
    477478            ratio*(self.Q[self.index+1,:] - self.Q[self.index,:])
    478479
    479480        #Return vector of interpolated values
    480         return q
     481        if x == None and y == None:
     482            return q
     483        else:
     484            from Numeric import ones, Float
     485            #Create one constant column for each value
     486            N = len(x)
     487            assert len(y) == N, 'x and y must have same length'
     488
     489            res = []
     490            for col in q:
     491                res.append(col*ones(N, Float))
     492           
     493            return res
    481494   
    482495
Note: See TracChangeset for help on using the changeset viewer.