Actual source code: ex4.c

slepc-3.17.2 2022-08-09
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[] = "Solves a standard eigensystem Ax=kx with the matrix loaded from a file.\n"
 12:   "This example works for both real and complex numbers.\n\n"
 13:   "The command line options are:\n"
 14:   "  -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";

 16: #include <slepceps.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat            A;               /* operator matrix */
 21:   EPS            eps;             /* eigenproblem solver context */
 22:   EPSType        type;
 23:   PetscReal      tol;
 24:   PetscInt       nev,maxit,its;
 25:   char           filename[PETSC_MAX_PATH_LEN];
 26:   PetscViewer    viewer;
 27:   PetscBool      flg,terse;

 29:   SlepcInitialize(&argc,&argv,(char*)0,help);

 31:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 32:         Load the operator matrix that defines the eigensystem, Ax=kx
 33:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 35:   PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n");
 36:   PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg);

 39: #if defined(PETSC_USE_COMPLEX)
 40:   PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
 41: #else
 42:   PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
 43: #endif
 44:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
 45:   MatCreate(PETSC_COMM_WORLD,&A);
 46:   MatSetFromOptions(A);
 47:   MatLoad(A,viewer);
 48:   PetscViewerDestroy(&viewer);

 50:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 51:                 Create the eigensolver and set various options
 52:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 54:   /*
 55:      Create eigensolver context
 56:   */
 57:   EPSCreate(PETSC_COMM_WORLD,&eps);

 59:   /*
 60:      Set operators. In this case, it is a standard eigenvalue problem
 61:   */
 62:   EPSSetOperators(eps,A,NULL);

 64:   /*
 65:      Set solver parameters at runtime
 66:   */
 67:   EPSSetFromOptions(eps);

 69:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 70:                       Solve the eigensystem
 71:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 73:   EPSSolve(eps);
 74:   EPSGetIterationNumber(eps,&its);
 75:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %" PetscInt_FMT "\n",its);

 77:   /*
 78:      Optional: Get some information from the solver and display it
 79:   */
 80:   EPSGetType(eps,&type);
 81:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
 82:   EPSGetDimensions(eps,&nev,NULL,NULL);
 83:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
 84:   EPSGetTolerances(eps,&tol,&maxit);
 85:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%" PetscInt_FMT "\n",(double)tol,maxit);

 87:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 88:                     Display solution and clean up
 89:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 91:   /* show detailed info unless -terse option is given by user */
 92:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
 93:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
 94:   else {
 95:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
 96:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
 97:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
 98:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 99:   }
100:   EPSDestroy(&eps);
101:   MatDestroy(&A);
102:   SlepcFinalize();
103:   return 0;
104: }

106: /*TEST

108:    test:
109:       suffix: 1
110:       args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -eps_nev 4 -terse
111:       requires: double !complex !defined(PETSC_USE_64BIT_INDICES)

113:    testset:
114:       args: -file ${DATAFILESPATH}/matrices/complex/qc324.petsc -eps_type ciss -terse
115:       requires: double complex datafilespath !defined(PETSC_USE_64BIT_INDICES)
116:       test:
117:          suffix: ciss_1
118:          args: -rg_type ellipse -rg_ellipse_center -.012-.08i -rg_ellipse_radius .05
119:       test:
120:          suffix: ciss_2
121:          args: -rg_type interval -rg_interval_endpoints -0.062,.038,-.13,-.03 -eps_max_it 1

123: TEST*/