grid_dist_id_iterator_dec_skin.hpp 6.5 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * grid_dist_id_iterator_dec_skin.hpp
 *
 *  Created on: Jan 4, 2017
 *      Author: i-bird
 */

#ifndef SRC_GRID_ITERATORS_GRID_DIST_ID_ITERATOR_DEC_SKIN_HPP_
#define SRC_GRID_ITERATORS_GRID_DIST_ID_ITERATOR_DEC_SKIN_HPP_


#include "grid_dist_id_iterator.hpp"
#include "Grid/grid_dist_util.hpp"
#include "grid_dist_id_iterator_util.hpp"

/*! \brief Given the decomposition it create an iterator
 *
 * Iterator across the local elements of the distributed grid
 *
 * \tparam dec Decomposition type
 *
 */
template<typename Decomposition>
class grid_dist_id_iterator_dec_skin : protected grid_skin_iterator_bc<Decomposition::dims>
{
	//! a_its element in this moment selected
	size_t a_its_p;

	//! grid list counter
	size_t g_c;

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

35
	//! Internal grid sub-iterator
incardon's avatar
incardon committed
36 37
	grid_key_dx_iterator_sub<Decomposition::dims> a_it;

38
	//! Internal struct
incardon's avatar
incardon committed
39 40 41 42 43 44 45 46
	struct gp_sub
	{
		//! from which grid this iterator come from
		size_t gc;

		//! Iterator
		grid_key_dx_iterator_sub<Decomposition::dims> it;

47 48 49 50 51 52
		/*! \brief constructor
		 *
		 * \param gc sub-domain
		 * \param it iterator
		 *
		 */
incardon's avatar
incardon committed
53 54 55 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 87 88 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 123 124 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
		gp_sub(size_t gc, grid_key_dx_iterator_sub<Decomposition::dims> && it)
		:gc(gc),it(it)
		{}
	};

	//! Actual sub-iterators
	openfpm::vector<gp_sub> a_its;

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


	/*! \brief from g_c increment g_c until you find a valid grid
	 *
	 */
	void selectValidGrid()
	{
		if (a_its_p < a_its.size())
		{
			g_c = a_its.get(a_its_p).gc;

			a_it.reinitialize(a_its.get(a_its_p).it);
		}
		else
			g_c = gdb_ext.size();
	}

	/*! \brief construct sub-iterators
	 *
	 *
	 *
	 */
	void construct_sub_it()
	{
		// Construct the sub iterators
		for (size_t i = 0 ; i < 2*Decomposition::dims; i++)
		{
			for (size_t gc = 0 ; gc < gdb_ext.size() ; gc++)
			{
				grid_key_dx<Decomposition::dims> start = this->sub_it[i].getStart();
				grid_key_dx<Decomposition::dims> stop = this->sub_it[i].getStop();

				grid_key_dx<Decomposition::dims> start_c;
				grid_key_dx<Decomposition::dims> stop_c;

				if (compute_subset<Decomposition>(gdb_ext,gc,start,stop,start_c,stop_c) == true)
				{
					// Convert global coordinate start_c stop_c into local
					// and calculate the grid sizes
					size_t sz[Decomposition::dims];
					for (size_t j = 0 ; j < Decomposition::dims ; j++)
						sz[j] = gdb_ext.get(gc).GDbox.getHigh(j) + 1;

					grid_sm<Decomposition::dims,void> g_sm(sz);

					// Non empty sub-set
					a_its.add(gp_sub(gc,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 constructor
	*
	* \param tmp iterator to copy
	*
	*/
	grid_dist_id_iterator_dec_skin(const grid_dist_id_iterator_dec_skin<Decomposition> & tmp)
	:grid_skin_iterator_bc<Decomposition::dims>(tmp),a_its_p(0)
	{
		this->operator=(tmp);
	}

	/*! \brief Copy constructor
	*
	* \param tmp iterator to copy
	*
	*/
	grid_dist_id_iterator_dec_skin(grid_dist_id_iterator_dec_skin<Decomposition> && tmp)
	:grid_skin_iterator_bc<Decomposition::dims>(tmp),a_its_p(0)
	{
		this->operator=(tmp);
	}

	/*! \brief Constructor of the distributed grid iterator
	 *
	 * \param dec Decomposition
153 154 155
	 * \param g_sm grid size on each direction
	 * \param A box A (must be contained into B)
	 * \param B box B
incardon's avatar
incardon committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	 * \param bc boundary conditions
	 *
	 */
	grid_dist_id_iterator_dec_skin(Decomposition & dec,
			                       const grid_sm<Decomposition::dims,void> & g_sm,
								   const Box<Decomposition::dims,size_t> & A,
								   const Box<Decomposition::dims,size_t> & B,
								   const size_t (& bc)[Decomposition::dims])
	:grid_skin_iterator_bc<Decomposition::dims>(g_sm,A,B,bc),a_its_p(0),g_c(0)
	{
		// From the decomposition construct gdb_ext
		create_gdb_ext<Decomposition::dims,Decomposition>(gdb_ext,dec,g_sm.getSize(),dec.getDomain(),spacing);

		// This iterato only work if A is contained into B
		if (A.isContained(B) == false)
			std::cout << __FILE__ << ":" << __LINE__ << ", Error Box A must be contained into box B" << std::endl;

		// construct sub_iterators
		construct_sub_it();

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

181
	//! Destructor
incardon's avatar
incardon committed
182 183 184 185 186 187
	~grid_dist_id_iterator_dec_skin()
	{
	}

	/*! \brief Get the next element
	 *
188
	 * \return itself
incardon's avatar
incardon committed
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 216 217 218 219 220 221 222 223 224 225 226
	 *
	 */
	inline grid_dist_id_iterator_dec_skin<Decomposition> & operator++()
	{
		++a_it;

		// check if a_it is at the end

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

			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
	 *
227 228 229
	 * \param i dimension
	 *
	 * \return the spacing
incardon's avatar
incardon committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	 *
	 */
	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()
	{
		const grid_dist_key_dx<Decomposition::dims> k = get_int();

		// 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;
	}

	/*! \brief Copy operator=
	*
	* \param tmp iterator to copy
	*
262 263
	* \return itself
	*
incardon's avatar
incardon committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	*/
	grid_dist_id_iterator_dec_skin<Decomposition> & operator=(const grid_dist_id_iterator_dec_skin<Decomposition> & tmp)
	{
		a_its_p = tmp.a_its_p;
		g_c = tmp.g_c;
		gdb_ext = tmp.gdb_ext;
		a_its = tmp.a_its;

		for (size_t i = 0 ; i < Decomposition::dims ; i++)
			spacing[i] = tmp.spacing[i];

		a_it.reinitialize(tmp.a_it);

		return *this;
	}

	/*! \brief Copy operator=
	*
	* \param tmp iterator to copy
	*
284 285
	* \return itself
	*
incardon's avatar
incardon committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	*/
	grid_dist_id_iterator_dec_skin<Decomposition> & operator=(grid_dist_id_iterator_dec_skin<Decomposition> && tmp)
	{
		a_its_p = tmp.a_its_p;
		g_c = tmp.g_c;
		gdb_ext = tmp.gdb_ext;
		a_its = tmp.a_its;

		for (size_t i = 0 ; i < Decomposition::dims ; i++)
			spacing[i] = tmp.spacing[i];

		a_it.reinitialize(tmp.a_it);

		return *this;
	}
};


#endif /* SRC_GRID_ITERATORS_GRID_DIST_ID_ITERATOR_DEC_SKIN_HPP_ */