Actual source code: ex171.c
petsc-3.14.6 2021-03-30
2: static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat A;
9: Vec x;
11: PetscViewer fd; /* viewer */
12: char file[PETSC_MAX_PATH_LEN]; /* input file name */
13: PetscReal norm;
14: PetscBool flg;
16: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
17: /* Determine file from which we read the matrix A */
18: PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg);
19: if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option");
21: /* Load matrix A */
22: PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);
23: MatCreate(PETSC_COMM_WORLD,&A);
24: MatLoad(A,fd);
25: PetscViewerDestroy(&fd);
26: MatCreateVecs(A,&x,NULL);
27: MatGetDiagonal(A,x);
28: VecScale(x,-1.0);
29: MatDiagonalSet(A,x,ADD_VALUES);
30: MatGetDiagonal(A,x);
31: VecNorm(x,NORM_2,&norm);
32: PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm);
34: /* Free data structures */
35: MatDestroy(&A);
36: VecDestroy(&x);
37: PetscFinalize();
38: return ierr;
39: }
42: /*TEST
44: test:
45: nsize: 4
46: requires: datafilespath !complex double !define(PETSC_USE_64BIT_INDICES)
47: args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump
49: TEST*/