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

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