ripopt 0.6.0

A memory-safe interior point optimizer in Rust
Documentation
# Electrolyte Thermodynamics Benchmark Report

This report describes a suite of 13 nonlinear optimization problems drawn from electrolyte thermodynamics and evaluates the performance of **ripopt** (a pure-Rust interior-point solver) against **Ipopt** (the widely used C++ solver). These problems are representative of the computational challenges encountered in process simulation for desalination, brine chemistry, geochemistry, and electrochemical systems.

## Background: Why Electrolytes Are Hard

Electrolyte solutions present distinctive challenges for nonlinear solvers:

1. **Singularities in activity coefficient models.** The Debye-Huckel limiting law contains $1/\sqrt{I}$ terms that diverge as ionic strength approaches zero, creating ill-conditioned Hessians.
2. **Exponential nonlinearities.** Models like Pitzer and NRTL use $\exp(-\alpha\tau)$ terms that produce steep, narrow valleys in the objective landscape.
3. **Extreme variable scaling.** Species concentrations in a single problem can span 18 orders of magnitude (e.g., $[\text{PO}_4^{3-}] \sim 10^{-19}$ to $[\text{H}_3\text{PO}_4] \sim 10^{-2}$ mol/kg).
4. **Tightly coupled constraints.** Electroneutrality and mass balance constraints link all species through nonlinear relationships.

## Activity Coefficient Models

All problems use one or more of the following thermodynamic models, with parameters from published sources at 25 °C (298.15 K).

### Extended Debye-Huckel (Truesdell-Jones)

$$\ln \gamma_i = \frac{-A_{DH} \, z_i^2 \sqrt{I}}{1 + a_i \, B_{DH} \sqrt{I}} + \dot{b}_i \, I$$

where $I = \frac{1}{2}\sum_j z_j^2 m_j$ is the ionic strength, $A_{DH} = 0.5091$, $B_{DH} = 0.3283$ A$^{-1}$(mol/kg)$^{-1/2}$, and $a_i$, $\dot{b}_i$ are ion-specific parameters.

### Pitzer Model (1:1 electrolyte)

The mean ionic activity coefficient for a 1:1 salt at molality $m$ (with $I = m$):

$$\ln \gamma_\pm = f^\gamma + m \, B^\gamma + m^2 \, C^\gamma$$

where:

$$f^\gamma = -A_\phi \left[\frac{\sqrt{I}}{1 + 1.2\sqrt{I}} + \frac{2}{1.2}\ln(1 + 1.2\sqrt{I})\right]$$

$$B^\gamma = 2\beta_0 + \frac{2\beta_1}{4I}\left[1 - (1 + 2\sqrt{I})e^{-2\sqrt{I}}\right], \quad C^\gamma = \frac{3}{2}C_\phi$$

with $A_\phi = 0.3915$. The osmotic coefficient is:

$$\phi = 1 - A_\phi \frac{\sqrt{I}}{1 + 1.2\sqrt{I}} + m(\beta_0 + \beta_1 e^{-2\sqrt{I}}) + m^2 C_\phi$$

### NRTL Model (binary liquid)

For a binary mixture of components 1 and 2:

$$\ln \gamma_1 = x_2^2 \left[\tau_{21}\left(\frac{G_{21}}{x_1 + x_2 G_{21}}\right)^2 + \frac{\tau_{12} G_{12}}{(x_2 + x_1 G_{12})^2}\right]$$

where $G_{ij} = \exp(-\alpha\,\tau_{ij})$.

## Problem Formulation

### Speciation problems (Problems 1-5, 13)

These minimize the total Gibbs free energy of a multicomponent aqueous solution:

$$\min_{\mathbf{x}} \quad f = \sum_i m_i \left(\frac{\mu_i^0}{RT} + \ln\gamma_i(I) + \ln m_i\right)$$

subject to mass balance and electroneutrality constraints. Variables are log-transformed: $x_i = \ln m_i$, so $m_i = e^{x_i}$. This transformation keeps variables $O(1)$ to $O(10)$ even when concentrations span many decades.

The standard chemical potentials $\mu_i^0/RT$ encode the equilibrium constants. For example, from the reaction $\text{HCO}_3^- \rightleftharpoons \text{CO}_3^{2-} + \text{H}^+$ with $K_2 = 10^{-10.33}$:

$$\frac{\mu^0_{\text{CO}_3^{2-}}}{RT} = \text{p}K_2 \cdot \ln 10 = 23.79$$

Constraints take the form:

