Reading and writing meshes
Use the readers to run on meshes made with other tools, and the VTK export to look at meshes and results in ParaView or pyvista.
OpenFOAM blockMeshDict
read_blockmeshdict() reads an OpenFOAM blockMeshDict and returns the
Mesh blockMesh would create, as phiPICT blocks: one block per hex,
with the same vertices, cell counts, gradings and curved edges. Patches keep their
names; set the conditions of those whose type does not determine one:
import torch
import phipict.meshing as pm
mesh = pm.read_blockmeshdict("examples/meshes/flow_past_cylinder/blockMeshDict")
print(mesh)
# Mesh(2D, 9 blocks, 78400 cells, 12 connections, patches=[inlet, side1, side2, outlet])
for patch in mesh.patches:
print(patch.name, patch.kind, len(mesh.faces(patch)))
mesh.set_bc("inlet", pm.Inflow((1.0, 0.0)))
mesh.set_bc("outlet", pm.Outflow())
mesh.set_bc("side1", pm.FreeSlip())
mesh.set_bc("side2", pm.FreeSlip())
assert mesh.check().ok
What is read, and how it maps:
blockMeshDict |
phiPICT |
|---|---|
|
block corners |
|
one block; the optional name becomes the block name |
|
|
|
|
|
|
|
|
|
named patch without a condition, set it with |
|
periodic connection |
|
a 2D mesh |
|
patch for faces not listed in |
Not supported, and reported as an error: #calc, #codeStream, #include
and other directives, projected vertices, edges and faces, mergePatchPairs
with entries (non-conforming interfaces), and collapsed hexes. A general OpenFOAM
polyMesh cannot be read: it is unstructured, and the solver needs structured
blocks.
load_blockmeshdict returns
the unbuilt BlockMesh, e.g. to change cell counts before building.
write_blockmeshdict() writes a BlockMesh as a blockMeshDict,
e.g. to run the same blocks in OpenFOAM:
walls = pm.Patch("walls", pm.Wall())
bm = pm.BlockMesh()
bm.add(pm.Quad([(0, 0), (4, 0), (0, 1), (4, 1)], cells=(32, 16),
grading=(None, pm.Symmetric(4.0)),
patches=pm.FacePatches(y_minus=walls, y_plus=walls,
x_minus=pm.Patch("inlet"),
x_plus=pm.Patch("outlet"))))
pm.write_blockmeshdict(bm, "blockMeshDict")
VTK
read_vtk() reads VTK structured grids: .vts files and .vtm
multi-block files of them, as written by phipict.io.export.write_vtk() or
ParaView. Each grid becomes a block, coinciding faces are connected, and the
patches written by write_vtk() are restored (without
conditions). Other VTK formats are read with pyvista, if installed
(pip install pyvista).
from phipict.io import export
export.write_vtk(mesh, "mesh") # writes mesh.vtm and mesh/
again = pm.read_vtk("mesh.vtm")
print(again)
Export to VTK
phipict.io.export writes meshes and simulation results as VTK XML files:
one .vts structured grid per block, collected in a .vtm multi-block file
that ParaView opens directly. Only numpy and the standard library are used.
from phipict.io import export
export.write_vtk(mesh, "out/mesh") # cell volume and scaled Jacobian as cell data
domain = mesh.get_domain(viscosity=0.01, dtype=torch.float64, device="cuda",
passive_scalar_channels=0)
export.write_vtk(domain, "out/solution", mesh=mesh) # fields and named patches
For a domain, the cell fields are chosen with Fields
(velocity, pressure, passive scalar, velocity source, electric potential,
viscosity; Fields.QUALITY adds the quality measures). Passing the mesh
also writes its boundary patches, one grid per patch face, so they can be shown
separately in ParaView. 2D meshes are written as grids of zero thickness in z.
Time series
VTKSeries writes snapshots into a .pvd collection
that ParaView plays as a time series. Its hook()
writes every n-th step during a simulation:
import phipict
from phipict import Hook, Hooks
series = export.VTKSeries("out/run", mesh=mesh)
hooks = (
Hooks()
.append(Hook.PRE, mesh.outflow_hook(domain, velocity=1.0))
.append(Hook.POST, series.hook(every=5))
)
sim = phipict.Simulation(domain=domain, dt=0.05, hooks=hooks)
for _ in range(10):
sim.single_step()
API
- phipict.meshing.read_blockmeshdict(path: str | Path, ndims: int | None = None, rel_tol: float = 0.0001) Mesh[source]
Read a blockMeshDict as a
Mesh.- Parameters:
path (str or pathlib.Path) – The blockMeshDict.
ndims (int or None, optional) – 2 or 3; None reads meshes with one cell between empty patches as 2D.
rel_tol (float, optional) – Face matching tolerance, see
Mesh. Default is 1e-4.
- Returns:
The mesh with its connections, periodic (cyclic) connections and patches.
- Return type:
- Raises:
FoamParseError – If the file uses unsupported features.
- phipict.meshing.write_blockmeshdict(block_mesh: BlockMesh, path: str | Path, thickness: float = 1.0) None[source]
Write the blocks of a
BlockMeshas a blockMeshDict.Cells and gradings are written as blockMesh
edgeGrading, curved edges asarc/polyLine/spline/BSpline, and patches with their condition’s type (wall,symmetryPlane) or the type they were read with. A 2D mesh is written with one cell ofthicknessin z betweenemptypatches.- Parameters:
block_mesh (BlockMesh) – The blocks.
path (str or pathlib.Path) – Output file.
thickness (float, optional) – Extent in z of a 2D mesh. Default is 1.
- Raises:
ValueError – If a grading or edge has no blockMesh equivalent (e.g. tanh), or a block uses PICT row interpolation.
- phipict.meshing.read_vtk(path: str | Path, rel_tol: float = 0.0001) Mesh[source]
Read structured grids as a mesh.
Every grid becomes a block; coinciding faces are connected. Grids of a
boundarygroup written byphipict.io.export.write_vtk()restore the patches (without conditions). Grids that are one vertex thick in z and flat are read as 2D.- Parameters:
path (str or pathlib.Path) – A
.vtmor.vtsfile, or any file pyvista reads.rel_tol (float, optional) – Face matching tolerance, see
Mesh. Default is 1e-4.
- Returns:
The mesh.
- Return type:
- phipict.io.export.write_vtk(target: _C.Domain | Mesh, path: str | os.PathLike[str], *, fields: Fields = <Fields.ALL: 63>, mesh: Mesh | None = None, batch_index: int = 0, compress: bool = True) Path[source]
Write a domain or mesh as a VTK multi-block file.
- Parameters:
target (phipict.Domain or phipict.meshing.Mesh) – What to write. A domain needs vertex coordinates on its blocks.
path (str or os.PathLike) – Output path;
.vtmis added if missing, the blocks go to a directory of the same name without the extension.fields (Fields, optional) – Cell fields to write (for a domain). Default is all present fields; add
Fields.QUALITYfor cell volume and scaled Jacobian. A mesh always gets the quality fields.mesh (Mesh or None, optional) – The mesh the domain was made from, to also write its boundary patches. Default is None.
batch_index (int, optional) – Environment to write from a batched domain. Default is 0.
compress (bool, optional) – Whether to zlib-compress the data. Default is True.
- Returns:
Path of the
.vtmfile.- Return type:
- class phipict.io.export.Fields(*values)[source]
Cell fields of a block. Members combine, e.g.
VELOCITY | PRESSURE.
- class phipict.io.export.VTKSeries(path: str | os.PathLike[str], *, fields: Fields = <Fields.ALL: 63>, mesh: Mesh | None = None, batch_index: int = 0, compress: bool = True)[source]
Snapshots of a domain over time.
- Parameters:
path (str or os.PathLike) – Collection file;
.pvdis added if missing. The snapshots go to a directory of the same name without the extension.fields (Fields, optional) – Cell fields to write. Default is all present fields.
mesh (Mesh or None, optional) – Mesh of the domain, to also write its boundary patches. Default is None.
batch_index (int, optional) – Environment to write from a batched domain. Default is 0.
compress (bool, optional) – Whether to compress the data. Default is True.
- hook(every: int = 1) Callable[[...], None][source]
A callback for
Hook.POSTthat writes everyevery-th step.- Parameters:
every (int, optional) – Write interval in steps. Default is 1.
- Returns:
The callback.
- Return type:
Callable
- write(domain: phipict._C.Domain, time: float) Path[source]
Write a snapshot and add it to the collection.
- Parameters:
domain (phipict.Domain) – The domain.
time (float) – Simulation time of the snapshot.
- Returns:
The snapshot’s
.vtmfile.- Return type: