FDScheme_unit_tests.hpp 10.6 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * FDScheme_unit_tests.hpp
 *
 *  Created on: Oct 5, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_FDSCHEME_UNIT_TESTS_HPP_
#define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_FDSCHEME_UNIT_TESTS_HPP_

#include "FiniteDifference/Derivative.hpp"
#include "FiniteDifference/Laplacian.hpp"
13
#include "Decomposition/CartDecomposition.hpp"
incardon's avatar
incardon committed
14
#include "util/grid_dist_testing.hpp"
incardon's avatar
incardon committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

constexpr unsigned int x = 0;
constexpr unsigned int y = 1;
constexpr unsigned int V = 0;

struct sys_nn
{
	// dimensionaly of the equation (2D problem 3D problem ...)
	static const unsigned int dims = 2;
	// number of fields in the system
	static const unsigned int nvar = 1;
	static const unsigned int ord = EQS_FIELD;

	// boundary at X and Y
	static const bool boundary[];

	// type of space float, double, ...
	typedef float stype;
33 34 35 36 37

	// Base grid
	typedef grid_dist_id<dims,stype,scalar<float>,CartDecomposition<2,stype> > b_grid;

	typedef void testing;
incardon's avatar
incardon committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
};

const bool sys_nn::boundary[] = {NON_PERIODIC,NON_PERIODIC};

struct sys_pp
{
	// dimensionaly of the equation (2D problem 3D problem ...)
	static const unsigned int dims = 2;
	// number of fields in the system
	static const unsigned int nvar = 1;
	static const unsigned int ord = EQS_FIELD;

	// boundary at X and Y
	static const bool boundary[];

	// type of space float, double, ...
	typedef float stype;
55 56 57 58 59

	// Base grid
	typedef grid_dist_id<dims,stype,scalar<float>,CartDecomposition<2,stype> > b_grid;

	typedef void testing;
incardon's avatar
incardon committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
};

const bool sys_pp::boundary[] = {PERIODIC,PERIODIC};

//////////////////////// STAGGERED CASE //////////////////////////////////

struct syss_nn
{
	// dimensionaly of the equation (2D problem 3D problem ...)
	static const unsigned int dims = 2;
	// number of fields in the system
	static const unsigned int nvar = 1;
	static const unsigned int ord = EQS_FIELD;

74
	static const unsigned int grid_type = STAGGERED_GRID;
incardon's avatar
incardon committed
75 76 77 78 79 80

	// boundary at X and Y
	static const bool boundary[];

	// type of space float, double, ...
	typedef float stype;
81 82 83 84 85

	// Base grid
	typedef grid_dist_id<dims,stype,scalar<float>,CartDecomposition<2,stype> > b_grid;

	typedef void testing;
incardon's avatar
incardon committed
86 87 88 89 90 91 92 93 94 95 96 97
};

const bool syss_nn::boundary[] = {NON_PERIODIC,NON_PERIODIC};

struct syss_pp
{
	// dimensionaly of the equation (2D problem 3D problem ...)
	static const unsigned int dims = 2;
	// number of fields in the system
	static const unsigned int nvar = 1;
	static const unsigned int ord = EQS_FIELD;

98
	static const unsigned int grid_type = STAGGERED_GRID;
incardon's avatar
incardon committed
99 100 101 102 103 104

	// boundary at X and Y
	static const bool boundary[];

	// type of space float, double, ...
	typedef float stype;
105 106 107 108 109

	// Base grid
	typedef grid_dist_id<dims,stype,scalar<float>,CartDecomposition<2,stype> > b_grid;

	typedef void testing;
incardon's avatar
incardon committed
110 111 112 113 114 115 116 117 118 119 120
};

const bool syss_pp::boundary[] = {PERIODIC,PERIODIC};

BOOST_AUTO_TEST_SUITE( fd_test )

