Meshes and their markers#
This page illustrates how to:
Create a
sumfem.discrete.MeshData, a mesh with named markers, from locator functions, from existing mesh tags, and from a Gmsh modelLook up the entities of a marker
A model refers to parts of its domain by marker names, e.g. a Dirichlet
condition on "Boundary" or an integral domain.ds("Inlet"). A
MeshData stores a
dolfinx.mesh.Mesh with its
MeshTags, and for each entity dimension
the tag values of each marker name. Below, each way of creating one marks the
boundary of the unit square "Boundary".
from mpi4py import MPI
import dolfinx.io.gmsh
import dolfinx.mesh
import gmsh
import numpy as np
from sumfem import discrete
From locator functions#
from_locators takes, for
each entity dimension and marker name, a function of the coordinates as
dolfinx.mesh.locate_entities() takes. With boundary=True, the
facets are only looked for on the boundary. Here the cells of the left half
are marked too:
square = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 16, 16)
tdim = square.topology.dim
from_locators = discrete.MeshData.from_locators(
square,
{
tdim: {"LeftHalf": lambda x: x[0] <= 0.5 + 1e-10},
tdim - 1: {"Boundary": lambda x: np.full(x.shape[1], True)},
},
boundary=True,
)
Each marker name stands for tag values in the mesh tags, which
from_locators chooses itself: a model only uses the names. What a name
marks is looked up with entities,
which gives the local indices of its entities. Here we print how many there
are on this process:
For the markers made from the locators:
print_markers(from_locators)
'Boundary': 64 entities of dimension 1
'LeftHalf': 256 entities of dimension 2
From a Gmsh model#
The physical groups of a mesh made with Gmsh become
markers with the groups’ names, with
from_gmsh.
dolfinx.io.gmsh.model_to_mesh() turns the Gmsh model, made on one
process, into a distributed mesh with its tags; a mesh read with
dolfinx.io.gmsh.read_from_msh() is used the same way.
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
if MPI.COMM_WORLD.rank == 0:
gmsh.model.add("square")
surface = gmsh.model.occ.addRectangle(0.0, 0.0, 0.0, 1.0, 1.0)
gmsh.model.occ.synchronize()
sides = [tag for _, tag in gmsh.model.getBoundary([(2, surface)], oriented=False)]
gmsh.model.addPhysicalGroup(2, [surface], tag=1, name="Square")
gmsh.model.addPhysicalGroup(1, sides, tag=2, name="Boundary")
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", 1 / 16)
gmsh.model.mesh.generate(2)
gmsh_data = dolfinx.io.gmsh.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0, gdim=2)
gmsh.finalize()
from_gmsh = discrete.MeshData.from_gmsh(gmsh_data)
The mesh is another one, but its boundary is marked by the same name, so a
model written with "Boundary" is discretized on any of these:
print_markers(from_gmsh)
'Boundary': 64 entities of dimension 1
'Square': 610 entities of dimension 2