Grading

A grading sets how the vertices are distributed along an edge. You need one whenever cells should not be equal: fine cells at walls to resolve boundary layers (Hartmann layers, the viscous sublayer), fine cells around an obstacle, coarse cells far away. Every block builder takes one grading per axis, and individual edges can have their own (see Curved edges).

Wherever a grading is expected, None means uniform and a number means an OpenFOAM expansion ratio (Simple):

import phipict.meshing as pm

wall_refined = pm.make_box((0, -1), (10, 1), cells=(64, 48),
                      grading=(None, pm.Symmetric(20.0)))
stretched = pm.make_box((0, 0), (5, 1), cells=(40, 10), grading=(4.0, None))

A grading computes the normalised vertex positions for a number of cells:

print(pm.Simple(4.0).weights(4))
# tensor([0.0000, 0.1098, 0.2841, 0.5608, 1.0000], dtype=torch.float64)

Which grading to use

Grading

Use it for

Uniform

equal cells (the default)

Simple

geometric growth along the edge, given as last cell / first cell (OpenFOAM simpleGrading); Simple(4) refines the start, Simple(0.25) the end

Symmetric

refinement at both ends, e.g. a channel between two walls; ratio of the centre cells to the end cells

FirstCell

a prescribed height of the first cell, e.g. for a target y+; the growth is solved for

MultiGrading

several geometric segments, e.g. fine in a shear layer in the middle of an edge (OpenFOAM multi-grading)

Geometric

a constant cell-to-cell ratio (PICT’s make_weights_exp)

Tanh, Cosine, ChebyshevBlend

smooth clustering laws common in spectral and DNS grids

Explicit

vertex positions computed elsewhere; fixes the cell count

Refinement at walls

y = pm.Symmetric(10.0).weights(20)          # both ends 10x finer than centre
wall = pm.FirstCell(1e-3).weights(40, length=2.0)
print(float(wall[1] * 2.0))                  # 0.001: first cell height
both = pm.FirstCell(1e-3, pm.Cluster.BOTH)   # the same at both ends

FirstCell needs the length of the edge, which the builders pass in.

Segments

MultiGrading combines geometric segments given as (relative length, relative cell count, expansion ratio), exactly as OpenFOAM does:

# 20% of the length with 30% of the cells refined towards the start,
# 60% uniform, 20% refined towards the end
shear = pm.MultiGrading([(0.2, 0.3, 4.0), (0.6, 0.4, 1.0), (0.2, 0.3, 0.25)])

Clustering laws

Tanh, Cosine, ChebyshevBlend and FirstCell take a Cluster that says where the small cells are: START, END or BOTH:

pm.Tanh(2.0)                          # both ends (default)
pm.Tanh(2.0, pm.Cluster.START)        # the start only
pm.Cosine()                           # Chebyshev-like spacing
pm.ChebyshevBlend(0.8)                # 80% sine mapping, 20% uniform

Direction

A grading refers to the direction of its edge or axis. Grading.reversed() gives the same distribution seen from the other end, e.g. for a block whose axis runs the other way:

toward_end = pm.Simple(4.0).reversed()     # the same as Simple(0.25)

BlockMesh mirrors gradings automatically when it passes them to a neighbour whose edge runs the other way.

Cell counts from sizes

cells_for_size() gives the number of uniform cells closest to a target size:

n = pm.cells_for_size(length=22.0, size=0.05)     # 440

API

Cell distributions along a block edge.

A grading maps a number of cells to the normalised vertex positions along an edge, n + 1 strictly increasing weights from 0 to 1:

pm.Simple(4.0).weights(8)          # last cell 4x the first (OpenFOAM simpleGrading)
pm.Symmetric(10.0).weights(32)     # fine at both ends, centre cells 10x larger
pm.FirstCell(1e-3).weights(64, length=2.0)   # first cell height 1e-3

Wherever a grading is accepted, a bare number means Simple with that ratio (the blockMesh idiom) and None means “not given”: Uniform, or the grading of a connected block where a BlockMesh can infer it.

class phipict.meshing.grading.ChebyshevBlend(gamma: float, cluster: Cluster = Cluster.BOTH)[source]

Blend of a sine (Chebyshev) mapping and uniform spacing.

gamma

Share of the sine mapping in [0, 1]: 0 is uniform, 1 a pure sine mapping.

Type:

float

cluster

Where the smallest cells are. Default is both ends.

Type:

Cluster

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Cluster(*values)[source]