- **Mass balance:** $\sum_i a_{ij} e^{x_i} = b_j$ (total element conservation)
- **Electroneutrality:** $\sum_i z_i e^{x_i} = 0$

### Phase equilibrium problems (Problems 6-9)

These find conditions where chemical potentials are equal across phases:

$$\ln(\gamma_i^\alpha \, x_i^\alpha) = \ln(\gamma_i^\beta \, x_i^\beta)$$

or satisfy solubility products:

$$\gamma_+^{\nu_+} \gamma_-^{\nu_-} m_+^{\nu_+} m_-^{\nu_-} = K_{sp}$$

### Parameter fitting problems (Problems 10-12)

Nonlinear least-squares minimization:

$$\min_{\boldsymbol{\theta}} \quad f = \sum_k \left[\phi_{\text{calc}}(m_k; \boldsymbol{\theta}) - \phi_{\text{data},k}\right]^2$$

where $\boldsymbol{\theta}$ are model parameters and data is synthetic (generated from known true parameters), ensuring $f^* = 0$ at the global optimum.

## Problem Catalog

### Category 1: Speciation / Chemical Equilibrium

| # | Problem                  | n | m | Activity Model           | Key Challenge                                                                 |
|---|--------------------------|---|---|--------------------------|-------------------------------------------------------------------------------|
| 1 | Water autoionization     | 1 | 0 | Extended DH              | Extreme scaling ($m \sim 10^{-7}$), $1/\sqrt{I}$ singularity                  |
| 2 | CO2-water speciation     | 5 | 2 | Extended DH              | 3 coupled equilibria, concentrations span $10^{-11}$ to $10^{-3}$             |
| 3 | NaCl strong electrolyte  | 4 | 3 | Extended DH + $\dot{b}I$ | Near-determined system at $I=0.1$ with ion-specific parameters                |
| 4 | CaCl2 + NaCl mixed       | 6 | 4 | Extended DH              | Divalent Ca$^{2+}$ ($4\times$ DH effect), CaOH$^+$ ion pair                   |
| 5 | Phosphoric acid (0.01 m) | 6 | 2 | Extended DH              | $z=-3$ ion ($9\times$ DH), 3 successive pK$_a$, range $10^{-19}$ to $10^{-2}$ |

### Category 2: Phase Equilibrium

| # | Problem                 | n | m | Activity Model   | Key Challenge                                               |
|---|-------------------------|---|---|------------------|-------------------------------------------------------------|
| 6 | HCl mean activity       | 1 | 0 | Pitzer           | Strongly $m$-dependent $\gamma_\pm$, nonlinear root-finding |
| 7 | NaCl solubility (SLE)   | 1 | 0 | Pitzer           | Concentrated (6.1 m), Pitzer at validity limit              |
| 8 | Water-butanol-NaCl LLE  | 2 | 2 | NRTL + Setchenow | $\exp()$ from NRTL, salting-out coupling                    |
| 9 | Saturated brine VLE+SLE | 3 | 3 | Pitzer           | Multi-phase coupling (solid + liquid + vapor)               |

### Category 3: Parameter Fitting

| #  | Problem               | n | m | Model          | Key Challenge                                                  |
|----|-----------------------|---|---|----------------|----------------------------------------------------------------|
| 10 | Pitzer NaCl fit       | 3 | 0 | Pitzer osmotic | Correlated $\beta_0$/$\beta_1$, $\exp(-2\sqrt{m})$ sensitivity |
| 11 | Multi-salt DH fit     | 8 | 0 | Extended DH    | 24 residuals, $a$/$\dot{b}$ compensation across 3 salts        |
| 12 | eNRTL T-dependent fit | 4 | 0 | eNRTL          | $\exp(-\alpha\tau)$ steep landscape, multiple local minima     |

### Category 4: Scale-Up

| #  | Problem             | n  | m | Model          | Key Challenge                                      |
|----|---------------------|----|---|----------------|----------------------------------------------------|
| 13 | Seawater speciation | 15 | 8 | DH + Setchenow | 18 orders of magnitude, 5 ion pairs, full coupling |

## Detailed Problem Descriptions

### Problem 1: Water Autoionization

The simplest speciation problem. A single variable $x = \ln m_{\text{H}^+}$ determines the equilibrium of $\text{H}_2\text{O} \rightleftharpoons \text{H}^+ + \text{OH}^-$ under the constraint $K_w = a_{\text{H}^+} \cdot a_{\text{OH}^-} = 1.012 \times 10^{-14}$. By symmetry $m_{\text{H}^+} = m_{\text{OH}^-}$, so ionic strength $I = m$. The DH activity coefficients with $a_{\text{H}^+} = 9.0$ A and $a_{\text{OH}^-} = 3.5$ A give $\gamma \approx 1$ at the solution ($I \sim 10^{-7}$), yielding $m^* \approx 1.006 \times 10^{-7}$.

