xref: /petsc/src/mat/tests/ex36.c (revision 1404853cd07923df46691f50dfd5de8bf2e82a6a)
1 static char help[] = "Tests assembly of a matrix from another matrix's hash table.\n\n";
2 
3 #include <petscmat.h>
4 
5 PetscErrorCode SetValues(Mat A)
6 {
7   PetscInt    m, n, i, j;
8   PetscScalar v;
9 
10   PetscFunctionBeginUser;
11   PetscCall(MatGetSize(A, &m, &n));
12   for (i = 0; i < m; i++) {
13     for (j = 0; j < n; j++) {
14       v = 10.0 * i + j + 1;
15       PetscCall(MatSetValues(A, 1, &i, 1, &j, &v, ADD_VALUES));
16     }
17   }
18   PetscFunctionReturn(PETSC_SUCCESS);
19 }
20 
21 PetscErrorCode CreateAndViewB(Mat A)
22 {
23   Mat B;
24 
25   PetscFunctionBeginUser;
26   /* Create B */
27   PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &B));
28   PetscCall(MatCopyHashToXAIJ(A, B));
29   PetscCall(MatView(B, PETSC_VIEWER_STDOUT_WORLD));
30   PetscCall(MatDestroy(&B));
31   PetscFunctionReturn(PETSC_SUCCESS);
32 }
33 
34 PetscErrorCode AssembleAndViewA(Mat A)
35 {
36   PetscFunctionBeginUser;
37   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
38   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
39   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
40   PetscFunctionReturn(PETSC_SUCCESS);
41 }
42 
43 int main(int argc, char **argv)
44 {
45   Mat A;
46 
47   PetscFunctionBeginUser;
48   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
49 
50   /* ------- Set values in A --------- */
51   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
52   PetscCall(MatSetSizes(A, 1, 1, PETSC_DETERMINE, PETSC_DETERMINE));
53   PetscCall(MatSetFromOptions(A));
54   PetscCall(MatSetUp(A));
55 
56   PetscCall(SetValues(A));
57   PetscCall(CreateAndViewB(A));
58   PetscCall(AssembleAndViewA(A));
59 
60   PetscCall(MatResetHash(A));
61 
62   PetscCall(SetValues(A));
63   PetscCall(CreateAndViewB(A));
64   PetscCall(AssembleAndViewA(A));
65 
66   PetscCall(MatDestroy(&A));
67   PetscCall(PetscFinalize());
68   return 0;
69 }
70 
71 /*TEST
72 
73    test:
74       suffix: seq
75       args: -mat_type seqaij
76       filter: grep -v "Mat Object"
77 
78    test:
79       suffix: mpi
80       args: -mat_type mpiaij
81       nsize: 4
82       filter: grep -v "Mat Object"
83 
84 TEST*/
85