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 equilibrium calculations #898

Merged
merged 3 commits into from
Jul 10, 2020
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
2 changes: 1 addition & 1 deletion include/cantera/thermo/IdealSolidSolnPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ class IdealSolidSolnPhase : public ThermoPhase
virtual bool addSpecies(shared_ptr<Species> spec);
virtual void initThermo();
virtual void initThermoXML(XML_Node& phaseNode, const std::string& id);
virtual void setToEquilState(const doublereal* lambda_RT);
virtual void setToEquilState(const doublereal* mu_RT);

//! Set the form for the standard and generalized concentrations
/*!
Expand Down
1 change: 0 additions & 1 deletion include/cantera/thermo/RedlichKwongMFTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ class RedlichKwongMFTP : public MixtureFugacityTP

virtual bool addSpecies(shared_ptr<Species> spec);
virtual void setParametersFromXML(const XML_Node& thermoNode);
virtual void setToEquilState(const doublereal* lambda_RT);
virtual void initThermoXML(XML_Node& phaseNode, const std::string& id);
virtual void initThermo();

Expand Down
6 changes: 3 additions & 3 deletions include/cantera/thermo/ThermoPhase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1457,10 +1457,10 @@ class ThermoPhase : public Phase
* temperature is unchanged. Any phase (ideal or not) that
* implements this method can be equilibrated by ChemEquil.
*
* @param lambda_RT Input vector of dimensionless element potentials
* The length is equal to nElements().
* @param mu_RT Input vector of dimensionless chemical potentials
* The length is equal to nSpecies().
*/
virtual void setToEquilState(const doublereal* lambda_RT) {
virtual void setToEquilState(const doublereal* mu_RT) {
throw NotImplementedError("ThermoPhase::setToEquilState");
}

Expand Down
10 changes: 10 additions & 0 deletions interfaces/cython/cantera/test/test_equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,13 @@ def test_vcs(self):

def test_vcs_est(self):
self.solve('vcs', estimate_equil=-1)


class Test_IdealSolidSolnPhase_Equil(utilities.CanteraTest):
def test_equil(self):
gas = ct.ThermoPhase('IdealSolidSolnPhaseExample.xml')
gas.TPX = 500, ct.one_atm, 'C2H2-graph: 1.0'

gas.equilibrate('TP', solver='element_potential')
self.assertNear(gas['C-graph'].X[0], 2.0 / 3.0)
self.assertNear(gas['H2-solute'].X[0], 1.0 / 3.0)
21 changes: 14 additions & 7 deletions src/thermo/IdealSolidSolnPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,21 +447,28 @@ void IdealSolidSolnPhase::initThermoXML(XML_Node& phaseNode, const std::string&
ThermoPhase::initThermoXML(phaseNode, id_);
}

void IdealSolidSolnPhase::setToEquilState(const doublereal* lambda_RT)
void IdealSolidSolnPhase::setToEquilState(const doublereal* mu_RT)
{
const vector_fp& grt = gibbs_RT_ref();

// set the pressure and composition to be consistent with the temperature
doublereal pres = 0.0;
double m_p0 = refPressure();
for (size_t k = 0; k < m_kk; k++) {
m_pp[k] = -grt[k];
for (size_t m = 0; m < nElements(); m++) {
m_pp[k] += nAtoms(k,m)*lambda_RT[m];
double tmp = -grt[k] + mu_RT[k];
if (tmp < -600.) {
m_pp[k] = 0.0;
} else if (tmp > 500.0) {
// Protect against inf results if the exponent is too high
double tmp2 = tmp / 500.;
tmp2 *= tmp2;
m_pp[k] = m_p0 * exp(500.) * tmp2;
} else {
m_pp[k] = m_p0 * exp(tmp);
}
m_pp[k] = m_Pref * exp(m_pp[k]);
pres += m_pp[k];
}
setState_PX(pres, &m_pp[0]);
// set state
setState_PX(pres, m_pp.data());
}

void IdealSolidSolnPhase::setStandardConcentrationModel(const std::string& model)
Expand Down
30 changes: 0 additions & 30 deletions src/thermo/RedlichKwongMFTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,36 +518,6 @@ doublereal RedlichKwongMFTP::critDensity() const
return mmw / vc;
}

void RedlichKwongMFTP::setToEquilState(const doublereal* mu_RT)
{
double tmp, tmp2;
_updateReferenceStateThermo();
getGibbs_RT_ref(m_tmpV.data());

// Within the method, we protect against inf results if the exponent is too
// high.
//
// If it is too low, we set the partial pressure to zero. This capability is
// needed by the elemental potential method.
doublereal pres = 0.0;
double m_p0 = refPressure();
for (size_t k = 0; k < m_kk; k++) {
tmp = -m_tmpV[k] + mu_RT[k];
if (tmp < -600.) {
m_pp[k] = 0.0;
} else if (tmp > 500.0) {
tmp2 = tmp / 500.;
tmp2 *= tmp2;
m_pp[k] = m_p0 * exp(500.) * tmp2;
} else {
m_pp[k] = m_p0 * exp(tmp);
}
pres += m_pp[k];
}
// set state
setState_PX(pres, &m_pp[0]);
}

bool RedlichKwongMFTP::addSpecies(shared_ptr<Species> spec)
{
bool added = MixtureFugacityTP::addSpecies(spec);
Expand Down