Boundary patches and conditions
Use patches to say what happens at the boundary of the domain: every block face
that is not connected to another block belongs to a Patch, which names
the boundary (“inlet”, “walls”, “cylinder”) and carries its condition. You attach
patches to block faces with FacePatches when building blocks, and set or
change conditions by patch name with Mesh.set_bc(), which is how you give
conditions to meshes read from files. One patch can span many faces of many
blocks.
Patch
A Patch is a name, a condition (bc) and, for imported patches, the
type it had in the file (kind). A mesh keeps one patch per name; blocks that
use patches of the same name share the same patch.
import torch
import phipict.meshing as pm
walls = pm.Patch("walls", pm.Wall())
inlet = pm.Patch("inlet") # condition set later
print(walls, inlet)
FacePatches
FacePatches assigns patches to the faces of one block. It is a small
typed record with one field per face (x_minus, x_plus, y_minus,
y_plus, z_minus, z_plus) rather than a dictionary, so a misspelled
face is an error. Faces left at None are to be connected or made periodic.
from phipict import Face
fp = pm.FacePatches(x_minus=inlet, y_minus=walls, y_plus=walls)
print(fp[Face.Y_MINUS].name) # walls
fp = fp.set(Face.X_PLUS, pm.Patch("outlet", pm.Outflow()))
all_walls = pm.FacePatches.uniform(walls, ndims=2) # every face
Block operations keep the patches with their faces: flipping or permuting the axes of a block moves the patches accordingly (see Blocks).
Conditions
The conditions are small frozen dataclasses:
Condition |
Meaning |
Solver setup |
|---|---|---|
|
no-slip wall, optionally moving ( |
|
|
free-slip wall or symmetry plane |
|
|
prescribed velocity |
|
|
advected outflow |
|
A velocity can be a vector, a tensor with one value per face cell
([1, dims, *face]), or a profile: a function that maps the centres of the
face cells [n, dims] to velocities [n, dims]. Profiles are evaluated when
the domain is made, so they need no knowledge of the face resolution:
def parabola(p: torch.Tensor) -> torch.Tensor:
u = 1.5 * (1.0 - p[:, 1] ** 2) # channel between y = -1 and 1
return torch.stack([u, torch.zeros_like(u)], dim=1)
inlet = pm.Patch("inlet", pm.Inflow(parabola))
lid = pm.Patch("lid", pm.Wall(velocity=(1.0, 0.0))) # moving wall
Other fields
Conditions for the passive scalar and the electric potential are passed as
extra specs of phipict.bc and applied with phipict.bc.set_bc()
after the velocity condition:
from phipict import bc
hartmann_walls = pm.Patch(
"hartmann_walls", pm.Wall(extra=(bc.Potential.ThinWall(cw=0.1),))
)
heated = pm.Patch(
"heated", pm.Wall(extra=(bc.Scalar.Dirichlet(torch.tensor([[1.0]])),))
)
Setting conditions by name
Mesh.set_bc() sets the condition of a patch. A misspelled name raises a
KeyError that suggests the closest names:
block = pm.make_box((0, -1), (4, 1), cells=(16, 8),
patches=pm.FacePatches(x_minus=inlet, x_plus=pm.Patch("outlet"),
y_minus=walls, y_plus=walls))
mesh = pm.Mesh([block])
mesh.set_bc("outlet", pm.Outflow())
try:
mesh.set_bc("outlt", pm.Outflow())
except KeyError as err:
print(err) # No patch called 'outlt'. Did you mean 'outlet'? ...
Mesh.get_domain() refuses meshes with patches that have no condition and
lists them. Mesh.assign_patch() puts further faces into a patch, e.g. all
remaining free faces.
Outflow
The solver needs the boundary fluxes of a domain to balance, and an outflow has to
move with the flow. Outflow handles both:
its initial velocity is the outward face normal (or a given
velocity), whichMesh.get_domain()scales so that the outflow carries exactly the inflow,Mesh.outflow_hook()returns the callback that advects the outflow velocity before every step.
import phipict
from phipict import Hook, Hooks
domain = mesh.get_domain(viscosity=0.01, dtype=torch.float64, device="cuda",
passive_scalar_channels=0)
hooks = Hooks().append(Hook.PRE, mesh.outflow_hook(domain, velocity=1.0))
sim = phipict.Simulation(domain=domain, dt=0.01, hooks=hooks)
API
- class phipict.meshing.Patch(name: str, bc: Wall | FreeSlip | Inflow | Outflow | None = None, kind: str | None = None)[source]
A named group of boundary faces with a condition.
Patches are compared by identity; a mesh keeps one patch per name.
- name
Name of the patch.
- Type:
- bc
Condition of the faces. None until set, e.g. for imported patches.
- Type:
BoundarySpec or None
- kind
Type of the patch in the file it was read from (
"wall","patch", …), for information.- Type:
str or None
- class phipict.meshing.FacePatches(x_minus: Patch | None = None, x_plus: Patch | None = None, y_minus: Patch | None = None, y_plus: Patch | None = None, z_minus: Patch | None = None, z_plus: Patch | None = None)[source]
The patch of each face of a block; None for faces without one.
Faces without a patch are connected to other blocks, or made periodic.
- classmethod from_faces(patches: Sequence[Patch | None]) FacePatches[source]
From one entry per face, in
Faceorder.
- set(face: Face | int, patch: Patch | None) FacePatches[source]
A copy with the patch of one face replaced.
- classmethod uniform(patch: Patch | None, ndims: int) FacePatches[source]
Every face of an
ndimsblock with the same patch.
- class phipict.meshing.Wall(velocity: Tensor | Sequence[float] | Callable[[Tensor], Tensor] | None = None, extra: tuple[Insulating | Open | Dirichlet | Current | ThinWall | Dirichlet | Neumann | Dirichlet | Neumann, ...] = ())[source]
No-slip wall, optionally moving (
CloseBoundary).- velocity
Wall velocity: static
(u, v[, w]), varying[1, dims, *face], or a profile, seeInflow. None is a resting wall.- Type:
torch.Tensor or sequence of float or Callable or None
- extra
Further conditions, e.g.
bc.Potential.ThinWall(cw=0.1).- Type:
tuple of phipict.bc.Spec
- class phipict.meshing.FreeSlip(extra: tuple[Insulating | Open | Dirichlet | Current | ThinWall | Dirichlet | Neumann | Dirichlet | Neumann, ...] = ())[source]
Free-slip wall or symmetry plane (
OpenBoundary).- extra
Further conditions.
- Type:
tuple of phipict.bc.Spec
- class phipict.meshing.Inflow(velocity: Tensor | Sequence[float] | Callable[[Tensor], Tensor], extra: tuple[Insulating | Open | Dirichlet | Current | ThinWall | Dirichlet | Neumann | Dirichlet | Neumann, ...] = ())[source]
Prescribed inflow velocity.
- velocity
Inflow velocity: static
(u, v[, w]), varying[1, dims, *face], or a profilefn(points) -> velocitiesthat maps the face cell centres[n, dims]to velocities[n, dims], e.g.lambda p: torch.stack([1 - p[:, 1] ** 2, 0 * p[:, 1]], 1).- Type:
torch.Tensor or sequence of float or Callable
- extra
Further conditions.
- Type:
tuple of phipict.bc.Spec
- class phipict.meshing.Outflow(velocity: Tensor | Sequence[float] | Callable[[Tensor], Tensor] | None = None, extra: tuple[Insulating | Open | Dirichlet | Current | ThinWall | Dirichlet | Neumann | Dirichlet | Neumann, ...] = ())[source]
Outflow with a varying velocity.
The face velocity must be updated during the simulation, e.g. with the callback of
Mesh.outflow_hook.Mesh.get_domainscales the initial velocity so that the outflow balances the inflow.- velocity
Initial velocity, as for
Inflow. None is the outward face normal, which the flux balance then scales.- Type:
torch.Tensor or sequence of float or Callable or None
- extra
Further conditions, e.g.
bc.Potential.Open().- Type:
tuple of phipict.bc.Spec