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
//! Typed construction and repository resolution for phase systems.
//!
//! This module is the boundary between user declarations and resolved
//! `SubsData` payloads. It does lookup/I-O and never evaluates or owns
//! thermodynamic cache state.

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

use crate::Thermodynamics::User_PhaseOrSolution::{CustomSubstance, PhaseOrSolution};
use crate::Thermodynamics::User_PhaseOrSolution2::OnePhase;
use crate::Thermodynamics::User_substances::{LibraryPriority, Phases, SubsData};
use crate::Thermodynamics::User_substances_error::{SubsDataError, SubsDataResult};
use crate::Thermodynamics::phase_layout::PhaseId;
use crate::Thermodynamics::thermo_lib_api::{ThermoData, ThermoLibraryError, ThermoRepository};
use std::sync::Arc;

use super::{PhasePhysicalState, PhaseSpec, ResolvedPhaseSystem};

/// Maps substances to their respective phases while maintaining order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubstancePhaseMapping {
    pub all_substances: Vec<String>,
    pub substance_to_phase: Vec<Option<String>>,
}

impl SubstancePhaseMapping {
    pub fn new(substances: Vec<String>, phases: Vec<Option<String>>) -> SubsDataResult<Self> {
        if substances.len() != phases.len() {
            return Err(SubsDataError::calculation_failed(
                "multiple",
                "substance-phase mapping",
                format!(
                    "substances length {} does not match phases length {}",
                    substances.len(),
                    phases.len()
                ),
            ));
        }
        Ok(Self {
            all_substances: substances,
            substance_to_phase: phases,
        })
    }

    pub fn get_phase_for_substance(&self, index: usize) -> Option<&Option<String>> {
        self.substance_to_phase.get(index)
    }
}

/// Compatibility input for callers which have not yet declared `PhaseSpec`s.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SubstancesContainer {
    SinglePhase(Vec<String>),
    MultiPhase(HashMap<String, Vec<String>>),
}

impl SubstancesContainer {
    /// Returns all unique substance names in deterministic order.
    pub fn get_all_substances(&self) -> Vec<String> {
        let mut substances = match self {
            Self::SinglePhase(substances) => substances.clone(),
            Self::MultiPhase(phase_substances) => {
                phase_substances.values().flatten().cloned().collect()
            }
        };
        substances.sort();
        substances.dedup();
        substances
    }

    /// Preserves duplicate components when one substance occurs in two phases.
    pub fn get_ordered_component_labels(&self) -> Vec<(Option<String>, String)> {
        match self {
            Self::SinglePhase(substances) => substances
                .iter()
                .cloned()
                .map(|substance| (None, substance))
                .collect(),
            Self::MultiPhase(phase_substances) => {
                let mut phase_names = phase_substances.keys().cloned().collect::<Vec<_>>();
                phase_names.sort();
                phase_names
                    .into_iter()
                    .flat_map(|phase_name| {
                        phase_substances
                            .get(&phase_name)
                            .into_iter()
                            .flatten()
                            .cloned()
                            .map(move |substance| (Some(phase_name.clone()), substance))
                    })
                    .collect()
            }
        }
    }

    pub fn get_ordered_substances(&self) -> Vec<String> {
        self.get_ordered_component_labels()
            .into_iter()
            .map(|(_, substance)| substance)
            .collect()
    }
}

/// Typed specification for building a thermodynamic system before resolution.
#[derive(Debug, Clone)]
pub struct SubstanceSystemSpec {
    phases: Vec<PhaseSpec>,
    library_priorities: Vec<String>,
    permitted_libraries: Vec<String>,
    explicit_search_instructions: Option<HashMap<String, String>>,
    search_in_nist: bool,
}

impl SubstanceSystemSpec {
    pub fn from_phases(phases: Vec<PhaseSpec>) -> Result<Self, SubstanceSystemFactoryError> {
        let spec = Self {
            phases,
            library_priorities: Vec::new(),
            permitted_libraries: Vec::new(),
            explicit_search_instructions: None,
            search_in_nist: false,
        };
        SubstanceSystemFactory::validate_spec(&spec)?;
        Ok(spec)
    }

    pub fn phases(&self) -> &[PhaseSpec] {
        &self.phases
    }

