Capacitance Matrix Method
Extends fast spectral solvers to irregular / masked domains using the
Sherman-Morrison capacitance correction (Buzbee, Golub & Nielson 1970).
The unknowns are every wet cell of the mask, with ψ = 0 on the coast (the
dry cells next to water): the same problem masked_laplacian and
method="cg" define.
Solver
Note
CapacitanceSolver is re-exported from spectraldiffx, so its docstring
describes spectraldiffx's own builder (unknowns: the wet cells off the
inner ring, ψ = 0 on that ring). finitevolX's
build_capacitance_solver fills
the same class with every wet cell as an unknown and ψ = 0 on the coast.
finitevolx.CapacitanceSolver
Bases: Module
Spectral Poisson/Helmholtz solver for masked irregular domains.
Solves (∇² − λ) ψ = f on the wet cells of mask with ψ = 0 on
the inner boundary (wet cells adjacent to a dry cell) and outside the
mask, using the five-point finite-difference Laplacian.
Method (Buzbee, Golub & Nielson 1970, via gaussx)
Let B be the rectangular operator (periodic / Dirichlet / Neumann by
base_bc), I the interior cells (wet, not on the inner boundary)
and C the inner-boundary cells, which are exactly the cells outside
I that the stencil of I reaches. The masked problem is
B[I][:, I] ψ_I = f_I. The capacitance method solves it with two fast
rectangular solves per call: find y with y[C] = 0 and
(B y)_k = f_k for every k ∉ C (point sources on C absorb the
constraints, via an |C| × |C| capacitance matrix factorised once at
construction). For a singular base (fft/dct with λ = 0) the
constant null vector is included in the capacitance system, so the PDE
holds exactly in the interior (gh-87).
Construct with :func:build_capacitance_solver.
Attributes:
| Name | Type | Description |
|---|---|---|
operator |
MaskedOperator
|
|
interior_indices |
Int[Array, 'Ni']
|
Flat (row-major) indices of the interior cells |
shape |
tuple[int, int]
|
Grid shape |
dx, dy |
float
|
Grid spacings (static; baked into |
lambda_ |
float
|
Helmholtz parameter (static). |
base_bc |
str
|
Rectangular base: |
Source code in .venv/lib/python3.12/site-packages/spectraldiffx/_src/fourier/capacitance.py
__call__(rhs)
Solve (∇² − λ)ψ = rhs on the masked domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
Float[Array, 'Ny Nx']
|
Right-hand side on the full rectangular grid. Only the interior cells are used; values on the inner boundary and outside the mask are ignored. |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'Ny Nx']
|
Solution ψ on the full grid: the masked solve on the interior, exactly zero on the inner boundary and outside the mask. |
Source code in .venv/lib/python3.12/site-packages/spectraldiffx/_src/fourier/capacitance.py
Factory
finitevolx.build_capacitance_solver(mask, dx, dy, lambda_=0.0, base_bc='dst')
Pre-compute the capacitance matrix and return a ready-to-use solver.
The solver's unknowns are every wet cell of mask, with ψ = 0 on the
coast ring (the dry cells 4-adjacent to a wet cell) and outside: the same
problem method="cg" solves through :func:masked_laplacian on that
mask (for ψ with a :class:Mask2D, the wall on the C-grid wall nodes).
The masked operator is the wet sub-block B[W][:, W] of the
rectangular five-point operator B = (∇² − λ)
(:func:~finitevolx.masked_helmholtz_operator with bc=base_bc), a
gaussx.MaskedOperator whose coupling set is the coast ring. This is
an offline function: it factorises the N_c × N_c capacitance
matrix (N_c = number of coast cells) with N_c rectangular solves.
The returned :class:CapacitanceSolver is then two rectangular solves
plus an O(N_c²) back-substitution per right-hand side and is
JIT-compilable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
np.ndarray of bool shape (Ny, Nx), or Mask2D
|
Physical domain mask. |
required |
dx
|
float
|
Grid spacing in x. |
required |
dy
|
float
|
Grid spacing in y. |
required |
lambda_
|
float
|
Helmholtz parameter λ ≥ 0. Use |
0.0
|
base_bc
|
('dst', 'fft', 'dct')
|
Rectangular base operator: Dirichlet (DST-I, the default:
non-singular for every λ ≥ 0), periodic (FFT) or Neumann (DCT-II).
The base only enters through cells beyond the array edge, so every
base gives the |
"dst"
|
Returns:
| Type | Description |
|---|---|
CapacitanceSolver
|
A callable equinox Module with all precomputed arrays baked in; it returns ψ on the full grid, zero on every dry cell. |
Raises:
| Type | Description |
|---|---|
ValueError
|
For an unknown |
Examples:
>>> mask = np.zeros((34, 34), dtype=bool)
>>> mask[1:-1, 1:-1] = True
>>> solver = build_capacitance_solver(Mask2D.from_mask(mask), dx, dy, lambda_=4.0)
>>> psi = streamfunction_from_vorticity(
... zeta, dx, dy, lambda_=4.0, method="capacitance", capacitance_solver=solver
... )
Source code in finitevolx/_src/solvers/elliptic.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
Masked Laplacian
finitevolx.masked_laplacian(psi, mask, dx, dy, lambda_=0.0)
Apply the masked discrete Helmholtz operator (∇² − λ)·ψ.
Enforces homogeneous Dirichlet conditions at the mask boundary by zeroing psi outside the mask before applying the 5-point stencil. The output is also zeroed outside the mask.
Neighbours beyond the array edge are zero (dry), so the array edge is a
homogeneous Dirichlet boundary one node outside the array, wherever the
mask is wet there; nothing wraps around. On an all-ones mask this is
the dense Dirichlet 5-point operator that the DST-I solvers invert, so
make_spectral_preconditioner(..., bc="dst") matches it exactly on a
rectangle; the FFT preconditioner (periodic) is an approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
psi
|
Float[Array, 'Ny Nx']
|
Field to which the operator is applied. |
required |
mask
|
Float[Array, 'Ny Nx'] or Mask2D
|
Binary mask: 1 inside the physical domain, 0 outside (land/exterior).
When an :class: |
required |
dx
|
float
|
Grid spacing in x. |
required |
dy
|
float
|
Grid spacing in y. |
required |
lambda_
|
float
|
Helmholtz parameter. Default: 0.0 (pure Laplacian). |
0.0
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'Ny Nx']
|
Result of (∇² − λ)·(ψ·mask), zeroed outside the mask. |