grid_dist_amr_unit_tests.cpp 7.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 18 19 20 21 22
/*
 * grid_dist_amr_dist_unit_tests.cpp
 *
 *  Created on: Sep 21, 2017
 *      Author: i-bird
 */


#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include "grid_dist_amr.hpp"

BOOST_AUTO_TEST_SUITE( grid_dist_amr_test )

/*! \brief Coarsest levels of the grid
 *
 * \param domain Simulation domain
 * \param coars_g coarsest grid resolution
 * \param n_lvl number of levels
 *
 */
incardon's avatar
incardon committed
23 24
template<typename grid_amr>
void Test3D_amr_create_levels(grid_amr & amr_g, Box<3,float> & domain, size_t coars_g, size_t n_lvl)
incardon's avatar
incardon committed
25 26 27
{
	size_t g_sz[3] = {coars_g,coars_g,coars_g};

incardon's avatar
incardon committed
28
	size_t tot_c = (coars_g - 1)*(coars_g - 1)*(coars_g - 1);
incardon's avatar
incardon committed
29
	size_t correct_result = 0;
incardon's avatar
incardon committed
30
	size_t correct_result_cell = 0;
incardon's avatar
incardon committed
31 32
	size_t fact = 1;

33
	for (size_t i = 0 ; i < n_lvl ; i++)
incardon's avatar
incardon committed
34
	{
35
		correct_result += coars_g*coars_g*coars_g;
incardon's avatar
incardon committed
36
		correct_result_cell += tot_c*fact;
37
		coars_g = 2*(coars_g - 1) + 1;
incardon's avatar
incardon committed
38 39 40
		fact *= 8;
	}

incardon's avatar
incardon committed
41 42
	amr_g.initLevels(n_lvl,g_sz);

incardon's avatar
incardon committed
43

incardon's avatar
incardon committed
44 45 46
	for (size_t i = 0 ; i < amr_g.getNLvl() ; i++)
	{
		// Fill the AMR with something
incardon's avatar
incardon committed
47

incardon's avatar
incardon committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		size_t count = 0;

		auto it = amr_g.getGridIterator(i);

		while (it.isNext())
		{
			auto key = it.get_dist();
			auto akey = amr_g.getAMRKey(i,key);

			amr_g.template insert<0>(akey) = 3.0;

			count++;

			++it;
		}
	}
incardon's avatar
incardon committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	// Iterate across all the levels initialized
	auto it = amr_g.getDomainIterator();

	size_t count = 0;

	while (it.isNext())
	{
		count++;

		++it;
	}

	Vcluster & v_cl = create_vcluster();

	v_cl.sum(count);
	v_cl.execute();

82
	BOOST_REQUIRE_EQUAL(count,correct_result);
incardon's avatar
incardon committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

	auto itc = amr_g.getDomainIteratorCells();

	size_t count_c = 0;

	while (itc.isNext())
	{
		count_c++;

		++itc;
	}

	v_cl.sum(count_c);
	v_cl.execute();

incardon's avatar
incardon committed
98 99 100 101 102 103 104 105 106 107 108
	auto it_level = amr_g.getDomainIteratorCells(3);

	while (it_level.isNext())
	{
		auto key = it_level.get();

		amr_g.template get<0>(3,key);

		++it_level;
	}

incardon's avatar
incardon committed
109
	BOOST_REQUIRE_EQUAL(count_c,correct_result_cell);
incardon's avatar
incardon committed
110 111
}

incardon's avatar
incardon committed
112 113


incardon's avatar
incardon committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127
template<unsigned int dim>
inline bool gr_is_inside(const grid_key_dx<dim> & key, const size_t (& sz)[dim])
{
	for (size_t i = 0 ; i < dim ; i++)
	{
		if (key.get(i) >= (long int)sz[i] || key.get(i) < 0)
		{
			return false;
		}
	}

	return true;
}

incardon's avatar
incardon committed
128 129
template <typename grid>
void Test3D_amr_child_parent_get(grid & amr_g, Box<3,float> & domain, size_t coars_g, size_t n_lvl)
incardon's avatar
incardon committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
	const int x = 0;
	const int y = 1;
	const int z = 2;

	size_t g_sz[3] = {coars_g,coars_g,coars_g};

	size_t tot = coars_g*coars_g*coars_g;
	size_t correct_result = 0;
	size_t fact = 1;

	for (size_t i = 0 ; i <  n_lvl ; i++)
	{
		correct_result += tot*fact;
		fact *= 8;
	}

