Cantera  2.0
Reaction.cpp
Go to the documentation of this file.
1 /**
2  * @file Reaction.cpp
3  *
4  */
5 
6 // Copyright 2001 California Institute of Technology
7 
8 #include "Reaction.h"
9 #include <iostream>
10 #include <stdio.h>
11 
12 using namespace std;
13 
14 namespace ckr
15 {
16 
17 Reaction forwardReaction(const Reaction& rxn)
18 {
19  Reaction r(rxn);
20  r.isReversible = false;
21  r.krev = RateCoeff();
22  return r;
23 }
24 
25 Reaction reverseReaction(const Reaction& rxn)
26 {
27  Reaction r(rxn);
28  if (rxn.isReversible && (r.krev.A > 0.0)) {
29  r.isReversible = false;
30  r.products = rxn.reactants;
31  r.reactants = rxn.products;
32  r.kf = rxn.krev;
33  r.krev = RateCoeff();
34  } else {
35  r.reactants.clear();
36  r.products.clear();
37  }
38  return r;
39 }
40 
41 Reaction& Reaction::operator=(const Reaction& b)
42 {
43  if (this == &b) {
44  return *this;
45  }
46  type = b.type;
47  reactants = b.reactants;
48  fwdOrder = b.fwdOrder;
49  products = b.products;
50  thirdBody = b.thirdBody;
51  e3b = b.e3b;
52  kf = b.kf;
53  kf_aux = b.kf_aux;
54  krev = b.krev;
55  duplicate = b.duplicate;
56  falloffType = b.falloffType;
57  falloffParameters = b.falloffParameters;
58  otherAuxData = b.otherAuxData;
59  isFalloffRxn = b.isFalloffRxn;
60  isChemActRxn = b.isChemActRxn;
61  isThreeBodyRxn = b.isThreeBodyRxn;
62  isDuplicate = b.isDuplicate;
63  lines = b.lines;
64  number = b.number;
65  comment = b.comment;
66  return *this;
67 }
68 
69 void Reaction::write(std::ostream& s) const
70 {
71  int nl = static_cast<int>(lines.size());
72  for (int nn = 0; nn < nl; nn++) {
73  s << lines[nn] << std::endl;
74  }
75  // int nr = reactants.size();
76  // int np = products.size();
77  // int n;
78  // double nu;
79  // for (n = 0; n < nr; n++) {
80  // nu = reactants[n].number;
81  // if (nu > 1) s << nu;
82  // s << reactants[n].name;
83  // if (n < nr - 1)
84  // s << " + ";
85  // else if (isThreeBodyRxn)
86  // s << " + " << thirdBody;
87  // else if (isFalloffRxn || isChemActRxn)
88  // s << " (+ " << thirdBody << ")";
89  // }
90 
91  // if (isReversible) s << " = ";
92  // else s << "=>";
93  // for (n = 0; n < np; n++) {
94  // nu = products[n].number;
95  // if (nu > 1) s << nu;
96  // s << products[n].name;
97  // if (n < np - 1)
98  // s << " + ";
99  // else if (isThreeBodyRxn)
100  // s << " + " << thirdBody;
101  // else if (isFalloffRxn || isChemActRxn)
102  // s << " (+ " << thirdBody << ")";
103  // }
104  // char kfstr[100];
105  // sprintf(kfstr, " %14.5g %4.2g %f", kf.A, kf.n, kf.E);
106  // s << kfstr << endl;
107 }
108 
109 
110 /**
111  * stoichiometric coefficient of species s in the reaction. Negative
112  * for reactants, positive for products, and zero if the species does
113  * not participate in the reaction.
114  */
115 
116 double Reaction::stoichCoefficient(const std::string& s) const
117 {
118  int k;
119  int nr = static_cast<int>(reactants.size());
120  for (k = 0; k < nr; k++)
121  if (reactants[k].name == s) {
122  return -reactants[k].number;
123  }
124  int np = static_cast<int>(products.size());
125  for (k = 0; k < np; k++)
126  if (products[k].name == s) {
127  return products[k].number;
128  }
129  return 0.0;
130 }
131 
132 /**
133  * used to find undeclared duplicate reactions.
134  * @todo could be made faster
135  */
136 bool Reaction::operator==(const Reaction& r) const
137 {
138  int nr = static_cast<int>(reactants.size());
139  int np = static_cast<int>(products.size());
140  if (int(r.reactants.size()) != nr ||
141  int(r.products.size()) != np || r.thirdBody != thirdBody) {
142  return false;
143  }
144 
145  std::string nm;
146  std::map<std::string, double> coeffs;
147  for (int ir = 0; ir < nr; ir++) {
148  coeffs[reactants[ir].name] = -reactants[ir].number;
149  }
150  for (int ip = 0; ip < np; ip++) {
151  coeffs[products[ip].name] = products[ip].number;
152  }
153  for (int jr = 0; jr < nr; jr++) {
154  nm = r.reactants[jr].name;
155  if (coeffs[nm] == 0.0) {
156  return false;
157  }
158  coeffs[nm] /= -r.reactants[jr].number;
159  }
160  for (int jp = 0; jp < np; jp++) {
161  nm = r.products[jp].name;
162  if (coeffs[nm] == 0.0) {
163  return false;
164  }
165  coeffs[nm] /= products[jp].number;
166  }
167  int nc = static_cast<int>(coeffs.size());
168  std::vector<double> ratios;
169  getMapValues(coeffs, ratios);
170 
171  if (!isReversible && ratios[0] < 0.0) {
172  return false;
173  }
174 
175  for (int ic = 0; ic < nc; ic++) {
176  if (ratios[ic] != ratios[0]) {
177  return false;
178  }
179  }
180  return true;
181 }
182 
183 }
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196