xref: /petsc/src/mat/tests/ex171.c (revision 732aec7a18f2199fb53bb9a2f3aef439a834ce31)
1 static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n";
2 
3 #include <petscmat.h>
4 
main(int argc,char ** args)5 int main(int argc, char **args)
6 {
7   Mat         A;
8   Vec         x;
9   PetscViewer fd;                       /* viewer */
10   char        file[PETSC_MAX_PATH_LEN]; /* input file name */
11   PetscReal   norm;
12   PetscBool   flg;
13 
14   PetscFunctionBeginUser;
15   PetscCall(PetscInitialize(&argc, &args, NULL, help));
16   /* Determine file from which we read the matrix A */
17   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
18   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");
19 
20   /* Load matrix A */
21   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
22   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
23   PetscCall(MatSetFromOptions(A));
24   PetscCall(MatLoad(A, fd));
25   PetscCall(PetscViewerDestroy(&fd));
26   PetscCall(MatCreateVecs(A, &x, NULL));
27   PetscCall(MatGetDiagonal(A, x));
28   PetscCall(VecScale(x, -1.0));
29   PetscCall(MatDiagonalSet(A, x, ADD_VALUES));
30   PetscCall(MatGetDiagonal(A, x));
31   PetscCall(VecNorm(x, NORM_2, &norm));
32   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Norm %g\n", (double)norm));
33 
34   /* Free data structures */
35   PetscCall(MatDestroy(&A));
36   PetscCall(VecDestroy(&x));
37   PetscCall(PetscFinalize());
38   return 0;
39 }
40 
41 /*TEST
42 
43    testset:
44       nsize: 4
45       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
46       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
47       output_file: output/ex171_1.out
48 
49       test:
50         suffix: 1
51         args: -mat_type aij
52 
53       test:
54         suffix: 2
55         requires: kokkos_kernels
56         args: -mat_type aijkokkos
57 
58 TEST*/
59