    pub fn builder(container: SubstancesContainer) -> SubstanceSystemSpecBuilder {
        SubstanceSystemSpecBuilder::new(container)
    }

    pub fn resolve(self) -> Result<CustomSubstance, SubstanceSystemFactoryError> {
        SubstanceSystemFactory::resolve_spec(self)
    }

    /// Resolves this specification against an explicitly supplied immutable
    /// catalog. All phase payloads share that same repository handle.
    pub fn resolve_with_repository(
        self,
        repository: Arc<ThermoRepository>,
    ) -> Result<CustomSubstance, SubstanceSystemFactoryError> {
        SubstanceSystemFactory::resolve_spec_with_repository(self, repository)
    }
}

/// Fluent compatibility builder for `SubstanceSystemSpec`.
#[derive(Debug, Clone)]
pub struct SubstanceSystemSpecBuilder {
    container: SubstancesContainer,
    phase_natures: Option<HashMap<String, Phases>>,
    library_priorities: Vec<String>,
    permitted_libraries: Vec<String>,
    explicit_search_instructions: Option<HashMap<String, String>>,
    search_in_nist: bool,
}

impl SubstanceSystemSpecBuilder {
    pub fn new(container: SubstancesContainer) -> Self {
        Self {
            container,
            phase_natures: None,
            library_priorities: Vec::new(),
            permitted_libraries: Vec::new(),
            explicit_search_instructions: None,
            search_in_nist: false,
        }
    }

    /// Supplies the physical state for every named phase in a multi-phase
    /// compatibility container. `MultiPhase` has no safe implicit state: a
    /// missing entry is rejected instead of silently becoming an ideal gas.
    /// `SinglePhase` remains the explicit ideal-gas convenience form.
    pub fn with_phase_natures(mut self, phase_natures: Option<HashMap<String, Phases>>) -> Self {
        self.phase_natures = phase_natures;
        self
    }

    pub fn with_library_priorities(mut self, library_priorities: Vec<String>) -> Self {
        self.library_priorities = library_priorities;
        self
    }

    pub fn with_permitted_libraries(mut self, permitted_libraries: Vec<String>) -> Self {
        self.permitted_libraries = permitted_libraries;
        self
    }

    pub fn with_explicit_search_instructions(
        mut self,
        explicit_search_instructions: Option<HashMap<String, String>>,
    ) -> Self {
        self.explicit_search_instructions = explicit_search_instructions;
        self
    }

    pub fn with_search_in_nist(mut self, search_in_nist: bool) -> Self {
        self.search_in_nist = search_in_nist;
        self
    }

    pub fn build(self) -> Result<SubstanceSystemSpec, SubstanceSystemFactoryError> {
        let phases = Self::legacy_container_to_phase_specs(self.container, self.phase_natures)?;
        let spec = SubstanceSystemSpec {
            phases,
            library_priorities: self.library_priorities,
            permitted_libraries: self.permitted_libraries,
            explicit_search_instructions: self.explicit_search_instructions,
            search_in_nist: self.search_in_nist,
        };
        SubstanceSystemFactory::validate_spec(&spec)?;
        Ok(spec)
    }

