Actual source code: test15f.F

slepc-3.7.2 2016-07-19
Report Typos and Errors
  1: !  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  2: !  SLEPc - Scalable Library for Eigenvalue Problem Computations
  3: !  Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
  4: !
  5: !  This file is part of SLEPc.
  6: !     
  7: !  SLEPc is free software: you can redistribute it and/or modify it under  the
  8: !  terms of version 3 of the GNU Lesser General Public License as published by
  9: !  the Free Software Foundation.
 10: !
 11: !  SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 12: !  WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 13: !  FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 14: !  more details.
 15: !
 16: !  You  should have received a copy of the GNU Lesser General  Public  License
 17: !  along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 18: !  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 19: !
 20: !  Program usage: mpiexec -n <np> ./test15f [-help] [-n <n>] [all SLEPc options] 
 21: !
 22: !  Description: Tests custom monitors from Fortran.
 23: !
 24: !  The command line options are:
 25: !    -n <n>, where <n> = number of grid points = matrix size
 26: !    -my_eps_monitor, activates the custom monitor
 27: !
 28: ! ---------------------------------------------------------------------- 
 29: !
 30:       program main
 31:       implicit none

 33: #include <petsc/finclude/petscsys.h>
 34: #include <petsc/finclude/petscvec.h>
 35: #include <petsc/finclude/petscmat.h>
 36: #include <slepc/finclude/slepcsys.h>
 37: #include <slepc/finclude/slepceps.h>

 39: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 40: !     Declarations
 41: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 42: !
 43: !  Variables:
 44: !     A     operator matrix
 45: !     eps   eigenproblem solver context

 47:       Mat            A
 48:       EPS            eps
 49:       EPSType        tname
 50:       PetscInt       n, i, Istart, Iend
 51:       PetscInt       nev
 52:       PetscInt       col(3)
 53:       PetscInt       i1,i2,i3
 54:       PetscMPIInt    rank
 55:       PetscErrorCode ierr
 56:       PetscBool      flg
 57:       PetscScalar    value(3)

 59: !  Note: Any user-defined Fortran routines (such as MyEPSMonitor)
 60: !  MUST be declared as external.

 62:       external MyEPSMonitor

 64: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 65: !     Beginning of program
 66: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

 68:       call SlepcInitialize(PETSC_NULL_CHARACTER,ierr)
 69:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
 70:       n = 30
 71:       call PetscOptionsGetInt(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,   &
 72:      &                        '-n',n,flg,ierr)

 74:       if (rank .eq. 0) then
 75:         write(*,100) n
 76:       endif
 77:  100  format (/'1-D Laplacian Eigenproblem, n =',I3,' (Fortran)')

 79: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 80: !     Compute the operator matrix that defines the eigensystem, Ax=kx
 81: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

 83:       call MatCreate(PETSC_COMM_WORLD,A,ierr)
 84:       call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n,ierr)
 85:       call MatSetFromOptions(A,ierr)
 86:       call MatSetUp(A,ierr)

 88:       i1 = 1
 89:       i2 = 2
 90:       i3 = 3
 91:       call MatGetOwnershipRange(A,Istart,Iend,ierr)
 92:       if (Istart .eq. 0) then 
 93:         i = 0
 94:         col(1) = 0
 95:         col(2) = 1
 96:         value(1) =  2.0
 97:         value(2) = -1.0
 98:         call MatSetValues(A,i1,i,i2,col,value,INSERT_VALUES,ierr)
 99:         Istart = Istart+1
100:       endif
101:       if (Iend .eq. n) then 
102:         i = n-1
103:         col(1) = n-2
104:         col(2) = n-1
105:         value(1) = -1.0
106:         value(2) =  2.0
107:         call MatSetValues(A,i1,i,i2,col,value,INSERT_VALUES,ierr)
108:         Iend = Iend-1
109:       endif
110:       value(1) = -1.0
111:       value(2) =  2.0
112:       value(3) = -1.0
113:       do i=Istart,Iend-1
114:         col(1) = i-1
115:         col(2) = i
116:         col(3) = i+1
117:         call MatSetValues(A,i1,i,i3,col,value,INSERT_VALUES,ierr)
118:       enddo

