| [fcf7f6] | 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 |
|
|---|
| [dfed1c] | 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_index_translations.hpp"
|
|---|
| 34 | #include "grid/grid_iterator.hpp"
|
|---|
| 35 | #include "grid/grid_iterator_suite.hpp"
|
|---|
| 36 | #include "grid/grid_properties.hpp"
|
|---|
| 37 |
|
|---|
| 38 | namespace VMG
|
|---|
| 39 | {
|
|---|
| 40 |
|
|---|
| 41 | class Comm;
|
|---|
| 42 | class Grid;
|
|---|
| 43 | class Multigrid;
|
|---|
| 44 | class Stencil;
|
|---|
| 45 |
|
|---|
| 46 | template <class T>
|
|---|
| 47 | class IsGrid
|
|---|
| 48 | {
|
|---|
| 49 | public:
|
|---|
| 50 | typedef GridIterator iterator;
|
|---|
| 51 |
|
|---|
| 52 | IsGrid(int level = 0) :
|
|---|
| 53 | level(level)
|
|---|
| 54 | {
|
|---|
| 55 | grid = NULL;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | IsGrid(const GlobalIndices& global,
|
|---|
| 59 | const LocalIndices& local,
|
|---|
| 60 | const SpatialExtent& extent,
|
|---|
| 61 | int level = 0) :
|
|---|
| 62 | level(level),
|
|---|
| 63 | global(global),
|
|---|
| 64 | local(local),
|
|---|
| 65 | extent(extent),
|
|---|
| 66 | iterators(local)
|
|---|
| 67 | {
|
|---|
| 68 | InitIsGrid();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | IsGrid(const IsGrid& rhs) :
|
|---|
| 72 | level(rhs.Level()),
|
|---|
| 73 | global(rhs.Global()),
|
|---|
| 74 | local(rhs.Local()),
|
|---|
| 75 | extent(rhs.Extent()),
|
|---|
| 76 | iterators(rhs.Iterators())
|
|---|
| 77 | {
|
|---|
| 78 | InitIsGrid();
|
|---|
| 79 | SetGrid(rhs);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | IsGrid(const Grid& rhs) :
|
|---|
| 83 | level(rhs.Level()),
|
|---|
| 84 | global(rhs.Global()),
|
|---|
| 85 | local(rhs.Local()),
|
|---|
| 86 | extent(rhs.Extent()),
|
|---|
| 87 | iterators(rhs.Iterators())
|
|---|
| 88 | {
|
|---|
| 89 | InitIsGrid();
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | virtual ~IsGrid();
|
|---|
| 93 |
|
|---|
| 94 | void SetGridSize(const Grid& grid);
|
|---|
| 95 | void SetGridSize(const GlobalIndices& global,
|
|---|
| 96 | const LocalIndices& local,
|
|---|
| 97 | const SpatialExtent& extent);
|
|---|
| 98 |
|
|---|
| 99 | IsGrid& operator=(const IsGrid& rhs);
|
|---|
| 100 |
|
|---|
| 101 | const GlobalIndices& Global() const {return global;}
|
|---|
| 102 | const LocalIndices& Local() const {return local;}
|
|---|
| 103 | const SpatialExtent& Extent() const {return extent;}
|
|---|
| 104 |
|
|---|
| 105 | GlobalIndices& Global() {return global;}
|
|---|
| 106 | LocalIndices& Local() {return local;}
|
|---|
| 107 | SpatialExtent& Extent() {return extent;}
|
|---|
| 108 |
|
|---|
| 109 | GridIteratorSuite& Iterators() {return iterators;}
|
|---|
| 110 | const GridIteratorSuite& Iterators() const {return iterators;}
|
|---|
| 111 |
|
|---|
| 112 | const vmg_float& MeshWidth() const {return Extent().MeshWidth().X();} ///< Mesh width of current level
|
|---|
| 113 |
|
|---|
| 114 | T& operator()(int x, int y, int z); ///< Returns a reference to the requested gridpoint.
|
|---|
| 115 | T& operator()(const Index& index);
|
|---|
| 116 |
|
|---|
| 117 | const T& GetVal(int x, int y, int z) const; ///< Returns the value of a requested gridpoint.
|
|---|
| 118 | const T& GetVal(const Index& index) const;
|
|---|
| 119 |
|
|---|
| 120 | void SetGrid(const IsGrid& rhs); ///< Overwrite current grid with values from another grid
|
|---|
| 121 | void SetBoundary(const IsGrid& rhs); ///< Overwrite boundary with values from rhs
|
|---|
| 122 |
|
|---|
| 123 | int GlobalLinearIndex(int x, int y, int z) const; ///< Compute a unique 1-dimensional global index
|
|---|
| 124 | int GlobalLinearIndex(const Index& index) const;
|
|---|
| 125 |
|
|---|
| 126 | const int& Level() const {return level;}
|
|---|
| 127 |
|
|---|
| 128 | bool IsActive() const {return Local().Size().Product() > 0;}
|
|---|
| 129 |
|
|---|
| 130 | private:
|
|---|
| 131 | void InitIsGrid();
|
|---|
| 132 |
|
|---|
| 133 | protected:
|
|---|
| 134 | int level;
|
|---|
| 135 |
|
|---|
| 136 | GlobalIndices global;
|
|---|
| 137 | LocalIndices local;
|
|---|
| 138 | SpatialExtent extent;
|
|---|
| 139 |
|
|---|
| 140 | GridIteratorSuite iterators;
|
|---|
| 141 |
|
|---|
| 142 | T* grid;
|
|---|
| 143 |
|
|---|
| 144 | static vmg_float correction;
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | template <class T>
|
|---|
| 148 | inline T& IsGrid<T>::operator()(int x, int y, int z)
|
|---|
| 149 | {
|
|---|
| 150 | return grid[z + local.SizeTotal().Z() * (y + local.SizeTotal().Y() * x)];
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | template <class T>
|
|---|
| 154 | inline T& IsGrid<T>::operator()(const Index& index)
|
|---|
| 155 | {
|
|---|
| 156 | return this->operator()(index.X(), index.Y(), index.Z());
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | template <class T>
|
|---|
| 160 | inline const T& IsGrid<T>::GetVal(int x, int y, int z) const
|
|---|
| 161 | {
|
|---|
| 162 | return grid[z + local.SizeTotal().Z() * (y + local.SizeTotal().Y() * x)];
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | template <class T>
|
|---|
| 166 | inline const T& IsGrid<T>::GetVal(const Index& index) const
|
|---|
| 167 | {
|
|---|
| 168 | return this->GetVal(index.X(), index.Y(), index.Z());
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | template <class T>
|
|---|
| 172 | inline int IsGrid<T>::GlobalLinearIndex(int x, int y, int z) const
|
|---|
| 173 | {
|
|---|
| [ac6d04] | 174 | return z + global.GlobalSize().Z() * (y + global.GlobalSize().Y() * x);
|
|---|
| [dfed1c] | 175 | }
|
|---|
| 176 |
|
|---|
| 177 | template <class T>
|
|---|
| 178 | inline int IsGrid<T>::GlobalLinearIndex(const Index& index) const
|
|---|
| 179 | {
|
|---|
| [ac6d04] | 180 | return index.Z() + global.GlobalSize().Z() * (index.Y() + global.GlobalSize().Y() * index.X());
|
|---|
| [dfed1c] | 181 | }
|
|---|
| 182 |
|
|---|
| 183 | template <class T>
|
|---|
| 184 | void IsGrid<T>::InitIsGrid()
|
|---|
| 185 | {
|
|---|
| 186 | grid = new T[local.SizeTotal().Product()];
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | template <class T>
|
|---|
| 190 | IsGrid<T>::~IsGrid()
|
|---|
| 191 | {
|
|---|
| 192 | delete [] grid;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | template <class T>
|
|---|
| [facba0] | 196 | void IsGrid<T>::SetGridSize(const Grid& rhs)
|
|---|
| [dfed1c] | 197 | {
|
|---|
| [facba0] | 198 | global = rhs.Global();
|
|---|
| 199 | local = rhs.Local();
|
|---|
| 200 | extent = rhs.Extent();
|
|---|
| 201 | iterators = rhs.Iterators();
|
|---|
| 202 | level = rhs.Level();
|
|---|
| [dfed1c] | 203 |
|
|---|
| 204 | delete [] grid;
|
|---|
| 205 | grid = new T[local.SizeTotal().Product()];
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | template <class T>
|
|---|
| 209 | void IsGrid<T>::SetGridSize(const GlobalIndices& global_,
|
|---|
| 210 | const LocalIndices& local_,
|
|---|
| 211 | const SpatialExtent& extent_)
|
|---|
| 212 | {
|
|---|
| 213 | global = global_;
|
|---|
| 214 | local = local_;
|
|---|
| 215 | extent = extent_;
|
|---|
| 216 | iterators.SetSubgrids(local);
|
|---|
| 217 | level = 0;
|
|---|
| 218 | delete [] grid;
|
|---|
| 219 | grid = new T[local.SizeTotal().Product()];
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | template <class T>
|
|---|
| 223 | IsGrid<T>& IsGrid<T>::operator=(const IsGrid<T>& rhs)
|
|---|
| 224 | {
|
|---|
| 225 | if (this != &rhs) {
|
|---|
| 226 |
|
|---|
| 227 | global = rhs.Global();
|
|---|
| 228 | local = rhs.Local();
|
|---|
| 229 | extent = rhs.Extent();
|
|---|
| 230 | iterators = rhs.Iterators();
|
|---|
| 231 | level = rhs.Level();
|
|---|
| 232 |
|
|---|
| 233 | delete [] grid;
|
|---|
| 234 | grid = new T[local.SizeTotal().Product()];
|
|---|
| 235 |
|
|---|
| 236 | SetGrid(rhs);
|
|---|
| 237 |
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | return *this;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | template <class T>
|
|---|
| 244 | void IsGrid<T>::SetGrid(const IsGrid<T>& rhs)
|
|---|
| 245 | {
|
|---|
| [65f11de] | 246 | for (typename IsGrid<T>::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
|
|---|
| [dfed1c] | 247 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | template <class T>
|
|---|
| 251 | void IsGrid<T>::SetBoundary(const IsGrid<T>& rhs)
|
|---|
| 252 | {
|
|---|
| [65f11de] | 253 | typename IsGrid<T>::iterator iter;
|
|---|
| [dfed1c] | 254 |
|
|---|
| 255 | for (int i=0; i<3; ++i) {
|
|---|
| 256 |
|
|---|
| 257 | for (iter = Iterators().Boundary1()[i].Begin(); iter != Iterators().Boundary1()[i].End(); ++iter)
|
|---|
| 258 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 259 |
|
|---|
| 260 | for (iter = Iterators().Boundary2()[i].Begin(); iter != Iterators().Boundary2()[i].End(); ++iter)
|
|---|
| 261 | (*this)(*iter) = rhs.GetVal(*iter);
|
|---|
| 262 |
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | #endif /* GRID_HPP_ */
|
|---|