grid_sm_unit_tests.hpp 6.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * grid_sm_test.hpp
 *
 *  Created on: Dec 14, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_
#define OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_

#include "iterators/grid_key_dx_iterator_sub_bc.hpp"
Yaroslav's avatar
Yaroslav committed
12
#include "grid_key_dx_iterator_hilbert.hpp"
13
14
15
16
17
18
19
20
21
22
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
55

BOOST_AUTO_TEST_SUITE( grid_sm_test )


BOOST_AUTO_TEST_CASE( grid_sm_linearization )
{
	const grid_key_dx<3> key1(1,2,3);
	const grid_key_dx<3> zero(0,0,0);
	const grid_key_dx<3> seven(7,7,7);
	const comb<3> c({1,0,-1});
	size_t sz[3] = {8,8,8};

	grid_sm<3,int> gs(sz);
	size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

	long int lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,146);
	lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,-1);
	lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,-1);

	for (size_t i = 0 ; i < 3 ; i++)
		bc[i] = PERIODIC;

	lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,146);
	lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,71);
	lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc);
	BOOST_REQUIRE_EQUAL(lin,62);
}


BOOST_AUTO_TEST_CASE( grid_iterator_sub_p )
{
	const grid_key_dx<3> key1(4,4,4);
	const grid_key_dx<3> key2(-1,-1,-1);
	const grid_key_dx<3> key3(9,9,9);
	size_t sz[3] = {8,8,8};

	grid_sm<3,int> gs(sz);

Pietro Incardona's avatar
Pietro Incardona committed
56
	grid_key_dx_iterator_sub_bc<3,no_stencil> it(gs,key2,key1,{PERIODIC,PERIODIC,PERIODIC});
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

	size_t cnt = 0;

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

		for (size_t i = 0 ; i < 3 ; i++)
		{
			BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
			BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
		}

		cnt++;

		++it;
	}

	BOOST_REQUIRE_EQUAL(cnt,216ul);

Pietro Incardona's avatar
Pietro Incardona committed
77
	grid_key_dx_iterator_sub_bc<3,no_stencil> it2(gs,key2,key3,{PERIODIC,PERIODIC,PERIODIC});
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

	cnt = 0;

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

		for (size_t i = 0 ; i < 3 ; i++)
		{
			BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
			BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
		}

		cnt++;

		++it2;
	}

	BOOST_REQUIRE_EQUAL(cnt,1331ul);

	cnt = 0;

	const grid_key_dx<3> key4(0,-1,0);
	const grid_key_dx<3> key5(2,2,2);

Pietro Incardona's avatar
Pietro Incardona committed
103
	grid_key_dx_iterator_sub_bc<3,no_stencil> it3(gs,key4,key5,{NON_PERIODIC,PERIODIC,NON_PERIODIC});
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

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

		for (size_t i = 0 ; i < 3 ; i++)
		{
			BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
			BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
		}

		cnt++;

		++it3;
	}

	BOOST_REQUIRE_EQUAL(cnt,36ul);

	// bc non periodic with out-of-bound

Pietro Incardona's avatar
Pietro Incardona committed
124
	grid_key_dx_iterator_sub_bc<3,no_stencil,do_not_print_warning_on_adjustment<3>> it4(gs,key4,key5,{NON_PERIODIC,NON_PERIODIC,NON_PERIODIC});
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

	cnt = 0;

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

		for (size_t i = 0 ; i < 3 ; i++)
		{
			BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true);
			BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
		}

		cnt++;

		++it4;
	}

	BOOST_REQUIRE_EQUAL(cnt,27ul);

	// case with no key

	const grid_key_dx<3> key6(-1,-1,-1);
	const grid_key_dx<3> key7(-1,-1,8);

Pietro Incardona's avatar
Pietro Incardona committed
150
	grid_key_dx_iterator_sub_bc<3,no_stencil,do_not_print_warning_on_adjustment<3>> it5(gs,key6,key7,{NON_PERIODIC,NON_PERIODIC,NON_PERIODIC});
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

	cnt = 0;

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

		for (size_t i = 0 ; i < 3 ; i++)
		{
			BOOST_REQUIRE_EQUAL(key.get(i) >= 0,true);
			BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true);
		}

		cnt++;

		++it5;
	}

	BOOST_REQUIRE_EQUAL(cnt,0ul);
}

