Пример #1
0
    def test_sww_header(self):
        """Test that constant sww information can be written correctly
        (non smooth)
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww' #Remove??
        self.domain.smooth = False

        sww = SWW_file(self.domain)
        sww.store_connectivity()

        # Check contents
        # Get NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r)  # Open existing file for append

        # Get the variables
        sww_revision = fid.revision_number
        try:
            revision_number = get_revision_number()
        except:
            revision_number = None
            
        assert str(revision_number) == sww_revision
        fid.close()

        #print "sww.filename", sww.filename
        os.remove(sww.filename)
Пример #2
0
def tsh2sww(infilename, sww_file_name=None, verbose=False):
    """
    This converts a mesh file (.tsh/.msh) to an .sww file.
    This is usefull to visualise the mesh.
    
    Note: This currently just writes the output file in the input file dir.
    """
    if verbose == True: log.critical('Creating domain from %s' % infilename)
    domain = pmesh_to_domain_instance(infilename, Domain)
    if verbose == True: log.critical("Number of triangles = %d" % len(domain))

    domain.smooth = True
    domain.format = 'sww'  #Native netcdf visualisation format

    file_path, filename = path.split(infilename)
    filename, ext = path.splitext(filename)

    if not (sww_file_name is None):
        file_path, filename = path.split(sww_file_name)
        filename, ext = path.splitext(filename)
    domain.set_name(filename)

    domain.reduction = mean
    if verbose == True: log.critical("file_path %s" % file_path)
    if file_path == "": file_path = "."
    domain.set_datadir(file_path)

    if verbose == True:
        log.critical(
            "Output written to %s%s%s.%s" %
            (domain.get_datadir(), sep, domain.get_name(), domain.format))

    sww = SWW_file(domain)
    sww.store_connectivity()
    sww.store_timestep('stage')
Пример #3
0
    def test_sww_constant(self):
        """Test that constant sww information can be written correctly
        (non smooth)
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww' #Remove??
        self.domain.smooth = False
        
        sww = SWW_file(self.domain)
        sww.store_connectivity()

        fid = NetCDFFile(sww.filename, netcdf_mode_r)  # Open existing file for append

        # Get the variables
        x = fid.variables['x']
        y = fid.variables['y']
        z = fid.variables['elevation']
        V = fid.variables['volumes']

        assert num.allclose (x[:], self.X.flatten())
        assert num.allclose (y[:], self.Y.flatten())
        assert num.allclose (z[:], self.F.flatten())

        P = len(self.domain)
        for k in range(P):
            assert V[k, 0] == 3*k
            assert V[k, 1] == 3*k+1
            assert V[k, 2] == 3*k+2

        fid.close()
        os.remove(sww.filename)
Пример #4
0
    def test_sww_variable2(self):
        """Test that sww information can be written correctly
        multiple timesteps. Use average as reduction operator
        """

        import time, os

        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True

        self.domain.reduction = mean

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()
        #self.domain.tight_slope_limiters = 1
        self.domain.evolve_to_end(finaltime=0.01)
        sww.store_timestep()

        # Check contents
        # Get NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r)

        # Get the variables
        x = fid.variables['x']
        y = fid.variables['y']
        z = fid.variables['elevation']
        time = fid.variables['time']
        stage = fid.variables['stage']

        #Check values
        Q = self.domain.quantities['stage']
        Q0 = Q.vertex_values[:, 0]
        Q1 = Q.vertex_values[:, 1]
        Q2 = Q.vertex_values[:, 2]

        A = stage[1, :]
        assert num.allclose(A[0],
                            old_div((Q2[0] + Q1[1]), 2),
                            rtol=1.0e-5,
                            atol=1.0e-5)
        assert num.allclose(A[1],
                            old_div((Q0[1] + Q1[3] + Q2[2]), 3),
                            rtol=1.0e-5,
                            atol=1.0e-5)
        assert num.allclose(A[2], Q0[3], rtol=1.0e-5, atol=1.0e-5)
        assert num.allclose(A[3],
                            old_div((Q0[0] + Q1[5] + Q2[4]), 3),
                            rtol=1.0e-5,
                            atol=1.0e-5)

        #Center point
        assert num.allclose(A[4], old_div((Q1[0] + Q2[1] + Q0[2] +\
                                   Q0[5] + Q2[6] + Q1[7]),6), rtol=1.0e-5, atol=1.0e-5)

        fid.close()

        #Cleanup
        os.remove(sww.filename)