BOOST_AUTO_TEST_CASE( fd_test_use_non_periodic)
{
	// grid size
	size_t sz[2]={16,16};

incardon's avatar
incardon committed
121 122 123
	// grid_dist_testing
	grid_dist_testing<2> g_map(sz);

incardon's avatar
incardon committed
124 125 126
	// grid_sm
	grid_sm<2,void> ginfo(sz);

incardon's avatar
incardon committed
127 128 129 130 131
	// Create several keys
	grid_dist_key_dx<2> key11(0,grid_key_dx<2>(1,1));
	grid_dist_key_dx<2> key00(0,grid_key_dx<2>(0,0));
	grid_dist_key_dx<2> key22(0,grid_key_dx<2>(2,2));
	grid_dist_key_dx<2> key1515(0,grid_key_dx<2>(15,15));
incardon's avatar
incardon committed
132 133 134 135 136

	// filled colums
	std::unordered_map<long int,float> cols_x;
	std::unordered_map<long int,float> cols_y;

incardon's avatar
incardon committed
137 138
	D<x,Field<V,sys_nn>,sys_nn>::value(g_map,key11,ginfo,cols_x,1);
	D<y,Field<V,sys_nn>,sys_nn>::value(g_map,key11,ginfo,cols_y,1);
incardon's avatar
incardon committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

	BOOST_REQUIRE_EQUAL(cols_x.size(),2);
	BOOST_REQUIRE_EQUAL(cols_y.size(),2);

	BOOST_REQUIRE_EQUAL(cols_x[17+1],1);
	BOOST_REQUIRE_EQUAL(cols_x[17-1],-1);

	BOOST_REQUIRE_EQUAL(cols_y[17+16],1);
	BOOST_REQUIRE_EQUAL(cols_y[17-16],-1);

	// filled colums

	std::unordered_map<long int,float> cols_xx;
	std::unordered_map<long int,float> cols_xy;
	std::unordered_map<long int,float> cols_yx;
	std::unordered_map<long int,float> cols_yy;

	// Composed derivative

incardon's avatar
incardon committed
158 159 160 161
	D<x,D<x,Field<V,sys_nn>,sys_nn>,sys_nn>::value(g_map,key22,ginfo,cols_xx,1);
	D<x,D<y,Field<V,sys_nn>,sys_nn>,sys_nn>::value(g_map,key22,ginfo,cols_xy,1);
	D<y,D<x,Field<V,sys_nn>,sys_nn>,sys_nn>::value(g_map,key22,ginfo,cols_yx,1);
	D<y,D<y,Field<V,sys_nn>,sys_nn>,sys_nn>::value(g_map,key22,ginfo,cols_yy,1);
incardon's avatar
incardon committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

	BOOST_REQUIRE_EQUAL(cols_xx.size(),3);
	BOOST_REQUIRE_EQUAL(cols_xy.size(),4);
	BOOST_REQUIRE_EQUAL(cols_yx.size(),4);
	BOOST_REQUIRE_EQUAL(cols_yy.size(),3);

	BOOST_REQUIRE_EQUAL(cols_xx[32],1);
	BOOST_REQUIRE_EQUAL(cols_xx[34],-2);
	BOOST_REQUIRE_EQUAL(cols_xx[36],1);

	BOOST_REQUIRE_EQUAL(cols_xy[17],1);
	BOOST_REQUIRE_EQUAL(cols_xy[19],-1);
	BOOST_REQUIRE_EQUAL(cols_xy[49],-1);
	BOOST_REQUIRE_EQUAL(cols_xy[51],1);

	BOOST_REQUIRE_EQUAL(cols_yx[17],1);
	BOOST_REQUIRE_EQUAL(cols_yx[19],-1);
	BOOST_REQUIRE_EQUAL(cols_yx[49],-1);
	BOOST_REQUIRE_EQUAL(cols_xy[51],1);

	BOOST_REQUIRE_EQUAL(cols_yy[2],1);
	BOOST_REQUIRE_EQUAL(cols_yy[34],-2);
	BOOST_REQUIRE_EQUAL(cols_yy[66],1);

	// Non periodic with one sided

	cols_x.clear();
	cols_y.clear();

incardon's avatar
incardon committed
191 192
	D<x,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key11,ginfo,cols_x,1);
	D<y,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key11,ginfo,cols_y,1);
incardon's avatar
incardon committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	BOOST_REQUIRE_EQUAL(cols_x.size(),2);
	BOOST_REQUIRE_EQUAL(cols_y.size(),2);

	BOOST_REQUIRE_EQUAL(cols_x[17+1],1);
	BOOST_REQUIRE_EQUAL(cols_x[17-1],-1);

	BOOST_REQUIRE_EQUAL(cols_y[17+16],1);
	BOOST_REQUIRE_EQUAL(cols_y[17-16],-1);

	// Border left

	cols_x.clear();
	cols_y.clear();

incardon's avatar
incardon committed
208 209
	D<x,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key00,ginfo,cols_x,1);
	D<y,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key00,ginfo,cols_y,1);
incardon's avatar
incardon committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

	BOOST_REQUIRE_EQUAL(cols_x.size(),3);
	BOOST_REQUIRE_EQUAL(cols_y.size(),3);

	BOOST_REQUIRE_EQUAL(cols_x[0],-1.5);
	BOOST_REQUIRE_EQUAL(cols_x[1],2);
	BOOST_REQUIRE_EQUAL(cols_x[2],-0.5);

	BOOST_REQUIRE_EQUAL(cols_y[0],-1.5);
	BOOST_REQUIRE_EQUAL(cols_y[16],2);
	BOOST_REQUIRE_EQUAL(cols_y[32],-0.5);

	// Border Top right

	cols_x.clear();
	cols_y.clear();

