Blame | Compare with Previous | Last modification | View Log | RSS feed
!! Program usage: mpirun -np n ex1f [-help] [-n <n>] [all SLEPc options]!! Description: Simple example that solves an eigensystem with the EPS object.! The standard symmetric eigenvalue problem to be solved corresponds to the! Laplacian operator in 1 dimension.!! The command line options are:! -n <n>, where <n> = number of grid points = matrix size!! ----------------------------------------------------------------------!program mainimplicit none#include "finclude/petsc.h"#include "finclude/petscvec.h"#include "finclude/petscmat.h"#include "finclude/slepc.h"#include "finclude/slepceps.h"! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Declarations! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -!! Variables:! A operator matrix! eps eigenproblem solver contextMat AEPS epsEPSType typePetscReal tol, errorPetscScalar kr, kiinteger rank, n, nev, ierr, maxit, i, its, nconvinteger col(3), Istart, IendPetscTruth flgPetscScalar value(3)! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Beginning of program! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -call SlepcInitialize(PETSC_NULL_CHARACTER,ierr)call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)n = 30call PetscOptionsGetInt(PETSC_NULL_CHARACTER,'-n',n,flg,ierr)if (rank .eq. 0) thenwrite(*,100) nendif100 format (/'1-D Laplacian Eigenproblem, n =',I3)! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Compute the operator matrix that defines the eigensystem, Ax=kx! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -call MatCreate(PETSC_COMM_WORLD,A,ierr)call MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n,ierr)call MatSetFromOptions(A,ierr)call MatGetOwnershipRange(A,Istart,Iend,ierr)if (Istart .eq. 0) theni = 0col(1) = 0col(2) = 1value(1) = 2.0value(2) = -1.0call MatSetValues(A,1,i,2,col,value,INSERT_VALUES,ierr)Istart = Istart+1endifif (Iend .eq. n) theni = n-1col(1) = n-2col(2) = n-1value(1) = -1.0value(2) = 2.0call MatSetValues(A,1,i,2,col,value,INSERT_VALUES,ierr)Iend = Iend-1endifvalue(1) = -1.0value(2) = 2.0value(3) = -1.0do i=Istart,Iend-1col(1) = i-1col(2) = icol(3) = i+1call MatSetValues(A,1,i,3,col,value,INSERT_VALUES,ierr)enddocall MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Create the eigensolver and display info! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! ** Create eigensolver contextcall EPSCreate(PETSC_COMM_WORLD,eps,ierr)! ** Set operators. In this case, it is a standard eigenvalue problemcall EPSSetOperators(eps,A,PETSC_NULL_OBJECT,ierr)call EPSSetProblemType(eps,EPS_HEP,ierr)! ** Set solver parameters at runtimecall EPSSetFromOptions(eps,ierr)! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Solve the eigensystem! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -call EPSSolve(eps,ierr)call EPSGetIterationNumber(eps,its,ierr)if (rank .eq. 0) thenwrite(*,110) itsendif110 format (/' Number of iterations of the method:',I4)! ** Optional: Get some information from the solver and display itcall EPSGetType(eps,type,ierr)if (rank .eq. 0) thenwrite(*,120) typeendif120 format (' Solution method: ',A)call EPSGetDimensions(eps,nev,PETSC_NULL_INTEGER,ierr)if (rank .eq. 0) thenwrite(*,130) nevendif130 format (/' Number of requested eigenvalues:',I2)call EPSGetTolerances(eps,tol,maxit,ierr)if (rank .eq. 0) thenwrite(*,140) tol, maxitendif140 format (' Stopping condition: tol=',1P,E10.4,', maxit=',I4)! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! Display solution and clean up! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -! ** Get number of converged eigenpairscall EPSGetConverged(eps,nconv,ierr)if (rank .eq. 0) thenwrite(*,150) nconvendif150 format (' Number of converged eigenpairs:',I2/)! ** Display eigenvalues and relative errorsif (nconv.gt.0 .and. rank.eq.0) thenwrite(*,*) ' k ||Ax-kx||/||kx||'write(*,*) ' ----------------- ------------------'do i=0,nconv-1! ** Get converged eigenpairs: i-th eigenvalue is stored in kr! ** (real part) and ki (imaginary part)call EPSGetEigenpair(eps,i,kr,ki,PETSC_NULL,PETSC_NULL,ierr)! ** Compute the relative error associated to each eigenpaircall EPSComputeRelativeError(eps,i,error,ierr)write(*,160) PetscRealPart(kr), error160 format (1P,' ',E12.4,' ',E12.4)enddowrite(*,*)endif! ** Free work spacecall EPSDestroy(eps,ierr)call MatDestroy(A,ierr)call SlepcFinalize(ierr)end