grid_dist_amr.hpp 22.7 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
/*
 * grid_amr_dist.hpp
 *
 *  Created on: Sep 21, 2017
 *      Author: i-bird
 */

#ifndef AMR_GRID_AMR_DIST_HPP_
#define AMR_GRID_AMR_DIST_HPP_

#include "Grid/grid_dist_id.hpp"
#include "Amr/grid_dist_amr_key_iterator.hpp"

#define AMR_IMPL_TRIVIAL 1
#define AMR_IMPL_PATCHES 2
#define AMR_IMPL_OPENVDB 3

incardon's avatar
incardon committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
template<typename Decomposition, typename garray>
class Decomposition_encap
{
	Decomposition & dec;
	garray & gd_array;

public:

	Decomposition_encap(Decomposition & dec, garray & gd_array)
	:dec(dec),gd_array(gd_array)
	{}

	Decomposition & internal_dec() const
	{
		return dec;
	}

	/*! \brief Start decomposition
	 *
	 */
	void decompose()
	{
		dec.decompose();
incardon's avatar
incardon committed
41

incardon's avatar
incardon committed
42 43 44 45 46 47 48 49 50 51 52 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
		for(size_t i = 0 ; i < gd_array.size() ; i++)
		{
			Ghost<Decomposition::dims,typename Decomposition::stype> gold = gd_array.get(i).getDecomposition().getGhost();
			gd_array.get(i).getDecomposition() = dec.duplicate(gold);
		}
	}

	/*! \brief Refine the decomposition, available only for ParMetis distribution, for Metis it is a null call
	 *
	 * \param ts number of time step from the previous load balancing
	 *
	 */
	void refine(size_t ts)
	{
		dec.refine();

		for(size_t i = 0 ; i < gd_array.size() ; i++)
		{
			Ghost<Decomposition::dims,typename Decomposition::stype> gold = gd_array.get(i).getDecomposition().getGhost();
			gd_array.get(i).getDecomposition() = dec.duplicate(gold);
		}
	}

	/*! \brief Refine the decomposition, available only for ParMetis distribution, for Metis it is a null call
	 *
	 * \param ts number of time step from the previous load balancing
	 *
	 */
	void redecompose(size_t ts)
	{
		dec.redecompose();

		for(size_t i = 0 ; i < gd_array.size() ; i++)
		{
			Ghost<Decomposition::dims,typename Decomposition::stype> gold = gd_array.get(i).getDecomposition().getGhost();
			gd_array.get(i).getDecomposition() = dec.duplicate(gold);
		}
	}

	auto getDistribution() -> decltype(dec.getDistribution())
	{
		return dec.getDistribution();
	}

	Decomposition_encap<Decomposition,garray> operator=(const Decomposition_encap<Decomposition,garray> & de) const
	{
		for(size_t i = 0 ; i < gd_array.size() ; i++)
incardon's avatar
incardon committed
89
		{gd_array.get(i).getDecomposition() = de.gd_array.get(i).getDecomposition();}
incardon's avatar
incardon committed
90 91 92 93 94 95 96 97 98

		return *this;
	}

	bool write(std::string output) const
	{
		return dec.write(output);
	}
};
incardon's avatar
incardon committed
99

incardon's avatar
incardon committed
100 101 102 103 104 105
template<unsigned int dim, typename St, typename T, unsigned int impl=AMR_IMPL_TRIVIAL ,typename Decomposition = CartDecomposition<dim,St>,typename Memory=HeapMemory , typename device_grid=grid_cpu<dim,T> >
class grid_dist_amr
{

};

incardon's avatar
incardon committed
106 107 108 109 110 111 112 113 114 115 116 117 118
/*! \brief It contain the offset necessary to move to coarser and finer level grids
 *
 */
template<unsigned int dim>
struct offset_mv
{
	//! offset to move up on an upper grid (coarse)
	Point<dim,long int> up;

	//! offset to move on the lower grid (finer)
	Point<dim,long int> dw;
};

