Flow123d  release_1.8.2-1603-g0109a2b
first_order_reaction_base.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file first_order_reaction_base.cc
15  * @brief
16  */
17 
20 
24 
25 
26 #include "system/global_defs.h"
27 #include "system/sys_profiler.hh"
28 
29 #include "mesh/mesh.h"
30 #include "la/distribution.hh"
31 #include "input/accessors.hh"
32 
33 FLOW123D_FORCE_LINK_IN_PARENT(padeApproximant);
34 FLOW123D_FORCE_LINK_IN_PARENT(linearODEAnalytic);
35 
36 
37 using namespace Input::Type;
38 
39 
41  : ReactionTerm(init_mesh, in_rec)
42 {
44  if ( num_it )
45  {
46  linear_ode_solver_ = (*num_it).factory< LinearODESolverBase, Input::Record >(*num_it);
47  }
48  else //default linear ode solver
49  {
51  }
52 }
53 
55 {
56 }
57 
59 {
60  OLD_ASSERT(distribution_ != nullptr, "Distribution has not been set yet.\n");
61  OLD_ASSERT(time_ != nullptr, "Time governor has not been set yet.\n");
63 
66 
67  // allocation
68  prev_conc_.resize(n_substances_);
69  reaction_matrix_.resize(n_substances_, n_substances_);
70  molar_matrix_.resize(n_substances_, n_substances_);
71  molar_mat_inverse_.resize(n_substances_, n_substances_);
72 
73  // initialize diagonal matrices with molar masses
74  molar_matrix_.zeros();
75  molar_mat_inverse_.zeros();
76  for (unsigned int i=0; i<n_substances_; ++i)
77  {
78  molar_matrix_(i,i) = substances_[i].molar_mass();
79  molar_mat_inverse_(i,i) = 1./substances_[i].molar_mass();
80  }
81 }
82 
83 
85 {
86  OLD_ASSERT(distribution_ != nullptr, "Distribution has not been set yet.\n");
87  OLD_ASSERT(time_ != nullptr, "Time governor has not been set yet.\n");
89 
91  // make scaling that takes into account different molar masses of substances
93  linear_ode_solver_->set_system_matrix(reaction_matrix_);
94 }
95 
96 
97 double **FirstOrderReactionBase::compute_reaction(double **concentrations, int loc_el) //multiplication of concentrations array by reaction matrix
98 {
99  unsigned int rows; // row in the concentration matrix, regards the substance index
100  arma::vec new_conc;
101 
102  // save previous concentrations to column vector
103  for(rows = 0; rows < n_substances_; rows++)
104  prev_conc_(rows) = concentrations[rows][loc_el];
105 
106  // compute new concetrations R*c
107  linear_ode_solver_->update_solution(prev_conc_, new_conc);
108 
109  // save new concentrations to the concentration matrix
110  for(rows = 0; rows < n_substances_; rows++)
111  concentrations[rows][loc_el] = new_conc(rows);
112 
113  return concentrations;
114 }
115 
117 {
118  //DBGMSG("FirstOrderReactionBases - update solution\n");
119  if(time_->is_changed_dt())
120  {
121  linear_ode_solver_->set_step(time_->dt());
122  }
123 
124  START_TIMER("linear reaction step");
125 
126  for (unsigned int loc_el = 0; loc_el < distribution_->lsize(); loc_el++)
128 
129  END_TIMER("linear reaction step");
130 }
131 
132 
133 unsigned int FirstOrderReactionBase::find_subst_name(const string &name)
134 {
135  unsigned int k=0;
136  for(; k < n_substances_; k++)
137  if (name == substances_[k].name()) return k;
138 
139  return k;
140 }
virtual void initialize_from_input()=0
Initializes private members of sorption from the input record.
arma::mat molar_matrix_
Diagonal matrix with molar masses of substances.
double ** concentration_matrix_
FLOW123D_FORCE_LINK_IN_PARENT(padeApproximant)
void initialize() override
Prepares the object to usage.
void zero_time_step() override
Moves the model to zero time.
SubstanceList substances_
Names belonging to substances.
Definition: mesh.h:99
Iterator< Ret > find(const string &key) const
unsigned int find_subst_name(const std::string &name)
shared_ptr< Type > const create(string name, Arguments...arguments) const
create an instance of a registered class
Definition: factory_impl.hh:49
virtual void assemble_ode_matrix(void)=0
Assembles the matrix of the ODEs.
unsigned int size() const
Definition: substance.hh:87
Class ReactionTerm is an abstract class representing reaction term in transport.
#define OLD_ASSERT(...)
Definition: global_defs.h:128
static Factory * instance()
Get the single instance of the factory.
Definition: factory_impl.hh:27
void update_solution(void) override
Updates the solution.
Global macros to enhance readability and debugging, general constants.
arma::mat reaction_matrix_
Reaction matrix.
Base class for linear ODE solver.
Distribution * distribution_
Pointer to reference to distribution of elements between processors.
FirstOrderReactionBase(Mesh &init_mesh, Input::Record in_rec)
Constructor.
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
bool is_changed_dt() const
#define START_TIMER(tag)
Starts a timer with specified tag.
#define OLD_ASSERT_LESS(a, b)
Definition: global_defs.h:131
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:444
Support classes for parallel programing.
arma::vec prev_conc_
Column vector storing previous concetrations on an element.
Input::Record input_record_
Definition: equation.hh:223
double dt() const
arma::mat molar_mat_inverse_
Inverse of molar_matrix_.
#define END_TIMER(tag)
Ends a timer with specified tag.
std::shared_ptr< LinearODESolverBase > linear_ode_solver_
unsigned int n_substances_
Number of all transported substances. It is the dimension of the reaction matrix. ...
virtual double ** compute_reaction(double **concentrations, int loc_el) override
Computes the reaction on a specified element.
~FirstOrderReactionBase(void)
Destructor.
TimeGovernor * time_
Definition: equation.hh:222
unsigned int lsize(int proc) const
get local size