Cantera 2.6.0
ThirdBodyCalc.h
Go to the documentation of this file.
1/**
2 * @file ThirdBodyCalc.h
3 */
4
5// This file is part of Cantera. See License.txt in the top-level directory or
6// at https://cantera.org/license.txt for license and copyright information.
7
8#ifndef CT_THIRDBODYCALC_H
9#define CT_THIRDBODYCALC_H
10
12
13namespace Cantera
14{
15
16//! Calculate and apply third-body effects on reaction rates, including non-
17//! unity third-body efficiencies.
18//! Used by legacy reactions
20{
21public:
22 void install(size_t rxnNumber, const std::map<size_t, double>& enhanced,
23 double dflt=1.0, size_t rxnIndex=npos) {
24 m_reaction_index.push_back(rxnNumber);
25 m_default.push_back(dflt);
26
27 m_species.emplace_back();
28 m_eff.emplace_back();
29 for (const auto& eff : enhanced) {
30 AssertTrace(eff.first != npos);
31 m_species.back().push_back(eff.first);
32 m_eff.back().push_back(eff.second - dflt);
33 }
34 if (rxnIndex == npos) {
35 m_true_index.push_back(rxnNumber);
36 } else {
37 m_true_index.push_back(rxnIndex);
38 }
39 }
40
41 void update(const vector_fp& conc, double ctot, double* work) {
42 for (size_t i = 0; i < m_species.size(); i++) {
43 double sum = 0.0;
44 for (size_t j = 0; j < m_species[i].size(); j++) {
45 sum += m_eff[i][j] * conc[m_species[i][j]];
46 }
47 work[i] = m_default[i] * ctot + sum;
48 }
49 }
50
51 //! Update third-body concentrations in full vector
52 void copy(const vector_fp& work, double* concm) {
53 for (size_t i = 0; i < m_true_index.size(); i++) {
54 concm[m_true_index[i]] = work[i];
55 }
56 }
57
58 void multiply(double* output, const double* work) {
59 for (size_t i = 0; i < m_reaction_index.size(); i++) {
60 output[m_reaction_index[i]] *= work[i];
61 }
62 }
63
64 size_t workSize() {
65 return m_reaction_index.size();
66 }
67
68protected:
69 //! Indices of reactions that use third-bodies within vector of concentrations
70 std::vector<size_t> m_reaction_index;
71
72 //! Actual index of reaction within the full reaction array
73 std::vector<size_t> m_true_index;
74
75 //! m_species[i][j] is the index of the j-th species in reaction i.
76 std::vector<std::vector<size_t> > m_species;
77
78 //! m_eff[i][j] is the efficiency of the j-th species in reaction i.
79 std::vector<vector_fp> m_eff;
80
81 //! The default efficiency for each reaction
83};
84
85
86//! Calculate and apply third-body effects on reaction rates, including non-
87//! unity third-body efficiencies.
89{
90public:
91 //! Install reaction that uses third-body effects in ThirdBodyCalc3 manager
92 void install(size_t rxnNumber, const std::map<size_t, double>& efficiencies,
93 double default_efficiency, bool mass_action) {
94 m_reaction_index.push_back(rxnNumber);
95 m_default.push_back(default_efficiency);
96
97 if (mass_action) {
98 m_mass_action_index.push_back(m_reaction_index.size() - 1);
99 } else {
100 m_no_mass_action_index.push_back(m_reaction_index.size() - 1);
101 }
102
103 m_species.emplace_back();
104 m_eff.emplace_back();
105 for (const auto& eff : efficiencies) {
106 AssertTrace(eff.first != npos);
107 m_species.back().push_back(eff.first);
108 m_eff.back().push_back(eff.second - default_efficiency);
109 m_efficiencyList.emplace_back(
110 static_cast<int>(rxnNumber),
111 static_cast<int>(eff.first), eff.second - default_efficiency);
112 }
113 }
114
115 //! Resize the sparse coefficient matrix
116 void resizeCoeffs(size_t nSpc, size_t nRxn) {
117 // Sparse Efficiency coefficient matrix
118 Eigen::SparseMatrix<double> efficiencies;
119 efficiencies.setZero();
120 efficiencies.resize(nRxn, nSpc);
121 efficiencies.reserve(m_efficiencyList.size());
122 efficiencies.setFromTriplets(
123 m_efficiencyList.begin(), m_efficiencyList.end());
124
125 // derivative matrix multipliers
126 std::vector<Eigen::Triplet<double>> triplets;
127 triplets.reserve(m_reaction_index.size() * nSpc);
128 for (size_t i = 0; i < m_default.size(); i++) {
129 if (m_default[i] != 0) {
130 for (size_t j = 0; j < nSpc; j++) {
131 triplets.emplace_back(
132 static_cast<int>(m_reaction_index[i]),
133 static_cast<int>(j), m_default[i]);
134 }
135 }
136 }
137 Eigen::SparseMatrix<double> defaults(nRxn, nSpc);
138 defaults.reserve(triplets.size());
139 defaults.setFromTriplets(triplets.begin(), triplets.end());
140 m_multipliers = efficiencies + defaults;
141 }
142
143 //! Update third-body concentrations in full vector
144 void update(const vector_fp& conc, double ctot, double* concm) const {
145 for (size_t i = 0; i < m_reaction_index.size(); i++) {
146 double sum = 0.0;
147 for (size_t j = 0; j < m_species[i].size(); j++) {
148 sum += m_eff[i][j] * conc[m_species[i][j]];
149 }
150 concm[m_reaction_index[i]] = m_default[i] * ctot + sum;
151 }
152 }
153
154 //! Multiply output with effective third-body concentration
155 void multiply(double* output, const double* concm) {
156 for (size_t i = 0; i < m_mass_action_index.size(); i++) {
158 output[ix] *= concm[ix];
159 }
160 }
161
162 //! Calculate derivatives with respect to species concentrations.
163 /*!
164 * @param product Product of law of mass action and rate terms.
165 */
166 Eigen::SparseMatrix<double> derivatives(const double* product) {
167 Eigen::Map<const Eigen::VectorXd> mapped(product, m_multipliers.rows());
168 return mapped.asDiagonal() * m_multipliers;
169 }
170
171 //! Scale entries involving third-body collider in law of mass action by factor
172 void scale(const double* in, double* out, double factor) const {
173 for (size_t i = 0; i < m_mass_action_index.size(); i++) {
175 out[ix] = factor * in[ix];
176 }
177 }
178
179 //! Scale entries involving third-body collider in rate expression
180 //! by third-body concentration and factor
181 void scaleM(const double* in, double* out,
182 const double* concm, double factor) const
183 {
184 for (size_t i = 0; i < m_no_mass_action_index.size(); i++) {
186 out[ix] = factor * concm[ix] * in[ix];
187 }
188 }
189
190 //! Return boolean indicating whether ThirdBodyCalc3 is empty
191 bool empty() const {
192 return m_reaction_index.empty();
193 }
194
195protected:
196 //! Indices of reactions that use third-bodies within vector of concentrations
197 std::vector<size_t> m_reaction_index;
198
199 //! Indices within m_reaction_index of reactions that consider third-body effects
200 //! in the law of mass action
201 std::vector<size_t> m_mass_action_index;
202
203 //! Indices within m_reaction_index of reactions that consider third-body effects
204 //! in the rate expression
205 std::vector<size_t> m_no_mass_action_index;
206
207 //! m_species[i][j] is the index of the j-th species in reaction i.
208 std::vector<std::vector<size_t> > m_species;
209
210 //! m_eff[i][j] is the efficiency of the j-th species in reaction i.
211 std::vector<vector_fp> m_eff;
212
213 //! The default efficiency for each reaction
215
216 //! Sparse efficiency matrix (compensated for defaults)
217 //! Each triplet corresponds to (reaction index, species index, efficiency)
218 std::vector<Eigen::Triplet<double>> m_efficiencyList;
219
220 //! Sparse derivative multiplier matrix
221 Eigen::SparseMatrix<double> m_multipliers;
222};
223
224
225}
226
227#endif
Calculate and apply third-body effects on reaction rates, including non- unity third-body efficiencie...
Definition: ThirdBodyCalc.h:89
void scaleM(const double *in, double *out, const double *concm, double factor) const
Scale entries involving third-body collider in rate expression by third-body concentration and factor...
Eigen::SparseMatrix< double > m_multipliers
Sparse derivative multiplier matrix.
std::vector< size_t > m_reaction_index
Indices of reactions that use third-bodies within vector of concentrations.
std::vector< Eigen::Triplet< double > > m_efficiencyList
Sparse efficiency matrix (compensated for defaults) Each triplet corresponds to (reaction index,...
void update(const vector_fp &conc, double ctot, double *concm) const
Update third-body concentrations in full vector.
std::vector< size_t > m_mass_action_index
Indices within m_reaction_index of reactions that consider third-body effects in the law of mass acti...
bool empty() const
Return boolean indicating whether ThirdBodyCalc3 is empty.
vector_fp m_default
The default efficiency for each reaction.
std::vector< vector_fp > m_eff
m_eff[i][j] is the efficiency of the j-th species in reaction i.
void multiply(double *output, const double *concm)
Multiply output with effective third-body concentration.
Eigen::SparseMatrix< double > derivatives(const double *product)
Calculate derivatives with respect to species concentrations.
void scale(const double *in, double *out, double factor) const
Scale entries involving third-body collider in law of mass action by factor.
void install(size_t rxnNumber, const std::map< size_t, double > &efficiencies, double default_efficiency, bool mass_action)
Install reaction that uses third-body effects in ThirdBodyCalc3 manager.
Definition: ThirdBodyCalc.h:92
void resizeCoeffs(size_t nSpc, size_t nRxn)
Resize the sparse coefficient matrix.
std::vector< size_t > m_no_mass_action_index
Indices within m_reaction_index of reactions that consider third-body effects in the rate expression.
std::vector< std::vector< size_t > > m_species
m_species[i][j] is the index of the j-th species in reaction i.
Calculate and apply third-body effects on reaction rates, including non- unity third-body efficiencie...
Definition: ThirdBodyCalc.h:20
std::vector< size_t > m_reaction_index
Indices of reactions that use third-bodies within vector of concentrations.
Definition: ThirdBodyCalc.h:70
vector_fp m_default
The default efficiency for each reaction.
Definition: ThirdBodyCalc.h:82
std::vector< vector_fp > m_eff
m_eff[i][j] is the efficiency of the j-th species in reaction i.
Definition: ThirdBodyCalc.h:79
std::vector< size_t > m_true_index
Actual index of reaction within the full reaction array.
Definition: ThirdBodyCalc.h:73
void copy(const vector_fp &work, double *concm)
Update third-body concentrations in full vector.
Definition: ThirdBodyCalc.h:52
std::vector< std::vector< size_t > > m_species
m_species[i][j] is the index of the j-th species in reaction i.
Definition: ThirdBodyCalc.h:76
This file contains definitions of constants, types and terms that are used in internal routines and a...
#define AssertTrace(expr)
Assertion must be true or an error is thrown.
Definition: ctexceptions.h:240
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:192
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:184