Flow123d  JS_before_hm-1881-gd692239a6
assembly_richards.hh
Go to the documentation of this file.
1 /*
2  * assembly_lmh.hh
3  *
4  * Created on: Apr 30, 2016
5  * Author: jb
6  */
7 
8 #ifndef SRC_FLOW_ASSEMBLY_LMH_HH_
9 #define SRC_FLOW_ASSEMBLY_LMH_HH_
10 
11 #include "system/index_types.hh"
12 #include "flow/assembly_lmh.hh"
13 #include "soil_models.hh"
14 #include "coupling/balance.hh"
15 
16 #include "badiff.h"
17 
18 
19 /**
20  * Prove of concept for general assembly scheme.
21  * Ideas:
22  * - Local assembly class for given DIM provides assembly methods for:
23  * - local assembly on elements of dimension DIM
24  * - local assembly on edges/faces of neighbouring elemnts of dimension DIM
25  * - local assembly of interactiong elements DIM and OTHER_DIM with OTHER_DIM < DIM
26  * - Such class template would be necessary to create particular instance of Assembler class, that
27  * takes job of pass through the mesh, precomputing necessary fields, etc.
28  * This allows to manage order of assembled entities in arbitrary way in order to use caches and vectorisation efficiently.
29  * - Separate local assembly template would be necessary for every pass through the mesh, but it may delegate its actions
30  * to a single general local assembly class.
31  * - The local assembly class gets an Accessor object for particular domain of the assembly, i.e. ElementAccessor for element assembly,
32  * FaceAccessor for interface intergrals assembly, and InteractionAccessor for assembly of interaction of two elements, e.g. for XFEM
33  * there may be an interaction between every 3D element within the enrichment with appropriate 1D elemnt on the well.
34  * These accessors provides methods to access fields values as well as the DOF indices on the element(s).
35  * - Results of the assembly are passed to the global linear algebra objects collected in the RichardSystem class, global indices
36  * (which are still local indicies of the single process) provided by accessors are used.
37  *
38  *
39  * TODO:
40  * - finish functional Richards model
41  * - move whole internals of the assembly_mh_matrix into local assembly classes
42  * - mean while: imporve accessors, imporve mesh and DOF handler interface; possibly create new mesh implementation used in Darcy first and then
43  * apply it to other equations
44  */
45 
46 
47 template<int dim>
48 class AssemblyRichards : public AssemblyLMH<dim> {
49 public:
50 
51  typedef std::shared_ptr<RichardsLMH::EqData> AssemblyDataPtrRichards;
52 
54  : AssemblyLMH<dim>(data),
55  ad_(data),
56  genuchten_on(false),
57  cross_section(1.0),
58  cr_disc_dofs(dim+1)
59  {}
60 
61 protected:
62  void reset_soil_model(const DHCellAccessor& dh_cell) {
63  ElementAccessor<3> ele = dh_cell.elm();
64  genuchten_on = (ad_->genuchten_p_head_scale.field_result({ele.region()}) != result_zeros);
65  if (genuchten_on) {
66  SoilData soil_data;
67  soil_data.n = ad_->genuchten_n_exponent.value(ele.centre(), ele);
68  soil_data.alpha = ad_->genuchten_p_head_scale.value(ele.centre(), ele);
69  soil_data.Qr = ad_->water_content_residual.value(ele.centre(), ele);
70  soil_data.Qs = ad_->water_content_saturated.value(ele.centre(), ele);
71  soil_data.Ks = ad_->conductivity.value(ele.centre(), ele);
72  //soil_data.cut_fraction = 0.99; // set by model
73 
74  ad_->soil_model_->reset(soil_data);
75  }
76  }
77 
79  {
80  double conductivity = 0;
81  if (genuchten_on) {
82  for (unsigned int i=0; i<ele->n_sides(); i++)
83  {
84  double phead = ad_->p_edge_solution.get( this->loc_schur_.row_dofs[i] );
85  conductivity += ad_->soil_model_->conductivity(phead);
86  }
87  conductivity /= ele->n_sides();
88  }
89  else {
90  conductivity = this->ad_->conductivity.value(ele.centre(), ele);
91  }
92  return conductivity;
93  }
94 
95  void update_water_content(const DHCellAccessor& dh_cell) {
96 
97  // dof indices for edge pressure must be updated
98  // since update_water_content is called also outside assemble cycle (init condition)
99  update_dofs(dh_cell);
100 
101  reset_soil_model(dh_cell);
102  const ElementAccessor<3> ele = dh_cell.elm();
103  double storativity = ad_->storativity.value(ele.centre(), ele)
104  + ad_->extra_storativity.value(ele.centre(), ele);
105  VectorMPI water_content_vec = ad_->water_content_ptr->vec();
106 
107  for (unsigned int i=0; i<ele->n_sides(); i++) {
108  double capacity = 0;
109  double water_content = 0;
110  double phead = ad_->p_edge_solution.get( edge_indices_[i] );
111  if (genuchten_on) {
112 
113  fadbad::B<double> x_phead(phead);
114  fadbad::B<double> evaluated( ad_->soil_model_->water_content_diff(x_phead) );
115  evaluated.diff(0,1);
116  water_content = evaluated.val();
117  capacity = x_phead.d(0);
118  }
119  ad_->capacity.set( cr_disc_dofs[i], capacity + storativity );
120  water_content_vec.set( cr_disc_dofs[i], water_content + storativity * phead);
121  }
122  }
123 
124  void assemble_sides(const DHCellAccessor& dh_cell) override
125  {
126  reset_soil_model(dh_cell);
127  const ElementAccessor<3> ele = dh_cell.elm();
128  cross_section = ad_->cross_section.value(ele.centre(), ele);
129 
130  double conductivity = compute_conductivity(ele);
131  double scale = 1 / cross_section / conductivity;
132  this->assemble_sides_scale(dh_cell,scale);
133  }
134 
135  /***
136  * Called from assembly_local_matrix, assumes precomputed:
137  * cross_section, genuchten_on, soil_model
138  */
139  void assemble_source_term(const DHCellAccessor& dh_cell) override
140  {
141  update_water_content(dh_cell);
142  const ElementAccessor<3> ele = dh_cell.elm();
143 
144  // set lumped source
145  double diagonal_coef = ele.measure() * cross_section / ele->n_sides();
146  double source_diagonal = diagonal_coef *
147  ( ad_->water_source_density.value(ele.centre(), ele)
148  + ad_->extra_source.value(ele.centre(), ele));
149 
150  VectorMPI water_content_vec = ad_->water_content_ptr->vec();
151 
152  for (unsigned int i=0; i<ele->n_sides(); i++)
153  {
154 
155  const int local_side = cr_disc_dofs[i];
156  if (this->dirichlet_edge[i] == 0) {
157 
158  double capacity = ad_->capacity.get(local_side);
159  double water_content_diff = -water_content_vec.get(local_side) + ad_->water_content_previous_time.get(local_side);
160  double mass_diagonal = diagonal_coef * capacity;
161 
162  /*
163  DebugOut().fmt("w diff: {:g} mass: {:g} w prev: {:g} w time: {:g} c: {:g} p: {:g} z: {:g}",
164  water_content_diff,
165  mass_diagonal * ad_->p_edge_solution[local_edge],
166  -ad_->water_content_previous_it[local_side],
167  ad_->water_content_previous_time[local_side],
168  capacity,
169  ad_->p_edge_solution[local_edge],
170  ele.centre()[0] );
171  */
172 
173 
174  double mass_rhs = mass_diagonal * ad_->p_edge_solution.get( this->loc_schur_.row_dofs[i] ) / ad_->time_step_
175  + diagonal_coef * water_content_diff / ad_->time_step_;
176 
177 // DBGCOUT(<< "source [" << loc_system_.row_dofs[this->loc_edge_dofs[i]] << ", " << loc_system_.row_dofs[this->loc_edge_dofs[i]]
178 // << "] mat: " << -mass_diagonal/this->ad_->time_step_
179 // << " rhs: " << -source_diagonal - mass_rhs
180 // << "\n");
181  this->loc_system_.add_value(this->loc_edge_dofs[i], this->loc_edge_dofs[i],
182  -mass_diagonal/ad_->time_step_,
183  -source_diagonal - mass_rhs);
184  }
185 
186  ad_->balance->add_mass_values(ad_->water_balance_idx, dh_cell, {local_side},
187  {0.0}, diagonal_coef*water_content_vec.get(local_side));
188  ad_->balance->add_source_values(ad_->water_balance_idx, ele.region().bulk_idx(),
189  {this->loc_system_.row_dofs[this->loc_edge_dofs[i]]},
190  {0},{source_diagonal});
191  }
192 
193  }
194 
195  /// Updates DoFs for edge pressure vector (dh CR) and for water content vector (dh CR_disc)
196  /// Be sure to call it before @p update_water_content().
197  void update_dofs(const DHCellAccessor& dh_cell)
198  {
199  edge_indices_ = dh_cell.cell_with_other_dh(ad_->dh_cr_.get()).get_loc_dof_indices();
200  cr_disc_dofs = dh_cell.cell_with_other_dh(ad_->dh_cr_disc_.get()).get_loc_dof_indices();
201  }
202 
204  double edge_scale, double edge_source_term) override
205  {
206  update_water_content(dh_cell);
207 
208  const ElementAccessor<3> ele = dh_cell.elm();
209 
210  VectorMPI water_content_vec = ad_->water_content_ptr->vec();
211 
212  for (unsigned int i=0; i<ele->n_sides(); i++) {
213 
214  double water_content = water_content_vec.get( cr_disc_dofs[i] );
215  double water_content_previous_time = ad_->water_content_previous_time.get( cr_disc_dofs[i] );
216 
217  solution[this->loc_side_dofs[i]]
218  += edge_source_term - edge_scale * (water_content - water_content_previous_time) / ad_->time_step_;
219  }
220 
221  IntIdx p_dof = dh_cell.cell_with_other_dh(ad_->dh_p_.get()).get_loc_dof_indices()(0);
222  ad_->conductivity_ptr->vec().set( p_dof, compute_conductivity(ele) );
223  }
224 
226 
229  LocDofVec cr_disc_dofs; ///< Dofs of discontinuous fields on element edges.
230  LocDofVec edge_indices_; ///< Dofs of discontinuous fields on element edges.
231 };
232 
233 
234 
235 #endif /* SRC_FLOW_ASSEMBLY_LMH_HH_ */
LocDofVec
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
result_zeros
@ result_zeros
Definition: field_algo_base.hh:74
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
AssemblyRichards::cr_disc_dofs
LocDofVec cr_disc_dofs
Dofs of discontinuous fields on element edges.
Definition: assembly_richards.hh:229
AssemblyRichards::compute_conductivity
double compute_conductivity(ElementAccessor< 3 > ele)
Definition: assembly_richards.hh:78
AssemblyRichards::update_dofs
void update_dofs(const DHCellAccessor &dh_cell)
Definition: assembly_richards.hh:197
AssemblyRichards
Definition: assembly_richards.hh:48
assembly_lmh.hh
SoilData::n
double n
Definition: soil_models.hh:40
IntIdx
int IntIdx
Definition: index_types.hh:25
AssemblyRichards::assemble_sides
void assemble_sides(const DHCellAccessor &dh_cell) override
Definition: assembly_richards.hh:124
SoilData::Qr
double Qr
Definition: soil_models.hh:42
AssemblyRichards::cross_section
double cross_section
Definition: assembly_richards.hh:228
AssemblyLMH::assemble_sides_scale
void assemble_sides_scale(const DHCellAccessor &dh_cell, double scale)
Definition: assembly_lmh.hh:381
ElementAccessor< 3 >
AssemblyRichards::genuchten_on
bool genuchten_on
Definition: assembly_richards.hh:227
AssemblyLMH
Definition: assembly_lmh.hh:39
AssemblyRichards::AssemblyDataPtrRichards
std::shared_ptr< RichardsLMH::EqData > AssemblyDataPtrRichards
Definition: assembly_richards.hh:51
AssemblyRichards::ad_
AssemblyDataPtrRichards ad_
Definition: assembly_richards.hh:225
AssemblyRichards::update_water_content
void update_water_content(const DHCellAccessor &dh_cell)
Updates water content in Richards.
Definition: assembly_richards.hh:95
index_types.hh
AssemblyRichards::AssemblyRichards
AssemblyRichards(AssemblyDataPtrRichards data)
Definition: assembly_richards.hh:53
VectorMPI::set
void set(unsigned int pos, double val)
Set value on given position.
Definition: vector_mpi.hh:111
DHCellAccessor::cell_with_other_dh
DHCellAccessor cell_with_other_dh(const DOFHandlerMultiDim *dh) const
Create new accessor with same local idx and given DOF handler. Actual and given DOF handler must be c...
Definition: dh_cell_accessor.hh:135
DHCellAccessor::elm
const ElementAccessor< 3 > elm() const
Return ElementAccessor to element of loc_ele_idx_.
Definition: dh_cell_accessor.hh:71
soil_models.hh
Element::n_sides
unsigned int n_sides() const
Definition: elements.h:138
DHCellAccessor
Cell accessor allow iterate over DOF handler cells.
Definition: dh_cell_accessor.hh:43
AssemblyRichards::assemble_source_term
void assemble_source_term(const DHCellAccessor &dh_cell) override
Definition: assembly_richards.hh:139
AssemblyRichards::postprocess_velocity_specific
void postprocess_velocity_specific(const DHCellAccessor &dh_cell, arma::vec &solution, double edge_scale, double edge_source_term) override
Definition: assembly_richards.hh:203
ElementAccessor::region
Region region() const
Definition: accessors.hh:201
SoilData::alpha
double alpha
Definition: soil_models.hh:41
SoilData::Ks
double Ks
Definition: soil_models.hh:45
DHCellAccessor::get_loc_dof_indices
LocDofVec get_loc_dof_indices() const
Returns the local indices of dofs associated to the cell on the local process.
Definition: dh_cell_accessor.hh:88
SoilData
Definition: soil_models.hh:39
AssemblyRichards::edge_indices_
LocDofVec edge_indices_
Dofs of discontinuous fields on element edges.
Definition: assembly_richards.hh:230
VectorMPI::get
double get(unsigned int pos) const
Return value on given position.
Definition: vector_mpi.hh:105
balance.hh
AssemblyRichards::reset_soil_model
void reset_soil_model(const DHCellAccessor &dh_cell)
Definition: assembly_richards.hh:62
VectorMPI
Definition: vector_mpi.hh:43
RegionIdx::bulk_idx
unsigned int bulk_idx() const
Returns index of the region in the bulk set.
Definition: region.hh:91
SoilData::Qs
double Qs
Definition: soil_models.hh:43
ElementAccessor::measure
double measure() const
Computes the measure of the element.
Definition: accessors_impl.hh:67
ElementAccessor::centre
arma::vec::fixed< spacedim > centre() const
Computes the barycenter.
Definition: accessors_impl.hh:89