Cell.h 13.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can 
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of 
//  the License, or (at your option) any later version.
//  
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT 
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
//  for more details.
//  
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file Cell.h
//! \ingroup core
//! \author Christian Godenschwager <christian.godenschwager@fau.de>
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#pragma once

#include "core/DataTypes.h"
#include "core/debug/Debug.h"
#include "core/mpi/BufferSizeTrait.h"
#include "core/mpi/RecvBuffer.h"
#include "core/mpi/SendBuffer.h"

#include <algorithm>
#include <iterator>
33
#include <iostream>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


namespace walberla {
namespace cell {



/*******************************************************************************************************************//**
 * \brief   A representation of a Cell's coordinates (in 3D)
 **********************************************************************************************************************/
class Cell
{
public:

   /*! \name Constructors */
   //@{
Sebastian Eibl's avatar
Sebastian Eibl committed
50
   Cell() = default;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
   inline Cell( const cell_idx_t _x, const cell_idx_t _y, const cell_idx_t _z ) { cell[0] = _x; cell[1] = _y; cell[2] = _z; }
 //inline Cell( const int        _x, const int        _y, const int        _z );
   inline Cell( const uint_t     _x, const uint_t     _y, const uint_t     _z );
   //@}

   /*! \name Arithmetic operators */
   //@{
   inline Cell operator+( const Cell & rhs ) const;
   inline Cell operator-( const Cell & rhs ) const;

   inline Cell & operator+=( const Cell & rhs );
   inline Cell & operator-=( const Cell & rhs );

   inline Cell operator+() const;
   inline Cell operator-() const;
   //@}

   /*! \name Comparison operators */
   //@{
   bool operator< ( const Cell & rhs ) const;
   bool operator==( const Cell & rhs ) const;
   bool operator!=( const Cell & rhs ) const { return !operator==( rhs ); }
   //@}

   /*! \name Access operators */
   //@{
77
78
   cell_idx_t   operator[]( std::size_t idx ) const;
   cell_idx_t & operator[]( std::size_t idx );
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

   cell_idx_t   x() const { return cell[0]; }
   cell_idx_t & x()       { return cell[0]; }
   cell_idx_t   y() const { return cell[1]; }
   cell_idx_t & y()       { return cell[1]; }
   cell_idx_t   z() const { return cell[2]; }
   cell_idx_t & z()       { return cell[2]; }
   //@}

   bool positiveIndicesOnly() const { return x() >= cell_idx_c(0) && y() >= cell_idx_c(0) && z() >= cell_idx_c(0); }

private:

   cell_idx_t cell[3]; ///< Array of the cells coordinates. cell == {x, y, z}.
};



/*! \name Stream operators */
//@{
std::ostream & operator<<( std::ostream & os, const Cell & cell );
std::istream & operator>>( std::istream & is,       Cell & cell );
//@}



// inline Cell::Cell( const int _x, const int _y, const int _z ) {
//
//    x() = cell_idx_c( _x ); y() = cell_idx_c( _y ); z() = cell_idx_c( _z );
// }



inline Cell::Cell( const uint_t _x, const uint_t _y, const uint_t _z )
{
   cell[0] = cell_idx_c( _x );
   cell[1] = cell_idx_c( _y );
   cell[2] = cell_idx_c( _z );
}



/*******************************************************************************************************************//**
 * \brief   Less-than comparison operator for Cells.
 *
124
 * Compares a cell's coordinates lexicographically (first x, then eventually y and (if necessary) finally z).
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 *
 * \param [in] rhs  the cell compared to *this.
 *
 * \return  \code std::lexicographical_compare(this->cell, this->cell + 3, rhs.cell, rhs.cell + 3). \endcode
 **********************************************************************************************************************/
inline bool Cell::operator<( const Cell & rhs ) const
{
   return std::lexicographical_compare( std::reverse_iterator<const cell_idx_t*>( this->cell + 3 ), std::reverse_iterator<const cell_idx_t*>( this->cell ),
                                        std::reverse_iterator<const cell_idx_t*>( rhs.cell + 3 ),   std::reverse_iterator<const cell_idx_t*>( rhs.cell ) );
}



