Esempio n. 1
0
    def test_time(self):

        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
        elements = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        domain = Generic_Domain(points, elements)
        domain.check_integrity()

        domain.conserved_quantities = ['stage', 'ymomentum']
        domain.evolved_quantities = ['stage', 'ymomentum']
        domain.quantities['stage'] =\
                                   Quantity(domain, [[1,2,3], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.quantities['ymomentum'] =\
                                   Quantity(domain, [[2,3,4], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.check_integrity()

        #Test time bdry, you need to provide a domain and function
        try:
            T = Time_boundary(domain)
        except:
            pass
        else:
            raise Exception('Should have raised exception')

        #Test time bdry, you need to provide a function
        try:
            T = Time_boundary()
        except:
            pass
        else:
            raise Exception('Should have raised exception')

        def function(t):
            return [1.0, 0.0]

        T = Time_boundary(domain, function)

        from anuga.config import default_boundary_tag
        domain.set_boundary({default_boundary_tag: T})

        #FIXME: should not necessarily be true always.
        #E.g. with None as a boundary object.
        assert len(domain.boundary) == len(domain.boundary_objects)

        q = T.evaluate(0, 2)  #Vol=0, edge=2

        assert num.allclose(q, [1.0, 0.0])
Esempio n. 2
0
    def test_transmissive(self):

        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
        elements = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        domain = Generic_Domain(points, elements)
        domain.check_integrity()

        domain.conserved_quantities = ['stage', 'ymomentum']
        domain.evolved_quantities = ['stage', 'ymomentum']
        domain.quantities['stage'] =\
                                   Quantity(domain, [[1,2,3], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.quantities['ymomentum'] =\
                                   Quantity(domain, [[2,3,4], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.check_integrity()

        #Test transmissve bdry
        try:
            T = Transmissive_boundary()
        except:
            pass
        else:
            raise Exception('Should have raised exception')

        T = Transmissive_boundary(domain)

        from anuga.config import default_boundary_tag
        domain.set_boundary({default_boundary_tag: T})

        #FIXME: should not necessarily be true always.
        #E.g. with None as a boundary object.
        assert len(domain.boundary) == len(domain.boundary_objects)

        q = T.evaluate(0, 2)  #Vol=0, edge=2

        assert num.allclose(q, [1.5, 2.5])

        # Now set the centroid_transmissive_bc flag to true
        domain.set_centroid_transmissive_bc(True)

        q = T.evaluate(0, 2)  #Vol=0, edge=2

        assert num.allclose(q, [2.0, 3.0])  # centroid value
    def test_predicted_boyd_flow(self):
        """test_predicted_boyd_flow
        
        Test that flows predicted by the boyd method are consistent with what what
        is calculated in engineering codes.
        The data was supplied by Petar Milevski
        """

        # FIXME(Ole) this is nowhere near finished
        path = get_pathname_from_package('anuga.culvert_flows')    
        
        length = 12.
        width = 5.

        dx = dy = 0.5           # Resolution: Length of subdivisions on both axes

        points, vertices, boundary = rectangular_cross(int(length/dx),
                                                       int(width/dy),
                                                       len1=length, 
                                                       len2=width)
        domain = anuga.Domain(points, vertices, boundary)   
        
        domain.set_name('test_culvert')                 # Output name
        domain.set_default_order(2)


        #----------------------------------------------------------------------
        # Setup initial conditions
        #----------------------------------------------------------------------

        def topography(x, y):
            # General Slope of Topography
            z=-x/10
            
            return z


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


        Q0 = domain.get_quantity('stage')
        Q1 = Quantity(domain)
        
        # Add depths to stage        
        head_water_depth = 0.169
        tail_water_depth = 0.089
        
        inlet_poly = [[0,0], [6,0], [6,5], [0,5]]
        outlet_poly = [[6,0], [12,0], [12,5], [6,5]]        
        
        Q1.set_values(Polygon_function([(inlet_poly, head_water_depth),
                                        (outlet_poly, tail_water_depth)]))
        
        domain.set_quantity('stage', Q0 + Q1)



        culvert = Culvert_flow(domain,
                               label='Test culvert',
                               description='4 m test culvert',   
                               end_point0=[4.0, 2.5], 
                               end_point1=[8.0, 2.5],
                               width=1.20, 
                               height=0.75,
                               culvert_routine=boyd_generalised_culvert_model,        
                               number_of_barrels=1,
                               verbose=False)
                               
        
        domain.forcing_terms.append(culvert)
        
        # Call
        culvert(domain)
    def test_predicted_boyd_flow(self):
        """test_predicted_boyd_flow
        
        Test that flows predicted by the boyd method are consistent with what what
        is calculated in engineering codes.
        The data was supplied by Petar Milevski
        """

        # FIXME(Ole) this is nowhere near finished
        path = get_pathname_from_package('anuga.culvert_flows')

        length = 12.
        width = 5.

        dx = dy = 0.5  # Resolution: Length of subdivisions on both axes

        points, vertices, boundary = rectangular_cross(int(length / dx),
                                                       int(width / dy),
                                                       len1=length,
                                                       len2=width)
        domain = anuga.Domain(points, vertices, boundary)

        domain.set_name('test_culvert')  # Output name
        domain.set_default_order(2)

        #----------------------------------------------------------------------
        # Setup initial conditions
        #----------------------------------------------------------------------

        def topography(x, y):
            # General Slope of Topography
            z = -x / 10

            return z

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

        Q0 = domain.get_quantity('stage')
        Q1 = Quantity(domain)

        # Add depths to stage
        head_water_depth = 0.169
        tail_water_depth = 0.089

        inlet_poly = [[0, 0], [6, 0], [6, 5], [0, 5]]
        outlet_poly = [[6, 0], [12, 0], [12, 5], [6, 5]]

        Q1.set_values(
            Polygon_function([(inlet_poly, head_water_depth),
                              (outlet_poly, tail_water_depth)]))

        domain.set_quantity('stage', Q0 + Q1)

        culvert = Culvert_flow(domain,
                               label='Test culvert',
                               description='4 m test culvert',
                               end_point0=[4.0, 2.5],
                               end_point1=[8.0, 2.5],
                               width=1.20,
                               height=0.75,
                               culvert_routine=boyd_generalised_culvert_model,
                               number_of_barrels=1,
                               verbose=False)

        domain.forcing_terms.append(culvert)

        # Call
        culvert(domain)
Esempio n. 5
0
    def test_fileboundary_exception(self):
        """Test that boundary object complains if number of
        conserved quantities are wrong
        """

        import time, os
        from math import sin, pi
        from anuga.config import time_format

        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
        elements = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        domain = Generic_Domain(points, elements)
        domain.conserved_quantities = ['stage', 'xmomentum', 'ymomentum']
        domain.evolved_quantities = ['stage', 'xmomentum', 'ymomentum']
        domain.quantities['stage'] =\
                                   Quantity(domain, [[1,2,3], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.quantities['xmomentum'] =\
                                   Quantity(domain, [[2,3,4], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])
        domain.quantities['ymomentum'] =\
                                   Quantity(domain, [[2,3,4], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.check_integrity()

        #Write file (with only two values)
        filename = 'boundarytest' + str(time.time())
        fid = open(filename + '.txt', 'w')
        start = time.mktime(time.strptime('2000', '%Y'))
        dt = 5 * 60  #Five minute intervals
        for i in range(10):
            t = start + i * dt
            t_string = time.strftime(time_format, time.gmtime(t))

            fid.write('%s,%f %f\n' % (t_string, 1.0 * i, sin(i * 2 * pi / 10)))
        fid.close()

        #Convert ASCII file to NetCDF (Which is what we really like!)
        from anuga.file_conversion.file_conversion import timefile2netcdf

        timefile2netcdf(filename + '.txt',
                        quantity_names=['stage', 'xmomentum'])

        try:
            F = File_boundary(filename + '.tms', domain)
        except:
            pass
        else:
            raise Exception('Should have raised an exception')

        os.remove(filename + '.txt')
        os.remove(filename + '.tms')
Esempio n. 6
0
    def NOtest_fileboundary_time_only(self):
        """Test that boundary values can be read from file and interpolated
        This is using the .tms file format
        
        See also test_util for comprenhensive testing of the underlying 
        file_function and also tests in test_datamanager which tests 
        file_function using the sts format
        """
        #FIXME (Ole): This test was disabled 18 August 2008 as no
        # need for this was found. Rather I implemented an Exception
        # to catch possible errors in the model setup

        import time, os
        from math import sin, pi
        from anuga.config import time_format

        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
        elements = [[1, 0, 2], [1, 2, 4], [4, 2, 5], [3, 1, 4]]

        domain = Generic_Domain(points, elements)
        domain.conserved_quantities = ['stage', 'ymomentum']
        domain.quantities['stage'] =\
                                   Quantity(domain, [[1,2,3], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.quantities['ymomentum'] =\
                                   Quantity(domain, [[2,3,4], [5,5,5],
                                                     [0,0,9], [-6, 3, 3]])

        domain.check_integrity()

        #Write file
        filename = 'boundarytest' + str(time.time())
        fid = open(filename + '.txt', 'w')
        start = time.mktime(time.strptime('2000', '%Y'))
        dt = 5 * 60  #Five minute intervals
        for i in range(10):
            t = start + i * dt
            t_string = time.strftime(time_format, time.gmtime(t))

            fid.write('%s,%f %f\n' % (t_string, 1.0 * i, sin(i * 2 * pi / 10)))
        fid.close()

        #Convert ASCII file to NetCDF (Which is what we really like!)

        from anuga.shallow_water.data_manager import timefile2netcdf

        timefile2netcdf(filename, quantity_names=['stage', 'ymomentum'])

        F = File_boundary(filename + '.tms', domain)

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

        #Check that midpoint coordinates at boundary are correctly computed
        assert num.allclose(F.midpoint_coordinates,
                            [[1.0, 0.0], [0.0, 1.0], [3.0, 0.0], [3.0, 1.0],
                             [1.0, 3.0], [0.0, 3.0]])

        #assert allclose(F.midpoint_coordinates[(3,2)], [0.0, 3.0])
        #assert allclose(F.midpoint_coordinates[(3,1)], [1.0, 3.0])
        #assert allclose(F.midpoint_coordinates[(0,2)], [0.0, 1.0])
        #assert allclose(F.midpoint_coordinates[(0,0)], [1.0, 0.0])
        #assert allclose(F.midpoint_coordinates[(2,0)], [3.0, 0.0])
        #assert allclose(F.midpoint_coordinates[(2,1)], [3.0, 1.0])

        #Check time interpolation
        from anuga.config import default_boundary_tag
        domain.set_boundary({default_boundary_tag: F})

        domain.time = 5 * 30 / 2  #A quarter way through first step
        q = F.evaluate()
        assert num.allclose(q, [1.0 / 4, sin(2 * pi / 10) / 4])

        domain.time = 2.5 * 5 * 60  #Half way between steps 2 and 3
        q = F.evaluate()
        assert num.allclose(
            q, [2.5, (sin(2 * 2 * pi / 10) + sin(3 * 2 * pi / 10)) / 2])
    def test_Okada_func(self):
        from os import sep, getenv
        import sys
        from anuga.abstract_2d_finite_volumes.mesh_factory \
        import rectangular_cross

        from anuga.abstract_2d_finite_volumes.quantity import Quantity
        from anuga.utilities.system_tools import get_pathname_from_package
        """
        Pick the test you want to do; T= 0 test a point source,
        T= 1  test single rectangular source, T= 2 test multiple
        rectangular sources
        """
        # Get path where this test is run
        path = get_pathname_from_package('anuga.tsunami_source')

        # Choose what test to proceed
        T = 1

        if T == 0:
            # Fortran output file
            filename = path + sep + 'tests' + sep + 'data' + sep + 'fullokada_SP.txt'

            # Initial condition of earthquake for multiple source
            x0 = 7000.0
            y0 = 10000.0
            length = 0
            width = 0
            strike = 0.0
            depth = 15.0
            slip = 10.0
            dip = 15.0
            rake = 90.0
            ns = 1
            NSMAX = 1
        elif T == 1:
            # Fortran output file
            filename = path + sep + 'tests' + sep + 'data' + sep + 'fullokada_SS.txt'

            # Initial condition of earthquake for multiple source
            x0 = 7000.0
            y0 = 10000.0
            length = 10.0
            width = 6.0
            strike = 0.0
            depth = 15.0
            slip = 10.0
            dip = 15.0
            rake = 90.0
            ns = 1
            NSMAX = 1

        elif T == 2:

            # Fortran output file
            filename = path + sep + 'tests' + sep + 'data' + sep + 'fullokada_MS.txt'

            # Initial condition of earthquake for multiple source
            x0 = [7000.0, 10000.0]
            y0 = [10000.0, 7000.0]
            length = [10.0, 10.0]
            width = [6.0, 6.0]
            strike = [0.0, 0.0]
            depth = [15.0, 15.0]
            slip = [10.0, 10.0]
            dip = [15.0, 15.0]
            rake = [90.0, 90.0]
            ns = 2
            NSMAX = 2

        # Get output file from original okada fortran script.
        # Vertical displacement is listed under tmp.
        polyline_file = open(filename, 'r')
        lines = polyline_file.readlines()
        polyline_file.close()
        tmp = []
        stage = []
        for line in lines[0:]:
            field = line.split('    ')
            z = float(field[2])
            tmp.append(z)

        #create domain
        dx = dy = 4000
        l = 100000
        w = 100000

        #create topography
        def topography(x, y):
            el = -1000
            return el

        #print int(l/dx)
        #print int(w/dy)
        points, vertices, boundary = rectangular_cross(int(old_div(l, dx)),
                                                       int(old_div(w, dy)),
                                                       len1=l,
                                                       len2=w)
        domain = Domain(points, vertices, boundary)
        domain.set_name('test')
        domain.set_quantity('elevation', topography)

        #create variable with elevation data to implement in okada
        zrec0 = Quantity(domain)
        zrec0.set_values(0.0)
        zrec = zrec0.get_vertex_values(xy=True)
        # call okada
        Ts= Okada_func(ns=ns, NSMAX=NSMAX,length=length, width=width, dip=dip, \
                       x0=x0, y0=y0, strike=strike, depth=depth, \
                       slip=slip, rake=rake,zrec=zrec)

        #create a variable to store vertical displacement throughout the domain
        tsunami = Quantity(domain)
        tsunami.set_values(Ts)

        # get vertical displacement at each point of the domain respecting
        # original script's order
        interpolation_points = []
        k = 0.0
        for i in range(0, 6):
            for j in range(0, 6):
                p = j * 4000
                Yt = p
                Xt = k
                interpolation_points.append([Xt, Yt])

            k = k + 4000
        Z = tsunami.get_values(interpolation_points=interpolation_points,
                               location='edges')

        stage = -Z  # FIXME(Ole): Why the sign flip?
        # Displacement in fortran code is looking downward
        #print tmp
        #print 'hello',stage
        assert num.allclose(stage, tmp, atol=1.e-3)
    def test_earthquake_tsunami(self):
        from os import sep, getenv
        import sys
        from anuga.abstract_2d_finite_volumes.mesh_factory \
                        import rectangular_cross
        from anuga.abstract_2d_finite_volumes.quantity import Quantity
        from anuga.utilities.system_tools import get_pathname_from_package
        """
        Pick the test you want to do; T= 0 test a point source,
        T= 1  test single rectangular source, T= 2 test multiple
        rectangular sources
        """

        # Get path where this test is run
        path= get_pathname_from_package('anuga.tsunami_source')
        
        # Choose what test to proceed
        T=1

        if T==0:
            # Fortran output file
            filename = path+sep+'tests'+sep+'data'+sep+'fullokada_SP.txt'
            
            # Initial condition of earthquake for multiple source
            x0 = 7000.0
            y0 = 10000.0
            length = 0
            width =0
            strike = 0.0
            depth = 15.0
            slip = 10.0
            dip =15.0
            rake =90.0
            ns=1
            NSMAX=1
        elif T==1:
            # Fortran output file
            filename = path+sep+'tests'+sep+'data'+sep+'fullokada_SS.txt'
            
            # Initial condition of earthquake for multiple source
            x0 = 7000.0
            y0 = 10000.0
            length = 10.0
            width =6.0
            strike = 0.0
            depth = 15.0
            slip = 10.0
            dip =15.0
            rake =90.0
            ns=1
            NSMAX=1
            
        elif T==2:

            # Fortran output file
            filename = path+sep+'tests'+sep+'data'+sep+'fullokada_MS.txt'
            
            # Initial condition of earthquake for multiple source
            x0 = [7000.0,10000.0]
            y0 = [10000.0,7000.0]
            length = [10.0,10.0]
            width =[6.0,6.0]
            strike = [0.0,0.0]
            depth = [15.0,15.0]
            slip = [10.0,10.0]
            dip = [15.0,15.0]
            rake = [90.0,90.0]
            ns=2
            NSMAX=2



        # Get output file from original okada fortran script.
        # Vertical displacement is listed under tmp.
        polyline_file=open(filename,'r')
        lines=polyline_file.readlines()
        polyline_file.close()
        tmp=[]
        stage=[]
        for line in lines [0:]:
            field = line.split('    ')
            z=float(field[2])
            tmp.append(z)

         
        # Create domain 
        dx = dy = 4000
        l=20000
        w=20000
        
        # Create topography
        def topography(x,y):
            el=-1000
            return el
        
        points, vertices, boundary = rectangular_cross(int(l/dx), int(w/dy),
                                               len1=l, len2=w)
        domain = Domain(points, vertices, boundary)   
        domain.set_name('test')
        domain.set_quantity('elevation',topography)
        Ts = earthquake_tsunami(ns=ns,NSMAX=NSMAX,length=length, width=width, strike=strike,\
                                depth=depth,dip=dip, xi=x0, yi=y0,z0=0, slip=slip, rake=rake,\
                                domain=domain, verbose=False)
        
        # Create a variable to store vertical displacement throughout the domain
        tsunami = Quantity(domain)
        tsunami.set_values(Ts)
        interpolation_points=[]

        #k=0.0
        #for i in range(0,6):
        #    for j in range(0,6):
        #        p=j*4000
        #        Yt=p
        #        Xt=k
        #        Z=tsunami.get_values(interpolation_points=[[Xt,Yt]]
        #                             ,location='edges')
        #        stage.append(-Z[0])
        #    k=k+4000
        #
        #assert allclose(stage,tmp,atol=1.e-3)

        # Here's a faster way - try that in the first test
        interpolation_points=[]
        k=0.0
        for i in range(0,6):
            for j in range(0,6):
                p=j*4000
                Yt=p
                Xt=k
                interpolation_points.append([Xt, Yt])

            k=k+4000
        Z=tsunami.get_values(interpolation_points=interpolation_points,
                             location='edges')

        stage = -Z # FIXME(Ole): Why the sign flip?
                   # Displacement in fortran code is looking downward
        #print 'c est fini'
        #print tmp
        #print 'hello',stage   
        assert num.allclose(stage,tmp,atol=1.e-3)