antioch-0.4.0
kinetics_evaluator.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // Antioch - A Gas Dynamics Thermochemistry Library
5 //
6 // Copyright (C) 2014-2016 Paul T. Bauman, Benjamin S. Kirk,
7 // Sylvain Plessis, Roy H. Stonger
8 //
9 // Copyright (C) 2013 The PECOS Development Team
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the Version 2.1 GNU Lesser General
13 // Public License as published by the Free Software Foundation.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
23 // Boston, MA 02110-1301 USA
24 //
25 //-----------------------------------------------------------------------el-
26 
27 #ifndef ANTIOCH_KINETICS_EVALUATOR_H
28 #define ANTIOCH_KINETICS_EVALUATOR_H
29 
30 // Antioch
32 #include "antioch/reaction_set.h"
34 
35 // C++
36 #include <vector>
37 
38 namespace Antioch
39 {
40  // Forward declarations
41  template<typename CoeffType>
42  class ReactionSet;
43 
44  template<typename CoeffType>
45  class ChemicalMixture;
46 
48 
52  template<typename CoeffType=double, typename StateType=CoeffType>
54  {
55  public:
56 
58  //as well as an \p example instantiation of the data type to be
59  //used as inputs. For scalar-valued inputs, "0" is a valid
60  //example input. For vector-valued inputs, an appropriately-sized
61  //vector should be used.
63  const StateType& example );
64 
66 
67  const ReactionSet<CoeffType>& reaction_set() const;
68 
70 
71  template <typename VectorStateType, typename KC>
72  void compute_mass_sources( const KC& conditions,
73  const VectorStateType& molar_densities,
74  const VectorStateType& h_RT_minus_s_R,
75  VectorStateType& mass_sources );
76 
78 
80  template <typename VectorStateType, typename KC>
81  void compute_mass_sources_and_derivs( const KC& conditions,
82  const VectorStateType& molar_densities,
83  const VectorStateType& h_RT_minus_s_R,
84  const VectorStateType& dh_RT_minus_s_R_dT,
85  VectorStateType& mass_sources,
86  VectorStateType& dmass_dT,
87  std::vector<VectorStateType>& dmass_drho_s );
88 
90 
91  template <typename VectorStateType, typename KC>
92  void compute_mole_sources( const KC& conditions,
93  const VectorStateType& molar_densities,
94  const VectorStateType& h_RT_minus_s_R,
95  VectorStateType& mole_sources );
96 
98 
100  template <typename VectorStateType, typename KC>
101  void compute_mole_sources_and_derivs( const KC& conditions,
102  const VectorStateType& molar_densities,
103  const VectorStateType& h_RT_minus_s_R,
104  const VectorStateType& dh_RT_minus_s_R_dT,
105  VectorStateType& mole_sources,
106  VectorStateType& dmole_dT,
107  std::vector<VectorStateType>& dmole_dX_s );
108 
109  unsigned int n_species() const;
110 
111  unsigned int n_reactions() const;
112 
113  protected:
114 
116 
118 
119  std::vector<StateType> _net_reaction_rates;
120 
121  std::vector<StateType> _dnet_rate_dT;
122 
123  std::vector<std::vector<StateType> > _dnet_rate_dX_s;
124  };
125 
126  /* ------------------------- Inline Functions -------------------------*/
127  template<typename CoeffType, typename StateType>
128  inline
130  {
131  return _reaction_set;
132  }
133 
134  template<typename CoeffType, typename StateType>
135  inline
137  {
138  return _chem_mixture.n_species();
139  }
140 
141  template<typename CoeffType, typename StateType>
142  inline
144  {
145  return _reaction_set.n_reactions();
146  }
147 
148 
149  template<typename CoeffType, typename StateType>
150  inline
152  ( const ReactionSet<CoeffType>& reaction_set,
153  const StateType& example )
154  : _reaction_set( reaction_set ),
155  _chem_mixture( reaction_set.chemical_mixture() ),
156  _net_reaction_rates( reaction_set.n_reactions(), example ),
157  _dnet_rate_dT( reaction_set.n_reactions(), example ),
158  _dnet_rate_dX_s( reaction_set.n_reactions() )
159  {
160 
161  for( unsigned int r = 0; r < this->n_reactions(); r++ )
162  {
163  _dnet_rate_dX_s[r].resize( reaction_set.n_species(), example );
164  }
165 
166  return;
167  }
168 
169 
170  template<typename CoeffType, typename StateType>
171  inline
173  {
174  return;
175  }
176 
177  template<typename CoeffType, typename StateType>
178  template<typename VectorStateType, typename KC>
179  inline
181  const VectorStateType& molar_densities,
182  const VectorStateType& h_RT_minus_s_R,
183  VectorStateType& mole_sources )
184  {
186  // antioch_assert_greater(T, 0.0);
187  antioch_assert_equal_to( molar_densities.size(), this->n_species() );
188  antioch_assert_equal_to( h_RT_minus_s_R.size(), this->n_species() );
189  antioch_assert_equal_to( mole_sources.size(), this->n_species() );
190 
192  Antioch::set_zero(_net_reaction_rates);
193 
194  Antioch::set_zero(mole_sources);
195 
196  typename constructor_or_reference<const KineticsConditions<StateType,VectorStateType>, const KC>::type //either (KineticsConditions<> &) or (KineticsConditions<>)
197  kinetics_conditions(conditions);
198  // compute the requisite reaction rates
199  this->_reaction_set.compute_reaction_rates( kinetics_conditions, molar_densities,
200  h_RT_minus_s_R, _net_reaction_rates );
201 
202  // compute the actual mole sources in kmol/sec/m^3
203  for (unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
204  {
205  const Reaction<CoeffType>& reaction = this->_reaction_set.reaction(rxn);
206  const StateType rate = _net_reaction_rates[rxn];
207 
208  // reactant contributions
209  for (unsigned int r = 0; r < reaction.n_reactants(); r++)
210  {
211  const unsigned int r_id = reaction.reactant_id(r);
212  const unsigned int r_stoich = reaction.reactant_stoichiometric_coefficient(r);
213 
214  mole_sources[r_id] -= (static_cast<CoeffType>(r_stoich)*rate);
215  }
216 
217  // product contributions
218  for (unsigned int p=0; p < reaction.n_products(); p++)
219  {
220  const unsigned int p_id = reaction.product_id(p);
221  const unsigned int p_stoich = reaction.product_stoichiometric_coefficient(p);
222 
223  mole_sources[p_id] += (static_cast<CoeffType>(p_stoich)*rate);
224  }
225  }
226 
227  return;
228  }
229 
230 
231  template<typename CoeffType, typename StateType>
232  template<typename VectorStateType, typename KC>
233  inline
235  const VectorStateType& molar_densities,
236  const VectorStateType& h_RT_minus_s_R,
237  VectorStateType& mass_sources )
238  {
239  // Quantities asserted in compute_mole_sources call
240  this->compute_mole_sources( conditions, molar_densities, h_RT_minus_s_R, mass_sources );
241 
242  // finally scale by molar mass
243  for (unsigned int s=0; s < this->n_species(); s++)
244  {
245  mass_sources[s] *= _chem_mixture.M(s);
246  }
247 
248  return;
249  }
250 
251  template<typename CoeffType, typename StateType>
252  template<typename VectorStateType, typename KC>
253  inline
255  const VectorStateType& molar_densities,
256  const VectorStateType& h_RT_minus_s_R,
257  const VectorStateType& dh_RT_minus_s_R_dT,
258  VectorStateType& mole_sources,
259  VectorStateType& dmole_dT,
260  std::vector<VectorStateType>& dmole_dX_s )
261  {
263  // antioch_assert_greater(T, 0.0);
264  antioch_assert_equal_to( molar_densities.size(), this->n_species() );
265  antioch_assert_equal_to( h_RT_minus_s_R.size(), this->n_species() );
266  antioch_assert_equal_to( dh_RT_minus_s_R_dT.size(), this->n_species() );
267  antioch_assert_equal_to( mole_sources.size(), this->n_species() );
268  antioch_assert_equal_to( dmole_dT.size(), this->n_species() );
269  antioch_assert_equal_to( dmole_dX_s.size(), this->n_species() );
270 #ifdef NDEBUG
271 #else
272  for (unsigned int s=0; s < this->n_species(); s++)
273  {
274  antioch_assert_equal_to( dmole_dX_s[s].size(), this->n_species() );
275  }
276 #endif
277 
279  Antioch::set_zero(_net_reaction_rates);
280  Antioch::set_zero(_dnet_rate_dT);
281 
282  Antioch::set_zero(mole_sources);
283  Antioch::set_zero(dmole_dT);
284  for (unsigned int s=0; s < this->n_species(); s++)
285  {
286  Antioch::set_zero(dmole_dX_s[s]);
287  }
288 
289  for (unsigned int rxn=0; rxn < this->n_reactions(); rxn++)
290  {
292  Antioch::set_zero(_dnet_rate_dX_s[rxn]);
293  }
294 
295  typename constructor_or_reference<const KineticsConditions<StateType,VectorStateType>, const KC>::type //either (KineticsConditions<> &) or (KineticsConditions<>)
296  kinetics_conditions(conditions);
297  // compute the requisite reaction rates
298  this->_reaction_set.compute_reaction_rates_and_derivs( kinetics_conditions, molar_densities,
299  h_RT_minus_s_R, dh_RT_minus_s_R_dT,
300  _net_reaction_rates,
301  _dnet_rate_dT,
302  _dnet_rate_dX_s );
303  // compute the actual mole sources in kmol/sec/m^3
304  for (unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
305  {
306 
307  const Reaction<CoeffType>& reaction = this->_reaction_set.reaction(rxn);
308 
310  const StateType rate = _net_reaction_rates[rxn];
311  const StateType drate_dT = _dnet_rate_dT[rxn];
312  const VectorStateType drate_dX_s = _dnet_rate_dX_s[rxn];
313 
314  // reactant contributions
315  for (unsigned int r = 0; r < reaction.n_reactants(); r++)
316  {
317  const unsigned int r_id = reaction.reactant_id(r);
318  const unsigned int r_stoich = reaction.reactant_stoichiometric_coefficient(r);
319 
320  mole_sources[r_id] -= (static_cast<CoeffType>(r_stoich)*rate);
321 
322  // d/dT rate contributions
323  dmole_dT[r_id] -= (static_cast<CoeffType>(r_stoich)*drate_dT);
324 
325  // d(.m)/dX_s rate contributions
326  for (unsigned int s=0; s < this->n_species(); s++)
327  {
328  dmole_dX_s[r_id][s] -= (static_cast<CoeffType>(r_stoich)*drate_dX_s[s]);
329  }
330  }
331 
332  // product contributions
333  for (unsigned int p=0; p < reaction.n_products(); p++)
334  {
335  const unsigned int p_id = reaction.product_id(p);
336  const unsigned int p_stoich = reaction.product_stoichiometric_coefficient(p);
337 
338  mole_sources[p_id] += (static_cast<CoeffType>(p_stoich)*rate);
339 
340  // d/dT rate contributions
341  dmole_dT[p_id] += (static_cast<CoeffType>(p_stoich)*drate_dT);
342 
343  // d/dX_s rate contributions
344  for (unsigned int s=0; s < this->n_species(); s++)
345  {
346  dmole_dX_s[p_id][s] += (static_cast<CoeffType>(p_stoich)*drate_dX_s[s]);
347  }
348  }
349  }
350 
351  return;
352  }
353 
354 
355  template<typename CoeffType, typename StateType>
356  template <typename VectorStateType, typename KC>
357  inline
359  const VectorStateType& molar_densities,
360  const VectorStateType& h_RT_minus_s_R,
361  const VectorStateType& dh_RT_minus_s_R_dT,
362  VectorStateType& mass_sources,
363  VectorStateType& dmass_dT,
364  std::vector<VectorStateType>& dmass_drho_s )
365  {
366  // Asserts are in compute_mole_sources
367  this->compute_mole_sources_and_derivs( conditions, molar_densities, h_RT_minus_s_R, dh_RT_minus_s_R_dT,
368  mass_sources, dmass_dT, dmass_drho_s );
369 
370  // Convert from mole units to mass units
371  for (unsigned int s=0; s < this->n_species(); s++)
372  {
373  mass_sources[s] *= _chem_mixture.M(s);
374  dmass_dT[s] *= _chem_mixture.M(s);
375 
376  for (unsigned int t=0; t < this->n_species(); t++)
377  {
378  dmass_drho_s[s][t] *= _chem_mixture.M(s)/_chem_mixture.M(t);
379  }
380 
381  }
382 
383  return;
384  }
385 
386 } // end namespace Antioch
387 
388 #endif // ANTIOCH_KINETICS_EVALUATOR_H
unsigned int n_reactions() const
const ChemicalMixture< CoeffType > & _chem_mixture
KineticsEvaluator(const ReactionSet< CoeffType > &reaction_set, const StateType &example)
Constructor. Requires a reaction set to be evaluated later,.
This class encapsulates all the reaction mechanisms considered in a chemical nonequilibrium simulatio...
#define antioch_assert_equal_to(expr1, expr2)
void compute_mole_sources_and_derivs(const KC &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, const VectorStateType &dh_RT_minus_s_R_dT, VectorStateType &mole_sources, VectorStateType &dmole_dT, std::vector< VectorStateType > &dmole_dX_s)
Compute species production/destruction rate derivatives.
void compute_mass_sources(const KC &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, VectorStateType &mass_sources)
Compute species production/destruction rates per unit volume.
void compute_mole_sources(const KC &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, VectorStateType &mole_sources)
Compute species molar production/destruction rates per unit volume.
unsigned int n_species() const
std::vector< StateType > _dnet_rate_dT
unsigned int product_stoichiometric_coefficient(const unsigned int p) const
Definition: reaction.h:561
unsigned int n_species() const
Definition: reaction_set.h:193
unsigned int reactant_id(const unsigned int r) const
Definition: reaction.h:534
std::vector< StateType > _net_reaction_rates
const ReactionSet< CoeffType > & reaction_set() const
unsigned int n_reactions() const
Definition: reaction_set.h:200
const ReactionSet< CoeffType > & _reaction_set
const ChemicalMixture< CoeffType > & chemical_mixture() const
Definition: reaction_set.h:248
unsigned int n_products() const
Definition: reaction.h:508
void set_zero(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a)
Definition: eigen_utils.h:217
Class to handle computing mass source terms for a given ReactionSet.
std::vector< std::vector< StateType > > _dnet_rate_dX_s
Class storing chemical mixture properties.
The parameters are reduced parameters.
unsigned int product_id(const unsigned int p) const
Definition: reaction.h:543
void compute_mass_sources_and_derivs(const KC &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, const VectorStateType &dh_RT_minus_s_R_dT, VectorStateType &mass_sources, VectorStateType &dmass_dT, std::vector< VectorStateType > &dmass_drho_s)
Compute species production/destruction rate derivatives.
unsigned int reactant_stoichiometric_coefficient(const unsigned int r) const
Definition: reaction.h:552
unsigned int n_reactants() const
Definition: reaction.h:498

Generated on Thu Jul 7 2016 11:09:45 for antioch-0.4.0 by  doxygen 1.8.8