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