Skip to content

Commit

Permalink
Fix species coefficient updates in PengRobinson.
Browse files Browse the repository at this point in the history
-Species coefficients were using phase critical termperature, rather than species critical temepratures.
-Fixing this required updates to regression tests.
-Removed commented code from previous commit (removing XML capabilities)
  • Loading branch information
decaluwe committed Jan 16, 2020
1 parent c322bb7 commit 7df4499
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 190 deletions.
170 changes: 8 additions & 162 deletions src/thermo/PengRobinson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,84 +569,6 @@ vector<double> PengRobinson::getCoeff(const std::string& iName)
return spCoeff;
}

/* void PengRobinson::initThermoXML(XML_Node& phaseNode, const std::string& id)
{
if (phaseNode.hasChild("thermo")) {
XML_Node& thermoNode = phaseNode.child("thermo");
std::string model = thermoNode["model"];
if (model != "PengRobinson" && model != "PengRobinson") {
throw CanteraError("PengRobinson::initThermoXML",
"Unknown thermo model : " + model);
}
// Go get all of the coefficients and factors in the
// activityCoefficients XML block
if (thermoNode.hasChild("activityCoefficients")) {
XML_Node& acNode = thermoNode.child("activityCoefficients");
// Count the number of species with parameters provided in the
// input file:
size_t nParams = 0;
// Loop through the children and read out fluid parameters. Process
// all the pureFluidParameters, first:
for (size_t i = 0; i < acNode.nChildren(); i++) {
XML_Node& xmlACChild = acNode.child(i);
if (caseInsensitiveEquals(xmlACChild.name(), "purefluidparameters")) {
readXMLPureFluid(xmlACChild);
nParams += 1;
}
}
// If any species exist which have undefined pureFluidParameters,
// search the database in 'critProperties.xml' to find critical
// temperature and pressure to calculate a and b.
// Loop through all species in the CTI file
size_t iSpecies = 0;
for (size_t i = 0; i < m_kk; i++) {
string iName = speciesName(i);
// Get the index of the species
iSpecies = speciesIndex(iName);
// Check if a and b are already populated (only the diagonal elements of a).
size_t counter = iSpecies + m_kk * iSpecies;
// If not, then search the database:
if (isnan(a_coeff_vec(0, counter))) {
vector<double> coeffArray;
// Search the database for the species name and calculate
// coefficients a and b, from critical properties:
// coeffArray[0] = a0, coeffArray[1] = b, coeffArray[2] = w;
coeffArray = getCoeff(iName);
// Check if species was found in the database of critical properties,
// and assign the results
if (!isnan(coeffArray[0])) {
//Assuming no temperature dependence (i,e a1 = 0)
setSpeciesCoeffs(iName, coeffArray[0], 0.0, coeffArray[1]);
}
}
}
// Loop back through the "activityCoefficients" children and process the
// crossFluidParameters in the XML tree:
for (size_t i = 0; i < acNode.nChildren(); i++) {
XML_Node& xmlACChild = acNode.child(i);
if (caseInsensitiveEquals(xmlACChild.name(), "crossfluidparameters")) {
readXMLCrossFluid(xmlACChild);
}
}
}
}
MixtureFugacityTP::initThermoXML(phaseNode, id);
}
*/
void PengRobinson::initThermo()
{
for (auto& item : m_species) {
Expand Down Expand Up @@ -708,84 +630,6 @@ void PengRobinson::initThermo()
}
}