incardon's avatar
incardon committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/*! \brief AMR Adaptive Multi Resolution Grid
 *
 * \tparam dim Dimensionality
 * \tparam St type of space
 * \tparam T what each point of the grid store
 * \tparam Decomposition type of decomposition
 *
 */
template<unsigned int dim, typename St, typename T,typename Decomposition,typename Memory, typename device_grid >
class grid_dist_amr<dim,St,T,AMR_IMPL_TRIVIAL,Decomposition,Memory,device_grid>
{
	//! Simulation domain
	Box<dim,St> domain;

	//! Ghost integer
	Ghost<dim,long int> g_int;

incardon's avatar
incardon committed
136 137 138
	//! Boundary conditions of the structure
	periodicity<dim> bc;

incardon's avatar
incardon committed
139
	//! array of grids
incardon's avatar
incardon committed
140 141 142 143 144 145
	//
	openfpm::vector<grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>,
								 HeapMemory,
								 typename memory_traits_lin<grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>>::type,
								 memory_traits_lin,
								 openfpm::grow_policy_identity,STD_VECTOR> gd_array;
incardon's avatar
incardon committed
146

incardon's avatar
incardon committed
147
	//! Type of structure sub-grid iterator
incardon's avatar
incardon committed
148 149
	typedef decltype(device_grid::type_of_subiterator()) device_sub_it;

incardon's avatar
incardon committed
150
	//! Type of structure for the grid iterator
151 152 153
	typedef decltype(device_grid::type_of_iterator()) device_it;

	//! Domain iterator for each distributed grid
incardon's avatar
incardon committed
154
	openfpm::vector<grid_dist_iterator<dim,device_grid,device_sub_it,FREE>> git;
incardon's avatar
incardon committed
155

156 157 158
	//! Domain and ghost iterator for each distributed grid
	openfpm::vector<grid_dist_iterator<dim,device_grid,device_it,FIXED>> git_g;

incardon's avatar
incardon committed
159 160
	//! Iterator for each distributed grid
	openfpm::vector<grid_dist_iterator_sub<dim,device_grid>> git_sub;
incardon's avatar
incardon committed
161

incardon's avatar
incardon committed
162 163 164
	//! Moving offsets
	openfpm::vector<openfpm::vector<offset_mv<dim>>> mv_off;

incardon's avatar
incardon committed
165
	//! background level
incardon's avatar
incardon committed
166 167
	T bck;

incardon's avatar
incardon committed
168
	/*! \brief Initialize the others levels
incardon's avatar
incardon committed
169
	 *
incardon's avatar
incardon committed
170
	 * \param n_grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>lvl number of levels
incardon's avatar
incardon committed
171
	 * \param g_sz_lvl grid size on each level
incardon's avatar
incardon committed
172 173
	 *
	 */
incardon's avatar
incardon committed
174
	void initialize_other(size_t n_lvl, size_t (& g_sz_lvl)[dim])
incardon's avatar
incardon committed
175 176 177
	{
		for (size_t i = 0; i < n_lvl - 1 ; i++)
		{
incardon's avatar
incardon committed
178
			for (size_t j = 0 ; j < dim ; j++)
incardon's avatar
incardon committed
179
			{g_sz_lvl[j] = (g_sz_lvl[j]-1)*2 + 1;}
incardon's avatar
incardon committed
180 181

			gd_array.add(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>(gd_array.get(0).getDecomposition(),g_sz_lvl,g_int));
incardon's avatar
incardon committed
182
			gd_array.last().setBackgroundValue(bck);
incardon's avatar
incardon committed
183 184 185 186

			gd_array.last().getDecomposition().free_geo_cell();
			gd_array.last().getDecomposition().getDistribution().destroy_internal_graph();
			gd_array.last().getDecomposition().free_fines();
incardon's avatar
incardon committed
187 188
		}

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 227 228 229 230 231 232 233 234 235 236 237
		recalculate_mvoff();
	}

public:


