8000 Speed up the equilibrium solver and recover from stalled Newton steps by rafatahmed · Pull Request #53 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content

Speed up the equilibrium solver and recover from stalled Newton steps - #53

Merged
rafatahmed merged 3 commits into
mainfrom
fix/acid-target-performance
Oct 20, 2026
Merged

rafatahmed merged 3 commits into
mainfrom
fix/acid-target-performance

Conversation

@rafatahmed
Copy link
Copy Markdown
Owner

Fixes #37 at its root cause rather than raising the benchmark budget.

Root cause

The acid-target timeout was a symptom: every nitric_target call (also used by users' acid plans) took about 70 s on the recording laptop. Profiling showed three compounding causes:

  1. The search: 44 cold equilibrium solves per call (fixed 40-step bisection plus bracketing).
  2. Each solve: about 126 Newton iterations.
  3. Each Newton iteration: a dense pure-Python Jacobian over 197 species × 20 × 20 components, which was 94% of runtime even though most entries are zero.

The benchmark also re-ran a full warm solve in setup() before every sample, doubling the cost.

Changes

  • Sparse Jacobian and sums: iterate each species' non-zero stoichiometry. The mathematics is identical; only floating-point summation order changes.
  • Shared bracketed root finder (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_options and 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.
  • Solver stall fix: a near-singular Jacobian after a cold start (Fe-EDDHA mixture, pH 7.396) made 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.
  • Benchmark: track_search_solves reports total solves per call, separately from final-state iterations. The protocol page notes that the 2026-10-20 snapshots predate this change.

Evidence

Before After
Mixed-product nitric target 70.3 s, 44 solves median 1.6 s, 12 solves
Mixed products / mixed phases solve 0.70 s / 1.52 s 0.08 s / 0.12 s
Alkalinity-to-carbon search 150 solves 49 solves
Full test suite (local) about 700 s about 90-160 s

Across all 21 PHREEQC fixtures, the largest output change versus main is 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

  • 331 tests pass (1 skipped), including new root-finder and stall regression tests.
  • sphinx -W builds cleanly.
  • PHREEQC is not installed locally; the phreeqc-live CI job will replay it.

…#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
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 Flahax | 🛠️ Build #35275780 | 📁 Comparing d2e68d5 against latest (a1e9659)

  🔍 Preview build  

2 files changed
± changelog.html
± evidence/benchmark-v1.html

@rafatahmed
rafatahmed merged commit e560755 into main Oct 20, 2026
6 checks passed
@rafatahmed
rafatahmed deleted the fix/acid-target-performance branch October 20, 2026 16:53
rafatahmed added a commit that referenced this pull request Nov 7, 2026
Speed up the equilibrium solver and recover from stalled Newton steps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make acid-target performance measurement complete within an explicit benchmark budget

1 participant

0