KiThe 0.3.6

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Typed inputs and pure evaluation helpers for phase thermodynamics.
//!
//! MAIN DATA STRUCTURES:
//!
//! ThermoEvaluationConditions - shared temperature and pressure for a numeric evaluation
//! PhaseComposition - typed phase composition payload for a numeric evaluation
//!
//!
//!IERARCHY:
//!                              NumericThermoCacheContext
//!                             /
//!                         PhaseEvaluationRequest   
//!                         /                     \
//! ThermoEvaluationConditions                   PhaseComposition
//!
//! This module owns the request boundary between a resolved phase system and
//! raw `SubsData` calculators.  It intentionally does not own cache state or
//! facade types: the same validated request can be evaluated by either a
//! single-phase or multi-phase view.
//!

use std::collections::{HashMap, HashSet};

use crate::Thermodynamics::User_substances::{Phases, SubsData};
use crate::Thermodynamics::User_substances_error::{SubsDataError, SubsDataResult};
use crate::Thermodynamics::phase_layout::{SystemLayout, ordered_phase_keys};

/// Typed phase composition payload used internally instead of anonymous tuples.
/// `total_amount` is the phase total (`Np`), while `component_amounts` keeps
/// the ordered mole vector for the phase components when it is available.
#[derive(Clone, Debug, PartialEq)]
pub struct PhaseComposition {
    total_amount: Option<f64>,
    component_amounts: Option<Vec<f64>>,
}

impl PhaseComposition {
    pub fn new(total_amount: Option<f64>, component_amounts: Option<Vec<f64>>) -> Self {
        Self {
            total_amount,
            component_amounts,
        }
    }

    pub fn try_new_numeric(
        total_amount: Option<f64>,
        component_amounts: Option<Vec<f64>>,
        phase_name: &Option<String>,
        operation: &str,
    ) -> SubsDataResult<Self> {
        let composition = Self::new(total_amount, component_amounts);
        composition.validate_numeric(phase_name, operation)?;
        Ok(composition)
    }

    pub fn into_legacy(self) -> (Option<f64>, Option<Vec<f64>>) {
        (self.total_amount, self.component_amounts)
    }

    pub fn total_amount(&self) -> Option<f64> {
        self.total_amount
    }

    pub fn component_amounts(&self) -> Option<&[f64]> {
        self.component_amounts.as_deref()
    }

    pub(crate) fn component_amounts_owned(&self) -> Option<Vec<f64>> {
        self.component_amounts.clone()
    }

    /// Validates a numeric phase composition before Gibbs/entropy evaluation.
    /// The phase total and every component amount must be finite and
    /// non-negative, and the total must match the summed component amounts.
    pub fn validate_numeric(
        &self,
        phase_name: &Option<String>,
        operation: &str,
    ) -> SubsDataResult<()> {
        let phase_label = phase_name.clone().unwrap_or_else(|| "None".to_string());
        let total = self.total_amount.ok_or_else(|| {
            SubsDataError::calculation_failed(
                phase_label.clone(),
                operation,
                "missing phase total amount",
            )
        })?;
        if !total.is_finite() || total <= 0.0 {
            return Err(SubsDataError::calculation_failed(
                phase_label.clone(),
                operation,
                format!("invalid phase total amount {}", total),
            ));
        }

        let components = self.component_amounts.as_ref().ok_or_else(|| {
            SubsDataError::calculation_failed(
                phase_label.clone(),
                operation,
                "missing component amount vector",
            )
        })?;
        if components.is_empty() {
            return Err(SubsDataError::calculation_failed(
                phase_label.clone(),
                operation,
                "empty component amount vector",
            ));
        }

        let mut sum = 0.0;
        for value in components {
            if !value.is_finite() || *value < 0.0 {
                return Err(SubsDataError::calculation_failed(
                    phase_label.clone(),
                    operation,
                    format!("invalid component amount {}", value),
                ));
            }
            sum += value;
        }

        let tolerance = 1e-9_f64.max(total.abs() * 1e-9_f64);
        if (sum - total).abs() > tolerance {
            return Err(SubsDataError::calculation_failed(
                phase_label,
                operation,
                format!("phase total {} does not match component sum {}", total, sum),
            ));
        }
        Ok(())
    }
}