 /******************************************************************************************************************//**
 * \brief   Equal comparison operator for Cells.
 *
141
 * Compares a cell's coordinates for equality (first x, then eventually y and (if necessary) finally z).
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 *
 * \param [in] rhs  The cell compared to *this.
 *
 * \return  \code (this->x == rhs.x) && (this->y == rhs.y) && (this->z == rhs.z) \endcode
 **********************************************************************************************************************/
inline bool Cell::operator==( const Cell & rhs ) const
{
   return ( this->x() == rhs.x() ) && ( this->y() == rhs.y() ) && ( this->z() == rhs.z() );
}



/*******************************************************************************************************************//**
 * \brief   Operator providing read-only element access of a const Cell.
 *
 * \param [in] idx  Zero-based index of the cell's coordinate component.
 *
 * \return  The idx-th coordinate component. This is equal to this->cell[i].
 **********************************************************************************************************************/
161
inline cell_idx_t Cell::operator[]( std::size_t idx ) const
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{
   WALBERLA_ASSERT_LESS( idx, 3, "Index 'idx' = " << idx << " out of bounds! Cell: " << *this );
   return cell[idx];
}

/*******************************************************************************************************************//**
 * \brief   Operator for component-wise addition of two cells.
 *
 * \param [in] rhs  The cell added to this.
 *
 * \return  a Cell which components are the sum of this and rhs components
 **********************************************************************************************************************/
Cell Cell::operator+( const Cell & rhs ) const
{
   Cell result;
   result.cell[0] = cell[0] + rhs.cell[0];
   result.cell[1] = cell[1] + rhs.cell[1];
   result.cell[2] = cell[2] + rhs.cell[2];
   return result;
}

/*******************************************************************************************************************//**
 * \brief   Operator for component-wise subtraction of two cells.
 *
 * \param [in] rhs  The cell subtracted from this.
 *
 * \return  a Cell which components are the difference of this and rhs components
 **********************************************************************************************************************/
Cell Cell::operator-( const Cell & rhs ) const
{
   Cell result;
   result.cell[0] = cell[0] - rhs.cell[0];
   result.cell[1] = cell[1] - rhs.cell[1];
   result.cell[2] = cell[2] - rhs.cell[2];
   return result;
}

/*******************************************************************************************************************//**
 * \brief   Additive compound assignment operator.
 *
 * \param [in] rhs  The cell added to this.
 *
 * \return  Reference to this.
 **********************************************************************************************************************/
Cell & Cell::operator+=( const Cell & rhs )
{
   cell[0] += rhs.cell[0];
   cell[1] += rhs.cell[1];
   cell[2] += rhs.cell[2];

   return *this;
}

/*******************************************************************************************************************//**
 * \brief   Subtractive compound assignment operator.
 *
 * \param [in] rhs  The cell subtracted from this.
 *
 * \return  Reference to this.
 **********************************************************************************************************************/
Cell & Cell::operator-=( const Cell & rhs )
{
   cell[0] -= rhs.cell[0];
   cell[1] -= rhs.cell[1];
   cell[2] -= rhs.cell[2];

   return *this;
}

/*******************************************************************************************************************//**
 * \brief   Unary plus operator.
 *
 * \return  *this unmodified.
 **********************************************************************************************************************/
inline Cell Cell::operator+() const
{
   return *this;
}

/*******************************************************************************************************************//**
 * \brief   Unary negation operator.
 *
 * \return  Cell with negated components.
 **********************************************************************************************************************/
inline Cell Cell::operator-() const
{
   return Cell( -x(), -y(), -z() );
}



