Since the convergence of Krylov subspace methods depends strongly on
the spectrum (eigenvalues) of the preconditioned operator, PETSc has specific
routines for eigenvalue approximation via the Arnoldi or Lanczos iteration.
First, before the linear solve one must call
ierr = KSPSetComputeEigenvalues(KSP ksp);Then after the SLES solve one calls
ierr = KSPComputeEigenvalues(KSP ksp, int n,double *realpart,double *complexpart,int *neig);Here, n is the size of the two arrays and the eigenvalues are inserted into those two arrays. Neig is the number of eigenvalues computed; this number depend depends on the size of the Krylov space generated during the linear system solution, for GMRES it is never larger than the restart parameter. There is an additional routine
ierr = KSPComputeEigenvaluesExplicitly(KSP ksp, int n,double *realpart, double *complexpart);that is useful only for very small problems. It explicitly computes the full representation of the preconditioned operator and calles LAPACK to compute its eigenvalues. It should be only used for matrices of size up to a couple hundred. The DrawSP*() routines are very useful for drawing scatter plots of the eigenvalues.
The eigenvalues may also be computed and displayed graphically with the options data base commands -ksp_plot_eigenvalues and -ksp_plot_eigenvalues_explicitly. Or they can be dumped to the screen in ASCII text via -ksp_compute_eigenvalues and -ksp_compute_eigenvalues_explicitly.