Subversion Repositories slepc-dev

Rev

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

Rev Author Line No. Line
6 dsic.upv.es!jroman 1
 
765 dsic.upv.es!jroman 2
static char help[] = "Standard symmetric eigenproblem corresponding to the Laplacian operator in 1 dimension.\n\n"
6 dsic.upv.es!jroman 3
  "The command line options are:\n\n"
4
  "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n\n";
5
 
6
#include "slepceps.h"
7
 
8
#undef __FUNCT__
9
#define __FUNCT__ "main"
10
int main( int argc, char **argv )
11
{
983 slepc 12
  Mat            A;               /* operator matrix */
13
  EPS            eps;             /* eigenproblem solver context */
14
  EPSType        type;
15
  PetscReal      error, tol,re, im;
16
  PetscScalar    kr, ki;
17
  Vec            xr, xi;
18
  PetscErrorCode ierr;
19
  PetscInt       n=30, i, Istart, Iend, col[3];
20
  int            nev, maxit,its, nconv;
21
  PetscTruth     FirstBlock=PETSC_FALSE, LastBlock=PETSC_FALSE;
22
  PetscScalar    value[3];
6 dsic.upv.es!jroman 23
 
24
  SlepcInitialize(&argc,&argv,(char*)0,help);
25
 
26
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
765 dsic.upv.es!jroman 27
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem, n=%d\n\n",n);CHKERRQ(ierr);
6 dsic.upv.es!jroman 28
 
29
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
30
     Compute the operator matrix that defines the eigensystem, Ax=kx
31
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
32
 
828 dsic.upv.es!antodo 33
  ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
34
  ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);CHKERRQ(ierr);
6 dsic.upv.es!jroman 35
  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
36
 
37
  ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
38
  if (Istart==0) FirstBlock=PETSC_TRUE;
39
  if (Iend==n) LastBlock=PETSC_TRUE;
40
  value[0]=-1.0; value[1]=2.0; value[2]=-1.0;
41
  for( i=(FirstBlock? Istart+1: Istart); i<(LastBlock? Iend-1: Iend); i++ ) {
42
    col[0]=i-1; col[1]=i; col[2]=i+1;
43
    ierr = MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr);
44
  }
45
  if (LastBlock) {
46
    i=n-1; col[0]=n-2; col[1]=n-1;
47
    ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
48
  }
49
  if (FirstBlock) {
50
    i=0; col[0]=0; col[1]=1; value[0]=2.0; value[1]=-1.0;
51
    ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
52
  }
53
 
54
  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
55
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
56
 
713 dsic.upv.es!antodo 57
  ierr = MatGetVecs(A,PETSC_NULL,&xr);CHKERRQ(ierr);
58
  ierr = MatGetVecs(A,PETSC_NULL,&xi);CHKERRQ(ierr);
59
 
6 dsic.upv.es!jroman 60
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61
                Create the eigensolver and set various options
62
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
63
  /*
64
     Create eigensolver context
65
  */
66
  ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
67
 
68
  /*
69
     Set operators. In this case, it is a standard eigenvalue problem
70
  */
71
  ierr = EPSSetOperators(eps,A,PETSC_NULL);CHKERRQ(ierr);
408 dsic.upv.es!antodo 72
  ierr = EPSSetProblemType(eps,EPS_HEP);CHKERRQ(ierr);
6 dsic.upv.es!jroman 73
 
74
  /*
75
     Set solver parameters at runtime
76
  */
77
  ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
78
 
79
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80
                      Solve the eigensystem
81
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
82
 
78 dsic.upv.es!antodo 83
  ierr = EPSSolve(eps);CHKERRQ(ierr);
84
  ierr = EPSGetIterationNumber(eps, &its);CHKERRQ(ierr);
765 dsic.upv.es!jroman 85
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);CHKERRQ(ierr);
6 dsic.upv.es!jroman 86
  /*
87
     Optional: Get some information from the solver and display it
88
  */
89
  ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
90
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
91
  ierr = EPSGetDimensions(eps,&nev,PETSC_NULL);CHKERRQ(ierr);
765 dsic.upv.es!jroman 92
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);CHKERRQ(ierr);
6 dsic.upv.es!jroman 93
  ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
765 dsic.upv.es!jroman 94
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);CHKERRQ(ierr);
6 dsic.upv.es!jroman 95
 
96
 
97
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
                    Display solution and clean up
99
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
100
  /*
101
     Get number of converged approximate eigenpairs
102
  */
132 dsic.upv.es!antodo 103
  ierr = EPSGetConverged(eps,&nconv);CHKERRQ(ierr);
765 dsic.upv.es!jroman 104
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged eigenpairs: %d\n\n",nconv);CHKERRQ(ierr);
6 dsic.upv.es!jroman 105
 
132 dsic.upv.es!antodo 106
  if (nconv>0) {
6 dsic.upv.es!jroman 107
    /*
108
       Display eigenvalues and relative errors
109
    */
110
    ierr = PetscPrintf(PETSC_COMM_WORLD,
97 dsic.upv.es!antodo 111
         "           k          ||Ax-kx||/||kx||\n"
112
         "   ----------------- ------------------\n" );CHKERRQ(ierr);
113
 
132 dsic.upv.es!antodo 114
    for( i=0; i<nconv; i++ ) {
97 dsic.upv.es!antodo 115
      /*
116
        Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
117
        ki (imaginary part)
118
      */
713 dsic.upv.es!antodo 119
      ierr = EPSGetEigenpair(eps,i,&kr,&ki,xr,xi);CHKERRQ(ierr);
97 dsic.upv.es!antodo 120
      /*
121
         Compute the relative error associated to each eigenpair
122
      */
123
      ierr = EPSComputeRelativeError(eps,i,&error);CHKERRQ(ierr);
124
 
125
#ifdef PETSC_USE_COMPLEX
132 dsic.upv.es!antodo 126
      re = PetscRealPart(kr);
127
      im = PetscImaginaryPart(kr);
128
#else
129
      re = kr;
130
      im = ki;
97 dsic.upv.es!antodo 131
#endif
132 dsic.upv.es!antodo 132
      if (im!=0.0) {
133
        ierr = PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12f\n",re,im,error);CHKERRQ(ierr);
97 dsic.upv.es!antodo 134
      } else {
132 dsic.upv.es!antodo 135
        ierr = PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12f\n",re,error);CHKERRQ(ierr);
97 dsic.upv.es!antodo 136
      }
6 dsic.upv.es!jroman 137
    }
138
    ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
139
  }
140
 
141
  /*
142
     Free work space
143
  */
144
  ierr = EPSDestroy(eps);CHKERRQ(ierr);
145
  ierr = MatDestroy(A);CHKERRQ(ierr);
713 dsic.upv.es!antodo 146
  ierr = VecDestroy(xr);CHKERRQ(ierr);
147
  ierr = VecDestroy(xi);CHKERRQ(ierr);
6 dsic.upv.es!jroman 148
  ierr = SlepcFinalize();CHKERRQ(ierr);
149
  return 0;
150
}