3.1-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 ()
136 {
137 // Compute scotch reordering
138 Dune::Timer watch;
139 // Create the graph as an adjacency list
140 std::vector<std::vector<Index>> graph(size());
141
142 // dofs on element centers (cell-centered methods)
143 if (codimension_ == 0)
144 {
145 for (const auto& element : elements(gridView_))
146 {
147 auto eIdx = indexSet_.index(element);
148 for (const auto& intersection : intersections(gridView_, element))
149 {
150 if (intersection.neighbor())
151 graph[eIdx].push_back(indexSet_.index(intersection.outside()));
152 }
153 }
154 }
155 // dof on vertices (box method)
156 else
157 {
158 for (const auto& element : elements(gridView_))
159 {
160 auto eIdx = indexSet_.index(element);
161 for (int vIdxLocal = 0; vIdxLocal < element.subEntities(codimension_); ++vIdxLocal)
162 {
163 auto vIdxGlobal = indexSet_.subIndex(element, vIdxLocal, codimension_);
164 graph[vIdxGlobal].push_back(eIdx);
165 }
166 }
167 }
168
169 permutation_ = ScotchBackend<Index>::computeGPSReordering(graph);
170 std::cout << "Scotch backend reordered index set of size " << size()
171 << " in " << watch.elapsed() << " seconds." << std::endl;
172 }
173
174private:
175 // GridView is needed to keep the IndexSet valid
176 const GridView gridView_;
177 const typename GridView::IndexSet& indexSet_;
178 const int codimension_;
179 // the map resulting from the reordering
180 std::vector<int> permutation_;
181};
182
183} // end namespace Dumux
184
185#else
186
187#warning "PTSCOTCH was not found on your system. Dumux::ReorderingDofMapper needs it to work -> fallback to MCMGMapper without reordering!"
188#include <dune/grid/common/mcmgmapper.hh>
189namespace Dumux {
190template<class GridView>
191using ReorderingDofMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
192} // end namespace Dumux
193
194#endif // HAVE_PTSCOTCH
195#endif // DUMUX_COMMON_REORDERING_DOF_MAPPER_HH
An interface to the scotch library for matrix reordering.
Dune::IteratorRange< typename MultiDomainGlue< DomainGridView, TargetGridView, DomainMapper, TargetMapper >::Intersections::const_iterator > intersections(const MultiDomainGlue< DomainGridView, TargetGridView, DomainMapper, TargetMapper > &glue)
Range generator to iterate with range-based for loops over all intersections as follows: for (const a...
Definition: glue.hh:62
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
Dune::MultipleCodimMultipleGeomTypeMapper< GridView > ReorderingDofMapper
Definition: reorderingdofmapper.hh:191
static std::vector< int > computeGPSReordering(const Graph &graph, std::size_t numPasses=5)
Definition: scotchbackend.hh:63