Actual source code: ex14.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 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:   const SVDType  type;
 36:   PetscReal      tol;
 37:   PetscInt       nsv,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 singular value problem
 47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 49:   PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem 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 singular value solver and set various options
 68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

 75:   /* 
 76:      Set operator
 77:   */
 78:   SVDSetOperator(svd,A);

 80:   /*
 81:      Set solver parameters at runtime
 82:   */
 83:   SVDSetFromOptions(svd);

 85:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 86:                       Solve the singular value system
 87:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 89:   SVDSolve(svd);
 90:   SVDGetIterationNumber(svd,&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:   SVDGetType(svd,&type);
 97:   PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
 98:   SVDGetDimensions(svd,&nsv,PETSC_NULL,PETSC_NULL);
 99:   PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %D\n",nsv);
100:   SVDGetTolerances(svd,&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:   SVDPrintSolution(svd,PETSC_NULL);
108:   SVDDestroy(&svd);
109:   MatDestroy(&A);
110:   SlepcFinalize();
111:   return 0;
112: }