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