incardon's avatar
incardon committed
227 228
	D<x,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key1515,ginfo,cols_x,1);
	D<y,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::value(g_map,key1515,ginfo,cols_y,1);
incardon's avatar
incardon committed
229 230 231 232 233 234 235 236 237 238

	BOOST_REQUIRE_EQUAL(cols_x.size(),3);
	BOOST_REQUIRE_EQUAL(cols_y.size(),3);

	BOOST_REQUIRE_EQUAL(cols_x[15*16+15],1.5);
	BOOST_REQUIRE_EQUAL(cols_x[15*16+14],-2);
	BOOST_REQUIRE_EQUAL(cols_x[15*16+13],0.5);

	BOOST_REQUIRE_EQUAL(cols_y[15*16+15],1.5);
	BOOST_REQUIRE_EQUAL(cols_y[14*16+15],-2);
incardon's avatar
incardon committed
239
	BOOST_REQUIRE_EQUAL(cols_y[13*16+15],0.5);
incardon's avatar
incardon committed
240 241 242
}


243 244 245 246 247 248 249 250
BOOST_AUTO_TEST_CASE( fd_test_use_staggered_non_periodic)
{
	// grid size
	size_t sz[2]={16,16};

	// grid_sm
	grid_sm<2,void> ginfo(sz);

incardon's avatar
incardon committed
251 252 253 254 255 256 257
	// grid_dist_testing
	grid_dist_testing<2> g_map(sz);

	// Create several keys
	grid_dist_key_dx<2> key11(0,grid_key_dx<2>(1,1));
	grid_dist_key_dx<2> key22(0,grid_key_dx<2>(2,2));
	grid_dist_key_dx<2> key1515(0,grid_key_dx<2>(15,15));
258 259 260 261 262

	// filled colums
	std::unordered_map<long int,float> cols_x;
	std::unordered_map<long int,float> cols_y;

incardon's avatar
incardon committed
263 264
	D<x,Field<V,syss_pp>,syss_pp>::value(g_map,key11,ginfo,cols_x,1);
	D<y,Field<V,syss_pp>,syss_pp>::value(g_map,key11,ginfo,cols_y,1);
265 266 267 268 269 270 271 272 273 274 275 276 277 278

	BOOST_REQUIRE_EQUAL(cols_x.size(),2);
	BOOST_REQUIRE_EQUAL(cols_y.size(),2);

	BOOST_REQUIRE_EQUAL(cols_x[17+1],1);
	BOOST_REQUIRE_EQUAL(cols_x[17],-1);

	BOOST_REQUIRE_EQUAL(cols_y[17+16],1);
	BOOST_REQUIRE_EQUAL(cols_y[17],-1);

	cols_x.clear();
	cols_y.clear();
}

incardon's avatar
incardon committed
279 280 281 282 283 284 285 286 287 288
/////////////// Laplacian test

