Block.h 13 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
//======================================================================================================================
//
//  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 Block.h
//! \ingroup blockforest
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================

#pragma once

#include "BlockID.h"
#include "BlockNeighborhoodSection.h"
#include "BlockReconstruction.h"
#include "Types.h"

#include "core/debug/Debug.h"
#include "core/math/AABB.h"
#include "core/mpi/RecvBuffer.h"
#include "core/mpi/SendBuffer.h"

#include "domain_decomposition/IBlock.h"

#include <vector>


namespace walberla {
namespace blockforest {



class BlockForest;
class PhantomBlock;
class SetupBlock;



class Block : public IBlock {

public:

   struct NeighborBlock {

      NeighborBlock( const BlockForest & forest, const BlockID & id, const uint_t process, const Set<SUID> & state = Set<SUID>::emptySet() );

      bool operator==( const NeighborBlock & rhs ) const { return ( id_ == rhs.id_ && process_ == rhs.process_ && state_ == rhs.state_ ); }
      bool operator!=( const NeighborBlock & rhs ) const { return !operator==( rhs ); }

      const BlockID &   getId()      const { return id_; }
            uint_t      getProcess() const { return process_; }
      const Set<SUID> & getState()   const { return state_; }
      const AABB &      getAABB()    const { return aabb_; }

      BlockID   id_;
      uint_t    process_;
      Set<SUID> state_;
      AABB      aabb_;
   };



   Block( BlockForest & forest, const SetupBlock * const block );
   Block( BlockForest & forest, const BlockID & id, const AABB & aabb, const Set<SUID> & state, const uint_t level,
          const BlockReconstruction::NeighborhoodReconstruction< Block > & neighborhoodReconstruction,
          const std::vector< BlockReconstruction::NeighborhoodReconstructionBlock > & neighbors );
   Block( BlockForest & forest, const PhantomBlock & phantom );
   Block( BlockForest & forest, const BlockID & id, const AABB & aabb, const uint_t level, mpi::RecvBuffer & buffer,
Michael Kuron's avatar
Michael Kuron committed
80
          const std::function< uint_t ( const uint_t ) > & processMapping = std::function< uint_t ( const uint_t ) >() );
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
260
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

   virtual ~Block() {}

   void toBuffer( mpi::SendBuffer & buffer ) const;

   const BlockForest & getForest() const { return forest_; }
         BlockForest & getForest()       { return forest_; }

   const BlockID & getId()      const { return id_; }
         uint_t    getProcess() const;
         uint_t    getLevel()   const { return level_; }

   inline       void                           clearNeighborhoodSection  ( const uint_t index );
   inline const std::vector< NeighborBlock* >& getNeighborhoodSection    ( const uint_t index ) const;
   inline       uint_t                         getNeighborhoodSectionSize( const uint_t index ) const;
   inline       void                           addNeighbor               ( const uint_t sectionIndex, const uint_t neighborIndex );
   inline const NeighborBlock &                getNeighbor               ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline const BlockID &                      getNeighborId             ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline       uint_t                         getNeighborProcess        ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline       bool                           neighborExistsLocally     ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline       bool                           neighborExistsRemotely    ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline const Set<SUID> &                    getNeighborState          ( const uint_t sectionIndex, const uint_t neighborIndex ) const;
   inline const AABB &                         getNeighborAABB           ( const uint_t sectionIndex, const uint_t neighborIndex ) const;

   inline bool neighborhoodSectionHasBlocks           ( const uint_t sectionIndex ) const;
   inline bool neighborhoodSectionHasSmallerBlocks    ( const uint_t sectionIndex ) const;
   inline bool neighborhoodSectionHasEquallySizedBlock( const uint_t sectionIndex ) const;
   inline bool neighborhoodSectionHasLargerBlock      ( const uint_t sectionIndex ) const;

                void                          clearNeighborhood     () { neighborhood_.clear(); }
          const std::vector< NeighborBlock >& getNeighborhood       () const { return neighborhood_; }
                uint_t                        getNeighborhoodSize   () const { return neighborhood_.size(); }
   inline       void                          addNeighbor           ( const BlockID& id, const uint_t process, const Set<SUID>& state );
   inline const NeighborBlock &               getNeighbor           ( const uint_t index ) const;
   inline const BlockID &                     getNeighborId         ( const uint_t index ) const;
   inline       uint_t                        getNeighborProcess    ( const uint_t index ) const;
   inline       bool                          neighborExistsLocally ( const uint_t index ) const;
                bool                          neighborExistsRemotely( const uint_t index ) const { return !neighborExistsLocally( index ); }
   inline const Set<SUID> &                   getNeighborState      ( const uint_t index ) const;
   inline const AABB &                        getNeighborAABB       ( const uint_t index ) const;

   void resetNeighborhood( const PhantomBlock & phantom );

   bool targetBlockHasTheSameSize() const { return targetLevel_ == level_; }
   bool targetBlockIsLarger() const { return targetLevel_ < level_; }
   bool targetBlockIsSmaller() const { return targetLevel_ > level_; }

   inline uint_t getTargetLevel() const { return targetLevel_; }
   inline void   setTargetLevel( const uint_t tl );
   
