grid_dist_id_iterator.hpp 7.15 KB
Newer Older
incardon's avatar
incardon committed
1
/*
incardon's avatar
incardon committed
2
 * grid_dist_id_iterator_sub.hpp
incardon's avatar
incardon committed
3 4 5 6 7 8 9 10
 *
 *  Created on: Feb 4, 2015
 *      Author: Pietro Incardona
 */

#ifndef GRID_DIST_ID_ITERATOR_HPP_
#define GRID_DIST_ID_ITERATOR_HPP_

incardon's avatar
incardon committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*! \brief This structure store the Box that define the domain inside the Ghost + domain box
 *
	\verbatim

                          (Ghost + Domain)
     +------------------+
     |                  |
     |  +------------+ <---------- (Domain)
     |  |            |  |
     |  |  Domain    |  |
     |  |  Box       |  |
     |  |            |  |
     |  |            |  |
     |  +------------+  |
     |                  |
     +------------------+
27
(0,0) local coordinate ---> ( x, y )
incardon's avatar
incardon committed
28 29 30 31 32 33 34 35 36 37 38 39 40

	\endverbatim

 *
 *  * Domain
 *
 * \tparam dim dimensionality
 *
 */
template<unsigned int dim>
struct GBoxes
{
	//! Ghost + Domain ghost
41
	Box<dim,long int> GDbox;
incardon's avatar
incardon committed
42
	//! Domain box
43
	Box<dim,long int> Dbox;
incardon's avatar
incardon committed
44
	//! origin of GDbox in global grid coordinates
45
	Point<dim,long int> origin;
incardon's avatar
incardon committed
46 47
};

incardon's avatar
incardon committed
48 49 50
#define FREE 1
#define FIXED 2

incardon's avatar
incardon committed
51
#include "grid_dist_key.hpp"
incardon's avatar
incardon committed
52
#include "VCluster.hpp"
incardon's avatar
incardon committed
53

incardon's avatar
incardon committed
54 55


incardon's avatar
incardon committed
56 57
/*! \brief Distributed grid iterator
 *
incardon's avatar
incardon committed
58
 * Iterator across the local elements of the distributed grid
incardon's avatar
incardon committed
59
 *
incardon's avatar
incardon committed
60 61 62 63
 * \tparam dim dimensionality of the grid
 * \tparam device_grid type of basic grid
 * \tparam impl implementation
 *
incardon's avatar
incardon committed
64
 */
incardon's avatar
incardon committed
65 66 67 68 69 70
template<unsigned int dim, typename device_grid, int impl >
class grid_dist_iterator
{

};

incardon's avatar
incardon committed
71

incardon's avatar
incardon committed
72 73 74 75 76 77 78 79 80
/*! \brief Distributed grid iterator
 *
 * Iterator across the local elements of the distributed grid
 *
 * \tparam dim dimensionality of the grid
 * \tparam device_grid type of basic grid
 * \tparam impl implementation
 *
 */
