Пример #1
0
def get_timeseries(sww_filename,gauges):
    """Generate time series for sww file based on gauges
    """

    gauge_locations = gauges.values()
    gauge_names = gauges.keys()

    #tempfile = 'xyz1234tempfile.sww' # Has to end with sww

    #os.system('cp %s %s' % (sww_filename, tempfile))

    f = file_function(sww_filename, 
                      quantities='stage',
                      interpolation_points=gauge_locations,
                      use_cache=True,
                      verbose=True)

    timevector = f.get_time()

    timeseries = {}
    for k, name in enumerate(gauge_names):
        model = timeseries[name] = []
        
        for t in timevector:
            model.append(f(t, point_id=k)[0])


    return num.array(timevector), timeseries
Пример #2
0
 def create_a_rain_operator(domain, base_filename):
     
     polygon = read_polygon(join(rain_polygon_dir, base_filename+'.csv'))
     rate = file_function(join(rain_rate_dir, base_filename+'.tms'), quantities=['rate'])
     
     return Rate_operator(domain, rate=rate, factor=1.0e-3, \
                          polygon = polygon)
def setup_boundaries(simulation):
    """
    Setup boundary conditions
    """

    domain = simulation.domain

    Bd = anuga.Dirichlet_boundary([0, 0, 0])
    #Bw = anuga.Time_boundary(domain=domain, function=wrapped_file_function)

    func = anuga.file_function(join('Forcing', 'Tide', 'Pioneer.tms'),
                               quantities='rainfall')
    Bw = anuga.Time_boundary(domain=domain,
                             function=lambda t: [func(t), 0.0, 0.0])

    domain.set_boundary({'west': Bd, 'south': Bd, 'north': Bd, 'east': Bw})
def wrapped_file_function(t):
    func = anuga.file_function(join('Forcing', 'Tide', 'Pioneer.tms'),
                               quantities='rainfall')
    return [func(t), 0.0, 0.0]
Пример #5
0
    def test_rate_operator_rate_from_file(self):
        from anuga.config import rho_a, rho_w, eta_w
        from math import pi, cos, sin

        a = [0.0, 0.0]
        b = [0.0, 2.0]
        c = [2.0, 0.0]
        d = [0.0, 4.0]
        e = [2.0, 2.0]
        f = [4.0, 0.0]

        points = [a, b, c, d, e, f]
        #             bac,     bce,     ecf,     dbe
        vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        #---------------------------------
        #Typical ASCII file
        #---------------------------------
        finaltime = 1200
        filename = 'test_file_function'
        fid = open(filename + '.txt', 'w')
        start = time.mktime(time.strptime('2000', '%Y'))
        dt = 60  #One minute intervals
        t = 0.0
        while t <= finaltime:
            t_string = time.strftime(time_format, time.gmtime(t + start))
            fid.write('%s, %f %f %f\n' %
                      (t_string, 2 * t, t**2, sin(t * pi / 600)))
            t += dt

        fid.close()

        #Convert ASCII file to NetCDF (Which is what we really like!)
        timefile2netcdf(filename + '.txt')

        #Create file function from time series
        F = file_function(
            filename + '.tms',
            quantities=['Attribute0', 'Attribute1', 'Attribute2'])

        #Now try interpolation
        for i in range(20):
            t = i * 10
            q = F(t)

            #Exact linear intpolation
            assert num.allclose(q[0], 2 * t)
            if i % 6 == 0:
                assert num.allclose(q[1], t**2)
                assert num.allclose(q[2], sin(t * pi / 600))

        #Check non-exact

        t = 90  #Halfway between 60 and 120
        q = F(t)
        assert num.allclose((120**2 + 60**2) / 2, q[1])
        assert num.allclose((sin(120 * pi / 600) + sin(60 * pi / 600)) / 2,
                            q[2])

        t = 100  #Two thirds of the way between between 60 and 120
        q = F(t)
        assert num.allclose(2 * 120**2 / 3 + 60**2 / 3, q[1])
        assert num.allclose(
            2 * sin(120 * pi / 600) / 3 + sin(60 * pi / 600) / 3, q[2])

        #os.remove(filename + '.txt')
        #os.remove(filename + '.tms')

        domain = Domain(points, vertices)

        #Flat surface with 1m of water
        domain.set_quantity('elevation', 0)
        domain.set_quantity('stage', 1.0)
        domain.set_quantity('friction', 0)

        Br = Reflective_boundary(domain)
        domain.set_boundary({'exterior': Br})

        #        print domain.quantities['elevation'].centroid_values
        #        print domain.quantities['stage'].centroid_values
        #        print domain.quantities['xmomentum'].centroid_values
        #        print domain.quantities['ymomentum'].centroid_values

        # Apply operator to these triangles
        indices = [0, 1, 3]

        rate = file_function('test_file_function.tms',
                             quantities=['Attribute1'])

        factor = 1000.0
        default_rate = 17.7

        operator = Rate_operator(domain, rate=rate, factor=factor, \
                      indices=indices, default_rate = default_rate)

        # Apply Operator
        domain.set_starttime(360.0)
        domain.timestep = 1.0

        operator()

        d = domain.get_time()**2 * factor + 1.0
        stage_ex0 = [d, d, 1., d]

        #        print d, domain.get_time(), F(360.0)

        #        print domain.quantities['elevation'].centroid_values
        #        print domain.quantities['stage'].centroid_values
        #        print domain.quantities['xmomentum'].centroid_values
        #        print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values,
                            stage_ex0)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.fractional_step_volume_integral,
                            ((d - 1.) * domain.areas[indices]).sum())

        domain.set_starttime(-10.0)
        domain.timestep = 1.0

        try:
            operator()
        except:
            pass
        else:
            raise Exception('Should have raised an exception, time too early')

        domain.set_starttime(1300.0)
        domain.timestep = 1.0

        operator()

        d = default_rate * factor + d
        stage_ex1 = [d, d, 1., d]

        #        print domain.quantities['elevation'].centroid_values
        #        print domain.quantities['stage'].centroid_values
        #        print domain.quantities['xmomentum'].centroid_values
        #        print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values,
                            stage_ex1)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.fractional_step_volume_integral,
                            ((d - 1.) * domain.areas[indices]).sum())
