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  =========================================================================*/
34 #ifndef vtkStructuredAMRGridConnectivity_h
35 #define vtkStructuredAMRGridConnectivity_h
36 
37 #include "vtkFiltersGeometryModule.h" // For export macro
39 
40 #include "vtkStructuredAMRNeighbor.h" // For vtkStructuredAMRNeighbor def.
41 
42 
43 // C++ includes
44 #include <map> // For STL map
45 #include <ostream> // For STL stream
46 #include <set> // For STL set
47 #include <vector> // For STL vector
48 
49 class VTKFILTERSGEOMETRY_EXPORT vtkStructuredAMRGridConnectivity :
51 {
52 public:
55  void PrintSelf(ostream& os, vtkIndent indent ) override;
56 
64  void Initialize(
65  const unsigned int NumberOfLevels,
66  const unsigned int N, const int RefinementRatio=-1);
67 
71  void ComputeNeighbors() override;
72 
76  void CreateGhostLayers(const int N=1) override;
77 
83  virtual void RegisterGrid(
84  const int gridIdx, const int level, const int refinementRatio,
85  int extents[6],
86  vtkUnsignedCharArray* nodesGhostArray,
87  vtkUnsignedCharArray* cellGhostArray,
88  vtkPointData* pointData,
89  vtkCellData* cellData,
90  vtkPoints* gridNodes);
91 
97  virtual void RegisterGrid(
98  const int gridIdx, const int level, int extents[6],
99  vtkUnsignedCharArray* nodesGhostArray,
100  vtkUnsignedCharArray* cellGhostArray,
101  vtkPointData* pointData,
102  vtkCellData* cellData,
103  vtkPoints* gridNodes);
104 
106 
111  vtkSetMacro(BalancedRefinement,bool);
112  vtkGetMacro(BalancedRefinement,bool);
114 
116 
121  vtkSetMacro(NodeCentered,bool);
122  vtkGetMacro(NodeCentered,bool);
124 
126 
130  vtkSetMacro(CellCentered,bool);
131  vtkGetMacro(CellCentered,bool);
133 
138  int GetNumberOfNeighbors(const int gridID);
139 
143  void GetGhostedExtent(const int gridID, int ext[6]);
144 
148  vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei);
149 
150 protected:
153 
157  void SetNumberOfGrids( const unsigned int N ) override;
158 
162  void CreateGhostedMaskArrays(const int gridID);
163 
167  void CreateGhostedExtent(const int gridID, const int N);
168 
172  void SetGhostedExtent(const int gridID, int ext[6]);
173 
177  void GetCoarsenedExtent(
178  const int gridIdx, int fromLevel, int toLevel, int ext[6]);
179 
183  void GetRefinedExtent(
184  const int gridIdx, int fromLevel, int toLevel, int ext[6]);
185 
189  void RefineExtent(
190  int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
191 
196  void GetCellRefinedExtent(
197  int orient[3], int ndim,
198  const int i, const int j, const int k,
199  const int fromLevel, const int toLevel,
200  int ext[6]);
201 
205  void CoarsenExtent(
206  int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
207 
211  void GetGridExtent( const int gridIdx, int ext[6] );
212 
216  int GetGridLevel( const int gridIdx );
217 
221  bool LevelExists( const int level );
222 
226  bool IsNodeInterior(
227  const int i, const int j, const int k, int ext[6]);
228 
232  bool IsNodeWithinExtent(
233  const int i, const int j, const int k, int ext[6]);
234 
238  bool IsNodeOnSharedBoundary(
239  const int i, const int j, const int k,
240  const int gridId,int gridExt[6]);
241 
245  bool IsNodeOnBoundaryOfExtent(
246  const int i, const int j, const int k, int ext[6] );
247 
251  void InsertGridAtLevel( const int level, const int gridID);
252 
257  void ComputeNeighborSendAndRcvExtent(const int gridID, const int N);
258 
263  void ComputeWholeExtent();
264 
269  void GetWholeExtentAtLevel(const int level, int ext[6]);
270 
275  void EstablishNeighbors(const int i, const int j);
276 
280  void GetNodeOrientation(
281  const int i, const int j, const int k,
282  int gridExt[6], int nodeOrientation[3]);
283 
291  void GetOrientationVector(
292  const int dataDescription, int orient[3], int &ndim);
293 
297  bool HasConstantRefinementRatio( );
298 
302  void SetRefinementRatioAtLevel( const int level, const int r);
303 
307  int GetRefinementRatioAtLevel(const int level);
308 
312  bool AreExtentsEqual(int ext1[6], int ext2[6] );
313 
317  void SetBlockTopology(const int gridID);
318 
326  int GetNumberOfConnectingBlockFaces( const int gridID );
327 
329 
343  bool HasBlockConnection(const int gridID, const int blockDirection)
344  {
345  // Sanity check
346  assert("pre: gridID is out-of-bounds" &&
347  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
348  assert("pre: BlockTopology has not been properly allocated" &&
349  (this->NumberOfGrids == this->BlockTopology.size()));
350  assert("pre: blockDirection is out-of-bounds" &&
351  (blockDirection >= 0) && (blockDirection < 6) );
352  bool status = false;
353  if( this->BlockTopology[ gridID ] & (1 << blockDirection) )
354  {
355  status = true;
356  }
357  return( status );
358  }
360 
375  void RemoveBlockConnection(const int gridID, const int blockDirection);
376 
391  void AddBlockConnection(const int gridID, const int blockDirection);
392 
397  void ClearBlockConnections( const int gridID );
398 
402  virtual void MarkNodeProperty(
403  const int gridId, const int i, const int j, const int k,
404  int gridExt[6], int wholeExt[6],
405  unsigned char &p);
406 
410  virtual void FillNodesGhostArray(
411  const int gridId, vtkUnsignedCharArray* nodesArray);
412 
416  virtual void FillCellsGhostArray(
417  const int gridId, vtkUnsignedCharArray* cellArray);
418 
422  void FillGhostArrays(
423  const int gridId,
424  vtkUnsignedCharArray* nodesArray,
425  vtkUnsignedCharArray* cellsArray ) override;
426 
438  vtkStructuredAMRNeighbor GetAMRNeighbor(
439  const int i, const int iLevel, int next1[6],
440  const int j, const int jLevel, int next2[6],
441  const int normalizedLevel,
442  const int levelDiff,
443  vtkStructuredNeighbor &nei);
444 
450  void ComputeAMRNeighborOverlapExtents(
451  const int iLevel, const int jLevel, const int normalizedLevel,
452  const vtkStructuredNeighbor &nei,
453  int orient[3], int ndim,
454  int gridOverlapExtent[6],
455  int neiOverlapExtent[6]);
456 
460  int Get1DOrientation(
461  const int idx, const int ExtentLo, const int ExtentHi,
462  const int OnLo, const int OnHi, const int NotOnBoundary);
463 
467  void PrintExtent(std::ostream& os, int ext[6]);
468 
472  void InitializeGhostData( const int gridID );
473 
477  void TransferRegisteredDataToGhostedData( const int gridID );
478 
482  void TransferLocalNodeCenteredNeighborData(
483  const int gridID, vtkStructuredAMRNeighbor &nei);
484 
489  void GetLocalCellCentersFromCoarserLevel(
490  const int gridID, vtkStructuredAMRNeighbor &nei);
491 
495  void GetLocalCellCentersFromFinerLevel(
496  const int gridID, vtkStructuredAMRNeighbor &nei);
497 
502  void GetLocalCellCentersAtSameLevel(
503  const int gridID, vtkStructuredAMRNeighbor &nei);
504 
508  void TransferLocalCellCenteredNeighborData(
509  const int gridID, vtkStructuredAMRNeighbor &nei);
510 
514  void TransferLocalNeighborData(
515  const int gridID, vtkStructuredAMRNeighbor &nei);
516 
520  virtual void TransferGhostDataFromNeighbors(const int gridID);
521 
526  void AverageFieldData(
527  vtkFieldData *source, vtkIdType *sourceIds, const int N,
528  vtkFieldData *target, vtkIdType targetIdx);
529 
536  void CopyFieldData(
537  vtkFieldData *source, vtkIdType sourceIdx,
538  vtkFieldData *target, vtkIdType targetIdx);
539 
540 
541  unsigned int NumberOfLevels; // The total number of levels;
542  int DataDimension; // The dimension of the data, i.e. 2 or 3
543  int DataDescription; // The data description, i.e., VTK_XY_PLANE, etc.
544  int WholeExtent[6]; // The whole extent w.r.t. to the root level, level 0.
545  int MaxLevel; // The max level of the AMR hierarchy
546  int RefinementRatio; // The refinement ratio, set in the initialization,iff,
547  // a constant refinement ratio is used. A value of -1
548  // indicates that the refinement ratio is not constant
549  // and the RefinementRatios vector is used instead.
550 
551  bool NodeCentered; // Indicates if the data is node-centered
552  bool CellCentered; // Indicates if the data is cell-centered
553 
554  bool BalancedRefinement; // If Balanced refinement is true, then adjacent
555  // grids in the hierarchy can only differ by one
556  // level.
557 
558  // AMRHierarchy stores the set of grid Ids in [0,N] for each level
559  std::map< int, std::set<int> > AMRHierarchy;
560 
561  // For each grid, [0,N] store the grid extents,level, and list of neighbors
562  std::vector< int > GridExtents; // size of this vector is 6*N
563  std::vector< int > GhostedExtents; // size of this vector is 6*N
564  std::vector< unsigned char > BlockTopology; // size of this vector is N
565  std::vector< int > GridLevels; // size of this vector is N
566  std::vector< std::vector<vtkStructuredAMRNeighbor> > Neighbors;
567 
568  // For each grid, [0,N], store the donor level,grid and cell information, a
569  // DonorLevel of -1 indicates that the cell is not receiving any information
570  // from a donor.
571  std::vector< std::vector<int> > CellCenteredDonorLevel;
572 
573 
574  // RefinementRatios stores the refinement ratio at each level, this vector
575  // is used only when the refinement ratio varies across levels
576  std::vector< int > RefinementRatios;
577 
578 private:
580  void operator=(const vtkStructuredAMRGridConnectivity&) = delete;
581 };
582 
583 //=============================================================================
584 // INLINE METHODS
585 //=============================================================================
586 
587 //------------------------------------------------------------------------------
588 inline int
590  const int gridID)
591 {
592  assert("pre: grid ID is out-of-bounds" &&
593  (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
594  assert("pre: neighbors vector has not been properly allocated" &&
595  (this->Neighbors.size()==this->NumberOfGrids));
596  return( static_cast<int>(this->Neighbors[gridID].size()) );
597 }
598 
599 //------------------------------------------------------------------------------
602  const int gridID, const int nei)
603 {
604  assert("pre: grid ID is out-of-bounds" &&
605  (gridID >= 0) && (gridID < static_cast<int>(this->NumberOfGrids)));
606  assert("pre: neighbors vector has not been properly allocated" &&
607  (this->Neighbors.size()==this->NumberOfGrids));
608  assert("pre: nei index is out-of-bounds" &&
609  (nei >= 0) &&
610  (nei < static_cast<int>(this->Neighbors[gridID].size())));
611  return( this->Neighbors[gridID][nei] );
612 }
613 
614 //------------------------------------------------------------------------------
615 inline int
617  const int idx, const int ExtentLo, const int ExtentHi,
618  const int OnLo, const int OnHi, const int NotOnBoundary)
619 {
620  if( idx == ExtentLo )
621  {
622  return OnLo;
623  }
624  else if( idx == ExtentHi )
625  {
626  return OnHi;
627  }
628  return NotOnBoundary;
629 }
630 
631 //------------------------------------------------------------------------------
632 inline
634  const int gridID )
635 {
636  // Sanity check
637  assert("pre: gridID is out-of-bounds" &&
638  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
639  assert("pre: BlockTopology has not been properly allocated" &&
640  (this->NumberOfGrids == this->BlockTopology.size()));
641 
642  int count = 0;
643  for( int i=0; i < 6; ++i )
644  {
645  if( this->HasBlockConnection( gridID, i ) )
646  {
647  ++count;
648  }
649  }
650  assert( "post: count must be in [0,5]" && (count >=0 && count <= 6) );
651  return( count );
652 }
653 
654 //------------------------------------------------------------------------------
656  const int gridID, const int blockDirection )
657 {
658  // Sanity check
659  assert("pre: gridID is out-of-bounds" &&
660  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
661  assert("pre: BlockTopology has not been properly allocated" &&
662  (this->NumberOfGrids == this->BlockTopology.size()));
663  assert("pre: blockDirection is out-of-bounds" &&
664  (blockDirection >= 0) && (blockDirection < 6) );
665 
666  this->BlockTopology[ gridID ] &= ~(1 << blockDirection);
667 }
668 
669 //------------------------------------------------------------------------------
671  const int gridID, const int blockDirection )
672 {
673  // Sanity check
674  assert("pre: gridID is out-of-bounds" &&
675  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
676  assert("pre: BlockTopology has not been properly allocated" &&
677  (this->NumberOfGrids == this->BlockTopology.size()));
678  assert("pre: blockDirection is out-of-bounds" &&
679  (blockDirection >= 0) && (blockDirection < 6) );
680  this->BlockTopology[ gridID ] |= (1 << blockDirection);
681 }
682 
683 //------------------------------------------------------------------------------
685  const int gridID )
686 {
687  // Sanity check
688  assert("pre: gridID is out-of-bounds" &&
689  (gridID >=0) && (gridID < static_cast<int>(this->NumberOfGrids)));
690  assert("pre: BlockTopology has not been properly allocated" &&
691  (this->NumberOfGrids == this->BlockTopology.size()));
692  for( int i=0; i < 6; ++i )
693  {
694  this->RemoveBlockConnection( gridID, i );
695  } // END for all block directions
696 }
697 
698 //------------------------------------------------------------------------------
699 inline
701  int ext1[6], int ext2[6])
702 {
703  for( int i=0; i < 6; ++i )
704  {
705  if( ext1[i] != ext2[i] )
706  {
707  return false;
708  }
709  } // END for
710  return true;
711 }
712 
713 //------------------------------------------------------------------------------
714 inline
716  std::ostream& os, int ext[6])
717 {
718  for( int i=0; i < 6; i+=2 )
719  {
720  os << "[";
721  os << ext[i] << " ";
722  os << ext[i+1] << "] ";
723  } // END for
724 }
725 
726 //------------------------------------------------------------------------------
727 inline
729 {
730  assert("pre: grid Index is out-of-bounds!" &&
731  (gridIdx < static_cast<int>(this->NumberOfGrids)));
732  assert("pre: grid levels vector has not been allocated" &&
733  (this->GridLevels.size()==this->NumberOfGrids) );
734  return( this->GridLevels[gridIdx] );
735 }
736 
737 //------------------------------------------------------------------------------
738 inline
740  const int level, const int r)
741 {
742  assert("pre: RefinementRatios vector is not propertly allocated" &&
743  this->RefinementRatios.size()==this->NumberOfLevels);
744  assert("pre: leve is out-of-bounds!" &&
745  (level >= 0) &&
746  (level < static_cast<int>(this->RefinementRatios.size())) );
747  assert("pre: invalid refinement ratio" && (r >= 2) );
748 
749  this->RefinementRatios[ level ] = r;
750 }
751 
752 //------------------------------------------------------------------------------
753 inline
755  const int level)
756 {
757  assert( "pre: RefinementRatios vector is not propertly allocated" &&
758  this->RefinementRatios.size()==this->NumberOfLevels);
759  assert( "pre: leve is out-of-bounds!" &&
760  (level >= 0) &&
761  (level < static_cast<int>(this->RefinementRatios.size())));
762  assert( "pre: refinement ratio for level has not been set" &&
763  (this->RefinementRatios[ level ] >= 2) );
764 
765  return(this->RefinementRatios[level]);
766 }
767 
768 //------------------------------------------------------------------------------
769 inline
771 {
772  if( this->RefinementRatio < 2 )
773  {
774  return false;
775  }
776  return true;
777 }
778 
779 //------------------------------------------------------------------------------
781  const int gridIdx, int ext[6])
782 {
783  assert("pre: grid index is out-of-bounds" &&
784  ( (gridIdx >= 0) &&
785  (gridIdx < static_cast<int>(this->GridExtents.size()) ) ) );
786 
787  for( int i=0; i < 6; ++i )
788  {
789  ext[ i ] = this->GridExtents[ gridIdx*6+i ];
790  }
791 }
792 
793 //------------------------------------------------------------------------------
795  const int level )
796 {
797  if( this->AMRHierarchy.find(level) != this->AMRHierarchy.end() )
798  {
799  return true;
800  }
801  return false;
802 }
803 
804 //------------------------------------------------------------------------------
806  const int level, const int gridID )
807 {
808  if( this->LevelExists( level ) )
809  {
810  this->AMRHierarchy[ level ].insert( gridID );
811  }
812  else
813  {
814  std::set<int> grids;
815  grids.insert( gridID );
816  this->AMRHierarchy[ level ] = grids;
817  }
818 }
819 
820 #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 *)
bool HasBlockConnection(const int gridID, const int blockDirection)
Checks if the block corresponding to the given grid ID has a block adjacent to it in the given block ...
represent and manipulate point attribute data
Definition: vtkPointData.h:37
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:38
int GetRefinementRatioAtLevel(const int level)
Returns the refinement ratio at the given level.
int vtkIdType
Definition: vtkType.h:347
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:39
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...
virtual void SetNumberOfGrids(const unsigned int N)=0
Sets the total number of grids in the domain.
dynamic, self-adjusting array of unsigned char
An internal, light-weight class used to store neighbor information.
int GetNumberOfNeighbors(const int gridID)
Returns the number of neighbors for the grid corresponding to the given grid ID.
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.
virtual void CreateGhostLayers(const int N=1)=0
Creates N layers of ghost layers where N is the number of cells that will be added to each grid...
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:39
represent and manipulate fields of data
Definition: vtkFieldData.h:56
void InsertGridAtLevel(const int level, const int gridID)
Inserts the grid corresponding to the given ID at the prescribed level.