def make_cylinder_mesh(radius=0.5, height=1, radial_subdivisions=10, height_subdivisions=1, max_volume=None, periodic=False, boundary_tagger=(lambda fvi, el, fn, all_v: [])): from meshpy.tet import MeshInfo, build from meshpy.geometry import make_cylinder points, facets, facet_holestarts, facet_markers = \ make_cylinder(radius, height, radial_subdivisions, height_subdivisions) assert len(facets) == len(facet_markers) if periodic: return _make_z_periodic_mesh( points, facets, facet_holestarts, facet_markers, height=height, max_volume=max_volume, boundary_tagger=boundary_tagger) else: mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) generated_mesh = build(mesh_info, max_volume=max_volume) from hedge.mesh import make_conformal_mesh_ext from hedge.mesh.element import Tetrahedron vertices = numpy.asarray(generated_mesh.points, dtype=float, order="C") return make_conformal_mesh_ext( vertices, [Tetrahedron(i, el_idx, vertices) for i, el_idx in enumerate(generated_mesh.elements)], boundary_tagger)
def make_inverse_mesh_info(rz, radial_subdiv): # chop off points with zero radius while rz[0][0] == 0: rz.pop(0) while rz[-1][0] == 0: rz.pop(-1) # construct outer cylinder ((min_r, max_r), (min_z, max_z)) = bounding_box(rz) if rz[0][1] < rz[-1][1]: # built in positive z direction rz.extend([ (max_r+2, max_z), (max_r+2, min_z), ]) else: rz.extend([ (max_r+2, min_z), (max_r+2, max_z), ]) from meshpy.tet import MeshInfo from meshpy.geometry import EXT_CLOSED_IN_RZ, \ generate_surface_of_revolution points, facets, facet_holestarts, facet_markers = \ generate_surface_of_revolution(rz, closure=EXT_CLOSED_IN_RZ, radial_subdiv=radial_subdiv) mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) return mesh_info
def make_mesh_info_with_inner_tube(rz, tube_r, radial_subdiv, max_inner_volume=1e-4): # chop off points with zero radius while rz[0][0] == 0: rz.pop(0) while rz[-1][0] == 0: rz.pop(-1) # construct outer cylinder first_z = rz[0][1] last_z = rz[-1][1] rz.insert(0, (tube_r, first_z)) rz.append((tube_r, last_z)) from meshpy.tet import MeshInfo from meshpy.geometry import EXT_OPEN, generate_surface_of_revolution outer_points, outer_facets, outer_facet_holestarts, outer_facet_markers = \ generate_surface_of_revolution(rz, closure=EXT_OPEN, radial_subdiv=radial_subdiv) outer_point_indices = tuple(range(len(outer_points))) inner_points, inner_facets, inner_facet_holestarts, inner_facet_markers = \ generate_surface_of_revolution( [(0,first_z), (tube_r, first_z), (tube_r, last_z), (0, last_z)], point_idx_offset=len(outer_points), radial_subdiv=radial_subdiv, ring_point_indices=[ None, outer_point_indices[:radial_subdiv], outer_point_indices[-radial_subdiv:], None, ] ) points = outer_points + inner_points facets = outer_facets + inner_facets facet_holestarts = outer_facet_holestarts + inner_facet_holestarts facet_markers = outer_facet_markers + inner_facet_markers mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) # set regional max. volume mesh_info.regions.resize(1) mesh_info.regions[0] = [0, 0,(first_z+last_z)/2, 0, max_inner_volume] return mesh_info
def make_mesh_info(rz, radial_subdiv): from meshpy.tet import MeshInfo from meshpy.geometry import EXT_OPEN, generate_surface_of_revolution points, facets, facet_holestarts, facet_markers = \ generate_surface_of_revolution(rz, closure=EXT_OPEN, radial_subdiv=radial_subdiv) mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) return mesh_info
def _make_z_periodic_mesh(points, facets, facet_holestarts, facet_markers, height, max_volume, boundary_tagger): from meshpy.tet import MeshInfo, build from meshpy.geometry import Marker mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) mesh_info.pbc_groups.resize(1) pbcg = mesh_info.pbc_groups[0] pbcg.facet_marker_1 = Marker.MINUS_Z pbcg.facet_marker_2 = Marker.PLUS_Z pbcg.set_transform(translation=[0, 0, height]) def zper_boundary_tagger(fvi, el, fn, all_v): # we only ask about *boundaries* # we should not try to have the user tag # the (periodicity-induced) interior faces face_marker = fvi2fm[frozenset(fvi)] if face_marker == Marker.MINUS_Z: return ["minus_z"] if face_marker == Marker.PLUS_Z: return ["plus_z"] result = boundary_tagger(fvi, el, fn, all_v) if face_marker == Marker.SHELL: result.append("shell") return result generated_mesh = build(mesh_info, max_volume=max_volume) fvi2fm = generated_mesh.face_vertex_indices_to_face_marker from hedge.mesh import make_conformal_mesh_ext from hedge.mesh.element import Tetrahedron vertices = numpy.asarray(generated_mesh.points, dtype=float, order="C") return make_conformal_mesh_ext( vertices, [Tetrahedron(i, el_idx, vertices) for i, el_idx in enumerate(generated_mesh.elements)], zper_boundary_tagger, periodicity=[None, None, ("minus_z", "plus_z")])
def brep2lar(model,cycles,holes): V,FV,EV = model FE = crossRelation(V,FV,EV) mesh_info = MeshInfo() mesh_info.set_points(V) mesh_info.set_facets_ex(cycles) #mesh_info.set_holes(holes) mesh = build(mesh_info,options=Options("pqYY")) W = [v for h,v in enumerate(mesh.points)] CW = [tet for k,tet in enumerate(mesh.elements)] def simplify(fun): def simplify0(simplices): cellDict = defaultdict(tuple) for cell in CAT(AA(fun)(simplices)): cellDict[tuple(sorted(cell))] = tuple(cell) return cellDict.values() return simplify0 FW = sorted(simplify(faces)(CW)) EW = sorted(simplify(edges)(FW)) return W,CW,FW,EW
def make_ball_mesh(r=0.5, subdivisions=10, max_volume=None, boundary_tagger=(lambda fvi, el, fn, all_v: [])): from meshpy.tet import MeshInfo, build from meshpy.geometry import make_ball points, facets, facet_holestarts, facet_markers = \ make_ball(r, subdivisions) mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, facet_markers) generated_mesh = build(mesh_info, max_volume=max_volume) vertices = numpy.asarray(generated_mesh.points, dtype=float, order="C") from hedge.mesh.element import Tetrahedron from hedge.mesh import make_conformal_mesh_ext return make_conformal_mesh_ext( vertices, [Tetrahedron(i, el_idx, vertices) for i, el_idx in enumerate(generated_mesh.elements)], boundary_tagger)
def brep2lar(model, cycles, holes): V, FV, EV = model FE = crossRelation(V, FV, EV) mesh_info = MeshInfo() mesh_info.set_points(V) mesh_info.set_facets_ex(cycles) #mesh_info.set_holes(holes) mesh = build(mesh_info, options=Options("pqYY")) W = [v for h, v in enumerate(mesh.points)] CW = [tet for k, tet in enumerate(mesh.elements)] def simplify(fun): def simplify0(simplices): cellDict = defaultdict(tuple) for cell in CAT(AA(fun)(simplices)): cellDict[tuple(sorted(cell))] = tuple(cell) return cellDict.values() return simplify0 FW = sorted(simplify(faces)(CW)) EW = sorted(simplify(edges)(FW)) return W, CW, FW, EW
x = np.sin(theta) y = np.cos(theta) base.extend([(x, y)]) (points, facets, facet_holestarts, markers) = generate_extrusion(rz_points=rz, base_shape=base) p_array = np.array(points) xs = p_array[:, 0] ys = p_array[:, 1] zs = p_array[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) for f in facets: plt.plot(xs[list(f[0])], ys[list(f[0])], zs[list(f[0])]) plt.show() for i_facet, poly_list in enumerate(facets): print(poly_list) mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, markers) mesh = build(mesh_info) print(mesh.elements) mesh.write_vtk('test.vtk')
def make_extrusion_with_fine_core(rz, inner_r, max_volume_inner=1e-4, max_volume_outer=5e-2, radial_subdiv=20): min_z = min(rz_pt[1] for rz_pt in rz) max_z = max(rz_pt[1] for rz_pt in rz) from meshpy.tet import MeshInfo, build from meshpy.geometry import generate_surface_of_revolution MINUS_Z_MARKER = 1 PLUS_Z_MARKER = 2 inner_points, inner_facets, inner_holes, inner_markers = \ generate_surface_of_revolution( [ (0, min_z), (inner_r, min_z), (inner_r, max_z), (0, max_z), ], ring_markers=[ MINUS_Z_MARKER, 0, PLUS_Z_MARKER ], radial_subdiv=radial_subdiv, ) inner_point_indices = tuple(range(len(inner_points))) outer_points, outer_facets, outer_holes, outer_markers = \ generate_surface_of_revolution( [(inner_r,min_z)] + rz + [(inner_r, max_z)], ring_markers=[MINUS_Z_MARKER] + [0]*(len(rz)-1) + [PLUS_Z_MARKER], point_idx_offset=len(inner_points), radial_subdiv=radial_subdiv, ring_point_indices= [ inner_point_indices[:radial_subdiv] ] + [None]*len(rz) + [inner_point_indices[radial_subdiv:]] ) mesh_info = MeshInfo() mesh_info.set_points(inner_points + outer_points) mesh_info.set_facets_ex( inner_facets + outer_facets, inner_holes + outer_holes, inner_markers + outer_markers, ) # set regional max. volume mesh_info.regions.resize(2) mesh_info.regions[0] = [0, 0, (max_z+min_z)/2, 0, max_volume_inner] mesh_info.regions[1] = [inner_r+(rz[0][0]-inner_r)/2, 0, (max_z+min_z)/2, 0, max_volume_outer] # add periodicity mesh_info.pbc_groups.resize(1) pbcg = mesh_info.pbc_groups[0] pbcg.facet_marker_1 = MINUS_Z_MARKER pbcg.facet_marker_2 = PLUS_Z_MARKER pbcg.set_transform(translation=[0,0,max_z-min_z]) mesh = build(mesh_info, verbose=True, volume_constraints=True) #print "%d elements" % len(mesh.elements) #mesh.write_vtk("gun.vtk") fvi2fm = mesh.face_vertex_indices_to_face_marker def zper_boundary_tagger(fvi, el, fn, points): face_marker = fvi2fm[frozenset(fvi)] if face_marker == MINUS_Z_MARKER: return ["minus_z"] elif face_marker == PLUS_Z_MARKER: return ["plus_z"] else: return ["shell"] vertices = numpy.asarray(mesh.points, dtype=float, order="C") from hedge.mesh import make_conformal_mesh_ext from hedge.mesh.element import Tetrahedron return make_conformal_mesh_ext( vertices, [Tetrahedron(i, el_idx, vertices) for i, el_idx in enumerate(mesh.elements)], zper_boundary_tagger, periodicity=[None, None, ("minus_z", "plus_z")])
def generateConeMesh(plot=True): x0 = 0 y0 = 0 z0 = 1 h = 1 r_bottom = 1.3 r_top = 1.5 r_scale = r_top / r_bottom rz = [(0, z0), (1, z0), (r_scale, z0 + h), (0, z0 + h)] base = [] # Create circle for theta in np.linspace(0, 2 * np.pi, 40): x = r_bottom * np.sin(theta) y = r_bottom * np.cos(theta) base.extend([(x, y)]) (points, facets, facet_holestarts, markers) = generate_extrusion(rz_points=rz, base_shape=base) if plot: p_array = np.array(points) xs = p_array[:, 0] + x0 ys = p_array[:, 1] + y0 zs = p_array[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) for f in facets: plt.plot(xs[list(f[0])], ys[list(f[0])], zs[list(f[0])]) if True: axLim = ax.get_w_lims() MAX = np.max(axLim) for direction in (-1, 1): for point in np.diag(direction * MAX * np.array([1, 1, 1])): ax.plot( [point[0]], [point[1]], [np.abs(point[2])], 'w') x = [0, 0] y = [0, 0] z = [1, 1 + 0.2] plt.plot(x, y, z) plt.show() mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets) mesh = build(mesh_info) # print(mesh.elements) cellList = [] vertexList = [] mupifMesh = Mesh.UnstructuredMesh() for i, p in enumerate(mesh.points): p = (p[0] + x0, p[1] + y0, p[2]) vertexList.extend([Vertex.Vertex(i, i, p)]) for i, t in enumerate(mesh.elements): cellList.extend([Cell.Tetrahedron_3d_lin(mupifMesh, i, i, t)]) mupifMesh.setup(vertexList, cellList) return(mupifMesh)
def generateConeMesh(plot=True): x0 = 0 y0 = 0 z0 = 1 h = 1 r_bottom = 1.3 r_top = 1.5 r_scale = r_top / r_bottom rz = [(0, z0), (1, z0), (r_scale, z0 + h), (0, z0 + h)] base = [] # Create circle for theta in np.linspace(0, 2 * np.pi, 40): x = r_bottom * np.sin(theta) y = r_bottom * np.cos(theta) base.extend([(x, y)]) (points, facets, facet_holestarts, markers) = generate_extrusion(rz_points=rz, base_shape=base) if plot: p_array = np.array(points) xs = p_array[:, 0] + x0 ys = p_array[:, 1] + y0 zs = p_array[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) for f in facets: plt.plot(xs[list(f[0])], ys[list(f[0])], zs[list(f[0])]) if True: axLim = ax.get_w_lims() MAX = np.max(axLim) for direction in (-1, 1): for point in np.diag(direction * MAX * np.array([1, 1, 1])): ax.plot([point[0]], [point[1]], [np.abs(point[2])], 'w') x = [0, 0] y = [0, 0] z = [1, 1 + 0.2] plt.plot(x, y, z) plt.show() mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets) mesh = build(mesh_info) # print(mesh.elements) cellList = [] vertexList = [] mupifMesh = Mesh.UnstructuredMesh() for i, p in enumerate(mesh.points): p = (p[0] + x0, p[1] + y0, p[2]) vertexList.extend([Vertex.Vertex(i, i, p)]) for i, t in enumerate(mesh.elements): cellList.extend([Cell.Tetrahedron_3d_lin(mupifMesh, i, i, t)]) mupifMesh.setup(vertexList, cellList) return (mupifMesh)
base.extend([(x, y)]) (points, facets, facet_holestarts, markers) = generate_extrusion(rz_points=rz, base_shape=base) p_array = np.array(points) xs = p_array[:, 0] ys = p_array[:, 1] zs = p_array[:, 2] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs) for f in facets: plt.plot(xs[list(f[0])], ys[list(f[0])], zs[list(f[0])]) plt.show() for i_facet, poly_list in enumerate(facets): print(poly_list) mesh_info = MeshInfo() mesh_info.set_points(points) mesh_info.set_facets_ex(facets, facet_holestarts, markers) mesh = build(mesh_info) print(mesh.elements) mesh.write_vtk('test.vtk')
def make_extrusion_with_fine_core(rz, inner_r, max_volume_inner=1e-4, max_volume_outer=5e-2, radial_subdiv=20): min_z = min(rz_pt[1] for rz_pt in rz) max_z = max(rz_pt[1] for rz_pt in rz) from meshpy.tet import MeshInfo, build from meshpy.geometry import generate_surface_of_revolution MINUS_Z_MARKER = 1 PLUS_Z_MARKER = 2 inner_points, inner_facets, inner_holes, inner_markers = \ generate_surface_of_revolution( [ (0, min_z), (inner_r, min_z), (inner_r, max_z), (0, max_z), ], ring_markers=[ MINUS_Z_MARKER, 0, PLUS_Z_MARKER ], radial_subdiv=radial_subdiv, ) inner_point_indices = tuple(range(len(inner_points))) outer_points, outer_facets, outer_holes, outer_markers = \ generate_surface_of_revolution( [(inner_r,min_z)] + rz + [(inner_r, max_z)], ring_markers=[MINUS_Z_MARKER] + [0]*(len(rz)-1) + [PLUS_Z_MARKER], point_idx_offset=len(inner_points), radial_subdiv=radial_subdiv, ring_point_indices= [ inner_point_indices[:radial_subdiv] ] + [None]*len(rz) + [inner_point_indices[radial_subdiv:]] ) mesh_info = MeshInfo() mesh_info.set_points(inner_points + outer_points) mesh_info.set_facets_ex( inner_facets + outer_facets, inner_holes + outer_holes, inner_markers + outer_markers, ) # set regional max. volume mesh_info.regions.resize(2) mesh_info.regions[0] = [0, 0, (max_z + min_z) / 2, 0, max_volume_inner] mesh_info.regions[1] = [ inner_r + (rz[0][0] - inner_r) / 2, 0, (max_z + min_z) / 2, 0, max_volume_outer ] # add periodicity mesh_info.pbc_groups.resize(1) pbcg = mesh_info.pbc_groups[0] pbcg.facet_marker_1 = MINUS_Z_MARKER pbcg.facet_marker_2 = PLUS_Z_MARKER pbcg.set_transform(translation=[0, 0, max_z - min_z]) mesh = build(mesh_info, verbose=True, volume_constraints=True) #print "%d elements" % len(mesh.elements) #mesh.write_vtk("gun.vtk") fvi2fm = mesh.face_vertex_indices_to_face_marker def zper_boundary_tagger(fvi, el, fn, points): face_marker = fvi2fm[frozenset(fvi)] if face_marker == MINUS_Z_MARKER: return ["minus_z"] elif face_marker == PLUS_Z_MARKER: return ["plus_z"] else: return ["shell"] vertices = numpy.asarray(mesh.points, dtype=float, order="C") from hedge.mesh import make_conformal_mesh_ext from hedge.mesh.element import Tetrahedron return make_conformal_mesh_ext( vertices, [ Tetrahedron(i, el_idx, vertices) for i, el_idx in enumerate(mesh.elements) ], zper_boundary_tagger, periodicity=[None, None, ("minus_z", "plus_z")])