version 3.11-dev
Loading...
Searching...
No Matches
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#include <type_traits>
17#include <vector>
18
19#include <dune/common/exceptions.hh>
20
21#include <dumux/common/typetraits/localdofs_.hh>
30
31namespace Dumux::Experimental {
32
39template<class TypeTag>
41{
46 using Element = typename GridView::template Codim<0>::Entity;
47 using ElementDiscretization = typename GetPropType<TypeTag, Properties::GridGeometry>::LocalView;
49 using GridDiscretization = GetPropType<TypeTag, Properties::GridGeometry>;
50 using Extrusion = Extrusion_t<GridDiscretization>;
52 using GridVariablesCache = typename GridVariables::GridVariablesCache;
53 using ElementVariables = typename GridVariablesCache::LocalView;
54 using TimeLoop = TimeLoopBase<Scalar>;
55
56public:
59
61 LocalResidual(const Problem* problem,
62 const TimeLoop* timeLoop = nullptr)
63 : problem_(problem)
64 , timeLoop_(timeLoop)
65 {}
66
71 // \{
72
82 const ElementDiscretization& elemDisc,
83 const ElementVariables& elemVars) const
84 {
85 checkStorageHook_();
86
87 ElementResidualVector storage(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
88
90 for (const auto& scv : scvs(elemDisc))
91 storage[scv.localDofIndex()] =
92 this->asImp().storageIntegral(elemDisc, elemVars, scv, /*isPreviousTimeLevel=*/false);
93
94 // allow models to contribute additional storage terms (e.g. hybrid CVFE/FE)
95 this->asImp().addToElementStorage(storage, this->problem(), element, elemDisc, elemVars, /*isPreviousTimeLevel=*/false);
96
97 return storage;
98 }
99
100 ElementResidualVector evalStorage(const Element& element,
101 const ElementDiscretization& elemDisc,
102 const ElementVariables& prevElemVars,
103 const ElementVariables& curElemVars) const
104 {
105 assert(!this->isStationary() && "no time loop set for storage term evaluation");
106
107 // initialize the residual vector for all local dofs in this element
108 ElementResidualVector residual(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
109
110 // evaluate the volume terms (storage + source terms)
111 // forward to the local residual specialized for the discretization methods
113 {
114 for (const auto& scv : scvs(elemDisc))
115 this->asImp().evalStorage(residual, this->problem(), element, elemDisc, prevElemVars, curElemVars, scv);
116 }
117
118 // allow for additional contributions (e.g. hybrid CVFE / FE schemes)
119 // discretized in time in the same way as the scv storage terms
120 checkStorageHook_();
121 ElementResidualVector additionalStorage(residual.size());
122 ElementResidualVector additionalPrevStorage(residual.size());
123 this->asImp().addToElementStorage(additionalStorage, this->problem(), element, elemDisc, curElemVars, /*isPreviousTimeLevel=*/false);
124 this->asImp().addToElementStorage(additionalPrevStorage, this->problem(), element, elemDisc, prevElemVars, /*isPreviousTimeLevel=*/true);
125 additionalStorage -= additionalPrevStorage;
126 additionalStorage /= timeLoop_->timeStepSize();
127 residual += additionalStorage;
128
129 return residual;
130 }
131
141 const ElementDiscretization& elemDisc,
142 const ElementVariables& elemVars) const
143 {
144 // initialize the residual vector for all local dofs in this element
145 ElementResidualVector residual(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
146
148 {
149 // evaluate the volume terms (storage + source terms)
150 // forward to the local residual specialized for the discretization methods
151 for (const auto& scv : scvs(elemDisc))
152 this->asImp().evalSource(residual, this->problem(), element, elemDisc, elemVars, scv);
153
154 // forward to the local residual specialized for the discretization methods
155 // TODO: Provide scvfs range only over interior scvfs
156 for (auto&& scvf : scvfs(elemDisc))
157 if(!scvf.boundary())
158 this->asImp().evalFlux(residual, this->problem(), element, elemDisc, elemVars, scvf);
159 }
160
161 // allow for additional contributions (e.g. hybrid CVFE / FE schemes)
162 this->asImp().addToElementFluxAndSourceResidual(residual, this->problem(), element, elemDisc, elemVars);
163
164 // point sources of local dofs without control volume
165 this->addPointSourcesOfNonCVLocalDofs(residual, this->problem(), elemDisc, elemVars);
166
167 // add boundary flux contributions
168 this->asImp().addBoundaryFluxIntegral(residual, this->problem(), elemDisc, elemVars);
169
170 return residual;
171 }
172
186 const Problem& problem,
187 const Element& element,
188 const ElementDiscretization& elemDisc,
189 const ElementVariables& elemVars,
190 bool isPreviousTimeLevel) const
191 {}
192
195 const Problem& problem,
196 const Element& element,
197 const ElementDiscretization& elemDisc,
198 const ElementVariables& curElemVars) const
199 {}
200
216 const Problem& problem,
217 const ElementDiscretization& elemDisc,
218 const ElementVariables& elemVars) const
219 {
220 if constexpr (Dumux::Detail::LocalDofs::hasNonCVLocalDofsInterface<ElementDiscretization>()
221 && requires { problem.pointSources(); })
222 {
223 const auto& pointSources = problem.pointSources();
224 if constexpr (!std::is_same_v<std::decay_t<decltype(pointSources)>, EmptyPointSources>)
225 {
226 if (pointSources.empty())
227 return;
228
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()))
233 {
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);
238 }
239 }
240 }
241 }
242
245 const Problem& problem,
246 const ElementDiscretization& elemDisc,
247 const ElementVariables& elemVars) const
248 {
249 if(!elemDisc.hasBoundaryFaces())
250 return;
251
252 for (const auto& boundaryFace : boundaryFaces(elemDisc))
253 {
254 const auto& bcTypes = problem.boundaryTypes(elemDisc, boundaryFace);
255 if(!bcTypes.hasFluxBoundary())
256 continue;
257
260 problem.addFEBoundaryFluxIntegral(residual, elemDisc, elemVars, boundaryFace, bcTypes);
261
263 for(const auto& scvf : scvfs(elemDisc, boundaryFace))
264 problem.addFVBoundaryFluxIntegral(residual, elemDisc, elemVars, scvf, bcTypes);
265 }
266 }
267
268 // \}
269
274 // \{
275
286 template<class SubControlVolume>
287 NumEqVector storageIntegral(const ElementDiscretization& elemDisc,
288 const ElementVariables& elemVars,
289 const SubControlVolume& scv,
290 bool isPreviousTimeLevel) const
291 {
292 DUNE_THROW(Dune::NotImplemented, "This model does not implement a storageIntegral method!");
293 }
294
305 template<class SubControlVolume>
306 NumEqVector sourceIntegral(const ElementDiscretization& elemDisc,
307 const ElementVariables& elemVars,
308 const SubControlVolume& scv) const
309 {
310 NumEqVector source(0.0);
311
312 const auto& problem = this->asImp().problem();
313 for (const auto& qpData : Dumux::CVFE::quadratureRule(elemDisc, scv))
314 source += qpData.weight() * problem.source(elemDisc, elemVars, qpData.ipData());
315
316 source *= elemVars[scv].extrusionFactor();
317
318 // add contribution from possible point sources
319 const auto& pointSources = problem.pointSources();
320 if (!pointSources.empty())
321 for (const auto& context : pointSources.contexts(elemDisc, scv))
322 {
323 auto psValues = pointSources.eval(elemDisc, elemVars, context);
324 source += psValues;
325 }
326
327 return source;
328 }
329
340 template<class SubControlVolumeFace>
341 NumEqVector fluxIntegral(const ElementDiscretization& elemDisc,
342 const ElementVariables& elemVars,
343 const SubControlVolumeFace& scvf) const
344 {
345 DUNE_THROW(Dune::NotImplemented, "This model does not implement a fluxIntegral method!");
346 }
347
348 // \}
349
354 // \{
355
369 template<class SubControlVolume>
371 const Problem& problem,
372 const Element& element,
373 const ElementDiscretization& elemDisc,
374 const ElementVariables& prevElemVars,
375 const ElementVariables& curElemVars,
376 const SubControlVolume& scv) const
377 {
378 // mass balance within the element. this is the
379 // \f$\frac{\partial S}{\partial t}\f$ term if using implicit or explicit
380 // euler as time discretization.
381 //
382 // TODO: We might need a more explicit way for
383 // doing the time discretization...
384
386 NumEqVector prevStorage = this->asImp().storageIntegral(elemDisc, prevElemVars, scv, /*previous time level?*/true);
387 NumEqVector storage = this->asImp().storageIntegral(elemDisc, curElemVars, scv, /*previous time level?*/false);
388
389 storage -= prevStorage;
390 storage /= timeLoop_->timeStepSize();
391
392 residual[scv.localDofIndex()] += storage;
393 }
394
407 template<class SubControlVolume>
409 const Problem& problem,
410 const Element& element,
411 const ElementDiscretization& elemDisc,
412 const ElementVariables& curElemVars,
413 const SubControlVolume& scv) const
414 {
416 NumEqVector source = this->asImp().sourceIntegral(elemDisc, curElemVars, scv);
418 residual[scv.localDofIndex()] -= source;
419 }
420
431 template<class SubControlVolumeFace>
433 const Problem& problem,
434 const Element& element,
435 const ElementDiscretization& elemDisc,
436 const ElementVariables& elemVars,
437 const SubControlVolumeFace& scvf) const {}
438
449 template<class SubControlVolumeFace>
450 NumEqVector evalFlux(const Problem& problem,
451 const Element& element,
452 const ElementDiscretization& elemDisc,
453 const ElementVariables& elemVars,
454 const SubControlVolumeFace& scvf) const
455 {
456 return asImp().evalFlux(problem, element, elemDisc, elemVars, scvf);
457 }
458
459 //\}
460
464 // \{
465
467 const Problem& problem() const
468 { return *problem_; }
469
472 const TimeLoop& timeLoop() const
473 { return *timeLoop_; }
474
476 bool isStationary() const
477 { return !timeLoop_; }
478
479 // \}
480
481protected:
482 Implementation& asImp()
483 { return *static_cast<Implementation*>(this); }
484
485 const Implementation& asImp() const
486 { return *static_cast<const Implementation*>(this); }
487
488private:
489 static constexpr void checkStorageHook_()
490 {
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.");
494 }
495
496 const Problem* problem_;
497 const TimeLoop* timeLoop_;
498};
499
500} // end namespace Dumux::Experimental
501
502#endif
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