12#ifndef DUMUX_BASE_LOCAL_RESIDUAL_HH
13#define DUMUX_BASE_LOCAL_RESIDUAL_HH
19#include <dune/common/exceptions.hh>
21#include <dumux/common/typetraits/localdofs_.hh>
39template<
class TypeTag>
46 using Element =
typename GridView::template Codim<0>::Entity;
53 using ElementVariables =
typename GridVariablesCache::LocalView;
82 const ElementDiscretization& elemDisc,
83 const ElementVariables& elemVars)
const
90 for (
const auto& scv :
scvs(elemDisc))
91 storage[scv.localDofIndex()] =
92 this->
asImp().storageIntegral(elemDisc, elemVars, scv,
false);
95 this->
asImp().addToElementStorage(storage, this->
problem(), element, elemDisc, elemVars,
false);
101 const ElementDiscretization& elemDisc,
102 const ElementVariables& prevElemVars,
103 const ElementVariables& curElemVars)
const
105 assert(!this->
isStationary() &&
"no time loop set for storage term evaluation");
114 for (
const auto& scv :
scvs(elemDisc))
115 this->
asImp().evalStorage(residual, this->
problem(), element, elemDisc, prevElemVars, curElemVars, scv);
123 this->
asImp().addToElementStorage(additionalStorage, this->
problem(), element, elemDisc, curElemVars,
false);
124 this->
asImp().addToElementStorage(additionalPrevStorage, this->
problem(), element, elemDisc, prevElemVars,
true);
125 additionalStorage -= additionalPrevStorage;
126 additionalStorage /= timeLoop_->timeStepSize();
127 residual += additionalStorage;
141 const ElementDiscretization& elemDisc,
142 const ElementVariables& elemVars)
const
151 for (
const auto& scv :
scvs(elemDisc))
152 this->
asImp().evalSource(residual, this->
problem(), element, elemDisc, elemVars, scv);
156 for (
auto&& scvf : scvfs(elemDisc))
158 this->
asImp().evalFlux(residual, this->
problem(), element, elemDisc, elemVars, scvf);
162 this->
asImp().addToElementFluxAndSourceResidual(residual, this->
problem(), element, elemDisc, elemVars);
168 this->
asImp().addBoundaryFluxIntegral(residual, this->
problem(), elemDisc, elemVars);
187 const Element& element,
188 const ElementDiscretization& elemDisc,
189 const ElementVariables& elemVars,
190 bool isPreviousTimeLevel)
const
196 const Element& element,
197 const ElementDiscretization& elemDisc,
198 const ElementVariables& curElemVars)
const
217 const ElementDiscretization& elemDisc,
218 const ElementVariables& elemVars)
const
220 if constexpr (Dumux::Detail::LocalDofs::hasNonCVLocalDofsInterface<ElementDiscretization>()
221 &&
requires {
problem.pointSources(); })
223 const auto& pointSources =
problem.pointSources();
224 if constexpr (!std::is_same_v<std::decay_t<
decltype(pointSources)>,
EmptyPointSources>)
226 if (pointSources.empty())
229 const auto& localBasis = elemDisc.feLocalBasis();
230 using ShapeValue =
typename std::decay_t<
decltype(localBasis)>::Traits::RangeType;
231 thread_local std::vector<ShapeValue> shapeValues;
232 for (
const auto& context : pointSources.contexts(elemDisc, elemDisc.element()))
234 const auto source = pointSources.eval(elemDisc, elemVars, context);
235 localBasis.evaluateFunction(context.localPosition(), shapeValues);
236 for (
const auto& localDof : nonCVLocalDofs(elemDisc))
237 residual[localDof.index()].axpy(-shapeValues[localDof.index()][0], source);
246 const ElementDiscretization& elemDisc,
247 const ElementVariables& elemVars)
const
249 if(!elemDisc.hasBoundaryFaces())
252 for (
const auto& boundaryFace : boundaryFaces(elemDisc))
254 const auto& bcTypes =
problem.boundaryTypes(elemDisc, boundaryFace);
255 if(!bcTypes.hasFluxBoundary())
260 problem.addFEBoundaryFluxIntegral(residual, elemDisc, elemVars, boundaryFace, bcTypes);
263 for(
const auto& scvf : scvfs(elemDisc, boundaryFace))
264 problem.addFVBoundaryFluxIntegral(residual, elemDisc, elemVars, scvf, bcTypes);
286 template<
class SubControlVolume>
288 const ElementVariables& elemVars,
289 const SubControlVolume& scv,
290 bool isPreviousTimeLevel)
const
292 DUNE_THROW(Dune::NotImplemented,
"This model does not implement a storageIntegral method!");
305 template<
class SubControlVolume>
307 const ElementVariables& elemVars,
308 const SubControlVolume& scv)
const
310 NumEqVector source(0.0);
314 source += qpData.weight() *
problem.source(elemDisc, elemVars, qpData.ipData());
316 source *= elemVars[scv].extrusionFactor();
319 const auto& pointSources =
problem.pointSources();
320 if (!pointSources.empty())
321 for (
const auto& context : pointSources.contexts(elemDisc, scv))
323 auto psValues = pointSources.eval(elemDisc, elemVars, context);
340 template<
class SubControlVolumeFace>
342 const ElementVariables& elemVars,
343 const SubControlVolumeFace& scvf)
const
345 DUNE_THROW(Dune::NotImplemented,
"This model does not implement a fluxIntegral method!");
369 template<
class SubControlVolume>
372 const Element& element,
373 const ElementDiscretization& elemDisc,
374 const ElementVariables& prevElemVars,
375 const ElementVariables& curElemVars,
376 const SubControlVolume& scv)
const
386 NumEqVector prevStorage = this->
asImp().storageIntegral(elemDisc, prevElemVars, scv,
true);
387 NumEqVector storage = this->
asImp().storageIntegral(elemDisc, curElemVars, scv,
false);
389 storage -= prevStorage;
390 storage /= timeLoop_->timeStepSize();
392 residual[scv.localDofIndex()] += storage;
407 template<
class SubControlVolume>
410 const Element& element,
411 const ElementDiscretization& elemDisc,
412 const ElementVariables& curElemVars,
413 const SubControlVolume& scv)
const
416 NumEqVector source = this->
asImp().sourceIntegral(elemDisc, curElemVars, scv);
418 residual[scv.localDofIndex()] -= source;
431 template<
class SubControlVolumeFace>
434 const Element& element,
435 const ElementDiscretization& elemDisc,
436 const ElementVariables& elemVars,
437 const SubControlVolumeFace& scvf)
const {}
449 template<
class SubControlVolumeFace>
451 const Element& element,
452 const ElementDiscretization& elemDisc,
453 const ElementVariables& elemVars,
454 const SubControlVolumeFace& scvf)
const
456 return asImp().evalFlux(
problem, element, elemDisc, elemVars, scvf);
468 {
return *problem_; }
473 {
return *timeLoop_; }
477 {
return !timeLoop_; }
483 {
return *
static_cast<Implementation*
>(
this); }
486 {
return *
static_cast<const Implementation*
>(
this); }
489 static constexpr void checkStorageHook_()
491 static_assert(!
requires { &Implementation::addToElementStorageResidual; },
492 "addToElementStorageResidual(prevElemVars, curElemVars) is not called anymore. "
493 "Implement addToElementStorage(..., elemVars, isPreviousTimeLevel) returning the storage at one time level.");
496 const Problem* problem_;
497 const TimeLoop* timeLoop_;
GVC GridVariablesCache
export type of the grid variables cache
Definition discretization/gridvariables.hh:33
ElementResidualVector evalStorage(const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &prevElemVars, const ElementVariables &curElemVars) const
Definition assembly/localresidual.hh:100
LocalResidual(const Problem *problem, const TimeLoop *timeLoop=nullptr)
the constructor
Definition assembly/localresidual.hh:61
void evalSource(ElementResidualVector &residual, const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, 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:408
NumEqVector sourceIntegral(const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const SubControlVolume &scv) const
Calculate the source term integral of the equation.
Definition assembly/localresidual.hh:306
NumEqVector evalFlux(const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const SubControlVolumeFace &scvf) const
Compute the fluxes of the local residual.
Definition assembly/localresidual.hh:450
ElementResidualVector evalFluxAndSource(const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars) const
Compute the flux and source.
Definition assembly/localresidual.hh:140
void addToElementStorage(ElementResidualVector &storage, const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars, bool isPreviousTimeLevel) const
Add additional storage contributions at one time level (e.g. hybrid CVFE or FE schemes).
Definition assembly/localresidual.hh:185
const Problem & problem() const
the problem
Definition assembly/localresidual.hh:467
const TimeLoop & timeLoop() const
Definition assembly/localresidual.hh:472
bool isStationary() const
returns true if the residual is stationary
Definition assembly/localresidual.hh:476
const Implementation & asImp() const
Definition assembly/localresidual.hh:485
void evalFlux(ElementResidualVector &residual, const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const SubControlVolumeFace &scvf) const
Compute the fluxes of the local residual.
Definition assembly/localresidual.hh:432
NumEqVector storageIntegral(const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const SubControlVolume &scv, bool isPreviousTimeLevel) const
Calculate the source term integral of the equation.
Definition assembly/localresidual.hh:287
void addPointSourcesOfNonCVLocalDofs(ElementResidualVector &residual, const Problem &problem, const ElementDiscretization &elemDisc, const ElementVariables &elemVars) const
Add the point sources to the local dofs without control volume.
Definition assembly/localresidual.hh:215
void addBoundaryFluxIntegral(ElementResidualVector &residual, const Problem &problem, const ElementDiscretization &elemDisc, const ElementVariables &elemVars) const
add boundary flux contributions
Definition assembly/localresidual.hh:244
void addToElementFluxAndSourceResidual(ElementResidualVector &residual, const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &curElemVars) const
add additional flux and source contributions (e.g. hybrid CVFE or FE schemes)
Definition assembly/localresidual.hh:194
ElementResidualVector evalStorageCurrentLevel(const Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars) const
Evaluate the raw (undivided) storage at the current time level.
Definition assembly/localresidual.hh:81
Implementation & asImp()
Definition assembly/localresidual.hh:482
void evalStorage(ElementResidualVector &residual, const Problem &problem, const Element &element, const ElementDiscretization &elemDisc, 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:370
NumEqVector fluxIntegral(const ElementDiscretization &elemDisc, const ElementVariables &elemVars, const SubControlVolumeFace &scvf) const
Calculate the flux integral of the equation.
Definition assembly/localresidual.hh:341
ReservedBlockVector< NumEqVector, Dumux::Detail::LocalDofs::maxNumLocalDofs< ElementDiscretization >()> ElementResidualVector
the container storing all element residuals
Definition assembly/localresidual.hh:58
A arithmetic block vector type based on DUNE's reserved vector.
Definition reservedblockvector.hh:26
Manages the handling of time dependent problems.
Definition common/timeloop.hh:84
Defines all properties used in Dumux.
Manages the handling of time dependent problems.
Concept for pure finite-element discretizations (no FV structure).
Definition concepts.hh:50
Concept for finite-volume discretizations.
Definition concepts.hh:26
Concept for hybrid finite-element/finite-volume discretizations.
Definition concepts.hh:39
Concepts for discretization types.
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
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:171
Definition assembly/assembler.hh:45
std::ranges::range auto scvs(const FVElementGeometry &fvGeometry, const LocalDof &localDof)
Definition localdof.hh:82
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition extrusion.hh:257
A helper to deduce a vector with the same size as numbers of equations.
Point source types and helpers for handling point sources.
Quadrature rules over sub-control volumes and sub-control volume faces.
A arithmetic block vector type based on DUNE's reserved vector.
Empty point sources.
Definition pointsources.hh:43