Flow123d  master-2fe7782
darcy_flow_lmh.hh
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 darcy_flow_lmh.hh
15  * @brief Lumped mixed-hybrid model of linear Darcy flow, possibly unsteady.
16  * @author Jan Brezina
17  *
18  * Main object for mixed-hybrid discretization of the linear elliptic PDE (Laplace)
19  * on a multidimensional domain. Discretization of saturated Darcy flow using
20  * RT0 approximation for the velocity
21  */
22 
23 /*
24  * list of files dependent on this one:
25  *
26  * posprocess.cc
27  * problem.cc
28  * main.hh
29  * transport.cc
30  */
31 
32 
33 #ifndef DARCY_FLOW_LMH_HH
34 #define DARCY_FLOW_LMH_HH
35 
36 #include <petscmat.h> // for Mat
37 #include <string.h> // for memcpy
38 #include <algorithm> // for swap
39 
40 #include <memory> // for shared_ptr, allocator...
41 #include <new> // for operator new[]
42 #include <string> // for string, operator<<
43 #include <vector> // for vector, vector<>::con...
44 #include <armadillo>
45 #include "fields/bc_field.hh" // for BCField
46 #include "fields/field.hh" // for Field
47 #include "fields/field_set.hh" // for FieldSet
48 #include "fields/field_values.hh" // for FieldValue<>::Scalar
49 #include "flow/darcy_flow_interface.hh" // for DarcyFlowInterface
50 #include "input/input_exception.hh" // for DECLARE_INPUT_EXCEPTION
51 #include "input/type_base.hh" // for Array
52 #include "input/type_generic.hh" // for Instance
53 #include "mesh/mesh.h" // for Mesh
54 #include "petscvec.h" // for Vec, _p_Vec, VecScatter
55 #include "system/exceptions.hh" // for ExcStream, operator<<
56 #include "tools/time_governor.hh" // for TimeGovernor
57 #include "la/vector_mpi.hh" // for VectorMPI
58 
59 
60 class Balance;
61 class DarcyFlowMHOutput;
62 class Element;
63 class Intersection;
64 class LinSys;
65 // class LinSys_BDDC;
66 class LocalSystem;
67 class LocalConstraint;
68 namespace Input {
69  class AbstractRecord;
70  class Record;
71  namespace Type {
72  class Record;
73  class Selection;
74  }
75 }
76 template<unsigned int dim, class TEqData> class ReadInitCondAssemblyLMH;
77 template<unsigned int dim, class TEqData> class MHMatrixAssemblyLMH;
78 template<unsigned int dim, class TEqData> class ReconstructSchurAssemblyLMH;
80 template< template<IntDim...> class DimAssembly> class GenericAssembly;
81 
82 
83 /**
84  * @brief Mixed-hybrid model of linear Darcy flow, possibly unsteady.
85  *
86  * Abstract class for various implementations of Darcy flow. In future there should be
87  * one further level of abstraction for general time dependent problem.
88  *
89  * maybe TODO:
90  * split compute_one_step to :
91  * 1) prepare_next_timestep
92  * 2) actualize_solution - this is for iterative nonlinear solvers
93  *
94  */
95 
96 
97 /**
98  * @brief Mixed-hybrid of steady Darcy flow with sources and variable density.
99  *
100  * solve equations:
101  * @f[
102  * q= -{\mathbf{K}} \nabla h -{\mathbf{K}} R \nabla z
103  * @f]
104  * @f[
105  * \mathrm{div} q = f
106  * @f]
107  *
108  * where
109  * - @f$ q @f$ is flux @f$[ms^{-1}]@f$ for 3d, @f$[m^2s^{-1}]@f$ for 2d and @f$[m^3s^{-1}]@f$ for 1d.
110  * - @f$ \mathbf{K} @f$ is hydraulic tensor ( its orientation for 2d, 1d case is questionable )
111  * - @f$ h = \frac{\pi}{\rho_0 g}+z @f$ is pressure head, @f$ \pi, \rho_0, g @f$ are the pressure, water density, and acceleration of gravity , respectively.
112  * Assumes gravity force acts counter to the direction of the @f$ z @f$ axis.
113  * - @f$ R @f$ is destity or gravity variability coefficient. For density driven flow it should be
114  * @f[
115  * R = \frac{\rho}{\rho_0} -1 = \rho_0^{-1}\sum_{i=1}^s c_i
116  * @f]
117  * where @f$ c_i @f$ is concentration in @f$ kg m^{-3} @f$.
118  *
119  * The time key is optional, when not specified the equation is forced to steady regime. Using Steady TimeGovernor which have no dt constraints.
120  *
121  *
122  * TODO:
123  * Make solution regular field (need FeSeystem and parallel DofHandler for edge pressures), then remove get_solution_vector from
124  * Equation interface.
125  */
126 /**
127  * Model for transition coefficients due to Martin, Jaffre, Roberts (see manual for full reference)
128  *
129  * TODO:
130  * - how we can reuse field values computed during assembly
131  *
132  */
133 
135 {
136 public:
137  TYPEDEF_ERR_INFO( EI_Reason, string);
138  DECLARE_EXCEPTION(ExcSolverDiverge,
139  << "Diverged nonlinear solver. Reason: " << EI_Reason::val
140  );
141  DECLARE_INPUT_EXCEPTION(ExcMissingTimeGovernor,
142  << "Missing the key 'time', obligatory for the transient problems.");
143 
144  /** Class with all fields used in the equation DarcyFlow.
145  * This is common to all implementations since this provides interface
146  * to this equation for possible coupling.
147  *
148  * This class uses the common output class DarcyFlowMHOutput.
149  * It is base class of RichardsLMH::EqFields.
150  * */
151  class EqFields : public FieldSet {
152  public:
153  /**
154  * For compatibility with old BCD file we have to assign integer codes starting from 1.
155  */
156  enum BC_Type {
157  none=0,
161  river=6
162  };
163 
164  /// Return a Selection corresponding to enum BC_Type.
166 
167  /// Creation of all fields.
168  EqFields();
169 
170  /// Return coords field
172  return this->X_;
173  }
174 
175 
183 
184  BCField<3, FieldValue<3>::Enum > bc_type; // Discrete need Selection for initialization
189 
192  Field<3, FieldValue<3>::Scalar > extra_storativity; /// Externally added storativity.
193  Field<3, FieldValue<3>::Scalar > extra_source; /// Externally added water source.
194 
200 
201  Field<3, FieldValue<3>::VectorFixed > gravity_field; /// Holds gravity vector acceptable in FieldModel
202  BCField<3, FieldValue<3>::VectorFixed > bc_gravity; /// Same as previous but used in boundary fields
206 
207  Field<3, FieldValue<3>::Scalar> ref_pressure; /// Precompute l2 difference outputs
210  };
211 
212  class EqData {
213  public:
215 
216  EqData(shared_ptr<EqFields> eq_fields);
217 
218  void init(); ///< Initialize vectors, ...
219  void reset(); ///< Reset data members
220 
221  /// Shared pointer of EqFields
222  std::shared_ptr<EqFields> eq_fields_;
223 
224  /**
225  * Gravity vector and constant shift of pressure potential. Used to convert piezometric head
226  * to pressure head and vice versa.
227  */
228  arma::vec4 gravity_;
230 
231  // Mirroring the following members of DarcyLMH:
233  std::shared_ptr<DOFHandlerMultiDim> dh_; ///< full DOF handler represents DOFs of sides, elements and edges
234  std::shared_ptr<SubDOFHandlerMultiDim> dh_cr_; ///< DOF handler represents DOFs of edges
235  std::shared_ptr<DOFHandlerMultiDim> dh_cr_disc_; ///< DOF handler represents DOFs of sides
236  std::shared_ptr<SubDOFHandlerMultiDim> dh_p_; ///< DOF handler represents DOFs of element pressure
237 
238 
240 
241  // TODO: check usage of MortarMethod in LMH
243 
244  int is_linear; ///< Hack fo BDDC solver.
245  bool force_no_neumann_bc; ///< auxiliary flag for switchting Dirichlet like BC
246 
247  /// Idicator of dirichlet or neumann type of switch boundary conditions.
249 
250  VectorMPI full_solution; //< full solution [vel,press,lambda] from 2. Schur complement
251 
252  // Propagate test for the time term to the assembly.
253  // This flag is necessary for switching BC to avoid setting zero neumann on the whole boundary in the steady case.
255 
256  // for time term assembly
257  double time_step_;
258 
259  std::shared_ptr<LinSys> lin_sys_schur; //< Linear system of the 2. Schur complement.
260  VectorMPI p_edge_solution; //< 2. Schur complement solution
261  VectorMPI p_edge_solution_previous; //< 2. Schur complement previous solution (iterative)
262  VectorMPI p_edge_solution_previous_time; //< 2. Schur complement previous solution (time)
263 
265 
266  /// Shared Balance object
267  std::shared_ptr<Balance> balance_;
268 
269  unsigned int nonlinear_iteration_; //< Actual number of completed nonlinear iterations, need to pass this information into assembly.
270 
271  /// Following data members are stored in vectors, one item for every cell.
272 // /** TODO: Investigate why the hell do we need this flag.
273 // * If removed, it does not break any of the integration tests,
274 // * however it must influence the Dirichlet rows in matrix.
275 // */
276 // std::vector<unsigned int> dirichlet_edge;
277 
281  std::array<std::vector<unsigned int>, 3> loc_side_dofs;
282  std::array<std::vector<unsigned int>, 3> loc_edge_dofs;
283  std::array<unsigned int, 3> loc_ele_dof;
284 
285 // // std::shared_ptr<MortarAssemblyBase> mortar_assembly;
286 
287  std::vector<bool> save_local_system_; ///< Flag for saving the local system. Currently used only in case of seepage BC.
288  std::vector<bool> bc_fluxes_reconstruted; ///< Flag indicating whether the fluxes for seepage BC has been reconstructed already.
289  std::array<unsigned int, 3> schur_offset_; ///< Index offset in the local system for the Schur complement (of dim = 1,2,3).
290  };
291 
293  template<unsigned int dim> using MHMatrixAssemblyLMHDim = MHMatrixAssemblyLMH<dim, EqData>;
295 
296  /// Selection for enum MortarMethod.
298 
299 
300 
301 
302 
303  DarcyLMH(Mesh &mesh, const Input::Record in_rec, TimeGovernor *tm = nullptr);
304 
306  static const Input::Type::Record & get_input_type();
307 
308  void init_eq_data();
309  void initialize() override;
310  virtual void initialize_specific();
311  void zero_time_step() override;
312  void update_solution() override;
313 
314  /// Solve the problem without moving to next time and without output.
315  void solve_time_step(bool output = true);
316 
317  /// postprocess velocity field (add sources)
318  virtual void accept_time_step();
319  virtual void postprocess();
320  virtual void output_data() override;
321 
322  virtual double solved_time() override;
323 
324  inline EqFields &eq_fields() { return *eq_fields_; }
325  inline EqData &eq_data() { return *eq_data_; }
326 
327  /// Sets external storarivity field (coupling with other equation).
329  { eq_fields_->extra_storativity = extra_stor; }
330 
331  /// Sets external source field (coupling with other equation).
332  void set_extra_source(const Field<3, FieldValue<3>::Scalar> &extra_src)
333  { eq_fields_->extra_source = extra_src; }
334 
335  virtual ~DarcyLMH() override;
336 
337 
338 protected:
339  /**
340  * Returns true is the fields involved in the time term have values that makes the time term zero.
341  * For time_global==true, it returns true if there are no field descriptors in the input list, so the
342  * fields )of the time ter) have their default values for whole simulation.
343  * If time_global==false (default), only the actual values are considered.
344  */
345  virtual bool zero_time_term(bool time_global=false);
346 
347  /// Solve method common to zero_time_step and update solution.
348  void solve_nonlinear();
349 
350  /**
351  * Create and preallocate MH linear system (including matrix, rhs and solution vectors)
352  */
354 
355  /**
356  * Read initial condition into solution vector.
357  * Must be called after create_linear_system.
358  *
359  * For the LMH scheme we have to be able to save edge pressures in order to
360  * restart simulation or use results of one simulation as initial condition for other one.
361  */
362 // void read_initial_condition();
363 
364  /**
365  * In some circumstances, the intial condition must be processed.
366  * It is called at the end of @p read_initial_condition().
367  * This is used in Richards equation due the update of water content.
368  */
369 // virtual void initial_condition_postprocess();
370 
371  /**
372  * Allocates linear system matrix for MH.
373  * TODO:
374  * - use general preallocation methods in DofHandler
375  */
376  void allocate_mh_matrix();
377 
378  /**
379  * Assembles linear system matrix for MH.
380  * Element by element assembly is done using dim-template assembly class.
381  * Assembles only steady part of the equation.
382  * TODO:
383  * - include time term - DONE
384  * - add support for Robin type sources
385  * - support for nonlinear solvers - assembly either residual vector, matrix, or both (using FADBAD++)
386  */
387 // void assembly_mh_matrix(MultidimAssembly& assembler);
388 
389 // void reconstruct_solution_from_schur(MultidimAssembly& assembler);
390 
391  /**
392  * Assembly or update whole linear system.
393  */
394  virtual void assembly_linear_system();
395 
396 // void set_mesh_data_for_bddc(LinSys_BDDC * bddc_ls);
397  /**
398  * Return a norm of residual vector.
399  * TODO: Introduce Equation::compute_residual() updating
400  * residual field, standard part of EqData.
401  */
402 // virtual double solution_precision() const;
403 
404  /// Print darcy flow matrix in matlab format into a file.
405  void print_matlab_matrix(string matlab_file);
406 
407  /// Get vector of all DOF indices of given component (0..side, 1..element, 2..edge)
408  std::vector<int> get_component_indices_vec(unsigned int component) const;
409 
410  /// Getter for the linear system of the 2. Schur complement.
412  { return *(eq_data_->lin_sys_schur); }
413 
414  /// Create and initialize assembly objects
415  virtual void initialize_asm();
416 
417  /// Call assemble of read_init_cond_assembly_
418  virtual void read_init_cond_asm();
419 
420  std::shared_ptr<Balance> balance_;
421 
423 
424  int size; // global size of MH matrix
425 
427 
428  // Setting of the nonlinear solver. TODO: Move to the solver class later on.
429  double tolerance_;
430  unsigned int min_n_it_;
431  unsigned int max_n_it_;
432 
433  std::shared_ptr<EqFields> eq_fields_;
434  std::shared_ptr<EqData> eq_data_;
435 
436 
437  friend class DarcyFlowMHOutput;
438  //friend class P0_CouplingAssembler;
439  //friend class P1_CouplingAssembler;
440 
441  /// general assembly objects, hold assembly objects of appropriate dimension
445 private:
446  /// Registrar of class to factory
447  static const int registrar;
448 
449  static std::string equation_name()
450  { return "Flow_Darcy_LMH";}
451 };
452 
453 #endif //DARCY_FLOW_LMH_HH
454 //-----------------------------------------------------------------------------
455 // vim: set cindent:
456 
MortarMethod
Type of experimental Mortar-like method for non-compatible 1d-2d interaction.
std::array< unsigned int, 3 > schur_offset_
Index offset in the local system for the Schur complement (of dim = 1,2,3).
unsigned int nonlinear_iteration_
arma::vec3 gravity_vec_
std::vector< bool > bc_fluxes_reconstruted
Flag indicating whether the fluxes for seepage BC has been reconstructed already.
std::map< LongIdx, LocalSystem > seepage_bc_systems
int is_linear
Hack fo BDDC solver.
EqData(shared_ptr< EqFields > eq_fields)
std::shared_ptr< SubDOFHandlerMultiDim > dh_p_
DOF handler represents DOFs of element pressure.
VectorMPI full_solution
std::vector< bool > save_local_system_
Flag for saving the local system. Currently used only in case of seepage BC.
void reset()
Reset data members.
std::vector< arma::vec > postprocess_solution_
MortarMethod mortar_method_
std::shared_ptr< SubDOFHandlerMultiDim > dh_cr_
DOF handler represents DOFs of edges.
bool force_no_neumann_bc
auxiliary flag for switchting Dirichlet like BC
VectorMPI p_edge_solution_previous_time
DarcyLMH::EqFields EqFields
std::shared_ptr< DOFHandlerMultiDim > dh_cr_disc_
DOF handler represents DOFs of sides.
VectorMPI p_edge_solution
VectorMPI p_edge_solution_previous
std::shared_ptr< DOFHandlerMultiDim > dh_
full DOF handler represents DOFs of sides, elements and edges
std::vector< LocalConstraint > loc_constraint_
std::vector< LocalSystem > loc_system_
Following data members are stored in vectors, one item for every cell.
std::vector< char > bc_switch_dirichlet
Idicator of dirichlet or neumann type of switch boundary conditions.
std::shared_ptr< LinSys > lin_sys_schur
std::array< unsigned int, 3 > loc_ele_dof
std::array< std::vector< unsigned int >, 3 > loc_edge_dofs
std::shared_ptr< EqFields > eq_fields_
Shared pointer of EqFields.
void init()
Initialize vectors, ...
std::shared_ptr< Balance > balance_
Shared Balance object.
std::array< std::vector< unsigned int >, 3 > loc_side_dofs
Field< 3, FieldValue< 3 >::Scalar > water_source_density
Field< 3, FieldValue< 3 >::Scalar > ref_divergence
Field< 3, FieldValue< 3 >::Scalar > extra_storativity
Field< 3, FieldValue< 3 >::Scalar > field_ele_pressure
Externally added water source.
BCField< 3, FieldValue< 3 >::Enum > bc_type
Field< 3, FieldValue< 3 >::VectorFixed > gravity_field
Field< 3, FieldValue< 3 >::Scalar > sigma
Field< 3, FieldValue< 3 >::Scalar > init_pressure
Field< 3, FieldValue< 3 >::Scalar > storativity
BCField< 3, FieldValue< 3 >::Scalar > bc_pressure
BCField< 3, FieldValue< 3 >::Scalar > bc_flux
Field< 3, FieldValue< 3 >::TensorFixed > anisotropy
Field< 3, FieldValue< 3 >::Scalar > water_source_sigma
Field< 3, FieldValue< 3 >::Scalar > cross_section
FieldCoords & X()
Return coords field.
Field< 3, FieldValue< 3 >::Scalar > conductivity
Field< 3, FieldValue< 3 >::VectorFixed > field_ele_velocity
Field< 3, FieldValue< 3 >::Scalar > field_edge_pressure
BCField< 3, FieldValue< 3 >::Scalar > bc_switch_piezo_head
Field< 3, FieldValue< 3 >::Scalar > field_ele_piezo_head
Field< 3, FieldValue< 3 >::Scalar > init_piezo_head
Same as previous but used in boundary fields.
Field< 3, FieldValue< 3 >::Scalar > water_source_ref_pressure
BCField< 3, FieldValue< 3 >::Scalar > bc_robin_sigma
static const Input::Type::Selection & get_bc_type_selection()
Return a Selection corresponding to enum BC_Type.
Field< 3, FieldValue< 3 >::Scalar > extra_source
Externally added storativity.
Field< 3, FieldValue< 3 >::VectorFixed > ref_velocity
Precompute l2 difference outputs.
Field< 3, FieldValue< 3 >::VectorFixed > flux
EqFields()
Creation of all fields.
BCField< 3, FieldValue< 3 >::Scalar > bc_switch_pressure
BCField< 3, FieldValue< 3 >::VectorFixed > bc_gravity
Holds gravity vector acceptable in FieldModel.
BCField< 3, FieldValue< 3 >::Scalar > bc_piezo_head
Field< 3, FieldValue< 3 >::Scalar > ref_pressure
Mixed-hybrid model of linear Darcy flow, possibly unsteady.
GenericAssemblyBase * mh_matrix_assembly_
void initialize() override
void print_matlab_matrix(string matlab_file)
Print darcy flow matrix in matlab format into a file.
virtual double solved_time() override
void zero_time_step() override
virtual void postprocess()
EqFields & eq_fields()
void solve_nonlinear()
Solve method common to zero_time_step and update solution.
static std::string equation_name()
void set_extra_source(const Field< 3, FieldValue< 3 >::Scalar > &extra_src)
Sets external source field (coupling with other equation).
virtual void initialize_specific()
static const int registrar
Registrar of class to factory.
DarcyFlowMHOutput * output_object
std::shared_ptr< EqData > eq_data_
unsigned int max_n_it_
DECLARE_INPUT_EXCEPTION(ExcMissingTimeGovernor,<< "Missing the key 'time', obligatory for the transient problems.")
bool data_changed_
GenericAssemblyBase * reconstruct_schur_assembly_
double tolerance_
virtual void initialize_asm()
Create and initialize assembly objects.
void set_extra_storativity(const Field< 3, FieldValue< 3 >::Scalar > &extra_stor)
Sets external storarivity field (coupling with other equation).
void update_solution() override
void allocate_mh_matrix()
unsigned int min_n_it_
void solve_time_step(bool output=true)
Solve the problem without moving to next time and without output.
std::vector< int > get_component_indices_vec(unsigned int component) const
Get vector of all DOF indices of given component (0..side, 1..element, 2..edge)
static const Input::Type::Record & type_field_descriptor()
EqData & eq_data()
void create_linear_system(Input::AbstractRecord rec)
static const Input::Type::Record & get_input_type()
DECLARE_EXCEPTION(ExcSolverDiverge,<< "Diverged nonlinear solver. Reason: "<< EI_Reason::val)
DarcyLMH(Mesh &mesh, const Input::Record in_rec, TimeGovernor *tm=nullptr)
CREATE AND FILL GLOBAL MH MATRIX OF THE WATER MODEL.
virtual bool zero_time_term(bool time_global=false)
std::shared_ptr< EqFields > eq_fields_
static const Input::Type::Selection & get_mh_mortar_selection()
Selection for enum MortarMethod.
std::shared_ptr< Balance > balance_
void init_eq_data()
virtual void accept_time_step()
postprocess velocity field (add sources)
virtual ~DarcyLMH() override
virtual void output_data() override
Write computed fields.
GenericAssembly< ReadInitCondAssemblyLMHDim > * read_init_cond_assembly_
general assembly objects, hold assembly objects of appropriate dimension
LinSys & lin_sys_schur()
Getter for the linear system of the 2. Schur complement.
TYPEDEF_ERR_INFO(EI_Reason, string)
virtual void assembly_linear_system()
virtual void read_init_cond_asm()
Call assemble of read_init_cond_assembly_.
Mesh & mesh()
Definition: equation.hh:181
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:161
FieldCoords X_
Field holds coordinates for computing of FieldFormulas.
Definition: field_set.hh:438
Class template representing a field with values dependent on: point, element, and region.
Definition: field.hh:93
Generic class of assemblation.
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Record type proxy class.
Definition: type_record.hh:182
Template for classes storing finite set of named values.
Definition: mesh.h:362
Basic time management functionality for unsteady (and steady) solvers (class Equation).
unsigned int uint
unsigned int IntDim
A dimension index type.
Definition: mixed.hh:19
Abstract linear system class.
Definition: balance.hh:40
Basic time management class.