Cantera  2.1.2
Kinetics.cpp
Go to the documentation of this file.
1 /**
2  * @file Kinetics.cpp Declarations for the base class for kinetics managers
3  * (see \ref kineticsmgr and class \link Cantera::Kinetics
4  * Kinetics\endlink).
5  *
6  * Kinetics managers calculate rates of progress of species due to
7  * homogeneous or heterogeneous kinetics.
8  */
9 // Copyright 2001-2004 California Institute of Technology
10 
15 
17 
18 using namespace std;
19 
20 
21 namespace Cantera
22 {
23 Kinetics::Kinetics() :
24  m_ii(0),
25  m_kk(0),
26  m_perturb(0),
27  m_reactants(0),
28  m_products(0),
29  m_thermo(0),
30  m_start(0),
31  m_phaseindex(),
32  m_surfphase(npos),
33  m_rxnphase(npos),
34  m_mindim(4),
35  m_dummygroups(0)
36 {
37 }
38 
40 
42  m_ii(0),
43  m_kk(0),
44  m_perturb(0),
45  m_reactants(0),
46  m_products(0),
47  m_thermo(0),
48  m_start(0),
49  m_phaseindex(),
50  m_surfphase(npos),
51  m_rxnphase(npos),
52  m_mindim(4),
53  m_dummygroups(0)
54 {
55  /*
56  * Call the assignment operator
57  */
58  *this = right;
59 }
60 
62 operator=(const Kinetics& right)
63 {
64  /*
65  * Check for self assignment.
66  */
67  if (this == &right) {
68  return *this;
69  }
70 
71  m_ii = right.m_ii;
72  m_kk = right.m_kk;
73  m_perturb = right.m_perturb;
74  m_reactants = right.m_reactants;
75  m_products = right.m_products;
76 
77  m_thermo = right.m_thermo; // DANGER -> shallow pointer copy
78 
79  m_start = right.m_start;
80  m_phaseindex = right.m_phaseindex;
81  m_surfphase = right.m_surfphase;
82  m_rxnphase = right.m_rxnphase;
83  m_mindim = right.m_mindim;
85 
86  return *this;
87 }
88 
89 Kinetics* Kinetics::duplMyselfAsKinetics(const std::vector<thermo_t*> & tpVector) const
90 {
91  Kinetics* ko = new Kinetics(*this);
92 
93  ko->assignShallowPointers(tpVector);
94  return ko;
95 }
96 
97 int Kinetics::type() const
98 {
99  return 0;
100 }
101 
102 void Kinetics::checkReactionIndex(size_t i) const
103 {
104  if (i >= m_ii) {
105  throw IndexError("checkReactionIndex", "reactions", i, m_ii-1);
106  }
107 }
108 
109 void Kinetics::checkReactionArraySize(size_t ii) const
110 {
111  if (m_ii > ii) {
112  throw ArraySizeError("checkReactionArraySize", ii, m_ii);
113  }
114 }
115 
116 void Kinetics::checkPhaseIndex(size_t m) const
117 {
118  if (m >= nPhases()) {
119  throw IndexError("checkPhaseIndex", "phase", m, nPhases()-1);
120  }
121 }
122 
123 void Kinetics::checkPhaseArraySize(size_t mm) const
124 {
125  if (nPhases() > mm) {
126  throw ArraySizeError("checkPhaseArraySize", mm, nPhases());
127  }
128 }
129 
130 void Kinetics::checkSpeciesIndex(size_t k) const
131 {
132  if (k >= m_kk) {
133  throw IndexError("checkSpeciesIndex", "species", k, m_kk-1);
134  }
135 }
136 
137 void Kinetics::checkSpeciesArraySize(size_t kk) const
138 {
139  if (m_kk > kk) {
140  throw ArraySizeError("checkSpeciesArraySize", kk, m_kk);
141  }
142 }
143 
144 void Kinetics::assignShallowPointers(const std::vector<thermo_t*> & tpVector)
145 {
146  size_t ns = tpVector.size();
147  if (ns != m_thermo.size()) {
148  throw CanteraError(" Kinetics::assignShallowPointers",
149  " Number of ThermoPhase objects arent't the same");
150  }
151  for (size_t i = 0; i < ns; i++) {
152  ThermoPhase* ntp = tpVector[i];
153  ThermoPhase* otp = m_thermo[i];
154  if (ntp->id() != otp->id()) {
155  throw CanteraError(" Kinetics::assignShallowPointers",
156  " id() of the ThermoPhase objects isn't the same");
157  }
158  if (ntp->eosType() != otp->eosType()) {
159  throw CanteraError(" Kinetics::assignShallowPointers",
160  " eosType() of the ThermoPhase objects isn't the same");
161  }
162  if (ntp->nSpecies() != otp->nSpecies()) {
163  throw CanteraError(" Kinetics::assignShallowPointers",
164  " Number of ThermoPhase objects isn't the same");
165  }
166  m_thermo[i] = tpVector[i];
167  }
168 
169 
170 }
171 
172 void Kinetics::selectPhase(const doublereal* data, const thermo_t* phase,
173  doublereal* phase_data)
174 {
175  for (size_t n = 0; n < nPhases(); n++) {
176  if (phase == m_thermo[n]) {
177  size_t nsp = phase->nSpecies();
178  copy(data + m_start[n],
179  data + m_start[n] + nsp, phase_data);
180  return;
181  }
182  }
183  throw CanteraError("Kinetics::selectPhase", "Phase not found.");
184 }
185 
186 string Kinetics::kineticsSpeciesName(size_t k) const
187 {
188  for (size_t n = m_start.size()-1; n != npos; n--) {
189  if (k >= m_start[n]) {
190  return thermo(n).speciesName(k - m_start[n]);
191  }
192  }
193  return "<unknown>";
194 }
195 
196 size_t Kinetics::kineticsSpeciesIndex(const std::string& nm) const
197 {
198  for (size_t n = 0; n < m_thermo.size(); n++) {
199  string id = thermo(n).id();
200  // Check the ThermoPhase object for a match
201  size_t k = thermo(n).speciesIndex(nm);
202  if (k != npos) {
203  return k + m_start[n];
204  }
205  }
206  return npos;
207 }
208 
209 size_t Kinetics::kineticsSpeciesIndex(const std::string& nm,
210  const std::string& ph) const
211 {
212  if (ph == "<any>") {
213  return kineticsSpeciesIndex(nm);
214  }
215 
216  for (size_t n = 0; n < m_thermo.size(); n++) {
217  string id = thermo(n).id();
218  if (ph == id) {
219  size_t k = thermo(n).speciesIndex(nm);
220  if (k == npos) {
221  return npos;
222  }
223  return k + m_start[n];
224  }
225  }
226  return npos;
227 }
228 
229 thermo_t& Kinetics::speciesPhase(const std::string& nm)
230 {
231  size_t np = m_thermo.size();
232  size_t k;
233  string id;
234  for (size_t n = 0; n < np; n++) {
235  k = thermo(n).speciesIndex(nm);
236  if (k != npos) {
237  return thermo(n);
238  }
239  }
240  throw CanteraError("speciesPhase", "unknown species "+nm);
241  return thermo(0);
242 }
243 
245 {
246  for (size_t n = m_start.size()-1; n != npos; n--) {
247  if (k >= m_start[n]) {
248  return n;
249  }
250  }
251  throw CanteraError("speciesPhaseIndex", "illegal species index: "+int2str(k));
252  return npos;
253 }
254 
256 {
257  // if not the first thermo object, set the start position
258  // to that of the last object added + the number of its species
259  if (m_thermo.size() > 0) {
260  m_start.push_back(m_start.back()
261  + m_thermo.back()->nSpecies());
262  }
263  // otherwise start at 0
264  else {
265  m_start.push_back(0);
266  }
267 
268  // the phase with lowest dimensionality is assumed to be the
269  // phase/interface at which reactions take place
270  if (thermo.nDim() <= m_mindim) {
271  m_mindim = thermo.nDim();
272  m_rxnphase = nPhases();
273  }
274 
275  // there should only be one surface phase
276  int ptype = -100;
277  if (type() == cEdgeKinetics) {
278  ptype = cEdge;
279  } else if (type() == cInterfaceKinetics) {
280  ptype = cSurf;
281  }
282  if (thermo.eosType() == ptype) {
283  m_surfphase = nPhases();
284  m_rxnphase = nPhases();
285  }
286  m_thermo.push_back(&thermo);
287  m_phaseindex[m_thermo.back()->id()] = nPhases();
288 }
289 
291 {
292  m_kk = 0;
293  for (size_t n = 0; n < nPhases(); n++) {
294  size_t nsp = m_thermo[n]->nSpecies();
295  m_kk += nsp;
296  }
297 }
298 
299 void Kinetics::err(const std::string& m) const
300 {
301  throw CanteraError("Kinetics::" + m,
302  "The default Base class method was called, when "
303  "the inherited class's method should "
304  "have been called");
305 }
306 
307 }
Array size error.
Definition: ctexceptions.h:123
std::string int2str(const int n, const std::string &fmt)
Convert an int to a string using a format converter.
Definition: stringUtils.cpp:40
std::vector< thermo_t * > m_thermo
m_thermo is a vector of pointers to ThermoPhase objects that are involved with this kinetics operator...
Definition: Kinetics.h:938
Header for a simple thermodynamics model of a surface phase derived from ThermoPhase, assuming an ideal solution model (see Thermodynamic Properties and class SurfPhase).
std::vector< std::vector< size_t > > m_reactants
This is a vector of vectors containing the reactants for each reaction.
Definition: Kinetics.h:908
virtual void assignShallowPointers(const std::vector< thermo_t * > &tpVector)
Reassign the pointers within the Kinetics object.
Definition: Kinetics.cpp:144
thermo_t & thermo(size_t n=0)
This method returns a reference to the nth ThermoPhase object defined in this kinetics mechanism...
Definition: Kinetics.h:288
size_t m_kk
The number of species in all of the phases that participate in this kinetics mechanism.
Definition: Kinetics.h:891
std::vector< size_t > m_start
m_start is a vector of integers specifying the beginning position for the species vector for the n'th...
Definition: Kinetics.h:945
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:173
virtual Kinetics * duplMyselfAsKinetics(const std::vector< thermo_t * > &tpVector) const
Duplication routine for objects which inherit from Kinetics.
Definition: Kinetics.cpp:89
std::string kineticsSpeciesName(size_t k) const
Return the name of the kth species in the kinetics manager.
Definition: Kinetics.cpp:186
void selectPhase(const doublereal *data, const thermo_t *phase, doublereal *phase_data)
Definition: Kinetics.cpp:172
std::vector< grouplist_t > m_dummygroups
Vector of group lists.
Definition: Kinetics.h:972
Kinetics()
Default constructor.
Definition: Kinetics.cpp:23
Base class for a phase with thermodynamic properties.
Definition: ThermoPhase.h:101
const int cSurf
A surface phase. Used by class SurfPhase.
Definition: mix_defs.h:40
void checkPhaseIndex(size_t m) const
Check that the specified phase index is in range Throws an exception if m is greater than nPhases() ...
Definition: Kinetics.cpp:116
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
Definition: Phase.cpp:229
size_t nPhases() const
The number of phases participating in the reaction mechanism.
Definition: Kinetics.h:228
thermo_t & speciesPhase(const std::string &nm)
This function looks up the name of a species and returns a reference to the ThermoPhase object of the...
Definition: Kinetics.cpp:229
void checkSpeciesArraySize(size_t mm) const
Check that an array size is at least nSpecies() Throws an exception if kk is less than nSpecies()...
Definition: Kinetics.cpp:137
void err(const std::string &m) const
Function indicating that a function inherited from the base class hasn't had a definition assigned to...
Definition: Kinetics.cpp:299
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range Throws an exception if k is greater than nSpecies(...
Definition: Kinetics.cpp:130
vector_fp m_perturb
Vector of perturbation factors for each reaction's rate of progress vector.
Definition: Kinetics.h:895
Public interface for kinetics managers.
Definition: Kinetics.h:131
std::string id() const
Return the string id for the phase.
Definition: Phase.cpp:119
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:68
Base class for kinetics managers and also contains the kineticsmgr module documentation (see Kinetics...
std::map< std::string, size_t > m_phaseindex
Mapping of the phase id, i.e., the id attribute in the xml phase element to the position of the phase...
Definition: Kinetics.h:955
const int cEdge
An edge between two 2D surfaces.
Definition: mix_defs.h:58
Declarations for the implicit integration of surface site density equations (see Kinetics Managers an...
Kinetics & operator=(const Kinetics &right)
Assignment operator.
Definition: Kinetics.cpp:62
size_t kineticsSpeciesIndex(size_t k, size_t n) const
The location of species k of phase n in species arrays.
Definition: Kinetics.h:331
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:252
size_t nDim() const
Returns the number of spatial dimensions (1, 2, or 3)
Definition: Phase.h:512
size_t m_mindim
number of spatial dimensions of lowest-dimensional phase.
Definition: Kinetics.h:968
void checkReactionIndex(size_t m) const
Check that the specified reaction index is in range Throws an exception if i is greater than nReactio...
Definition: Kinetics.cpp:102
virtual int eosType() const
Equation of state type flag.
Definition: ThermoPhase.h:151
size_t speciesPhaseIndex(size_t k)
This function takes as an argument the kineticsSpecies index (i.e., the list index in the list of spe...
Definition: Kinetics.cpp:244
std::vector< std::vector< size_t > > m_products
This is a vector of vectors containing the products for each reaction.
Definition: Kinetics.h:921
void checkReactionArraySize(size_t ii) const
Check that an array size is at least nReactions() Throws an exception if ii is less than nReactions()...
Definition: Kinetics.cpp:109
size_t m_ii
Number of reactions in the mechanism.
Definition: Kinetics.h:887
virtual int type() const
Identifies the kinetics manager type.
Definition: Kinetics.cpp:97
size_t m_surfphase
Index in the list of phases of the one surface phase.
Definition: Kinetics.h:958
An array index is out of range.
Definition: ctexceptions.h:153
size_t m_rxnphase
Phase Index where reactions are assumed to be taking place.
Definition: Kinetics.h:965
virtual void finalize()
Finish adding reactions and prepare for use.
Definition: Kinetics.cpp:290
virtual void addPhase(thermo_t &thermo)
Add a phase to the kinetics manager object.
Definition: Kinetics.cpp:255
virtual ~Kinetics()
Destructor.
Definition: Kinetics.cpp:39
void checkPhaseArraySize(size_t mm) const
Check that an array size is at least nPhases() Throws an exception if mm is less than nPhases()...
Definition: Kinetics.cpp:123
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:246