Where a grading puts its smallest cells.

class phipict.meshing.grading.Cosine(cluster: Cluster = Cluster.BOTH)[source]

Cosine (Chebyshev-like) spacing.

cluster

Where the smallest cells are. Default is both ends.

Type:

Cluster

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Explicit(values: Sequence[float] | Tensor)[source]

Given vertex weights.

values

Strictly increasing weights from 0 to 1; their count fixes the number of cells.

Type:

torch.Tensor

property cells: int

Number of cells the weights describe.

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.FirstCell(size: float, cluster: Cluster = Cluster.START)[source]

Geometric grading with a prescribed size of the smallest cell.

The expansion ratio is solved for, so the first cell has size for the edge length it is used on, e.g. to hit a target y+ at a wall.

size

Size of the smallest cell(s), in the units of the coordinates.

Type:

float

cluster

Where the smallest cells are. Default is the start.

Type:

Cluster

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Geometric(ratio: float, cluster: Cluster = Cluster.START)[source]

Cells that grow by a constant factor from one cell to the next.

Sizes grow by ratio per cell moving away from cluster (for BOTH, away from both ends towards the centre), so the smallest cells are at cluster for a ratio above 1. This is PICT’s make_weights_exp, including its split of the cells for BOTH: Geometric(1.05, Cluster.BOTH) equals make_weights_exp(n, 1.05, "BOTH").

ratio

Size ratio of neighbouring cells.

Type:

float

cluster

Where the growth starts. Default is the start.

Type:

Cluster

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Grading[source]

Distribution of the vertices along an edge.

reversed() → Grading[source]

The same grading seen from the other end of the edge.

abstractmethod weights(cells: int, length: float = 1.0) → Tensor[source]

Normalised vertex positions along the edge.

Parameters:
  • cells (int) – Number of cells, at least 1.

  • length (float, optional) – Length of the edge. Only gradings given in absolute sizes use it. Default is 1.

Returns:

cells + 1 strictly increasing float64 weights from 0 to 1.

Return type:

torch.Tensor

class phipict.meshing.grading.MultiGrading(segments: Sequence[Segment | Sequence[float]])[source]

Piecewise geometric grading, OpenFOAM’s multi-grading.

MultiGrading([Segment(0.2, 0.3, 4), Segment(0.6, 0.4, 1), Segment(0.2, 0.3, 0.25)]) is blockMesh’s ((0.2 0.3 4) (0.6 0.4 1) (0.2 0.3 0.25)). Relative lengths and cell counts are normalised, and the cell counts are rounded like blockMesh does, with the last segment taking the remainder.

segments

The segments from start to end.

Type:

tuple of Segment

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Segment(length: float, cells: float, ratio: float)[source]

One segment of a MultiGrading.

length

Relative length of the segment.

Type:

float

cells

Relative number of cells in the segment.

Type:

float

ratio

Expansion ratio within the segment, as for Simple.

Type:

float

class phipict.meshing.grading.Simple(ratio: float)[source]

Geometric growth with OpenFOAM’s simpleGrading expansion ratio.

ratio

Size of the last cell over the size of the first. Above 1 refines the start, below 1 the end.

Type:

float

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Symmetric(ratio: float)[source]

Geometric grading that is fine at both ends, e.g. between two walls.

ratio

Size of the centre cells over the size of the end cells. Below 1 refines the centre instead.

Type:

float

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Tanh(beta: float, cluster: Cluster = Cluster.BOTH)[source]

Hyperbolic tangent stretching.

beta

Stretching strength, 0 is uniform. Larger values cluster harder.

Type:

float

cluster

Where the smallest cells are. Default is both ends.

Type:

Cluster

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

class phipict.meshing.grading.Uniform[source]

Equal cells.

reversed() → Grading[source]

See Grading.reversed().

weights(cells: int, length: float = 1.0) → Tensor[source]

See Grading.weights().

phipict.meshing.grading.as_grading(grading: Grading | float | int | None) → Grading[source]

Normalise a grading shorthand.

Parameters:

grading (Grading or float or None) – A grading, a Simple expansion ratio, or None for Uniform.

Returns:

The grading.

Return type:

Grading

Raises:

TypeError – If grading is none of the above.

phipict.meshing.grading.cells_for_size(length: float, size: float) → int[source]

Number of uniform cells that comes closest to a target cell size.

Parameters:
  • length (float) – Length of the edge.

  • size (float) – Target cell size.

Returns:

Number of cells, at least 1.

Return type:

int