version 3.11-dev
felocalresidual.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_NAVIERSTOKES_MOMENTUM_CVFE_FE_LOCAL_RESIDUAL_HELPER_HH
13#define DUMUX_NAVIERSTOKES_MOMENTUM_CVFE_FE_LOCAL_RESIDUAL_HELPER_HH
14
15#include <dune/geometry/quadraturerules.hh>
16#include <dumux/common/typetraits/localdofs_.hh>
20
21#include "flux.hh"
22
23namespace Dumux {
24
29template<class Scalar, class NumEqVector, class LocalBasis, class Extrusion>
31{
32 using RangeType = typename LocalBasis::Traits::RangeType;
33
34public:
45 template<class ResidualVector, class Problem, class FVElementGeometry, class ElementVariables>
46 static void addStorageTerms(ResidualVector& residual,
47 const Problem& problem,
48 const FVElementGeometry& fvGeometry,
49 const ElementVariables& prevElemVars,
50 const ElementVariables& curElemVars,
51 const Scalar timeStepSize)
52 {
53 if constexpr (Detail::LocalDofs::hasNonCVLocalDofsInterface<FVElementGeometry>())
54 {
55 // Make sure we don't iterate over quadrature points if there are no hybrid dofs
56 if (nonCVLocalDofs(fvGeometry).empty())
57 return;
58
59 const auto& localBasis = fvGeometry.feLocalBasis();
60 std::vector<RangeType> integralShapeFunctions(localBasis.size(), RangeType(0.0));
61
62 // We apply mass lumping such that we only need to calculate the integral of basis functions
63 const auto& geometry = fvGeometry.elementGeometry();
64 const auto& element = fvGeometry.element();
65 using GlobalPosition = typename FVElementGeometry::GridGeometry::GlobalCoordinate;
67
68 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, element))
69 {
70 const auto& ipData = qpData.ipData();
71 // Obtain and store shape function values and gradients at the current quad point
72 FeIpData feIpData(geometry, ipData.local(), ipData.global(), localBasis);
73
74 // get density from the problem
75 for (const auto& localDof : nonCVLocalDofs(fvGeometry))
76 integralShapeFunctions[localDof.index()] += qpData.weight() * feIpData.shapeValue(localDof.index());
77 }
78
79 for (const auto& localDof : nonCVLocalDofs(fvGeometry))
80 {
81 const auto localDofIdx = localDof.index();
82 const auto& data = ipData(fvGeometry, localDof);
83 const auto curDensity = problem.density(element, fvGeometry, data, false);
84 const auto prevDensity = problem.density(element, fvGeometry, data, true);
85 const auto curVelocity = curElemVars[localDofIdx].velocity();
86 const auto prevVelocity = prevElemVars[localDofIdx].velocity();
87 auto timeDeriv = (curDensity*curVelocity - prevDensity*prevVelocity);
88 timeDeriv /= timeStepSize;
89
90 // add storage to residual
91 for (int eqIdx = 0; eqIdx < NumEqVector::dimension; ++eqIdx)
92 residual[localDofIdx][eqIdx] += integralShapeFunctions[localDofIdx]*timeDeriv[eqIdx];
93 }
94 }
95 }
96
107 template<class ResidualVector, class Problem, class FVElementGeometry,
108 class ElementVariables, class ElementFluxVariablesCache, class ElementBoundaryTypes>
109 static void addFluxAndSourceTerms(ResidualVector& residual,
110 const Problem& problem,
111 const FVElementGeometry& fvGeometry,
112 const ElementVariables& elemVars,
113 const ElementFluxVariablesCache& elemFluxVarsCache,
114 const ElementBoundaryTypes& elemBcTypes)
115 {
116 if constexpr (Detail::LocalDofs::hasNonCVLocalDofsInterface<FVElementGeometry>())
117 {
118 // Make sure we don't iterate over quadrature points if there are no hybrid dofs
119 if (nonCVLocalDofs(fvGeometry).empty())
120 return;
121
122 if (!problem.pointSourceMap().empty())
123 DUNE_THROW(Dune::NotImplemented, "Point sources are not implemented for hybrid momentum schemes.");
124
125 static const bool enableUnsymmetrizedVelocityGradient
126 = getParamFromGroup<bool>(problem.paramGroup(), "FreeFlow.EnableUnsymmetrizedVelocityGradient", false);
127
128 const auto& element = fvGeometry.element();
129 using FluxVariablesCache = typename ElementFluxVariablesCache::FluxVariablesCache;
131 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, element))
132 {
133 const auto& ipData = qpData.ipData();
134 // Obtain and store shape function values and gradients at the current quad point
135 const auto& cache = elemFluxVarsCache[ipData];
136 FluxFunctionContext context(problem, fvGeometry, elemVars, cache);
137 const auto& v = context.velocity();
138 const auto& gradV = context.gradVelocity();
139
140 // get viscosity from the problem
141 const Scalar mu = problem.effectiveViscosity(element, fvGeometry, ipData);
142 // get density from the problem
143 const Scalar density = problem.density(element, fvGeometry, ipData);
144
145 for (const auto& localDof : nonCVLocalDofs(fvGeometry))
146 {
147 const auto localDofIdx = localDof.index();
148 NumEqVector fluxAndSourceTerm(0.0);
149 // add advection term
150 if (problem.enableInertiaTerms())
151 fluxAndSourceTerm -= density*(v*cache.gradN(localDofIdx))*v;
152
153 // add diffusion term
154 fluxAndSourceTerm += enableUnsymmetrizedVelocityGradient ?
155 mu*mv(gradV, cache.gradN(localDofIdx))
156 : mu*mv(gradV + getTransposed(gradV), cache.gradN(localDofIdx));
157
158 // add pressure term
159 fluxAndSourceTerm -= problem.pressure(element, fvGeometry, ipData) * cache.gradN(localDofIdx);
160
161 // finally add source and Neumann term and add everything to residual
162 auto sourceAtIp = problem.source(fvGeometry, elemVars, ipData);
163 // add gravity term rho*g (note that gravity might be zero in case it's disabled in the problem)
164 sourceAtIp += density * problem.gravity();
165
166 const auto& shapeValues = cache.shapeValues();
167 for (int eqIdx = 0; eqIdx < NumEqVector::dimension; ++eqIdx)
168 {
169 fluxAndSourceTerm[eqIdx] -= shapeValues[localDofIdx] * sourceAtIp[eqIdx];
170 residual[localDofIdx][eqIdx] += qpData.weight()*fluxAndSourceTerm[eqIdx];
171 }
172 }
173 }
174
175 if (elemBcTypes.hasNeumann())
176 addBoundaryFluxes(residual, problem, fvGeometry, elemVars, elemFluxVarsCache, elemBcTypes);
177 }
178 }
179
190 template<class ResidualVector, class Problem, class FVElementGeometry,
191 class ElementVariables, class ElementFluxVariablesCache, class ElementBoundaryTypes>
192 static void addBoundaryFluxes(ResidualVector& residual,
193 const Problem& problem,
194 const FVElementGeometry& fvGeometry,
195 const ElementVariables& elemVars,
196 const ElementFluxVariablesCache& elemFluxVarsCache,
197 const ElementBoundaryTypes& elemBcTypes)
198 {
199 ResidualVector flux(0.0);
200
201 const auto& element = fvGeometry.element();
202 for (const auto& intersection : intersections(fvGeometry.gridGeometry().gridView(), element))
203 {
204 if (!intersection.boundary())
205 continue;
206
207 const auto& bcTypes = elemBcTypes.get(fvGeometry, intersection);
208 if (!bcTypes.hasNeumann())
209 continue;
210
211 problem.addBoundaryFluxIntegrals(flux, fvGeometry, elemVars, elemFluxVarsCache, intersection, bcTypes);
212 }
213 residual += flux;
214 }
215};
216
217} // end namespace Dumux
218
219#endif
Boundary flag to store e.g. in sub control volume faces.
Definition: fem/interpolationpointdata.hh:21
Helper class for evaluating FE-based local residuals.
Definition: felocalresidual.hh:31
static void addFluxAndSourceTerms(ResidualVector &residual, const Problem &problem, const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const ElementFluxVariablesCache &elemFluxVarsCache, const ElementBoundaryTypes &elemBcTypes)
Add flux and source residual contribution for non-CV local dofs.
Definition: felocalresidual.hh:109
static void addBoundaryFluxes(ResidualVector &residual, const Problem &problem, const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const ElementFluxVariablesCache &elemFluxVarsCache, const ElementBoundaryTypes &elemBcTypes)
Evaluate Neumann boundary contributions.
Definition: felocalresidual.hh:192
static void addStorageTerms(ResidualVector &residual, const Problem &problem, const FVElementGeometry &fvGeometry, const ElementVariables &prevElemVars, const ElementVariables &curElemVars, const Scalar timeStepSize)
Add storage residual contribution for non-CV local dofs.
Definition: felocalresidual.hh:46
Context for interpolating data on interpolation points.
Definition: flux.hh:99
Helper classes to compute the integration elements.
Shape functions and gradients at an interpolation point.
Dune::DenseVector< V >::derived_type mv(const Dune::DenseMatrix< MAT > &M, const Dune::DenseVector< V > &v)
Returns the result of the projection of a vector v with a Matrix M.
Definition: math.hh:829
Dune::FieldMatrix< Scalar, n, m > getTransposed(const Dune::FieldMatrix< Scalar, m, n > &M)
Transpose a FieldMatrix.
Definition: math.hh:712
typename NumEqVectorTraits< PrimaryVariables >::type NumEqVector
A vector with the same size as numbers of equations This is the default implementation and has to be ...
Definition: numeqvector.hh:34
auto quadratureRule(const FVElementGeometry &fvGeometry, const typename FVElementGeometry::SubControlVolume &scv, QuadratureRules::MidpointQuadrature)
Midpoint quadrature for scv.
Definition: quadraturerules.hh:148
std::string density(int phaseIdx) noexcept
I/O name of density for multiphase systems.
Definition: name.hh:53
Definition: adapt.hh:17