Skip to content

Commit

Permalink
[Kinetics] tweak performance of MultiRate
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Mar 30, 2021
1 parent ed37e2e commit 6015710
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
26 changes: 13 additions & 13 deletions include/cantera/kinetics/MultiRate.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MultiBulkRates final : public MultiRateBase

virtual ReactionRateBase& rate(size_t rxn_index) override {
size_t j = m_indices[rxn_index];
return static_cast<ReactionRateBase&>(m_rates[j]);
return static_cast<ReactionRateBase&>(m_rxn_rates.at(j).second);
}

virtual void add(const size_t rxn_index,
Expand All @@ -85,25 +85,24 @@ class MultiBulkRates final : public MultiRateBase
"Reaction rate is already linked to a reaction rate "
"evaluator. Re-linked reaction rates are not allowed");
}
size_t j = m_rates.size();
size_t j = m_rxn_rates.size();
m_indices[rxn_index] = j;
m_rates.push_back(static_cast<RateType&>(*rate));
m_rxn.push_back(rxn_index);
m_rxn_rates.emplace_back(rxn_index, static_cast<RateType&>(*rate));
// keep link to original object
m_bases.push_back(rate);
}

virtual bool replace(const size_t rxn_index,
shared_ptr<ReactionRateBase> rate) override {
if (!m_rates.size()) {
if (!m_rxn_rates.size()) {
throw CanteraError("MultiBulkRate::replace",
"Invalid operation: cannot replace rate object "
"in empty rate handler.");
} else if (typeid(*rate) != typeid(RateType)) {
throw CanteraError("MultiBulkRate::replace",
"Invalid operation: cannot replace rate object of type '{}' "
"with a new rate of type '{}'.",
m_rates[0].type(), rate->type());
m_rxn_rates.at(0).second.type(), rate->type());
} else if (rate->linked()) {
// ensure there are no dangling objects (unlikely)
throw CanteraError("MultiBulkRate::replace",
Expand All @@ -112,7 +111,7 @@ class MultiBulkRates final : public MultiRateBase
}
if (m_indices.find(rxn_index) != m_indices.end()) {
size_t j = m_indices[rxn_index];
m_rates[j] = static_cast<RateType&>(*rate);
m_rxn_rates.at(j).second = static_cast<RateType&>(*rate);
// release evaluator from previously used rate object and update
m_bases[j]->releaseEvaluator();
m_bases[j] = rate;
Expand All @@ -123,8 +122,8 @@ class MultiBulkRates final : public MultiRateBase

virtual void getRateConstants(const ThermoPhase& bulk,
double* kf, double* concm) const override {
for (size_t i = 0; i < m_rates.size(); i++) {
kf[m_rxn[i]] = m_rates[i].eval(m_shared, concm[m_rxn[i]]);
for (const auto& rxn : m_rxn_rates) {
kf[rxn.first] = rxn.second.eval(m_shared, concm[rxn.first]);
}
}

Expand All @@ -135,8 +134,8 @@ class MultiBulkRates final : public MultiRateBase
// update reaction-specific data for each reaction. This loop
// is efficient as all function calls are de-virtualized, and
// all of the rate objects are contiguous in memory
for (size_t i = 0; i < m_rates.size(); i++) {
m_rates[i].update(m_shared, concm[m_rxn[i]]);
for (auto& rxn : m_rxn_rates) {
rxn.second.update(m_shared, concm[rxn.first]);
}
}
}
Expand All @@ -145,8 +144,9 @@ class MultiBulkRates final : public MultiRateBase
//! Raw pointers to reaction rate objects managed by Reaction object
std::vector<shared_ptr<ReactionRateBase>> m_bases;

std::vector<RateType> m_rates; //!< Reaction rate objects
std::vector<size_t> m_rxn; //!< Index within overall rate vector
//! Vector of pairs of reaction rates indices and reaction rates
std::vector<std::pair<size_t, RateType>> m_rxn_rates;

std::map<size_t, size_t> m_indices; //!< Mapping of indices
DataType m_shared;
};
Expand Down
4 changes: 0 additions & 4 deletions include/cantera/kinetics/Reaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,6 @@ class Reaction3 : public Reaction
virtual void setParameters(const AnyMap& node, const Kinetics& kin);

//! Get pointer to third-body
shared_ptr<ThirdBody> thirdBody() {
return m_third_body;
}

shared_ptr<ThirdBody> thirdBody() const {
return m_third_body;
}
Expand Down

0 comments on commit 6015710

Please sign in to comment.