Пример #5
0
 def _create_sww(self,stage=10.0, timestep=2.0):
     self.sww = SWW_file(self.domain)
     self.sww.store_connectivity()
     self.sww.store_timestep()
     self.domain.set_quantity('stage', stage) # This is automatically limited
     # so it will not be less than the elevation
     self.domain.set_time(self.domain.get_time()+timestep)
     self.sww.store_timestep()
Пример #6
0
    def test_sww_variable(self):
        """Test that sww information can be written correctly
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True
        self.domain.reduction = mean

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()

        # Check contents
        # Get NetCDF
        fid = NetCDFFile(sww.filename,
                         netcdf_mode_r)  # Open existing file for append

        # Get the variables
        time = fid.variables['time']
        stage = fid.variables['stage']

        Q = self.domain.quantities['stage']
        Q0 = Q.vertex_values[:, 0]
        Q1 = Q.vertex_values[:, 1]
        Q2 = Q.vertex_values[:, 2]

        A = stage[0, :]

        #         print stage[0,:]
        #         print A[0], (Q2[0] + Q1[1])/2
        #         print A[1], (Q0[1] + Q1[3] + Q2[2])/3
        #         print A[2], Q0[3]
        #         print A[3], (Q0[0] + Q1[5] + Q2[4])/3
        assert num.allclose(A[0],
                            old_div((Q2[0] + Q1[1]), 2),
                            rtol=1.0e-5,
                            atol=1.0e-5)

        assert num.allclose(A[1],
                            old_div((Q0[1] + Q1[3] + Q2[2]), 3),
                            rtol=1.0e-5,
                            atol=1.0e-5)
        assert num.allclose(A[2], Q0[3])
        assert num.allclose(A[3],
                            old_div((Q0[0] + Q1[5] + Q2[4]), 3),
                            rtol=1.0e-5,
                            atol=1.0e-5)

        #Center point
        assert num.allclose(A[4], old_div((Q1[0] + Q2[1] + Q0[2] +\
                                   Q0[5] + Q2[6] + Q1[7]),6), rtol=1.0e-5, atol=1.0e-5)

        fid.close()
        os.remove(sww.filename)
Пример #7
0
    def test_sww_range(self):
        """Test that constant sww information can be written correctly
        Use non-smooth to be able to compare to quantity values.
        """

        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.set_store_vertices_uniquely(True)
        
        sww = SWW_file(self.domain)        

        dqs = self.domain.get_quantity('stage')
        dqx = self.domain.get_quantity('xmomentum')
        dqy = self.domain.get_quantity('ymomentum')        
        xmom_min = ymom_min = stage_min = sys.maxint 
        xmom_max = ymom_max = stage_max = -stage_min        
        for t in self.domain.evolve(yieldstep = 1, finaltime = 1):
            wmax = max(dqs.get_values().flatten())
            if wmax > stage_max: stage_max = wmax
            wmin = min(dqs.get_values().flatten())
            if wmin < stage_min: stage_min = wmin            
            
            uhmax = max(dqx.get_values().flatten())
            if uhmax > xmom_max: xmom_max = uhmax
            uhmin = min(dqx.get_values().flatten())
            if uhmin < xmom_min: xmom_min = uhmin                        
            
            vhmax = max(dqy.get_values().flatten())
            if vhmax > ymom_max: ymom_max = vhmax
            vhmin = min(dqy.get_values().flatten())
            if vhmin < ymom_min: ymom_min = vhmin                                    
            
            
            
        # Get NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append

        # Get the variables
        range = fid.variables['stage_range'][:]
        assert num.allclose(range,[stage_min, stage_max])

        range = fid.variables['xmomentum_range'][:]
        #print range
        assert num.allclose(range, [xmom_min, xmom_max])
        
        range = fid.variables['ymomentum_range'][:]
        #print range
        assert num.allclose(range, [ymom_min, ymom_max])        


        
        fid.close()
        os.remove(sww.filename)
Пример #8
0
    def test_sww_minimum_storable_height(self):
        """Test that sww information can be written correctly
        multiple timesteps using a different reduction operator (min)
        """

        import time, os

        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True
        self.domain.reduction = min
        self.domain.minimum_storable_height = 100

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()

        #self.domain.tight_slope_limiters = 1
        self.domain.evolve_to_end(finaltime = 0.01)
        sww.store_timestep()

        #Check contents
        #Get NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r)

        # Get the variables
        x = fid.variables['x']
        y = fid.variables['y']
        z = fid.variables['elevation']
        time = fid.variables['time']
        stage = fid.variables['stage']
        xmomentum = fid.variables['xmomentum']
        ymomentum = fid.variables['ymomentum']        

        #Check values
        Q = self.domain.quantities['stage']
        Q0 = Q.vertex_values[:,0]
        Q1 = Q.vertex_values[:,1]
        Q2 = Q.vertex_values[:,2]

        A = stage[1,:]
        assert num.allclose(stage[1,:], z[:])


        assert num.allclose(xmomentum, 0.0)
        assert num.allclose(ymomentum, 0.0)        
        
        fid.close()

        #Cleanup
        os.remove(sww.filename)
Пример #9
0
    def test_sww_extent(self):
        """Not a test, rather a look at the sww format
        """

        import time, os

        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True
        self.domain.reduction = mean
        self.domain.set_datadir('.')
        #self.domain.tight_slope_limiters = 1

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()
        self.domain.time = 2.

        #Modify stage at second timestep
        stage = self.domain.quantities['stage'].vertex_values
        self.domain.set_quantity('stage', old_div(stage, 2))

        sww.store_timestep()

        file_and_extension_name = self.domain.get_name() + ".sww"
        #print "file_and_extension_name",file_and_extension_name
        [xmin, xmax, ymin, ymax, stagemin, stagemax] = \
               extent_sww(file_and_extension_name )

        assert num.allclose(xmin, 0.0)
        assert num.allclose(xmax, 1.0)
        assert num.allclose(ymin, 0.0)
        assert num.allclose(ymax, 1.0)

        # FIXME (Ole): Revisit these numbers
        #assert num.allclose(stagemin, -0.85), 'stagemin=%.4f' %stagemin
        #assert num.allclose(stagemax, 0.15), 'stagemax=%.4f' %stagemax

        #Cleanup
        os.remove(sww.filename)
Пример #10
0
    def test_sww_constant_smooth(self):
        """Test that constant sww information can be written correctly
        (non smooth)
        """
        self.domain.set_name('datatest' + str(id(self)))
        self.domain.format = 'sww'
        self.domain.smooth = True

        sww = SWW_file(self.domain)
        sww.store_connectivity()

        # Check contents
        # Get NetCDF
        fid = NetCDFFile(sww.filename,
                         netcdf_mode_r)  # Open existing file for append

        # Get the variables
        X = fid.variables['x'][:]
        Y = fid.variables['y'][:]
        Z = fid.variables['elevation'][:]
        V = fid.variables['volumes']

        assert num.allclose([X[0], Y[0]], num.array([0.0, 0.0]))
        assert num.allclose([X[1], Y[1]], num.array([0.0, 0.5]))
        assert num.allclose([X[2], Y[2]], num.array([0.0, 1.0]))
        assert num.allclose([X[4], Y[4]], num.array([0.5, 0.5]))
        assert num.allclose([X[7], Y[7]], num.array([1.0, 0.5]))

        assert Z[4] == -0.5

        assert V[2, 0] == 4
        assert V[2, 1] == 5
        assert V[2, 2] == 1
        assert V[4, 0] == 6
        assert V[4, 1] == 7
        assert V[4, 2] == 3

        fid.close()
        os.remove(sww.filename)
Пример #11
0
    def test_sww2pts_centroids_de0(self):
        """Test that sww information can be converted correctly to pts data at specified coordinates
        - in this case, the centroids.
        """

        import time, os
        from anuga.file.netcdf import NetCDFFile
        # Used for points that lie outside mesh
        NODATA_value = 1758323

        # Setup
        from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular

        # Create shallow water domain
        domain = Domain(*rectangular(2, 2))

        B = Transmissive_boundary(domain)
        domain.set_boundary({'left': B, 'right': B, 'top': B, 'bottom': B})

        domain.set_name('datatest_de0')

        ptsfile = domain.get_name() + '_elevation.pts'
        swwfile = domain.get_name() + '.sww'

        domain.set_datadir('.')
        domain.format = 'sww'
        domain.set_quantity('elevation', lambda x, y: -x - y)

        domain.geo_reference = Geo_reference(56, 308500, 6189000)

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()

        #self.domain.tight_slope_limiters = 1
        domain.evolve_to_end(finaltime=0.01)
        sww.store_timestep()

        # Check contents in NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r)

        # Get the variables
        x = fid.variables['x'][:]
        y = fid.variables['y'][:]
        elevation = fid.variables['elevation'][:]
        time = fid.variables['time'][:]
        stage = fid.variables['stage'][:]

        volumes = fid.variables['volumes'][:]

        # Invoke interpolation for vertex points
        points = num.concatenate((x[:, num.newaxis], y[:, num.newaxis]),
                                 axis=1)
        points = num.ascontiguousarray(points)
        sww2pts(domain.get_name() + '.sww',
                quantity='elevation',
                data_points=points,
                NODATA_value=NODATA_value)
        ref_point_values = elevation
        point_values = Geospatial_data(ptsfile).get_attributes()
        #print 'P', point_values
        #print 'Ref', ref_point_values
        assert num.allclose(point_values, ref_point_values)

        # Invoke interpolation for centroids
        points = domain.get_centroid_coordinates()
        #print points
        sww2pts(domain.get_name() + '.sww',
                quantity='elevation',
                data_points=points,
                NODATA_value=NODATA_value)
        #ref_point_values = [-0.5, -0.5, -1, -1, -1, -1, -1.5, -1.5]   #At centroids

        ref_point_values = [
            -0.77777777, -0.77777777, -0.99999998, -0.99999998, -0.99999998,
            -0.99999998, -1.22222221, -1.22222221
        ]
        point_values = Geospatial_data(ptsfile).get_attributes()
        #print 'P', point_values
        #print 'Ref', ref_point_values
        assert num.allclose(point_values, ref_point_values)

        fid.close()

        #Cleanup
        os.remove(sww.filename)
        os.remove(ptsfile)
Пример #12
0
    def setUp(self):
        #print "****set up****"
        # Create an sww file
        
        # Set up an sww that has a geo ref.
        # have it cover an area in Australia.  'gong maybe
        #Don't have many triangles though!
        
        #Site Name:    GDA-MGA: (UTM with GRS80 ellipsoid) 
        #Zone:   56    
        #Easting:  222908.705  Northing: 6233785.284 
        #Latitude:   -34  0 ' 0.00000 ''  Longitude: 150  0 ' 0.00000 '' 
        #Grid Convergence:  -1  40 ' 43.13 ''  Point Scale: 1.00054660

        #geo-ref
        #Zone:   56    
        #Easting:  220000  Northing: 6230000 


        #have  a big area covered.
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-33,152],[-35,152],[-35,150],[-33,150]]
        spat = Geospatial_data(data_points=points_lat_long,
                               points_are_lats_longs=True)
        points_ab = spat.get_data_points( absolute = True)
        
        geo =  Geo_reference(56,400000,6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)
        
        domain.default_order=2
        #Set some field values
        #domain.set_quantity('stage', 1.0)
        domain.set_quantity('elevation', -0.5)
        domain.set_quantity('friction', 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        ######################
        #Initial condition - with jumps
        bed = domain.quantities['elevation'].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 0.3
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i,:] = bed[i,:] + h
            else:
                stage[i,:] = bed[i,:]

        domain.set_quantity('stage', stage)
        domain.set_quantity('xmomentum', stage*22.0)
        domain.set_quantity('ymomentum', stage*55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain = domain

        C = domain.get_vertex_coordinates()
        self.X = C[:,0:6:2].copy()
        self.Y = C[:,1:6:2].copy()

        self.F = bed
      
        #sww_file = tempfile.mktemp("")
        self.domain.set_name('tid_P0')
        self.domain.format = 'sww'
        self.domain.smooth = True
        self.domain.reduction = mean

        sww = SWW_file(self.domain)
        sww.store_connectivity()
        sww.store_timestep()
        self.domain.time = 2.
        sww.store_timestep()
        self.sww = sww # so it can be deleted
        
        #Create another sww file
        mesh_file = tempfile.mktemp(".tsh")
        points_lat_long = [[-35,152],[-36,152],[-36,150],[-35,150]]
        spat = Geospatial_data(data_points=points_lat_long,
                               points_are_lats_longs=True)
        points_ab = spat.get_data_points( absolute = True)
        
        geo =  Geo_reference(56,400000,6000000)
        spat.set_geo_reference(geo)
        m = Mesh()
        m.add_vertices(spat)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)

        os.remove(mesh_file)
        
        domain.default_order=2
        #Set some field values
        #domain.set_quantity('stage', 1.0)
        domain.set_quantity('elevation', -40)
        domain.set_quantity('friction', 0.03)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        ######################
        #Initial condition - with jumps
        bed = domain.quantities['elevation'].vertex_values
        stage = num.zeros(bed.shape, num.float)

        h = 30.
        for i in range(stage.shape[0]):
            if i % 2 == 0:
                stage[i,:] = bed[i,:] + h
            else:
                stage[i,:] = bed[i,:]

        domain.set_quantity('stage', stage)
        domain.set_quantity('xmomentum', stage*22.0)
        domain.set_quantity('ymomentum', stage*55.0)

        domain.distribute_to_vertices_and_edges()

        self.domain2 = domain

        C = domain.get_vertex_coordinates()
        self.X2 = C[:,0:6:2].copy()
        self.Y2 = C[:,1:6:2].copy()

        self.F2 = bed
      
        #sww_file = tempfile.mktemp("")
        domain.set_name('tid_P1')
        domain.format = 'sww'
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.time = 2.
        sww.store_timestep()
        self.swwII = sww # so it can be deleted

        # print "sww.filename", sww.filename
        #Create a csv file
        self.csv_file = tempfile.mktemp(".csv")
        fd = open(self.csv_file,'wb')
        writer = csv.writer(fd)
        writer.writerow(['LONGITUDE','LATITUDE',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow(['151.5','-34','199770','130000','Metal','Timber',20.])
        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200.])
        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200.])
        fd.close()

        #Create a csv file
        self.csv_fileII = tempfile.mktemp(".csv")
        fd = open(self.csv_fileII,'wb')
        writer = csv.writer(fd)
        writer.writerow(['LONGITUDE','LATITUDE',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow(['151.5','-34','199770','130000','Metal','Timber',20.])
        writer.writerow(['151','-34.5','150000','76000','Metal','Double Brick',200.])
        writer.writerow(['151','-34.25','150000','76000','Metal','Brick Veneer',200.])
        fd.close()
Пример #13
0
    def test_inundation_damage_list(self):

        # create mesh
        mesh_file = tempfile.mktemp(".tsh")    
        points = [[0.0,0.0],[6.0,0.0],[6.0,6.0],[0.0,6.0]]
        m = Mesh()
        m.add_vertices(points)
        m.auto_segment()
        m.generate_mesh(verbose=False)
        m.export_mesh_file(mesh_file)
        
        #Create shallow water domain
        domain = Domain(mesh_file)
        os.remove(mesh_file)
        
        domain.default_order=2

        #Set some field values
        domain.set_quantity('elevation', elevation_function)
        domain.set_quantity('friction', 0.03)
        domain.set_quantity('xmomentum', 22.0)
        domain.set_quantity('ymomentum', 55.0)

        ######################
        # Boundary conditions
        B = Transmissive_boundary(domain)
        domain.set_boundary( {'exterior': B})

        # This call mangles the stage values.
        domain.distribute_to_vertices_and_edges()
        domain.set_quantity('stage', 0.3)

        #sww_file = tempfile.mktemp("")
        domain.set_name('datatest' + str(time.time()))
        domain.format = 'sww'
        domain.smooth = True
        domain.reduction = mean

        sww = SWW_file(domain)
        sww.store_connectivity()
        sww.store_timestep()
        domain.set_quantity('stage', -0.3)
        domain.time = 2.
        sww.store_timestep()
        
        #Create a csv file
        csv_file = tempfile.mktemp(".csv")
        fd = open(csv_file,'wb')
        writer = csv.writer(fd)
        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
        fd.close()
        
        extension = ".csv"
        csv_fileII = tempfile.mktemp(extension)
        fd = open(csv_fileII,'wb')
        writer = csv.writer(fd)
        writer.writerow(['x','y',STR_VALUE_LABEL,CONT_VALUE_LABEL,'ROOF_TYPE',WALL_TYPE_LABEL, SHORE_DIST_LABEL])
        writer.writerow([5.5,0.5,'10','130000','Metal','Timber',20])
        writer.writerow([4.5,1.0,'150','76000','Metal','Double Brick',20])
        writer.writerow([0.1,1.5,'100','76000','Metal','Brick Veneer',300])
        writer.writerow([6.1,1.5,'100','76000','Metal','Brick Veneer',300])
        fd.close()
        
        sww_file = domain.get_name() + "." + domain.format
        #print "sww_file",sww_file
        marker='_gosh'
        inundation_damage(sww_file, [csv_file, csv_fileII],
                          exposure_file_out_marker=marker,
                          verbose=False)

        # Test one file
        csv_handle = Exposure(csv_file[:-4]+marker+extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        #print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]
        #pprint(struct_loss)
        assert num.allclose(struct_loss,[10.0, 150.0, 66.55333347876866, 0.0])       
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        #print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth, [3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3])
       
        # Test another file
        csv_handle = Exposure(csv_fileII[:-4]+marker+extension)
        struct_loss = csv_handle.get_column(EventDamageModel.STRUCT_LOSS_TITLE)
        #print "struct_loss",struct_loss
        struct_loss = [float(x) for x in struct_loss]

        #pprint(struct_loss)
        assert num.allclose(struct_loss, [10.0, 150.0, 66.553333478768664, 0.0])       
        depth = csv_handle.get_column(EventDamageModel.MAX_DEPTH_TITLE)
        #print "depth",depth
        depth = [float(x) for x in depth]
        assert num.allclose(depth,[3.000000011920929, 2.9166666785875957, 2.2666666785875957, -0.3]) 
        os.remove(sww.filename)
        os.remove(csv_file)
        os.remove(csv_fileII)
Пример #14
0
    def test_sww_extrema(self):
        """Test that extrema of quantities can be retrieved at every vertex
        Extrema are updated at every *internal* timestep
        """

        domain = self.domain
        
        domain.set_name('extrema_test' + str(id(self)))
        domain.format = 'sww'
        domain.smooth = True

        assert domain.quantities_to_be_monitored is None
        assert domain.monitor_polygon is None
        assert domain.monitor_time_interval is None        
        
        domain.set_quantities_to_be_monitored(['xmomentum',
                                               'ymomentum',
                                               'stage-elevation'])

        assert domain.monitor_polygon is None
        assert domain.monitor_time_interval is None


        domain.set_quantities_to_be_monitored(['xmomentum',
                                               'ymomentum',
                                               'stage-elevation'],
                                              polygon=domain.get_boundary_polygon(),
                                              time_interval=[0,1])
        
        
        assert len(domain.quantities_to_be_monitored) == 3
        assert domain.quantities_to_be_monitored.has_key('stage-elevation')
        assert domain.quantities_to_be_monitored.has_key('xmomentum')                
        assert domain.quantities_to_be_monitored.has_key('ymomentum')        

        
        #domain.protect_against_isolated_degenerate_timesteps = True
        #domain.tight_slope_limiters = 1
        domain.tight_slope_limiters = 0 # Backwards compatibility
        domain.use_centroid_velocities = 0 # Backwards compatibility (7/5/8)
        
        
        sww = SWW_file(domain)

        for t in domain.evolve(yieldstep = 1, finaltime = 1):
            pass
            #print domain.timestepping_statistics()
            domain.quantity_statistics(precision = '%.8f') # Silent

            
        # Get NetCDF
        fid = NetCDFFile(sww.filename, netcdf_mode_r) # Open existing file for append

        # Get the variables
        extrema = fid.variables['stage-elevation.extrema'][:]
        assert num.allclose(extrema, [0.00, 0.30]) or \
            num.allclose(extrema, [ 0., 0.3222025])

        loc = fid.variables['stage-elevation.min_location'][:]
        assert num.allclose(loc, [0.16666667, 0.33333333])

        loc = fid.variables['stage-elevation.max_location'][:]        
        assert num.allclose(loc, [0.8333333, 0.16666667])        

        time = fid.variables['stage-elevation.max_time'][:]
        assert num.allclose(time, 0.0) or \
            num.allclose(time, 0.35077909)              

        extrema = fid.variables['xmomentum.extrema'][:]
        # Padarn Note: Had to add an extra possibility here (the final one [-0.06062178  0.47518688])
        # to pass this assertion. Cannot see what would be causing this.
        assert num.allclose(extrema,[-0.06062178, 0.47873023]) or \
            num.allclose(extrema, [-0.06062178, 0.47847986]) or \
            num.allclose(extrema, [-0.06062178, 0.47848481]) or \
            num.allclose(extrema, [-0.06062178, 0.47763887]) or \
            num.allclose(extrema, [-0.06062178, 0.46691909])or \
            num.allclose(extrema, [-0.06062178, 0.47503704]) or \
            num.allclose(extrema, [-0.06062178,  0.47518688]) or \
            num.allclose(extrema, [-0.06062178,  0.49014235])


        
        extrema = fid.variables['ymomentum.extrema'][:]
        assert num.allclose(extrema,[0.00, 0.0625786]) or \
            num.allclose(extrema,[0.00, 0.06062178])

        time_interval = fid.variables['extrema.time_interval'][:]
        assert num.allclose(time_interval, [0,1])
        
        polygon = fid.variables['extrema.polygon'][:]        
        assert num.allclose(polygon, domain.get_boundary_polygon())
        
        fid.close()
        #print "sww.filename", sww.filename
        os.remove(sww.filename)