Skip to content

Commit

Permalink
[Kinetics] make GasKinetics extendable
Browse files Browse the repository at this point in the history
- deprecate magic numbers
  • Loading branch information
ischoegl authored and Ingmar Schoegl committed Dec 28, 2019
1 parent 7c62752 commit b1bc0e3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 38 deletions.
20 changes: 20 additions & 0 deletions include/cantera/kinetics/GasKinetics.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ class GasKinetics : public BulkKinetics
//! reactions.
virtual void update_rates_C();

//! register a function that adds a reaction
void reg_addRxn(const std::string& name,
std::function<void(shared_ptr<Reaction>)> f) {
m_addRxn[name] = f;
}

//! register a function that modifies a reaction
void reg_modRxn(const std::string& name,
std::function<void(size_t, shared_ptr<Reaction>)> f) {
m_modRxn[name] = f;
}

protected:
//! Reaction index of each falloff reaction
std::vector<size_t> m_fallindx;
Expand Down Expand Up @@ -117,6 +129,14 @@ class GasKinetics : public BulkKinetics

//! Update the equilibrium constants in molar units.
void updateKc();

//! map functions adding reactions
std::unordered_map<std::string,
std::function<void(shared_ptr<Reaction>)>> m_addRxn;

//! map functions modifying reactions
std::unordered_map<std::string,
std::function<void(size_t, shared_ptr<Reaction>)>> m_modRxn;
};

}
Expand Down
99 changes: 61 additions & 38 deletions src/kinetics/GasKinetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,59 @@ GasKinetics::GasKinetics(thermo_t* thermo) :
m_logStandConc(0.0),
m_pres(0.0)
{
reg_addRxn("elementary",
[&](shared_ptr<Reaction> R) {
addElementaryReaction(dynamic_cast<ElementaryReaction&>(*R));
});
reg_modRxn("elementary",
[&](size_t i, shared_ptr<Reaction> R) {
modifyElementaryReaction(i, dynamic_cast<ElementaryReaction&>(*R));
});

reg_addRxn("three-body",
[&](shared_ptr<Reaction> R) {
addThreeBodyReaction(dynamic_cast<ThreeBodyReaction&>(*R));
});
reg_modRxn("three-body",
[&](size_t i, shared_ptr<Reaction> R) {
modifyThreeBodyReaction(i, dynamic_cast<ThreeBodyReaction&>(*R));
});

reg_addRxn("falloff",
[&](shared_ptr<Reaction> R) {
addFalloffReaction(dynamic_cast<FalloffReaction&>(*R));
});
reg_modRxn("falloff",
[&](size_t i, shared_ptr<Reaction> R) {
modifyFalloffReaction(i, dynamic_cast<FalloffReaction&>(*R));
});

reg_addRxn("chemically-activated",
[&](shared_ptr<Reaction> R) {
addFalloffReaction(dynamic_cast<FalloffReaction&>(*R));
});
reg_modRxn("chemically-activated",
[&](size_t i, shared_ptr<Reaction> R) {
modifyFalloffReaction(i, dynamic_cast<FalloffReaction&>(*R));
});

reg_addRxn("pressure-dependent-Arrhenius",
[&](shared_ptr<Reaction> R) {
addPlogReaction(dynamic_cast<PlogReaction&>(*R));
});

reg_modRxn("pressure-dependent-Arrhenius",
[&](size_t i, shared_ptr<Reaction> R) {
modifyPlogReaction(i, dynamic_cast<PlogReaction&>(*R));
});
reg_addRxn("Chebyshev",
[&](shared_ptr<Reaction> R) {
addChebyshevReaction(dynamic_cast<ChebyshevReaction&>(*R));
});
reg_modRxn("Chebyshev",
[&](size_t i, shared_ptr<Reaction> R) {
modifyChebyshevReaction(i, dynamic_cast<ChebyshevReaction&>(*R));
});
}

void GasKinetics::update_rates_T()
Expand Down Expand Up @@ -230,26 +283,11 @@ bool GasKinetics::addReaction(shared_ptr<Reaction> r)
return false;
}

switch (r->reaction_type) {
case ELEMENTARY_RXN:
addElementaryReaction(dynamic_cast<ElementaryReaction&>(*r));
break;
case THREE_BODY_RXN:
addThreeBodyReaction(dynamic_cast<ThreeBodyReaction&>(*r));
break;
case FALLOFF_RXN:
case CHEMACT_RXN:
addFalloffReaction(dynamic_cast<FalloffReaction&>(*r));
break;
case PLOG_RXN:
addPlogReaction(dynamic_cast<PlogReaction&>(*r));
break;
case CHEBYSHEV_RXN:
addChebyshevReaction(dynamic_cast<ChebyshevReaction&>(*r));
break;
default:
try {
m_addRxn[r->type()](r);
} catch (std::out_of_range&) {
throw CanteraError("GasKinetics::addReaction",
"Unknown reaction type specified: {}", r->reaction_type);
"Unknown reaction type specified: '{}'", r->type());
}
return true;
}
Expand Down Expand Up @@ -323,26 +361,11 @@ void GasKinetics::modifyReaction(size_t i, shared_ptr<Reaction> rNew)
// operations common to all reaction types
BulkKinetics::modifyReaction(i, rNew);

switch (rNew->reaction_type) {
case ELEMENTARY_RXN:
modifyElementaryReaction(i, dynamic_cast<ElementaryReaction&>(*rNew));
break;
case THREE_BODY_RXN:
modifyThreeBodyReaction(i, dynamic_cast<ThreeBodyReaction&>(*rNew));
break;
case FALLOFF_RXN:
case CHEMACT_RXN:
modifyFalloffReaction(i, dynamic_cast<FalloffReaction&>(*rNew));
break;
case PLOG_RXN:
modifyPlogReaction(i, dynamic_cast<PlogReaction&>(*rNew));
break;
case CHEBYSHEV_RXN:
modifyChebyshevReaction(i, dynamic_cast<ChebyshevReaction&>(*rNew));
break;
default:
try {
m_modRxn[rNew->type()](i, rNew);
} catch (std::out_of_range&) {
throw CanteraError("GasKinetics::modifyReaction",
"Unknown reaction type specified: {}", rNew->reaction_type);
"Unknown reaction type specified: '{}'", rNew->type());
}

// invalidate all cached data
Expand Down

0 comments on commit b1bc0e3

Please sign in to comment.