	/*! \brief Constructor
	 *
	 * \param domain Simulation domain
	 * \param g ghost extension
	 *
	 */
	grid_dist_amr(const Box<dim,St> & domain, const Ghost<dim,long int> & g)
	:domain(domain),g_int(g)
	{
		// set boundary consitions to non periodic

		for (size_t i = 0; i < dim ; i++)
		{bc.bc[i] = NON_PERIODIC;}
	}

	/*! \brief Constructor
	 *
	 * \param domain Simulation domain
	 * \param g ghost extension
	 * \param bc boundary conditions
	 *
	 */
	grid_dist_amr(const Box<dim,St> & domain, const Ghost<dim,long int> & g, periodicity<dim> & bc)
	:domain(domain),g_int(g),bc(bc)
	{
	}

	/*! \brief Initialize the amr grid
	 *
	 * \param dec Decomposition (this parameter is useful in case we want to constrain the AMR to an external decomposition)
	 * \param n_lvl maximum number of levels (0 mean no additional levels)
	 * \param g_sz coarsest grid size on each direction
	 *
	 */
	void initLevels(const Decomposition & dec, size_t n_lvl,const size_t (& g_sz)[dim])
	{
		size_t g_sz_lvl[dim];

		for (size_t i = 0; i < dim ; i++)
		{g_sz_lvl[i] = g_sz[i];}

		// Add the coarse level
		gd_array.add(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>(dec,g_sz,g_int));
incardon's avatar
incardon committed
238
		gd_array.last().setBackgroundValue(bck);
incardon's avatar
incardon committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

		initialize_other(n_lvl,g_sz_lvl);
	}

	/*! \brief Initialize the amr grid
	 *
	 * \param dec Decomposition (this parameter is useful in case we want to constrain the AMR to an external decomposition)
	 * \param n_lvl maximum number of levels (0 mean no additional levels)
	 * \param g_sz coarsest grid size on each direction
	 *
	 */
	void initLevels(const Decomposition_encap<Decomposition,decltype(gd_array)> & dec, size_t n_lvl,const size_t (& g_sz)[dim])
	{
		initLevels(dec.internal_dec(),n_lvl,g_sz);
	}

