Actual source code: test15f.F
slepc-3.6.1 2015-09-03
1: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2: ! SLEPc - Scalable Library for Eigenvalue Problem Computations
3: ! Copyright (c) 2002-2015, 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: mpirun -np n 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_CHARACTER,'-n',n,flg,ierr)
73: if (rank .eq. 0) then
74: write(*,100) n
75: endif
76: 100 format (/'1-D Laplacian Eigenproblem, n =',I3,' (Fortran)')
78: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79: ! Compute the operator matrix that defines the eigensystem, Ax=kx
80: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82: call MatCreate(PETSC_COMM_WORLD,A,ierr)
83: call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n,ierr)
84: call MatSetFromOptions(A,ierr)
85: call MatSetUp(A,ierr)
87: i1 = 1
88: i2 = 2
89: i3 = 3
90: call MatGetOwnershipRange(A,Istart,Iend,ierr)
91: if (Istart .eq. 0) then
92: i = 0
93: col(1) = 0
94: col(2) = 1
95: value(1) = 2.0
96: value(2) = -1.0
97: call MatSetValues(A,i1,i,i2,col,value,INSERT_VALUES,ierr)
98: Istart = Istart+1
99: endif
100: if (Iend .eq. n) then
101: i = n-1
102: col(1) = n-2
103: col(2) = n-1
104: value(1) = -1.0
105: value(2) = 2.0
106: call MatSetValues(A,i1,i,i2,col,value,INSERT_VALUES,ierr)
107: Iend = Iend-1
108: endif
109: value(1) = -1.0
110: value(2) = 2.0
111: value(3) = -1.0
112: do i=Istart,Iend-1
113: col(1) = i-1
114: col(2) = i
115: col(3) = i+1
116: call MatSetValues(A,i1,i,i3,col,value,INSERT_VALUES,ierr)
117: enddo
119: call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
120: call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
122: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123: ! Create the eigensolver and display info
124: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
126: ! ** Create eigensolver context
127: call EPSCreate(PETSC_COMM_WORLD,eps,ierr)
129: ! ** Set operators. In this case, it is a standard eigenvalue problem
130: call EPSSetOperators(eps,A,PETSC_NULL_OBJECT,ierr)
131: call EPSSetProblemType(eps,EPS_HEP,ierr)
133: ! ** Set user-defined monitor
134: call PetscOptionsHasName(PETSC_NULL_CHARACTER,'-my_eps_monitor', &
135: & flg,ierr)
136: if (flg) then
137: call EPSMonitorSet(eps,MyEPSMonitor,PETSC_NULL_OBJECT, &
138: & PETSC_NULL_FUNCTION,ierr)
139: endif
141: ! ** Set solver parameters at runtime
142: call EPSSetFromOptions(eps,ierr)
144: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
145: ! Solve the eigensystem
146: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148: call EPSSolve(eps,ierr)
150: ! ** Optional: Get some information from the solver and display it
151: call EPSGetType(eps,tname,ierr)
152: if (rank .eq. 0) then
153: write(*,120) tname
154: endif
155: 120 format (' Solution method: ',A)
156: call EPSGetDimensions(eps,nev,PETSC_NULL_INTEGER, &
157: & PETSC_NULL_INTEGER,ierr)
158: if (rank .eq. 0) then
159: write(*,130) nev
160: endif
161: 130 format (' Number of requested eigenvalues:',I2)
163: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
164: ! Display solution and clean up
165: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
167: call EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_NULL_OBJECT,ierr)
168: call EPSDestroy(eps,ierr)
169: call MatDestroy(A,ierr)
171: call SlepcFinalize(ierr)
172: end
174: ! --------------------------------------------------------------
175: !
176: ! MyEPSMonitor - This is a user-defined routine for monitoring
177: ! the EPS iterative solvers.
178: !
179: ! Input Parameters:
180: ! eps - eigensolver context
181: ! its - iteration number
182: ! nconv - number of converged eigenpairs
183: ! eigr - real part of the eigenvalues
184: ! eigi - imaginary part of the eigenvalues
185: ! errest- relative error estimates for each eigenpair
186: ! nest - number of error estimates
187: ! dummy - optional user-defined monitor context (unused here)
188: !
189: subroutine MyEPSMonitor(eps,its,nconv,eigr,eigi,errest,nest,dummy,&
190: & ierr)
192: implicit none
194: #include <petsc/finclude/petscsys.h>
195: #include <petsc/finclude/petscvec.h>
196: #include <petsc/finclude/petscmat.h>
197: #include <slepc/finclude/slepcsys.h>
198: #include <slepc/finclude/slepceps.h>
200: EPS eps
201: Vec x
202: PetscErrorCode ierr
203: PetscInt its,nconv,nest,dummy
204: PetscScalar eigr(*),eigi(*)
205: PetscReal re,errest(*)
206: PetscMPIInt rank
208: call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)
209: if (its .gt. 0 .and. rank .eq. 0) then
210: re = PetscRealPart(eigr(nconv+1))
211: write(6,140) its,nconv,re,errest(nconv+1)
212: endif
214: 140 format(i3,' EPS nconv=',i2,' first unconverged value (error) ', &
215: & f6.4,' (',g9.3,')')
216: 0
217: end