/// Physical conditions shared by a numeric phase-property evaluation.
///
/// Keeping temperature and pressure together prevents validating one input
/// while silently forwarding an invalid companion value to a calculator.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ThermoEvaluationConditions {
    temperature: f64,
    pressure: f64,
}

impl ThermoEvaluationConditions {
    pub fn new(temperature: f64, pressure: f64) -> SubsDataResult<Self> {
        if !temperature.is_finite() || temperature <= 0.0 {
            return Err(SubsDataError::InvalidTemperature(temperature));
        }
        if !pressure.is_finite() || pressure <= 0.0 {
            return Err(SubsDataError::InvalidPhysicalValue {
                field: "pressure".to_string(),
                value: pressure,
            });
        }
        Ok(Self {
            temperature,
            pressure,
        })
    }

    pub fn temperature(self) -> f64 {
        self.temperature
    }

    pub fn pressure(self) -> f64 {
        self.pressure
    }
}

/// Complete input to one numeric phase-property evaluation.
///
/// Temperature, pressure, and phase compositions are one logical request:
/// separating them made it too easy to evaluate with one composition and
/// publish cache provenance for another.
#[derive(Clone, Debug, PartialEq)]
pub struct PhaseEvaluationRequest {
    conditions: ThermoEvaluationConditions,
    compositions: HashMap<Option<String>, PhaseComposition>,
}

impl PhaseEvaluationRequest {
    pub fn new(
        conditions: ThermoEvaluationConditions,
        compositions: HashMap<Option<String>, PhaseComposition>,
    ) -> Self {
        Self {
            conditions,
            compositions,
        }
    }

    pub fn from_numeric(
        temperature: f64,
        pressure: f64,
        compositions: HashMap<Option<String>, PhaseComposition>,
    ) -> SubsDataResult<Self> {
        Ok(Self::new(
            ThermoEvaluationConditions::new(temperature, pressure)?,
            compositions,
        ))
    }

    pub fn conditions(&self) -> ThermoEvaluationConditions {
        self.conditions
    }

    pub fn compositions(&self) -> &HashMap<Option<String>, PhaseComposition> {
        &self.compositions
    }
}

/// Provenance for a cached numeric phase-property result.
///
/// A numeric result is reusable only for the exact physical conditions and
/// phase compositions that produced it, and only while the resolved phase
/// configuration is unchanged.
#[derive(Clone, Debug, PartialEq)]
pub struct NumericThermoCacheContext {
    request: PhaseEvaluationRequest,
    configuration_revision: usize,
}

impl NumericThermoCacheContext {
    pub(crate) fn new(request: PhaseEvaluationRequest, configuration_revision: usize) -> Self {
        Self {
            request,
            configuration_revision,
        }
    }

    pub fn conditions(&self) -> ThermoEvaluationConditions {
        self.request.conditions()
    }

    pub fn compositions(&self) -> &HashMap<Option<String>, PhaseComposition> {
        self.request.compositions()
    }

    pub fn request(&self) -> &PhaseEvaluationRequest {
        &self.request
    }

    pub fn configuration_revision(&self) -> usize {
        self.configuration_revision
    }

    pub(crate) fn matches_request(
        &self,
        request: &PhaseEvaluationRequest,
        configuration_revision: usize,
    ) -> bool {
        self.request == *request && self.configuration_revision == configuration_revision
    }

    pub(crate) fn matches_parts(
        &self,
        conditions: ThermoEvaluationConditions,
        compositions: &HashMap<Option<String>, PhaseComposition>,
        configuration_revision: usize,
    ) -> bool {
        self.request.conditions() == conditions
            && self.request.compositions() == compositions
            && self.configuration_revision == configuration_revision
    }
}

pub(crate) fn typed_phase_composition_for<'a>(
    compositions: &'a HashMap<Option<String>, PhaseComposition>,
    phase_name: &Option<String>,
) -> SubsDataResult<&'a PhaseComposition> {
    compositions
        .get(phase_name)
        .ok_or_else(|| SubsDataError::MissingData {
            field: "phase data".to_string(),
            substance: phase_name.clone().unwrap_or_else(|| "None".to_string()),
        })
}

