CartDecomposition_unit_test.hpp 9.29 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4
#ifndef CARTDECOMPOSITION_UNIT_TEST_HPP
#define CARTDECOMPOSITION_UNIT_TEST_HPP

#include "CartDecomposition.hpp"
5
#include "util/mathutil.hpp"
incardon's avatar
incardon committed
6

7
BOOST_AUTO_TEST_SUITE (CartDecomposition_test)
incardon's avatar
incardon committed
8

9
#define SUB_UNIT_FACTOR 1024
incardon's avatar
incardon committed
10

11
void setComputationCosts(CartDecomposition<2, float> &dec, size_t n_v, Point<2, float> center, float radius, size_t weight_h, size_t weight_l)
incardon's avatar
incardon committed
12
{
13 14 15 16
	float radius2 = pow(radius, 2);
	float eq;

	// Position structure for the single vertex
17
	float pos[2];
18

19
	for (size_t i = 0; i < n_v; i++)
20 21 22 23 24 25 26 27 28 29 30 31 32 33
	{
		dec.getSubSubDomainPosition(i, pos);

		eq = pow((pos[0] - center.get(0)), 2) + pow((pos[1] - center.get(1)), 2);

		if (eq <= radius2)
			dec.setSubSubDomainComputationCost(i, weight_h);
		else
			dec.setSubSubDomainComputationCost(i, weight_l);
	}
}

void setComputationCosts3D(CartDecomposition<3, float> &dec, size_t n_v, Point<3, float> center, float radius, size_t weight_h, size_t weight_l)
{
34
	float radius2 = radius * radius;
35 36 37 38 39
	float eq;

	// Position structure for the single vertex
	float pos[3];

40
	for (size_t i = 0; i < n_v; i++)
41 42 43 44 45 46 47 48 49 50 51 52 53 54
	{
		dec.getSubSubDomainPosition(i, pos);

		eq = pow((pos[0] - center.get(0)), 2) + pow((pos[1] - center.get(1)), 2) + pow((pos[2] - center.get(2)), 2);

		if (eq <= radius2)
			dec.setSubSubDomainComputationCost(i, weight_h);
		else
			dec.setSubSubDomainComputationCost(i, weight_l);
	}
}



55
BOOST_AUTO_TEST_CASE( CartDecomposition_non_periodic_test)
56 57 58 59 60 61 62 63
{
	// Vcluster
	Vcluster & vcl = *global_v_cluster;

	// Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

	//! [Create CartDecomposition]
64
	CartDecomposition<3, float> dec(vcl);
65 66

	// Physical domain
67
	Box<3, float> box( { 0.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0 });
68 69 70 71 72 73 74 75
	size_t div[3];

	// Get the number of processor and calculate the number of sub-domain
	// for each processor (SUB_UNIT_FACTOR=64)
	size_t n_proc = vcl.getProcessingUnits();
	size_t n_sub = n_proc * SUB_UNIT_FACTOR;

	// Set the number of sub-domains on each dimension (in a scalable way)
76 77
	for (int i = 0; i < 3; i++)
	{	div[i] = openfpm::math::round_big_2(pow(n_sub,1.0/3));}
78 79

	// Define ghost
80
	Ghost<3, float> g(0.01);
81 82

	// Boundary conditions
83
	size_t bc[] = { NON_PERIODIC, NON_PERIODIC, NON_PERIODIC };
84

incardon's avatar
incardon committed
85
	// Decompose
86
	dec.setParameters(div,box,bc,g);
Pietro Incardona's avatar
Pietro Incardona committed
87
	dec.decompose();
incardon's avatar
incardon committed
88

89 90
	//! [Create CartDecomposition]

91
	// For each calculated ghost box
92
	for (size_t i = 0; i < dec.getNIGhostBox(); i++)
93 94 95 96 97 98 99 100 101 102 103 104
	{
		SpaceBox<3,float> b = dec.getIGhostBox(i);
		size_t proc = dec.getIGhostBoxProcessor(i);

		// sample one point inside the box
		Point<3,float> p = b.rnd();

		// Check that ghost_processorsID return that processor number
		const openfpm::vector<size_t> & pr = dec.template ghost_processorID<CartDecomposition<3,float>::processor_id>(p);

		bool found = false;

105
		for (size_t j = 0; j < pr.size(); j++)
106 107
		{
			if (pr.get(j) == proc)
108
			{	found = true; break;}
109 110 111 112 113 114 115 116 117
		}

		if (found == false)
		{
			const openfpm::vector<size_t> pr2 = dec.template ghost_processorID<CartDecomposition<3,float>::processor_id>(p);
		}

		BOOST_REQUIRE_EQUAL(found,true);
	}
incardon's avatar
incardon committed
118 119

	// Check the consistency
120

incardon's avatar
incardon committed
121 122
	bool val = dec.check_consistency();
	BOOST_REQUIRE_EQUAL(val,true);
123 124

	// We duplicate the decomposition
125
	CartDecomposition<3, float> dec2 = dec.duplicate();
incardon's avatar
incardon committed
126 127
	dec2.check_consistency();

128
	// check that dec and dec2 contain the same information
129 130
	bool ret = dec.is_equal(dec2);

incardon's avatar
incardon committed
131
	// We check if the two decomposition are equal
132
	BOOST_REQUIRE_EQUAL(ret,true);
incardon's avatar
incardon committed
133

134 135 136
	// We duplicate the decomposition redefining the ghost

	// Define ghost
137
	Ghost<3, float> g3(0.005);
138

139
	// We duplicate the decomposition redefining the ghost
140
	CartDecomposition<3, float> dec3 = dec.duplicate(g3);
141 142 143 144

	ret = dec3.check_consistency();
	BOOST_REQUIRE_EQUAL(ret,true);

145
	// Check that dec3 is equal to dec2 with the exception of the ghost part
146 147
	ret = dec3.is_equal_ng(dec2);
	BOOST_REQUIRE_EQUAL(ret,true);
incardon's avatar
incardon committed
148 149
}