incardon's avatar
incardon committed
147
	amr_g.initLevels(n_lvl,g_sz);
incardon's avatar
incardon committed
148

incardon's avatar
incardon committed
149
	//////// Add something /////
incardon's avatar
incardon committed
150

incardon's avatar
incardon committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	for (size_t i = 0 ; i < amr_g.getNLvl() ; i++)
	{
		// Fill the AMR with something

		size_t count = 0;

		auto it = amr_g.getGridIterator(i);

		while (it.isNext())
		{
			auto key = it.get_dist();
			auto akey = amr_g.getAMRKey(i,key);

			amr_g.template insert<0>(akey) = 3.0;

			count++;

			++it;
		}
	}

	////////////////////////////
incardon's avatar
incardon committed
173

174
	std::string test = amr_g.getSpacing(0).toString();
incardon's avatar
incardon committed
175

incardon's avatar
incardon committed
176 177 178 179 180 181 182 183
	// Iterate across all the levels initialized
	auto it = amr_g.getDomainIterator();

	while (it.isNext())
	{
		auto key = it.get();
		auto gkey = it.getGKey();

incardon's avatar
incardon committed
184 185 186
		amr_g.template insert<0>(key) = gkey.get(0);
		amr_g.template insert<1>(key) = gkey.get(1);
		amr_g.template insert<2>(key) = gkey.get(2);
incardon's avatar
incardon committed
187 188 189 190

		++it;
	}

incardon's avatar
incardon committed
191
	amr_g.template ghost_get<0,1,2>();