	/*! \brief Recalculate the offset array for the moveLvlUp and moveLvlDw
	 *
	 *
	 *
	 */
	void recalculate_mvoff()
	{
incardon's avatar
incardon committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
		// Here we calculate the offset to move one level up and one level down
		// in global coordinated moving one level up is multiply the coordinates by 2
		// and moving one level down is dividing by 2. In local coordinates is the same
		// with the exception that because of the decomposition you can have an offset
		// look at the picture below
		//
		// (-1)  (0)
		//  * |   *     *    coarse level
		//  * |*  *  *  *    finer level
		//    |(0)(1)
		//
		//  Line of the decomposition
		//
		// The coarse level point 0 in local coordinates converted to the finer level is not
		// just 2*0 = 0 but is 2*(0) + 1 so a formula like 2*x+offset is required. here we calculate
		// these offset. In the case of moving from finer to coarse is the same the formula is
		// Integer_round(x+1)/2 - 1
		//
		mv_off.resize(gd_array.size());

		for (size_t i = 1 ; i < gd_array.size() ; i++)
		{
			auto & g_box_c = gd_array.get(i-1).getLocalGridsInfo();
			auto & g_box_f = gd_array.get(i).getLocalGridsInfo();

#ifdef SE_CLASS1

			if (g_box_c.size() != g_box_f.size())
			{
				std::cerr << __FILE__ << ":" << __LINE__ << " error it seem that the AMR construction between level " <<
						i << " and " << i-1 << " is inconsistent" << std::endl;
			}

#endif

			mv_off.get(i-1).resize(g_box_f.size());
			mv_off.get(i).resize(g_box_f.size());

			for (size_t j = 0 ; j < g_box_f.size() ; j++)
			{
				for (size_t s = 0 ; s < dim ; s++)
				{
					size_t d_orig_c = g_box_c.get(j).origin.get(s);
					size_t d_orig_f = g_box_f.get(j).origin.get(s);

					mv_off.get(i-1).get(j).dw.get(s) = d_orig_c*2 - d_orig_f;
					mv_off.get(i).get(j).up.get(s) = d_orig_c*2 - d_orig_f;
				}
			}
incardon's avatar
incardon committed
311 312 313
		}
	}

incardon's avatar
incardon committed
314
	/*! \brief Initialize the amr grid
incardon's avatar
incardon committed
315
	 *
incardon's avatar
incardon committed
316 317 318
	 * \param n_lvl maximum number of levels (0 mean no additional levels)
	 * \param g_sz coarsest grid size on each direction
	 * \param opt options
incardon's avatar
incardon committed
319 320
	 *
	 */
incardon's avatar
incardon committed
321
	void initLevels(size_t n_lvl,const size_t (& g_sz)[dim], size_t opt = 0)
incardon's avatar
incardon committed
322
	{
incardon's avatar
incardon committed
323
		size_t g_sz_lvl[dim];
incardon's avatar
incardon committed
324 325

		for (size_t i = 0; i < dim ; i++)
incardon's avatar
incardon committed
326 327 328 329 330 331
		{g_sz_lvl[i] = g_sz[i];}

		// Add the coarse level
		gd_array.add(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>(g_sz,domain,g_int,bc,opt));

		initialize_other(n_lvl,g_sz_lvl);
incardon's avatar
incardon committed
332 333
	}

incardon's avatar
incardon committed
334
	/*! \brief Add the computation cost on the decomposition using a resolution function
incardon's avatar
incardon committed
335
	 *
incardon's avatar
incardon committed
336 337 338 339
	 *
	 * \param md Model to use
	 * \param ts It is an optional parameter approximately should be the number of ghost get between two
	 *           rebalancing at first decomposition this number can be ignored (default = 1) because not used
incardon's avatar
incardon committed
340 341
	 *
	 */
incardon's avatar
incardon committed
342
	template <typename Model>inline void addComputationCosts(Model md=Model(), size_t ts = 1)
incardon's avatar
incardon committed
343
	{
incardon's avatar
incardon committed
344
		gd_array.get(0).addComputationCosts(md,ts);
incardon's avatar
incardon committed
345 346
	}

incardon's avatar
incardon committed
347
	/*! \brief Get the object that store the information about the decomposition
incardon's avatar
incardon committed
348
	 *
incardon's avatar
incardon committed
349
	 * \return the decomposition object
incardon's avatar
incardon committed
350 351
	 *
	 */
incardon's avatar
incardon committed
352
	Decomposition_encap<Decomposition,decltype(gd_array)> getDecomposition()
incardon's avatar
incardon committed
353
	{
incardon's avatar
incardon committed
354
		Decomposition_encap<Decomposition,decltype(gd_array)> tmp(gd_array.get(0).getDecomposition(),gd_array);
incardon's avatar
incardon committed
355

incardon's avatar
incardon committed
356
		return tmp;
incardon's avatar
incardon committed
357 358
	}

incardon's avatar
incardon committed
359
	/*! \brief Get the underlying grid level
incardon's avatar
incardon committed
360
	 *
incardon's avatar
incardon committed
361 362 363
	 * \param lvl level
	 *
	 * \return the grid level
incardon's avatar
incardon committed
364 365
	 *
	 */
incardon's avatar
incardon committed
366
	grid_dist_id<dim,St,T,Decomposition,Memory,device_grid> & getLevel(size_t lvl)
incardon's avatar
incardon committed
367
	{
incardon's avatar
incardon committed
368
		return gd_array.get(lvl);
incardon's avatar
incardon committed
369 370
	}

371

incardon's avatar
incardon committed
372 373 374 375
	grid_dist_amr_key_iterator<dim,device_grid,
							   decltype(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>::type_of_subiterator()),
							   decltype(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>::type_of_subiterator()) >
	getDomainIteratorCells()
incardon's avatar
incardon committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	{
		git_sub.clear();

		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
			grid_key_dx<dim> start;
			grid_key_dx<dim> stop;

			for (size_t j = 0 ; j < dim ; j++)
			{
				start.set_d(j,0);
				stop.set_d(j,getGridInfoVoid(i).size(j) - 2);
			}

			git_sub.add(gd_array.get(i).getSubDomainIterator(start,stop));
		}

incardon's avatar
incardon committed
393 394 395
		return grid_dist_amr_key_iterator<dim,device_grid,
											decltype(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>::type_of_subiterator()),
											decltype(grid_dist_id<dim,St,T,Decomposition,Memory,device_grid>::type_of_subiterator())>(git_sub);
incardon's avatar
incardon committed
396 397
	}