150 151
BOOST_AUTO_TEST_CASE( CartDecomposition_periodic_test)
{
152 153 154
	// Vcluster
	Vcluster & vcl = *global_v_cluster;

155 156 157
	// Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

158
	//! [Create CartDecomposition]
159
	CartDecomposition<3, float> dec(vcl);
160 161

	// Physical domain
162
	Box<3, float> box( { 0.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0 });
163 164 165 166 167 168 169
	size_t div[3];

	// Get the number of processor and calculate the number of sub-domain
	// for each processor (SUB_UNIT_FACTOR=64)
	size_t n_proc = vcl.getProcessingUnits();
	size_t n_sub = n_proc * SUB_UNIT_FACTOR;

170
	// Set the number of sub-domains on each dimension (in a scalable way)
171 172
	for (int i = 0; i < 3; i++)
	{	div[i] = openfpm::math::round_big_2(pow(n_sub,1.0/3));}
173

174
	// Define ghost
175
	Ghost<3, float> g(0.01);
176 177

	// Boundary conditions
178
	size_t bc[] = { PERIODIC, PERIODIC, PERIODIC };
179 180 181

	// Decompose
	dec.setParameters(div,box,bc,g);
Pietro Incardona's avatar
Pietro Incardona committed
182
	dec.decompose();
183

184 185 186
	//! [Create CartDecomposition]

	// For each calculated ghost box
187
	for (size_t i = 0; i < dec.getNIGhostBox(); i++)
188 189 190
	{
		SpaceBox<3,float> b = dec.getIGhostBox(i);
		size_t proc = dec.getIGhostBoxProcessor(i);
191

192 193
		// sample one point inside the box
		Point<3,float> p = b.rnd();
194

195 196
		// Check that ghost_processorsID return that processor number
		const openfpm::vector<size_t> & pr = dec.template ghost_processorID<CartDecomposition<3,float>::processor_id>(p);
197

198
		bool found = false;
199

200
		for (size_t j = 0; j < pr.size(); j++)
201 202
		{
			if (pr.get(j) == proc)
203
			{	found = true; break;}
204
		}
205

206
		if (found == false)
207
		{
208
			const openfpm::vector<size_t> pr2 = dec.template ghost_processorID<CartDecomposition<3,float>::processor_id>(p);
209 210
		}

211 212
		BOOST_REQUIRE_EQUAL(found,true);
	}
213

214 215 216
	// Check the consistency
	bool val = dec.check_consistency();
	BOOST_REQUIRE_EQUAL(val,true);
217

218
	// We duplicate the decomposition
219
	CartDecomposition<3, float> dec2 = dec.duplicate();
220
	dec2.check_consistency();
221

222
	bool ret = dec.is_equal(dec2);
223

224 225
	// We check if the two decomposition are equal
	BOOST_REQUIRE_EQUAL(ret,true);
226

227
	// check that dec and dec2 contain the same information
228

229
	// We duplicate the decomposition redefining the ghost
230

231
	// Define ghost
232
	Ghost<3, float> g3(0.005);
233

234
	// We duplicate the decomposition refefining the ghost
235
	CartDecomposition<3, float> dec3 = dec.duplicate(g3);
236

237 238
	ret = dec3.check_consistency();
	BOOST_REQUIRE_EQUAL(ret,true);
239

240 241 242
	// Check that g3 is equal to dec2 with the exception of the ghost part
	ret = dec3.is_equal_ng(dec2);
	BOOST_REQUIRE_EQUAL(ret,true);
243 244
}

