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 <unordered_map>
17#include <utility>
18#include <vector>
19#include <ranges>
20#include <memory>
21
23
24#include <dumux/common/concepts/localdofs_.hh>
25
26// make the local view function available whenever we use this class
30#include "elementvariables.hh"
31
32namespace Dumux::Experimental {
33
34template<class P, class V, class IPD>
36{
37 using Problem = P;
38 using Variables = V;
40
41 template<class GridVariablesCache, bool cachingEnabled>
43};
44
49template<class Traits, bool enableCaching>
51
52// specialization in case of storing the local variables
53template<class Traits>
54class FEGridVariablesCache<Traits, /*cachingEnabled*/true>
55{
57public:
59 using Problem = typename Traits::Problem;
60
62 using Variables = typename Traits::Variables;
63
65 using InterpolationPointData = typename Traits::InterpolationPointData;
66
68 static constexpr bool cachingEnabled = true;
69
71 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
72
74 using MutableLocalView = typename LocalView::MutableView;
75
76 FEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
77
78 template<class GridDiscretization, class SolutionVector>
79 void init(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
80 {
81 variables_.resize(gridDiscretization.gridView().size(0));
82 ipDataCache_ = std::make_shared<InterpolationPointDataCache>();
83 ipDataCache_->resize(gridDiscretization.gridView().size(0));
84
85 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
86 {
87 const auto element = gridDiscretization.element(eIdx);
88 const auto elemDisc = localView(gridDiscretization).bindElement(element);
89
90 // get the element solution
91 auto elemSol = elementSolution(element, sol, gridDiscretization);
92
93 variables_[eIdx].resize(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
94 for (const auto& localDof : localDofs(elemDisc))
95 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
96
97 ipDataCache_->update(problem, element, elemDisc, variables_[eIdx]);
98 });
99 }
100
101 template<class GridDiscretization, class SolutionVector>
102 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
103 {
104 if constexpr (InterpolationPointData::isSolDependent)
105 {
106 auto newIpDataCache = std::make_shared<InterpolationPointDataCache>();
107 newIpDataCache->resize(gridDiscretization.gridView().size(0));
108
109 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem(), newIpDataCache](const std::size_t eIdx)
110 {
111 const auto element = gridDiscretization.element(eIdx);
112 const auto elemDisc = localView(gridDiscretization).bindElement(element);
113
114 // get the element solution
115 auto elemSol = elementSolution(element, sol, gridDiscretization);
116
117 for (const auto& localDof : localDofs(elemDisc))
118 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
119
120 newIpDataCache->update(problem, element, elemDisc, variables_[eIdx]);
121 });
122
123 ipDataCache_ = std::move(newIpDataCache);
124 }
125 else
126 {
127 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
128 {
129 const auto element = gridDiscretization.element(eIdx);
130 const auto elemDisc = localView(gridDiscretization).bindElement(element);
131
132 // get the element solution
133 auto elemSol = elementSolution(element, sol, gridDiscretization);
134
135 for (const auto& localDof : localDofs(elemDisc))
136 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
137 });
138 }
139 }
140
141 template<class LocalDof>
142 const Variables& variables(const LocalDof& localDof) const
143 { return variables_[localDof.elementIndex()][localDof.index()]; }
144
145 template<class LocalDof>
146 Variables& variables(const LocalDof& localDof)
147 { return variables_[localDof.elementIndex()][localDof.index()]; }
148
149 const Variables& variables(const std::size_t eIdx, const std::size_t localIdx) const
150 { return variables_[eIdx][localIdx]; }
151
152 Variables& variables(const std::size_t eIdx, const std::size_t localIdx)
153 { return variables_[eIdx][localIdx]; }
154
155 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
156 { return ipDataCache_->elementCache(eIdx, qpIdx); }
157
158 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
159 { return ipDataCache_->elementCache(eIdx, qpIdx); }
160
161 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
162 { return ipDataCache_->boundaryFaceCache(eIdx, bfIdx, qpIdx); }
163
164 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
165 { return ipDataCache_->boundaryFaceCache(eIdx, bfIdx, qpIdx); }
166
167 const Problem& problem() const
168 { return *problemPtr_; }
169
170private:
171 class InterpolationPointDataCache
172 {
173 public:
175 {
176 std::vector<InterpolationPointData> elementCache;
177 std::unordered_map<int, std::vector<InterpolationPointData>> boundaryFaceCache;
178
179 template<class Problem, class ElementDiscretization, class ElementVariables>
180 void update(const Problem& problem,
181 const typename ElementDiscretization::Element& element,
182 const ElementDiscretization& elemDisc,
183 const ElementVariables& elemVars)
184 {
185 const auto elemQuadRule = Dumux::CVFE::quadratureRule(elemDisc, element);
186 elementCache.resize(std::ranges::size(elemQuadRule));
187 for (const auto& qpData : elemQuadRule)
188 elementCache[qpData.ipData().qpIndex()].update(problem, element, elemDisc, elemVars, qpData.ipData());
189
190 boundaryFaceCache.clear();
191 for (const auto& boundaryFace : boundaryFaces(elemDisc))
192 {
193 auto& bfCache = boundaryFaceCache[boundaryFace.index()];
194 const auto quadRule = Dumux::CVFE::quadratureRule(elemDisc, boundaryFace);
195 bfCache.resize(std::ranges::size(quadRule));
196 for (const auto& qpData : quadRule)
197 bfCache[qpData.ipData().qpIndex()].update(problem,
198 element,
199 elemDisc,
200 elemVars,
201 qpData.ipData());
202 }
203 }
204 };
205
206 InterpolationPointDataCache()
207 {}
208
209 void resize(const std::size_t numElements)
210 { elementCaches_.resize(numElements); }
211
212 template<class Problem, class ElementDiscretization, class ElementVariables>
213 void update(const Problem& problem,
214 const typename ElementDiscretization::Element& element,
215 const ElementDiscretization& elemDisc,
216 const ElementVariables& elemVars)
217 {
218 const auto eIdx = elemDisc.gridDiscretization().elementMapper().index(element);
219 elementCaches_[eIdx].update(problem, element, elemDisc, elemVars);
220 }
221
222 // access operator
223 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
224 { return elementCaches_[eIdx].elementCache[qpIdx]; }
225
226 // access operator
227 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
228 { return elementCaches_[eIdx].elementCache[qpIdx]; }
229
230 // access operator
231 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
232 { return elementCaches_[eIdx].boundaryFaceCache.at(bfIdx)[qpIdx]; }
233
234 // access operator
235 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
236 { return elementCaches_[eIdx].boundaryFaceCache[bfIdx][qpIdx]; }
237
238 const ElementCache& cache(std::size_t eIdx) const
239 { return elementCaches_[eIdx]; }
240
241 ElementCache& cache(std::size_t eIdx)
242 { return elementCaches_[eIdx]; }
243
244 std::vector<ElementCache> elementCaches_;
245 };
246
247public:
248 const auto& cache(std::size_t eIdx) const
249 { return ipDataCache_->cache(eIdx); }
250
251 auto& cache(std::size_t eIdx)
252 { return ipDataCache_->cache(eIdx); }
253
254private:
255 const Problem* problemPtr_;
256 std::vector<std::vector<Variables>> variables_;
257 std::shared_ptr<InterpolationPointDataCache> ipDataCache_;
258};
259
260// Specialization when the current local variables are not stored
261template<class Traits>
262class FEGridVariablesCache<Traits, /*cachingEnabled*/false>
263{
265
266public:
268 using Problem = typename Traits::Problem;
269
271 using Variables = typename Traits::Variables;
272
274 static constexpr bool cachingEnabled = false;
275
277 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
278
280 using MutableLocalView = typename LocalView::MutableView;
281
283 using InterpolationPointData = typename Traits::InterpolationPointData;
284
285 FEGridVariablesCache(const Problem& problem) : problemPtr_(&problem) {}
286
287 template<class GridDiscretization, class SolutionVector>
288 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol) {}
289
290 const Problem& problem() const
291 { return *problemPtr_;}
292
293private:
294 const Problem* problemPtr_;
295};
296
297} // end namespace Dumux::Experimental
298
299#endif
The (stencil) element variables class for finite element schemes.
Definition fem/elementvariables.hh:40
FEGridVariablesCache(const Problem &problem)
Definition fem/gridvariablescache.hh:285
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:288
typename Traits::Variables Variables
export the variables type
Definition fem/gridvariablescache.hh:271
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition fem/gridvariablescache.hh:274
typename Traits::Problem Problem
export the problem type
Definition fem/gridvariablescache.hh:268
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition fem/gridvariablescache.hh:280
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition fem/gridvariablescache.hh:283
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition fem/gridvariablescache.hh:277
const Problem & problem() const
Definition fem/gridvariablescache.hh:290
const Variables & variables(const std::size_t eIdx, const std::size_t localIdx) const
Definition fem/gridvariablescache.hh:149
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition fem/gridvariablescache.hh:68
const auto & cache(std::size_t eIdx) const
Definition fem/gridvariablescache.hh:248
const Variables & variables(const LocalDof &localDof) const
Definition fem/gridvariablescache.hh:142
const InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx) const
Definition fem/gridvariablescache.hh:155
auto & cache(std::size_t eIdx)
Definition fem/gridvariablescache.hh:251
typename Traits::Variables Variables
export the variables type
Definition fem/gridvariablescache.hh:62
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition fem/gridvariablescache.hh:65
InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx)
Definition fem/gridvariablescache.hh:158
void init(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:79
Variables & variables(const LocalDof &localDof)
Definition fem/gridvariablescache.hh:146
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition fem/gridvariablescache.hh:102
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition fem/gridvariablescache.hh:71
Variables & variables(const std::size_t eIdx, const std::size_t localIdx)
Definition fem/gridvariablescache.hh:152
const InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
Definition fem/gridvariablescache.hh:161
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition fem/gridvariablescache.hh:74
typename Traits::Problem Problem
export the problem type
Definition fem/gridvariablescache.hh:59
FEGridVariablesCache(const Problem &problem)
Definition fem/gridvariablescache.hh:76
const Problem & problem() const
Definition fem/gridvariablescache.hh:167
InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
Definition fem/gridvariablescache.hh:164
Base class for the grid local variables.
Definition fem/gridvariablescache.hh:50
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
Free function to get the local view of a grid cache object.
auto quadratureRule(const FVElementGeometry &fvGeometry, const typename FVElementGeometry::SubControlVolume &scv, QuadratureRules::MidpointQuadrature)
Midpoint quadrature for scv.
Definition quadraturerules.hh:159
Definition assembly/assembler.hh:44
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:36
V Variables
Definition fem/gridvariablescache.hh:38
IPD InterpolationPointData
Definition fem/gridvariablescache.hh:39
FEElementVariables< GridVariablesCache, cachingEnabled > LocalView
Definition fem/gridvariablescache.hh:42
P Problem
Definition fem/gridvariablescache.hh:37
void update(const Problem &problem, const typename ElementDiscretization::Element &element, const ElementDiscretization &elemDisc, const ElementVariables &elemVars)
Definition fem/gridvariablescache.hh:180
std::unordered_map< int, std::vector< InterpolationPointData > > boundaryFaceCache
Definition fem/gridvariablescache.hh:177
std::vector< InterpolationPointData > elementCache
Definition fem/gridvariablescache.hh:176