def main(): mr = HedgeMeshReceiver() import sys from meshpy.gmsh_reader import read_gmsh read_gmsh(mr, sys.argv[1])
def main(): mr = GmshMeshReceiver() import sys from meshpy.gmsh_reader import read_gmsh read_gmsh(mr, sys.argv[1])
def read_gmsh(filename, force_ambient_dim=None): """Read a gmsh mesh file from *filename* and return a :class:`meshmode.mesh.Mesh`. :arg force_ambient_dim: if not None, truncate point coordinates to this many dimensions. """ from meshpy.gmsh_reader import read_gmsh recv = GmshMeshReceiver() read_gmsh(recv, filename, force_dimension=force_ambient_dim) return recv.get_mesh()
def read_gmsh(filename, force_dimension=None, periodicity=None, allow_internal_boundaries=False, tag_mapper=lambda tag: tag, boundary_tagger=None): """ :param force_dimension: if not None, truncate point coordinates to this many dimensions. """ mr = HedgeGmshMeshReceiver(force_dimension, tag_mapper) from meshpy.gmsh_reader import read_gmsh read_gmsh(mr, filename, force_dimension=force_dimension) return mr.build_mesh(periodicity=periodicity, allow_internal_boundaries=allow_internal_boundaries, tag_mapper=tag_mapper, boundary_tagger=boundary_tagger)
def read_gmsh(fname): # create a receiver from the GmshMeshReceiverBase interface rc = reader.GmshMeshReceiverNumPy() # read the mesh from fname file rd = reader.read_gmsh(rc, fname) # points p = np.array(list(map(lambda x: x[0:2], rc.points))) # 3 indicies of verticies t = np.array(list(filter(lambda x: x.shape == (3,), rc.elements))) # 2 boundary edge indicies be = np.array(list(filter(lambda x: x.shape == (2,), rc.elements))) # Dirichlet boundary edge indicies # find indicies for atributes I0 = np.nonzero(np.array(list(rc.element_markers)) == 20); bd0 = np.array(list(rc.elements))[I0[0]] bd = np.zeros((len(bd0),2)); for j in range(len(bd)): bd[j][0] = bd0[j][0]; bd[j][1] = bd0[j][1]; #print list(rc.elements) return (p, t, be, bd)
def build(self, file_name): ### Parse gmsh file ################### mr = GmshMeshReceiverNumPy() read_gmsh(mr, file_name) elem_type_inds = {} # Find the element types for i in range(len(mr.element_types)): e = mr.element_types[i] if e in elem_type_inds: elem_type_inds[e] += [i] else: elem_type_inds[e] = [i] # Split out the ones we care about hex_type = None quad_type = None for t in elem_type_inds.keys(): if isinstance(t, GmshHexahedralElement): hex_type = t if isinstance(t, GmshQuadrilateralElement): quad_type = t if hex_type is None: raise InvalidGmshMesh("No hex elements found.") if quad_type is None: raise InvalidGmshMesh("No quad face elements found.") assert hex_type.order == 1, "Only linear maps currently supported" hex_inds = elem_type_inds[hex_type] hex_inds = np.sort(hex_inds) quad_inds = elem_type_inds[quad_type] quad_inds = np.sort(quad_inds) # Build connectivity maps elem_to_node = np.zeros((len(hex_inds), hex_type.node_count()), dtype=np.int) for i in range(len(hex_inds)): ind = hex_inds[i] elem_to_node[i, :] = mr.elements[ind] bndy_face_to_node = np.zeros((len(quad_inds), quad_type.node_count()), dtype=np.int) bf2n = bndy_face_to_node for i in range(len(quad_inds)): ind = quad_inds[i] bf2n[i, :] = mr.elements[ind] ### Nodes array nodes = np.array(mr.points) ## Remove nodes not linked to elements. # These show up when nodes used to construct the mesh are not # nodes of the generated elements. used_nodes = np.unique(elem_to_node) max_node = np.max(nodes) unused_nodes = set(np.arange(max_node)) - set(used_nodes) unused_nodes = np.sort(list(unused_nodes)) for inode in unused_nodes[::-1]: elem_to_node[elem_to_node > inode] -= 1 bf2n[bf2n > inode] -= 1 nodes = nodes[used_nodes] self.nodes = nodes assert np.all(np.unique(elem_to_node) == np.arange(len(nodes))) vertices = nodes self.vertices = vertices # Switch nodes to lex ordering inds = hex_type.get_lexicographic_gmsh_node_indices() elem_to_node = elem_to_node[:, inds] self.elem_to_node = elem_to_node inds = quad_type.get_lexicographic_gmsh_node_indices() bndy_face_to_node = bf2n[:, inds] self.bndy_face_to_node = bndy_face_to_node elem_to_vertex = elem_to_node self.elem_to_vertex = elem_to_vertex boundary_vertices = np.unique(bndy_face_to_node.ravel()) self.boundary_vertices = boundary_vertices ### Build connectivity arrays ############################# geom = HexElement self.geom = geom # Build set of edges and edge maps elem_to_edge = np.zeros((len(elem_to_vertex), geom.n_edges), dtype=np.int) elem_to_edge_dir = np.zeros((len(elem_to_vertex), geom.n_edges), dtype=np.int) edge_id = {} eid = 0 for ielem in range(len(elem_to_vertex)): etv = elem_to_vertex[ielem] elem_edges = etv[geom.edge_to_vertex] for iedge in range(geom.n_edges): edge = elem_edges[iedge] # Edge direction elem_to_edge_dir[ielem, iedge] = 1 if edge[0] < edge[1] else -1 edge.sort() t = tuple(edge) if not t in edge_id: edge_id[t] = eid eid += 1 elem_to_edge[ielem, iedge] = edge_id[t] assert len(edge_id) == eid edge_to_vertex = np.zeros((len(edge_id), geom.n_vertex_per_edge), dtype=np.int) for k, v in edge_id.iteritems(): edge_to_vertex[v, :] = k assert np.all(edge_to_vertex[:, 0] < edge_to_vertex[:, 1]) self.elem_to_edge = elem_to_edge self.elem_to_edge_dir = elem_to_edge_dir self.edge_to_vertex = edge_to_vertex self.edge_id = edge_id # Build set of faces and face maps elem_to_face = np.zeros((len(elem_to_vertex), geom.n_faces), dtype=np.int) elem_to_face_dir = np.zeros((len(elem_to_vertex), geom.n_faces), dtype=np.int) face_id = {} fid = 0 for ielem in range(len(elem_to_vertex)): etv = elem_to_vertex[ielem] elem_faces = etv[geom.face_to_vertex] for iface in range(geom.n_faces): face = elem_faces[iface] # How far to rotate the face elem_to_face_dir[ielem, iface] = np.argmin(face) face.sort() t = tuple(face) if not t in face_id: face_id[t] = fid fid += 1 elem_to_face[ielem, iface] = face_id[t] assert len(face_id) == fid face_to_vertex = np.zeros((len(face_id), geom.n_vertex_per_face), dtype=np.int) for k, v in face_id.iteritems(): face_to_vertex[v, :] = k assert np.all(face_to_vertex[:, 0] < face_to_vertex[:, 1]) assert np.all(face_to_vertex[:, 1] < face_to_vertex[:, 2]) assert np.all(face_to_vertex[:, 2] < face_to_vertex[:, 3]) self.elem_to_face = elem_to_face self.elem_to_face_dir = elem_to_face_dir self.face_to_vertex = face_to_vertex self.face_id = face_id # Add component counts self.n_elems = len(elem_to_vertex) self.n_vertices = len(vertices) self.n_edges = len(edge_id) self.n_faces = len(face_id) # Find edges on the boundary boundary_edges = [] for iedge in range(self.n_edges): edge = self.edge_to_vertex[iedge] if (edge[0] in boundary_vertices) and\ (edge[1] in boundary_vertices): boundary_edges.append(iedge) boundary_edges = np.array(boundary_edges, dtype=np.int) self.boundary_edges = boundary_edges # Find faces on the boundary boundary_faces = [] for iface in range(self.n_faces): face = self.face_to_vertex[iface] if (face[0] in boundary_vertices) and\ (face[1] in boundary_vertices) and\ (face[2] in boundary_vertices) and\ (face[3] in boundary_vertices): boundary_faces.append(iface) boundary_faces = np.array(boundary_faces, dtype=np.int) self.boundary_faces = boundary_faces