Actual source code: krylovschur.c

  1: /*                       

  3:    SLEPc eigensolver: "krylovschur"

  5:    Method: Krylov-Schur

  7:    Algorithm:

  9:        Single-vector Krylov-Schur method for both symmetric and non-symmetric
 10:        problems.

 12:    References:

 14:        [1] "Krylov-Schur Methods in SLEPc", SLEPc Technical Report STR-7, 
 15:            available at http://www.grycap.upv.es/slepc.

 17:        [2] G.W. Stewart, "A Krylov-Schur Algorithm for Large Eigenproblems",
 18:            SIAM J. Matrix Analysis and App., 23(3), pp. 601-614, 2001. 

 20:    Last update: Feb 2009

 22:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 23:    SLEPc - Scalable Library for Eigenvalue Problem Computations
 24:    Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain

 26:    This file is part of SLEPc.
 27:       
 28:    SLEPc is free software: you can redistribute it and/or modify it under  the
 29:    terms of version 3 of the GNU Lesser General Public License as published by
 30:    the Free Software Foundation.

 32:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 33:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 34:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 35:    more details.

 37:    You  should have received a copy of the GNU Lesser General  Public  License
 38:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 39:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40: */

 42:  #include private/epsimpl.h
 43:  #include slepcblaslapack.h

 45: PetscErrorCode EPSSolve_KRYLOVSCHUR_DEFAULT(EPS);

 51: PetscErrorCode EPSSetUp_KRYLOVSCHUR(EPS eps)
 52: {

 56:   if (eps->ncv) { /* ncv set */
 57:     if (eps->ncv<eps->nev) SETERRQ(1,"The value of ncv must be at least nev");
 58:   }
 59:   else if (eps->mpd) { /* mpd set */
 60:     eps->ncv = PetscMin(eps->n,eps->nev+eps->mpd);
 61:   }
 62:   else { /* neither set: defaults depend on nev being small or large */
 63:     if (eps->nev<500) eps->ncv = PetscMin(eps->n,PetscMax(2*eps->nev,eps->nev+15));
 64:     else { eps->mpd = 500; eps->ncv = PetscMin(eps->n,eps->nev+eps->mpd); }
 65:   }
 66:   if (!eps->mpd) eps->mpd = eps->ncv;
 67:   if (eps->ncv>eps->nev+eps->mpd) SETERRQ(1,"The value of ncv must not be larger than nev+mpd");
 68:   if (!eps->max_it) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
 69:   if (!eps->which) eps->which = EPS_LARGEST_MAGNITUDE;
 70:   if (eps->ishermitian && (eps->which==EPS_LARGEST_IMAGINARY || eps->which==EPS_SMALLEST_IMAGINARY))
 71:     SETERRQ(1,"Wrong value of eps->which");

 73:   if (!eps->extraction) {
 74:     EPSSetExtraction(eps,EPS_RITZ);
 75:   } else if (eps->extraction!=EPS_RITZ && eps->extraction!=EPS_HARMONIC) {
 76:     SETERRQ(PETSC_ERR_SUP,"Unsupported extraction type");
 77:   }

 79:   EPSAllocateSolution(eps);
 80:   PetscFree(eps->T);
 81:   if (!eps->ishermitian || eps->extraction==EPS_HARMONIC) {
 82:     PetscMalloc(eps->ncv*eps->ncv*sizeof(PetscScalar),&eps->T);
 83:   }
 84:   EPSDefaultGetWork(eps,1);

 86:   /* dispatch solve method */
 87:   if (eps->leftvecs) SETERRQ(PETSC_ERR_SUP,"Left vectors not supported in this solver");
 88:   if (eps->ishermitian) {
 89:     switch (eps->extraction) {
 90:       case EPS_RITZ:     eps->ops->solve = EPSSolve_KRYLOVSCHUR_SYMM; break;
 91:       case EPS_HARMONIC: eps->ops->solve = EPSSolve_KRYLOVSCHUR_HARMONIC; break;
 92:       default: SETERRQ(PETSC_ERR_SUP,"Unsupported extraction type");
 93:     }
 94:   } else {
 95:     switch (eps->extraction) {
 96:       case EPS_RITZ: eps->ops->solve = EPSSolve_KRYLOVSCHUR_DEFAULT; break;
 97:       case EPS_HARMONIC: eps->ops->solve = EPSSolve_KRYLOVSCHUR_HARMONIC; break;
 98:       default: SETERRQ(PETSC_ERR_SUP,"Unsupported extraction type");
 99:     }
100:   }
101:   return(0);
102: }

