antioch-0.4.0
photochemical_rate_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 // C++
33 #include <limits>
34 #include <string>
35 #include <iostream>
36 #include <iomanip>
37 #include <fstream>
38 
39 // Antioch
41 
42 #include "antioch/particle_flux.h"
46 
47 #include "antioch/vector_utils.h"
48 
49 template <typename Scalar>
50 int check_rate(const Scalar & rate_exact, const Scalar & rate)
51 {
52  const Scalar tol = std::numeric_limits<Scalar>::epsilon() * 100;
53  int return_flag = (rate_exact > tol); // not zero
54  if(return_flag)std::cout << "Error: rate is null" << std::endl;
55  if( std::abs( (rate - rate_exact)/rate_exact ) > tol )
56  {
57  std::cout << std::scientific << std::setprecision(16)
58  << "Error: Mismatch in rate values." << std::endl
59  << "rate = " << rate << std::endl
60  << "rate_exact = " << rate_exact << std::endl
61  << "relative error = " << std::abs(rate_exact - rate)/rate_exact << std::endl
62  << "tolerance = " << tol << std::endl;
63 
64  return_flag = 1;
65  }
66 
67  return return_flag;
68 }
69 
70 template <typename Scalar>
71 int tester(std::string path_to_files)
72 {
73  std::ifstream CH4(path_to_files + "/CH4_hv_cs.dat");
74  std::ifstream hv(path_to_files + "/solar_flux.dat");
75 
76  std::string first_line;
77 
78  getline(CH4,first_line);
79  getline(hv,first_line);
80 
81  std::vector<Scalar> CH4_cs;
82  std::vector<Scalar> CH4_lambda;
83  std::vector<Scalar> hv_irr;
84  std::vector<Scalar> hv_lambda;
85 
86  while(!CH4.eof())
87  {
88  Scalar cs,l(-1);
89  CH4 >> l >> cs;
90  if(!CH4.good())break;
91  CH4_lambda.push_back(l);
92  CH4_cs.push_back(cs);
93  }
94  CH4.close();
95 
96  while(!hv.eof())
97  {
98  Scalar w,l(-1),dw;
99  hv >> l >> w >> dw;
100  if(!hv.good())break;
101  hv_lambda.push_back(l * 10); //nm -> Angström
102  hv_irr.push_back(w * 1e-4L // * 1e-4: m-2 -> cm-2
103  / (Antioch::Constants::Planck_constant<Scalar>() * Antioch::Constants::light_celerity<Scalar>() / l)// /(h*c/lambda): energy -> number of photons
104  / 10); // by Angström
105  }
106  hv.close();
107 
108  Antioch::ParticleFlux<std::vector<Scalar> > part_flux(hv_lambda,hv_irr);
109 
110  Antioch::PhotochemicalRate<Scalar, std::vector<Scalar> > rate_hv(CH4_cs,CH4_lambda);
111 
113  std::vector<Scalar> sigma_rescaled(hv_lambda.size());
114  bin.y_on_custom_grid(CH4_lambda,CH4_cs,hv_lambda,sigma_rescaled);
115 
116  Scalar rate = rate_hv.rate(part_flux);
117 
118  Scalar rate_exact(0);
119 
120  for(unsigned int il = 0; il < hv_lambda.size() - 1; il++)
121  {
122  rate_exact += sigma_rescaled[il] * hv_irr[il] * (hv_lambda[il+1] - hv_lambda[il]);
123  }
124 
125  int return_flag = check_rate(rate_exact,rate);
126 
127  // multiplying by 2 the cross-section
128  int il = CH4_cs.size() * 2 / 3;
129  CH4_cs[il] *= 2;
130 
131  bin.y_on_custom_grid(CH4_lambda,CH4_cs,hv_lambda,sigma_rescaled);
132 
133  Antioch::set_zero(rate_exact);
134  for(unsigned int il = 0; il < hv_lambda.size() - 1; il++)
135  {
136  rate_exact += sigma_rescaled[il] * hv_irr[il] * (hv_lambda[il+1] - hv_lambda[il]);
137  }
139  rate = rate_hv.rate(part_flux);
140 
141  return_flag = check_rate(rate_exact,rate) || return_flag;
142 
143  // multiplying by 2 one value of the cross-section
144  il = CH4_cs.size()/2;
145  CH4_cs[il] *= 2;
146 
147  bin.y_on_custom_grid(CH4_lambda,CH4_cs,hv_lambda,sigma_rescaled);
148 
149  Antioch::set_zero(rate_exact);
150  for(unsigned int il = 0; il < hv_lambda.size() - 1; il++)
151  {
152  rate_exact += sigma_rescaled[il] * hv_irr[il] * (hv_lambda[il+1] - hv_lambda[il]);
153  }
154 
155 // the other way to reset
157  rate = rate_hv.rate(part_flux);
158 
159  return_flag = check_rate(rate_exact,rate) || return_flag;
160 
161  return return_flag;
162 }
163 
164 int main(int argc, char* argv[])
165 {
166  if( argc < 2 )
167  {
168  // TODO: Need more consistent error handling.
169  std::cerr << "Error: Must specify input files location." << std::endl;
170  antioch_error();
171  }
172 
173  return (tester<float>(std::string(argv[1])) ||
174  tester<double>(std::string(argv[1])) ||
175  tester<long double>(std::string(argv[1]))
176  );
177 }
Stores the incoming flux of particles.
Definition: particle_flux.h:38
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...
#define antioch_error()
int check_rate(const Scalar &rate_exact, const Scalar &rate)
void set_parameter(KineticsModel::Parameters parameter, int l, CoeffType new_value)
set one value of one parameter, characterized by enum and its index
void set_zero(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a)
Definition: eigen_utils.h:217
void y_on_custom_grid(const VectorCoeffType &x_old, const VectorCoeffType &y_old, const VectorStateType &x_new, VectorStateType &y_new) const
int main(int argc, char *argv[])
int tester(std::string path_to_files)
Photochemical rate.
Definition: kinetics_type.h:65

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