antioch-0.4.0
kinetics_settings_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 // C++
28 #include <limits>
29 #include <vector>
30 // Antioch
31 
33 
35 //#include "antioch/physical_constants.h"
36 //#include "antioch/units.h"
37 
38 #include "antioch/vector_utils.h"
39 
40 
41 
42 template <typename Scalar>
43 int test_values(const Scalar & Cf, const Scalar & eta, const Scalar & Ea, const Scalar & D, const Scalar & Tref, const Scalar & R, const Antioch::KineticsType<Scalar> & rate_base)
44 {
45  using std::abs;
46  using std::exp;
47  using std::pow;
48 
49  int return_flag = 0;
50 
51  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 100;
52 
53  for(Scalar T = 300.1L; T <= 2500.1L; T += 10.L)
54  {
55 
56  const Scalar rate_exact = Cf*pow(T/Tref,eta)*exp(-Ea/(R*T) + D * T);
57  const Scalar derive_exact = exp(-Ea/(R*T) + D * T) * pow(T/Tref,eta) * Cf * (Ea/(R*T*T) + eta/T + D );
58 
60 
61  Scalar rate1 = rate_base(cond);
62  Scalar deriveRate1 = rate_base.derivative(cond);
63  Scalar rate;
64  Scalar deriveRate;
65 
66  rate_base.compute_rate_and_derivative(cond,rate,deriveRate);
67 
68  if( abs( (rate1 - rate_exact)/rate_exact ) > tol )
69  {
70  std::cerr << std::scientific << std::setprecision(16)
71  << "Error: Mismatch in rate values." << std::endl
72  << "T = " << T << " K" << std::endl
73  << "rate(T) = " << rate1 << std::endl
74  << "rate_exact = " << rate_exact << std::endl
75  << "relative difference = " << abs( (rate1 - rate_exact)/rate_exact ) << std::endl
76  << "tolerance = " << tol << std::endl
77  << "on rate " << rate_base << std::endl << std::endl;
78 
79  return_flag = 1;
80  }
81  if( abs( (rate - rate_exact)/rate_exact ) > tol )
82  {
83  std::cerr << std::scientific << std::setprecision(16)
84  << "Error: Mismatch in rate values." << std::endl
85  << "T = " << T << " K" << std::endl
86  << "rate(T) = " << rate << std::endl
87  << "rate_exact = " << rate_exact << std::endl
88  << "relative difference = " << abs( (rate - rate_exact)/rate_exact ) << std::endl
89  << "tolerance = " << tol << std::endl
90  << "on rate " << rate_base << std::endl << std::endl;
91 
92  return_flag = 1;
93  }
94  if( abs( (deriveRate1 - derive_exact)/derive_exact ) > tol )
95  {
96  std::cerr << std::scientific << std::setprecision(16)
97  << "Error: Mismatch in rate derivative values." << std::endl
98  << "T = " << T << " K" << std::endl
99  << "drate_dT(T) = " << deriveRate1 << std::endl
100  << "derive_exact = " << derive_exact << std::endl
101  << "relative difference = " << abs( (deriveRate1 - derive_exact)/derive_exact ) << std::endl
102  << "tolerance = " << tol << std::endl
103  << "on rate " << rate_base << std::endl << std::endl;
104 
105  return_flag = 1;
106  }
107  if( abs( (deriveRate - derive_exact)/derive_exact ) > tol )
108  {
109  std::cerr << std::scientific << std::setprecision(16)
110  << "Error: Mismatch in rate derivative values." << std::endl
111  << "T = " << T << " K" << std::endl
112  << "drate_dT(T) = " << deriveRate << std::endl
113  << "derive_exact = " << derive_exact << std::endl
114  << "relative difference = " << abs( (deriveRate - derive_exact)/derive_exact ) << std::endl
115  << "tolerance = " << tol << std::endl
116  << "on rate " << rate_base << std::endl << std::endl;
117 
118  return_flag = 1;
119  }
120 
121  if(return_flag)break;
122  }
123  return return_flag;
124 }
125 
126 template <typename Scalar>
127 int tester()
128 {
129 
130  const Scalar zero(0.L);
131  Scalar Cf = 1.4L;
132  Scalar eta = 1.2L;
133  Scalar Ea = 298;
134  Scalar D = 0.05L;
135  Scalar Tref = 1;
136  Scalar R = 1;
137 
138  Scalar Cf_reset = 1e-7L;
139  Scalar eta_reset = 1.5L;
140  Scalar Ea_reset = 36000;
141  Scalar D_reset = -5e-2L;
142  Scalar Tref_reset = 298;
143  Scalar R_reset = Antioch::Constants::R_universal<Scalar>() / Antioch::Units<Scalar>("cal").get_SI_factor();
144 
146 
147  // constant
148  std::vector<Scalar> coeffs(1,Cf);
149 
150  Antioch::KineticsType<Scalar> * kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::CONSTANT);
151 
152  int return_flag = test_values(Cf,zero,zero,zero,Tref,R,*kin_base);
153 
155 
156  return_flag = test_values(Cf_reset,zero,zero,zero,Tref,R,*kin_base) || return_flag;
157 
158  delete kin_base;
159 
160  // Hercourt-Essen
161  coeffs.resize(3);
162  coeffs[0] = Cf;
163  coeffs[1] = eta;
164  coeffs[2] = Tref;
165 
166  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::HERCOURT_ESSEN);
167 
168  return_flag = test_values(Cf,eta,zero,zero,Tref,R,*kin_base) || return_flag;
169 
171 
172  return_flag = test_values(Cf,eta,zero,zero,Tref_reset,R,*kin_base) || return_flag;
173 
174  delete kin_base;
175 
176  // Arrhenius
177  coeffs.resize(3);
178  coeffs[0] = Cf;
179  coeffs[1] = Ea;
180  coeffs[2] = R;
181 
182  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::ARRHENIUS);
183 
184  return_flag = test_values(Cf,zero,Ea,zero,Tref,R,*kin_base) || return_flag;
185 
187 
188  return_flag = test_values(Cf,zero,Ea_reset,zero,Tref,R,*kin_base) || return_flag;
189 
190  delete kin_base;
191 
192  // Berthelot
193  coeffs.resize(2);
194  coeffs[0] = Cf;
195  coeffs[1] = D;
196 
197  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::BERTHELOT);
198 
199  return_flag = test_values(Cf,zero,zero,D,Tref,R,*kin_base) || return_flag;
200 
202 
203  return_flag = test_values(Cf,zero,zero,D_reset,Tref,R,*kin_base) || return_flag;
204 
205  delete kin_base;
206 
207  // Berthelot Hercourt-Essen
208  coeffs.resize(4);
209  coeffs[0] = Cf;
210  coeffs[1] = eta;
211  coeffs[2] = D;
212  coeffs[3] = Tref;
213 
214  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::BHE);
215 
216  return_flag = test_values(Cf,eta,zero,D,Tref,R,*kin_base) || return_flag;
217 
219 
221 
222  return_flag = test_values(Cf_reset,eta,zero,D_reset,Tref,R,*kin_base) || return_flag;
223 
224  delete kin_base;
225 
226  // Kooij
227  coeffs.resize(5);
228  coeffs[0] = Cf;
229  coeffs[1] = eta;
230  coeffs[2] = Ea;
231  coeffs[3] = Tref;
232  coeffs[4] = R;
233 
234  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::KOOIJ);
235 
236  return_flag = test_values(Cf,eta,Ea,zero,Tref,R,*kin_base) || return_flag;
237 
239 
240  Antioch::reset_parameter_of_rate(*kin_base,Antioch::KineticsModel::E,Ea_reset,"cal/mol");
241 
242  return_flag = test_values(Cf,eta,Ea_reset,zero,Tref,R_reset,*kin_base) || return_flag;
243 
244  delete kin_base;
245 
246  // Van't Hoff
247  coeffs.resize(6);
248  coeffs[0] = Cf;
249  coeffs[1] = eta;
250  coeffs[2] = Ea;
251  coeffs[3] = D;
252  coeffs[4] = Tref;
253  coeffs[5] = R;
254 
255  kin_base = Antioch::build_rate<Scalar>(coeffs,Antioch::KineticsModel::VANTHOFF);
256 
257  return_flag = test_values(Cf,eta,Ea,D,Tref,R,*kin_base) || return_flag;
258 
260 
262 
264 
265  return_flag = test_values(Cf_reset,eta,Ea,D_reset,Tref_reset,R,*kin_base) || return_flag;
266 
268  Ea_reset = -50000; // sensitive here...
269  coeffs[0] = Cf_reset;
270  coeffs[1] = eta_reset;
271  coeffs[2] = Ea_reset;
272  coeffs[3] = D_reset;
273  coeffs[4] = Tref_reset;
274  coeffs[5] = R_reset;
275  Antioch::reset_rate(*kin_base,coeffs);
276 
277  return_flag = test_values(Cf_reset,eta_reset,Ea_reset,D_reset,Tref_reset,R_reset,*kin_base) || return_flag;
278 
279  return return_flag;
280 }
281 
282 int main()
283 {
284  return (tester<double>() ||
285  tester<long double>() ||
286  tester<float>());
287 }
StateType derivative(const KineticsConditions< StateType, VectorStateType > &conditions) const
void compute_rate_and_derivative(const KineticsConditions< StateType, VectorStateType > &conditions, StateType &rate, StateType &drate_dT) const
base class for kinetics models
Definition: kinetics_type.h:82
void reset_parameter_of_rate(KineticsType< CoeffType, VectorCoeffType > &rate, KineticsModel::Parameters parameter, const CoeffType new_value, const std::string &unit="SI")
The rate constant models derived from the Arrhenius model have an activation energy which is stored a...
int test_values(const Scalar &Cf, const Scalar &eta, const Scalar &Ea, const Scalar &D, const Scalar &Tref, const Scalar &R, const Antioch::KineticsType< Scalar > &rate_base)
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
int tester()
T get_SI_factor() const
Multiplicative coefficient getter.
Definition: units.h:334
void reset_rate(KineticsType< CoeffType, VectorCoeffType > &kin, const VectorType &coefs)
An advanced unit class.
Definition: units.h:111
This class contains the conditions of the chemistry.
int main()

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