Actual source code: ex57.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: static char help[] = "Reads in a binary file, extracts a submatrix from it, and writes to another binary file.\n\
  3: Options:\n\
  4:   -fin  <mat>  : input matrix file\n\
  5:   -fout <mat>  : output marrix file\n\
  6:   -start <row> : the row from where the submat should be extracted\n\
  7:   -m  <sx>  : the size of the submatrix\n";

  9: #include <petscmat.h>
 10: #include <petscvec.h>

 12: int main(int argc,char **args)
 13: {
 14:   char           fin[PETSC_MAX_PATH_LEN],fout[PETSC_MAX_PATH_LEN] ="default.mat";
 15:   PetscViewer    fdin,fdout;
 16:   Vec            b;
 17:   MatType        mtype = MATSEQBAIJ;
 18:   Mat            A,*B;
 20:   PetscInt       start=0;
 21:   PetscInt       m;
 22:   IS             isrow,iscol;
 23:   PetscBool      flg;

 25:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 26:   PetscOptionsGetString(NULL,NULL,"-fin",fin,sizeof(fin),&flg);
 27:   if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fin option");
 28:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin);

 30:   PetscOptionsGetString(NULL,NULL,"-fout",fout,sizeof(fout),&flg);
 31:   if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout);}
 32:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,FILE_MODE_WRITE,&fdout);

 34:   MatCreate(PETSC_COMM_SELF,&A);
 35:   MatSetType(A,mtype);
 36:   MatLoad(A,fdin);
 37:   PetscViewerDestroy(&fdin);

 39:   MatGetSize(A,&m,&m);
 40:   m /= 2;
 41:   PetscOptionsGetInt(NULL,NULL,"-start",&start,NULL);
 42:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);

 44:   ISCreateStride(PETSC_COMM_SELF,m,start,1,&isrow);
 45:   ISCreateStride(PETSC_COMM_SELF,m,start,1,&iscol);
 46:   MatCreateSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
 47:   MatView(B[0],fdout);

 49:   VecCreate(PETSC_COMM_SELF,&b);
 50:   VecSetSizes(b,PETSC_DECIDE,m);
 51:   VecSetFromOptions(b);
 52:   MatView(B[0],fdout);
 53:   PetscViewerDestroy(&fdout);

 55:   MatDestroy(&A);
 56:   MatDestroy(&B[0]);
 57:   VecDestroy(&b);
 58:   PetscFree(B);
 59:   ISDestroy(&iscol);
 60:   ISDestroy(&isrow);
 61:   PetscFinalize();
 62:   return ierr;
 63: }

 65: /*TEST

 67:     test:
 68:       args: -fin ${DATAFILESPATH}/matrices/small -fout joe -start 2 -m 4
 69:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)

 71: TEST*/