Blocks
A MeshBlock is a single discretised block: its vertex coordinates, a
name and the patches of its faces. You create blocks with the builders below
(make_box(), make_quad(), make_hexa(), make_annulus()) when you mesh a
geometry in code, or wrap existing vertex tensors with
MeshBlock.from_coords(). The block operations (transform, reindex, split,
join, extend, extrude) are what you use to assemble more complex geometries from
simple blocks; patches move with their faces through all of them.
All operations return new blocks; a block is not changed in place.
The vertices are stored as the solver stores them, [dims, (nz+1,) ny+1,
nx+1] in float64:
import torch
import phipict.meshing as pm
from phipict import Face
b = pm.make_box((0, 0), (2, 1), cells=(4, 3))
print(b.ndims, b.cells, b.n_cells, list(b.coords.shape)) # 2 (4, 3) 12 [2, 4, 5]
print(b.face_coords(Face.X_PLUS).T) # vertices of the +x face
print(b.cell_centers().shape) # [2, 3, 4], like a cell field
Existing vertex tensors
MeshBlock.from_coords() wraps an existing tensor, with or without the batch
dimension of the solver layout. This is the way in for grids made with other
tools, e.g. phipict.grid.shapes:
x = torch.linspace(0, 1, 5, dtype=torch.float64)
y = torch.linspace(0, 1, 3, dtype=torch.float64)
yy, xx = torch.meshgrid(y, x, indexing="ij")
coords = torch.stack([xx, yy])[None] # [1, 2, 3, 5]
block = pm.MeshBlock.from_coords(coords, name="imported")
Builders
The builders describe a block by its corners, cells and gradings. Each axis takes
a cell count and a grading (None for uniform, a number for an
OpenFOAM expansion ratio).
make_box()
An axis-aligned 2D or 3D block, the most common case: channels, ducts, cavities, the outer blocks of multi-block meshes.
walls = pm.Patch("walls", pm.Wall())
duct = pm.make_box(
(0.0, -1.0, -1.0), (2.0, 1.0, 1.0),
cells=(50, 40, 40),
grading=(None, pm.Symmetric(200.0), pm.Symmetric(50.0)),
patches=pm.FacePatches(y_minus=walls, y_plus=walls, z_minus=walls, z_plus=walls),
name="duct",
)
make_quad() and make_hexa()
General quadrilateral (2D) and hexahedral (3D) blocks from their corners, with
optional curved edges (see Curved edges). Corners are listed x fastest: (-x-y,
+x-y, -x+y, +x+y), and in 3D the same four again at +z.
# a quadrilateral with a curved bottom edge
bump = pm.make_quad(
[(0, 0), (2, 0), (0, 1), (2, 1)],
cells=(32, 16),
grading=(None, 3.0),
edges=pm.QuadEdges(y_minus=pm.Arc(through=(1.0, 0.2))),
)
# a hexahedron with one arc edge between corners 0 and 1
corners = [(0, 0, 0), (1, 0, 0), (0, 1, 0), (1, 1, 0),
(0, 0, 1), (1, 0, 1), (0, 1, 1), (1, 1, 1)]
hexahedron = pm.make_hexa(corners, cells=(8, 8, 8),
edges=[pm.CornerEdge(0, 1, pm.Arc(through=(0.5, -0.1, 0.0)))])
The interior follows from the edges by transfinite interpolation. cells may be
None along an axis whose count is fixed by a Points edge or an
Explicit grading.
make_annulus()
A ring segment, e.g. the blocks of an O-grid around a cylinder. The x axis runs
along the angle, the y axis outwards. With None radial cells, the radial
cells grow with the radius so that they stay roughly square (PICT’s
make_torus_2D); annulus_radial_weights() gives that distribution.
ring = pm.make_annulus((0, 0), r_inner=0.5, r_outer=1.0, start_angle=135.0,
angle=-90.0, cells=(24, None))
print(ring.cells) # (24, 8): radial cells from the square-cell rule
A clockwise (negative) angle gives a right-handed block; for a counter-clockwise one, flip an axis afterwards.
Transformations
Geometric transformations move the vertices:
b = pm.make_box((0, 0), (2, 1), cells=(8, 4))
moved = b.translate((1.0, 0.5))
turned = b.rotate(30.0, center=(1.0, 0.5)) # degrees, counter-clockwise
scaled = b.scale((2.0, 1.0))
mirrored = b.mirror("y", at=0.0) # stays right-handed
b3 = pm.make_box((0, 0, 0), (1, 1, 1), cells=(4, 4, 4)).rotate(45.0, axis=(1, 0, 0))
MeshBlock.mirror() also reverses the vertex order along x, so the mirrored
block keeps positive cell volumes; the patches move with their faces.
Reindexing
MeshBlock.flip() and MeshBlock.permute() change the order of the
vertices without moving them. The solver does not care about the orientation of
blocks (connections are found in any orientation), so these are needed only to
make a block right-handed, or to match an expected index order:
walls = pm.Patch("walls")
b = pm.make_box((0, 0), (2, 1), cells=(8, 4), patches=pm.FacePatches(y_minus=walls))
f = b.flip("y") # the wall patch is now on +y, where the face went
print(f.patches.y_plus.name) # walls
p = b.permute("yx") # x and y swapped: left-handed, flip one axis
p = p.flip("x")
Splitting and joining
MeshBlock.split() cuts a block at a vertex index, e.g. to make faces match
a neighbour; MeshBlock.concat() joins two blocks with a common face into one
(fewer, larger blocks are faster to solve):
long = pm.make_box((0, 0), (3, 1), cells=(12, 4))
first, second = long.split("x", 4) # 4 and 8 cells along x
again = first.concat(second, "x")
assert torch.equal(again.coords, long.coords)
Growing and extruding
MeshBlock.extend() adds cell layers beyond a face by continuing the grid
lines, e.g. an outflow buffer; MeshBlock.extrude() makes a 3D block from a
2D one:
out = pm.Patch("outlet", pm.Outflow())
b = pm.make_box((0, 0), (1, 1), cells=(8, 8), patches=pm.FacePatches(x_plus=out))
longer = b.extend(Face.X_PLUS, length=2.0, cells=8, grading=pm.Simple(3.0))
print(longer.cells, longer.patches.x_plus.name) # (16, 8) outlet
slab = b.extrude((0.0, 0.5), cells=4) # [3, 5, 9, 9] vertices
For whole meshes, use Mesh.extrude(), which also keeps the connections.
API
- class phipict.meshing.MeshBlock(coords: Tensor, name: str = 'block', patches: FacePatches = <factory>)[source]
A structured block of a mesh.
- coords
Vertices
[dims, (nz + 1,) ny + 1, nx + 1], float64 on the CPU.- Type:
- name
Name of the block, also used for the solver block.
- Type:
- patches
Patch of each boundary face.
- Type:
- cell_centers() Tensor[source]
Cell centres, the mean of each cell’s corner vertices.
- Returns:
[dims, (nz,) ny, nx], the layout of the solver’s cell fields without the batch dimension.- Return type:
- concat(other: MeshBlock, axis: int | str, tol: float = 1e-09) MeshBlock[source]
One block of this and
other, joined along an axis.The
+axisface of this block must coincide with the-axisface ofother. The faces along the joint must have the same patch or none.
- extend(face: Face | int, length: float, cells: int, grading: Grading | float | int | None = None) MeshBlock[source]
Grown beyond a face by extrapolating its outermost grid lines.
Each face vertex moves on along the direction of the grid line ending in it, e.g. to add an outflow buffer.
- Parameters:
- Returns:
The larger block.
- Return type:
- extrude(z_range: tuple[float, float], cells: int, grading: Grading | float | int | None = None, z_patches: tuple[Patch | None, Patch | None] = (None, None)) MeshBlock[source]
A 3D block from a 2D block, extruded along z.
- face_coords(face: Face | int) Tensor[source]
Vertices of a face.
- Parameters:
face (Face) – The face.
- Returns:
[dims, n_t1 + 1(, n_t2 + 1)], indexed by the face’s tangential axes in the solver’s order (seetangential_axes()).- Return type:
- flip(axis: int | str) MeshBlock[source]
With the vertex order along a logical axis reversed.
This mirrors the block’s orientation (the vertices do not move); the patches of the two faces of the axis swap.
- classmethod from_coords(coords: Tensor, name: str = 'block', patches: FacePatches | None = None) MeshBlock[source]
A block from a vertex tensor, e.g. one of
phipict.grid.shapes.- Parameters:
coords (torch.Tensor) – Vertices
[(1,) dims, (nz + 1,) ny + 1, nx + 1].name (str, optional) – Name of the block. Default is
"block".patches (FacePatches or None, optional) – Patches of the faces. Default is None (no patches).
- Returns:
The block.
- Return type:
- mirror(axis: int | str, at: float = 0.0) MeshBlock[source]
Mirrored at the plane
axis = at.The vertex order along the block’s own x axis is reversed as well, so the block stays right-handed; the patches follow their faces.
- property n_cells: int
Total number of cells.
- property ndims: int
Number of spatial dimensions.
- permute(axes: Sequence[int | str]) MeshBlock[source]
With the logical axes reordered: new axis
iis old axisaxes[i].An odd permutation changes the handedness of the block; combine it with
flip()to keep it right-handed.
- rotate(angle: float, axis: Sequence[float] | None = None, center: Sequence[float] | None = None, degrees: bool = True) MeshBlock[source]
Rotated about
center(default: the origin).- Parameters:
angle (float) – Rotation angle, counter-clockwise.
axis (sequence of float or None, optional) – Rotation axis of a 3D block. Default is z.
center (sequence of float or None, optional) – Point the rotation is about. Default is the origin.
degrees (bool, optional) – Whether
angleis in degrees. Default is True.
- Returns:
The rotated block.
- Return type:
- scale(factor: float | Sequence[float], center: Sequence[float] | None = None) MeshBlock[source]
Scaled about
center(default: the origin), per axis if a sequence.A negative factor mirrors the block; see
mirror()to keep it right-handed.
- phipict.meshing.make_box(lower: Sequence[float] | Tensor, upper: Sequence[float] | Tensor, cells: Sequence[int], grading: Sequence[Grading | float | int | None] | Grading | float | int | None = None, *, patches: FacePatches | None = None, name: str = 'box') MeshBlock[source]
An axis-aligned 2D or 3D block.
- Parameters:
lower (sequence of float) – Opposite corners; the dimension follows from their length.
upper (sequence of float) – Opposite corners; the dimension follows from their length.
cells (sequence of int) – Number of cells per axis.
grading (sequence or Grading or float or None, optional) – Grading per axis, or one for all. Default is uniform.
patches (FacePatches or None, optional) – Patches of the faces. Default is none.
name (str, optional) – Name of the block. Default is
"box".
- Returns:
The block.
- Return type:
- phipict.meshing.make_quad(corners: Sequence[Sequence[float] | Tensor] | Tensor, cells: Sequence[int | None], grading: Sequence[Grading | float | int | None] | Grading | float | int | None = None, edges: QuadEdges | None = None, *, patches: FacePatches | None = None, name: str = 'quad', interpolation: Interpolation = Interpolation.TFI) MeshBlock[source]
A 2D block from four corners and optional curved edges.
- Parameters:
corners (sequence of point) –
(-x-y, +x-y, -x+y, +x+y).cells (sequence of int or None) – Cells along x and y; None where a
Pointsedge or an explicit grading fixes it.grading (sequence or Grading or float or None, optional) – Grading per axis, or one for both. Default is uniform.
edges (QuadEdges or None, optional) – Shapes and gradings of individual edges. Default is straight.
patches (FacePatches or None, optional) – Patches of the faces. Default is none.
name (str, optional) – Name of the block. Default is
"quad".interpolation (Interpolation, optional) – Interpolation of the interior. Default is transfinite.
- Returns:
The block.
- Return type:
- phipict.meshing.make_hexa(corners: Sequence[Sequence[float] | Tensor] | Tensor, cells: Sequence[int | None], grading: Sequence[Grading | float | int | None] | Grading | float | int | None = None, edges: Sequence[CornerEdge] = (), *, patches: FacePatches | None = None, name: str = 'hex') MeshBlock[source]
A 3D block from eight corners and optional curved edges.
- Parameters:
corners (sequence of point) – Corners in solver order, index
i + 2 j + 4 kfor(x, y, z) = (i, j, k).cells (sequence of int or None) – Cells along x, y and z.
grading (sequence or Grading or float or None, optional) – Grading per axis, or one for all. Default is uniform.
edges (sequence of CornerEdge, optional) – Shapes and gradings of individual edges. Default is straight.
patches (FacePatches or None, optional) – Patches of the faces. Default is none.
name (str, optional) – Name of the block. Default is
"hex".
- Returns:
The block.
- Return type:
- phipict.meshing.make_annulus(center: Sequence[float] | Tensor, r_inner: float, r_outer: float, start_angle: float, angle: float, cells: tuple[int, int | None], radial_grading: Grading | float | int | None = None, *, patches: FacePatches | None = None, name: str = 'annulus') MeshBlock[source]
A 2D ring segment; x runs along the angle, y outwards along the radius.
- Parameters:
center (point) – Centre of the ring.
r_inner (float) – Radii.
r_outer (float) – Radii.
start_angle (float) – Angle of the
-xface in degrees, 0 is the x axis.angle (float) – Angular extent in degrees, counter-clockwise if positive. A negative (clockwise) extent gives a right-handed block; for a positive one flip an axis afterwards.
cells (tuple of int and int or None) – Cells along the angle and the radius. With None radially, the cells grow with the radius to stay roughly square (PICT’s
make_torus_2D).radial_grading (Grading or float or None, optional) – Radial grading if the radial cells are given. Default is uniform.
patches (FacePatches or None, optional) – Patches of the faces. Default is none.
name (str, optional) – Name of the block. Default is
"annulus".
- Returns:
The block.
- Return type: