Repository navigation
Speed up the equilibrium solver and recover from stalled Newton steps - #53
Merged
Merged
Conversation
…#37) The acid-target benchmark timed out because each call ran 44 cold equilibrium solves, each building a dense Jacobian in pure Python (94% of runtime). The Jacobian and component sums now iterate only each species' non-zero stoichiometry. The nitric-target, acid-option and alkalinity searches share one bracketed Illinois root finder with their previous stopping rules, in place of fixed bisections. The mixed-product nitric target fell from 44 solves and ~70 s to 12 solves and ~1 s. Across every PHREEQC fixture the largest change is 6e-12 relative in species and 4e-13 in the dose. The calculated-pH search keeps its bisection so the shipped phosphate conductivity fit reproduces exactly. The solver could raise 'line search stalled' at valid states where a cold start met a near-singular Jacobian (Fe-EDDHA mixture, pH 7.396). A damped least-squares step now recovers there, and only there, so converging solves are unchanged. Remaining low-pH non-convergence is tracked in #52. Benchmark v1 adds track_search_solves so search work is reported separately from final-state iterations.
…rmance # Conflicts: # CHANGELOG.md # tests/test_chemistry_acceptance.py
rafatahmed
added a commit
that referenced
this pull request
Nov 7, 2026
Speed up the equilibrium solver and recover from stalled Newton steps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #37 at its root cause rather than raising the benchmark budget.
Root cause
The acid-target timeout was a symptom: every
nitric_targetcall (also used by users' acid plans) took about 70 s on the recording laptop. Profiling showed three compounding causes:The benchmark also re-ran a full warm solve in
setup()before every sample, doubling the cost.Changes
flahax/root_finding.py: Illinois method, bisection on any stray step or when two steps fail to halve the bracket). It replaces the fixed bisections in the nitric target,plan_acid_optionsand the alkalinity-to-carbon search, with the same stopping rules. The calculated-pH search keeps its bisection so the shipped phosphate conductivity fit reproduces bit-for-bit.solve()raise "line search stalled" at valid states. A damped least-squares (Levenberg-Marquardt) step now recovers. It runs only where the solver used to raise, so every converging solve is unchanged. The new finder exposed this; bisection had happened to step around it.track_search_solvesreports total solves per call, separately from final-state iterations. The protocol page notes that the 2026-10-20 snapshots predate this change.Evidence
Across all 21 PHREEQC fixtures, the largest output change versus
mainis 5.9e-12 relative in species/activities, 1.3e-12 in SI and 3.5e-13 in the nitric dose. Iteration counts are unchanged.Not fixed here
A pH 4-9 scan found 215 non-converging solves between pH 4.2 and 5.05 for the chelate-rich mixed fixtures (274 on
main). It needs its own solver-design work and is tracked in #52.Verification
sphinx -Wbuilds cleanly.phreeqc-liveCI job will replay it.