Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1376 slepc 1
/*
2
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1672 slepc 3
   SLEPc - Scalable Library for Eigenvalue Problem Computations
2116 eromero 4
   Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
1294 slepc 5
 
1672 slepc 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.
11
 
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.
16
 
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/>.
1376 slepc 19
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20
*/
21
 
1294 slepc 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";
26
 
2283 jroman 27
#include <slepcsvd.h>
1294 slepc 28
 
29
#undef __FUNCT__
30
#define __FUNCT__ "main"
31
int main( int argc, char **argv )
32
{
2316 jroman 33
  Mat            A;               /* operator matrix */
34
  SVD            svd;             /* singular value problem solver context */
1507 slepc 35
  const SVDType  type;
2316 jroman 36
  PetscReal      error, tol, sigma;
37
  PetscInt       nsv, maxit, i, its, nconv;
38
  char           filename[256];
39
  PetscViewer    viewer;
40
  PetscBool      flg;
2317 jroman 41
  PetscErrorCode ierr;
1294 slepc 42
 
43
  SlepcInitialize(&argc,&argv,(char*)0,help);
44
 
45
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46
        Load the operator matrix that defines the singular value problem
47
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
48
 
49
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nSingular value problem stored in file.\n\n");CHKERRQ(ierr);
50
  ierr = PetscOptionsGetString(PETSC_NULL,"-file",filename,256,&flg);CHKERRQ(ierr);
51
  if (!flg) {
2214 jroman 52
    SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name with the -file option.");
1294 slepc 53
  }
54
 
55
#if defined(PETSC_USE_COMPLEX)
56
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");CHKERRQ(ierr);
57
#else
58
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");CHKERRQ(ierr);
59
#endif
60
  ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
2246 eromero 61
  ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
62
  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
63
  ierr = MatLoad(A,viewer);CHKERRQ(ierr);
2305 jroman 64
  ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1294 slepc 65
 
66
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
67
                Create the singular value solver and set various options
68
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
69
 
70
  /*
71
     Create singular value solver context
72
  */
73
  ierr = SVDCreate(PETSC_COMM_WORLD,&svd);CHKERRQ(ierr);
74
 
75
  /*
76
     Set operator
77
  */
78
  ierr = SVDSetOperator(svd,A);CHKERRQ(ierr);
79
 
80
  /*
81
     Set solver parameters at runtime
82
  */
83
  ierr = SVDSetFromOptions(svd);CHKERRQ(ierr);
84
 
85
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
                      Solve the singular value system
87
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
88
 
89
  ierr = SVDSolve(svd);CHKERRQ(ierr);
90
  ierr = SVDGetIterationNumber(svd, &its);CHKERRQ(ierr);
91
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr);
92
 
93
  /*
94
     Optional: Get some information from the solver and display it
95
  */
96
  ierr = SVDGetType(svd,&type);CHKERRQ(ierr);
97
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
1575 slepc 98
  ierr = SVDGetDimensions(svd,&nsv,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1294 slepc 99
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested singular values: %d\n",nsv);CHKERRQ(ierr);
100
  ierr = SVDGetTolerances(svd,&tol,&maxit);CHKERRQ(ierr);
101
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr);
102
 
103
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104
                    Display solution and clean up
105
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106
 
107
  /*
108
     Get number of converged singular triplets
109
  */
110
  ierr = SVDGetConverged(svd,&nconv);CHKERRQ(ierr);
111
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate singular triplets: %d\n\n",nconv);CHKERRQ(ierr);
112
 
113
  if (nconv>0) {
114
    /*
115
       Display singular values and relative errors
116
    */
117
    ierr = PetscPrintf(PETSC_COMM_WORLD,
118
         "          sigma           residual norm\n"
119
         "  --------------------- ------------------\n" );CHKERRQ(ierr);
120
    for( i=0; i<nconv; i++ ) {
121
      /*
122
         Get converged singular triplets: i-th singular value is stored in sigma
123
      */
124
      ierr = SVDGetSingularTriplet(svd,i,&sigma,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
125
 
126
      /*
127
         Compute the error associated to each singular triplet
128
      */
1317 slepc 129
      ierr = SVDComputeRelativeError(svd,i,&error);CHKERRQ(ierr);
2330 jroman 130
      ierr = PetscPrintf(PETSC_COMM_WORLD,"       % 6f      ",sigma);CHKERRQ(ierr);
1294 slepc 131
      ierr = PetscPrintf(PETSC_COMM_WORLD," % 12g\n",error);CHKERRQ(ierr);
132
    }
133
    ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
134
  }
135
 
136
  /*
137
     Free work space
138
  */
2312 jroman 139
  ierr = SVDDestroy(&svd);CHKERRQ(ierr);
2305 jroman 140
  ierr = MatDestroy(&A);CHKERRQ(ierr);
1294 slepc 141
  ierr = SlepcFinalize();CHKERRQ(ierr);
142
  return 0;
143
}