3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
19
26#ifndef DUMUX_COMMON_REORDERING_DOF_MAPPER_HH
27#define DUMUX_COMMON_REORDERING_DOF_MAPPER_HH
28
29#if HAVE_PTSCOTCH
30
31#include <dune/common/timer.hh>
32#include <dune/grid/common/mapper.hh>
33
35
36namespace Dumux {
37
43template<class GridView>
45: public Dune::Mapper<typename GridView::Grid, ReorderingDofMapper<GridView>, typename GridView::IndexSet::IndexType>
46{
47 using Index = typename GridView::IndexSet::IndexType;
48 using ParentType = typename Dune::Mapper<typename GridView::Grid, ReorderingDofMapper<GridView>, Index>;
49 using Element = typename GridView::template Codim<0>::Entity;
50public:
51
57 template<class Layout>
58 ReorderingDofMapper (const GridView& gridView, Layout&& layout)
59 : gridView_(gridView)
60 , indexSet_(&gridView.indexSet())
61 , codimension_(layout(indexSet_->types(0)[0], GridView::dimension) ? 0 : GridView::dimension)
62 {
63 update_();
64 }
65
73 template<class EntityType>
74 Index index (const EntityType& e) const
75 {
76 // map the index using the permutation obtained from the reordering algorithm
77 return static_cast<Index>(permutation_[indexSet_->index(e)]);
78 }
79
87 Index subIndex (const Element& e, int i, unsigned int codim) const
88 {
89 return indexSet_->subIndex(e, i, codim);
90 }
91
100 std::size_t size () const
101 {
102 return indexSet_->size(codimension_);
103 }
104
111 template<class EntityType>
112 bool contains (const EntityType& e, Index& result) const
113 {
114 result = index(e);
115 return true;
116 }
117
126 bool contains (const Element& e, int i, int cc, Index& result) const
127 {
128 result = indexSet_->subIndex(e, i, cc);
129 return true;
130 }
131
135 void update (const GridView& gridView)
136 {
137 gridView_ = gridView;
138 indexSet_ = &gridView_.indexSet();
139 update_();
140 }
141
142 void update (GridView&& gridView)
143 {
144 gridView_ = std::move(gridView);
145 indexSet_ = &gridView_.indexSet();
146 update_();
147 }
148
152 [[deprecated("Use update(gridView) instead! Will be removed after release 3.4.")]]
153 void update ()
154 {
155 update_();
156 }
157
158private:
159 void update_()
160 {
161 // Compute scotch reordering
162 Dune::Timer watch;
163 // Create the graph as an adjacency list
164 std::vector<std::vector<Index>> graph(size());
165
166 // dofs on element centers (cell-centered methods)
167 if (codimension_ == 0)
168 {
169 for (const auto& element : elements(gridView_))
170 {
171 auto eIdx = indexSet_->index(element);
172 for (const auto& intersection : intersections(gridView_, element))
173 {
174 if (intersection.neighbor())
175 graph[eIdx].push_back(indexSet_->index(intersection.outside()));
176 }
177 }
178 }
179 // dof on vertices (box method)
180 else
181 {
182 for (const auto& element : elements(gridView_))
183 {
184 auto eIdx = indexSet_->index(element);
185 for (int vIdxLocal = 0; vIdxLocal < element.subEntities(codimension_); ++vIdxLocal)
186 {
187 auto vIdxGlobal = indexSet_->subIndex(element, vIdxLocal, codimension_);
188 graph[vIdxGlobal].push_back(eIdx);
189 }
190 }
191 }
192
193 permutation_ = ScotchBackend<Index>::computeGPSReordering(graph);
194 std::cout << "Scotch backend reordered index set of size " << size()
195 << " in " << watch.elapsed() << " seconds." << std::endl;
196 }
197 // GridView is needed to keep the IndexSet valid
198 GridView gridView_;
199 const typename GridView::IndexSet* indexSet_;
200 const int codimension_;
201 // the map resulting from the reordering
202 std::vector<int> permutation_;
203};
204
205} // end namespace Dumux
206
207#else
208
209#warning "PTSCOTCH was not found on your system. Dumux::ReorderingDofMapper needs it to work -> fallback to MCMGMapper without reordering!"
210#include <dune/grid/common/mcmgmapper.hh>
211namespace Dumux {
212template<class GridView>
213using ReorderingDofMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
214} // end namespace Dumux
215
216#endif // HAVE_PTSCOTCH
217#endif
An interface to the scotch library for matrix reordering.
Definition: adapt.hh:29
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > ReorderingDofMapper
Definition: reorderingdofmapper.hh:213
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition: scotchbackend.hh:63