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.

Choosing the linear solver inside newton

Every Newton iteration solves one linear system J Δx=gJ\,\Delta x = g. From pymrm 3.0.0, newton does this with a direct sparse LU factorisation (scipy.sparse.linalg.splu) at every problem size, unless you ask for something else. Direct LU is robust: it does not depend on a preconditioner or an iterative tolerance, and newton reuses the factorisation for its damping test. Its cost is the fill-in of the factors. On a 1-D or 2-D grid that fill-in is modest, but on a 3-D grid it grows quickly, and the factorisation can then dominate the run time and the memory.

This example solves the same steady diffusion-reaction problem in 2-D and in 3-D with three linear solvers:

solverwhat it ishow it is passed
direct LUthe defaultnothing, or solver="lu"
ILU + GMRESincomplete LU as preconditioner of GMRES, SciPy onlyan object with a factorize method
AMG + CGalgebraic multigrid as preconditioner of conjugate gradients (pyamg, optional)an object with a factorize method

An object with a factorize(jac) method that returns an object with a .solve(rhs) method is pymrm’s protocol for a user-supplied linear solver. newton calls factorize once per Jacobian and solve for every right-hand side, including those of the damping test. A plain function solver(jac, rhs) also works, but then newton cannot tell factorising from solving.

Imports

The model

A cube (or square) of catalyst with unit side, surface concentration c=1c = 1 on every face, and Langmuir-Hinshelwood kinetics, in dimensionless form:

∇2c=ϕ2 c1+Kc,c=1 on the boundary,\nabla^2 c = \frac{\phi^2\, c}{1 + K c}, \qquad c = 1 \text{ on the boundary},

with ϕ2=50\phi^2 = 50 and K=5K = 5. The residual is g(c)=−∇2c+ϕ2c/(1+Kc)g(c) = -\nabla^2 c + \phi^2 c/(1 + K c), and its Jacobian J=−∇2+diag ⁣(ϕ2/(1+Kc)2)J = -\nabla^2 + \mathrm{diag}\!\left(\phi^2/(1 + K c)^2\right) is symmetric and positive definite on this uniform grid: the Laplacian part is symmetric negative definite and the rate derivative is positive. That is what makes conjugate gradients applicable. A convective term or a coupled multi-component rate would make JJ non-symmetric; use GMRES or BiCGSTAB then.

The diffusion operator is constant, so it is assembled once.

Two factorize objects

Each factorize builds the preconditioner for the current Jacobian and returns an object whose solve runs the Krylov iteration. Two rules matter:

  1. Solve tightly. The iterative solve must be accurate well below the Newton tolerance (here a relative residual of 10-10). A loose inner solve makes Newton converge linearly instead of quadratically, and newton’s backward-error test then keeps iterating.

  2. Raise on failure. A Krylov method that stops at its iteration limit returns an approximate answer with info != 0. Raise an exception instead of returning it, so that a failed solve cannot pass as a Newton step.

Comparison

The same Newton iteration (default options, tol=1e-9) from c=1c = 1, on a 2-D grid of 1502 and a 3-D grid of 323 cells. The table reports the wall time, the Newton iterations and the largest difference from the direct-LU solution.

    grid  unknowns       solver     time  nit  diff to LU
 150^2       22500    direct LU    0.30s    5     0.0e+00
 150^2       22500  ILU + GMRES    6.14s    5     4.6e-15
 150^2       22500     AMG + CG    0.45s    5     4.7e-15
  32^3       32768    direct LU   20.33s    4     0.0e+00
  32^3       32768  ILU + GMRES    6.46s    4     1.1e-15
  32^3       32768     AMG + CG    0.62s    4     1.1e-15

All solvers reach the same solution, to round-off, in the same number of Newton iterations: the linear solver changes the cost of an iteration, not the iteration.

Timings on one machine at larger sizes (same model and solver classes, tol=1e-9; they depend on the machine and its load, so compare ratios, not seconds):

gridunknownsdirect LUILU + GMRESAMG + CG
150222 5000.32 s6.0 s0.36 s
200240 0000.65 s15 s0.58 s
5002250 0007.2 sfails (GMRES limit)3.8 s
30327 00012.8 s4.5 s0.43 s
503125 000432 s48 s2.1 s

In 2-D, direct LU and AMG are equally fast up to 40 000 unknowns, and AMG is twice as fast at 250 000. In 3-D the LU factors fill in: at 27 000 unknowns AMG is already 30 times faster than LU, and at 125 000 unknowns 200 times. ILU-preconditioned Krylov solvers are the weakest choice: in 2-D they are 20 times slower than LU, in 3-D they beat LU but stay 10 to 20 times slower than AMG, they need a drop tolerance tuned to the problem, and GMRES ran out of iterations on the largest 2-D grid. This is why pymrm 3.0.0 dropped its automatic switch from LU to ILU-preconditioned BiCGSTAB above 50 000 unknowns.

Rule of thumb. Keep the default direct LU for 1-D and 2-D models: it needs no tuning, and AMG gains at most a factor of two there. For a 3-D model, plug in AMG through a factorize object, with a tight inner tolerance and an exception on failure. CG requires a symmetric positive-definite Jacobian, as here; with convection or coupled kinetics, test AMG with GMRES or BiCGSTAB on the problem itself.

Checking the result

Agreement between solvers shows that each linear solver did its job, but all three solve the same discrete equations, so it cannot reveal an error in those equations. Two independent checks follow:

  1. residual_check confirms that the returned solution satisfies the discrete equations: its backward error must be at round-off level.

  2. Mass balance: at steady state, the diffusive flux into the pellet through its faces equals the total reaction rate inside. Both sides are computed independently: the flux from the boundary faces and the rate from the cell values. The face gradients come from the same construct_grad operators, so the balance tests the conservation of the discretisation and the convergence of the solve, not the gradient formula itself.

residual_check passes for every solver and grid
reaction 8.053434, inflow 8.053434, relative difference 1.3e-15