   const std::vector< uint_t > & getTargetProcess() const { return targetProcess_; }
   void clearTargetProcess() { targetProcess_.clear(); }
   void addTargetProcess( const uint_t process ) { targetProcess_.push_back( process ); }
   void setTargetProcess( const uint_t index, const uint_t process ) { WALBERLA_ASSERT_GREATER( targetProcess_.size(), index ); targetProcess_[index] = process; }

protected:

   bool equal( const IBlock* rhs ) const;

private:

   BlockForest & forest_;

   BlockID id_;
   uint_t  level_; // 0=coarse (= block is located on the initial grid) -> 1 -> 2 -> finer

   std::vector< NeighborBlock* >  neighborhoodSection_[26]; // the 26 neighborhood sections
   std::vector< NeighborBlock  >  neighborhood_;            // all neighbor blocks

   uint_t targetLevel_; // | level_ - targetLevel_ | <= 1
   std::vector< uint_t > targetProcess_;
};



inline void Block::clearNeighborhoodSection( const uint_t index )
{
   WALBERLA_ASSERT_LESS( index, 26 );

   neighborhoodSection_[index].clear();
}



inline const std::vector< Block::NeighborBlock* >& Block::getNeighborhoodSection( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, 26 );

   return neighborhoodSection_[index];
}



inline uint_t Block::getNeighborhoodSectionSize( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, 26 );

   return neighborhoodSection_[index].size();
}



inline void Block::addNeighbor( const uint_t sectionIndex, const uint_t neighborIndex )
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhood_.size() );
   WALBERLA_ASSERT_LESS( neighborhoodSection_[sectionIndex].size(), getBlockMaxNeighborhoodSectionSize( sectionIndex ) );

   // ATTENTION: if "neighborhood_" is changed afterwards, this pointer might get invalidated!
   neighborhoodSection_[sectionIndex].push_back( &(neighborhood_[ neighborIndex ]) );
}



inline const Block::NeighborBlock& Block::getNeighbor( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return *(neighborhoodSection_[sectionIndex][neighborIndex]);
}



inline const BlockID& Block::getNeighborId( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return neighborhoodSection_[sectionIndex][neighborIndex]->id_;
}



inline uint_t Block::getNeighborProcess( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return neighborhoodSection_[sectionIndex][neighborIndex]->process_;
}



inline bool Block::neighborExistsLocally( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return neighborhoodSection_[sectionIndex][neighborIndex]->process_ == getProcess();
}



inline bool Block::neighborExistsRemotely( const uint_t sectionIndex, const uint_t neighborIndex ) const {

   return !neighborExistsLocally( sectionIndex, neighborIndex );
}



inline const Set<SUID> & Block::getNeighborState( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return neighborhoodSection_[sectionIndex][neighborIndex]->state_;
}



inline const AABB & Block::getNeighborAABB( const uint_t sectionIndex, const uint_t neighborIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );
   WALBERLA_ASSERT_LESS( neighborIndex, neighborhoodSection_[sectionIndex].size() );

   return neighborhoodSection_[sectionIndex][neighborIndex]->aabb_;
}



inline bool Block::neighborhoodSectionHasBlocks( const uint_t sectionIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );

   return !neighborhoodSection_[sectionIndex].empty();
}

inline bool Block::neighborhoodSectionHasSmallerBlocks( const uint_t sectionIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );

   return !neighborhoodSection_[sectionIndex].empty() && neighborhoodSection_[sectionIndex][0]->id_.getUsedBits() > id_.getUsedBits();
}

inline bool Block::neighborhoodSectionHasEquallySizedBlock( const uint_t sectionIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );

   return !neighborhoodSection_[sectionIndex].empty() && neighborhoodSection_[sectionIndex][0]->id_.getUsedBits() == id_.getUsedBits();
}

inline bool Block::neighborhoodSectionHasLargerBlock( const uint_t sectionIndex ) const
{
   WALBERLA_ASSERT_LESS( sectionIndex, 26 );

   return !neighborhoodSection_[sectionIndex].empty() && neighborhoodSection_[sectionIndex][0]->id_.getUsedBits() < id_.getUsedBits();
}



inline void Block::addNeighbor( const BlockID & id, const uint_t process, const Set<SUID> & state )
{
#ifndef NDEBUG
   for( uint_t i = 0; i != neighborhood_.size(); ++i )
      WALBERLA_ASSERT( neighborhood_[i].getId() < id || id < neighborhood_[i].getId() );
#endif

   neighborhood_.push_back( NeighborBlock( forest_, id, process, state ) );
}



inline const Block::NeighborBlock& Block::getNeighbor( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index];
}



inline const BlockID& Block::getNeighborId( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index].id_;
}



inline uint_t Block::getNeighborProcess( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index].process_;
}



inline bool Block::neighborExistsLocally ( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index].process_ == getProcess();
}



inline const Set<SUID> & Block::getNeighborState( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index].state_;
}



inline const AABB & Block::getNeighborAABB( const uint_t index ) const
{
   WALBERLA_ASSERT_LESS( index, neighborhood_.size() );

   return neighborhood_[index].aabb_;
}



inline void Block::setTargetLevel( const uint_t tl )
{
   WALBERLA_ASSERT( tl == level_ || tl == (level_ - uint_t(1)) || tl == (level_ + uint_t(1)) );

   targetLevel_ = tl;
}



} // namespace blockforest

typedef blockforest::Block Block;

} // namespace walberla