def get_interpolated_quantities_at_polyline_midpoints(filename, quantity_names=None, polyline=None, verbose=False): """Get values for quantities interpolated to polyline midpoints from SWW. filename path to file to read quantity_names quantity names to get polyline representation of desired cross-section may contain multiple sections allowing complex shapes assume UTM coordinates verbose True if this function is to be verbose Returns (segments, i_func) where segments is a list of Triangle_intersection instances and i_func is an instance of Interpolation_function. Note: For 'polyline' assume absolute UTM coordinates. This function is used by get_flow_through_cross_section and get_energy_through_cross_section. """ from anuga.fit_interpolate.interpolate import Interpolation_function # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(filename, quantities=quantity_names, verbose=verbose) mesh, quantities, time = X # Find all intersections and associated triangles. segments = mesh.get_intersecting_segments(polyline, verbose=verbose) # Get midpoints interpolation_points = segment_midpoints(segments) # Interpolate if verbose: log.critical( 'Interpolating - total number of interpolation points = %d' % len(interpolation_points)) I = Interpolation_function(time, quantities, quantity_names=quantity_names, vertex_coordinates=mesh.nodes, triangles=mesh.triangles, interpolation_points=interpolation_points, verbose=verbose) return segments, I
def get_interpolated_quantities_at_polyline_midpoints(filename, quantity_names=None, polyline=None, verbose=False): """Get values for quantities interpolated to polyline midpoints from SWW. filename path to file to read quantity_names quantity names to get polyline representation of desired cross-section may contain multiple sections allowing complex shapes assume UTM coordinates verbose True if this function is to be verbose Returns (segments, i_func) where segments is a list of Triangle_intersection instances and i_func is an instance of Interpolation_function. Note: For 'polyline' assume absolute UTM coordinates. This function is used by get_flow_through_cross_section and get_energy_through_cross_section. """ from anuga.fit_interpolate.interpolate import Interpolation_function # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(filename, quantities=quantity_names, verbose=verbose) mesh, quantities, time = X # Find all intersections and associated triangles. segments = mesh.get_intersecting_segments(polyline, verbose=verbose) # Get midpoints interpolation_points = segment_midpoints(segments) # Interpolate if verbose: log.critical('Interpolating - total number of interpolation points = %d' % len(interpolation_points)) I = Interpolation_function(time, quantities, quantity_names=quantity_names, vertex_coordinates=mesh.nodes, triangles=mesh.triangles, interpolation_points=interpolation_points, verbose=verbose) return segments, I
def test_get_mesh_and_quantities_from_unique_vertices_DE0_sww_file(self): """test_get_mesh_and_quantities_from_unique_vertices_sww_file(self): """ # Generate a test sww file with non trivial georeference import time, os # Setup #from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular # Create basic mesh (100m x 5m) width = 5 length = 50 t_end = 10 points, vertices, boundary = rectangular(10, 1, length, width) # Create shallow water domain domain = Domain(points, vertices, boundary, geo_reference = Geo_reference(56,308500,6189000)) domain.set_name('test_get_mesh_and_quantities_from_unique_vertices_sww_file') swwfile = domain.get_name() + '.sww' domain.set_datadir('.') domain.set_flow_algorithm('DE0') domain.set_store_vertices_uniquely() Br = Reflective_boundary(domain) # Side walls Bd = Dirichlet_boundary([1, 0, 0]) # inflow domain.set_boundary( {'left': Bd, 'right': Bd, 'top': Br, 'bottom': Br}) for t in domain.evolve(yieldstep=1, finaltime = t_end): pass # Read it # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(swwfile, quantities=['elevation', 'stage', 'xmomentum', 'ymomentum'], verbose=False) mesh, quantities, time = X #print quantities #print time dhash = domain.get_nodes()[:,0]*10+domain.get_nodes()[:,1] mhash = mesh.nodes[:,0]*10+mesh.nodes[:,1] #print 'd_nodes',len(dhash) #print 'm_nodes',len(mhash) di = num.argsort(dhash) mi = num.argsort(mhash) minv = num.argsort(mi) dinv = num.argsort(di) #print 'd_tri',len(domain.get_triangles()) #print 'm_tri',len(mesh.triangles) # Check that mesh has been recovered # triangle order should be ok assert num.allclose(mesh.nodes[mi,:],domain.get_nodes()[di,:]) assert num.alltrue(minv[mesh.triangles] == dinv[domain.get_triangles()]) # Check that time has been recovered assert num.allclose(time, range(t_end+1)) z=domain.get_quantity('elevation').get_values(location='vertices').flatten() assert num.allclose(quantities['elevation'], z) for q in ['stage', 'xmomentum', 'ymomentum']: # Get quantity at last timestep q_ref=domain.get_quantity(q).get_values(location='vertices').flatten() #print q,quantities[q] q_sww=quantities[q][-1,:] msg = 'Quantity %s failed to be recovered' %q assert num.allclose(q_ref, q_sww, atol=1.0e-6), msg
def test_get_mesh_and_quantities_from_de0_sww_file(self): """test_get_mesh_and_quantities_from_sww_file(self): """ # Generate a test sww file with non trivial georeference import time, os # Setup #from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular # Create basic mesh (100m x 5m) width = 5 length = 50 t_end = 10 points, vertices, boundary = rectangular(length, width, 50, 5) # Create shallow water domain domain = Domain(points, vertices, boundary, geo_reference = Geo_reference(56,308500,6189000)) domain.set_name('test_get_mesh_and_quantities_from_sww_file') swwfile = domain.get_name() + '.sww' domain.set_datadir('.') domain.set_flow_algorithm('DE0') Br = Reflective_boundary(domain) # Side walls Bd = Dirichlet_boundary([1, 0, 0]) # inflow domain.set_boundary( {'left': Bd, 'right': Bd, 'top': Br, 'bottom': Br}) for t in domain.evolve(yieldstep=1, finaltime = t_end): pass # Read it # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(swwfile, quantities=['elevation', 'stage', 'xmomentum', 'ymomentum'], verbose=False) mesh, quantities, time = X # Check that mesh has been recovered assert num.alltrue(mesh.triangles == domain.get_triangles()) assert num.allclose(mesh.nodes, domain.get_nodes()) # Check that time has been recovered assert num.allclose(time, range(t_end+1)) # Check that quantities have been recovered # (sww files use single precision) z=domain.get_quantity('elevation').get_values(location='unique vertices') assert num.allclose(quantities['elevation'], z) for q in ['stage', 'xmomentum', 'ymomentum']: # Get quantity at last timestep q_ref=domain.get_quantity(q).get_values(location='unique vertices') #print q,quantities[q] q_sww=quantities[q][-1,:] msg = 'Quantity %s failed to be recovered' %q assert num.allclose(q_ref, q_sww, atol=1.0e-2), msg
def test_get_mesh_and_quantities_from_unique_vertices_sww_file(self): """test_get_mesh_and_quantities_from_unique_vertices_sww_file(self): """ # Generate a test sww file with non trivial georeference import time, os # Setup #from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular # Create basic mesh (100m x 5m) width = 5 length = 50 t_end = 10 points, vertices, boundary = rectangular(10, 1, length, width) # Create shallow water domain domain = Domain(points, vertices, boundary, geo_reference=Geo_reference(56, 308500, 6189000)) domain.set_name( 'test_get_mesh_and_quantities_from_unique_vertices_sww_file') swwfile = domain.get_name() + '.sww' domain.set_datadir('.') domain.set_store_vertices_uniquely() Br = Reflective_boundary(domain) # Side walls Bd = Dirichlet_boundary([1, 0, 0]) # inflow domain.set_boundary({'left': Bd, 'right': Bd, 'top': Br, 'bottom': Br}) for t in domain.evolve(yieldstep=1, finaltime=t_end): pass # Read it # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file( swwfile, quantities=['elevation', 'stage', 'xmomentum', 'ymomentum'], verbose=False) mesh, quantities, time = X #print quantities #print time dhash = domain.get_nodes()[:, 0] * 10 + domain.get_nodes()[:, 1] mhash = mesh.nodes[:, 0] * 10 + mesh.nodes[:, 1] #print 'd_nodes',len(dhash) #print 'm_nodes',len(mhash) di = num.argsort(dhash) mi = num.argsort(mhash) minv = num.argsort(mi) dinv = num.argsort(di) #print 'd_tri',len(domain.get_triangles()) #print 'm_tri',len(mesh.triangles) # Check that mesh has been recovered # triangle order should be ok assert num.allclose(mesh.nodes[mi, :], domain.get_nodes()[di, :]) assert num.alltrue( minv[mesh.triangles] == dinv[domain.get_triangles()]) # Check that time has been recovered assert num.allclose(time, range(t_end + 1)) z = domain.get_quantity('elevation').get_values( location='unique vertices') assert num.allclose(quantities['elevation'], z) for q in ['stage', 'xmomentum', 'ymomentum']: # Get quantity at last timestep q_ref = domain.get_quantity(q).get_values( location='unique vertices') #print q,quantities[q] q_sww = quantities[q][-1, :] msg = 'Quantity %s failed to be recovered' % q assert num.allclose(q_ref[di], q_sww[mi], atol=1.0e-6), msg
def test_get_mesh_and_quantities_from_sww_file(self): """test_get_mesh_and_quantities_from_sww_file(self): """ # Generate a test sww file with non trivial georeference import time, os # Setup #from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular # Create basic mesh (100m x 5m) width = 5 length = 50 t_end = 10 points, vertices, boundary = rectangular(length, width, 50, 5) # Create shallow water domain domain = Domain(points, vertices, boundary, geo_reference=Geo_reference(56, 308500, 6189000)) domain.set_name('test_get_mesh_and_quantities_from_sww_file') swwfile = domain.get_name() + '.sww' domain.set_datadir('.') Br = Reflective_boundary(domain) # Side walls Bd = Dirichlet_boundary([1, 0, 0]) # inflow domain.set_boundary({'left': Bd, 'right': Bd, 'top': Br, 'bottom': Br}) for t in domain.evolve(yieldstep=1, finaltime=t_end): pass # Read it # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file( swwfile, quantities=['elevation', 'stage', 'xmomentum', 'ymomentum'], verbose=False) mesh, quantities, time = X # Check that mesh has been recovered assert num.alltrue(mesh.triangles == domain.get_triangles()) assert num.allclose(mesh.nodes, domain.get_nodes()) # Check that time has been recovered assert num.allclose(time, range(t_end + 1)) # Check that quantities have been recovered # (sww files use single precision) z = domain.get_quantity('elevation').get_values( location='unique vertices') assert num.allclose(quantities['elevation'], z) for q in ['stage', 'xmomentum', 'ymomentum']: # Get quantity at last timestep q_ref = domain.get_quantity(q).get_values( location='unique vertices') #print q,quantities[q] q_sww = quantities[q][-1, :] msg = 'Quantity %s failed to be recovered' % q assert num.allclose(q_ref, q_sww, atol=1.0e-6), msg
def get_flow_through_cross_section(filename, polyline, verbose=False): """Obtain flow (m^3/s) perpendicular to specified cross section. filename path to SWW file to read polyline representation of desired cross-section - it may contain multiple sections allowing for complex shapes. Assume absolute UTM coordinates. Format [[x0, y0], [x1, y1], ...] verbose True if this function is to be verbose Return (time, Q) where time is a list of all stored times in SWW file and Q is a hydrograph of total flow across given segments for all stored times. The normal flow is computed for each triangle intersected by the polyline and added up. Multiple segments at different angles are specified the normal flows may partially cancel each other. The typical usage of this function would be to get flow through a channel, and the polyline would then be a cross section perpendicular to the flow. """ print 'In get_flow_through_cross_section' quantity_names =['elevation', 'stage', 'xmomentum', 'ymomentum'] # Get values for quantities at each midpoint of poly line from sww file # X = get_interpolated_quantities_at_polyline_midpoints(filename, # quantity_names=\ # quantity_names, # polyline=polyline, # verbose=verbose) from anuga.fit_interpolate.interpolate import Interpolation_function print 'In get_interpolated_quantities_at_polyline_midpoints' # Get mesh and quantities from sww file X = get_mesh_and_quantities_from_file(filename, quantities=quantity_names, verbose=verbose) mesh, quantities, time = X # Find all intersections and associated triangles. segments = mesh.get_intersecting_segments(polyline, verbose=verbose) # Get midpoints interpolation_points = segment_midpoints(segments) # Interpolate if verbose: print 'Interpolating - total number of interpolation points = %d' % len(interpolation_points) interpolation_function = Interpolation_function(time, quantities, quantity_names=quantity_names, vertex_coordinates=mesh.nodes, triangles=mesh.triangles, interpolation_points=interpolation_points, verbose=verbose) # Get vectors for time and interpolation_points time = interpolation_function.time interpolation_points = interpolation_function.interpolation_points if verbose: print 'Computing hydrograph' # Compute hydrograph Q = [] for t in time: total_flow = 0 for i in range(len(interpolation_points)): elevation, stage, uh, vh = interpolation_function(t, point_id=i) normal = segments[i].normal # Inner product of momentum vector with segment normal [m^2/s] normal_momentum = uh*normal[0] + vh*normal[1] # Flow across this segment [m^3/s] segment_flow = normal_momentum * segments[i].length # Accumulate total_flow += segment_flow # Store flow at this timestep Q.append(total_flow) return time, Q