diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index dddc81e11..fedf5c812 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -894,6 +894,9 @@ private: //! Vector of the species names std::vector m_speciesNames; + //! Map of species names to indices + std::map m_speciesIndices; + size_t m_mm; //!< Number of elements. vector_fp m_atomicWeights; //!< element atomic weights (kg kmol-1) vector_int m_atomicNumbers; //!< element atomic numbers diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 4486f7bdc..561394908 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -256,19 +256,17 @@ void Phase::getAtoms(size_t k, double* atomArray) const size_t Phase::speciesIndex(const std::string& nameStr) const { - std::string pn; - std::string sn = parseSpeciesName(nameStr, pn); - if (pn == "" || pn == m_name || pn == m_id) { - vector::const_iterator it = m_speciesNames.begin(); - for (size_t k = 0; k < m_kk; k++) { - if (*it == sn) { - return k; - } - ++it; + if (nameStr.find(':') != npos) { + std::string pn; + std::string sn = parseSpeciesName(nameStr, pn); + if (pn == "" || pn == m_name || pn == m_id) { + return getValue(m_speciesIndices, sn, npos); + } else { + return npos; } - return npos; + } else { + return getValue(m_speciesIndices, nameStr, npos); } - return npos; } string Phase::speciesName(size_t k) const @@ -868,6 +866,7 @@ bool Phase::addSpecies(shared_ptr& spec) { } m_speciesNames.push_back(spec->name); + m_speciesIndices[spec->name] = m_kk; m_speciesCharge.push_back(spec->charge); m_speciesSize.push_back(spec->size); size_t ne = nElements();