24#ifndef DUMUX_ASSEMBLY_COLORING_HH
25#define DUMUX_ASSEMBLY_COLORING_HH
33#include <dune/common/timer.hh>
34#include <dune/common/exceptions.hh>
43template <
class Gr
idGeometry>
44std::vector<std::vector<std::size_t>>
45computeConnectedElements(
const GridGeometry& gg)
47 std::vector<std::vector<std::size_t>> connectedElements;
51 connectedElements.resize(gg.gridView().size(0));
52 const auto& eMapper = gg.elementMapper();
53 for (
const auto& element : elements(gg.gridView()))
55 const auto eIdx = eMapper.index(element);
56 for (
const auto& intersection : intersections(gg.gridView(), element))
57 if (intersection.neighbor())
58 connectedElements[eMapper.index(intersection.outside())].push_back(eIdx);
65 static constexpr int dim = GridGeometry::GridView::dimension;
66 connectedElements.resize(gg.gridView().size(dim));
67 const auto& vMapper = gg.vertexMapper();
68 for (
const auto& element : elements(gg.gridView()))
70 const auto eIdx = gg.elementMapper().index(element);
71 for (
int i = 0; i <
element.subEntities(dim); i++)
72 connectedElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
84 std::vector<std::vector<std::size_t>> vToElements;
85 static constexpr int dim = GridGeometry::GridView::dimension;
86 vToElements.resize(gg.gridView().size(dim));
87 const auto& vMapper = gg.vertexMapper();
88 for (
const auto& element : elements(gg.gridView()))
90 const auto eIdx = gg.elementMapper().index(element);
91 for (
int i = 0; i <
element.subEntities(dim); i++)
92 vToElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
95 connectedElements.resize(gg.gridView().size(0));
96 for (
const auto& element : elements(gg.gridView()))
98 const auto eIdx = gg.elementMapper().index(element);
99 for (
int i = 0; i <
element.subEntities(dim); i++)
101 const auto& e = vToElements[vMapper.subIndex(element, i, dim)];
102 connectedElements[eIdx].insert(connectedElements[eIdx].end(), e.begin(), e.end());
106 std::sort(connectedElements[eIdx].begin(), connectedElements[eIdx].end());
107 connectedElements[eIdx].erase(
108 std::unique(connectedElements[eIdx].begin(), connectedElements[eIdx].end()),
109 connectedElements[eIdx].end()
116 return connectedElements;
119 DUNE_THROW(Dune::NotImplemented,
120 "Missing coloring scheme implementation for this discretization method"
123 return connectedElements;
138template<
class Gr
idGeometry,
class ConnectedElements>
139void addNeighborColors(
const GridGeometry& gg,
140 const typename GridGeometry::LocalView::Element& element,
141 const std::vector<int>& colors,
142 const ConnectedElements& connectedElements,
143 std::vector<int>& neighborColors)
153 const auto& eMapper = gg.elementMapper();
154 for (
const auto& intersection : intersections(gg.gridView(), element))
156 if (intersection.neighbor())
159 const auto nIdx = eMapper.index(intersection.outside());
160 neighborColors.push_back(colors[nIdx]);
163 for (
const auto nnIdx : connectedElements[eMapper.index(intersection.outside())])
164 neighborColors.push_back(colors[nnIdx]);
174 const auto& vMapper = gg.vertexMapper();
175 static constexpr int dim = GridGeometry::GridView::dimension;
177 for (
int i = 0; i <
element.subEntities(dim); i++)
178 for (
auto eIdx : connectedElements[vMapper.subIndex(element, i, dim)])
179 neighborColors.push_back(colors[eIdx]);
186 const auto& eMapper = gg.elementMapper();
187 for (
const auto& intersection : intersections(gg.gridView(),
element))
188 if (intersection.neighbor())
189 neighborColors.push_back(colors[eMapper.index(intersection.outside())]);
193 DUNE_THROW(Dune::NotImplemented,
194 "Missing coloring scheme implementation for this discretization method"
203int smallestAvailableColor(
const std::vector<int>& colors,
204 std::vector<bool>& colorUsed)
206 const int numColors = colors.size();
207 colorUsed.assign(numColors,
false);
213 for (
int i = 0; i < numColors; i++)
214 if (colors[i] >= 0 && colors[i] < numColors)
215 colorUsed[colors[i]] =
true;
218 for (
int i = 0; i < numColors; i++)
250template<
class Gr
idGeometry>
255 using ElementSeed =
typename GridGeometry::GridView::Grid::template Codim<0>::EntitySeed;
258 using Sets = std::deque<std::vector<ElementSeed>>;
259 using Colors = std::vector<int>;
261 Coloring(std::size_t size) : sets{}, colors(size, -1) {}
267 Coloring coloring(gg.gridView().size(0));
270 std::vector<int> neighborColors; neighborColors.reserve(30);
271 std::vector<bool> colorUsed; colorUsed.reserve(30);
274 const auto connectedElements = Detail::computeConnectedElements(gg);
276 for (
const auto& element : elements(gg.gridView()))
279 neighborColors.clear();
280 Detail::addNeighborColors(gg, element, coloring.colors, connectedElements, neighborColors);
283 const auto color = Detail::smallestAvailableColor(neighborColors, colorUsed);
286 coloring.colors[gg.elementMapper().index(element)] = color;
289 if (color < coloring.sets.size())
290 coloring.sets[color].push_back(element.seed());
292 coloring.sets.push_back(std::vector<ElementSeed>{ element.seed() });
296 std::cout << Fmt::format(
"Colored {} elements with {} colors in {} seconds.\n",
297 gg.gridView().size(0), coloring.sets.size(), timer.elapsed());
303template<
class DiscretizationMethod>
306template<>
struct SupportsColoring<DiscretizationMethods::CCTpfa> :
public std::true_type {};
307template<>
struct SupportsColoring<DiscretizationMethods::CCMpfa> :
public std::true_type {};
309template<>
struct SupportsColoring<DiscretizationMethods::FCStaggered> :
public std::true_type {};
The available discretization methods in Dumux.
Formatting based on the fmt-library which implements std::format of C++20.
Adaption of the non-isothermal two-phase two-component flow model to problems with CO2.
Definition: adapt.hh:29
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition: coloring.hh:251
Distance implementation details.
Definition: cvfelocalresidual.hh:37
constexpr CCMpfa ccmpfa
Definition: method.hh:135
constexpr FCDiamond fcdiamond
Definition: method.hh:141
constexpr CCTpfa cctpfa
Definition: method.hh:134
CVFE< CVFEMethods::CR_RT > FCDiamond
Definition: method.hh:90
constexpr Box box
Definition: method.hh:136
CVFE< CVFEMethods::PQ1 > Box
Definition: method.hh:83
CVFE< CVFEMethods::PQ1Bubble > PQ1Bubble
Definition: method.hh:97
constexpr PQ1Bubble pq1bubble
Definition: method.hh:137
constexpr FCStaggered fcstaggered
Definition: method.hh:140
Traits specifying if a given discretization tag supports coloring.
Definition: coloring.hh:304