use crate::Thermodynamics::ChemEquilibrium::equilibrium_constraints::TemperatureBounds;
use crate::Thermodynamics::ChemEquilibrium::equilibrium_nonlinear::ReactionExtentError;
use crate::Thermodynamics::ChemEquilibrium::phase_boundary_validation::PurePhaseBoundaryStructuralTolerances;
use crate::Thermodynamics::ChemEquilibrium::pure_phase_ph_validation::{
PurePhasePhMaterializedState, PurePhasePhProblem, PurePhasePhSolverSettings,
};
use crate::Thermodynamics::ChemEquilibrium::real_pure_phase_fixtures::{
RealPurePhaseInventory, ResolvedRealPurePhaseFixture,
};
pub(crate) const BOUDOUARD_PH_PRESSURE_PA: f64 = 100_000.0;
pub(crate) const BOUDOUARD_PH_REFERENCE_TEMPERATURE_K: f64 = 700.0;
const BOUDOUARD_PH_TEMPERATURE_LOWER_K: f64 = 695.0;
const BOUDOUARD_PH_TEMPERATURE_UPPER_K: f64 = 705.0;
const MIN_INTERIOR_GRAPHITE_MOLES: f64 = 1e-6;
#[derive(Clone)]
pub(crate) struct BoudouardPhInteriorReference {
pub(crate) inventory: RealPurePhaseInventory,
pub(crate) problem: PurePhasePhProblem,
pub(crate) state: PurePhasePhMaterializedState,
}
impl BoudouardPhInteriorReference {
pub(crate) fn target_enthalpy(&self) -> f64 {
self.state.total_enthalpy
}
}
pub(crate) fn build_boudouard_ph_interior_reference(
fixture: &ResolvedRealPurePhaseFixture,
) -> Result<BoudouardPhInteriorReference, ReactionExtentError> {
let inventory = RealPurePhaseInventory::new(vec![1.19, 0.005], 0.0)?;
let bounds = TemperatureBounds::new(
BOUDOUARD_PH_TEMPERATURE_LOWER_K,
BOUDOUARD_PH_TEMPERATURE_UPPER_K,
)?;
let seed_problem = fixture.to_ph_problem(
&inventory,
BOUDOUARD_PH_PRESSURE_PA,
BOUDOUARD_PH_PRESSURE_PA,
0.0,
bounds,
)?;
seed_problem
.validate_strict_independent_family(PurePhaseBoundaryStructuralTolerances::default())?;
let inner = seed_problem.solve_inner_extent_at_temperature(
BOUDOUARD_PH_REFERENCE_TEMPERATURE_K,
PurePhasePhSolverSettings::default(),
)?;
let state = seed_problem
.materialize_state_at_extent(inner.extent, BOUDOUARD_PH_REFERENCE_TEMPERATURE_K)?;
if state.candidate_moles <= MIN_INTERIOR_GRAPHITE_MOLES {
return Err(ReactionExtentError::ValidationNotApplicable {
path: "real_boudouard_ph_interior_reference",
message: format!(
"local Boudouard scalar root at {} K has non-interior graphite amount {:e}",
BOUDOUARD_PH_REFERENCE_TEMPERATURE_K, state.candidate_moles
),
});
}
let problem = seed_problem.with_target_enthalpy(state.total_enthalpy)?;
Ok(BoudouardPhInteriorReference {
inventory,
problem,
state,
})
}