Cantera  2.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReactionStoichMgr.cpp
Go to the documentation of this file.
1 //------------------------------------------------
2 ///
3 /// @file ReactionStoichMgr.cpp
4 ///
5 ///
6 //------------------------------------------------
7 
9 
13 
14 #include <fstream>
15 
16 using namespace std;
17 
18 namespace Cantera
19 {
20 ReactionStoichMgr::ReactionStoichMgr()
21 {
22  warn_deprecated("class ReactionStoichMgr",
23  "To be removed after Cantera 2.2.");
24  m_dummy.resize(10,1.0);
25 }
26 
27 ReactionStoichMgr::ReactionStoichMgr(const ReactionStoichMgr& right) :
28  m_reactants(right.m_reactants),
29  m_revproducts(right.m_revproducts),
30  m_irrevproducts(right.m_irrevproducts),
31  m_dummy(right.m_dummy)
32 {
33 }
34 
35 ReactionStoichMgr& ReactionStoichMgr::operator=(const ReactionStoichMgr& right)
36 {
37  if (this != &right) {
38 
39  m_reactants = right.m_reactants;
40  m_revproducts = right.m_revproducts;
41  m_irrevproducts = right.m_irrevproducts;
42  m_dummy = right.m_dummy;
43  }
44  return *this;
45 }
46 
47 void ReactionStoichMgr::add(size_t rxn, const std::vector<size_t>& reactants,
48  const std::vector<size_t>& products,
49  bool reversible)
50 {
51 
52  m_reactants.add(rxn, reactants);
53 
54  if (reversible) {
55  m_revproducts.add(rxn, products);
56  } else {
57  m_irrevproducts.add(rxn, products);
58  }
59 }
60 
61 // Add the reaction into the stoichiometric manager
62 void ReactionStoichMgr::add(size_t rxn, const ReactionData& r)
63 {
64  size_t k;
65  std::vector<size_t> rk;
66  doublereal frac;
67  doublereal oo, os, of;
68  bool doGlobal = false;
69  std::vector<size_t> extReactants = r.reactants;
70  vector_fp extRStoich = r.rstoich;
71  vector_fp extROrder = r.rorder;
72 
73  //
74  // If we have a complete global reaction then we need to do something more complete
75  // than the previous treatment. Basically we will use the reactant manager to calculate the
76  // global forward reaction rate of progress.
77  //
78  if (r.forwardFullOrder_.size() > 0) {
79  //
80  // Trigger a treatment where the order of the reaction and the stoichiometry
81  // are treated as different.
82  //
83  doGlobal = true;
84  size_t nsp = r.forwardFullOrder_.size();
85  //
86  // Set up a signal vector to indicate whether the species has been added into
87  // the input vectors for the stoich manager
88  //
89  vector_int kHandled(nsp, 0);
90  //
91  // Loop over the reactants which are also nonzero stoichioemtric entries
92  // making sure the forwardFullOrder_ entries take precedence over rorder entries
93  //
94  for (size_t kk = 0; kk < r.reactants.size(); kk++) {
95  k = r.reactants[kk];
96  os = r.rstoich[kk];
97  oo = r.rorder[kk];
98  of = r.forwardFullOrder_[k];
99  if (of != oo) {
100  extROrder[kk] = of;
101  }
102  kHandled[k] = 1;
103  }
104  for (k = 0; k < nsp; k++) {
105  of = r.forwardFullOrder_[k];
106  if (of != 0.0) {
107  if (kHandled[k] == 0) {
108  //
109  // Add extra entries to reactant inputs. Set their reactant stoichiometric entries to zero.
110  //
111  extReactants.push_back(k);
112  extROrder.push_back(of);
113  extRStoich.push_back(0.0);
114  }
115  }
116  }
117  }
118 
119  bool isfrac = false;
120  for (size_t n = 0; n < r.reactants.size(); n++) {
121  size_t ns = size_t(r.rstoich[n]);
122  frac = r.rstoich[n] - 1.0*int(r.rstoich[n]);
123  if (frac != 0.0) {
124  isfrac = true;
125  }
126  for (size_t m = 0; m < ns; m++) {
127  rk.push_back(r.reactants[n]);
128  }
129  }
130 
131  //
132  // If the reaction is non-mass action add it in in a general way
133  // Reactants get extra terms for the forward reaction rate of progress
134  // that may have zero stoichiometries.
135  //
136  if (doGlobal) {
137  m_reactants.add(rxn, extReactants, extROrder, extRStoich);
138  } else {
139  //
140  // this is confusing. The only issue should be whether rorder is different than rstoich!
141  //
142  if (isfrac || r.global || rk.size() > 3) {
143  m_reactants.add(rxn, r.reactants, r.rorder, r.rstoich);
144  } else {
145  m_reactants.add(rxn, rk);
146  }
147  }
148 
149  std::vector<size_t> pk;
150  isfrac = false;
151  for (size_t n = 0; n < r.products.size(); n++) {
152  size_t ns = size_t(r.pstoich[n]);
153  frac = r.pstoich[n] - 1.0*int(r.pstoich[n]);
154  if (frac != 0.0) {
155  isfrac = true;
156  }
157  for (size_t m = 0; m < ns; m++) {
158  pk.push_back(r.products[n]);
159  }
160  }
161 
162  if (r.reversible) {
163  //
164  // this is confusing. The only issue should be whether porder is different than pstoich!
165  //
166  if (pk.size() > 3 || r.isReversibleWithFrac) {
167  m_revproducts.add(rxn, r.products, r.porder, r.pstoich);
168  } else {
169  m_revproducts.add(rxn, pk);
170  }
171  } else {
172  //
173  // this is confusing. The only issue should be whether porder is different than pstoich!
174  //
175  if (isfrac || pk.size() > 3) {
176  m_irrevproducts.add(rxn, r.products, r.porder, r.pstoich);
177  } else {
178  m_irrevproducts.add(rxn, pk);
179  }
180  }
181 }
182 
183 void ReactionStoichMgr::getCreationRates(size_t nsp, const doublereal* ropf,
184  const doublereal* ropr, doublereal* c)
185 {
186  // zero out the output array
187  fill(c, c + nsp, 0.0);
188 
189  // the forward direction creates product species
190  m_revproducts.incrementSpecies(ropf, c);
191  m_irrevproducts.incrementSpecies(ropf, c);
192 
193  // the reverse direction creates reactant species
194  m_reactants.incrementSpecies(ropr, c);
195 }
196 
197 void ReactionStoichMgr::getDestructionRates(size_t nsp, const doublereal* ropf,
198  const doublereal* ropr,
199  doublereal* d)
200 {
201  fill(d, d + nsp, 0.0);
202  // the reverse direction destroys products in reversible reactions
203  m_revproducts.incrementSpecies(ropr, d);
204  // the forward direction destroys reactants
205  m_reactants.incrementSpecies(ropf, d);
206 }
207 
209  const doublereal* ropnet,
210  doublereal* w)
211 {
212  fill(w, w + nsp, 0.0);
213  // products are created for positive net rate of progress
214  m_revproducts.incrementSpecies(ropnet, w);
215  m_irrevproducts.incrementSpecies(ropnet, w);
216  // reactants are destroyed for positive net rate of progress
217  m_reactants.decrementSpecies(ropnet, w);
218 }
219 
220 void ReactionStoichMgr::getReactionDelta(size_t nr, const doublereal* g,
221  doublereal* dg)
222 {
223  fill(dg, dg + nr, 0.0);
224  // products add
225  m_revproducts.incrementReactions(g, dg);
226  m_irrevproducts.incrementReactions(g, dg);
227  // reactants subtract
228  m_reactants.decrementReactions(g, dg);
229 }
230 
231 void ReactionStoichMgr::getRevReactionDelta(size_t nr, const doublereal* g,
232  doublereal* dg)
233 {
234  fill(dg, dg + nr, 0.0);
235  m_revproducts.incrementReactions(g, dg);
236  m_reactants.decrementReactions(g, dg);
237 }
238 
239 void ReactionStoichMgr::multiplyReactants(const doublereal* c, doublereal* r)
240 {
241  m_reactants.multiply(c, r);
242 }
243 
244 void ReactionStoichMgr::multiplyRevProducts(const doublereal* c, doublereal* r)
245 {
246  m_revproducts.multiply(c, r);
247 }
248 
249 void ReactionStoichMgr::write(const string& filename)
250 {
251  ofstream f(filename.c_str());
252  f << "namespace mech {" << endl;
258  f << "} // namespace mech" << endl;
259  f.close();
260 }
261 
263 {
264  f << " void getCreationRates(const doublereal* rf, const doublereal* rb," << endl;
265  f << " doublereal* c) {" << endl;
266  map<size_t, string> out;
267  m_revproducts.writeIncrementSpecies("rf",out);
268  m_irrevproducts.writeIncrementSpecies("rf",out);
269  m_reactants.writeIncrementSpecies("rb",out);
270  map<size_t, string>::iterator b;
271  for (b = out.begin(); b != out.end(); ++b) {
272  string rhs = wrapString(b->second);
273  rhs[1] = '=';
274  f << " c[" << b->first << "] " << rhs << ";" << endl;
275  }
276  f << " }" << endl << endl << endl;
277 }
278 
280 {
281  f << " void getDestructionRates(const doublereal* rf, const doublereal* rb," << endl;
282  f << " doublereal* d) {" << endl;
283  map<size_t, string> out;
284  m_revproducts.writeIncrementSpecies("rb",out);
285  m_reactants.writeIncrementSpecies("rf",out);
286  map<size_t, string>::iterator b;
287  for (b = out.begin(); b != out.end(); ++b) {
288  string rhs = wrapString(b->second);
289  rhs[1] = '=';
290  f << " d[" << b->first << "] " << rhs << ";" << endl;
291  }
292  f << " }" << endl << endl << endl;
293 }
294 
296 {
297  f << " void getNetProductionRates(const doublereal* r, doublereal* w) {" << endl;
298  map<size_t, string> out;
299  m_revproducts.writeIncrementSpecies("r",out);
300  m_irrevproducts.writeIncrementSpecies("r",out);
301  m_reactants.writeDecrementSpecies("r",out);
302  map<size_t, string>::iterator b;
303  for (b = out.begin(); b != out.end(); ++b) {
304  string rhs = wrapString(b->second);
305  rhs[1] = '=';
306  f << " w[" << b->first << "] " << rhs << ";" << endl;
307  }
308  f << " }" << endl << endl << endl;
309 }
310 
312 {
313  f << " void multiplyReactants(const doublereal* c, doublereal* r) {" << endl;
314  map<size_t, string> out;
315  m_reactants.writeMultiply("c",out);
316  map<size_t, string>::iterator b;
317  for (b = out.begin(); b != out.end(); ++b) {
318  string rhs = b->second;
319  f << " r[" << b->first << "] *= " << rhs << ";" << endl;
320  }
321  f << " }" << endl << endl << endl;
322 }
323 
325 {
326  f << " void multiplyRevProducts(const doublereal* c, doublereal* r) {" << endl;
327  map<size_t, string> out;
328  m_revproducts.writeMultiply("c",out);
329  map<size_t, string>::iterator b;
330  for (b = out.begin(); b != out.end(); ++b) {
331  string rhs = b->second;
332  f << " r[" << b->first << "] *= " << rhs << ";" << endl;
333  }
334  f << " }" << endl << endl << endl;
335 }
336 }
virtual void getNetProductionRates(size_t nsp, const doublereal *ropnet, doublereal *w)
Species net production rates.
void writeDestructionRates(std::ostream &f)
virtual void getDestructionRates(size_t nSpecies, const doublereal *fwdRatesOfProgress, const doublereal *revRatesOfProgress, doublereal *destructionRates)
Species destruction rates.
vector_fp forwardFullOrder_
Reaction order for the forward direction of the reaction.
Definition: ReactionData.h:87
void writeMultiplyReactants(std::ostream &f)
virtual void getCreationRates(size_t nSpecies, const doublereal *fwdRatesOfProgress, const doublereal *revRatesOfProgress, doublereal *creationRates)
Species creation rates.
Header file declaring class ReactionStoichMgr.
virtual void getRevReactionDelta(size_t nr, const doublereal *g, doublereal *dg)
Given an array of species properties 'g', return in array 'dg' the change in this quantity in the rev...
void writeNetProductionRates(std::ostream &f)
vector_fp pstoich
Product stoichiometric coefficients, in the order given by products.
Definition: ReactionData.h:101
virtual void getReactionDelta(size_t nReactions, const doublereal *g, doublereal *dg)
Calculates the change of a molar species property in a reaction.
vector_fp porder
Reaction order of the reverse reaction with respect to each product species, in the order given by pr...
Definition: ReactionData.h:80
void warn_deprecated(const std::string &method, const std::string &extra)
Print a warning indicating that method is deprecated.
Definition: global.cpp:78
bool isReversibleWithFrac
Some reactions can be elementary reactions but have fractional stoichiometries with respect to some p...
Definition: ReactionData.h:198
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:159
void writeMultiplyRevProducts(std::ostream &f)
std::vector< size_t > products
Indices of product species.
Definition: ReactionData.h:64
virtual void multiplyReactants(const doublereal *C, doublereal *R)
Given an array of concentrations C, multiply the entries in array R by the concentration products for...
void writeCreationRates(std::ostream &f)
virtual void write(const std::string &filename)
Intermediate class which stores data about a reaction and its rate parameterization before adding the...
Definition: ReactionData.h:22
Reaction mechanism stoichiometry manager.
std::vector< size_t > reactants
Indices of reactant species.
Definition: ReactionData.h:63
vector_fp rstoich
Reactant stoichiometric coefficients, in the order given by reactants.
Definition: ReactionData.h:94
std::string wrapString(const std::string &s, const int len)
Line wrap a string via a copy operation.
bool reversible
True if the current reaction is reversible. False otherwise.
Definition: ReactionData.h:137
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:157
virtual void multiplyRevProducts(const doublereal *c, doublereal *r)
Given an array of concentrations C, multiply the entries in array R by the concentration products for...
bool global
True for "global" reactions which do not follow elementary mass action kinetics, i.e.
Definition: ReactionData.h:191
vector_fp rorder
Reaction order with respect to each reactant species, in the order given by reactants.
Definition: ReactionData.h:72
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
virtual void add(size_t rxn, const std::vector< size_t > &reactants, const std::vector< size_t > &products, bool reversible)
Add a reaction with mass-action kinetics.