Pietro Incardona's avatar
Pietro Incardona committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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

////////////////////////// CartDecomposition extended

BOOST_AUTO_TEST_CASE( CartDecomposition_ext_non_periodic_test)
{
	// Vcluster
	Vcluster & vcl = *global_v_cluster;

	// Initialize the global VCluster
	init_global_v_cluster(&boost::unit_test::framework::master_test_suite().argc,&boost::unit_test::framework::master_test_suite().argv);

	//! [Create CartDecomposition]
	CartDecomposition<3,float> dec(vcl);

	// Physical domain
	Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});
	size_t div[3];

	// Get the number of processor and calculate the number of sub-domain
	// for each processor (SUB_UNIT_FACTOR=64)
	size_t n_proc = vcl.getProcessingUnits();
	size_t n_sub = n_proc * SUB_UNIT_FACTOR;

	// Set the number of sub-domains on each dimension (in a scalable way)
	for (int i = 0 ; i < 3 ; i++)
	{div[i] = openfpm::math::round_big_2(pow(n_sub,1.0/3));}

	// Define ghost
	Ghost<3,float> g(0.01);

	// Boundary conditions
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

	// Decompose
	dec.setParameters(div,box,bc,g);
	dec.decompose();

	//! [Extend CartDecomposition]

	Box<3,float> box_ext({-0.1,-0.1,-0.1},{1.1,1.1,1.1});

	// Use the old decomposition to extend on a bigger domain
287 288 289
	CartDecomposition_ext<3,float> dec_ext(vcl);

	dec_ext.setParameters(dec,g,box_ext);
Pietro Incardona's avatar
Pietro Incardona committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307

	//! [Extend CartDecomposition]

	// Check the new decomposition is fully contained in the new one, and there are
	// box not fully contained i the old box

	BOOST_REQUIRE_EQUAL(dec_ext.getNSubDomain(),dec.getNSubDomain());

	double volume = 0.0;
	for (size_t i = 0; i < dec_ext.getNSubDomain() ; i++)
	{
		volume += dec_ext.getSubDomain(i).getVolume();
		BOOST_REQUIRE_EQUAL(dec_ext.getSubDomain(i).isContained(dec.getSubDomain(i)),true);
	}

	vcl.sum(volume);
	vcl.execute();

308
	BOOST_REQUIRE_CLOSE(volume,1.728,0.0001);
Pietro Incardona's avatar
Pietro Incardona committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

	BOOST_REQUIRE_EQUAL(dec.getNNProcessors(),dec_ext.getNNProcessors());

	double volume_g = 0.0;
	double volume_ge = 0.0;
	for (size_t p = 0; p < dec.getNNProcessors(); p++)
	{
		BOOST_REQUIRE_EQUAL(dec.getProcessorNEGhost(p),dec_ext.getProcessorNEGhost(p));
		for (size_t i = 0; i < dec.getProcessorNEGhost(p); i++)
		{
			volume_g += dec.getProcessorEGhostBox(p,i).getVolume();
			volume_ge += dec_ext.getProcessorEGhostBox(p,i).getVolume();

			BOOST_REQUIRE_EQUAL(dec_ext.getProcessorEGhostBox(p,i).isContained(dec_ext.getProcessorEGhostBox(p,i)),true);
		}
	}

	vcl.sum(volume_g);
	vcl.sum(volume_ge);
	vcl.execute();

	BOOST_REQUIRE(volume_ge > volume_g*1.05);

	volume_g = 0.0;
	volume_ge = 0.0;
	for (size_t p = 0; p < dec.getNNProcessors(); p++)
	{
		for (size_t i = 0; i< dec.getProcessorNIGhost(p); i++)
		{
			volume_g += dec.getProcessorIGhostBox(p,i).getVolume();
			volume_ge += dec_ext.getProcessorIGhostBox(p,i).getVolume();
			BOOST_REQUIRE_EQUAL(dec_ext.getProcessorIGhostBox(p,i).isContained(dec.getProcessorIGhostBox(p,i)),true);
		}
	}

	vcl.sum(volume_g);
	vcl.sum(volume_ge);
	vcl.execute();

348
	BOOST_REQUIRE(volume_ge > volume_g*1.05);
Pietro Incardona's avatar
Pietro Incardona committed
349 350 351
}


incardon's avatar
incardon committed
352 353 354
BOOST_AUTO_TEST_SUITE_END()

#endif