Flow123d  JS_before_hm-981-g3f34df1
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 
22 
23 
24 #include "system/global_defs.h"
25 #include "system/sys_profiler.hh"
26 
27 #include "mesh/mesh.h"
28 #include "la/distribution.hh"
29 #include "input/accessors.hh"
30 
31 
32 
33 using namespace Input::Type;
34 
35 
37  : ReactionTerm(init_mesh, in_rec)
38 {
39  linear_ode_solver_ = std::make_shared<LinearODESolver>();
40 }
41 
43 {
44 }
45 
47 {
48  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
49  ASSERT_LT(0, substances_.size()).error("No substances for rection term.\n");
50 
53 
54  // allocation
55  prev_conc_.resize(n_substances_);
59 
60  // initialize diagonal matrices with molar masses
61  molar_matrix_.zeros();
62  molar_mat_inverse_.zeros();
63  for (unsigned int i=0; i<n_substances_; ++i)
64  {
65  molar_matrix_(i,i) = substances_[i].molar_mass();
66  molar_mat_inverse_(i,i) = 1./substances_[i].molar_mass();
67  }
68 }
69 
70 
72 {
73  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
74  ASSERT_LT(0, substances_.size()).error("No substances for rection term.\n");
75 
77  // make scaling that takes into account different molar masses of substances
79 
80  linear_ode_solver_->set_system_matrix(reaction_matrix_);
81 }
82 
83 
85 {
86  unsigned int sbi; // row in the concentration matrix, regards the substance index
87  arma::vec new_conc;
88 
89  IntIdx dof_p0 = dh_cell.get_loc_dof_indices()[0];
90 
91  // save previous concentrations to column vector
92  for(sbi = 0; sbi < n_substances_; sbi++)
93  prev_conc_(sbi) = conc_mobile_fe[sbi]->vec()[dof_p0];
94 
95  // compute new concetrations R*c
96  linear_ode_solver_->update_solution(prev_conc_, new_conc);
97 
98  // save new concentrations to the concentration matrix
99  for(sbi = 0; sbi < n_substances_; sbi++)
100  conc_mobile_fe[sbi]->vec()[dof_p0] = new_conc(sbi);
101 }
102 
104 {
105  //DebugOut() << "FirstOrderReactionBases - update solution\n";
106  if(time_->is_changed_dt())
107  {
108  linear_ode_solver_->set_step(time_->dt());
109  }
110 
111  START_TIMER("linear reaction step");
112 
113  for ( DHCellAccessor dh_cell : dof_handler_->own_range() )
114  {
115  compute_reaction(dh_cell);
116  }
117  END_TIMER("linear reaction step");
118 }
119 
120 
121 unsigned int FirstOrderReactionBase::find_subst_name(const string &name)
122 {
123  unsigned int k=0;
124  for(; k < n_substances_; k++)
125  if (name == substances_[k].name()) return k;
126 
127  return k;
128 }
129 
130 
132 {
133  if (!linear_ode_solver_->evaluate_time_constraint(time_constraint)) return false;
134 
135  DebugOut().fmt("CFL constraint(first order reaction): {}.\n", time_constraint);
136 
137  return true;
138 }
virtual void initialize_from_input()=0
Initializes private members of sorption from the input record.
ArmaVec< double, N > vec
Definition: armor.hh:861
arma::mat molar_matrix_
Diagonal matrix with molar masses of substances.
int IntIdx
Definition: index_types.hh:25
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:78
Cell accessor allow iterate over DOF handler cells.
unsigned int find_subst_name(const std::string &name)
LocDofVec get_loc_dof_indices() const
Returns the local indices of dofs associated to the cell on the local process.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
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.
void update_solution(void) override
Updates the solution.
Global macros to enhance readability and debugging, general constants.
arma::mat reaction_matrix_
Reaction matrix.
FirstOrderReactionBase(Mesh &init_mesh, Input::Record in_rec)
Constructor.
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
bool is_changed_dt() const
#define START_TIMER(tag)
Starts a timer with specified tag.
virtual void compute_reaction(const DHCellAccessor &dh_cell) override
Computes the reaction on a specified element.
Support classes for parallel programing.
std::shared_ptr< LinearODESolver > linear_ode_solver_
arma::vec prev_conc_
Column vector storing previous concetrations on an element.
double dt() const
arma::mat molar_mat_inverse_
Inverse of molar_matrix_.
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
FieldFEScalarVec conc_mobile_fe
FieldFEs representing P0 interpolation of mobile concentration (passed from transport).
std::shared_ptr< DOFHandlerMultiDim > dof_handler_
Pointer to DOF handler used through the reaction tree.
#define END_TIMER(tag)
Ends a timer with specified tag.
unsigned int n_substances_
Number of all transported substances. It is the dimension of the reaction matrix. ...
#define DebugOut()
Macro defining &#39;debug&#39; record of log.
Definition: logger.hh:264
~FirstOrderReactionBase(void)
Destructor.
bool evaluate_time_constraint(double &time_constraint) override
Computes a constraint for time step.
TimeGovernor * time_
Definition: equation.hh:219