KASKADE 7 development version
forEach.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2019 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12/*
13 * foreach.hh
14 *
15 * @brief: for_each implementations for easy iteration over grids using functors and lambdas.
16 * TODO: Maybe some methods should be removed since not really needed?
17 *
18 * Created on: 28.04.2013
19 * Author: Lars Lubkoll
20 */
21
22#ifndef KASKADE_FOREACH_HH_
23#define KASKADE_FOREACH_HH_
24
25#include <algorithm>
26
28
29namespace Kaskade
30{
32 template <class GridView, class Functor>
33 void forEachCell(GridView const& gridView, Functor functor)
34 {
35 for(auto const& element : elements(gridView)) {
36 functor(element);
37 }
38 }
39
41 template <class GridView, class Functor>
42 void forEachVertex(GridView const& gridView, Functor functor)
43 {
44 for(auto const& vertex : vertices(gridView)) {
45 functor(vertex);
46 }
47 }
48
51 template <class GridView, class Functor>
52 void forEachFace(GridView const& gridView, Functor functor)
53 {
54 for(auto const& element : elements(gridView)) {
55 for(auto const& intersection : intersections(gridView,element)) {
56 functor(intersection);
57 }
58 }
59 }
60
62 template <class GridView, class Functor>
63 void forEachFaceOfCell(GridView const& gridView, Functor functor)
64 {
65 for(auto const& element : elements(gridView)) {
66 for(auto const& intersection : intersections(gridView,element)) {
67 functor(element, intersection);
68 }
69 }
70 }
71
72// template <class GridView, class Functor>
73// void forEachBoundaryFace(GridView const& gridView, Functor functor)
74// {
75// forEachFace(gridView, [&](typename GridView::Intersection const& face)
76// {
77// if(face.boundary()) functor(face);
78// });
79// }
80
81 // seems to be faster, at least for UG grid (tested in experiment); for the old version see above
87 template <class GridView, class Functor>
88 void forEachBoundaryFace(GridView const& gridView, Functor functor)
89 {
90 for(auto const& element : elements(gridView))
91 if(element.hasBoundaryIntersections()) // this if clause accelerates iteration over boundary faces
92 for(auto const& intersection : intersections(gridView,element))
93 if(intersection.boundary())
94 functor(intersection);
95 }
96
98 template <class GridView, class Functor>
99 void forEachInnerFace(GridView const& gridView, Functor functor)
100 {
101 forEachFace(gridView, [&](typename GridView::Intersection const& face)
102 {
103 if(!face.boundary()) functor(face);
104 });
105 }
106
107
109 template <class GridView, class Functor>
110 void forEachCellAndEachFace(GridView const& gridView, Functor functor)
111 {
112 for(auto const& element : elements(gridView)) {
113 functor(element);
114 for(auto const& intersection : intersections(gridView,element)) {
115 functor(intersection);
116 }
117 }
118 }
119
120 template <class GridManager, class Functor>
121 void markCells(GridManager& gridManager, Functor functor)
122 {
123 forEachCell(gridManager.grid().leafGridView(), [&](typename GridManager::Grid::LeafGridView::template Codim<0>::Entity const& cell)
124 {
125#ifdef TESTOUTPUT
126 size_t marked = 0, notMarked = 0;
127#endif
128
129 bool mark = false;
130 mark = mark || functor(cell);
131 std::for_each(gridManager.grid().leafGridView().ibegin(cell), gridManager.grid().leafGridView().iend(cell), [&](typename GridManager::Grid::LeafGridView::Intersection const& face)
132 {
133 mark = mark || functor(face);
134 });
135 if(mark) gridManager.mark(1,cell);
136
137#ifdef TESTOUTPUT
138 if(mark) ++marked;
139 else ++notMarked;
140 std::cout << "Marked " << marked << " cells of " << (marked+notMarked) << " cells." << std::endl;
141#endif
142 });
143 }
144} // end of namespace Kaskade
145
146
147#endif /* KASKADE_FOREACH_HH_ */
Grid const & grid() const
Returns a const reference on the owned grid.
Definition: gridmanager.hh:292
bool mark(int refCount, typename Grid::template Codim< 0 >::Entity const &cell)
Marks the given cell for refinement or coarsening.
Definition: gridmanager.hh:330
Class that takes care of the transfer of data (of each FunctionSpaceElement) after modification of th...
Definition: gridmanager.hh:680
void forEachBoundaryFace(GridView const &gridView, Functor functor)
iterates over each boundary face and applies functor to face. Each boundary face is visited exactly o...
Definition: forEach.hh:88
void forEachCell(GridView const &gridView, Functor functor)
DEPRECATED: use range based for loops and Dune::elements() directly. iterates over each cell and appl...
Definition: forEach.hh:33
void forEachCellAndEachFace(GridView const &gridView, Functor functor)
iterates over each cell and applies functor to cell and then iterates over each face of cell and appl...
Definition: forEach.hh:110
void forEachVertex(GridView const &gridView, Functor functor)
DEPRECATED: use range based for loops and Dune::vertices() directly. iterates over each vertex and ap...
Definition: forEach.hh:42
void forEachFace(GridView const &gridView, Functor functor)
Definition: forEach.hh:52
void forEachFaceOfCell(GridView const &gridView, Functor functor)
same as forEachFace, but the functor is applied to the face and the cell from which the face is visit...
Definition: forEach.hh:63
void markCells(GridManager &gridManager, Functor functor)
Definition: forEach.hh:121
void forEachInnerFace(GridView const &gridView, Functor functor)
iterates over each inner face and applies functor to face. Each inner face is visited exactly twice (...
Definition: forEach.hh:99