incardon's avatar
incardon committed
81
template<unsigned int dim, typename device_grid>
incardon's avatar
incardon committed
82
class grid_dist_iterator<dim,device_grid,FREE>
incardon's avatar
incardon committed
83 84 85 86 87
{
	//! grid list counter
	size_t g_c;

	//! List of the grids we are going to iterate
88
	const openfpm::vector<device_grid> & gList;
incardon's avatar
incardon committed
89

incardon's avatar
incardon committed
90
	//! Extension of each grid: domain and ghost + domain
91
	const openfpm::vector<GBoxes<device_grid::dims>> & gdb_ext;
incardon's avatar
incardon committed
92

incardon's avatar
incardon committed
93
	//! Actual iterator
incardon's avatar
incardon committed
94 95
	grid_key_dx_iterator_sub<dim> a_it;

Pietro Incardona's avatar
Pietro Incardona committed
96 97
	//! stop point (is the grid size)
	grid_key_dx<dim> stop;
incardon's avatar
incardon committed
98

incardon's avatar
incardon committed
99 100 101 102 103 104
	/*! \brief from g_c increment g_c until you find a valid grid
	 *
	 */
	void selectValidGrid()
	{
		// When the grid has size 0 potentially all the other informations are garbage
105
		while (g_c < gList.size() && (gList.get(g_c).size() == 0 || gdb_ext.get(g_c).Dbox.isValid() == false ) ) g_c++;
incardon's avatar
incardon committed
106 107 108 109

		// get the next grid iterator
		if (g_c < gList.size())
		{
110
			a_it.reinitialize(gList.get(g_c).getIterator(gdb_ext.get(g_c).Dbox.getKP1(),gdb_ext.get(g_c).Dbox.getKP2()));
incardon's avatar
incardon committed
111 112 113
		}
	}

incardon's avatar
incardon committed
114 115
	public:

incardon's avatar
incardon committed
116
	/*! \brief Constructor of the distributed grid iterator
incardon's avatar
incardon committed
117 118
	 *
	 * \param gk std::vector of the local grid
Pietro Incardona's avatar
Pietro Incardona committed
119 120
	 * \param gdb_ext set of local subdomains
	 * \param stop end point
incardon's avatar
incardon committed
121 122
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
123
	grid_dist_iterator(const openfpm::vector<device_grid> & gk, const openfpm::vector<GBoxes<device_grid::dims>> & gdb_ext, const grid_key_dx<dim> & stop)
Pietro Incardona's avatar
Pietro Incardona committed
124
	:g_c(0),gList(gk),gdb_ext(gdb_ext),stop(stop)
incardon's avatar
incardon committed
125
	{
incardon's avatar
incardon committed
126
		// Initialize the current iterator
incardon's avatar
incardon committed
127
		// with the first grid
incardon's avatar
incardon committed
128
		selectValidGrid();
incardon's avatar
incardon committed
129 130 131 132
	}

	// Destructor
	~grid_dist_iterator()
incardon's avatar
incardon committed
133 134
	{
	}
incardon's avatar
incardon committed
135

incardon's avatar
incardon committed
136
	/*! \brief Get the next element
incardon's avatar
incardon committed
137
	 *
incardon's avatar
incardon committed
138
	 * \return the next grid_key
incardon's avatar
incardon committed
139 140
	 *
	 */
incardon's avatar
incardon committed
141

incardon's avatar
incardon committed
142
	inline grid_dist_iterator<dim,device_grid,FREE> operator++()
incardon's avatar
incardon committed
143
	{
incardon's avatar
incardon committed
144 145 146 147 148 149 150 151 152 153 154
		++a_it;

		// check if a_it is at the end

		if (a_it.isNext() == true)
			return *this;
		else
		{
			// switch to the new grid
			g_c++;

incardon's avatar
incardon committed
155
			selectValidGrid();
incardon's avatar
incardon committed
156
		}
incardon's avatar
incardon committed
157 158

		return *this;
incardon's avatar
incardon committed
159 160 161 162 163 164 165
	}

	/*! \brief Check if there is the next element
	 *
	 * \return true if there is the next, false otherwise
	 *
	 */
incardon's avatar
incardon committed
166
	inline bool isNext()
incardon's avatar
incardon committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180
	{
		// If there are no other grid stop

		if (g_c >= gList.size())
			return false;

		return true;
	}

	/*! \brief Get the actual key
	 *
	 * \return the actual key
	 *
	 */
incardon's avatar
incardon committed
181
	inline grid_dist_key_dx<dim> get()
incardon's avatar
incardon committed
182 183 184
	{
		return grid_dist_key_dx<dim>(g_c,a_it.get());
	}
Pietro Incardona's avatar
Pietro Incardona committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

	/*! \brief it return the stop point of the iterator
	 *
	 * The stop point of the iterator is just the grid size
	 *
	 * \return the stop point
	 *
	 */
	inline grid_key_dx<dim> getStop() const
	{
		return stop;
	}

	/*! \brief it return the start point of the iterator
	 *
	 * The start point of the iterator is the point with all coordinates zeros
	 *
	 * \return the start point
	 *
	 */
	inline grid_key_dx<dim> getStart() const
	{
		grid_key_dx<dim> start;

		start.zero();

		return start;
	}
Pietro Incardona's avatar
Pietro Incardona committed
213 214 215 216 217 218 219 220 221 222 223 224

	/*! \brief Get the boxes
	 *
	 *  Get the boxes that define the local grids
	 *
	 * \return Vector of local boxes
	 *
	 */
	inline const openfpm::vector<GBoxes<device_grid::dims>> & getGBoxes()
	{
		return gdb_ext;
	}
incardon's avatar
incardon committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
};


/*! \brief Distributed grid iterator
 *
 * Iterator across the local elements of the distributed grid
 *
 * \tparam dim dimensionality of the grid
 * \tparam device_grid type of basic grid
 * \tparam impl implementation
 *
 */
template<unsigned int dim, typename device_grid>
class grid_dist_iterator<dim,device_grid,FIXED>
{
	//! grid list counter
	size_t g_c;