### Problem 2: CO2-Water Speciation

Five species [H$_2$CO$_3$, HCO$_3^-$, CO$_3^{2-}$, H$^+$, OH$^-$] subject to total carbon balance (0.001 mol/kg) and electroneutrality. Three equilibria are encoded in the chemical potentials: $\text{p}K_1 = 6.35$, $\text{p}K_2 = 10.33$, $\text{p}K_w = 14.0$. The solution gives pH $\approx$ 5.65 with H$_2$CO$_3$ as the dominant carbon species ($\sim 9.95 \times 10^{-4}$ mol/kg).

### Problem 3: NaCl Strong Electrolyte

Four species [Na$^+$, Cl$^-$, H$^+$, OH$^-$] at 0.1 mol/kg NaCl. Three equality constraints fix the Na and Cl balances and enforce electroneutrality. Uses ion-specific $\dot{b}$ parameters ($\dot{b}_{\text{Na}} = 0.075$, $\dot{b}_{\text{Cl}} = 0.015$) that extend the DH model to higher ionic strength. The solution gives pH $\approx$ 7 with $\gamma_{\text{Na}^+} \approx 0.78$, $\gamma_{\text{Cl}^-} \approx 0.76$.

### Problem 4: CaCl2 + NaCl Mixed Electrolyte

Six species including the divalent Ca$^{2+}$ (whose DH term scales as $z^2 = 4$) and the ion pair CaOH$^+$ (formation constant $K = 10^{1.3}$). Four constraints (Ca, Na, Cl balances + electroneutrality) make this a nearly square system. The challenge is that Ca$^{2+}$ dominates the ionic strength contribution while CaOH$^+$ is a trace species ($\sim 10^{-6}$ mol/kg).

### Problem 5: Phosphoric Acid

The triprotic acid H$_3$PO$_4$ at 0.01 mol/kg produces six species through three successive dissociations ($\text{p}K_a = 2.148, 7.199, 12.35$) plus water autoionization. The PO$_4^{3-}$ ion has $z = -3$ (giving a $9\times$ DH correction) and its concentration ($\sim 2.8 \times 10^{-19}$ mol/kg) is 17 orders of magnitude below the dominant species. This extreme range, handled via log-transformation, is the defining challenge.

### Problem 6: HCl Mean Activity

Find the molality $m$ of HCl where the Pitzer mean ionic activity $a_\pm = \gamma_\pm m$ matches a target value (computed at $m = 1.0$ with published parameters $\beta_0 = 0.1775$, $\beta_1 = 0.2945$, $C_\phi = 0.00080$). The nonlinearity comes from the strongly $m$-dependent Pitzer $\gamma_\pm$.

### Problem 7: NaCl Solubility

Find the saturation molality satisfying $\gamma_\pm^2 m^2 = K_{sp} = 37.584$ using the Pitzer model ($\beta_0 = 0.0765$, $\beta_1 = 0.2664$, $C_\phi = 0.00127$). The solution $m \approx 6.14$ mol/kg pushes the Pitzer model near its recommended validity limit, where the $\exp(-2\sqrt{m})$ term in $B^\gamma$ becomes extremely small.

### Problem 8: Water-Butanol-NaCl LLE

A liquid-liquid equilibrium between aqueous and organic phases, modeled by NRTL ($\tau_{12} = 0.50$, $\tau_{21} = 4.50$, $\alpha = 0.40$). Adding 1.0 mol/kg NaCl to the aqueous phase shifts the equilibrium via the Setchenow effect ($\ln \gamma_{\text{BuOH}}^{\text{salted}} = \ln \gamma_{\text{BuOH}}^0 + k_s m_{\text{salt}}$, $k_s = 0.19$), reducing aqueous butanol solubility ("salting out"). Two equality constraints enforce equal chemical potentials for both butanol and water across phases.

### Problem 9: Saturated Brine VLE + SLE

