lanczos.h: residual-based convergence test, and the new last parameter const int ghosts=1 (0 none, 1 merge repeated eigenvalues, 2=full reorthogonalization
This commit is contained in:
1 parent
c56521469c
commit
d77ca1257a
2 files changed
+93
-23
No files matched your search
+32
-6
@@ -170,24 +170,50 @@ implicitly given one, built column by column by \code{gemv} on unit vectors (for
|
|||||||
\laheader{lanczos.h}
|
\laheader{lanczos.h}
|
||||||
|
|
||||||
\begin{lareference}
|
\begin{lareference}
|
||||||
\lafunc{template <typename T, typename Matrix> void lanczos(const Matrix &bigmat, NRVec<T> &eivals, NRVec<T> *eivecs, const char *eivecsfile, int nroots=1, const bool verbose=0, const double eps=LA_precision<normtype>::eigensolver_eps(), const bool incore=1, int maxit=100, const int maxkrylov = 1000, void (*initguess)(NRVec<T> &)=NULL, const typename LA_traits<T>::normtype *target=NULL)}{
|
\lafunc{template <typename T, typename Matrix> void lanczos(const Matrix &bigmat, NRVec<T> &eivals, NRVec<T> *eivecs, const char *eivecsfile, int nroots=1, const bool verbose=0, const double eps=LA_precision<normtype>::eigensolver_eps(), const bool incore=1, int maxit=100, const int maxkrylov = 1000, void (*initguess)(NRVec<T> &)=NULL, const typename LA_traits<T>::normtype *target=NULL, const int ghosts=1)}{
|
||||||
Lanczos method for the \code{nroots} lowest (or closest to \code{*target}) eigenvalues of a hermitian matrix. The parameters
|
Lanczos method for the \code{nroots} lowest (or closest to \code{*target}) eigenvalues of a hermitian matrix. The parameters
|
||||||
have the same meaning as for \code{davidson}, with these differences: \code{bigmat.issymmetric()} must be \code{true}
|
have the same meaning as for \code{davidson}, with these differences: \code{bigmat.issymmetric()} must be \code{true}
|
||||||
(\code{laerror} otherwise); the start vector is normalized by the function; \code{incore=false} keeps only the Lanczos vectors
|
(\code{laerror} otherwise); the start vector is normalized by the function; \code{incore=false} keeps only the Lanczos vectors
|
||||||
$v_k$ in one scratch file; the default \code{maxkrylov} is 1000.}
|
$v_k$ in one scratch file; the default \code{maxkrylov} is 1000. \code{ghosts} selects the treatment of the spurious copies of
|
||||||
|
converged eigenvalues (see below): 0 none, 1 merging of multiple eigenvalues (default), 2 full reorthogonalization. Since
|
||||||
|
\code{T} is deduced also from \code{initguess}, a call giving \code{target} or \code{ghosts} has to pass the null pointers with
|
||||||
|
their types, e.g.\ \code{(void (*)(NRVec<double> \&))NULL, (const double *)NULL}.}
|
||||||
\end{lareference}
|
\end{lareference}
|
||||||
|
|
||||||
\paragraph{Algorithm.} Plain three-term Lanczos recurrence $w=Av_j-\beta_{j-1}v_{j-1}$, $\alpha_j=v_j\cdot w$,
|
\paragraph{Algorithm.} Plain three-term Lanczos recurrence $w=Av_j-\beta_{j-1}v_{j-1}$, $\alpha_j=v_j\cdot w$,
|
||||||
$w\leftarrow w-\alpha_jv_j$, $\beta_j=|w|$, $v_{j+1}=w/\beta_j$, one \code{gemv} per iteration (with $\beta=1$ in the
|
$w\leftarrow w-\alpha_jv_j$, $\beta_j=|w|$, $v_{j+1}=w/\beta_j$, one \code{gemv} per iteration (with $\beta=1$ in the
|
||||||
\code{gemv} sense, accumulating into $-\beta_{j-1}v_{j-1}$). The vectors are not reorthogonalized, so that in long runs
|
\code{gemv} sense, accumulating into $-\beta_{j-1}v_{j-1}$). In finite precision the vectors lose their orthogonality as
|
||||||
copies of converged eigenvalues may appear (a known property of the method in finite precision). If $\beta_j=0$ (invariant
|
eigenvalues converge, and spurious copies (``ghosts'') of converged eigenvalues appear in long runs, with small residuals,
|
||||||
|
so that a copy of a lower root can be returned in place of a higher one. \code{ghosts} selects the remedy:
|
||||||
|
\begin{itemize}
|
||||||
|
\item 0: none, the plain recurrence.
|
||||||
|
\item 1 (default): numerically multiple eigenvalues of the tridiagonal matrix $T$ (copies of a converged eigenvalue) are kept
|
||||||
|
once in each iteration, the copy with the smallest residual; two eigenvalues count as equal within
|
||||||
|
$100\,(j+1)\,\epsilon\max|\theta|$ ($\epsilon$ the machine precision). This is the first half of the filtering of Cullum and
|
||||||
|
Willoughby; its second half (simple eigenvalues of $T$ which are also eigenvalues of $T$ without its first row and column are
|
||||||
|
dropped as spurious) is not used, since it drops also true eigenvalues weakly represented in the start vector. The cost is
|
||||||
|
negligible and the vectors are not touched, but with several roots and a tight \code{eps} more iterations may be needed than
|
||||||
|
with 2 (the iterations producing the copies do not improve the higher roots).
|
||||||
|
\item 2: full reorthogonalization of each new $w$ to all stored $v_k$ (one \code{dot} and one \code{axpy} per vector), repeated
|
||||||
|
once if the norm of $w$ drops below 0.7 of its value before (``twice is enough''); the coefficients are discarded, so that
|
||||||
|
$T$ stays tridiagonal. The cost is about $k^2/2$ \code{dot}--\code{axpy} pairs in $k$ iterations, i.e.\ what \code{davidson}
|
||||||
|
spends on its orthogonalization; with \code{incore=false} each iteration reads all previous vectors from the scratch file.
|
||||||
|
\end{itemize}
|
||||||
|
Neither prevents eigenvectors of other symmetries (spin multiplicities, point group, particle number in a qubit
|
||||||
|
representation), which are absent from the start vector in exact arithmetic, from growing in from rounding errors of
|
||||||
|
\code{gemv} in long runs; they are true eigenvectors of the matrix. If $\beta_j=0$ (invariant
|
||||||
subspace), a random vector orthonormalized to all previous $v_k$ is used to continue. The tridiagonal matrix is diagonalized
|
subspace), a random vector orthonormalized to all previous $v_k$ is used to continue. The tridiagonal matrix is diagonalized
|
||||||
in each iteration by \code{diagonalize3}. All $v_k$ are stored (in core or in the scratch file) to build the eigenvectors at the
|
in each iteration by \code{diagonalize3}. All $v_k$ are stored (in core or in the scratch file) to build the eigenvectors at the
|
||||||
end. If the diagonal elements $v_j\cdot Av_j$ have an imaginary part larger than $10^{-6}$, \code{laerror} reports a
|
end. If the diagonal elements $v_j\cdot Av_j$ have an imaginary part larger than $10^{-6}$, \code{laerror} reports a
|
||||||
non-hermitian matrix.
|
non-hermitian matrix.
|
||||||
|
|
||||||
\paragraph{Convergence.} Converged when the changes of the \code{nroots} lowest (or closest to the target) eigenvalues
|
\paragraph{Convergence.} Converged when the residual norms $|Ay-\theta y|$ of the Ritz vectors $y$ of the \code{nroots} lowest
|
||||||
between two iterations are all $\le$ \code{eps}; this is checked once the dimension exceeds \code{nroots}. There is no restart:
|
(or closest to the target) eigenvalues $\theta$ are all $\le$ \code{eps}; this is checked once the dimension reaches
|
||||||
|
\code{nroots}. The residual norm is obtained without any additional \code{gemv} as $\beta_j|s_j|$, the norm of the next $w$
|
||||||
|
times the last component of the eigenvector of the tridiagonal matrix (exact in exact arithmetic), and it is printed with
|
||||||
|
\code{verbose}. The error of the eigenvalue is typically much smaller than \code{eps}. With \code{ghosts=0} a copy of a converged
|
||||||
|
eigenvalue (see above) has a small residual as well and is not recognized; with several roots and a very tight \code{eps}
|
||||||
|
the longer runs make such copies likely. There is no restart:
|
||||||
the method fails with \code{laerror} when more than \code{maxit} iterations are needed or when the dimension reaches
|
the method fails with \code{laerror} when more than \code{maxit} iterations are needed or when the dimension reaches
|
||||||
\code{maxkrylov} (the effective limit is $\min(\mathtt{maxit},\mathtt{maxkrylov}-1)$ iterations).
|
\code{maxkrylov} (the effective limit is $\min(\mathtt{maxit},\mathtt{maxkrylov}-1)$ iterations).
|
||||||
|
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ namespace LA {
|
|||||||
//matrix can be any class which has nrows(), ncols(), diagonalof(), issymmetric(), and gemv() available
|
//matrix can be any class which has nrows(), ncols(), diagonalof(), issymmetric(), and gemv() available
|
||||||
//does not even have to be explicitly stored - direct CI
|
//does not even have to be explicitly stored - direct CI
|
||||||
//therefore the whole implementation must be a template in a header
|
//therefore the whole implementation must be a template in a header
|
||||||
|
//ghosts: treatment of the spurious copies of converged eigenvalues due to the loss of orthogonality of the Lanczos vectors:
|
||||||
|
//0 none (plain Lanczos), 1 numerically multiple eigenvalues of the tridiagonal matrix are kept once (cheap),
|
||||||
|
//2 full reorthogonalization of each new vector to all previous ones (one dot and one axpy per stored vector and iteration)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -41,7 +44,7 @@ template <typename T, typename Matrix>
|
|||||||
extern void lanczos(const Matrix &bigmat, NRVec<T> &eivals, NRVec<T> *eivecs, const char *eivecsfile,
|
extern void lanczos(const Matrix &bigmat, NRVec<T> &eivals, NRVec<T> *eivecs, const char *eivecsfile,
|
||||||
int nroots=1, const bool verbose=0, const double eps=LA_precision<typename LA_traits<T>::normtype>::eigensolver_eps(),
|
int nroots=1, const bool verbose=0, const double eps=LA_precision<typename LA_traits<T>::normtype>::eigensolver_eps(),
|
||||||
const bool incore=1, int maxit=100, const int maxkrylov = 1000,
|
const bool incore=1, int maxit=100, const int maxkrylov = 1000,
|
||||||
void (*initguess)(NRVec<T> &)=NULL, const typename LA_traits<T>::normtype *target=NULL)
|
void (*initguess)(NRVec<T> &)=NULL, const typename LA_traits<T>::normtype *target=NULL, const int ghosts=1)
|
||||||
{
|
{
|
||||||
if(!bigmat.issymmetric()) laerror("lanczos only for hermitean matrices");
|
if(!bigmat.issymmetric()) laerror("lanczos only for hermitean matrices");
|
||||||
bool flag=0;
|
bool flag=0;
|
||||||
@@ -94,7 +97,7 @@ if(LA_traits<T>::imagpart(tmp)>1e-6) laerror("matrix probably not hermitian in l
|
|||||||
vec2.axpy(-alpha[0],vec1);
|
vec2.axpy(-alpha[0],vec1);
|
||||||
|
|
||||||
NRVec<typename LA_traits<T>::normtype> r(1); r[0]=alpha[0];
|
NRVec<typename LA_traits<T>::normtype> r(1); r[0]=alpha[0];
|
||||||
NRVec<typename LA_traits<T>::normtype> rold;
|
typename LA_traits<T>::normtype betanext=vec2.norm(); //norm of the last w vector, the next beta
|
||||||
NRMat<typename LA_traits<T>::normtype> smallV;
|
NRMat<typename LA_traits<T>::normtype> smallV;
|
||||||
int krylovsize=1;
|
int krylovsize=1;
|
||||||
//iterative Lanczos
|
//iterative Lanczos
|
||||||
@@ -105,7 +108,7 @@ for(j=1; j<maxkrylov;++j)
|
|||||||
if(it>maxit) {std::cout <<"too many interations in lanczos\n"; flag=1; goto finished;}
|
if(it>maxit) {std::cout <<"too many interations in lanczos\n"; flag=1; goto finished;}
|
||||||
++krylovsize;
|
++krylovsize;
|
||||||
//extend the Krylov space (last w vector expected in vec2, last v vector in vec1)
|
//extend the Krylov space (last w vector expected in vec2, last v vector in vec1)
|
||||||
beta[j-1] = vec2.norm();
|
beta[j-1] = betanext;
|
||||||
if(beta[j-1] > 0)
|
if(beta[j-1] > 0)
|
||||||
{
|
{
|
||||||
vec2 /= beta[j-1];
|
vec2 /= beta[j-1];
|
||||||
@@ -143,23 +146,64 @@ for(j=1; j<maxkrylov;++j)
|
|||||||
}
|
}
|
||||||
vec1.axpy(-alpha[j],vec2);
|
vec1.axpy(-alpha[j],vec2);
|
||||||
vec1.swap(vec2); //move new w vector to vec2, new v vector to vec1
|
vec1.swap(vec2); //move new w vector to vec2, new v vector to vec1
|
||||||
|
if(ghosts==2) //full reorthogonalization of the new w to all v_k, once more if its norm drops much ("twice is enough")
|
||||||
|
{
|
||||||
|
typename LA_traits<T>::normtype wnorm=vec2.norm();
|
||||||
|
NRVec<T> vec3;
|
||||||
|
if(!incore) vec3.resize(n);
|
||||||
|
for(int pass=0; pass<2; ++pass)
|
||||||
|
{
|
||||||
|
for(int k=0; k<=j; ++k)
|
||||||
|
{
|
||||||
|
if(!incore) s0->get(vec3,k);
|
||||||
|
const NRVec<T> &vk= incore? v0[k] : vec3;
|
||||||
|
T f=vk.dot(vec2);
|
||||||
|
vec2.axpy(-f,vk);
|
||||||
|
}
|
||||||
|
betanext=vec2.norm();
|
||||||
|
if(betanext>0.7*wnorm) break;
|
||||||
|
wnorm=betanext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else betanext=vec2.norm();
|
||||||
|
|
||||||
//diagonalize the tridiagonal
|
//diagonalize the tridiagonal
|
||||||
smallV.resize(j+1,j+1);
|
smallV.resize(j+1,j+1);
|
||||||
rold=r;
|
|
||||||
r=alpha.subvector(0,j);
|
r=alpha.subvector(0,j);
|
||||||
NRVec<typename LA_traits<T>::normtype> rr=beta.subvector(0,j-1);
|
NRVec<typename LA_traits<T>::normtype> rr=beta.subvector(0,j-1);
|
||||||
diagonalize3(r,rr,&smallV);
|
diagonalize3(r,rr,&smallV);
|
||||||
if(target) //resort eigenpairs by distance from the target
|
if(ghosts==1 && j>=1) //keep one of numerically multiple eigenvalues (copies of a converged eigenvalue), that with the smallest residual
|
||||||
{
|
{ //(the spurious eigenvalue test of Cullum and Willoughby is not used, it drops also true eigenvalues weakly represented in the start vector)
|
||||||
NRVec<typename LA_traits<T>::normtype> key(j+1);
|
const typename LA_traits<T>::normtype tol= 100*(j+1)*std::numeric_limits<typename LA_traits<T>::normtype>::epsilon()*std::max(abs(r[0]),abs(r[j]));
|
||||||
for(int i=0; i<=j; ++i) key[i] = abs(r[i] - *target);
|
|
||||||
NRPerm<int> p(j+1);
|
|
||||||
key.sort(0,p);
|
|
||||||
|
|
||||||
NRVec<typename LA_traits<T>::normtype> rp(j+1);
|
NRVec<typename LA_traits<T>::normtype> rp(j+1);
|
||||||
NRMat<typename LA_traits<T>::normtype> smallVp(j+1,j+1);
|
NRMat<typename LA_traits<T>::normtype> smallVp(j+1,j+1);
|
||||||
for(int i=0; i<=j; ++i)
|
int ngood=0;
|
||||||
|
for(int i=0; i<=j; )
|
||||||
|
{
|
||||||
|
int k=i, best=i;
|
||||||
|
while(k<j && r[k+1]-r[k]<=tol) {++k; if(abs(smallV(j,k))<abs(smallV(j,best))) best=k;}
|
||||||
|
rp[ngood]=r[best];
|
||||||
|
for(int l=0; l<=j; ++l) smallVp(l,ngood)=smallV(l,best);
|
||||||
|
++ngood;
|
||||||
|
i=k+1;
|
||||||
|
}
|
||||||
|
if(ngood<=j)
|
||||||
|
{
|
||||||
|
r=rp; r.resize(ngood,true);
|
||||||
|
smallV=smallVp.submatrix(0,j,0,ngood-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(target) //resort eigenpairs by distance from the target
|
||||||
|
{
|
||||||
|
const int nr=r.size();
|
||||||
|
NRVec<typename LA_traits<T>::normtype> key(nr);
|
||||||
|
for(int i=0; i<nr; ++i) key[i] = abs(r[i] - *target);
|
||||||
|
NRPerm<int> p(nr);
|
||||||
|
key.sort(0,p);
|
||||||
|
|
||||||
|
NRVec<typename LA_traits<T>::normtype> rp(nr);
|
||||||
|
NRMat<typename LA_traits<T>::normtype> smallVp(j+1,nr);
|
||||||
|
for(int i=0; i<nr; ++i)
|
||||||
{
|
{
|
||||||
rp[i]= r[p[i+1]-1];
|
rp[i]= r[p[i+1]-1];
|
||||||
for(int k=0; k<=j; ++k) smallVp(k,i) = smallV(k,p[i+1]-1);
|
for(int k=0; k<=j; ++k) smallVp(k,i) = smallV(k,p[i+1]-1);
|
||||||
@@ -170,18 +214,18 @@ for(j=1; j<maxkrylov;++j)
|
|||||||
|
|
||||||
if(verbose)
|
if(verbose)
|
||||||
{
|
{
|
||||||
for(int iroot=0; iroot<std::min(krylovsize,nroots); ++iroot)
|
for(int iroot=0; iroot<std::min(r.size(),nroots); ++iroot)
|
||||||
{
|
{
|
||||||
std::cout <<"Lanczos: iter="<<it <<" dim="<<krylovsize<<" root="<<iroot<<" eigenvalue="<<r[iroot]<<"\n";
|
std::cout <<"Lanczos: iter="<<it <<" dim="<<krylovsize<<" root="<<iroot<<" eigenvalue="<<r[iroot]<<" residual="<<betanext*abs(smallV(j,iroot))<<"\n";
|
||||||
}
|
}
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
}
|
}
|
||||||
//convergence test when we have enough roots even in rold
|
//convergence test on the residual norms of the Ritz vectors, |H y - r y| = beta_{j+1} |smallV(j,iroot)|
|
||||||
if(krylovsize>nroots)
|
if(r.size()>=nroots)
|
||||||
{
|
{
|
||||||
bool conv=true;
|
bool conv=true;
|
||||||
for(int iroot=0; iroot<nroots; ++iroot)
|
for(int iroot=0; iroot<nroots; ++iroot)
|
||||||
if(abs(r[iroot]-rold[iroot])>eps) conv=false;
|
if(betanext*abs(smallV(j,iroot))>eps) conv=false;
|
||||||
if(conv)
|
if(conv)
|
||||||
{
|
{
|
||||||
flag=0;
|
flag=0;
|
||||||
|
|||||||
Reference in new issue
Block a user