VTK  9.2.6
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 
38 #include "vtkFiltersGeometryModule.h" // For export macro
39 
40 #include "vtkStructuredAMRNeighbor.h" // For vtkStructuredAMRNeighbor def.
41 
42 // C++ includes
43 #include <map> // For STL map
44 #include <ostream> // For STL stream
45 #include <set> // For STL set
46 #include <vector> // For STL vector
47 
48 class VTKFILTERSGEOMETRY_EXPORT vtkStructuredAMRGridConnectivity
50 {
51 public:
54  void PrintSelf(ostream& os, vtkIndent indent) override;
55 
63  void Initialize(
64  const unsigned int NumberOfLevels, const unsigned int N, const int RefinementRatio = -1);
65 
69  void ComputeNeighbors() override;
70 
74  void CreateGhostLayers(const int N = 1) override;
75 
81  virtual void RegisterGrid(const int gridIdx, const int level, const int refinementRatio,
82  int extents[6], vtkUnsignedCharArray* nodesGhostArray, vtkUnsignedCharArray* cellGhostArray,
83  vtkPointData* pointData, vtkCellData* cellData, vtkPoints* gridNodes);
84 
90  virtual void RegisterGrid(const int gridIdx, const int level, int extents[6],
91  vtkUnsignedCharArray* nodesGhostArray, vtkUnsignedCharArray* cellGhostArray,
92  vtkPointData* pointData, vtkCellData* cellData, vtkPoints* gridNodes);
93 
95 
100  vtkSetMacro(BalancedRefinement, bool);
101  vtkGetMacro(BalancedRefinement, bool);
103 
105 
110  vtkSetMacro(NodeCentered, bool);
111  vtkGetMacro(NodeCentered, bool);
113 
115 
119  vtkSetMacro(CellCentered, bool);
120  vtkGetMacro(CellCentered, bool);
122 
127  int GetNumberOfNeighbors(const int gridID);
128 
132  void GetGhostedExtent(const int gridID, int ext[6]);
133 
137  vtkStructuredAMRNeighbor GetNeighbor(const int gridID, const int nei);
138 
139 protected:
142 
146  void SetNumberOfGrids(const unsigned int N) override;
147 
151  void CreateGhostedMaskArrays(const int gridID);
152 
156  void CreateGhostedExtent(const int gridID, const int N);
157 
161  void SetGhostedExtent(const int gridID, int ext[6]);
162 
166  void GetCoarsenedExtent(const int gridIdx, int fromLevel, int toLevel, int ext[6]);
167 
171  void GetRefinedExtent(const int gridIdx, int fromLevel, int toLevel, int ext[6]);
172 
176  void RefineExtent(int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
177 
182  void GetCellRefinedExtent(int orient[3], int ndim, const int i, const int j, const int k,
183  const int fromLevel, const int toLevel, int ext[6]);
184 
188  void CoarsenExtent(int orient[3], int ndim, int fromLevel, int toLevel, int ext[6]);
189 
193  void GetGridExtent(const int gridIdx, int ext[6]);
194 
198  int GetGridLevel(const int gridIdx);
199 
203  bool LevelExists(const int level);
204 
208  bool IsNodeInterior(const int i, const int j, const int k, int GridExtent[6]);
209 
213  bool IsNodeWithinExtent(const int i, const int j, const int k, int GridExtent[6]);
214 
218  bool IsNodeOnSharedBoundary(
219  const int i, const int j, const int k, const int gridId, int gridExt[6]);
220 
224  bool IsNodeOnBoundaryOfExtent(const int i, const int j, const int k, int ext[6]);
225 
229  void InsertGridAtLevel(const int level, const int gridID);
230 
235  void ComputeNeighborSendAndRcvExtent(const int gridID, const int N);
236 
241  void ComputeWholeExtent();
242 
247  void GetWholeExtentAtLevel(const int level, int ext[6]);
248 
253  void EstablishNeighbors(const int i, const int j);
254 
258  void GetNodeOrientation(
259  const int i, const int j, const int k, int gridExt[6], int nodeOrientation[3]);
260 
268  void GetOrientationVector(const int dataDescription, int orient[3], int& ndim);
269 
273  bool HasConstantRefinementRatio();
274 
278  void SetRefinementRatioAtLevel(const int level, const int r);
279 
283  int GetRefinementRatioAtLevel(const int level);
284 
288  bool AreExtentsEqual(int ext1[6], int ext2[6]);
289 
293  void SetBlockTopology(const int gridID);
294 
302  int GetNumberOfConnectingBlockFaces(const int gridID);
303 
305 
319  bool HasBlockConnection(const int gridID, const int blockDirection)
320  {
321  // Sanity check
322  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
323  (gridID < static_cast<int>(this->NumberOfGrids)));
324  assert("pre: BlockTopology has not been properly allocated" &&
325  (this->NumberOfGrids == this->BlockTopology.size()));
326  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
327  bool status = false;
328  if (this->BlockTopology[gridID] & (1 << blockDirection))
329  {
330  status = true;
331  }
332  return (status);
333  }
335 
350  void RemoveBlockConnection(const int gridID, const int blockDirection);
351 
366  void AddBlockConnection(const int gridID, const int blockDirection);
367 
372  void ClearBlockConnections(const int gridID);
373 
377  virtual void MarkNodeProperty(const int gridId, const int i, const int j, const int k,
378  int gridExt[6], int wholeExt[6], unsigned char& p);
379 
383  virtual void FillNodesGhostArray(const int gridId, vtkUnsignedCharArray* nodesArray);
384 
388  virtual void FillCellsGhostArray(const int gridId, vtkUnsignedCharArray* cellsArray);
389 
393  void FillGhostArrays(
394  const int gridId, vtkUnsignedCharArray* nodesArray, vtkUnsignedCharArray* cellsArray) override;
395 
407  vtkStructuredAMRNeighbor GetAMRNeighbor(const int i, const int iLevel, int next1[6], const int j,
408  const int jLevel, int next2[6], const int normalizedLevel, const int levelDiff,
409  vtkStructuredNeighbor& nei);
410 
416  void ComputeAMRNeighborOverlapExtents(const int iLevel, const int jLevel,
417  const int normalizedLevel, const vtkStructuredNeighbor& nei, int orient[3], int ndim,
418  int gridOverlapExtent[6], int neiOverlapExtent[6]);
419 
423  int Get1DOrientation(const int idx, const int ExtentLo, const int ExtentHi, const int OnLo,
424  const int OnHi, const int NotOnBoundary);
425 
429  void PrintExtent(std::ostream& os, int ext[6]);
430 
434  void InitializeGhostData(const int gridID);
435 
439  void TransferRegisteredDataToGhostedData(const int gridID);
440 
444  void TransferLocalNodeCenteredNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
445 
450  void GetLocalCellCentersFromCoarserLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
451 
455  void GetLocalCellCentersFromFinerLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
456 
461  void GetLocalCellCentersAtSameLevel(const int gridID, vtkStructuredAMRNeighbor& nei);
462 
466  void TransferLocalCellCenteredNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
467 
471  void TransferLocalNeighborData(const int gridID, vtkStructuredAMRNeighbor& nei);
472 
476  virtual void TransferGhostDataFromNeighbors(const int gridID);
477 
482  void AverageFieldData(vtkFieldData* source, vtkIdType* sourceIds, const int N,
483  vtkFieldData* target, vtkIdType targetIdx);
484 
491  void CopyFieldData(
492  vtkFieldData* source, vtkIdType sourceIdx, vtkFieldData* target, vtkIdType targetIdx);
493 
494  unsigned int NumberOfLevels; // The total number of levels;
495  int DataDimension; // The dimension of the data, i.e. 2 or 3
496  int DataDescription; // The data description, i.e., VTK_XY_PLANE, etc.
497  int WholeExtent[6]; // The whole extent w.r.t. to the root level, level 0.
498  int MaxLevel; // The max level of the AMR hierarchy
499  int RefinementRatio; // The refinement ratio, set in the initialization,iff,
500  // a constant refinement ratio is used. A value of -1
501  // indicates that the refinement ratio is not constant
502  // and the RefinementRatios vector is used instead.
503 
504  bool NodeCentered; // Indicates if the data is node-centered
505  bool CellCentered; // Indicates if the data is cell-centered
506 
507  bool BalancedRefinement; // If Balanced refinement is true, then adjacent
508  // grids in the hierarchy can only differ by one
509  // level.
510 
511  // AMRHierarchy stores the set of grid Ids in [0,N] for each level
512  std::map<int, std::set<int>> AMRHierarchy;
513 
514  // For each grid, [0,N] store the grid extents,level, and list of neighbors
515  std::vector<int> GridExtents; // size of this vector is 6*N
516  std::vector<int> GhostedExtents; // size of this vector is 6*N
517  std::vector<unsigned char> BlockTopology; // size of this vector is N
518  std::vector<int> GridLevels; // size of this vector is N
519  std::vector<std::vector<vtkStructuredAMRNeighbor>> Neighbors;
520 
521  // For each grid, [0,N], store the donor level,grid and cell information, a
522  // DonorLevel of -1 indicates that the cell is not receiving any information
523  // from a donor.
524  std::vector<std::vector<int>> CellCenteredDonorLevel;
525 
526  // RefinementRatios stores the refinement ratio at each level, this vector
527  // is used only when the refinement ratio varies across levels
528  std::vector<int> RefinementRatios;
529 
530 private:
532  void operator=(const vtkStructuredAMRGridConnectivity&) = delete;
533 };
534 
535 //=============================================================================
536 // INLINE METHODS
537 //=============================================================================
538 
539 //------------------------------------------------------------------------------
541 {
542  assert("pre: grid ID is out-of-bounds" && (gridID >= 0) &&
543  (gridID < static_cast<int>(this->NumberOfGrids)));
544  assert("pre: neighbors vector has not been properly allocated" &&
545  (this->Neighbors.size() == this->NumberOfGrids));
546  return (static_cast<int>(this->Neighbors[gridID].size()));
547 }
548 
549 //------------------------------------------------------------------------------
551  const int gridID, const int nei)
552 {
553  assert("pre: grid ID is out-of-bounds" && (gridID >= 0) &&
554  (gridID < static_cast<int>(this->NumberOfGrids)));
555  assert("pre: neighbors vector has not been properly allocated" &&
556  (this->Neighbors.size() == this->NumberOfGrids));
557  assert("pre: nei index is out-of-bounds" && (nei >= 0) &&
558  (nei < static_cast<int>(this->Neighbors[gridID].size())));
559  return (this->Neighbors[gridID][nei]);
560 }
561 
562 //------------------------------------------------------------------------------
563 inline int vtkStructuredAMRGridConnectivity::Get1DOrientation(const int idx, const int ExtentLo,
564  const int ExtentHi, const int OnLo, const int OnHi, const int NotOnBoundary)
565 {
566  if (idx == ExtentLo)
567  {
568  return OnLo;
569  }
570  else if (idx == ExtentHi)
571  {
572  return OnHi;
573  }
574  return NotOnBoundary;
575 }
576 
577 //------------------------------------------------------------------------------
579 {
580  // Sanity check
581  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
582  (gridID < static_cast<int>(this->NumberOfGrids)));
583  assert("pre: BlockTopology has not been properly allocated" &&
584  (this->NumberOfGrids == this->BlockTopology.size()));
585 
586  int count = 0;
587  for (int i = 0; i < 6; ++i)
588  {
589  if (this->HasBlockConnection(gridID, i))
590  {
591  ++count;
592  }
593  }
594  assert("post: count must be in [0,5]" && (count >= 0 && count <= 6));
595  return (count);
596 }
597 
598 //------------------------------------------------------------------------------
600  const int gridID, const int blockDirection)
601 {
602  // Sanity check
603  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
604  (gridID < static_cast<int>(this->NumberOfGrids)));
605  assert("pre: BlockTopology has not been properly allocated" &&
606  (this->NumberOfGrids == this->BlockTopology.size()));
607  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
608 
609  this->BlockTopology[gridID] &= ~(1 << blockDirection);
610 }
611 
612 //------------------------------------------------------------------------------
614  const int gridID, const int blockDirection)
615 {
616  // Sanity check
617  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
618  (gridID < static_cast<int>(this->NumberOfGrids)));
619  assert("pre: BlockTopology has not been properly allocated" &&
620  (this->NumberOfGrids == this->BlockTopology.size()));
621  assert("pre: blockDirection is out-of-bounds" && (blockDirection >= 0) && (blockDirection < 6));
622  this->BlockTopology[gridID] |= (1 << blockDirection);
623 }
624 
625 //------------------------------------------------------------------------------
627 {
628  // Sanity check
629  assert("pre: gridID is out-of-bounds" && (gridID >= 0) &&
630  (gridID < static_cast<int>(this->NumberOfGrids)));
631  assert("pre: BlockTopology has not been properly allocated" &&
632  (this->NumberOfGrids == this->BlockTopology.size()));
633  for (int i = 0; i < 6; ++i)
634  {
635  this->RemoveBlockConnection(gridID, i);
636  } // END for all block directions
637 }
638 
639 //------------------------------------------------------------------------------
640 inline bool vtkStructuredAMRGridConnectivity::AreExtentsEqual(int ext1[6], int ext2[6])
641 {
642  for (int i = 0; i < 6; ++i)
643  {
644  if (ext1[i] != ext2[i])
645  {
646  return false;
647  }
648  } // END for
649  return true;
650 }
651 
652 //------------------------------------------------------------------------------
653 inline void vtkStructuredAMRGridConnectivity::PrintExtent(std::ostream& os, int ext[6])
654 {
655  for (int i = 0; i < 6; i += 2)
656  {
657  os << "[";
658  os << ext[i] << " ";
659  os << ext[i + 1] << "] ";
660  } // END for
661 }
662 
663 //------------------------------------------------------------------------------
665 {
666  assert("pre: grid Index is out-of-bounds!" && (gridIdx < static_cast<int>(this->NumberOfGrids)));
667  assert("pre: grid levels vector has not been allocated" &&
668  (this->GridLevels.size() == this->NumberOfGrids));
669  return (this->GridLevels[gridIdx]);
670 }
671 
672 //------------------------------------------------------------------------------
674  const int level, const int r)
675 {
676  assert("pre: RefinementRatios vector is not properly allocated" &&
677  this->RefinementRatios.size() == this->NumberOfLevels);
678  assert("pre: leve is out-of-bounds!" && (level >= 0) &&
679  (level < static_cast<int>(this->RefinementRatios.size())));
680  assert("pre: invalid refinement ratio" && (r >= 2));
681 
682  this->RefinementRatios[level] = r;
683 }
684 
685 //------------------------------------------------------------------------------
687 {
688  assert("pre: RefinementRatios vector is not properly allocated" &&
689  this->RefinementRatios.size() == this->NumberOfLevels);
690  assert("pre: leve is out-of-bounds!" && (level >= 0) &&
691  (level < static_cast<int>(this->RefinementRatios.size())));
692  assert(
693  "pre: refinement ratio for level has not been set" && (this->RefinementRatios[level] >= 2));
694 
695  return (this->RefinementRatios[level]);
696 }
697 
698 //------------------------------------------------------------------------------
700 {
701  if (this->RefinementRatio < 2)
702  {
703  return false;
704  }
705  return true;
706 }
707 
708 //------------------------------------------------------------------------------
709 inline void vtkStructuredAMRGridConnectivity::GetGridExtent(const int gridIdx, int ext[6])
710 {
711  assert("pre: grid index is out-of-bounds" &&
712  ((gridIdx >= 0) && (gridIdx < static_cast<int>(this->GridExtents.size()))));
713 
714  for (int i = 0; i < 6; ++i)
715  {
716  ext[i] = this->GridExtents[gridIdx * 6 + i];
717  }
718 }
719 
720 //------------------------------------------------------------------------------
722 {
723  if (this->AMRHierarchy.find(level) != this->AMRHierarchy.end())
724  {
725  return true;
726  }
727  return false;
728 }
729 
730 //------------------------------------------------------------------------------
731 inline void vtkStructuredAMRGridConnectivity::InsertGridAtLevel(const int level, const int gridID)
732 {
733  if (this->LevelExists(level))
734  {
735  this->AMRHierarchy[level].insert(gridID);
736  }
737  else
738  {
739  std::set<int> grids;
740  grids.insert(gridID);
741  this->AMRHierarchy[level] = grids;
742  }
743 }
744 
745 #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.
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:41
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:41
int GetRefinementRatioAtLevel(const int level)
Returns the refinement ratio at the given level.
std::vector< std::vector< int > > CellCenteredDonorLevel
int vtkIdType
Definition: vtkType.h:332
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:39
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
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.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
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:39
represent and manipulate fields of data
Definition: vtkFieldData.h:62
void InsertGridAtLevel(const int level, const int gridID)
Inserts the grid corresponding to the given ID at the prescribed level.