def triang_export(path_to_ocean_scale,polygon_nu): """ Parameters: path_to_ocean_scale:File name or path to IHO World Seas shapefile as distributed in marineregions.org by Flanders Marine Institute. No direct download is permitted as per License, hence no direct availability through code. Download the latest shapefile and use as input for this code. polygon_nu: Integer The polygon number as defined in the IHO World Seas Shapefile from 0 to 21. Returns: File of Numpy Arrays that includes an array of the points and an array of the point combinations by index, that creates every triangle mesh. Description: 1. A shapefile with the IHO world seas polygons is read. 2. An individual polygon from the former shapefile is used as basis for the creation of an exterior linering and facets . (order of linereings connection) that serves as input for meshpy.triangle. 3. Meshpy triangle result (points,facets) given as numpy arrays 4. Refinement of meshpy triangles into 4 subdivisions for every triangle taking the former point results. 5.Values exported as numpy files with the number of the polygon as the name. """ gdf=gpd.read_file("{}.shp".format(path_to_ocean_scale)) polygon=gdf.geometry.iloc[polygon_nu] linering=list(polygon.exterior.coords) facets=round_trip_connect(0,len(linering)-1) info=triangle.MeshInfo() info.set_points(linering) info.set_facets(facets) mesh=triangle.build(info,max_volume=0.2,min_angle=30) mesh_points=np.array(mesh.points) mesh_facets=np.array(mesh.facets) sub=triangle.subdivide_facets(4,mesh_points.tolist(),mesh_facets.tolist()) sub_points=np.array(sub[0]) sub_facets=np.array(sub[1]) info.set_points(sub_points) info.set_facets(sub_facets) mesh2=triangle.build(info,max_volume=0.5,min_angle=30) mesh_points2=np.array(mesh2.points) mesh_tris2=np.array(mesh2.elements) np.savez("{}.npz".format(polygon_nu),mesh_points2,mesh_tris2)
def make_rect_mesh(a=(0, 0), b=(1, 1), max_area=None, boundary_tagger=(lambda fvi, el, fn, all_v: []), periodicity=None, subdivisions=None, refine_func=None): """Create an unstructured rectangular mesh. :param a: the lower left hand point of the rectangle :param b: the upper right hand point of the rectangle :param max_area: maximum area of each triangle. :param periodicity: either None, or a tuple of bools specifying whether the mesh is to be periodic in x and y. :param subdivisions: If not *None*, this is a 2-tuple specifying the number of facet subdivisions in X and Y. :param refine_func: A refinement function as taken by :func:`meshpy.triangle.build`. """ import meshpy.triangle as triangle if max_area is not None: if refine_func is not None: raise ValueError("cannot specify both refine_func and max_area") def refine_func(vertices, area): return area > max_area marker2tag = { 1: "minus_x", 2: "minus_y", 3: "plus_x", 4: "plus_y", } points = [a, (b[0], a[1]), b, (a[0], b[1])] facets = list(_round_trip_connect(0, 3)) facet_markers = [2, 3, 4, 1] if subdivisions is not None: points, facets, facet_markers = triangle.subdivide_facets( [subdivisions[0], subdivisions[1], subdivisions[0], subdivisions[1]], points, facets, facet_markers) return finish_2d_rect_mesh(points, facets, facet_markers, marker2tag, refine_func, periodicity, boundary_tagger)
def make_box(a, b, subdivisions=None): """ :param subdivisions: is a tuple of integers specifying the number of subdivisions along each axis. """ a = [float(ai) for ai in a] b = [float(bi) for bi in b] assert len(a) == len(b) dimensions = len(a) if dimensions == 2: # CAUTION: Do not change point or facet order here. # Other code depends on this staying the way it is. points = [ (a[0], a[1]), (b[0], a[1]), (b[0], b[1]), (a[0], b[1]), ] facets = [(0, 1), (1, 2), (2, 3), (3, 0)] facet_markers = [ Marker.MINUS_Y, Marker.PLUS_X, Marker.PLUS_Y, Marker.MINUS_X] elif dimensions == 3: # 7--------6 # /| /| # 4--------5 | z # | | | | ^ # | 3------|-2 | y # |/ |/ |/ # 0--------1 +--->x points = [ (a[0], a[1], a[2]), (b[0], a[1], a[2]), (b[0], b[1], a[2]), (a[0], b[1], a[2]), (a[0], a[1], b[2]), (b[0], a[1], b[2]), (b[0], b[1], b[2]), (a[0], b[1], b[2]), ] facets = [ (0, 1, 2, 3), (0, 1, 5, 4), (1, 2, 6, 5), (7, 6, 2, 3), (7, 3, 0, 4), (4, 5, 6, 7) ] facet_markers = [Marker.MINUS_Z, Marker.MINUS_Y, Marker.PLUS_X, Marker.PLUS_Y, Marker.MINUS_X, Marker.PLUS_Z] else: raise ValueError("unsupported dimension count: %d" % len(a)) if subdivisions is not None: if dimensions != 2: raise NotImplementedError( "subdivision not implemented for any " "dimension count other than 2") from meshpy.triangle import subdivide_facets points, facets, facet_markers = subdivide_facets( [subdivisions[0], subdivisions[1], subdivisions[0], subdivisions[1]], points, facets, facet_markers) return points, facets, None, facet_markers
def make_box(a, b, subdivisions=None): """ :param subdivisions: is a tuple of integers specifying the number of subdivisions along each axis. """ a = [float(ai) for ai in a] b = [float(bi) for bi in b] assert len(a) == len(b) dimensions = len(a) if dimensions == 2: # CAUTION: Do not change point or facet order here. # Other code depends on this staying the way it is. points = [ (a[0], a[1]), (b[0], a[1]), (b[0], b[1]), (a[0], b[1]), ] facets = [(0, 1), (1, 2), (2, 3), (3, 0)] facet_markers = [ Marker.MINUS_Y, Marker.PLUS_X, Marker.PLUS_Y, Marker.MINUS_X ] elif dimensions == 3: # 7--------6 # /| /| # 4--------5 | z # | | | | ^ # | 3------|-2 | y # |/ |/ |/ # 0--------1 +--->x points = [ (a[0], a[1], a[2]), (b[0], a[1], a[2]), (b[0], b[1], a[2]), (a[0], b[1], a[2]), (a[0], a[1], b[2]), (b[0], a[1], b[2]), (b[0], b[1], b[2]), (a[0], b[1], b[2]), ] facets = [(0, 1, 2, 3), (0, 1, 5, 4), (1, 2, 6, 5), (7, 6, 2, 3), (7, 3, 0, 4), (4, 5, 6, 7)] facet_markers = [ Marker.MINUS_Z, Marker.MINUS_Y, Marker.PLUS_X, Marker.PLUS_Y, Marker.MINUS_X, Marker.PLUS_Z ] else: raise ValueError("unsupported dimension count: %d" % len(a)) if subdivisions is not None: if dimensions != 2: raise NotImplementedError("subdivision not implemented for any " "dimension count other than 2") from meshpy.triangle import subdivide_facets points, facets, facet_markers = subdivide_facets([ subdivisions[0], subdivisions[1], subdivisions[0], subdivisions[1] ], points, facets, facet_markers) return points, facets, None, facet_markers
def make_glued_rect_mesh(a=(0,0), b=(1,1), max_area=None, boundary_tagger=(lambda fvi, el, fn, points: []), periodicity=None, subdivisions=None, refine_func=None): """Create two unstructured rectangular meshes, side-by-side. @arg a: the lower left hand point of the base rectangle @arg b: the upper right hand point of the base rectangle @arg max_area: maximum area of each triangle. @arg periodicity: either None, or a tuple of bools specifying whether the mesh is to be periodic in x and y. @arg subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y. @arg refine_func: A refinement function as taken by C{meshpy.triangle.build}. """ import meshpy.triangle as triangle def round_trip_connect(start, end): for i in range(start, end): yield i, i+1 yield end, start if max_area is not None: if refine_func is not None: raise ValueError, "cannot specify both refine_func and max_area" def refine_func(vertices, area): return area > max_area marker2tag = { 1: "minus_x", 2: "minus_y", 3: "plus_x", 4: "plus_y", } w = b[0]-a[0] h = b[1]-a[1] assert w > 0 assert h > 0 # 3----4----5 # | | | # 0----1----2 points = [ a, (a[0]+w,a[1]), (a[0]+2*w,a[1]), (a[0],a[1]+h), (a[0]+w,a[1]+h), (a[0]+2*w,a[1]+h), ] facets = [(0,1),(1,2),(2,5),(5,4),(4,3),(3,0), (1,4)] facet_markers = [2, 2, 3, 4, 4, 1, 0] if subdivisions is not None: points, facets, facet_markers = triangle.subdivide_facets( [subdivisions[0], subdivisions[0], subdivisions[1], subdivisions[0], subdivisions[0], subdivisions[1], subdivisions[1]], points, facets, facet_markers) from hedge.mesh import finish_2d_rect_mesh return finish_2d_rect_mesh(points, facets, facet_markers, marker2tag, refine_func, periodicity, boundary_tagger)
def make_fine_center_rect_mesh(a=(0,0), b=(1,1), boundary_tagger=(lambda fvi, el, fn, points: []), periodicity=None, inner_width=0.1, max_area=None, inner_max_area=0.02, subdivisions=None, inner_subdivisions=None, refine_func=None): """Create an unstructured rectangular mesh. @arg a: the lower left hand point of the rectangle @arg b: the upper right hand point of the rectangle @arg periodicity: either None, or a tuple of bools specifying whether the mesh is to be periodic in x and y. @arg inner_width: total width of the refined inner "channel". @arg max_area: maximum area of each triangle outside the "channel". @arg inner_max_area: maximum area of each triangle inside the "channel". @arg subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y. @arg inner_subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y used on the channel boundary. @arg refine_func: A refinement function as taken by C{meshpy.triangle.build}. """ import meshpy.triangle as triangle def round_trip_connect(start, end): for i in range(start, end): yield i, i+1 yield end, start if max_area is not None: if refine_func is not None: raise ValueError, "cannot specify both refine_func and max_area" def refine_func(vertices, area): from pytools import average centroid_y = average(vertex[1] for vertex in vertices) if a[1]+fine_start_h <= centroid_y <= a[1]+fine_end_h: return area > inner_max_area else: return area > max_area marker2tag = { 1: "minus_x", 2: "minus_y", 3: "plus_x", 4: "plus_y", } w = b[0]-a[0] h = b[1]-a[1] assert w > 0 assert h > 0 assert inner_width > 0 fine_start_h = h*0.5-inner_width*0.5 fine_end_h = h*0.5+inner_width*0.5 # 6-----------7 a[1]+h = b[1] # | coarse | # 4-----------5 a[1]+fine_end_h # | fine | # 2-----------3 a[1]+fine_start_h # | coarse | # 0-----------1 a[1] a = numpy.asarray(a) b = numpy.asarray(b) x = numpy.array([1,0]) y = numpy.array([0,1]) points = [ a, a+w*x, a+fine_start_h*y, a+fine_start_h*y+w*x, a+fine_end_h*y, a+fine_end_h*y+w*x, a+h*y, a+h*y+w*x, ] facets = [ (0,1),(0,2),(1,3), (2,3),(2,4),(3,5), (4,5),(4,6),(5,7), (6,7) ] facet_markers = [ 2,1,3, 0,1,3, 0,1,3, 4 ] if subdivisions is not None: assert inner_subdivisions is not None points, facets, facet_markers = triangle.subdivide_facets([ subdivisions[0], subdivisions[1], subdivisions[1], inner_subdivisions[0], inner_subdivisions[1], inner_subdivisions[1], inner_subdivisions[0], subdivisions[1], subdivisions[1], subdivisions[0]], points, facets, facet_markers) from hedge.mesh import finish_2d_rect_mesh return finish_2d_rect_mesh(points, facets, facet_markers, marker2tag, refine_func, periodicity, boundary_tagger)
def make_fine_center_rect_mesh( a=(0, 0), b=(1, 1), boundary_tagger=(lambda fvi, el, fn, points: []), periodicity=None, inner_width=0.1, max_area=None, inner_max_area=0.02, subdivisions=None, inner_subdivisions=None, refine_func=None): """Create an unstructured rectangular mesh. @arg a: the lower left hand point of the rectangle @arg b: the upper right hand point of the rectangle @arg periodicity: either None, or a tuple of bools specifying whether the mesh is to be periodic in x and y. @arg inner_width: total width of the refined inner "channel". @arg max_area: maximum area of each triangle outside the "channel". @arg inner_max_area: maximum area of each triangle inside the "channel". @arg subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y. @arg inner_subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y used on the channel boundary. @arg refine_func: A refinement function as taken by C{meshpy.triangle.build}. """ import meshpy.triangle as triangle def round_trip_connect(start, end): for i in range(start, end): yield i, i + 1 yield end, start if max_area is not None: if refine_func is not None: raise ValueError, "cannot specify both refine_func and max_area" def refine_func(vertices, area): from pytools import average centroid_y = average(vertex[1] for vertex in vertices) if a[1] + fine_start_h <= centroid_y <= a[1] + fine_end_h: return area > inner_max_area else: return area > max_area marker2tag = { 1: "minus_x", 2: "minus_y", 3: "plus_x", 4: "plus_y", } w = b[0] - a[0] h = b[1] - a[1] assert w > 0 assert h > 0 assert inner_width > 0 fine_start_h = h * 0.5 - inner_width * 0.5 fine_end_h = h * 0.5 + inner_width * 0.5 # 6-----------7 a[1]+h = b[1] # | coarse | # 4-----------5 a[1]+fine_end_h # | fine | # 2-----------3 a[1]+fine_start_h # | coarse | # 0-----------1 a[1] a = numpy.asarray(a) b = numpy.asarray(b) x = numpy.array([1, 0]) y = numpy.array([0, 1]) points = [ a, a + w * x, a + fine_start_h * y, a + fine_start_h * y + w * x, a + fine_end_h * y, a + fine_end_h * y + w * x, a + h * y, a + h * y + w * x, ] facets = [(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (3, 5), (4, 5), (4, 6), (5, 7), (6, 7)] facet_markers = [2, 1, 3, 0, 1, 3, 0, 1, 3, 4] if subdivisions is not None: assert inner_subdivisions is not None points, facets, facet_markers = triangle.subdivide_facets([ subdivisions[0], subdivisions[1], subdivisions[1], inner_subdivisions[0], inner_subdivisions[1], inner_subdivisions[1], inner_subdivisions[0], subdivisions[1], subdivisions[1], subdivisions[0] ], points, facets, facet_markers) from hedge.mesh import finish_2d_rect_mesh return finish_2d_rect_mesh(points, facets, facet_markers, marker2tag, refine_func, periodicity, boundary_tagger)
def make_glued_rect_mesh(a=(0, 0), b=(1, 1), max_area=None, boundary_tagger=(lambda fvi, el, fn, points: []), periodicity=None, subdivisions=None, refine_func=None): """Create two unstructured rectangular meshes, side-by-side. @arg a: the lower left hand point of the base rectangle @arg b: the upper right hand point of the base rectangle @arg max_area: maximum area of each triangle. @arg periodicity: either None, or a tuple of bools specifying whether the mesh is to be periodic in x and y. @arg subdivisions: If not C{None}, this is a 2-tuple specifying the number of facet subdivisions in X and Y. @arg refine_func: A refinement function as taken by C{meshpy.triangle.build}. """ import meshpy.triangle as triangle def round_trip_connect(start, end): for i in range(start, end): yield i, i + 1 yield end, start if max_area is not None: if refine_func is not None: raise ValueError, "cannot specify both refine_func and max_area" def refine_func(vertices, area): return area > max_area marker2tag = { 1: "minus_x", 2: "minus_y", 3: "plus_x", 4: "plus_y", } w = b[0] - a[0] h = b[1] - a[1] assert w > 0 assert h > 0 # 3----4----5 # | | | # 0----1----2 points = [ a, (a[0] + w, a[1]), (a[0] + 2 * w, a[1]), (a[0], a[1] + h), (a[0] + w, a[1] + h), (a[0] + 2 * w, a[1] + h), ] facets = [(0, 1), (1, 2), (2, 5), (5, 4), (4, 3), (3, 0), (1, 4)] facet_markers = [2, 2, 3, 4, 4, 1, 0] if subdivisions is not None: points, facets, facet_markers = triangle.subdivide_facets([ subdivisions[0], subdivisions[0], subdivisions[1], subdivisions[0], subdivisions[0], subdivisions[1], subdivisions[1] ], points, facets, facet_markers) from hedge.mesh import finish_2d_rect_mesh return finish_2d_rect_mesh(points, facets, facet_markers, marker2tag, refine_func, periodicity, boundary_tagger)