VTK
vtkStructuredAMRGridConnectivity.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkStructuredAMRGridConnectivity.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14  =========================================================================*/
31 #ifndef vtkStructuredAMRGridConnectivity_h
32 #define vtkStructuredAMRGridConnectivity_h
33 
34 #include "vtkFiltersGeometryModule.h" // For export macro
36 
37 #include "vtkStructuredAMRNeighbor.h" // For vtkStructuredAMRNeighbor def.
38 
39 
40 // C++ includes
41 #include <map> // For STL map
42 #include <ostream> // For STL stream
43 #include <set> // For STL set
44 #include <vector> // For STL vector
45 
46 class VTKFILTERSGEOMETRY_EXPORT vtkStructuredAMRGridConnectivity :
48 {
49 public:
52  void PrintSelf(ostream& os, vtkIndent indent ) VTK_OVERRIDE;
53 
61  void Initialize(
62  const unsigned int NumberOfLevels,
63  const unsigned int N, const int RefinementRatio=-1);
64 
68  void ComputeNeighbors() VTK_OVERRIDE;
69 
73  void CreateGhostLayers(const int N=1) VTK_OVERRIDE;
74 
80  virtual void RegisterGrid(
81  const int gridIdx, const int level, const int refinementRatio,
82  int extents[6],
83  vtkUnsignedCharArray* nodesGhostArray,
84  vtkUnsignedCharArray* cellGhostArray,
85  vtkPointData* pointData,
86  vtkCellData* cellData,
87  vtkPoints* gridNodes);
88 
94  virtual void RegisterGrid(
95  const int gridIdx, const int level, int extents[6],
96  vtkUnsignedCharArray* nodesGhostArray,
97  vtkUnsignedCharArray* cellGhostArray,
98  vtkPointData* pointData,
99  vtkCellData* cellData,
100  vtkPoints* gridNodes);
101 
103 
108  vtkSetMacro(BalancedRefinement,bool);
109  vtkGetMacro(BalancedRefinement,bool);
111 
113 
118  vtkSetMacro(NodeCentered,bool);
119  vtkGetMacro(NodeCentered,bool);
121 
123 
127  vtkSetMacro(CellCentered,bool);
128  vtkGetMacro(CellCentered,bool);
130 
135  int GetNumberOfNeighbors(const int gridID);
136 
140  void GetGhostedExtent(const int gridID, int ext[6]);
141 
145  vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei);
146 
147 protected:
149  ~vtkStructuredAMRGridConnectivity() VTK_OVERRIDE;
150 
154  void SetNumberOfGrids( const unsigned int N ) VTK_OVERRIDE;
155 
159  void CreateGhostedMaskArrays(const int gridID);
160 
164  void CreateGhostedExtent(const int gridID, const int N);
165 
169  void SetGhostedExtent(const int gridID, int ext[6]);
170 
174  void GetCoarsenedExtent(
175  const int gridIdx, int fromLevel, int toLevel, int ext[6]);
176 
180  void GetRefinedExtent(
181  const int gridIdx, int fromLevel, int toLevel, int ext[6]);
182 
186  void RefineExtent(
187  int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
188 
193  void GetCellRefinedExtent(
194  int orient[3], int ndim,
195  const int i, const int j, const int k,
196  const int fromLevel, const int toLevel,
197  int ext[6]);
198 
202  void CoarsenExtent(
203  int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
204 
208  void GetGridExtent( const int gridIdx, int ext[6] );
209 
213  int GetGridLevel( const int gridIdx );
214 
218  bool LevelExists( const int level );
219 
223  bool IsNodeInterior(
224  const int i, const int j, const int k, int ext[6]);
225 
229  bool IsNodeWithinExtent(
230  const int i, const int j, const int k, int ext[6]);
231 
235  bool IsNodeOnSharedBoundary(
236  const int i, const int j, const int k,
237  const int gridId,int gridExt[6]);
238 
242  bool IsNodeOnBoundaryOfExtent(
243  const int i, const int j, const int k, int ext[6] );
244 
248  void InsertGridAtLevel( const int level, const int gridID);
249 
254  void ComputeNeighborSendAndRcvExtent(const int gridID, const int N);
255 
260  void ComputeWholeExtent();
261 
266  void GetWholeExtentAtLevel(const int level, int ext[6]);
267 
272  void EstablishNeighbors(const int i, const int j);
273 
277  void GetNodeOrientation(
278  const int i, const int j, const int k,
279  int gridExt[6], int nodeOrientation[3]);
280 
288  void GetOrientationVector(
289  const int dataDescription, int orient[3], int &ndim);
290 
294  bool HasConstantRefinementRatio( );
295 
299  void SetRefinementRatioAtLevel( const int level, const int r);
300 
304  int GetRefinementRatioAtLevel(const int level);
305 
309  bool AreExtentsEqual(int ext1[6], int ext2[6] );
310 
314  void SetBlockTopology(const int gridID);
315 
323  int GetNumberOfConnectingBlockFaces( const int gridID );
324 
326 
340  bool HasBlockConnection(const int gridID, const int blockDirection)
341  {
342  // Sanity check
343  assert("pre: gridID is out-of-bounds" &&
344  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
345  assert("pre: BlockTopology has not been properly allocated" &&
346  (this->NumberOfGrids == this->BlockTopology.size()));
347  assert("pre: blockDirection is out-of-bounds" &&
348  (blockDirection >= 0) && (blockDirection < 6) );
349  bool status = false;
350  if( this->BlockTopology[ gridID ] & (1 << blockDirection) )
351  {
352  status = true;
353  }
354  return( status );
355  }
357 
372  void RemoveBlockConnection(const int gridID, const int blockDirection);
373 
388  void AddBlockConnection(const int gridID, const int blockDirection);
389 
394  void ClearBlockConnections( const int gridID );
395 
399  virtual void MarkNodeProperty(
400  const int gridId, const int i, const int j, const int k,
401  int gridExt[6], int wholeExt[6],
402  unsigned char &p);
403 
407  virtual void FillNodesGhostArray(
408  const int gridId, vtkUnsignedCharArray* nodesArray);
409 
413  virtual void FillCellsGhostArray(
414  const int gridId, vtkUnsignedCharArray* cellArray);
415 
419  void FillGhostArrays(
420  const int gridId,
421  vtkUnsignedCharArray* nodesArray,
422  vtkUnsignedCharArray* cellsArray ) VTK_OVERRIDE;
423 
435  vtkStructuredAMRNeighbor GetAMRNeighbor(
436  const int i, const int iLevel, int next1[6],
437  const int j, const int jLevel, int next2[6],
438  const int normalizedLevel,
439  const int levelDiff,
440  vtkStructuredNeighbor &nei);
441 
447  void ComputeAMRNeighborOverlapExtents(
448  const int iLevel, const int jLevel, const int normalizedLevel,
449  const vtkStructuredNeighbor &nei,
450  int orient[3], int ndim,
451  int gridOverlapExtent[6],
452  int neiOverlapExtent[6]);
453 
457  int Get1DOrientation(
458  const int idx, const int ExtentLo, const int ExtentHi,
459  const int OnLo, const int OnHi, const int NotOnBoundary);
460 
464  void PrintExtent(std::ostream& os, int ext[6]);
465 
469  void InitializeGhostData( const int gridID );
470 
474  void TransferRegisteredDataToGhostedData( const int gridID );
475 
479  void TransferLocalNodeCenteredNeighborData(
480  const int gridID, vtkStructuredAMRNeighbor &nei);
481 
486  void GetLocalCellCentersFromCoarserLevel(
487  const int gridID, vtkStructuredAMRNeighbor &nei);
488 
492  void GetLocalCellCentersFromFinerLevel(
493  const int gridID, vtkStructuredAMRNeighbor &nei);
494 
499  void GetLocalCellCentersAtSameLevel(
500  const int gridID, vtkStructuredAMRNeighbor &nei);
501 
505  void TransferLocalCellCenteredNeighborData(
506  const int gridID, vtkStructuredAMRNeighbor &nei);
507 
511  void TransferLocalNeighborData(
512  const int gridID, vtkStructuredAMRNeighbor &nei);
513 
517  virtual void TransferGhostDataFromNeighbors(const int gridID);
518 
523  void AverageFieldData(
524  vtkFieldData *source, vtkIdType *sourceIds, const int N,
525  vtkFieldData *target, vtkIdType targetIdx);
526 
533  void CopyFieldData(
534  vtkFieldData *source, vtkIdType sourceIdx,
535  vtkFieldData *target, vtkIdType targetIdx);
536 
537 
538  unsigned int NumberOfLevels; // The total number of levels;
539  int DataDimension; // The dimension of the data, i.e. 2 or 3
540  int DataDescription; // The data description, i.e., VTK_XY_PLANE, etc.
541  int WholeExtent[6]; // The whole extent w.r.t. to the root level, level 0.
542  int MaxLevel; // The max level of the AMR hierarchy
543  int RefinementRatio; // The refinement ratio, set in the initialization,iff,
544  // a constant refinement ratio is used. A value of -1
545  // indicates that the refinement ratio is not constant
546  // and the RefinementRatios vector is used instead.
547 
548  bool NodeCentered; // Indicates if the data is node-centered
549  bool CellCentered; // Indicates if the data is cell-centered
550 
551  bool BalancedRefinement; // If Balanced refinement is true, then adjacent
552  // grids in the hierarchy can only differ by one
553  // level.
554 
555  // AMRHierarchy stores the the set of grid Ids in [0,N] for each level
556  std::map< int, std::set<int> > AMRHierarchy;
557 
558  // For each grid, [0,N] store the grid extents,level, and list of neighbors
559  std::vector< int > GridExtents; // size of this vector is 6*N
560  std::vector< int > GhostedExtents; // size of this vector is 6*N
561  std::vector< unsigned char > BlockTopology; // size of this vector is N
562  std::vector< int > GridLevels; // size of this vector is N
563  std::vector< std::vector<vtkStructuredAMRNeighbor> > Neighbors;
564 
565  // For each grid, [0,N], store the donor level,grid and cell information, a
566  // DonorLevel of -1 indicates that the cell is not receiving any information
567  // from a donor.
568  std::vector< std::vector<int> > CellCenteredDonorLevel;
569 
570 
571  // RefinementRatios stores the refinement ratio at each level, this vector
572  // is used only when the refinement ratio varies across levels
573  std::vector< int > RefinementRatios;
574 
575 private:
577  void operator=(const vtkStructuredAMRGridConnectivity&) VTK_DELETE_FUNCTION;
578 };
579 
580 //=============================================================================
581 // INLINE METHODS
582 //=============================================================================
583 
584 //------------------------------------------------------------------------------
585 inline int
586 vtkStructuredAMRGridConnectivity::GetNumberOfNeighbors(
587  const int gridID)
588 {
589  assert("pre: grid ID is out-of-bounds" &&
590  (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
591  assert("pre: neighbors vector has not been properly allocated" &&
592  (this->Neighbors.size()==this->NumberOfGrids));
593  return( static_cast<int>(this->Neighbors[gridID].size()) );
594 }
595 
596 //------------------------------------------------------------------------------
599  const int gridID, const int nei)
600 {
601  assert("pre: grid ID is out-of-bounds" &&
602  (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
603  assert("pre: neighbors vector has not been properly allocated" &&
604  (this->Neighbors.size()==this->NumberOfGrids));
605  assert("pre: nei index is out-of-bounds" &&
606  (nei >= 0) &&
607  (nei < static_cast<int>(this->Neighbors[gridID].size())));
608  return( this->Neighbors[gridID][nei] );
609 }
610 
611 //------------------------------------------------------------------------------
612 inline int
614  const int idx, const int ExtentLo, const int ExtentHi,
615  const int OnLo, const int OnHi, const int NotOnBoundary)
616 {
617  if( idx == ExtentLo )
618  {
619  return OnLo;
620  }
621  else if( idx == ExtentHi )
622  {
623  return OnHi;
624  }
625  return NotOnBoundary;
626 }
627 
628 //------------------------------------------------------------------------------
629 inline
631  const int gridID )
632 {
633  // Sanity check
634  assert("pre: gridID is out-of-bounds" &&
635  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
636  assert("pre: BlockTopology has not been properly allocated" &&
637  (this->NumberOfGrids == this->BlockTopology.size()));
638 
639  int count = 0;
640  for( int i=0; i < 6; ++i )
641  {
642  if( this->HasBlockConnection( gridID, i ) )
643  {
644  ++count;
645  }
646  }
647  assert( "post: count must be in [0,5]" && (count >=0 && count <= 6) );
648  return( count );
649 }
650 
651 //------------------------------------------------------------------------------
653  const int gridID, const int blockDirection )
654 {
655  // Sanity check
656  assert("pre: gridID is out-of-bounds" &&
657  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
658  assert("pre: BlockTopology has not been properly allocated" &&
659  (this->NumberOfGrids == this->BlockTopology.size()));
660  assert("pre: blockDirection is out-of-bounds" &&
661  (blockDirection >= 0) && (blockDirection < 6) );
662 
663  this->BlockTopology[ gridID ] &= ~(1 << blockDirection);
664 }
665 
666 //------------------------------------------------------------------------------
668  const int gridID, const int blockDirection )
669 {
670  // Sanity check
671  assert("pre: gridID is out-of-bounds" &&
672  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
673  assert("pre: BlockTopology has not been properly allocated" &&
674  (this->NumberOfGrids == this->BlockTopology.size()));
675  assert("pre: blockDirection is out-of-bounds" &&
676  (blockDirection >= 0) && (blockDirection < 6) );
677  this->BlockTopology[ gridID ] |= (1 << blockDirection);
678 }
679 
680 //------------------------------------------------------------------------------
682  const int gridID )
683 {
684  // Sanity check
685  assert("pre: gridID is out-of-bounds" &&
686  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
687  assert("pre: BlockTopology has not been properly allocated" &&
688  (this->NumberOfGrids == this->BlockTopology.size()));
689  for( int i=0; i < 6; ++i )
690  {
691  this->RemoveBlockConnection( gridID, i );
692  } // END for all block directions
693 }
694 
695 //------------------------------------------------------------------------------
696 inline
698  int ext1[6], int ext2[6])
699 {
700  for( int i=0; i < 6; ++i )
701  {
702  if( ext1[i] != ext2[i] )
703  {
704  return false;
705  }
706  } // END for
707  return true;
708 }
709 
710 //------------------------------------------------------------------------------
711 inline
713  std::ostream& os, int ext[6])
714 {
715  for( int i=0; i < 6; i+=2 )
716  {
717  os << "[";
718  os << ext[i] << " ";
719  os << ext[i+1] << "] ";
720  } // END for
721 }
722 
723 //------------------------------------------------------------------------------
724 inline
726 {
727  assert("pre: grid Index is out-of-bounds!" &&
728  (gridIdx < static_cast<int>(this->NumberOfGrids)));
729  assert("pre: grid levels vector has not been allocated" &&
730  (this->GridLevels.size()==this->NumberOfGrids) );
731  return( this->GridLevels[gridIdx] );
732 }
733 
734 //------------------------------------------------------------------------------
735 inline
737  const int level, const int r)
738 {
739  assert("pre: RefinementRatios vector is not propertly allocated" &&
740  this->RefinementRatios.size()==this->NumberOfLevels);
741  assert("pre: leve is out-of-bounds!" &&
742  (level >= 0) &&
743  (level < static_cast<int>(this->RefinementRatios.size())) );
744  assert("pre: invalid refinement ratio" && (r >= 2) );
745 
746  this->RefinementRatios[ level ] = r;
747 }
748 
749 //------------------------------------------------------------------------------
750 inline
752  const int level)
753 {
754  assert( "pre: RefinementRatios vector is not propertly allocated" &&
755  this->RefinementRatios.size()==this->NumberOfLevels);
756  assert( "pre: leve is out-of-bounds!" &&
757  (level >= 0) &&
758  (level < static_cast<int>(this->RefinementRatios.size())));
759  assert( "pre: refinement ratio for level has not been set" &&
760  (this->RefinementRatios[ level ] >= 2) );
761 
762  return(this->RefinementRatios[level]);
763 }
764 
765 //------------------------------------------------------------------------------
766 inline
768 {
769  if( this->RefinementRatio < 2 )
770  {
771  return false;
772  }
773  return true;
774 }
775 
776 //------------------------------------------------------------------------------
778  const int gridIdx, int ext[6])
779 {
780  assert("pre: grid index is out-of-bounds" &&
781  ( (gridIdx >= 0) &&
782  (gridIdx < static_cast<int>(this->GridExtents.size()) ) ) );
783 
784  for( int i=0; i < 6; ++i )
785  {
786  ext[ i ] = this->GridExtents[ gridIdx*6+i ];
787  }
788 }
789 
790 //------------------------------------------------------------------------------
792  const int level )
793 {
794  if( this->AMRHierarchy.find(level) != this->AMRHierarchy.end() )
795  {
796  return true;
797  }
798  return false;
799 }
800 
801 //------------------------------------------------------------------------------
803  const int level, const int gridID )
804 {
805  if( this->LevelExists( level ) )
806  {
807  this->AMRHierarchy[ level ].insert( gridID );
808  }
809  else
810  {
811  std::set<int> grids;
812  grids.insert( gridID );
813  this->AMRHierarchy[ level ] = grids;
814  }
815 }
816 
817 #endif /* VTKSTRUCTUREDAMRGRIDCONNECTIVITY_H_ */
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void RemoveBlockConnection(const int gridID, const int blockDirection)
Removes a block connection along the given direction for the block corresponding to the given gridID...
int GetNumberOfConnectingBlockFaces(const int gridID)
Returns the number of faces of the block corresponding to the given grid ID that are adjacent to at l...
bool LevelExists(const int level)
Checks if the given level has been registered.
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
represent and manipulate point attribute data
Definition: vtkPointData.h:31
int GetGridLevel(const int gridIdx)
Returns the level of the grid with the corresponding grid ID.
void SetRefinementRatioAtLevel(const int level, const int r)
Sets the refinement ratio at the given level.
virtual void ComputeNeighbors()=0
Computes the grid neighboring topology for the domain.
std::vector< std::vector< vtkStructuredAMRNeighbor > > Neighbors
represent and manipulate cell attribute data
Definition: vtkCellData.h:32
int GetRefinementRatioAtLevel(const int level)
Returns the refinement ratio at the given level.
int vtkIdType
Definition: vtkType.h:345
int Get1DOrientation(const int idx, const int ExtentLo, const int ExtentHi, const int OnLo, const int OnHi, const int NotOnBoundary)
Get 1-D orientation.
std::vector< std::vector< int > > CellCenteredDonorLevel
a simple class to control print indentation
Definition: vtkIndent.h:33
void GetGridExtent(const int gridIdx, int ext[6])
Gets the grid extent for the grid with the given grid ID.
A superclass that defines the interface to be implemented by all concrete grid connectivity classes...
vtkSetMacro(IgnoreDriverBugs, bool)
When set known driver bugs are ignored during driver feature detection.
dynamic, self-adjusting array of unsigned char
An internal, light-weight class used to store neighbor information.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
void PrintExtent(std::ostream &os, int ext[6])
Prints the extent.
std::map< int, std::set< int > > AMRHierarchy
void ClearBlockConnections(const int gridID)
Clears all block connections for the block corresponding to the given grid ID.
An internal, light-weight object used to store neighbor information for AMR grids.
vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei)
Returns the AMR neighbor for the patch with the corresponding grid ID.
virtual void FillGhostArrays(const int gridId, vtkUnsignedCharArray *nodesArray, vtkUnsignedCharArray *cellsArray)=0
Fills the ghost arrays for the given grid.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
bool AreExtentsEqual(int ext1[6], int ext2[6])
Checks if the extent ext1 and ext2 are equal.
bool HasConstantRefinementRatio()
Checks if a constant refinement ratio has been specified.
void AddBlockConnection(const int gridID, const int blockDirection)
Adds a block connection along the given direction for the block corresponding to the given gridID...
represent and manipulate 3D points
Definition: vtkPoints.h:33
represent and manipulate fields of data
Definition: vtkFieldData.h:53
void InsertGridAtLevel(const int level, const int gridID)
Inserts the grid corresponding to the given ID at the prescribed level.