version 3.11-dev
Loading...
Searching...
No Matches
fem/fegriddiscretization.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_DISCRETIZATION_HH
13#define DUMUX_DISCRETIZATION_FE_GRID_DISCRETIZATION_HH
14
15#include <span>
16#include <ranges>
17#include <utility>
18#include <vector>
19#include <unordered_map>
20
21#include <dune/common/reservedvector.hh>
22
24
29
32
33namespace Dumux::Experimental {
34
41template<class GV, class Traits>
43: public BaseGridGeometry<GV, Traits>
44{
45 using ThisType = FEGridDiscretization<GV, Traits>;
46 using ParentType = BaseGridGeometry<GV, Traits>;
47 using GridIndexType = typename IndexTraits<GV>::GridIndex;
48 using LocalIndexType = typename IndexTraits<GV>::LocalIndex;
49 static const int dim = GV::dimension;
50
51public:
53 using DiscretizationMethod = typename Traits::DiscretizationMethod;
54 static constexpr DiscretizationMethod discMethod{};
55
56 static constexpr std::size_t maxNumElementDofs = Traits::maxNumElementDofs;
57
60
64 using LocalView = typename Traits::template LocalView<ThisType, true>;
68 using DofMapper = typename Traits::DofMapper;
70 using FeCache = typename Traits::FeCache;
72 using GridView = GV;
76 using ElementQuadratureRule = typename Traits::ElementQuadratureRule;
78 using IntersectionQuadratureRule = typename Traits::IntersectionQuadratureRule;
80 using BoundaryFaceQuadratureRule = typename Traits::BoundaryFaceQuadratureRule;
81
83 FEGridDiscretization(std::shared_ptr<BasicGridGeometry> gd)
84 : ParentType(std::move(gd))
85 , dofMapper_(this->gridView(), Traits::layout())
86 , cache_(*this)
87 , periodicGridTraits_(this->gridView().grid())
88 {
89 update_();
90 }
91
96
98 const DofMapper& dofMapper() const
99 { return dofMapper_; }
100
102 std::size_t numDofs() const
103 { return this->dofMapper().size(); }
104
107 {
109 update_();
110 }
111
114 {
115 ParentType::update(std::move(gridView));
116 update_();
117 }
118
120 const FeCache& feCache() const
121 { return feCache_; }
122
124 bool dofOnBoundary(GridIndexType dofIdx) const
125 { return boundaryDofIndices_[dofIdx]; }
126
128 bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
129 { return periodicDofMap_.count(dofIdx); }
130
132 [[deprecated("Will be removed after release 3.11. Use periodicallyMappedDofs, returning a range of dofs")]]
133 GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
134 { return periodicDofMap_.at(dofIdx)[0]; }
135
137 const std::ranges::range auto periodicallyMappedDofs(GridIndexType dofIdx) const
138 { return std::views::all(periodicDofMap_.at(dofIdx)); }
139
141 const std::unordered_map<GridIndexType, std::vector<GridIndexType>>& periodicDofMap() const
142 { return periodicDofMap_; }
143
145 friend inline LocalView localView(const FEGridDiscretization& gd)
146 { return { gd.cache_ }; }
147
148private:
149 class FEGridDiscretizationCache
150 {
151 friend class FEGridDiscretization;
152 public:
154 using DofHelper = typename Traits::DofHelper;
155
156 explicit FEGridDiscretizationCache(const FEGridDiscretization& gd)
157 : gridDiscretization_(&gd)
158 {}
159
160 const FEGridDiscretization& gridDiscretization() const
161 { return *gridDiscretization_; }
162
164 auto boundaryFaces(GridIndexType eIdx) const -> std::span<const BoundaryFace>
165 {
166 if (auto it = boundaryFaces_.find(eIdx); it != boundaryFaces_.end())
167 return {it->second};
168 return {};
169 }
170
171 private:
172 void clear_()
173 { boundaryFaces_.clear(); }
174
175 std::unordered_map<GridIndexType, Dune::ReservedVector<BoundaryFace, 2*dim>> boundaryFaces_;
176
177 const FEGridDiscretization* gridDiscretization_;
178 };
179
180public:
183 using Cache = FEGridDiscretizationCache;
184
185private:
187 void update_()
188 {
189 using DofHelper = typename Cache::DofHelper;
190
191 dofMapper_.update(this->gridView());
192 cache_.clear_();
193 boundaryDofIndices_.assign(numDofs(), false);
194 periodicDofMap_.clear();
195
196 for (const auto& element : elements(this->gridView()))
197 {
198 auto elementGeometry = element.geometry();
199 const auto& localCoefficients = this->feCache().get(element.type()).localCoefficients();
200 const auto eIdx = this->elementMapper().index(element);
201
202 LocalIndexType numBoundaryFaces = 0;
203 for (const auto& intersection : intersections(this->gridView(), element))
204 {
205 if (intersection.boundary() && !periodicGridTraits_.isPeriodic(intersection))
206 {
207 // add one boundary face per boundary intersection
208 const auto isGeometry = intersection.geometry();
209 cache_.boundaryFaces_[eIdx].push_back(BoundaryFace{
210 isGeometry.center(),
211 isGeometry.volume(),
212 intersection.centerUnitOuterNormal(),
213 numBoundaryFaces++,
214 static_cast<LocalIndexType>(intersection.indexInInside()),
215 typename BoundaryFace::Traits::BoundaryFlag{intersection}
216 });
217
218 // mark all dofs on this intersection as boundary dofs
219 for (LocalIndexType keyIdx = 0; keyIdx < localCoefficients.size(); ++keyIdx)
220 {
221 if (DofHelper::localDofOnIntersection(elementGeometry.type(),
222 intersection.indexInInside(),
223 localCoefficients.localKey(keyIdx)))
224 {
225 const auto dofIdxGlobal = DofHelper::dofIndex(
226 this->dofMapper(), element, localCoefficients.localKey(keyIdx));
227 boundaryDofIndices_[dofIdxGlobal] = true;
228 }
229 }
230 }
231
232 // inform the grid discretization if we have periodic boundaries
233 else if (periodicGridTraits_.isPeriodic(intersection))
234 {
235 this->setPeriodic();
236
237 const auto eps = 1e-7*(elementGeometry.corner(1) - elementGeometry.corner(0)).two_norm();
238 for (int localDofIdx = 0; localDofIdx < localCoefficients.size(); ++localDofIdx)
239 {
240 if (!DofHelper::localDofOnIntersection(elementGeometry.type(),
241 intersection.indexInInside(),
242 localCoefficients.localKey(localDofIdx)))
243 continue;
244
245 const auto dofIdxGlobal = DofHelper::dofIndex(
246 this->dofMapper(), element, localCoefficients.localKey(localDofIdx));
247 const auto dofPos = DofHelper::dofPosition(elementGeometry, localCoefficients.localKey(localDofIdx));
248
249 const auto& outside = intersection.outside();
250 const auto outsideGeometry = outside.geometry();
251 const auto& localCoefficientsOut = this->feCache().get(outsideGeometry.type()).localCoefficients();
252 for (const auto& isOutside : intersections(this->gridView(), outside))
253 {
254 if (periodicGridTraits_.isPeriodic(isOutside))
255 {
256 for (int localDofIdxOut = 0; localDofIdxOut < localCoefficientsOut.size(); ++localDofIdxOut)
257 {
258 const auto& localKeyOut = localCoefficientsOut.localKey(localDofIdxOut);
259 if (!DofHelper::localDofOnIntersection(outsideGeometry.type(),
260 isOutside.indexInInside(),
261 localKeyOut))
262 continue;
263
264 const auto dofIdxGlobalOut = DofHelper::dofIndex(this->dofMapper(), outside, localKeyOut);
265 const auto dofPosOutside = DofHelper::dofPosition(outsideGeometry, localKeyOut);
266 const auto shift = std::abs((this->bBoxMax()-this->bBoxMin())*intersection.centerUnitOuterNormal());
267 if ((dofPosOutside - dofPos + shift*intersection.centerUnitOuterNormal()).two_norm() < eps)
268 Dumux::Detail::addPeriodicallyMappedDof(periodicDofMap_, dofIdxGlobal, dofIdxGlobalOut);
269 }
270 }
271 }
272 }
273 }
274 }
275 }
276
277 Dumux::Detail::closePeriodicDofMap(periodicDofMap_);
278
279 // error check: periodic boundaries currently don't work in parallel
280 if (this->isPeriodic() && this->gridView().comm().size() > 1)
281 DUNE_THROW(Dune::NotImplemented, "Periodic boundaries for FE method for parallel simulations!");
282 }
283
284 DofMapper dofMapper_;
285 const FeCache feCache_;
286 // dofs on the boundary
287 std::vector<bool> boundaryDofIndices_;
288 // a map for periodic boundary dofs
289 std::unordered_map<GridIndexType, std::vector<GridIndexType>> periodicDofMap_;
290 Cache cache_;
292};
293
294} // end namespace Dumux::Experimental
295
296#endif
Base class for grid geometries.
Implementation of a boundary face related to primary grid elements (dune intersections).
const ElementMapper & elementMapper() const
Returns the mapper for elements to indices for constant grids.
Definition basegridgeometry.hh:112
void setPeriodic(bool value=true)
Set the periodicity of the grid geometry.
Definition basegridgeometry.hh:169
const GlobalCoordinate & bBoxMax() const
The coordinate of the corner of the GridView's bounding box with the largest values.
Definition basegridgeometry.hh:156
Element element(GridIndexType eIdx) const
Get an element from a global element index.
Definition basegridgeometry.hh:142
const GridView & gridView() const
Return the gridView this grid geometry object lives on.
Definition basegridgeometry.hh:100
void update(const GridView &gridView)
Update all fvElementGeometries (call this after grid adaption).
Definition basegridgeometry.hh:88
const GlobalCoordinate & bBoxMin() const
The coordinate of the corner of the GridView's bounding box with the smallest values.
Definition basegridgeometry.hh:149
bool isPeriodic() const
Returns if the grid geometry is periodic (at all).
Definition basegridgeometry.hh:162
Class for a boundary face related to primary grid elements (dune intersections).
Definition boundaryface.hh:69
Default class for finite element grid discretizations.
Definition fem/fegriddiscretization.hh:44
typename Traits::BoundaryFaceQuadratureRule BoundaryFaceQuadratureRule
the quadrature rule type for boundary faces
Definition fem/fegriddiscretization.hh:80
Experimental::BoundaryFace< GV > BoundaryFace
export the boundary face type
Definition fem/fegriddiscretization.hh:59
friend LocalView localView(const FEGridDiscretization &gd)
local view of this object (constructed with the internal cache)
Definition fem/fegriddiscretization.hh:145
typename Traits::ElementQuadratureRule ElementQuadratureRule
the quadrature rule type for elements
Definition fem/fegriddiscretization.hh:76
BasicGridGeometry_t< GV, Traits > BasicGridGeometry
export basic grid geometry type for the alternative constructor
Definition fem/fegriddiscretization.hh:62
typename Traits::template LocalView< ThisType, true > LocalView
export the type of the element discretization (the local view type)
Definition fem/fegriddiscretization.hh:64
const std::ranges::range auto periodicallyMappedDofs(GridIndexType dofIdx) const
The indices of the d.o.f.s on the other side of the periodic boundaries.
Definition fem/fegriddiscretization.hh:137
GridIndexType periodicallyMappedDof(GridIndexType dofIdx) const
The index of the d.o.f. on the other side of the periodic boundary.
Definition fem/fegriddiscretization.hh:133
void update(const GridView &gridView)
update all geometries (call this after grid adaption)
Definition fem/fegriddiscretization.hh:106
GV GridView
export the grid view type
Definition fem/fegriddiscretization.hh:72
typename PeriodicGridTraits< typename GV::Grid >::SupportsPeriodicity SupportsPeriodicity
export whether the grid supports periodicity
Definition fem/fegriddiscretization.hh:74
static constexpr DiscretizationMethod discMethod
Definition fem/fegriddiscretization.hh:54
bool dofOnPeriodicBoundary(GridIndexType dofIdx) const
If a d.o.f. is on a periodic boundary.
Definition fem/fegriddiscretization.hh:128
typename Traits::IntersectionQuadratureRule IntersectionQuadratureRule
the quadrature rule type for intersections
Definition fem/fegriddiscretization.hh:78
bool dofOnBoundary(GridIndexType dofIdx) const
If a d.o.f. is on the boundary.
Definition fem/fegriddiscretization.hh:124
FEGridDiscretization(const GridView &gridView)
Constructor.
Definition fem/fegriddiscretization.hh:93
const DofMapper & dofMapper() const
The dof mapper.
Definition fem/fegriddiscretization.hh:98
std::size_t numDofs() const
The total number of degrees of freedom.
Definition fem/fegriddiscretization.hh:102
void update(GridView &&gridView)
update all geometries (call this after grid adaption)
Definition fem/fegriddiscretization.hh:113
const std::unordered_map< GridIndexType, std::vector< GridIndexType > > & periodicDofMap() const
Returns the map between dofs across periodic boundaries.
Definition fem/fegriddiscretization.hh:141
FEGridDiscretizationCache Cache
Definition fem/fegriddiscretization.hh:183
const FeCache & feCache() const
The finite element cache for creating local FE bases.
Definition fem/fegriddiscretization.hh:120
typename Traits::FeCache FeCache
export the finite element cache type
Definition fem/fegriddiscretization.hh:70
typename Traits::DiscretizationMethod DiscretizationMethod
export the discretization method this geometry belongs to
Definition fem/fegriddiscretization.hh:53
Extrusion_t< Traits > Extrusion
export the type of extrusion
Definition fem/fegriddiscretization.hh:66
static constexpr std::size_t maxNumElementDofs
Definition fem/fegriddiscretization.hh:56
typename Traits::DofMapper DofMapper
export dof mapper type
Definition fem/fegriddiscretization.hh:68
FEGridDiscretization(std::shared_ptr< BasicGridGeometry > gd)
Constructor.
Definition fem/fegriddiscretization.hh:83
Helper classes to compute the integration elements.
void closePeriodicDofMap(std::unordered_map< Index, std::vector< Index > > &periodicDofMap)
Map every degree of freedom to all others it is identified with.
Definition periodicdofmap.hh:48
Dune::Std::detected_or_t< Dumux::BasicGridGeometry< GV, typename T::ElementMapper, typename T::VertexMapper >, Detail::SpecifiesBaseGridGeometry, T > BasicGridGeometry_t
Type of the basic grid geometry implementation used as backend.
Definition basegridgeometry.hh:38
void addPeriodicallyMappedDof(std::unordered_map< Index, std::vector< Index > > &periodicDofMap, std::type_identity_t< Index > dofIdx, std::type_identity_t< Index > periodicDofIdx)
Register a degree of freedom as periodically mapped to another one.
Definition periodicdofmap.hh:29
BaseGridGeometry(std::shared_ptr< BaseImplementation > impl)
Constructor from a BaseImplementation.
Definition basegridgeometry.hh:72
decltype(auto) gridDiscretization(const T &t, Args &&... args)
The grid discretization.
Definition griddiscretization.hh:65
Defines the index types used for grid and local indices.
Definition assembly/assembler.hh:46
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition extrusion.hh:257
Helpers for maps between degrees of freedom identified across periodic boundaries.
Grid properties related to periodicity.
Quadrature rules over sub-control volumes and sub-control volume faces.
typename GridView::IndexSet::IndexType GridIndex
Definition indextraits.hh:27
unsigned int LocalIndex
Definition indextraits.hh:28
Definition periodicgridtraits.hh:24
Definition periodicgridtraits.hh:23