398 399 400 401 402 403 404 405 406 407 408 409 410 411
	grid_dist_iterator_sub<dim,device_grid> getDomainIteratorCells(size_t lvl)
	{
		grid_key_dx<dim> start;
		grid_key_dx<dim> stop;

		for (size_t j = 0 ; j < dim ; j++)
		{
			start.set_d(j,0);
			stop.set_d(j,getGridInfoVoid(lvl).size(j) - 2);
		}

		return gd_array.get(lvl).getSubDomainIterator(start,stop);
	}

412 413 414 415 416
	/*! \brief Get an iterator to the grid
	 *
	 * \return an iterator to the grid
	 *
	 */
incardon's avatar
incardon committed
417
	auto getGridGhostIterator(size_t lvl) -> decltype(gd_array.get(lvl).getGridGhostIterator(grid_key_dx<dim>(),grid_key_dx<dim>()))
418 419 420 421 422 423 424
	{
		grid_key_dx<dim> key_start;
		grid_key_dx<dim> key_stop;

		for (size_t i = 0 ; i < dim ; i++)
		{
			key_start.set_d(i,g_int.getLow(i));
incardon's avatar
incardon committed
425
			key_stop.set_d(i,g_int.getHigh(i) + getGridInfoVoid(lvl).size(i) -1);
426 427
		}

incardon's avatar
incardon committed
428
		return gd_array.get(lvl).getGridGhostIterator(key_start,key_stop);
429 430
	}

incardon's avatar
incardon committed
431 432 433 434 435 436 437 438 439 440
	/*! \brief Get an iterator to the grid
	 *
	 * \return an iterator to the grid
	 *
	 */
	auto getGridIterator(size_t lvl) -> decltype(gd_array.get(lvl).getGridIterator())
	{
		return gd_array.get(lvl).getGridIterator();
	}

incardon's avatar
incardon committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	/*! \brief Get an iterator to the grid
	 *
	 * \return an iterator to the grid
	 *
	 */
	auto getGridIteratorCells(size_t lvl) -> decltype(gd_array.get(lvl).getGridIterator())
	{
		grid_key_dx<dim> start;
		grid_key_dx<dim> stop;

		for (size_t j = 0 ; j < dim ; j++)
		{
			start.set_d(j,0);
			stop.set_d(j,getGridInfoVoid(lvl).size(j) - 2);
		}

		return gd_array.get(lvl).getGridIterator(start,stop);
	}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	/*! \brief return an iterator over the level lvl
	 *
	 * \param lvl level
	 *
	 * \return an iterator over the level lvl selected
	 *
	 */
	grid_dist_iterator<dim,device_grid,decltype(device_grid::type_of_subiterator()),FREE>
	getDomainIterator(size_t lvl) const
	{
		return gd_array.get(lvl).getDomainIterator();
	}

    /*! \brief return an iterator over the level lvl
     *
     * \param lvl level
     *
     * \return an iterator over the level lvl selected
     *
     */
    grid_dist_iterator<dim,device_grid,
482
	decltype(device_grid::type_of_iterator()),
483 484 485 486 487 488
    FIXED>
    getDomainGhostIterator(size_t lvl) const
    {
            return gd_array.get(lvl).getDomainGhostIterator();
    }

incardon's avatar
incardon committed
489 490 491 492 493
	/*! \brief Get domain iterator
	 *
	 * \return an iterator over all the grid levels
	 *
	 */
incardon's avatar
incardon committed
494 495
	grid_dist_amr_key_iterator<dim,device_grid, decltype(device_grid::type_of_subiterator())>
	getDomainIterator()
