Actual source code: ex14.c
slepc-3.10.1 2018-10-23
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2018, 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 singular value problem 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 <slepcsvd.h>

     18: int main(int argc,char **argv)
     19: {
     20:   Mat            A;               /* operator matrix */
     21:   SVD            svd;             /* singular value problem solver context */
     22:   SVDType        type;
     23:   PetscReal      tol;
     24:   PetscInt       nsv,maxit,its;
     25:   char           filename[PETSC_MAX_PATH_LEN];
     26:   PetscViewer    viewer;
     27:   PetscBool      flg,terse;

     30:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

     32:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     33:         Load the operator matrix that defines the singular value problem
     34:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

     51:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     52:                 Create the singular value solver and set various options
     53:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     55:   /*
     56:      Create singular value solver context
     57:   */
     58:   SVDCreate(PETSC_COMM_WORLD,&svd);

     60:   /*
     61:      Set operator
     62:   */
     63:   SVDSetOperator(svd,A);

     65:   /*
     66:      Set solver parameters at runtime
     67:   */
     68:   SVDSetFromOptions(svd);

     70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     71:                       Solve the singular value system
     72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     74:   SVDSolve(svd);
     75:   SVDGetIterationNumber(svd,&its);
     76:   PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);

     78:   /*
     79:      Optional: Get some information from the solver and display it
     80:   */
     81:   SVDGetType(svd,&type);
     82:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
     83:   SVDGetDimensions(svd,&nsv,NULL,NULL);
     84:   PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %D\n",nsv);
     85:   SVDGetTolerances(svd,&tol,&maxit);
     86:   PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%D\n",(double)tol,maxit);

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

     92:   /* show detailed info unless -terse option is given by user */
     93:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
     94:   if (terse) {
     95:     SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);
     96:   } else {
     97:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
     98:     SVDReasonView(svd,PETSC_VIEWER_STDOUT_WORLD);
     99:     SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
    100:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
    101:   }
    102:   SVDDestroy(&svd);
    103:   MatDestroy(&A);
    104:   SlepcFinalize();
    105:   return ierr;
    106: }
    107: /*TEST

    109:    testset:
    110:       requires: double !complex !define(PETSC_USE_64BIT_INDICES)
    111:       args: -file ${SLEPC_DIR}/share/slepc/datafiles/matrices/rdb200.petsc -terse
    112:       test:
    113:          suffix: 1
    114:          args: -svd_nsv 4
    115:       test:
    116:          suffix: 2
    117:          args: -svd_nsv 2 -svd_type cyclic -svd_cyclic_explicitmatrix -svd_cyclic_st_type sinvert -svd_cyclic_eps_target 12.0 -svd_cyclic_st_ksp_type preonly -svd_cyclic_st_pc_type lu

    119: TEST*/