BOOST_AUTO_TEST_CASE( fd_test_lap_use_periodic)
{
	// grid size
	size_t sz[2]={16,16};

	// grid_sm
	grid_sm<2,void> ginfo(sz);

incardon's avatar
incardon committed
289 290 291 292 293 294 295
	// grid_dist_testing
	grid_dist_testing<2> g_map(sz);

	// Create several keys
	grid_dist_key_dx<2> key11(0,grid_key_dx<2>(1,1));
	grid_dist_key_dx<2> key22(0,grid_key_dx<2>(2,2));
	grid_dist_key_dx<2> key1414(0,grid_key_dx<2>(14,14));
incardon's avatar
incardon committed
296 297 298 299

	// filled colums
	std::unordered_map<long int,float> cols;

incardon's avatar
incardon committed
300
	Lap<Field<V,sys_pp>,sys_pp>::value(g_map,key11,ginfo,cols,1);
incardon's avatar
incardon committed
301 302 303 304 305 306 307 308 309 310 311 312

	BOOST_REQUIRE_EQUAL(cols.size(),5);

	BOOST_REQUIRE_EQUAL(cols[1],1);
	BOOST_REQUIRE_EQUAL(cols[17-1],1);
	BOOST_REQUIRE_EQUAL(cols[17+1],1);
	BOOST_REQUIRE_EQUAL(cols[17+16],1);

	BOOST_REQUIRE_EQUAL(cols[17],-4);

	cols.clear();

incardon's avatar
incardon committed
313
	Lap<Field<V,sys_pp>,sys_pp>::value(g_map,key1414,ginfo,cols,1);
incardon's avatar
incardon committed
314 315 316

	BOOST_REQUIRE_EQUAL(cols.size(),5);

incardon's avatar
incardon committed
317
	BOOST_REQUIRE_EQUAL(cols[14*16+13],1);
incardon's avatar
incardon committed
318
	BOOST_REQUIRE_EQUAL(cols[14*16+15],1);
incardon's avatar
incardon committed
319 320
	BOOST_REQUIRE_EQUAL(cols[13*16+14],1);
	BOOST_REQUIRE_EQUAL(cols[15*16+14],1);
incardon's avatar
incardon committed
321

incardon's avatar
incardon committed
322
	BOOST_REQUIRE_EQUAL(cols[14*16+14],-4);
incardon's avatar
incardon committed
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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

//////////////// Position ////////////////////

BOOST_AUTO_TEST_CASE( fd_test_use_staggered_position)
{
	// grid size
/*	size_t sz[2]={16,16};

	// Create a derivative row Matrix
	grid_key_dx<2> key11(1,1);
	grid_key_dx<2> key00(0,0);
	grid_key_dx<2> key22(2,2);
	grid_key_dx<2> key1515(15,15);

	openfpm::vector<comb<2>> vx_c;
	comb<2> cx = {{1,0}};
	vx_c.add(cx);         // like v_x

	openfpm::vector<comb<2>> vy_c;
	comb<2> cy = {{0,1}};
	vy_c.add(cy);         // like v_y

	grid_key_dx<2> key_ret_x = D<x,Field<V,sys_nn>,sys_nn>::position(key11,0,vx_c);
	grid_key_dx<2> key_ret_y = D<y,Field<V,sys_nn>,sys_nn>::position(key11,0,vx_c);

	BOOST_REQUIRE_EQUAL(key_ret_x.get(0),1);
	BOOST_REQUIRE_EQUAL(key_ret_x.get(1),1);

	BOOST_REQUIRE_EQUAL(key_ret_y.get(0),0);
	BOOST_REQUIRE_EQUAL(key_ret_y.get(1),0);

	key_ret_x = D<x,Field<V,sys_nn>,sys_nn>::position(key11,0,vy_c);
	key_ret_y = D<y,Field<V,sys_nn>,sys_nn>::position(key11,0,vy_c);

	BOOST_REQUIRE_EQUAL(key_ret_x.get(0),0);
	BOOST_REQUIRE_EQUAL(key_ret_x.get(1),0);

	BOOST_REQUIRE_EQUAL(key_ret_y.get(0),1);
	BOOST_REQUIRE_EQUAL(key_ret_y.get(1),1);

	// Composed derivative

	grid_key_dx<2> key_ret_xx = D<x,D<x,Field<V,sys_nn>,sys_nn>,sys_nn>::position(key00,0);
	grid_key_dx<2> key_ret_xy = D<x,D<y,Field<V,sys_nn>,sys_nn>,sys_nn>::position(key00,0);
	grid_key_dx<2> key_ret_yx = D<y,D<x,Field<V,sys_nn>,sys_nn>,sys_nn>::position(key00,0);
	grid_key_dx<2> key_ret_yy = D<y,D<y,Field<V,sys_nn>,sys_nn>,sys_nn>::position(key00,0);

	BOOST_REQUIRE_EQUAL(key_ret_xx.get(0),1);
	BOOST_REQUIRE_EQUAL(key_ret_xx.get(1),0);

	BOOST_REQUIRE_EQUAL(key_ret_xy.get(0),1);
	BOOST_REQUIRE_EQUAL(key_ret_xy.get(1),0);

	BOOST_REQUIRE_EQUAL(key_ret_xy.get(0),0);
	BOOST_REQUIRE_EQUAL(key_ret_xy.get(1),1);

	BOOST_REQUIRE_EQUAL(key_ret_xy.get(0),0);
	BOOST_REQUIRE_EQUAL(key_ret_xy.get(1),1);

	////////////////// Non periodic with one sided

	key_ret_x = D<x,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::position(key00,0,vx_c);
	key_ret_y = D<y,Field<V,sys_nn>,sys_nn,CENTRAL_B_ONE_SIDE>::position(key00,0,vx_c);

	BOOST_REQUIRE_EQUAL(key_ret_x.get(0),0);
	BOOST_REQUIRE_EQUAL(key_ret_x.get(1),1);

	BOOST_REQUIRE_EQUAL(key_ret_x,0);
	BOOST_REQUIRE_EQUAL(key_ret_y,0);

	// Border left*/


}

BOOST_AUTO_TEST_SUITE_END()


#endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_FDSCHEME_UNIT_TESTS_HPP_ */