Пример #6
0
    domain.set_name(output_filename)  # Name of output sww file
    domain.set_minimum_storable_height(0.001)  # Don't store w < 0.01m
    domain.set_store_vertices_smoothly(True)
    domain.set_flow_algorithm(alg)
else:

    domain = None

domain = distribute(domain)

#-------------------------
# Boundary Conditions
#-------------------------

# Create boundary function from timeseries provided in file
wave_function = anuga.file_function(boundary_filename, domain, verbose=verbose)

# Create and assign boundary objects
Bts = anuga.Transmissive_n_momentum_zero_t_momentum_set_stage_boundary(
    domain, wave_function)
Br = anuga.Reflective_boundary(domain)
domain.set_boundary({'wave': Bts, 'wall': Br})

#-------------------------------------------------------------------------
# Produce a documentation of parameters
#-------------------------------------------------------------------------
from anuga.validation_utilities import save_parameters_tex

save_parameters_tex(domain)

#-------------------------
Пример #7
0
    def test_rate_operator_rate_from_file(self):
        from anuga.config import rho_a, rho_w, eta_w
        from math import pi, cos, sin

        a = [0.0, 0.0]
        b = [0.0, 2.0]
        c = [2.0, 0.0]
        d = [0.0, 4.0]
        e = [2.0, 2.0]
        f = [4.0, 0.0]

        points = [a, b, c, d, e, f]
        #             bac,     bce,     ecf,     dbe
        vertices = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        #---------------------------------
        #Typical ASCII file
        #---------------------------------
        finaltime = 1200
        filename = 'test_file_function'
        fid = open(filename + '.txt', 'w')
        start = time.mktime(time.strptime('2000', '%Y'))
        dt = 60  #One minute intervals
        t = 0.0
        while t <= finaltime:
            t_string = time.strftime(time_format, time.gmtime(t + start))
            fid.write('%s, %f %f %f\n' %
                      (t_string, 2 * t, t**2, sin(old_div(t * pi, 600))))
            t += dt

        fid.close()

        #Convert ASCII file to NetCDF (Which is what we really like!)
        timefile2netcdf(filename + '.txt')

        #Create file function from time series
        F = file_function(
            filename + '.tms',
            quantities=['Attribute0', 'Attribute1', 'Attribute2'])

        #Now try interpolation
        for i in range(20):
            t = i * 10
            q = F(t)

            #Exact linear intpolation
            assert num.allclose(q[0], 2 * t)
            if i % 6 == 0:
                assert num.allclose(q[1], t**2)
                assert num.allclose(q[2], sin(old_div(t * pi, 600)))

        #Check non-exact

        t = 90  #Halfway between 60 and 120
        q = F(t)
        assert num.allclose(old_div((120**2 + 60**2), 2), q[1])
        assert num.allclose(
            old_div((sin(old_div(120 * pi, 600)) + sin(old_div(60 * pi, 600))),
                    2), q[2])

        t = 100  #Two thirds of the way between between 60 and 120
        q = F(t)
        assert num.allclose(old_div(2 * 120**2, 3) + old_div(60**2, 3), q[1])
        assert num.allclose(
            old_div(2 * sin(old_div(120 * pi, 600)), 3) +
            old_div(sin(old_div(60 * pi, 600)), 3), q[2])

        #os.remove(filename + '.txt')
        #os.remove(filename + '.tms')

        domain = Domain(points, vertices)

        #Flat surface with 1m of water
        domain.set_quantity('elevation', 0)
        domain.set_quantity('stage', 1.0)
        domain.set_quantity('friction', 0)

        Br = Reflective_boundary(domain)
        domain.set_boundary({'exterior': Br})

        #        print domain.quantities['elevation'].centroid_values
        #        print domain.quantities['stage'].centroid_values
        #        print domain.quantities['xmomentum'].centroid_values
        #        print domain.quantities['ymomentum'].centroid_values

        # Apply operator to these triangles
        indices = [0, 1, 3]

        rate = file_function(filename + '.tms', quantities=['Attribute1'])

        # Make starttime of domain consistent with tms file starttime
        domain.set_starttime(rate.starttime)

        factor = 1000.0
        default_rate = 17.7

        operator = Rate_operator(domain, rate=rate, factor=factor, \
                      indices=indices, default_rate = default_rate)

        # Apply Operator
        domain.set_time(360.0)
        domain.timestep = 1.0

        operator()

        d = domain.get_time()**2 * factor + 1.0
        stage_ex0 = [d, d, 1., d]

        #        print d, domain.get_time(), F(360.0)

        #        print domain.quantities['elevation'].centroid_values
        #        print domain.quantities['stage'].centroid_values
        #        print domain.quantities['xmomentum'].centroid_values
        #        print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values,
                            stage_ex0)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.fractional_step_volume_integral,
                            ((d - 1.) * domain.areas[indices]).sum())

        domain.set_time(1300.0)
        domain.timestep = 1.0

        operator()

        d = default_rate * factor + d
        stage_ex1 = [d, d, 1., d]

        #         print domain.quantities['elevation'].centroid_values
        #         print domain.quantities['stage'].centroid_values
        #         print domain.quantities['xmomentum'].centroid_values
        #         print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values,
                            stage_ex1)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.fractional_step_volume_integral,
                            ((d - 1.) * domain.areas[indices]).sum())

        tmp = numpy.zeros_like(domain.quantities['stage'].centroid_values)
        tmp[:] = domain.quantities['stage'].centroid_values

        d0 = domain.fractional_step_volume_integral

        domain.set_time(-10.0)
        domain.timestep = 1.0

        operator()

        d = default_rate * factor
        stage_ex2 = numpy.array([d, d, 0., d]) + numpy.array(stage_ex1)

        assert num.allclose(domain.quantities['stage'].centroid_values,
                            stage_ex2)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values,
                            0.0)
        assert num.allclose(domain.fractional_step_volume_integral,
                            d0 + (d * domain.areas[indices]).sum())

        # test timestepping_statistics
        stats = operator.timestepping_statistics()
        import re
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?",
                        stats)
        assert num.allclose(float(rr[1]), 17.7)
        assert num.allclose(float(rr[2]), 106200.0)
    def test_rate_operator_rate_from_file(self):
        from anuga.config import rho_a, rho_w, eta_w
        from math import pi, cos, sin

        a = [0.0, 0.0]
        b = [0.0, 2.0]
        c = [2.0, 0.0]
        d = [0.0, 4.0]
        e = [2.0, 2.0]
        f = [4.0, 0.0]

        points = [a, b, c, d, e, f]
        #             bac,     bce,     ecf,     dbe
        vertices = [[1,0,2], [1,2,4], [4,2,5], [3,1,4]]


        #---------------------------------
        #Typical ASCII file
        #---------------------------------
        finaltime = 1200
        filename = 'test_file_function'
        fid = open(filename + '.txt', 'w')
        start = time.mktime(time.strptime('2000', '%Y'))
        dt = 60  #One minute intervals
        t = 0.0
        while t <= finaltime:
            t_string = time.strftime(time_format, time.gmtime(t+start))
            fid.write('%s, %f %f %f\n' %(t_string, 2*t, t**2, sin(t*pi/600)))
            t += dt

        fid.close()

        #Convert ASCII file to NetCDF (Which is what we really like!)
        timefile2netcdf(filename+'.txt')


        #Create file function from time series
        F = file_function(filename + '.tms',
                          quantities = ['Attribute0',
                                        'Attribute1',
                                        'Attribute2'])


        #Now try interpolation
        for i in range(20):
            t = i*10
            q = F(t)

            #Exact linear intpolation
            assert num.allclose(q[0], 2*t)
            if i%6 == 0:
                assert num.allclose(q[1], t**2)
                assert num.allclose(q[2], sin(t*pi/600))

        #Check non-exact

        t = 90 #Halfway between 60 and 120
        q = F(t)
        assert num.allclose( (120**2 + 60**2)/2, q[1] )
        assert num.allclose( (sin(120*pi/600) + sin(60*pi/600))/2, q[2] )


        t = 100 #Two thirds of the way between between 60 and 120
        q = F(t)
        assert num.allclose( 2*120**2/3 + 60**2/3, q[1] )
        assert num.allclose( 2*sin(120*pi/600)/3 + sin(60*pi/600)/3, q[2] )

        #os.remove(filename + '.txt')
        #os.remove(filename + '.tms')


        domain = Domain(points, vertices)

        #Flat surface with 1m of water
        domain.set_quantity('elevation', 0)
        domain.set_quantity('stage', 1.0)
        domain.set_quantity('friction', 0)

        Br = Reflective_boundary(domain)
        domain.set_boundary({'exterior': Br})

