Flow123d  JS_before_hm-989-g79825ac
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 #include "flow/darcy_flow_mh.hh" // for DarcyMH::EqData
60 
61 class Balance;
62 class DarcyFlowMHOutput;
63 class Element;
64 class Intersection;
65 class LinSys;
66 // class LinSys_BDDC;
67 class LocalSystem;
68 namespace Input {
69  class AbstractRecord;
70  class Record;
71  namespace Type {
72  class Record;
73  class Selection;
74  }
75 }
76 
77 template<int spacedim, class Value> class FieldAddPotential;
78 template<int spacedim, class Value> class FieldDivide;
79 
80 /**
81  * @brief Mixed-hybrid model of linear Darcy flow, possibly unsteady.
82  *
83  * Abstract class for various implementations of Darcy flow. In future there should be
84  * one further level of abstraction for general time dependent problem.
85  *
86  * maybe TODO:
87  * split compute_one_step to :
88  * 1) prepare_next_timestep
89  * 2) actualize_solution - this is for iterative nonlinear solvers
90  *
91  */
92 
93 
94 /**
95  * @brief Mixed-hybrid of steady Darcy flow with sources and variable density.
96  *
97  * solve equations:
98  * @f[
99  * q= -{\mathbf{K}} \nabla h -{\mathbf{K}} R \nabla z
100  * @f]
101  * @f[
102  * \mathrm{div} q = f
103  * @f]
104  *
105  * where
106  * - @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.
107  * - @f$ \mathbf{K} @f$ is hydraulic tensor ( its orientation for 2d, 1d case is questionable )
108  * - @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.
109  * Assumes gravity force acts counter to the direction of the @f$ z @f$ axis.
110  * - @f$ R @f$ is destity or gravity variability coefficient. For density driven flow it should be
111  * @f[
112  * R = \frac{\rho}{\rho_0} -1 = \rho_0^{-1}\sum_{i=1}^s c_i
113  * @f]
114  * where @f$ c_i @f$ is concentration in @f$ kg m^{-3} @f$.
115  *
116  * The time key is optional, when not specified the equation is forced to steady regime. Using Steady TimeGovernor which have no dt constraints.
117  *
118  *
119  * TODO:
120  * Make solution regular field (need FeSeystem and parallel DofHandler for edge pressures), then remove get_solution_vector from
121  * Equation interface.
122  */
123 /**
124  * Model for transition coefficients due to Martin, Jaffre, Roberts (see manual for full reference)
125  *
126  * TODO:
127  * - how we can reuse field values computed during assembly
128  *
129  */
130 
132 {
133 public:
134  TYPEDEF_ERR_INFO( EI_Reason, string);
135  DECLARE_EXCEPTION(ExcSolverDiverge,
136  << "Diverged nonlinear solver. Reason: " << EI_Reason::val
137  );
138  DECLARE_INPUT_EXCEPTION(ExcMissingTimeGovernor,
139  << "Missing the key 'time', obligatory for the transient problems.");
140 
141  /** Class with all fields used in the equation DarcyFlow.
142  * This is common to all implementations since this provides interface
143  * to this equation for possible coupling.
144  *
145  * This class is derived from DarcyMH::EqData especially due to the common output class DarcyFlowMHOutput.
146  * This is the only dependence between DarcyMH and DarcyLMH classes.
147  * It is also base class of RichardsLMH::EqData.
148  * */
149  class EqData : public DarcyMH::EqData {
150  public:
151 
152  EqData();
153 
154  std::shared_ptr<SubDOFHandlerMultiDim> dh_p_; ///< DOF handler represents DOFs of element pressure
155 
156  // Propagate test for the time term to the assembly.
157  // This flag is necessary for switching BC to avoid setting zero neumann on the whole boundary in the steady case.
159 
160  // for time term assembly
161  double time_step_;
162 
163  std::shared_ptr<LinSys> lin_sys_schur; //< Linear system of the 2. Schur complement.
164  VectorMPI p_edge_solution; //< 2. Schur complement solution
165  VectorMPI p_edge_solution_previous; //< 2. Schur complement previous solution (iterative)
166  VectorMPI p_edge_solution_previous_time; //< 2. Schur complement previous solution (time)
167 
169  };
170 
171  /// Selection for enum MortarMethod.
172  static const Input::Type::Selection & get_mh_mortar_selection();
173 
174 
175 
176 
177 
178  DarcyLMH(Mesh &mesh, const Input::Record in_rec, TimeGovernor *tm = nullptr);
179 
180  static const Input::Type::Record & type_field_descriptor();
181  static const Input::Type::Record & get_input_type();
182 
183  void init_eq_data();
184  void initialize() override;
185  virtual void initialize_specific();
186  void zero_time_step() override;
187  void update_solution() override;
188 
189  /// Solve the problem without moving to next time and without output.
190  void solve_time_step(bool output = true);
191 
192  /// postprocess velocity field (add sources)
193  virtual void accept_time_step();
194  virtual void postprocess();
195  virtual void output_data() override;
196 
197 
198  EqData &data() { return *data_; }
199 
200  /// Sets external storarivity field (coupling with other equation).
202  { data_->extra_storativity = extra_stor; }
203 
204  /// Sets external source field (coupling with other equation).
205  void set_extra_source(const Field<3, FieldValue<3>::Scalar> &extra_src)
206  { data_->extra_source = extra_src; }
207 
208  virtual ~DarcyLMH() override;
209 
210 
211 protected:
212  /**
213  * Returns true is the fields involved in the time term have values that makes the time term zero.
214  * For time_global==true, it returns true if there are no field descriptors in the input list, so the
215  * fields )of the time ter) have their default values for whole simulation.
216  * If time_global==false (default), only the actual values are considered.
217  */
218  virtual bool zero_time_term(bool time_global=false);
219 
220  /// Solve method common to zero_time_step and update solution.
221  void solve_nonlinear();
222 
223  /**
224  * Create and preallocate MH linear system (including matrix, rhs and solution vectors)
225  */
226  void create_linear_system(Input::AbstractRecord rec);
227 
228  /**
229  * Read initial condition into solution vector.
230  * Must be called after create_linear_system.
231  *
232  * For the LMH scheme we have to be able to save edge pressures in order to
233  * restart simulation or use results of one simulation as initial condition for other one.
234  */
235  void read_initial_condition();
236 
237  /**
238  * In some circumstances, the intial condition must be processed.
239  * It is called at the end of @p read_initial_condition().
240  * This is used in Richards equation due the update of water content.
241  */
242  virtual void initial_condition_postprocess();
243 
244  /**
245  * Allocates linear system matrix for MH.
246  * TODO:
247  * - use general preallocation methods in DofHandler
248  */
249  void allocate_mh_matrix();
250 
251  /**
252  * Assembles linear system matrix for MH.
253  * Element by element assembly is done using dim-template assembly class.
254  * Assembles only steady part of the equation.
255  * TODO:
256  * - include time term - DONE
257  * - add support for Robin type sources
258  * - support for nonlinear solvers - assembly either residual vector, matrix, or both (using FADBAD++)
259  */
260  void assembly_mh_matrix(MultidimAssembly& assembler);
261 
262  void reconstruct_solution_from_schur(MultidimAssembly& assembler);
263 
264  /**
265  * Assembly or update whole linear system.
266  */
267  virtual void assembly_linear_system();
268 
269 // void set_mesh_data_for_bddc(LinSys_BDDC * bddc_ls);
270  /**
271  * Return a norm of residual vector.
272  * TODO: Introduce Equation::compute_residual() updating
273  * residual field, standard part of EqData.
274  */
275  virtual double solution_precision() const;
276 
277  /// Print darcy flow matrix in matlab format into a file.
278  void print_matlab_matrix(string matlab_file);
279 
280  /// Get vector of all DOF indices of given component (0..side, 1..element, 2..edge)
281  std::vector<int> get_component_indices_vec(unsigned int component) const;
282 
283  /// Getter for the linear system of the 2. Schur complement.
285  { return *(data_->lin_sys_schur); }
286 
287  std::shared_ptr<Balance> balance_;
288 
290 
291  int size; // global size of MH matrix
292 
294 
295  // Setting of the nonlinear solver. TODO: Move to the solver class later on.
296  double tolerance_;
297  unsigned int min_n_it_;
298  unsigned int max_n_it_;
299  unsigned int nonlinear_iteration_; //< Actual number of completed nonlinear iterations, need to pass this information into assembly.
300 
301  std::shared_ptr<EqData> data_;
302 
303  friend class DarcyFlowMHOutput;
304  //friend class P0_CouplingAssembler;
305  //friend class P1_CouplingAssembler;
306 
307 private:
308  /// Registrar of class to factory
309  static const int registrar;
310 };
311 
312 #endif //DARCY_FLOW_LMH_HH
313 //-----------------------------------------------------------------------------
314 // vim: set cindent:
315 
VectorMPI p_edge_solution_previous
void set_extra_source(const Field< 3, FieldValue< 3 >::Scalar > &extra_src)
Sets external source field (coupling with other equation).
static const int registrar
Registrar of class to factory.
TYPEDEF_ERR_INFO(EI_KeyName, const string)
VectorMPI p_edge_solution
Abstract linear system class.
Definition: balance.hh:40
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:92
std::shared_ptr< LinSys > lin_sys_schur
Definition: mesh.h:78
std::shared_ptr< SubDOFHandlerMultiDim > dh_p_
DOF handler represents DOFs of element pressure.
Mixed-hybrid model of linear Darcy flow, possibly unsteady.
Basic time management functionality for unsteady (and steady) solvers (class Equation).
Basic time management class.
DarcyFlowMHOutput * output_object
unsigned int min_n_it_
double tolerance_
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
bool data_changed_
unsigned int max_n_it_
std::shared_ptr< EqData > data_
unsigned int nonlinear_iteration_
Accessor to the polymorphic input data of a type given by an AbstracRecord object.
Definition: accessors.hh:458
void set_extra_storativity(const Field< 3, FieldValue< 3 >::Scalar > &extra_stor)
Sets external storarivity field (coupling with other equation).
EqData & data()
LinSys & lin_sys_schur()
Getter for the linear system of the 2. Schur complement.
std::shared_ptr< Balance > balance_
DECLARE_EXCEPTION(ExcWrongDefaultJSON,<< "Consistency Error: Not valid JSON of Default value "<< EI_DefaultStr::qval<< " of type "<< EI_TypeName::qval<< ";\n"<< "During declaration of the key: "<< EI_KeyName::qval)
VectorMPI p_edge_solution_previous_time
mixed-hybrid model of linear Darcy flow, possibly unsteady.
Record type proxy class.
Definition: type_record.hh:182
DECLARE_INPUT_EXCEPTION(ExcInputMessage,<< EI_Message::val)
Simple input exception that accepts just string message.
Template for classes storing finite set of named values.
std::map< LongIdx, LocalSystem > seepage_bc_systems