petsc_solver_unit_tests.cpp 3.52 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
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
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
89
90
91
92
93
94
95
96
/*
 * petsc_solver_unit_tests.hpp
 *
 *  Created on: Jun 19, 2017
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_SOLVERS_PETSC_SOLVER_UNIT_TESTS_CPP_
#define OPENFPM_NUMERICS_SRC_SOLVERS_PETSC_SOLVER_UNIT_TESTS_CPP_

#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "petsc_solver_report_unit_tests.hpp"

#include "Grid/grid_dist_id.hpp"
#include "Matrix/SparseMatrix.hpp"
#include "Vector/Vector.hpp"
#include "FiniteDifference/Laplacian.hpp"
#include "FiniteDifference/FDScheme.hpp"
#include "Solvers/petsc_solver.hpp"


BOOST_AUTO_TEST_SUITE( mg_solvers_test )


BOOST_AUTO_TEST_CASE( laplacian_3D_int_zero_mg )
{
	constexpr unsigned int phi = 0;
	typedef Field<phi,poisson_nn_helm> phi_f;

	Vcluster & v_cl = create_vcluster();
	if (v_cl.getProcessingUnits() != 3)
		return;

    Ghost<3,long int> g(2);
	Ghost<3,long int> stencil_max(2);
	Box<3,float> domain({0.0,0.0,0.0},{1.0,1.0,1.0});

	periodicity<3> p({PERIODIC,PERIODIC,PERIODIC});

	typedef Lap<phi_f,poisson_nn_helm,CENTRAL_SYM> poisson;
	grid_dist_id<3,float,aggregate<float>> psi({64,64,64},domain,g,p);
	grid_dist_id<3,float,aggregate<float>> psi2(psi.getDecomposition(),{64,64,64},g);

	// Fill B

	size_t center_x = psi.size(0) / 2;
	size_t center_y = psi.size(1) / 2;
	size_t center_z = psi.size(2) / 2;
	auto it = psi.getDomainIterator();

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

		float sx = (float)(gkey.get(0))-center_x;
		float sy = (float)(gkey.get(1))-center_y;
		float sz = (float)(gkey.get(2))-center_z;

		float gs = 100.0*exp(-((sx*sx)+(sy*sy)+(sz*sz))/100.0);

		psi.get<0>(key) = sin(2*M_PI*sx/psi.size(0))*sin(2*M_PI*sy/psi.size(1))*sin(2*M_PI*sz/psi.size(2))*gs;
		psi2.get<0>(key) = sin(2*M_PI*sx/psi.size(0))*sin(2*M_PI*sy/psi.size(1))*sin(2*M_PI*sz/psi.size(2))*gs;

		++it;
	}

	FDScheme<poisson_nn_helm> fd(stencil_max, domain, psi);

	fd.template impose_dit<0>(poisson(),psi,psi.getDomainIterator());

	petsc_solver<double> solver;
	solver.setSolver(KSPBCGS);
	solver.setAbsTol(0.01);
	solver.setMaxIter(500);

	////////

	solver.setPreconditioner(PCHYPRE_BOOMERAMG);
	solver.setPreconditionerAMG_nl(6);
	solver.setPreconditionerAMG_maxit(3);
	solver.setPreconditionerAMG_relax("SOR/Jacobi");
	solver.setPreconditionerAMG_cycleType("V",6,6);
	solver.setPreconditionerAMG_coarsen("HMIS");
	solver.setPreconditionerAMG_coarsenNodalType(0);

	timer tm_solve;
	tm_solve.start();
	auto x_ = solver.solve(fd.getA(),fd.getB());
	tm_solve.stop();

	fd.template copy<phi>(x_,psi);
	psi.write("AMG_psi");

jenkins's avatar
jenkins committed
97
98
99
	#ifdef HAVE_OSX
	bool check = compare("AMG_psi_" + std::to_string(v_cl.getProcessUnitID()) + ".vtk","test/AMG_psi_" + std::to_string(v_cl.getProcessUnitID()) + "_test_osx.vtk");
	#else
incardon's avatar
incardon committed
100
	bool check = compare("AMG_psi_" + std::to_string(v_cl.getProcessUnitID()) + ".vtk","test/AMG_psi_test_" + std::to_string(v_cl.getProcessUnitID()) + ".vtk");
jenkins's avatar
jenkins committed
101
102
	#endif

incardon's avatar
incardon committed
103
104
105
106
107
108
109
110
111
112
	BOOST_REQUIRE_EQUAL(check,true);

	// Resolve
	timer tm_solve2;
	tm_solve2.start();
	auto x2_ = solver.solve(fd.getB());
	tm_solve2.stop();

	fd.template copy<phi>(x_,psi2);
	psi2.write("AMG_psi2");
jenkins's avatar
jenkins committed
113
114
115
116
117
118

	#ifdef HAVE_OSX
	check = compare("AMG_psi2_" + std::to_string(v_cl.getProcessUnitID()) + ".vtk","test/AMG_psi2_" + std::to_string(v_cl.getProcessUnitID()) + "_test_osx.vtk");
	#else
	check = compare("AMG_psi2_" + std::to_string(v_cl.getProcessUnitID()) + ".vtk","test/AMG_psi2_" + std::to_string(v_cl.getProcessUnitID()) + "_test_osx.vtk");
	#endif
incardon's avatar
incardon committed
119
120
121
122
123
124
	BOOST_REQUIRE_EQUAL(check,true);
}

BOOST_AUTO_TEST_SUITE_END()

#endif /* OPENFPM_NUMERICS_SRC_SOLVERS_PETSC_SOLVER_UNIT_TESTS_CPP_ */