[Thermo] Add functions to save/restore state excluding composition

This commit is contained in:
Ray Speth
2025-09-19 06:50:02 -04:00
committed by Bryan Weber
parent 69e974cc07
commit 30495c415d
2 changed files with 61 additions and 16 deletions
+24
View File
@@ -298,6 +298,30 @@ public:
//! (such as "TD", "TP", "SV").
virtual vector<string> partialStates() const;
//! Get the size of the partial state vector of the phase.
//! The partial state vector excludes composition. Vectors of this size are used by
//! savePartialState() and restorePartialState().
//! @since New in %Cantera 3.2
virtual size_t partialStateSize() const { return 2; }
//! Save the current thermodynamic state of the phase, excluding composition.
//! The default implementation corresponds to the default implementation of
//! nativeState().
//! @param lenstate Length of the state array. Must be >= partialStateSize().
//! @param[out] state Array of state variables, in the order defined by
//! nativeState().
//! @since New in %Cantera 3.2
virtual void savePartialState(size_t lenstate, double* state) const;
//! Set the internal thermodynamic state of the phase, excluding composition.
//! The default implementation corresponds to the default implementation of
//! nativeState().
//! @param lenstate Length of the state array. Must be >= partialStateSize().
//! @param[in] state Array of state variables, in the order defined by
//! nativeState().
//! @since New in %Cantera 3.2
virtual void restorePartialState(size_t lenstate, const double* state);
//! Return size of vector defining internal state of the phase.
//! Used by saveState() and restoreState().
virtual size_t stateSize() const;
+37 -16
View File
@@ -225,11 +225,41 @@ vector<string> Phase::partialStates() const
}
}
void Phase::savePartialState(size_t lenstate, double* state) const
{
if (lenstate < partialStateSize()) {
throw ArraySizeError("Phase::savePartialState", lenstate, partialStateSize());
}
auto native = nativeState();
state[native.at("T")] = temperature();
if (isCompressible()) {
state[native.at("D")] = density();
} else {
state[native.at("P")] = pressure();
}
}
void Phase::restorePartialState(size_t lenstate, const double* state)
{
if (lenstate < partialStateSize()) {
throw ArraySizeError("Phase::restorePartialState", lenstate, partialStateSize());
}
auto native = nativeState();
setTemperature(state[native.at("T")]);
if (isCompressible()) {
setDensity(state[native.at("D")]);
} else {
setPressure(state[native.at("P")]);
}
}
size_t Phase::stateSize() const {
if (isPure()) {
return 2;
return partialStateSize();
} else {
return nSpecies() + 2;
return partialStateSize() + nSpecies();
}
}
@@ -241,15 +271,11 @@ void Phase::saveState(vector<double>& state) const
void Phase::saveState(size_t lenstate, double* state) const
{
auto native = nativeState();
// function assumes default definition of nativeState
state[native.at("T")] = temperature();
if (isCompressible()) {
state[native.at("D")] = density();
} else {
state[native.at("P")] = pressure();
if (lenstate < stateSize()) {
throw ArraySizeError("Phase::saveState", lenstate, stateSize());
}
savePartialState(lenstate, state);
auto native = nativeState();
if (native.count("X")) {
getMoleFractions(state + native["X"]);
} else if (native.count("Y")) {
@@ -269,14 +295,9 @@ void Phase::restoreState(size_t lenstate, const double* state)
throw ArraySizeError("Phase::restoreState",
lenstate, ls);
}
restorePartialState(lenstate, state);
auto native = nativeState();
setTemperature(state[native.at("T")]);
if (isCompressible()) {
setDensity(state[native.at("D")]);
} else {
setPressure(state[native.at("P")]);
}
if (native.count("X")) {
setMoleFractions_NoNorm(state + native["X"]);