version 3.10-dev
discretization/staggered/fvelementgeometry.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-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_DISCRETIZATION_STAGGERED_FV_ELEMENT_GEOMETRY_HH
13#define DUMUX_DISCRETIZATION_STAGGERED_FV_ELEMENT_GEOMETRY_HH
14
15#include <optional>
16#include <bitset>
17
21
22namespace Dumux {
23
33template<class GG, bool enableGridGeometryCache>
35
44template<class GG>
45class StaggeredFVElementGeometry<GG, true> : public CCTpfaFVElementGeometry<GG, true>
46{
48 using GridView = typename GG::GridView;
49 using GridIndexType = typename IndexTraits<GridView>::GridIndex;
50 using LocalIndexType = typename IndexTraits<GridView>::LocalIndex;
51public:
53 using Element = typename GridView::template Codim<0>::Entity;
55 using SubControlVolumeFace = typename GG::SubControlVolumeFace;
56
57 using ParentType::ParentType;
58
61 template<class CellCenterOrFaceFVGridGeometry>
62 StaggeredFVElementGeometry(const CellCenterOrFaceFVGridGeometry& gridGeometry)
63 : ParentType(gridGeometry.actualGridGeometry()) {}
64
66 using ParentType::scvf;
67 const SubControlVolumeFace& scvf(GridIndexType eIdx, LocalIndexType localScvfIdx) const
68 {
69 return this->gridGeometry().scvf(eIdx, localScvfIdx);
70 }
71};
72
81template<class GG>
83{
85 using GridView = typename GG::GridView;
86 using GridIndexType = typename IndexTraits<GridView>::GridIndex;
87 using LocalIndexType = typename IndexTraits<GridView>::LocalIndex;
88public:
90 using Element = typename GridView::template Codim<0>::Entity;
92 using SubControlVolume = typename GG::SubControlVolume;
94 using SubControlVolumeFace = typename GG::SubControlVolumeFace;
96 using GridGeometry = GG;
97
100 template<class CellCenterOrFaceFVGridGeometry>
101 StaggeredFVElementGeometry(const CellCenterOrFaceFVGridGeometry& gridGeometry)
102 : gridGeometryPtr_(&gridGeometry.actualGridGeometry()) {}
103
106 : gridGeometryPtr_(&gridGeometry) {}
107
109 const SubControlVolumeFace& scvf(GridIndexType eIdx, LocalIndexType localScvfIdx) const
110 {
111 return scvf(this->gridGeometry().localToGlobalScvfIndex(eIdx, localScvfIdx));
112 }
113
116 const SubControlVolume& scv(GridIndexType scvIdx) const
117 {
118 if (scvIdx == scvIndices_[0])
119 return scvs_[0];
120 else
121 return neighborScvs_[findLocalIndex_(scvIdx, neighborScvIndices_)];
122 }
123
126 const SubControlVolumeFace& scvf(GridIndexType scvfIdx) const
127 {
128 auto it = std::find(scvfIndices_.begin(), scvfIndices_.end(), scvfIdx);
129 if (it != scvfIndices_.end())
130 return scvfs_[std::distance(scvfIndices_.begin(), it)];
131 else
132 return neighborScvfs_[findLocalIndex_(scvfIdx, neighborScvfIndices_)];
133 }
134
140 friend inline Dune::IteratorRange<typename std::array<SubControlVolume, 1>::const_iterator>
141 scvs(const ThisType& g)
142 {
143 using IteratorType = typename std::array<SubControlVolume, 1>::const_iterator;
144 return Dune::IteratorRange<IteratorType>(g.scvs_.begin(), g.scvs_.end());
145 }
146
152 friend inline Dune::IteratorRange<typename std::vector<SubControlVolumeFace>::const_iterator>
153 scvfs(const ThisType& g)
154 {
155 using IteratorType = typename std::vector<SubControlVolumeFace>::const_iterator;
156 return Dune::IteratorRange<IteratorType>(g.scvfs_.begin(), g.scvfs_.end());
157 }
158
160 std::size_t numScv() const
161 { return scvs_.size(); }
162
164 std::size_t numScvf() const
165 { return scvfs_.size(); }
166
173 {
174 this->bind_(element);
175 return std::move(*this);
176 }
177
179 void bind(const Element& element) &
180 { this->bind_(element); }
181
188 {
189 this->bindElement_(element);
190 return std::move(*this);
191 }
192
194 void bindElement(const Element& element) &
195 { this->bindElement_(element); }
196
198 bool isBound() const
199 { return static_cast<bool>(element_); }
200
202 const Element& element() const
203 { return *element_; }
204
207 { return *gridGeometryPtr_; }
208
210 bool hasBoundaryScvf() const
211 { return hasBoundaryScvf_; }
212
214 typename SubControlVolume::Traits::Geometry geometry(const SubControlVolume& scv) const
215 { return gridGeometryPtr_->element(scv.dofIndex()).geometry(); }
216
218 typename SubControlVolumeFace::Traits::Geometry geometry(const SubControlVolumeFace& scvf) const
219 {
220 const auto insideElementIndex = scvf.insideScvIdx();
221 const auto elementGeometry = (insideElementIndex != scvIndices_[0]) ?
222 element_->geometry() :
223 gridGeometryPtr_->element(insideElementIndex).geometry();
224 const auto center = elementGeometry.center();
225 const auto normalAxis = Dumux::normalAxis(scvf.unitOuterNormal());
226
227 auto lowerLeft = elementGeometry.corner(0);
228 auto upperRight = elementGeometry.corner(elementGeometry.corners()-1);
229
230 // shift corners to scvf plane and halve lateral faces
231 lowerLeft[normalAxis] = center[normalAxis];
232 upperRight[normalAxis] = center[normalAxis];
233
234 auto inPlaneAxes = std::move(std::bitset<SubControlVolumeFace::Traits::dimWorld>{}.set());
235 inPlaneAxes.set(normalAxis, false);
236
237 return {lowerLeft, upperRight, inPlaneAxes};
238 }
239
240private:
242 void bindElement_(const Element& element)
243 {
244 clear_();
245 element_ = element;
246 scvfs_.reserve(element.subEntities(1));
247 scvfIndices_.reserve(element.subEntities(1));
248 makeElementGeometries_();
249 }
250
253 void bind_(const Element& element)
254 {
255 bindElement_(element);
256
257 neighborScvs_.reserve(element.subEntities(1));
258 neighborScvfIndices_.reserve(element.subEntities(1));
259 neighborScvfs_.reserve(element.subEntities(1));
260
261 std::vector<GridIndexType> handledNeighbors;
262 handledNeighbors.reserve(element.subEntities(1));
263 for (const auto& intersection : intersections(gridGeometry().gridView(), element))
264 {
265 if (intersection.neighbor())
266 {
267 const auto outside = intersection.outside();
268 const auto outsideIdx = gridGeometry().elementMapper().index(outside);
269
270 // make outside geometries only if not done yet (could happen on non-conforming grids)
271 if ( std::find(handledNeighbors.begin(), handledNeighbors.end(), outsideIdx) == handledNeighbors.end() )
272 {
273 makeNeighborGeometries_(outside, outsideIdx);
274 handledNeighbors.push_back(outsideIdx);
275 }
276 }
277 }
278 }
279
281 void makeElementGeometries_()
282 {
283 const auto& element = *element_;
284 const auto eIdx = gridGeometry().elementMapper().index(element);
285 scvs_[0] = SubControlVolume(element.geometry(), eIdx);
286 scvIndices_[0] = eIdx;
287
288 const auto& scvFaceIndices = gridGeometry().scvfIndicesOfScv(eIdx);
289 const auto& neighborVolVarIndices = gridGeometry().neighborVolVarIndices(eIdx);
290
291 typename GridGeometry::GeometryHelper geometryHelper(element, gridGeometry().gridView());
292
293 int scvfCounter = 0;
294 for (const auto& intersection : intersections(gridGeometry().gridView(), element))
295 {
296 const auto& scvfNeighborVolVarIndex = neighborVolVarIndices[scvfCounter];
297
298 if (intersection.neighbor() || intersection.boundary())
299 {
300 geometryHelper.updateLocalFace(gridGeometry().intersectionMapper(), intersection);
301 std::vector<GridIndexType> scvIndices{eIdx, scvfNeighborVolVarIndex};
302 scvfs_.emplace_back(intersection,
303 intersection.geometry(),
304 scvFaceIndices[scvfCounter],
305 scvIndices,
306 geometryHelper);
307 scvfIndices_.emplace_back(scvFaceIndices[scvfCounter]);
308 scvfCounter++;
309
310 if (intersection.boundary())
311 hasBoundaryScvf_ = true;
312 }
313 }
314 }
315
317 void makeNeighborGeometries_(const Element& element, const GridIndexType eIdx)
318 {
319 // using ScvfGridIndexStorage = typename SubControlVolumeFace::Traits::GridIndexStorage;
320
321 // create the neighbor scv
322 neighborScvs_.emplace_back(element.geometry(), eIdx);
323 neighborScvIndices_.push_back(eIdx);
324
325 const auto& scvFaceIndices = gridGeometry().scvfIndicesOfScv(eIdx);
326 const auto& neighborVolVarIndices = gridGeometry().neighborVolVarIndices(eIdx);
327
328 typename GridGeometry::GeometryHelper geometryHelper(element, gridGeometry().gridView());
329
330 int scvfCounter = 0;
331 for (const auto& intersection : intersections(gridGeometry().gridView(), element))
332 {
333 const auto& scvfNeighborVolVarIndex = neighborVolVarIndices[scvfCounter];
334 geometryHelper.updateLocalFace(gridGeometry().intersectionMapper(), intersection);
335
336 if (intersection.neighbor())
337 {
338 // only create subcontrol faces where the outside element is the bound element
339 if (intersection.outside() == *element_)
340 {
341 std::vector<GridIndexType> scvIndices{eIdx, scvfNeighborVolVarIndex};
342 neighborScvfs_.emplace_back(intersection,
343 intersection.geometry(),
344 scvFaceIndices[scvfCounter],
345 scvIndices,
346 geometryHelper);
347
348 neighborScvfIndices_.push_back(scvFaceIndices[scvfCounter]);
349 }
350 scvfCounter++;
351 }
352 else if (intersection.boundary())
353 scvfCounter++;
354 }
355 }
356
357 const LocalIndexType findLocalIndex_(const GridIndexType idx,
358 const std::vector<GridIndexType>& indices) const
359 {
360 auto it = std::find(indices.begin(), indices.end(), idx);
361 assert(it != indices.end() && "Could not find the scv/scvf! Make sure to properly bind this class!");
362 return std::distance(indices.begin(), it);
363 }
364
366 void clear_()
367 {
368 scvfIndices_.clear();
369 scvfs_.clear();
370
371 neighborScvIndices_.clear();
372 neighborScvfIndices_.clear();
373 neighborScvs_.clear();
374 neighborScvfs_.clear();
375
376 hasBoundaryScvf_ = false;
377 }
378
379 std::optional<Element> element_;
380 const GridGeometry* gridGeometryPtr_;
381
382 // local storage after binding an element
383 std::array<GridIndexType, 1> scvIndices_;
384 std::array<SubControlVolume, 1> scvs_;
385
386 std::vector<GridIndexType> scvfIndices_;
387 std::vector<SubControlVolumeFace> scvfs_;
388
389 std::vector<GridIndexType> neighborScvIndices_;
390 std::vector<SubControlVolume> neighborScvs_;
391
392 std::vector<GridIndexType> neighborScvfIndices_;
393 std::vector<SubControlVolumeFace> neighborScvfs_;
394
395 bool hasBoundaryScvf_ = false;
396};
397
398
399} // end namespace
400
401#endif
Stencil-local finite volume geometry (scvs and scvfs) for cell-centered TPFA models Specialization fo...
Definition: discretization/cellcentered/tpfa/fvelementgeometry.hh:63
typename GridView::template Codim< 0 >::Entity Element
export type of the element
Definition: discretization/cellcentered/tpfa/fvelementgeometry.hh:71
typename GG::SubControlVolumeFace SubControlVolumeFace
export type of subcontrol volume face
Definition: discretization/cellcentered/tpfa/fvelementgeometry.hh:75
Stencil-local finite volume geometry (scvs and scvfs) for cell-centered TPFA models This builds up th...
Definition: discretization/cellcentered/tpfa/fvelementgeometry.hh:53
Base class for the finite volume geometry vector for staggered models This locally builds up the sub ...
Definition: discretization/staggered/fvelementgeometry.hh:83
StaggeredFVElementGeometry bind(const Element &element) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: discretization/staggered/fvelementgeometry.hh:172
typename GridView::template Codim< 0 >::Entity Element
export type of the element
Definition: discretization/staggered/fvelementgeometry.hh:90
bool hasBoundaryScvf() const
Returns whether one of the geometry's scvfs lies on a boundary.
Definition: discretization/staggered/fvelementgeometry.hh:210
void bindElement(const Element &element) &
bind this local view to a specific element
Definition: discretization/staggered/fvelementgeometry.hh:194
friend Dune::IteratorRange< typename std::vector< SubControlVolumeFace >::const_iterator > scvfs(const ThisType &g)
Definition: discretization/staggered/fvelementgeometry.hh:153
typename GG::SubControlVolume SubControlVolume
export type of subcontrol volume
Definition: discretization/staggered/fvelementgeometry.hh:92
SubControlVolumeFace::Traits::Geometry geometry(const SubControlVolumeFace &scvf) const
Create the geometry of a given sub control volume face.
Definition: discretization/staggered/fvelementgeometry.hh:218
const SubControlVolume & scv(GridIndexType scvIdx) const
Definition: discretization/staggered/fvelementgeometry.hh:116
StaggeredFVElementGeometry(const CellCenterOrFaceFVGridGeometry &gridGeometry)
Definition: discretization/staggered/fvelementgeometry.hh:101
SubControlVolume::Traits::Geometry geometry(const SubControlVolume &scv) const
Create the geometry of a given sub control volume.
Definition: discretization/staggered/fvelementgeometry.hh:214
std::size_t numScv() const
number of sub control volumes in this fv element geometry
Definition: discretization/staggered/fvelementgeometry.hh:160
const SubControlVolumeFace & scvf(GridIndexType eIdx, LocalIndexType localScvfIdx) const
Get a sub control volume face with an element index and a local scvf index.
Definition: discretization/staggered/fvelementgeometry.hh:109
const GridGeometry & gridGeometry() const
The grid finite volume geometry we are a restriction of.
Definition: discretization/staggered/fvelementgeometry.hh:206
std::size_t numScvf() const
number of sub control volumes in this fv element geometry
Definition: discretization/staggered/fvelementgeometry.hh:164
StaggeredFVElementGeometry bindElement(const Element &element) &&
bind the local view (r-value overload) This overload is called when an instance of this class is a te...
Definition: discretization/staggered/fvelementgeometry.hh:187
StaggeredFVElementGeometry(const GridGeometry &gridGeometry)
Constructor.
Definition: discretization/staggered/fvelementgeometry.hh:105
const Element & element() const
The bound element.
Definition: discretization/staggered/fvelementgeometry.hh:202
void bind(const Element &element) &
bind this local view to a specific element (full stencil)
Definition: discretization/staggered/fvelementgeometry.hh:179
bool isBound() const
Returns true if bind/bindElement has already been called.
Definition: discretization/staggered/fvelementgeometry.hh:198
typename GG::SubControlVolumeFace SubControlVolumeFace
export type of subcontrol volume face
Definition: discretization/staggered/fvelementgeometry.hh:94
friend Dune::IteratorRange< typename std::array< SubControlVolume, 1 >::const_iterator > scvs(const ThisType &g)
Definition: discretization/staggered/fvelementgeometry.hh:141
const SubControlVolumeFace & scvf(GridIndexType scvfIdx) const
Definition: discretization/staggered/fvelementgeometry.hh:126
GG GridGeometry
export type of finite volume grid geometry
Definition: discretization/staggered/fvelementgeometry.hh:96
const SubControlVolumeFace & scvf(GridIndexType eIdx, LocalIndexType localScvfIdx) const
Definition: discretization/staggered/fvelementgeometry.hh:67
StaggeredFVElementGeometry(const CellCenterOrFaceFVGridGeometry &gridGeometry)
Definition: discretization/staggered/fvelementgeometry.hh:62
Stencil-local finite volume geometry (scvs and scvfs) for staggered models This builds up the sub con...
Definition: discretization/staggered/fvelementgeometry.hh:34
Stencil-local finite volume geometry (scvs and scvfs) for cell-centered TPFA models This builds up th...
static std::size_t normalAxis(const Vector &v)
Returns the normal axis index of a unit vector (0 = x, 1 = y, 2 = z)
Definition: normalaxis.hh:26
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:282
Corners::value_type center(const Corners &corners)
The center of a given list of corners.
Definition: center.hh:24
Defines the index types used for grid and local indices.
Definition: adapt.hh:17
typename GridView::IndexSet::IndexType GridIndex
Definition: indextraits.hh:27
unsigned int LocalIndex
Definition: indextraits.hh:28