incardon's avatar
incardon committed
496 497 498 499 500 501 502 503
	{
		git.clear();

		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
			git.add(gd_array.get(i).getDomainIterator());
		}

incardon's avatar
incardon committed
504
		return grid_dist_amr_key_iterator<dim,device_grid,decltype(device_grid::type_of_subiterator())>(git);
incardon's avatar
incardon committed
505
	}
incardon's avatar
incardon committed
506

507 508 509 510 511
    /*! \brief Get domain iterator
     *
     * \return an iterator over all the grid levels
     *
     */
512 513
    grid_dist_amr_key_iterator<dim,device_grid, decltype(device_grid::type_of_iterator()),
    		                   grid_dist_iterator<dim,device_grid,decltype(device_grid::type_of_iterator()),FIXED>>
514 515
    getDomainGhostIterator()
    {
516
            git_g.clear();
517 518 519

            for (size_t i = 0 ; i < gd_array.size() ; i++)
            {
520
                    git_g.add(gd_array.get(i).getDomainGhostIterator());
521 522
            }

523 524
            return grid_dist_amr_key_iterator<dim,device_grid,decltype(device_grid::type_of_iterator()),
            		                          grid_dist_iterator<dim,device_grid,decltype(device_grid::type_of_iterator()),FIXED>>(git_g);
525
    }
incardon's avatar
incardon committed
526

incardon's avatar
incardon committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
	template <unsigned int p>inline auto get(const grid_dist_amr_key<dim> & v1) const -> typename std::add_lvalue_reference<decltype(gd_array.get(v1.getLvl()).template get<p>(v1.getKey()))>::type
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(v1.getLvl()).template get<p>(v1.getKey());
	}

	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
	template <unsigned int p>inline auto get(const grid_dist_amr_key<dim> & v1) -> typename std::add_lvalue_reference<decltype(gd_array.get(v1.getLvl()).template get<p>(v1.getKey()))>::type
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(v1.getLvl()).template get<p>(v1.getKey());
	}

incardon's avatar
incardon committed
559 560 561 562 563 564 565 566 567

	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
incardon's avatar
incardon committed
568
	template <unsigned int p>inline auto get(size_t lvl, const grid_dist_key_dx<dim> & v1) const -> typename std::add_lvalue_reference<decltype(gd_array.get(lvl).template get<p>(v1))>::type
incardon's avatar
incardon committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(lvl).template get<p>(v1);
	}

	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
incardon's avatar
incardon committed
584
	template <unsigned int p>inline auto get(size_t lvl, const grid_dist_key_dx<dim> & v1) -> typename std::add_lvalue_reference<decltype(gd_array.get(lvl).template get<p>(v1))>::type
incardon's avatar
incardon committed
585 586 587 588 589 590 591
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(lvl).template get<p>(v1);
	}

incardon's avatar
incardon committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	//////////////////// Insert functions


	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
	template <unsigned int p>
	inline auto insert(const grid_dist_amr_key<dim> & v1)
	-> typename std::add_lvalue_reference<decltype(gd_array.get(v1.getLvl()).template insert<p>(v1.getKey()))>::type
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(v1.getLvl()).template insert<p>(v1.getKey());
	}



	/*! \brief Get the reference of the selected element
	 *
	 * \tparam p property to get (is an integer)
	 * \param v1 grid_key that identify the element in the grid
	 *
	 * \return the selected element
	 *
	 */
incardon's avatar
incardon committed
623 624
	template <unsigned int p>inline auto insert(size_t lvl, const grid_dist_key_dx<dim> & v1)
	-> typename std::add_lvalue_reference<decltype(gd_array.get(lvl).template insert<p>(v1))>::type
incardon's avatar
incardon committed
625 626 627 628 629 630 631 632 633
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(lvl).template insert<p>(v1);
	}

	//////////////////////////////////////

