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