[Thermo] Add getMass/MoleFractionsByName functions with thresholds

This makes it easier to set the composition of a phase based on the composition
of another phase with a different set of species. The threshold argument allows
species with negligible concentrations to be skipped.

Deprecate the unused getMoleFractionsByName function that didn't return a
value.
This commit is contained in:
Ray Speth 2014-09-18 22:58:59 +00:00
parent 022c31fe49
commit 09db5a498a
3 changed files with 88 additions and 0 deletions

View file

@ -429,8 +429,17 @@ public:
//! Get the mole fractions by name.
//! @param[out] x composition map containing the species mole fractions.
//! @deprecated To be removed after Cantera 2.2. use
//! `compositionMap getMoleFractionsByName(double threshold)`
//! instead.
void getMoleFractionsByName(compositionMap& x) const;
//! Get the mole fractions by name.
//! @param threshold Exclude species with mole fractions less than or
//! equal to this threshold.
//! @return Map of species names to mole fractions
compositionMap getMoleFractionsByName(double threshold=0.0) const;
//! Return the mole fraction of a single species
//! @param k species index
//! @return Mole fraction of the species
@ -441,6 +450,12 @@ public:
//! @return Mole fraction of the species
doublereal moleFraction(const std::string& name) const;
//! Get the mass fractions by name.
//! @param threshold Exclude species with mass fractions less than or
//! equal to this threshold.
//! @return Map of species names to mass fractions
compositionMap getMassFractionsByName(double threshold=0.0) const;
//! Return the mass fraction of a single species
//! @param k species index
//! @return Mass fraction of the species

View file

@ -536,6 +536,10 @@ const vector_fp& Phase::molecularWeights() const
void Phase::getMoleFractionsByName(compositionMap& x) const
{
warn_deprecated("void Phase::getMoleFractionsByName(compositionMap&)",
"To be removed after Cantera 2.2. Use"
" 'compositionMap getMoleFractionsByName(double threshold)'"
" instead");
x.clear();
size_t kk = nSpecies();
for (size_t k = 0; k < kk; k++) {
@ -543,6 +547,30 @@ void Phase::getMoleFractionsByName(compositionMap& x) const
}
}
compositionMap Phase::getMoleFractionsByName(double threshold) const
{
compositionMap comp;
for (size_t k = 0; k < m_kk; k++) {
double x = moleFraction(k);
if (x > threshold) {
comp[speciesName(k)] = x;
}
}
return comp;
}
compositionMap Phase::getMassFractionsByName(double threshold) const
{
compositionMap comp;
for (size_t k = 0; k < m_kk; k++) {
double x = massFraction(k);
if (x > threshold) {
comp[speciesName(k)] = x;
}
}
return comp;
}
void Phase::getMoleFractions(doublereal* const x) const
{
scale(m_ym.begin(), m_ym.end(), x, m_mmw);

View file

@ -1,5 +1,6 @@
#include "gtest/gtest.h"
#include "cantera/thermo/ThermoPhase.h"
#include "cantera/thermo/ThermoFactory.h"
#include <vector>
namespace Cantera
@ -45,5 +46,49 @@ TEST_F(ThermoPhase_Fixture, SetAndGetElementPotentials)
EXPECT_DOUBLE_EQ(setLambda[2], getLambda[2]);
}
class TestThermoMethods : public testing::Test
{
public:
ThermoPhase* thermo;
TestThermoMethods() {
thermo = newPhase("h2o2.xml", "");
}
~TestThermoMethods() {
delete thermo;
}
};
TEST_F(TestThermoMethods, getMoleFractionsByName)
{
thermo->setMoleFractionsByName("O2:0.2, H2:0.3, AR:0.5");
compositionMap X = thermo->getMoleFractionsByName();
EXPECT_DOUBLE_EQ(X["O2"], 0.2);
EXPECT_DOUBLE_EQ(X["H2"], 0.3);
EXPECT_DOUBLE_EQ(X["AR"], 0.5);
thermo->setMoleFractionsByName("OH:1e-9, O2:0.2, H2:0.3, AR:0.5");
X = thermo->getMoleFractionsByName();
EXPECT_EQ(X.size(), (size_t) 4);
X = thermo->getMoleFractionsByName(1e-5);
EXPECT_EQ(X.size(), (size_t) 3);
}
TEST_F(TestThermoMethods, getMassFractionsByName)
{
thermo->setMassFractionsByName("O2:0.2, H2:0.3, AR:0.5");
compositionMap Y = thermo->getMassFractionsByName();
EXPECT_DOUBLE_EQ(Y["O2"], 0.2);
EXPECT_DOUBLE_EQ(Y["H2"], 0.3);
EXPECT_DOUBLE_EQ(Y["AR"], 0.5);
thermo->setMassFractionsByName("OH:1e-9, O2:0.2, H2:0.3, AR:0.5");
Y = thermo->getMassFractionsByName();
EXPECT_EQ(Y.size(), (size_t) 4);
Y = thermo->getMassFractionsByName(1e-5);
EXPECT_EQ(Y.size(), (size_t) 3);
}
}