All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  This file is part of the Open Porous Media project (OPM).
5 
6  OPM 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 2 of the License, or
9  (at your option) any later version.
10 
11  OPM 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 OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef EWOMS_ELEMENT_BORDER_LIST_FROM_GRID_HH
28 #define EWOMS_ELEMENT_BORDER_LIST_FROM_GRID_HH
29 
30 #include "overlaptypes.hh"
31 #include "blacklist.hh"
32 
33 #include <opm/common/Unused.hpp>
34 
35 #include <dune/grid/common/datahandleif.hh>
36 #include <dune/grid/common/gridenums.hh>
37 #include <dune/istl/bcrsmatrix.hh>
38 #include <dune/istl/scalarproducts.hh>
39 #include <dune/istl/operators.hh>
40 #include <dune/common/version.hh>
41 
42 #include <algorithm>
43 
44 namespace Ewoms {
45 namespace Linear {
51 template <class GridView, class ElementMapper>
53 {
55  typedef BlackList::PeerBlackList PeerBlackList;
56  typedef BlackList::PeerBlackLists PeerBlackLists;
57 
58  typedef typename GridView::template Codim<0>::Entity Element;
59 
60  class BorderListHandle_
61  : public Dune::CommDataHandleIF<BorderListHandle_, ProcessRank>
62  {
63  public:
64  BorderListHandle_(const GridView& gridView,
65  const ElementMapper& map,
66  BlackList& blackList,
67  BorderList& borderList)
68  : gridView_(gridView)
69  , map_(map)
70  , blackList_(blackList)
71  , borderList_(borderList)
72  {
73  auto elemIt = gridView_.template begin<0>();
74  const auto& elemEndIt = gridView_.template end<0>();
75  for (; elemIt != elemEndIt; ++elemIt) {
76  if (elemIt->partitionType() != Dune::InteriorEntity) {
77  Index elemIdx = static_cast<Index>(map_.index(*elemIt));
78  blackList_.addIndex(elemIdx);
79  }
80  }
81  }
82 
83  // data handle methods
84  bool contains(int dim OPM_UNUSED, int codim) const
85  { return codim == 0; }
86 
87  bool fixedsize(int dim OPM_UNUSED, int codim OPM_UNUSED) const
88  { return true; }
89 
90  template <class EntityType>
91  size_t size(const EntityType& e OPM_UNUSED) const
92  { return 2; }
93 
94  template <class MessageBufferImp, class EntityType>
95  void gather(MessageBufferImp& buff, const EntityType& e) const
96  {
97  unsigned myIdx = static_cast<unsigned>(map_.index(e));
98  buff.write(static_cast<unsigned>(gridView_.comm().rank()));
99  buff.write(myIdx);
100  }
101 
102  template <class MessageBufferImp>
103  void scatter(MessageBufferImp& buff,
104  const Element& e,
105  size_t n OPM_UNUSED)
106  {
107  // discard the index if it is not on the process boundary
108  bool isInteriorNeighbor = false;
109  auto isIt = gridView_.ibegin(e);
110  const auto& isEndIt = gridView_.iend(e);
111  for (; isIt != isEndIt; ++isIt) {
112  if (!isIt->neighbor())
113  continue;
114  else if (isIt->outside().partitionType() == Dune::InteriorEntity) {
115  isInteriorNeighbor = true;
116  break;
117  }
118  }
119  if (!isInteriorNeighbor)
120  return;
121 
122  BorderIndex bIdx;
123 
124  bIdx.localIdx = static_cast<Index>(map_.index(e));
125  {
126  ProcessRank tmp;
127  buff.read(tmp);
128  bIdx.peerRank = tmp;
129  peerSet_.insert(tmp);
130  }
131  {
132  unsigned tmp;
133  buff.read(tmp);
134  bIdx.peerIdx = static_cast<Index>(tmp);
135  }
136  bIdx.borderDistance = 1;
137 
138  borderList_.push_back(bIdx);
139  }
140 
141  // this template method is needed because the above one only works for codim-0
142  // entities (i.e., elements) but the dune grid uses some code which causes the
143  // compiler to invoke the scatter method for every codim...
144  template <class MessageBufferImp, class EntityType>
145  void scatter(MessageBufferImp& buff OPM_UNUSED,
146  const EntityType& e OPM_UNUSED,
147  size_t n OPM_UNUSED)
148  { }
149 
150  const std::set<ProcessRank>& peerSet() const
151  { return peerSet_; }
152 
153  private:
154  GridView gridView_;
155  const ElementMapper& map_;
156  std::set<ProcessRank> peerSet_;
157  BlackList& blackList_;
158  BorderList& borderList_;
159  };
160 
161  class PeerBlackListHandle_
162  : public Dune::CommDataHandleIF<PeerBlackListHandle_, int>
163  {
164  public:
165  PeerBlackListHandle_(const GridView& gridView,
166  const ElementMapper& map,
167  PeerBlackLists& peerBlackLists)
168  : gridView_(gridView)
169  , map_(map)
170  , peerBlackLists_(peerBlackLists)
171  {}
172 
173  // data handle methods
174  bool contains(int dim OPM_UNUSED, int codim) const
175  { return codim == 0; }
176 
177  bool fixedsize(int dim OPM_UNUSED, int codim OPM_UNUSED) const
178  { return true; }
179 
180  template <class EntityType>
181  size_t size(const EntityType& e OPM_UNUSED) const
182  { return 2; }
183 
184  template <class MessageBufferImp, class EntityType>
185  void gather(MessageBufferImp& buff, const EntityType& e) const
186  {
187  buff.write(static_cast<int>(gridView_.comm().rank()));
188  buff.write(static_cast<int>(map_.index(e)));
189  }
190 
191  template <class MessageBufferImp, class EntityType>
192  void scatter(MessageBufferImp& buff, const EntityType& e, size_t n OPM_UNUSED)
193  {
194  int peerRank;
195  int peerIdx;
196  Index localIdx;
197 
198  buff.read(peerRank);
199  buff.read(peerIdx);
200  localIdx = static_cast<Index>(map_.index(e));
201 
203  pIdx.nativeIndexOfPeer = static_cast<Index>(peerIdx);
204  pIdx.myOwnNativeIndex = static_cast<Index>(localIdx);
205 
206  peerBlackLists_[static_cast<ProcessRank>(peerRank)].push_back(pIdx);
207  }
208 
209  const PeerBlackList& peerBlackList(ProcessRank peerRank) const
210  { return peerBlackLists_.at(peerRank); }
211 
212  private:
213  GridView gridView_;
214  const ElementMapper& map_;
215  PeerBlackLists peerBlackLists_;
216  };
217 
218 public:
219  ElementBorderListFromGrid(const GridView& gridView, const ElementMapper& map)
220  : gridView_(gridView)
221  , map_(map)
222  {
223  BorderListHandle_ blh(gridView, map, blackList_, borderList_);
224  gridView.communicate(blh,
225  Dune::InteriorBorder_All_Interface,
226  Dune::BackwardCommunication);
227 
228  PeerBlackListHandle_ pblh(gridView, map, peerBlackLists_);
229  gridView.communicate(pblh,
230  Dune::InteriorBorder_All_Interface,
231  Dune::BackwardCommunication);
232 
233  auto peerIt = blh.peerSet().begin();
234  const auto& peerEndIt = blh.peerSet().end();
235  for (; peerIt != peerEndIt; ++peerIt) {
236  blackList_.setPeerList(*peerIt, pblh.peerBlackList(*peerIt));
237  }
238  }
239 
240  // Access to the border list.
241  const BorderList& borderList() const
242  { return borderList_; }
243 
244  // Access to the black-list indices.
245  const BlackList& blackList() const
246  { return blackList_; }
247 
248 private:
249  const GridView gridView_;
250  const ElementMapper& map_;
251 
252  BorderList borderList_;
253 
254  BlackList blackList_;
255  PeerBlackLists peerBlackLists_;
256 };
257 
258 } // namespace Linear
259 } // namespace Ewoms
260 
261 #endif
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
Index peerIdx
Index of the entity for the peer process.
Definition: overlaptypes.hh:107
Index localIdx
Index of the entity for the local process.
Definition: overlaptypes.hh:104
Expresses which degrees of freedom are blacklisted for the parallel linear solvers and which domestic...
Definition: blacklist.hh:47
ProcessRank peerRank
Rank of the peer process.
Definition: overlaptypes.hh:110
This files provides several data structures for storing tuples of indices of remote and/or local proc...
BorderDistance borderDistance
Distance to the process border for the peer (in hops)
Definition: overlaptypes.hh:113
Uses communication on the grid to find the initial seed list of indices for methods which use element...
Definition: elementborderlistfromgrid.hh:52
A single index intersecting with the process boundary.
Definition: overlaptypes.hh:101