VTK  9.0.1
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 
35 #include "vtkFiltersGeometryModule.h" // For export macro
36 
37 #include "vtkStructuredAMRNeighbor.h" // For vtkStructuredAMRNeighbor def.
38 
39 // C++ includes
40 #include <map> // For STL map
41 #include <ostream> // For STL stream
42 #include <set> // For STL set
43 #include <vector> // For STL vector
44 
45 class VTKFILTERSGEOMETRY_EXPORT vtkStructuredAMRGridConnectivity
47 {
48 public:
51  void PrintSelf(ostream& os, vtkIndent indent) override;
52 
60  void Initialize(
61  const unsigned int NumberOfLevels, const unsigned int N, const int RefinementRatio = -1);
62 
66  void ComputeNeighbors() override;
67 
71  void CreateGhostLayers(const int N = 1) override;
72 
78  virtual void RegisterGrid(const int gridIdx, const int level, const int refinementRatio,
79  int extents[6], vtkUnsignedCharArray* nodesGhostArray, vtkUnsignedCharArray* cellGhostArray,
80  vtkPointData* pointData, vtkCellData* cellData, vtkPoints* gridNodes);
81 
87  virtual void RegisterGrid(const int gridIdx, const int level, int extents[6],
88  vtkUnsignedCharArray* nodesGhostArray, vtkUnsignedCharArray* cellGhostArray,
89  vtkPointData* pointData, vtkCellData* cellData, vtkPoints* gridNodes);
90 
92 
97  vtkSetMacro(BalancedRefinement, bool);
98  vtkGetMacro(BalancedRefinement, bool);
100 
102 
107  vtkSetMacro(NodeCentered, bool);
108  vtkGetMacro(NodeCentered, bool);
110 
112 
116  vtkSetMacro(CellCentered, bool);
117  vtkGetMacro(CellCentered, bool);
119 
124  int GetNumberOfNeighbors(const int gridID);
125 
129  void GetGhostedExtent(const int gridID, int ext[6]);
130 
134  vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei);
135 
136 protected:
139 
143  void SetNumberOfGrids(const unsigned int N) override;
144 
148  void CreateGhostedMaskArrays(const int gridID);
149 
153  void CreateGhostedExtent(const int gridID, const int N);
154 
158  void SetGhostedExtent(const int gridID, int ext[6]);
159 
163  void GetCoarsenedExtent(const int gridIdx, int fromLevel, int toLevel, int ext[6]);
164 
168  void GetRefinedExtent(const int gridIdx, int fromLevel, int toLevel, int ext[6]);
169 
173  void RefineExtent(int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
174 
179  void GetCellRefinedExtent(int orient[3], int ndim, const int i, const int j, const int k,
180  const int fromLevel, const int toLevel, int ext[6]);
181 
185  void CoarsenExtent(int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
186 
190  void GetGridExtent(const int gridIdx, int ext[6]);
191 
195  int GetGridLevel(const int gridIdx);
196 
200  bool LevelExists(const int level);
201 
205  bool IsNodeInterior(const int i, const int j, const int k, int ext[6]);
206 
210  bool IsNodeWithinExtent(const int i, const int j, const int k, int ext[6]);
211 
215  bool IsNodeOnSharedBoundary(
216  const int i, const int j, const int k, const int gridId, int gridExt[6]);
217 
221  bool IsNodeOnBoundaryOfExtent(const int i, const int j, const int k, int ext[6]);
222 
226  void InsertGridAtLevel(const int level, const int gridID);
227 
232  void ComputeNeighborSendAndRcvExtent(const int gridID, const int N);
233 
238  void ComputeWholeExtent();
239 
244  void GetWholeExtentAtLevel(const int level, int ext[6]);
245 
250  void EstablishNeighbors(const int i, const int j);
251 
255  void GetNodeOrientation(
256  const int i, const int j, const int k, int gridExt[6], int nodeOrientation[3]);
257 
265  void GetOrientationVector(const int dataDescription, int orient[3], int& ndim);
266 
270  bool HasConstantRefinementRatio();
271 
275  void SetRefinementRatioAtLevel(const int level, const int r);
276 
280  int GetRefinementRatioAtLevel(const int level);
281 
285  bool AreExtentsEqual(int ext1[6], int ext2[6]);
286 
290  void SetBlockTopology(const int gridID);
291 
299  int GetNumberOfConnectingBlockFaces(const int gridID);
300 
302 
316  bool HasBlockConnection(const int gridID, const int blockDirection)
317  {
318  // Sanity check
319  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
320  (gridID < static_cast<int>(this->NumberOfGrids)));
321  assert("pre: BlockTopology has not been properly allocated" &&
322  (this->NumberOfGrids == this->BlockTopology.size()));
323  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
324  bool status = false;
325  if (this->BlockTopology[gridID] & (1 << blockDirection))
326  {
327  status = true;
328  }
329  return (status);
330  }
332 
347  void RemoveBlockConnection(const int gridID, const int blockDirection);
348 
363  void AddBlockConnection(const int gridID, const int blockDirection);
364 
369  void ClearBlockConnections(const int gridID);
370 
374  virtual void MarkNodeProperty(const int gridId, const int i, const int j, const int k,
375  int gridExt[6], int wholeExt[6], unsigned char& p);
376 
380  virtual void FillNodesGhostArray(const int gridId, vtkUnsignedCharArray* nodesArray);
381 
385  virtual void FillCellsGhostArray(const int gridId, vtkUnsignedCharArray* cellArray);
386 
390  void FillGhostArrays(
391  const int gridId, vtkUnsignedCharArray* nodesArray, vtkUnsignedCharArray* cellsArray) override;
392 
404  vtkStructuredAMRNeighbor GetAMRNeighbor(const int i, const int iLevel, int next1[6], const int j,
405  const int jLevel, int next2[6], const int normalizedLevel, const int levelDiff,
406  vtkStructuredNeighbor& nei);
407 
413  void ComputeAMRNeighborOverlapExtents(const int iLevel, const int jLevel,
414  const int normalizedLevel, const vtkStructuredNeighbor& nei, int orient[3], int ndim,
415  int gridOverlapExtent[6], int neiOverlapExtent[6]);
416 
420  int Get1DOrientation(const int idx, const int ExtentLo, const int ExtentHi, const int OnLo,
421  const int OnHi, const int NotOnBoundary);
422 
426  void PrintExtent(std::ostream& os, int ext[6]);
427 
431  void InitializeGhostData(const int gridID);
432 
436  void TransferRegisteredDataToGhostedData(const int gridID);
437 
441  void TransferLocalNodeCenteredNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
442 
447  void GetLocalCellCentersFromCoarserLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
448 
452  void GetLocalCellCentersFromFinerLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
453 
458  void GetLocalCellCentersAtSameLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
459 
463  void TransferLocalCellCenteredNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
464 
468  void TransferLocalNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
469 
473  virtual void TransferGhostDataFromNeighbors(const int gridID);
474 
479  void AverageFieldData(vtkFieldData* source, vtkIdType* sourceIds, const int N,
480  vtkFieldData* target, vtkIdType targetIdx);
481 
488  void CopyFieldData(
489  vtkFieldData* source, vtkIdType sourceIdx, vtkFieldData* target, vtkIdType targetIdx);
490 
491  unsigned int NumberOfLevels; // The total number of levels;
492  int DataDimension; // The dimension of the data, i.e. 2 or 3
493  int DataDescription; // The data description, i.e., VTK_XY_PLANE, etc.
494  int WholeExtent[6]; // The whole extent w.r.t. to the root level, level 0.
495  int MaxLevel; // The max level of the AMR hierarchy
496  int RefinementRatio; // The refinement ratio, set in the initialization,iff,
497  // a constant refinement ratio is used. A value of -1
498  // indicates that the refinement ratio is not constant
499  // and the RefinementRatios vector is used instead.
500 
501  bool NodeCentered; // Indicates if the data is node-centered
502  bool CellCentered; // Indicates if the data is cell-centered
503 
504  bool BalancedRefinement; // If Balanced refinement is true, then adjacent
505  // grids in the hierarchy can only differ by one
506  // level.
507 
508  // AMRHierarchy stores the set of grid Ids in [0,N] for each level
509  std::map<int, std::set<int> > AMRHierarchy;
510 
511  // For each grid, [0,N] store the grid extents,level, and list of neighbors
512  std::vector<int> GridExtents; // size of this vector is 6*N
513  std::vector<int> GhostedExtents; // size of this vector is 6*N
514  std::vector<unsigned char> BlockTopology; // size of this vector is N
515  std::vector<int> GridLevels; // size of this vector is N
516  std::vector<std::vector<vtkStructuredAMRNeighbor> > Neighbors;
517 
518  // For each grid, [0,N], store the donor level,grid and cell information, a
519  // DonorLevel of -1 indicates that the cell is not receiving any information
520  // from a donor.
521  std::vector<std::vector<int> > CellCenteredDonorLevel;
522 
523  // RefinementRatios stores the refinement ratio at each level, this vector
524  // is used only when the refinement ratio varies across levels
525  std::vector<int> RefinementRatios;
526 
527 private:
529  void operator=(const vtkStructuredAMRGridConnectivity&) = delete;
530 };
531 
532 //=============================================================================
533 // INLINE METHODS
534 //=============================================================================
535 
536 //------------------------------------------------------------------------------
538 {
539  assert("pre: grid ID is out-of-bounds" && (gridID >= 0) &&
540  (gridID < static_cast<int>(this->NumberOfGrids)));
541  assert("pre: neighbors vector has not been properly allocated" &&
542  (this->Neighbors.size() == this->NumberOfGrids));
543  return (static_cast<int>(this->Neighbors[gridID].size()));
544 }
545 
546 //------------------------------------------------------------------------------
548  const int gridID, const int nei)
549 {
550  assert("pre: grid ID is out-of-bounds" && (gridID >= 0) &&
551  (gridID < static_cast<int>(this->NumberOfGrids)));
552  assert("pre: neighbors vector has not been properly allocated" &&
553  (this->Neighbors.size() == this->NumberOfGrids));
554  assert("pre: nei index is out-of-bounds" && (nei >= 0) &&
555  (nei < static_cast<int>(this->Neighbors[gridID].size())));
556  return (this->Neighbors[gridID][nei]);
557 }
558 
559 //------------------------------------------------------------------------------
560 inline int vtkStructuredAMRGridConnectivity::Get1DOrientation(const int idx, const int ExtentLo,
561  const int ExtentHi, const int OnLo, const int OnHi, const int NotOnBoundary)
562 {
563  if (idx == ExtentLo)
564  {
565  return OnLo;
566  }
567  else if (idx == ExtentHi)
568  {
569  return OnHi;
570  }
571  return NotOnBoundary;
572 }
573 
574 //------------------------------------------------------------------------------
576 {
577  // Sanity check
578  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
579  (gridID < static_cast<int>(this->NumberOfGrids)));
580  assert("pre: BlockTopology has not been properly allocated" &&
581  (this->NumberOfGrids == this->BlockTopology.size()));
582 
583  int count = 0;
584  for (int i = 0; i < 6; ++i)
585  {
586  if (this->HasBlockConnection(gridID, i))
587  {
588  ++count;
589  }
590  }
591  assert("post: count must be in [0,5]" && (count >= 0 && count <= 6));
592  return (count);
593 }
594 
595 //------------------------------------------------------------------------------
597  const int gridID, const int blockDirection)
598 {
599  // Sanity check
600  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
601  (gridID < static_cast<int>(this->NumberOfGrids)));
602  assert("pre: BlockTopology has not been properly allocated" &&
603  (this->NumberOfGrids == this->BlockTopology.size()));
604  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
605 
606  this->BlockTopology[gridID] &= ~(1 << blockDirection);
607 }
608 
609 //------------------------------------------------------------------------------
611  const int gridID, const int blockDirection)
612 {
613  // Sanity check
614  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
615  (gridID < static_cast<int>(this->NumberOfGrids)));
616  assert("pre: BlockTopology has not been properly allocated" &&
617  (this->NumberOfGrids == this->BlockTopology.size()));
618  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
619  this->BlockTopology[gridID] |= (1 << blockDirection);
620 }
621 
622 //------------------------------------------------------------------------------
624 {
625  // Sanity check
626  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
627  (gridID < static_cast<int>(this->NumberOfGrids)));
628  assert("pre: BlockTopology has not been properly allocated" &&
629  (this->NumberOfGrids == this->BlockTopology.size()));
630  for (int i = 0; i < 6; ++i)
631  {
632  this->RemoveBlockConnection(gridID, i);
633  } // END for all block directions
634 }
635 
636 //------------------------------------------------------------------------------
637 inline bool vtkStructuredAMRGridConnectivity::AreExtentsEqual(int ext1[6], int ext2[6])
638 {
639  for (int i = 0; i < 6; ++i)
640  {
641  if (ext1[i] != ext2[i])
642  {
643  return false;
644  }
645  } // END for
646  return true;
647 }
648 
649 //------------------------------------------------------------------------------
650 inline void vtkStructuredAMRGridConnectivity::PrintExtent(std::ostream& os, int ext[6])
651 {
652  for (int i = 0; i < 6; i += 2)
653  {
654  os << "[";
655  os << ext[i] << " ";
656  os << ext[i + 1] << "] ";
657  } // END for
658 }
659 
660 //------------------------------------------------------------------------------
662 {
663  assert("pre: grid Index is out-of-bounds!" && (gridIdx < static_cast<int>(this->NumberOfGrids)));
664  assert("pre: grid levels vector has not been allocated" &&
665  (this->GridLevels.size() == this->NumberOfGrids));
666  return (this->GridLevels[gridIdx]);
667 }
668 
669 //------------------------------------------------------------------------------
671  const int level, const int r)
672 {
673  assert("pre: RefinementRatios vector is not properly allocated" &&
674  this->RefinementRatios.size() == this->NumberOfLevels);
675  assert("pre: leve is out-of-bounds!" && (level >= 0) &&
676  (level < static_cast<int>(this->RefinementRatios.size())));
677  assert("pre: invalid refinement ratio" && (r >= 2));
678 
679  this->RefinementRatios[level] = r;
680 }
681 
682 //------------------------------------------------------------------------------
684 {
685  assert("pre: RefinementRatios vector is not properly allocated" &&
686  this->RefinementRatios.size() == this->NumberOfLevels);
687  assert("pre: leve is out-of-bounds!" && (level >= 0) &&
688  (level < static_cast<int>(this->RefinementRatios.size())));
689  assert(
690  "pre: refinement ratio for level has not been set" && (this->RefinementRatios[level] >= 2));
691 
692  return (this->RefinementRatios[level]);
693 }
694 
695 //------------------------------------------------------------------------------
697 {
698  if (this->RefinementRatio < 2)
699  {
700  return false;
701  }
702  return true;
703 }
704 
705 //------------------------------------------------------------------------------
706 inline void vtkStructuredAMRGridConnectivity::GetGridExtent(const int gridIdx, int ext[6])
707 {
708  assert("pre: grid index is out-of-bounds" &&
709  ((gridIdx >= 0) && (gridIdx < static_cast<int>(this->GridExtents.size()))));
710 
711  for (int i = 0; i < 6; ++i)
712  {
713  ext[i] = this->GridExtents[gridIdx * 6 + i];
714  }
715 }
716 
717 //------------------------------------------------------------------------------
719 {
720  if (this->AMRHierarchy.find(level) != this->AMRHierarchy.end())
721  {
722  return true;
723  }
724  return false;
725 }
726 
727 //------------------------------------------------------------------------------
728 inline void vtkStructuredAMRGridConnectivity::InsertGridAtLevel(const int level, const int gridID)
729 {
730  if (this->LevelExists(level))
731  {
732  this->AMRHierarchy[level].insert(gridID);
733  }
734  else
735  {
736  std::set<int> grids;
737  grids.insert(gridID);
738  this->AMRHierarchy[level] = grids;
739  }
740 }
741 
742 #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...
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
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.
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:31
int GetGridLevel(const int gridIdx)
Returns the level of the grid with the corresponding grid ID.
std::map< int, std::set< int > > AMRHierarchy
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.
represent and manipulate cell attribute data
Definition: vtkCellData.h:32
int GetRefinementRatioAtLevel(const int level)
Returns the refinement ratio at the given level.
std::vector< std::vector< int > > CellCenteredDonorLevel
int vtkIdType
Definition: vtkType.h:338
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph *>::edge_descriptor e, vtkGraph *)
int Get1DOrientation(const int idx, const int ExtentLo, const int ExtentHi, const int OnLo, const int OnHi, const int NotOnBoundary)
Get 1-D orientation.
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...
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.
void PrintExtent(std::ostream &os, int ext[6])
Prints the extent.
void ClearBlockConnections(const int gridID)
Clears all block connections for the block corresponding to the given grid ID.
std::vector< std::vector< vtkStructuredAMRNeighbor > > Neighbors
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: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.