def test_create(cell_type): comm = MPI.COMM_WORLD mesh = create_unit_cube(comm, 6, 6, 6, cell_type) marked_lines = locate_entities(mesh, 1, lambda x: np.isclose(x[1], 0.5)) f_v = mesh.topology.connectivity(1, 0).array.reshape(-1, 2) entities = create_adjacencylist(f_v[marked_lines]) values = np.full(marked_lines.shape[0], 2, dtype=np.int32) mt = create_meshtags(mesh, 1, entities, values) assert mt.indices.shape == marked_lines.shape
def test_create(cell_type): comm = MPI.COMM_WORLD mesh = UnitCubeMesh(comm, 6, 6, 6, cell_type) mesh.topology.create_connectivity_all() marked_lines = locate_entities(mesh, 1, lambda x: numpy.isclose(x[1], 0.5)) f_v = mesh.topology.connectivity(1, 0).array().reshape(-1, 2) entities = cpp.graph.AdjacencyList_int32(f_v[marked_lines]) values = numpy.full(marked_lines.shape[0], 2, dtype=numpy.int32) mt = create_meshtags(mesh, 1, entities, values) assert mt.indices.shape == marked_lines.shape
marked_facets = topologies[gmsh_facet_id]["topology"] facet_values = topologies[gmsh_facet_id]["cell_data"] else: gmsh_cell_id = MPI.COMM_WORLD.bcast(None, root=0) num_nodes = MPI.COMM_WORLD.bcast(None, root=0) cells, x = np.empty([0, num_nodes]), np.empty([0, 3]) marked_facets, facet_values = np.empty((0, 3)), np.empty((0, )) mesh = create_mesh(MPI.COMM_WORLD, cells, x, ufl_mesh_from_gmsh(gmsh_cell_id, 3)) mesh.name = "ball_d1" local_entities, local_values = extract_local_entities(mesh, 2, marked_facets, facet_values) mesh.topology.create_connectivity(2, 0) mt = create_meshtags(mesh, 2, cpp.graph.AdjacencyList_int32(local_entities), np.int32(local_values)) mt.name = "ball_d1_surface" with XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "w") as file: file.write_mesh(mesh) mesh.topology.create_connectivity(2, 3) file.write_meshtags( mt, geometry_xpath="/Xdmf/Domain/Grid[@Name='ball_d1']/Geometry") # Create a distributed (parallel) mesh with quadratic geometry. # Generate mesh on rank 0, then build a distributed mesh. :: if MPI.COMM_WORLD.rank == 0: # Using model.setCurrent(model_name) lets you change between models model.setCurrent("Sphere minus box")
def gmsh_model_to_mesh(model, cell_data=False, facet_data=False, gdim=None, exportMesh=False, fileName="mesh.msh"): """ Given a GMSH model, create a DOLFIN-X mesh and MeshTags. Can theoretically export to msh file model: The GMSH model cell_data: Boolean, True of a mesh tag for cell data should be returned (Default: False) facet_data: Boolean, True if a mesh tag for facet data should be returned (Default: False) gdim: Geometrical dimension of problem (Default: 3) """ if gdim is None: gdim = 3 if MPI.COMM_WORLD.rank == 0: # Get mesh geometry x = extract_gmsh_geometry(model) # Get mesh topology for each element topologies = extract_gmsh_topology_and_markers(model) # Get information about each cell type from the msh files num_cell_types = len(topologies.keys()) cell_information = {} cell_dimensions = numpy.zeros(num_cell_types, dtype=numpy.int32) for i, element in enumerate(topologies.keys()): properties = model.mesh.getElementProperties(element) name, dim, order, num_nodes, local_coords, _ = properties cell_information[i] = { "id": element, "dim": dim, "num_nodes": num_nodes } cell_dimensions[i] = dim # Sort elements by ascending dimension perm_sort = numpy.argsort(cell_dimensions) # Broadcast cell type data and geometric dimension cell_id = cell_information[perm_sort[-1]]["id"] tdim = cell_information[perm_sort[-1]]["dim"] num_nodes = cell_information[perm_sort[-1]]["num_nodes"] cell_id, num_nodes = MPI.COMM_WORLD.bcast([cell_id, num_nodes], root=0) # Check for facet data and broadcast if found if facet_data: if tdim - 1 in cell_dimensions: num_facet_nodes = MPI.COMM_WORLD.bcast( cell_information[perm_sort[-2]]["num_nodes"], root=0) gmsh_facet_id = cell_information[perm_sort[-2]]["id"] marked_facets = numpy.asarray( topologies[gmsh_facet_id]["topology"], dtype=numpy.int64) facet_values = numpy.asarray( topologies[gmsh_facet_id]["cell_data"], dtype=numpy.int32) else: raise ValueError("No facet data found in file.") cells = numpy.asarray(topologies[cell_id]["topology"], dtype=numpy.int64) cell_values = numpy.asarray(topologies[cell_id]["cell_data"], dtype=numpy.int32) else: cell_id, num_nodes = MPI.COMM_WORLD.bcast([None, None], root=0) cells, x = numpy.empty([0, num_nodes], dtype=numpy.int32), numpy.empty([0, gdim]) cell_values = numpy.empty((0, ), dtype=numpy.int32) if facet_data: num_facet_nodes = MPI.COMM_WORLD.bcast(None, root=0) marked_facets = numpy.empty((0, num_facet_nodes), dtype=numpy.int32) facet_values = numpy.empty((0, ), dtype=numpy.int32) # Create distributed mesh ufl_domain = ufl_mesh_from_gmsh(cell_id, gdim) gmsh_cell_perm = perm_gmsh(to_type(str(ufl_domain.ufl_cell())), num_nodes) cells = cells[:, gmsh_cell_perm] mesh = create_mesh(MPI.COMM_WORLD, cells, x[:, :gdim], ufl_domain) # Create MeshTags for cells if cell_data: local_entities, local_values = distribute_entity_data( mesh, mesh.topology.dim, cells, cell_values) mesh.topology.create_connectivity(mesh.topology.dim, 0) adj = AdjacencyList_int32(local_entities) ct = create_meshtags(mesh, mesh.topology.dim, adj, numpy.int32(local_values)) ct.name = "cells" # Create MeshTags for facets if facet_data: # Permute facets from MSH to Dolfin-X ordering # FIXME: This does not work for prism meshes facet_type = cell_entity_type(to_type(str(ufl_domain.ufl_cell())), mesh.topology.dim - 1, 0) gmsh_facet_perm = perm_gmsh(facet_type, num_facet_nodes) marked_facets = marked_facets[:, gmsh_facet_perm] local_entities, local_values = distribute_entity_data( mesh, mesh.topology.dim - 1, marked_facets, facet_values) mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim) adj = AdjacencyList_int32(local_entities) ft = create_meshtags(mesh, mesh.topology.dim - 1, adj, numpy.int32(local_values)) ft.name = "facets" if exportMesh: gmsh.write(fileName) if cell_data and facet_data: return mesh, ct, ft elif cell_data and not facet_data: return mesh, ct elif not cell_data and facet_data: return mesh, ft else: return mesh
def gmsh_to_dolfin(gmsh_model, tdim: int, comm=MPI.COMM_WORLD, prune_y=False, prune_z=False): """Converts a gmsh model object into `dolfinx.Mesh` and `dolfinx.MeshTags` for physical tags. Parameters ---------- gmsh_model tdim Topological dimension of the mesh comm: optional ghost_mode: optional prune_y: optional Prune y-components. Used to embed a flat geometries into lower dimension. prune_z: optional Prune z-components. Used to embed a flat geometries into lower dimension. Note ---- User must call `geo.synchronize()` and `mesh.generate()` before passing the model into this method. """ rank = comm.rank logger = logging.getLogger("dolfiny") if rank == 0: # Map from internal gmsh cell type number to gmsh cell name # see https://gitlab.onelab.info/gmsh/gmsh/blob/master/Common/GmshDefines.h#L75 gmsh_cellname = { 1: 'line', 2: 'triangle', 3: "quad", 4: 'tetra', 5: "hexahedron", 8: 'line3', 9: 'triangle6', 10: "quad9", 11: 'tetra10', 12: 'hexahedron27', 15: 'vertex', 21: 'triangle10', 26: 'line4', 29: 'tetra20', 36: 'quad16', # 92: 'hexahedron64', } gmsh_dolfin = { "vertex": (CellType.point, 0), "line": (CellType.interval, 1), "line3": (CellType.interval, 2), "line4": (CellType.interval, 3), "triangle": (CellType.triangle, 1), "triangle6": (CellType.triangle, 2), "triangle10": (CellType.triangle, 3), "quad": (CellType.quadrilateral, 1), "quad9": (CellType.quadrilateral, 2), "quad16": (CellType.quadrilateral, 3), "tetra": (CellType.tetrahedron, 1), "tetra10": (CellType.tetrahedron, 2), "tetra20": (CellType.tetrahedron, 3), "hexahedron": (CellType.hexahedron, 1), "hexahedron27": (CellType.hexahedron, 2), # "hexahedron64": (CellType.hexahedron, 3), } # Number of nodes for gmsh cell type nodes = { 'vertex': 1, 'line': 2, 'line3': 3, 'line4': 4, 'triangle': 3, 'triangle6': 6, 'triangle10': 10, 'tetra': 4, 'tetra10': 10, 'tetra20': 20, 'quad': 4, 'quad9': 9, 'quad16': 16, 'hexahedron': 8, 'hexahedron27': 27, # 'hexahedron64': 64, } # node_tags, coord, param_coords = gmsh_model.mesh.getNodes() # FIXME: This silences the RuntimeWarning (ctypes / PEP3118 format string) caused by Gmsh/numpy import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore", RuntimeWarning) node_tags, coord, param_coords = gmsh_model.mesh.getNodes() # Fetch elements for the mesh cell_types, cell_tags, cell_node_tags = gmsh_model.mesh.getElements( dim=tdim) unused_nodes = numpy.setdiff1d(node_tags, cell_node_tags) unused_nodes_indices = [] # FIXME: This would be expensive for many unused nodes case for unused_node in unused_nodes: unused_nodes_indices.append( numpy.where(node_tags == unused_node)[0]) unused_nodes_indices = numpy.asarray(unused_nodes_indices) # Every node has 3 components in gmsh dim = 3 points = numpy.reshape(coord, (-1, dim)) # Delete unreferenced nodes points = numpy.delete(points, unused_nodes_indices, axis=0) node_tags = numpy.delete(node_tags, unused_nodes_indices) # Prepare a map from node tag to index in coords array nmap = numpy.argsort(node_tags - 1) cells = {} if len(cell_types) > 1: raise RuntimeError("Mixed topology meshes not supported.") try: cellname = gmsh_cellname[cell_types[0]] except KeyError: raise RuntimeError( f"Gmsh cell code {cell_types[0]:d} not supported.") try: num_nodes = nodes[cellname] except KeyError: raise RuntimeError( f"Cannot determine number of nodes for Gmsh cell type \"{cellname:s}\"." ) logger.info(f"Processing mesh of gmsh cell name \"{cellname:s}\"") # Shift 1-based numbering and apply node map cells[cellname] = nmap[cell_node_tags[0] - 1] cells[cellname] = numpy.reshape(cells[cellname], (-1, num_nodes)) if prune_z: if not numpy.allclose(points[:, 2], 0.0): raise RuntimeError("Non-zero z-component would be pruned.") points = points[:, :-1] if prune_y: if not numpy.allclose(points[:, 1], 0.0): raise RuntimeError("Non-zero y-component would be pruned.") if prune_z: # In the case we already pruned z-component points = points[:, 0] else: points = points[:, [0, 2]] try: dolfin_cell_type, order = gmsh_dolfin[cellname] except KeyError: raise RuntimeError( f"Cannot determine dolfin cell type for Gmsh cell type \"{cellname:s}\"." ) perm = cpp.io.perm_gmsh(dolfin_cell_type, num_nodes) logger.info(f"Mesh will be permuted with {perm}") cells = cells[cellname][:, perm] logger.info( f"Constructing mesh for tdim: {tdim:d}, gdim: {points.shape[1]:d}") logger.info(f"Number of elements: {cells.shape[0]:d}") cells_shape, pts_shape, cellname = comm.bcast( [cells.shape, points.shape, cellname], root=0) else: cells_shape, pts_shape, cellname = comm.bcast([None, None, None], root=0) cells = numpy.empty((0, cells_shape[1])) points = numpy.empty((0, pts_shape[1])) mesh = create_mesh(comm, cells, points, ufl_mesh_from_gmsh(cellname, pts_shape[1])) mts = {} # Get physical groups (dimension, tag) pgdim_pgtags = comm.bcast( gmsh_model.getPhysicalGroups() if rank == 0 else None, root=0) for pgdim, pgtag in pgdim_pgtags: # For the current physical tag there could be multiple entities # e.g. user tagged bottom and up boundary part with one physical tag entity_tags = comm.bcast(gmsh_model.getEntitiesForPhysicalGroup( pgdim, pgtag) if rank == 0 else None, root=0) pg_tag_name = comm.bcast( gmsh_model.getPhysicalName(pgdim, pgtag) if rank == 0 else None, root=0) if pg_tag_name == "": pg_tag_name = f"tag_{pgtag:d}" if rank == 0: _mt_cells = [] _mt_values = [] for i, entity_tag in enumerate(entity_tags): pgcell_types, pgcell_tags, pgnode_tags = gmsh_model.mesh.getElements( pgdim, entity_tag) assert (len(pgcell_types) == 1) pgcellname = gmsh_cellname[pgcell_types[0]] pgnum_nodes = nodes[pgcellname] # Shift 1-based numbering and apply node map pgnode_tags[0] = nmap[pgnode_tags[0] - 1] _mt_cells.append(pgnode_tags[0].reshape(-1, pgnum_nodes)) _mt_values.append( numpy.full(_mt_cells[-1].shape[0], pgtag, dtype=numpy.int32)) # Stack all topology and value data. This prepares data # for one MVC per (dim, physical tag) instead of multiple MVCs _mt_values = numpy.hstack(_mt_values) _mt_cells = numpy.vstack(_mt_cells) # Fetch the permutation needed for physical group pgdolfin_cell_type, pgorder = gmsh_dolfin[pgcellname] pgpermutation = cpp.io.perm_gmsh(pgdolfin_cell_type, pgnum_nodes) _mt_cells[:, :] = _mt_cells[:, pgpermutation] logger.info(f"Constructing MVC for tdim: {pgdim:d}") logger.info(f"Number of data values: {_mt_values.shape[0]:d}") mt_cells_shape, pgdim = comm.bcast([_mt_cells.shape, pgdim], root=0) else: mt_cells_shape, pgdim = comm.bcast([None, None], root=0) _mt_cells = numpy.empty((0, mt_cells_shape[1])) _mt_values = numpy.empty((0, )) local_entities, local_values = extract_local_entities( mesh, pgdim, _mt_cells, _mt_values) mesh.topology.create_connectivity(pgdim, 0) mt = create_meshtags(mesh, pgdim, cpp.graph.AdjacencyList_int32(local_entities), numpy.int32(local_values)) mt.name = pg_tag_name mts[pg_tag_name] = mt return mesh, mts
marked_facets = topologies[gmsh_facet_id]["topology"].astype(np.int64) facet_values = topologies[gmsh_facet_id]["cell_data"].astype(np.int32) else: gmsh_cell_id = MPI.COMM_WORLD.bcast(None, root=0) num_nodes = MPI.COMM_WORLD.bcast(None, root=0) cells, x = np.empty([0, num_nodes]), np.empty([0, 3]) marked_facets, facet_values = np.empty((0, 3), dtype=np.int64), np.empty( (0, ), dtype=np.int32) mesh = create_mesh(MPI.COMM_WORLD, cells, x, ufl_mesh_from_gmsh(gmsh_cell_id, 3)) mesh.name = "ball_d1" entities, values = distribute_entity_data(mesh, 2, marked_facets, facet_values) mesh.topology.create_connectivity(2, 0) mt = create_meshtags(mesh, 2, create_adjacencylist(entities), np.int32(values)) mt.name = "ball_d1_surface" with XDMFFile(MPI.COMM_WORLD, "mesh.xdmf", "w") as file: file.write_mesh(mesh) mesh.topology.create_connectivity(2, 3) file.write_meshtags( mt, geometry_xpath="/Xdmf/Domain/Grid[@Name='ball_d1']/Geometry") # Create a distributed (parallel) mesh with quadratic geometry. Generate # mesh on rank 0, then build a distributed mesh. :: if MPI.COMM_WORLD.rank == 0: # Using model.setCurrent(model_name) lets you change between models model.setCurrent("Sphere minus box")