incardon's avatar
incardon committed
634 635 636 637 638 639 640 641 642 643 644
	/*! \brief Get the internal distributed grid
	 *
	 * \param lvl level
	 *
	 * \return the internal distributed grid
	 *
	 */
	grid_dist_id<dim,St,T,Decomposition,Memory,device_grid> & getDistGrid(size_t lvl)
	{
		return gd_array.get(lvl);
	}
incardon's avatar
incardon committed
645

incardon's avatar
incardon committed
646
	//////////////////// Remove functions
incardon's avatar
incardon committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

	/*! \brief Remove a grid point (this function make sense only in case of
	 *         sparse grid)
	 *
	 * \param v1 grid_key that identify the element in the AMR grid to eleminate
	 *
	 */
	inline void remove(const grid_dist_amr_key<dim> & v1)
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(v1.getLvl()).remove(v1.getKey());
	}

	/*! \brief Remove a grid point (this function make sense only in case of
	 *         sparse grid)
	 *
	 * \param v1 grid_key that identify the element in the AMR grid to eleminate
	 *
	 */
	void remove(size_t lvl, const grid_dist_key_dx<dim> & v1)
	{
#ifdef SE_CLASS2
		check_valid(this,8);
#endif
		return gd_array.get(lvl).remove(v1);
	}

	//////////////////////////////////////

incardon's avatar
incardon committed
678 679 680 681 682 683 684 685 686
	/*! \brief It synchronize the ghost parts
	 *
	 * \tparam prp... Properties to synchronize
	 *
	 */
	template<int... prp> void ghost_get()
	{
		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
incardon's avatar
incardon committed
687
			gd_array.get(i).template ghost_get<prp...>();
incardon's avatar
incardon committed
688 689 690
		}
	}

incardon's avatar
incardon committed
691 692 693 694 695 696 697 698 699 700 701 702 703
	/*! \brief It move all the grid parts that do not belong to the local processor to the respective processor
	 *
	 */
	void map(size_t opt = 0)
	{
		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
			gd_array.get(i).map();
		}

		recalculate_mvoff();
	}

incardon's avatar
incardon committed
704 705 706 707 708 709 710 711 712
	/*! \brief Apply the ghost put
	 *
	 * \tparam prp... Properties to apply ghost put
	 *
	 */
	template<template<typename,typename> class op,int... prp> void ghost_put()
	{
		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
incardon's avatar
incardon committed
713
			gd_array.get(i).template ghost_put<op,prp...>();
incardon's avatar
incardon committed
714 715 716
		}
	}

incardon's avatar
incardon committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
	/*! \brief Return the number of inserted points on a particular level
	 *
	 * \return the number of inserted points
	 *
	 */
	size_t size_inserted(size_t lvl)
	{
		return gd_array.get(lvl).size_local_inserted();
	}

	/*! \brief set the background value
	 *
	 * You can use this function make sense in case of sparse in case of dense
	 * it does nothing
	 *
	 */
	void setBackgroundValue(T & bv)
	{
		for (size_t i = 0 ; i < getNLvl() ; i++)
		{gd_array.get(i).setBackgroundValue(bv);}
incardon's avatar
incardon committed
737 738

		meta_copy<T>::meta_copy_(bv,bck);
incardon's avatar
incardon committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752
	}

	/*! \brief delete all the points in the grid
	 *
	 * In case of sparse grid in delete all the inserted points, in case
	 * of dense it does nothing
	 *
	 */
	void clear()
	{
		for (size_t i = 0 ; i < getNLvl() ; i++)
		{gd_array.get(i).clear();}
	}

