version 3.11-dev
Loading...
Searching...
No Matches
cvfe/hybrid/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_HYBRID_CVFE_GRID_VARIABLES_CACHE_HH
13#define DUMUX_DISCRETIZATION_HYBRID_CVFE_GRID_VARIABLES_CACHE_HH
14
15#include <utility>
16#include <vector>
17#include <ranges>
18#include <memory>
19
21
22#include <dumux/common/concepts/localdofs_.hh>
23
24// make the local view function available whenever we use this class
28#include "elementvariables.hh"
29
31
32template<class P, class V, class IPD>
34{
35 using Problem = P;
36 using Variables = V;
38
39 template<class GridVariablesCache, bool cachingEnabled>
41};
42
50template<class Traits, bool enableCaching, bool enableIpDataCaching = enableCaching>
52
53// specialization in case of storing the local variables
54template<class Traits, bool enableIpDataCaching>
55class HybridCVFEGridVariablesCache<Traits, /*cachingEnabled*/true, enableIpDataCaching>
56{
58public:
60 using Problem = typename Traits::Problem;
61
63 using Variables = typename Traits::Variables;
64
66 using InterpolationPointData = typename Traits::InterpolationPointData;
67
69 static constexpr bool cachingEnabled = true;
70
72 static constexpr bool interpolationPointDataCachingEnabled = enableIpDataCaching;
73
74 static_assert(enableIpDataCaching || !InterpolationPointData::isSolDependent,
75 "Solution-dependent interpolation point data are stored together with the variables");
76
78 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
79
81 using MutableLocalView = typename LocalView::MutableView;
82
84
85 template<class GridDiscretization, class SolutionVector>
86 void init(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
87 {
88 variables_.resize(gridDiscretization.gridView().size(0));
89 if constexpr (enableIpDataCaching)
90 ipDataCache_ = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(gridDiscretization.gridView().size(0));
91
92 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
93 {
94 const auto element = gridDiscretization.element(eIdx);
95 const auto elemDisc = localView(gridDiscretization).bindElement(element);
96
97 // get the element solution
98 auto elemSol = elementSolution(element, sol, gridDiscretization);
99
100 variables_[eIdx].resize(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
101 for (const auto& localDof : localDofs(elemDisc))
102 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
103
104 if constexpr (enableIpDataCaching)
105 (*ipDataCache_)[eIdx].update(problem, element, elemDisc, variables_[eIdx]);
106 });
107 }
108
109 template<class GridDiscretization, class SolutionVector>
110 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
111 {
112 if constexpr (InterpolationPointData::isSolDependent)
113 {
114 auto newIpDataCache = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(*ipDataCache_);
115
116 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem(), newIpDataCache](const std::size_t eIdx)
117 {
118 const auto element = gridDiscretization.element(eIdx);
119 const auto elemDisc = localView(gridDiscretization).bindElement(element);
120
121 // get the element solution
122 auto elemSol = elementSolution(element, sol, gridDiscretization);
123
124 for (const auto& localDof : localDofs(elemDisc))
125 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
126
127 (*newIpDataCache)[eIdx].update(problem, element, elemDisc, variables_[eIdx]);
128 });
129
130 ipDataCache_ = std::move(newIpDataCache);
131 }
132 else
133 {
134 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
135 {
136 const auto element = gridDiscretization.element(eIdx);
137 const auto elemDisc = localView(gridDiscretization).bindElement(element);
138
139 // get the element solution
140 auto elemSol = elementSolution(element, sol, gridDiscretization);
141
142 for (const auto& localDof : localDofs(elemDisc))
143 variables_[eIdx][localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
144 });
145 }
146 }
147
148 template<class ScvOrLocalDof>
149 const Variables& variables(const ScvOrLocalDof& scvOrLocalDof) const
150 {
151 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
152 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
153 else
154 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
155 }
156
157 template<class ScvOrLocalDof>
158 Variables& variables(const ScvOrLocalDof& scvOrLocalDof)
159 {
160 if constexpr (Concept::LocalDof<ScvOrLocalDof>)
161 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.index()];
162 else
163 return variables_[scvOrLocalDof.elementIndex()][scvOrLocalDof.localDofIndex()];
164 }
165
166 const Variables& variables(const std::size_t eIdx, const std::size_t localIdx) const
167 { return variables_[eIdx][localIdx]; }
168
169 Variables& variables(const std::size_t eIdx, const std::size_t localIdx)
170 { return variables_[eIdx][localIdx]; }
171
173 const std::vector<Variables>& localDofVariables(const std::size_t eIdx) const
174 { return variables_[eIdx]; }
175
176 const InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
177 { return (*ipDataCache_)[eIdx].scvfCache(scvfIdx, qpIdx); }
178
179 InterpolationPointData& scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
180 { return (*ipDataCache_)[eIdx].scvfCache(scvfIdx, qpIdx); }
181
182 const InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx) const
183 { return (*ipDataCache_)[eIdx].elementCache(qpIdx); }
184
185 InterpolationPointData& elementCache(std::size_t eIdx, std::size_t qpIdx)
186 { return (*ipDataCache_)[eIdx].elementCache(qpIdx); }
187
188 const InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
189 { return (*ipDataCache_)[eIdx].boundaryFaceCache(bfIdx, qpIdx); }
190
191 InterpolationPointData& boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
192 { return (*ipDataCache_)[eIdx].boundaryFaceCache(bfIdx, qpIdx); }
193
194 const auto& cache(std::size_t eIdx) const
195 { return (*ipDataCache_)[eIdx]; }
196
197 auto& cache(std::size_t eIdx)
198 { return (*ipDataCache_)[eIdx]; }
199
200 const Problem& problem() const
201 { return *problemPtr_; }
202
203private:
205
206 const Problem* problemPtr_;
207 std::vector<std::vector<Variables>> variables_;
208 std::shared_ptr<std::vector<ElementInterpolationPointDataCache>> ipDataCache_;
209};
210
211// Specialization when the current local variables are not stored
212template<class Traits, bool enableIpDataCaching>
213class HybridCVFEGridVariablesCache<Traits, /*cachingEnabled*/false, enableIpDataCaching>
214{
216
217public:
219 using Problem = typename Traits::Problem;
220
222 using Variables = typename Traits::Variables;
223
225 static constexpr bool cachingEnabled = false;
226
228 using InterpolationPointData = typename Traits::InterpolationPointData;
229
231 static constexpr bool interpolationPointDataCachingEnabled = enableIpDataCaching;
232
233 static_assert(!enableIpDataCaching || !InterpolationPointData::isSolDependent,
234 "Solution-dependent interpolation point data are stored together with the variables");
235
237 using LocalView = typename Traits::template LocalView<ThisType, cachingEnabled>;
238
240 using MutableLocalView = typename LocalView::MutableView;
241
243
244 template<class GridDiscretization, class SolutionVector>
245 void init(const GridDiscretization& gridDiscretization, const SolutionVector& sol)
246 {
247 if constexpr (enableIpDataCaching)
248 {
249 auto ipDataCache = std::make_shared<std::vector<ElementInterpolationPointDataCache>>(gridDiscretization.gridView().size(0));
250 Dumux::parallelFor(gridDiscretization.gridView().size(0), [&, &problem = problem()](const std::size_t eIdx)
251 {
252 const auto element = gridDiscretization.element(eIdx);
253 const auto elemDisc = localView(gridDiscretization).bindElement(element);
254 const auto elemSol = elementSolution(element, sol, gridDiscretization);
255
256 std::vector<Variables> variables(Dumux::Detail::LocalDofs::numLocalDofs(elemDisc));
257 for (const auto& localDof : localDofs(elemDisc))
258 variables[localDof.index()].update(elemSol, problem, elemDisc, ipData(elemDisc, localDof));
259
260 (*ipDataCache)[eIdx].update(problem, element, elemDisc, variables);
261 });
262 ipDataCache_ = std::move(ipDataCache);
263 }
264 }
265
266 template<class GridDiscretization, class SolutionVector>
267 void update(const GridDiscretization& gridDiscretization, const SolutionVector& sol) {}
268
269 const auto& cache(std::size_t eIdx) const
270 { return (*ipDataCache_)[eIdx]; }
271
272 const Problem& problem() const
273 { return *problemPtr_;}
274
275private:
277
278 const Problem* problemPtr_;
279 std::shared_ptr<const std::vector<ElementInterpolationPointDataCache>> ipDataCache_;
280};
281
282} // end namespace Dumux::Experimental::CVFE
283
284#endif
Interpolation point data at the quadrature points of the sub-control volume faces,...
Definition elementinterpolationpointdatacache.hh:128
The (stencil) element variables class for hybrid control-volume finite element.
Definition cvfe/hybrid/elementvariables.hh:42
auto & cache(std::size_t eIdx)
Definition cvfe/hybrid/gridvariablescache.hh:197
InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx)
Definition cvfe/hybrid/gridvariablescache.hh:185
const std::vector< Variables > & localDofVariables(const std::size_t eIdx) const
The variables of all local dofs of an element.
Definition cvfe/hybrid/gridvariablescache.hh:173
const InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:188
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition cvfe/hybrid/gridvariablescache.hh:78
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition cvfe/hybrid/gridvariablescache.hh:81
void init(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition cvfe/hybrid/gridvariablescache.hh:86
const InterpolationPointData & scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:176
const Variables & variables(const std::size_t eIdx, const std::size_t localIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:166
static constexpr bool interpolationPointDataCachingEnabled
make it possible to query if the interpolation point data are stored for the whole grid
Definition cvfe/hybrid/gridvariablescache.hh:72
const auto & cache(std::size_t eIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:194
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition cvfe/hybrid/gridvariablescache.hh:69
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition cvfe/hybrid/gridvariablescache.hh:110
HybridCVFEGridVariablesCache(const Problem &problem)
Definition cvfe/hybrid/gridvariablescache.hh:83
const Variables & variables(const ScvOrLocalDof &scvOrLocalDof) const
Definition cvfe/hybrid/gridvariablescache.hh:149
Variables & variables(const std::size_t eIdx, const std::size_t localIdx)
Definition cvfe/hybrid/gridvariablescache.hh:169
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition cvfe/hybrid/gridvariablescache.hh:66
Variables & variables(const ScvOrLocalDof &scvOrLocalDof)
Definition cvfe/hybrid/gridvariablescache.hh:158
typename Traits::Problem Problem
export the problem type
Definition cvfe/hybrid/gridvariablescache.hh:60
InterpolationPointData & scvfCache(std::size_t eIdx, std::size_t scvfIdx, std::size_t qpIdx)
Definition cvfe/hybrid/gridvariablescache.hh:179
const InterpolationPointData & elementCache(std::size_t eIdx, std::size_t qpIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:182
typename Traits::Variables Variables
export the variables type
Definition cvfe/hybrid/gridvariablescache.hh:63
InterpolationPointData & boundaryFaceCache(std::size_t eIdx, int bfIdx, std::size_t qpIdx)
Definition cvfe/hybrid/gridvariablescache.hh:191
const Problem & problem() const
Definition cvfe/hybrid/gridvariablescache.hh:200
typename Traits::InterpolationPointData InterpolationPointData
export interpolation point data type
Definition cvfe/hybrid/gridvariablescache.hh:228
typename Traits::template LocalView< ThisType, cachingEnabled > LocalView
export the type of the local view
Definition cvfe/hybrid/gridvariablescache.hh:237
void init(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition cvfe/hybrid/gridvariablescache.hh:245
void update(const GridDiscretization &gridDiscretization, const SolutionVector &sol)
Definition cvfe/hybrid/gridvariablescache.hh:267
static constexpr bool interpolationPointDataCachingEnabled
make it possible to query if the interpolation point data are stored for the whole grid
Definition cvfe/hybrid/gridvariablescache.hh:231
static constexpr bool cachingEnabled
make it possible to query if caching is enabled
Definition cvfe/hybrid/gridvariablescache.hh:225
typename LocalView::MutableView MutableLocalView
export the type of the mutable local view
Definition cvfe/hybrid/gridvariablescache.hh:240
const auto & cache(std::size_t eIdx) const
Definition cvfe/hybrid/gridvariablescache.hh:269
typename Traits::Problem Problem
export the problem type
Definition cvfe/hybrid/gridvariablescache.hh:219
typename Traits::Variables Variables
export the variables type
Definition cvfe/hybrid/gridvariablescache.hh:222
const Problem & problem() const
Definition cvfe/hybrid/gridvariablescache.hh:272
HybridCVFEGridVariablesCache(const Problem &problem)
Definition cvfe/hybrid/gridvariablescache.hh:242
The grid variables cache class for hybrid control-volume finite element methods.
Definition cvfe/hybrid/gridvariablescache.hh:51
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 for hybrid control-volume finite element methods.
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 cvfe/hybrid/elementvariables.hh:32
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 cvfe/hybrid/gridvariablescache.hh:34
IPD InterpolationPointData
Definition cvfe/hybrid/gridvariablescache.hh:37
P Problem
Definition cvfe/hybrid/gridvariablescache.hh:35
V Variables
Definition cvfe/hybrid/gridvariablescache.hh:36
HybridCVFEElementVariables< GridVariablesCache, cachingEnabled > LocalView
Definition cvfe/hybrid/gridvariablescache.hh:40