elementborderlistfromgrid.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  Copyright (C) 2011-2013 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef EWOMS_ELEMENT_BORDER_LIST_FROM_GRID_HH
26 #define EWOMS_ELEMENT_BORDER_LIST_FROM_GRID_HH
27 
28 #include "overlaptypes.hh"
29 #include "blacklist.hh"
30 
31 #include <dune/grid/common/datahandleif.hh>
32 #include <dune/grid/common/gridenums.hh>
33 #include <dune/istl/bcrsmatrix.hh>
34 #include <dune/istl/scalarproducts.hh>
35 #include <dune/istl/operators.hh>
36 #include <dune/common/version.hh>
37 
38 #include <algorithm>
39 
40 namespace Ewoms {
41 namespace Linear {
47 template <class GridView, class ElementMapper>
49 {
51  typedef BlackList::PeerBlackList PeerBlackList;
52  typedef BlackList::PeerBlackLists PeerBlackLists;
53 
54  typedef typename GridView::template Codim<0>::Entity Element;
55 
56  class BorderListHandle_
57  : public Dune::CommDataHandleIF<BorderListHandle_, int>
58  {
59  public:
60  BorderListHandle_(const GridView &gridView,
61  const ElementMapper &map,
64  : gridView_(gridView)
65  , map_(map)
66  , blackList_(blackList)
67  , borderList_(borderList)
68  {
69  auto elemIt = gridView_.template begin<0>();
70  const auto& elemEndIt = gridView_.template end<0>();
71  for (; elemIt != elemEndIt; ++elemIt) {
72  if (elemIt->partitionType() != Dune::InteriorEntity) {
73 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
74  int elemIdx = map_.index(*elemIt);
75 #else
76  int elemIdx = map_.map(*elemIt);
77 #endif
78  blackList_.addIndex(elemIdx);
79  }
80  }
81  }
82 
83  // data handle methods
84  bool contains(int dim, int codim) const
85  { return codim == 0; }
86 
87  bool fixedsize(int dim, int codim) const
88  { return true; }
89 
90  template <class EntityType>
91  size_t size(const EntityType &e) const
92  { return 2; }
93 
94  template <class MessageBufferImp, class EntityType>
95  void gather(MessageBufferImp &buff, const EntityType &e) const
96  {
97 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
98  int myIdx = map_.index(e);
99 #else
100  int myIdx = map_.map(e);
101 #endif
102  buff.write(static_cast<int>(gridView_.comm().rank()));
103  buff.write(myIdx);
104  }
105 
106  template <class MessageBufferImp>
107  void scatter(MessageBufferImp &buff, const Element &e, size_t n)
108  {
109  // discard the index if it is not on the process boundary
110  bool isInteriorNeighbor = false;
111  auto isIt = gridView_.ibegin(e);
112  const auto &isEndIt = gridView_.iend(e);
113  for (; isIt != isEndIt; ++isIt) {
114  if (!isIt->neighbor())
115  continue;
116 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
117  else if (isIt->outside().partitionType() == Dune::InteriorEntity) {
118 #else
119  else if (isIt->outside()->partitionType() == Dune::InteriorEntity) {
120 #endif
121  isInteriorNeighbor = true;
122  break;
123  }
124  }
125  if (!isInteriorNeighbor)
126  return;
127 
128  BorderIndex bIdx;
129 
130 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
131  bIdx.localIdx = map_.index(e);
132 #else
133  bIdx.localIdx = map_.map(e);
134 #endif
135  {
136  int tmp;
137  buff.read(tmp);
138  bIdx.peerRank = tmp;
139  peerSet_.insert(tmp);
140  }
141  {
142  int tmp;
143  buff.read(tmp);
144  bIdx.peerIdx = tmp;
145  }
146  bIdx.borderDistance = 1;
147 
148  borderList_.push_back(bIdx);
149  }
150 
151  // this template method is needed because the above one only works for codim-0
152  // entities (i.e., elements) but the dune grid uses some code which causes the
153  // compiler to invoke the scatter method for every codim...
154  template <class MessageBufferImp, class EntityType>
155  void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
156  { }
157 
158  const std::set<ProcessRank>& peerSet() const
159  { return peerSet_; }
160 
161  private:
162  GridView gridView_;
163  const ElementMapper &map_;
164  std::set<ProcessRank> peerSet_;
165  BlackList &blackList_;
166  BorderList &borderList_;
167  };
168 
169  class PeerBlackListHandle_
170  : public Dune::CommDataHandleIF<PeerBlackListHandle_, int>
171  {
172  public:
173  PeerBlackListHandle_(const GridView &gridView,
174  const ElementMapper &map,
175  PeerBlackLists& peerBlackLists)
176  : gridView_(gridView)
177  , map_(map)
178  , peerBlackLists_(peerBlackLists)
179  {}
180 
181  // data handle methods
182  bool contains(int dim, int codim) const
183  { return codim == 0; }
184 
185  bool fixedsize(int dim, int codim) const
186  { return true; }
187 
188  template <class EntityType>
189  size_t size(const EntityType &e) const
190  { return 2; }
191 
192  template <class MessageBufferImp, class EntityType>
193  void gather(MessageBufferImp &buff, const EntityType &e) const
194  {
195  buff.write(static_cast<int>(gridView_.comm().rank()));
196 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
197  buff.write(static_cast<int>(map_.index(e)));
198 #else
199  buff.write(static_cast<int>(map_.map(e)));
200 #endif
201  }
202 
203  template <class MessageBufferImp, class EntityType>
204  void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
205  {
206  int peerRank;
207  int peerIdx;
208  int localIdx;
209 
210  buff.read(peerRank);
211  buff.read(peerIdx);
212 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2, 4)
213  localIdx = map_.index(e);
214 #else
215  localIdx = map_.map(e);
216 #endif
217 
218  PeerBlackListedEntry pIdx;
219  pIdx.nativeIndexOfPeer = peerIdx;
220  pIdx.myOwnNativeIndex = localIdx;
221 
222  peerBlackLists_[peerRank].push_back(pIdx);
223  }
224 
225  const PeerBlackList& peerBlackList(ProcessRank peerRank) const
226  { return peerBlackLists_.at(peerRank); }
227 
228  private:
229  GridView gridView_;
230  const ElementMapper &map_;
231  PeerBlackLists peerBlackLists_;
232  };
233 
234 public:
235  ElementBorderListFromGrid(const GridView &gridView, const ElementMapper &map)
236  : gridView_(gridView)
237  , map_(map)
238  {
239  BorderListHandle_ blh(gridView, map, blackList_, borderList_);
240  gridView.communicate(blh,
241  Dune::InteriorBorder_All_Interface,
242  Dune::BackwardCommunication);
243 
244  PeerBlackListHandle_ pblh(gridView, map, peerBlackLists_);
245  gridView.communicate(pblh,
246  Dune::InteriorBorder_All_Interface,
247  Dune::BackwardCommunication);
248 
249  auto peerIt = blh.peerSet().begin();
250  const auto& peerEndIt = blh.peerSet().end();
251  for (; peerIt != peerEndIt; ++peerIt) {
252  blackList_.setPeerList(*peerIt, pblh.peerBlackList(*peerIt));
253  }
254  }
255 
256  // Access to the border list.
257  const BorderList &borderList() const
258  { return borderList_; }
259 
260  // Access to the black-list indices.
261  const BlackList& blackList() const
262  { return blackList_; }
263 
264 private:
265  const GridView gridView_;
266  const ElementMapper &map_;
267 
268  BorderList borderList_;
269 
270  BlackList blackList_;
271  PeerBlackLists peerBlackLists_;
272 };
273 
274 } // namespace Linear
275 } // namespace Ewoms
276 
277 #endif
Index nativeIndexOfPeer
Definition: blacklist.hh:49
const BlackList & blackList() const
Definition: elementborderlistfromgrid.hh:261
This files provides several data structures for storing tuples of indices of remote and/or local proc...
const BorderList & borderList() const
Definition: elementborderlistfromgrid.hh:257
std::vector< PeerBlackListedEntry > PeerBlackList
Definition: blacklist.hh:52
ElementBorderListFromGrid(const GridView &gridView, const ElementMapper &map)
Definition: elementborderlistfromgrid.hh:235
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
Definition: blacklist.hh:45
BorderDistance borderDistance
Distance to the process border for the peer (in hops)
Definition: overlaptypes.hh:112
std::list< BorderIndex > BorderList
This class managages a list of indices which are on the border of a process' partition of the grid...
Definition: overlaptypes.hh:119
Index localIdx
Index of the entity for the local process.
Definition: overlaptypes.hh:103
Index peerIdx
Index of the entity for the peer process.
Definition: overlaptypes.hh:106
A single index intersecting with the process boundary.
Definition: overlaptypes.hh:100
Uses communication on the grid to find the initial seed list of indices for methods which use element...
Definition: elementborderlistfromgrid.hh:48
Definition: baseauxiliarymodule.hh:35
Definition: blacklist.hh:48
std::map< ProcessRank, PeerBlackList > PeerBlackLists
Definition: blacklist.hh:53
ProcessRank peerRank
Rank of the peer process.
Definition: overlaptypes.hh:109
Index myOwnNativeIndex
Definition: blacklist.hh:50
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
unsigned ProcessRank
The type of the rank of a process.
Definition: overlaptypes.hh:48