version 3.11-dev
assembly/localresidual.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_BASE_LOCAL_RESIDUAL_HH
13#define DUMUX_BASE_LOCAL_RESIDUAL_HH
14
15#include <cassert>
16
17#include <dune/common/exceptions.hh>
18
19#include <dumux/common/typetraits/localdofs_.hh>
26
27namespace Dumux::Experimental {
28
35template<class TypeTag>
37{
42 using Element = typename GridView::template Codim<0>::Entity;
43 using FVElementGeometry = typename GetPropType<TypeTag, Properties::GridGeometry>::LocalView;
46 using Extrusion = Extrusion_t<GridGeometry>;
49 using GridVariablesCache = typename GridVariables::GridVariablesCache;
50 using ElementVariables = typename GridVariablesCache::LocalView;
52
53public:
56
58 LocalResidual(const Problem* problem,
59 const TimeLoop* timeLoop = nullptr)
60 : problem_(problem)
61 , timeLoop_(timeLoop)
62 {}
63
68 // \{
69
80 ElementResidualVector evalStorage(const Element& element,
81 const FVElementGeometry& fvGeometry,
82 const ElementVariables& prevElemVars,
83 const ElementVariables& curElemVars) const
84 {
85 assert(!this->isStationary() && "no time loop set for storage term evaluation");
86
87 // initialize the residual vector for all local dofs in this element
88 ElementResidualVector residual(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry));
89
90 // evaluate the volume terms (storage + source terms)
91 // forward to the local residual specialized for the discretization methods
92 for (const auto& scv : scvs(fvGeometry))
93 this->asImp().evalStorage(residual, this->problem(), element, fvGeometry, prevElemVars, curElemVars, scv);
94
95 // allow for additional contributions (e.g. hybrid CVFE / FE schemes)
96 this->asImp().addToElementStorageResidual(residual, this->problem(), element, fvGeometry, prevElemVars, curElemVars);
97
98 return residual;
99 }
100
111 const FVElementGeometry& fvGeometry,
112 const ElementVariables& elemVars,
113 const ElementBoundaryTypes& bcTypes) const
114 {
115 // initialize the residual vector for all local dofs in this element
116 ElementResidualVector residual(Dumux::Detail::LocalDofs::numLocalDofs(fvGeometry));
117
118 // evaluate the volume terms (storage + source terms)
119 // forward to the local residual specialized for the discretization methods
120 for (const auto& scv : scvs(fvGeometry))
121 this->asImp().evalSource(residual, this->problem(), element, fvGeometry, elemVars, scv);
122
123 // forward to the local residual specialized for the discretization methods
124 for (auto&& scvf : scvfs(fvGeometry))
125 this->asImp().evalFlux(residual, this->problem(), element, fvGeometry, elemVars, bcTypes, scvf);
126
127 // allow for additional contributions (e.g. hybrid CVFE / FE schemes)
128 this->asImp().addToElementFluxAndSourceResidual(residual, this->problem(), element, fvGeometry, elemVars, bcTypes);
129
130 return residual;
131 }
132
135 const Problem& problem,
136 const Element& element,
137 const FVElementGeometry& fvGeometry,
138 const ElementVariables& prevElemVars,
139 const ElementVariables& curElemVars) const
140 {}
141
144 const Problem& problem,
145 const Element& element,
146 const FVElementGeometry& fvGeometry,
147 const ElementVariables& curElemVars,
148 const ElementBoundaryTypes& bcTypes) const
149 {}
150
151 // \}
152
157 // \{
158
169 template<class SubControlVolume>
170 NumEqVector storageIntegral(const FVElementGeometry& fvGeometry,
171 const ElementVariables& elemVars,
172 const SubControlVolume& scv,
173 bool isPreviousTimeLevel) const
174 {
175 DUNE_THROW(Dune::NotImplemented, "This model does not implement a storageIntegral method!");
176 }
177
188 template<class SubControlVolume>
189 NumEqVector sourceIntegral(const FVElementGeometry& fvGeometry,
190 const ElementVariables& elemVars,
191 const SubControlVolume& scv) const
192 {
193 NumEqVector source(0.0);
194
195 const auto& problem = this->asImp().problem();
196 for (const auto& qpData : CVFE::quadratureRule(fvGeometry, scv))
197 source += qpData.weight() * problem.source(fvGeometry, elemVars, qpData.ipData());
198
199 // ToDo: point source data with ipData
200 // add contribution from possible point sources
201 if (!problem.pointSourceMap().empty())
202 source += Extrusion::volume(fvGeometry, scv) * problem.scvPointSources(fvGeometry.element(), fvGeometry, elemVars, scv);
203
204 source *= elemVars[scv].extrusionFactor();
205
206 return source;
207 }
208
219 template<class SubControlVolumeFace>
220 NumEqVector fluxIntegral(const FVElementGeometry& fvGeometry,
221 const ElementVariables& elemVars,
222 const SubControlVolumeFace& scvf) const
223 {
224 DUNE_THROW(Dune::NotImplemented, "This model does not implement a fluxIntegral method!");
225 }
226
227 // \}
228
233 // \{
234
248 template<class SubControlVolume>
250 const Problem& problem,
251 const Element& element,
252 const FVElementGeometry& fvGeometry,
253 const ElementVariables& prevElemVars,
254 const ElementVariables& curElemVars,
255 const SubControlVolume& scv) const
256 {
257 // mass balance within the element. this is the
258 // \f$\frac{\partial S}{\partial t}\f$ term if using implicit or explicit
259 // euler as time discretization.
260 //
261 // TODO: We might need a more explicit way for
262 // doing the time discretization...
263
265 NumEqVector prevStorage = this->asImp().storageIntegral(fvGeometry, prevElemVars, scv, /*previous time level?*/true);
266 NumEqVector storage = this->asImp().storageIntegral(fvGeometry, curElemVars, scv, /*previous time level?*/false);
267
268 storage -= prevStorage;
269 storage /= timeLoop_->timeStepSize();
270
271 residual[scv.localDofIndex()] += storage;
272 }
273
286 template<class SubControlVolume>
288 const Problem& problem,
289 const Element& element,
290 const FVElementGeometry& fvGeometry,
291 const ElementVariables& curElemVars,
292 const SubControlVolume& scv) const
293 {
295 NumEqVector source = this->asImp().sourceIntegral(fvGeometry, curElemVars, scv);
297 residual[scv.localDofIndex()] -= source;
298 }
299
311 template<class SubControlVolumeFace>
313 const Problem& problem,
314 const Element& element,
315 const FVElementGeometry& fvGeometry,
316 const ElementVariables& elemVars,
317 const ElementBoundaryTypes& elemBcTypes,
318 const SubControlVolumeFace& scvf) const {}
319
330 template<class SubControlVolumeFace>
331 NumEqVector evalFlux(const Problem& problem,
332 const Element& element,
333 const FVElementGeometry& fvGeometry,
334 const ElementVariables& elemVars,
335 const SubControlVolumeFace& scvf) const
336 {
337 return asImp().evalFlux(problem, element, fvGeometry, elemVars, scvf);
338 }
339
340 //\}
341
345 // \{
346
348 const Problem& problem() const
349 { return *problem_; }
350
353 const TimeLoop& timeLoop() const
354 { return *timeLoop_; }
355
357 bool isStationary() const
358 { return !timeLoop_; }
359
360 // \}
361
362protected:
363 Implementation& asImp()
364 { return *static_cast<Implementation*>(this); }
365
366 const Implementation& asImp() const
367 { return *static_cast<const Implementation*>(this); }
368
369private:
370 const Problem* problem_;
371 const TimeLoop* timeLoop_;
372};
373
374} // end namespace Dumux::Experimental
375
376#endif
GVC GridVariablesCache
export type of the grid variables cache
Definition: discretization/gridvariables.hh:33
The element-wise residual for grid-based discretization schemes.
Definition: assembly/localresidual.hh:37
LocalResidual(const Problem *problem, const TimeLoop *timeLoop=nullptr)
the constructor
Definition: assembly/localresidual.hh:58
void addToElementStorageResidual(ElementResidualVector &residual, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &prevElemVars, const ElementVariables &curElemVars) const
add additional storage contributions (e.g. hybrid CVFE or FE schemes)
Definition: assembly/localresidual.hh:134
void evalFlux(ElementResidualVector &residual, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const ElementBoundaryTypes &elemBcTypes, const SubControlVolumeFace &scvf) const
Compute the fluxes of the local residual.
Definition: assembly/localresidual.hh:312
NumEqVector evalFlux(const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const SubControlVolumeFace &scvf) const
Compute the fluxes of the local residual.
Definition: assembly/localresidual.hh:331
const Problem & problem() const
the problem
Definition: assembly/localresidual.hh:348
ElementResidualVector evalStorage(const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &prevElemVars, const ElementVariables &curElemVars) const
Compute the storage local residual, i.e. the deviation of the storage term from zero for instationary...
Definition: assembly/localresidual.hh:80
void evalStorage(ElementResidualVector &residual, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &prevElemVars, const ElementVariables &curElemVars, const SubControlVolume &scv) const
Compute the storage local residual, i.e. the deviation of the storage term from zero for instationary...
Definition: assembly/localresidual.hh:249
const TimeLoop & timeLoop() const
Definition: assembly/localresidual.hh:353
void evalSource(ElementResidualVector &residual, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &curElemVars, const SubControlVolume &scv) const
Compute the source local residual, i.e. the deviation of the source term from zero.
Definition: assembly/localresidual.hh:287
bool isStationary() const
returns true if the residual is stationary
Definition: assembly/localresidual.hh:357
const Implementation & asImp() const
Definition: assembly/localresidual.hh:366
NumEqVector fluxIntegral(const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const SubControlVolumeFace &scvf) const
Calculate the flux integral of the equation.
Definition: assembly/localresidual.hh:220
NumEqVector sourceIntegral(const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const SubControlVolume &scv) const
Calculate the source term integral of the equation.
Definition: assembly/localresidual.hh:189
Implementation & asImp()
Definition: assembly/localresidual.hh:363
NumEqVector storageIntegral(const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const SubControlVolume &scv, bool isPreviousTimeLevel) const
Calculate the source term integral of the equation.
Definition: assembly/localresidual.hh:170
void addToElementFluxAndSourceResidual(ElementResidualVector &residual, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &curElemVars, const ElementBoundaryTypes &bcTypes) const
add additional flux and source contributions (e.g. hybrid CVFE or FE schemes)
Definition: assembly/localresidual.hh:143
ElementResidualVector evalFluxAndSource(const Element &element, const FVElementGeometry &fvGeometry, const ElementVariables &elemVars, const ElementBoundaryTypes &bcTypes) const
Compute the flux and source.
Definition: assembly/localresidual.hh:110
A arithmetic block vector type based on DUNE's reserved vector.
Definition: reservedblockvector.hh:26
virtual Scalar timeStepSize() const =0
Returns the suggested time step length .
The default time loop for instationary simulations.
Definition: common/timeloop.hh:139
Defines all properties used in Dumux.
Manages the handling of time dependent problems.
Helper classes to compute the integration elements.
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 volume(const Geometry &geo, unsigned int integrationOrder=4)
The volume of a given geometry.
Definition: volume.hh:159
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:296
auto quadratureRule(const FVElementGeometry &fvGeometry, const typename FVElementGeometry::SubControlVolume &scv, QuadratureRules::MidpointQuadrature)
Midpoint quadrature for scv.
Definition: quadraturerules.hh:148
Definition: assembly/assembler.hh:44
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition: extrusion.hh:166
std::ranges::range auto scvs(const FVElementGeometry &fvGeometry, const LocalDof &localDof)
Definition: localdof.hh:79
A helper to deduce a vector with the same size as numbers of equations.
Quadrature rules over sub-control volumes and sub-control volume faces.
A arithmetic block vector type based on DUNE's reserved vector.