source: src/grid/is_grid.hpp@ 2d4211

Last change on this file since 2d4211 was fcf7f6, checked in by Julian Iseringhausen <isering@…>, 14 years ago

vmg: Added license files and headers (GPLv3).

git-svn-id: https://svn.version.fz-juelich.de/scafacos/trunk@1812 5161e1c8-67bf-11de-9fd5-51895aff932f

  • Property mode set to 100644
File size: 6.3 KB
Line 
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_index_translations.hpp"
34#include "grid/grid_iterator.hpp"
35#include "grid/grid_iterator_suite.hpp"
36#include "grid/grid_properties.hpp"
37
38namespace VMG
39{
40
41class Comm;
42class Grid;
43class Multigrid;
44class Stencil;
45
46template <class T>
47class IsGrid
48{
49public:
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
130private:
131 void InitIsGrid();
132
133protected:
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
147template <class T>
148inline 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
153template <class T>
154inline T& IsGrid<T>::operator()(const Index& index)
155{
156 return this->operator()(index.X(), index.Y(), index.Z());
157}
158
159template <class T>
160inline 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
165template <class T>
166inline const T& IsGrid<T>::GetVal(const Index& index) const
167{
168 return this->GetVal(index.X(), index.Y(), index.Z());
169}
170
171template <class T>
172inline int IsGrid<T>::GlobalLinearIndex(int x, int y, int z) const
173{
174 return z + global.GlobalSize().Z() * (y + global.GlobalSize().Y() * x);
175}
176
177template <class T>
178inline int IsGrid<T>::GlobalLinearIndex(const Index& index) const
179{
180 return index.Z() + global.GlobalSize().Z() * (index.Y() + global.GlobalSize().Y() * index.X());
181}
182
183template <class T>
184void IsGrid<T>::InitIsGrid()
185{
186 grid = new T[local.SizeTotal().Product()];
187}
188
189template <class T>
190IsGrid<T>::~IsGrid()
191{
192 delete [] grid;
193}
194
195template <class T>
196void IsGrid<T>::SetGridSize(const Grid& rhs)
197{
198 global = rhs.Global();
199 local = rhs.Local();
200 extent = rhs.Extent();
201 iterators = rhs.Iterators();
202 level = rhs.Level();
203
204 delete [] grid;
205 grid = new T[local.SizeTotal().Product()];
206}
207
208template <class T>
209void 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
222template <class T>
223IsGrid<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
243template <class T>
244void IsGrid<T>::SetGrid(const IsGrid<T>& rhs)
245{
246 for (typename IsGrid<T>::iterator iter = Iterators().CompleteGrid().Begin(); iter != Iterators().CompleteGrid().End(); ++iter)
247 (*this)(*iter) = rhs.GetVal(*iter);
248}
249
250template <class T>
251void IsGrid<T>::SetBoundary(const IsGrid<T>& rhs)
252{
253 typename IsGrid<T>::iterator iter;
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_ */
Note: See TracBrowser for help on using the repository browser.