Flow123d  JS_before_hm-1001-gfa0c761
hm_iterative.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 hm_iterative.cc
15  * @brief
16  * @author Jan Stebel
17  */
18 
19 #include "hm_iterative.hh"
20 #include "system/sys_profiler.hh"
21 #include "input/input_type.hh"
22 #include "flow/richards_lmh.hh"
23 #include "fields/field_fe.hh" // for create_field_fe()
24 
25 
26 FLOW123D_FORCE_LINK_IN_CHILD(coupling_iterative)
27 
28 
29 namespace it = Input::Type;
30 
31 
32 const it::Record & HM_Iterative::get_input_type() {
33  return it::Record("Coupling_Iterative",
34  "Record with data for iterative coupling of flow and mechanics.\n")
38  .declare_key("flow_equation", RichardsLMH::get_input_type(),
40  "Flow equation, provides the velocity field as a result.")
41  .declare_key("mechanics_equation", Elasticity::get_input_type(),
42  "Mechanics, provides the displacement field.")
43  .declare_key("input_fields", it::Array(
45  .make_field_descriptor_type("Coupling_Iterative")),
47  "Input fields of the HM coupling.")
48  .declare_key( "iteration_parameter", it::Double(), it::Default("1"),
49  "Tuning parameter for iterative splitting. Its default value"
50  "corresponds to a theoretically optimal value with fastest convergence." )
51  .declare_key( "max_it", it::Integer(0), it::Default("100"),
52  "Maximal count of HM iterations." )
53  .declare_key( "min_it", it::Integer(0), it::Default("1"),
54  "Minimal count of HM iterations." )
55  .declare_key( "a_tol", it::Double(0), it::Default("0"),
56  "Absolute tolerance for difference in HM iteration." )
57  .declare_key( "r_tol", it::Double(0), it::Default("1e-7"),
58  "Relative tolerance for difference in HM iteration." )
59  .close();
60 }
61 
62 
63 const int HM_Iterative::registrar = Input::register_class< HM_Iterative, Mesh &, const Input::Record >("Coupling_Iterative")
65 
66 
68 {
69  *this += alpha.name("biot_alpha")
70  .units(UnitSI().dimensionless())
71  .input_default("0.0")
73 
74  *this += density.name("fluid_density")
75  .units(UnitSI().kg().m(-3))
76  .input_default("0.0")
78 
79  *this += gravity.name("gravity")
80  .units(UnitSI().m().s(-2))
81  .input_default("9.81")
83 
84  *this += beta.name("relaxation_beta")
85  .units(UnitSI().dimensionless())
87 
88  *this += pressure_potential.name("pressure_potential")
89  .units(UnitSI().m())
91 
92  *this += flow_source.name("extra_flow_source")
93  .units(UnitSI().s(-1))
95 }
96 
97 
99 {
100  // initialize coupling fields with FieldFE
101  set_mesh(mesh);
102 
103  potential_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(mesh, MixedPtr<FE_CR>());
104  pressure_potential.set_field(mesh.region_db().get_region_set("ALL"), potential_ptr_);
105 
106  beta_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(mesh, MixedPtr<FE_P_disc>(0));
107  beta.set_field(mesh.region_db().get_region_set("ALL"), beta_ptr_);
108 
109  flow_source_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(beta_ptr_->get_dofhandler());
110  flow_source.set_field(mesh.region_db().get_region_set("ALL"), flow_source_ptr_);
111 
112  old_pressure_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(beta_ptr_->get_dofhandler());
113  old_iter_pressure_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(beta_ptr_->get_dofhandler());
114  div_u_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(beta_ptr_->get_dofhandler());
115  old_div_u_ptr_ = create_field_fe<3, FieldValue<3>::Scalar>(beta_ptr_->get_dofhandler());
116 }
117 
118 
119 
121 : DarcyFlowInterface(mesh, in_record),
122  IterativeCoupling(in_record)
123 {
124  START_TIMER("HM constructor");
125  using namespace Input;
126 
127  time_ = new TimeGovernor(in_record.val<Record>("time"));
128  ASSERT( time_->is_default() == false ).error("Missing key 'time' in Coupling_Iterative.");
129 
130  // setup flow equation
131  Record flow_rec = in_record.val<Record>("flow_equation");
132  // Need explicit template types here, since reference is used (automatically passing by value)
133  flow_ = std::make_shared<RichardsLMH>(*mesh_, flow_rec, time_);
134  flow_->initialize();
135  std::stringstream ss; // print warning message with table of uninitialized fields
136  if ( FieldCommon::print_message_table(ss, "flow") )
137  WarningOut() << ss.str();
138 
139  // setup mechanics
140  Record mech_rec = in_record.val<Record>("mechanics_equation");
141  mechanics_ = std::make_shared<Elasticity>(*mesh_, mech_rec, this->time_);
142  mechanics_->data()["cross_section"].copy_from(flow_->data()["cross_section"]);
143  mechanics_->initialize();
144 
145  // read parameters controlling the iteration
146  beta_ = in_record.val<double>("iteration_parameter");
147 
148  this->eq_data_ = &data_;
149 
150  // setup input fields
151  data_.set_input_list( in_record.val<Input::Array>("input_fields"), time() );
152 
153  data_.initialize(*mesh_);
154  mechanics_->set_potential_load(data_.pressure_potential);
155 }
156 
157 
159 {
160 }
161 
162 
163 template<int dim, class Value>
164 void copy_field(const FieldCommon &from_field_common, FieldFE<dim, Value> &to_field)
165 {
166  auto dh = to_field.get_dofhandler();
167  auto vec = to_field.vec();
168  Field<dim,Value> from_field;
169  from_field.copy_from(from_field_common);
170 
171  for ( auto cell : dh->own_range() )
172  vec[cell.local_idx()] = from_field.value(cell.elm().centre(), cell.elm());
173 }
174 
175 
176 
178 {
180  std::stringstream ss;
181  if ( FieldCommon::print_message_table(ss, "coupling_iterative") )
182  WarningOut() << ss.str();
183 
184  flow_->zero_time_step();
186  mechanics_->zero_time_step();
187 
188  copy_field(*flow_->data().field("pressure_p0"), *data_.old_pressure_ptr_);
189  copy_field(*flow_->data().field("pressure_p0"), *data_.old_iter_pressure_ptr_);
190  copy_field(mechanics_->data().output_divergence, *data_.div_u_ptr_);
191 }
192 
193 
195 {
196  time_->next_time();
197  time_->view("HM");
199 
200  solve_step();
201 }
202 
204 {
205  // pass displacement (divergence) to flow
206  // and solve flow problem
208  flow_->solve_time_step(false);
209 
210  // pass pressure to mechanics and solve mechanics
212  mechanics_->solve_linear_system();
213 }
214 
215 
217 {
218  mechanics_->update_output_fields();
219  copy_field(mechanics_->data().output_divergence, *data_.div_u_ptr_);
220  copy_field(*flow_->data().field("pressure_p0"), *data_.old_iter_pressure_ptr_);
221 }
222 
223 
225 {
226  flow_->accept_time_step();
227  flow_->output_data();
228  mechanics_->output_data();
229 
230  copy_field(*flow_->data().field("pressure_p0"), *data_.old_pressure_ptr_);
231  copy_field(mechanics_->data().output_divergence, *data_.old_div_u_ptr_);
232 }
233 
234 
236 {
237  auto potential_vec_ = data_.potential_ptr_->vec();
238  auto dh = data_.potential_ptr_->get_dofhandler();
239  Field<3, FieldValue<3>::Scalar> field_edge_pressure;
240  field_edge_pressure.copy_from(*flow_->data().field("pressure_edge"));
241  for ( auto ele : dh->local_range() )
242  {
243  auto elm = ele.elm();
244  LocDofVec dof_indices = ele.get_loc_dof_indices();
245  for ( auto side : ele.side_range() )
246  {
247  double alpha = data_.alpha.value(side.centre(), elm);
248  double density = data_.density.value(side.centre(), elm);
249  double gravity = data_.gravity.value(side.centre(), elm);
250  double pressure = field_edge_pressure.value(side.centre(), elm);
251  double potential = -alpha*density*gravity*pressure;
252 
253  potential_vec_[dof_indices[side.side_idx()]] = potential;
254  }
255  }
256 
258  mechanics_->set_potential_load(data_.pressure_potential);
259 }
260 
261 
263 {
264  auto beta_vec = data_.beta_ptr_->vec();
265  auto src_vec = data_.flow_source_ptr_->vec();
266  auto dh = data_.beta_ptr_->get_dofhandler();
267  Field<3,FieldValue<3>::Scalar> field_ele_pressure;
268  field_ele_pressure.copy_from(*flow_->data().field("pressure_p0"));
269  for ( auto ele : dh->local_range() )
270  {
271  auto elm = ele.elm();
272 
273  double alpha = data_.alpha.value(elm.centre(), elm);
274  double young = mechanics_->data().young_modulus.value(elm.centre(), elm);
275  double poisson = mechanics_->data().poisson_ratio.value(elm.centre(), elm);
276  double beta = beta_ * 0.5*alpha*alpha/(2*lame_mu(young, poisson)/elm.dim() + lame_lambda(young, poisson));
277 
278  double old_p = data_.old_pressure_ptr_->value(elm.centre(), elm);
279  double p = field_ele_pressure.value(elm.centre(), elm);
280  double div_u = data_.div_u_ptr_->value(elm.centre(), elm);
281  double old_div_u = data_.old_div_u_ptr_->value(elm.centre(), elm);
282  double src = (beta*(p-old_p) + alpha*(old_div_u - div_u)) / time_->dt();
283 
284  beta_vec[ele.local_idx()] = beta;
285  src_vec[ele.local_idx()] = src;
286  }
287 
290  flow_->set_extra_storativity(data_.beta);
291  flow_->set_extra_source(data_.flow_source);
292 }
293 
294 
295 void HM_Iterative::compute_iteration_error(double& abs_error, double& rel_error)
296 {
297  auto dh = data_.beta_ptr_->get_dofhandler();
298  double p_dif2 = 0, p_norm2 = 0;
299  Field<3,FieldValue<3>::Scalar> field_ele_pressure;
300  field_ele_pressure.copy_from(*flow_->data().field("pressure_p0"));
301  for (auto cell : dh->own_range())
302  {
303  auto elm = cell.elm();
304  double new_p = field_ele_pressure.value(elm.centre(), elm);
305  double old_p = data_.old_iter_pressure_ptr_->value(elm.centre(), elm);
306  p_dif2 += pow(new_p - old_p, 2)*elm.measure();
307  p_norm2 += pow(old_p, 2)*elm.measure();
308  }
309 
310  double send_data[] = { p_dif2, p_norm2 };
311  double recv_data[2];
312  MPI_Allreduce(&send_data, &recv_data, 2, MPI_DOUBLE, MPI_SUM, PETSC_COMM_WORLD);
313  abs_error = sqrt(recv_data[0]);
314  rel_error = abs_error / sqrt(recv_data[1]);
315 
316  MessageOut().fmt("HM Iteration {} abs. difference: {} rel. difference: {}\n"
317  "--------------------------------------------------------",
318  iteration(), abs_error, rel_error);
319 }
320 
321 
322 
324  flow_.reset();
325  mechanics_.reset();
326 }
327 
328 
329 
TimeGovernor & time()
Definition: equation.hh:149
FieldSet * eq_data_
Definition: equation.hh:227
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:73
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
ArmaVec< double, N > vec
Definition: armor.hh:861
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > old_div_u_ptr_
Class for solution of fully coupled flow and mechanics using fixed-stress iterative splitting...
unsigned int size() const
Returns number of keys in the Record.
Definition: type_record.hh:602
double beta_
Tuning parameter for iterative splitting.
Class Input::Type::Default specifies default value of keys of a Input::Type::Record.
Definition: type_record.hh:61
static const int registrar
FieldCommon & flags_add(FieldFlag::Flags::Mask mask)
#define MessageOut()
Macro defining &#39;message&#39; record of log.
Definition: logger.hh:255
void update_potential()
Abstract linear system class.
Definition: balance.hh:40
void compute_iteration_error(double &abs_error, double &rel_error) override
Compute absolute and relative error in the solution.
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:92
void initialize(Mesh &mesh)
Definition: hm_iterative.cc:98
unsigned int iteration()
Definition: hm_iterative.hh:78
void next_time()
Proceed to the next time according to current estimated time step.
static const Input::Type::Record & get_input_type()
Declare input record type for the equation TransportDG.
Definition: elasticity.cc:43
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
#define MPI_SUM
Definition: mpi.h:196
Definition: mesh.h:78
void update_flow_fields()
double lame_mu(double young, double poisson)
Definition: elasticity.cc:128
void zero_time_step() override
static const Input::Type::Record & record_template()
Definition: hm_iterative.hh:39
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > potential_ptr_
FieldFE for pressure_potential field.
const TimeStep & step(int index=-1) const
Class for declaration of the integral input data.
Definition: type_base.hh:483
Basic time management functionality for unsteady (and steady) solvers (class Equation).
FieldCommon & units(const UnitSI &units)
Set basic units of the field.
static const Input::Type::Record & get_input_type()
Define input record.
Definition: hm_iterative.cc:32
void update_after_iteration() override
Save data (e.g. solution fields) for the next iteration.
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Class for declaration of inputs sequences.
Definition: type_base.hh:339
void view(const char *name="") const
static Input::Type::Record & record_template()
Template Record with common keys for derived equations.
Definition: equation.cc:35
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > old_pressure_ptr_
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > div_u_ptr_
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:196
void update_solution() override
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
Mesh & mesh()
Definition: equation.hh:177
Field< 3, FieldValue< 3 >::Scalar > pressure_potential
Potential -alpha*pressure whose gradient is passed to mechanics as additional load.
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > flow_source_ptr_
FieldCommon & input_default(const string &input_default)
static constexpr Mask equation_external_output
Match an output field, that can be also copy of other field.
Definition: field_flag.hh:58
void set_time_result_changed()
Manually mark flag that the field has been changed.
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Field< 3, FieldValue< 3 >::Scalar > alpha
Biot coefficient.
const Ret val(const string &key) const
std::shared_ptr< Elasticity > mechanics_
solute transport with chemistry through operator splitting
#define START_TIMER(tag)
Starts a timer with specified tag.
Mesh * mesh_
Definition: equation.hh:218
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) const
Definition: field.hh:434
Field< 3, FieldValue< 3 >::Scalar > gravity
Standard gravity.
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
static Input::Type::Abstract & get_input_type()
static constexpr Mask in_rhs
A field is part of the right hand side of the equation.
Definition: field_flag.hh:51
Field< 3, FieldValue< 3 >::Scalar > flow_source
void initialize() override
Field< 3, FieldValue< 3 >::Scalar > beta
#define MPI_DOUBLE
Definition: mpi.h:156
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:216
std::shared_ptr< DOFHandlerMultiDim > get_dofhandler() const
Definition: field_fe.hh:142
#define MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm)
Definition: mpi.h:612
void set_input_list(Input::Array input_list, const TimeGovernor &tg)
Definition: field_set.hh:193
static const Input::Type::Record & get_input_type()
Definition: richards_lmh.cc:79
double dt() const
void set_field(const RegionSet &domain, FieldBasePtr field, double time=0.0)
Definition: field.impl.hh:227
HM_Iterative(Mesh &mesh, Input::Record in_record)
bool set_time(const TimeStep &time, LimitSide limit_side)
Definition: field_set.cc:157
void solve_iteration() override
Solve equations and update data (fields).
double lame_lambda(double young, double poisson)
Definition: elasticity.cc:134
void copy_field(const FieldCommon &from_field_common, FieldFE< dim, Value > &to_field)
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:258
FieldCommon & name(const string &name)
VectorMPI & vec()
Definition: field_fe.hh:146
void update_after_converged() override
Save data after iterations have finished.
void set_mesh(const Mesh &mesh)
Definition: field_set.hh:186
Record type proxy class.
Definition: type_record.hh:182
FieldCommon & flags(FieldFlag::Flags::Mask mask)
void copy_from(const FieldCommon &other) override
Definition: field.impl.hh:345
Class for representation SI units of Fields.
Definition: unit_si.hh:40
std::shared_ptr< RichardsLMH > flow_
steady or unsteady water flow simulator based on MH scheme
static bool print_message_table(ostream &stream, std::string equation_name)
Definition: field_common.cc:96
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > old_iter_pressure_ptr_
Field< 3, FieldValue< 3 >::Scalar > density
Density of fluid.
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > beta_ptr_
TimeGovernor * time_
Definition: equation.hh:219
Definition: field.hh:60