Subversion Repositories slepc-dev

Rev

Go to most recent revision | 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
6 dsic.upv.es!jroman 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
 
1156 slepc 22
static char help[] = "Solves a generalized eigensystem Ax=kBx with matrices loaded from a file.\n"
6 dsic.upv.es!jroman 23
  "This example works for both real and complex numbers.\n\n"
1156 slepc 24
  "The command line options are:\n"
6 dsic.upv.es!jroman 25
  "  -f1 <filename>, where <filename> = matrix (A) file in PETSc binary form.\n"
26
  "  -f2 <filename>, where <filename> = matrix (B) file in PETSc binary form.\n\n";
27
 
28
#include "slepceps.h"
29
 
30
#undef __FUNCT__
31
#define __FUNCT__ "main"
32
int main( int argc, char **argv )
33
{
983 slepc 34
  Mat            A,B;             /* matrices */
35
  EPS            eps;             /* eigenproblem solver context */
1507 slepc 36
  const EPSType  type;
983 slepc 37
  PetscReal      error, tol, re, im;
38
  PetscScalar    kr, ki;
39
  PetscErrorCode ierr;
1505 slepc 40
  PetscInt       nev, maxit, i, its, lits, nconv;
983 slepc 41
  char           filename[256];
42
  PetscViewer    viewer;
43
  PetscTruth     flg;
6 dsic.upv.es!jroman 44
 
45
 
46
  SlepcInitialize(&argc,&argv,(char*)0,help);
47
 
48
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
        Load the matrices that define the eigensystem, Ax=kBx
50
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
51
 
52
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized eigenproblem stored in file.\n\n");CHKERRQ(ierr);
53
  ierr = PetscOptionsGetString(PETSC_NULL,"-f1",filename,256,&flg);CHKERRQ(ierr);
54
  if (!flg) {
2214 jroman 55
    SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix A with the -f1 option.");
6 dsic.upv.es!jroman 56
  }
57
 
58
#if defined(PETSC_USE_COMPLEX)
59
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrices from binary files...\n");CHKERRQ(ierr);
60
#else
61
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrices from binary files...\n");CHKERRQ(ierr);
62
#endif
1041 slepc 63
  ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
410 dsic.upv.es!antodo 64
  ierr = MatLoad(viewer,MATAIJ,&A);CHKERRQ(ierr);
6 dsic.upv.es!jroman 65
  ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
66
 
67
  ierr = PetscOptionsGetString(PETSC_NULL,"-f2",filename,256,&flg);CHKERRQ(ierr);
68
  if (!flg) {
2214 jroman 69
    SETERRQ(PETSC_COMM_WORLD,1,"Must indicate a file name for matrix B with the -f2 option.");
6 dsic.upv.es!jroman 70
  }
71
 
1041 slepc 72
  ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr);
417 dsic.upv.es!antodo 73
  ierr = MatLoad(viewer,MATAIJ,&B);CHKERRQ(ierr);
6 dsic.upv.es!jroman 74
  ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
75
 
76
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77
                Create the eigensolver and set various options
78
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79
 
80
  /*
81
     Create eigensolver context
82
  */
83
  ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
84
 
85
  /*
86
     Set operators. In this case, it is a generalized eigenvalue problem
87
  */
88
  ierr = EPSSetOperators(eps,A,B);CHKERRQ(ierr);
89
 
90
  /*
91
     Set solver parameters at runtime
92
  */
93
  ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
94
 
95
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
96
                      Solve the eigensystem
97
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98
 
78 dsic.upv.es!antodo 99
  ierr = EPSSolve(eps);CHKERRQ(ierr);
6 dsic.upv.es!jroman 100
 
101
  /*
102
     Optional: Get some information from the solver and display it
103
  */
178 dsic.upv.es!antodo 104
  ierr = EPSGetIterationNumber(eps, &its);CHKERRQ(ierr);
105
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr);
1211 slepc 106
  ierr = EPSGetOperationCounters(eps,PETSC_NULL,PETSC_NULL,&lits);CHKERRQ(ierr);
178 dsic.upv.es!antodo 107
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of linear iterations of the method: %d\n",lits);CHKERRQ(ierr);
6 dsic.upv.es!jroman 108
  ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
109
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
1575 slepc 110
  ierr = EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6 dsic.upv.es!jroman 111
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);CHKERRQ(ierr);
112
  ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
113
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr);
114
 
115
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116
                    Display solution and clean up
117
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118
 
119
  /*
120
     Get number of converged eigenpairs
121
  */
132 dsic.upv.es!antodo 122
  ierr = EPSGetConverged(eps,&nconv);CHKERRQ(ierr);
123
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %d\n\n",nconv);CHKERRQ(ierr);
6 dsic.upv.es!jroman 124
 
132 dsic.upv.es!antodo 125
  if (nconv>0) {
6 dsic.upv.es!jroman 126
    /*
127
       Display eigenvalues and relative errors
128
    */
129
    ierr = PetscPrintf(PETSC_COMM_WORLD,
709 dsic.upv.es!jroman 130
         "           k             ||Ax-kBx||/||kx||\n"
97 dsic.upv.es!antodo 131
         "  --------------------- ------------------\n" );CHKERRQ(ierr);
132 dsic.upv.es!antodo 132
    for( i=0; i<nconv; i++ ) {
97 dsic.upv.es!antodo 133
      /*
134
         Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
135
         ki (imaginary part)
136
      */
137
      ierr = EPSGetEigenpair(eps,i,&kr,&ki,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
138
 
139
      /*
140
         Compute the relative error associated to each eigenpair
141
      */
142
      ierr = EPSComputeRelativeError(eps,i,&error);CHKERRQ(ierr);
143
 
6 dsic.upv.es!jroman 144
#if defined(PETSC_USE_COMPLEX)
97 dsic.upv.es!antodo 145
      re = PetscRealPart(kr);
146
      im = PetscImaginaryPart(kr);
6 dsic.upv.es!jroman 147
#else
97 dsic.upv.es!antodo 148
      re = kr;
149
      im = ki;
6 dsic.upv.es!jroman 150
#endif
97 dsic.upv.es!antodo 151
      if( im != 0.0 ) {
152
        ierr = PetscPrintf(PETSC_COMM_WORLD," % 6f %+6f i",re,im);CHKERRQ(ierr);
132 dsic.upv.es!antodo 153
      } else {
154
        ierr = PetscPrintf(PETSC_COMM_WORLD,"       % 6f      ",re); CHKERRQ(ierr);
155
      }
1162 slepc 156
      ierr = PetscPrintf(PETSC_COMM_WORLD," % 12g\n",error);CHKERRQ(ierr);
6 dsic.upv.es!jroman 157
    }
132 dsic.upv.es!antodo 158
    ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
6 dsic.upv.es!jroman 159
  }
160
 
161
  /*
162
     Free work space
163
  */
164
  ierr = EPSDestroy(eps);CHKERRQ(ierr);
165
  ierr = MatDestroy(A);CHKERRQ(ierr);
166
  ierr = MatDestroy(B);CHKERRQ(ierr);
167
  ierr = SlepcFinalize();CHKERRQ(ierr);
168
  return 0;
169
}
170