Flow123d  JS_before_hm-2115-gf629a871a
dual_porosity.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 dual_porosity.cc
15  * @brief
16  */
17 
18 #include <iostream>
19 #include <stdlib.h>
20 #include <math.h>
21 
24 #include "system/system.hh"
25 #include "system/sys_profiler.hh"
26 
27 #include "la/distribution.hh"
28 #include "mesh/mesh.h"
29 #include "mesh/elements.h"
30 #include "mesh/region.hh"
31 #include "mesh/accessors.hh"
32 #include "fields/field_fe.hh"
34 
35 #include "reaction/sorption.hh"
38 #include "input/factory.hh"
39 
40 FLOW123D_FORCE_LINK_IN_CHILD(dualPorosity)
41 
42 
43 using namespace Input::Type;
44 
45 
46 
48  return Record("DualPorosity",
49  "Dual porosity model in transport problems.\n"
50  "Provides computing the concentration of substances in mobile and immobile zone.\n"
51  )
53  .declare_key("input_fields", Array(DualPorosity::EqFields().make_field_descriptor_type("DualPorosity")), Default::obligatory(),
54  "Containes region specific data necessary to construct dual porosity model.")
55  .declare_key("scheme_tolerance", Double(0.0), Default("1e-3"),
56  "Tolerance according to which the explicit Euler scheme is used or not."
57  "Set 0.0 to use analytic formula only (can be slower).")
58 
59  .declare_key("reaction_mobile", ReactionTerm::it_abstract_mobile_term(), Default::optional(), "Reaction model in mobile zone.")
60  .declare_key("reaction_immobile", ReactionTerm::it_abstract_immobile_term(), Default::optional(), "Reaction model in immobile zone.")
61  .declare_key("output",
62  EqFields().output_fields.make_output_type("DualPorosity", ""),
63  IT::Default("{ \"fields\": [ \"conc_immobile\" ] }"),
64  "Setting of the fields output.")
65  .close();
66 }
67 
68 const int DualPorosity::registrar =
69  Input::register_class< DualPorosity, Mesh &, Input::Record >("DualPorosity") +
71 
74 {
76  .name("diffusion_rate_immobile")
77  .description("Diffusion coefficient of non-equilibrium linear exchange between mobile and immobile zone.")
78  .input_default("0")
79  .units( UnitSI().s(-1) );
80 
81  *this += porosity_immobile
82  .name("porosity_immobile")
83  .description("Porosity of the immobile zone.")
84  .input_default("0")
86  .set_limits(0.0);
87 
88  *this += init_conc_immobile
89  .name("init_conc_immobile")
90  .description("Initial concentration of substances in the immobile zone.")
91  .units( UnitSI().kg().m(-3) );
92 
93  //creating field for porosity that is set later from the governing equation (transport)
94  *this +=porosity
95  .name("porosity")
96  .description("Concentration solution in the mobile phase.")
99  .set_limits(0.0);
100 
101  *this += conc_immobile
102  .name("conc_immobile")
103  .units( UnitSI().kg().m(-3) )
105 
106  output_fields += *this;
107 
108 }
109 
111 {}
112 
114  : ReactionTerm(init_mesh, in_rec)
115 {
116  eq_fields_ = std::make_shared<EqFields>();
117  eq_fields_->add_coords_field();
118  //set pointer to equation data fieldset
119  this->eq_fieldset_ = eq_fields_.get();
120  this->eq_fields_base_ = std::static_pointer_cast<ReactionTerm::EqFields>(eq_fields_);
121 
122  //reads input and creates possibly other reaction terms
123  make_reactions();
124  //read value from input
125  scheme_tolerance_ = input_record_.val<double>("scheme_tolerance");
126 }
127 
129 {
130 }
131 
132 
135  if ( reactions_it )
136  {
137  // TODO: allowed instances in this case are only
138  // FirstOrderReaction, RadioactiveDecay and SorptionMob
139  reaction_mobile = (*reactions_it).factory< ReactionTerm, Mesh &, Input::Record >(*mesh_, *reactions_it);
140  } else
141  {
142  reaction_mobile = nullptr;
143  }
144 
145  reactions_it = input_record_.find<Input::AbstractRecord>("reaction_immobile");
146  if ( reactions_it )
147  {
148  // TODO: allowed instances in this case are only
149  // FirstOrderReaction, RadioactiveDecay and SorptionImmob
150  reaction_immobile = (*reactions_it).factory< ReactionTerm, Mesh &, Input::Record >(*mesh_, *reactions_it);
151  } else
152  {
153  reaction_immobile = nullptr;
154  }
155 
156 }
157 
159 {
160  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
161  ASSERT_LT(0, eq_data_->substances_.size()).error("No substances for rection term.\n");
162  ASSERT(output_stream_ != nullptr).error("Null output stream.\n");
163 
165 
166  if(reaction_mobile)
167  {
168  reaction_mobile->substances(eq_data_->substances_)
169  .output_stream(output_stream_)
170  .concentration_fields(eq_fields_->conc_mobile_fe)
171  .set_time_governor(*time_);
172  reaction_mobile->initialize();
173  }
174 
176  {
177  reaction_immobile->substances(eq_data_->substances_)
178  .output_stream(output_stream_)
179  .concentration_fields(eq_fields_->conc_immobile_fe)
180  .set_time_governor(*time_);
181  reaction_immobile->initialize();
182  }
183 
184 }
185 
187 {
188  eq_fields_->set_components(eq_data_->substances_.names());
189  //setting fields that are set from input file
192 
193  //setting fields in data
194  eq_fields_->set_mesh(*mesh_);
195 
196  //initialization of output
197  eq_fields_->output_fields.set_components(eq_data_->substances_.names());
198  eq_fields_->output_fields.set_mesh(*mesh_);
199  eq_fields_->output_fields.output_type(OutputTime::ELEM_DATA);
200  eq_fields_->conc_immobile.setup_components();
201 
202 
203  //creating field fe and output multifield for sorbed concentrations
204  eq_fields_->conc_immobile_fe.resize(eq_data_->substances_.size());
205  for (unsigned int sbi = 0; sbi < eq_data_->substances_.size(); sbi++)
206  {
207  // create shared pointer to a FieldFE and push this Field to output_field on all regions
208  eq_fields_->conc_immobile_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(eq_data_->dof_handler_);
209  eq_fields_->conc_immobile[sbi].set(eq_fields_->conc_immobile_fe[sbi], 0);
210  }
211 
212  eq_fields_->output_fields.initialize(output_stream_, mesh_, input_record_.val<Input::Record>("output"),time());
213 }
214 
215 
217 {
218  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
219  ASSERT_LT(0, eq_data_->substances_.size()).error("No substances for rection term.\n");
220  ASSERT(output_stream_ != nullptr).error("Null output stream.\n");
221 
222  //coupling - passing fields
223  if(reaction_mobile)
224  if (typeid(*reaction_mobile) == typeid(SorptionMob))
225  {
226  reaction_mobile->eq_fieldset().set_field("porosity", (*eq_fields_)["porosity"]);
227  reaction_mobile->eq_fieldset().set_field("porosity_immobile", (*eq_fields_)["porosity_immobile"]);
228  }
230  if (typeid(*reaction_immobile) == typeid(SorptionImmob))
231  {
232  reaction_immobile->eq_fieldset().set_field("porosity", (*eq_fields_)["porosity"]);
233  reaction_immobile->eq_fieldset().set_field("porosity_immobile", (*eq_fields_)["porosity_immobile"]);
234  }
235 
236  eq_fields_->set_time(time_->step(0), LimitSide::right);
237  std::stringstream ss; // print warning message with table of uninitialized fields
238  if ( FieldCommon::print_message_table(ss, "dual porosity") ) {
239  WarningOut() << ss.str();
240  }
242 
243  output_data();
244 
245  if(reaction_mobile)
246  reaction_mobile->zero_time_step();
247 
249  reaction_immobile->zero_time_step();
250 
251 }
252 
254 {
255  for ( DHCellAccessor dh_cell : eq_data_->dof_handler_->own_range() ) {
256  IntIdx dof_p0 = dh_cell.get_loc_dof_indices()[0];
257  const ElementAccessor<3> ele = dh_cell.elm();
258 
259  //setting initial solid concentration for substances involved in adsorption
260  for (unsigned int sbi = 0; sbi < eq_data_->substances_.size(); sbi++)
261  {
262  eq_fields_->conc_immobile_fe[sbi]->vec().set( dof_p0, eq_fields_->init_conc_immobile[sbi].value(ele.centre(), ele) );
263  }
264  }
265 }
266 
268 {
269  eq_fields_->set_time(time_->step(-2), LimitSide::right);
270 
271  START_TIMER("dual_por_exchange_step");
272  for ( DHCellAccessor dh_cell : eq_data_->dof_handler_->own_range() )
273  {
274  compute_reaction(dh_cell);
275  }
276  END_TIMER("dual_por_exchange_step");
277 
278  if(reaction_mobile) reaction_mobile->update_solution();
279  if(reaction_immobile) reaction_immobile->update_solution();
280 }
281 
282 
284 {
285  unsigned int sbi;
286  double conc_average, // weighted (by porosity) average of concentration
287  conc_mob, conc_immob, // new mobile and immobile concentration
288  previous_conc_mob, previous_conc_immob, // mobile and immobile concentration in previous time step
289  conc_max, //difference between concentration and average concentration
290  por_mob, por_immob; // mobile and immobile porosity
291 
292  // get data from fields
293  ElementAccessor<3> ele = dh_cell.elm();
294  IntIdx dof_p0 = dh_cell.get_loc_dof_indices()[0];
295  por_mob = eq_fields_->porosity.value(ele.centre(),ele);
296  por_immob = eq_fields_->porosity_immobile.value(ele.centre(),ele);
297  arma::Col<double> diff_vec(eq_data_->substances_.size());
298  for (sbi=0; sbi<eq_data_->substances_.size(); sbi++) // Optimize: SWAP LOOPS
299  diff_vec[sbi] = eq_fields_->diffusion_rate_immobile[sbi].value(ele.centre(), ele);
300 
301  // if porosity_immobile == 0 then mobile concentration stays the same
302  // and immobile concentration cannot change
303  if (por_immob == 0.0) return;
304 
305  double exponent,
306  temp_exponent = (por_mob + por_immob) / (por_mob * por_immob) * time_->dt();
307 
308  for (sbi = 0; sbi < eq_data_->substances_.size(); sbi++) //over all substances
309  {
310  exponent = diff_vec[sbi] * temp_exponent;
311  //previous values
312  previous_conc_mob = eq_fields_->conc_mobile_fe[sbi]->vec().get(dof_p0);
313  previous_conc_immob = eq_fields_->conc_immobile_fe[sbi]->vec().get(dof_p0);
314 
315  // ---compute average concentration------------------------------------------
316  conc_average = ((por_mob * previous_conc_mob) + (por_immob * previous_conc_immob))
317  / (por_mob + por_immob);
318 
319  conc_max = std::max(previous_conc_mob-conc_average, previous_conc_immob-conc_average);
320 
321  // the following 2 conditions guarantee:
322  // 1) stability of forward Euler's method
323  // 2) the error of forward Euler's method will not be large
324  if(time_->dt() <= por_mob*por_immob/(max(diff_vec)*(por_mob+por_immob)) &&
325  conc_max <= (2*scheme_tolerance_/(exponent*exponent)*conc_average)) // forward euler
326  {
327  double temp = diff_vec[sbi]*(previous_conc_immob - previous_conc_mob) * time_->dt();
328  // ---compute concentration in mobile area
329  conc_mob = temp / por_mob + previous_conc_mob;
330 
331  // ---compute concentration in immobile area
332  conc_immob = -temp / por_immob + previous_conc_immob;
333  }
334  else //analytic solution
335  {
336  double temp = exp(-exponent);
337  // ---compute concentration in mobile area
338  conc_mob = (previous_conc_mob - conc_average) * temp + conc_average;
339 
340  // ---compute concentration in immobile area
341  conc_immob = (previous_conc_immob - conc_average) * temp + conc_average;
342  }
343 
344  eq_fields_->conc_mobile_fe[sbi]->vec().set(dof_p0, conc_mob);
345  eq_fields_->conc_immobile_fe[sbi]->vec().set(dof_p0, conc_immob);
346  }
347 }
348 
349 
351 {
352  eq_fields_->output_fields.set_time(time_->step(), LimitSide::right);
353 
354  // Register fresh output data
355  eq_fields_->output_fields.output(time_->step());
356 
357  if (time_->tlevel() !=0) {
358  // zero_time_step call zero_time_Step of subreactions which performs its own output
359  if (reaction_mobile) reaction_mobile->output_data();
360  if (reaction_immobile) reaction_immobile->output_data();
361  }
362 }
DualPorosity::EqFields::EqFields
EqFields()
Collect all fields.
Definition: dual_porosity.cc:72
FieldCommon::units
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
Definition: field_common.hh:152
LimitSide::right
@ right
ReactionTerm::output_stream_
std::shared_ptr< OutputTime > output_stream_
Pointer to a transport output stream.
Definition: reaction_term.hh:146
EquationBase::mesh_
Mesh * mesh_
Definition: equation.hh:220
DualPorosity::registrar
static const int registrar
Registrar of class to factory.
Definition: dual_porosity.hh:144
DualPorosity::EqFields::diffusion_rate_immobile
MultiField< 3, FieldValue< 3 >::Scalar > diffusion_rate_immobile
Mass transfer coefficients between mobile and immobile pores.
Definition: dual_porosity.hh:68
DualPorosity::initialize
void initialize() override
Prepares the object to usage.
Definition: dual_porosity.cc:158
ReactionTerm::EqData::EqData
EqData()
Constructor.
Definition: dual_porosity.cc:110
TimeGovernor::dt
double dt() const
Definition: time_governor.hh:558
UnitSI::dimensionless
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
ReactionTerm::it_abstract_immobile_term
static Input::Type::Abstract & it_abstract_immobile_term()
Definition: reaction_term.cc:37
factory.hh
ReactionTerm::ReactionTerm
ReactionTerm(Mesh &init_mesh, Input::Record in_rec)
Definition: reaction_term.cc:51
DualPorosity::scheme_tolerance_
double scheme_tolerance_
Dual porosity computational scheme tolerance.
Definition: dual_porosity.hh:140
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
DualPorosity::eq_fields_
std::shared_ptr< EqFields > eq_fields_
Equation fields - all fields are in this set.
Definition: dual_porosity.hh:126
EquationBase::time_
TimeGovernor * time_
Definition: equation.hh:221
distribution.hh
Support classes for parallel programing.
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
IntIdx
int IntIdx
Definition: index_types.hh:25
EquationBase::time
TimeGovernor & time()
Definition: equation.hh:149
FieldSet::set_input_list
void set_input_list(Input::Array input_list, const TimeGovernor &tg)
Definition: field_set.hh:282
Input::Type::Double
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
DualPorosity::compute_reaction
void compute_reaction(const DHCellAccessor &dh_cell) override
Compute reaction on a single element.
Definition: dual_porosity.cc:283
DualPorosity::update_solution
void update_solution(void) override
Definition: dual_porosity.cc:267
FLOW123D_FORCE_LINK_IN_CHILD
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:157
DualPorosity::DualPorosity
DualPorosity()
ElementAccessor< 3 >
DualPorosity::EqFields::init_conc_immobile
MultiField< 3, FieldValue< 3 >::Scalar > init_conc_immobile
Initial concentrations in the immobile zone.
Definition: dual_porosity.hh:71
system.hh
OutputTime::ELEM_DATA
@ ELEM_DATA
Definition: output_time.hh:111
FieldCommon::set_limits
FieldCommon & set_limits(double min, double max=std::numeric_limits< double >::max())
Definition: field_common.hh:158
FieldCommon::flags
FieldCommon & flags(FieldFlag::Flags::Mask mask)
Definition: field_common.hh:191
field_fe.hh
DualPorosity::make_reactions
void make_reactions()
Resolves construction of following reactions.
Definition: dual_porosity.cc:133
ASSERT_LT
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than)
Definition: asserts.hh:296
Input::Iterator
Definition: accessors.hh:143
SorptionImmob
Sorption model in immobile zone in case dual porosity is considered.
Definition: sorption.hh:139
Input::Type::Record::size
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:602
Input::Type::Default
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
Input::Type::Record::derive_from
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
SorptionMob
Sorption model in mobile zone in case dual porosity is considered.
Definition: sorption.hh:113
accessors.hh
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
DualPorosity::input_field_set_
FieldSet input_field_set_
Definition: dual_porosity.hh:131
FieldFlag::equation_result
static constexpr Mask equation_result
Match result fields. These are never given by input or copy of input.
Definition: field_flag.hh:55
elements.h
sys_profiler.hh
TimeGovernor::tlevel
int tlevel() const
Definition: time_governor.hh:600
TimeGovernor::step
const TimeStep & step(int index=-1) const
Definition: time_governor.cc:753
DualPorosity::reaction_mobile
std::shared_ptr< ReactionTerm > reaction_mobile
Reaction running in mobile zone.
Definition: dual_porosity.hh:133
sorption.hh
This file contains classes representing sorption model. Sorption model can be computed both in case t...
DHCellAccessor::elm
const ElementAccessor< 3 > elm() const
Return ElementAccessor to element of loc_ele_idx_.
Definition: dh_cell_accessor.hh:71
Input::AbstractRecord
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
Input::Type::Default::obligatory
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
DualPorosity::EqFields::porosity_immobile
Field< 3, FieldValue< 3 >::Scalar > porosity_immobile
Immobile porosity field.
Definition: dual_porosity.hh:69
FieldCommon::print_message_table
static bool print_message_table(ostream &stream, std::string equation_name)
Definition: field_common.cc:96
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
EquationBase::input_record_
Input::Record input_record_
Definition: equation.hh:222
DualPorosity::~DualPorosity
~DualPorosity(void)
Destructor.
Definition: dual_porosity.cc:128
first_order_reaction.hh
Input::Type::Record::declare_key
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:503
mesh.h
DualPorosity::EqFields::porosity
Field< 3, FieldValue< 3 >::Scalar > porosity
Porosity field.
Definition: dual_porosity.hh:73
DualPorosity::set_initial_condition
void set_initial_condition()
Sets initial condition from input.
Definition: dual_porosity.cc:253
Input::Record::find
Iterator< Ret > find(const string &key) const
Definition: accessors_impl.hh:91
FieldFlag::input_copy
static constexpr Mask input_copy
Definition: field_flag.hh:44
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Input::Type
Definition: balance.hh:41
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
DualPorosity::reaction_immobile
std::shared_ptr< ReactionTerm > reaction_immobile
Reaction running in immobile zone.
Definition: dual_porosity.hh:134
FieldCommon::input_default
FieldCommon & input_default(const string &input_default)
Definition: field_common.hh:139
DualPorosity::output_data
void output_data(void) override
Main output routine.
Definition: dual_porosity.cc:350
Mesh
Definition: mesh.h:355
DualPorosity::EqFields
DualPorosity fields.
Definition: dual_porosity.hh:61
Input::Type::Array
Class for declaration of inputs sequences.
Definition: type_base.hh:339
dual_porosity.hh
Class Dual_por_exchange implements the model of dual porosity.
DualPorosity::zero_time_step
void zero_time_step() override
Definition: dual_porosity.cc:216
DHCellAccessor
Cell accessor allow iterate over DOF handler cells.
Definition: dh_cell_accessor.hh:43
DualPorosity::get_input_type
static const Input::Type::Record & get_input_type()
Definition: dual_porosity.cc:47
Input::Array
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
ReactionTerm::it_abstract_term
static Input::Type::Abstract & it_abstract_term()
Definition: reaction_term.cc:25
fe_value_handler.hh
ReactionTerm::eq_fields_base_
std::shared_ptr< EqFields > eq_fields_base_
Equation data - all fields needs in assembly class.
Definition: reaction_term.hh:149
DHCellAccessor::get_loc_dof_indices
LocDofVec get_loc_dof_indices() const
Returns the local indices of dofs associated to the cell on the local process.
Definition: dh_cell_accessor.hh:88
region.hh
DualPorosity::EqFields::output_fields
EquationOutput output_fields
Fields indended for output, i.e. all input fields plus those representing solution.
Definition: dual_porosity.hh:79
EquationBase::eq_fieldset_
FieldSet * eq_fieldset_
Definition: equation.hh:229
DualPorosity::initialize_fields
void initialize_fields()
Initializes field sets.
Definition: dual_porosity.cc:186
FieldCommon::description
FieldCommon & description(const string &description)
Definition: field_common.hh:127
reaction_term.hh
Class ReactionTerm is an abstract class representing reaction term in transport.
ReactionTerm::it_abstract_mobile_term
static Input::Type::Abstract & it_abstract_mobile_term()
Definition: reaction_term.cc:31
ReactionTerm
Definition: reaction_term.hh:46
radioactive_decay.hh
Input::Type::Default::optional
static Default optional()
The factory function to make an empty default value which is optional.
Definition: type_record.hh:124
START_TIMER
#define START_TIMER(tag)
Starts a timer with specified tag.
Definition: sys_profiler.hh:115
ReactionTerm::eq_data_
std::shared_ptr< EqData > eq_data_
Equation data - all data needs in assembly class.
Definition: reaction_term.hh:152
END_TIMER
#define END_TIMER(tag)
Ends a timer with specified tag.
Definition: sys_profiler.hh:149
DualPorosity::EqFields::conc_immobile
MultiField< 3, FieldValue< 3 >::Scalar > conc_immobile
Calculated concentrations in the immobile zone.
Definition: dual_porosity.hh:75
FieldCommon::name
FieldCommon & name(const string &name)
Definition: field_common.hh:120
ElementAccessor::centre
arma::vec::fixed< spacedim > centre() const
Computes the barycenter.
Definition: accessors_impl.hh:89