Actual source code: test3.c

slepc-3.17.0 2022-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */

 11: static char help[] = "Test MFN interface functions.\n\n"
 12:   "The command line options are:\n"
 13:   "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n\n";

 15: #include <slepcmfn.h>

 17: int main(int argc,char **argv)
 18: {
 19:   Mat                  A,B;
 20:   MFN                  mfn;
 21:   FN                   f;
 22:   MFNConvergedReason   reason;
 23:   MFNType              type;
 24:   PetscReal            norm,tol;
 25:   Vec                  v,y;
 26:   PetscInt             N,n=4,Istart,Iend,i,j,II,ncv,its,maxit;
 27:   PetscBool            flg,testprefix=PETSC_FALSE;
 28:   const char           *prefix;
 29:   PetscViewerAndFormat *vf;

 31:   SlepcInitialize(&argc,&argv,(char*)0,help);
 32:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 33:   N = n*n;
 34:   PetscPrintf(PETSC_COMM_WORLD,"\nSquare root of Laplacian y=sqrt(A)*e_1, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,n);
 35:   PetscOptionsGetBool(NULL,NULL,"-test_prefix",&testprefix,NULL);

 37:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 38:                  Compute the discrete 2-D Laplacian, A
 39:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 41:   MatCreate(PETSC_COMM_WORLD,&A);
 42:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 43:   MatSetFromOptions(A);
 44:   MatSetUp(A);

 46:   MatGetOwnershipRange(A,&Istart,&Iend);
 47:   for (II=Istart;II<Iend;II++) {
 48:     i = II/n; j = II-i*n;
 49:     if (i>0) MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);
 50:     if (i<n-1) MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);
 51:     if (j>0) MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);
 52:     if (j<n-1) MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);
 53:     MatSetValue(A,II,II,4.0,INSERT_VALUES);
 54:   }

 56:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 57:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 58:   MatSetOption(A,MAT_HERMITIAN,PETSC_TRUE);

 60:   MatCreateVecs(A,NULL,&v);
 61:   VecSetValue(v,0,1.0,INSERT_VALUES);
 62:   VecAssemblyBegin(v);
 63:   VecAssemblyEnd(v);
 64:   VecDuplicate(v,&y);

 66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 67:              Create the solver, set the matrix and the function
 68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 69:   MFNCreate(PETSC_COMM_WORLD,&mfn);
 70:   MFNSetOperator(mfn,A);
 71:   MFNGetFN(mfn,&f);
 72:   FNSetType(f,FNSQRT);

 74:   MFNSetType(mfn,MFNKRYLOV);
 75:   MFNGetType(mfn,&type);
 76:   PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type);

 78:   /* test prefix usage */
 79:   if (testprefix) {
 80:     MFNSetOptionsPrefix(mfn,"check_");
 81:     MFNAppendOptionsPrefix(mfn,"myprefix_");
 82:     MFNGetOptionsPrefix(mfn,&prefix);
 83:     PetscPrintf(PETSC_COMM_WORLD," MFN prefix is currently: %s\n",prefix);
 84:   }

 86:   /* test some interface functions */
 87:   MFNGetOperator(mfn,&B);
 88:   MatView(B,PETSC_VIEWER_STDOUT_WORLD);
 89:   MFNSetTolerances(mfn,1e-4,500);
 90:   MFNSetDimensions(mfn,6);
 91:   MFNSetErrorIfNotConverged(mfn,PETSC_TRUE);
 92:   /* test monitors */
 93:   PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf);
 94:   MFNMonitorSet(mfn,(PetscErrorCode (*)(MFN,PetscInt,PetscReal,void*))MFNMonitorDefault,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
 95:   /* MFNMonitorCancel(mfn); */
 96:   MFNSetFromOptions(mfn);

 98:   /* query properties and print them */
 99:   MFNGetTolerances(mfn,&tol,&maxit);
100:   PetscPrintf(PETSC_COMM_WORLD," Tolerance: %g, max iterations: %" PetscInt_FMT "\n",(double)tol,maxit);
101:   MFNGetDimensions(mfn,&ncv);
102:   PetscPrintf(PETSC_COMM_WORLD," Subspace dimension: %" PetscInt_FMT "\n",ncv);
103:   MFNGetErrorIfNotConverged(mfn,&flg);
104:   if (flg) PetscPrintf(PETSC_COMM_WORLD," Erroring out if convergence fails\n");

106:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107:                            Solve  y=sqrt(A)*v
108:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

110:   MFNSolve(mfn,v,y);
111:   MFNGetConvergedReason(mfn,&reason);
112:   MFNGetIterationNumber(mfn,&its);
113:   PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d\n",(int)reason);
114:   /* PetscPrintf(PETSC_COMM_WORLD," its = %" PetscInt_FMT "\n",its); */
115:   VecNorm(y,NORM_2,&norm);
116:   PetscPrintf(PETSC_COMM_WORLD," sqrt(A)*v has norm %g\n",(double)norm);

118:   /*
119:      Free work space
120:   */
121:   MFNDestroy(&mfn);
122:   MatDestroy(&A);
123:   VecDestroy(&v);
124:   VecDestroy(&y);
125:   SlepcFinalize();
126:   return 0;
127: }

129: /*TEST

131:    test:
132:       suffix: 1
133:       args: -mfn_monitor_cancel -mfn_converged_reason -mfn_view -log_exclude mfn,bv,fn -mfn_monitor draw::draw_lg -draw_virtual

135:    test:
136:       suffix: 2
137:       args: -test_prefix -check_myprefix_mfn_monitor
138:       filter: sed -e "s/estimate [0-9]\.[0-9]*e[+-]\([0-9]*\)/estimate (removed)/g" | sed -e "s/4.0[0-9]*e-10/4.03e-10/"

140: TEST*/