Iterative Solvers & Preconditioners
Preconditioned Conjugate Gradient solver and preconditioner factories for masked-domain elliptic problems.
Conjugate Gradient
finitevolx.solve_cg(matvec, rhs, x0=None, preconditioner=None, rtol=1e-06, atol=None, max_steps=500, *, throw=False)
Preconditioned Conjugate Gradient solver for symmetric linear operators.
Solves A(x) = rhs where matvec implements the symmetric linear
operator A. The operator need not be explicitly formed; only
matrix-vector products are required.
The solve is delegated to :class:lineax.CG, which provides a
numerically stabilised PCG implementation with periodic residual
recomputation to guard against floating-point drift.
The operator A is assumed to be negative definite, which is
the case for the Helmholtz operator (nabla^2 - lambda) when
lambda > 0. Note that lambda < 0 yields an indefinite operator
and can make CG stall (info.converged is then False); use
lambda = 0 only if the operator is strictly negative semidefinite
via the mask (e.g. the masked Laplacian with homogeneous Dirichlet BCs
on a non-trivial domain).
The preconditioner, if supplied, should approximate A^{-1}
(the negative-definite inverse). The required sign-flip for
lineax.CG is handled internally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matvec
|
callable
|
Function implementing the symmetric linear operator |
required |
rhs
|
Float[Array, '...']
|
Right-hand side of the linear system. |
required |
x0
|
Float[Array, '...'] or None
|
Initial guess. Defaults to zeros. |
None
|
preconditioner
|
callable or None
|
Optional approximate inverse of |
None
|
rtol
|
float
|
Relative convergence tolerance. Default: 1e-6. |
1e-06
|
atol
|
float or None
|
Absolute convergence tolerance. |
None
|
max_steps
|
int or None
|
Maximum CG iterations. |
500
|
throw
|
bool
|
Raise when CG does not converge (as |
False
|
Notes
Convergence. max_steps exhaustion never raises unless
throw=True. info.converged is checked against the explicit
residual of the un-preconditioned system, r = rhs - A(x):
max|r_i| <= atol + rtol max|rhs_i| with the atol / rtol the
solve used (s · √ε for the rescaled float32 solve). lineax alone
reports success when CG stalls, e.g. after 0–1 iterations on an
indefinite operator (lambda_ < 0) or with a preconditioner of the
wrong sign; this check reports converged=False there.
Masks. The returned x can be non-zero outside the mask: a
preconditioner that ignores the mask (the spectral ones) leaks into the
dry points, which the masked operator then never sees. Multiply by the
mask before using x; the wrappers (:func:streamfunction_from_vorticity,
:func:pressure_from_divergence, :func:pv_inversion) do.
Gradients. The solve is differentiable with respect to anything
matvec and rhs close over (lambda_, dx, …), through
lineax's implicit derivative. x0 and the preconditioner do not
change the converged solution, so their gradient is stopped; that is
what lets a preconditioner built from the same lambda_ / dx
stay in a differentiated solve.
Tolerances. lineax's CG stops when every entry satisfies
|r_i| <= atol + rtol |rhs_i|. Where rhs_i = 0 (land, the ghost
ring, quiet regions) that is |r_i| <= atol with an absolute
atol. A fixed 1e-6 sits at float32's rounding floor once
|rhs| ~ 1 and below it for an elliptic right-hand side of order
1/dx², so float32 CG never meets it: it runs to max_steps and can
diverge with a spectral preconditioner. Rescaling by s = max|rhs_i|
makes the effective atol = √ε · max|rhs_i| relative to the
right-hand side (and unit-agnostic). s carries no gradient: it only
changes the iteration count. This is the policy gaussx's CG strategies
use (gaussx gh-639).
In float64 the default stays the historical absolute atol = 1e-6,
which is not unit-agnostic: a right-hand side far below 1e-6 in
magnitude (e.g. SI vorticity ~1e-9) is "converged" at the zero
iterate. Pass an atol scaled to the right-hand side in that case.
Returns:
| Name | Type | Description |
|---|---|---|
x |
Float[Array, '...']
|
Approximate solution, same shape as rhs. |
info |
CGInfo
|
Named tuple with fields |
Source code in finitevolx/_src/solvers/iterative.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 | |
finitevolx.CGInfo
Bases: NamedTuple
Convergence diagnostics returned by :func:solve_cg.
Source code in finitevolx/_src/solvers/iterative.py
converged
instance-attribute
True if lineax reported success and the explicit residual satisfies
max|rhs - A(x)| <= atol + rtol max|rhs|.
iterations
instance-attribute
Number of PCG iterations performed.
residual_norm
instance-attribute
L2 norm of the explicit final residual rhs - A(x).
Preconditioners
finitevolx.make_spectral_preconditioner(dx, dy, lambda_=0.0, bc='fft')
Return a spectral-solve preconditioner for PCG.
The preconditioner applies the rectangular spectral solver as an
approximate inverse of the Helmholtz operator (∇² − λ). It is
particularly effective when the physical domain is a subset of the
rectangle (masked-domain problems).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dx
|
float
|
Grid spacing in x. |
required |
dy
|
float
|
Grid spacing in y. |
required |
lambda_
|
float
|
Helmholtz parameter. Must match the parameter used in the main problem. Default: 0.0 (Poisson). |
0.0
|
bc
|
('fft', 'dst', 'dct')
|
Spectral solver type to use as the preconditioner.
Default: |
"fft"
|
Returns:
| Type | Description |
|---|---|
Callable
|
A function |
Source code in finitevolx/_src/solvers/preconditioners.py
finitevolx.make_nystrom_preconditioner(matvec, shape, rank=50, key=None)
Build a randomized Nyström preconditioner for a symmetric operator.
Delegates the low-rank Nyström construction to
:func:gaussx.randomized_nystrom and :class:gaussx.NystromPreconditioner
(Frangella, Tropp & Udell, 2023), then adapts it to the finitevolX
convention: the returned callable operates on 2-D fields and approximates
A^{-1} for the symmetric negative-definite operator A.
gaussx works with positive-semidefinite operators, so this sketches the
PSD operator B = -A as B ≈ U Λ̂ Uᵀ (rank l) and applies
M⁻¹ r = -[ U (Λ̂ + μ)⁻¹ Uᵀ r + (r - U Uᵀ r) / (λ̂_l + μ) ],
i.e. the exact inverse on the captured top of the spectrum and a scalar
1 / (λ̂_l + μ) on its complement. μ = √ε · λ̂_1 is a relative
regulariser (ε the machine epsilon of the dtype), so the
preconditioner rescales with the operator (contract 6) and stays
well-defined for a singular A (e.g. a Neumann Poisson problem).
The Nyström sketch captures the largest eigenvalues of -A. For a
Laplacian these are the grid-scale modes, so it deflates the top of the
spectrum but leaves the smooth modes (the small eigenvalues that set the
condition number) to CG: use make_spectral_preconditioner or
make_multigrid_preconditioner when iteration count matters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matvec
|
callable
|
Function implementing the symmetric (negative-definite) linear operator
|
required |
shape
|
(int, int)
|
Spatial shape |
required |
rank
|
int
|
Number of probing vectors (approximation rank). Default: 50. |
50
|
key
|
Array or None
|
PRNG key for the random probing matrix. If |
None
|
Returns:
| Type | Description |
|---|---|
Callable
|
A function |
Source code in finitevolx/_src/solvers/preconditioners.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
finitevolx.make_preconditioner(kind, *, dx=None, dy=None, lambda_=0.0, bc='fft', matvec=None, shape=None, rank=50, key=None, mg_solver=None)
Convenience factory that builds a preconditioner by name.
Dispatches to :func:make_spectral_preconditioner,
:func:make_nystrom_preconditioner, or
:func:make_multigrid_preconditioner based on kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
('spectral', 'nystrom', 'multigrid')
|
Which preconditioner to build. |
"spectral"
|
dx
|
float or None
|
Grid spacings. Required for |
None
|
dy
|
float or None
|
Grid spacings. Required for |
None
|
lambda_
|
float
|
Helmholtz parameter. Used by |
0.0
|
bc
|
('fft', 'dst', 'dct')
|
Spectral solver type. Used by |
"fft"
|
matvec
|
callable or None
|
Operator |
None
|
shape
|
(int, int) or None
|
Spatial shape |
None
|
rank
|
int
|
Approximation rank. Used by |
50
|
key
|
Array or None
|
PRNG key. Used by |
None
|
mg_solver
|
MultigridSolver or None
|
Pre-built multigrid solver. Required for |
None
|
Returns:
| Type | Description |
|---|---|
callable
|
A function |
Raises:
| Type | Description |
|---|---|
ValueError
|
If kind is unknown or required arguments are missing. |
Examples:
>>> pc = make_preconditioner("spectral", dx=dx, dy=dy, lambda_=10.0)
>>> pc = make_preconditioner("nystrom", matvec=A, shape=(64, 64), rank=30)
>>> pc = make_preconditioner("multigrid", mg_solver=mg)
Source code in finitevolx/_src/solvers/preconditioners.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
Helmholtz operator as a gaussx operator
The rectangular and masked Helmholtz operators as structured gaussx linear
operators, for gaussx.solve, gaussx.logdet, gaussx.sqrt and SPDE
(Matérn) priors on the model grid.
finitevolx.helmholtz_operator(shape, dx, dy, lambda_=0.0, bc='fft')
The rectangular Helmholtz operator (∇² − λ) as a gaussx operator.
The 5-point FD2 Laplacian on a rectangle with periodic ("fft"),
homogeneous Dirichlet ("dst", DST-I) or homogeneous Neumann
("dct", DCT-II) boundaries is diagonalised by that transform:
A = V⁻¹ Λ V, Λ[j, i] = Λʸ_j + Λˣ_i − λ
with the same eigenvalues (fft_eigenvalues, dst1_eigenvalues,
dct2_eigenvalues) as the spectral solvers, so gaussx.solve(A, f)
equals solve_helmholtz_{bc}(f, dx, dy, λ). gaussx.logdet,
gaussx.sqrt, gaussx.inv and gaussx.trace are elementwise in
Λ (gaussx.diag_inv is not: it densifies), and the algebra stays
closed (-A, A - c I, c A), so a
PSD κ² − ∇² is -helmholtz_operator(..., lambda_=κ²).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
(int, int)
|
Field shape |
required |
dx
|
float
|
Grid spacings. |
required |
dy
|
float
|
Grid spacings. |
required |
lambda_
|
float
|
Helmholtz parameter |
0.0
|
bc
|
('fft', 'dst', 'dct')
|
Rectangle boundary condition, as for the spectral solvers. |
"fft"
|
Notes
The Poisson null mode. With λ = 0 and bc="fft" or "dct"
the constant is in the null space: Λ[0, 0] = 0. gaussx treats an
exactly-zero eigenvalue as the pseudo-inverse (a zero coefficient), so
gaussx.solve and gaussx.inv return the zero-mean solution — the
same gauge as solve_helmholtz_fft / _dct — and stay finite, while
gaussx.logdet is -inf (the slogdet convention for a singular
operator). Use λ > 0 (or bc="dst") for a log-determinant. A
right-hand side with a non-zero mean has no solution; the pseudo-inverse
silently drops that mean, exactly as the spectral solvers do.
Returns:
| Type | Description |
|---|---|
DiagonalizedOperator
|
Symmetric, |
Examples:
>>> A = helmholtz_operator((Ny, Nx), dx, dy, lambda_=1.0, bc="dst")
>>> psi = gaussx.solve(A, f.ravel()).reshape(Ny, Nx) # = solve_helmholtz_dst
>>> logdet = gaussx.logdet(-A) # log det(1 - ∇²)
Source code in finitevolx/_src/solvers/linear_operators.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
finitevolx.masked_helmholtz_operator(mask, dx, dy, lambda_=0.0, bc='dst')
The masked Helmholtz operator of :func:masked_laplacian as a gaussx operator.
masked_laplacian applies the 5-point (∇² − λ) to ψ zeroed
outside the mask and outside the array (homogeneous Dirichlet on the
coast and one node beyond the array edge) and keeps the wet rows. As a
matrix that is the wet sub-block A[m][:, m] of the Dirichlet
rectangular operator A (:func:helmholtz_operator with
bc="dst"). This returns that sub-block as a gaussx.MaskedOperator
acting on the wet values only, with the coupling ring (the dry cells the
stencil reaches) precomputed, so gaussx.solve is the capacitance
method: exact, two DST solves per right-hand side.
The mask is concrete and the construction runs on the host (it builds
the |C| × |C| capacitance factorisation); build it once per geometry
and reuse it, like build_capacitance_solver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
array of shape (Ny, Nx), or Mask2D
|
Wet cells ( |
required |
dx
|
float
|
Grid spacings. |
required |
dy
|
float
|
Grid spacings. |
required |
lambda_
|
float
|
Helmholtz parameter |
0.0
|
bc
|
('dst', 'fft', 'dct')
|
Rectangular base operator whose solves the capacitance method uses.
The base enters the masked block only through cells beyond the
array edge: with |
"dst"
|
Returns:
| Type | Description |
|---|---|
MaskedOperator
|
|
Examples:
>>> A_m = masked_helmholtz_operator(mask, dx, dy, lambda_=1.0)
>>> wet = np.asarray(mask, bool).ravel()
>>> psi_wet = gaussx.solve(A_m, f.ravel()[wet])
>>> psi = jnp.zeros(mask.size).at[wet].set(psi_wet).reshape(mask.shape)