Skip to content

Commit

Permalink
[Kinetics] Fix duplicate reaction check to handle unchanged species
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Aug 2, 2017
1 parent 513b432 commit 4c489c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 4 additions & 0 deletions interfaces/cython/cantera/test/test_kinetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ def test_declared_duplicate(self):
def test_unmatched_duplicate(self):
self.check('J')

def test_nonreacting_species(self):
gas = ct.Solution(self.infile, 'K')
self.assertEqual(gas.n_reactions, 3)


class TestReaction(utilities.CanteraTest):
@classmethod
Expand Down
28 changes: 16 additions & 12 deletions src/kinetics/Kinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,23 @@ std::pair<size_t, size_t> Kinetics::checkDuplicates(bool throw_err) const
double Kinetics::checkDuplicateStoich(std::map<int, double>& r1,
std::map<int, double>& r2) const
{
auto b = r1.begin(), e = r1.end();
int k1 = b->first;
std::unordered_set<int> keys; // species keys (k+1 or -k-1)
for (auto& r : r1) {
keys.insert(r.first);
}
for (auto& r : r2) {
keys.insert(r.first);
}
int k1 = r1.begin()->first;
// check for duplicate written in the same direction
doublereal ratio = 0.0;
if (r1[k1] && r2[k1]) {
ratio = r2[k1]/r1[k1];
++b;
bool different = false;
for (; b != e; ++b) {
k1 = b->first;
if (!r1[k1] || !r2[k1] || fabs(r2[k1]/r1[k1] - ratio) > 1.e-8) {
for (int k : keys) {
if ((r1[k] && !r2[k]) ||
(!r1[k] && r2[k]) ||
(r1[k] && fabs(r2[k]/r1[k] - ratio) > 1.e-8)) {
different = true;
break;
}
Expand All @@ -201,16 +207,14 @@ double Kinetics::checkDuplicateStoich(std::map<int, double>& r1,
}

// check for duplicate written in the reverse direction
b = r1.begin();
k1 = b->first;
if (r1[k1] == 0.0 || r2[-k1] == 0.0) {
return 0.0;
}
ratio = r2[-k1]/r1[k1];
++b;
for (; b != e; ++b) {
k1 = b->first;
if (!r1[k1] || !r2[-k1] || fabs(r2[-k1]/r1[k1] - ratio) > 1.e-8) {
for (int k : keys) {
if ((r1[k] && !r2[-k]) ||
(!r1[k] && r2[-k]) ||
(r1[k] && fabs(r2[-k]/r1[k] - ratio) > 1.e-8)) {
return 0.0;
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/data/duplicate-reactions.cti
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ make_gas('G', 'G-*')
make_gas('H', 'H-*')
make_gas('I', 'I-*')
make_gas('J', ['C-1', 'F-1', 'I-1']) # unmatched duplicate
make_gas('K', 'K-*')

kf = [1e10, 0, 100]

Expand Down Expand Up @@ -60,3 +61,8 @@ three_body_reaction('O + H2 + M <=> H + OH + M', kf, id='H-2')
# Declared duplicate (allowed)
reaction('O + H2 <=> H + OH', kf, options='duplicate', id='I-1')
reaction('H + OH <=> O + H2', kf, options='duplicate', id='I-2')

# Not actually duplicates
reaction('O + 2 H <=> H2O', kf, id='K-0') # random other reaction
reaction('2 OH + H2 <=> H2O2 + H2', kf, id='K-1')
reaction('2 OH <=> H2O2', kf, id='K-2')

0 comments on commit 4c489c1

Please sign in to comment.