 /******************************************************************************************************************//**
 * \brief   Operator providing element access of a Cell.
 *
 * \param [in] idx  Zero-based index of the cell's coordinate component.
 *
 * \return  The idx-th coordinate component. This is equal to this->cell[i].
 **********************************************************************************************************************/
260
inline cell_idx_t & Cell::operator[]( std::size_t idx )
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
{
   WALBERLA_ASSERT_LESS( idx, 3, "Index 'idx' = " << idx << " out of bounds! Cell: " << *this );
   return cell[idx];
}



/*******************************************************************************************************************//**
 * \brief   Stream output operator for a Cell object.
 *
 * Serializes a Cell like (x,y,z). Example: (7,36,1211).
 *
 * \param [in,out] os  The output stream.
 * \param [in]   cell  The cell to be serialized.
 *
 * \return  A reference to the modified output stream.
 **********************************************************************************************************************/
inline std::ostream & operator<<( std::ostream & os, const Cell & cell )
{
   os << "(" << cell.x() << "," << cell.y() << "," << cell.z() << ")";
   return os;
}

/*******************************************************************************************************************//**
 * \brief   Stream input operator for a Cell object.
 *
 * Deserializes a Cell like (x,y,z). Example: (7,36,1211). Whitespaces between commas or brackets
 * are ignored.
 *
 * \param [in,out] is  The input stream.
 * \param [out]  cell  The result cell.
 *
 * \return  A reference to the modified input stream.
 **********************************************************************************************************************/
inline std::istream & operator>>( std::istream & is, Cell & cell )
{
   if( !is ) return is;

   char bracket1, bracket2, comma1, comma2;
   cell_idx_t x(0), y(0), z(0);
   const std::istream::pos_type pos( is.tellg() );
   const std::istream::fmtflags oldFlags( is.flags() );

   // Setting the 'skip whitespaces' flag
   is >> std::skipws;

   // Extracting the vector
   if( !(is >> bracket1 >> x >> comma1 >> y >> comma2 >> z >> bracket2) ||
       bracket1 != '(' || comma1 != ',' || comma2 != ',' || bracket2 != ')' )
   {
         is.clear();
         is.seekg( pos );
         is.setstate( std::istream::failbit );
         is.flags( oldFlags );
         return is;
   }

   // Transferring the input to the vector values
   cell.x() = x; cell.y() = y; cell.z() = z;

   // Resetting the flags
   is.flags( oldFlags );

   return is;
}


/*******************************************************************************************************************//**
 * \brief   Provides a hash value for a Cell based on its coordinates.
 *
 * \param [in]   cell  The cell to be hashed.
 *
333
334
335
 * \return  a hash that is unique for cell indices from 0 to 2 million
 *          (64bit architectures) or from 0 to 1000 (32bit architectures)
 *          in all three dimensions.
336
337
338
 **********************************************************************************************************************/
inline std::size_t hash_value( const Cell & cell )
{
339
   std::size_t seed;
340

341
   if( sizeof(std::size_t) >= 8 )
342
343
344
345
346
347
348
349
350
351
352
   {
      seed = (static_cast<std::size_t>(cell.x()) << 42) +
             (static_cast<std::size_t>(cell.y()) << 21) +
             (static_cast<std::size_t>(cell.z()) << 0);
   }
   else
   {
      seed = (static_cast<std::size_t>(cell.x()) << 21) +
             (static_cast<std::size_t>(cell.y()) << 10) +
             (static_cast<std::size_t>(cell.z()) << 0);
   }
353

354
   return seed;
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
}



//======================================================================================================================
//
//  Send/Recv Buffer Serialization Specialization
//
//======================================================================================================================

template< typename T,    // Element type of SendBuffer
          typename G >   // Growth policy of SendBuffer
mpi::GenericSendBuffer<T,G>& operator<<( mpi::GenericSendBuffer<T,G> & buf, const Cell & cell )
{
   return buf << cell.x() << cell.y() << cell.z();
}

template< typename T >   // Element type  of RecvBuffer
mpi::GenericRecvBuffer<T>& operator>>( mpi::GenericRecvBuffer<T> & buf, Cell & cell )
{
   return buf >> cell.x() >> cell.y() >> cell.z();
}


} // namespace cell

using cell::Cell;

namespace mpi
{
   template<>
   struct BufferSizeTrait< Cell> {
      static const bool constantSize = true;
      static const uint_t size = 3 * sizeof(cell_idx_t) + BUFFER_DEBUG_OVERHEAD;
   };
}


} // namespace walberla
394
395
396
397
398
399
400
401
402
403
404
405

namespace std
{
   template<>
   struct hash< walberla::Cell >
   {
      std::size_t operator()( walberla::Cell const & cell ) const noexcept
      {
         return walberla::cell::hash_value( cell );
      }
   };
} // namespace std