// based on mat/examples/tutorials/ex16.c static char help[] = "Reads a matrix from PETSc binary file and saves its structure as an image. \n\n"; /* Example: ./mat2img -f -size */ #include #include #include #define call(f) ierr=(f);CHKERRQ(ierr); #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { Mat A; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscInt size; PetscErrorCode ierr; PetscBool flg; PetscMPIInt rank; PetscDraw draw; PetscViewer viewer; PetscInitialize(&argc,&args,(char*)0,help); call(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); /* Determine files from which we read the linear systems. */ call(PetscOptionsGetString(NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg)); if (!flg) { strcpy(file, "matrix.bin"); } call(PetscOptionsGetInt(NULL,"-size",&size,&flg)); if (!flg) { size = 600; } /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ call(PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd)); /* Load the matrix; then destroy the viewer. */ call(MatCreate(PETSC_COMM_WORLD,&A)); call(MatSetFromOptions(A)); call(MatLoad(A,fd)); call(PetscViewerDrawOpen(PETSC_COMM_WORLD, PETSC_NULL, file, 100, 100, size, size, &viewer)); call(MatView(A, viewer)); call(PetscViewerDrawGetDraw(viewer, 0, &draw)); call(PetscDrawFlush(draw)); call(PetscDrawSetSave(draw, "matrix", PETSC_FALSE)); call(PetscDrawSetSaveFinalImage(draw, "matrix2")); call(PetscViewerDestroy(&fd)); call(MatDestroy(&A)); call(PetscFinalize()); return 0; }