Three coupled phase equilibria for the NaCl-water system:
- **SLE**: $\gamma_\pm^2 m^2 = K_{sp}$ (salt precipitation)
- **Water activity**: $a_w = \exp(-\phi \cdot 2m \cdot M_w)$ (osmotic coefficient from Pitzer)
- **VLE**: $p_w = a_w \cdot p_w^*$ (Raoult's law, $p_w^* = 3.169$ kPa at 25 °C)

The solution gives $m \approx 6.14$ mol/kg, $a_w \approx 0.753$, $p_w \approx 2.39$ kPa.

### Problem 10: Pitzer NaCl Parameter Fit

Fit three Pitzer parameters $[\beta_0, \beta_1, C_\phi]$ to 11 synthetic osmotic coefficient data points at molalities from 0.1 to 6.0. The osmotic coefficient is linear in these parameters, making the Hessian a constant Gauss-Newton approximation $H = 2 J^T J$. True parameters: $[0.0765, 0.2664, 0.00127]$.

### Problem 11: Multi-Salt Debye-Huckel Fit

Fit 8 ion-specific DH parameters ($a_i$, $\dot{b}_i$ for Na$^+$, K$^+$, Ca$^{2+}$, Cl$^-$) to 24 synthetic mean ionic activity coefficient data points across NaCl, KCl, and CaCl$_2$ at 8 molalities each. The challenge is parameter compensation: $a$ and $\dot{b}$ can trade off against each other, creating ridges in the objective landscape.

### Problem 12: eNRTL Temperature-Dependent Fit

Fit 4 parameters ($A_{ca}$, $B_{ca}$, $A_{wc}$, $B_{wc}$) of a temperature-dependent eNRTL model ($\tau = A + B/T$) to 32 synthetic data points (4 temperatures $\times$ 8 molalities). The $\exp(-0.2\tau)$ terms create a steep landscape with multiple local minima, making this the most difficult fitting problem in the suite.

### Problem 13: Seawater Speciation

The largest and most physically realistic problem. Fifteen species---

Na$^+$, K$^+$, Mg$^{2+}$, Ca$^{2+}$, Cl$^-$, SO$_4^{2-}$, HCO$_3^-$, CO$_3^{2-}$, H$^+$, OH$^-$, MgSO$_4$(aq), CaSO$_4$(aq), MgOH$^+$, NaSO$_4^-$, KSO$_4^-$

---are subject to 8 constraints (6 element balances + carbon balance + electroneutrality). Five ion-pair formation reactions ($K_{\text{MgSO}_4} = 10^{2.23}$, $K_{\text{CaSO}_4} = 10^{2.30}$, $K_{\text{MgOH}^+} = 10^{2.58}$, $K_{\text{NaSO}_4^-} = 10^{0.70}$, $K_{\text{KSO}_4^-} = 10^{0.85}$) plus carbonate and water equilibria create a fully coupled system. Concentrations span from $\sim 10^{-20}$ (CO$_3^{2-}$ at low pH) to $\sim 0.57$ mol/kg (Cl$^-$). Activity coefficients use DH for charged species and a Setchenow term ($\ln\gamma = 0.1 I$) for neutral ion pairs.

Standard seawater composition (mol/kg): Na = 0.4861, K = 0.01058, Mg = 0.05474, Ca = 0.01065, Cl = 0.5658, SO$_4$ = 0.02927, C = 0.002048.

## Benchmark Results

All problems solved at tolerance $10^{-6}$ with a maximum of 3000 iterations.

```
Electrolyte Thermodynamics Benchmark: ripopt vs ipopt
====================================================

Problem                     n   m |  ripopt obj  iter  time(s) |   ipopt obj  iter  time(s)
-------------------------------------------------------------------------------------------
--- Speciation / Chemical Equilibrium --- 
Water autoionization        1   0 |   1.9740e-7     8   0.0001 |   3.1004e-7     7   0.0018
CO2-water speciation        5   2 |  -6.9201e-3    12   0.0002 |  -6.9337e-3    28   0.0052
NaCl speciation             4   3 |  -4.8327e-1     7   0.0001 |  -4.8327e-1     7   0.0010
CaCl2+NaCl mixed            6   4 |  -7.7239e-1     9   0.0002 |  -7.7237e-1     9   0.0017
  status: ripopt=Acceptable
Phosphoric acid             6   2 |  -5.5258e-2    12   0.0001 |  -5.5312e-2     6   0.0011
--- Phase Equilibrium ---
HCl mean activity           1   0 |  2.6584e-17     7   0.0000 |  8.8924e-17     5   0.0008
NaCl solubility             1   0 |  1.1133e-15     5   0.0000 |  7.9738e-22     5   0.0008
BuOH-water LLE              2   2 |  7.8258e-10     6   0.0001 |  7.8258e-10     4   0.0008
Saturated brine             3   3 |    0.0000e0     7   0.0001 |    0.0000e0     4   0.0008
--- Parameter Fitting ---
Pitzer NaCl fit             3   0 |  2.2386e-17    24   0.0000 |  3.6776e-16     5   0.0009
Multi-salt DH fit           8   0 |  1.0009e-14    96   0.0001 |  2.9006e-10   142   0.0255
eNRTL T-dep fit             4   0 |  2.2467e-15    75   0.0001 |  3.7390e-12     8   0.0014
--- Scale-Up ---
Seawater speciation        15   8 |   -1.3483e0    22   0.0003 |   -1.3628e0    23   0.0060
  status: ipopt=Infeasible
-------------------------------------------------------------------------------------------
```

## Performance Summary

|                  | ripopt    | Ipopt                    |
|------------------|-----------|--------------------------|
| Problems solved  | **13/13** | 12/13                    |
| Total iterations | 283       | 253                      |
| Total wall time  | ~1.3 ms   | ~35 ms                   |
| Failures         | 0         | 1 (seawater: Infeasible) |

### Robustness

Both solvers handle the speciation, phase equilibrium, and parameter fitting categories well. The key differentiator is **Problem 13 (seawater speciation)**, where Ipopt declares infeasibility while ripopt converges to a physically correct solution (pH = 8.10, consistent with published seawater values). This problem combines the worst-case features of the suite: 15 tightly coupled variables, 8 constraints, divalent ions, ion pairs, and concentrations spanning 18 orders of magnitude.

The CaCl2+NaCl problem (Problem 4) is the only case where ripopt returns `Acceptable` rather than `Optimal`---the constraint violation ($1.1 \times 10^{-6}$) satisfies the acceptable tolerance but not the strict tolerance. Ipopt converges to `Optimal` on this problem with the same iteration count.

### Iteration Counts

On speciation and phase equilibrium problems, the solvers are comparable (typically 5-12 iterations each). The parameter fitting problems show the largest divergence:

- **Multi-salt DH fit**: ripopt uses 96 iterations (with numerical Hessian) to reach $f = 10^{-14}$; Ipopt uses 142 iterations but only reaches $f = 2.9 \times 10^{-10}$. ripopt finds a tighter optimum with fewer iterations.
- **eNRTL T-dependent fit**: Ipopt converges in 8 iterations vs. ripopt's 75, but both reach residuals well below $10^{-10}$. The difference likely reflects Ipopt's more aggressive barrier parameter strategy on this particular landscape.
- **Pitzer NaCl fit**: Ipopt converges in 5 iterations vs. ripopt's 24, reflecting the well-conditioned Gauss-Newton structure that Ipopt exploits more aggressively.

### Wall Time

ripopt is 10-30x faster per problem on this benchmark. This is partly architectural (no C FFI overhead, no Ipopt initialization cost) and partly reflects that these are very small problems (n $\leq$ 15) where per-iteration overhead dominates over linear algebra. The absolute times (0.1-0.3 ms for ripopt, 1-25 ms for Ipopt) are both negligible for single solves but would matter in inner loops of process simulators where thousands of flash calculations are performed.

### Solution Quality

Both solvers reach comparable objective values on all problems where both succeed. For the parameter fitting problems, ripopt generally achieves tighter residuals ($10^{-14}$ to $10^{-17}$) compared to Ipopt ($10^{-10}$ to $10^{-16}$), indicating slightly better convergence to the global minimum of these synthetic problems.

## Conclusions

1. **ripopt solves all 13 electrolyte thermodynamics problems**, including the challenging seawater speciation where Ipopt fails. This demonstrates robustness on problems with deep nonlinearities, extreme scaling, and tight coupling.

2. **The log-transformation strategy is essential.** Representing concentrations as $x_i = \ln m_i$ converts 18 orders of magnitude into a variable range of $\sim$[-46, 0], making the problem tractable for interior-point methods.

3. **Gibbs energy minimization** (rather than root-finding on the equilibrium equations) provides the solver with proper curvature information through the Hessian, enabling rapid convergence (typically 6-25 iterations for speciation problems).

4. **Small electrolyte problems favor ripopt's low-overhead design.** The sub-millisecond solve times would enable use in tight inner loops (e.g., flash calculations within process flowsheet solvers).

5. **Both solvers handle the standard problems comparably.** The differentiator is the hardest problem (seawater), where ripopt's robustness on ill-conditioned systems provides an advantage.