28 #ifndef ANTIOCH_REACTION_SET_H
29 #define ANTIOCH_REACTION_SET_H
58 template<
typename CoeffType=
double>
65 ReactionSet(
const ChemicalMixture<CoeffType>& chem_mixture );
87 const Reaction<CoeffType>&
reaction(
const unsigned int r)
const;
90 Reaction<CoeffType>&
reaction(
const unsigned int r);
93 unsigned int reaction_by_id(
const std::string & reaction_id)
const;
98 template <
typename ParamType>
99 void set_parameter_of_reaction(
const std::string & reaction_id,
const std::vector<std::string> & keywords, ParamType value);
109 template <
typename StateType,
typename VectorStateType,
typename VectorReactionsType>
111 const VectorStateType& molar_densities,
112 const VectorStateType& h_RT_minus_s_R,
113 VectorReactionsType& net_reaction_rates )
const;
116 template <
typename StateType,
typename VectorStateType,
typename VectorReactionsType,
typename MatrixReactionsType>
118 const VectorStateType& molar_densities,
119 const VectorStateType& h_RT_minus_s_R,
120 const VectorStateType& dh_RT_minus_s_R_dT,
121 VectorReactionsType& net_reaction_rates,
122 VectorReactionsType& dnet_rate_dT,
123 MatrixReactionsType& dnet_rate_dX_s )
const;
126 template <
typename StateType,
typename VectorStateType>
128 const KineticsConditions<StateType,VectorStateType>& conditions,
129 const VectorStateType& molar_densities,
130 const VectorStateType& h_RT_minus_s_R ,
131 std::vector<VectorStateType> &lossMatrix,
132 std::vector<VectorStateType> &prodMatrix,
133 std::vector<VectorStateType> &netMatrix)
const;
136 template<
typename StateType,
typename VectorStateType>
138 const VectorStateType& molar_densities,
139 const VectorStateType& h_RT_minus_s_R,
140 VectorStateType& net_rates,
141 VectorStateType& kfwd_const,
142 VectorStateType& kbkwd_const,
143 VectorStateType& kfwd,
144 VectorStateType& kbkwd,
145 VectorStateType& fwd_conc,
146 VectorStateType& bkwd_conc)
const;
150 void print( std::ostream& os = std::cout )
const;
153 friend std::ostream& operator<<( std::ostream& os, const ReactionSet<CoeffType>& rset )
170 void find_kinetics_model_parameter(
const unsigned int r,
const std::vector<std::string> & keywords,
unsigned int & nr, std::string & unit,
int & l)
const;
191 template<
typename CoeffType>
195 return _chem_mixture.n_species();
198 template<
typename CoeffType>
202 return _reactions.size();
205 template<
typename CoeffType>
209 _reactions.push_back(reaction);
212 _reactions.back()->initialize(_reactions.size() - 1);
217 template<
typename CoeffType>
224 delete _reactions[nr];
227 _reactions.erase(_reactions.begin() + nr);
230 template<
typename CoeffType>
235 return *_reactions[r];
238 template<
typename CoeffType>
243 return *_reactions[r];
246 template<
typename CoeffType>
250 return _chem_mixture;
254 template<
typename CoeffType>
257 : _chem_mixture(chem_mixture),
264 template<
typename CoeffType>
268 for(
unsigned int ir = 0; ir < _reactions.size(); ir++)
delete _reactions[ir];
272 template<
typename CoeffType>
273 template<
typename StateType,
typename VectorStateType,
typename VectorReactionsType>
276 const VectorStateType& molar_densities,
277 const VectorStateType& h_RT_minus_s_R,
278 VectorReactionsType& net_reaction_rates )
const
289 const StateType P0_RT = _P0_R/conditions.
T();
292 for (
unsigned int rxn=0; rxn<this->n_reactions(); rxn++)
294 net_reaction_rates[rxn] = this->reaction(rxn).compute_rate_of_progress(molar_densities, conditions, P0_RT, h_RT_minus_s_R);
300 template<
typename CoeffType>
301 template<
typename StateType,
typename VectorStateType,
typename VectorReactionsType,
typename MatrixReactionsType>
304 const VectorStateType& molar_densities,
305 const VectorStateType& h_RT_minus_s_R,
306 const VectorStateType& dh_RT_minus_s_R_dT,
307 VectorReactionsType& net_reaction_rates,
308 VectorReactionsType& dnet_rate_dT,
309 MatrixReactionsType& dnet_rate_dX_s )
const
316 for (
unsigned int r=0; r < this->n_reactions(); r++)
329 const StateType P0_RT = _P0_R/conditions.
T();
332 for (
unsigned int rxn=0; rxn<this->n_reactions(); rxn++)
334 this->reaction(rxn).compute_rate_of_progress_and_derivatives( molar_densities, _chem_mixture,
335 conditions, P0_RT, h_RT_minus_s_R, dh_RT_minus_s_R_dT,
336 net_reaction_rates[rxn],
338 dnet_rate_dX_s[rxn] );
345 template<
typename CoeffType>
350 for(r = 0; r < this->n_reactions(); r++)
352 if(this->reaction(r).id() == reaction_id)
355 if(r >= this->n_reactions())
357 std::string errmsg =
"Error: did not find reaction \"" + reaction_id +
"\"\nIds are: ";
358 for(r = 0; r < this->n_reactions(); r++)
360 errmsg += this->reaction(r).id() +
", ";
361 if(r%10 == 0)errmsg +=
"\n";
372 template<
typename CoeffType>
380 switch(this->reaction(r).type())
384 nr = std::stoi(keywords[1]);
385 if(keywords.size() > 2)unit = keywords[2];
414 if(keywords.size() > 2)unit = keywords[2];
421 l = std::stoi(keywords[1]);
422 if(keywords.size() > 2)unit = keywords[2];
428 if(keywords.size() > 1)unit = keywords[1];
436 template<
typename CoeffType>
451 if(!this->chemical_mixture().species_name_map().count(keywords[1]))
454 species = this->chemical_mixture().species_name_map().at(keywords[1]);
460 template<
typename CoeffType>
471 unsigned int r = this->reaction_by_id(reaction_id);
484 std::string unit(
"SI");
487 this->find_kinetics_model_parameter(r,keywords,nr,unit,l);
490 parameter = reaction(r).get_parameter_of_rate(paramKin, nr, unit);
496 this->find_chemical_process_parameter(paramChem, keywords, species);
499 parameter = reaction(r).get_parameter_of_chemical_process(paramChem, species);
509 template<
typename CoeffType>
510 template <
typename ParamType>
517 unsigned int r = this->reaction_by_id(reaction_id);
529 std::string unit(
"SI");
532 this->find_kinetics_model_parameter(r,keywords,nr,unit,l);
535 (l < 0)?this->reaction(r).set_parameter_of_rate(paramKin, value, nr, unit):
536 this->reaction(r).set_parameter_of_rate(paramKin, value, nr, l, unit);
542 this->find_chemical_process_parameter(paramChem, keywords, species);
545 this->reaction(r).set_parameter_of_chemical_process(paramChem, value, species);
553 template<
typename CoeffType>
554 template<
typename StateType,
typename VectorStateType>
558 const VectorStateType& molar_densities,
559 const VectorStateType& h_RT_minus_s_R,
560 std::vector<VectorStateType>& lossMatrix,
561 std::vector<VectorStateType>& prodMatrix,
562 std::vector<VectorStateType>& netMatrix )
const
566 VectorStateType netRate,kfwd_const,kbkwd_const,kfwd,kbkwd,fwd_conc,bkwd_conc;
569 get_reactive_scheme(conditions,molar_densities,h_RT_minus_s_R,netRate,kfwd_const,kbkwd_const,kfwd,kbkwd,fwd_conc,bkwd_conc);
571 lossMatrix.resize(this->n_species());
572 prodMatrix.resize(this->n_species());
573 netMatrix.resize(this->n_species());
575 for(
unsigned int s = 0; s < this->n_species(); s++)
577 lossMatrix[s].resize(this->n_reactions(),0.);
578 prodMatrix[s].resize(this->n_reactions(),0.);
579 netMatrix[s].resize(this->n_reactions(),0.);
584 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
587 for (
unsigned int r=0; r<reaction.
n_reactants(); r++)
593 for (
unsigned int p=0; p<reaction.
n_products(); p++)
602 output <<
"# molar units considered for those budgets (kmol, m3, s)" << std::endl;
603 output <<
"# formatted as follow:" << std::endl;
604 output <<
"# rows: species, columns: reactions" << std::endl;
605 output <<
"#" << std::endl;
606 output <<
"# | equation" << std::endl;
607 output <<
"# | rate coefficient forward , rate coefficient backward" << std::endl;
608 output <<
"# | forward concentrations , backward concentrations" << std::endl;
609 output <<
"# | rate forward , rate backward" << std::endl;
610 output <<
"# ------------------------------------------------------------------" << std::endl;
611 output <<
"# species | production term , loss term" << std::endl;
612 output <<
"# species | net production/loss term" << std::endl;
613 output <<
"#" << std::endl << std::endl;
618 output << std::setw(10) <<
" ";
619 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
621 output << std::setw(31) << this->_reactions[rxn]->equation();
625 output << std::setw(10) <<
" ";
626 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
628 output << std::setw(15) << kfwd_const[rxn] <<
"," << std::setw(15) << kbkwd_const[rxn];
632 output << std::setw(10) <<
" ";
633 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
635 output << std::setw(15) << fwd_conc[rxn] <<
"," << std::setw(15) << bkwd_conc[rxn];
639 output << std::setw(10) <<
" ";
640 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
642 output << std::setw(15) << kfwd[rxn] <<
"," << std::setw(15) << kbkwd[rxn];
645 output <<
"----------------------------" << std::endl;
649 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
651 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species();
652 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
654 output << std::setw(15) << std::scientific << std::setprecision(6) << prodMatrix[isp][rxn] <<
","
655 << std::setw(15) << std::scientific << std::setprecision(6) << lossMatrix[isp][rxn];
659 output << std::setw(10) <<
"";
660 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
662 output << std::setw(31) << std::scientific << std::setprecision(16) << netMatrix[isp][rxn];
670 output <<
"# production table" << std::endl << std::endl;
673 output << std::setw(10) <<
" ";
674 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
676 output << std::setw(31) << this->_reactions[rxn]->equation();
678 output << std::setw(31) <<
"Total" << std::endl;
679 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
681 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species();
682 CoeffType rowTotal(0.);
683 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
685 output << std::setw(31) << std::scientific << std::setprecision(16) << prodMatrix[isp][rxn];
686 rowTotal += prodMatrix[isp][rxn];
688 output << std::setw(31) << std::scientific << std::setprecision(16) << rowTotal << std::endl;
690 output << std::endl << std::endl;
692 output <<
"# loss table" << std::endl << std::endl;
695 output << std::setw(10) <<
" ";
696 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
698 output << std::setw(31) << this->_reactions[rxn]->equation();
700 output << std::setw(31) <<
"Total" << std::endl;
701 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
703 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species();
704 CoeffType rowTotal(0.);
705 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
707 output << std::setw(31) << std::scientific << std::setprecision(16) << lossMatrix[isp][rxn];
708 rowTotal += lossMatrix[isp][rxn];
710 output << std::setw(31) << std::scientific << std::setprecision(16) << rowTotal << std::endl;
712 output << std::endl << std::endl;
714 output <<
"# net table" << std::endl << std::endl;
717 output << std::setw(10) <<
" ";
718 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
720 output << std::setw(31) << this->_reactions[rxn]->equation();
722 output << std::setw(31) <<
"Total" << std::endl;
724 CoeffType columnTotal(0.);
725 std::vector<CoeffType> columnSum;
726 columnSum.resize(this->n_reactions(),0.);
727 CoeffType netTotalRow(0.);
729 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
731 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species();
732 CoeffType rowTotal(0.);
733 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
735 output << std::setw(31) << std::scientific << std::setprecision(16) << netMatrix[isp][rxn];
736 rowTotal += netMatrix[isp][rxn];
737 columnSum[rxn] += netMatrix[isp][rxn];
739 output << std::setw(31) << std::scientific << std::setprecision(16) << rowTotal << std::endl;
740 netTotalRow += rowTotal;
742 output << std::setw(10) <<
"Total";
743 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
745 output << std::setw(31) << std::scientific << std::setprecision(16) << columnSum[rxn];
746 columnTotal += columnSum[rxn];
748 output << std::endl << std::endl;
749 output <<
"sum of row sums: " << std::scientific << std::setprecision(16) << netTotalRow << std::endl;
750 output <<
"sum of column sums: " << std::scientific << std::setprecision(16) << columnTotal << std::endl << std::endl;
751 output <<
"# net table, mass here (kg, m3, s)" << std::endl << std::endl;
754 output << std::setw(10) <<
" ";
755 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
757 output << std::setw(31) << this->_reactions[rxn]->equation();
759 output << std::setw(31) <<
"Total" << std::endl;
762 columnSum.resize(this->n_reactions(),0.);
765 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
767 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species();
768 CoeffType rowTotal(0.);
769 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
771 output << std::setw(31) << std::scientific << std::setprecision(16) << netMatrix[isp][rxn];
772 rowTotal += netMatrix[isp][rxn] * _chem_mixture.M(isp);
773 columnSum[rxn] += netMatrix[isp][rxn] * _chem_mixture.M(isp);
775 output << std::setw(31) << std::scientific << std::setprecision(16) << rowTotal << std::endl;
776 netTotalRow += rowTotal;
778 output << std::setw(10) <<
"Total";
779 for(
unsigned int rxn = 0; rxn < this->n_reactions(); rxn++)
781 output << std::setw(31) << std::scientific << std::setprecision(16) << columnSum[rxn];
782 columnTotal += columnSum[rxn];
784 output << std::endl << std::endl;
786 output << std::endl << std::endl;
787 output <<
"# Mixture informations" << std::endl;
788 output <<
"# species / concentration / molar mass" << std::endl;
789 for(
unsigned int isp = 0; isp < this->n_species(); isp++)
791 output << std::setw(10) << _chem_mixture.chemical_species()[isp]->species()
792 << std::setw(31) << molar_densities[isp]
793 << std::setw(31) << _chem_mixture.M(isp) << std::endl;
799 template<
typename CoeffType>
800 template<
typename StateType,
typename VectorStateType>
803 const VectorStateType& molar_densities,
804 const VectorStateType& h_RT_minus_s_R,
805 VectorStateType& net_rates,
806 VectorStateType& kfwd_const,
807 VectorStateType& kbkwd_const,
808 VectorStateType& kfwd,
809 VectorStateType& kbkwd,
810 VectorStateType& fwd_conc,
811 VectorStateType& bkwd_conc)
const
820 const StateType P0_RT = _P0_R/conditions.
T();
822 net_rates.resize(this->n_reactions(),0);
823 kfwd_const.resize(this->n_reactions(),0);
824 kfwd.resize(this->n_reactions(),0);
825 kbkwd_const.resize(this->n_reactions(),0);
826 kbkwd.resize(this->n_reactions(),0);
827 fwd_conc.resize(this->n_reactions(),1);
828 bkwd_conc.resize(this->n_reactions(),1);
831 for (
unsigned int rxn=0; rxn<this->n_reactions(); rxn++)
835 kfwd[rxn] = kfwd_const[rxn];
837 for (
unsigned int r=0; r<reaction.
n_reactants(); r++)
842 kfwd[rxn] *= fwd_conc[rxn];
848 kbkwd_const[rxn] = kfwd_const[rxn]/keq;
849 kbkwd[rxn] = kbkwd_const[rxn];
850 for (
unsigned int p=0; p<reaction.
n_products(); p++)
852 bkwd_conc[rxn] *=
pow( molar_densities[reaction.
product_id(p)],
855 kbkwd[rxn] *= bkwd_conc[rxn];
858 net_rates[rxn] = kfwd[rxn] - kbkwd[rxn];
866 template<
typename CoeffType>
870 os <<
"# Number of reactions: " << this->n_reactions() <<
"\n";
872 for (
unsigned int r=0; r < this->n_reactions(); r++)
874 os <<
"# " << r <<
'\n'
875 << (this->reaction(r)) <<
"\n";
883 #endif // ANTIOCH_REACTION_SET_H
std::vector< Reaction< CoeffType > * > _reactions
#define antioch_assert(asserted)
CoeffType R_universal()
Universal Gas Constant, R, expressed in J/(mol-K)
StateType equilibrium_constant(const StateType &P0_RT, const VectorStateType &h_RT_minus_s_R) const
#define antioch_warning(message)
void print_chemical_scheme(std::ostream &output, const KineticsConditions< StateType, VectorStateType > &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, std::vector< VectorStateType > &lossMatrix, std::vector< VectorStateType > &prodMatrix, std::vector< VectorStateType > &netMatrix) const
StateType compute_forward_rate_coefficient(const VectorStateType &molar_densities, const KineticsConditions< StateType, VectorStateType > &conditions) const
#define antioch_assert_equal_to(expr1, expr2)
const Reaction< CoeffType > & reaction(const unsigned int r) const
#define antioch_assert_greater(expr1, expr2)
void set_parameter_of_reaction(const std::string &reaction_id, const std::vector< std::string > &keywords, ParamType value)
change a parameter of a reaction
#define antioch_assert_less(expr1, expr2)
unsigned int product_stoichiometric_coefficient(const unsigned int p) const
void remove_reaction(unsigned int nr)
remove a reaction from the system.
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
unsigned int n_species() const
unsigned int reactant_id(const unsigned int r) const
CoeffType product_partial_order(const unsigned int p) const
max(const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a, const _Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &b) ANTIOCH_AUTOFUNC(_Matrix< _Scalar ANTIOCH_COMMA _Rows ANTIOCH_COMMA _Cols ANTIOCH_COMMA _Options ANTIOCH_COMMA _MaxRows ANTIOCH_COMMA _MaxCols >
unsigned int n_reactions() const
void print(std::ostream &os=std::cout) const
Formatted print, by default to std::cout.
void compute_reaction_rates_and_derivs(const KineticsConditions< StateType, VectorStateType > &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, const VectorStateType &dh_RT_minus_s_R_dT, VectorReactionsType &net_reaction_rates, VectorReactionsType &dnet_rate_dT, MatrixReactionsType &dnet_rate_dX_s) const
Compute the rates of progress and derivatives for each reaction.
KineticsModel::Parameters string_to_kin_enum(const std::string &str)
unsigned int reaction_by_id(const std::string &reaction_id) const
const ChemicalMixture< CoeffType > & chemical_mixture() const
CoeffType get_parameter_of_reaction(const std::string &reaction_id, const std::vector< std::string > &keywords) const
void find_chemical_process_parameter(ReactionType::Parameters paramChem, const std::vector< std::string > &keywords, unsigned int &species) const
helper function
unsigned int n_products() const
void set_zero(_Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &a)
const CoeffType _P0_R
Scaling for equilibrium constant.
Class storing chemical mixture properties.
The parameters are reduced parameters.
unsigned int product_id(const unsigned int p) const
unsigned int reactant_stoichiometric_coefficient(const unsigned int r) const
CoeffType reactant_partial_order(const unsigned int r) const
const ChemicalMixture< CoeffType > & _chem_mixture
void add_reaction(Reaction< CoeffType > *reaction)
Add a reaction to the system.
void compute_reaction_rates(const KineticsConditions< StateType, VectorStateType > &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, VectorReactionsType &net_reaction_rates) const
Compute the rates of progress for each reaction.
const StateType & T() const
unsigned int n_reactants() const
void find_kinetics_model_parameter(const unsigned int r, const std::vector< std::string > &keywords, unsigned int &nr, std::string &unit, int &l) const
helper function
This class contains the conditions of the chemistry.
void get_reactive_scheme(const KineticsConditions< StateType, VectorStateType > &conditions, const VectorStateType &molar_densities, const VectorStateType &h_RT_minus_s_R, VectorStateType &net_rates, VectorStateType &kfwd_const, VectorStateType &kbkwd_const, VectorStateType &kfwd, VectorStateType &kbkwd, VectorStateType &fwd_conc, VectorStateType &bkwd_conc) const
ReactionType::Parameters string_to_chem_enum(const std::string &str)