AluGridCartesianIndexMapper.hpp
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*/
23
24#ifndef OPM_ALUGRID_CARTESIAN_INDEX_MAPPER_HPP
25#define OPM_ALUGRID_CARTESIAN_INDEX_MAPPER_HPP
26
27#include <dune/alugrid/grid.hh>
28#include <dune/alugrid/3d/gridview.hh>
29#include <opm/grid/common/CartesianIndexMapper.hpp>
30#include <dune/grid/common/datahandleif.hh>
31#include <dune/grid/utility/persistentcontainer.hh>
32
33#include <array>
34#include <cassert>
35#include <cstddef>
36#include <memory>
37#include <stdexcept>
38#include <vector>
39
40namespace Dune {
41
46//#if HAVE_MPI
47 template <>
48 class CartesianIndexMapper<Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming>>
49{
50public:
51
52#if HAVE_MPI
53 using Grid = Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming, Dune::ALUGridMPIComm>;
54#else
55 using Grid = Dune::ALUGrid<3, 3, Dune::cube, Dune::nonconforming, Dune::ALUGridNoComm>;
56#endif //HAVE_MPI
57
58 // data handle for communicating global ids during load balance and communication
59 template <class GridView>
60 class GlobalIndexDataHandle : public Dune::CommDataHandleIF<GlobalIndexDataHandle<GridView>, int>
61 {
62 // global id
63 class GlobalCellIndex
64 {
65 public:
66 GlobalCellIndex()
67 : idx_(-1)
68 {}
69
70 GlobalCellIndex& operator=(const int index)
71 {
72 idx_ = index;
73 return *this;
74 }
75
76 int index() const
77 { return idx_; }
78
79 private:
80 int idx_;
81 };
82
83 using GlobalIndexContainer = typename Dune::PersistentContainer<Grid, GlobalCellIndex>;
84
85 public:
86 // constructor copying cartesian index to persistent container
87 GlobalIndexDataHandle(const GridView& gridView,
88 std::vector<int>& cartesianIndex)
89 : gridView_(gridView)
90 , globalIndex_(gridView.grid(), 0)
91 , cartesianIndex_(cartesianIndex)
92 {
93 globalIndex_.resize();
94 initialize();
95 }
96
97 // constructor copying cartesian index to persistent container
98 GlobalIndexDataHandle(const GlobalIndexDataHandle& other) = delete ;
99
100 // destrcutor writing load balanced cartesian index back to vector
102 { finalize(); }
103
104 bool contains(int /* dim */, int codim) const
105 { return codim == 0; }
106
107 bool fixedsize(int /* dim */, int /* codim */) const
108 { return true; }
109
112 template<class MessageBufferImp, class EntityType>
113 void gather(MessageBufferImp& buff, const EntityType& element) const
114 {
115 int globalIdx = globalIndex_[element].index();
116 buff.write(globalIdx);
117 }
118
121 template<class MessageBufferImp, class EntityType>
122 void scatter(MessageBufferImp& buff, const EntityType& element, std::size_t /* n */)
123 {
124 int globalIdx = -1;
125 buff.read(globalIdx);
126 if (globalIdx >= 0)
127 {
128 globalIndex_.resize();
129 globalIndex_[element] = globalIdx;
130 }
131 }
132
135 template<class EntityType>
136 std::size_t size(const EntityType& /* en */) const
137 { return 1; }
138
139 protected:
140 // initialize persistent container from given vector
142 {
143 auto idx = cartesianIndex_.begin();
144 auto it = gridView_.template begin<0>();
145 const auto& endIt = gridView_.template end<0>();
146
147 for (; it != endIt; ++it, ++idx)
148 globalIndex_[*it] = *idx;
149 }
150
151 // update vector from given persistent container
152 void finalize()
153 {
154 std::vector<int> newIndex ;
155 newIndex.reserve(gridView_.indexSet().size(0));
156
157 auto it = gridView_.template begin<0>();
158 const auto& endIt = gridView_.template end<0>();
159 for (; it != endIt; ++it)
160 newIndex.push_back(globalIndex_[*it].index()) ;
161
162 cartesianIndex_.swap(newIndex);
163 }
164
165 GridView gridView_;
166 GlobalIndexContainer globalIndex_;
167 std::vector<int>& cartesianIndex_;
168 };
169
170public:
172 static constexpr int dimension = Grid::dimension ;
173
176 const std::array<int, dimension>& cartDims,
177 const std::vector<int>& cartesianIndex)
178 : grid_(grid)
179 , cartesianDimensions_(cartDims)
180 , cartesianIndex_(cartesianIndex)
181 , cartesianSize_(computeCartesianSize())
182 {}
183
185 const std::array<int, dimension>& cartesianDimensions() const
186 { return cartesianDimensions_; }
187
189 int cartesianSize() const
190 { return cartesianSize_; }
191
193 int compressedSize() const
194 { return cartesianIndex_.size(); }
195
198 { return cartesianIndex_.size(); }
199
201 int cartesianIndex(const int compressedElementIndex) const
202 {
203 assert(compressedElementIndex < compressedSize());
204 return cartesianIndex_[compressedElementIndex];
205 }
206
208 int cartesianIndex(const std::array<int, dimension>& coords) const
209 {
210 int cartIndex = coords[0];
211 int factor = cartesianDimensions()[0];
212 for (int i=1; i < dimension; ++i) {
213 cartIndex += coords[i] * factor;
214 factor *= cartesianDimensions()[i];
215 }
216
217 return cartIndex;
218 }
219
221 void cartesianCoordinate(const int compressedElementIndex, std::array<int, dimension>& coords) const
222 {
223 int gc = cartesianIndex(compressedElementIndex);
224 if constexpr (dimension == 3) {
225 coords[0] = gc % cartesianDimensions()[0];
226 gc /= cartesianDimensions()[0];
227 coords[1] = gc % cartesianDimensions()[1];
228 coords[2] = gc / cartesianDimensions()[1];
229 }
230 else if constexpr (dimension == 2) {
231 coords[0] = gc % cartesianDimensions()[0];
232 coords[1] = gc / cartesianDimensions()[0];
233 }
234 else if constexpr (dimension == 1)
235 coords[0] = gc ;
236 else
237 throw std::invalid_argument("cartesianCoordinate not implemented for dimension " + std::to_string(dimension));
238 }
239
241 void cartesianCoordinateLevel(const int compressedElementIndex, std::array<int, dimension>& coords, int level) const
242 {
243 if (level) {
244 throw std::invalid_argument("Invalid level.\n");
245 }
246 cartesianCoordinate(compressedElementIndex, coords);
247 }
248
249 template <class GridView>
250 std::unique_ptr<GlobalIndexDataHandle<GridView> > dataHandle(const GridView& gridView)
251 {
252 using DataHandle = GlobalIndexDataHandle<GridView>;
253 assert(&grid_ == &gridView.grid());
254 return std::make_unique<DataHandle>(gridView, cartesianIndex_);
255 }
256
257protected:
259 {
260 int size = cartesianDimensions()[0];
261 for (int d=1; d<dimension; ++d)
262 size *= cartesianDimensions()[d];
263 return size ;
264 }
265
266 const Grid& grid_;
267 const std::array<int, dimension> cartesianDimensions_;
268 std::vector<int> cartesianIndex_;
269 const int cartesianSize_ ;
270};
271
272} // end namespace Dune
273
274#endif // OPM_ALUGRID_CARTESIAN_INDEX_MAPPER_HPP
void gather(MessageBufferImp &buff, const EntityType &element) const
loop over all internal data handlers and call gather for given entity
Definition: AluGridCartesianIndexMapper.hpp:113
std::size_t size(const EntityType &) const
loop over all internal data handlers and return sum of data size of given entity
Definition: AluGridCartesianIndexMapper.hpp:136
void scatter(MessageBufferImp &buff, const EntityType &element, std::size_t)
loop over all internal data handlers and call scatter for given entity
Definition: AluGridCartesianIndexMapper.hpp:122
GlobalIndexDataHandle(const GridView &gridView, std::vector< int > &cartesianIndex)
Definition: AluGridCartesianIndexMapper.hpp:87
std::vector< int > & cartesianIndex_
Definition: AluGridCartesianIndexMapper.hpp:167
bool contains(int, int codim) const
Definition: AluGridCartesianIndexMapper.hpp:104
int cartesianSize() const
return total number of cells in the logical Cartesian grid
Definition: AluGridCartesianIndexMapper.hpp:189
std::unique_ptr< GlobalIndexDataHandle< GridView > > dataHandle(const GridView &gridView)
Definition: AluGridCartesianIndexMapper.hpp:250
CartesianIndexMapper(const Grid &grid, const std::array< int, dimension > &cartDims, const std::vector< int > &cartesianIndex)
constructor taking grid
Definition: AluGridCartesianIndexMapper.hpp:175
const Grid & grid_
Definition: AluGridCartesianIndexMapper.hpp:266
const std::array< int, dimension > cartesianDimensions_
Definition: AluGridCartesianIndexMapper.hpp:267
const std::array< int, dimension > & cartesianDimensions() const
return Cartesian dimensions, i.e. number of cells in each direction
Definition: AluGridCartesianIndexMapper.hpp:185
Dune::ALUGrid< 3, 3, Dune::cube, Dune::nonconforming, Dune::ALUGridMPIComm > Grid
Definition: AluGridCartesianIndexMapper.hpp:53
int cartesianIndex(const std::array< int, dimension > &coords) const
return index of the cells in the logical Cartesian grid
Definition: AluGridCartesianIndexMapper.hpp:208
void cartesianCoordinateLevel(const int compressedElementIndex, std::array< int, dimension > &coords, int level) const
Only for unifying calls with CartesianIndexMapper<CpGrid> where levels are relevant.
Definition: AluGridCartesianIndexMapper.hpp:241
int compressedLevelZeroSize() const
return number of cells in the active grid. Only for unifying calls with CpGrid and PolyhedralGrid spe...
Definition: AluGridCartesianIndexMapper.hpp:197
void cartesianCoordinate(const int compressedElementIndex, std::array< int, dimension > &coords) const
return Cartesian coordinate, i.e. IJK, for a given cell
Definition: AluGridCartesianIndexMapper.hpp:221
const int cartesianSize_
Definition: AluGridCartesianIndexMapper.hpp:269
int computeCartesianSize() const
Definition: AluGridCartesianIndexMapper.hpp:258
int cartesianIndex(const int compressedElementIndex) const
return index of the cells in the logical Cartesian grid
Definition: AluGridCartesianIndexMapper.hpp:201
std::vector< int > cartesianIndex_
Definition: AluGridCartesianIndexMapper.hpp:268
int compressedSize() const
return number of cells in the active grid
Definition: AluGridCartesianIndexMapper.hpp:193
Definition: CollectDataOnIORank.hpp:49
Definition: SupportsFaceTag.hpp:27
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)