120:       call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
121:       call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)

123: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
124: !     Create the eigensolver and display info
125: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

127: !     ** Create eigensolver context
128:       call EPSCreate(PETSC_COMM_WORLD,eps,ierr)

130: !     ** Set operators. In this case, it is a standard eigenvalue problem
131:       call EPSSetOperators(eps,A,PETSC_NULL_OBJECT,ierr)
132:       call EPSSetProblemType(eps,EPS_HEP,ierr)

134: !     ** Set user-defined monitor
135:       call PetscOptionsHasName(PETSC_NULL_OBJECT,PETSC_NULL_CHARACTER,  &
136:      &                        '-my_eps_monitor',flg,ierr)
137:       if (flg) then
138:         call EPSMonitorSet(eps,MyEPSMonitor,PETSC_NULL_OBJECT,          &
139:      &                     PETSC_NULL_FUNCTION,ierr)
140:       endif

142: !     ** Set solver parameters at runtime
143:       call EPSSetFromOptions(eps,ierr)

145: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
146: !     Solve the eigensystem
147: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

149:       call EPSSolve(eps,ierr) 

151: !     ** Optional: Get some information from the solver and display it
152:       call EPSGetType(eps,tname,ierr)
153:       if (rank .eq. 0) then
154:         write(*,120) tname
155:       endif
156:  120  format (' Solution method: ',A)
157:       call EPSGetDimensions(eps,nev,PETSC_NULL_INTEGER,                 &
158:      &                      PETSC_NULL_INTEGER,ierr)
159:       if (rank .eq. 0) then
160:         write(*,130) nev
161:       endif
162:  130  format (' Number of requested eigenvalues:',I2)

164: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
165: !     Display solution and clean up
166: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

168:       call EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_NULL_OBJECT,ierr)
169:       call EPSDestroy(eps,ierr)
170:       call MatDestroy(A,ierr)

172:       call SlepcFinalize(ierr)
173:       end

175: ! --------------------------------------------------------------
176: !
177: !  MyEPSMonitor - This is a user-defined routine for monitoring
178: !  the EPS iterative solvers.
179: !
180: !  Input Parameters:
181: !    eps   - eigensolver context
182: !    its   - iteration number
183: !    nconv - number of converged eigenpairs
184: !    eigr  - real part of the eigenvalues
185: !    eigi  - imaginary part of the eigenvalues
186: !    errest- relative error estimates for each eigenpair
187: !    nest  - number of error estimates
188: !    dummy - optional user-defined monitor context (unused here)
189: !
190:       subroutine MyEPSMonitor(eps,its,nconv,eigr,eigi,errest,nest,dummy,&
191:      &                        ierr)

193:       implicit none

195: #include <petsc/finclude/petscsys.h>
196: #include <petsc/finclude/petscvec.h>
197: #include <petsc/finclude/petscmat.h>
198: #include <slepc/finclude/slepcsys.h>
199: #include <slepc/finclude/slepceps.h>

201:       EPS            eps
202:       Vec            x
203:       PetscErrorCode ierr
204:       PetscInt       its,nconv,nest,dummy
205:       PetscScalar    eigr(*),eigi(*)
206:       PetscReal      re,errest(*)
207:       PetscMPIInt    rank

209:       call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
210:       if (its .gt. 0 .and. rank .eq. 0) then
211:         re = PetscRealPart(eigr(nconv+1))
212:         write(6,140) its,nconv,re,errest(nconv+1)
213:       endif

215:  140  format(i3,' EPS nconv=',i2,' first unconverged value (error) ',   &
216:      &       f6.4,' (',g9.3,')')
217:       0
218:       end