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
!
2
!  Program usage: mpirun -np n ex1f [-help] [-n <n>] [all SLEPc options]
3
!
4
!  Description: Simple example that solves an eigensystem with the EPS object.
5
!  The standard symmetric eigenvalue problem to be solved corresponds to the
6
!  Laplacian operator in 1 dimension.
7
!
8
!  The command line options are:
9
!    -n <n>, where <n> = number of grid points = matrix size
10
!
11
! ----------------------------------------------------------------------
12
!
13
      program main
14
      implicit none
15
 
16
#include "finclude/petsc.h"
17
#include "finclude/petscvec.h"
18
#include "finclude/petscmat.h"
19
#include "finclude/slepc.h"
20
#include "finclude/slepceps.h"
21
 
22
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23
!     Declarations
24
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25
!
26
!  Variables:
27
!     A     operator matrix
28
!     eps   eigenproblem solver context
29
 
30
      Mat          A
31
      EPS          eps
32
      EPSType      type
1087 slepc 33
      PetscReal    tol, error
97 dsic.upv.es!antodo 34
      PetscScalar  kr, ki
132 dsic.upv.es!antodo 35
      integer      rank, n, nev, ierr, maxit, i, its, nconv
6 dsic.upv.es!jroman 36
      integer      col(3), Istart, Iend
37
      PetscTruth   flg
38
      PetscScalar  value(3)
39
 
40
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41
!     Beginning of program
42
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43
 
44
      call SlepcInitialize(PETSC_NULL_CHARACTER,ierr)
45
      call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
46
      n = 30
47
      call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)
48
 
49
      if (rank .eq. 0) then
214 dsic.upv.es!antodo 50
        write(*,100) n
6 dsic.upv.es!jroman 51
      endif
1034 slepc 52
 100  format (/'1-D Laplacian Eigenproblem, n =',I3)
6 dsic.upv.es!jroman 53
 
54
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55
!     Compute the operator matrix that defines the eigensystem, Ax=kx
56
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57
 
828 dsic.upv.es!antodo 58
      call MatCreate(PETSC_COMM_WORLD,A,ierr)
59
      call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n,ierr)
6 dsic.upv.es!jroman 60
      call MatSetFromOptions(A,ierr)
61
 
62
      call MatGetOwnershipRange(A,Istart,Iend,ierr)
63
      if (Istart .eq. 0) then
64
        i = 0
65
        col(1) = 0
66
        col(2) = 1
67
        value(1) =  2.0
68
        value(2) = -1.0
69
        call MatSetValues(A,1,i,2,col,value,INSERT_VALUES,ierr)
70
        Istart = Istart+1
71
      endif
72
      if (Iend .eq. n) then
73
        i = n-1
74
        col(1) = n-2
75
        col(2) = n-1
76
        value(1) = -1.0
77
        value(2) =  2.0
78
        call MatSetValues(A,1,i,2,col,value,INSERT_VALUES,ierr)
79
        Iend = Iend-1
80
      endif
81
      value(1) = -1.0
82
      value(2) =  2.0
83
      value(3) = -1.0
84
      do i=Istart,Iend-1
85
        col(1) = i-1
86
        col(2) = i
87
        col(3) = i+1
88
        call MatSetValues(A,1,i,3,col,value,INSERT_VALUES,ierr)
89
      enddo
90
 
91
      call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
92
      call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
93
 
94
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
95
!     Create the eigensolver and display info
96
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97
 
98
!     ** Create eigensolver context
99
      call EPSCreate(PETSC_COMM_WORLD,eps,ierr)
100
 
101
!     ** Set operators. In this case, it is a standard eigenvalue problem
102
      call EPSSetOperators(eps,A,PETSC_NULL_OBJECT,ierr)
395 dsic.upv.es!antodo 103
      call EPSSetProblemType(eps,EPS_HEP,ierr)
6 dsic.upv.es!jroman 104
 
105
!     ** Set solver parameters at runtime
106
      call EPSSetFromOptions(eps,ierr)
107
 
108
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109
!     Solve the eigensystem
110
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
 
78 dsic.upv.es!antodo 112
      call EPSSolve(eps,ierr)
113
      call EPSGetIterationNumber(eps,its,ierr)
6 dsic.upv.es!jroman 114
      if (rank .eq. 0) then
1034 slepc 115
        write(*,110) its
6 dsic.upv.es!jroman 116
      endif
1034 slepc 117
 110  format (/' Number of iterations of the method:',I4)
6 dsic.upv.es!jroman 118
 
119
!     ** Optional: Get some information from the solver and display it
120
      call EPSGetType(eps,type,ierr)
121
      if (rank .eq. 0) then
1034 slepc 122
        write(*,120) type
6 dsic.upv.es!jroman 123
      endif
1034 slepc 124
 120  format (' Solution method: ',A)
6 dsic.upv.es!jroman 125
      call EPSGetDimensions(eps,nev,PETSC_NULL_INTEGER,ierr)
126
      if (rank .eq. 0) then
1034 slepc 127
        write(*,130) nev
6 dsic.upv.es!jroman 128
      endif
1034 slepc 129
 130  format (/' Number of requested eigenvalues:',I2)
6 dsic.upv.es!jroman 130
      call EPSGetTolerances(eps,tol,maxit,ierr)
131
      if (rank .eq. 0) then
1034 slepc 132
        write(*,140) tol, maxit
6 dsic.upv.es!jroman 133
      endif
1034 slepc 134
 140  format (' Stopping condition: tol=',1P,E10.4,', maxit=',I4)
6 dsic.upv.es!jroman 135
 
136
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137
!     Display solution and clean up
138
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139
 
214 dsic.upv.es!antodo 140
!     ** Get number of converged eigenpairs
132 dsic.upv.es!antodo 141
      call EPSGetConverged(eps,nconv,ierr)
6 dsic.upv.es!jroman 142
      if (rank .eq. 0) then
214 dsic.upv.es!antodo 143
        write(*,150) nconv
6 dsic.upv.es!jroman 144
      endif
1034 slepc 145
 150  format (' Number of converged eigenpairs:',I2/)
6 dsic.upv.es!jroman 146
 
147
!     ** Display eigenvalues and relative errors
132 dsic.upv.es!antodo 148
      if (nconv.gt.0 .and. rank.eq.0) then
214 dsic.upv.es!antodo 149
        write(*,*) '         k          ||Ax-kx||/||kx||'
150
        write(*,*) ' ----------------- ------------------'
132 dsic.upv.es!antodo 151
        do i=0,nconv-1
214 dsic.upv.es!antodo 152
!         ** Get converged eigenpairs: i-th eigenvalue is stored in kr
153
!         ** (real part) and ki (imaginary part)
154
          call EPSGetEigenpair(eps,i,kr,ki,PETSC_NULL,PETSC_NULL,ierr)
97 dsic.upv.es!antodo 155
 
214 dsic.upv.es!antodo 156
!         ** Compute the relative error associated to each eigenpair
97 dsic.upv.es!antodo 157
          call EPSComputeRelativeError(eps,i,error,ierr)
158
 
1087 slepc 159
          write(*,160) PetscRealPart(kr), error
160
 160      format (1P,'   ',E12.4,'       ',E12.4)
1034 slepc 161
 
6 dsic.upv.es!jroman 162
        enddo
214 dsic.upv.es!antodo 163
        write(*,*)
6 dsic.upv.es!jroman 164
      endif
165
 
166
!     ** Free work space
167
      call EPSDestroy(eps,ierr)
168
      call MatDestroy(A,ierr)
169
 
170
      call SlepcFinalize(ierr)
171
      end
172