Steady State¶
steady_state
¶
Steady states of autonomous ODE right-hand sides.
:func:steady_state finds y* with rhs(t, y*) = 0 by pseudo-transient
continuation (PTC). Each iteration solves the implicit-Euler step
(I / dt - J) delta = rhs(t, y)
and grows dt by the ratio of successive residuals, so early iterations
follow the dynamics and later ones become Newton steps. Three features of
epidemic models need handling (issue #192):
- Absorbing states. Cumulative counters (deaths, incidence) never reach a
zero derivative. Pass them as
fixed: they keep their initial value and are excluded from the convergence norms. :func:sink_statesfinds states that no derivative depends on. - Conserved totals. When
w @ rhs(t, y) = 0for everyy, the totalw @ yis conserved, the steady states form a family indexed by it, andJis singular. Asdtgrows,I / dt - Jbecomes ill conditioned and the total drifts. Pass the conserved directions asinvariants: the step solves a bordered system that holdsW @ yat its initial value exactly and stays nonsingular asdtgrows without bound. :func:conserved_quantitiesgives the structural invariants of a stoichiometry matrix; :func:linear_invariantsalso finds totals that are conserved because rates balance (birthsmu * N), which the stoichiometry alone misses. - Slow modes. A small residual can hide a large state error along a slowly
decaying mode (error ~ residual / rate). Convergence therefore also requires
a small Newton correction
-J^{-1} rhs, which measures that error, and the final correction is applied to the returned state.
The iteration count is static and every diagnostic stays an array, so the
solver runs under jax.jit and jax.vmap. Pass
loop=jax.lax.fori_loop there, so the iteration is compiled once rather
than unrolled.
SteadyStateConfig(dt0=1.0, dt_max=1000000000000.0, min_growth=2.0, max_growth=10.0, max_iterations=100, residual_tol=1e-09, step_tol=1e-09, atol=1e-08, early_exit=True)
dataclass
¶
Controls for :func:steady_state.
Attributes:
| Name | Type | Description |
|---|---|---|
dt0 |
float
|
Initial pseudo-time step, in the RHS's time units. |
dt_max |
float
|
Largest pseudo-time step. |
min_growth |
float
|
Smallest factor by which |
max_growth |
float
|
Largest factor by which |
max_iterations |
int
|
Iteration budget; the loop is unrolled this many times
when |
residual_tol |
float
|
Bound on the RMS of |
step_tol |
float
|
Bound on the RMS of the Newton correction
|
atol |
float
|
Absolute floor of the per-state scale |
early_exit |
bool
|
With the default Python loop, stop as soon as the solve
converges. This converts the convergence flag to a host boolean,
so under |
__post_init__()
¶
Validate the controls.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a control is outside its valid range. |
Source code in src/op_engine/steady_state.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 | |
SteadyStateConvergenceError(result)
¶
Bases: RuntimeError
Raised at an eager boundary when a steady-state solve did not converge.
Store the failed result.
Source code in src/op_engine/steady_state.py
173 174 175 176 177 178 179 180 181 | |
SteadyStateResult(state, residual, converged, iterations, residual_norm, step_norm, dt, invariant_drift)
dataclass
¶
Candidate steady state and array-valued diagnostics.
Every diagnostic is a zero-dimensional array in the state's namespace, so
a traced solve returns them without a host conversion; check them
afterwards, or call :func:require_steady_state at an eager boundary.
Attributes:
| Name | Type | Description |
|---|---|---|
state |
Array
|
The candidate steady state, including fixed states. |
residual |
Array
|
|
converged |
Array
|
Whether both the residual and the Newton correction met their tolerances. |
iterations |
Array
|
PTC iterations performed before convergence. |
residual_norm |
Array
|
Scaled residual RMS at |
step_norm |
Array
|
Scaled RMS of the last Newton correction. |
dt |
Array
|
Final pseudo-time step. |
invariant_drift |
Array
|
Largest |
conserved_quantities(stoichiometry, *, tol=1e-10)
¶
Return the structural conservation laws of a reaction network.
Rows w satisfy w @ stoichiometry = 0, so w @ y is unchanged by
every reaction, whatever the rates. Totals conserved only because rates
balance (births mu * N against deaths) are not structural; use
:func:linear_invariants for those.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stoichiometry
|
object
|
|
required |
tol
|
float
|
Relative singular-value threshold. |
1e-10
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/op_engine/steady_state.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
linear_invariants(rhs, states, *, t=0.0, jacobian=None, fixed=None, tol=1e-06)
¶
Find totals w @ y that the dynamics conserve at sampled states.
A row w is returned when w @ J(y) = 0 and w @ rhs(t, y) = 0 at
every sampled y, which includes rate-balanced totals that
:func:conserved_quantities misses. Sample a few varied states away from
special points such as the disease-free state.
The test is numerical. A mode decaying more slowly than tol times the
fastest rate looks conserved, and each row's accuracy is limited by the
Jacobian's error relative to the slowest real rate: a slow mode tilts the
rows toward itself. The default Jacobian uses central differences for
that reason; an exact one (jax.jacfwd, for example) is better still.
When you know an invariant, such as a row of ones over the living
compartments, pass that exact row to :func:steady_state and use this
function to confirm that nothing else is conserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
SteadyStateRhs
|
|
required |
states
|
Sequence[Array]
|
Sample states, each a one-dimensional array. |
required |
t
|
float
|
Evaluation time. |
0.0
|
jacobian
|
SteadyStateJacobian | None
|
Dense Jacobian callback; central differences by default. |
None
|
fixed
|
Sequence[int] | Array | None
|
Indices, or a boolean mask, of states that
:func: |
None
|
tol
|
float
|
Singular-value threshold relative to the fastest rate. |
1e-06
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/op_engine/steady_state.py
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 | |
require_steady_state(result)
¶
Return a converged result or raise.
This converts the convergence flag to a Python boolean; do not call it
inside jax.jit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
SteadyStateResult
|
Result to check. |
required |
Returns:
| Type | Description |
|---|---|
SteadyStateResult
|
|
Raises:
| Type | Description |
|---|---|
SteadyStateConvergenceError
|
If the solve did not converge. |
Source code in src/op_engine/steady_state.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
sink_states(rhs, states, *, t=0.0, jacobian=None, tol=1e-09)
¶
Return a mask of states no derivative depends on.
Cumulative counters and absorbing compartments that feed nothing back have
a zero Jacobian column. They never reach a zero derivative while their
inflow is positive, so hold them fixed in :func:steady_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
SteadyStateRhs
|
|
required |
states
|
Sequence[Array]
|
Sample states. |
required |
t
|
float
|
Evaluation time. |
0.0
|
jacobian
|
SteadyStateJacobian | None
|
Dense Jacobian callback; central differences by default. |
None
|
tol
|
float
|
Largest column entry, relative to the largest Jacobian entry, still treated as zero. |
1e-09
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean |
Source code in src/op_engine/steady_state.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | |
steady_state(rhs, y0, *, t=0.0, jacobian=None, fixed=None, invariants=None, config=None, loop=None)
¶
Find a steady state of rhs near y0 by pseudo-transient continuation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rhs
|
SteadyStateRhs
|
|
required |
y0
|
Array
|
Starting state. It also fixes the absorbing states' values and the invariant totals. |
required |
t
|
float
|
Time at which the autonomous RHS is evaluated. |
0.0
|
jacobian
|
SteadyStateJacobian | None
|
|
None
|
fixed
|
Sequence[int] | Array | None
|
Indices, or a boolean mask, of states held at |
None
|
invariants
|
Array | Sequence[Sequence[float]] | None
|
|
None
|
config
|
SteadyStateConfig | None
|
Iteration controls. |
None
|
loop
|
SteadyStateLoop | None
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
SteadyStateResult
|
The candidate steady state and diagnostics. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If shapes are inconsistent. |
TypeError
|
If |
Source code in src/op_engine/steady_state.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |