def test_boundary_timeII(self): """ test_boundary_timeII(self): Test that starttime can be set in the middle of a boundary condition """ boundary_starttime = 0 boundary_filename = self.create_sww_boundary(boundary_starttime) #print "boundary_filename",boundary_filename filename = tempfile.mktemp(".sww") #print "filename",filename dir, base = os.path.split(filename) senario_name = base[:-4] mesh = Mesh() mesh.add_region_from_polygon([[10, 10], [90, 10], [90, 90], [10, 90]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(senario_name) domain.set_datadir(dir) new_starttime = 0. domain.set_starttime(new_starttime) # Setup initial conditions domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 0.0) Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False) # Setup boundary conditions domain.set_boundary({'exterior': Bf}) for t in domain.evolve(yieldstep=5, finaltime=9.0): pass #print domain.boundary_statistics() #domain.write_time() #print "domain.time", domain.time # do an assertion on the time of the produced sww file fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read times = fid.variables['time'][:] stage = fid.variables['stage'][:] #print stage #print "times", times #print "fid.starttime", fid.starttime assert num.allclose(fid.starttime, new_starttime) fid.close() #print "stage[2,0]", stage[2,0] msg = "This test is a bit hand crafted, based on the output file. " msg += "Not logic. " msg += "It's testing that starttime is working" assert num.allclose(stage[2, 0], 4.85825), msg # clean up os.remove(boundary_filename) os.remove(filename)
def test_boundary_timeII_1_5(self): """ test_boundary_timeII(self): Test that starttime can be set in the middle of a boundary condition """ boundary_starttime = 0 boundary_filename = self.create_sww_boundary(boundary_starttime) # print "boundary_filename",boundary_filename filename = tempfile.mktemp(".sww") # print "filename",filename dir, base = os.path.split(filename) senario_name = base[:-4] mesh = Mesh() mesh.add_region_from_polygon([[10, 10], [90, 10], [90, 90], [10, 90]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(senario_name) domain.set_datadir(dir) domain.set_flow_algorithm("1_5") new_starttime = 0.0 domain.set_starttime(new_starttime) # Setup initial conditions domain.set_quantity("elevation", 0.0) domain.set_quantity("stage", 0.0) Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False) # Setup boundary conditions domain.set_boundary({"exterior": Bf}) for t in domain.evolve(yieldstep=5, finaltime=9.0): pass # print domain.boundary_statistics() # domain.write_time() # print "domain.time", domain.time # do an assertion on the time of the produced sww file fid = NetCDFFile(filename, netcdf_mode_r) # Open existing file for read times = fid.variables["time"][:] stage = fid.variables["stage"][:] # print stage # print "times", times # print "fid.starttime", fid.starttime assert num.allclose(fid.starttime, new_starttime) fid.close() # print "stage[2,0]", stage[2,0] msg = "This test is a bit hand crafted, based on the output file. " msg += "Not logic. " msg += "It's testing that starttime is working" assert num.allclose(stage[2, 0], 4.88601), msg # clean up os.remove(boundary_filename) os.remove(filename)
def create_sww_boundary(self, boundary_starttime): """ This creates a boundary file with ; time stage 0 5 10 2.15268 20 13.9773 """ tide = 5 boundary_filename = tempfile.mktemp(".sww") dir, base = os.path.split(boundary_filename) boundary_name = base[:-4] # Setup computational domain mesh = Mesh() mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(boundary_name) domain.set_datadir(dir) domain.set_starttime(boundary_starttime) domain.set_low_froude(0) # Setup initial conditions domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', tide) # Setup boundary conditions Bd = anuga.Dirichlet_boundary([tide, 0., 0.]) # Constant boundary values Bd = anuga.Time_boundary( domain=domain, # Time dependent boundary function=lambda t: [t, 0.0, 0.0]) domain.set_boundary({'exterior': Bd}) for t in domain.evolve(yieldstep=10, finaltime=20.0): pass #print domain.boundary_statistics('stage') q = Bd.evaluate() # FIXME (Ole): This test would not have passed in # changeset:5846. msg = 'Time boundary not evaluated correctly' assert num.allclose(t, q[0]), msg #print domain.get_quantity('stage').get_values() #domain.write_time() #print "domain.time", domain.time return boundary_filename
def test_boundary_time(self): """ test_boundary_time(self): test that the starttime of a boundary condition is carried thru to the output sww file. """ boundary_starttime = 0 boundary_filename = self.create_sww_boundary(boundary_starttime) filename = tempfile.mktemp(".sww") dir, base = os.path.split(filename) senario_name = base[:-4] mesh = Mesh() ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(senario_name) domain.set_datadir(dir) # Setup initial conditions domain.set_quantity('elevation', 0.0) domain.set_quantity('stage', 0.0) Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False) # Setup boundary conditions domain.set_boundary({'exterior': Bf}) for t in domain.evolve(yieldstep=5.0, finaltime=10.0): pass #print domain.write_time() #print "domain.time", domain.time # do an assertion on the time of the produced sww file fid = NetCDFFile(filename, netcdf_mode_r) #Open existing file for read times = fid.variables['time'][:] #print "times", times #print "fid.starttime", fid.starttime assert num.allclose(fid.starttime, boundary_starttime) fid.close() # clean up os.remove(boundary_filename) os.remove(filename)
def test_boundary_time(self): """ test_boundary_time(self): test that the starttime of a boundary condition is carried thru to the output sww file. """ boundary_starttime = 0 boundary_filename = self.create_sww_boundary(boundary_starttime) filename = tempfile.mktemp(".sww") dir, base = os.path.split(filename) senario_name = base[:-4] mesh = Mesh() ###mesh.add_region_from_polygon([[10,10], [90,10], [90,90], [10,90]]) mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(senario_name) domain.set_datadir(dir) # Setup initial conditions domain.set_quantity("elevation", 0.0) domain.set_quantity("stage", 0.0) Bf = anuga.File_boundary(boundary_filename, domain, use_cache=False, verbose=False) # Setup boundary conditions domain.set_boundary({"exterior": Bf}) for t in domain.evolve(yieldstep=5.0, finaltime=10.0): pass # print domain.write_time() # print "domain.time", domain.time # do an assertion on the time of the produced sww file fid = NetCDFFile(filename, netcdf_mode_r) # Open existing file for read times = fid.variables["time"][:] # print "times", times # print "fid.starttime", fid.starttime assert num.allclose(fid.starttime, boundary_starttime) fid.close() # clean up os.remove(boundary_filename) os.remove(filename)
def create_sww_boundary(self, boundary_starttime): """ This creates a boundary file with ; time stage 0 5 10 2.15268 20 13.9773 """ tide = 5 boundary_filename = tempfile.mktemp(".sww") dir, base = os.path.split(boundary_filename) boundary_name = base[:-4] # Setup computational domain mesh = Mesh() mesh.add_region_from_polygon([[0, 0], [100, 0], [100, 100], [0, 100]]) mesh.generate_mesh(verbose=False) domain = pmesh_to_domain_instance(mesh, anuga.Domain) domain.set_name(boundary_name) domain.set_datadir(dir) domain.set_starttime(boundary_starttime) # Setup initial conditions domain.set_quantity("elevation", 0.0) domain.set_quantity("stage", tide) # Setup boundary conditions Bd = anuga.Dirichlet_boundary([tide, 0.0, 0.0]) # Constant boundary values Bd = anuga.Time_boundary(domain=domain, function=lambda t: [t, 0.0, 0.0]) # Time dependent boundary domain.set_boundary({"exterior": Bd}) for t in domain.evolve(yieldstep=10, finaltime=20.0): pass # print domain.boundary_statistics('stage') q = Bd.evaluate() # FIXME (Ole): This test would not have passed in # changeset:5846. msg = "Time boundary not evaluated correctly" assert num.allclose(t, q[0]), msg # print domain.get_quantity('stage').get_values() # domain.write_time() # print "domain.time", domain.time return boundary_filename
def _create_mesh_from_regions(bounding_polygon, boundary_tags, maximum_triangle_area=None, filename=None, interior_regions=None, interior_holes=None, hole_tags=None, poly_geo_reference=None, mesh_geo_reference=None, minimum_triangle_angle=28.0, fail_if_polygons_outside=True, breaklines=None, verbose=True, regionPtArea=None): """_create_mesh_from_regions - internal function. See create_mesh_from_regions for documentation. """ # check the segment indexes - throw an error if they are out of bounds if boundary_tags is not None: max_points = len(bounding_polygon) for key in boundary_tags.keys(): if len([x for x in boundary_tags[key] if x > max_points-1]) >= 1: msg = 'Boundary tag %s has segment out of bounds. '\ %(str(key)) msg += 'Number of points in bounding polygon = %d' % max_points raise SegmentError(msg) for i in range(max_points): found = False for tag in boundary_tags: if i in boundary_tags[tag]: found = True if found is False: msg = 'Segment %d was not assigned a boundary_tag.' % i msg += 'Default tag "exterior" will be assigned to missing segment' #raise Exception(msg) # Fixme: Use proper Python warning if verbose: log.critical('WARNING: %s' % msg) #In addition I reckon the polygons could be of class Geospatial_data #(DSG) If polygons were classes caching would break in places. # Simple check bounding_polygon = ensure_numeric(bounding_polygon, num.float) msg = 'Bounding polygon must be a list of points or an Nx2 array' assert len(bounding_polygon.shape) == 2, msg assert bounding_polygon.shape[1] == 2, msg # if interior_regions is not None: # Test that all the interior polygons are inside the # bounding_poly and throw out those that aren't fully # included. #Note, Both poly's have the same geo_ref, # therefore don't take into account # geo_ref polygons_inside_boundary = [] for interior_polygon, res in interior_regions: indices = inside_polygon(interior_polygon, bounding_polygon, closed = True, verbose = False) if len(indices) <> len(interior_polygon): msg = 'Interior polygon %s is not fully inside'\ %(str(interior_polygon)) msg += ' bounding polygon: %s.' %(str(bounding_polygon)) if fail_if_polygons_outside is True: raise PolygonError(msg) else: msg += ' I will ignore it.' log.critical(msg) else: polygons_inside_boundary.append([interior_polygon, res]) # Record only those that were fully contained interior_regions = polygons_inside_boundary # the following segment of code could be used to Test that all the # interior polygons are inside the bounding_poly... however it might need # to be change a bit # #count = 0 #for i in range(len(interior_regions)): # region = interior_regions[i] # interior_polygon = region[0] # if len(inside_polygon(interior_polygon, bounding_polygon, # closed = True, verbose = False)) <> len(interior_polygon): # print 'WARNING: interior polygon %d is outside bounding polygon' %(i) # count += 1 #if count == 0: # print 'interior regions OK' #else: # print 'check out your interior polygons' # print 'check %s in production directory' %figname # import sys; sys.exit() if interior_holes is not None: # Test that all the interior polygons are inside the bounding_poly for interior_polygon in interior_holes: # Test that we have a polygon if len(num.array(interior_polygon).flat) < 6: msg = 'Interior hole polygon %s has too few (<3) points.\n' \ %(str(interior_polygon)) msg = msg + '(Insure that you have specified a LIST of interior hole polygons)' raise PolygonError(msg) indices = inside_polygon(interior_polygon, bounding_polygon, closed = True, verbose = False) if len(indices) <> len(interior_polygon): msg = 'Interior polygon %s is outside bounding polygon: %s'\ %(str(interior_polygon), str(bounding_polygon)) raise PolygonError(msg) # Resolve geo referencing if mesh_geo_reference is None: xllcorner = min(bounding_polygon[:,0]) yllcorner = min(bounding_polygon[:,1]) # if poly_geo_reference is None: zone = DEFAULT_ZONE else: zone = poly_geo_reference.get_zone() [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \ [(xllcorner,yllcorner)]) # create a geo_ref, based on the llc of the bounding_polygon mesh_geo_reference = Geo_reference(xllcorner = xllcorner, yllcorner = yllcorner, zone = zone) m = Mesh(geo_reference=mesh_geo_reference) # build a list of discrete segments from the breakline polygons if breaklines is not None: points, verts = polylist2points_verts(breaklines) m.add_points_and_segments(points, verts) # Do bounding polygon m.add_region_from_polygon(bounding_polygon, segment_tags=boundary_tags, geo_reference=poly_geo_reference) # Find one point inside region automatically if interior_regions is not None: excluded_polygons = [] for polygon, res in interior_regions: excluded_polygons.append( polygon ) else: excluded_polygons = None # Convert bounding poly to absolute values # this sort of thing can be fixed with the geo_points class if poly_geo_reference is not None: bounding_polygon_absolute = \ poly_geo_reference.get_absolute(bounding_polygon) else: bounding_polygon_absolute = bounding_polygon inner_point = point_in_polygon(bounding_polygon_absolute) inner = m.add_region(inner_point[0], inner_point[1]) inner.setMaxArea(maximum_triangle_area) # Do interior regions # if interior_regions is not None: # for polygon, res in interior_regions: # m.add_region_from_polygon(polygon, # geo_reference=poly_geo_reference) # # convert bounding poly to absolute values # if poly_geo_reference is not None: # polygon_absolute = \ # poly_geo_reference.get_absolute(polygon) # else: # polygon_absolute = polygon # inner_point = point_in_polygon(polygon_absolute) # region = m.add_region(inner_point[0], inner_point[1]) # region.setMaxArea(res) if interior_regions is not None: for polygon, res in interior_regions: m.add_region_from_polygon(polygon, max_triangle_area=res, geo_reference=poly_geo_reference) # Do interior holes if interior_holes is not None: for n, polygon in enumerate(interior_holes): try: tags = hole_tags[n] except: tags = {} m.add_hole_from_polygon(polygon, segment_tags=tags, geo_reference=poly_geo_reference) # 22/04/2014 # Add user-specified point-based regions with max area if(regionPtArea is not None): for i in range(len(regionPtArea)): inner = m.add_region(regionPtArea[i][0], regionPtArea[i][1]) inner.setMaxArea(regionPtArea[i][2]) # NOTE (Ole): This was moved here as it is annoying if mesh is always # stored irrespective of whether the computation # was cached or not. This caused Domain to # recompute as it has meshfile as a dependency # Decide whether to store this mesh or return it if filename is None: return m else: if verbose: log.critical("Generating mesh to file '%s'" % filename) m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle, verbose=verbose) m.export_mesh_file(filename) return m
def _create_mesh_from_regions(bounding_polygon, boundary_tags, maximum_triangle_area=None, filename=None, interior_regions=None, interior_holes=None, hole_tags=None, poly_geo_reference=None, mesh_geo_reference=None, minimum_triangle_angle=28.0, fail_if_polygons_outside=True, breaklines=None, verbose=True, regionPtArea=None): """_create_mesh_from_regions - internal function. See create_mesh_from_regions for documentation. """ # check the segment indexes - throw an error if they are out of bounds if boundary_tags is not None: max_points = len(bounding_polygon) for key in boundary_tags.keys(): if len([x for x in boundary_tags[key] if x > max_points - 1]) >= 1: msg = 'Boundary tag %s has segment out of bounds. '\ %(str(key)) msg += 'Number of points in bounding polygon = %d' % max_points raise SegmentError(msg) for i in range(max_points): found = False for tag in boundary_tags: if i in boundary_tags[tag]: found = True if found is False: msg = 'Segment %d was not assigned a boundary_tag.' % i msg += 'Default tag "exterior" will be assigned to missing segment' #raise Exception(msg) # Fixme: Use proper Python warning if verbose: log.critical('WARNING: %s' % msg) #In addition I reckon the polygons could be of class Geospatial_data #(DSG) If polygons were classes caching would break in places. # Simple check bounding_polygon = ensure_numeric(bounding_polygon, num.float) msg = 'Bounding polygon must be a list of points or an Nx2 array' assert len(bounding_polygon.shape) == 2, msg assert bounding_polygon.shape[1] == 2, msg # if interior_regions is not None: # Test that all the interior polygons are inside the # bounding_poly and throw out those that aren't fully # included. #Note, Both poly's have the same geo_ref, # therefore don't take into account # geo_ref polygons_inside_boundary = [] for interior_polygon, res in interior_regions: indices = inside_polygon(interior_polygon, bounding_polygon, closed=True, verbose=False) if len(indices) <> len(interior_polygon): msg = 'Interior polygon %s is not fully inside'\ %(str(interior_polygon)) msg += ' bounding polygon: %s.' % (str(bounding_polygon)) if fail_if_polygons_outside is True: raise PolygonError(msg) else: msg += ' I will ignore it.' log.critical(msg) else: polygons_inside_boundary.append([interior_polygon, res]) # Record only those that were fully contained interior_regions = polygons_inside_boundary # the following segment of code could be used to Test that all the # interior polygons are inside the bounding_poly... however it might need # to be change a bit # #count = 0 #for i in range(len(interior_regions)): # region = interior_regions[i] # interior_polygon = region[0] # if len(inside_polygon(interior_polygon, bounding_polygon, # closed = True, verbose = False)) <> len(interior_polygon): # print 'WARNING: interior polygon %d is outside bounding polygon' %(i) # count += 1 #if count == 0: # print 'interior regions OK' #else: # print 'check out your interior polygons' # print 'check %s in production directory' %figname # import sys; sys.exit() if interior_holes is not None: # Test that all the interior polygons are inside the bounding_poly for interior_polygon in interior_holes: # Test that we have a polygon if len(num.array(interior_polygon).flat) < 6: msg = 'Interior hole polygon %s has too few (<3) points.\n' \ %(str(interior_polygon)) msg = msg + '(Insure that you have specified a LIST of interior hole polygons)' raise PolygonError(msg) indices = inside_polygon(interior_polygon, bounding_polygon, closed=True, verbose=False) if len(indices) <> len(interior_polygon): msg = 'Interior polygon %s is outside bounding polygon: %s'\ %(str(interior_polygon), str(bounding_polygon)) raise PolygonError(msg) # Resolve geo referencing if mesh_geo_reference is None: xllcorner = min(bounding_polygon[:, 0]) yllcorner = min(bounding_polygon[:, 1]) # if poly_geo_reference is None: zone = DEFAULT_ZONE else: zone = poly_geo_reference.get_zone() [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \ [(xllcorner,yllcorner)]) # create a geo_ref, based on the llc of the bounding_polygon mesh_geo_reference = Geo_reference(xllcorner=xllcorner, yllcorner=yllcorner, zone=zone) m = Mesh(geo_reference=mesh_geo_reference) # build a list of discrete segments from the breakline polygons if breaklines is not None: points, verts = polylist2points_verts(breaklines) m.add_points_and_segments(points, verts) # Do bounding polygon m.add_region_from_polygon(bounding_polygon, segment_tags=boundary_tags, geo_reference=poly_geo_reference) # Find one point inside region automatically if interior_regions is not None: excluded_polygons = [] for polygon, res in interior_regions: excluded_polygons.append(polygon) else: excluded_polygons = None # Convert bounding poly to absolute values # this sort of thing can be fixed with the geo_points class if poly_geo_reference is not None: bounding_polygon_absolute = \ poly_geo_reference.get_absolute(bounding_polygon) else: bounding_polygon_absolute = bounding_polygon inner_point = point_in_polygon(bounding_polygon_absolute) inner = m.add_region(inner_point[0], inner_point[1]) inner.setMaxArea(maximum_triangle_area) # Do interior regions # if interior_regions is not None: # for polygon, res in interior_regions: # m.add_region_from_polygon(polygon, # geo_reference=poly_geo_reference) # # convert bounding poly to absolute values # if poly_geo_reference is not None: # polygon_absolute = \ # poly_geo_reference.get_absolute(polygon) # else: # polygon_absolute = polygon # inner_point = point_in_polygon(polygon_absolute) # region = m.add_region(inner_point[0], inner_point[1]) # region.setMaxArea(res) if interior_regions is not None: for polygon, res in interior_regions: m.add_region_from_polygon(polygon, max_triangle_area=res, geo_reference=poly_geo_reference) # Do interior holes if interior_holes is not None: for n, polygon in enumerate(interior_holes): try: tags = hole_tags[n] except: tags = {} m.add_hole_from_polygon(polygon, segment_tags=tags, geo_reference=poly_geo_reference) # 22/04/2014 # Add user-specified point-based regions with max area if (regionPtArea is not None): for i in range(len(regionPtArea)): inner = m.add_region(regionPtArea[i][0], regionPtArea[i][1]) inner.setMaxArea(regionPtArea[i][2]) # NOTE (Ole): This was moved here as it is annoying if mesh is always # stored irrespective of whether the computation # was cached or not. This caused Domain to # recompute as it has meshfile as a dependency # Decide whether to store this mesh or return it if filename is None: return m else: if verbose: log.critical("Generating mesh to file '%s'" % filename) m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle, verbose=verbose) m.export_mesh_file(filename) return m