Date: 2024-04-26T10:21:50+00:00

Actual source code: lanczos.c
slepc-3.21.1 2024-04-26
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */
     10: /*
     11:    SLEPc eigensolver: "lanczos"

     13:    Method: Explicitly Restarted Symmetric/Hermitian Lanczos

     15:    Algorithm:

     17:        Lanczos method for symmetric (Hermitian) problems, with explicit
     18:        restart and deflation. Several reorthogonalization strategies can
     19:        be selected.

     21:    References:

     23:        [1] "Lanczos Methods in SLEPc", SLEPc Technical Report STR-5,
     24:            available at https://slepc.upv.es.
     25: */

     27: #include <slepc/private/epsimpl.h>
     28: #include <slepcblaslapack.h>

     30: typedef struct {
     31:   EPSLanczosReorthogType reorthog;      /* user-provided reorthogonalization parameter */
     32:   PetscInt               allocsize;     /* number of columns of work BV's allocated at setup */
     33:   BV                     AV;            /* work BV used in selective reorthogonalization */
     34: } EPS_LANCZOS;

     36: static PetscErrorCode EPSSetUp_Lanczos(EPS eps)
     37: {
     38:   EPS_LANCZOS        *lanczos = (EPS_LANCZOS*)eps->data;
     39:   BVOrthogRefineType refine;
     40:   BVOrthogBlockType  btype;
     41:   PetscReal          eta;

     43:   PetscFunctionBegin;
     44:   EPSCheckHermitianDefinite(eps);
     45:   PetscCall(EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd));
     46:   PetscCheck(eps->ncv<=eps->nev+eps->mpd,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"The value of ncv must not be larger than nev+mpd");
     47:   if (eps->max_it==PETSC_DEFAULT) eps->max_it = PetscMax(100,2*eps->n/eps->ncv);
     48:   if (!eps->which) PetscCall(EPSSetWhichEigenpairs_Default(eps));
     49:   PetscCheck(eps->which!=EPS_ALL,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support computing all eigenvalues");
     50:   EPSCheckUnsupported(eps,EPS_FEATURE_ARBITRARY | EPS_FEATURE_REGION | EPS_FEATURE_EXTRACTION);
     51:   EPSCheckIgnored(eps,EPS_FEATURE_BALANCE);

     53:   PetscCheck(lanczos->reorthog!=(EPSLanczosReorthogType)-1,PetscObjectComm((PetscObject)eps),PETSC_ERR_USER_INPUT,"You should explicitly provide the reorthogonalization type, e.g., -eps_lanczos_reorthog local. Note that the EPSLANCZOS solver is *NOT RECOMMENDED* for general use, because it uses explicit restart which typically has slow convergence. The recommended solver is EPSKRYLOVSCHUR (the default), which implements Lanczos with thick restart in the case of symmetric/Hermitian problems");

     55:   PetscCall(EPSAllocateSolution(eps,1));
     56:   PetscCall(EPS_SetInnerProduct(eps));
     57:   if (lanczos->reorthog != EPS_LANCZOS_REORTHOG_FULL) {
     58:     PetscCall(BVGetOrthogonalization(eps->V,NULL,&refine,&eta,&btype));
     59:     PetscCall(BVSetOrthogonalization(eps->V,BV_ORTHOG_MGS,refine,eta,btype));
     60:     PetscCall(PetscInfo(eps,"Switching to MGS orthogonalization\n"));
     61:   }
     62:   if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_SELECTIVE) {
     63:     if (!lanczos->allocsize) {
     64:       PetscCall(BVDuplicate(eps->V,&lanczos->AV));
     65:       PetscCall(BVGetSizes(lanczos->AV,NULL,NULL,&lanczos->allocsize));
     66:     } else { /* make sure V and AV have the same size */
     67:       PetscCall(BVGetSizes(eps->V,NULL,NULL,&lanczos->allocsize));
     68:       PetscCall(BVResize(lanczos->AV,lanczos->allocsize,PETSC_FALSE));
     69:     }
     70:   }

     72:   PetscCall(DSSetType(eps->ds,DSHEP));
     73:   PetscCall(DSSetCompact(eps->ds,PETSC_TRUE));
     74:   PetscCall(DSAllocate(eps->ds,eps->ncv+1));
     75:   if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_LOCAL) PetscCall(EPSSetWorkVecs(eps,1));
     76:   PetscFunctionReturn(PETSC_SUCCESS);
     77: }

     79: /*
     80:    EPSLocalLanczos - Local reorthogonalization.

     82:    This is the simplest variant. At each Lanczos step, the corresponding Lanczos vector
     83:    is orthogonalized with respect to the two previous Lanczos vectors, according to
     84:    the three term Lanczos recurrence. WARNING: This variant does not track the loss of
     85:    orthogonality that occurs in finite-precision arithmetic and, therefore, the
     86:    generated vectors are not guaranteed to be (semi-)orthogonal.
     87: */
     88: static PetscErrorCode EPSLocalLanczos(EPS eps,PetscReal *alpha,PetscReal *beta,PetscInt k,PetscInt *M,PetscBool *breakdown)
     89: {
     90:   PetscInt       i,j,m = *M;
     91:   Mat            Op;
     92:   PetscBool      *which,lwhich[100];
     93:   PetscScalar    *hwork,lhwork[100];

     95:   PetscFunctionBegin;
     96:   if (m > 100) PetscCall(PetscMalloc2(m,&which,m,&hwork));
     97:   else {
     98:     which = lwhich;
     99:     hwork = lhwork;
    100:   }
    101:   for (i=0;i<k;i++) which[i] = PETSC_TRUE;

    103:   PetscCall(BVSetActiveColumns(eps->V,0,m));
    104:   PetscCall(STGetOperator(eps->st,&Op));
    105:   for (j=k;j<m;j++) {
    106:     PetscCall(BVMatMultColumn(eps->V,Op,j));
    107:     which[j] = PETSC_TRUE;
    108:     if (j-2>=k) which[j-2] = PETSC_FALSE;
    109:     PetscCall(BVOrthogonalizeSomeColumn(eps->V,j+1,which,hwork,beta+j,breakdown));
    110:     alpha[j] = PetscRealPart(hwork[j]);
    111:     if (PetscUnlikely(*breakdown)) {
    112:       *M = j+1;
    113:       break;
    114:     } else PetscCall(BVScaleColumn(eps->V,j+1,1/beta[j]));
    115:   }
    116:   PetscCall(STRestoreOperator(eps->st,&Op));
    117:   if (m > 100) PetscCall(PetscFree2(which,hwork));
    118:   PetscFunctionReturn(PETSC_SUCCESS);
    119: }

    121: /*
    122:    DenseTridiagonal - Solves a real tridiagonal Hermitian Eigenvalue Problem.

    124:    Input Parameters:
    125: +  n   - dimension of the eigenproblem
    126: .  D   - pointer to the array containing the diagonal elements
    127: -  E   - pointer to the array containing the off-diagonal elements

    129:    Output Parameters:
    130: +  w  - pointer to the array to store the computed eigenvalues
    131: -  V  - pointer to the array to store the eigenvectors

    133:    Notes:
    134:    If V is NULL then the eigenvectors are not computed.

    136:    This routine use LAPACK routines xSTEVR.
    137: */
    138: static PetscErrorCode DenseTridiagonal(PetscInt n_,PetscReal *D,PetscReal *E,PetscReal *w,PetscScalar *V)
    139: {
    140:   PetscReal      abstol = 0.0,vl,vu,*work;
    141:   PetscBLASInt   il,iu,m,*isuppz,n,lwork,*iwork,liwork,info;
    142:   const char     *jobz;
    143: #if defined(PETSC_USE_COMPLEX)
    144:   PetscInt       i,j;
    145:   PetscReal      *VV=NULL;
    146: #endif

    148:   PetscFunctionBegin;
    149:   PetscCall(PetscBLASIntCast(n_,&n));
    150:   PetscCall(PetscBLASIntCast(20*n_,&lwork));
    151:   PetscCall(PetscBLASIntCast(10*n_,&liwork));
    152:   if (V) {
    153:     jobz = "V";
    154: #if defined(PETSC_USE_COMPLEX)
    155:     PetscCall(PetscMalloc1(n*n,&VV));
    156: #endif
    157:   } else jobz = "N";
    158:   PetscCall(PetscMalloc3(2*n,&isuppz,lwork,&work,liwork,&iwork));
    159:   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
    160: #if defined(PETSC_USE_COMPLEX)
    161:   PetscCallBLAS("LAPACKstevr",LAPACKstevr_(jobz,"A",&n,D,E,&vl,&vu,&il,&iu,&abstol,&m,w,VV,&n,isuppz,work,&lwork,iwork,&liwork,&info));
    162: #else
    163:   PetscCallBLAS("LAPACKstevr",LAPACKstevr_(jobz,"A",&n,D,E,&vl,&vu,&il,&iu,&abstol,&m,w,V,&n,isuppz,work,&lwork,iwork,&liwork,&info));
    164: #endif
    165:   PetscCall(PetscFPTrapPop());
    166:   SlepcCheckLapackInfo("stevr",info);
    167: #if defined(PETSC_USE_COMPLEX)
    168:   if (V) {
    169:     for (i=0;i<n;i++)
    170:       for (j=0;j<n;j++)
    171:         V[i*n+j] = VV[i*n+j];
    172:     PetscCall(PetscFree(VV));
    173:   }
    174: #endif
    175:   PetscCall(PetscFree3(isuppz,work,iwork));
    176:   PetscFunctionReturn(PETSC_SUCCESS);
    177: }

    179: /*
    180:    EPSSelectiveLanczos - Selective reorthogonalization.
    181: */
    182: static PetscErrorCode EPSSelectiveLanczos(EPS eps,PetscReal *alpha,PetscReal *beta,PetscInt k,PetscInt *M,PetscBool *breakdown,PetscReal anorm)
    183: {
    184:   EPS_LANCZOS    *lanczos = (EPS_LANCZOS*)eps->data;
    185:   PetscInt       i,j,m = *M,n,nritz=0,nritzo;
    186:   Vec            vj1,av;
    187:   Mat            Op;
    188:   PetscReal      *d,*e,*ritz,norm;
    189:   PetscScalar    *Y,*hwork;
    190:   PetscBool      *which;

    192:   PetscFunctionBegin;
    193:   PetscCall(PetscCalloc6(m+1,&d,m,&e,m,&ritz,m*m,&Y,m,&which,m,&hwork));
    194:   for (i=0;i<k;i++) which[i] = PETSC_TRUE;
    195:   PetscCall(STGetOperator(eps->st,&Op));

    197:   for (j=k;j<m;j++) {
    198:     PetscCall(BVSetActiveColumns(eps->V,0,m));

    200:     /* Lanczos step */
    201:     PetscCall(BVMatMultColumn(eps->V,Op,j));
    202:     which[j] = PETSC_TRUE;
    203:     if (j-2>=k) which[j-2] = PETSC_FALSE;
    204:     PetscCall(BVOrthogonalizeSomeColumn(eps->V,j+1,which,hwork,&norm,breakdown));
    205:     alpha[j] = PetscRealPart(hwork[j]);
    206:     beta[j] = norm;
    207:     if (PetscUnlikely(*breakdown)) {
    208:       *M = j+1;
    209:       break;
    210:     }

    212:     /* Compute eigenvalues and eigenvectors Y of the tridiagonal block */
    213:     n = j-k+1;
    214:     for (i=0;i<n;i++) {
    215:       d[i] = alpha[i+k];
    216:       e[i] = beta[i+k];
    217:     }
    218:     PetscCall(DenseTridiagonal(n,d,e,ritz,Y));

    220:     /* Estimate ||A|| */
    221:     for (i=0;i<n;i++)
    222:       if (PetscAbsReal(ritz[i]) > anorm) anorm = PetscAbsReal(ritz[i]);

    224:     /* Compute nearly converged Ritz vectors */
    225:     nritzo = 0;
    226:     for (i=0;i<n;i++) {
    227:       if (norm*PetscAbsScalar(Y[i*n+n-1]) < PETSC_SQRT_MACHINE_EPSILON*anorm) nritzo++;
    228:     }
    229:     if (nritzo>nritz) {
    230:       nritz = 0;
    231:       for (i=0;i<n;i++) {
    232:         if (norm*PetscAbsScalar(Y[i*n+n-1]) < PETSC_SQRT_MACHINE_EPSILON*anorm) {
    233:           PetscCall(BVSetActiveColumns(eps->V,k,k+n));
    234:           PetscCall(BVGetColumn(lanczos->AV,nritz,&av));
    235:           PetscCall(BVMultVec(eps->V,1.0,0.0,av,Y+i*n));
    236:           PetscCall(BVRestoreColumn(lanczos->AV,nritz,&av));
    237:           nritz++;
    238:         }
    239:       }
    240:     }
    241:     if (nritz > 0) {
    242:       PetscCall(BVGetColumn(eps->V,j+1,&vj1));
    243:       PetscCall(BVSetActiveColumns(lanczos->AV,0,nritz));
    244:       PetscCall(BVOrthogonalizeVec(lanczos->AV,vj1,hwork,&norm,breakdown));
    245:       PetscCall(BVRestoreColumn(eps->V,j+1,&vj1));
    246:       if (PetscUnlikely(*breakdown)) {
    247:         *M = j+1;
    248:         break;
    249:       }
    250:     }
    251:     PetscCall(BVScaleColumn(eps->V,j+1,1.0/norm));
    252:   }

    254:   PetscCall(STRestoreOperator(eps->st,&Op));
    255:   PetscCall(PetscFree6(d,e,ritz,Y,which,hwork));
    256:   PetscFunctionReturn(PETSC_SUCCESS);
    257: }

    259: static void update_omega(PetscReal *omega,PetscReal *omega_old,PetscInt j,PetscReal *alpha,PetscReal *beta,PetscReal eps1,PetscReal anorm)
    260: {
    261:   PetscInt  k;
    262:   PetscReal T,binv;

    264:   PetscFunctionBegin;
    265:   /* Estimate of contribution to roundoff errors from A*v
    266:        fl(A*v) = A*v + f,
    267:      where ||f|| \approx eps1*||A||.
    268:      For a full matrix A, a rule-of-thumb estimate is eps1 = sqrt(n)*eps */
    269:   T = eps1*anorm;
    270:   binv = 1.0/beta[j+1];

    272:   /* Update omega(1) using omega(0)==0 */
    273:   omega_old[0]= beta[1]*omega[1] + (alpha[0]-alpha[j])*omega[0] - beta[j]*omega_old[0];
    274:   if (omega_old[0] > 0) omega_old[0] = binv*(omega_old[0] + T);
    275:   else omega_old[0] = binv*(omega_old[0] - T);

    277:   /* Update remaining components */
    278:   for (k=1;k<j-1;k++) {
    279:     omega_old[k] = beta[k+1]*omega[k+1] + (alpha[k]-alpha[j])*omega[k] + beta[k]*omega[k-1] - beta[j]*omega_old[k];
    280:     if (omega_old[k] > 0) omega_old[k] = binv*(omega_old[k] + T);
    281:     else omega_old[k] = binv*(omega_old[k] - T);
    282:   }
    283:   omega_old[j-1] = binv*T;

    285:   /* Swap omega and omega_old */
    286:   for (k=0;k<j;k++) {
    287:     omega[k] = omega_old[k];
    288:     omega_old[k] = omega[k];
    289:   }
    290:   omega[j] = eps1;
    291:   PetscFunctionReturnVoid();
    292: }

    294: static void compute_int(PetscBool *which,PetscReal *mu,PetscInt j,PetscReal delta,PetscReal eta)
    295: {
    296:   PetscInt  i,k,maxpos;
    297:   PetscReal max;
    298:   PetscBool found;

    300:   PetscFunctionBegin;
    301:   /* initialize which */
    302:   found = PETSC_FALSE;
    303:   maxpos = 0;
    304:   max = 0.0;
    305:   for (i=0;i<j;i++) {
    306:     if (PetscAbsReal(mu[i]) >= delta) {
    307:       which[i] = PETSC_TRUE;
    308:       found = PETSC_TRUE;
    309:     } else which[i] = PETSC_FALSE;
    310:     if (PetscAbsReal(mu[i]) > max) {
    311:       maxpos = i;
    312:       max = PetscAbsReal(mu[i]);
    313:     }
    314:   }
    315:   if (!found) which[maxpos] = PETSC_TRUE;

    317:   for (i=0;i<j;i++) {
    318:     if (which[i]) {
    319:       /* find left interval */
    320:       for (k=i;k>=0;k--) {
    321:         if (PetscAbsReal(mu[k])<eta || which[k]) break;
    322:         else which[k] = PETSC_TRUE;
    323:       }
    324:       /* find right interval */
    325:       for (k=i+1;k<j;k++) {
    326:         if (PetscAbsReal(mu[k])<eta || which[k]) break;
    327:         else which[k] = PETSC_TRUE;
    328:       }
    329:     }
    330:   }
    331:   PetscFunctionReturnVoid();
    332: }

    334: /*
    335:    EPSPartialLanczos - Partial reorthogonalization.
    336: */
    337: static PetscErrorCode EPSPartialLanczos(EPS eps,PetscReal *alpha,PetscReal *beta,PetscInt k,PetscInt *M,PetscBool *breakdown,PetscReal anorm)
    338: {
    339:   EPS_LANCZOS    *lanczos = (EPS_LANCZOS*)eps->data;
    340:   PetscInt       i,j,m = *M;
    341:   Mat            Op;
    342:   PetscReal      norm,*omega,lomega[100],*omega_old,lomega_old[100],eps1,delta,eta;
    343:   PetscBool      *which,lwhich[100],*which2,lwhich2[100];
    344:   PetscBool      reorth = PETSC_FALSE,force_reorth = PETSC_FALSE;
    345:   PetscBool      fro = PETSC_FALSE,estimate_anorm = PETSC_FALSE;
    346:   PetscScalar    *hwork,lhwork[100];

    348:   PetscFunctionBegin;
    349:   if (m>100) PetscCall(PetscMalloc5(m,&omega,m,&omega_old,m,&which,m,&which2,m,&hwork));
    350:   else {
    351:     omega     = lomega;
    352:     omega_old = lomega_old;
    353:     which     = lwhich;
    354:     which2    = lwhich2;
    355:     hwork     = lhwork;
    356:   }

    358:   eps1 = PetscSqrtReal((PetscReal)eps->n)*PETSC_MACHINE_EPSILON/2;
    359:   delta = PETSC_SQRT_MACHINE_EPSILON/PetscSqrtReal((PetscReal)eps->ncv);
    360:   eta = PetscPowReal(PETSC_MACHINE_EPSILON,3.0/4.0)/PetscSqrtReal((PetscReal)eps->ncv);
    361:   if (anorm < 0.0) {
    362:     anorm = 1.0;
    363:     estimate_anorm = PETSC_TRUE;
    364:   }
    365:   for (i=0;i<PetscMax(100,m);i++) omega[i] = omega_old[i] = 0.0;
    366:   for (i=0;i<k;i++) which[i] = PETSC_TRUE;

    368:   PetscCall(BVSetActiveColumns(eps->V,0,m));
    369:   PetscCall(STGetOperator(eps->st,&Op));
    370:   for (j=k;j<m;j++) {
    371:     PetscCall(BVMatMultColumn(eps->V,Op,j));
    372:     if (fro) {
    373:       /* Lanczos step with full reorthogonalization */
    374:       PetscCall(BVOrthogonalizeColumn(eps->V,j+1,hwork,&norm,breakdown));
    375:       alpha[j] = PetscRealPart(hwork[j]);
    376:     } else {
    377:       /* Lanczos step */
    378:       which[j] = PETSC_TRUE;
    379:       if (j-2>=k) which[j-2] = PETSC_FALSE;
    380:       PetscCall(BVOrthogonalizeSomeColumn(eps->V,j+1,which,hwork,&norm,breakdown));
    381:       alpha[j] = PetscRealPart(hwork[j]);
    382:       beta[j] = norm;

    384:       /* Estimate ||A|| if needed */
    385:       if (estimate_anorm) {
    386:         if (j>k) anorm = PetscMax(anorm,PetscAbsReal(alpha[j])+norm+beta[j-1]);
    387:         else anorm = PetscMax(anorm,PetscAbsReal(alpha[j])+norm);
    388:       }

    390:       /* Check if reorthogonalization is needed */
    391:       reorth = PETSC_FALSE;
    392:       if (j>k) {
    393:         update_omega(omega,omega_old,j,alpha,beta-1,eps1,anorm);
    394:         for (i=0;i<j-k;i++) {
    395:           if (PetscAbsReal(omega[i]) > delta) reorth = PETSC_TRUE;
    396:         }
    397:       }
    398:       if (reorth || force_reorth) {
    399:         for (i=0;i<k;i++) which2[i] = PETSC_FALSE;
    400:         for (i=k;i<=j;i++) which2[i] = PETSC_TRUE;
    401:         if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_PERIODIC) {
    402:           /* Periodic reorthogonalization */
    403:           if (force_reorth) force_reorth = PETSC_FALSE;
    404:           else force_reorth = PETSC_TRUE;
    405:           for (i=0;i<j-k;i++) omega[i] = eps1;
    406:         } else {
    407:           /* Partial reorthogonalization */
    408:           if (force_reorth) force_reorth = PETSC_FALSE;
    409:           else {
    410:             force_reorth = PETSC_TRUE;
    411:             compute_int(which2+k,omega,j-k,delta,eta);
    412:             for (i=0;i<j-k;i++) {
    413:               if (which2[i+k]) omega[i] = eps1;
    414:             }
    415:           }
    416:         }
    417:         PetscCall(BVOrthogonalizeSomeColumn(eps->V,j+1,which2,hwork,&norm,breakdown));
    418:       }
    419:     }

    421:     if (PetscUnlikely(*breakdown || norm < eps->n*anorm*PETSC_MACHINE_EPSILON)) {
    422:       *M = j+1;
    423:       break;
    424:     }
    425:     if (!fro && norm*delta < anorm*eps1) {
    426:       fro = PETSC_TRUE;
    427:       PetscCall(PetscInfo(eps,"Switching to full reorthogonalization at iteration %" PetscInt_FMT "\n",eps->its));
    428:     }
    429:     beta[j] = norm;
    430:     PetscCall(BVScaleColumn(eps->V,j+1,1.0/norm));
    431:   }

    433:   PetscCall(STRestoreOperator(eps->st,&Op));
    434:   if (m>100) PetscCall(PetscFree5(omega,omega_old,which,which2,hwork));
    435:   PetscFunctionReturn(PETSC_SUCCESS);
    436: }

    438: /*
    439:    EPSBasicLanczos - Computes an m-step Lanczos factorization. The first k
    440:    columns are assumed to be locked and therefore they are not modified. On
    441:    exit, the following relation is satisfied:

    443:                     OP * V - V * T = f * e_m^T

    445:    where the columns of V are the Lanczos vectors, T is a tridiagonal matrix,
    446:    f is the residual vector and e_m is the m-th vector of the canonical basis.
    447:    The Lanczos vectors (together with vector f) are B-orthogonal (to working
    448:    accuracy) if full reorthogonalization is being used, otherwise they are
    449:    (B-)semi-orthogonal. On exit, beta contains the B-norm of f and the next
    450:    Lanczos vector can be computed as v_{m+1} = f / beta.

    452:    This function simply calls another function which depends on the selected
    453:    reorthogonalization strategy.
    454: */
    455: static PetscErrorCode EPSBasicLanczos(EPS eps,PetscInt k,PetscInt *m,PetscReal *betam,PetscBool *breakdown,PetscReal anorm)
    456: {
    457:   EPS_LANCZOS        *lanczos = (EPS_LANCZOS*)eps->data;
    458:   PetscScalar        *T;
    459:   PetscInt           i,n=*m,ld;
    460:   PetscReal          *alpha,*beta;
    461:   BVOrthogRefineType orthog_ref;
    462:   Mat                Op,M;

    464:   PetscFunctionBegin;
    465:   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
    466:   switch (lanczos->reorthog) {
    467:     case EPS_LANCZOS_REORTHOG_LOCAL:
    468:       PetscCall(DSGetArrayReal(eps->ds,DS_MAT_T,&alpha));
    469:       beta = alpha + ld;
    470:       PetscCall(EPSLocalLanczos(eps,alpha,beta,k,m,breakdown));
    471:       *betam = beta[*m-1];
    472:       PetscCall(DSRestoreArrayReal(eps->ds,DS_MAT_T,&alpha));
    473:       break;
    474:     case EPS_LANCZOS_REORTHOG_FULL:
    475:       PetscCall(STGetOperator(eps->st,&Op));
    476:       PetscCall(DSGetMat(eps->ds,DS_MAT_T,&M));
    477:       PetscCall(BVMatLanczos(eps->V,Op,M,k,m,betam,breakdown));
    478:       PetscCall(DSRestoreMat(eps->ds,DS_MAT_T,&M));
    479:       PetscCall(STRestoreOperator(eps->st,&Op));
    480:       break;
    481:     case EPS_LANCZOS_REORTHOG_SELECTIVE:
    482:       PetscCall(DSGetArrayReal(eps->ds,DS_MAT_T,&alpha));
    483:       beta = alpha + ld;
    484:       PetscCall(EPSSelectiveLanczos(eps,alpha,beta,k,m,breakdown,anorm));
    485:       *betam = beta[*m-1];
    486:       PetscCall(DSRestoreArrayReal(eps->ds,DS_MAT_T,&alpha));
    487:       break;
    488:     case EPS_LANCZOS_REORTHOG_PERIODIC:
    489:     case EPS_LANCZOS_REORTHOG_PARTIAL:
    490:       PetscCall(DSGetArrayReal(eps->ds,DS_MAT_T,&alpha));
    491:       beta = alpha + ld;
    492:       PetscCall(EPSPartialLanczos(eps,alpha,beta,k,m,breakdown,anorm));
    493:       *betam = beta[*m-1];
    494:       PetscCall(DSRestoreArrayReal(eps->ds,DS_MAT_T,&alpha));
    495:       break;
    496:     case EPS_LANCZOS_REORTHOG_DELAYED:
    497:       PetscCall(PetscMalloc1(n*n,&T));
    498:       PetscCall(BVGetOrthogonalization(eps->V,NULL,&orthog_ref,NULL,NULL));
    499:       if (orthog_ref == BV_ORTHOG_REFINE_NEVER) PetscCall(EPSDelayedArnoldi1(eps,T,n,k,m,betam,breakdown));
    500:       else PetscCall(EPSDelayedArnoldi(eps,T,n,k,m,betam,breakdown));
    501:       n = *m;
    502:       PetscCall(DSGetArrayReal(eps->ds,DS_MAT_T,&alpha));
    503:       beta = alpha + ld;
    504:       for (i=k;i<n-1;i++) {
    505:         alpha[i] = PetscRealPart(T[n*i+i]);
    506:         beta[i] = PetscRealPart(T[n*i+i+1]);
    507:       }
    508:       alpha[n-1] = PetscRealPart(T[n*(n-1)+n-1]);
    509:       beta[n-1] = *betam;
    510:       PetscCall(DSRestoreArrayReal(eps->ds,DS_MAT_T,&alpha));
    511:       PetscCall(PetscFree(T));
    512:       break;
    513:   }
    514:   PetscFunctionReturn(PETSC_SUCCESS);
    515: }

    517: static PetscErrorCode EPSSolve_Lanczos(EPS eps)
    518: {
    519:   EPS_LANCZOS    *lanczos = (EPS_LANCZOS*)eps->data;
    520:   PetscInt       nconv,i,j,k,l,x,n,*perm,restart,ncv=eps->ncv,r,ld;
    521:   Vec            vi,vj,w;
    522:   Mat            U;
    523:   PetscScalar    *Y,*ritz,stmp;
    524:   PetscReal      *bnd,anorm,beta,norm,rtmp,resnorm;
    525:   PetscBool      breakdown;
    526:   char           *conv,ctmp;

    528:   PetscFunctionBegin;
    529:   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
    530:   PetscCall(PetscMalloc4(ncv,&ritz,ncv,&bnd,ncv,&perm,ncv,&conv));

    532:   /* The first Lanczos vector is the normalized initial vector */
    533:   PetscCall(EPSGetStartVector(eps,0,NULL));

    535:   anorm = -1.0;
    536:   nconv = 0;

    538:   /* Restart loop */
    539:   while (eps->reason == EPS_CONVERGED_ITERATING) {
    540:     eps->its++;

    542:     /* Compute an ncv-step Lanczos factorization */
    543:     n = PetscMin(nconv+eps->mpd,ncv);
    544:     PetscCall(DSSetDimensions(eps->ds,n,nconv,PETSC_DEFAULT));
    545:     PetscCall(EPSBasicLanczos(eps,nconv,&n,&beta,&breakdown,anorm));
    546:     PetscCall(DSSetDimensions(eps->ds,n,nconv,0));
    547:     PetscCall(DSSetState(eps->ds,DS_STATE_INTERMEDIATE));
    548:     PetscCall(BVSetActiveColumns(eps->V,nconv,n));

    550:     /* Solve projected problem */
    551:     PetscCall(DSSolve(eps->ds,ritz,NULL));
    552:     PetscCall(DSSort(eps->ds,ritz,NULL,NULL,NULL,NULL));
    553:     PetscCall(DSSynchronize(eps->ds,ritz,NULL));

    555:     /* Estimate ||A|| */
    556:     for (i=nconv;i<n;i++)
    557:       anorm = PetscMax(anorm,PetscAbsReal(PetscRealPart(ritz[i])));

    559:     /* Compute residual norm estimates as beta*abs(Y(m,:)) + eps*||A|| */
    560:     PetscCall(DSGetArray(eps->ds,DS_MAT_Q,&Y));
    561:     for (i=nconv;i<n;i++) {
    562:       resnorm = beta*PetscAbsScalar(Y[n-1+i*ld]) + PETSC_MACHINE_EPSILON*anorm;
    563:       PetscCall((*eps->converged)(eps,ritz[i],eps->eigi[i],resnorm,&bnd[i],eps->convergedctx));
    564:       if (bnd[i]<eps->tol) conv[i] = 'C';
    565:       else conv[i] = 'N';
    566:     }
    567:     PetscCall(DSRestoreArray(eps->ds,DS_MAT_Q,&Y));

    569:     /* purge repeated ritz values */
    570:     if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_LOCAL) {
    571:       for (i=nconv+1;i<n;i++) {
    572:         if (conv[i] == 'C' && PetscAbsScalar((ritz[i]-ritz[i-1])/ritz[i]) < eps->tol) conv[i] = 'R';
    573:       }
    574:     }

    576:     /* Compute restart vector */
    577:     if (breakdown) PetscCall(PetscInfo(eps,"Breakdown in Lanczos method (it=%" PetscInt_FMT " norm=%g)\n",eps->its,(double)beta));
    578:     else {
    579:       restart = nconv;
    580:       while (restart<n && conv[restart] != 'N') restart++;
    581:       if (restart >= n) {
    582:         breakdown = PETSC_TRUE;
    583:       } else {
    584:         for (i=restart+1;i<n;i++) {
    585:           if (conv[i] == 'N') {
    586:             PetscCall(SlepcSCCompare(eps->sc,ritz[restart],0.0,ritz[i],0.0,&r));
    587:             if (r>0) restart = i;
    588:           }
    589:         }
    590:         PetscCall(DSGetArray(eps->ds,DS_MAT_Q,&Y));
    591:         PetscCall(BVMultColumn(eps->V,1.0,0.0,n,Y+restart*ld+nconv));
    592:         PetscCall(DSRestoreArray(eps->ds,DS_MAT_Q,&Y));
    593:       }
    594:     }

    596:     /* Count and put converged eigenvalues first */
    597:     for (i=nconv;i<n;i++) perm[i] = i;
    598:     for (k=nconv;k<n;k++) {
    599:       if (conv[perm[k]] != 'C') {
    600:         j = k + 1;
    601:         while (j<n && conv[perm[j]] != 'C') j++;
    602:         if (j>=n) break;
    603:         l = perm[k]; perm[k] = perm[j]; perm[j] = l;
    604:       }
    605:     }

    607:     /* Sort eigenvectors according to permutation */
    608:     PetscCall(DSGetArray(eps->ds,DS_MAT_Q,&Y));
    609:     for (i=nconv;i<k;i++) {
    610:       x = perm[i];
    611:       if (x != i) {
    612:         j = i + 1;
    613:         while (perm[j] != i) j++;
    614:         /* swap eigenvalues i and j */
    615:         stmp = ritz[x]; ritz[x] = ritz[i]; ritz[i] = stmp;
    616:         rtmp = bnd[x]; bnd[x] = bnd[i]; bnd[i] = rtmp;
    617:         ctmp = conv[x]; conv[x] = conv[i]; conv[i] = ctmp;
    618:         perm[j] = x; perm[i] = i;
    619:         /* swap eigenvectors i and j */
    620:         for (l=0;l<n;l++) {
    621:           stmp = Y[l+x*ld]; Y[l+x*ld] = Y[l+i*ld]; Y[l+i*ld] = stmp;
    622:         }
    623:       }
    624:     }
    625:     PetscCall(DSRestoreArray(eps->ds,DS_MAT_Q,&Y));

    627:     /* compute converged eigenvectors */
    628:     PetscCall(DSGetMat(eps->ds,DS_MAT_Q,&U));
    629:     PetscCall(BVMultInPlace(eps->V,U,nconv,k));
    630:     PetscCall(DSRestoreMat(eps->ds,DS_MAT_Q,&U));

    632:     /* purge spurious ritz values */
    633:     if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_LOCAL) {
    634:       for (i=nconv;i<k;i++) {
    635:         PetscCall(BVGetColumn(eps->V,i,&vi));
    636:         PetscCall(VecNorm(vi,NORM_2,&norm));
    637:         PetscCall(VecScale(vi,1.0/norm));
    638:         w = eps->work[0];
    639:         PetscCall(STApply(eps->st,vi,w));
    640:         PetscCall(VecAXPY(w,-ritz[i],vi));
    641:         PetscCall(BVRestoreColumn(eps->V,i,&vi));
    642:         PetscCall(VecNorm(w,NORM_2,&norm));
    643:         PetscCall((*eps->converged)(eps,ritz[i],eps->eigi[i],norm,&bnd[i],eps->convergedctx));
    644:         if (bnd[i]>=eps->tol) conv[i] = 'S';
    645:       }
    646:       for (i=nconv;i<k;i++) {
    647:         if (conv[i] != 'C') {
    648:           j = i + 1;
    649:           while (j<k && conv[j] != 'C') j++;
    650:           if (j>=k) break;
    651:           /* swap eigenvalues i and j */
    652:           stmp = ritz[j]; ritz[j] = ritz[i]; ritz[i] = stmp;
    653:           rtmp = bnd[j]; bnd[j] = bnd[i]; bnd[i] = rtmp;
    654:           ctmp = conv[j]; conv[j] = conv[i]; conv[i] = ctmp;
    655:           /* swap eigenvectors i and j */
    656:           PetscCall(BVGetColumn(eps->V,i,&vi));
    657:           PetscCall(BVGetColumn(eps->V,j,&vj));
    658:           PetscCall(VecSwap(vi,vj));
    659:           PetscCall(BVRestoreColumn(eps->V,i,&vi));
    660:           PetscCall(BVRestoreColumn(eps->V,j,&vj));
    661:         }
    662:       }
    663:       k = i;
    664:     }

    666:     /* store ritz values and estimated errors */
    667:     for (i=nconv;i<n;i++) {
    668:       eps->eigr[i] = ritz[i];
    669:       eps->errest[i] = bnd[i];
    670:     }
    671:     nconv = k;
    672:     PetscCall(EPSMonitor(eps,eps->its,nconv,eps->eigr,eps->eigi,eps->errest,n));
    673:     PetscCall((*eps->stopping)(eps,eps->its,eps->max_it,nconv,eps->nev,&eps->reason,eps->stoppingctx));

    675:     if (eps->reason == EPS_CONVERGED_ITERATING) { /* copy restart vector */
    676:       PetscCall(BVCopyColumn(eps->V,n,nconv));
    677:       if (lanczos->reorthog == EPS_LANCZOS_REORTHOG_LOCAL && !breakdown) {
    678:         /* Reorthonormalize restart vector */
    679:         PetscCall(BVOrthonormalizeColumn(eps->V,nconv,PETSC_FALSE,NULL,&breakdown));
    680:       }
    681:       if (breakdown) {
    682:         /* Use random vector for restarting */
    683:         PetscCall(PetscInfo(eps,"Using random vector for restart\n"));
    684:         PetscCall(EPSGetStartVector(eps,nconv,&breakdown));
    685:       }
    686:       if (PetscUnlikely(breakdown)) { /* give up */
    687:         eps->reason = EPS_DIVERGED_BREAKDOWN;
    688:         PetscCall(PetscInfo(eps,"Unable to generate more start vectors\n"));
    689:       }
    690:     }
    691:   }
    692:   eps->nconv = nconv;

    694:   PetscCall(PetscFree4(ritz,bnd,perm,conv));
    695:   PetscFunctionReturn(PETSC_SUCCESS);
    696: }

    698: static PetscErrorCode EPSSetFromOptions_Lanczos(EPS eps,PetscOptionItems *PetscOptionsObject)
    699: {
    700:   EPS_LANCZOS            *lanczos = (EPS_LANCZOS*)eps->data;
    701:   PetscBool              flg;
    702:   EPSLanczosReorthogType reorthog=EPS_LANCZOS_REORTHOG_LOCAL,curval;

    704:   PetscFunctionBegin;
    705:   PetscOptionsHeadBegin(PetscOptionsObject,"EPS Lanczos Options");

    707:     curval = (lanczos->reorthog==(EPSLanczosReorthogType)-1)? EPS_LANCZOS_REORTHOG_LOCAL: lanczos->reorthog;
    708:     PetscCall(PetscOptionsEnum("-eps_lanczos_reorthog","Lanczos reorthogonalization","EPSLanczosSetReorthog",EPSLanczosReorthogTypes,(PetscEnum)curval,(PetscEnum*)&reorthog,&flg));
    709:     if (flg) PetscCall(EPSLanczosSetReorthog(eps,reorthog));

    711:   PetscOptionsHeadEnd();
    712:   PetscFunctionReturn(PETSC_SUCCESS);
    713: }

    715: static PetscErrorCode EPSLanczosSetReorthog_Lanczos(EPS eps,EPSLanczosReorthogType reorthog)
    716: {
    717:   EPS_LANCZOS *lanczos = (EPS_LANCZOS*)eps->data;

    719:   PetscFunctionBegin;
    720:   switch (reorthog) {
    721:     case EPS_LANCZOS_REORTHOG_LOCAL:
    722:     case EPS_LANCZOS_REORTHOG_FULL:
    723:     case EPS_LANCZOS_REORTHOG_DELAYED:
    724:     case EPS_LANCZOS_REORTHOG_SELECTIVE:
    725:     case EPS_LANCZOS_REORTHOG_PERIODIC:
    726:     case EPS_LANCZOS_REORTHOG_PARTIAL:
    727:       if (lanczos->reorthog != reorthog) {
    728:         lanczos->reorthog = reorthog;
    729:         eps->state = EPS_STATE_INITIAL;
    730:       }
    731:       break;
    732:     default:
    733:       SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Invalid reorthogonalization type");
    734:   }
    735:   PetscFunctionReturn(PETSC_SUCCESS);
    736: }

    738: /*@
    739:    EPSLanczosSetReorthog - Sets the type of reorthogonalization used during the Lanczos
    740:    iteration.

    742:    Logically Collective

    744:    Input Parameters:
    745: +  eps - the eigenproblem solver context
    746: -  reorthog - the type of reorthogonalization

    748:    Options Database Key:
    749: .  -eps_lanczos_reorthog - Sets the reorthogonalization type (either 'local', 'selective',
    750:                          'periodic', 'partial', 'full' or 'delayed')

    752:    Level: advanced

    754: .seealso: EPSLanczosGetReorthog(), EPSLanczosReorthogType
    755: @*/
    756: PetscErrorCode EPSLanczosSetReorthog(EPS eps,EPSLanczosReorthogType reorthog)
    757: {
    758:   PetscFunctionBegin;
    761:   PetscTryMethod(eps,"EPSLanczosSetReorthog_C",(EPS,EPSLanczosReorthogType),(eps,reorthog));
    762:   PetscFunctionReturn(PETSC_SUCCESS);
    763: }

    765: static PetscErrorCode EPSLanczosGetReorthog_Lanczos(EPS eps,EPSLanczosReorthogType *reorthog)
    766: {
    767:   EPS_LANCZOS *lanczos = (EPS_LANCZOS*)eps->data;

    769:   PetscFunctionBegin;
    770:   *reorthog = lanczos->reorthog;
    771:   PetscFunctionReturn(PETSC_SUCCESS);
    772: }

    774: /*@
    775:    EPSLanczosGetReorthog - Gets the type of reorthogonalization used during
    776:    the Lanczos iteration.

    778:    Not Collective

    780:    Input Parameter:
    781: .  eps - the eigenproblem solver context

    783:    Output Parameter:
    784: .  reorthog - the type of reorthogonalization

    786:    Level: advanced

    788: .seealso: EPSLanczosSetReorthog(), EPSLanczosReorthogType
    789: @*/
    790: PetscErrorCode EPSLanczosGetReorthog(EPS eps,EPSLanczosReorthogType *reorthog)
    791: {
    792:   PetscFunctionBegin;
    794:   PetscAssertPointer(reorthog,2);
    795:   PetscUseMethod(eps,"EPSLanczosGetReorthog_C",(EPS,EPSLanczosReorthogType*),(eps,reorthog));
    796:   PetscFunctionReturn(PETSC_SUCCESS);
    797: }

    799: static PetscErrorCode EPSReset_Lanczos(EPS eps)
    800: {
    801:   EPS_LANCZOS    *lanczos = (EPS_LANCZOS*)eps->data;

    803:   PetscFunctionBegin;
    804:   PetscCall(BVDestroy(&lanczos->AV));
    805:   lanczos->allocsize = 0;
    806:   PetscFunctionReturn(PETSC_SUCCESS);
    807: }

    809: static PetscErrorCode EPSDestroy_Lanczos(EPS eps)
    810: {
    811:   PetscFunctionBegin;
    812:   PetscCall(PetscFree(eps->data));
    813:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSLanczosSetReorthog_C",NULL));
    814:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSLanczosGetReorthog_C",NULL));
    815:   PetscFunctionReturn(PETSC_SUCCESS);
    816: }

    818: static PetscErrorCode EPSView_Lanczos(EPS eps,PetscViewer viewer)
    819: {
    820:   EPS_LANCZOS    *lanczos = (EPS_LANCZOS*)eps->data;
    821:   PetscBool      isascii;

    823:   PetscFunctionBegin;
    824:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
    825:   if (isascii) {
    826:     if (lanczos->reorthog != (EPSLanczosReorthogType)-1) PetscCall(PetscViewerASCIIPrintf(viewer,"  %s reorthogonalization\n",EPSLanczosReorthogTypes[lanczos->reorthog]));
    827:   }
    828:   PetscFunctionReturn(PETSC_SUCCESS);
    829: }

    831: SLEPC_EXTERN PetscErrorCode EPSCreate_Lanczos(EPS eps)
    832: {
    833:   EPS_LANCZOS    *ctx;

    835:   PetscFunctionBegin;
    836:   PetscCall(PetscNew(&ctx));
    837:   eps->data = (void*)ctx;
    838:   ctx->reorthog = (EPSLanczosReorthogType)-1;

    840:   eps->useds = PETSC_TRUE;

    842:   eps->ops->solve          = EPSSolve_Lanczos;
    843:   eps->ops->setup          = EPSSetUp_Lanczos;
    844:   eps->ops->setupsort      = EPSSetUpSort_Default;
    845:   eps->ops->setfromoptions = EPSSetFromOptions_Lanczos;
    846:   eps->ops->destroy        = EPSDestroy_Lanczos;
    847:   eps->ops->reset          = EPSReset_Lanczos;
    848:   eps->ops->view           = EPSView_Lanczos;
    849:   eps->ops->backtransform  = EPSBackTransform_Default;
    850:   eps->ops->computevectors = EPSComputeVectors_Hermitian;

    852:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSLanczosSetReorthog_C",EPSLanczosSetReorthog_Lanczos));
    853:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSLanczosGetReorthog_C",EPSLanczosGetReorthog_Lanczos));
    854:   PetscFunctionReturn(PETSC_SUCCESS);
    855: }