106: /*
107:    EPSProjectedKSNonsym - Solves the projected eigenproblem in the Krylov-Schur
108:    method (non-symmetric case).

110:    On input:
111:      l is the number of vectors kept in previous restart (0 means first restart)
112:      S is the projected matrix (leading dimension is lds)

114:    On output:
115:      S has (real) Schur form with diagonal blocks sorted appropriately
116:      Q contains the corresponding Schur vectors (order n, leading dimension n)
117: */
118: PetscErrorCode EPSProjectedKSNonsym(EPS eps,PetscInt l,PetscScalar *S,PetscInt lds,PetscScalar *Q,PetscInt n)
119: {
121:   PetscInt       i;

124:   if (l==0) {
125:     PetscMemzero(Q,n*n*sizeof(PetscScalar));
126:     for (i=0;i<n;i++)
127:       Q[i*(n+1)] = 1.0;
128:   } else {
129:     /* Reduce S to Hessenberg form, S <- Q S Q' */
130:     EPSDenseHessenberg(n,eps->nconv,S,lds,Q);
131:   }
132:   /* Reduce S to (quasi-)triangular form, S <- Q S Q' */
133:   EPSDenseSchur(n,eps->nconv,S,lds,Q,eps->eigr,eps->eigi);
134:   /* Sort the remaining columns of the Schur form */
135:   EPSSortDenseSchur(eps,n,eps->nconv,S,lds,Q,eps->eigr,eps->eigi);
136:   return(0);
137: }

141: PetscErrorCode EPSSolve_KRYLOVSCHUR_DEFAULT(EPS eps)
142: {
144:   PetscInt       i,k,l,lwork,nv;
145:   Vec            u=eps->work[0];
146:   PetscScalar    *S=eps->T,*Q,*work;
147:   PetscReal      beta;
148:   PetscTruth     breakdown;

151:   PetscMemzero(S,eps->ncv*eps->ncv*sizeof(PetscScalar));
152:   PetscMalloc(eps->ncv*eps->ncv*sizeof(PetscScalar),&Q);
153:   lwork = 7*eps->ncv;
154:   PetscMalloc(lwork*sizeof(PetscScalar),&work);

156:   /* Get the starting Arnoldi vector */
157:   EPSGetStartVector(eps,0,eps->V[0],PETSC_NULL);
158:   l = 0;
159: 
160:   /* Restart loop */
161:   while (eps->reason == EPS_CONVERGED_ITERATING) {
162:     eps->its++;

164:     /* Compute an nv-step Arnoldi factorization */
165:     nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
166:     EPSBasicArnoldi(eps,PETSC_FALSE,S,eps->ncv,eps->V,eps->nconv+l,&nv,u,&beta,&breakdown);
167:     VecScale(u,1.0/beta);

169:     /* Solve projected problem */
170:     EPSProjectedKSNonsym(eps,l,S,eps->ncv,Q,nv);

172:     /* Check convergence */
173:     EPSKrylovConvergence(eps,PETSC_FALSE,eps->nconv,nv-eps->nconv,S,eps->ncv,Q,eps->V,nv,beta,1.0,&k,work);
174:     if (eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
175:     if (k >= eps->nev) eps->reason = EPS_CONVERGED_TOL;
176: 
177:     /* Update l */
178:     if (eps->reason != EPS_CONVERGED_ITERATING || breakdown) l = 0;
179:     else {
180:       l = (nv-k)/2;
181: #if !defined(PETSC_USE_COMPLEX)
182:       if (S[(k+l-1)*(eps->ncv+1)+1] != 0.0) {
183:         if (k+l<nv-1) l = l+1;
184:         else l = l-1;
185:       }
186: #endif
187:     }

189:     if (eps->reason == EPS_CONVERGED_ITERATING) {
190:       if (breakdown) {
191:         /* Start a new Arnoldi factorization */
192:         PetscInfo2(eps,"Breakdown in Krylov-Schur method (it=%i norm=%g)\n",eps->its,beta);
193:         EPSGetStartVector(eps,k,eps->V[k],&breakdown);
194:         if (breakdown) {
195:           eps->reason = EPS_DIVERGED_BREAKDOWN;
196:           PetscInfo(eps,"Unable to generate more start vectors\n");
197:         }
198:       } else {
199:         /* Prepare the Rayleigh quotient for restart */
200:         for (i=k;i<k+l;i++) {
201:           S[i*eps->ncv+k+l] = Q[(i+1)*nv-1]*beta;
202:         }
203:       }
204:     }
205:     /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
206:     SlepcUpdateVectors(nv,eps->V,eps->nconv,k+l,Q,nv,PETSC_FALSE);

208:     if (eps->reason == EPS_CONVERGED_ITERATING && !breakdown) {
209:       VecCopy(u,eps->V[k+l]);
210:     }
211:     eps->nconv = k;

213:     EPSMonitor(eps,eps->its,eps->nconv,eps->eigr,eps->eigi,eps->errest,nv);
214: 
215:   }

217:   PetscFree(Q);
218:   PetscFree(work);
219:   return(0);
220: }

225: PetscErrorCode EPSCreate_KRYLOVSCHUR(EPS eps)
226: {
228:   eps->data                      = PETSC_NULL;
229:   eps->ops->setup                = EPSSetUp_KRYLOVSCHUR;
230:   eps->ops->setfromoptions       = PETSC_NULL;
231:   eps->ops->destroy              = EPSDestroy_Default;
232:   eps->ops->view                 = PETSC_NULL;
233:   eps->ops->backtransform        = EPSBackTransform_Default;
234:   eps->ops->computevectors       = EPSComputeVectors_Schur;
235:   return(0);
236: }