3.5-git
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
149private:
150 void update_()
151 {
152 // Compute scotch reordering
153 Dune::Timer watch;
154 // Create the graph as an adjacency list
155 std::vector<std::vector<Index>> graph(size());
156
157 // dofs on element centers (cell-centered methods)
158 if (codimension_ == 0)
159 {
160 for (const auto& element : elements(gridView_))
161 {
162 auto eIdx = indexSet_->index(element);
163 for (const auto& intersection : intersections(gridView_, element))
164 {
165 if (intersection.neighbor())
166 graph[eIdx].push_back(indexSet_->index(intersection.outside()));
167 }
168 }
169 }
170 // dof on vertices (box method)
171 else
172 {
173 for (const auto& element : elements(gridView_))
174 {
175 auto eIdx = indexSet_->index(element);
176 for (int vIdxLocal = 0; vIdxLocal < element.subEntities(codimension_); ++vIdxLocal)
177 {
178 auto vIdxGlobal = indexSet_->subIndex(element, vIdxLocal, codimension_);
179 graph[vIdxGlobal].push_back(eIdx);
180 }
181 }
182 }
183
184 permutation_ = ScotchBackend<Index>::computeGPSReordering(graph);
185 std::cout << "Scotch backend reordered index set of size " << size()
186 << " in " << watch.elapsed() << " seconds." << std::endl;
187 }
188 // GridView is needed to keep the IndexSet valid
189 GridView gridView_;
190 const typename GridView::IndexSet* indexSet_;
191 const int codimension_;
192 // the map resulting from the reordering
193 std::vector<int> permutation_;
194};
195
196} // end namespace Dumux
197
198#else
199
200#warning "PTSCOTCH was not found on your system. Dumux::ReorderingDofMapper needs it to work -> fallback to MCMGMapper without reordering!"
201#include <dune/grid/common/mcmgmapper.hh>
202namespace Dumux {
203template<class GridView>
204using ReorderingDofMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
205} // end namespace Dumux
206
207#endif // HAVE_PTSCOTCH
208#endif
An interface to the scotch library for matrix reordering.
Definition: adapt.hh:29
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > ReorderingDofMapper
Definition: reorderingdofmapper.hh:204
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition: scotchbackend.hh:169