Skip to content

Commit

Permalink
[Thermo] Keep PureFluidPhase state data consistent
Browse files Browse the repository at this point in the history
Previously, calls to setTemperature, setDensity, and setState_TR did not result
in the underlying Substance object being updated. In addition, the
isothermalCompressibility and thermalExpansionCoeff methods did not synchronize
the state.

Now, setting the state of the PureFluidPhase object always sets the state of the
Substance object, and no synchronization is required in property calculation
functions.
  • Loading branch information
speth committed Aug 3, 2018
1 parent b5b542d commit fb68cae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
5 changes: 2 additions & 3 deletions include/cantera/thermo/PureFluidPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class PureFluidPhase : public ThermoPhase
* @param p Pressure (Pa)
*/
virtual void setPressure(doublereal p);
virtual void setTemperature(double T);
virtual void setDensity(double rho);

virtual void getChemPotentials(doublereal* mu) const {
mu[0] = gibbs_mole();
Expand Down Expand Up @@ -178,9 +180,6 @@ class PureFluidPhase : public ThermoPhase
*/
void Set(tpx::PropertyPair::type n, double x, double y) const;

//! Sets the state using a TPX::TV call
void setTPXState() const;

private:
//! Pointer to the underlying tpx object Substance that does the work
mutable std::unique_ptr<tpx::Substance> m_sub;
Expand Down
5 changes: 5 additions & 0 deletions interfaces/cython/cantera/test/test_purefluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def test_thermal_expansion_coeff_lowP(self):
self.assertNear(ref.thermal_expansion_coeff,
self.water.thermal_expansion_coeff, 1e-5)

def test_thermal_expansion_coeff_TD(self):
for T in [440, 550, 660]:
self.water.TD = T, 0.1
self.assertNear(T * self.water.thermal_expansion_coeff, 1.0, 1e-2)

def test_fd_properties_twophase(self):
self.water.TX = 400, 0.1
self.assertEqual(self.water.cp, np.inf)
Expand Down
35 changes: 16 additions & 19 deletions src/thermo/PureFluidPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,60 +85,60 @@ double PureFluidPhase::maxTemp(size_t k) const

doublereal PureFluidPhase::enthalpy_mole() const
{
setTPXState();
return m_sub->h() * m_mw;
}

doublereal PureFluidPhase::intEnergy_mole() const
{
setTPXState();
return m_sub->u() * m_mw;
}

doublereal PureFluidPhase::entropy_mole() const
{
setTPXState();
return m_sub->s() * m_mw;
}

doublereal PureFluidPhase::gibbs_mole() const
{
setTPXState();
return m_sub->g() * m_mw;
}

doublereal PureFluidPhase::cp_mole() const
{
setTPXState();
return m_sub->cp() * m_mw;
}

doublereal PureFluidPhase::cv_mole() const
{
setTPXState();
return m_sub->cv() * m_mw;
}

doublereal PureFluidPhase::pressure() const
{
setTPXState();
return m_sub->P();
}

void PureFluidPhase::setPressure(doublereal p)
{
Set(tpx::PropertyPair::TP, temperature(), p);
setDensity(1.0/m_sub->v());
ThermoPhase::setDensity(1.0/m_sub->v());
}

void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
void PureFluidPhase::setTemperature(double T)
{
m_sub->Set(n, x, y);
ThermoPhase::setTemperature(T);
Set(tpx::PropertyPair::TV, T, m_sub->v());
}

void PureFluidPhase::setTPXState() const
void PureFluidPhase::setDensity(double rho)
{
Set(tpx::PropertyPair::TV, temperature(), 1.0/density());
ThermoPhase::setDensity(rho);
Set(tpx::PropertyPair::TV, m_sub->Temp(), 1.0/rho);
}

void PureFluidPhase::Set(tpx::PropertyPair::type n, double x, double y) const
{
m_sub->Set(n, x, y);
}

doublereal PureFluidPhase::isothermalCompressibility() const
Expand Down Expand Up @@ -356,24 +356,21 @@ doublereal PureFluidPhase::satPressure(doublereal t)

doublereal PureFluidPhase::vaporFraction() const
{
setTPXState();
return m_sub->x();
}

void PureFluidPhase::setState_Tsat(doublereal t, doublereal x)
{
setTemperature(t);
setTPXState();
Set(tpx::PropertyPair::TX, t, x);
setDensity(1.0/m_sub->v());
ThermoPhase::setTemperature(t);
ThermoPhase::setDensity(1.0/m_sub->v());
}

void PureFluidPhase::setState_Psat(doublereal p, doublereal x)
{
setTPXState();
Set(tpx::PropertyPair::PX, p, x);
setTemperature(m_sub->Temp());
setDensity(1.0/m_sub->v());
ThermoPhase::setTemperature(m_sub->Temp());
ThermoPhase::setDensity(1.0/m_sub->v());
}

std::string PureFluidPhase::report(bool show_thermo, doublereal threshold) const
Expand Down

0 comments on commit fb68cae

Please sign in to comment.