[Kinetics] Check for undeclared species when using Reaction objects

Skip the reaction or raise an exception based on the
'm_skipUndeclaredSpecies' flag
This commit is contained in:
Ray Speth 2014-11-15 00:46:59 +00:00
parent d72c2e7132
commit ffe3eb5e5a
3 changed files with 66 additions and 1 deletions

View file

@ -806,6 +806,14 @@ public:
*/
virtual void addReaction(shared_ptr<Reaction> 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<size_t, std::vector<grouplist_t> > m_rgroups;
std::map<size_t, std::vector<grouplist_t> > m_pgroups;

View file

@ -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<Reaction> 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();

View file

@ -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<ElementaryReaction> 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<ElementaryReaction> R(new ElementaryReaction(reac, prod, rate));
kin.skipUndeclaredSpecies(true);
kin.addReaction(R);
ASSERT_EQ(0, kin.nReactions());
}
class InterfaceKineticsFromScratch : public testing::Test
{
public: