1 2 static char help[] = "Tests MatDiagonalSet() on MatLoad() matrix \n\n"; 3 4 #include <petscmat.h> 5 6 int main(int argc,char **args) 7 { 8 Mat A; 9 Vec x; 10 PetscViewer fd; /* viewer */ 11 char file[PETSC_MAX_PATH_LEN]; /* input file name */ 12 PetscReal norm; 13 PetscBool flg; 14 15 PetscCall(PetscInitialize(&argc,&args,(char*)0,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(MatLoad(A,fd)); 24 PetscCall(PetscViewerDestroy(&fd)); 25 PetscCall(MatCreateVecs(A,&x,NULL)); 26 PetscCall(MatGetDiagonal(A,x)); 27 PetscCall(VecScale(x,-1.0)); 28 PetscCall(MatDiagonalSet(A,x,ADD_VALUES)); 29 PetscCall(MatGetDiagonal(A,x)); 30 PetscCall(VecNorm(x,NORM_2,&norm)); 31 PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Norm %g\n",(double)norm)); 32 33 /* Free data structures */ 34 PetscCall(MatDestroy(&A)); 35 PetscCall(VecDestroy(&x)); 36 PetscCall(PetscFinalize()); 37 return 0; 38 } 39 40 /*TEST 41 42 test: 43 nsize: 4 44 requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES) 45 args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64 -malloc_dump 46 47 TEST*/ 48