Actual source code: ex4.c

slepc-3.22.2 2024-12-02
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:   PetscFunctionBeginUser;
 30:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));

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

 36:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n"));
 37:   PetscCall(PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&flg));
 38:   PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Must indicate a file name with the -file option");

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

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

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

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

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

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

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

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

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

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

107: /*TEST

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

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

124: TEST*/