Actual source code: test14.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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 EPS interface functions.\n\n";

 13: #include <slepceps.h>

 15: int main(int argc,char **argv)
 16: {
 17:   Mat                A,B;         /* problem matrix */
 18:   EPS                eps;         /* eigenproblem solver context */
 19:   ST                 st;
 20:   KSP                ksp;
 21:   DS                 ds;
 22:   PetscReal          cut,tol;
 23:   PetscScalar        target;
 24:   PetscInt           n=20,i,its,nev,ncv,mpd,Istart,Iend;
 25:   PetscBool          flg,pur,track;
 26:   EPSConvergedReason reason;
 27:   EPSType            type;
 28:   EPSExtraction      extr;
 29:   EPSBalance         bal;
 30:   EPSWhich           which;
 31:   EPSConv            conv;
 32:   EPSStop            stop;
 33:   EPSProblemType     ptype;
 34:   PetscErrorCode     ierr;
 35:   PetscViewerAndFormat *vf;

 37:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 38:   PetscPrintf(PETSC_COMM_WORLD,"\nDiagonal Eigenproblem, n=%D\n\n",n);

 40:   MatCreate(PETSC_COMM_WORLD,&A);
 41:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
 42:   MatSetFromOptions(A);
 43:   MatSetUp(A);
 44:   MatGetOwnershipRange(A,&Istart,&Iend);
 45:   for (i=Istart;i<Iend;i++) {
 46:     MatSetValue(A,i,i,i+1,INSERT_VALUES);
 47:   }
 48:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 49:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 52:              Create eigensolver and test interface functions
 53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 54:   EPSCreate(PETSC_COMM_WORLD,&eps);
 55:   EPSSetOperators(eps,A,NULL);
 56:   EPSGetOperators(eps,&B,NULL);
 57:   MatView(B,NULL);

 59:   EPSSetType(eps,EPSKRYLOVSCHUR);
 60:   EPSGetType(eps,&type);
 61:   PetscPrintf(PETSC_COMM_WORLD," Type set to %s\n",type);

 63:   EPSGetProblemType(eps,&ptype);
 64:   PetscPrintf(PETSC_COMM_WORLD," Problem type before changing = %d",(int)ptype);
 65:   EPSSetProblemType(eps,EPS_HEP);
 66:   EPSGetProblemType(eps,&ptype);
 67:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d.",(int)ptype);
 68:   EPSIsGeneralized(eps,&flg);
 69:   if (flg) { PetscPrintf(PETSC_COMM_WORLD," generalized"); }
 70:   EPSIsHermitian(eps,&flg);
 71:   if (flg) { PetscPrintf(PETSC_COMM_WORLD," hermitian"); }
 72:   EPSIsPositive(eps,&flg);
 73:   if (flg) { PetscPrintf(PETSC_COMM_WORLD," positive"); }

 75:   EPSGetExtraction(eps,&extr);
 76:   PetscPrintf(PETSC_COMM_WORLD,"\n Extraction before changing = %d",(int)extr);
 77:   EPSSetExtraction(eps,EPS_HARMONIC);
 78:   EPSGetExtraction(eps,&extr);
 79:   PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)extr);

 81:   EPSSetBalance(eps,EPS_BALANCE_ONESIDE,8,1e-6);
 82:   EPSGetBalance(eps,&bal,&its,&cut);
 83:   PetscPrintf(PETSC_COMM_WORLD," Balance: %s, its=%D, cutoff=%g\n",EPSBalanceTypes[bal],its,(double)cut);

 85:   EPSSetPurify(eps,PETSC_FALSE);
 86:   EPSGetPurify(eps,&pur);
 87:   PetscPrintf(PETSC_COMM_WORLD," Eigenvector purification: %s\n",pur?"on":"off");
 88:   EPSGetTrackAll(eps,&track);

 90:   EPSSetTarget(eps,4.8);
 91:   EPSGetTarget(eps,&target);
 92:   EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
 93:   EPSGetWhichEigenpairs(eps,&which);
 94:   PetscPrintf(PETSC_COMM_WORLD," Which = %d, target = %g\n",(int)which,(double)PetscRealPart(target));

 96:   EPSSetDimensions(eps,4,PETSC_DEFAULT,PETSC_DEFAULT);
 97:   EPSGetDimensions(eps,&nev,&ncv,&mpd);
 98:   PetscPrintf(PETSC_COMM_WORLD," Dimensions: nev=%D, ncv=%D, mpd=%D\n",nev,ncv,mpd);

100:   EPSSetTolerances(eps,2.2e-4,200);
101:   EPSGetTolerances(eps,&tol,&its);
102:   PetscPrintf(PETSC_COMM_WORLD," Tolerance = %.5f, max_its = %D\n",(double)tol,its);

104:   EPSSetConvergenceTest(eps,EPS_CONV_ABS);
105:   EPSGetConvergenceTest(eps,&conv);
106:   EPSSetStoppingTest(eps,EPS_STOP_BASIC);
107:   EPSGetStoppingTest(eps,&stop);
108:   PetscPrintf(PETSC_COMM_WORLD," Convergence test = %d, stopping test = %d\n",(int)conv,(int)stop);

110:   PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf);
111:   EPSMonitorSet(eps,(PetscErrorCode (*)(EPS,PetscInt,PetscInt,PetscScalar*,PetscScalar*,PetscReal*,PetscInt,void*))EPSMonitorFirst,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
112:   EPSMonitorCancel(eps);

114:   EPSGetST(eps,&st);
115:   STGetKSP(st,&ksp);
116:   KSPSetTolerances(ksp,1e-8,1e-35,PETSC_DEFAULT,PETSC_DEFAULT);
117:   STView(st,NULL);
118:   EPSGetDS(eps,&ds);
119:   DSView(ds,NULL);

121:   EPSSetFromOptions(eps);
122:   EPSSolve(eps);
123:   EPSGetConvergedReason(eps,&reason);
124:   EPSGetIterationNumber(eps,&its);
125:   PetscPrintf(PETSC_COMM_WORLD," Finished - converged reason = %d, its=%D\n",(int)reason,its);

127:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128:                     Display solution and clean up
129:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
130:   EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
131:   EPSDestroy(&eps);
132:   MatDestroy(&A);
133:   SlepcFinalize();
134:   return ierr;
135: }

137: /*TEST

139:    test:
140:       suffix: 1
141:       args: -eps_ncv 14

143: TEST*/