    fn legacy_container_to_phase_specs(
        container: SubstancesContainer,
        phase_natures: Option<HashMap<String, Phases>>,
    ) -> Result<Vec<PhaseSpec>, SubstanceSystemFactoryError> {
        match container {
            SubstancesContainer::SinglePhase(components) => {
                Ok(vec![PhaseSpec::ideal_gas(PhaseId::new(None), components)?])
            }
            SubstancesContainer::MultiPhase(phases) => {
                if phases.is_empty() {
                    return Err(SubstanceSystemFactoryError::InvalidSpecification {
                        field: "phases".to_string(),
                        reason: "a phase system must contain at least one phase".to_string(),
                    });
                }
                let natures = phase_natures.ok_or_else(|| {
                    SubstanceSystemFactoryError::InvalidSpecification {
                        field: "physical phase state".to_string(),
                        reason: "named multi-phase systems require an explicit physical state for every phase"
                            .to_string(),
                    }
                })?;
                for phase_name in phases.keys() {
                    if !natures.contains_key(phase_name) {
                        return Err(SubstanceSystemFactoryError::InvalidSpecification {
                            field: "physical phase state".to_string(),
                            reason: format!(
                                "missing physical state for declared phase '{}'",
                                phase_name
                            ),
                        });
                    }
                }
                for phase_name in natures.keys() {
                    if !phases.contains_key(phase_name) {
                        return Err(SubstanceSystemFactoryError::InvalidSpecification {
                            field: "physical phase state".to_string(),
                            reason: format!(
                                "physical state declared for unknown phase '{}'",
                                phase_name
                            ),
                        });
                    }
                }
                phases
                    .into_iter()
                    .map(|(name, components)| {
                        let id = PhaseId::new(Some(name.clone()));
                        let physical_state = natures.get(&name).copied().ok_or_else(|| {
                            SubstanceSystemFactoryError::InvalidSpecification {
                                field: "physical phase state".to_string(),
                                reason: format!(
                                    "missing physical state for declared phase '{}'",
                                    name
                                ),
                            }
                        })?;
                        match PhasePhysicalState::from(physical_state) {
                            PhasePhysicalState::Gas => PhaseSpec::ideal_gas(id, components),
                            physical_state => {
                                PhaseSpec::pure_condensed(id, components, physical_state)
                            }
                        }
                    })
                    .collect()
            }
        }
    }
}

/// Typed errors for phase-system specification and resolution.
#[derive(Debug)]
pub enum SubstanceSystemFactoryError {
    InvalidSpecification { field: String, reason: String },
    Repository(ThermoLibraryError),
    Resolution(SubsDataError),
}

impl fmt::Display for SubstanceSystemFactoryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidSpecification { field, reason } => {
                write!(f, "invalid specification in {}: {}", field, reason)
            }
            Self::Repository(err) => write!(f, "thermodynamic repository error: {}", err),
            Self::Resolution(err) => write!(f, "{}", err),
        }
    }
}

impl std::error::Error for SubstanceSystemFactoryError {}

impl From<SubsDataError> for SubstanceSystemFactoryError {
    fn from(value: SubsDataError) -> Self {
        Self::Resolution(value)
    }
}

/// Resolves typed phase specifications into ready-to-evaluate facades.
pub struct SubstanceSystemFactory;

impl SubstanceSystemFactory {
    pub(crate) fn validate_spec(
        spec: &SubstanceSystemSpec,
    ) -> Result<(), SubstanceSystemFactoryError> {
        if spec.phases.is_empty() {
            return Err(SubstanceSystemFactoryError::InvalidSpecification {
                field: "phases".to_string(),
                reason: "a phase system must contain at least one phase".to_string(),
            });
        }
        let mut seen_ids = HashSet::with_capacity(spec.phases.len());
        for phase in &spec.phases {
            phase.validate()?;
            if !seen_ids.insert(phase.id().clone()) {
                return Err(SubstanceSystemFactoryError::InvalidSpecification {
                    field: "phase id".to_string(),
                    reason: format!("phase {:?} is declared more than once", phase.id()),
                });
            }
        }
        Ok(())
    }

    fn apply_lookup_policy(
        subs_data: &mut SubsData,
        library_priorities: &[String],
        permitted_libraries: &[String],
        explicit_search_instructions: Option<&HashMap<String, String>>,
    ) {
        subs_data.set_multiple_library_priorities(
            library_priorities.to_vec(),
            LibraryPriority::Priority,
        );
        subs_data.set_multiple_library_priorities(
            permitted_libraries.to_vec(),
            LibraryPriority::Permitted,
        );
        if let Some(instructions) = explicit_search_instructions {
            subs_data.set_explicit_search_instructions(instructions.clone());
        }
    }

    fn resolve_subs_data(
        subs_data: &mut SubsData,
        search_in_nist: bool,
    ) -> Result<(), SubstanceSystemFactoryError> {
        subs_data.search_substances()?;
        if search_in_nist {
            subs_data.if_not_found_go_NIST()?;
        }
        subs_data.parse_all_thermal_coeffs()?;
        Ok(())
    }

