Skip to content

Commit

Permalink
[Thermo] Fix pure fluid state restoration after property calculation
Browse files Browse the repository at this point in the history
After calculating properties using finite differences (e.g. cp), the fluid was
returned to its original state by setting the state in terms of pressure and
temperature. However, because setting the state using these variables is an
iterative calculation with some error tolerance, the state was not restored
exactly. By restoring the state based on the initial temperature and density,
the restored state is identical to the initial state.
  • Loading branch information
speth committed Jan 14, 2020
1 parent 5b34d25 commit 5579ea4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 9 additions & 0 deletions interfaces/cython/cantera/test/test_purefluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,31 @@ def test_set_minmax(self):
def check_fd_properties(self, T1, P1, T2, P2, tol):
# Properties which are computed as finite differences
self.water.TP = T1, P1
h1a = self.water.enthalpy_mass
cp1 = self.water.cp_mass
cv1 = self.water.cv_mass
k1 = self.water.isothermal_compressibility
alpha1 = self.water.thermal_expansion_coeff
h1b = self.water.enthalpy_mass

self.water.TP = T2, P2
h2a = self.water.enthalpy_mass
cp2 = self.water.cp_mass
cv2 = self.water.cv_mass
k2 = self.water.isothermal_compressibility
alpha2 = self.water.thermal_expansion_coeff
h2b = self.water.enthalpy_mass

self.assertNear(cp1, cp2, tol)
self.assertNear(cv1, cv2, tol)
self.assertNear(k1, k2, tol)
self.assertNear(alpha1, alpha2, tol)

# calculating these finite difference properties should not perturbe the
# state of the object
self.assertEqual(h1a, h1b)
self.assertEqual(h2a, h2b)

def test_properties_near_min(self):
self.check_fd_properties(self.water.min_temp*(1+1e-5), 101325,
self.water.min_temp*(1+1e-4), 101325, 1e-2)
Expand Down
9 changes: 6 additions & 3 deletions src/tpx/Sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ double Substance::cv()
double Substance::cp()
{
double Tsave = T, dt = 1.e-4*T;
double RhoSave = Rho;
double T1 = std::max(Tmin(), Tsave - dt);
double T2 = std::min(Tmax(), Tsave + dt);
double p0 = P();
Expand Down Expand Up @@ -115,13 +116,14 @@ double Substance::cp()
}
double s2 = s();

Set(PropertyPair::TP, Tsave, p0);
Set(PropertyPair::TV, Tsave, 1.0 / RhoSave);
return T*(s2 - s1)/(T2-T1);
}

double Substance::thermalExpansionCoeff()
{
double Tsave = T, dt = 1.e-4*T;
double RhoSave = Rho;
double T1 = std::max(Tmin(), Tsave - dt);
double T2 = std::min(Tmax(), Tsave + dt);
double p0 = P();
Expand Down Expand Up @@ -153,13 +155,14 @@ double Substance::thermalExpansionCoeff()
}
double v2 = v();

Set(PropertyPair::TP, Tsave, p0);
Set(PropertyPair::TV, Tsave, 1.0 / RhoSave);
return 2.0*(v2 - v1)/((v2 + v1)*(T2-T1));
}

double Substance::isothermalCompressibility()
{
double Psave = P(), dp = 1.e-4*Psave;
double RhoSave = Rho;
double x0 = x();

if (TwoPhase()) {
Expand Down Expand Up @@ -191,7 +194,7 @@ double Substance::isothermalCompressibility()
}
double v2 = v();

Set(PropertyPair::TP, T, Psave);
Set(PropertyPair::TV, T, 1.0 / RhoSave);
return -(v2 - v1)/(v0*(P2-P1));
}

Expand Down

0 comments on commit 5579ea4

Please sign in to comment.