/// Validates the complete numeric request before any phase calculator runs.
///
/// The supplied phase keys must exactly match the resolved system and each
/// amount vector must follow that phase's declared component order. This
/// front-loads failure so a later malformed phase cannot alter an earlier one.
pub(crate) fn validate_phase_evaluation_request(
    expected_components: impl IntoIterator<Item = (Option<String>, usize)>,
    conditions: ThermoEvaluationConditions,
    compositions: &HashMap<Option<String>, PhaseComposition>,
    operation: &str,
) -> SubsDataResult<()> {
    let expected = expected_components.into_iter().collect::<HashMap<_, _>>();
    let supplied = compositions.keys().cloned().collect::<HashSet<_>>();
    let declared = expected.keys().cloned().collect::<HashSet<_>>();
    if supplied != declared {
        return Err(SubsDataError::calculation_failed(
            "phase system",
            operation,
            "composition phases must exactly match the resolved phase set",
        ));
    }

    for (phase, component_count) in expected {
        let composition = typed_phase_composition_for(compositions, &phase)?;
        composition.validate_numeric(&phase, operation)?;
        let actual_count = composition.component_amounts().map_or(0, <[f64]>::len);
        if actual_count != component_count {
            return Err(SubsDataError::calculation_failed(
                phase.clone().unwrap_or_else(|| "None".to_string()),
                operation,
                format!(
                    "component amount vector has length {}; expected {}",
                    actual_count, component_count
                ),
            ));
        }
    }

    let _ = conditions;
    Ok(())
}

/// Read-only bridge over the still transitional raw `SubsData` storage.
///
/// `OnePhase` keeps a direct payload while `PhaseOrSolution` currently keeps
/// a phase-keyed map because equilibrium consumers still mutate those legacy
/// fields. New algorithms receive this view and never publish calculator
/// state back into either facade.
#[derive(Clone, Copy)]
pub(crate) enum PhaseDataView<'a> {
    Single(&'a SubsData),
    Multi(&'a HashMap<Option<String>, SubsData>),
}

impl<'a> PhaseDataView<'a> {
    pub(crate) fn single(data: &'a SubsData) -> Self {
        Self::Single(data)
    }

    pub(crate) fn multi(data: &'a HashMap<Option<String>, SubsData>) -> Self {
        Self::Multi(data)
    }

    pub(crate) fn layout(self) -> SystemLayout {
        match self {
            Self::Single(data) => SystemLayout::from_single_phase(&data.substances),
            Self::Multi(data) => SystemLayout::from_phase_map(data),
        }
    }

    fn expected_component_counts(self) -> Vec<(Option<String>, usize)> {
        match self {
            Self::Single(data) => vec![(None, data.substances.len())],
            Self::Multi(data) => data
                .iter()
                .map(|(phase, data)| (phase.clone(), data.substances.len()))
                .collect(),
        }
    }

    /// Evaluation receives a private mutable copy because `SubsData` still
    /// prepares coefficient caches internally. This preserves the public
    /// `&self` evaluation contract during the migration.
    fn to_owned_phase_map(self) -> HashMap<Option<String>, SubsData> {
        match self {
            Self::Single(data) => HashMap::from([(None, data.clone())]),
            Self::Multi(data) => data.clone(),
        }
    }

    /// Rejects zero gas-component amounts before the raw calculators evaluate
    /// their chemical-potential correction `ln(n_i / Np)`.
    ///
    /// A zero amount remains meaningful for a condensed component, but a
    /// component-wise ideal-gas chemical potential is undefined at zero mole
    /// fraction.  The legacy calculator also treats a missing phase entry as
    /// gas, so this validation deliberately preserves that established rule.
    fn validate_gas_composition_domain(
        self,
        compositions: &HashMap<Option<String>, PhaseComposition>,
        operation: &str,
    ) -> SubsDataResult<()> {
        let validate_phase = |phase_name: &Option<String>, subs_data: &SubsData| {
            let composition = typed_phase_composition_for(compositions, phase_name)?;
            let component_amounts = composition.component_amounts().ok_or_else(|| {
                SubsDataError::calculation_failed(
                    phase_name.clone().unwrap_or_else(|| "None".to_string()),
                    operation,
                    "missing component amount vector",
                )
            })?;

            for (substance, amount) in subs_data.substances.iter().zip(component_amounts) {
                let uses_ideal_gas_correction = matches!(
                    subs_data.map_of_phases.get(substance),
                    None | Some(Some(Phases::Gas))
                );
                if uses_ideal_gas_correction && *amount == 0.0 {
                    return Err(SubsDataError::calculation_failed(
                        substance.clone(),
                        operation,
                        format!(
                            "zero component amount in phase {:?} is outside the ideal-gas chemical-potential domain",
                            phase_name
                        ),
                    ));
                }
            }
            Ok(())
        };

        match self {
            Self::Single(data) => validate_phase(&None, data),
            Self::Multi(data) => {
                for phase_name in ordered_phase_keys(data) {
                    let subs_data =
                        data.get(&phase_name)
                            .ok_or_else(|| SubsDataError::MissingData {
                                field: "resolved phase data".to_string(),
                                substance: phase_name.clone().unwrap_or_else(|| "None".to_string()),
                            })?;
                    validate_phase(&phase_name, subs_data)?;
                }
                Ok(())
            }
        }
    }
}

/// Numeric property family evaluated by the shared raw-data bridge.
#[derive(Clone, Copy)]
pub(crate) enum NumericPhaseProperty {
    Gibbs,
    Entropy,
}

impl NumericPhaseProperty {
    fn operation_name(self) -> &'static str {
        match self {
            Self::Gibbs => "Gibbs free energy",
            Self::Entropy => "entropy",
        }
    }
}

