version 3.11-dev
Loading...
Searching...
No Matches
fem/gridvariablescache.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_DISCRETIZATION_FE_GRID_LOCAL_VARIABLES_HH
13#define DUMUX_DISCRETIZATION_FE_GRID_LOCAL_VARIABLES_HH
14
15#include <type_traits>
16#include <utility>
17#include <vector>
18#include <ranges>
19#include <memory>
20
22
23#include <dumux/common/concepts/localdofs_.hh>
24
25// make the local view function available whenever we use this class
29#include "elementvariables.hh"
30
31namespace Dumux::Experimental {
32
33template<class P, class V, class IPD>
35{
36 using Problem = P;
37 using Variables = V;
39
40 template<class GridVariablesCache, bool cachingEnabled>
42};
43
51template<class Traits, bool enableCaching, bool enableIpDataCaching = enableCaching>
53
54// specialization in case of storing the local variables
55template<class Traits, bool enableIpDataCaching>
56class FEGridVariablesCache<Traits, /*cachingEnabled*/true, enableIpDataCaching>
57{
59public:
61 using Problem = typename Traits::Problem;
62
64 using Variables = typename Traits::Variables;
65
67 using InterpolationPointData = typename Traits::InterpolationPointData;
68
70 static constexpr bool cachingEnabled = true;
71
73 static constexpr bool interpolationPointDataCachingEnabled = enableIpDataCaching;
74
75 static_assert(enableIpDataCaching || !InterpolationPointData::isSolDependent,
76 "Solution-dependent interpolation point data are stored together with the variables");
77
79 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
80
82 using MutableLocalView = typename LocalView::MutableView;
83
84 FEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
85
86 template<class GridDiscretization, class SolutionVector>
87 void init(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
88 {
89 variables_.resize(gridDiscretization.gridView().size(0));
90 if constexpr (enableIpDataCaching)
91 ipDataCache_ = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(gridDiscretization.gridView().size(0));
92
93 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
94 {
95 const auto element = gridDiscretization.element(eIdx);
96 const auto elemDisc = localView(gridDiscretization).bindElement(element);
97
98 // get the element solution
99 auto elemSol = elementSolution(element, sol, gridDiscretization);
100
101 variables_[eIdx].resize(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
102 for (const auto& localDof : localDofs(elemDisc))
103 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
104
105 if constexpr (enableIpDataCaching)
106 (*ipDataCache_)[eIdx].update(problem, element, elemDisc, variables_[eIdx]);
107 });
108 }
109
110 template<class GridDiscretization, class SolutionVector>
111 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
112 {
113 if constexpr (InterpolationPointData::isSolDependent)
114 {
115 auto newIpDataCache = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(*ipDataCache_);
116
117 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem(), newIpDataCache](const std::size_t eIdx)
118 {
119 const auto element = gridDiscretization.element(eIdx);
120 const auto elemDisc = localView(gridDiscretization).bindElement(element);
121
122 // get the element solution
123 auto elemSol = elementSolution(element, sol, gridDiscretization);
124
125 for (const auto& localDof : localDofs(elemDisc))
126 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
127
128 (*newIpDataCache)[eIdx].update(problem, element, elemDisc, variables_[eIdx]);
129 });
130
131 ipDataCache_ = std::move(newIpDataCache);
132 }
133 else
134 {
135 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
136 {
137 const auto element = gridDiscretization.element(eIdx);
138 const auto elemDisc = localView(gridDiscretization).bindElement(element);
139
140 // get the element solution
141 auto elemSol = elementSolution(element, sol, gridDiscretization);
142
143 for (const auto& localDof : localDofs(elemDisc))
144 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
145 });
146 }
147 }
148
149 template<class LocalDof>
150 const Variables& variables(const LocalDof& localDof) const
151 { return variables_[localDof.elementIndex()][localDof.index()]; }
152
153 template<class LocalDof>
154 Variables& variables(const LocalDof& localDof)
155 { return variables_[localDof.elementIndex()][localDof.index()]; }
156
157 const Variables& variables(const std::size_t eIdx, const std::size_t localIdx) const
158 { return variables_[eIdx][localIdx]; }
159
160 Variables& variables(const std::size_t eIdx, const std::size_t localIdx)
161 { return variables_[eIdx][localIdx]; }
162
164 const std::vector<Variables>& localDofVariables(const std::size_t eIdx) const
165 { return variables_[eIdx]; }
166
167 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
168 { return (*ipDataCache_)[eIdx].elementCache(qpIdx); }
169
170 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
171 { return (*ipDataCache_)[eIdx].elementCache(qpIdx); }
172
173 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
174 { return (*ipDataCache_)[eIdx].boundaryFaceCache(bfIdx, qpIdx); }
175
176 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
177 { return (*ipDataCache_)[eIdx].boundaryFaceCache(bfIdx, qpIdx); }
178
179 const auto& cache(std::size_t eIdx) const
180 { return (*ipDataCache_)[eIdx]; }
181
182 auto& cache(std::size_t eIdx)
183 { return (*ipDataCache_)[eIdx]; }
184
185 const Problem& problem() const
186 { return *problemPtr_; }
187
188private:
190
191 const Problem* problemPtr_;
192 std::vector<std::vector<Variables>> variables_;
193 std::shared_ptr<std::vector<ElementInterpolationPointDataCache>> ipDataCache_;
194};
195
196// Specialization when the current local variables are not stored
197template<class Traits, bool enableIpDataCaching>
198class FEGridVariablesCache<Traits, /*cachingEnabled*/false, enableIpDataCaching>
199{
201
202public:
204 using Problem = typename Traits::Problem;
205
207 using Variables = typename Traits::Variables;
208
210 static constexpr bool cachingEnabled = false;
211
213 using InterpolationPointData = typename Traits::InterpolationPointData;
214
216 static constexpr bool interpolationPointDataCachingEnabled = enableIpDataCaching;
217
218 static_assert(!enableIpDataCaching || !InterpolationPointData::isSolDependent,
219 "Solution-dependent interpolation point data are stored together with the variables");
220
222 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
223
225 using MutableLocalView = typename LocalView::MutableView;
226
227 FEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
228
229 template<class GridDiscretization, class SolutionVector>
230 void init(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
231 {
232 if constexpr (enableIpDataCaching)
233 {
234 auto ipDataCache = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(gridDiscretization.gridView().size(0));
235 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
236 {
237 const auto element = gridDiscretization.element(eIdx);
238 const auto elemDisc = localView(gridDiscretization).bindElement(element);
239 const auto elemSol = elementSolution(element, sol, gridDiscretization);
240
241 std::vector<Variables> variables(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
242 for (const auto& localDof : localDofs(elemDisc))
243 variables[localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
244
245 (*ipDataCache)[eIdx].update(problem, element, elemDisc, variables);
246 });
247 ipDataCache_ = std::move(ipDataCache);
248 }
249 }
250
251 template<class GridDiscretization, class SolutionVector>
252 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol) {}
253
254 const auto& cache(std::size_t eIdx) const
255 { return (*ipDataCache_)[eIdx]; }
256
257 const Problem& problem() const
258 { return *problemPtr_;}
259
260private:
262
263 const Problem* problemPtr_;
264 std::shared_ptr<const std::vector<ElementInterpolationPointDataCache>> ipDataCache_;
265};
266
267} // end namespace Dumux::Experimental
268
269#endif
Interpolation point data at the quadrature points of an element and of its boundary faces.
Definition elementinterpolationpointdatacache.hh:80
The (stencil) element variables class for finite element schemes.
Definition fem/elementvariables.hh:40
typename Traits::Variables Variables
export the variables type
Definition fem/gridvariablescache.hh:207
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition fem/gridvariablescache.hh:222
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition fem/gridvariablescache.hh:213
const Problem & problem() const
Definition fem/gridvariablescache.hh:257
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:252
static constexpr bool interpolationPointDataCachingEnabled
make it possible to query if the interpolation point data are stored for the whole grid
Definition fem/gridvariablescache.hh:216
typename Traits::Problem Problem
export the problem type
Definition fem/gridvariablescache.hh:204
const auto & cache(std::size_t eIdx) const
Definition fem/gridvariablescache.hh:254
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition fem/gridvariablescache.hh:225
void init(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:230
FEGridVariablesCache(const Problem &problem)
Definition fem/gridvariablescache.hh:227
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition fem/gridvariablescache.hh:210
const Variables & variables(const std::size_t eIdx, const std::size_t localIdx) const
Definition fem/gridvariablescache.hh:157
typename Traits::Variables Variables
export the variables type
Definition fem/gridvariablescache.hh:64
const InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx) const
Definition fem/gridvariablescache.hh:167
auto & cache(std::size_t eIdx)
Definition fem/gridvariablescache.hh:182
InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx)
Definition fem/gridvariablescache.hh:170
const Problem & problem() const
Definition fem/gridvariablescache.hh:185
const Variables & variables(const LocalDof &localDof) const
Definition fem/gridvariablescache.hh:150
const InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
Definition fem/gridvariablescache.hh:173
InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
Definition fem/gridvariablescache.hh:176
const std::vector< Variables > & localDofVariables(const std::size_t eIdx) const
The variables of all local dofs of an element.
Definition fem/gridvariablescache.hh:164
Variables & variables(const LocalDof &localDof)
Definition fem/gridvariablescache.hh:154
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition fem/gridvariablescache.hh:67
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:111
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition fem/gridvariablescache.hh:82
FEGridVariablesCache(const Problem &problem)
Definition fem/gridvariablescache.hh:84
typename Traits::Problem Problem
export the problem type
Definition fem/gridvariablescache.hh:61
static constexpr bool interpolationPointDataCachingEnabled
make it possible to query if the interpolation point data are stored for the whole grid
Definition fem/gridvariablescache.hh:73
const auto & cache(std::size_t eIdx) const
Definition fem/gridvariablescache.hh:179
void init(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:87
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition fem/gridvariablescache.hh:79
Variables & variables(const std::size_t eIdx, const std::size_t localIdx)
Definition fem/gridvariablescache.hh:160
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition fem/gridvariablescache.hh:70
Base class for the grid local variables.
Definition fem/gridvariablescache.hh:52
Base class for all standard finite volume or finite element problems.
Definition common/problem.hh:39
The local element solution class for control-volume finite element methods.
The element variables class.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition localview.hh:26
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethods::cctpfa||GridGeometry::discMethod==DiscretizationMethods::ccmpfa, CCElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for cell-centered schemes.
Definition cellcentered/elementsolution.hh:101
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading).
Definition parallel_for.hh:160
decltype(auto) gridDiscretization(const T &t, Args &&... args)
The grid discretization.
Definition griddiscretization.hh:65
Free function to get the local view of a grid cache object.
Definition assembly/assembler.hh:46
auto localDofs(const FVElementGeometry &fvGeometry)
range over local dofs
Definition localdof.hh:50
Parallel for loop (multithreading).
Quadrature rules over sub-control volumes and sub-control volume faces.
Definition fem/gridvariablescache.hh:35
V Variables
Definition fem/gridvariablescache.hh:37
IPD InterpolationPointData
Definition fem/gridvariablescache.hh:38
FEElementVariables< GridVariablesCache, cachingEnabled > LocalView
Definition fem/gridvariablescache.hh:41
P Problem
Definition fem/gridvariablescache.hh:36