incardon's avatar
incardon committed
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
	amr_g.write("amr_write_test");

	// now we check that move space work

	auto it2 = amr_g.getDomainIterator();

	bool match = true;
	while (it2.isNext())
	{
		auto key = it2.get();
		auto gkey = it2.getGKey();

		auto key_px = key.moveSpace(x,1);
		auto key_gpx = amr_g.getGKey(key_px);

		auto key_mx = key.moveSpace(x,-1);
		auto key_gmx = amr_g.getGKey(key_mx);

		auto key_py = key.moveSpace(y,1);
		auto key_gpy = amr_g.getGKey(key_py);

		auto key_my = key.moveSpace(y,-1);
		auto key_gmy = amr_g.getGKey(key_my);

		auto key_pz = key.moveSpace(z,1);
		auto key_gpz = amr_g.getGKey(key_pz);

		auto key_mz = key.moveSpace(z,-1);
		auto key_gmz = amr_g.getGKey(key_mz);

		if (gr_is_inside(key_gpx,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
224 225 226
			match &= amr_g.template get<0>(key_px) == gkey.get(0) + 1;
			match &= amr_g.template get<1>(key_px) == gkey.get(1);
			match &= amr_g.template get<2>(key_px) == gkey.get(2);
incardon's avatar
incardon committed
227 228 229 230
		}

		if (gr_is_inside(key_gmx,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
231 232 233
			match &= amr_g.template get<0>(key_mx) == gkey.get(0) - 1;
			match &= amr_g.template get<1>(key_mx) == gkey.get(1);
			match &= amr_g.template get<2>(key_mx) == gkey.get(2);
incardon's avatar
incardon committed
234 235 236 237
		}

		if (gr_is_inside(key_gpy,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
238 239 240
			match &= amr_g.template get<0>(key_py) == gkey.get(0);
			match &= amr_g.template get<1>(key_py) == gkey.get(1) + 1;
			match &= amr_g.template get<2>(key_py) == gkey.get(2);
incardon's avatar
incardon committed
241 242 243 244
		}

		if (gr_is_inside(key_gmy,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
245 246 247
			match &= amr_g.template get<0>(key_my) == gkey.get(0);
			match &= amr_g.template get<1>(key_my) == gkey.get(1) - 1;
			match &= amr_g.template get<2>(key_my) == gkey.get(2);
incardon's avatar
incardon committed
248 249 250 251
		}

		if (gr_is_inside(key_gpz,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
252 253 254
			match &= amr_g.template get<0>(key_pz) == gkey.get(0);
			match &= amr_g.template get<1>(key_pz) == gkey.get(1);
			match &= amr_g.template get<2>(key_pz) == gkey.get(2) + 1;
incardon's avatar
incardon committed
255 256 257 258
		}

		if (gr_is_inside(key_gmz,amr_g.getGridInfoVoid(it2.getLvl()).getSize()) == true)
		{
incardon's avatar
incardon committed
259 260 261
			match &= amr_g.template get<0>(key_mz) == gkey.get(0);
			match &= amr_g.template get<1>(key_mz) == gkey.get(1);
			match &= amr_g.template get<2>(key_mz) == gkey.get(2) - 1;
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
		}

		// Test to go to all the levels down

		size_t lvl = it2.getLvl();

		if (lvl < amr_g.getNLvl() - 1)
		{
			auto key_l1 = key;
			amr_g.moveLvlDw(key_l1);
			auto key_gl1 = amr_g.getGKey(key_l1);

			for (size_t s = 0 ; s < 3 ; s++)
			{
				match &= key_gl1.get(s) >> 1 == gkey.get(s);
				match &= amr_g.template get<0>(key_l1) == key_gl1.get(0);
				match &= amr_g.template get<1>(key_l1) == key_gl1.get(1);
				match &= amr_g.template get<2>(key_l1) == key_gl1.get(2);
			}
		}

		if (lvl != 0)
		{
			auto key_l1 = key;
			amr_g.moveLvlUp(key_l1);
			auto key_gl1 = amr_g.getGKey(key_l1);

			for (size_t s = 0 ; s < 3 ; s++)
			{
				match &= gkey.get(s) >> 1 == key_gl1.get(s);

				match &= amr_g.template get<0>(key_l1) == key_gl1.get(0);
				match &= amr_g.template get<1>(key_l1) == key_gl1.get(1);
				match &= amr_g.template get<2>(key_l1) == key_gl1.get(2);
			}
		}

		++it2;
	}

	BOOST_REQUIRE_EQUAL(match,true);
}

incardon's avatar
incardon committed
305 306
template <typename> struct Debug;

incardon's avatar
incardon committed
307 308 309 310 311 312 313 314
BOOST_AUTO_TEST_CASE( grid_dist_amr_get_child_test )
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	long int k = 16*16*16*create_vcluster().getProcessingUnits();
	k = std::pow(k, 1/3.);

incardon's avatar
incardon committed
315 316 317 318
	Ghost<3,long int> g(1);
	grid_dist_amr<3,float,aggregate<long int,long int,long int>> amr_g(domain3,g);

	Test3D_amr_child_parent_get(amr_g,domain3,k,4);
incardon's avatar
incardon committed
319 320
}

incardon's avatar
incardon committed
321 322 323 324 325 326 327 328
BOOST_AUTO_TEST_CASE( grid_dist_amr_test )
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	long int k = 16*16*16*create_vcluster().getProcessingUnits();
	k = std::pow(k, 1/3.);

incardon's avatar
incardon committed
329 330 331 332 333 334 335 336
	Ghost<3,float> g(0.05);
	grid_dist_amr<3,float,aggregate<float>> amr_g(domain3,g);

	Test3D_amr_create_levels(amr_g,domain3,k,4);

	sgrid_dist_amr<3,float,aggregate<float>> amr_g2(domain3,g);

	Test3D_amr_create_levels(amr_g2,domain3,k,4);
incardon's avatar
incardon committed
337 338
}

incardon's avatar
incardon committed
339

340 341 342 343 344 345 346
BOOST_AUTO_TEST_CASE( grid_dist_amr_get_child_test_low_res )
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

	long int k = 2;

incardon's avatar
incardon committed
347 348 349 350 351 352 353 354
	Ghost<3,long int> g(1);
	grid_dist_amr<3,float,aggregate<long int,long int,long int>> amr_g(domain3,g);

	Test3D_amr_child_parent_get(amr_g,domain3,k,4);

	sgrid_dist_amr<3,float,aggregate<long int,long int,long int>> amr_g2(domain3,g);

	Test3D_amr_child_parent_get(amr_g2,domain3,k,4);
355 356
}

incardon's avatar
incardon committed
357
BOOST_AUTO_TEST_SUITE_END()