Nonlinear Solver¶
nonlinear_solver
¶
Backend-neutral contracts for nonlinear stage solves.
The portable dense implementation intentionally performs a fixed number of Newton iterations. Static iteration counts keep the numerical loop traceable by array systems such as JAX; convergence is reported as array-valued diagnostics and is never converted to a host boolean inside the solve.
DenseNewtonSolver(config=NewtonConfig())
dataclass
¶
Portable dense Newton solver with statically unrolled iterations.
The solver has no hidden warm-start state. Every call begins from the
explicit initial_guess supplied by the numerical method. Automatic
differentiation follows the performed Newton iterations; this class does
not install an implicit-function or custom derivative rule.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
NewtonConfig
|
Static iteration, tolerance, and damping configuration. |
solve(problem, initial_guess)
¶
Run a fixed number of dense Newton updates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
NonlinearProblem
|
Residual and dense Jacobian callbacks. |
required |
initial_guess
|
Array
|
Explicit warm start in the desired array namespace. |
required |
Returns:
| Type | Description |
|---|---|
NonlinearSolveResult
|
Candidate root with array-valued convergence diagnostics. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If callbacks change namespace or dtype. |
ValueError
|
If no dense Jacobian is supplied or shapes are invalid. |
Source code in src/op_engine/nonlinear_solver.py
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
NewtonConfig(max_iterations=8, rtol=1e-08, atol=1e-10, damping=1.0)
dataclass
¶
Static configuration for the portable dense Newton solver.
Attributes:
| Name | Type | Description |
|---|---|---|
max_iterations |
int
|
Exact number of Newton updates to unroll. |
rtol |
float
|
Relative convergence tolerance against the initial residual norm. |
atol |
float
|
Absolute convergence tolerance. |
damping |
float
|
Fixed multiplier applied to every Newton update. |
__post_init__()
¶
Validate static solver parameters.
Raises:
| Type | Description |
|---|---|
ValueError
|
If an iteration count or tolerance is invalid. |
Source code in src/op_engine/nonlinear_solver.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
NonlinearConvergenceError(result)
¶
Bases: RuntimeError
Raised by an eager boundary when a nonlinear solve did not converge.
Store the failed result and its diagnostics.
Source code in src/op_engine/nonlinear_solver.py
299 300 301 302 303 304 305 306 307 | |
NonlinearProblem(residual, jacobian=None, jvp=None)
dataclass
¶
Residual and optional derivative actions for a nonlinear equation.
residual(x) must return an array with the same shape, dtype, and array
namespace as x. jacobian(x) returns a dense square array acting on
the flattened residual. jvp(x, vector) returns J(x) @ vector with
the same structure as x. Solvers may require either derivative form;
:class:DenseNewtonSolver specifically requires jacobian.
Attributes:
| Name | Type | Description |
|---|---|---|
residual |
NonlinearResidual
|
Residual function whose root is sought. |
jacobian |
NonlinearJacobian | None
|
Optional dense Jacobian function. |
jvp |
JacobianVectorProduct | None
|
Optional matrix-free Jacobian-vector product. |
__post_init__()
¶
Validate callback presence.
Raises:
| Type | Description |
|---|---|
TypeError
|
If a supplied callback is not callable. |
Source code in src/op_engine/nonlinear_solver.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
NonlinearSolveDiagnostics(converged, iterations, residual_evaluations, jacobian_evaluations, initial_residual_norm, residual_norm, step_norm)
dataclass
¶
Array-safe diagnostics from a nonlinear solve.
converged and the norm fields are zero-dimensional arrays in the
initial guess's namespace. Keeping them on-device avoids an accidental
host synchronization or tracer conversion in compiled code.
Attributes:
| Name | Type | Description |
|---|---|---|
converged |
Array
|
Boolean array indicating finite residual convergence. |
iterations |
int
|
Number of nonlinear updates performed. |
residual_evaluations |
int
|
Number of residual evaluations performed. |
jacobian_evaluations |
int
|
Number of dense Jacobian evaluations performed. |
initial_residual_norm |
Array
|
RMS norm before the first update. |
residual_norm |
Array
|
RMS norm after the final update. |
step_norm |
Array
|
RMS norm of the final damped update. |
NonlinearSolveResult(value, residual, diagnostics)
dataclass
¶
Candidate root, final residual, and convergence diagnostics.
NonlinearSolver
¶
Bases: Protocol
Backend-neutral interface implemented by nonlinear solvers.
solve(problem, initial_guess)
¶
Return a candidate root and explicit convergence diagnostics.
Source code in src/op_engine/nonlinear_solver.py
143 144 145 146 147 148 149 | |
require_converged(result)
¶
Return a converged result or raise at an eager host boundary.
This function intentionally converts the scalar convergence array to a
Python boolean. Do not call it from jax.jit or another traced region;
compiled integrations must return diagnostics and validate them afterward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
NonlinearSolveResult
|
Result to validate. |
required |
Returns:
| Type | Description |
|---|---|
NonlinearSolveResult
|
The unchanged converged result. |
Raises:
| Type | Description |
|---|---|
NonlinearConvergenceError
|
If the result did not converge. |
Source code in src/op_engine/nonlinear_solver.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |