From d1cda15f42690f12a0f672d915b6b398317d936f Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Tue, 31 Mar 2015 15:37:14 -0400 Subject: [PATCH] Improve performance of importing phases Replace the O(k) implementation of Phase::speciesIndex with one that is O(log(k)). This function is called frequently as part of Kinetics object initialization. Optimize for the more common case where the species name is not qualified with a phase name. --- include/cantera/thermo/Phase.h | 3 +++ src/thermo/Phase.cpp | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) 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();