antioch-0.4.0
kinetics_vec_unit.C
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 // $Id$
28 //
29 //--------------------------------------------------------------------------
30 //--------------------------------------------------------------------------
31 
32 #include "antioch_config.h"
33 
34 #include <valarray>
35 
36 #ifdef ANTIOCH_HAVE_EIGEN
37 #include "Eigen/Dense"
38 #endif
39 
40 #ifdef ANTIOCH_HAVE_METAPHYSICL
41 #include "metaphysicl/numberarray.h"
42 #endif
43 
44 #ifdef ANTIOCH_HAVE_VEXCL
45 #include "vexcl/vexcl.hpp"
46 #endif
47 
48 // Antioch
49 
50 // Declare metaprogramming overloads before they're used
56 
60 #include "antioch/reaction_set.h"
62 #include "antioch/cea_thermo.h"
64 
65 #include "antioch/eigen_utils.h"
67 #include "antioch/valarray_utils.h"
68 #include "antioch/vector_utils.h"
69 #include "antioch/vexcl_utils.h"
70 
71 #ifdef ANTIOCH_HAVE_GRVY
72 #include "grvy.h"
73 
74 GRVY::GRVY_Timer_Class gt;
75 #endif
76 
77 // C++
78 #include <limits>
79 #include <string>
80 #include <vector>
81 
82 
83 template <typename PairScalars>
84 int vectester(const std::string& input_name,
85  const PairScalars& example,
86  const std::string& testname)
87 {
88  using std::abs;
89 
90  typedef typename Antioch::value_type<PairScalars>::type Scalar;
91 
92  std::vector<std::string> species_str_list;
93  const unsigned int n_species = 5;
94  species_str_list.reserve(n_species);
95  species_str_list.push_back( "N2" );
96  species_str_list.push_back( "O2" );
97  species_str_list.push_back( "N" );
98  species_str_list.push_back( "O" );
99  species_str_list.push_back( "NO" );
100 
101  Antioch::ChemicalMixture<Scalar> chem_mixture( species_str_list );
102  Antioch::ReactionSet<Scalar> reaction_set( chem_mixture );
103  Antioch::CEAThermodynamics<Scalar> thermo( chem_mixture );
104 
105  Antioch::read_reaction_set_data_xml<Scalar>( input_name, true, reaction_set );
106 
107  Antioch::KineticsEvaluator<Scalar,PairScalars> kinetics( reaction_set, example );
108  std::vector<PairScalars> omega_dot(n_species, example);
109 
110  PairScalars P = example;
111 
112  // Mass fractions
113  PairScalars Y_vals = example;
114 
115  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
116  {
117  P[2*tuple ] = 1.0e5;
118  P[2*tuple+1] = 1.2e5;
119 
120  Y_vals[2*tuple ] = 0.2;
121  Y_vals[2*tuple+1] = 0.25;
122  }
123 
124  std::vector<PairScalars> Y(n_species,Y_vals);
125 
126  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
127  Y[4][2*tuple+1] = 0;
128 
129  const unsigned int n_T_samples = 10;
130  const Scalar T0 = 500;
131  const Scalar T_inc = 500;
132 
133  const PairScalars R_mix = chem_mixture.R(Y);
134 
135  std::vector<PairScalars> molar_densities(n_species,example);
136  std::vector<PairScalars> h_RT_minus_s_R(n_species, example);
137 
138  int return_flag = 0;
139 
140  for( unsigned int i = 0; i < n_T_samples; i++ )
141  {
142  PairScalars T = example;
143 
144  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
145  {
146  T[2*tuple ] = T0 + T_inc*static_cast<Scalar>(i);
147  T[2*tuple+1] = T[0]+T_inc/2;
148  }
149 
151 
152 #ifdef ANTIOCH_HAVE_GRVY
153  const std::string testnormal = testname + "-normal";
154  gt.BeginTimer(testnormal);
155 #endif
156 
157  const PairScalars rho = P/(R_mix*T);
158  chem_mixture.molar_densities(rho,Y,molar_densities);
159 
160  typedef typename Antioch::CEAThermodynamics<Scalar>::
161  template Cache<PairScalars> Cache;
162 
163  thermo.h_RT_minus_s_R(Cache(T),h_RT_minus_s_R);
164 
165  kinetics.compute_mass_sources( cond, molar_densities, h_RT_minus_s_R, omega_dot );
166 
167 #ifdef ANTIOCH_HAVE_GRVY
168  gt.EndTimer(testnormal);
169 #endif
170 
171  // Omega dot had better sum to 0.0
172  PairScalars sum = omega_dot[0];
173  for( unsigned int s = 1; s < n_species; s++ )
174  {
175  sum += omega_dot[s];
176  }
177  const Scalar sum_tol = std::numeric_limits<Scalar>::epsilon() * 5e6; // 5e-10;
178  const PairScalars abs_sum = abs(sum);
179  if( Antioch::max(abs_sum) > sum_tol )
180  {
181  return_flag = 1;
182  std::cerr << "Error: omega_dot did not sum to 0.0." << std::endl
183  << std::scientific << std::setprecision(16)
184  << "T = " << T << std::endl
185  << "sum = " << sum << ", sum_tol = " << sum_tol << std::endl;
186  for( unsigned int s = 0; s < n_species; s++ )
187  {
188  std::cerr << std::scientific << std::setprecision(16)
189  << "omega_dot(" << chem_mixture.chemical_species()[s]->species() << ") = "
190  << omega_dot[s] << std::endl;
191  }
192  std::cout << std::endl << std::endl;
193  }
194  }
195 
196 #ifdef ANTIOCH_HAVE_EIGEN
197  {
198  // To do: tests with Eigen instead of std vectors
199  }
200 #endif // ANTIOCH_HAVE_EIGEN
201 
202  return return_flag;
203 }
204 
205 
206 int main(int argc, char* argv[])
207 {
208  // Check command line count.
209  if( argc < 2 )
210  {
211  // TODO: Need more consistent error handling.
212  std::cerr << "Error: Must specify reaction set XML input file." << std::endl;
213  antioch_error();
214  }
215 
216  int returnval = 0;
217 
218  returnval = returnval ||
219  vectester (argv[1], std::valarray<float>(2*ANTIOCH_N_TUPLES), "valarray<float>");
220  returnval = returnval ||
221  vectester (argv[1], std::valarray<double>(2*ANTIOCH_N_TUPLES), "valarray<double>");
222  returnval = returnval ||
223  vectester (argv[1], std::valarray<long double>(2*ANTIOCH_N_TUPLES), "valarray<ld>");
224 #ifdef ANTIOCH_HAVE_EIGEN
225  returnval = returnval ||
226  vectester (argv[1], Eigen::Array<float, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXf");
227  returnval = returnval ||
228  vectester (argv[1], Eigen::Array<double, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXd");
229  returnval = returnval ||
230  vectester (argv[1], Eigen::Array<long double, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXld");
231 #endif
232 #ifdef ANTIOCH_HAVE_METAPHYSICL
233  returnval = returnval ||
234  vectester (argv[1], MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, float>(0), "NumberArray<float>");
235  returnval = returnval ||
236  vectester (argv[1], MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, double>(0), "NumberArray<double>");
237  returnval = returnval ||
238  vectester (argv[1], MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, long double>(0), "NumberArray<ld>");
239 #endif
240 #ifdef ANTIOCH_HAVE_VEXCL
241  vex::Context ctx_f (vex::Filter::All);
242  if (!ctx_f.empty())
243  returnval = returnval ||
244  vectester (argv[1], vex::vector<float> (ctx_f, 2*ANTIOCH_N_TUPLES), "vex::vector<float>");
245 
246  vex::Context ctx_d (vex::Filter::DoublePrecision);
247  if (!ctx_d.empty())
248  returnval = returnval ||
249  vectester (argv[1], vex::vector<double> (ctx_d, 2*ANTIOCH_N_TUPLES), "vex::vector<double>");
250 #endif
251 
252 #ifdef ANTIOCH_HAVE_GRVY
253  gt.Finalize();
254  gt.Summarize();
255 #endif
256 
257  return returnval;
258 }
Antioch::enable_if_c< is_eigen< T >::value, typename value_type< T >::type >::type max(const T &in)
Definition: eigen_utils.h:88
This class encapsulates all the reaction mechanisms considered in a chemical nonequilibrium simulatio...
int main(int argc, char *argv[])
CoeffType R(const unsigned int s) const
Gas constant for species s in [J/kg-K].
#define antioch_error()
Class to handle computing mass source terms for a given ReactionSet.
X(const unsigned int species, const StateType &M, const StateType &mass_fraction) const template< typename VectorStateType > void X(typename Antioch VectorStateType void molar_densities(const StateType &rho, const VectorStateType &mass_fractions, VectorStateType &molar_densities) const
Class storing chemical mixture properties.
StateType h_RT_minus_s_R(const Cache< StateType > &cache, unsigned int species) const
We currently need different specializations for scalar vs vector inputs here.
Definition: cea_thermo.h:420
const std::vector< ChemicalSpecies< CoeffType > * > & chemical_species() const
This class contains the conditions of the chemistry.
We parse the file here, with an exhaustive unit management.
int vectester(const std::string &input_name, const PairScalars &example, const std::string &testname)

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