grid_dist_id_iterator_dec.hpp 5.63 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * grid_dist_id_iterator_dec.hpp
 *
 *  Created on: Jan 27, 2016
 *      Author: i-bird
 */

#ifndef SRC_GRID_GRID_DIST_ID_ITERATOR_DEC_HPP_
#define SRC_GRID_GRID_DIST_ID_ITERATOR_DEC_HPP_

#include "grid_dist_id_iterator.hpp"
incardon's avatar
incardon committed
12 13
#include "Grid/grid_dist_util.hpp"
#include "grid_dist_id_iterator_util.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
14 15 16 17 18 19 20 21

/*! \brief Given the decomposition it create an iterator
 *
 * Iterator across the local elements of the distributed grid
 *
 * \tparam dec Decomposition type
 *
 */
incardon's avatar
incardon committed
22
template<typename Decomposition, bool ghost_or_domain = false>
Pietro Incardona's avatar
Pietro Incardona committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class grid_dist_id_iterator_dec
{
	//! grid list counter
	size_t g_c;

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

	//! Actual iterator
	grid_key_dx_iterator_sub<Decomposition::dims> a_it;

	//! start key
	grid_key_dx<Decomposition::dims> start;

	//! stop key
	grid_key_dx<Decomposition::dims> stop;

	//! Spacing
	typename Decomposition::stype spacing[Decomposition::dims];


	/*! \brief from g_c increment g_c until you find a valid grid
	 *
	 */
	void selectValidGrid()
	{
		// start and stop for the subset grid
		grid_key_dx<Decomposition::dims> start_c;
		grid_key_dx<Decomposition::dims> stop_c;

		// When the grid has size 0 potentially all the other informations are garbage
		while (g_c < gdb_ext.size() &&
incardon's avatar
incardon committed
55
			   (gdb_ext.get(g_c).Dbox.isValid() == false || compute_subset<Decomposition,ghost_or_domain>(gdb_ext,g_c,start,stop,start_c,stop_c) == false ))
Pietro Incardona's avatar
Pietro Incardona committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		{g_c++;}

		// get the next grid iterator
		if (g_c < gdb_ext.size())
		{
			// Calculate the resolution of the local grid
			size_t sz[Decomposition::dims];
			for (size_t i = 0 ; i < Decomposition::dims ; i++)
				sz[i] = gdb_ext.get(g_c).GDbox.getP2()[i] + 1;

			grid_sm<Decomposition::dims,void> g_sm(sz);
			a_it.reinitialize(grid_key_dx_iterator_sub<Decomposition::dims>(g_sm,start_c,stop_c));
		}
	}

	/*! \brief Get the actual key
	 *
	 * \return the actual key
	 *
	 */
	inline grid_dist_key_dx<Decomposition::dims> get_int()
	{
		return grid_dist_key_dx<Decomposition::dims>(g_c,a_it.get());
	}

	public:

	/*! \brief Copy operator=
	*
	* \param tmp iterator to copy
	*
incardon's avatar
incardon committed
87 88
	* \return itself
	*
Pietro Incardona's avatar
Pietro Incardona committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	*/
	grid_dist_id_iterator_dec<Decomposition> & operator=(const grid_dist_id_iterator_dec<Decomposition> & tmp)
	{
		g_c = tmp.g_c;
		gdb_ext = tmp.gdb_ext;
		a_it.reinitialize(tmp.a_it);

		start = tmp.start;
		stop = tmp.stop;

		return *this;
	}

	/*! \brief Copy constructor
	*
	* \param tmp iterator to copy
	*
	*/
	grid_dist_id_iterator_dec(const grid_dist_id_iterator_dec<Decomposition> & tmp)
	{
		this->operator=(tmp);
	}

	/*! \brief Constructor of the distributed grid iterator
	 *
	 * \param dec Decomposition
	 * \param sz size of the grid
	 *
	 */
	grid_dist_id_iterator_dec(Decomposition & dec, const size_t (& sz)[Decomposition::dims])
	:g_c(0)
	{
		// Initialize start and stop
		start.zero();
123 124
		for (size_t i = 0 ; i < Decomposition::dims ; i++)
			stop.set_d(i,sz[i]-1);
Pietro Incardona's avatar
Pietro Incardona committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

		// From the decomposition construct gdb_ext
		create_gdb_ext<Decomposition::dims,Decomposition>(gdb_ext,dec,sz,dec.getDomain(),spacing);

		// Initialize the current iterator
		// with the first grid
		selectValidGrid();
	}

	/*! \brief Constructor of the distributed grid iterator
	 *
	 * \param dec Decomposition
	 * \param sz size of the grid
	 * \param start point
	 * \param stop point
	 *
	 */
	grid_dist_id_iterator_dec(Decomposition & dec, const size_t (& sz)[Decomposition::dims], grid_key_dx<Decomposition::dims> start, grid_key_dx<Decomposition::dims> stop)
	:g_c(0),start(start),stop(stop)
	{
		// From the decomposition construct gdb_ext
		create_gdb_ext<Decomposition::dims,Decomposition>(gdb_ext,dec,sz,dec.getDomain(),spacing);

		// Initialize the current iterator
		// with the first grid
		selectValidGrid();
	}

	// Destructor
	~grid_dist_id_iterator_dec()
	{
	}

	/*! \brief Get the next element
	 *
	 * \return the next grid_key
	 *
	 */

incardon's avatar
incardon committed
164
	inline grid_dist_id_iterator_dec<Decomposition,ghost_or_domain> & operator++()
Pietro Incardona's avatar
Pietro Incardona committed
165 166 167 168 169 170
	{
		++a_it;

		// check if a_it is at the end

		if (a_it.isNext() == true)
incardon's avatar
incardon committed
171
		{return *this;}
Pietro Incardona's avatar
Pietro Incardona committed
172 173 174 175 176 177 178 179 180 181 182 183 184 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 213 214 215
		else
		{
			// switch to the new grid
			g_c++;

			selectValidGrid();
		}

		return *this;
	}

	/*! \brief Check if there is the next element
	 *
	 * \return true if there is the next, false otherwise
	 *
	 */
	inline bool isNext()
	{
		// If there are no other grid stop

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

		return true;
	}

	/*! \brief Get the spacing of the grid
	 *
	 * \param i
	 *
	 */
	inline typename Decomposition::stype getSpacing(size_t i)
	{
		return spacing[i];
	}

	/*! \brief Get the actual global key of the grid
	 *
	 *
	 * \return the global position in the grid
	 *
	 */
	inline grid_key_dx<Decomposition::dims> get()
	{
incardon's avatar
incardon committed
216
		const grid_dist_key_dx<Decomposition::dims> k = get_int();
Pietro Incardona's avatar
Pietro Incardona committed
217 218 219 220 221 222 223 224 225 226 227

		// Get the sub-domain id
		size_t sub_id = k.getSub();

		grid_key_dx<Decomposition::dims> k_glob = k.getKey();

		// shift
		k_glob = k_glob + gdb_ext.get(sub_id).origin;

		return k_glob;
	}
incardon's avatar
incardon committed
228

229 230 231 232 233 234 235 236 237 238 239 240 241
	/*! \brief Get the actual grid key for a distributed grid
	 *
	 * \note if you are using this iterator and you need the position for grid_dist_id use this
	 *       function to retrieve the grid point
	 *
	 * \return the grid point
	 *
	 */
	inline grid_dist_key_dx<Decomposition::dims> get_dist()
	{
		return get_int();
	}

incardon's avatar
incardon committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	/*! \brief Get the starting point of the sub-grid we are iterating
	 *
	 * \return the starting point
	 *
	 */
	inline grid_key_dx<Decomposition::dims> getStart()
	{
		return start;
	}

	/*! \brief Get the starting point of the sub-grid we are iterating
	 *
	 * \return the stop point
	 *
	 */
	inline grid_key_dx<Decomposition::dims> getStop()
	{
		return stop;
	}
Pietro Incardona's avatar
Pietro Incardona committed
261 262 263 264
};


#endif /* SRC_GRID_GRID_DIST_ID_ITERATOR_DEC_HPP_ */