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);
 
   53        static constexpr int dim = GridGeometry::GridView::dimension;
 
   54        connectedElements.resize(gg.gridView().size(dim));
 
   55        const auto& vMapper = gg.vertexMapper();
 
   56        for (
const auto& element : elements(gg.gridView()))
 
   58            const auto eIdx = gg.elementMapper().index(element);
 
   59            for (
int i = 0; i < 
element.subEntities(dim); i++)
 
   60                connectedElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
 
   72        std::vector<std::vector<std::size_t>> vToElements;
 
   73        static constexpr int dim = GridGeometry::GridView::dimension;
 
   74        vToElements.resize(gg.gridView().size(dim));
 
   75        const auto& vMapper = gg.vertexMapper();
 
   76        for (
const auto& element : elements(gg.gridView()))
 
   78            const auto eIdx = gg.elementMapper().index(element);
 
   79            for (
int i = 0; i < 
element.subEntities(dim); i++)
 
   80                vToElements[vMapper.subIndex(element, i, dim)].push_back(eIdx);
 
   83        connectedElements.resize(gg.gridView().size(0));
 
   84        for (
const auto& element : elements(gg.gridView()))
 
   86            const auto eIdx = gg.elementMapper().index(element);
 
   87            for (
int i = 0; i < 
element.subEntities(dim); i++)
 
   89                const auto& e = vToElements[vMapper.subIndex(element, i, dim)];
 
   90                connectedElements[eIdx].insert(connectedElements[eIdx].end(), e.begin(), e.end());
 
   94            std::sort(connectedElements[eIdx].begin(), connectedElements[eIdx].end());
 
   95            connectedElements[eIdx].erase(
 
   96                std::unique(connectedElements[eIdx].begin(), connectedElements[eIdx].end()),
 
   97                connectedElements[eIdx].end()
 
  104        return connectedElements;
 
  107        DUNE_THROW(Dune::NotImplemented,
 
  108            "Missing coloring scheme implementation for this discretization method" 
  111    return connectedElements;
 
  126template<
class Gr
idGeometry, 
class ConnectedElements>
 
  127void addNeighborColors(
const GridGeometry& gg,
 
  128                       const typename GridGeometry::LocalView::Element& element,
 
  129                       const std::vector<int>& colors,
 
  130                       const ConnectedElements& connectedElements,
 
  131                       std::vector<int>& neighborColors)
 
  141        const auto& eMapper = gg.elementMapper();
 
  142        for (
const auto& intersection : intersections(gg.gridView(), element))
 
  144            if (intersection.neighbor())
 
  147                const auto nIdx = eMapper.index(intersection.outside());
 
  148                neighborColors.push_back(colors[nIdx]);
 
  151                for (
const auto nnIdx : connectedElements[eMapper.index(intersection.outside())])
 
  152                    neighborColors.push_back(colors[nnIdx]);
 
  162        const auto& vMapper = gg.vertexMapper();
 
  163        static constexpr int dim = GridGeometry::GridView::dimension;
 
  165        for (
int i = 0; i < 
element.subEntities(dim); i++)
 
  166            for (
auto eIdx : connectedElements[vMapper.subIndex(element, i, dim)])
 
  167                neighborColors.push_back(colors[eIdx]);
 
  174        const auto& eMapper = gg.elementMapper();
 
  175        for (
const auto& intersection : intersections(gg.gridView(), element))
 
  176            if (intersection.neighbor())
 
  177                neighborColors.push_back(colors[eMapper.index(intersection.outside())]);
 
  181        DUNE_THROW(Dune::NotImplemented,
 
  182            "Missing coloring scheme implementation for this discretization method" 
  191int smallestAvailableColor(
const std::vector<int>& colors,
 
  192                           std::vector<bool>& colorUsed)
 
  194    const int numColors = colors.size();
 
  195    colorUsed.assign(numColors, 
false);
 
  201    for (
int i = 0; i < numColors; i++)
 
  202        if (colors[i] >= 0 && colors[i] < numColors)
 
  203            colorUsed[colors[i]] = 
true;
 
  206    for (
int i = 0; i < numColors; i++)
 
  238template<
class Gr
idGeometry>
 
  243    using ElementSeed = 
typename GridGeometry::GridView::Grid::template Codim<0>::EntitySeed;
 
  246        using Sets = std::deque<std::vector<ElementSeed>>;
 
  247        using Colors = std::vector<int>;
 
  249        Coloring(std::size_t size) : sets{}, colors(size, -1) {}
 
  255    Coloring coloring(gg.gridView().size(0));
 
  258    std::vector<int> neighborColors; neighborColors.reserve(30);
 
  259    std::vector<bool> colorUsed; colorUsed.reserve(30);
 
  262    const auto connectedElements = Detail::computeConnectedElements(gg);
 
  264    for (
const auto& element : elements(gg.gridView()))
 
  267        neighborColors.clear();
 
  268        Detail::addNeighborColors(gg, element, coloring.colors, connectedElements, neighborColors);
 
  271        const auto color = Detail::smallestAvailableColor(neighborColors, colorUsed);
 
  274        coloring.colors[gg.elementMapper().index(element)] = color;
 
  277        if (color < coloring.sets.size())
 
  278            coloring.sets[color].push_back(element.seed());
 
  280            coloring.sets.push_back(std::vector<ElementSeed>{ element.seed() });
 
  284        std::cout << Fmt::format(
"Colored {} elements with {} colors in {} seconds.\n",
 
  285                                 gg.gridView().size(0), coloring.sets.size(), timer.elapsed());
 
 
  291template<
class DiscretizationMethod>
 
The available discretization methods in Dumux.
Definition cellcentered/mpfa/elementvolumevariables.hh:24
Distance implementation details.
Definition cvfelocalresidual.hh:25
constexpr CCMpfa ccmpfa
Definition method.hh:146
constexpr FCDiamond fcdiamond
Definition method.hh:152
constexpr CCTpfa cctpfa
Definition method.hh:145
constexpr Box box
Definition method.hh:147
constexpr PQ1Bubble pq1bubble
Definition method.hh:148
constexpr FCStaggered fcstaggered
Definition method.hh:151
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition coloring.hh:239
Traits specifying if a given discretization tag supports coloring.
Definition coloring.hh:292