Actual source code: ex222.c

petsc-3.13.6 2020-09-29
Report Typos and Errors
  1: static char help[] = "Tests MatComputeOperator() and MatComputeOperatorTranspose()\n\n";

  3:  #include <petscmat.h>

  5: int main(int argc,char **argv)
  6: {
  7:   Mat            A,Ae,Aet;
  8:   char           filename[PETSC_MAX_PATH_LEN];
  9:   char           expltype[128],*etype = NULL;
 10:   PetscInt       bs = 1;
 11:   PetscBool      flg, check = PETSC_TRUE;

 14:   PetscInitialize(&argc,&argv,(char*) 0,help);if (ierr) return ierr;

 16:   PetscOptionsGetString(NULL,NULL,"-expl_type",expltype,sizeof(expltype),&flg);
 17:   if (flg) {
 18:     PetscStrallocpy(expltype,&etype);
 19:   }
 20:   PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg);
 21:   PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL);
 22:   if (!flg) {
 23:     PetscInt M = 13,N = 6;

 25:     PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);
 26:     PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);
 27:     MatCreateDense(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,M,N,NULL,&A);
 28:     MatSetBlockSize(A,bs);
 29:     MatSetRandom(A,NULL);
 30:   } else {
 31:     PetscViewer viewer;

 33:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
 34:     MatCreate(PETSC_COMM_WORLD,&A);
 35:     MatSetBlockSize(A,bs);
 36:     MatSetFromOptions(A);
 37:     MatLoad(A,viewer);
 38:     PetscViewerDestroy(&viewer);
 39:   }
 40:   PetscObjectSetName((PetscObject)A,"Matrix");
 41:   MatViewFromOptions(A,NULL,"-view_expl");

 43:   MatComputeOperator(A,etype,&Ae);
 44:   PetscObjectSetName((PetscObject)Ae,"Explicit matrix");
 45:   MatViewFromOptions(Ae,NULL,"-view_expl");

 47:   PetscOptionsGetBool(NULL,NULL,"-check",&check,NULL);
 48:   if (check) {
 49:     Mat A2;
 50:     PetscReal err,tol = PETSC_SMALL;

 52:     PetscOptionsGetReal(NULL,NULL,"-tol",&tol,NULL);
 53:     MatConvert(A,etype,MAT_INITIAL_MATRIX,&A2);
 54:     MatAXPY(A2,-1.0,Ae,DIFFERENT_NONZERO_PATTERN);
 55:     MatNorm(A2,NORM_FROBENIUS,&err);
 56:     if (err > tol) {
 57:       PetscPrintf(PETSC_COMM_WORLD,"Error %g > %g (type %s)\n",(double)err,(double)tol,etype);
 58:     }
 59:     MatDestroy(&A2);
 60:   }

 62:   MatComputeOperatorTranspose(A,etype,&Aet);
 63:   PetscObjectSetName((PetscObject)Aet,"Explicit matrix transpose");
 64:   MatViewFromOptions(Aet,NULL,"-view_expl");

 66:   PetscFree(etype);
 67:   MatDestroy(&Ae);
 68:   MatDestroy(&Aet);
 69:   MatDestroy(&A);
 70:   PetscFinalize();
 71:   return ierr;
 72: }



 76: /*TEST

 78:    test:
 79:      output_file: output/ex222_null.out

 81:    testset:
 82:      suffix: matexpl_rect
 83:      output_file: output/ex222_null.out
 84:      nsize: {{1 3}}
 85:      args: -expl_type {{dense aij baij}}

 87:    testset:
 88:      suffix: matexpl_square
 89:      output_file: output/ex222_null.out
 90:      nsize: {{1 3}}
 91:      args: -bs {{1 2 3}} -M 36 -N 36 -expl_type {{dense aij baij sbaij}}

 93: TEST*/