Actual source code: ex9.c

petsc-3.10.5 2019-03-28
Report Typos and Errors

  2: static char help[] = "Tests MatCreateComposite()\n\n";

  4: /*T
  5:    Concepts: Mat^composite matrices
  6:    Processors: n
  7: T*/

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

 18: int main(int argc,char **args)
 19: {
 20:   Mat            A[3],B;                       /* matrix */
 21:   PetscViewer    fd;                      /* viewer */
 22:   char           file[PETSC_MAX_PATH_LEN];            /* input file name */
 24:   PetscBool      flg;
 25:   Vec            x,y,z,work;
 26:   PetscReal      rnorm;

 28:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 29:   /*
 30:      Determine files from which we read the two linear systems
 31:      (matrix and right-hand-side vector).
 32:   */
 33:   PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 34:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

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

 42:   /*
 43:      Load the matrix; then destroy the viewer.
 44:   */
 45:   MatCreate(PETSC_COMM_WORLD,&A[0]);
 46:   MatLoad(A[0],fd);
 47:   PetscViewerDestroy(&fd);

 49:   MatDuplicate(A[0],MAT_COPY_VALUES,&A[1]);
 50:   MatDuplicate(A[0],MAT_COPY_VALUES,&A[2]);
 51:   MatShift(A[1],1.0);
 52:   MatShift(A[1],2.0);

 54:   MatCreateVecs(A[0],&x,&y);
 55:   VecDuplicate(y,&work);
 56:   VecDuplicate(y,&z);

 58:   VecSet(x,1.0);
 59:   MatMult(A[0],x,z);
 60:   MatMultAdd(A[1],x,z,z);
 61:   MatMultAdd(A[2],x,z,z);

 63:   MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);
 64:   MatMult(B,x,y);
 65:   MatDestroy(&B);
 66:   VecAXPY(y,-1.0,z);
 67:   VecNorm(y,NORM_2,&rnorm);
 68:   if (rnorm > 1.e-10) {
 69:     PetscPrintf(PETSC_COMM_WORLD,"Error with composite add %g\n",(double)rnorm);
 70:   }

 72:   MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);
 73:   MatCompositeMerge(B);
 74:   MatMult(B,x,y);
 75:   MatDestroy(&B);
 76:   VecAXPY(y,-1.0,z);
 77:   VecNorm(y,NORM_2,&rnorm);
 78:   if (rnorm > 1.e-10) {
 79:     PetscPrintf(PETSC_COMM_WORLD,"Error with composite add after merge %g\n",(double)rnorm);
 80:   }

 82:   VecSet(x,1.0);
 83:   MatMult(A[0],x,z);
 84:   MatMult(A[1],z,work);
 85:   MatMult(A[2],work,z);

 87:   MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);
 88:   MatCompositeSetType(B,MAT_COMPOSITE_MULTIPLICATIVE);
 89:   MatMult(B,x,y);
 90:   MatDestroy(&B);
 91:   VecAXPY(y,-1.0,z);
 92:   VecNorm(y,NORM_2,&rnorm);
 93:   if (rnorm > 1.e-10) {
 94:     PetscPrintf(PETSC_COMM_WORLD,"Error with composite multiplicative %g\n",(double)rnorm);
 95:   }

 97:   MatCreateComposite(PETSC_COMM_WORLD,3,A,&B);
 98:   MatCompositeSetType(B,MAT_COMPOSITE_MULTIPLICATIVE);
 99:   MatCompositeMerge(B);
100:   MatMult(B,x,y);
101:   MatDestroy(&B);
102:   VecAXPY(y,-1.0,z);
103:   VecNorm(y,NORM_2,&rnorm);
104:   if (rnorm > 1.e-10) {
105:     PetscPrintf(PETSC_COMM_WORLD,"Error with composite multiplicative after merge %g\n",(double)rnorm);
106:   }

108:   /*
109:      Free work space.  All PETSc objects should be destroyed when they
110:      are no longer needed.
111:   */
112:   VecDestroy(&x);
113:   VecDestroy(&y);
114:   VecDestroy(&work);
115:   VecDestroy(&z);
116:   MatDestroy(&A[0]);
117:   MatDestroy(&A[1]);
118:   MatDestroy(&A[2]);

120:   PetscFinalize();
121:   return ierr;
122: }





128: /*TEST

130:    test:
131:       nsize: 2
132:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
133:       args: -f ${DATAFILESPATH}/matrices/medium -viewer_binary_skip_info
134:       TODO: Need to develop comparison test

136: TEST*/