	//! List of the grids we are going to iterate
244
	const openfpm::vector<device_grid> & gList;
incardon's avatar
incardon committed
245 246 247 248 249 250 251

	//! Extension of each grid: domain and ghost + domain
	const openfpm::vector<GBoxes<device_grid::dims>> & gdb_ext;

	//! Actual iterator
	grid_key_dx_iterator<dim> a_it;

252 253 254 255 256 257
	/*! \brief from g_c increment g_c until you find a valid grid
	 *
	 */
	void selectValidGrid()
	{
		// When the grid has size 0 potentially all the other informations are garbage
258
		while (g_c < gList.size() && (gList.get(g_c).size() == 0 || gdb_ext.get(g_c).Dbox.isValid() == false ) ) g_c++;
259 260 261 262

		// get the next grid iterator
		if (g_c < gList.size())
		{
263
			a_it.reinitialize(gList.get(g_c).getIterator(gdb_ext.get(g_c).Dbox.getKP1(),gdb_ext.get(g_c).Dbox.getKP2()));
264 265 266
		}
	}

incardon's avatar
incardon committed
267 268
	public:

incardon's avatar
incardon committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	/*! \brief Copy operator=
	*
	* \param tmp iterator to copy
	*
	*/
	grid_dist_iterator<dim,device_grid,FIXED> & operator=(const grid_dist_iterator<dim,device_grid,FIXED> & tmp)
	{
		g_c = tmp.g_c;
		gList = tmp.gList;
		gdb_ext = tmp.gdb_ext;
		a_it.reinitialize(tmp.a_it);

		return *this;
	}

	/*! \brief Constructor of the distributed grid iterator
incardon's avatar
incardon committed
285 286 287 288
	 *
	 * \param gk std::vector of the local grid
	 *
	 */
289
	grid_dist_iterator(const openfpm::vector<device_grid> & gk, const openfpm::vector<GBoxes<device_grid::dims>> & gdb_ext)
incardon's avatar
incardon committed
290 291 292 293
	:g_c(0),gList(gk),gdb_ext(gdb_ext)
	{
		// Initialize the current iterator
		// with the first grid
294
		selectValidGrid();
incardon's avatar
incardon committed
295 296 297 298 299 300
	}

	// Destructor
	~grid_dist_iterator()
	{
	}
incardon's avatar
incardon committed
301

incardon's avatar
incardon committed
302 303 304 305 306 307
	/*! \brief Get the next element
	 *
	 * \return the next grid_key
	 *
	 */

incardon's avatar
incardon committed
308
	grid_dist_iterator<dim,device_grid,FIXED> operator++()
incardon's avatar
incardon committed
309
	{
incardon's avatar
incardon committed
310
		++a_it;
incardon's avatar
incardon committed
311 312 313

		// check if a_it is at the end

incardon's avatar
incardon committed
314
		if (a_it.isNext() == true)
incardon's avatar
incardon committed
315 316 317 318 319
			return *this;
		else
		{
			// switch to the new grid
			g_c++;
320
			selectValidGrid();
incardon's avatar
incardon committed
321 322 323 324 325 326 327 328 329 330
		}

		return *this;
	}

	/*! \brief Check if there is the next element
	 *
	 * \return true if there is the next, false otherwise
	 *
	 */
incardon's avatar
incardon committed
331
	bool isNext()
incardon's avatar
incardon committed
332 333 334 335
	{
		// If there are no other grid stop

		if (g_c >= gList.size())
incardon's avatar
incardon committed
336 337 338
			return false;

		return true;
incardon's avatar
incardon committed
339 340 341 342 343 344 345 346 347
	}

	/*! \brief Get the actual key
	 *
	 * \return the actual key
	 *
	 */
	grid_dist_key_dx<dim> get()
	{
incardon's avatar
incardon committed
348
		return grid_dist_key_dx<dim>(g_c,a_it.get());
incardon's avatar
incardon committed
349
	}
Pietro Incardona's avatar
Pietro Incardona committed
350 351 352 353 354 355 356 357 358 359 360 361

	/*! \brief Get the boxes
	 *
	 *  Get the boxes that define the local grids
	 *
	 * \return Vector of local boxes
	 *
	 */
	inline const openfpm::vector<GBoxes<device_grid::dims>> & getGBoxes()
	{
		return gdb_ext;
	}
incardon's avatar
incardon committed
362 363
};

incardon's avatar
incardon committed
364
#endif /* GRID_DIST_ID_ITERATOR_SUB_HPP_ */