OGS
TESLocalAssembler-impl.h
Go to the documentation of this file.
1 
10 #pragma once
11 
19 #include "TESLocalAssembler.h"
20 #include "TESReactionAdaptor.h"
21 
22 namespace
23 {
24 enum class MatOutType
25 {
26  OGS5,
27  PYTHON
28 };
29 
30 const MatOutType MATRIX_OUTPUT_FORMAT = MatOutType::PYTHON;
31 
32 // TODO move to some location in the OGS core.
33 template <typename Mat>
34 void ogs5OutMat(const Mat& mat)
35 {
36  for (unsigned r = 0; r < mat.rows(); ++r)
37  {
38  switch (MATRIX_OUTPUT_FORMAT)
39  {
40  case MatOutType::OGS5:
41  if (r != 0)
42  {
43  std::printf("\n");
44  }
45  std::printf("|");
46  break;
47  case MatOutType::PYTHON:
48  if (r != 0)
49  {
50  std::printf(",\n");
51  }
52  std::printf("[");
53  break;
54  }
55 
56  for (unsigned c = 0; c < mat.cols(); ++c)
57  {
58  switch (MATRIX_OUTPUT_FORMAT)
59  {
60  case MatOutType::OGS5:
61  std::printf(" %.16e", mat(r, c));
62  break;
63  case MatOutType::PYTHON:
64  if (c != 0)
65  {
66  std::printf(",");
67  }
68  std::printf(" %23.16g", mat(r, c));
69  break;
70  }
71  }
72 
73  switch (MATRIX_OUTPUT_FORMAT)
74  {
75  case MatOutType::OGS5:
76  std::printf(" | ");
77  break;
78  case MatOutType::PYTHON:
79  std::printf(" ]");
80  break;
81  }
82  }
83  std::printf("\n");
84 }
85 
86 template <typename Vec>
87 void ogs5OutVec(const Vec& vec)
88 {
89  for (unsigned r = 0; r < vec.size(); ++r)
90  {
91  switch (MATRIX_OUTPUT_FORMAT)
92  {
93  case MatOutType::OGS5:
94  if (r != 0)
95  {
96  std::printf("\n");
97  }
98  std::printf("| %.16e | ", vec[r]);
99  break;
100  case MatOutType::PYTHON:
101  if (r != 0)
102  {
103  std::printf(",\n");
104  }
105  std::printf("[ %23.16g ]", vec[r]);
106  break;
107  }
108  }
109  std::printf("\n");
110 }
111 
112 } // anonymous namespace
113 
114 namespace ProcessLib
115 {
116 namespace TES
117 {
118 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
121  std::size_t const /*local_matrix_size*/,
122  bool is_axially_symmetric,
123  unsigned const integration_order,
124  AssemblyParams const& asm_params)
125  : _element(e),
126  _integration_method(integration_order),
127  _shape_matrices(NumLib::initShapeMatrices<ShapeFunction,
128  ShapeMatricesType, GlobalDim>(
129  e, is_axially_symmetric, _integration_method)),
130  _d(asm_params, _integration_method.getNumberOfPoints(), GlobalDim)
131 {
132 }
133 
134 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
136  double const /*t*/, double const /*dt*/, std::vector<double> const& local_x,
137  std::vector<double> const& /*local_xdot*/,
138  std::vector<double>& local_M_data, std::vector<double>& local_K_data,
139  std::vector<double>& local_b_data)
140 {
141  auto const local_matrix_size = local_x.size();
142  // This assertion is valid only if all nodal d.o.f. use the same shape matrices.
143  assert(local_matrix_size == ShapeFunction::NPOINTS * NODAL_DOF);
144 
145  auto local_M = MathLib::createZeroedMatrix<NodalMatrixType>(
146  local_M_data, local_matrix_size, local_matrix_size);
147  auto local_K = MathLib::createZeroedMatrix<NodalMatrixType>(
148  local_K_data, local_matrix_size, local_matrix_size);
149  auto local_b = MathLib::createZeroedVector<NodalVectorType>(local_b_data,
150  local_matrix_size);
151 
152  unsigned const n_integration_points =
153  _integration_method.getNumberOfPoints();
154 
155  _d.preEachAssemble();
156 
157  for (unsigned ip = 0; ip < n_integration_points; ip++)
158  {
159  auto const& sm = _shape_matrices[ip];
160  auto const& wp = _integration_method.getWeightedPoint(ip);
161  auto const weight = wp.getWeight();
162 
163  _d.assembleIntegrationPoint(ip, local_x, sm, weight, local_M, local_K,
164  local_b);
165  }
166 
167  if (_d.getAssemblyParameters().output_element_matrices)
168  {
169  std::puts("### Element: ?");
170 
171  std::puts("---Velocity of water");
172  for (auto const& vs : _d.getData().velocity)
173  {
174  std::printf("| ");
175  for (auto v : vs)
176  {
177  std::printf("%23.16e ", v);
178  }
179  std::printf("|\n");
180  }
181 
182  std::printf("\n---Mass matrix: \n");
183  ogs5OutMat(local_M);
184  std::printf("\n");
185 
186  std::printf("---Laplacian + Advective + Content matrix: \n");
187  ogs5OutMat(local_K);
188  std::printf("\n");
189 
190  std::printf("---RHS: \n");
191  ogs5OutVec(local_b);
192  std::printf("\n");
193  }
194 }
195 
196 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
197 std::vector<double> const&
200  const double /*t*/,
201  std::vector<GlobalVector*> const& /*x*/,
202  std::vector<NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
203  std::vector<double>& /*cache*/) const
204 {
205  return _d.getData().solid_density;
206 }
207 
208 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
209 std::vector<double> const&
212  const double /*t*/,
213  std::vector<GlobalVector*> const& /*x*/,
214  std::vector<NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
215  std::vector<double>& cache) const
216 {
217  auto const rho_SR = _d.getData().solid_density;
218  auto const rho_SR_dry = _d.getAssemblyParameters().rho_SR_dry;
219 
220  cache.clear();
221  cache.reserve(rho_SR.size());
222 
223  transform(cbegin(rho_SR), cend(rho_SR), back_inserter(cache),
224  [&](auto const rho) { return rho / rho_SR_dry - 1.0; });
225 
226  return cache;
227 }
228 
229 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
230 std::vector<double> const&
233  const double /*t*/, std::vector<GlobalVector*> const& /*x*/,
234  std::vector<NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
235  std::vector<double>& cache) const
236 {
237  auto const fac = _d.getData().reaction_adaptor->getReactionDampingFactor();
238  auto const num_integration_points = _d.getData().solid_density.size();
239 
240  cache.clear();
241  cache.resize(num_integration_points, fac);
242 
243  return cache;
244 }
245 
246 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
247 std::vector<double> const&
250  const double /*t*/,
251  std::vector<GlobalVector*> const& /*x*/,
252  std::vector<NumLib::LocalToGlobalIndexMap const*> const& /*dof_table*/,
253  std::vector<double>& /*cache*/) const
254 {
255  return _d.getData().reaction_rate;
256 }
257 
258 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
259 std::vector<double> const&
262  const double /*t*/,
263  std::vector<GlobalVector*> const& x,
264  std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_table,
265  std::vector<double>& cache) const
266 {
267  auto const n_integration_points = _integration_method.getNumberOfPoints();
268 
269  constexpr int process_id = 0; // monolithic scheme
270  auto const indices =
271  NumLib::getIndices(_element.getID(), *dof_table[process_id]);
272  assert(!indices.empty());
273  auto const local_x = x[process_id]->get(indices);
274  // local_x is ordered by component, local_x_mat is row major
275  auto const local_x_mat = MathLib::toMatrix<
276  Eigen::Matrix<double, NODAL_DOF, Eigen::Dynamic, Eigen::RowMajor>>(
277  local_x, NODAL_DOF, ShapeFunction_::NPOINTS);
278 
279  cache.clear();
280  auto cache_mat = MathLib::createZeroedMatrix<
281  Eigen::Matrix<double, GlobalDim, Eigen::Dynamic, Eigen::RowMajor>>(
282  cache, GlobalDim, n_integration_points);
283 
284  for (unsigned i = 0; i < n_integration_points; ++i)
285  {
286  double p, T, x;
287  NumLib::shapeFunctionInterpolate(local_x, _shape_matrices[i].N, p, T,
288  x);
289  const double eta_GR = fluid_viscosity(p, T, x);
290 
291  auto const& k = _d.getAssemblyParameters().solid_perm_tensor;
292  cache_mat.col(i).noalias() =
293  k * (_shape_matrices[i].dNdx *
294  local_x_mat.row(COMPONENT_ID_PRESSURE).transpose()) /
295  -eta_GR;
296  }
297 
298  return cache;
299 }
300 
301 template <typename ShapeFunction_, typename IntegrationMethod_, int GlobalDim>
303  checkBounds(std::vector<double> const& local_x,
304  std::vector<double> const& local_x_prev_ts)
305 {
306  return _d.getReactionAdaptor().checkBounds(local_x, local_x_prev_ts);
307 }
308 
309 } // namespace TES
310 } // namespace ProcessLib
std::vector< double > const & getIntPtReactionRate(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &cache) const override
bool checkBounds(std::vector< double > const &local_x, std::vector< double > const &local_x_prev_ts) override
std::vector< double > const & getIntPtDarcyVelocity(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &cache) const override
void assemble(double const t, double const dt, std::vector< double > const &local_x, std::vector< double > const &local_xdot, std::vector< double > &local_M_data, std::vector< double > &local_K_data, std::vector< double > &local_b_data) override
TESLocalAssembler(MeshLib::Element const &e, std::size_t const local_matrix_size, bool is_axially_symmetric, unsigned const integration_order, AssemblyParams const &asm_params)
std::vector< double > const & getIntPtLoading(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &cache) const override
std::vector< double > const & getIntPtSolidDensity(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &cache) const override
std::vector< double > const & getIntPtReactionDampingFactor(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &cache) const override
Eigen::Map< Matrix > createZeroedMatrix(std::vector< double > &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
Definition: EigenMapTools.h:32
static const double r
Eigen::Map< const Matrix > toMatrix(std::vector< double > const &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
Definition: EigenMapTools.h:72
void shapeFunctionInterpolate(const NodalValues &nodal_values, const ShapeMatrix &shape_matrix_N, double &interpolated_value, ScalarTypes &... interpolated_values)
Definition: Interpolation.h:79
std::vector< GlobalIndexType > getIndices(std::size_t const mesh_item_id, NumLib::LocalToGlobalIndexMap const &dof_table)
std::vector< typename ShapeMatricesType::ShapeMatrices, Eigen::aligned_allocator< typename ShapeMatricesType::ShapeMatrices > > initShapeMatrices(MeshLib::Element const &e, bool const is_axially_symmetric, IntegrationMethod const &integration_method)
const unsigned NODAL_DOF
double fluid_viscosity(const double p, const double T, const double x)
const unsigned COMPONENT_ID_PRESSURE