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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Activity models for chemical species in the canonical equilibrium solver.
//!
//! # Purpose
//!
//! This module defines the **activity model** for each thermodynamic phase in
//! the equilibrium calculation. The activity `a_i` relates the chemical potential
//! to the standard state:
//!
//! ```text
//! μ_i(T, P, x) = μ_i°(T) + R·T·ln(a_i)
//! ```
//!
//! The module provides two models: ideal gas and ideal solution. Both are used
//! by the residual and Jacobian generators in [`equilibrium_log_moles`](super::equilibrium_log_moles)
//! and by the symbolic engine in [`equilibrium_rst_backend`](super::equilibrium_rst_backend).
//!
//! # Physical and Mathematical Background
//!
//! ## Ideal Gas
//!
//! For an ideal gas mixture:
//!
//! ```text
//! a_i = x_i · P / P0 = (n_i / N_phase) · (P / P0)
//! ln(a_i) = ln(n_i) - ln(N_phase) + ln(P / P0)
//! ```
//!
//! where `x_i` is the mole fraction, `P` is the system pressure, and `P0` is
//! the reference pressure (typically 1 atm = 101325 Pa).
//!
//! ## Ideal Solution
//!
//! For an ideal condensed solution:
//!
//! ```text
//! a_i = x_i = n_i / N_phase
//! ln(a_i) = ln(n_i) - ln(N_phase)
//! ```
//!
//! For a pure condensed phase, `x_i = 1` and `ln(a_i) = 0`.
//!
//! ## Phase Offset
//!
//! The `log_phase_offset` method returns the pressure-dependent part:
//!
//! - Ideal gas: `ln(P / P0)`
//! - Ideal solution: `0.0`
//!
//! This offset is **constant with respect to log-mole coordinates**, so the
//! analytical Jacobian differentiates only the `ln(n_i) - ln(N_phase)` terms.
//!
//! # Key Structures
//!
//! | Structure | Role |
//! |-----------|------|
//! | [`PhaseActivityModel`] | Enum: IdealGas or IdealSolution |
//!
//! # Key Functions
//!
//! | Function | Purpose |
//! |----------|---------|
//! | [`PhaseActivityModel::log_activity`] | Full log-activity from physical moles |
//! | [`PhaseActivityModel::log_phase_offset`] | Pressure-dependent offset only |
//! | [`phase_activity_models`] | Maps phase descriptors to activity models |
//!
//! # Dataflow
//!
//! ```text
//! Phase descriptor (from EquilibriumLogMoles or EquilibriumProblem)
//! │
//! v
//! PhaseActivityModel::log_phase_offset(P, P0)
//! ├── IdealGas → ln(P / P0)
//! └── IdealSolution → 0.0
//! │
//! v
//! PhaseActivityModel::log_activity(n_i, N_phase, P, P0)
//! ├── Validates inputs (finite, positive)
//! ├── Computes ln(n_i / N_phase) + phase_offset
//! └── Returns ln(a_i)
//! │
//! v
//! Used by: evaluate_equilibrium_logmole_residual()
//! Used by: equilibrium_logmole_residual2() (deprecated)
//! Used by: RST symbolic engine
//! ```
//!
//! # Examples
//!
//! ```rust
//! use KiThe::Thermodynamics::ChemEquilibrium::equilibrium_activity::PhaseActivityModel;
//!
//! let model = PhaseActivityModel::IdealGas;
//! let offset = model.log_phase_offset(101325.0, 101325.0).unwrap();
//! assert_eq!(offset, 0.0); // P == P0
//!
//! let activity = model.log_activity(0.5, 1.0, 101325.0, 101325.0).unwrap();
//! assert!((activity - (-0.693)).abs() < 1e-3); // ln(0.5)
//! ```
//!
//! # Non-obvious Details
//!
//! - Both `log_activity` and `log_phase_offset` validate their inputs and return
//! [`ReactionExtentError::InvalidConditions`] for non-finite or non-positive values.
//! - The activity model is **per-phase**, not per-species. All species in the same
//! phase share the same activity model.
//! - For pure condensed phases (IdealSolution with one species), `log_activity`
//! returns `0.0` because `n_i = N_phase` and the phase offset is zero.
//!
//! # Related Modules
//!
//! - [`equilibrium_log_moles`](super::equilibrium_log_moles) — residual/Jacobian evaluation
//! - [`equilibrium_rst_backend`](super::equilibrium_rst_backend) — symbolic backend
//! - [`equilibrium_problem`](super::equilibrium_problem) — problem definition
//!
use cratePhase;
use crateReactionExtentError;
/// Supported activity correction for one declared phase.
/// Builds models in declared phase order.