version 3.10-dev
reorderingdofmapper.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
14#ifndef DUMUX_COMMON_REORDERING_DOF_MAPPER_HH
15#define DUMUX_COMMON_REORDERING_DOF_MAPPER_HH
16
17#if HAVE_PTSCOTCH
18
19#include <dune/common/timer.hh>
20#include <dune/grid/common/mapper.hh>
21
23
24namespace Dumux {
25
31template<class GridView>
33: public Dune::Mapper<typename GridView::Grid, ReorderingDofMapper<GridView>, typename GridView::IndexSet::IndexType>
34{
35 using Index = typename GridView::IndexSet::IndexType;
36 using ParentType = typename Dune::Mapper<typename GridView::Grid, ReorderingDofMapper<GridView>, Index>;
37 using Element = typename GridView::template Codim<0>::Entity;
38public:
39
45 template<class Layout>
46 ReorderingDofMapper (const GridView& gridView, Layout&& layout)
47 : gridView_(gridView)
48 , indexSet_(&gridView.indexSet())
49 , codimension_(layout(indexSet_->types(0)[0], GridView::dimension) ? 0 : GridView::dimension)
50 {
51 update_();
52 }
53
61 template<class EntityType>
62 Index index (const EntityType& e) const
63 {
64 // map the index using the permutation obtained from the reordering algorithm
65 return static_cast<Index>(permutation_[indexSet_->index(e)]);
66 }
67
75 Index subIndex (const Element& e, int i, unsigned int codim) const
76 {
77 return indexSet_->subIndex(e, i, codim);
78 }
79
88 std::size_t size () const
89 {
90 return indexSet_->size(codimension_);
91 }
92
99 template<class EntityType>
100 bool contains (const EntityType& e, Index& result) const
101 {
102 result = index(e);
103 return true;
104 }
105
114 bool contains (const Element& e, int i, int cc, Index& result) const
115 {
116 result = indexSet_->subIndex(e, i, cc);
117 return true;
118 }
119
123 void update (const GridView& gridView)
124 {
125 gridView_ = gridView;
126 indexSet_ = &gridView_.indexSet();
127 update_();
128 }
129
130 void update (GridView&& gridView)
131 {
132 gridView_ = std::move(gridView);
133 indexSet_ = &gridView_.indexSet();
134 update_();
135 }
136
137private:
138 void update_()
139 {
140 // Compute scotch reordering
141 Dune::Timer watch;
142 // Create the graph as an adjacency list
143 std::vector<std::vector<Index>> graph(size());
144
145 // dofs on element centers (cell-centered methods)
146 if (codimension_ == 0)
147 {
148 for (const auto& element : elements(gridView_))
149 {
150 auto eIdx = indexSet_->index(element);
151 for (const auto& intersection : intersections(gridView_, element))
152 {
153 if (intersection.neighbor())
154 graph[eIdx].push_back(indexSet_->index(intersection.outside()));
155 }
156 }
157 }
158 // dof on vertices (box method)
159 else
160 {
161 for (const auto& element : elements(gridView_))
162 {
163 auto eIdx = indexSet_->index(element);
164 for (int vIdxLocal = 0; vIdxLocal < element.subEntities(codimension_); ++vIdxLocal)
165 {
166 auto vIdxGlobal = indexSet_->subIndex(element, vIdxLocal, codimension_);
167 graph[vIdxGlobal].push_back(eIdx);
168 }
169 }
170 }
171
172 permutation_ = ScotchBackend<Index>::computeGPSReordering(graph);
173 std::cout << "Scotch backend reordered index set of size " << size()
174 << " in " << watch.elapsed() << " seconds." << std::endl;
175 }
176 // GridView is needed to keep the IndexSet valid
177 GridView gridView_;
178 const typename GridView::IndexSet* indexSet_;
179 const int codimension_;
180 // the map resulting from the reordering
181 std::vector<int> permutation_;
182};
183
184} // end namespace Dumux
185
186#else
187
188#warning "PTSCOTCH was not found on your system. Dumux::ReorderingDofMapper needs it to work -> fallback to MCMGMapper without reordering!"
189#include <dune/grid/common/mcmgmapper.hh>
190namespace Dumux {
191template<class GridView>
192using ReorderingDofMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
193} // end namespace Dumux
194
195#endif // HAVE_PTSCOTCH
196#endif
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition: scotchbackend.hh:154
Definition: adapt.hh:17
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > ReorderingDofMapper
Definition: reorderingdofmapper.hh:192
An interface to the scotch library for matrix reordering.