| [de061d] | 1 | /*
 | 
|---|
 | 2 |  *    vmg - a versatile multigrid solver
 | 
|---|
 | 3 |  *    Copyright (C) 2012 Institute for Numerical Simulation, University of Bonn
 | 
|---|
 | 4 |  *
 | 
|---|
 | 5 |  *  vmg is free software: you can redistribute it and/or modify
 | 
|---|
 | 6 |  *  it under the terms of the GNU General Public License as published by
 | 
|---|
 | 7 |  *  the Free Software Foundation, either version 3 of the License, or
 | 
|---|
 | 8 |  *  (at your option) any later version.
 | 
|---|
 | 9 |  *
 | 
|---|
 | 10 |  *  vmg is distributed in the hope that it will be useful,
 | 
|---|
 | 11 |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 12 |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 13 |  *  GNU General Public License for more details.
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  *  You should have received a copy of the GNU General Public License
 | 
|---|
 | 16 |  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
 | 17 |  */
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | /**
 | 
|---|
 | 20 |  * @file   is_grid.hpp
 | 
|---|
 | 21 |  * @author Julian Iseringhausen <isering@ins.uni-bonn.de>
 | 
|---|
 | 22 |  * @date   Mon Apr 18 12:53:45 2011
 | 
|---|
 | 23 |  *
 | 
|---|
 | 24 |  * @brief  Grid-like class that holds arbitrary data.
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  */
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #ifndef IS_GRID_HPP_
 | 
|---|
 | 29 | #define IS_GRID_HPP_
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #include "base/object.hpp"
 | 
|---|
 | 32 | #include "grid/grid.hpp"
 | 
|---|
 | 33 | #include "grid/grid_iterator.hpp"
 | 
|---|
 | 34 | #include "grid/grid_iterator_suite.hpp"
 | 
|---|
 | 35 | #include "grid/grid_properties.hpp"
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | namespace VMG
 | 