/// Evaluates one numeric property over either a single or multi-phase view.
/// Validation intentionally happens before the first calculator runs.
pub(crate) fn evaluate_numeric_phase_property(
    data: PhaseDataView<'_>,
    request: &PhaseEvaluationRequest,
    property: NumericPhaseProperty,
) -> SubsDataResult<HashMap<Option<String>, HashMap<String, f64>>> {
    validate_phase_evaluation_request(
        data.expected_component_counts(),
        request.conditions(),
        request.compositions(),
        property.operation_name(),
    )?;
    data.validate_gas_composition_domain(request.compositions(), property.operation_name())?;

    let mut phase_data = data.to_owned_phase_map();
    let mut values = HashMap::with_capacity(phase_data.len());
    for phase_name in ordered_phase_keys(&phase_data) {
        let subsdata =
            phase_data
                .get_mut(&phase_name)
                .ok_or_else(|| SubsDataError::MissingData {
                    field: "resolved phase data".to_string(),
                    substance: phase_name.clone().unwrap_or_else(|| "None".to_string()),
                })?;
        let composition = typed_phase_composition_for(request.compositions(), &phase_name)?;
        let phase_values = match property {
            NumericPhaseProperty::Gibbs => subsdata.calc_dG_for_one_phase(
                request.conditions().pressure(),
                request.conditions().temperature(),
                composition.component_amounts_owned(),
                composition.total_amount(),
            )?,
            NumericPhaseProperty::Entropy => subsdata.calculate_S_for_one_phase(
                request.conditions().pressure(),
                request.conditions().temperature(),
                composition.component_amounts_owned(),
                composition.total_amount(),
            )?,
        };
        values.insert(phase_name, phase_values);
    }
    Ok(values)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn request_with_zero_first_component() -> PhaseEvaluationRequest {
        PhaseEvaluationRequest::new(
            ThermoEvaluationConditions::new(300.0, 101_325.0).unwrap(),
            HashMap::from([(
                None,
                PhaseComposition::try_new_numeric(Some(1.0), Some(vec![0.0, 1.0]), &None, "test")
                    .unwrap(),
            )]),
        )
    }

    fn phase_data(phase: Option<Phases>) -> SubsData {
        let mut data = SubsData::new();
        data.substances = vec!["A".to_string(), "B".to_string()];
        data.map_of_phases.insert("A".to_string(), phase);
        data.map_of_phases.insert("B".to_string(), phase);
        data
    }

    #[test]
    fn zero_gas_component_is_rejected_before_numeric_calculation() {
        let data = phase_data(Some(Phases::Gas));
        let error = evaluate_numeric_phase_property(
            PhaseDataView::single(&data),
            &request_with_zero_first_component(),
            NumericPhaseProperty::Gibbs,
        )
        .expect_err("zero ideal-gas amount must not reach ln(n_i / Np)");

        assert!(
            error
                .to_string()
                .contains("ideal-gas chemical-potential domain"),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn zero_condensed_component_remains_valid_at_the_phase_boundary() {
        let data = phase_data(Some(Phases::Solid));
        PhaseDataView::single(&data)
            .validate_gas_composition_domain(
                request_with_zero_first_component().compositions(),
                "test",
            )
            .expect("zero condensed amount is outside the ideal-gas domain and remains valid");
    }
}