Introduce 'getValue' to avoid making std::map members mutable

This commit is contained in:
Ray Speth 2014-10-03 01:45:49 +00:00
parent 91f3a2b073
commit a74fda8ad2
9 changed files with 73 additions and 44 deletions

View file

@ -18,6 +18,7 @@
#include "ct_defs.h"
#include "global.h"
#include <stdexcept>
#include <numeric>
@ -702,6 +703,34 @@ void deepStdVectorPointerCopy(const std::vector<D*> &fromVec, std::vector<D*> &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 <class T, class U>
const U& getValue(const std::map<T, U>& m, const T& key) {
typename std::map<T,U>::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 <class T, class U>
const U& getValue(const std::map<T, U>& m, const T& key, const U& default_val) {
typename std::map<T,U>::const_iterator iter = m.find(key);
return (iter == m.end()) ? default_val : iter->second;
}
}
#endif

View file

@ -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<Arrhenius> m_rates;
mutable std::map<size_t, std::pair<int, size_t> > m_index;
std::map<size_t, std::pair<int, size_t> > m_index;
std::vector<size_t> m_irrev;
@ -201,8 +202,8 @@ protected:
std::vector<int> m_rxntype;
mutable std::vector<std::map<size_t, doublereal> > m_rrxn;
mutable std::vector<std::map<size_t, doublereal> > m_prxn;
std::vector<std::map<size_t, doublereal> > m_rrxn;
std::vector<std::map<size_t, doublereal> > m_prxn;
/**
* Difference between the input global reactants order

View file

@ -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<Arrhenius> m_falloff_high_rates;
Rate1<Arrhenius> m_rates;
mutable std::map<size_t, std::pair<int, size_t> > m_index;
std::map<size_t, std::pair<int, size_t> > m_index;
FalloffMgr m_falloffn;
@ -191,8 +191,8 @@ protected:
std::vector<int> m_rxntype;
mutable std::vector<std::map<size_t, doublereal> > m_rrxn;
mutable std::vector<std::map<size_t, doublereal> > m_prxn;
std::vector<std::map<size_t, doublereal> > m_rrxn;
std::vector<std::map<size_t, doublereal> > m_prxn;
/**
* Difference between the input global reactants order

View file

@ -478,7 +478,7 @@ protected:
* The first pair is the reactionType of the reaction.
* The second pair is ...
*/
mutable std::map<size_t, std::pair<int, size_t> > m_index;
std::map<size_t, std::pair<int, size_t> > 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<std::map<size_t, doublereal> > m_rrxn;
std::vector<std::map<size_t, doublereal> > 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<std::map<size_t, doublereal> > m_prxn;
std::vector<std::map<size_t, doublereal> > m_prxn;
//! Vector of reactionType for the reactions defined within this object
/*!

View file

@ -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<size_t, size_t> m_loc;
std::map<size_t, size_t> m_loc;
//! Map between the vector index where the coefficients are kept and the species index
/*!

View file

@ -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

View file

@ -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<NasaPoly1> &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<NasaPoly1> &mlg = m_low[grp-1];
const std::vector<NasaPoly1> &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<NasaPoly1> &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<NasaPoly1> &mlg = m_low[grp-1];
NasaPoly1* nlow = &(mlg[pos]);
std::vector<NasaPoly1> &mhg = m_high[grp-1];

View file

@ -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<size_t, size_t> m_group_map;
std::map<size_t, size_t> 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<size_t, size_t> m_posInGroup_map;
std::map<size_t, size_t> m_posInGroup_map;
//! Species name as a function of the species index
mutable std::map<size_t, std::string> m_name;
std::map<size_t, std::string> m_name;
protected:
//! Compute the nondimensional heat capacity using the given NASA polynomial

View file

@ -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<ShomatePoly> &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<ShomatePoly> &mlg = m_low[grp-1];
const std::vector<ShomatePoly> &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<ShomatePoly> &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<size_t, size_t> m_group_map;
std::map<size_t, size_t> 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<size_t, size_t> m_posInGroup_map;
std::map<size_t, size_t> m_posInGroup_map;
};
}