From a74fda8ad21f53f887d86c95ca65cf4817fd02a1 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 3 Oct 2014 01:45:49 +0000 Subject: [PATCH] Introduce 'getValue' to avoid making std::map members mutable --- include/cantera/base/utilities.h | 29 ++++++++++++++++++++ include/cantera/kinetics/AqueousKinetics.h | 13 +++++---- include/cantera/kinetics/GasKinetics.h | 12 ++++---- include/cantera/kinetics/InterfaceKinetics.h | 9 ++---- include/cantera/thermo/SimpleThermo.h | 11 ++++---- src/kinetics/InterfaceKinetics.cpp | 4 +-- src/thermo/NasaThermo.cpp | 16 +++++------ src/thermo/NasaThermo.h | 6 ++-- src/thermo/ShomateThermo.h | 17 ++++++------ 9 files changed, 73 insertions(+), 44 deletions(-) diff --git a/include/cantera/base/utilities.h b/include/cantera/base/utilities.h index 51c4fdf10..e97058833 100644 --- a/include/cantera/base/utilities.h +++ b/include/cantera/base/utilities.h @@ -18,6 +18,7 @@ #include "ct_defs.h" #include "global.h" +#include #include @@ -702,6 +703,34 @@ void deepStdVectorPointerCopy(const std::vector &fromVec, std::vector &t //! Check to see that a number is finite (not NaN, +Inf or -Inf) void checkFinite(const double tmp); + +//! Const accessor for a value in a std::map. +/*! + * This is a const alternative to operator[]. Roughly equivalent to the 'at' + * member function introduced in C++11. Throws std::out_of_range if the key + * does not exist. + */ +template +const U& getValue(const std::map& m, const T& key) { + typename std::map::const_iterator iter = m.find(key); + if (iter == m.end()) { + throw std::out_of_range("std::map: key not found"); + } else { + return iter->second; + } +} + +//! Const accessor for a value in a std::map. +/* + * Similar to the two-argument version of getValue, but returns *default_val* + * if the key is not found instead of throwing an exception. + */ +template +const U& getValue(const std::map& m, const T& key, const U& default_val) { + typename std::map::const_iterator iter = m.find(key); + return (iter == m.end()) ? default_val : iter->second; +} + } #endif diff --git a/include/cantera/kinetics/AqueousKinetics.h b/include/cantera/kinetics/AqueousKinetics.h index 5255e93dc..95889db31 100644 --- a/include/cantera/kinetics/AqueousKinetics.h +++ b/include/cantera/kinetics/AqueousKinetics.h @@ -12,6 +12,7 @@ #include "Kinetics.h" #include "ReactionStoichMgr.h" #include "RateCoeffMgr.h" +#include "cantera/base/utilities.h" namespace Cantera { @@ -65,11 +66,11 @@ public: } virtual doublereal reactantStoichCoeff(size_t k, size_t i) const { - return m_rrxn[k][i]; + return getValue(m_rrxn[k], i, 0.0); } virtual doublereal productStoichCoeff(size_t k, size_t i) const { - return m_prxn[k][i]; + return getValue(m_prxn[k], i, 0.0); } //! @name Reaction Rates Of Progress @@ -123,7 +124,7 @@ public: //! @{ virtual int reactionType(size_t i) const { - return m_index[i].first; + return getValue(m_index, i).first; } virtual std::string reactionString(size_t i) const { @@ -185,7 +186,7 @@ protected: Rate1 m_rates; - mutable std::map > m_index; + std::map > m_index; std::vector m_irrev; @@ -201,8 +202,8 @@ protected: std::vector m_rxntype; - mutable std::vector > m_rrxn; - mutable std::vector > m_prxn; + std::vector > m_rrxn; + std::vector > m_prxn; /** * Difference between the input global reactants order diff --git a/include/cantera/kinetics/GasKinetics.h b/include/cantera/kinetics/GasKinetics.h index abbc5b3d5..b24b6a4d0 100644 --- a/include/cantera/kinetics/GasKinetics.h +++ b/include/cantera/kinetics/GasKinetics.h @@ -53,11 +53,11 @@ public: } virtual doublereal reactantStoichCoeff(size_t k, size_t i) const { - return m_rrxn[k][i]; + return getValue(m_rrxn[k], i, 0.0); } virtual doublereal productStoichCoeff(size_t k, size_t i) const { - return m_prxn[k][i]; + return getValue(m_prxn[k], i, 0.0); } //! @} @@ -101,7 +101,7 @@ public: //! @{ virtual int reactionType(size_t i) const { - return m_index[i].first; + return getValue(m_index, i).first; } virtual std::string reactionString(size_t i) const { @@ -167,7 +167,7 @@ protected: Rate1 m_falloff_high_rates; Rate1 m_rates; - mutable std::map > m_index; + std::map > m_index; FalloffMgr m_falloffn; @@ -191,8 +191,8 @@ protected: std::vector m_rxntype; - mutable std::vector > m_rrxn; - mutable std::vector > m_prxn; + std::vector > m_rrxn; + std::vector > m_prxn; /** * Difference between the input global reactants order diff --git a/include/cantera/kinetics/InterfaceKinetics.h b/include/cantera/kinetics/InterfaceKinetics.h index 2c0e1b752..3c58f03ff 100644 --- a/include/cantera/kinetics/InterfaceKinetics.h +++ b/include/cantera/kinetics/InterfaceKinetics.h @@ -478,7 +478,7 @@ protected: * The first pair is the reactionType of the reaction. * The second pair is ... */ - mutable std::map > m_index; + std::map > m_index; //! Vector of irreversible reaction numbers /*! @@ -507,11 +507,8 @@ protected: * kinetics object. For each species, there exists a map, with the * reaction number being the key, and the reactant stoichiometric * coefficient for the species being the value. - * - * HKM -> mutable because search sometimes creates extra - * entries. To be fixed in future... */ - mutable std::vector > m_rrxn; + std::vector > m_rrxn; //! m_prxn is a vector of maps, containing the reactant //! stoichiometric coefficient information @@ -521,7 +518,7 @@ protected: * exists a map, with the reaction number being the key, and the product * stoichiometric coefficient for the species being the value. */ - mutable std::vector > m_prxn; + std::vector > m_prxn; //! Vector of reactionType for the reactions defined within this object /*! diff --git a/include/cantera/thermo/SimpleThermo.h b/include/cantera/thermo/SimpleThermo.h index 0d8b07612..94c13e934 100644 --- a/include/cantera/thermo/SimpleThermo.h +++ b/include/cantera/thermo/SimpleThermo.h @@ -11,6 +11,7 @@ #include "SpeciesThermoMgr.h" #include "speciesThermoTypes.h" #include "cantera/base/global.h" +#include "cantera/base/utilities.h" namespace Cantera { @@ -203,7 +204,7 @@ public: doublereal* h_RT, doublereal* s_R) const { doublereal logt = log(t); doublereal rt = 1.0/t; - size_t loc = m_loc[k]; + size_t loc = getValue(m_loc, k); cp_R[k] = m_cp0_R[loc]; h_RT[k] = rt*(m_h0_R[loc] + (t - m_t0[loc]) * m_cp0_R[loc]); s_R[k] = m_s0_R[loc] + m_cp0_R[loc] * (logt - m_logt0[loc]); @@ -213,7 +214,7 @@ public: if (k == npos) { return m_tlow_max; } else { - return m_tlow[m_loc[k]]; + return m_tlow[getValue(m_loc, k)]; } } @@ -221,7 +222,7 @@ public: if (k == npos) { return m_thigh_min; } else { - return m_thigh[m_loc[k]]; + return m_thigh[getValue(m_loc, k)]; } } @@ -252,7 +253,7 @@ public: doublereal& maxTemp_, doublereal& refPressure_) const { type = reportType(index); - size_t loc = m_loc[index]; + size_t loc = getValue(m_loc, index); if (type == SIMPLE) { c[0] = m_t0[loc]; c[1] = m_h0_R[loc] * GasConstant; @@ -280,7 +281,7 @@ protected: * This index keeps track of it. * indexData = m_loc[kspec] */ - mutable std::map m_loc; + std::map m_loc; //! Map between the vector index where the coefficients are kept and the species index /*! diff --git a/src/kinetics/InterfaceKinetics.cpp b/src/kinetics/InterfaceKinetics.cpp index ae4539b01..8eb003c16 100644 --- a/src/kinetics/InterfaceKinetics.cpp +++ b/src/kinetics/InterfaceKinetics.cpp @@ -1418,12 +1418,12 @@ int InterfaceKinetics::phaseStability(const size_t iphase) const //================================================================================================================== doublereal InterfaceKinetics::reactantStoichCoeff(size_t kSpecKin, size_t irxn) const { - return m_rrxn[kSpecKin][irxn]; + return getValue(m_rrxn[kSpecKin], irxn, 0.0); } //================================================================================================================== doublereal InterfaceKinetics::productStoichCoeff(size_t kSpecKin, size_t irxn) const { - return m_prxn[kSpecKin][irxn]; + return getValue(m_prxn[kSpecKin], irxn, 0.0); } //================================================================================================================== int InterfaceKinetics::reactionType(size_t irxn) const diff --git a/src/thermo/NasaThermo.cpp b/src/thermo/NasaThermo.cpp index 740b0066a..58f7f5e15 100644 --- a/src/thermo/NasaThermo.cpp +++ b/src/thermo/NasaThermo.cpp @@ -127,8 +127,8 @@ void NasaThermo::update_one(size_t k, doublereal t, doublereal* cp_R, m_t[4] = 1.0/t; m_t[5] = log(t); - size_t grp = m_group_map[k]; - size_t pos = m_posInGroup_map[k]; + size_t grp = getValue(m_group_map, k); + size_t pos = getValue(m_posInGroup_map, k); const std::vector &mlg = m_low[grp-1]; const NasaPoly1* nlow = &(mlg[pos]); @@ -179,8 +179,8 @@ void NasaThermo::reportParams(size_t index, int& type, { type = reportType(index); if (type == NASA) { - size_t grp = m_group_map[index]; - size_t pos = m_posInGroup_map[index]; + size_t grp = getValue(m_group_map, index); + size_t pos = getValue(m_posInGroup_map, index); const std::vector &mlg = m_low[grp-1]; const std::vector &mhg = m_high[grp-1]; const NasaPoly1* lowPoly = &(mlg[pos]); @@ -215,8 +215,8 @@ void NasaThermo::reportParams(size_t index, int& type, doublereal NasaThermo::reportOneHf298(const size_t k) const { - size_t grp = m_group_map[k]; - size_t pos = m_posInGroup_map[k]; + size_t grp = getValue(m_group_map, k); + size_t pos = getValue(m_posInGroup_map, k); const std::vector &mlg = m_low[grp-1]; const NasaPoly1* nlow = &(mlg[pos]); doublereal tmid = nlow->maxTemp(); @@ -233,8 +233,8 @@ doublereal NasaThermo::reportOneHf298(const size_t k) const void NasaThermo::modifyOneHf298(const size_t k, const doublereal Hf298New) { - size_t grp = m_group_map[k]; - size_t pos = m_posInGroup_map[k]; + size_t grp = getValue(m_group_map, k); + size_t pos = getValue(m_posInGroup_map, k); std::vector &mlg = m_low[grp-1]; NasaPoly1* nlow = &(mlg[pos]); std::vector &mhg = m_high[grp-1]; diff --git a/src/thermo/NasaThermo.h b/src/thermo/NasaThermo.h index 58094ec50..3b7f394d0 100644 --- a/src/thermo/NasaThermo.h +++ b/src/thermo/NasaThermo.h @@ -219,17 +219,17 @@ protected: * for that species are stored. group indices start at 1, * so a decrement is always performed to access vectors. */ - mutable std::map m_group_map; + std::map m_group_map; /*! * This map takes as its index, the species index in the phase. * It returns the position index within the group, where the * temperature polynomials for that species are stored. */ - mutable std::map m_posInGroup_map; + std::map m_posInGroup_map; //! Species name as a function of the species index - mutable std::map m_name; + std::map m_name; protected: //! Compute the nondimensional heat capacity using the given NASA polynomial diff --git a/src/thermo/ShomateThermo.h b/src/thermo/ShomateThermo.h index 2723ad57f..f5dcf9e80 100644 --- a/src/thermo/ShomateThermo.h +++ b/src/thermo/ShomateThermo.h @@ -13,6 +13,7 @@ #include "cantera/thermo/SpeciesThermoMgr.h" #include "ShomatePoly.h" #include "cantera/base/global.h" +#include "cantera/base/utilities.h" namespace Cantera { @@ -223,8 +224,8 @@ public: m_t[5] = 1.0/GasConstant; m_t[6] = 1.0/(GasConstant * t); - size_t grp = m_group_map[k]; - size_t pos = m_posInGroup_map[k]; + size_t grp = getValue(m_group_map, k); + size_t pos = getValue(m_posInGroup_map, k); const std::vector &mlg = m_low[grp-1]; const ShomatePoly* nlow = &(mlg[pos]); @@ -297,8 +298,8 @@ public: doublereal& refPressure) const { type = reportType(index); if (type == SHOMATE) { - size_t grp = m_group_map[index]; - size_t pos = m_posInGroup_map[index]; + size_t grp = getValue(m_group_map, index); + size_t pos = getValue(m_posInGroup_map, index); int itype = SHOMATE; const std::vector &mlg = m_low[grp-1]; const std::vector &mhg = m_high[grp-1]; @@ -337,8 +338,8 @@ public: doublereal h; doublereal t = 298.15; - size_t grp = m_group_map[k]; - size_t pos = m_posInGroup_map[k]; + size_t grp = getValue(m_group_map, k); + size_t pos = getValue(m_posInGroup_map, k); const std::vector &mlg = m_low[grp-1]; const ShomatePoly* nlow = &(mlg[pos]); @@ -444,14 +445,14 @@ protected: * for that species are stored. group indices start at 1, * so a decrement is always performed to access vectors. */ - mutable std::map m_group_map; + std::map m_group_map; /*! * This map takes as its index, the species index in the phase. * It returns the position index within the group, where the * temperature polynomials for that species are stored. */ - mutable std::map m_posInGroup_map; + std::map m_posInGroup_map; }; }