Flow123d  JS_before_hm-1008-g3dab983
transport.h
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 transport.h
15  * @brief
16  * @todo
17  * - remove transport_sources
18  * - in create_transport_matric_mpi, there there is condition edge_flow > ZERO
19  * this makes matrix sparser, but can lead to elements without outflow and other problems
20  * when there are big differences in fluxes, more over it doesn't work if overall flow is very small
21  */
22 
23 #ifndef TRANSPORT_H_
24 #define TRANSPORT_H_
25 
26 
27 #include <memory> // for shared_ptr
28 #include <vector> // for vector
29 #include <petscmat.h>
30 #include "fem/fe_values.hh" // for FEValues
31 #include "fields/field.hh" // for Field
32 #include "fields/bc_multi_field.hh"
33 #include "fields/field_values.hh"
34 #include "fields/multi_field.hh"
35 #include "la/vector_mpi.hh"
37 #include "input/type_base.hh" // for Array
38 #include "input/type_generic.hh" // for Instance
39 #include "input/accessors.hh"
40 #include "system/index_types.hh"
41 #include "mesh/region.hh" // for RegionSet
42 #include "petscvec.h" // for Vec, _p_Vec
43 #include "tools/time_marks.hh" // for TimeMark, TimeM...
44 #include "transport/substance.hh" // for SubstanceList
47 
48 class OutputTime;
49 class Mesh;
50 class Distribution;
51 class Balance;
52 namespace Input {
53  namespace Type {
54  class Record;
55  class Selection;
56  }
57 }
58 
59 
60 //=============================================================================
61 // TRANSPORT
62 //=============================================================================
63 
64 /**
65  * TODO:
66  * - doxy documentation
67  * - make separate method for changing time step (rescaling only), reassembly matrix only when data are changed
68  *
69  * - needs support from EqData to determine next change of the data for 1) transport matrix 2) source vector
70  * this allows us precisely choose interval where to fix timestep
71  * : field->next_change_time() - returns time jump or actual time in case of time dep. field
72  */
73 
74 
75 
76 /**
77  * Auxiliary container class for Finite element and related objects of all dimensions.
78  * Its purpose is to provide templated access to these objects, applicable in
79  * the assembling methods.
80  */
82 public:
83 
86 
87  template<unsigned int dim>
88  inline FiniteElement<dim> *fe();
89 
90  inline Quadrature &q(unsigned int dim);
91 
92  inline FEValues<3> &fe_values(unsigned int dim)
93  {
94  ASSERT_DBG( dim >= 1 && dim <= 3 );
95  return fe_values_[dim-1];
96  }
97 
98 private:
99 
100  /// Finite elements for the solution of the advection-diffusion equation.
105 
106  /// Quadratures used in assembling methods.
108 
109  /// FESideValues objects for side flux calculating.
110  FEValues<3> fe_values_[3];
111 };
112 
113 
114 /**
115  * Class that implements explicit finite volumes scheme with upwind. The timestep is given by CFL condition.
116  *
117  */
119 public:
120  class EqData : public TransportEqData {
121  public:
122 
123  EqData();
124  virtual ~EqData() {};
125 
126  /// Override generic method in order to allow specification of the boundary conditions through the old bcd files.
127  RegionSet read_boundary_list_item(Input::Record rec);
128 
129  /**
130  * Boundary conditions (Dirichlet) for concentrations.
131  * They are applied only on water inflow part of the boundary.
132  */
134 
135  /// Initial concentrations.
137 
140 
141  MultiField<3, FieldValue<3>::Scalar> conc_mobile; ///< Calculated concentrations in the mobile zone.
142  FieldFEScalarVec conc_mobile_fe; ///< Underlaying FieldFE for each substance of conc_mobile.
143 
144  /// Fields indended for output, i.e. all input fields plus those representing solution.
146  };
147 
148 
150 
151  static const Input::Type::Record & get_input_type();
152 
153  static const IT::Selection & get_output_selection();
154 
155  /**
156  * Constructor.
157  */
158  ConvectionTransport(Mesh &init_mesh, const Input::Record in_rec);
159  /**
160  * TODO: destructor
161  */
162  virtual ~ConvectionTransport();
163 
164  void initialize() override;
165 
166  /**
167  * Initialize solution at zero time.
168  */
169  void zero_time_step() override;
170  /**
171  * Evaluates CFL condition.
172  * Assembles the transport matrix and vector (including sources, bc terms).
173  * @param time_constraint is the value CFL constraint (return parameter)
174  * @return true if CFL is changed since previous step, false otherwise
175  */
176  bool evaluate_time_constraint(double &time_constraint) override;
177  /**
178  * Calculates one time step of explicit transport.
179  */
180  void update_solution() override;
181 
182  /** Compute P0 interpolation of the solution (used reaction term).
183  * Empty - solution is already P0 interpolation.
184  */
185  void compute_p0_interpolation() override {};
186 
187  /// Not used in this class.
188  void update_after_reactions(bool) override {};
189 
190  /**
191  * Set time interval which is considered as one time step by TransportOperatorSplitting.
192  * In particular the velocity field doesn't change over this interval.
193  *
194  * Dependencies:
195  *
196  * velocity, porosity -> matrix, source_vector
197  * matrix -> time_step
198  *
199  * data_read_times -> time_step (not necessary if we won't stick to jump times)
200  * data -> source_vector
201  * time_step -> scaling
202  *
203  *
204  *
205  */
206  void set_target_time(double target_time) override;
207 
208  /**
209  * Use Balance object from upstream equation (e.g. in various couplings) instead of own instance.
210  */
211  void set_balance_object(std::shared_ptr<Balance> balance) override;
212 
214  { return subst_idx; }
215 
216  /**
217  * @brief Write computed fields.
218  */
219  virtual void output_data() override;
220 
221  void set_output_stream(std::shared_ptr<OutputTime> stream) override
222  { output_stream_ = stream; }
223 
224 
225  /**
226  * Getters.
227  */
228  inline std::shared_ptr<OutputTime> output_stream() override
229  { return output_stream_; }
230 
231  /// Getter for P0 interpolation by FieldFE.
232  FieldFEScalarVec& get_p0_interpolation() override;
233 
234  Vec get_component_vec(unsigned int sbi) override;
235 
236  void get_par_info(LongIdx * &el_4_loc, Distribution * &el_ds) override;
237 
238  LongIdx *get_row_4_el() override;
239 
240  /// Returns number of transported substances.
241  inline unsigned int n_substances() override
242  { return substances_.size(); }
243 
244  /// Returns reference to the vector of substance names.
245  inline SubstanceList &substances() override
246  { return substances_; }
247 
248 private:
249 
250  /**
251  * Assembly convection term part of the matrix and boundary matrix for application of boundary conditions.
252  *
253  * Discretization of the convection term use explicit time scheme and finite volumes with full upwinding.
254  * We count on with exchange between dimensions and mixing on edges where more then two elements connect (can happen for 2D and 1D elements in
255  * 3D embedding space)
256  *
257  * In order to get multiplication matrix for explicit transport one have to scale the convection part by the acctual time step and
258  * add time term, i. e. unit matrix (see. transport_matrix_step_mpi)
259  *
260  * Updates CFL time step constrain.
261  */
262  void create_transport_matrix_mpi();
263  void create_mass_matrix();
264 
265  void make_transport_partitioning(); //
266  void set_initial_condition();
267  void read_concentration_sources();
268  void set_boundary_conditions();
269 
270  /** @brief Assembles concentration sources for each substance.
271  * note: the source of concentration is multiplied by time interval (gives the mass, not the flow like before)
272  */
273  void compute_concentration_sources();
274 
275  /**
276  * Finish explicit transport matrix (time step scaling)
277  */
278  void transport_matrix_step_mpi(double time_step); //
279 
280  void alloc_transport_vectors();
281  void alloc_transport_structs_mpi();
282 
283  /**
284  * @brief Wrapper of following method, call side_flux with correct template parameter.
285  */
286  double side_flux(const DHCellSide &cell_side);
287 
288  /**
289  * @brief Calculate flux on side of given element specified by dimension.
290  */
291  template<unsigned int dim>
292  double calculate_side_flux(const DHCellSide &cell);
293 
294 
295 
296  /// Registrar of class to factory
297  static const int registrar;
298 
299  /**
300  * Parameters of the equation, some are shared with other implementations since EqData is derived from TransportBase::TransportEqData
301  */
303 
304  //@{
305  /**
306  * Flag indicates the state of object (transport matrix or source or boundary term).
307  * If false, the object is freshly assembled and not rescaled.
308  * If true, the object is scaled (not necessarily with the current time step).
309  */
310  bool is_convection_matrix_scaled, is_src_term_scaled, is_bc_term_scaled;
311 
312  /// Flag indicates that porosity or cross_section changed during last time.
314  //@}
315 
316  double **sources_corr;
318 
319 
320  TimeMark::Type target_mark_type; ///< TimeMark type for time marks denoting end of every time interval where transport matrix remains constant.
321  double cfl_max_step; ///< Time step constraint coming from CFL condition.
322 
323  Vec vcfl_flow_, ///< Parallel vector for flow contribution to CFL condition.
324  vcfl_source_; ///< Parallel vector for source term contribution to CFL condition.
325  double *cfl_flow_, *cfl_source_;
326 
327 
328  VecScatter vconc_out_scatter;
329  Mat tm; // PETSc transport matrix
330  Vec mass_diag; // diagonal entries in pass matrix (cross_section * porosity)
331  Vec vpmass_diag; // diagonal entries in mass matrix from last time (cross_section * porosity)
332  Vec *v_tm_diag; // additions to PETSC transport matrix on the diagonal - from sources (for each substance)
333  double **tm_diag;
334 
335  /// Time when the transport matrix was created.
336  /// TODO: when we have our own classes for LA objects, we can use lazy dependence to check
337  /// necessity for matrix update
339  double transport_bc_time; ///< Time of the last update of the boundary condition terms.
340 
341  ///
342  Vec *vpconc; // previous concentration vector
343  Vec *bcvcorr; // boundary condition correction vector
345  double **cumulative_corr;
346 
347  /// Record with input specification.
349 
350  std::shared_ptr<OutputTime> output_stream_;
351 
352 
356 
357  /// Transported substances.
359 
360  /**
361  * Temporary solution how to pass velocity field form the flow model.
362  * TODO: introduce FieldDiscrete -containing true DOFHandler and data vector and pass such object together with other
363  * data. Possibly make more general set_data method, allowing setting data given by name. needs support from EqDataBase.
364  */
365  std::shared_ptr<DOFHandlerMultiDim> dh_;
366 
367  /// List of indices used to call balance methods for a set of quantities.
369 
370  /// Finite element objects
372 
374 };
375 #endif /* TRANSPORT_H_ */
bool is_mass_diag_changed
Flag indicates that porosity or cross_section changed during last time.
Definition: transport.h:313
const vector< unsigned int > & get_subst_idx() override
Return substance indices used in balance.
Definition: transport.h:213
double transport_matrix_time
Definition: transport.h:338
QGauss::array q_
Quadratures used in assembling methods.
Definition: transport.h:107
unsigned int n_substances() override
Returns number of transported substances.
Definition: transport.h:241
Abstract linear system class.
Definition: balance.hh:40
MultiField< 3, FieldValue< 3 >::Scalar > conc_mobile
Calculated concentrations in the mobile zone.
Definition: transport.h:141
Class template representing a field with values dependent on: point, element, and region...
Definition: field.hh:92
std::shared_ptr< OutputTime > output_stream_
Definition: transport.h:350
TimeMark::Type target_mark_type
TimeMark type for time marks denoting end of every time interval where transport matrix remains const...
Definition: transport.h:320
Field< 3, FieldValue< 3 >::Scalar > region_id
Definition: transport.h:138
double transport_bc_time
Time of the last update of the boundary condition terms.
Definition: transport.h:339
Coupling of a transport model with a reaction model by operator splitting.
Definition: mesh.h:78
EquationOutput output_fields
Fields indended for output, i.e. all input fields plus those representing solution.
Definition: transport.h:145
double ** sources_corr
Definition: transport.h:316
LongIdx * el_4_loc
Definition: transport.h:354
Class FEValues calculates finite element data on the actual cells such as shape function values...
BCMultiField< 3, FieldValue< 3 >::Scalar > bc_conc
Definition: transport.h:133
MultiField< 3, FieldValue< 3 >::Scalar > init_conc
Initial concentrations.
Definition: transport.h:136
const Input::Record input_rec
Record with input specification.
Definition: transport.h:348
Base class for quadrature rules on simplices in arbitrary dimensions.
Definition: quadrature.hh:48
SubstanceList & substances() override
Returns reference to the vector of substance names.
Definition: transport.h:245
Field< 3, FieldValue< 3 >::Scalar > subdomain
Definition: transport.h:139
FieldFEScalarVec conc_mobile_fe
Underlaying FieldFE for each substance of conc_mobile.
Definition: transport.h:142
std::shared_ptr< OutputTime > output_stream() override
Definition: transport.h:228
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
static const int registrar
Registrar of class to factory.
Definition: transport.h:297
SubstanceList substances_
Transported substances.
Definition: transport.h:358
FEValues< 3 > & fe_values(unsigned int dim)
Definition: transport.h:92
std::shared_ptr< DOFHandlerMultiDim > dh_
Definition: transport.h:365
The class for outputting data during time.
Definition: output_time.hh:50
vector< unsigned int > subst_idx
List of indices used to call balance methods for a set of quantities.
Definition: transport.h:368
void update_after_reactions(bool) override
Not used in this class.
Definition: transport.h:188
void compute_p0_interpolation() override
Definition: transport.h:185
std::array< QGauss, 4 > array
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
double * cfl_source_
Definition: transport.h:325
void set_output_stream(std::shared_ptr< OutputTime > stream) override
Setter for output stream.
Definition: transport.h:221
VecScatter vconc_out_scatter
Definition: transport.h:328
FiniteElement< 1 > * fe1_
Definition: transport.h:102
double ** tm_diag
Definition: transport.h:333
ConcentrationTransportBase FactoryBaseType
Definition: transport.h:149
Classes for storing substance data.
#define ASSERT_DBG(expr)
Vec vcfl_source_
Parallel vector for source term contribution to CFL condition.
Definition: transport.h:323
Definitions of particular quadrature rules on simplices.
double ** cumulative_corr
Definition: transport.h:345
Record type proxy class.
Definition: type_record.hh:182
FETransportObjects feo_
Finite element objects.
Definition: transport.h:371
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:87
Abstract class for the description of a general finite element on a reference simplex in dim dimensio...
FiniteElement< 3 > * fe3_
Definition: transport.h:104
FiniteElement< 0 > * fe0_
Finite elements for the solution of the advection-diffusion equation.
Definition: transport.h:101
Distribution * el_ds
Definition: transport.h:355
Template for classes storing finite set of named values.
Side accessor allows to iterate over sides of DOF handler cell.
LongIdx * row_4_el
Definition: transport.h:353
double cfl_max_step
Time step constraint coming from CFL condition.
Definition: transport.h:321
FiniteElement< 2 > * fe2_
Definition: transport.h:103