12#ifndef DUMUX_ASSEMBLY_COLORING_HH
13#define DUMUX_ASSEMBLY_COLORING_HH
21#include <dune/common/timer.hh>
22#include <dune/common/exceptions.hh>
31template <
class Gr
idGeometry>
32std::vector<std::vector<std::size_t>>
33computeConnectedElements(
const GridGeometry& gg)
35 std::vector<std::vector<std::size_t>> connectedElements;
39 connectedElements.resize(gg.gridView().size(0));
40 const auto& eMapper = gg.elementMapper();
41 for (
const auto& element : elements(gg.gridView()))
43 const auto eIdx = eMapper.index(element);
44 for (
const auto& intersection : intersections(gg.gridView(), element))
45 if (intersection.neighbor())
46 connectedElements[eMapper.index(intersection.outside())].push_back(eIdx);
54 static constexpr int dim = GridGeometry::GridView::dimension;
55 connectedElements.resize(gg.gridView().size(dim));
56 const auto& vMapper = gg.vertexMapper();
57 for (
const auto& element : elements(gg.gridView()))
59 const auto eIdx = gg.elementMapper().index(element);
60 for (
int i = 0; i <
element.subEntities(dim); i++)
61 connectedElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
73 std::vector<std::vector<std::size_t>> vToElements;
74 static constexpr int dim = GridGeometry::GridView::dimension;
75 vToElements.resize(gg.gridView().size(dim));
76 const auto& vMapper = gg.vertexMapper();
77 for (
const auto& element : elements(gg.gridView()))
79 const auto eIdx = gg.elementMapper().index(element);
80 for (
int i = 0; i <
element.subEntities(dim); i++)
81 vToElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
84 connectedElements.resize(gg.gridView().size(0));
85 for (
const auto& element : elements(gg.gridView()))
87 const auto eIdx = gg.elementMapper().index(element);
88 for (
int i = 0; i <
element.subEntities(dim); i++)
90 const auto& e = vToElements[vMapper.subIndex(element, i, dim)];
91 connectedElements[eIdx].insert(connectedElements[eIdx].end(), e.begin(), e.end());
95 std::sort(connectedElements[eIdx].begin(), connectedElements[eIdx].end());
96 connectedElements[eIdx].erase(
97 std::unique(connectedElements[eIdx].begin(), connectedElements[eIdx].end()),
98 connectedElements[eIdx].end()
105 return connectedElements;
108 DUNE_THROW(Dune::NotImplemented,
109 "Missing coloring scheme implementation for this discretization method"
112 return connectedElements;
127template<
class Gr
idGeometry,
class ConnectedElements>
128void addNeighborColors(
const GridGeometry& gg,
129 const typename GridGeometry::LocalView::Element& element,
130 const std::vector<int>& colors,
131 const ConnectedElements& connectedElements,
132 std::vector<int>& neighborColors)
142 const auto& eMapper = gg.elementMapper();
143 for (
const auto& intersection : intersections(gg.gridView(), element))
145 if (intersection.neighbor())
148 const auto nIdx = eMapper.index(intersection.outside());
149 neighborColors.push_back(colors[nIdx]);
152 for (
const auto nnIdx : connectedElements[eMapper.index(intersection.outside())])
153 neighborColors.push_back(colors[nnIdx]);
164 const auto& vMapper = gg.vertexMapper();
165 static constexpr int dim = GridGeometry::GridView::dimension;
167 for (
int i = 0; i <
element.subEntities(dim); i++)
168 for (
auto eIdx : connectedElements[vMapper.subIndex(element, i, dim)])
169 neighborColors.push_back(colors[eIdx]);
176 const auto& eMapper = gg.elementMapper();
177 for (
const auto& intersection : intersections(gg.gridView(),
element))
178 if (intersection.neighbor())
179 neighborColors.push_back(colors[eMapper.index(intersection.outside())]);
183 DUNE_THROW(Dune::NotImplemented,
184 "Missing coloring scheme implementation for this discretization method"
193int smallestAvailableColor(
const std::vector<int>& colors,
194 std::vector<bool>& colorUsed)
196 const int numColors = colors.size();
197 colorUsed.assign(numColors,
false);
203 for (
int i = 0; i < numColors; i++)
204 if (colors[i] >= 0 && colors[i] < numColors)
205 colorUsed[colors[i]] =
true;
208 for (
int i = 0; i < numColors; i++)
240template<
class Gr
idGeometry>
245 using ElementSeed =
typename GridGeometry::GridView::Grid::template Codim<0>::EntitySeed;
248 using Sets = std::deque<std::vector<ElementSeed>>;
249 using Colors = std::vector<int>;
251 Coloring(std::size_t size) : sets{}, colors(size, -1) {}
257 Coloring coloring(gg.gridView().size(0));
260 std::vector<int> neighborColors; neighborColors.reserve(30);
261 std::vector<bool> colorUsed; colorUsed.reserve(30);
264 const auto connectedElements = Detail::computeConnectedElements(gg);
266 for (
const auto& element : elements(gg.gridView()))
269 neighborColors.clear();
270 Detail::addNeighborColors(gg, element, coloring.colors, connectedElements, neighborColors);
273 const auto color = Detail::smallestAvailableColor(neighborColors, colorUsed);
276 coloring.colors[gg.elementMapper().index(element)] = color;
279 if (color < coloring.sets.size())
280 coloring.sets[color].push_back(element.seed());
282 coloring.sets.push_back(std::vector<ElementSeed>{ element.seed() });
286 std::cout << Fmt::format(
"Colored {} elements with {} colors in {} seconds.\n",
287 gg.gridView().size(0), coloring.sets.size(), timer.elapsed());
293template<
class DiscretizationMethod>
296template<>
struct SupportsColoring<DiscretizationMethods::CCTpfa> :
public std::true_type {};
297template<>
struct SupportsColoring<DiscretizationMethods::CCMpfa> :
public std::true_type {};
299template<>
struct SupportsColoring<DiscretizationMethods::FCStaggered> :
public std::true_type {};
The available discretization methods in Dumux.
Definition: cvfelocalresidual.hh:30
constexpr CCMpfa ccmpfa
Definition: method.hh:155
constexpr FCDiamond fcdiamond
Definition: method.hh:162
constexpr PQ2 pq2
Definition: method.hh:157
constexpr CCTpfa cctpfa
Definition: method.hh:154
CVFE< CVFEMethods::CR_RT > FCDiamond
Definition: method.hh:105
constexpr Box box
Definition: method.hh:156
CVFE< CVFEMethods::PQ1 > Box
Definition: method.hh:98
CVFE< CVFEMethods::PQ2 > PQ2
Definition: method.hh:118
CVFE< CVFEMethods::PQ1Bubble > PQ1Bubble
Definition: method.hh:112
constexpr PQ1Bubble pq1bubble
Definition: method.hh:158
constexpr FCStaggered fcstaggered
Definition: method.hh:161
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition: coloring.hh:241
Traits specifying if a given discretization tag supports coloring.
Definition: coloring.hh:294