Skip to content

fix(alkalinity): count fluoride as a conservative strong-acid anion (#458) - #467

Merged
rkingsbury merged 1 commit into
mainfrom
rk/fix-alkalinity-458
Sep 9, 2026
Merged

fix(alkalinity): count fluoride as a conservative strong-acid anion (#458)#467
rkingsbury merged 1 commit into
mainfrom
rk/fix-alkalinity-458

Conversation

@rkingsbury

Copy link
Copy Markdown
Member

Summary

Follow-up to #458 / #459. While investigating the residual ~9% discrepancy between
Solution.alkalinity and the alkalinity reported by the phreeqc2026 engine
(PhrqSol.get_alkalinity()) for seawater, the gap decomposed — via an exact charge-balance
identity — into two independent causes:

  1. Fluoride (F⁻) was omitted from the conservative strong-acid-anion set. (~3.4 mg/L as CaCO₃ for seawater)
  2. Residual charge imbalance of non-electroneutral inputs, which is a definitional difference
    between the conservative (Stumm) alkalinity and PHREEQC's titration alkalinity. (~7 mg/L for the unbalanced seawater preset)

This PR addresses only cause (1), which is unambiguous: F⁻ is a strong-acid anion (like Cl⁻/Br⁻/I⁻)
and must count against the conservative alkalinity. Cause (2) is left as-is by design — subtracting the
charge imbalance would redefine alkalinity for non-electroneutral inputs (and the conservative and
titration definitions already coincide for any charge-balanced solution). The docstring now notes this
explicitly, and the divergence origin is written up in the #458 discussion.

Changes

  • Add ("F", -1.0): -1 to the acid_anions map in Solution.alkalinity.
  • Document that the conservative alkalinity equals the engine's titration alkalinity only for a
    charge-balanced solution; non-electroneutral inputs additionally differ by the residual charge imbalance.
  • New regression test test_alkalinity_fluoride_is_conservative: a charge-balanced NaF solution has
    zero conservative alkalinity (Na⁺ and F⁻ cancel), and adding 1 mmol/L F⁻ lowers alkalinity by ~50 mg/L as CaCO₃.
  • Minor: combine a pre-existing nested if in the engine cross-check (ruff SIM102) and add a CHANGELOG entry.

Verification

  • Seawater conservative alkalinity drops from ~117.5 to ~114.1 mg/L as CaCO₃ (the fluoride term),
    matching the charge-balanced PHREEQC value.
  • Full tests/test_solution.py, tests/test_engine_phreeqc2026.py, and tests/phreeqc/ suites pass (796 tests).

🤖 Generated with Claude Code

Fluoride (F-) is a strong-acid anion but was missing from the conservative
anion set used by Solution.alkalinity, leaving the conservative alkalinity
of fluoride-bearing waters (e.g. seawater) ~3.4 mg/L as CaCO3 too high
relative to the PHREEQC engine's titration alkalinity. Add ("F", -1.0): -1
to the acid_anions map and document that the conservative and titration
definitions coincide only for a charge-balanced solution.

Refs #458

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.77%. Comparing base (845653a) to head (51a1caa).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #467      +/-   ##
==========================================
- Coverage   87.78%   87.77%   -0.01%     
==========================================
  Files          14       14              
  Lines        1989     1988       -1     
  Branches      347      346       -1     
==========================================
- Hits         1746     1745       -1     
  Misses        192      192              
  Partials       51       51              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rkingsbury

Copy link
Copy Markdown
Member Author

@jjstickel I'm going to merge this now to address source 1, but let me know your thoughts about the definitional difference:

  1. Residual charge imbalance of non-electroneutral inputs, which is a definitional difference
    between the conservative (Stumm) alkalinity and PHREEQC's titration alkalinity. (~7 mg/L for the unbalanced seawater preset)

@rkingsbury rkingsbury added the fix Bug Fixes label Sep 9, 2026
@rkingsbury
rkingsbury merged commit 0b64202 into main Sep 9, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant