8 #ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_UNIT_TESTS_HPP_
9 #define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_UNIT_TESTS_HPP_
11 #include "Matrix/SparseMatrix.hpp"
12 #include "Vector/Vector.hpp"
13 #include "Solvers/umfpack_solver.hpp"
14 #include "Solvers/petsc_solver.hpp"
20 BOOST_AUTO_TEST_SUITE( sparse_matrix_test_suite )
24 BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_parallel)
38 auto & triplets = sm.getMatrixTriplets();
43 triplets.add(triplet(0,0,-2));
44 triplets.add(triplet(0,1,1));
47 triplets.add(triplet(1,0,1));
48 triplets.add(triplet(1,1,-2));
49 triplets.add(triplet(1,2,1));
52 triplets.add(triplet(2,1,1));
53 triplets.add(triplet(2,2,-2));
54 triplets.add(triplet(2,3,1));
64 triplets.add(triplet(3,2,1));
65 triplets.add(triplet(3,3,-2));
66 triplets.add(triplet(3,4,1));
69 triplets.add(triplet(4,3,1));
70 triplets.add(triplet(4,4,-2));
71 triplets.add(triplet(4,5,1));
74 triplets.add(triplet(5,4,1));
75 triplets.add(triplet(5,5,-2));
76 triplets.add(triplet(5,6,1));
85 triplets.add(triplet(6,5,1));
86 triplets.add(triplet(6,6,-2));
87 triplets.add(triplet(6,7,1));
90 triplets.add(triplet(7,6,1));
91 triplets.add(triplet(7,7,-2));
92 triplets.add(triplet(7,8,1));
95 triplets.add(triplet(8,7,1));
96 triplets.add(triplet(8,8,-2));
110 BOOST_REQUIRE_EQUAL(sm(0,0),-2);
111 BOOST_REQUIRE_EQUAL(sm(0,1),1);
113 BOOST_REQUIRE_EQUAL(sm(1,0),1);
114 BOOST_REQUIRE_EQUAL(sm(1,1),-2);
115 BOOST_REQUIRE_EQUAL(sm(1,2),1);
117 BOOST_REQUIRE_EQUAL(sm(2,1),1);
118 BOOST_REQUIRE_EQUAL(sm(2,2),-2);
119 BOOST_REQUIRE_EQUAL(sm(2,3),1);
121 BOOST_REQUIRE_EQUAL(sm(3,2),1);
122 BOOST_REQUIRE_EQUAL(sm(3,3),-2);
123 BOOST_REQUIRE_EQUAL(sm(3,4),1);
125 BOOST_REQUIRE_EQUAL(sm(4,3),1);
126 BOOST_REQUIRE_EQUAL(sm(4,4),-2);
127 BOOST_REQUIRE_EQUAL(sm(4,5),1);
129 BOOST_REQUIRE_EQUAL(sm(5,4),1);
130 BOOST_REQUIRE_EQUAL(sm(5,5),-2);
131 BOOST_REQUIRE_EQUAL(sm(5,6),1);
133 BOOST_REQUIRE_EQUAL(sm(6,5),1);
134 BOOST_REQUIRE_EQUAL(sm(6,6),-2);
135 BOOST_REQUIRE_EQUAL(sm(6,7),1);
137 BOOST_REQUIRE_EQUAL(sm(7,6),1);
138 BOOST_REQUIRE_EQUAL(sm(7,7),-2);
139 BOOST_REQUIRE_EQUAL(sm(7,8),1);
141 BOOST_REQUIRE_EQUAL(sm(8,7),1);
142 BOOST_REQUIRE_EQUAL(sm(8,8),-2);
153 BOOST_REQUIRE_CLOSE(x(0), -4.5, 0.001);
154 BOOST_REQUIRE_CLOSE(x(1), -8, 0.001);
155 BOOST_REQUIRE_CLOSE(x(2), -10.5, 0.001);
159 BOOST_REQUIRE_CLOSE(x(3), -12.0, 0.001);
160 BOOST_REQUIRE_CLOSE(x(4), -12.5, 0.001);
161 BOOST_REQUIRE_CLOSE(x(5), -12.0, 0.001);
165 BOOST_REQUIRE_CLOSE(x(6), -10.5, 0.001);
166 BOOST_REQUIRE_CLOSE(x(7), -8, 0.001);
167 BOOST_REQUIRE_CLOSE(x(8), -4.5, 0.001);
173 BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_petsc)
187 auto & triplets = sm.getMatrixTriplets();
192 triplets.add(triplet(0,0,-2));
193 triplets.add(triplet(0,1,1));
196 triplets.add(triplet(1,0,1));
197 triplets.add(triplet(1,1,-2));
198 triplets.add(triplet(1,2,1));
201 triplets.add(triplet(2,1,1));
202 triplets.add(triplet(2,2,-2));
203 triplets.add(triplet(2,3,1));
213 triplets.add(triplet(3,2,1));
214 triplets.add(triplet(3,3,-2));
215 triplets.add(triplet(3,4,1));
218 triplets.add(triplet(4,3,1));
219 triplets.add(triplet(4,4,-2));
220 triplets.add(triplet(4,5,1));
223 triplets.add(triplet(5,4,1));
224 triplets.add(triplet(5,5,-2));
225 triplets.add(triplet(5,6,1));
234 triplets.add(triplet(6,5,1));
235 triplets.add(triplet(6,6,-2));
236 triplets.add(triplet(6,7,1));
239 triplets.add(triplet(7,6,1));
240 triplets.add(triplet(7,7,-2));
241 triplets.add(triplet(7,8,1));
244 triplets.add(triplet(8,7,1));
245 triplets.add(triplet(8,8,-2));
253 Mat & matp = sm.getMat();
257 PetscInt r0[1] = {0};
258 PetscInt r1[1] = {1};
259 PetscInt r2[1] = {2};
260 PetscInt r0cols[3] = {0,1};
261 PetscInt r1cols[3] = {0,1,2};
262 PetscInt r2cols[3] = {1,2,3};
265 MatGetValues(matp,1,r0,2,r0cols,y);
267 BOOST_REQUIRE_EQUAL(y[0],-2);
268 BOOST_REQUIRE_EQUAL(y[1],1);
270 MatGetValues(matp,1,r1,3,r1cols,y);
272 BOOST_REQUIRE_EQUAL(y[0],1);
273 BOOST_REQUIRE_EQUAL(y[1],-2);
274 BOOST_REQUIRE_EQUAL(y[2],1);
276 MatGetValues(matp,1,r2,3,r2cols,y);
278 BOOST_REQUIRE_EQUAL(y[0],1);
279 BOOST_REQUIRE_EQUAL(y[1],-2);
280 BOOST_REQUIRE_EQUAL(y[2],1);
285 PetscInt r0[1] = {3};
286 PetscInt r1[1] = {4};
287 PetscInt r2[1] = {5};
288 PetscInt r0cols[3] = {2,3,4};
289 PetscInt r1cols[3] = {3,4,5};
290 PetscInt r2cols[3] = {4,5,6};
293 MatGetValues(matp,1,r0,3,r0cols,y);
295 BOOST_REQUIRE_EQUAL(y[0],1);
296 BOOST_REQUIRE_EQUAL(y[1],-2);
297 BOOST_REQUIRE_EQUAL(y[2],1);
299 MatGetValues(matp,1,r1,3,r1cols,y);
301 BOOST_REQUIRE_EQUAL(y[0],1);
302 BOOST_REQUIRE_EQUAL(y[1],-2);
303 BOOST_REQUIRE_EQUAL(y[2],1);
305 MatGetValues(matp,1,r2,3,r2cols,y);
307 BOOST_REQUIRE_EQUAL(y[0],1);
308 BOOST_REQUIRE_EQUAL(y[1],-2);
309 BOOST_REQUIRE_EQUAL(y[2],1);
313 PetscInt r0[1] = {6};
314 PetscInt r1[1] = {7};
315 PetscInt r2[1] = {8};
316 PetscInt r0cols[3] = {5,6,7};
317 PetscInt r1cols[3] = {6,7,8};
318 PetscInt r2cols[3] = {7,8};
321 MatGetValues(matp,1,r0,3,r0cols,y);
323 BOOST_REQUIRE_EQUAL(y[0],1);
324 BOOST_REQUIRE_EQUAL(y[1],-2);
325 BOOST_REQUIRE_EQUAL(y[2],1);
327 MatGetValues(matp,1,r1,3,r1cols,y);
329 BOOST_REQUIRE_EQUAL(y[0],1);
330 BOOST_REQUIRE_EQUAL(y[1],-2);
331 BOOST_REQUIRE_EQUAL(y[2],1);
333 MatGetValues(matp,1,r2,2,r2cols,y);
335 BOOST_REQUIRE_EQUAL(y[0],1);
336 BOOST_REQUIRE_EQUAL(y[1],-2);
344 BOOST_AUTO_TEST_CASE(sparse_matrix_eigen_petsc_solve)
360 auto & triplets = sm.getMatrixTriplets();
365 triplets.add(triplet(0,0,-2));
366 triplets.add(triplet(0,1,1));
370 for (
size_t i = 1 ; i < loc ; i++)
372 triplets.add(triplet(i,i-1,1));
373 triplets.add(triplet(i,i,-2));
374 triplets.add(triplet(i,i+1,1));
382 for (
size_t i = loc ; i < 2*loc ; i++)
384 triplets.add(triplet(i,i-1,1));
385 triplets.add(triplet(i,i,-2));
386 triplets.add(triplet(i,i+1,1));
394 for (
size_t i = 2*loc ; i < 3*loc-1 ; i++)
396 triplets.add(triplet(i,i-1,1));
397 triplets.add(triplet(i,i,-2));
398 triplets.add(triplet(i,i+1,1));
404 triplets.add(triplet(3*loc-1,3*loc-2,1));
405 triplets.add(triplet(3*loc-1,3*loc-1,-2));
419 BOOST_AUTO_TEST_SUITE_END()
size_t getProcessUnitID()
Get the process unit id.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support...
It store the non zero elements of the matrix.
Vector< double, PETSC_BASE > solve(SparseMatrix< double, int, PETSC_BASE > &A, const Vector< double, PETSC_BASE > &b, bool initial_guess=false)
Here we invert the matrix and solve the system.
static Vector< T > solve(const SparseMatrix< T, impl > &A, const Vector< T > &b)
stub solve
Implementation of VCluster class.
Sparse Matrix implementation.
This class is able to do Matrix inversion in parallel with PETSC solvers.
size_t getProcessingUnits()
Get the total number of processors.