#        print domain.quantities['elevation'].centroid_values
#        print domain.quantities['stage'].centroid_values
#        print domain.quantities['xmomentum'].centroid_values
#        print domain.quantities['ymomentum'].centroid_values

        # Apply operator to these triangles
        indices = [0,1,3]


        rate = file_function(filename + '.tms', quantities=['Attribute1'])
        

        # Make starttime of domain consistent with tms file starttime
        domain.set_starttime(rate.starttime)
                    
        factor = 1000.0
        default_rate= 17.7

        operator = Rate_operator(domain, rate=rate, factor=factor, \
                      indices=indices, default_rate = default_rate)


        # Apply Operator
        domain.set_time(360.0)
        domain.timestep = 1.0

        operator()



        d = domain.get_time()**2 * factor + 1.0
        stage_ex0 = [ d,  d,   1.,  d]

#        print d, domain.get_time(), F(360.0)

#        print domain.quantities['elevation'].centroid_values
#        print domain.quantities['stage'].centroid_values
#        print domain.quantities['xmomentum'].centroid_values
#        print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex0)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
        assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())


        domain.set_time(-10.0)
        domain.timestep = 1.0

        try:
            operator()
        except:
            pass
        else:
            raise Exception('Should have raised an exception, time too early')


        domain.set_time(1300.0)
        domain.timestep = 1.0

        operator()

        d = default_rate*factor + d
        stage_ex1 = [ d,  d,   1.,  d]

