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
6 dsic.upv.es!jroman 1
 
2
static char help[] = "Solves an eigensystem loaded from a file.\n\n"
3
  "This example works for both real and complex numbers.\n\n"
4
  "The command line options are:\n\n"
5
  "  -file <filename>, where <filename> = matrix file in PETSc binary form.\n\n";
6
 
7
#include "slepceps.h"
8
 
9
#undef __FUNCT__
10
#define __FUNCT__ "main"
11
int main( int argc, char **argv )
12
{
13
  Vec         *x;              /* basis vectors */
14
  Mat         A;               /* operator matrix */
15
  EPS         eps;             /* eigenproblem solver context */
16
  EPSType     type;
17
  PetscReal   *error, tol, re, im;
18
  PetscScalar *kr, *ki;
19
  int         nev, ierr, maxit, i, its, nconv;
20
  char        filename[256];
21
  PetscViewer viewer;
22
  PetscTruth  flg;
23
 
24
 
25
  SlepcInitialize(&argc,&argv,(char*)0,help);
26
 
27
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28
        Load the operator matrix that defines the eigensystem, Ax=kx
29
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
30
 
31
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nEigenproblem stored in file.\n\n");CHKERRQ(ierr);
32
  ierr = PetscOptionsGetString(PETSC_NULL,"-file",filename,256,&flg);CHKERRQ(ierr);
33
  if (!flg) {
34
    SETERRQ(1,"Must indicate a file name with the -file option.");
35
  }
36
 
37
#if defined(PETSC_USE_COMPLEX)
38
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");CHKERRQ(ierr);
39
#else
40
  ierr = PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");CHKERRQ(ierr);
41
#endif
42
  ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,PETSC_BINARY_RDONLY,&viewer);CHKERRQ(ierr);
43
  ierr = MatLoad(viewer,MATMPIAIJ,&A);CHKERRQ(ierr);
44
  ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
45
 
46
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
47
                Create the eigensolver and set various options
48
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
49
 
50
  /*
51
     Create eigensolver context
52
  */
53
  ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
54
 
55
  /*
56
     Set operators. In this case, it is a standard eigenvalue problem
57
  */
58
  ierr = EPSSetOperators(eps,A,PETSC_NULL);CHKERRQ(ierr);
59
 
60
  /*
61
     Set solver parameters at runtime
62
  */
63
  ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
64
 
65
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66
                      Solve the eigensystem
67
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
68
 
69
  ierr = EPSSolve(eps,&its);CHKERRQ(ierr);
70
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr);
71
 
72
  /*
73
     Optional: Get some information from the solver and display it
74
  */
75
  ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
76
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
77
  ierr = EPSGetDimensions(eps,&nev,PETSC_NULL);CHKERRQ(ierr);
78
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);CHKERRQ(ierr);
79
  ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
80
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr);
81
 
82
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
83
                    Display solution and clean up
84
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85
 
86
  /*
87
     Get number of converged eigenpairs
88
  */
89
  ierr = EPSGetConverged(eps,&nconv);CHKERRQ(ierr);
90
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged approximate eigenpairs: %d\n\n",nconv);CHKERRQ(ierr);
91
 
92
  if (nconv>0) {
93
    /*
94
       Get converged eigenpairs: i-th eigenvalue is stored in kr[i] (real part) and
95
       ki[i] (imaginary part), and the corresponding eigenvector is stored in x[i]
96
    */
97
    ierr = EPSGetSolution(eps,&kr,&ki,&x);CHKERRQ(ierr);
98
 
99
    /*
100
       Compute the relative error associated to each eigenpair
101
    */
102
    ierr = PetscMalloc(nconv*sizeof(PetscReal),&error);CHKERRQ(ierr);
103
    ierr = EPSComputeError(eps,error);CHKERRQ(ierr);
104
 
105
    /*
106
       Display eigenvalues and relative errors
107
    */
108
    ierr = PetscPrintf(PETSC_COMM_WORLD,
109
         "           k              ||Ax-kx||/|k|\n"
110
         "  --------------------- -----------------\n" );CHKERRQ(ierr);
111
    for( i=0; i<nconv; i++ ) {
112
#if defined(PETSC_USE_COMPLEX)
113
      re = PetscRealPart(kr[i]);
114
      im = PetscImaginaryPart(kr[i]);
115
#else
116
      re = kr[i];
117
      im = ki[i];
118
#endif
119
      if( im>0.0 ) ierr = PetscPrintf(PETSC_COMM_WORLD," % 6f + %6f i",re,im);
120
      else if( im<0.0 ) ierr = PetscPrintf(PETSC_COMM_WORLD," % 6f - %6f i",re,-im);
121
      else ierr = PetscPrintf(PETSC_COMM_WORLD,"       % 6f      ",re);
122
      CHKERRQ(ierr);
123
      ierr = PetscPrintf(PETSC_COMM_WORLD," % 12f\n",error[i]);CHKERRQ(ierr);
124
    }
125
    ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
126
    ierr = PetscFree(error);CHKERRQ(ierr);
127
  }
128
 
129
  /*
130
     Free work space
131
  */
132
  ierr = EPSDestroy(eps);CHKERRQ(ierr);
133
  ierr = MatDestroy(A);CHKERRQ(ierr);
134
  ierr = SlepcFinalize();CHKERRQ(ierr);
135
  return 0;
136
}
137