antioch-0.4.0
berthelothercourtessen_rate_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 // valarray has to be declared before Antioch or gcc can't find the
28 // right versions of exp() and pow() to use??
29 
30 #include "antioch_config.h"
31 
32 #include <valarray>
33 
34 #ifdef ANTIOCH_HAVE_EIGEN
35 #include "Eigen/Dense"
36 #endif
37 
38 #ifdef ANTIOCH_HAVE_METAPHYSICL
39 #include "metaphysicl/numberarray.h"
40 #endif
41 
42 #ifdef ANTIOCH_HAVE_VEXCL
43 #include "vexcl/vexcl.hpp"
44 #endif
45 
49 
51 
52 #include "antioch/eigen_utils.h"
54 #include "antioch/valarray_utils.h"
55 
56 #include <cmath>
57 #include <limits>
58 
59 #ifdef ANTIOCH_HAVE_GRVY
60 #include "grvy.h"
61 
62 GRVY::GRVY_Timer_Class gt;
63 #endif
64 
65 template <typename PairScalars>
66 int check_rate_and_derivative(const PairScalars & rate_exact, const PairScalars & derive_exact,
67  const PairScalars & rate, const PairScalars & derive, const PairScalars & T)
68 {
69  typedef typename Antioch::value_type<PairScalars>::type Scalar;
70  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 2;
71 
72  int return_flag(0);
73  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
74  {
75  if( abs( (rate[2*tuple] - rate_exact[2*tuple])/rate_exact[2*tuple] ) > tol )
76  {
77  std::cout << std::scientific << std::setprecision(16)
78  << "Error: Mismatch in rate values." << std::endl
79  << "T = " << T << " K" << std::endl
80  << "rate(T) = " << rate[2*tuple] << std::endl
81  << "rate_exact = " << rate_exact[2*tuple] << std::endl
82  << "relative difference = " << abs( (rate[2*tuple] - rate_exact[2*tuple])/rate_exact[2*tuple] ) << std::endl
83  << "tolerance = " << tol << std::endl;
84 
85  return_flag = 1;
86  }
87  if( abs( (rate[2*tuple+1] - rate_exact[2*tuple+1])/rate_exact[2*tuple+1] ) > tol )
88  {
89  std::cout << std::scientific << std::setprecision(16)
90  << "Error: Mismatch in rate values." << std::endl
91  << "T = " << T << " K" << std::endl
92  << "rate(T) = " << rate[2*tuple] << std::endl
93  << "rate_exact = " << rate_exact[2*tuple+1] << std::endl
94  << "relative difference = " << abs( (rate[2*tuple] - rate_exact[2*tuple+1])/rate_exact[2*tuple+1] ) << std::endl
95  << "tolerance = " << tol << std::endl;
96 
97  return_flag = 1;
98  }
99  if( abs( (derive[2*tuple] - derive_exact[2*tuple])/derive_exact[2*tuple] ) > tol )
100  {
101  std::cout << std::scientific << std::setprecision(16)
102  << "Error: Mismatch in rate derivative values." << std::endl
103  << "T = " << T << " K" << std::endl
104  << "drate_dT(T) = " << derive[2*tuple] << std::endl
105  << "derive_exact = " << derive_exact[2*tuple] << std::endl
106  << "relative difference = " << abs( (derive[2*tuple] - derive_exact[2*tuple])/derive_exact[2*tuple] ) << std::endl
107  << "tolerance = " << tol << std::endl;
108 
109  return_flag = 1;
110  }
111  if( abs( (derive[2*tuple+1] - derive_exact[2*tuple+1])/derive_exact[2*tuple+1] ) > tol )
112  {
113  std::cout << std::scientific << std::setprecision(16)
114  << "Error: Mismatch in rate derivative values." << std::endl
115  << "T = " << T << " K" << std::endl
116  << "drate_dT(T) = " << derive[2*tuple+1] << std::endl
117  << "derive_exact = " << derive_exact[2*tuple+1] << std::endl
118  << "relative difference = " << abs( (derive[2*tuple+1] - derive_exact[2*tuple+1])/derive_exact[2*tuple+1] ) << std::endl
119  << "tolerance = " << tol << std::endl;
120 
121  return_flag = 1;
122  }
123 
124  }
125  return return_flag;
126 }
127 
128 template <typename PairScalars>
129 int vectester(const PairScalars& example, const std::string & testname)
130 {
131  using std::abs;
132  using std::exp;
133  using std::pow;
134 
135  typedef typename Antioch::value_type<PairScalars>::type Scalar;
136 
137  const Scalar Cf = 1.4;
138  const Scalar eta = 1.2;
139  const Scalar D = -2.5;
140 
141  Antioch::BerthelotHercourtEssenRate<Scalar> berthelothercourtessen_rate(Cf,eta,D);
142 
143  // Construct from example to avoid resizing issues
144  PairScalars T = example;
145  PairScalars rate_exact = example;
146  PairScalars derive_exact = example;
147 
148  for (unsigned int tuple=0; tuple != ANTIOCH_N_TUPLES; ++tuple)
149  {
150  T[2*tuple] = 1500.1;
151  T[2*tuple+1] = 1600.1;
152  rate_exact[2*tuple] = Cf*pow(Scalar(1500.1),eta)*exp(D*1500.1);
153  rate_exact[2*tuple+1] = Cf*pow(Scalar(1600.1),eta)*exp(D*1600.1);
154  derive_exact[2*tuple] = Cf * pow(Scalar(1500.1),eta) * exp(D*Scalar(1500.1)) * (D + eta/Scalar(1500.1));
155  derive_exact[2*tuple+1] = Cf * pow(Scalar(1600.1),eta) * exp(D*Scalar(1600.1)) * (D + eta/Scalar(1600.1));
156  }
158 
159  int return_flag = 0;
160 
161 // KineticsConditions
162 #ifdef ANTIOCH_HAVE_GRVY
163  gt.BeginTimer(testname);
164 #endif
165 
166  PairScalars rate = berthelothercourtessen_rate(cond);
167  PairScalars derive = berthelothercourtessen_rate.derivative(cond);
168 
169 #ifdef ANTIOCH_HAVE_GRVY
170  gt.EndTimer(testname);
171 #endif
172 
173  return_flag = check_rate_and_derivative(rate_exact, derive_exact, rate, derive,T) || return_flag;
174 
175 #ifdef ANTIOCH_HAVE_GRVY
176  gt.BeginTimer(testname);
177 #endif
178 
179  berthelothercourtessen_rate.rate_and_derivative(cond,rate,derive);
180 
181 #ifdef ANTIOCH_HAVE_GRVY
182  gt.EndTimer(testname);
183 #endif
184 
185 // T
186 #ifdef ANTIOCH_HAVE_GRVY
187  gt.BeginTimer(testname);
188 #endif
189 
190  rate = berthelothercourtessen_rate(T);
191  derive = berthelothercourtessen_rate.derivative(T);
192 
193 #ifdef ANTIOCH_HAVE_GRVY
194  gt.EndTimer(testname);
195 #endif
196 
197  return_flag = check_rate_and_derivative(rate_exact, derive_exact, rate, derive,T) || return_flag;
198 
199 #ifdef ANTIOCH_HAVE_GRVY
200  gt.BeginTimer(testname);
201 #endif
202 
203  berthelothercourtessen_rate.rate_and_derivative(T,rate,derive);
204 
205 #ifdef ANTIOCH_HAVE_GRVY
206  gt.EndTimer(testname);
207 #endif
208  return_flag = check_rate_and_derivative(rate_exact, derive_exact, rate, derive,T) || return_flag;
209 
210 
211 
212 
213  std::cout << "Berthelot Hercourt Essen rate: " << berthelothercourtessen_rate << std::endl;
214 
215  return return_flag;
216 }
217 
218 
219 int main()
220 {
221  int returnval = 0;
222 
223  returnval = returnval ||
224  vectester (std::valarray<float>(2*ANTIOCH_N_TUPLES), "valarray<float>");
225  returnval = returnval ||
226  vectester (std::valarray<double>(2*ANTIOCH_N_TUPLES), "valarray<double>");
227  returnval = returnval ||
228  vectester (std::valarray<long double>(2*ANTIOCH_N_TUPLES), "valarray<ld>");
229 #ifdef ANTIOCH_HAVE_EIGEN
230  returnval = returnval ||
231  vectester (Eigen::Array<float, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXf");
232  returnval = returnval ||
233  vectester (Eigen::Array<double, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXd");
234  returnval = returnval ||
235  vectester (Eigen::Array<long double, 2*ANTIOCH_N_TUPLES, 1>(), "Eigen::ArrayXld");
236 #endif
237 #ifdef ANTIOCH_HAVE_METAPHYSICL
238  returnval = returnval ||
239  vectester (MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, float> (0), "NumberArray<float>");
240  returnval = returnval ||
241  vectester (MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, double> (0), "NumberArray<double>");
242  returnval = returnval ||
243  vectester (MetaPhysicL::NumberArray<2*ANTIOCH_N_TUPLES, long double> (0), "NumberArray<ld>");
244 #endif
245 #ifdef ANTIOCH_HAVE_VEXCL
246  vex::Context ctx_f (vex::Filter::All);
247  if (!ctx_f.empty())
248  returnval = returnval ||
249  vectester (vex::vector<float> (ctx_f, 2*ANTIOCH_N_TUPLES), "vex::vector<float>");
250 
251  vex::Context ctx_d (vex::Filter::DoublePrecision);
252  if (!ctx_d.empty())
253  returnval = returnval ||
254  vectester (vex::vector<double> (ctx_d, 2*ANTIOCH_N_TUPLES), "vex::vector<double>");
255 #endif
256 
257 #ifdef ANTIOCH_HAVE_GRVY
258  gt.Finalize();
259  gt.Summarize();
260 #endif
261 
262  return returnval;
263 }
int check_rate_and_derivative(const PairScalars &rate_exact, const PairScalars &derive_exact, const PairScalars &rate, const PairScalars &derive, const PairScalars &T)
_Cf VectorStateType VectorStateType(*) VectorStateType voi rate_and_derivative)(const KineticsConditions< StateType, VectorStateType > &T, StateType &rate, StateType &drate_dT) const
Berthelot Hercourt-Essen rate equation.
int vectester(const PairScalars &example, const std::string &testname)
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
_Cf VectorStateType VectorStateType derivative(const KineticsConditions< StateType, VectorStateType > &T) const ANTIOCH_AUTOFUNC(StateType
This class contains the conditions of the chemistry.

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