Boundary Conditions

Boundary conditions can be applied to solution arrays and to the sparse matrix systems that arise from discretised differential equations. Numgrids supports Dirichlet, Neumann, and Robin boundary conditions. BoundaryFace identifies which face of the domain a condition is imposed on, and apply_bcs() applies a collection of conditions in a single call.

class numgrids.BoundaryFace(grid: Grid, axis_index: int, side: str)[source]

A selection of boundary points on one face of a grid.

For an N-dimensional grid, each non-periodic axis contributes two faces: "low" (index 0 along that axis) and "high" (last index).

Parameters:
  • grid (Grid) – The numerical grid.

  • axis_index (int) – Which axis this face belongs to.

  • side (str) – "low" or "high".

property mask: ndarray[tuple[Any, ...], dtype[_ScalarT]]

Boolean mask of shape grid.shape, True on this face.

property flat_indices: ndarray[tuple[Any, ...], dtype[_ScalarT]]

Flat (1-D) indices of this face’s points in the ravelled grid.

property normal_sign: int

Sign of the outward unit normal along the face’s axis.

Returns -1 for the "low" face (outward normal points in the negative axis direction) and +1 for "high".

class numgrids.DirichletBC(face: BoundaryFace, value=0.0)[source]

Dirichlet boundary condition: \(u = g\) on a boundary face.

Parameters:
  • face (BoundaryFace) – The boundary face.

  • value (float, NDArray, or callable) – The prescribed boundary value g.

apply(u: ndarray[tuple[Any, ...], dtype[_ScalarT]]) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Set u to g on the boundary face (in-place).

Returns:

The modified array (same object as u).

Return type:

NDArray

apply_to_system(L: spmatrix, rhs: ndarray[tuple[Any, ...], dtype[_ScalarT]]) tuple[spmatrix, ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]

Replace boundary rows in L with identity rows and set rhs = g.

Parameters:
  • L (spmatrix) – System matrix of shape (grid.size, grid.size).

  • rhs (NDArray) – Right-hand side vector of shape (grid.size,).

Returns:

Modified (L, rhs).

Return type:

tuple of (spmatrix, NDArray)

class numgrids.NeumannBC(face: BoundaryFace, value=0.0)[source]

Neumann boundary condition: \(\partial u / \partial n = g\).

The outward normal derivative is sign * du/d(axis) where sign is +1 on the "high" face and -1 on the "low" face.

Parameters:
  • face (BoundaryFace) – The boundary face.

  • value (float, NDArray, or callable) – The prescribed normal-derivative value g.

apply(u: ndarray[tuple[Any, ...], dtype[_ScalarT]]) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Adjust the boundary layer of u so that the discrete normal derivative approximates g (first-order, in-place).

For higher accuracy, prefer apply_to_system().

apply_to_system(L: spmatrix, rhs: ndarray[tuple[Any, ...], dtype[_ScalarT]]) tuple[spmatrix, ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]

Replace boundary rows with the normal-derivative operator row.

Uses the full differentiation matrix from Diff.

class numgrids.RobinBC(face: BoundaryFace, a: float, b: float, value=0.0)[source]

Robin boundary condition: \(a\,u + b\,\partial u/\partial n = g\).

Parameters:
  • face (BoundaryFace) – The boundary face.

  • a (float) – Coefficient of u.

  • b (float) – Coefficient of the normal derivative.

  • value (float, NDArray, or callable) – The prescribed value g.

apply(u: ndarray[tuple[Any, ...], dtype[_ScalarT]]) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Not supported for Robin conditions; use apply_to_system().

apply_to_system(L: spmatrix, rhs: ndarray[tuple[Any, ...], dtype[_ScalarT]]) tuple[spmatrix, ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]

Replace boundary rows with a * I + b * sign * D.

numgrids.apply_bcs(bcs: list[BoundaryCondition], L: spmatrix, rhs: ndarray[tuple[Any, ...], dtype[_ScalarT]]) tuple[spmatrix, ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]

Apply a sequence of boundary conditions to a linear system.

Conditions are applied in list order; for overlapping points (e.g. corners) the last condition wins.

Parameters:
  • bcs (list of BoundaryCondition) – Boundary conditions to apply.

  • L (spmatrix) – System matrix of shape (grid.size, grid.size).

  • rhs (NDArray) – Right-hand side vector of shape (grid.size,).

Return type:

tuple of (spmatrix, NDArray)