Yaroslav's avatar
Yaroslav committed
172
173
BOOST_AUTO_TEST_CASE( grid_key_dx_iterator_hilbert_test )
{
Yaroslav's avatar
Yaroslav committed
174
	// 2D test
Yaroslav's avatar
Yaroslav committed
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
	{
		size_t count = 0;

		//An order of a hilberts curve
		int32_t m = 2;

		grid_key_dx<2> start (0,0);

		//Create an iterator
		grid_key_dx_iterator_hilbert<2> h_it(m);

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

			++h_it;
		}

		//(2^m)^dim
		BOOST_REQUIRE_EQUAL(count, (size_t)16);

		h_it.reset();

		bool val = h_it.get() == start;

		BOOST_REQUIRE_EQUAL(val,true);
	}

Yaroslav's avatar
Yaroslav committed
203
	// 3D test
Yaroslav's avatar
Yaroslav committed
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
	{
		size_t count = 0;

		//An order of a hilberts curve
		int32_t m = 2;

		grid_key_dx<3> start (0,0,0);

		//Create an iterator
		grid_key_dx_iterator_hilbert<3> h_it(m);

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

			++h_it;
		}

		//(2^m)^dim
		BOOST_REQUIRE_EQUAL(count, (size_t)64);

		h_it.reset();

		bool val = h_it.get() == start;

		BOOST_REQUIRE_EQUAL(val,true);
	}
}

Yaroslav's avatar
Yaroslav committed
233

Pietro Incardona's avatar
Pietro Incardona committed
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
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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
BOOST_AUTO_TEST_CASE( grid_iterator_sp_test )
{
	size_t sz[3] = {16,16,16};

	grid_cpu<3, Point_test<float> > c3(sz);
	c3.setMemory();

	grid_key_dx<3> start(2,2,2);
	grid_key_dx<3> stop(10,10,10);

	auto info = c3.getGrid();

	grid_key_dx_iterator_sp<3> it(info,info.LinId(start),info.LinId(stop));

	size_t count = 0;

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

		++it;
	}

	BOOST_REQUIRE_EQUAL(count,2185ul);
}

BOOST_AUTO_TEST_CASE( grid_iterator_test_use)
{
	{
	//! [Grid iterator test usage]
	size_t count = 0;

	// Subdivisions
	size_t div[3] = {16,16,16};

	// grid info
	grid_sm<3,void> g_info(div);

	// Create a grid iterator
	grid_key_dx_iterator<3> g_it(g_info);

	// Iterate on all the elements
	while (g_it.isNext())
	{
		grid_key_dx<3> key = g_it.get();

		// set the grid key to zero without any reason ( to avoid warning compilations )
		key.zero();

		count++;

		++g_it;
	}

	BOOST_REQUIRE_EQUAL(count, (size_t)16*16*16);
	//! [Grid iterator test usage]
	}

	{
	size_t count = 0;
	// Iterate only on the internal elements

	//! [Sub-grid iterator test usage]
	// Subdivisions
	size_t div[3] = {16,16,16};

	// grid info
	grid_sm<3,void> g_info(div);

	grid_key_dx<3> start(1,1,1);
	grid_key_dx<3> stop(14,14,14);

	// Create a grid iterator (start and stop included)
	grid_key_dx_iterator_sub<3> g_it(g_info,start,stop);

	// Iterate on all the elements
	while (g_it.isNext())
	{
		grid_key_dx<3> key = g_it.get();

		// set the grid key to zero without any reason ( to avoid warning compilations )
		key.zero();

		count++;

		++g_it;
	}

	BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14);

	//! [Sub-grid iterator test usage]

	// reset the iterator and check that it start from gk_start
	g_it.reset();

	bool val = g_it.get() == start;

	BOOST_REQUIRE_EQUAL(val,true);
	}
}

BOOST_AUTO_TEST_CASE( grid_sub_iterator_test )
{
	//! [Sub-grid iterator test usage]
	// Subdivisions
	size_t count = 0;
	typedef Point_test<float> p;

	size_t div[3] = {16,16,16};

	// grid info
	grid_cpu<3,Point_test<float>> g(div);
	g.setMemory();

	grid_key_dx<3> start(1,1,1);
	grid_key_dx<3> stop(14,14,14);

	// Create a grid iterator (start and stop included)
	auto g_it =  g.getIterator(start,stop);

	// Iterate on all the elements
	while (g_it.isNext())
	{
		grid_key_dx<3> key = g_it.get();

		// set the x value
		g.template get<p::x>(key) = 1.0;

		count++;

		++g_it;
	}

	BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14);

	//! [Sub-grid iterator test usage]
}

372
373
374
375
BOOST_AUTO_TEST_SUITE_END()


#endif /* OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_ */