cartesianindexmapper.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 2015 IRIS AS
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 3 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 */
21 #ifndef OPM_CARTESIANINDEXMAPPER_HEADER
22 #define OPM_CARTESIANINDEXMAPPER_HEADER
23 
24 #include <array>
25 #include <cassert>
26 
27 #include <dune/common/exceptions.hh>
28 #include <dune/grid/common/datahandleif.hh>
29 #include <dune/grid/utility/persistentcontainer.hh>
30 
31 namespace Dune
32 {
36  template< class Grid >
38  {
39  public:
40  // data handle for communicating global ids during load balance and communication
41  template <class GridView>
42  class GlobalIndexDataHandle : public Dune::CommDataHandleIF< GlobalIndexDataHandle<GridView>, int >
43  {
44  // global id
45  class GlobalCellIndex
46  {
47  int idx_;
48  public:
49  GlobalCellIndex() : idx_(-1) {}
50  GlobalCellIndex& operator= ( const int index ) { idx_ = index; return *this; }
51  int index() const { return idx_; }
52  };
53 
54  typedef typename Dune::PersistentContainer< Grid, GlobalCellIndex > GlobalIndexContainer;
55 
56  GridView gridView_;
57  GlobalIndexContainer globalIndex_;
58  std::vector<int>& cartesianIndex_;
59  public:
60  // constructor copying cartesian index to persistent container
61  GlobalIndexDataHandle( const GridView& gridView,
62  std::vector<int>& cartesianIndex )
63  : gridView_( gridView ),
64  globalIndex_( gridView.grid(), 0 ),
65  cartesianIndex_( cartesianIndex )
66  {
67  globalIndex_.resize();
68  initialize();
69  }
70 
71  // constructor copying cartesian index to persistent container
72  GlobalIndexDataHandle( const GlobalIndexDataHandle& other ) = delete ;
73 
74  // destrcutor writing load balanced cartesian index back to vector
76  {
77  finalize();
78  //std::cout << "CartesianIndex " << cartesianIndex_.size() << std::endl;
79  //for( size_t i=0; i<cartesianIndex_.size(); ++i )
80  // std::cout << "cart[ " << i << " ] = " << cartesianIndex_[ i ] << std::endl;
81  }
82 
83  bool contains ( int dim, int codim ) const { return codim == 0; }
84  bool fixedsize( int dim, int codim ) const { return true; }
85 
88  template<class MessageBufferImp, class EntityType>
89  void gather (MessageBufferImp& buff, const EntityType& element ) const
90  {
91  int globalIdx = globalIndex_[ element ].index();
92  buff.write( globalIdx );
93  }
94 
97  template<class MessageBufferImp, class EntityType>
98  void scatter (MessageBufferImp& buff, const EntityType& element, size_t n)
99  {
100  int globalIdx = -1;
101  buff.read( globalIdx );
102  if( globalIdx >= 0 )
103  {
104  globalIndex_.resize();
105  globalIndex_[ element ] = globalIdx;
106  }
107  }
108 
111  template<class EntityType>
112  size_t size (const EntityType& en) const
113  {
114  return 1;
115  }
116 
117  protected:
118  // initialize persistent container from given vector
119  void initialize()
120  {
121  auto idx = cartesianIndex_.begin();
122  for(auto it = gridView_.template begin<0>(),
123  end = gridView_.template end<0>(); it != end; ++it, ++idx )
124  {
125  globalIndex_[ *it ] = *idx;
126  }
127  }
128 
129  // update vector from given persistent container
130  void finalize()
131  {
132  std::vector< int > newIndex ;
133  newIndex.reserve( gridView_.indexSet().size( 0 ) );
134 
135  for(auto it = gridView_.template begin<0>(),
136  end = gridView_.template end<0>(); it != end; ++it)
137  {
138  newIndex.push_back( globalIndex_[ *it ].index() ) ;
139  }
140 
141  cartesianIndex_.swap( newIndex );
142  }
143 
144  };
145 
146  public:
148  static const int dimension = Grid :: dimension ;
149 
151  CartesianIndexMapper( const Grid& grid,
152  const std::array<int, dimension>& cartDims,
153  const std::vector<int>& cartesianIndex )
154  : grid_( grid ),
155  cartesianDimensions_( cartDims ),
156  cartesianIndex_( cartesianIndex ),
158  {
159  }
160 
162  const std::array<int, dimension>& cartesianDimensions() const
163  {
164  return cartesianDimensions_;
165  }
166 
168  int cartesianSize() const
169  {
170  return cartesianSize_;
171  }
172 
174  int compressedSize() const
175  {
176  return cartesianIndex_.size();
177  }
178 
180  int cartesianIndex( const int compressedElementIndex ) const
181  {
182  assert( compressedElementIndex < compressedSize() );
183  return cartesianIndex_[ compressedElementIndex ];
184  }
185 
187  int cartesianIndex( const std::array<int,dimension>& coords ) const
188  {
189  int cartIndex = coords[ 0 ];
190  int factor = cartesianDimensions()[ 0 ];
191  for( int i=1; i<dimension; ++i )
192  {
193  cartIndex += coords[ i ] * factor;
194  factor *= cartesianDimensions()[ i ];
195  }
196  return cartIndex;
197  }
198 
200  void cartesianCoordinate(const int compressedElementIndex, std::array<int,dimension>& coords) const
201  {
202  int gc = cartesianIndex( compressedElementIndex );
203  if( dimension >=2 )
204  {
205  for( int d=0; d<dimension-2; ++d )
206  {
207  coords[d] = gc % cartesianDimensions()[d]; gc /= cartesianDimensions()[d];
208  }
209 
210  coords[dimension-2] = gc % cartesianDimensions()[dimension-2];
211  coords[dimension-1] = gc / cartesianDimensions()[dimension-1];
212  }
213  else
214  coords[ 0 ] = gc ;
215  }
216 
217  template <class GridView>
218  std::unique_ptr< GlobalIndexDataHandle< GridView > > dataHandle( const GridView& gridView )
219  {
220  typedef GlobalIndexDataHandle< GridView > DataHandle ;
221  assert( &grid_ == &gridView.grid() );
222  return std::unique_ptr< DataHandle > (new DataHandle( gridView, cartesianIndex_ ));
223  }
224 
225  protected:
227  {
228  int size = cartesianDimensions()[ 0 ];
229  for( int d=1; d<dimension; ++d )
230  size *= cartesianDimensions()[ d ];
231  return size ;
232  }
233 
234  protected:
235  const Grid& grid_;
236  const std::array<int, dimension> cartesianDimensions_;
237  std::vector<int> cartesianIndex_;
238  const int cartesianSize_ ;
239  };
240 
241 } // end namespace Opm
242 #endif
CartesianIndexMapper(const Grid &grid, const std::array< int, dimension > &cartDims, const std::vector< int > &cartesianIndex)
constructor taking grid
Definition: cartesianindexmapper.hh:151
Definition: cartesianindexmapper.hh:42
size_t size(const EntityType &en) const
loop over all internal data handlers and return sum of data size of given entity
Definition: cartesianindexmapper.hh:112
int computeCartesianSize() const
Definition: cartesianindexmapper.hh:226
int compressedSize() const
return number of cells in the active grid
Definition: cartesianindexmapper.hh:174
int cartesianSize() const
return total number of cells in the logical Cartesian grid
Definition: cartesianindexmapper.hh:168
const std::array< int, dimension > cartesianDimensions_
Definition: cartesianindexmapper.hh:236
int cartesianIndex(const int compressedElementIndex) const
return index of the cells in the logical Cartesian grid
Definition: cartesianindexmapper.hh:180
std::vector< int > cartesianIndex_
Definition: cartesianindexmapper.hh:237
void initialize()
Definition: cartesianindexmapper.hh:119
std::unique_ptr< GlobalIndexDataHandle< GridView > > dataHandle(const GridView &gridView)
Definition: cartesianindexmapper.hh:218
const std::array< int, dimension > & cartesianDimensions() const
return Cartesian dimensions, i.e. number of cells in each direction
Definition: cartesianindexmapper.hh:162
void gather(MessageBufferImp &buff, const EntityType &element) const
loop over all internal data handlers and call gather for given entity
Definition: cartesianindexmapper.hh:89
Definition: cartesianindexmapper.hh:31
Interface class to access the logical Cartesian grid as used in industry standard simulator decks...
Definition: cartesianindexmapper.hh:37
const int cartesianSize_
Definition: cartesianindexmapper.hh:238
int cartesianIndex(const std::array< int, dimension > &coords) const
return index of the cells in the logical Cartesian grid
Definition: cartesianindexmapper.hh:187
const Grid & grid_
Definition: cartesianindexmapper.hh:235
bool contains(int dim, int codim) const
Definition: cartesianindexmapper.hh:83
bool fixedsize(int dim, int codim) const
Definition: cartesianindexmapper.hh:84
void finalize()
Definition: cartesianindexmapper.hh:130
~GlobalIndexDataHandle()
Definition: cartesianindexmapper.hh:75
void cartesianCoordinate(const int compressedElementIndex, std::array< int, dimension > &coords) const
return Cartesian coordinate, i.e. IJK, for a given cell
Definition: cartesianindexmapper.hh:200
GlobalIndexDataHandle(const GridView &gridView, std::vector< int > &cartesianIndex)
Definition: cartesianindexmapper.hh:61
static const int dimension
dimension of the grid
Definition: cartesianindexmapper.hh:148
void scatter(MessageBufferImp &buff, const EntityType &element, size_t n)
loop over all internal data handlers and call scatter for given entity
Definition: cartesianindexmapper.hh:98