#        print domain.quantities['elevation'].centroid_values
#        print domain.quantities['stage'].centroid_values
#        print domain.quantities['xmomentum'].centroid_values
#        print domain.quantities['ymomentum'].centroid_values

        assert num.allclose(domain.quantities['stage'].centroid_values, stage_ex1)
        assert num.allclose(domain.quantities['xmomentum'].centroid_values, 0.0)
        assert num.allclose(domain.quantities['ymomentum'].centroid_values, 0.0)
        assert num.allclose(domain.fractional_step_volume_integral, ((d-1.)*domain.areas[indices]).sum())
Пример #9
0
def wrapped_file_function(t):
    func = anuga.file_function(join('Forcing','Tide','Pioneer.tms'), quantities='rainfall')
    return [func(t), 0.0, 0.0]    
Пример #10
0
    quantity_names=None,
    time_as_seconds=True)
TS_file = 'tmsfile_SD_CF.tms'
print 'TMS file ' + TS_file + ' created'
# --------

Bd = anuga.Dirichlet_boundary([tide, 0, 0])  # Mean water level
Bs = anuga.Transmissive_stage_zero_momentum_boundary(
    domain)  # Neutral boundary

if project.scenario == 'fixed_wave':
    # Huge 17.135m wave starting after 60 seconds and lasting 60 minutes.
    Bw = anuga.Transmissive_n_momentum_zero_t_momentum_set_stage_boundary(
        domain=domain, function=lambda t: [(60 < t < 3660) * 17.135, 0, 0])
    # Option 2: Set boundary stage from predefined TMS file output from EasyWave
    wave_f = anuga.file_function(TS_file, domain, quantities='Attribute0')
    Btms = anuga.Transmissive_momentum_set_stage_boundary(
        domain=domain, function=lambda t: [wave_f(t), 0, 0])
    # Bt used if transmissive boundary raise too small timestep exception
    Bt = anuga.Time_boundary(domain=domain,
                             function=lambda t: [wave_f(t), 0, 0])

    domain.set_boundary({
        'onshore': Bd,
        'west': Bs,
        'bottom_ocean': Btms,
        'east': Bs
    })

