Flow123d  release_3.0.0-968-gc87a28e79
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 "fields/field.hh" // for Field
31 #include "fields/bc_multi_field.hh"
32 #include "fields/field_values.hh"
33 #include "fields/multi_field.hh"
34 #include "la/vector_mpi.hh"
36 #include "input/type_base.hh" // for Array
37 #include "input/type_generic.hh" // for Instance
38 #include "input/accessors.hh"
39 #include "mesh/long_idx.hh"
40 #include "mesh/region.hh" // for RegionSet
41 #include "petscvec.h" // for Vec, _p_Vec
42 #include "tools/time_marks.hh" // for TimeMark, TimeM...
43 #include "transport/substance.hh" // for SubstanceList
45 
46 class OutputTime;
47 class Mesh;
48 class Distribution;
49 class Balance;
50 class MH_DofHandler;
51 namespace Input {
52  namespace Type {
53  class Record;
54  class Selection;
55  }
56 }
57 template <int spacedim, class Value> class FieldFE;
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  * Class that implements explicit finite volumes scheme with upwind. The timestep is given by CFL condition.
78  *
79  */
81 public:
82 
83  class EqData : public TransportEqData {
84  public:
85 
86  EqData();
87  virtual ~EqData() {};
88 
89  /// Override generic method in order to allow specification of the boundary conditions through the old bcd files.
91 
92  /**
93  * Boundary conditions (Dirichlet) for concentrations.
94  * They are applied only on water inflow part of the boundary.
95  */
97 
98  /// Initial concentrations.
100 
103  MultiField<3, FieldValue<3>::Scalar> conc_mobile; ///< Calculated concentrations in the mobile zone.
104 
105 
106  /// Fields indended for output, i.e. all input fields plus those representing solution.
108  };
109 
110 
112 
113  static const Input::Type::Record & get_input_type();
114 
115  static const IT::Selection & get_output_selection();
116 
117  /**
118  * Constructor.
119  */
120  ConvectionTransport(Mesh &init_mesh, const Input::Record in_rec);
121  /**
122  * TODO: destructor
123  */
124  virtual ~ConvectionTransport();
125 
126  void initialize() override;
127 
128  /**
129  * Initialize solution at zero time.
130  */
131  void zero_time_step() override;
132  /**
133  * Evaluates CFL condition.
134  * Assembles the transport matrix and vector (including sources, bc terms).
135  * @param time_constraint is the value CFL constraint (return parameter)
136  * @return true if CFL is changed since previous step, false otherwise
137  */
138  bool evaluate_time_constraint(double &time_constraint) override;
139  /**
140  * Calculates one time step of explicit transport.
141  */
142  void update_solution() override;
143 
144  void calculate_concentration_matrix() override {};
145 
146  void update_after_reactions(bool solution_changed) override {};
147 
148  /**
149  * Set time interval which is considered as one time step by TransportOperatorSplitting.
150  * In particular the velocity field doesn't change over this interval.
151  *
152  * Dependencies:
153  *
154  * velocity, porosity -> matrix, source_vector
155  * matrix -> time_step
156  *
157  * data_read_times -> time_step (not necessary if we won't stick to jump times)
158  * data -> source_vector
159  * time_step -> scaling
160  *
161  *
162  *
163  */
164  void set_target_time(double target_time) override;
165 
166  /**
167  * Use Balance object from upstream equation (e.g. in various couplings) instead of own instance.
168  */
169  void set_balance_object(std::shared_ptr<Balance> balance) override;
170 
172  { return subst_idx; }
173 
174  /**
175  * @brief Write computed fields.
176  */
177  virtual void output_data() override;
178 
179  inline void set_velocity_field(const MH_DofHandler &dh) override
180  { mh_dh=&dh; }
181 
182  void set_output_stream(std::shared_ptr<OutputTime> stream) override
183  { output_stream_ = stream; }
184 
185 
186  /**
187  * Getters.
188  */
189  inline std::shared_ptr<OutputTime> output_stream() override
190  { return output_stream_; }
191 
192  double **get_concentration_matrix() override;
193 
194  const Vec &get_solution(unsigned int sbi) override
195  { return vconc[sbi]; }
196 
197  void get_par_info(LongIdx * &el_4_loc, Distribution * &el_ds) override;
198 
199  LongIdx *get_row_4_el() override;
200 
201  /// Returns number of transported substances.
202  inline unsigned int n_substances() override
203  { return substances_.size(); }
204 
205  /// Returns reference to the vector of substance names.
206  inline SubstanceList &substances() override
207  { return substances_; }
208 
209 private:
210 
211  /**
212  * Assembly convection term part of the matrix and boundary matrix for application of boundary conditions.
213  *
214  * Discretization of the convection term use explicit time scheme and finite volumes with full upwinding.
215  * 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
216  * 3D embedding space)
217  *
218  * In order to get multiplication matrix for explicit transport one have to scale the convection part by the acctual time step and
219  * add time term, i. e. unit matrix (see. transport_matrix_step_mpi)
220  *
221  * Updates CFL time step constrain.
222  */
224  void create_mass_matrix();
225 
226  void make_transport_partitioning(); //
227  void set_initial_condition();
230 
231  /** @brief Assembles concentration sources for each substance.
232  * note: the source of concentration is multiplied by time interval (gives the mass, not the flow like before)
233  */
235 
236  /**
237  * Finish explicit transport matrix (time step scaling)
238  */
239  void transport_matrix_step_mpi(double time_step); //
240 
243 
244  /**
245  * Communicate parallel concentration vectors into sequential output vector.
246  */
247  void output_vector_gather();
248 
249 
250 
251  /// Registrar of class to factory
252  static const int registrar;
253 
254  /**
255  * Parameters of the equation, some are shared with other implementations since EqData is derived from TransportBase::TransportEqData
256  */
258 
259  //@{
260  /**
261  * Flag indicates the state of object (transport matrix or source or boundary term).
262  * If false, the object is freshly assembled and not rescaled.
263  * If true, the object is scaled (not necessarily with the current time step).
264  */
266 
267  /// Flag indicates that porosity or cross_section changed during last time.
269  //@}
270 
271  double **sources_corr;
273 
274 
275  TimeMark::Type target_mark_type; ///< TimeMark type for time marks denoting end of every time interval where transport matrix remains constant.
276  double cfl_max_step; ///< Time step constraint coming from CFL condition.
277 
278  Vec vcfl_flow_, ///< Parallel vector for flow contribution to CFL condition.
279  vcfl_source_; ///< Parallel vector for source term contribution to CFL condition.
281 
282 
283  VecScatter vconc_out_scatter;
284  Mat tm; // PETSc transport matrix
285  Vec mass_diag; // diagonal entries in pass matrix (cross_section * porosity)
286  Vec vpmass_diag; // diagonal entries in mass matrix from last time (cross_section * porosity)
287  Vec *v_tm_diag; // additions to PETSC transport matrix on the diagonal - from sources (for each substance)
288  double **tm_diag;
289 
290  /// Time when the transport matrix was created.
291  /// TODO: when we have our own classes for LA objects, we can use lazy dependence to check
292  /// necessity for matrix update
294  double transport_bc_time; ///< Time of the last update of the boundary condition terms.
295 
296  /// Concentration vectors for mobile phase.
297  Vec *vconc; // concentration vector
298  /// Concentrations for phase, substance, element
299  double **conc;
300 
301  ///
302  Vec *vpconc; // previous concentration vector
303  Vec *bcvcorr; // boundary condition correction vector
305  double **cumulative_corr;
306 
308 
309  // Temporary objects holding pointers to appropriate FieldFE
310  // TODO remove after final fix of equations
311  /// Fields correspond with \p out_conc.
313 
314  /// Record with input specification.
316 
317  std::shared_ptr<OutputTime> output_stream_;
318 
319 
323 
324  /// Transported substances.
326 
327  /**
328  * Temporary solution how to pass velocity field form the flow model.
329  * TODO: introduce FieldDiscrete -containing true DOFHandler and data vector and pass such object together with other
330  * data. Possibly make more general set_data method, allowing setting data given by name. needs support from EqDataBase.
331  */
333 
334  /// List of indices used to call balance methods for a set of quantities.
336 
338 };
339 #endif /* TRANSPORT_H_ */
ConvectionTransport::set_balance_object
void set_balance_object(std::shared_ptr< Balance > balance) override
Definition: transport.cc:877
ConvectionTransport::out_conc
std::vector< VectorMPI > out_conc
Definition: transport.h:307
ConvectionTransport::EqData::conc_mobile
MultiField< 3, FieldValue< 3 >::Scalar > conc_mobile
Calculated concentrations in the mobile zone.
Definition: transport.h:103
ConvectionTransport::is_bc_term_scaled
bool is_bc_term_scaled
Definition: transport.h:265
ConvectionTransport::vcfl_flow_
Vec vcfl_flow_
Parallel vector for flow contribution to CFL condition.
Definition: transport.h:278
ConvectionTransport::mass_diag
Vec mass_diag
Definition: transport.h:285
ConvectionTransport::FactoryBaseType
ConcentrationTransportBase FactoryBaseType
Definition: transport.h:111
ConvectionTransport::create_mass_matrix
void create_mass_matrix()
Definition: transport.cc:693
ConvectionTransport::EqData::init_conc
MultiField< 3, FieldValue< 3 >::Scalar > init_conc
Initial concentrations.
Definition: transport.h:99
ConvectionTransport::row_4_el
LongIdx * row_4_el
Definition: transport.h:320
vector_mpi.hh
ConvectionTransport::EqData::read_boundary_list_item
RegionSet read_boundary_list_item(Input::Record rec)
Override generic method in order to allow specification of the boundary conditions through the old bc...
ConvectionTransport::get_input_type
static const Input::Type::Record & get_input_type()
Definition: transport.cc:67
ConvectionTransport::substances
SubstanceList & substances() override
Returns reference to the vector of substance names.
Definition: transport.h:206
Input
Abstract linear system class.
Definition: balance.hh:37
ConvectionTransport::output_stream
std::shared_ptr< OutputTime > output_stream() override
Definition: transport.h:189
SubstanceList::size
unsigned int size() const
Definition: substance.hh:87
SubstanceList
Definition: substance.hh:70
TransportEqData
Definition: transport_operator_splitting.hh:147
ConvectionTransport::transport_matrix_time
double transport_matrix_time
Definition: transport.h:293
Balance
Definition: balance.hh:116
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
ConvectionTransport::output_field_ptr
std::vector< std::shared_ptr< FieldFE< 3, FieldValue< 3 >::Scalar > > > output_field_ptr
Fields correspond with out_conc.
Definition: transport.h:312
ConvectionTransport::sources_corr
double ** sources_corr
Definition: transport.h:271
ConvectionTransport::get_solution
const Vec & get_solution(unsigned int sbi) override
Return PETSc vector with solution for sbi-th substance.
Definition: transport.h:194
ConvectionTransport::el_ds
Distribution * el_ds
Definition: transport.h:322
substance.hh
Classes for storing substance data.
ConvectionTransport::~ConvectionTransport
virtual ~ConvectionTransport()
Definition: transport.cc:197
std::vector< Region >
ConvectionTransport::get_row_4_el
LongIdx * get_row_4_el() override
Return global array of order of elements within parallel vector.
Definition: transport.cc:851
ConvectionTransport::n_substances
unsigned int n_substances() override
Returns number of transported substances.
Definition: transport.h:202
ConvectionTransport::output_vector_gather
void output_vector_gather()
Definition: transport.cc:821
ConvectionTransport::vcumulative_corr
Vec * vcumulative_corr
Definition: transport.h:304
ConvectionTransport::set_velocity_field
void set_velocity_field(const MH_DofHandler &dh) override
Pass velocity from flow to transport.
Definition: transport.h:179
ConvectionTransport::output_stream_
std::shared_ptr< OutputTime > output_stream_
Definition: transport.h:317
type_base.hh
ConvectionTransport::transport_bc_time
double transport_bc_time
Time of the last update of the boundary condition terms.
Definition: transport.h:294
ConvectionTransport::is_mass_diag_changed
bool is_mass_diag_changed
Flag indicates that porosity or cross_section changed during last time.
Definition: transport.h:268
ConvectionTransport::cumulative_corr
double ** cumulative_corr
Definition: transport.h:305
ConvectionTransport::vconc
Vec * vconc
Concentration vectors for mobile phase.
Definition: transport.h:297
ConvectionTransport::read_concentration_sources
void read_concentration_sources()
equation_output.hh
Distribution
Definition: distribution.hh:50
ConvectionTransport::EqData
Definition: transport.h:83
ConvectionTransport::alloc_transport_structs_mpi
void alloc_transport_structs_mpi()
Definition: transport.cc:298
ConvectionTransport::EqData::EqData
EqData()
Definition: transport.cc:83
ConvectionTransport::bcvcorr
Vec * bcvcorr
Definition: transport.h:303
ConvectionTransport::is_convection_matrix_scaled
bool is_convection_matrix_scaled
Definition: transport.h:265
ConvectionTransport::cfl_source_
double * cfl_source_
Definition: transport.h:280
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
ConvectionTransport::data_
EqData data_
Definition: transport.h:257
ConvectionTransport::input_rec
const Input::Record input_rec
Record with input specification.
Definition: transport.h:315
ConvectionTransport::evaluate_time_constraint
bool evaluate_time_constraint(double &time_constraint) override
Definition: transport.cc:503
ConvectionTransport::registrar
static const int registrar
Registrar of class to factory.
Definition: transport.h:252
ConvectionTransport::zero_time_step
void zero_time_step() override
Definition: transport.cc:478
type_generic.hh
ConvectionTransport::v_tm_diag
Vec * v_tm_diag
Definition: transport.h:287
ConvectionTransport::set_initial_condition
void set_initial_condition()
Definition: transport.cc:247
ConvectionTransport::mh_dh
const MH_DofHandler * mh_dh
Definition: transport.h:332
accessors.hh
EquationOutput
Definition: equation_output.hh:40
ConvectionTransport::vpmass_diag
Vec vpmass_diag
Definition: transport.h:286
ConvectionTransport::vcfl_source_
Vec vcfl_source_
Parallel vector for source term contribution to CFL condition.
Definition: transport.h:279
ConvectionTransport::output_data
virtual void output_data() override
Write computed fields.
Definition: transport.cc:857
bc_multi_field.hh
ConvectionTransport::EqData::output_fields
EquationOutput output_fields
Fields indended for output, i.e. all input fields plus those representing solution.
Definition: transport.h:107
field_values.hh
TimeMark::Type
Definition: time_marks.hh:60
ConvectionTransport::subst_idx
vector< unsigned int > subst_idx
List of indices used to call balance methods for a set of quantities.
Definition: transport.h:335
ConvectionTransport::EqData::bc_conc
BCMultiField< 3, FieldValue< 3 >::Scalar > bc_conc
Definition: transport.h:96
ConvectionTransport::v_sources_corr
Vec * v_sources_corr
Definition: transport.h:272
OutputTime
The class for outputting data during time.
Definition: output_time.hh:50
ConvectionTransport
Definition: transport.h:80
ConvectionTransport::conc
double ** conc
Concentrations for phase, substance, element.
Definition: transport.h:299
Input::Type::Selection
Template for classes storing finite set of named values.
Definition: type_selection.hh:65
ConvectionTransport::substances_
SubstanceList substances_
Transported substances.
Definition: transport.h:325
ConvectionTransport::calculate_concentration_matrix
void calculate_concentration_matrix() override
Calculate the array of concentrations per element (for reactions).
Definition: transport.h:144
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
ConvectionTransport::tm
Mat tm
Definition: transport.h:284
ConvectionTransport::EqData::subdomain
Field< 3, FieldValue< 3 >::Scalar > subdomain
Definition: transport.h:102
FieldFE
Definition: field.hh:56
BCMultiField
Definition: bc_multi_field.hh:29
ConcentrationTransportBase
Definition: transport_operator_splitting.hh:61
ConvectionTransport::update_after_reactions
void update_after_reactions(bool solution_changed) override
Perform changes to transport solution after reaction step.
Definition: transport.h:146
ConvectionTransport::is_src_term_scaled
bool is_src_term_scaled
Definition: transport.h:265
Mesh
Definition: mesh.h:80
ConvectionTransport::make_transport_partitioning
void make_transport_partitioning()
Definition: transport.cc:170
ConvectionTransport::get_concentration_matrix
double ** get_concentration_matrix() override
Getter for array of concentrations per element.
Definition: transport.cc:837
TransportOperatorSplitting
Coupling of a transport model with a reaction model by operator splitting.
Definition: transport_operator_splitting.hh:213
multi_field.hh
ConvectionTransport::target_mark_type
TimeMark::Type target_mark_type
TimeMark type for time marks denoting end of every time interval where transport matrix remains const...
Definition: transport.h:275
ConvectionTransport::el_4_loc
LongIdx * el_4_loc
Definition: transport.h:321
MultiField
Class for representation of a vector of fields of the same physical quantity.
Definition: multi_field.hh:85
ConvectionTransport::update_solution
void update_solution() override
Definition: transport.cc:568
region.hh
ConvectionTransport::get_subst_idx
const vector< unsigned int > & get_subst_idx() override
Return substance indices used in balance.
Definition: transport.h:171
ConvectionTransport::compute_concentration_sources
void compute_concentration_sources()
Assembles concentration sources for each substance. note: the source of concentration is multiplied b...
Definition: transport.cc:419
long_idx.hh
ConvectionTransport::ConvectionTransport
ConvectionTransport(Mesh &init_mesh, const Input::Record in_rec)
Definition: transport.cc:111
ConvectionTransport::get_par_info
void get_par_info(LongIdx *&el_4_loc, Distribution *&el_ds) override
Return array of indices of local elements and parallel distribution of elements.
Definition: transport.cc:841
ConvectionTransport::set_target_time
void set_target_time(double target_time) override
Definition: transport.cc:670
ConvectionTransport::get_output_selection
static const IT::Selection & get_output_selection()
ConvectionTransport::set_output_stream
void set_output_stream(std::shared_ptr< OutputTime > stream) override
Setter for output stream.
Definition: transport.h:182
time_marks.hh
ConvectionTransport::alloc_transport_vectors
void alloc_transport_vectors()
Definition: transport.cc:264
ConvectionTransport::EqData::~EqData
virtual ~EqData()
Definition: transport.h:87
ConvectionTransport::vconc_out_scatter
VecScatter vconc_out_scatter
Definition: transport.h:283
Field
Class template representing a field with values dependent on: point, element, and region.
Definition: field.hh:83
ConvectionTransport::tm_diag
double ** tm_diag
Definition: transport.h:288
ConvectionTransport::vpconc
Vec * vpconc
Definition: transport.h:302
ConvectionTransport::cfl_max_step
double cfl_max_step
Time step constraint coming from CFL condition.
Definition: transport.h:276
ConvectionTransport::initialize
void initialize() override
Definition: transport.cc:128
ConvectionTransport::set_boundary_conditions
void set_boundary_conditions()
Definition: transport.cc:353
transport_operator_splitting.hh
ConvectionTransport::cfl_flow_
double * cfl_flow_
Definition: transport.h:280
MH_DofHandler
Definition: mh_dofhandler.hh:43
field.hh
ConvectionTransport::EqData::region_id
Field< 3, FieldValue< 3 >::Scalar > region_id
Definition: transport.h:101
ConvectionTransport::transport_matrix_step_mpi
void transport_matrix_step_mpi(double time_step)
ConvectionTransport::create_transport_matrix_mpi
void create_transport_matrix_mpi()
Definition: transport.cc:726