Flow123d  jenkins-Flow123d-linux-release-multijob-282
first_order_reaction.cc
Go to the documentation of this file.
4 
5 #include "system/global_defs.h"
6 #include "mesh/mesh.h"
7 
8 using namespace Input::Type;
9 
11  = Record("FirstOrderReactionReactant", "A record describing a reactant of a reaction.")
12  .allow_auto_conversion("name")
13  .declare_key("name", String(), Default::obligatory(),
14  "The name of the reactant.")
15  //.declare_key("stoichiometric_coefficient", Integer(0.0), Default::optional(1.0)) //in future
16  ;
17 
19  = Record("FirstOrderReactionProduct", "A record describing a product of a reaction.")
20  .allow_auto_conversion("name")
21  .declare_key("name", String(), Default::obligatory(),
22  "The name of the product.")
23  //.declare_key("stoichiometric_coefficient", Integer(0.0), Default::optional(1.0)) //in future
24  .declare_key("branching_ratio", Double(0.0), Default("1.0"),
25  "The branching ratio of the product when there are more products.\n"
26  "The value must be positive. Further, the branching ratios of all products are normalized "
27  "in order to sum to one.\n"
28  "The default value 1.0, should only be used in the case of single product.");
29 
31  = Record("Reaction", "Describes a single first order chemical reaction.")
33  "An array of reactants. Do not use array, reactions with only one reactant (decays) are implemented at the moment!")
34  .declare_key("reaction_rate", Double(0.0), Default::obligatory(),
35  "The reaction rate coefficient of the first order reaction.")
37  "An array of products.")
38  ;
39 
40 
42  = Record("FirstOrderReaction", "A model of first order chemical reactions (decompositions of a reactant into products).")
45  "An array of first order chemical reactions.")
47  "Numerical solver for the system of first order ordinary differential equations coming from the model.");
48 
49 
51  : FirstOrderReactionBase(init_mesh, in_rec)
52 {
53 }
54 
56 {
57 }
58 
60 {
61  // create decay matrix
63  unsigned int reactant_index, product_index; //global indices of the substances
64  double exponent; //temporary variable for k
65  for (unsigned int i_reaction = 0; i_reaction < reaction_rates_.size(); i_reaction++) {
66  reactant_index = substance_ids_[i_reaction][0];
67  exponent = reaction_rates_[i_reaction];
68  reaction_matrix_(reactant_index, reactant_index) = -exponent;
69 
70  for (unsigned int i_product = 1; i_product < substance_ids_[i_reaction].size(); ++i_product){
71  product_index = substance_ids_[i_reaction][i_product];
72  reaction_matrix_(product_index, reactant_index) = exponent * bifurcation_[i_reaction][i_product-1];
73  }
74  }
75 }
76 
77 
79 {
80  unsigned int idx; //temporary variable, indexing substances
81 
82  Input::Array reactions_array = input_record_.val<Input::Array>("reactions");
83 
84  substance_ids_.resize( reactions_array.size() );
85  reaction_rates_.resize( reactions_array.size() );
86  bifurcation_.resize( reactions_array.size() );
87 
88  int i_reaction=0;
89  for (Input::Iterator<Input::Record> react_it = reactions_array.begin<Input::Record>();
90  react_it != reactions_array.end(); ++react_it, ++i_reaction)
91  {
92  //read reaction rate
93  reaction_rates_[i_reaction] = react_it->val<double>("reaction_rate");
94 
95  //read reactant name, product names and branching ratios
96  Input::Array reactant_array = react_it->val<Input::Array>("reactants");
97  Input::Array product_array = react_it->val<Input::Array>("products");
98 
99  //resize substance_ids array
100  substance_ids_[i_reaction].resize( product_array.size()+1 );
101  bifurcation_[i_reaction].resize(product_array.size());
102 
103  if(reactant_array.size() != 1)
104  xprintf(UsrErr, "More than one reactant is not available at the moment.");
105 
106  //take only one reactant
107  Input::Iterator<Input::Record> reactant_it = reactant_array.begin<Input::Record>();
108  {
109  string reactant_name = reactant_it->val<string>("name");
110  idx = find_subst_name(reactant_name);
111  if (idx < substances_.size())
112  substance_ids_[i_reaction][0] = idx;
113  else THROW(ReactionTerm::ExcUnknownSubstance()
114  << ReactionTerm::EI_Substance(reactant_name)
115  << (*reactant_it).ei_address());
116  }
117 
118  // set products
119  unsigned int i_product = 0;
120  for(Input::Iterator<Input::Record> product_it = product_array.begin<Input::Record>();
121  product_it != product_array.end(); ++product_it, i_product++)
122  {
123  string product_name = product_it->val<string>("name");
124  idx = find_subst_name(product_name);
125  if (idx < substances_.size())
126  substance_ids_[i_reaction][i_product+1] = idx;
127  else THROW(ReactionTerm::ExcUnknownSubstance()
128  << ReactionTerm::EI_Substance(product_name)
129  << product_array.ei_address());
130 
131  bifurcation_[i_reaction][i_product] = product_it->val<double>("branching_ratio");
132  }
133 
134 
135  //Normalization of branching ratios in bifurcation vector by its norm.
136  //sum:
137  double sum=0;
138  for(auto &b : bifurcation_[i_reaction])
139  sum += b;
140  //Normalization:
141  for(auto &b : bifurcation_[i_reaction])
142  b = b / sum;
143  }
144 }
Iterator< ValueType > begin() const
Accessor to input data conforming to declared Array.
Definition: accessors.hh:558
static Input::Type::Record input_type_reactant
Input record for a reactant of a reaction.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:39
static Default obligatory()
Definition: type_record.hh:89
???
SubstanceList substances_
Names belonging to substances.
static Input::Type::AbstractRecord input_type
Definition: mesh.h:109
Record & derive_from(AbstractRecord &parent)
Definition: type_record.cc:197
unsigned int find_subst_name(const std::string &name)
Base class for linear reactions and decay chain.
static Input::Type::Record input_type
Input record for class FirstOrderReaction.
unsigned int size() const
Definition: substance.hh:99
Class for declaration of inputs sequences.
Definition: type_base.hh:239
~FirstOrderReaction(void)
Destructor.
IteratorBase end() const
static Input::Type::AbstractRecord input_type
static Input::Type::Record input_type_single_reaction
Input record which defines particular reaction.
static Default optional()
Definition: type_record.hh:102
Global macros to enhance readability and debugging, general constants.
Record & allow_auto_conversion(const string &from_key)
Definition: type_record.cc:111
arma::mat reaction_matrix_
Reaction matrix.
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:392
static Input::Type::Record input_type_product
Input record for a product of a reaction.
Accessor to the data with type Type::Record.
Definition: accessors.hh:327
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:100
void initialize_from_input() override
Initializes private members of sorption from the input record.
Definition: system.hh:72
Input::Record input_record_
Definition: equation.hh:220
FirstOrderReaction(Mesh &init_mesh, Input::Record in_rec)
Constructor.
std::vector< std::vector< unsigned int > > substance_ids_
unsigned int size() const
Record type proxy class.
Definition: type_record.hh:169
unsigned int n_substances_
Number of all transported substances. It is the dimension of the reaction matrix. ...
std::vector< double > reaction_rates_
Vector of reaction rates of the transported substances.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:34
void assemble_ode_matrix(void) override
Implements the assembly of the system matrix of the ODEs.
std::vector< std::vector< double > > bifurcation_
Record & declare_key(const string &key, const KeyType &type, const Default &default_value, const string &description)
Definition: type_record.cc:430