Flow123d  JS_before_hm-62-ge56f9d5
assembly_lmh.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 "mesh/long_idx.hh"
13 #include "soil_models.hh"
14 #include "coupling/balance.hh"
15 
16 #include "tools/include_fadbad.hh" // for "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 AssemblyLMH : public AssemblyMH<dim> {
49 public:
50 
51  typedef std::shared_ptr<RichardsLMH::EqData> AssemblyDataPtr;
52 
53  AssemblyLMH(AssemblyDataPtr data)
54  : AssemblyMH<dim>(data),
55  ad_(data),
56  genuchten_on(false),
57  cross_section(1.0),
58  soil_model(data->soil_model_)
59  {}
60 
62  genuchten_on = (this->ad_->genuchten_p_head_scale.field_result({ele.region()}) != result_zeros);
63  if (genuchten_on) {
64  SoilData soil_data;
65  soil_data.n = this->ad_->genuchten_n_exponent.value(ele.centre(), ele.element_accessor());
66  soil_data.alpha = this->ad_->genuchten_p_head_scale.value(ele.centre(), ele.element_accessor());
67  soil_data.Qr = this->ad_->water_content_residual.value(ele.centre(), ele.element_accessor());
68  soil_data.Qs = this->ad_->water_content_saturated.value(ele.centre(), ele.element_accessor());
69  soil_data.Ks = this->ad_->conductivity.value(ele.centre(), ele.element_accessor());
70  //soil_data.cut_fraction = 0.99; // set by model
71 
72  soil_model->reset(soil_data);
73  }
74 
75  }
76 
78  reset_soil_model(ele);
79  double storativity = this->ad_->storativity.value(ele.centre(), ele.element_accessor())
80  +this->ad_->extra_storativity.value(ele.centre(), ele.element_accessor());
81  for (unsigned int i=0; i<ele.element_accessor()->n_sides(); i++) {
82  double capacity = 0;
83  double water_content = 0;
84  double phead = ad_->phead_edge_[ele.edge_local_idx(i)];
85  if (genuchten_on) {
86 
87  fadbad::B<double> x_phead(phead);
88  fadbad::B<double> evaluated( soil_model->water_content_diff(x_phead) );
89  evaluated.diff(0,1);
90  water_content = evaluated.val();
91  capacity = x_phead.d(0);
92  }
93  ad_->capacity[ele.side_local_idx(i)] = capacity + storativity;
94  ad_->water_content_previous_it[ele.side_local_idx(i)] = water_content + storativity * phead;
95  }
96  }
97 
99  {
100  reset_soil_model(ele);
101  cross_section = this->ad_->cross_section.value(ele.centre(), ele.element_accessor());
102 
103 
104  double conductivity, head;
105  if (genuchten_on) {
106  conductivity=0;
107  head=0;
108  for (unsigned int i=0; i<ele.element_accessor()->n_sides(); i++)
109  {
110  uint local_edge = ele.edge_local_idx(i);
111  double phead = ad_->phead_edge_[local_edge];
112  conductivity += soil_model->conductivity(phead);
113  head += ad_->phead_edge_[local_edge];
114  }
115  conductivity /= ele.n_sides();
116  head /= ele.n_sides();
117  } else {
118  conductivity = this->ad_->conductivity.value(ele.centre(), ele.element_accessor());
119  }
120 
121  double scale = 1 / cross_section / conductivity;
122  this->assemble_sides_scale(ele,scale);
123  }
124 
125  /***
126  * Called from assembly_local_matrix, assumes precomputed:
127  * cross_section, genuchten_on, soil_model
128  */
130  {
131 
132  // set lumped source
133  double diagonal_coef = ele.measure() * cross_section / ele.n_sides();
134 
135 
136  double source_diagonal = diagonal_coef * (
137  this->ad_->water_source_density.value(ele.centre(), ele.element_accessor())
138  + this->ad_->extra_source.value(ele.centre(), ele.element_accessor())
139  );
140 
142  for (unsigned int i=0; i<ele.element_accessor()->n_sides(); i++)
143  {
144 
145  uint local_edge = ele.edge_local_idx(i);
146  uint local_side = ele.side_local_idx(i);
147  if (this->dirichlet_edge[i] == 0) {
148 
149  double capacity = this->ad_->capacity[local_side];
150  double water_content_diff = -ad_->water_content_previous_it[local_side] + ad_->water_content_previous_time[local_side];
151  double mass_diagonal = diagonal_coef * capacity;
152 
153  /*
154  DebugOut().fmt("w diff: {:g} mass: {:g} w prev: {:g} w time: {:g} c: {:g} p: {:g} z: {:g}",
155  water_content_diff,
156  mass_diagonal * ad_->phead_edge_[local_edge],
157  -ad_->water_content_previous_it[local_side],
158  ad_->water_content_previous_time[local_side],
159  capacity,
160  ad_->phead_edge_[local_edge],
161  ele.centre()[0] );
162  */
163 
164 
165  double mass_rhs = mass_diagonal * ad_->phead_edge_[local_edge] / this->ad_->time_step_
166  + diagonal_coef * water_content_diff / this->ad_->time_step_;
167 
168 // DBGCOUT(<< "source [" << loc_system_.row_dofs[this->loc_edge_dofs[i]] << ", " << loc_system_.row_dofs[this->loc_edge_dofs[i]]
169 // << "] mat: " << -mass_diagonal/this->ad_->time_step_
170 // << " rhs: " << -source_diagonal - mass_rhs
171 // << "\n");
172  this->loc_system_.add_value(this->loc_edge_dofs[i], this->loc_edge_dofs[i],
173  -mass_diagonal/this->ad_->time_step_,
174  -source_diagonal - mass_rhs);
175  }
176 
177  if (ad_->balance != nullptr) {
178  ad_->balance->add_mass_vec_value(ad_->water_balance_idx, ele.region().bulk_idx(),
179  diagonal_coef*ad_->water_content_previous_it[local_side]);
180  ad_->balance->add_source_values(ad_->water_balance_idx, ele.region().bulk_idx(), {(LongIdx)local_edge},{0},{source_diagonal});
181  }
182  }
183 
184  }
185 
186  AssemblyDataPtr ad_;
187 
190  std::shared_ptr<SoilModelBase> soil_model;
191 };
192 
193 
194 
195 #endif /* SRC_FLOW_ASSEMBLY_LMH_HH_ */
const arma::vec3 centre() const
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: long_idx.hh:22
AssemblyLMH(AssemblyDataPtr data)
Definition: assembly_lmh.hh:53
unsigned int uint
double cross_section
ElementAccessor< 3 > element_accessor()
void reset_soil_model(LocalElementAccessorBase< 3 > ele)
Definition: assembly_lmh.hh:61
void assemble_source_term(LocalElementAccessorBase< 3 > ele) override
double alpha
Definition: soil_models.hh:41
std::shared_ptr< SoilModelBase > soil_model
AssemblyDataPtr ad_
double Qr
Definition: soil_models.hh:42
unsigned int n_sides() const
Definition: elements.h:134
void assemble_sides_scale(LocalElementAccessorBase< 3 > ele_ac, double scale)
double Qs
Definition: soil_models.hh:43
std::shared_ptr< RichardsLMH::EqData > AssemblyDataPtr
Definition: assembly_lmh.hh:51
void assemble_sides(LocalElementAccessorBase< 3 > ele) override
Definition: assembly_lmh.hh:98
double Ks
Definition: soil_models.hh:45
unsigned int bulk_idx() const
Returns index of the region in the bulk set.
Definition: region.hh:91
void update_water_content(LocalElementAccessorBase< 3 > ele) override
Definition: assembly_lmh.hh:77
double n
Definition: soil_models.hh:40