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=1e-06, max_steps=500)
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 will cause CG to fail; 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
|
Absolute convergence tolerance. Default: 1e-6. |
1e-06
|
max_steps
|
int or None
|
Maximum CG iterations. |
500
|
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
48 49 50 51 52 53 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 | |
finitevolx.CGInfo
Bases: NamedTuple
Convergence diagnostics returned by :func:solve_cg.
Source code in finitevolx/_src/solvers/iterative.py
converged
instance-attribute
True if the solver converged within the requested tolerances.
iterations
instance-attribute
Number of PCG iterations performed.
residual_norm
instance-attribute
L2 norm of the final residual A(x) - rhs.
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
:class:gaussx.NystromPreconditioner, 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 probes the PSD
operator -A (obtaining (-A)^{-1}) and negates the result to recover
A^{-1}. The resulting callable can be passed straight to
:func:~finitevolx._src.solvers.iterative.solve_cg.
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
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
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |