Actual source code: ex140.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests MatLoad(), MatZeroRowsColumns(), MatView() for MPIBAIJ.\n\n";

  4: #include <petscmat.h>

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

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

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

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

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

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

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

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