|---|
 | 38 | {
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | class Comm;
 | 
|---|
 | 41 | class Grid;
 | 
|---|
 | 42 | class Multigrid;
 | 
|---|
 | 43 | class Stencil;
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | template <class T>
 | 
|---|
 | 46 | class IsGrid
 | 
|---|
 | 47 | {
 | 
|---|
 | 48 | public:
 | 
|---|
 | 49 |   typedef GridIterator iterator;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 |   IsGrid(int level = 0) :
 | 
|---|
 | 52 |     level(level)
 | 
|---|
 | 53 |   {
 | 
|---|
 | 54 |     grid = NULL;
 | 
|---|
 | 55 |   }
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 |   IsGrid(const GlobalIndices& global,
 | 
|---|
 | 58 |          const LocalIndices& local,
 | 
|---|
 | 59 |          const SpatialExtent& extent,
 | 
|---|
 | 60 |          int level = 0) :
 | 
|---|
 | 61 |     level(level),
 | 
|---|
 | 62 |     global(global),
 | 
|---|
 | 63 |     local(local),
 | 
|---|
 | 64 |     extent(extent),
 | 
|---|
 | 65 |     iterators(local)
 | 
|---|
 | 66 |   {
 | 
|---|
 | 67 |     InitIsGrid();
 | 
|---|
 | 68 |   }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |   IsGrid(const IsGrid& rhs) :
 | 
|---|
 | 71 |     level(rhs.Level()),
 | 
|---|
 | 72 |     global(rhs.Global()),
 | 
|---|
 | 73 |     local(rhs.Local()),
 | 
|---|
 | 74 |     extent(rhs.Extent()),
 | 
|---|
 | 75 |     iterators(rhs.Iterators())
 | 
|---|
 | 76 |   {
 | 
|---|
 | 77 |     InitIsGrid();
 | 
|---|
 | 78 |     SetGrid(rhs);
 | 
|---|
 | 79 |   }
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 |   IsGrid(const Grid& rhs) :
 | 
|---|
 | 82 |     level(rhs.Level()),
 | 
|---|
 | 83 |     global(rhs.Global()),
 | 
|---|
 | 84 |     local(rhs.Local()),
 | 
|---|
 | 85 |     extent(rhs.Extent()),
 | 
|---|
 | 86 |     iterators(rhs.Iterators())
 | 
|---|
 | 87 |   {
 | 
|---|
 | 88 |     InitIsGrid();
 | 
|---|
 | 89 |   }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |   virtual ~IsGrid();
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |   void SetGridSize(const Grid& grid);
 | 
|---|
 | 94 |   void SetGridSize(const GlobalIndices& global,
 | 
|---|
 | 95 |                    const LocalIndices& local,
 | 
|---|
 | 96 |                    const SpatialExtent& extent);
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   IsGrid& operator=(const IsGrid& rhs);
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |   const GlobalIndices& Global() const {return global;}
 | 
|---|
 | 101 |   const LocalIndices& Local() const {return local;}
 | 
|---|
 | 102 |   const SpatialExtent& Extent() const {return extent;}
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   GlobalIndices& Global() {return global;}
 | 
|---|
 | 105 |   LocalIndices& Local() {return local;}
 | 
|---|
 | 106 |   SpatialExtent& Extent() {return extent;}
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 |   GridIteratorSuite& Iterators() {return iterators;}
 | 
|---|
 | 109 |   const GridIteratorSuite& Iterators() const {return iterators;}
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |   const vmg_float& MeshWidth() const {return Extent().MeshWidth().X();} ///< Mesh width of current level
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   T& operator()(int x, int y, int z);  ///< Returns a reference to the requested gridpoint.
 | 
|---|
 | 114 |   T& operator()(const Index& index);
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |   const T& GetVal(int x, int y, int z) const; ///< Returns the value of a requested gridpoint.
 | 
|---|
 | 117 |   const T& GetVal(const Index& index) const;
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 |   void SetGrid(const IsGrid& rhs);     ///< Overwrite current grid with values from another grid
 | 
|---|
 | 120 |   void SetBoundary(const IsGrid& rhs); ///< Overwrite boundary with values from rhs
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 |   int GlobalLinearIndex(int x, int y, int z) const;  ///< Compute a unique 1-dimensional global index
 | 
|---|
 | 123 |   int GlobalLinearIndex(const Index& index) const;
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   const int& Level() const {return level;}
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |   bool IsActive() const {return Local().Size().Product() > 0;}
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | private:
 | 
|---|
 | 130 |   void InitIsGrid();
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | protected:
 | 
|---|
 | 133 |   int level;
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 |   GlobalIndices global;
 | 
|---|
 | 136 |   LocalIndices local;
 | 
|---|
 | 137 |   SpatialExtent extent;
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |   GridIteratorSuite iterators;
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   T* grid;
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |   static vmg_float correction;
 | 
|---|
 | 144 | };
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | template <class T>
 | 
|---|
 | 147 | inline T& IsGrid<T>::operator()(int x, int y, int z)
 | 
|---|
 | 148 | {
 | 
|---|
 | 149 |   return grid[z + local.SizeTotal().Z() * (y + local.SizeTotal().Y() * x)];
 | 
|---|
 | 150 | }
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 | template <class T>
 | 
|---|
 | 153 | inline T& IsGrid<T>::operator()(const Index& index)
 | 
|---|
 | 154 | {
 | 
|---|
 | 155 |   return this->operator()(index.X(), index.Y(), index.Z());
 | 
|---|
 | 156 | }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | template <class T>
 | 
|---|
 | 159 | inline const T& IsGrid<T>::GetVal(int x, int y, int z) const
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   return grid[z + local.SizeTotal().Z() * (y + local.SizeTotal().Y() * x)];
 | 
|---|
 | 162 | }
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 | template <class T>
 | 
|---|
 | 165 | inline const T& IsGrid<T>::GetVal(const Index& index) const
 | 
|---|
 | 166 | {
 | 
|---|
 | 167 |   return this->GetVal(index.X(), index.Y(), index.Z());
 | 
|---|
 | 168 | }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | template <class T>
 | 
|---|
 | 171 | inline int IsGrid<T>::GlobalLinearIndex(int x, int y, int z) const
 | 
