# SPDX-License-Identifier: AGPL-3.0-only
# -*- coding: utf-8 -*-
"""Scientific equations for SMEW's nonlinear solves.
To change an equation, edit its ``*_residual`` function below. Functions with
one unknown return a value; larger systems fill ``out``. The matching
``*_equations`` functions pass the results to SciPy's ``fsolve``. Python uses
this file directly; compiled runs require a Cython rebuild after edits.
"""
import numpy as np
from numba import njit
# Number of unknowns in each vector system. Update alongside the residuals,
# initial guesses, and solution unpacking when adding or removing unknowns.
BIOGEOCHEM_N_UNKNOWNS = 16
TOTAL_TO_CEC_N_UNKNOWNS = 13
# p contains trial values; out receives residuals in place.
# Keep state indices and buffer sizes consistent with callers when adding unknowns.
# After edits, rebuild with `spin install-native` and check with `spin test`.
# Return one residual; shared by the SciPy wrapper and compiled cminpack callback.
[docs]
@njit(nogil=True, error_model="numpy")
def water_alk_residual(H, Alk, k1, k2, CO2_w, k_w):
"""Return prescribed minus calculated aqueous alkalinity at hydrogen ``H``.
Shared by rainwater equilibrium and the alternative soil hydrogen estimate.
Parameters after ``H`` are ``(Alk, k1, k2, CO2_w, k_w)`` in both solvers.
"""
return Alk - (
k1 * CO2_w / H
+ 2 * k1 * k2 * CO2_w / H**2
- H
+ k_w / H
)
# SciPy only: unpack p[0] and return the scalar residual to fsolve.
[docs]
@njit(nogil=True, error_model="numpy")
def water_alk_equations(p, Alk, k1, k2, CO2_w, k_w):
"""SciPy adapter for :func:`water_alk_residual`; ``p`` contains only ``H``."""
return water_alk_residual(p[0], Alk, k1, k2, CO2_w, k_w)
# Fill out with 16 residuals; used by the SciPy wrapper and compiled cminpack callback.
[docs]
@njit(nogil=True, error_model="numpy")
def biogeochem_residual(
p,
Alk_tot,
n,
Zr,
s,
IC_tot,
k1,
k2,
k_H,
k_w,
CEC_tot,
conv_Al,
Al_tot,
K1,
K2,
K3,
K4,
Mg_tot,
Ca_tot,
Na_tot,
K_tot,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
out,
):
"""Main equilibrium residuals for aqueous/adsorbed species.
State and residual order: Alk, CO2_w, H, R_alk, Al_w, Al, Mg, Ca, Na, K, f_Al, f_Mg, f_Na, f_K, f_H, f_Ca.
"""
Alk = p[0]
CO2_w = p[1]
H = p[2]
R_alk = p[3]
Al_w = p[4]
Al = p[5]
Mg = p[6]
Ca = p[7]
Na = p[8]
K = p[9]
f_Al = p[10]
f_Mg = p[11]
f_Na = p[12]
f_K = p[13]
f_H = p[14]
f_Ca = p[15]
# Precompute
nZrs1000 = n * Zr * s * 1000
out[0] = (Alk_tot - R_alk) - Alk*nZrs1000
out[1] = IC_tot - (CO2_w*(1 + k1/H + k2*k1/H**2)*s + (CO2_w/k_H)*(1 - s)) * (n*Zr*1000)
out[2] = (k1*CO2_w/H + 2*k1*k2*CO2_w/H**2 - H + k_w/H) - Alk
out[3] = R_alk - (f_Mg + f_Ca + f_Na + f_K)*CEC_tot
out[4] = Al_w*nZrs1000 + (f_Al/3)*CEC_tot*conv_Al - Al_tot
out[5] = Al - (H**4 / (H**4 + H**3*K1 + H**2*K1*K2 + H*K1*K2*K3 + K1*K2*K3*K4))*Al_w
out[6] = Mg*nZrs1000 + f_Mg/2*CEC_tot - Mg_tot
out[7] = Ca*nZrs1000 + f_Ca/2*CEC_tot - Ca_tot
out[8] = Na*nZrs1000 + f_Na*CEC_tot - Na_tot
out[9] = K*nZrs1000 + f_K*CEC_tot - K_tot
out[10] = f_Al - (Al/conv_Al)*(f_Ca**3 / (K_Ca_Al * Ca**3))**0.5
out[11] = f_Mg - Mg*(f_Ca / (K_Ca_Mg*Ca))
out[12] = f_Na - Na*(f_Ca / (K_Ca_Na*Ca))**0.5
out[13] = f_K - K*(f_Ca / (K_Ca_K*Ca))**0.5
out[14] = f_H - H*(f_Ca / (K_Ca_H*Ca))**0.5
out[15] = 1 - (f_Ca + f_Al + f_Mg + f_Na + f_K + f_H)
return 0
# SciPy only: allocate out and return the 16 residuals as a tuple to fsolve.
[docs]
@njit(nogil=True, error_model="numpy")
def biogeochem_equations(
p,
Alk_tot,
n,
Zr,
s,
IC_tot,
k1,
k2,
k_H,
k_w,
CEC_tot,
conv_Al,
Al_tot,
K1,
K2,
K3,
K4,
Mg_tot,
Ca_tot,
Na_tot,
K_tot,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
):
out = np.empty(BIOGEOCHEM_N_UNKNOWNS)
biogeochem_residual(
p,
Alk_tot,
n,
Zr,
s,
IC_tot,
k1,
k2,
k_H,
k_w,
CEC_tot,
conv_Al,
Al_tot,
K1,
K2,
K3,
K4,
Mg_tot,
Ca_tot,
Na_tot,
K_tot,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
out,
)
return (
out[0], out[1], out[2], out[3],
out[4], out[5], out[6], out[7],
out[8], out[9], out[10], out[11],
out[12], out[13], out[14], out[15],
)
# Return one residual; shared by the SciPy wrapper and compiled cminpack callback.
[docs]
@njit(nogil=True, error_model="numpy")
def cec_calcium_residual(
f_Ca,
Al,
conv_Al,
Ca,
Mg,
Na,
K,
H,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
):
return 1 - (
f_Ca
+ (Al/conv_Al)*(f_Ca**3 / (K_Ca_Al*Ca**3))**0.5
+ Mg*(f_Ca / (K_Ca_Mg*Ca))
+ Na*(f_Ca / (K_Ca_Na*Ca))**0.5
+ K*(f_Ca / (K_Ca_K*Ca))**0.5
+ H*(f_Ca / (K_Ca_H*Ca))**0.5
)
# SciPy only: unpack p[0] and return the scalar residual to fsolve.
[docs]
@njit(nogil=True, error_model="numpy")
def cec_calcium_equation(
p,
Al,
conv_Al,
Ca,
Mg,
Na,
K,
H,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
):
"""CEC saturation residual for the calcium fraction ``p[0]``."""
return cec_calcium_residual(
p[0],
Al,
conv_Al,
Ca,
Mg,
Na,
K,
H,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H
)
# Return one residual; shared by the SciPy wrapper and compiled cminpack callback.
[docs]
@njit(nogil=True, error_model="numpy")
def f_cec_alk_to_conc_residual(
Ca,
f_Ca,
K_Ca_Mg,
f_Mg,
K_Ca_Na,
f_Na,
K_Ca_K,
f_K,
Alk_in,
):
"""
Solvability equation: 0 = 2*Mg + 2*Ca + Na + K - Alk_in
"""
return (
2*((Ca/f_Ca)*K_Ca_Mg*f_Mg) # 2*Mg
+ 2*Ca
+ ((Ca/f_Ca)*K_Ca_Na)**0.5*f_Na # Na
+ ((Ca/f_Ca)*K_Ca_K)**0.5*f_K # K
- Alk_in
)
# SciPy only: unpack p[0] and return the scalar residual to fsolve.
[docs]
@njit(nogil=True, error_model="numpy")
def f_cec_alk_to_conc_equation(
p,
f_Ca,
K_Ca_Mg,
f_Mg,
K_Ca_Na,
f_Na,
K_Ca_K,
f_K,
Alk_in,
):
"""
Solvability equation 0 = 2*Mg + 2*Ca + Na + K - Alk_in for the calcium ``p[0]``.
"""
return f_cec_alk_to_conc_residual(
p[0], # Ca
f_Ca,
K_Ca_Mg,
f_Mg,
K_Ca_Na,
f_Na,
K_Ca_K,
f_K,
Alk_in,
)
# Fill out with 13 residuals; used by the SciPy wrapper and compiled cminpack callback.
[docs]
@njit(nogil=True, error_model="numpy")
def total_to_cec_residual(
p,
H,
n,
Zr,
s,
CEC_tot,
conv_Al,
K1,
K2,
K3,
K4,
Ca_tot,
Mg_tot,
K_tot,
Na_tot,
f_acid,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
out,
):
"""Residuals for the 13 initial concentrations and CEC fractions."""
Al_w = p[0]
Al = p[1]
Al_tot = p[2]
Mg = p[3]
Ca = p[4]
Na = p[5]
K = p[6]
f_Mg = p[7]
f_Na = p[8]
f_K = p[9]
f_Ca = p[10]
f_Al = p[11]
f_H = p[12]
n_Zr_s_1000 = n*Zr*s*1000
out[0] = Al_w*n_Zr_s_1000 + (f_Al/3)*CEC_tot*conv_Al - Al_tot
out[1] = Al - (H**4 / (H**4 + H**3*K1 + H**2*K1*K2 + H*K1*K2*K3 + K1*K2*K3*K4))*Al_w
out[2] = Mg*n_Zr_s_1000 + (f_Mg/2)*CEC_tot - Mg_tot
out[3] = Ca*n_Zr_s_1000 + (f_Ca/2)*CEC_tot - Ca_tot
out[4] = Na*n_Zr_s_1000 + f_Na*CEC_tot - Na_tot
out[5] = K*n_Zr_s_1000 + f_K*CEC_tot - K_tot
out[6] = f_Al - (Al/conv_Al)*(f_Ca**3 / (K_Ca_Al*Ca**3))**0.5
out[7] = f_H - H*(f_Ca / (K_Ca_H*Ca))**0.5
out[8] = f_H + f_Al - f_acid
out[9] = f_Mg - Mg*(f_Ca / (K_Ca_Mg*Ca))
out[10] = f_Na - Na*(f_Ca / (K_Ca_Na*Ca))**0.5
out[11] = f_K - K*(f_Ca / (K_Ca_K*Ca))**0.5
out[12] = 1 - (f_Ca + f_Al + f_Mg + f_Na + f_K + f_H)
return 0
# SciPy only: allocate out and return the 13 residuals as a tuple to fsolve.
[docs]
@njit(nogil=True, error_model="numpy")
def total_to_cec_equations(
p,
H,
n,
Zr,
s,
CEC_tot,
conv_Al,
K1,
K2,
K3,
K4,
Ca_tot,
Mg_tot,
K_tot,
Na_tot,
f_acid,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
):
out = np.empty(TOTAL_TO_CEC_N_UNKNOWNS)
total_to_cec_residual(
p,
H,
n,
Zr,
s,
CEC_tot,
conv_Al,
K1,
K2,
K3,
K4,
Ca_tot,
Mg_tot,
K_tot,
Na_tot,
f_acid,
K_Ca_Al,
K_Ca_Mg,
K_Ca_Na,
K_Ca_K,
K_Ca_H,
out
)
return (
out[0], out[1], out[2], out[3],
out[4], out[5], out[6], out[7],
out[8], out[9], out[10], out[11],
out[12],
)