24#ifndef DUMUX_ASSEMBLY_COLORING_HH
25#define DUMUX_ASSEMBLY_COLORING_HH
32#include <dune/common/timer.hh>
33#include <dune/common/exceptions.hh>
42template <
class Gr
idGeometry>
43std::vector<std::vector<std::size_t>>
44computeDofToElementMap(
const GridGeometry& gg)
46 std::vector<std::vector<std::size_t>> dofToElements;
50 dofToElements.resize(gg.gridView().size(0));
51 const auto& eMapper = gg.elementMapper();
52 for (
const auto& element : elements(gg.gridView()))
54 const auto eIdx = eMapper.index(element);
55 for (
const auto& intersection : intersections(gg.gridView(), element))
56 if (intersection.neighbor())
57 dofToElements[eMapper.index(intersection.outside())].push_back(eIdx);
63 static constexpr int dim = GridGeometry::GridView::dimension;
64 dofToElements.resize(gg.gridView().size(dim));
65 const auto& vMapper = gg.vertexMapper();
66 for (
const auto& element : elements(gg.gridView()))
68 const auto eIdx = gg.elementMapper().index(element);
69 for (
int i = 0; i <
element.subEntities(dim); i++)
70 dofToElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
75 DUNE_THROW(Dune::NotImplemented,
76 "Missing coloring scheme implementation for this discretization method"
94template<
class Gr
idGeometry,
class DofToElementMap>
95void addNeighborColors(
const GridGeometry& gg,
96 const typename GridGeometry::LocalView::Element& element,
97 const std::vector<int>& colors,
98 const DofToElementMap& dofToElement,
99 std::vector<int>& neighborColors)
105 const auto& eMapper = gg.elementMapper();
106 for (
const auto& intersection : intersections(gg.gridView(), element))
108 if (intersection.neighbor())
111 const auto nIdx = eMapper.index(intersection.outside());
112 neighborColors.push_back(colors[nIdx]);
115 for (
const auto nnIdx : dofToElement[eMapper.index(intersection.outside())])
116 neighborColors.push_back(colors[nnIdx]);
125 const auto& vMapper = gg.vertexMapper();
126 static constexpr int dim = GridGeometry::GridView::dimension;
128 for (
int i = 0; i <
element.subEntities(dim); i++)
129 for (
auto eIdx : dofToElement[vMapper.subIndex(element, i, dim)])
130 neighborColors.push_back(colors[eIdx]);
134 DUNE_THROW(Dune::NotImplemented,
135 "Missing coloring scheme implementation for this discretization method"
144int smallestAvailableColor(
const std::vector<int>& colors,
145 std::vector<bool>& colorUsed)
147 const int numColors = colors.size();
148 colorUsed.assign(numColors,
false);
154 for (
int i = 0; i < numColors; i++)
155 if (colors[i] >= 0 && colors[i] < numColors)
156 colorUsed[colors[i]] =
true;
159 for (
int i = 0; i < numColors; i++)
191template<
class Gr
idGeometry>
196 using ElementSeed =
typename GridGeometry::GridView::Grid::template Codim<0>::EntitySeed;
199 using Sets = std::deque<std::vector<ElementSeed>>;
200 using Colors = std::vector<int>;
202 Coloring(std::size_t size) : sets{}, colors(size, -1) {}
208 Coloring coloring(gg.gridView().size(0));
211 std::vector<int> neighborColors; neighborColors.reserve(30);
212 std::vector<bool> colorUsed; colorUsed.reserve(30);
215 const auto dofToElement = Detail::computeDofToElementMap(gg);
217 for (
const auto& element : elements(gg.gridView()))
220 neighborColors.clear();
221 Detail::addNeighborColors(gg, element, coloring.colors, dofToElement, neighborColors);
224 const auto color = Detail::smallestAvailableColor(neighborColors, colorUsed);
227 coloring.colors[gg.elementMapper().index(element)] = color;
230 if (color < coloring.sets.size())
231 coloring.sets[color].push_back(element.seed());
233 coloring.sets.push_back(std::vector<ElementSeed>{ element.seed() });
237 std::cout << Fmt::format(
"Colored {} elements with {} colors in {} seconds.\n",
238 gg.gridView().size(0), coloring.sets.size(), timer.elapsed());
244template<
class DiscretizationMethod>
247template<>
struct SupportsColoring<DiscretizationMethods::CCTpfa> :
public std::true_type {};
The available discretization methods in Dumux.
Formatting based on the fmt-library which implements std::format of C++20.
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition: coloring.hh:192
Distance implementation details.
Definition: fclocalassembler.hh:42
constexpr CCTpfa cctpfa
Definition: method.hh:137
constexpr Box box
Definition: method.hh:139
Traits specifying if a given discretization tag supports coloring.
Definition: coloring.hh:245