Date: 2017-05-17T08:27:06+00:00

Actual source code: ex17.c
slepc-3.7.4 2017-05-17
Report Typos and Errors

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

      6:    This file is part of SLEPc.

      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 polynomial eigenproblem P(l)x = 0 with matrices loaded from a file.\n\n"
     23:   "The command line options are:\n"
     24:   "-A <filename1,filename2, ...> , where <filename1,.. > = matrices A0 ... files in PETSc binary form.\n\n";

     26: #include <slepcpep.h>

     28: #define MAX_MATRICES 40

     32: int main(int argc,char **argv)
     33: {
     34:   Mat            A[MAX_MATRICES]; /* problem matrices */
     35:   PEP            pep;             /* polynomial eigenproblem solver context */
     36:   PEPType        type;
     37:   PetscReal      tol;
     38:   PetscInt       nev,maxit,its,nmat=MAX_MATRICES,i;
     39:   char*          filenames[MAX_MATRICES];
     40:   PetscViewer    viewer;
     41:   PetscBool      flg,terse;

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

     46:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     47:         Load the matrices that define the polynomial eigenproblem
     48:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     50:   PetscPrintf(PETSC_COMM_WORLD,"\nPolynomial eigenproblem stored in file.\n\n");
     51: #if defined(PETSC_USE_COMPLEX)
     52:   PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrices from binary files...\n");
     53: #else
     54:   PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrices from binary files...\n");
     55: #endif
     56:   PetscOptionsGetStringArray(NULL,NULL,"-A",filenames,&nmat,&flg);
     57:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a comma-separated list of file names with the -A option");
     58:   for (i=0;i<nmat;i++) {
     59:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filenames[i],FILE_MODE_READ,&viewer);
     60:     MatCreate(PETSC_COMM_WORLD,&A[i]);
     61:     MatSetFromOptions(A[i]);
     62:     MatLoad(A[i],viewer);
     63:     PetscViewerDestroy(&viewer);
     64:   }
     65:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     66:                 Create the eigensolver and set various options
     67:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     69:   /*
     70:      Create eigensolver context
     71:   */
     72:   PEPCreate(PETSC_COMM_WORLD,&pep);

     74:   /*
     75:      Set matrices
     76:   */
     77:   PEPSetOperators(pep,nmat,A);
     78:   /*
     79:      Set solver parameters at runtime
     80:   */
     81:   PEPSetFromOptions(pep);

     83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     84:                       Solve the eigensystem
     85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     86:
     87:   PEPSolve(pep);
     88:   PEPGetIterationNumber(pep,&its);
     89:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);

     91:   /*
     92:      Optional: Get some information from the solver and display it
     93:   */
     94:   PEPGetType(pep,&type);
     95:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
     96:   PEPGetDimensions(pep,&nev,NULL,NULL);
     97:   PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);
     98:   PEPGetTolerances(pep,&tol,&maxit);
     99:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%D\n",(double)tol,maxit);

    101:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    102:                     Display solution and clean up
    103:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    105:   /* show detailed info unless -terse option is given by user */
    106:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
    107:   if (terse) {
    108:     PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
    109:   } else {
    110:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
    111:     PEPReasonView(pep,PETSC_VIEWER_STDOUT_WORLD);
    112:     PEPErrorView(pep,PEP_ERROR_BACKWARD,PETSC_VIEWER_STDOUT_WORLD);
    113:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    114:   }
    115:   PEPDestroy(&pep);
    116:   for (i=0;i<nmat;i++) {
    117:     MatDestroy(&A[i]);
    118:     PetscFree(filenames[i]);
    119:   }
    120:   SlepcFinalize();
    121:   return ierr;
    122: }