1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Numerical primitives used throughout the VLE engine.
//!
//! The legacy VB6 + Pascal codebases hand-rolled their own solvers (Regula
//! Falsi, golden-section search, naive Newton-Raphson) inline with each
//! thermodynamic routine. This module extracts every numerical method into
//! one well-tested place so the rest of the engine can call into algorithms
//! by name instead of copy-pasting the legacy implementations.
//!
//! ## What lives here
//!
//! - [`utils`] — small reusable helpers (`SumFrac`, vector norms, convergence
//! checks, `is_near_zero`). Used by every other algorithm in this module.
//! - [`cubic`] — Cardano's formula for `a·x³ + b·x² + c·x + d = 0` with the
//! (12) Poling & Prausnitz robustness for near-degenerate discriminants.
//! The workhorse for cubic-EOS Z-factor and volume-root calculations
//! (M7+).
//! - [`root_finding`] — bracketed scalar root finders. [`root_finding::brent`]
//! is the recommended default (combines bisection + secant + inverse-
//! quadratic interpolation, super-linear convergence with the safety of
//! bisection). [`root_finding::illinois`] is a lighter alternative when
//! you want predictable per-iter cost. Both replace the legacy Regula
//! Falsi from `clsSatPressureSolver.cls`.
//! - [`halley`] — Halley's method (cubic convergence) for scalar equations
//! where you have analytical first and second derivatives. Used in
//! Rachford-Rice (M9).
//! - [`broyden`] — Broyden's "good" quasi-Newton method for multi-variable
//! systems `F(x) = 0`. Rank-1 Jacobian update + periodic finite-difference
//! refresh. Used by the flash and bubble/dew-point iterations in M9.
//!
//! ## Conventions
//!
//! - **Errors as enums**, not panics. Every fallible algorithm returns
//! `Result<T, SpecificError>` where `SpecificError` tells the caller
//! *what* went wrong (no bracket, no convergence, malformed input, …).
//! Callers up the stack collapse them into engine-level errors.
//! - **Pure-function API.** Functions take their callback as a `Fn(f64) ->
//! f64`-style argument and return their result. No globals, no mutable
//! shared state. This keeps the algorithms trivially parallel-safe and
//! easy to test in isolation.
//! - **Tolerances are required, not defaulted.** Every iterative method
//! asks the caller for `xtol` / `max_iter`. Sensible defaults belong at
//! the call site, where the caller knows the physical scale of the
//! variable being solved for.