incardon's avatar
incardon committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	/*! \brief Get an object containing the grid informations for a specific level
	 *
	 * \param lvl level
	 *
	 * \return an information object about this grid
	 *
	 */
	const grid_sm<dim,void> & getGridInfoVoid(size_t lvl) const
	{
		return gd_array.get(lvl).getGridInfoVoid();
	}

	/*! \brief Return the maximum number of levels in the AMR struct
	 *
	 * \return the number of levels
	 *
	 */
	size_t getNLvl()
	{
		return gd_array.size();
	}

	/*! \brief Move down (to finer level) the key
	 *
	 * \param key multi-resolution AMR key
	 *
	 */
	void moveLvlDw(grid_dist_amr_key<dim> & key)
	{
#ifdef SE_CLASS1

		if (key.getLvl() >= getNLvl() - 1)
		{std::cerr << __FILE__ << ":" << __LINE__ << " error: we are already at the last level, we cannot go one level down" << std::endl;}

#endif

		auto & key_ref = key.getKeyRef().getKeyRef();
		size_t lvl = key.getLvl();

		for (size_t i = 0 ; i < dim ; i++)
		{
			key_ref.set_d(i,(key_ref.get(i) << 1) + mv_off.get(key.getLvl()).get(key.getKeyRef().getSub()).dw.get(i) );
		}

		key.setLvl(lvl+1);
	}

incardon's avatar
incardon committed
800 801 802 803 804 805 806 807 808 809 810
	/*! \brief From a distributed key it return a AMR key that contain also the grid level
	 *
	 * \param lvl level
	 * \param key distributed key
	 *
	 */
	inline grid_dist_amr_key<dim> getAMRKey(size_t lvl, grid_dist_key_dx<dim> key)
	{
		return grid_dist_amr_key<dim>(lvl,key);
	}

incardon's avatar
incardon committed
811 812 813 814 815 816 817 818 819
	/*! \brief Move up (to coarser level) the key
	 *
	 * \param key multi-resolution AMR key
	 *
	 */
	void moveLvlUp(grid_dist_amr_key<dim> & key)
	{
#ifdef SE_CLASS1

incardon's avatar
incardon committed
820 821
		if (key.getLvl() == 0)
		{std::cerr << __FILE__ << ":" << __LINE__ << " error: we are already at the top level, we cannot go one level up" << std::endl;}
incardon's avatar
incardon committed
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

#endif

		auto & key_ref = key.getKeyRef().getKeyRef();
		size_t lvl = key.getLvl();

		for (size_t i = 0 ; i < dim ; i++)
		{
			key_ref.set_d(i,(key_ref.get(i) - mv_off.get(key.getLvl()).get(key.getKeyRef().getSub()).up.get(i)) >> 1);
		}

		key.setLvl(lvl-1);
	}

	/*! \brief Get the position on the grid in global coordinates
	 *
	 * \param v1 amr key
	 *
	 * \return the position in global coordinates
	 *
	 */
	grid_key_dx<dim> getGKey(const grid_dist_amr_key<dim> & v1)
	{
		return gd_array.get(v1.getLvl()).getGKey(v1.getKey());
	}

incardon's avatar
incardon committed
848 849 850 851 852 853 854 855 856 857 858 859
	/*! \brief return the spacing for the grid in the level lvl
	 *
	 * \param lvl level
	 *
	 * \return return the spacing
	 *
	 */
	Point<dim,St> getSpacing(size_t lvl)
	{
		return gd_array.get(lvl).getSpacing();
	}

incardon's avatar
incardon committed
860 861 862 863 864
	/*! \brief Write on vtk file
	 *
	 * \param output filename output
	 *
	 */
incardon's avatar
incardon committed
865
	bool write(std::string output, size_t opt = VTK_WRITER | FORMAT_ASCII )
incardon's avatar
incardon committed
866 867 868 869 870
	{
		bool ret = true;

		for (size_t i = 0 ; i < gd_array.size() ; i++)
		{
incardon's avatar
incardon committed
871
			ret &= gd_array.get(i).write(output + "_" + std::to_string(i),opt);
incardon's avatar
incardon committed
872 873 874 875
		}

		return ret;
	}
incardon's avatar
incardon committed
876 877
};

incardon's avatar
incardon committed
878 879
template<unsigned int dim, typename St, typename T>
using sgrid_dist_amr = grid_dist_amr<dim,St,T,AMR_IMPL_TRIVIAL,CartDecomposition<dim,St>,HeapMemory,sgrid_cpu<dim,T,HeapMemory>>;
incardon's avatar
incardon committed
880 881

#endif /* AMR_GRID_AMR_DIST_HPP_ */