Actual source code: ex12.c


  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: }

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

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

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

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

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

106: /*TEST

108:    test:
109:       args: -f0 ${wPETSC_DIR}/share/petsc/datafiles/matrices/ns-real-int32-float64
110:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)

112: TEST*/