openCARP-backend#
Writes and reads linear meshes in the ASCII mesh format of the cardiac simulator
openCARP: node coordinates (.pts), elements with region tags
(.elem), fiber and sheet directions per element (.lon), and markers as vertex (.vtx)
and surface (.surf) files. The backend needs no extra dependencies.
The mesh, its tags and its fiber and sheet directions are written with
io4dolfinx.write_mesh(), io4dolfinx.write_meshtags() and
io4dolfinx.write_cell_data(), with the same on_input_mesh in every call, as shown in the
module documentation below. Rank 0 writes all files. openCARP expects coordinates in µm, so the factor
converting the mesh coordinates to µm must always be given as backend_args["scale"].
With markers={"ENDO": (1, 2), "EPI": (2, 2)}, a heart mesh gives heart.pts,
heart.elem, heart.lon, heart_ENDO.vtx, heart_ENDO.surf, heart_EPI.vtx and
heart_EPI.surf, and heart.markers.json with the name, value and dimension of each marker.
Writing the mesh again removes the .lon and marker files of the old mesh, and writing facet tags
again replaces the facet markers.
Triangle (
Tr) and quadrilateral (Qd) meshes in 2D, and tetrahedral (Tt) and hexahedral (Hx) meshes in 3D are supported. 2D meshes are written withz = 0, and read back with 2D coordinates. 2D meshes with 3D coordinates in a plane of constantzare also written.Elements get the orientation used by openCARP’s
mesher: positive volume in 3D, counter-clockwise in 2D.Cells without a cell tag get region
0(setbackend_args={"default_tag": ...}to change it).Fibers and sheets are interpolated into cell midpoints and normalised. 2D directions get
z = 0, and are read back with two components. Fields in a quadrature space cannot be written; generate them in a P-1 or DG-0 space.Each facet marker value gets a
.vtxfile with its nodes and, for 3D meshes, a.surffile with its facets (TrorQd), oriented out of the mesh. A facet between two elements points out of the element whose centre comes first in lexicographic(x, y, z)order. The files are named frommarkers, or by the value if it has no name; values that would give the same file name are rejected. Passbackend_args={"surf": False}to skip the.surffiles.
Reading takes the path of any of the mesh files. Coordinates are divided by scale:
args = {"scale": 1000.0, "markers": {"ENDO": (1, 2), "EPI": (2, 2)}}
mesh = io4dolfinx.read_mesh("heart.elem", MPI.COMM_WORLD, backend="opencarp", backend_args=args)
regions = io4dolfinx.read_meshtags("heart.elem", mesh, "cell", backend="opencarp")
facets = io4dolfinx.read_meshtags("heart.elem", mesh, "facet", backend="opencarp", backend_args=args)
endo = io4dolfinx.read_meshtags("heart.elem", mesh, "ENDO", backend="opencarp", backend_args=args)
f0 = io4dolfinx.read_cell_data("heart.elem", "fiber", mesh, backend="opencarp")
"facet" reads the .surf files of the facet markers listed in heart.markers.json, with
their values. A marker name reads its .surf file, or its .vtx file as vertex tags if there is
no .surf (as for 2D meshes). This also works for marker files written by other tools; their value
is taken from markers, else from the name if it is an integer, else it is 1.
Examples that write a mesh with this backend, run openCARP on it and read the results back (a planar wave in a square, and the Niederer et al. (2011) benchmark) are in opencarp-io4dolfinx-example.
openCARP interface to io4dolfinx.
openCARP stores a mesh as a set of ASCII files sharing a base name, with 0-based node indices and coordinates in µm. See https://opencarp.org/documentation/user-manual (section “Mesh files”).
<base>.pts: number of nodes, then onex y zline per node. 2D meshes havez = 0.<base>.elem: number of elements, then one line per element with its type, its nodes and its tag, e.g.Tt n0 n1 n2 n3 tag. Linear triangles (Tr), quadrilaterals (Qd), tetrahedra (Tt) and hexahedra (Hx) are supported, one type per mesh. The tag is the region of the element; in DOLFINx terms the value of a celldolfinx.mesh.MeshTags.<base>.lon: number of directions (1 for fibers, 2 for fibers and sheets), then onefx fy fz [sx sy sz]line per element, in the order of.elem.<base>_<marker>.vtx: number of nodes, the domain (extrafor indices into the whole mesh) and one node index per line, for stimuli and boundary conditions.<base>_<marker>.surf: number of faces, then oneTr n0 n1 n2orQd n0 n1 n2 n3line per face of a 3D mesh.<base>.markers.json: the name, value and dimension of each marker written by io4dolfinx, so that reading gives the original values, and writing the mesh again removes the marker files of the old mesh. openCARP does not read it.
Each file is written by rank 0.
Writing a mesh with markers and fibers takes the generic io4dolfinx functions, called
in this order with backend="opencarp":
args = {"scale": 1000.0, "markers": {"ENDO": (1, 2), "EPI": (2, 2)}} # mesh in mm
io4dolfinx.write_mesh(
"heart", mesh, backend="opencarp", backend_args=args, on_input_mesh=True
)
for tags in (cell_tags, facet_tags):
io4dolfinx.write_meshtags(
"heart", mesh, tags, backend="opencarp", backend_args=args, on_input_mesh=True
)
io4dolfinx.write_cell_data(
"heart", f0, 0.0, io4dolfinx.FileMode.write, args, "opencarp", on_input_mesh=True
)
io4dolfinx.write_cell_data(
"heart", s0, 0.0, io4dolfinx.FileMode.append, args, "opencarp", on_input_mesh=True
)
The mesh comes first, as tags and directions are written against
<base>.elem.Cell tags set the regions in
<base>.elem, facet tags give a.vtx(and in 3D a.surf) file per marker value.io4dolfinx.write_cell_data()withFileMode.writewrites the fibers, and withFileMode.appendadds the sheets. Fields are interpolated into DG-0, so fields in a quadrature space must first be generated in a P-1 or DG-0 space.
The ordering of nodes and elements must be the same in every step, so pass the same
on_input_mesh to every function:
With
on_input_mesh=True(as above), the files use the ordering of the input mesh and are the same whatever the number of processes. Use this for reproducible files.With
on_input_mesh=False(the default), the files use the DOLFINx ordering, which depends on the partitioning.
Mixing the two gives directions and markers on the wrong elements and nodes.
The files are read with io4dolfinx.read_mesh(), io4dolfinx.read_meshtags()
and io4dolfinx.read_cell_data(), given the path of any of the mesh files, e.g.
"heart.elem".
Elements are written with the orientation used by openCARP’s mesher: positive
volume in 3D, counter-clockwise in the xy-plane in 2D. Hexahedra list one face
(nodes 0-3) followed by the nodes opposite nodes 0, 3, 2 and 1.
Backend arguments:
"scale"(required for meshes): factor converting mesh coordinates to µm, e.g.1000.0for a mesh in mm. Coordinates are divided by it when read."default_tag": region tag of cells without a cell tag (default0)."markers": map from marker name to value, or to(value, dim)as used by cardiac-geometriesx. Names the.vtxand.surffiles of a marker value, which otherwise use the value itself. When reading, it takes precedence over the values in<base>.markers.json."surf": write.surffiles for facet tags of 3D meshes (defaultTrue)."vtx_domain": domain written to.vtxfiles (default"extra", meaning indices into the whole mesh, as written;"intra"means the myocardium submesh).
SPDX License identifier: MIT
Copyright: Henrik Finsberg and Simula Research Laboratory
- io4dolfinx.backends.opencarp.backend.read_cell_data(filename: Path | str, name: str, comm: Comm, time: str | float | None, backend_args: dict[str, Any] | None) tuple[ndarray[tuple[Any, ...], dtype[int64]], ndarray][source]#
Read the fiber or sheet direction of each element from
<base>.lon.- Parameters:
filename – Base name of the files, or the path of one of them
name –
"fiber"or"sheet"comm – Communicator to launch IO on.
time – Not used
backend_args – The backend arguments
- Returns:
A tuple (topology, dofs) where topology contains the vertex indices of the cells, dofs the degrees of freedom within that cell. A 2D mesh gets the x- and y-components.
- io4dolfinx.backends.opencarp.backend.read_function_names(filename: Path | str, comm: Comm, backend_args: dict[str, Any] | None) list[str][source]#
Read the names of the directions in
<base>.lon.- Parameters:
filename – Base name of the files, or the path of one of them
comm – MPI communicator to launch IO on.
backend_args – Arguments to backend
- Returns:
["fiber"]or["fiber", "sheet"]
- io4dolfinx.backends.opencarp.backend.read_mesh_data(filename: Path | str, comm: Comm, time: str | float | None = 0.0, read_from_partition: bool = False, backend_args: dict[str, Any] | None = None) ReadMeshData[source]#
Read a linear mesh from
<base>.ptsand<base>.elem.Node and cell data is read in contiguous chunks on each process. A mesh of 2D elements must lie in a plane of constant
z, and gets 2D coordinates.- Parameters:
filename – Base name of the files, or the path of one of them
comm – The MPI communciator to distribute the mesh over
time – Not used
read_from_partition – Read mesh with partition from file (not supported)
backend_args – Arguments to backend,
"scale"is required
- Returns:
The mesh topology, geometry, UFL domain and partition function
- io4dolfinx.backends.opencarp.backend.read_meshtags_data(filename: str | Path, comm: Comm, name: str, backend_args: dict[str, Any] | None = None) MeshTagsData[source]#
Read mesh tags from openCARP files.
- Parameters:
filename – Base name of the files, or the path of one of them
comm – MPI communicator used in storage
name –
"cell"for the region tags in<base>.elem,"facet"for the faces of the.surffiles of all facet markers in<base>.markers.json, otherwise the name of a marker: the faces of<base>_<name>.surfor, if there is none, the nodes of<base>_<name>.vtx.backend_args – Arguments to backend. The value of a marker is taken from
"markers", else from<base>.markers.json, else from its name if that is an integer, else it is1.
- Returns:
Internal data structure for the mesh tags read from file
- io4dolfinx.backends.opencarp.backend.write_data(filename: Path | str, array_data: ArrayData, comm: Comm, time: str | float | None, mode: FileMode, backend_args: dict[str, Any] | None)[source]#
Write a direction per element to
<base>.lon, normalised to unit length.2D directions get
z = 0.Writing with
FileMode.writegives the fiber direction. Writing again withFileMode.appendadds the sheet direction.- Parameters:
filename – Base name of the files, or the path of one of them
array_data – Cell data in the ordering the mesh was written in, see the
on_input_meshargument ofio4dolfinx.write_cell_data()comm – MPI communicator to open the file with
time – Not used
mode – Write for the fiber, append for the sheet direction
backend_args – The backend arguments
- io4dolfinx.backends.opencarp.backend.write_mesh(filename: Path | str, comm: Comm, mesh: MeshData, backend_args: dict[str, Any] | None = None, mode: FileMode = FileMode.write, time: float = 0.0)[source]#
Write a linear mesh to
<base>.ptsand<base>.elem.Supports triangle and quadrilateral meshes in 2D (also with 3D coordinates in a plane of constant z), and tetrahedral and hexahedral meshes in 3D. Every cell gets the region tag
backend_args["default_tag"], until cell tags are written withwrite_meshtags(). Elements with the opposite orientation of openCARP’smesherare mirrored. The<base>.lonand marker files of an earlier mesh are removed.- Parameters:
filename – Base name of the files, or the path of one of them
comm – MPI communicator used in storage
mesh – Mesh data, from
io4dolfinx.write_mesh()backend_args – Arguments to backend,
"scale"is requiredmode – Only
FileMode.writeis supportedtime – Not used
- io4dolfinx.backends.opencarp.backend.write_meshtags(filename: str | Path, comm: Comm, data: MeshTagsData, backend_args: dict[str, Any] | None = None)[source]#
Write mesh tags to the openCARP files of a mesh written earlier.
Cell tags set the region tags in
<base>.elem. Untagged cells keep their tag.Other tags are written, per value, to
<base>_<marker>.vtxwith the nodes of the tagged entities.Facet tags of a 3D mesh are also written to
<base>_<marker>.surf, as triangles or quadrilaterals with outward normals (ifbackend_args["surf"]). A facet between two elements points out of the element whose centre comes first in lexicographic(x, y, z)order.
<marker>is the name of the value inbackend_args["markers"], or the value itself. The markers are recorded in<base>.markers.json, and replace the markers of the same dimension written earlier.- Parameters:
filename – Base name of the files, or the path of one of them
comm – MPI communicator used in storage
data – Mesh tags with nodes in the ordering the mesh was written in, see the
on_input_meshargument ofio4dolfinx.write_meshtags()backend_args – Arguments to backend