Skip to content

Commit

Permalink
[Thermo] Add function 'resetHf298'
Browse files Browse the repository at this point in the history
Using this instead of modifyHf298 to reset the thermo data to its original
state avoids round-off errors that otherwise make modifications to the
species thermo data irreversible.
  • Loading branch information
speth committed Apr 30, 2016
1 parent 0f66915 commit 0e2ea09
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 2 deletions.
3 changes: 3 additions & 0 deletions include/cantera/thermo/ConstCpPoly.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ConstCpPoly: public SpeciesThermoInterpType

virtual doublereal reportHf298(doublereal* const h298 = 0) const;
virtual void modifyOneHf298(const size_t k, const doublereal Hf298New);
virtual void resetHf298();

protected:
//! Base temperature
Expand All @@ -100,6 +101,8 @@ class ConstCpPoly: public SpeciesThermoInterpType
doublereal m_s0_R;
//! log of the t0 value
doublereal m_logt0;
//! Original value of h0_R, restored by calling resetHf298()
double m_h0_R_orig;
};

}
Expand Down
1 change: 1 addition & 0 deletions include/cantera/thermo/GeneralSpeciesThermo.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class GeneralSpeciesThermo : public SpeciesThermo
virtual doublereal reportOneHf298(const size_t k) const;

virtual void modifyOneHf298(const size_t k, const doublereal Hf298New);
virtual void resetHf298(const size_t k);

private:
//! Provide the SpeciesthermoInterpType object
Expand Down
1 change: 1 addition & 0 deletions include/cantera/thermo/LatticeSolidPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class LatticeSolidPhase : public ThermoPhase
void setLatticeMoleFractionsByName(int n, const std::string& x);

virtual void modifyOneHf298SS(const size_t k, const doublereal Hf298New);
virtual void resetHf298(const size_t k=npos);

protected:
//! Current value of the pressure
Expand Down
7 changes: 7 additions & 0 deletions include/cantera/thermo/NasaPoly1.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class NasaPoly1 : public SpeciesThermoInterpType
: SpeciesThermoInterpType(tlow, thigh, pref)
, m_coeff(coeffs, coeffs+7)
{
m_coeff5_orig = m_coeff[5];
}

virtual SpeciesThermoInterpType*
Expand Down Expand Up @@ -163,9 +164,15 @@ class NasaPoly1 : public SpeciesThermoInterpType
m_coeff[5] += (delH) / GasConstant;
}

virtual void resetHf298() {
m_coeff[5] = m_coeff5_orig;
}

protected:
//! array of polynomial coefficients, stored in the order [a0, ..., a6]
vector_fp m_coeff;

double m_coeff5_orig;
};

}
Expand Down
5 changes: 5 additions & 0 deletions include/cantera/thermo/NasaPoly2.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class NasaPoly2 : public SpeciesThermoInterpType
return h;
}

void resetHf298() {
mnp_low.resetHf298();
mnp_high.resetHf298();
}

void modifyOneHf298(const size_t k, const doublereal Hf298New) {
doublereal h298now = reportHf298(0);
doublereal delH = Hf298New - h298now;
Expand Down
11 changes: 11 additions & 0 deletions include/cantera/thermo/ShomatePoly.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ShomatePoly : public SpeciesThermoInterpType
for (size_t i = 0; i < 7; i++) {
m_coeff[i] = coeffs[i] * 1000 / GasConstant;
}
m_coeff5_orig = m_coeff[5];
}

virtual SpeciesThermoInterpType*
Expand Down Expand Up @@ -168,9 +169,14 @@ class ShomatePoly : public SpeciesThermoInterpType
m_coeff[5] += delH / (1e3 * GasConstant);
}

virtual void resetHf298() {
m_coeff[5] = m_coeff5_orig;
}

protected:
//! Array of coefficients
vector_fp m_coeff;
double m_coeff5_orig;
};

//! The Shomate polynomial parameterization for two temperature ranges for one
Expand Down Expand Up @@ -331,6 +337,11 @@ class ShomatePoly2 : public SpeciesThermoInterpType
msp_high.modifyOneHf298(k, hnew);
}

virtual void resetHf298() {
msp_low.resetHf298();
msp_high.resetHf298();
}

protected:
//! Midrange temperature (kelvin)
doublereal m_midT;
Expand Down
7 changes: 7 additions & 0 deletions include/cantera/thermo/SpeciesThermo.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ class SpeciesThermo
*/
virtual void modifyOneHf298(const size_t k, const doublereal Hf298New) = 0;

//! Restore the original heat of formation of one or more species
/*!
* Resets changes made by modifyOneHf298(). If the species index is not
* specified, the heats of formation for all species are restored.
*/
virtual void resetHf298(const size_t k) = 0;

//! Check if data for all species (0 through nSpecies-1) has been installed.
bool ready(size_t nSpecies);