|---|
 | 172 | {
 | 
|---|
 | 173 |   return z + global.GlobalSize().Z() * (y + global.GlobalSize().Y() * x);
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 | template <class T>
 | 
|---|
 | 177 | inline int IsGrid<T>::GlobalLinearIndex(const Index& index) const
 | 
|---|
 | 178 | {
 | 
|---|
 | 179 |   return index.Z() + global.GlobalSize().Z() * (index.Y() + global.GlobalSize().Y() * index.X());
 | 
|---|
 | 180 | }
 | 
|---|
 | 181 | 
 | 
|---|
 | 182 | template <class T>
 | 
|---|
 | 183 | void IsGrid<T>::InitIsGrid()
 | 
|---|
 | 184 | {
 | 
|---|
 | 185 |   grid = new T[local.SizeTotal().Product()];
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 | template <class T>
 | 
|---|
 | 189 | IsGrid<T>::~IsGrid()
 | 
|---|
 | 190 | {
 | 
|---|
 | 191 |   delete [] grid;
 | 
|---|
 | 192 | }
 | 
|---|
 | 193 | 
 | 
|---|
 | 194 | template <class T>
 | 
|---|
 | 195 | void IsGrid<T>::SetGridSize(const Grid& rhs)
 | 
|---|
 | 196 | {
 | 
|---|
 | 197 |   global = rhs.Global();
 | 
|---|
 | 198 |   local = rhs.Local();
 | 
|---|
 | 199 |   extent = rhs.Extent();
 | 
|---|
 | 200 |   iterators = rhs.Iterators();
 | 
|---|
 | 201 |   level = rhs.Level();
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |   delete [] grid;
 | 
|---|
 | 204 |   grid = new T[local.SizeTotal().Product()];
 | 
|---|
 | 205 | }
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 | template <class T>
 | 
|---|
 | 208 | void IsGrid<T>::SetGridSize(const GlobalIndices& global_,
 | 
|---|
 | 209 |                             const LocalIndices& local_,
 | 
|---|
 | 210 |                             const SpatialExtent& extent_)
 | 
|---|
 | 211 | {
 | 
|---|
 | 212 |   global = global_;
 | 
|---|
 | 213 |   local = local_;
 | 
|---|
 | 214 |   extent = extent_;
 | 
|---|
 | 215 |   iterators.SetSubgrids(local);
 | 
|---|
 | 216 |   level = 0;
 | 
|---|
 | 217 |   delete [] grid;
 | 
|---|
 | 218 |   grid = new T[local.SizeTotal().Product()];
 | 
|---|
 | 219 | }
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 | template <class T>
 | 
|---|
 | 222 | IsGrid<T>& IsGrid<T>::operator=(const IsGrid<T>& rhs)
 | 
|---|
 | 223 | {
 | 
|---|
 | 224 |   if (this != &rhs) {
 | 
|---|
 | 225 | 
 | 
|---|
 | 226 |     global = rhs.Global();
 | 
|---|
 | 227 |     local = rhs.Local();
 | 
|---|
 | 228 |     extent = rhs.Extent();
 | 
|---|
 | 229 |     iterators = rhs.Iterators();
 | 
|---|
 | 230 |     level = rhs.Level();
 | 
|---|
 | 231 | 
 | 
|---|
 | 232 |     delete [] grid;
 | 
|---|
 | 233 |     grid = new T[local.SizeTotal().Product()];
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |     SetGrid(rhs);
 | 
|---|
 | 236 | 
 | 
|---|
 | 237 |   }
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 |   return *this;
 | 
|---|
 | 240 | }
 | 
|---|
 | 241 | 
 | 
|---|
 | 242 | template <class T>
 | 
|---|
 | 243 | void IsGrid<T>::SetGrid(const IsGrid<T>& rhs)
 | 
|---|
 | 244 | {
 | 
|---|
 | 245 |   for (typename IsGrid<T>::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
 | 
|---|
 | 246 |     (*this)(*iter) = rhs.GetVal(*iter);
 | 
|---|
 | 247 | }
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 | template <class T>
 | 
|---|
 | 250 | void IsGrid<T>::SetBoundary(const IsGrid<T>& rhs)
 | 
|---|
 | 251 | {
 | 
|---|
 | 252 |   typename IsGrid<T>::iterator iter;
 | 
|---|
 | 253 | 
 | 
|---|
 | 254 |   for (int i=0; i<3; ++i) {
 | 
|---|
 | 255 | 
 | 
|---|
 | 256 |     for (iter = Iterators().Boundary1()[i].Begin(); iter != Iterators().Boundary1()[i].End(); ++iter)
 | 
|---|
 | 257 |       (*this)(*iter) = rhs.GetVal(*iter);
 | 
|---|
 | 258 | 
 | 
|---|
 | 259 |     for (iter = Iterators().Boundary2()[i].Begin(); iter != Iterators().Boundary2()[i].End(); ++iter)
 | 
|---|
 | 260 |       (*this)(*iter) = rhs.GetVal(*iter);
 | 
|---|
 | 261 | 
 | 
|---|
 | 262 |   }
 | 
|---|
 | 263 | }
 | 
|---|
 | 264 | 
 | 
|---|
 | 265 | 
 | 
|---|
 | 266 | }
 | 
|---|
 | 267 | 
 | 
|---|
 | 268 | #endif /* GRID_HPP_ */
 | 
|---|