Actual source code: ex140.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Tests MatLoad(), MatZeroRowsColumns(), MatView() for MPIBAIJ.\n\n";

  4:  #include <petscmat.h>

  6: int main(int argc,char **args)
  7: {
  8:   Mat            A;
 10:   char           file[PETSC_MAX_PATH_LEN];
 11:   PetscBool      aij,sbaij,flg;
 12:   PetscViewer    fd;
 13:   MatType        type = MATBAIJ;
 14:   PetscInt       n1   = 7, idx1[] = {1,5,6,8,9,12,15};
 15:   PetscInt       n2   = 5, idx2[] = {7,22,30,13,19};
 16:   Vec            b,x;
 17:   IS             is;
 18:   PetscInt       i;
 19:   PetscMPIInt    rank;

 21:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 22:   MPI_Comm_size(PETSC_COMM_WORLD,&rank);
 23:   PetscOptionsHasName(NULL,NULL,"-aij",&aij);
 24:   if (aij) type = MATAIJ;
 25:   PetscOptionsHasName(NULL,NULL,"-sbaij",&sbaij);
 26:   if (sbaij) type = MATSBAIJ;

 28:   PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 29:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

 31:   /*
 32:      Open binary file.  Note that we use FILE_MODE_READ to indicate
 33:      reading from this file.
 34:   */
 35:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);

 37:   /*
 38:      Load the matrix; then destroy the viewer.
 39:   */
 40:   MatCreate(PETSC_COMM_WORLD,&A);
 41:   MatSetType(A,type);
 42:   MatLoad(A,fd);
 43:   VecCreate(PETSC_COMM_WORLD,&b);
 44:   VecLoad(b,fd);
 45:   PetscViewerDestroy(&fd);

 47:   /* save original matrix and vector for testing with MATLAB */
 48:   VecView(b,PETSC_VIEWER_BINARY_WORLD);
 49:   MatView(A,PETSC_VIEWER_BINARY_WORLD);

 51:   if (!rank) {
 52:     ISCreateGeneral(PETSC_COMM_WORLD,n1,idx1,PETSC_COPY_VALUES,&is);
 53:   } else {
 54:     ISCreateGeneral(PETSC_COMM_WORLD,n2,idx2,PETSC_COPY_VALUES,&is);
 55:   }
 56:   VecDuplicate(b,&x);
 57:   VecSet(x,0.0);
 58:   if (!rank) {
 59:     for (i=0; i<n1; i++) {
 60:       VecSetValue(x,idx1[i],1.0,INSERT_VALUES);
 61:     }
 62:   } else {
 63:     for (i=0; i<n2; i++) {
 64:       VecSetValue(x,idx2[i],1.0,INSERT_VALUES);
 65:     }
 66:   }
 67:   VecAssemblyBegin(x);
 68:   VecAssemblyEnd(x);
 69:   VecView(x,PETSC_VIEWER_BINARY_WORLD);

 71:   MatZeroRowsColumnsIS(A,is,2.0,x,b);
 72:   /*
 73:      Save the matrix and vector; then destroy the viewer.
 74:   */
 75:   ISView(is,PETSC_VIEWER_BINARY_WORLD);
 76:   VecView(b,PETSC_VIEWER_BINARY_WORLD);
 77:   MatView(A,PETSC_VIEWER_BINARY_WORLD);
 78:   VecDestroy(&x);
 79:   VecDestroy(&b);
 80:   ISDestroy(&is);
 81:   MatDestroy(&A);
 82:   PetscFinalize();
 83:   return ierr;
 84: }