Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Redlich-Kwong partial molar properties #1218

Merged
merged 2 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/cantera/thermo/RedlichKwongMFTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ class RedlichKwongMFTP : public MixtureFugacityTP
virtual void getPartialMolarEnthalpies(doublereal* hbar) const;
virtual void getPartialMolarEntropies(doublereal* sbar) const;
virtual void getPartialMolarIntEnergies(doublereal* ubar) const;
virtual void getPartialMolarCp(doublereal* cpbar) const;
virtual void getPartialMolarCp(double* cpbar) const {
throw NotImplementedError("RedlichKwongMFTP::getPartialMolarCp");
}
virtual void getPartialMolarVolumes(doublereal* vbar) const;
//! @}

Expand Down
35 changes: 23 additions & 12 deletions interfaces/cython/cantera/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,20 +663,25 @@ def checkConversion(self, basename, cls=ct.Solution, ctiphases=(),

return ctiPhase, yamlPhase

def checkThermo(self, ctiPhase, yamlPhase, temperatures, tol=1e-7):
def checkThermo(self, ctiPhase, yamlPhase, temperatures, tol=1e-7, check_cp=True):
for T in temperatures:
ctiPhase.TP = T, ct.one_atm
yamlPhase.TP = T, ct.one_atm
cp_cti = ctiPhase.partial_molar_cp
cp_yaml = yamlPhase.partial_molar_cp
if check_cp:
cp_cti = ctiPhase.partial_molar_cp
cp_yaml = yamlPhase.partial_molar_cp
else:
with pytest.raises(ct.CanteraError):
yamlPhase.partial_molar_cp
h_cti = ctiPhase.partial_molar_enthalpies
h_yaml = yamlPhase.partial_molar_enthalpies
s_cti = ctiPhase.partial_molar_entropies
s_yaml = yamlPhase.partial_molar_entropies
self.assertNear(ctiPhase.density, yamlPhase.density)
for i in range(ctiPhase.n_species):
message = ' for species {0} at T = {1}'.format(i, T)
self.assertNear(cp_cti[i], cp_yaml[i], tol, msg='cp'+message)
if check_cp:
self.assertNear(cp_cti[i], cp_yaml[i], tol, msg='cp'+message)
self.assertNear(h_cti[i], h_yaml[i], tol, msg='h'+message)
self.assertNear(s_cti[i], s_yaml[i], tol, msg='s'+message)

Expand Down Expand Up @@ -779,13 +784,13 @@ def test_Redlich_Kwong_CO2(self):
ctiGas, yamlGas = self.checkConversion('co2_RK_example')
for P in [1e5, 2e6, 1.3e7]:
yamlGas.TP = ctiGas.TP = 300, P
self.checkThermo(ctiGas, yamlGas, [300, 400, 500])
self.checkThermo(ctiGas, yamlGas, [300, 400, 500], check_cp=False)

@utilities.slow_test
def test_Redlich_Kwong_ndodecane(self):
self.convert("nDodecane_Reitz", self.cantera_data_path)
ctiGas, yamlGas = self.checkConversion('nDodecane_Reitz')
self.checkThermo(ctiGas, yamlGas, [300, 400, 500])
self.checkThermo(ctiGas, yamlGas, [300, 400, 500], check_cp=False)
self.checkKinetics(ctiGas, yamlGas, [300, 500, 1300], [1e5, 2e6, 1.4e7],
1e-6)

Expand Down Expand Up @@ -889,20 +894,26 @@ def checkConversion(self, basename, cls=ct.Solution, ctmlphases=(),

return ctmlPhase, yamlPhase

def checkThermo(self, ctmlPhase, yamlPhase, temperatures, pressure=ct.one_atm, tol=1e-7):
def checkThermo(self, ctmlPhase, yamlPhase, temperatures, pressure=ct.one_atm,
tol=1e-7, check_cp=True):
for T in temperatures:
ctmlPhase.TP = T, pressure
yamlPhase.TP = T, pressure
cp_ctml = ctmlPhase.partial_molar_cp
cp_yaml = yamlPhase.partial_molar_cp
if check_cp:
cp_ctml = ctmlPhase.partial_molar_cp
cp_yaml = yamlPhase.partial_molar_cp
else:
with pytest.raises(ct.CanteraError):
yamlPhase.partial_molar_cp
h_ctml = ctmlPhase.partial_molar_enthalpies
h_yaml = yamlPhase.partial_molar_enthalpies
s_ctml = ctmlPhase.partial_molar_entropies
s_yaml = yamlPhase.partial_molar_entropies
self.assertNear(ctmlPhase.density, yamlPhase.density)
for i in range(ctmlPhase.n_species):
message = ' for species {0} at T = {1}'.format(ctmlPhase.species_names[i], T)
self.assertNear(cp_ctml[i], cp_yaml[i], tol, msg='cp'+message)
if check_cp:
self.assertNear(cp_ctml[i], cp_yaml[i], tol, msg='cp'+message)
self.assertNear(h_ctml[i], h_yaml[i], tol, msg='h'+message)
self.assertNear(s_ctml[i], s_yaml[i], tol, msg='s'+message)

Expand Down Expand Up @@ -1002,13 +1013,13 @@ def test_Redlich_Kwong_CO2(self):
ctmlGas, yamlGas = self.checkConversion('co2_RK_example')
for P in [1e5, 2e6, 1.3e7]:
yamlGas.TP = ctmlGas.TP = 300, P
self.checkThermo(ctmlGas, yamlGas, [300, 400, 500])
self.checkThermo(ctmlGas, yamlGas, [300, 400, 500], check_cp=False)

@utilities.slow_test
def test_Redlich_Kwong_ndodecane(self):
self.convert("nDodecane_Reitz", self.cantera_data_path)
ctmlGas, yamlGas = self.checkConversion('nDodecane_Reitz')
self.checkThermo(ctmlGas, yamlGas, [300, 400, 500])
self.checkThermo(ctmlGas, yamlGas, [300, 400, 500], check_cp=False)
self.checkKinetics(ctmlGas, yamlGas, [300, 500, 1300], [1e5, 2e6, 1.4e7],
1e-6)

Expand Down
15 changes: 7 additions & 8 deletions src/thermo/RedlichKwongMFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,13 @@ void RedlichKwongMFTP::getPartialMolarEntropies(doublereal* sbar) const

void RedlichKwongMFTP::getPartialMolarIntEnergies(doublereal* ubar) const
{
getIntEnergy_RT(ubar);
scale(ubar, ubar+m_kk, ubar, RT());
}

void RedlichKwongMFTP::getPartialMolarCp(doublereal* cpbar) const
{
getCp_R(cpbar);
scale(cpbar, cpbar+m_kk, cpbar, GasConstant);
// u_k = h_k - P * v_k
getPartialMolarVolumes(m_partialMolarVolumes.data());
getPartialMolarEnthalpies(ubar);
double p = pressure();
for (size_t k = 0; k < nSpecies(); k++) {
ubar[k] -= p * m_partialMolarVolumes[k];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I was fearing some long derivation on which I'd have to go and run due diligence to check 😂

}
}

void RedlichKwongMFTP::getPartialMolarVolumes(doublereal* vbar) const
Expand Down
18 changes: 18 additions & 0 deletions test/thermo/RedlichKwongMFTP_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,22 @@ TEST_F(RedlichKwongMFTP_Test, localCritProperties)
EXPECT_NEAR(test_phase->critPressure(), 22.064e6, 1e-4);
}

TEST_F(RedlichKwongMFTP_Test, partialMolarIntEnergy)
{
// Check that sum(X_k * u) = u
set_r(0.4);
test_phase->setState_TP(400, 1.3e7);
double u_ref = test_phase->intEnergy_mole();
size_t kk = test_phase->nSpecies();
vector_fp uk(kk);
vector_fp X(kk);
test_phase->getMoleFractions(X.data());
test_phase->getPartialMolarIntEnergies(uk.data());
double u_test = 0;
for (size_t k = 0; k < kk; k++) {
u_test += uk[k] * X[k];
}
EXPECT_NEAR(u_ref, u_test, 1e-13 * std::abs(u_ref));
}

};