if project.scenario == 'slide':
    # Boundary conditions for slide scenario
Пример #11
0
    parameter_file.write('\\end{verbatim}\n')
    parameter_file.close()

else:

    domain = None

domain = distribute(domain)

#-------------------------
# Boundary Conditions
#-------------------------

# Create boundary function from timeseries provided in file
function = anuga.file_function(project.boundary_filename,
                               domain,
                               verbose=verbose)

# Create and assign boundary objects
Bts = anuga.Transmissive_momentum_set_stage_boundary(domain, function)
Br = anuga.Reflective_boundary(domain)
domain.set_boundary({'wave': Bts, 'wall': Br})

#-------------------------
# Evolve through time
#-------------------------
import time
t0 = time.time()

for t in domain.evolve(yieldstep=0.05, finaltime=22.5):
    if myid == 0 and verbose: domain.write_time()
Пример #12
0
    #domain.set_quantities_to_be_monitored('stage')

    domain.set_flow_algorithm(alg)

else:
    
    domain = None

domain = distribute(domain)

#-------------------------
# Boundary Conditions
#-------------------------

# Create boundary function from timeseries provided in file
wave_function = anuga.file_function(project.boundary_filename,
                         domain, verbose=verbose)

# Create and assign boundary objects
Bts = anuga.Transmissive_n_momentum_zero_t_momentum_set_stage_boundary(domain, wave_function)
Br = anuga.Reflective_boundary(domain)
domain.set_boundary({'wave': Bts, 'wall': Br})

#-------------------------------------------------------------------------
# Produce a documentation of parameters
#-------------------------------------------------------------------------
from anuga.validation_utilities import save_parameters_tex
save_parameters_tex(domain)


#-------------------------
# Evolve through time
Пример #13
0
## Setup boundary conditions
##-----------------------------------------------------------------------

domain.set_quantity('elevation', topography)
domain.set_quantity('friction', 0.01)  # Constant friction
domain.set_quantity('stage', expression='elevation')  # Dry initial condition

Bi = anuga.Dirichlet_boundary([5.0, 0.0, 0.0])
Br = anuga.Reflective_boundary(domain)  # Solid reflective wall
domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br})

################ Define Fractional Operators ##########################
line0 = [[10.0, 10.0], [30.0, 10.0]]
#line0 = [[29.0, 10.0], [30.0, 10.0]]
poly1 = [[0.0, 10.0], [0.0, 15.0], [5.0, 15.0], [5.0, 10.0]]
Q0 = anuga.file_function('data/test_hydrograph.tms', quantities=['hydrograph'])
Q1 = 5.0

samples = 50

inlet0 = None
inlet1 = None
boyd_box0 = None

inlet0 = Inlet_operator(domain,
                        line0,
                        Q0,
                        logging=True,
                        description='inlet0',
                        verbose=False)
inlet1 = Inlet_operator(domain,
domain.set_quantity('friction', 0.01)         # Constant friction 
domain.set_quantity('stage',
                        expression='elevation')   # Dry initial condition

    
Bi = anuga.Dirichlet_boundary([5.0, 0.0, 0.0])
Br = anuga.Reflective_boundary(domain)              # Solid reflective wall
domain.set_boundary({'left': Br, 'right': Br, 'top': Br, 'bottom': Br})



    ################ Define Fractional Operators ##########################
line0 = [[10.0, 10.0], [30.0, 10.0]]
#line0 = [[29.0, 10.0], [30.0, 10.0]]
poly1 = [[0.0, 10.0], [0.0, 15.0], [5.0, 15.0], [5.0, 10.0]]
Q0 = anuga.file_function('../data/test_hydrograph.tms', quantities=['hydrograph'])
Q1 = 5.0

samples = 50


inlet0 = None
inlet1 = None
boyd_box0 = None

inlet0 = Inlet_operator(domain, line0, Q0, logging=True, description='inlet0', verbose = False)
inlet1 = Inlet_operator(domain, poly1, Q1, logging=True, description='inlet1', verbose = False)

# Enquiry point [ 19.    2.5] is contained in two domains in 4 proc case

boyd_box0 = Boyd_box_operator(domain,