Enable sparse Jacobian storage and resolution in NewtonRaphsonSolver

Description

Graph analysis (graph_analysis=True) already gave a sparsity pattern, but it was only used to perturb several unknowns at once during finite differencing: the Jacobian was still a dense matrix, factorized by a dense LU. The Jacobian can now be sparse all the way through (storage, finite differences, secant updates and LU factorization) when the problem makes it worthwhile.

Changes

  • FfdJacobianEvaluation
    • The sparsity pattern is kept sparse (it was converted with .toarray()).
    • Grouped finite differences cost O(nnz), instead of one dense O(n²) product per group.
    • Partial Jacobian updates now work with groups: only the groups involved in the residues to update are evaluated. They were silently disabled before.
    • Optional CSC storage of the Jacobian (sparse_storage). Secant updates of a sparse Jacobian use a Schubert update instead of Broyden's, which preserves the pattern.
  • NewtonRaphsonSolver: new options to choose between sparse and dense resolution when a pattern is available:
    • sparse: "auto" (default), True or False;
    • sparse_min_size (default 100) and sparse_max_density (default 0.05), for the "auto" rule: sparse if the problem has at least sparse_min_size unknowns and a density of at most sparse_max_density;
    • when the linear solver is left at its default, it is switched to SparseLUSolver or DenseLUSolver accordingly;
    • a linear solver set explicitly is kept, the Jacobian storage follows it, and a warning is logged if the other solver is recommended.
  • Secant updates use the step actually applied (x - x_prev), not the proposed one. Step components lost to round-off near convergence used to make the Schubert update cancel Jacobian entries, leading to singular matrices.
  • Smaller changes: sparse-aware Jacobian norm for the automatic tolerance, sparse Jacobians written out properly in the debug log, and a SolverResults.jac type hint allowing sparse matrices.
  • Documentation: new section "Sparse storage and linear solver" in the GraphAnalysis tutorial; also fixes the explanation of example 1 (100 unknowns, not 10).

Benchmark

Multi-particle dynamics (PointMass) integrated with CrankNicolson (100 time steps, graph_analysis=True). Times are for the Newton-Raphson solves only; graph analysis is timed separately.

Particles Unknowns Density Dense Sparse Speed-up
10 60 4.2 % 0.46 s 0.42 s 1.1×
30 180 1.4 % 1.40 s 0.50 s 2.8×
100 600 0.42 % 4.80 s 0.84 s 5.7×
300 1,800 0.14 % 24.0 s 2.13 s 11×
1,000 6,000 0.04 % 466 s 2.65 s 175×
3,000 18,000 0.014 % out of reach (about 2.6 GB matrix) 6.86 s —

Dense and sparse trajectories agree to within 1e-13. These figures were taken before the secant-update fix; re-running two sizes afterwards gave the same or slightly better times (300 particles: 1.39 s).

Behaviour changes

  • Without graph analysis: no functional change; secant updates differ only at round-off level.
  • With graph_analysis=True:
    • problems with at least 100 unknowns and at most 5 % density now use sparse resolution automatically (sparse=False restores dense resolution);
    • partial Jacobian updates now take place;
    • NewtonRaphsonSolver.sparsity returns the pattern as a CSR matrix, no longer as a dense array (tests updated).

Tests

  • New cosapp/core/numerics/tests/test_jacobian.py: grouped vs column-by-column finite differences (dense and sparse storage), partial updates by group, Schubert update (pattern kept, secant equation).
  • New TestSparseResolutionSelection in test_NonLinearSolver.py: automatic choice and its thresholds, forced modes, change of mode between runs, explicitly set linear solvers (with warnings), pickling with sparse storage, and steps lost to round-off.
  • Full suite passes.

Known issues, not addressed here (already present on master)

Each issue fails with an explicit error ("singular Jacobian", exception) and behaves the same with and without this MR; none gives a wrong solution silently.

  • return in the finally clause of NewtonRaphsonSolver._solve swallows exceptions (also a SyntaxWarning with Python 3.14, PEP 765).
  • Graph-analysis bugs:
    • an augmented assignment on a slice (self.y[1:] += ...) gives a wrong pattern;
    • self.y[:-1] -= ... raises ValueError;
    • a setup inherited from a parent class raises KeyError.
  • Graph-analysis cost grows as n² with array sizes (22 s for 18,000 unknowns), because create_dependency_matrix builds a dense matrix for every path.
  • Implicit time drivers use one solver for both their intrinsic and transient problems, and do not expose the new options officially yet.

Merge request reports

Loading
Loading