Actual source code: ex4.c

  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:       
  8:    SLEPc is free software: you can redistribute it and/or modify it under  the
  9:    terms of version 3 of the GNU Lesser General Public License as published by
 10:    the Free Software Foundation.

 12:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 13:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 14:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 15:    more details.

 17:    You  should have received a copy of the GNU Lesser General  Public  License
 18:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 19:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 20: */

 22: static char help[] = "Solves a standard eigensystem Ax=kx with the matrix loaded from a file.\n"
 23:   "This example works for both real and complex numbers.\n\n"
 24:   "The command line options are:\n"
 25:   "  -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";

 27: #include <slepceps.h>

 31: int main(int argc,char **argv)
 32: {
 33:   Mat            A;               /* operator matrix */
 34:   EPS            eps;             /* eigenproblem solver context */
 35:   const EPSType  type;
 36:   PetscReal      tol;
 37:   PetscInt       nev,maxit,its;
 38:   char           filename[PETSC_MAX_PATH_LEN];
 39:   PetscViewer    viewer;
 40:   PetscBool      flg;

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

 45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 46:         Load the operator matrix that defines the eigensystem, Ax=kx
 47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 49:   PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n");
 50:   PetscOptionsGetString(PETSC_NULL,"-file",filename,PETSC_MAX_PATH_LEN,&flg);
 51:   if (!flg) {
 52:     SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name with the -file option.");
 53:   }

 55: #if defined(PETSC_USE_COMPLEX)
 56:   PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
 57: #else
 58:   PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
 59: #endif
 60:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
 61:   MatCreate(PETSC_COMM_WORLD,&A);
 62:   MatSetFromOptions(A);
 63:   MatLoad(A,viewer);
 64:   PetscViewerDestroy(&viewer);

 66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 67:                 Create the eigensolver and set various options
 68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 70:   /* 
 71:      Create eigensolver context
 72:   */
 73:   EPSCreate(PETSC_COMM_WORLD,&eps);

 75:   /* 
 76:      Set operators. In this case, it is a standard eigenvalue problem
 77:   */
 78:   EPSSetOperators(eps,A,PETSC_NULL);

 80:   /*
 81:      Set solver parameters at runtime
 82:   */
 83:   EPSSetFromOptions(eps);

 85:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 86:                       Solve the eigensystem
 87:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 89:   EPSSolve(eps);
 90:   EPSGetIterationNumber(eps,&its);
 91:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);

 93:   /*
 94:      Optional: Get some information from the solver and display it
 95:   */
 96:   EPSGetType(eps,&type);
 97:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
 98:   EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);
 99:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);
100:   EPSGetTolerances(eps,&tol,&maxit);
101:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);

103:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
104:                     Display solution and clean up
105:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

107:   EPSPrintSolution(eps,PETSC_NULL);
108:   EPSDestroy(&eps);
109:   MatDestroy(&A);
110:   SlepcFinalize();
111:   return 0;
112: }