Expand Down
10 changes: 10 additions & 0 deletions include/cantera/thermo/SpeciesThermoInterpType.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "cantera/base/ct_defs.h"
#include "speciesThermoTypes.h"
#include "cantera/base/ctexceptions.h"

namespace Cantera
{
Expand Down Expand Up @@ -278,6 +279,15 @@ class SpeciesThermoInterpType
*/
virtual void modifyOneHf298(const size_t k, const doublereal Hf298New);

//! Restore the original heat of formation for this species
/*!
* Resets changes made by modifyOneHf298().
*/
virtual void resetHf298() {
throw CanteraError("SpeciesThermoInterpType::resetHf298",
"Not implemented");
}

protected:
//! lowest valid temperature
doublereal m_lowT;
Expand Down
7 changes: 7 additions & 0 deletions include/cantera/thermo/ThermoPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ class ThermoPhase : public Phase
invalidateCache();
}

//! Restore the original heat of formation of one or more species
/*!
* Resets changes made by modifyOneHf298SS(). If the species index is not
* specified, the heats of formation for all species are restored.
*/
virtual void resetHf298(const size_t k=npos);

//! Maximum temperature for which the thermodynamic data for the species
//! are valid.
/*!
Expand Down
5 changes: 5 additions & 0 deletions src/thermo/ConstCpPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ConstCpPoly::ConstCpPoly(double tlow, double thigh, double pref,
m_s0_R = coeffs[2] / GasConstant;
m_cp0_R = coeffs[3] / GasConstant;
m_logt0 = log(m_t0);
m_h0_R_orig = m_h0_R;
}

SpeciesThermoInterpType*
Expand Down Expand Up @@ -103,4 +104,8 @@ void ConstCpPoly::modifyOneHf298(const size_t k, const doublereal Hf298New)
m_h0_R += delH / GasConstant;
}

void ConstCpPoly::resetHf298() {
m_h0_R = m_h0_R_orig;
}

}
8 changes: 8 additions & 0 deletions src/thermo/GeneralSpeciesThermo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,12 @@ void GeneralSpeciesThermo::modifyOneHf298(const size_t k, const doublereal Hf298
}
}

void GeneralSpeciesThermo::resetHf298(const size_t k)
{
SpeciesThermoInterpType* sp_ptr = provideSTIT(k);
if (sp_ptr) {
sp_ptr->resetHf298();
}
}

}
18 changes: 18 additions & 0 deletions src/thermo/LatticeSolidPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,22 @@ void LatticeSolidPhase::modifyOneHf298SS(const size_t k, const doublereal Hf298N
_updateThermo();
}

void LatticeSolidPhase::resetHf298(const size_t k)
{
if (k != npos) {
for (size_t n = 0; n < m_lattice.size(); n++) {
if (lkstart_[n+1] < k) {
size_t kk = k-lkstart_[n];
m_lattice[n]->speciesThermo().resetHf298(kk);
}
}
} else {
for (size_t n = 0; n < m_lattice.size(); n++) {
m_lattice[n]->speciesThermo().resetHf298(npos);
}
}
invalidateCache();
_updateThermo();
}

} // End namespace Cantera
1 change: 0 additions & 1 deletion src/thermo/SpeciesThermoInterpType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "cantera/thermo/SpeciesThermoInterpType.h"
#include "cantera/thermo/VPSSMgr.h"
#include "cantera/thermo/PDSS.h"
#include "cantera/base/ctexceptions.h"

namespace Cantera
{
Expand Down
11 changes: 11 additions & 0 deletions src/thermo/ThermoPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ ThermoPhase* ThermoPhase::duplMyselfAsThermoPhase() const
return new ThermoPhase(*this);
}

void ThermoPhase::resetHf298(size_t k) {
if (k != npos) {
m_spthermo->resetHf298(k);
} else {
for (size_t k = 0; k < nSpecies(); k++) {
m_spthermo->resetHf298(k);
}
}
invalidateCache();
}

int ThermoPhase::activityConvention() const
{
return cAC_CONVENTION_MOLAR;
Expand Down
6 changes: 5 additions & 1 deletion test/thermo/thermoParameterizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ TEST(Shomate, modifyOneHf298)
{
ShomatePoly2 S(200, 6000, 101325, co2_shomate_coeffs);

EXPECT_NEAR(-393.5224e6, S.reportHf298(), 1e4);
double hf = S.reportHf298();
EXPECT_NEAR(-393.5224e6, hf, 1e4);
double Htest = -400e6;
S.modifyOneHf298(npos, Htest);
double cp, h, s;
S.updatePropertiesTemp(298.15, &cp, &h, &s);
EXPECT_DOUBLE_EQ(Htest, h * 298.15 * GasConstant);
EXPECT_DOUBLE_EQ(Htest, S.reportHf298());
S.resetHf298();
S.updatePropertiesTemp(298.15, &cp, &h, &s);
EXPECT_DOUBLE_EQ(hf, h * 298.15 * GasConstant);
}

0 comments on commit 0e2ea09

Please sign in to comment.