diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index be997960a..ec4540c39 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -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 diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 992f738d8..b46e3f5e0 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -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); diff --git a/test/thermo/ThermoPhase_Test.cpp b/test/thermo/ThermoPhase_Test.cpp index 2367addbf..08056861a 100644 --- a/test/thermo/ThermoPhase_Test.cpp +++ b/test/thermo/ThermoPhase_Test.cpp @@ -1,5 +1,6 @@ #include "gtest/gtest.h" #include "cantera/thermo/ThermoPhase.h" +#include "cantera/thermo/ThermoFactory.h" #include 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); +} + +}