/*void PengRobinson::readXMLPureFluid(XML_Node& pureFluidParam)
{
string xname = pureFluidParam.name();
if (xname != "pureFluidParameters") {
throw CanteraError("PengRobinson::readXMLPureFluid",
"Incorrect name for processing this routine: " + xname);
}
double a0 = 0.0;
double a1 = 0.0;
double b = 0.0;
double w = 0.0;
for (size_t iChild = 0; iChild < pureFluidParam.nChildren(); iChild++) {
XML_Node& xmlChild = pureFluidParam.child(iChild);
string nodeName = toLowerCopy(xmlChild.name());
if (nodeName == "a_coeff") {
vector_fp vParams;
string iModel = toLowerCopy(xmlChild.attrib("model"));
getFloatArray(xmlChild, vParams, true, "Pascal-m6/kmol2", "a_coeff");
if (vParams.size() == 1) {
a0 = vParams[0];
} else if (vParams.size() == 2) {
a0 = vParams[0];
a1 = vParams[1];
} else {
throw CanteraError("PengRobinson::readXMLPureFluid",
"unknown model or incorrect number of parameters");
}
} else if (nodeName == "b_coeff") {
b = getFloatCurrent(xmlChild, "toSI");
} else if (nodeName == "acentric_factor") {
w = getFloatCurrent(xmlChild);
}
}
calculateAlpha(pureFluidParam.attrib("species"), a0, b, w);
setSpeciesCoeffs(pureFluidParam.attrib("species"), a0, b, w);
}
void PengRobinson::readXMLCrossFluid(XML_Node& CrossFluidParam)
{
string xname = CrossFluidParam.name();
if (xname != "crossFluidParameters") {
throw CanteraError("PengRobinson::readXMLCrossFluid",
"Incorrect name for processing this routine: " + xname);
}
string iName = CrossFluidParam.attrib("species1");
string jName = CrossFluidParam.attrib("species2");
size_t num = CrossFluidParam.nChildren();
for (size_t iChild = 0; iChild < num; iChild++) {
XML_Node& xmlChild = CrossFluidParam.child(iChild);
string nodeName = toLowerCopy(xmlChild.name());
if (nodeName == "a_coeff") {
vector_fp vParams;
getFloatArray(xmlChild, vParams, true, "Pascal-m6/kmol2", "a_coeff");
string iModel = toLowerCopy(xmlChild.attrib("model"));
if (iModel == "constant" && vParams.size() == 1) {
setBinaryCoeffs(iName, jName, vParams[0], 0.0);
} else if (iModel == "linear_a") {
setBinaryCoeffs(iName, jName, vParams[0], vParams[1]);
} else {
throw CanteraError("PengRobinson::readXMLCrossFluid",
"unknown model ({}) or wrong number of parameters ({})",
iModel, vParams.size());
}
}
}
}
void PengRobinson::setParametersFromXML(const XML_Node& thermoNode)
{
MixtureFugacityTP::setParametersFromXML(thermoNode);
}
*/
double PengRobinson::sresid() const
{
double molarV = molarVolume();
Expand Down Expand Up @@ -981,12 +825,12 @@ void PengRobinson::updateAB()
double temp = temperature();
//Update aAlpha_i
double sqt_alpha;
double critTemp = critTemperature();
double sqt_T_reduced = sqrt(temp / critTemp);

// Update indiviual alpha
for (size_t j = 0; j < m_kk; j++) {
sqt_alpha = 1 + kappa_vec_[j] * (1 - sqt_T_reduced);
size_t counter = j * m_kk + j;
double critTemp_j = speciesCritTemperature(a_vec_Curr_[counter],b_vec_Curr_[j]);
sqt_alpha = 1 + kappa_vec_[j] * (1 - sqrt(temp / critTemp_j));
alpha_vec_Curr_[j] = sqt_alpha*sqt_alpha;
}

Expand Down Expand Up @@ -1059,10 +903,12 @@ double PengRobinson::d2aAlpha_dT2() const
{
double temp, fac1, fac2, alphaij, alphai, alphaj, d2aAlphadT2 = 0.0, num;
double k;
double sqt_Tr = sqrt(temperature() / critTemperature()); //we need species critical temperature
double coeff1 = 1 / (critTemperature()*critTemperature()*sqt_Tr);
double coeff2 = sqt_Tr - 1;
for (size_t i = 0; i < m_kk; i++) {
size_t counter = i + m_kk * i;
double Tcrit_i = speciesCritTemperature(a_vec_Curr_[counter], b_vec_Curr_[i]);
double sqt_Tr = sqrt(temperature() / Tcrit_i); //we need species critical temperature
double coeff1 = 1 / (Tcrit_i*Tcrit_i*sqt_Tr);
double coeff2 = sqt_Tr - 1;
// Calculate first and second derivatives of alpha for individual species
k = kappa_vec_[i];
dalphadT_vec_Curr_[i] = coeff1 *(k* k*coeff2 - k);
Expand Down
56 changes: 28 additions & 28 deletions test/thermo/PengRobinson_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ TEST_F(PengRobinson_Test, chem_potentials)
* calculated using the model.
*/
const double expected_result[9] = {
-4.5736182681761962e+008,
-4.5733771904416579e+008,
-4.5732943831449223e+008,
-4.5732206687414169e+008,
-4.5731546826955432e+008,
-4.5730953161186475e+008,
-4.5730416590547645e+008,
-4.5729929581635743e+008,
-4.5729485847173005e+008
-4.5736172836328608e+008,
-4.5735067964144629e+008,
-4.5734081696030951e+008,
-4.5733195900446898e+008,
-4.5732395963640761e+008,
-4.5731669975736719e+008,
-4.5731008132866347e+008,
-4.5730402291258186e+008,
-4.5729845630046815e+008
};

double xmin = 0.6;
Expand Down Expand Up @@ -126,30 +126,30 @@ TEST_F(PengRobinson_Test, setTP)

// All sub-cooled liquid:
const double p1[6] = {
1.7474528924963985e+002,
1.6800540828415956e+002,
1.62278413743154e+002,
1.5728963799103039e+002,
1.5286573762819748e+002,
1.4888956030449546e+002
1.7504058222993714e+002,
1.6824762614489907e+002,
1.6248354709581241e+002,
1.5746729362032696e+002,
1.5302217175386241e+002,
1.4902908974486667e+002
};
// Phase change between temperatures 4 & 5:
const double p2[6] = {
7.5565889855724288e+002,
7.2577747673480337e+002,
6.913183942651284e+002,
6.494661249672663e+002,
5.9240469307757724e+002,
3.645826047440932e+002
7.5732259810273172e+002,
7.2766981078381912e+002,
6.935475475396446e+002,
6.5227027102964917e+002,
5.9657442842753153e+002,
3.9966973266966875e+002
};
// Supercritical; no discontinuity in rho values:
const double p3[6] = {
8.047430802847415e+002,
7.8291565113595595e+002,
7.5958477920749681e+002,
7.3445460137134626e+002,
7.0712433093853724e+002,
6.77034438769492e+002
8.0601205067780199e+002,
7.8427655940884574e+002,
7.6105347579146576e+002,
7.3605202492828505e+002,
7.0887891410210011e+002,
6.7898591969734434e+002
};

for(int i=0; i<6; ++i)
Expand All @@ -159,7 +159,7 @@ TEST_F(PengRobinson_Test, setTP)
test_phase->setState_TP(temp, 5542027.5);
EXPECT_NEAR(test_phase->density(),p1[i],1.e-8);

test_phase->setState_TP(temp, 7389370.);
test_phase->setState_TP(temp, 7388370.);
EXPECT_NEAR(test_phase->density(),p2[i],1.e-8);

test_phase->setState_TP(temp, 9236712.5);
Expand Down

0 comments on commit 7df4499

Please sign in to comment.