8#ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_UNIT_TESTS_HPP_
9#define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_UNIT_TESTS_HPP_
11#define BOOST_TEST_DYN_LINK
12#include <boost/test/unit_test.hpp>
14#include "Matrix/SparseMatrix.hpp"
15#include "Vector/Vector.hpp"
16#include "Solvers/umfpack_solver.hpp"
17#include "Solvers/petsc_solver.hpp"
23BOOST_AUTO_TEST_SUITE( sparse_matrix_test_suite )
27BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_parallel)
29#if defined(HAVE_EIGEN) && defined(HAVE_SUITESPARSE)
43 auto & triplets = sm.getMatrixTriplets();
115 BOOST_REQUIRE_EQUAL(sm(0,0),-2);
116 BOOST_REQUIRE_EQUAL(sm(0,1),1);
118 BOOST_REQUIRE_EQUAL(sm(1,0),1);
119 BOOST_REQUIRE_EQUAL(sm(1,1),-2);
120 BOOST_REQUIRE_EQUAL(sm(1,2),1);
122 BOOST_REQUIRE_EQUAL(sm(2,1),1);
123 BOOST_REQUIRE_EQUAL(sm(2,2),-2);
124 BOOST_REQUIRE_EQUAL(sm(2,3),1);
126 BOOST_REQUIRE_EQUAL(sm(3,2),1);
127 BOOST_REQUIRE_EQUAL(sm(3,3),-2);
128 BOOST_REQUIRE_EQUAL(sm(3,4),1);
130 BOOST_REQUIRE_EQUAL(sm(4,3),1);
131 BOOST_REQUIRE_EQUAL(sm(4,4),-2);
132 BOOST_REQUIRE_EQUAL(sm(4,5),1);
134 BOOST_REQUIRE_EQUAL(sm(5,4),1);
135 BOOST_REQUIRE_EQUAL(sm(5,5),-2);
136 BOOST_REQUIRE_EQUAL(sm(5,6),1);
138 BOOST_REQUIRE_EQUAL(sm(6,5),1);
139 BOOST_REQUIRE_EQUAL(sm(6,6),-2);
140 BOOST_REQUIRE_EQUAL(sm(6,7),1);
142 BOOST_REQUIRE_EQUAL(sm(7,6),1);
143 BOOST_REQUIRE_EQUAL(sm(7,7),-2);
144 BOOST_REQUIRE_EQUAL(sm(7,8),1);
146 BOOST_REQUIRE_EQUAL(sm(8,7),1);
147 BOOST_REQUIRE_EQUAL(sm(8,8),-2);
153 auto x = solver.
solve(sm,v);
159 BOOST_REQUIRE_CLOSE(x(0), -4.5, 0.001);
160 BOOST_REQUIRE_CLOSE(x(1), -8, 0.001);
161 BOOST_REQUIRE_CLOSE(x(2), -10.5, 0.001);
165 BOOST_REQUIRE_CLOSE(x(3), -12.0, 0.001);
166 BOOST_REQUIRE_CLOSE(x(4), -12.5, 0.001);
167 BOOST_REQUIRE_CLOSE(x(5), -12.0, 0.001);
171 BOOST_REQUIRE_CLOSE(x(6), -10.5, 0.001);
172 BOOST_REQUIRE_CLOSE(x(7), -8, 0.001);
173 BOOST_REQUIRE_CLOSE(x(8), -4.5, 0.001);
181BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_petsc)
195 auto & triplets = sm.getMatrixTriplets();
261 Mat & matp = sm.getMat();
265 PetscInt r0[1] = {0};
266 PetscInt r1[1] = {1};
267 PetscInt r2[1] = {2};
268 PetscInt r0cols[3] = {0,1};
269 PetscInt r1cols[3] = {0,1,2};
270 PetscInt r2cols[3] = {1,2,3};
273 MatGetValues(matp,1,r0,2,r0cols,y);
275 BOOST_REQUIRE_EQUAL(y[0],-2);
276 BOOST_REQUIRE_EQUAL(y[1],1);
278 MatGetValues(matp,1,r1,3,r1cols,y);
280 BOOST_REQUIRE_EQUAL(y[0],1);
281 BOOST_REQUIRE_EQUAL(y[1],-2);
282 BOOST_REQUIRE_EQUAL(y[2],1);
284 MatGetValues(matp,1,r2,3,r2cols,y);
286 BOOST_REQUIRE_EQUAL(y[0],1);
287 BOOST_REQUIRE_EQUAL(y[1],-2);
288 BOOST_REQUIRE_EQUAL(y[2],1);
293 PetscInt r0[1] = {3};
294 PetscInt r1[1] = {4};
295 PetscInt r2[1] = {5};
296 PetscInt r0cols[3] = {2,3,4};
297 PetscInt r1cols[3] = {3,4,5};
298 PetscInt r2cols[3] = {4,5,6};
301 MatGetValues(matp,1,r0,3,r0cols,y);
303 BOOST_REQUIRE_EQUAL(y[0],1);
304 BOOST_REQUIRE_EQUAL(y[1],-2);
305 BOOST_REQUIRE_EQUAL(y[2],1);
307 MatGetValues(matp,1,r1,3,r1cols,y);
309 BOOST_REQUIRE_EQUAL(y[0],1);
310 BOOST_REQUIRE_EQUAL(y[1],-2);
311 BOOST_REQUIRE_EQUAL(y[2],1);
313 MatGetValues(matp,1,r2,3,r2cols,y);
315 BOOST_REQUIRE_EQUAL(y[0],1);
316 BOOST_REQUIRE_EQUAL(y[1],-2);
317 BOOST_REQUIRE_EQUAL(y[2],1);
321 PetscInt r0[1] = {6};
322 PetscInt r1[1] = {7};
323 PetscInt r2[1] = {8};
324 PetscInt r0cols[3] = {5,6,7};
325 PetscInt r1cols[3] = {6,7,8};
326 PetscInt r2cols[3] = {7,8};
329 MatGetValues(matp,1,r0,3,r0cols,y);
331 BOOST_REQUIRE_EQUAL(y[0],1);
332 BOOST_REQUIRE_EQUAL(y[1],-2);
333 BOOST_REQUIRE_EQUAL(y[2],1);
335 MatGetValues(matp,1,r1,3,r1cols,y);
337 BOOST_REQUIRE_EQUAL(y[0],1);
338 BOOST_REQUIRE_EQUAL(y[1],-2);
339 BOOST_REQUIRE_EQUAL(y[2],1);
341 MatGetValues(matp,1,r2,2,r2cols,y);
343 BOOST_REQUIRE_EQUAL(y[0],1);
344 BOOST_REQUIRE_EQUAL(y[1],-2);
352BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_petsc_solve)
368 auto & triplets = sm.getMatrixTriplets();
378 for (
size_t i = 1 ; i < loc ; i++)
380 triplets.add(
triplet(i,i-1,1));
382 triplets.add(
triplet(i,i+1,1));
390 for (
size_t i = loc ; i < 2*loc ; i++)
392 triplets.add(
triplet(i,i-1,1));
394 triplets.add(
triplet(i,i+1,1));
402 for (
size_t i = 2*loc ; i < 3*loc-1 ; i++)
404 triplets.add(
triplet(i,i-1,1));
406 triplets.add(
triplet(i,i+1,1));
412 triplets.add(
triplet(3*loc-1,3*loc-2,1));
413 triplets.add(
triplet(3*loc-1,3*loc-1,-2));
427BOOST_AUTO_TEST_SUITE_END()
Sparse Matrix implementation.
size_t getProcessUnitID()
Get the process unit id.
size_t getProcessingUnits()
Get the total number of processors.
Implementation of VCluster class.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support.
In case T does not match the PETSC precision compilation create a stub structure.
static Vector< T > solve(const SparseMatrix< T, impl > &A, const Vector< T > &b)
Solve the linear system.
stub when library compiled without eigen
static Vector< T > solve(const SparseMatrix< T, id_type, impl > &A, const Vector< T, impl > &b)
stub solve
It store the non zero elements of the matrix.