    /// Resolves validated declarations into one immutable, layout-aligned system.
    pub fn resolve_phase_system(
        spec: SubstanceSystemSpec,
    ) -> Result<ResolvedPhaseSystem, SubstanceSystemFactoryError> {
        let repository = ThermoData::try_default_repository()
            .map_err(SubstanceSystemFactoryError::Repository)?;
        Self::resolve_phase_system_with_repository(spec, repository)
    }

    /// Resolves validated declarations against one injected immutable catalog.
    ///
    /// Every resulting `SubsData` owns query-local calculators and search state,
    /// but borrows the same read-only library payload through `Arc`.
    pub fn resolve_phase_system_with_repository(
        spec: SubstanceSystemSpec,
        repository: Arc<ThermoRepository>,
    ) -> Result<ResolvedPhaseSystem, SubstanceSystemFactoryError> {
        Self::validate_spec(&spec)?;
        let SubstanceSystemSpec {
            phases,
            library_priorities,
            permitted_libraries,
            explicit_search_instructions,
            search_in_nist,
        } = spec;
        let mut resolved_data = HashMap::with_capacity(phases.len());
        for phase in &phases {
            let phase_key = phase.id().as_option().clone();
            if phases.len() > 1 && phase_key.is_none() {
                return Err(SubstanceSystemFactoryError::InvalidSpecification {
                    field: "phase id".to_string(),
                    reason: "multi-phase systems require named phases".to_string(),
                });
            }
            let mut phase_data = SubsData::from_thermo_repository(Arc::clone(&repository));
            phase_data.substances = phase.components().to_vec();
            Self::apply_lookup_policy(
                &mut phase_data,
                &library_priorities,
                &permitted_libraries,
                explicit_search_instructions.as_ref(),
            );
            // A phase model carries physical context, but state-specific
            // library lookup remains opt-in. The compatibility projection is
            // retained for NIST fallback without restricting local records.
            phase_data.map_of_phases = phase.legacy_component_phase_map();
            Self::resolve_subs_data(&mut phase_data, search_in_nist)?;
            resolved_data.insert(phase_key, phase_data);
        }
        ResolvedPhaseSystem::new_with_nist_fallback_policy(phases, resolved_data, search_in_nist)
    }

    pub fn resolve_spec(
        spec: SubstanceSystemSpec,
    ) -> Result<CustomSubstance, SubstanceSystemFactoryError> {
        let repository = ThermoData::try_default_repository()
            .map_err(SubstanceSystemFactoryError::Repository)?;
        Self::resolve_spec_with_repository(spec, repository)
    }

    /// Resolves a facade against one explicit repository handle.
    pub fn resolve_spec_with_repository(
        spec: SubstanceSystemSpec,
        repository: Arc<ThermoRepository>,
    ) -> Result<CustomSubstance, SubstanceSystemFactoryError> {
        let resolved = Self::resolve_phase_system_with_repository(spec, repository)?;
        let is_single_phase = resolved.phase_specs().len() == 1
            && resolved.phase_specs()[0].id().as_option().is_none();
        if is_single_phase {
            let mut one_phase = OnePhase::new();
            one_phase.install_resolved_system(resolved)?;
            Ok(CustomSubstance::OnePhase(one_phase))
        } else {
            let mut phase_system = PhaseOrSolution::new();
            phase_system.install_resolved_system(resolved);
            Ok(CustomSubstance::PhaseOrSolution(phase_system))
        }
    }

    /// Compatibility adapter returning the legacy string error type.
    pub fn create_system(
        container: SubstancesContainer,
        physical_phase_natures: Option<HashMap<String, Phases>>,
        library_priorities: Vec<String>,
        permitted_libraries: Vec<String>,
        explicit_search_instructions: Option<HashMap<String, String>>,
        search_in_nist: bool,
    ) -> Result<CustomSubstance, String> {
        let spec = SubstanceSystemSpecBuilder::new(container)
            .with_phase_natures(physical_phase_natures)
            .with_library_priorities(library_priorities)
            .with_permitted_libraries(permitted_libraries)
            .with_explicit_search_instructions(explicit_search_instructions)
            .with_search_in_nist(search_in_nist)
            .build()
            .map_err(|error| error.to_string())?;
        Self::resolve_spec(spec).map_err(|error| error.to_string())
    }
}