OGS
LiquidFlowLocalAssembler-impl.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
12
13namespace ProcessLib
14{
15namespace LiquidFlow
16{
17template <typename ShapeFunction, int GlobalDim>
19 double const t, double const dt, std::vector<double> const& local_x,
20 std::vector<double> const& /*local_x_prev*/,
21 std::vector<double>& local_M_data, std::vector<double>& local_K_data,
22 std::vector<double>& local_b_data)
23{
24 // Dummy integration point
25 unsigned const ip = 0;
26 // auto const& ip_data = _ip_data[ip];
27 auto const& Ns = _process_data.shape_matrix_cache
28 .NsHigherOrder<typename ShapeFunction::MeshElement>();
29 auto const& N = Ns[ip];
30
32 std::nullopt, _element.getID(),
35 _element, N))};
36
37 auto const& medium = *_process_data.media_map.getMedium(_element.getID());
39 vars.temperature =
41 .template value<double>(vars, pos, t, dt);
42 vars.liquid_phase_pressure = std::numeric_limits<double>::quiet_NaN();
43 GlobalDimMatrixType const permeability =
46 vars, pos, t, dt));
47 // Note: For Inclined 1D in 2D/3D or 2D element in 3D, the first item in
48 // the assert must be changed to permeability.rows() ==
49 // _element->getDimension()
50 assert(permeability.rows() == GlobalDim || permeability.rows() == 1);
51
52 if (permeability.size() == 1)
53 { // isotropic or 1D problem.
55 t, dt, local_x, local_M_data, local_K_data, local_b_data);
56 }
57 else
58 {
60 t, dt, local_x, local_M_data, local_K_data, local_b_data);
61 }
62}
63
64template <typename ShapeFunction, int GlobalDim>
66 MathLib::Point3d const& p_local_coords, double const t,
67 std::vector<double> const& local_x) const
68{
69 // TODO (tf) Temporary value not used by current material models. Need
70 // extension of getFlux interface
71 double const dt = std::numeric_limits<double>::quiet_NaN();
72
73 // Note: Axial symmetry is set to false here, because we only need dNdx
74 // here, which is not affected by axial symmetry.
75 auto const shape_matrices =
77 GlobalDim>(_element,
78 false /*is_axially_symmetric*/,
79 std::array{p_local_coords})[0];
80
81 // create pos object to access the correct media property
83 pos.setElementID(_element.getID());
84
85 auto const& medium = *_process_data.media_map.getMedium(_element.getID());
86 auto const& fluid_phase = fluidPhase(medium);
87
89
90 double pressure = 0.0;
91 NumLib::shapeFunctionInterpolate(local_x, shape_matrices.N, pressure);
92 vars.liquid_phase_pressure = pressure;
93
94 GlobalDimMatrixType const intrinsic_permeability =
97 vars, pos, t, dt));
98 auto const viscosity =
100 .template value<double>(vars, pos, t, dt);
101
102 Eigen::Vector3d flux(0.0, 0.0, 0.0);
103 flux.head<GlobalDim>() =
104 -intrinsic_permeability / viscosity * shape_matrices.dNdx *
105 Eigen::Map<const NodalVectorType>(local_x.data(), local_x.size());
106
107 return flux;
108}
109
110template <typename ShapeFunction, int GlobalDim>
111template <typename LaplacianGravityVelocityCalculator>
113 assembleMatrixAndVector(double const t, double const dt,
114 std::vector<double> const& local_x,
115 std::vector<double>& local_M_data,
116 std::vector<double>& local_K_data,
117 std::vector<double>& local_b_data)
118{
119 auto const local_matrix_size = local_x.size();
120 assert(local_matrix_size == ShapeFunction::NPOINTS);
121
123 local_M_data, local_matrix_size, local_matrix_size);
125 local_K_data, local_matrix_size, local_matrix_size);
127 local_b_data, local_matrix_size);
128 const auto local_p_vec =
129 MathLib::toVector<NodalVectorType>(local_x, local_matrix_size);
130
131 unsigned const n_integration_points =
132 _integration_method.getNumberOfPoints();
133
135 pos.setElementID(_element.getID());
136
137 auto const& medium = *_process_data.media_map.getMedium(_element.getID());
138 auto const& fluid_phase = fluidPhase(medium);
139
141 auto& phase_pressure = medium.hasPhase("Gas") ? vars.gas_phase_pressure
143
144 GlobalDimVectorType const projected_body_force_vector =
145 _process_data.element_rotation_matrices[_element.getID()] *
146 _process_data.element_rotation_matrices[_element.getID()].transpose() *
147 _process_data.specific_body_force;
148
149 auto const& Ns = _process_data.shape_matrix_cache
150 .NsHigherOrder<typename ShapeFunction::MeshElement>();
151
152 for (unsigned ip = 0; ip < n_integration_points; ip++)
153 {
154 auto const& ip_data = _ip_data[ip];
155 auto const& N = Ns[ip];
156
157 phase_pressure = N.dot(local_p_vec);
159 std::nullopt, _element.getID(),
162 _element, N))};
163
164 vars.temperature =
166 .template value<double>(vars, pos, t, dt);
167
168 auto const [fluid_density, viscosity] =
170 fluid_phase, vars);
171
172 auto const porosity =
174 .template value<double>(vars, pos, t, dt);
175 auto const specific_storage =
177 .template value<double>(vars, pos, t, dt);
178
179 auto const get_drho_dp = [&]()
180 {
182 .template dValue<double>(vars, _process_data.phase_variable,
183 pos, t, dt);
184 };
185 auto const storage =
186 _process_data.equation_balance_type == EquationBalanceType::volume
187 ? specific_storage
188 : specific_storage + porosity * get_drho_dp() / fluid_density;
189
190 double const scaling_factor =
191 _process_data.equation_balance_type == EquationBalanceType::volume
192 ? 1.0
193 : fluid_density;
194 // Assemble mass matrix, M
195 local_M.noalias() += scaling_factor * storage * N.transpose() * N *
196 ip_data.integration_weight;
197
198 GlobalDimMatrixType const permeability =
201 vars, pos, t, dt));
202
203 // Assemble Laplacian, K, and RHS by the gravitational term
204 LaplacianGravityVelocityCalculator::calculateLaplacianAndGravityTerm(
205 local_K, local_b, ip_data, scaling_factor * permeability, viscosity,
206 fluid_density, projected_body_force_vector,
207 _process_data.has_gravity);
208 }
209}
210
211template <typename ShapeFunction, int GlobalDim>
212template <typename VelocityCacheType>
214 bool const is_scalar_permeability, const double t, const double dt,
215 std::vector<double> const& local_x,
217 VelocityCacheType& darcy_velocity_at_ips) const
218{
219 if (is_scalar_permeability)
220 { // isotropic or 1D problem.
222 t, dt, local_x, pos, darcy_velocity_at_ips);
223 }
224 else
225 {
227 t, dt, local_x, pos, darcy_velocity_at_ips);
228 }
229}
230
231template <typename ShapeFunction, int GlobalDim>
232std::vector<double> const&
234 const double t,
235 std::vector<GlobalVector*> const& x,
236 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
237 std::vector<double>& velocity_cache) const
238{
239 // TODO (tf) Temporary value not used by current material models. Need
240 // extension of secondary variable interface.
241 double const dt = std::numeric_limits<double>::quiet_NaN();
242
243 constexpr int process_id = 0;
244 auto const indices =
245 NumLib::getIndices(_element.getID(), *dof_tables[process_id]);
246 auto const local_x = x[process_id]->get(indices);
247 auto const n_integration_points = _integration_method.getNumberOfPoints();
248 velocity_cache.clear();
249
250 // Dummy integration point
251 unsigned const ip = 0;
252 // auto const& ip_data = _ip_data[ip];
253 auto const& Ns = _process_data.shape_matrix_cache
254 .NsHigherOrder<typename ShapeFunction::MeshElement>();
255 auto const& N = Ns[ip];
256
258 std::nullopt, _element.getID(),
261 _element, N))};
262
263 auto const& medium = *_process_data.media_map.getMedium(_element.getID());
265 vars.temperature =
267 .template value<double>(vars, pos, t, dt);
268 vars.liquid_phase_pressure = std::numeric_limits<double>::quiet_NaN();
269
270 GlobalDimMatrixType const permeability =
273 vars, pos, t, dt));
274
275 assert(permeability.rows() == GlobalDim || permeability.rows() == 1);
276
277 bool const is_scalar_permeability = (permeability.size() == 1);
278
279 auto velocity_cache_vectors = MathLib::createZeroedMatrix<
280 Eigen::Matrix<double, GlobalDim, Eigen::Dynamic, Eigen::RowMajor>>(
281 velocity_cache, GlobalDim, n_integration_points);
282
283 computeDarcyVelocity(is_scalar_permeability, t, dt, local_x, pos,
284 velocity_cache_vectors);
285
286 return velocity_cache;
287}
288
289template <typename ShapeFunction, int GlobalDim>
290template <typename LaplacianGravityVelocityCalculator,
291 typename VelocityCacheType>
294 const double t, const double dt, std::vector<double> const& local_x,
295 ParameterLib::SpatialPosition const& /* TODO{naumov) delete */,
296 VelocityCacheType& darcy_velocity_at_ips) const
297{
298 auto const local_matrix_size = local_x.size();
299 assert(local_matrix_size == ShapeFunction::NPOINTS);
300
301 const auto local_p_vec =
302 MathLib::toVector<NodalVectorType>(local_x, local_matrix_size);
303
304 unsigned const n_integration_points =
305 _integration_method.getNumberOfPoints();
306
307 auto const& medium = *_process_data.media_map.getMedium(_element.getID());
308 auto const& fluid_phase = fluidPhase(medium);
309
311 auto& phase_pressure = medium.hasPhase("Gas") ? vars.gas_phase_pressure
313
314 GlobalDimVectorType const projected_body_force_vector =
315 _process_data.element_rotation_matrices[_element.getID()] *
316 _process_data.element_rotation_matrices[_element.getID()].transpose() *
317 _process_data.specific_body_force;
318
319 auto const& Ns = _process_data.shape_matrix_cache
320 .NsHigherOrder<typename ShapeFunction::MeshElement>();
321
322 for (unsigned ip = 0; ip < n_integration_points; ip++)
323 {
324 auto const& ip_data = _ip_data[ip];
325 auto const& N = Ns[ip];
326
327 phase_pressure = N.dot(local_p_vec);
329 std::nullopt, _element.getID(),
332 _element, N))};
333
334 vars.temperature =
336 .template value<double>(vars, pos, t, dt);
337 auto const [fluid_density, viscosity] =
339 fluid_phase, vars);
340
341 GlobalDimMatrixType const permeability =
344 vars, pos, t, dt));
345
346 darcy_velocity_at_ips.col(ip) =
347 LaplacianGravityVelocityCalculator::calculateVelocity(
348 local_p_vec, ip_data, permeability, viscosity, fluid_density,
349 projected_body_force_vector, _process_data.has_gravity);
350 }
351}
352
353template <typename ShapeFunction, int GlobalDim>
356 Eigen::Map<NodalMatrixType>& local_K,
357 Eigen::Map<NodalVectorType>& local_b,
359 GlobalDimMatrixType const& permeability_with_density_factor,
360 double const mu, double const rho_L,
361 GlobalDimVectorType const& specific_body_force, bool const has_gravity)
362{
363 const double K = permeability_with_density_factor(0, 0) / mu;
364 const double fac = K * ip_data.integration_weight;
365 local_K.noalias() += fac * ip_data.dNdx.transpose() * ip_data.dNdx;
366
367 if (has_gravity)
368 {
369 local_b.noalias() +=
370 (fac * rho_L) * ip_data.dNdx.transpose() * specific_body_force;
371 }
372}
373
374template <typename ShapeFunction, int GlobalDim>
375Eigen::Matrix<double, GlobalDim, 1>
378 Eigen::Map<const NodalVectorType> const& local_p,
380 GlobalDimMatrixType const& permeability, double const mu,
381 double const rho_L, GlobalDimVectorType const& specific_body_force,
382 bool const has_gravity)
383{
384 const double K = permeability(0, 0) / mu;
385 // Compute the velocity
386 GlobalDimVectorType velocity = -K * ip_data.dNdx * local_p;
387 // gravity term
388 if (has_gravity)
389 {
390 velocity += (K * rho_L) * specific_body_force;
391 }
392 return velocity;
393}
394
395template <typename ShapeFunction, int GlobalDim>
398 Eigen::Map<NodalMatrixType>& local_K,
399 Eigen::Map<NodalVectorType>& local_b,
401 GlobalDimMatrixType const& permeability_with_density_factor,
402 double const mu, double const rho_L,
403 GlobalDimVectorType const& specific_body_force, bool const has_gravity)
404{
405 const double fac = ip_data.integration_weight / mu;
406 local_K.noalias() += fac * ip_data.dNdx.transpose() *
407 permeability_with_density_factor * ip_data.dNdx;
408
409 if (has_gravity)
410 {
411 local_b.noalias() += (fac * rho_L) * ip_data.dNdx.transpose() *
412 permeability_with_density_factor *
413 specific_body_force;
414 }
415}
416
417template <typename ShapeFunction, int GlobalDim>
418Eigen::Matrix<double, GlobalDim, 1>
421 Eigen::Map<const NodalVectorType> const& local_p,
423 GlobalDimMatrixType const& permeability, double const mu,
424 double const rho_L, GlobalDimVectorType const& specific_body_force,
425 bool const has_gravity)
426{
427 // Compute the velocity
428 GlobalDimVectorType velocity = -permeability * ip_data.dNdx * local_p / mu;
429
430 // gravity term
431 if (has_gravity)
432 {
433 velocity += (rho_L / mu) * permeability * specific_body_force;
434 }
435 return velocity;
436}
437
438} // namespace LiquidFlow
439} // namespace ProcessLib
void setElementID(std::size_t element_id)
Eigen::Vector3d getFlux(MathLib::Point3d const &p_local_coords, double const t, std::vector< double > const &local_x) const override
std::vector< IntegrationPointData< GlobalDimNodalMatrixType > > _ip_data
typename ShapeMatricesType::GlobalDimVectorType GlobalDimVectorType
ShapeMatrixPolicyType< ShapeFunction, GlobalDim > ShapeMatricesType
void computeProjectedDarcyVelocity(const double t, const double dt, std::vector< double > const &local_x, ParameterLib::SpatialPosition const &pos, VelocityCacheType &darcy_velocity_at_ips) const
void assemble(double const t, double const dt, std::vector< double > const &local_x, std::vector< double > const &, std::vector< double > &local_M_data, std::vector< double > &local_K_data, std::vector< double > &local_b_data) override
NumLib::GenericIntegrationMethod const & _integration_method
void assembleMatrixAndVector(double const t, double const dt, std::vector< double > const &local_x, std::vector< double > &local_M_data, std::vector< double > &local_K_data, std::vector< double > &local_b_data)
void computeDarcyVelocity(bool const is_scalar_permeability, const double t, const double dt, std::vector< double > const &local_x, ParameterLib::SpatialPosition const &pos, VelocityCacheType &darcy_velocity_at_ips) const
typename ShapeMatricesType::GlobalDimMatrixType GlobalDimMatrixType
std::vector< double > const & getIntPtDarcyVelocity(const double t, std::vector< GlobalVector * > const &x, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_table, std::vector< double > &velocity_cache) const override
std::tuple< double, double > getFluidDensityAndViscosity(double const t, double const dt, ParameterLib::SpatialPosition const &pos, MaterialPropertyLib::Phase const &fluid_phase, MaterialPropertyLib::VariableArray &vars)
It computes fluid density and viscosity for single phase flow model.
constexpr Eigen::Matrix< double, GlobalDim, GlobalDim > formEigenTensor(MaterialPropertyLib::PropertyDataType const &values)
Eigen::Map< Vector > createZeroedVector(std::vector< double > &data, Eigen::VectorXd::Index size)
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
Eigen::Map< Matrix > createZeroedMatrix(std::vector< double > &data, Eigen::MatrixXd::Index rows, Eigen::MatrixXd::Index cols)
void shapeFunctionInterpolate(const NodalValues &, const ShapeMatrix &)
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 > > computeShapeMatrices(MeshLib::Element const &e, bool const is_axially_symmetric, PointContainer const &points)
std::array< double, 3 > interpolateCoordinates(MeshLib::Element const &e, typename ShapeMatricesType::ShapeMatrices::ShapeType const &N)
static void calculateLaplacianAndGravityTerm(Eigen::Map< NodalMatrixType > &local_K, Eigen::Map< NodalVectorType > &local_b, IntegrationPointData< GlobalDimNodalMatrixType > const &ip_data, GlobalDimMatrixType const &permeability_with_density_factor, double const mu, double const rho_L, GlobalDimVectorType const &specific_body_force, bool const has_gravity)
static Eigen::Matrix< double, GlobalDim, 1 > calculateVelocity(Eigen::Map< const NodalVectorType > const &local_p, IntegrationPointData< GlobalDimNodalMatrixType > const &ip_data, GlobalDimMatrixType const &permeability, double const mu, double const rho_L, GlobalDimVectorType const &specific_body_force, bool const has_gravity)
static void calculateLaplacianAndGravityTerm(Eigen::Map< NodalMatrixType > &local_K, Eigen::Map< NodalVectorType > &local_b, IntegrationPointData< GlobalDimNodalMatrixType > const &ip_data, GlobalDimMatrixType const &permeability_with_density_factor, double const mu, double const rho_L, GlobalDimVectorType const &specific_body_force, bool const has_gravity)
static Eigen::Matrix< double, GlobalDim, 1 > calculateVelocity(Eigen::Map< const NodalVectorType > const &local_p, IntegrationPointData< GlobalDimNodalMatrixType > const &ip_data, GlobalDimMatrixType const &permeability, double const mu, double const rho_L, GlobalDimVectorType const &specific_body_force, bool const has_gravity)