Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pymrm.solve

Back to modules overview

Nonlinear-solver utilities used by pymrm.

View module source on GitHub

Public API

SymbolTypeSummary
KrylovBreakdownclassAn iterative linear solver did not converge (status 6).
NewtonBreakdownclassRaised by newton when no Newton step can be formed.
clip_approachfunctionProject values onto bounds, optionally with a relaxed approach rule.
continuationfunctionSolve function(x, p) = 0 along a sequence of parameter values.
newtonfunctionSolve function(x) = 0 with a (damped) Newton iteration.
pseudo_transientfunctionReach a steady state of function(x) = 0 by implicit pseudo-time steps.

KrylovBreakdown(message, result = None)

Open dedicated reference page

An iterative linear solver did not converge (status 6).

Also a RuntimeError, as raised up to pymrm 2.5.0.

View source on GitHub

Members

__init__(message, result = None)

Initialize self. See help(type(self)) for accurate signature.

View source on GitHub

NewtonBreakdown(message, result = None)

Open dedicated reference page

Raised by newton when no Newton step can be formed.

A singular Jacobian (status 5), a non-finite residual or Jacobian that no damping can avoid (status 4) or a non-finite Newton step (status 6). It is a LinAlgError and so a ValueError, as the exceptions these cases raised up to pymrm 2.5.0. result holds the scipy.optimize.OptimizeResult with the status and the last iterate x.

View source on GitHub

Members

__init__(message, result = None)

Initialize self. See help(type(self)) for accurate signature.

View source on GitHub

clip_approach(values, dummy, lower_bounds = 0, upper_bounds = None, factor = 0)

Open dedicated reference page

Project values onto bounds, optionally with a relaxed approach rule.

Parameters

View source on GitHub

continuation(function, initial_guess, params, *, max_halvings = 6, max_change = 0.2, newton_kwargs = None, verbose = 0)

Open dedicated reference page

Solve function(x, p) = 0 along a sequence of parameter values.

Natural-parameter continuation: each point starts from a secant extrapolation of the two previous solutions and is corrected with newton. A failed correction halves the parameter step (at most max_halvings times per requested value). A correction that moves x by more than max_change times max |x| from the prediction counts as failed: near a turning point (fold) the corrector otherwise jumps silently to another solution branch. The march then stops and reports where; the fold lies between the last parameter reached and the next one. Following a branch around a fold needs arclength continuation, which this function does not do.

Parameters

Returns

View source on GitHub

newton(function, initial_guess, args = (), tol = 1.49012e-08, maxfev = 100, solver = None, lin_solver_kwargs = None, callback = None, rtol = 0.0, *, damping = 'auto', bounds = None, tau = 0.99, scale = None, refresh = None, backward_tol = 1e-08, stagnation = False, row_scaling = False, recoverable = (FloatingPointError, OverflowError, ZeroDivisionError), verbose = 0)

Open dedicated reference page

Solve function(x) = 0 with a (damped) Newton iteration.

Every iteration solves J dx = g with a direct sparse LU factorisation and updates x <- x - lam dx. With the default options the step length lam is 1 (plain Newton) unless the full step gives a non-finite residual, and convergence requires both a small step (tol) and a small backward error (backward_tol). For hard problems, damping, bounds and scale make the iteration robust far from the solution; see the Notes.

Parameters

Returns

Raises

Notes

When plain Newton fails. Far from the solution a full step can leave the region where the residual is meaningful (a negative concentration or temperature), and the iteration ends in a non-finite residual or diverges. damping='guarded' handles the non-finite case at no cost elsewhere. Use damping='monotone' when full steps overshoot without producing non-finite values; add scale when unknowns of different units are mixed (it makes the step and damping tests weigh them equally), and bounds only for unknowns that the residual cannot accept beyond a limit. Where a solution depends on a parameter, warm starts along that parameter (continuation) remain the most reliable route; damping does not choose between multiple solutions.

Kinetics near zero concentration. A hard lower bound at zero often hurts convergence: the discrete solution may itself be slightly negative near steep fronts, and max(c, 0) in a rate makes its derivative zero below zero, so the Jacobian loses the term that pulls c back. Prefer rates that are smooth through zero and first order near it, for example k c (c**2 + eps**2)**((n - 1)/2) instead of k c**n with n < 1, or c / (1 + K abs(c)) for Langmuir-Hinshelwood forms, with eps a small fraction of a reference concentration (check that results do not change when it is reduced tenfold). A guard bound below zero, such as bounds=(-1e-3 * c_ref, None), then only stops wild overshoots.

Convergence verdict. The step test is absolute. A trace unknown much smaller than tol passes it immediately; the default backward_tol=1e-8 then keeps the iteration going until the residual is also small relative to the equations, without changing the meaning of tol. pymrm.checks.residual_check gives a stricter, purely componentwise verdict after the fact.

Changes in 3.0.0. Before, newton took plain full steps even into a non-finite residual, stopped on the step test alone, and switched to ILU-BiCGSTAB above 50 000 unknowns. Pass damping="none", backward_tol=None for the old iteration; it then takes the same steps, evaluations and results as before. A singular Jacobian, a non-finite residual or step, or an iterative linear solver that does not converge still raise, now as NewtonBreakdown with the result attached.

View source on GitHub

pseudo_transient(function, initial_guess, mass = 1.0, dt = 0.001, args = (), *, dt_max = np.inf, max_steps = 200, growth = 10.0, steady_ratio = 0.1, steady_every = 10, newton_kwargs = None, steady_kwargs = None, verbose = 0)

Open dedicated reference page

Reach a steady state of function(x) = 0 by implicit pseudo-time steps.

When Newton from the initial guess fails (ignition, extinction, a guess far from any solution), march mass dx/dt = -function(x) with implicit Euler steps instead: each step solves function(x) + mass (x - x_old) / dt = 0 with newton, which is well conditioned for small dt. After a step whose Newton solve took at most 3 iterations dt doubles, or grows by the residual reduction ||g_old|| / ||g|| when that is larger (switched evolution relaxation; at most growth per step and at most dt_max); after a harder step it is kept, and a failed step is retried with dt / 4. The residual may rise for a while, as it does through an ignition.

Measured on the Weisz-Hicks pellets where newton(..., damping="guarded") fails: the march reached the steady state in 18 of 22, mostly within 20 steps. The four it did not reach (a steep reaction front crawling at tiny dt) were solved by damping="monotone"; try both. A steady Newton solve from the current state is tried every steady_every steps and whenever the residual norm has dropped by steady_ratio since the last attempt; it ends the march when it converges.

Parameters

Returns

View source on GitHub