From ffe3eb5e5affec95b003d00f8113a17aa9d89e20 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Sat, 15 Nov 2014 00:46:59 +0000 Subject: [PATCH] [Kinetics] Check for undeclared species when using Reaction objects Skip the reaction or raise an exception based on the 'm_skipUndeclaredSpecies' flag --- include/cantera/kinetics/Kinetics.h | 11 +++++++++ src/kinetics/Kinetics.cpp | 32 ++++++++++++++++++++++++++- test/kinetics/kineticsFromScratch.cpp | 24 ++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/include/cantera/kinetics/Kinetics.h b/include/cantera/kinetics/Kinetics.h index b2076f4d6..8844a679b 100644 --- a/include/cantera/kinetics/Kinetics.h +++ b/include/cantera/kinetics/Kinetics.h @@ -806,6 +806,14 @@ public: */ virtual void addReaction(shared_ptr r); + //! Determine behavior when adding a new reaction that contains species not + //! defined in any of the phases associated with this kinetics manager. If + //! set to true, the reaction will silently be ignored. If false, (the + //! default) an exception will be raised. + void skipUndeclaredSpecies(bool skip) { + m_skipUndeclaredSpecies = skip; + } + //! @deprecated To be removed after Cantera 2.2. No longer called as part //! of addReaction. virtual void installReagents(const ReactionData& r) { @@ -1073,6 +1081,9 @@ protected: //! Net rate-of-progress for each reaction vector_fp m_ropnet; + //! @see skipUndeclaredSpecies() + bool m_skipUndeclaredSpecies; + private: std::map > m_rgroups; std::map > m_pgroups; diff --git a/src/kinetics/Kinetics.cpp b/src/kinetics/Kinetics.cpp index b56b394ee..b8238c420 100644 --- a/src/kinetics/Kinetics.cpp +++ b/src/kinetics/Kinetics.cpp @@ -22,7 +22,8 @@ Kinetics::Kinetics() : m_thermo(0), m_surfphase(npos), m_rxnphase(npos), - m_mindim(4) + m_mindim(4), + m_skipUndeclaredSpecies(false) { } @@ -75,6 +76,7 @@ Kinetics& Kinetics::operator=(const Kinetics& right) m_ropf = right.m_ropf; m_ropr = right.m_ropr; m_ropnet = right.m_ropnet; + m_skipUndeclaredSpecies = right.m_skipUndeclaredSpecies; return *this; } @@ -648,6 +650,34 @@ void Kinetics::addReaction(ReactionData& r) { void Kinetics::addReaction(shared_ptr r) { + // Check for undeclared species + for (Composition::const_iterator iter = r->reactants.begin(); + iter != r->reactants.end(); + ++iter) { + if (kineticsSpeciesIndex(iter->first) == npos) { + if (m_skipUndeclaredSpecies) { + return; + } else { + throw CanteraError("Kinetics::addReaction", "Reaction '" + + r->equation() + "' contains the undeclared species '" + + iter->first + "'"); + } + } + } + for (Composition::const_iterator iter = r->products.begin(); + iter != r->products.end(); + ++iter) { + if (kineticsSpeciesIndex(iter->first) == npos) { + if (m_skipUndeclaredSpecies) { + return; + } else { + throw CanteraError("Kinetics::addReaction", "Reaction '" + + r->equation() + "' contains the undeclared species '" + + iter->first + "'"); + } + } + } + checkReactionBalance(*r); size_t irxn = nReactions(); diff --git a/test/kinetics/kineticsFromScratch.cpp b/test/kinetics/kineticsFromScratch.cpp index 3c35cd8ae..e24252acd 100644 --- a/test/kinetics/kineticsFromScratch.cpp +++ b/test/kinetics/kineticsFromScratch.cpp @@ -161,6 +161,30 @@ TEST_F(KineticsFromScratch, add_chebyshev_reaction) check_rates(4); } +TEST_F(KineticsFromScratch, undeclared_species) +{ + Composition reac = parseCompString("CO:1 OH:1"); + Composition prod = parseCompString("CO2:1 H:1"); + Arrhenius rate(3.87e1, 2.7, 6260.0 / GasConst_cal_mol_K); + shared_ptr R(new ElementaryReaction(reac, prod, rate)); + + ASSERT_THROW(kin.addReaction(R), CanteraError); + ASSERT_EQ(0, kin.nReactions()); +} + +TEST_F(KineticsFromScratch, skip_undeclared_species) +{ + Composition reac = parseCompString("CO:1 OH:1"); + Composition prod = parseCompString("CO2:1 H:1"); + Arrhenius rate(3.87e1, 2.7, 6260.0 / GasConst_cal_mol_K); + shared_ptr R(new ElementaryReaction(reac, prod, rate)); + + kin.skipUndeclaredSpecies(true); + kin.addReaction(R); + ASSERT_EQ(0, kin.nReactions()); +} + + class InterfaceKineticsFromScratch : public testing::Test { public: