Flow123d  JS_before_hm-1602-g5680f2c
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 
47 const Record & DualPorosity::get_input_type() {
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::EqData().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  EqData().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 
73 {
75  .name("diffusion_rate_immobile")
76  .description("Diffusion coefficient of non-equilibrium linear exchange between mobile and immobile zone.")
77  .input_default("0")
78  .units( UnitSI().s(-1) );
79 
80  *this += porosity_immobile
81  .name("porosity_immobile")
82  .description("Porosity of the immobile zone.")
83  .input_default("0")
85  .set_limits(0.0);
86 
87  *this += init_conc_immobile
88  .name("init_conc_immobile")
89  .description("Initial concentration of substances in the immobile zone.")
90  .units( UnitSI().kg().m(-3) );
91 
92  //creating field for porosity that is set later from the governing equation (transport)
93  *this +=porosity
94  .name("porosity")
95  .description("Concentration solution in the mobile phase.")
98  .set_limits(0.0);
99 
100  *this += conc_immobile
101  .name("conc_immobile")
102  .units( UnitSI().kg().m(-3) )
104 
105  output_fields += *this;
106 
107 }
108 
110  : ReactionTerm(init_mesh, in_rec)
111 {
112  //set pointer to equation data fieldset
113  this->eq_fieldset_ = &data_;
114 
115  //reads input and creates possibly other reaction terms
116  make_reactions();
117  //read value from input
118  scheme_tolerance_ = input_record_.val<double>("scheme_tolerance");
119 }
120 
122 {
123 }
124 
125 
128  if ( reactions_it )
129  {
130  // TODO: allowed instances in this case are only
131  // FirstOrderReaction, RadioactiveDecay and SorptionMob
132  reaction_mobile = (*reactions_it).factory< ReactionTerm, Mesh &, Input::Record >(*mesh_, *reactions_it);
133  } else
134  {
135  reaction_mobile = nullptr;
136  }
137 
138  reactions_it = input_record_.find<Input::AbstractRecord>("reaction_immobile");
139  if ( reactions_it )
140  {
141  // TODO: allowed instances in this case are only
142  // FirstOrderReaction, RadioactiveDecay and SorptionImmob
143  reaction_immobile = (*reactions_it).factory< ReactionTerm, Mesh &, Input::Record >(*mesh_, *reactions_it);
144  } else
145  {
146  reaction_immobile = nullptr;
147  }
148 
149 }
150 
152 {
153  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
154  ASSERT_LT(0, substances_.size()).error("No substances for rection term.\n");
155  ASSERT(output_stream_ != nullptr).error("Null output stream.\n");
156 
158 
159  if(reaction_mobile)
160  {
161  reaction_mobile->substances(substances_)
162  .output_stream(output_stream_)
163  .concentration_fields(conc_mobile_fe)
164  .set_time_governor(*time_);
165  reaction_mobile->initialize();
166  }
167 
169  {
170  reaction_immobile->substances(substances_)
171  .output_stream(output_stream_)
172  .concentration_fields(data_.conc_immobile_fe)
173  .set_time_governor(*time_);
174  reaction_immobile->initialize();
175  }
176 
177 }
178 
180 {
182  //setting fields that are set from input file
185 
186  //setting fields in data
187  data_.set_mesh(*mesh_);
188 
189  //initialization of output
194 
195 
196  //creating field fe and output multifield for sorbed concentrations
198  for (unsigned int sbi = 0; sbi < substances_.size(); sbi++)
199  {
200  // create shared pointer to a FieldFE and push this Field to output_field on all regions
201  data_.conc_immobile_fe[sbi] = create_field_fe< 3, FieldValue<3>::Scalar >(dof_handler_);
203  }
204 
206 }
207 
208 
210 {
211  ASSERT(time_ != nullptr).error("Time governor has not been set yet.\n");
212  ASSERT_LT(0, substances_.size()).error("No substances for rection term.\n");
213  ASSERT(output_stream_ != nullptr).error("Null output stream.\n");
214 
215  //coupling - passing fields
216  if(reaction_mobile)
217  if (typeid(*reaction_mobile) == typeid(SorptionMob))
218  {
219  reaction_mobile->eq_fieldset().set_field("porosity", data_["porosity"]);
220  reaction_mobile->eq_fieldset().set_field("porosity_immobile", data_["porosity_immobile"]);
221  }
223  if (typeid(*reaction_immobile) == typeid(SorptionImmob))
224  {
225  reaction_immobile->eq_fieldset().set_field("porosity", data_["porosity"]);
226  reaction_immobile->eq_fieldset().set_field("porosity_immobile", data_["porosity_immobile"]);
227  }
228 
230  std::stringstream ss; // print warning message with table of uninitialized fields
231  if ( FieldCommon::print_message_table(ss, "dual porosity") ) {
232  WarningOut() << ss.str();
233  }
235 
236  output_data();
237 
238  if(reaction_mobile)
239  reaction_mobile->zero_time_step();
240 
242  reaction_immobile->zero_time_step();
243 
244 }
245 
247 {
248  for ( DHCellAccessor dh_cell : dof_handler_->own_range() ) {
249  IntIdx dof_p0 = dh_cell.get_loc_dof_indices()[0];
250  const ElementAccessor<3> ele = dh_cell.elm();
251 
252  //setting initial solid concentration for substances involved in adsorption
253  for (unsigned int sbi = 0; sbi < substances_.size(); sbi++)
254  {
255  data_.conc_immobile_fe[sbi]->vec().set( dof_p0, data_.init_conc_immobile[sbi].value(ele.centre(), ele) );
256  }
257  }
258 }
259 
261 {
263 
264  START_TIMER("dual_por_exchange_step");
265  for ( DHCellAccessor dh_cell : dof_handler_->own_range() )
266  {
267  compute_reaction(dh_cell);
268  }
269  END_TIMER("dual_por_exchange_step");
270 
271  if(reaction_mobile) reaction_mobile->update_solution();
272  if(reaction_immobile) reaction_immobile->update_solution();
273 }
274 
275 
277 {
278  unsigned int sbi;
279  double conc_average, // weighted (by porosity) average of concentration
280  conc_mob, conc_immob, // new mobile and immobile concentration
281  previous_conc_mob, previous_conc_immob, // mobile and immobile concentration in previous time step
282  conc_max, //difference between concentration and average concentration
283  por_mob, por_immob; // mobile and immobile porosity
284 
285  // get data from fields
286  ElementAccessor<3> ele = dh_cell.elm();
287  IntIdx dof_p0 = dh_cell.get_loc_dof_indices()[0];
288  por_mob = data_.porosity.value(ele.centre(),ele);
289  por_immob = data_.porosity_immobile.value(ele.centre(),ele);
290  arma::Col<double> diff_vec(substances_.size());
291  for (sbi=0; sbi<substances_.size(); sbi++) // Optimize: SWAP LOOPS
292  diff_vec[sbi] = data_.diffusion_rate_immobile[sbi].value(ele.centre(), ele);
293 
294  // if porosity_immobile == 0 then mobile concentration stays the same
295  // and immobile concentration cannot change
296  if (por_immob == 0.0) return;
297 
298  double exponent,
299  temp_exponent = (por_mob + por_immob) / (por_mob * por_immob) * time_->dt();
300 
301  for (sbi = 0; sbi < substances_.size(); sbi++) //over all substances
302  {
303  exponent = diff_vec[sbi] * temp_exponent;
304  //previous values
305  previous_conc_mob = conc_mobile_fe[sbi]->vec().get(dof_p0);
306  previous_conc_immob = data_.conc_immobile_fe[sbi]->vec().get(dof_p0);
307 
308  // ---compute average concentration------------------------------------------
309  conc_average = ((por_mob * previous_conc_mob) + (por_immob * previous_conc_immob))
310  / (por_mob + por_immob);
311 
312  conc_max = std::max(previous_conc_mob-conc_average, previous_conc_immob-conc_average);
313 
314  // the following 2 conditions guarantee:
315  // 1) stability of forward Euler's method
316  // 2) the error of forward Euler's method will not be large
317  if(time_->dt() <= por_mob*por_immob/(max(diff_vec)*(por_mob+por_immob)) &&
318  conc_max <= (2*scheme_tolerance_/(exponent*exponent)*conc_average)) // forward euler
319  {
320  double temp = diff_vec[sbi]*(previous_conc_immob - previous_conc_mob) * time_->dt();
321  // ---compute concentration in mobile area
322  conc_mob = temp / por_mob + previous_conc_mob;
323 
324  // ---compute concentration in immobile area
325  conc_immob = -temp / por_immob + previous_conc_immob;
326  }
327  else //analytic solution
328  {
329  double temp = exp(-exponent);
330  // ---compute concentration in mobile area
331  conc_mob = (previous_conc_mob - conc_average) * temp + conc_average;
332 
333  // ---compute concentration in immobile area
334  conc_immob = (previous_conc_immob - conc_average) * temp + conc_average;
335  }
336 
337  conc_mobile_fe[sbi]->vec().set(dof_p0, conc_mob);
338  data_.conc_immobile_fe[sbi]->vec().set(dof_p0, conc_immob);
339  }
340 }
341 
342 
344 {
346 
347  // Register fresh output data
349 
350  if (time_->tlevel() !=0) {
351  // zero_time_step call zero_time_Step of subreactions which performs its own output
352  if (reaction_mobile) reaction_mobile->output_data();
353  if (reaction_immobile) reaction_immobile->output_data();
354  }
355 }
356 
357 
358 bool DualPorosity::evaluate_time_constraint(double &time_constraint)
359 {
360  bool cfl_changed = false;
361  if (reaction_mobile)
362  {
363  if (reaction_mobile->evaluate_time_constraint(time_constraint))
364  cfl_changed = true;
365  }
366  if (reaction_immobile)
367  {
368  double cfl_immobile;
369  if (reaction_immobile->evaluate_time_constraint(cfl_immobile))
370  {
371  time_constraint = std::min(time_constraint, cfl_immobile);
372  cfl_changed = true;
373  }
374  }
375 
376  return cfl_changed;
377 }
TimeGovernor & time()
Definition: equation.hh:149
void output_type(OutputTime::DiscreteSpace rt)
Definition: field_set.hh:303
Sorption model in immobile zone in case dual porosity is considered.
Definition: sorption.hh:132
void set_initial_condition()
Sets initial condition from input.
std::shared_ptr< ReactionTerm > reaction_mobile
Reaction running in mobile zone.
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:602
int tlevel() const
void output_data(void) override
Main output routine.
const std::vector< std::string > & names()
Definition: substance.hh:85
MultiField< 3, FieldValue< 3 >::Scalar > conc_immobile
Calculated concentrations in the immobile zone.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
int IntIdx
Definition: index_types.hh:25
void output(TimeStep step)
Abstract linear system class.
Definition: balance.hh:40
SubstanceList substances_
Names belonging to substances.
EquationOutput output_fields
Fields indended for output, i.e. all input fields plus those representing solution.
void zero_time_step() override
Definition: mesh.h:77
Field< 3, FieldValue< 3 >::Scalar > porosity_immobile
Immobile porosity field.
Iterator< Ret > find(const string &key) const
Cell accessor allow iterate over DOF handler cells.
~DualPorosity(void)
Destructor.
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
const TimeStep & step(int index=-1) const
static Input::Type::Abstract & it_abstract_mobile_term()
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
unsigned int size() const
Definition: substance.hh:87
void set(std::vector< typename Field< spacedim, Value >::FieldBasePtr > field_vec, double time, std::vector< std::string > region_set_names={"ALL"})
Class for declaration of inputs sequences.
Definition: type_base.hh:339
std::shared_ptr< OutputTime > output_stream_
Pointer to a transport output stream.
void update_solution(void) override
ReactionTerm(Mesh &init_mesh, Input::Record in_rec)
Constructor.
Class ReactionTerm is an abstract class representing reaction term in transport.
Class Dual_por_exchange implements the model of dual porosity.
arma::vec::fixed< spacedim > centre() const
Computes the barycenter.
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
double scheme_tolerance_
Dual porosity computational scheme tolerance.
void setup_components()
std::shared_ptr< ReactionTerm > reaction_immobile
Reaction running in immobile zone.
const ElementAccessor< 3 > elm() const
Return ElementAccessor to element of loc_ele_idx_.
void make_reactions()
Resolves construction of following reactions.
void initialize() override
Prepares the object to usage.
Class for declaration of the input data that are floating point numbers.
Definition: type_base.hh:534
static constexpr Mask equation_result
Match result fields. These are never given by input or copy of input.
Definition: field_flag.hh:55
static constexpr Mask input_copy
Definition: field_flag.hh:44
FieldCommon & input_default(const string &input_default)
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
const Ret val(const string &key) const
FieldSet * eq_fieldset_
Definition: equation.hh:227
#define START_TIMER(tag)
Starts a timer with specified tag.
bool evaluate_time_constraint(double &time_constraint) override
Computes a constraint for time step.
void initialize_fields()
Initializes field sets.
static Input::Type::Abstract & it_abstract_immobile_term()
Mesh * mesh_
Definition: equation.hh:218
void compute_reaction(const DHCellAccessor &dh_cell) override
Compute reaction on a single element.
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) const
Definition: field.hh:451
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
EqData()
Collect all fields.
Class representing dual porosity model in transport.
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
static Input::Type::Abstract & it_abstract_term()
Support classes for parallel programing.
Sorption model in mobile zone in case dual porosity is considered.
Definition: sorption.hh:106
FieldCommon & description(const string &description)
void set_input_list(Input::Array input_list, const TimeGovernor &tg)
Definition: field_set.hh:282
void initialize(std::shared_ptr< OutputTime > stream, Mesh *mesh, Input::Record in_rec, const TimeGovernor &tg)
Input::Record input_record_
Definition: equation.hh:220
void set_components(const std::vector< string > &names)
Definition: field_set.hh:268
double dt() const
FieldFEScalarVec conc_immobile_fe
Underlaying FieldFE for each substance of conc_immobile.
#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).
bool set_time(const TimeStep &time, LimitSide limit_side)
Definition: field_set.cc:165
std::shared_ptr< DOFHandlerMultiDim > dof_handler_
Pointer to DOF handler used through the reaction tree.
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:270
FieldCommon & name(const string &name)
#define END_TIMER(tag)
Ends a timer with specified tag.
MultiField< 3, FieldValue< 3 >::Scalar > diffusion_rate_immobile
Mass transfer coefficients between mobile and immobile pores.
static const int registrar
Registrar of class to factory.
void set_mesh(const Mesh &mesh)
Definition: field_set.hh:274
MultiField< 3, FieldValue< 3 >::Scalar > init_conc_immobile
Initial concentrations in the immobile zone.
Record type proxy class.
Definition: type_record.hh:182
FieldCommon & set_limits(double min, double max=std::numeric_limits< double >::max())
Field< 3, FieldValue< 3 >::Scalar > porosity
Porosity field.
FieldCommon & flags(FieldFlag::Flags::Mask mask)
Class for representation SI units of Fields.
Definition: unit_si.hh:40
static const Input::Type::Record & get_input_type()
static UnitSI & dimensionless()
Returns dimensionless unit.
Definition: unit_si.cc:55
static bool print_message_table(ostream &stream, std::string equation_name)
Definition: field_common.cc:96
DualPorosity data.
This file contains classes representing sorption model. Sorption model can be computed both in case t...
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
TimeGovernor * time_
Definition: equation.hh:219
FieldSet input_data_set_