diff --git a/include/cantera/equil/MultiPhase.h b/include/cantera/equil/MultiPhase.h index 8849527e2..7335ec95e 100644 --- a/include/cantera/equil/MultiPhase.h +++ b/include/cantera/equil/MultiPhase.h @@ -132,6 +132,15 @@ public: return m_nel; } + //! Check that the specified element index is in range + //! Throws an exception if m is greater than nElements()-1 + void checkElementIndex(size_t m) const; + + //! Check that an array size is at least nElements() + //! Throws an exception if mm is less than nElements(). Used before calls + //! which take an array pointer. + void checkElementArraySize(size_t mm) const; + //! Returns the string name of the global element \a m. /*! * @param m index of the global element @@ -149,6 +158,15 @@ public: return m_nsp; } + //! Check that the specified species index is in range + //! Throws an exception if k is greater than nSpecies()-1 + void checkSpeciesIndex(size_t k) const; + + //! Check that an array size is at least nSpecies() + //! Throws an exception if kk is less than nSpecies(). Used before calls + //! which take an array pointer. + void checkSpeciesArraySize(size_t kk) const; + //! Name of species with global index \a kGlob /*! * @param kGlob global species index @@ -222,6 +240,15 @@ public: */ phase_t& phase(index_t n); + //! Check that the specified phase index is in range + //! Throws an exception if m is greater than nPhases() + void checkPhaseIndex(size_t m) const; + + //! Check that an array size is at least nPhases() + //! Throws an exception if mm is less than nPhases(). Used before calls + //! which take an array pointer. + void checkPhaseArraySize(size_t mm) const; + //! Returns the moles of global species \c k. /*! * Returns the moles of global species k. diff --git a/include/cantera/kinetics/Kinetics.h b/include/cantera/kinetics/Kinetics.h index 1dfeafd92..9798dca8a 100644 --- a/include/cantera/kinetics/Kinetics.h +++ b/include/cantera/kinetics/Kinetics.h @@ -214,6 +214,23 @@ public: return m_ii; } + //! Check that the specified reaction index is in range + //! Throws an exception if i is greater than nReactions() + void checkReactionIndex(size_t m) const; + + //! Check that an array size is at least nReactions() + //! Throws an exception if ii is less than nReactions(). Used before calls + //! which take an array pointer. + void checkReactionArraySize(size_t ii) const; + + //! Check that the specified species index is in range + //! Throws an exception if k is greater than nSpecies()-1 + void checkSpeciesIndex(size_t k) const; + + //! Check that an array size is at least nSpecies() + //! Throws an exception if kk is less than nSpecies(). Used before calls + //! which take an array pointer. + void checkSpeciesArraySize(size_t mm) const; //@} @@ -232,6 +249,15 @@ public: return m_thermo.size(); } + //! Check that the specified phase index is in range + //! Throws an exception if m is greater than nPhases() + void checkPhaseIndex(size_t m) const; + + //! Check that an array size is at least nPhases() + //! Throws an exception if mm is less than nPhases(). Used before calls + //! which take an array pointer. + void checkPhaseArraySize(size_t mm) const; + /** * Return the phase index of a phase in the list of phases * defined within the object. diff --git a/include/cantera/oneD/Domain1D.h b/include/cantera/oneD/Domain1D.h index c02b6ee32..1e11db3ed 100644 --- a/include/cantera/oneD/Domain1D.h +++ b/include/cantera/oneD/Domain1D.h @@ -177,11 +177,45 @@ public: return m_nv; } + //! Check that the specified component index is in range + //! Throws an exception if n is greater than nComponents()-1 + void checkComponentIndex(size_t n) const { + if (n >= m_nv) { + throw IndexError("checkComponentIndex", "points", n, m_nv-1); + } + } + + //! Check that an array size is at least nComponents() + //! Throws an exception if nn is less than nComponents(). Used before calls + //! which take an array pointer. + void checkComponentArraySize(size_t nn) const { + if (m_nv > nn) { + throw ArraySizeError("checkComponentArraySize", nn, m_nv); + } + } + /// Number of grid points in this domain. size_t nPoints() const { return m_points; } + //! Check that the specified point index is in range + //! Throws an exception if n is greater than nPoints()-1 + void checkPointIndex(size_t n) const { + if (n >= m_points) { + throw IndexError("checkPointIndex", "points", n, m_points-1); + } + } + + //! Check that an array size is at least nPoints() + //! Throws an exception if nn is less than nPoints(). Used before calls + //! which take an array pointer. + void checkPointArraySize(size_t nn) const { + if (m_points > nn) { + throw ArraySizeError("checkPointArraySize", nn, m_points); + } + } + /// Name of the nth component. May be overloaded. virtual std::string componentName(size_t n) const { if (m_name[n] != "") { diff --git a/include/cantera/oneD/OneDim.h b/include/cantera/oneD/OneDim.h index df8575fb1..560a60d49 100644 --- a/include/cantera/oneD/OneDim.h +++ b/include/cantera/oneD/OneDim.h @@ -60,6 +60,23 @@ public: size_t domainIndex(std::string name); + //! Check that the specified domain index is in range + //! Throws an exception if n is greater than nDomains()-1 + void checkDomainIndex(size_t n) const { + if (n >= m_nd) { + throw IndexError("checkDomainIndex", "domains", n, m_nd-1); + } + } + + //! Check that an array size is at least nDomains() + //! Throws an exception if nn is less than nDomains(). Used before calls + //! which take an array pointer. + void checkDomainArraySize(size_t nn) const { + if (m_nd > nn) { + throw ArraySizeError("checkDomainArraySize", nn, m_nd); + } + } + /// The index of the start of domain i in the solution vector. size_t start(size_t i) const { return m_dom[i]->loc(); diff --git a/include/cantera/thermo/Phase.h b/include/cantera/thermo/Phase.h index c2e079f36..4db630c1f 100644 --- a/include/cantera/thermo/Phase.h +++ b/include/cantera/thermo/Phase.h @@ -193,6 +193,15 @@ public: //! Number of elements. size_t nElements() const; + //! Check that the specified element index is in range + //! Throws an exception if m is greater than nElements()-1 + void checkElementIndex(size_t m) const; + + //! Check that an array size is at least nElements() + //! Throws an exception if mm is less than nElements(). Used before calls + //! which take an array pointer. + void checkElementArraySize(size_t mm) const; + //! Number of atoms of element \c m in species \c k. //! @param k species index //! @param m element index @@ -230,6 +239,17 @@ public: size_t nSpecies() const { return m_kk; } + + //! Check that the specified species index is in range + //! Throws an exception if k is greater than nSpecies()-1 + void checkSpeciesIndex(size_t k) const; + + //! Check that an array size is at least nSpecies() + //! Throws an exception if kk is less than nSpecies(). Used before calls + //! which take an array pointer. + void checkSpeciesArraySize(size_t kk) const; + + //!@} end group Element and Species Information //! Save the current internal state of the phase diff --git a/include/cantera/transport/TransportBase.h b/include/cantera/transport/TransportBase.h index 7cbd1b5cd..45b35ded9 100644 --- a/include/cantera/transport/TransportBase.h +++ b/include/cantera/transport/TransportBase.h @@ -262,6 +262,15 @@ public: return m_nDim; } + //! Check that the specified species index is in range + //! Throws an exception if k is greater than nSpecies() + void checkSpeciesIndex(size_t k) const; + + //! Check that an array size is at least nSpecies() + //! Throws an exception if kk is less than nSpecies(). Used before calls + //! which take an array pointer. + void checkSpeciesArraySize(size_t kk) const; + /** * @name Transport Properties */ diff --git a/src/clib/ct.cpp b/src/clib/ct.cpp index 84e420ff9..666388c06 100644 --- a/src/clib/ct.cpp +++ b/src/clib/ct.cpp @@ -182,12 +182,9 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (lenx >= p.nSpecies()) { - p.getMoleFractions(x); - return 0; - } else { - return -1; - } + p.checkSpeciesArraySize(lenx); + p.getMoleFractions(x); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -206,12 +203,9 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (leny >= p.nSpecies()) { - p.getMassFractions(y); - return 0; - } else { - return -1; - } + p.checkSpeciesArraySize(leny); + p.getMassFractions(y); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -230,16 +224,13 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (lenx >= p.nSpecies()) { - if (norm) { - p.setMoleFractions(x); - } else { - p.setMoleFractions_NoNorm(x); - } - return 0; + p.checkSpeciesArraySize(lenx); + if (norm) { + p.setMoleFractions(x); } else { - return -1; + p.setMoleFractions_NoNorm(x); } + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -267,16 +258,13 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (leny >= p.nSpecies()) { - if (norm) { - p.setMassFractions(y); - } else { - p.setMassFractions_NoNorm(y); - } - return 0; + p.checkSpeciesArraySize(leny); + if (norm) { + p.setMassFractions(y); } else { - return -10; + p.setMassFractions_NoNorm(y); } + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -303,13 +291,10 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (lenm >= p.nElements()) { - const vector_fp& wt = p.atomicWeights(); - copy(wt.begin(), wt.end(), atw); - return 0; - } else { - return -10; - } + p.checkElementArraySize(lenm); + const vector_fp& wt = p.atomicWeights(); + copy(wt.begin(), wt.end(), atw); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -319,13 +304,10 @@ extern "C" { { try { ThermoPhase& p = ThermoCabinet::item(n); - if (lenm >= p.nSpecies()) { - const vector_fp& wt = p.molecularWeights(); - copy(wt.begin(), wt.end(), mw); - return 0; - } else { - return -10; - } + p.checkElementArraySize(lenm); + const vector_fp& wt = p.molecularWeights(); + copy(wt.begin(), wt.end(), mw); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -562,13 +544,9 @@ extern "C" { { try { ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nsp = thrm.nSpecies(); - if (lenm >= nsp) { - thrm.getChemPotentials(murt); - return 0; - } else { - return -10; - } + thrm.checkSpeciesArraySize(lenm); + thrm.getChemPotentials(murt); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -578,14 +556,10 @@ extern "C" { { try { ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nel = thrm.nElements(); - if (lenm >= nel) { - equilibrate(thrm, "TP", 0); - thrm.getElementPotentials(lambda); - return 0; - } else { - return -10; - } + thrm.checkElementArraySize(lenm); + equilibrate(thrm, "TP", 0); + thrm.getElementPotentials(lambda); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -679,7 +653,13 @@ extern "C" { doublereal th_minTemp(int n, int k) { try { - return ThermoCabinet::item(n).minTemp(k); + ThermoPhase& ph = ThermoCabinet::item(n); + if (k != -1) { + ph.checkSpeciesIndex(k); + return ph.minTemp(k); + } else { + return ph.minTemp(); + } } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -688,7 +668,13 @@ extern "C" { doublereal th_maxTemp(int n, int k) { try { - return ThermoCabinet::item(n).maxTemp(k); + ThermoPhase& ph = ThermoCabinet::item(n); + if (k != -1) { + ph.checkSpeciesIndex(k); + return ph.maxTemp(k); + } else { + return ph.maxTemp(); + } } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -699,13 +685,9 @@ extern "C" { { try { ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nsp = thrm.nSpecies(); - if (lenm >= nsp) { - thrm.getEnthalpy_RT_ref(h_rt); - return 0; - } else { - return -10; - } + thrm.checkSpeciesArraySize(lenm); + thrm.getEnthalpy_RT_ref(h_rt); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -715,13 +697,9 @@ extern "C" { { try { ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nsp = thrm.nSpecies(); - if (lenm >= nsp) { - thrm.getEntropy_R_ref(s_r); - return 0; - } else { - return -10; - } + thrm.checkSpeciesArraySize(lenm); + thrm.getEntropy_R_ref(s_r); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -731,13 +709,9 @@ extern "C" { { try { ThermoPhase& thrm = ThermoCabinet::item(n); - size_t nsp = thrm.nSpecies(); - if (lenm >= nsp) { - thrm.getCp_R_ref(cp_r); - return 0; - } else { - return -10; - } + thrm.checkSpeciesArraySize(lenm); + thrm.getCp_R_ref(cp_r); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1000,7 +974,10 @@ extern "C" { double kin_reactantStoichCoeff(int n, int k, int i) { try { - return KineticsCabinet::item(n).reactantStoichCoeff(k,i); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkSpeciesIndex(k); + kin.checkReactionIndex(i); + return kin.reactantStoichCoeff(k,i); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -1009,7 +986,10 @@ extern "C" { double kin_productStoichCoeff(int n, int k, int i) { try { - return KineticsCabinet::item(n).productStoichCoeff(k,i); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkSpeciesIndex(k); + kin.checkReactionIndex(i); + return kin.productStoichCoeff(k,i); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -1018,7 +998,9 @@ extern "C" { int kin_reactionType(int n, int i) { try { - return KineticsCabinet::item(n).reactionType(i); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkReactionIndex(i); + return kin.reactionType(i); } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1028,12 +1010,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getFwdRatesOfProgress(fwdROP); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getFwdRatesOfProgress(fwdROP); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1043,12 +1022,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getRevRatesOfProgress(revROP); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getRevRatesOfProgress(revROP); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1057,7 +1033,9 @@ extern "C" { int kin_isReversible(int n, int i) { try { - return (int) KineticsCabinet::item(n).isReversible(i); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkReactionIndex(i); + return (int) kin.isReversible(i); } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1067,12 +1045,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getNetRatesOfProgress(netROP); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getNetRatesOfProgress(netROP); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1082,12 +1057,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getFwdRateConstants(kfwd); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getFwdRateConstants(kfwd); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1101,41 +1073,31 @@ extern "C" { if (doIrreversible != 0) { doirrev = true; } - if (len >= k.nReactions()) { - k.getRevRateConstants(krev, doirrev); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getRevRateConstants(krev, doirrev); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } } - int kin_getActivationEnergies(int n, size_t len, double* E) { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getActivationEnergies(E); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getActivationEnergies(E); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } } - int kin_getDelta(int n, int job, size_t len, double* delta) { try { Kinetics& k = KineticsCabinet::item(n); - if (len < k.nReactions()) { - return ERR; - } + k.checkReactionArraySize(len); switch (job) { case 0: k.getDeltaEnthalpy(delta); @@ -1164,33 +1126,25 @@ extern "C" { } } - int kin_getDeltaEntropy(int n, size_t len, double* deltaS) { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getDeltaEntropy(deltaS); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getDeltaEntropy(deltaS); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } } - int kin_getCreationRates(int n, size_t len, double* cdot) { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nTotalSpecies()) { - k.getCreationRates(cdot); - return 0; - } else { - return ERR; - } + k.checkSpeciesArraySize(len); + k.getCreationRates(cdot); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1200,12 +1154,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nTotalSpecies()) { - k.getDestructionRates(ddot); - return 0; - } else { - return ERR; - } + k.checkSpeciesArraySize(len); + k.getDestructionRates(ddot); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1215,12 +1166,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nTotalSpecies()) { - k.getNetProductionRates(wdot); - return 0; - } else { - return ERR; - } + k.checkSpeciesArraySize(len); + k.getNetProductionRates(wdot); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1229,19 +1177,18 @@ extern "C" { int kin_getSourceTerms(int n, size_t len, double* ydot) { try { + // @todo This function only works for single phase kinetics Kinetics& k = KineticsCabinet::item(n); ThermoPhase& p = k.thermo(); const vector_fp& mw = p.molecularWeights(); size_t nsp = mw.size(); double rrho = 1.0/p.density(); - if (len >= nsp) { - k.getNetProductionRates(ydot); - multiply_each(ydot, ydot + nsp, mw.begin()); - scale(ydot, ydot + nsp, ydot, rrho); - return 0; - } else { - return ERR; - } + k.checkSpeciesArraySize(len); + k.checkSpeciesArraySize(nsp); + k.getNetProductionRates(ydot); + multiply_each(ydot, ydot + nsp, mw.begin()); + scale(ydot, ydot + nsp, ydot, rrho); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1250,7 +1197,9 @@ extern "C" { double kin_multiplier(int n, int i) { try { - return KineticsCabinet::item(n).multiplier(i); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkReactionIndex(i); + return kin.multiplier(i); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -1259,7 +1208,9 @@ extern "C" { size_t kin_phase(int n, size_t i) { try { - return ThermoCabinet::index(KineticsCabinet::item(n).thermo(i)); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkPhaseIndex(i); + return ThermoCabinet::index(kin.thermo(i)); } catch (...) { return handleAllExceptions(npos, npos); } @@ -1269,12 +1220,9 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); - if (len >= k.nReactions()) { - k.getEquilibriumConstants(kc); - return 0; - } else { - return ERR; - } + k.checkReactionArraySize(len); + k.getEquilibriumConstants(kc); + return 0; } catch (...) { return handleAllExceptions(-1, ERR); } @@ -1284,6 +1232,7 @@ extern "C" { { try { Kinetics& k = KineticsCabinet::item(n); + k.checkReactionIndex(i); string r = k.reactionString(i); int lout = min(len, (int)r.size()); copy(r.c_str(), r.c_str() + lout, buf); @@ -1298,7 +1247,9 @@ extern "C" { { try { if (v >= 0.0) { - KineticsCabinet::item(n).setMultiplier(i,v); + Kinetics& kin = KineticsCabinet::item(n); + kin.checkReactionIndex(i); + kin.setMultiplier(i,v); return 0; } else { return ERR; @@ -1326,8 +1277,7 @@ extern "C" { //------------------- Transport --------------------------- - size_t newTransport(char* model, - int ith, int loglevel) + size_t newTransport(char* model, int ith, int loglevel) { try { string mstr = string(model); @@ -1360,7 +1310,9 @@ extern "C" { int trans_getThermalDiffCoeffs(int n, int ldt, double* dt) { try { - TransportCabinet::item(n).getThermalDiffCoeffs(dt); + Transport& tr = TransportCabinet::item(n); + tr.checkSpeciesArraySize(ldt); + tr.getThermalDiffCoeffs(dt); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1370,7 +1322,9 @@ extern "C" { int trans_getMixDiffCoeffs(int n, int ld, double* d) { try { - TransportCabinet::item(n).getMixDiffCoeffs(d); + Transport& tr = TransportCabinet::item(n); + tr.checkSpeciesArraySize(ld); + tr.getMixDiffCoeffs(d); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1380,7 +1334,10 @@ extern "C" { int trans_getBinDiffCoeffs(int n, int ld, double* d) { try { - TransportCabinet::item(n).getBinaryDiffCoeffs(ld,d); + // @todo length of d should be passed for bounds checking + Transport& tr = TransportCabinet::item(n); + tr.checkSpeciesArraySize(ld); + tr.getBinaryDiffCoeffs(ld,d); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1390,7 +1347,10 @@ extern "C" { int trans_getMultiDiffCoeffs(int n, int ld, double* d) { try { - TransportCabinet::item(n).getMultiDiffCoeffs(ld,d); + // @todo length of d should be passed for bounds checking + Transport& tr = TransportCabinet::item(n); + tr.checkSpeciesArraySize(ld); + tr.getMultiDiffCoeffs(ld,d); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -1444,8 +1404,7 @@ extern "C" { } } - int import_kinetics(int nxml, char* id, - int nphases, integer* ith, int nkin) + int import_kinetics(int nxml, char* id, int nphases, integer* ith, int nkin) { try { vector phases; @@ -1463,8 +1422,7 @@ extern "C" { } - int phase_report(int nth, - int ibuf, char* buf, int show_thermo) + int phase_report(int nth, int ibuf, char* buf, int show_thermo) { try { bool stherm = (show_thermo != 0); @@ -1649,8 +1607,8 @@ extern "C" { } } - int ck_to_cti(char* in_file, char* db_file, - char* tr_file, char* id_tag, int debug, int validate) + int ck_to_cti(char* in_file, char* db_file, char* tr_file, + char* id_tag, int debug, int validate) { try { bool dbg = (debug != 0); diff --git a/src/clib/ctmultiphase.cpp b/src/clib/ctmultiphase.cpp index b6ba554c6..0c619ce1a 100644 --- a/src/clib/ctmultiphase.cpp +++ b/src/clib/ctmultiphase.cpp @@ -17,42 +17,6 @@ using namespace Cantera; typedef Cabinet mixCabinet; template<> mixCabinet* mixCabinet::__storage = 0; -static bool checkSpecies(int i, size_t k) -{ - try { - if (k >= mixCabinet::item(i).nSpecies()) - throw CanteraError("checkSpecies", - "illegal species index ("+int2str(k)+") "); - return true; - } catch (...) { - return Cantera::handleAllExceptions(false, false); - } -} - -static bool checkElement(int i, size_t m) -{ - try { - if (m >= mixCabinet::item(i).nElements()) - throw CanteraError("checkElement", - "illegal element index ("+int2str(m)+") "); - return true; - } catch (...) { - return Cantera::handleAllExceptions(false, false); - } -} - -static bool checkPhase(int i, int n) -{ - try { - if (n < 0 || n >= int(mixCabinet::item(i).nPhases())) - throw CanteraError("checkPhase", - "illegal phase index ("+int2str(n)+") "); - return true; - } catch (...) { - return Cantera::handleAllExceptions(false, false); - } -} - extern "C" { int mix_new() @@ -143,7 +107,10 @@ extern "C" { size_t mix_speciesIndex(int i, int k, int p) { try { - return mixCabinet::item(i).speciesIndex(k, p); + MultiPhase& mix = mixCabinet::item(i); + mix.checkPhaseIndex(p); + mix.checkSpeciesIndex(k); + return mix.speciesIndex(k, p); } catch (...) { return handleAllExceptions(npos, npos); } @@ -152,12 +119,10 @@ extern "C" { doublereal mix_nAtoms(int i, int k, int m) { try { - bool ok = (checkSpecies(i,k) && checkElement(i,m)); - if (ok) { - return mixCabinet::item(i).nAtoms(k,m); - } else { - return DERR; - } + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesIndex(k); + mix.checkElementIndex(m); + return mixCabinet::item(i).nAtoms(k,m); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -175,10 +140,9 @@ extern "C" { doublereal mix_phaseMoles(int i, int n) { try { - if (!checkPhase(i, n)) { - return DERR; - } - return mixCabinet::item(i).phaseMoles(n); + MultiPhase& mix = mixCabinet::item(i); + mix.checkPhaseIndex(n); + return mix.phaseMoles(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -187,13 +151,12 @@ extern "C" { int mix_setPhaseMoles(int i, int n, double v) { try { - if (!checkPhase(i, n)) { - return ERR; - } + MultiPhase& mix = mixCabinet::item(i); + mix.checkPhaseIndex(n); if (v < 0.0) { return -1; } - mixCabinet::item(i).setPhaseMoles(n, v); + mix.setPhaseMoles(n, v); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -203,10 +166,9 @@ extern "C" { int mix_setMoles(int i, size_t nlen, double* n) { try { - if (nlen < mixCabinet::item(i).nSpecies()) { - throw CanteraError("setMoles","array size too small."); - } - mixCabinet::item(i).setMoles(n); + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesArraySize(nlen); + mix.setMoles(n); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -276,10 +238,9 @@ extern "C" { doublereal mix_phaseCharge(int i, int p) { try { - if (!checkPhase(i,p)) { - return DERR; - } - return mixCabinet::item(i).phaseCharge(p); + MultiPhase& mix = mixCabinet::item(i); + mix.checkPhaseIndex(p); + return mix.phaseCharge(p); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -310,10 +271,9 @@ extern "C" { doublereal mix_speciesMoles(int i, int k) { try { - if (!checkSpecies(i,k)) { - return DERR; - } - return mixCabinet::item(i).speciesMoles(k); + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesIndex(k); + return mix.speciesMoles(k); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -322,19 +282,16 @@ extern "C" { doublereal mix_elementMoles(int i, int m) { try { - if (!checkElement(i,m)) { - return DERR; - } - return mixCabinet::item(i).elementMoles(m); + MultiPhase& mix = mixCabinet::item(i); + mix.checkElementIndex(m); + return mix.elementMoles(m); } catch (...) { return handleAllExceptions(DERR, DERR); } } - - doublereal mix_equilibrate(int i, char* XY, - doublereal rtol, int maxsteps, - int maxiter, int loglevel) + doublereal mix_equilibrate(int i, char* XY, doublereal rtol, int maxsteps, + int maxiter, int loglevel) { try { return equilibrate(mixCabinet::item(i), XY, @@ -344,11 +301,9 @@ extern "C" { } } - doublereal mix_vcs_equilibrate(int i, char* XY, int estimateEquil, - int printLvl, int solver, - doublereal rtol, int maxsteps, - int maxiter, int loglevel) + int printLvl, int solver, doublereal rtol, + int maxsteps, int maxiter, int loglevel) { try { #ifdef WITH_VCSNONIDEAL @@ -369,10 +324,9 @@ extern "C" { int mix_getChemPotentials(int i, size_t lenmu, double* mu) { try { - if (lenmu < mixCabinet::item(i).nSpecies()) { - throw CanteraError("getChemPotentials","array too small"); - } - mixCabinet::item(i).getChemPotentials(mu); + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesArraySize(lenmu); + mix.getChemPotentials(mu); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -380,13 +334,12 @@ extern "C" { } int mix_getValidChemPotentials(int i, double bad_mu, - int standard, size_t lenmu, double* mu) + int standard, size_t lenmu, double* mu) { try { bool st = (standard == 1); - if (lenmu < mixCabinet::item(i).nSpecies()) { - throw CanteraError("getChemPotentials","array too small"); - } + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesArraySize(lenmu); mixCabinet::item(i).getValidChemPotentials(bad_mu, mu, st); return 0; } catch (...) { @@ -442,7 +395,9 @@ extern "C" { size_t mix_speciesPhaseIndex(int i, int k) { try { - return mixCabinet::item(i).speciesPhaseIndex(k); + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesIndex(k); + return mix.speciesPhaseIndex(k); } catch (...) { return handleAllExceptions(npos, npos); } @@ -451,7 +406,9 @@ extern "C" { double mix_moleFraction(int i, int k) { try { - return mixCabinet::item(i).moleFraction(k); + MultiPhase& mix = mixCabinet::item(i); + mix.checkSpeciesIndex(k); + return mix.moleFraction(k); } catch (...) { return handleAllExceptions(DERR, DERR); } diff --git a/src/clib/ctonedim.cpp b/src/clib/ctonedim.cpp index a5068f082..1716620a2 100644 --- a/src/clib/ctonedim.cpp +++ b/src/clib/ctonedim.cpp @@ -106,7 +106,9 @@ extern "C" { int domain_componentName(int i, int n, int sz, char* buf) { try { - string nm = DomainCabinet::item(i).componentName(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + string nm = dom.componentName(n); size_t lout = std::min(sz, nm.size()); copy(nm.c_str(), nm.c_str() + lout, buf); buf[lout] = '\0'; @@ -119,8 +121,7 @@ extern "C" { size_t domain_componentIndex(int i, char* name) { try { - size_t n = DomainCabinet::item(i).componentIndex(string(name)); - return n; + return DomainCabinet::item(i).componentIndex(string(name)); } catch (...) { return handleAllExceptions(-1, ERR); } @@ -129,7 +130,9 @@ extern "C" { double domain_grid(int i, int n) { try { - return DomainCabinet::item(i).grid(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkPointIndex(n); + return dom.grid(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -138,7 +141,9 @@ extern "C" { int domain_setBounds(int i, int n, double lower, double upper) { try { - DomainCabinet::item(i).setBounds(n, lower, upper); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + dom.setBounds(n, lower, upper); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -148,7 +153,9 @@ extern "C" { double domain_upperBound(int i, int n) { try { - return DomainCabinet::item(i).upperBound(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + return dom.upperBound(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -157,7 +164,9 @@ extern "C" { double domain_lowerBound(int i, int n) { try { - return DomainCabinet::item(i).lowerBound(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + return dom.lowerBound(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -167,7 +176,9 @@ extern "C" { double atol, int itime) { try { - DomainCabinet::item(i).setTolerances(n, rtol, atol, itime); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + dom.setTolerances(n, rtol, atol, itime); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -177,7 +188,9 @@ extern "C" { double domain_rtol(int i, int n) { try { - return DomainCabinet::item(i).rtol(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + return dom.rtol(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -186,7 +199,9 @@ extern "C" { double domain_atol(int i, int n) { try { - return DomainCabinet::item(i).atol(n); + Domain1D& dom = DomainCabinet::item(i); + dom.checkComponentIndex(n); + return dom.atol(n); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -195,6 +210,9 @@ extern "C" { int domain_setupGrid(int i, size_t npts, double* grid) { try { + Domain1D& dom = DomainCabinet::item(i); + dom.checkPointIndex(npts-1); + dom.checkPointArraySize(npts); DomainCabinet::item(i).setupGrid(npts, grid); return 0; } catch (...) { @@ -249,7 +267,6 @@ extern "C" { int reactingsurf_new() { try { - //writelog("in reactingsurf_new\n"); Domain1D* i = new ReactingSurf1D(); return DomainCabinet::add(i); } catch (...) { @@ -525,8 +542,7 @@ extern "C" { } } - int sim1D_setValue(int i, int dom, int comp, - int localPoint, double value) + int sim1D_setValue(int i, int dom, int comp, int localPoint, double value) { try { SimCabinet::item(i).setValue(dom, comp, localPoint, value); @@ -537,15 +553,18 @@ extern "C" { } int sim1D_setProfile(int i, int dom, int comp, - size_t np, double* pos, size_t nv, double* v) + size_t np, double* pos, size_t nv, double* v) { try { + Sim1D& sim = SimCabinet::item(i); + sim.checkDomainIndex(dom); + sim.domain(dom).checkComponentIndex(comp); vector_fp vv, pv; for (size_t n = 0; n < np; n++) { vv.push_back(v[n]); pv.push_back(pos[n]); } - SimCabinet::item(i).setProfile(dom, comp, pv, vv); + sim.setProfile(dom, comp, pv, vv); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -555,7 +574,10 @@ extern "C" { int sim1D_setFlatProfile(int i, int dom, int comp, double v) { try { - SimCabinet::item(i).setFlatProfile(dom, comp, v); + Sim1D& sim = SimCabinet::item(i); + sim.checkDomainIndex(dom); + sim.domain(dom).checkComponentIndex(comp); + sim.setFlatProfile(dom, comp, v); return 0; } catch (...) { return handleAllExceptions(-1, ERR); @@ -621,7 +643,7 @@ extern "C" { } int sim1D_setRefineCriteria(int i, int dom, double ratio, - double slope, double curve, double prune) + double slope, double curve, double prune) { try { SimCabinet::item(i).setRefineCriteria(dom, ratio, slope, curve, prune); @@ -631,8 +653,7 @@ extern "C" { } } - int sim1D_save(int i, char* fname, char* id, - char* desc) + int sim1D_save(int i, char* fname, char* id, char* desc) { try { string sname = string(fname); @@ -679,7 +700,10 @@ extern "C" { double sim1D_value(int i, int idom, int icomp, int localPoint) { try { - return SimCabinet::item(i).value(idom, icomp, localPoint); + Sim1D& sim = SimCabinet::item(i); + sim.checkDomainIndex(idom); + sim.domain(idom).checkComponentIndex(icomp); + return sim.value(idom, icomp, localPoint); } catch (...) { return handleAllExceptions(DERR, DERR); } @@ -688,7 +712,10 @@ extern "C" { double sim1D_workValue(int i, int idom, int icomp, int localPoint) { try { - return SimCabinet::item(i).workValue(idom, icomp, localPoint); + Sim1D& sim = SimCabinet::item(i); + sim.checkDomainIndex(idom); + sim.domain(idom).checkComponentIndex(icomp); + return sim.workValue(idom, icomp, localPoint); } catch (...) { return handleAllExceptions(DERR, DERR); } diff --git a/src/equil/MultiPhase.cpp b/src/equil/MultiPhase.cpp index 86adfbed7..a0feadebf 100644 --- a/src/equil/MultiPhase.cpp +++ b/src/equil/MultiPhase.cpp @@ -267,6 +267,21 @@ MultiPhase::phase_t& MultiPhase::phase(index_t n) m_phase[n]->setPressure(m_press); return *m_phase[n]; } + +void MultiPhase::checkPhaseIndex(size_t m) const +{ + if (m >= nPhases()) { + throw IndexError("checkPhaseIndex", "phase", m, nPhases()-1); + } +} + +void MultiPhase::checkPhaseArraySize(size_t mm) const +{ + if (nPhases() > mm) { + throw ArraySizeError("checkPhaseIndex", mm, nPhases()); + } +} + //==================================================================================================================== /// Moles of species \c k. doublereal MultiPhase::speciesMoles(index_t k) const @@ -1007,6 +1022,21 @@ void MultiPhase::setTemperature(const doublereal T) m_temp = T; updatePhases(); } + +void MultiPhase::checkElementIndex(size_t m) const +{ + if (m >= m_nel) { + throw IndexError("checkElementIndex", "elements", m, m_nel-1); + } +} + +void MultiPhase::checkElementArraySize(size_t mm) const +{ + if (m_nel > mm) { + throw ArraySizeError("checkElementArraySize", mm, m_nel); + } +} + //==================================================================================================================== // Name of element \a m. std::string MultiPhase::elementName(size_t m) const @@ -1024,6 +1054,21 @@ size_t MultiPhase::elementIndex(std::string name) const } return npos; } + +void MultiPhase::checkSpeciesIndex(size_t k) const +{ + if (k >= m_nsp) { + throw IndexError("checkSpeciesIndex", "species", k, m_nsp-1); + } +} + +void MultiPhase::checkSpeciesArraySize(size_t kk) const +{ + if (m_nsp > kk) { + throw ArraySizeError("checkSpeciesArraySize", kk, m_nsp); + } +} + //==================================================================================================================== // Name of species with global index \a k. std::string MultiPhase::speciesName(const size_t k) const diff --git a/src/kinetics/Kinetics.cpp b/src/kinetics/Kinetics.cpp index 460e3bb77..b064401f5 100644 --- a/src/kinetics/Kinetics.cpp +++ b/src/kinetics/Kinetics.cpp @@ -130,6 +130,49 @@ int Kinetics::type() const { return 0; } + +void Kinetics::checkReactionIndex(size_t i) const +{ + if (i >= m_ii) { + throw IndexError("checkReactionIndex", "reactions", i, m_ii-1); + } +} + +void Kinetics::checkReactionArraySize(size_t ii) const +{ + if (m_ii > ii) { + throw ArraySizeError("checkReactionArraySize", ii, m_ii); + } +} + +void Kinetics::checkPhaseIndex(size_t m) const +{ + if (m >= nPhases()) { + throw IndexError("checkPhaseIndex", "phase", m, nPhases()-1); + } +} + +void Kinetics::checkPhaseArraySize(size_t mm) const +{ + if (nPhases() > mm) { + throw ArraySizeError("checkPhaseArraySize", mm, nPhases()); + } +} + +void Kinetics::checkSpeciesIndex(size_t k) const +{ + if (k >= m_kk) { + throw IndexError("checkSpeciesIndex", "species", k, m_kk-1); + } +} + +void Kinetics::checkSpeciesArraySize(size_t kk) const +{ + if (m_kk > kk) { + throw ArraySizeError("checkSpeciesArraySize", kk, m_kk); + } +} + //==================================================================================================================== void Kinetics::assignShallowPointers(const std::vector & tpVector) { diff --git a/src/thermo/Phase.cpp b/src/thermo/Phase.cpp index 76fe6ab86..d0393f67b 100644 --- a/src/thermo/Phase.cpp +++ b/src/thermo/Phase.cpp @@ -152,12 +152,23 @@ size_t Phase::nElements() const return m_mm; } +void Phase::checkElementIndex(size_t m) const +{ + if (m >= m_mm) { + throw IndexError("checkElementIndex", "elements", m, m_mm-1); + } +} + +void Phase::checkElementArraySize(size_t mm) const +{ + if (m_mm > mm) { + throw ArraySizeError("checkElementArraySize", mm, m_mm); + } +} + string Phase::elementName(size_t m) const { - if (m >= nElements()) { - throw IndexError("Elements::elementName", "m_elementNames", - m, nElements()); - } + checkElementIndex(m); return m_elementNames[m]; } @@ -214,12 +225,8 @@ int Phase::changeElementType(int m, int elem_type) doublereal Phase::nAtoms(size_t k, size_t m) const { - if (m >= m_mm) { - throw IndexError("Phase::nAtoms", "", m, nElements()); - } - if (k >= nSpecies()) { - throw IndexError("Phase::nAtoms", "", k, nSpecies()); - } + checkElementIndex(m); + checkSpeciesIndex(k); return m_speciesComp[m_mm * k + m]; } @@ -249,8 +256,7 @@ size_t Phase::speciesIndex(std::string nameStr) const string Phase::speciesName(size_t k) const { - if (k >= nSpecies()) - throw IndexError("Phase::speciesName", "m_speciesNames", k, nSpecies()); + checkSpeciesIndex(k); return m_speciesNames[k]; } @@ -259,6 +265,20 @@ const vector& Phase::speciesNames() const return m_speciesNames; } +void Phase::checkSpeciesIndex(size_t k) const +{ + if (k >= m_kk) { + throw IndexError("checkSpeciesIndex", "species", k, m_kk-1); + } +} + +void Phase::checkSpeciesArraySize(size_t kk) const +{ + if (m_kk > kk) { + throw ArraySizeError("checkSpeciesArraySize", kk, m_kk); + } +} + std::string Phase::speciesSPName(int k) const { std::string sn = speciesName(k); @@ -466,9 +486,7 @@ void Phase::setState_RY(doublereal rho, doublereal* y) doublereal Phase::molecularWeight(size_t k) const { - if (k >= nSpecies()) { - throw IndexError("Phase::molecularWeight", "m_weight", k, nSpecies()); - } + checkSpeciesIndex(k); return m_molwts[k]; } @@ -514,13 +532,8 @@ void Phase::getMoleFractions(doublereal* const x) const doublereal Phase::moleFraction(size_t k) const { - if (k < m_kk) { - return m_ym[k] * m_mmw; - } else { - throw CanteraError("Phase::moleFraction", - "illegal species index number"); - } - return 0.0; + checkSpeciesIndex(k); + return m_ym[k] * m_mmw; } doublereal Phase::moleFraction(std::string nameSpec) const @@ -540,11 +553,8 @@ const doublereal* Phase::moleFractdivMMW() const doublereal Phase::massFraction(size_t k) const { - if (k < m_kk) { - return m_y[k]; - } - throw CanteraError("State:massFraction", "illegal species index number"); - return 0.0; + checkSpeciesIndex(k); + return m_y[k]; } doublereal Phase::massFraction(std::string nameSpec) const @@ -564,11 +574,8 @@ void Phase::getMassFractions(doublereal* const y) const doublereal Phase::concentration(const size_t k) const { - if (k < m_kk) { - return m_y[k] * m_dens * m_rmolwts[k] ; - } - throw CanteraError("State:massFraction", "illegal species index number"); - return 0.0; + checkSpeciesIndex(k); + return m_y[k] * m_dens * m_rmolwts[k] ; } void Phase::getConcentrations(doublereal* const c) const diff --git a/src/transport/TransportBase.cpp b/src/transport/TransportBase.cpp index 3683a59d2..caef3bfc1 100644 --- a/src/transport/TransportBase.cpp +++ b/src/transport/TransportBase.cpp @@ -85,6 +85,20 @@ void Transport::setNDim(const int ndim) m_nDim = ndim; } +void Transport::checkSpeciesIndex(size_t k) const +{ + if (k >= m_nsp) { + throw IndexError("checkSpeciesIndex", "species", k, m_nsp-1); + } +} + +void Transport::checkSpeciesArraySize(size_t kk) const +{ + if (m_nsp > kk) { + throw ArraySizeError("checkSpeciesArraySize", kk, m_nsp); + } +} + /* Set transport model parameters. This method may be * overloaded in subclasses to set model-specific parameters. */ diff --git a/src/zeroD/Reactor.cpp b/src/zeroD/Reactor.cpp index 5a716878d..17644534c 100644 --- a/src/zeroD/Reactor.cpp +++ b/src/zeroD/Reactor.cpp @@ -338,12 +338,12 @@ void Reactor::evalEqs(doublereal time, doublereal* y, void Reactor::addSensitivityReaction(size_t rxn) { - m_pnum.push_back(rxn); - m_pname.push_back(name()+": "+m_kin->reactionString(rxn)); - m_mult_save.push_back(1.0); if (rxn >= m_kin->nReactions()) throw CanteraError("Reactor::addSensitivityReaction", "Reaction number out of range ("+int2str(rxn)+")"); + m_pnum.push_back(rxn); + m_pname.push_back(name()+": "+m_kin->reactionString(rxn)); + m_mult_save.push_back(1.0); }