Actual source code: ex12.c

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

  2: static char help[] = "Reads a PETSc matrix and vector from a file; expands the matrix with the vector\n\n";

  4: /*T
  5:    Concepts: Mat^ordering a matrix - loading a binary matrix and vector;
  6:    Concepts: Mat^loading a binary matrix and vector;
  7:    Concepts: Vectors^loading a binary vector;
  8:    Concepts: PetscLog^preloading executable
  9:    Processors: 1
 10: T*/

 12: /*
 13:   Include "petscmat.h" so that we can use matrices.
 14:   automatically includes:
 15:      petscsys.h       - base PETSc routines   petscvec.h    - vectors
 16:      petscmat.h    - matrices
 17:      petscis.h     - index sets            petscviewer.h - viewers
 18: */
 19:  #include <petscmat.h>

 21: /*

 23:    Adds a new column and row to the vector (the last) containing the vector
 24: */
 25: PetscErrorCode PadMatrix(Mat A,Vec v,PetscScalar c,Mat *B)
 26: {
 27:   PetscErrorCode    ierr;
 28:   PetscInt          n,i,*cnt,*indices,nc;
 29:   const PetscInt    *aj;
 30:   const PetscScalar *vv,*aa;

 33:   MatGetSize(A,&n,NULL);
 34:   VecGetArrayRead(v,&vv);
 35:   PetscMalloc1(n,&indices);
 36:   for (i=0; i<n; i++) indices[i] = i;

 38:   /* determine number of nonzeros per row in the new matrix */
 39:   PetscMalloc1(n+1,&cnt);
 40:   for (i=0; i<n; i++) {
 41:     MatGetRow(A,i,&nc,NULL,NULL);
 42:     cnt[i] = nc + (vv[i] != 0.0);
 43:     MatRestoreRow(A,i,&nc,NULL,NULL);
 44:   }
 45:   cnt[n] = 1;
 46:   for (i=0; i<n; i++) {
 47:     cnt[n] += (vv[i] != 0.0);
 48:   }
 49:   MatCreateSeqAIJ(PETSC_COMM_SELF,n+1,n+1,0,cnt,B);
 50:   MatSetOption(*B,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);

 52:   /* copy over the matrix entries from the matrix and then the vector */
 53:   for (i=0; i<n; i++) {
 54:     MatGetRow(A,i,&nc,&aj,&aa);
 55:     MatSetValues(*B,1,&i,nc,aj,aa,INSERT_VALUES);
 56:     MatRestoreRow(A,i,&nc,&aj,&aa);
 57:   }
 58:   MatSetValues(*B,1,&n,n,indices,vv,INSERT_VALUES);
 59:   MatSetValues(*B,n,indices,1,&n,vv,INSERT_VALUES);
 60:   MatSetValues(*B,1,&n,1,&n,&c,INSERT_VALUES);

 62:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
 63:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
 64:   VecRestoreArrayRead(v,&vv);
 65:   PetscFree(cnt);
 66:   PetscFree(indices);
 67:   return(0);
 68: }


 71: int main(int argc,char **args)
 72: {
 73:   Mat            A,B;
 74:   PetscViewer    fd;                        /* viewer */
 75:   char           file[PETSC_MAX_PATH_LEN];  /* input file name */
 77:   PetscBool      flg;
 78:   Vec            v;

 80:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 81:   /*
 82:      Determine files from which we read the two linear systems
 83:      (matrix and right-hand-side vector).
 84:   */
 85:   PetscOptionsGetString(NULL,NULL,"-f0",file,PETSC_MAX_PATH_LEN,&flg);
 86:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option");

 88:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);

 90:   MatCreate(PETSC_COMM_WORLD,&A);
 91:   MatSetType(A,MATSEQAIJ);
 92:   MatLoad(A,fd);
 93:   VecCreate(PETSC_COMM_WORLD,&v);
 94:   VecLoad(v,fd);
 95:   MatView(A,PETSC_VIEWER_STDOUT_SELF);
 96:   PadMatrix(A,v,3.0,&B);
 97:   MatView(B,PETSC_VIEWER_STDOUT_SELF);
 98:   MatDestroy(&B);
 99:   MatDestroy(&A);
100:   VecDestroy(&v);
101:   PetscViewerDestroy(&fd);

103:   PetscFinalize();
104:   return ierr;
105: }