Date: 2016-09-29T11:12:15+00:00

Actual source code: ex14.c
slepc-3.7.3 2016-09-29
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 singular value problem 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 <slepcsvd.h>

     31: int main(int argc,char **argv)
     32: {
     33:   Mat            A;               /* operator matrix */
     34:   SVD            svd;             /* singular value problem solver context */
     35:   SVDType        type;
     36:   PetscReal      tol;
     37:   PetscInt       nsv,maxit,its;
     38:   char           filename[PETSC_MAX_PATH_LEN];
     39:   PetscViewer    viewer;
     40:   PetscBool      flg,terse;

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

     45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     46:         Load the operator matrix that defines the singular value problem
     47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     49:   PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n");
     50:   PetscOptionsGetString(NULL,NULL,"-file",filename,PETSC_MAX_PATH_LEN,&flg);
     51:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name with the -file option");

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

     64:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     65:                 Create the singular value solver and set various options
     66:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     68:   /*
     69:      Create singular value solver context
     70:   */
     71:   SVDCreate(PETSC_COMM_WORLD,&svd);

     73:   /*
     74:      Set operator
     75:   */
     76:   SVDSetOperator(svd,A);

     78:   /*
     79:      Set solver parameters at runtime
     80:   */
     81:   SVDSetFromOptions(svd);

     83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     84:                       Solve the singular value system
     85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     87:   SVDSolve(svd);
     88:   SVDGetIterationNumber(svd,&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:   SVDGetType(svd,&type);
     95:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
     96:   SVDGetDimensions(svd,&nsv,NULL,NULL);
     97:   PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %D\n",nsv);
     98:   SVDGetTolerances(svd,&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:     SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
    109:   } else {
    110:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
    111:     SVDReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
    112:     SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
    113:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    114:   }
    115:   SVDDestroy(&svd);
    116:   MatDestroy(&A);
    117:   SlepcFinalize();
    118:   return ierr;
    119: }