Skip to content

Commit

Permalink
[Thermo] Fix finite-difference properties in two-phase region
Browse files Browse the repository at this point in the history
These properties are actually infinite in the two-phase region, but attempting
to compute them by finite difference would incorrectly give a finite result, so
they need to be treated as a special case.
  • Loading branch information
speth committed Oct 21, 2016
1 parent a92245f commit 3f3b965
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
7 changes: 7 additions & 0 deletions interfaces/cython/cantera/test/test_purefluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ def test_thermal_expansion_coeff_lowP(self):
self.water.TP = 450, 12
self.assertNear(ref.thermal_expansion_coeff,
self.water.thermal_expansion_coeff, 1e-5)

def test_fd_properties_twophase(self):
self.water.TX = 400, 0.1
self.assertEqual(self.water.cp, np.inf)
self.assertEqual(self.water.isothermal_compressibility, np.inf)
self.assertEqual(self.water.thermal_expansion_coeff, np.inf)

def test_TPX(self):
self.water.TX = 400, 0.8
T,P,X = self.water.TPX
Expand Down
28 changes: 22 additions & 6 deletions src/tpx/Sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ double Substance::cp()
double T2 = std::min(Tmax(), Tsave + dt);
double p0 = P();
double x0 = x();
if (TwoPhase()) {
// In the two-phase region, cp is infinite
return std::numeric_limits<double>::infinity();
}

Set(PropertyPair::TP, T1, p0);
double x1 = x();
if ((x0 == 1.0 || x0 == 0.0) && x1 != x0) {
if (x1 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// T-dT is not, just take a one-sided difference
T1 = Tsave;
Expand All @@ -103,7 +107,7 @@ double Substance::cp()

Set(PropertyPair::TP, T2, p0);
double x2 = x();
if ((x0 == 1.0 || x0 == 0.0) && x2 != x0) {
if (x2 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// T+dT is not, just take a one-sided difference
T2 = Tsave;
Expand All @@ -123,9 +127,15 @@ double Substance::thermalExpansionCoeff()
double p0 = P();
double x0 = x();

if (TwoPhase()) {
// In the two-phase region, the thermal expansion coefficient is
// infinite
return std::numeric_limits<double>::infinity();
}

Set(PropertyPair::TP, T1, p0);
double x1 = x();
if ((x0 == 1.0 || x0 == 0.0) && x1 != x0) {
if (x1 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// T-dT is not, just take a one-sided difference
T1 = Tsave;
Expand All @@ -135,7 +145,7 @@ double Substance::thermalExpansionCoeff()

Set(PropertyPair::TP, T2, p0);
double x2 = x();
if ((x0 == 1.0 || x0 == 0.0) && x2 != x0) {
if (x2 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// T+dT is not, just take a one-sided difference
T2 = Tsave;
Expand All @@ -151,13 +161,19 @@ double Substance::isothermalCompressibility()
{
double Psave = P(), dp = 1.e-4*Psave;
double x0 = x();

if (TwoPhase()) {
// In the two-phase region, the isothermal compressibility is infinite
return std::numeric_limits<double>::infinity();
}

double v0 = v();
double P1 = Psave - dp;
double P2 = Psave + dp;

Set(PropertyPair::TP, T, P1);
double x1 = x();
if ((x0 == 1.0 || x0 == 0.0) && x1 != x0) {
if (x1 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// P-dP is not, just take a one-sided difference
P1 = Psave;
Expand All @@ -167,7 +183,7 @@ double Substance::isothermalCompressibility()

Set(PropertyPair::TP, T, P2);
double x2 = x();
if ((x0 == 1.0 || x0 == 0.0) && x2 != x0) {
if (x2 != x0) {
// If the initial state was pure liquid or pure vapor, and the state at
// P+dP is not, just take a one-sided difference
P2 = Psave;
Expand Down

0 comments on commit 3f3b965

Please sign in to comment.