Date: 2023-01-26T12:19:30+00:00

Actual source code: nleigs-fullb.c
slepc-3.18.2 2023-01-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:    Full basis for the linearization of the rational approximation of non-linear eigenproblems
     12: */

     14: #include <slepc/private/nepimpl.h>
     15: #include "nleigs.h"

     17: static PetscErrorCode MatMult_FullBasis_Sinvert(Mat M,Vec x,Vec y)
     18: {
     19:   NEP_NLEIGS        *ctx;
     20:   NEP               nep;
     21:   const PetscScalar *px;
     22:   PetscScalar       *beta,*s,*xi,*t,*py,sigma;
     23:   PetscInt          nmat,d,i,k,m;
     24:   Vec               xx,xxx,yy,yyy,w,ww,www;

     26:   MatShellGetContext(M,&nep);
     27:   ctx = (NEP_NLEIGS*)nep->data;
     28:   beta = ctx->beta; s = ctx->s; xi = ctx->xi;
     29:   sigma = ctx->shifts[0];
     30:   nmat = ctx->nmat;
     31:   d = nmat-1;
     32:   m = nep->nloc;
     33:   PetscMalloc1(ctx->nmat,&t);
     34:   xx = ctx->w[0]; xxx = ctx->w[1]; yy = ctx->w[2]; yyy=ctx->w[3];
     35:   w = nep->work[0]; ww = nep->work[1]; www = nep->work[2];
     36:   VecGetArrayRead(x,&px);
     37:   VecGetArray(y,&py);
     38:   VecPlaceArray(xx,px+(d-1)*m);
     39:   VecPlaceArray(xxx,px+(d-2)*m);
     40:   VecPlaceArray(yy,py+(d-2)*m);
     41:   VecCopy(xxx,yy);
     42:   VecAXPY(yy,beta[d-1]/xi[d-2],xx);
     43:   VecScale(yy,1.0/(s[d-2]-sigma));
     44:   VecResetArray(xx);
     45:   VecResetArray(xxx);
     46:   VecResetArray(yy);
     47:   for (i=d-3;i>=0;i--) {
     48:     VecPlaceArray(xx,px+(i+1)*m);
     49:     VecPlaceArray(xxx,px+i*m);
     50:     VecPlaceArray(yy,py+i*m);
     51:     VecPlaceArray(yyy,py+(i+1)*m);
     52:     VecCopy(xxx,yy);
     53:     VecAXPY(yy,beta[i+1]/xi[i],xx);
     54:     VecAXPY(yy,-beta[i+1]*(1.0-sigma/xi[i]),yyy);
     55:     VecScale(yy,1.0/(s[i]-sigma));
     56:     VecResetArray(xx);
     57:     VecResetArray(xxx);
     58:     VecResetArray(yy);
     59:     VecResetArray(yyy);
     60:   }
     61:   if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
     62:     VecZeroEntries(w);
     63:     for (k=0;k<nep->nt;k++) {
     64:       VecZeroEntries(ww);
     65:       VecPlaceArray(xx,px+(d-1)*m);
     66:       VecAXPY(ww,-ctx->coeffD[k+nep->nt*d]/beta[d],xx);
     67:       VecResetArray(xx);
     68:       for (i=0;i<d-1;i++) {
     69:         VecPlaceArray(yy,py+i*m);
     70:         VecAXPY(ww,-ctx->coeffD[nep->nt*i+k],yy);
     71:         VecResetArray(yy);
     72:       }
     73:       MatMult(nep->A[k],ww,www);
     74:       VecAXPY(w,1.0,www);
     75:     }
     76:   } else {
     77:     VecPlaceArray(xx,px+(d-1)*m);
     78:     MatMult(ctx->D[d],xx,w);
     79:     VecScale(w,-1.0/beta[d]);
     80:     VecResetArray(xx);
     81:     for (i=0;i<d-1;i++) {
     82:       VecPlaceArray(yy,py+i*m);
     83:       MatMult(ctx->D[i],yy,ww);
     84:       VecResetArray(yy);
     85:       VecAXPY(w,-1.0,ww);
     86:     }
     87:   }
     88:   VecPlaceArray(yy,py+(d-1)*m);
     89:   KSPSolve(ctx->ksp[0],w,yy);
     90:   NEPNLEIGSEvalNRTFunct(nep,d-1,sigma,t);
     91:   for (i=0;i<d-1;i++) {
     92:     VecPlaceArray(yyy,py+i*m);
     93:     VecAXPY(yyy,t[i],yy);
     94:     VecResetArray(yyy);
     95:   }
     96:   VecScale(yy,t[d-1]);
     97:   VecResetArray(yy);
     98:   VecRestoreArrayRead(x,&px);
     99:   VecRestoreArray(y,&py);
    100:   PetscFree(t);
    101:   return 0;
    102: }

    104: static PetscErrorCode MatMultTranspose_FullBasis_Sinvert(Mat M,Vec x,Vec y)
    105: {
    106:   NEP_NLEIGS        *ctx;
    107:   NEP               nep;
    108:   const PetscScalar *px;
    109:   PetscScalar       *beta,*s,*xi,*t,*py,sigma;
    110:   PetscInt          nmat,d,i,k,m;
    111:   Vec               xx,yy,yyy,w,z0;

    113:   MatShellGetContext(M,&nep);
    114:   ctx = (NEP_NLEIGS*)nep->data;
    115:   beta = ctx->beta; s = ctx->s; xi = ctx->xi;
    116:   sigma = ctx->shifts[0];
    117:   nmat = ctx->nmat;
    118:   d = nmat-1;
    119:   m = nep->nloc;
    120:   PetscMalloc1(ctx->nmat,&t);
    121:   xx = ctx->w[0]; yy = ctx->w[1]; yyy=ctx->w[2];
    122:   w = nep->work[0]; z0 = nep->work[1];
    123:   VecGetArrayRead(x,&px);
    124:   VecGetArray(y,&py);
    125:   NEPNLEIGSEvalNRTFunct(nep,d,sigma,t);
    126:   VecPlaceArray(xx,px+(d-1)*m);
    127:   VecCopy(xx,w);
    128:   VecScale(w,t[d-1]);
    129:   VecResetArray(xx);
    130:   for (i=0;i<d-1;i++) {
    131:     VecPlaceArray(xx,px+i*m);
    132:     VecAXPY(w,t[i],xx);
    133:     VecResetArray(xx);
    134:   }
    135:   KSPSolveTranspose(ctx->ksp[0],w,z0);

    137:   VecPlaceArray(yy,py);
    138:   if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
    139:     VecZeroEntries(yy);
    140:     for (k=0;k<nep->nt;k++) {
    141:       MatMult(nep->A[k],z0,w);
    142:       VecAXPY(yy,ctx->coeffD[k],w);
    143:     }
    144:   } else MatMultTranspose(ctx->D[0],z0,yy);
    145:   VecPlaceArray(xx,px);
    146:   VecAXPY(yy,-1.0,xx);
    147:   VecResetArray(xx);
    148:   VecScale(yy,-1.0/(s[0]-sigma));
    149:   VecResetArray(yy);
    150:   for (i=2;i<d;i++) {
    151:     VecPlaceArray(yy,py+(i-1)*m);
    152:     if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
    153:       VecZeroEntries(yy);
    154:       for (k=0;k<nep->nt;k++) {
    155:         MatMult(nep->A[k],z0,w);
    156:         VecAXPY(yy,ctx->coeffD[k+(i-1)*nep->nt],w);
    157:       }
    158:     } else MatMultTranspose(ctx->D[i-1],z0,yy);
    159:     VecPlaceArray(yyy,py+(i-2)*m);
    160:     VecAXPY(yy,beta[i-1]*(1.0-sigma/xi[i-2]),yyy);
    161:     VecResetArray(yyy);
    162:     VecPlaceArray(xx,px+(i-1)*m);
    163:     VecAXPY(yy,-1.0,xx);
    164:     VecResetArray(xx);
    165:     VecScale(yy,-1.0/(s[i-1]-sigma));
    166:     VecResetArray(yy);
    167:   }
    168:   VecPlaceArray(yy,py+(d-1)*m);
    169:   if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
    170:     VecZeroEntries(yy);
    171:     for (k=0;k<nep->nt;k++) {
    172:       MatMult(nep->A[k],z0,w);
    173:       VecAXPY(yy,ctx->coeffD[k+d*nep->nt],w);
    174:     }
    175:   } else MatMultTranspose(ctx->D[d],z0,yy);
    176:   VecScale(yy,-1.0/beta[d]);
    177:   VecPlaceArray(yyy,py+(d-2)*m);
    178:   VecAXPY(yy,beta[d-1]/xi[d-2],yyy);
    179:   VecResetArray(yyy);
    180:   VecResetArray(yy);

    182:   for (i=d-2;i>0;i--) {
    183:     VecPlaceArray(yyy,py+(i-1)*m);
    184:     VecPlaceArray(yy,py+i*m);
    185:     VecAXPY(yy,beta[i]/xi[i-1],yyy);
    186:     VecResetArray(yyy);
    187:     VecResetArray(yy);
    188:   }

    190:   VecRestoreArrayRead(x,&px);
    191:   VecRestoreArray(y,&py);
    192:   PetscFree(t);
    193:   return 0;
    194: }

    196: static PetscErrorCode BackTransform_FullBasis(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
    197: {
    198:   NEP            nep;

    200:   STShellGetContext(st,&nep);
    201:   NEPNLEIGSBackTransform((PetscObject)nep,n,eigr,eigi);
    202:   return 0;
    203: }

    205: static PetscErrorCode Apply_FullBasis(ST st,Vec x,Vec y)
    206: {
    207:   NEP            nep;
    208:   NEP_NLEIGS     *ctx;

    210:   STShellGetContext(st,&nep);
    211:   ctx = (NEP_NLEIGS*)nep->data;
    212:   MatMult(ctx->A,x,y);
    213:   return 0;
    214: }

    216: static PetscErrorCode ApplyTranspose_FullBasis(ST st,Vec x,Vec y)
    217: {
    218:   NEP            nep;
    219:   NEP_NLEIGS     *ctx;

    221:   STShellGetContext(st,&nep);
    222:   ctx = (NEP_NLEIGS*)nep->data;
    223:   MatMultTranspose(ctx->A,x,y);
    224:   return 0;
    225: }

    227: PetscErrorCode NEPSetUp_NLEIGS_FullBasis(NEP nep)
    228: {
    229:   NEP_NLEIGS     *ctx=(NEP_NLEIGS*)nep->data;
    230:   ST             st;
    231:   Mat            Q;
    232:   PetscInt       i=0,deg=ctx->nmat-1;
    233:   PetscBool      trackall,istrivial,ks;
    234:   PetscScalar    *epsarray,*neparray;
    235:   Vec            veps,w=NULL;
    236:   EPSWhich       which;

    239:   if (!ctx->eps) NEPNLEIGSGetEPS(nep,&ctx->eps);
    240:   EPSGetST(ctx->eps,&st);
    241:   EPSSetTarget(ctx->eps,nep->target);
    242:   STSetDefaultShift(st,nep->target);
    243:   if (!((PetscObject)(ctx->eps))->type_name) EPSSetType(ctx->eps,EPSKRYLOVSCHUR);
    244:   else {
    245:     PetscObjectTypeCompare((PetscObject)ctx->eps,EPSKRYLOVSCHUR,&ks);
    247:   }
    248:   STSetType(st,STSHELL);
    249:   STShellSetContext(st,nep);
    250:   STShellSetBackTransform(st,BackTransform_FullBasis);
    251:   KSPGetOperators(ctx->ksp[0],&Q,NULL);
    252:   MatCreateVecsEmpty(Q,&ctx->w[0],&ctx->w[1]);
    253:   MatCreateVecsEmpty(Q,&ctx->w[2],&ctx->w[3]);
    254:   MatCreateShell(PetscObjectComm((PetscObject)nep),deg*nep->nloc,deg*nep->nloc,deg*nep->n,deg*nep->n,nep,&ctx->A);
    255:   MatShellSetOperation(ctx->A,MATOP_MULT,(void(*)(void))MatMult_FullBasis_Sinvert);
    256:   MatShellSetOperation(ctx->A,MATOP_MULT_TRANSPOSE,(void(*)(void))MatMultTranspose_FullBasis_Sinvert);
    257:   STShellSetApply(st,Apply_FullBasis);
    258:   STShellSetApplyTranspose(st,ApplyTranspose_FullBasis);
    259:   EPSSetOperators(ctx->eps,ctx->A,NULL);
    260:   EPSSetProblemType(ctx->eps,EPS_NHEP);
    261:   switch (nep->which) {
    262:     case NEP_TARGET_MAGNITUDE:   which = EPS_TARGET_MAGNITUDE; break;
    263:     case NEP_TARGET_REAL:        which = EPS_TARGET_REAL; break;
    264:     case NEP_TARGET_IMAGINARY:   which = EPS_TARGET_IMAGINARY; break;
    265:     case NEP_WHICH_USER:         which = EPS_WHICH_USER;
    266:       EPSSetEigenvalueComparison(ctx->eps,nep->sc->comparison,nep->sc->comparisonctx);
    267:       break;
    268:     default: SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"Should set a target selection in NEPSetWhichEigenpairs()");
    269:   }
    270:   EPSSetWhichEigenpairs(ctx->eps,which);
    271:   RGIsTrivial(nep->rg,&istrivial);
    272:   if (!istrivial) EPSSetRG(ctx->eps,nep->rg);
    273:   EPSSetDimensions(ctx->eps,nep->nev,nep->ncv,nep->mpd);
    274:   EPSSetTolerances(ctx->eps,SlepcDefaultTol(nep->tol),nep->max_it);
    275:   EPSSetTwoSided(ctx->eps,nep->twosided);
    276:   /* Transfer the trackall option from pep to eps */
    277:   NEPGetTrackAll(nep,&trackall);
    278:   EPSSetTrackAll(ctx->eps,trackall);

    280:   /* process initial vector */
    281:   if (nep->nini<0) {
    282:     VecCreateMPI(PetscObjectComm((PetscObject)ctx->eps),deg*nep->nloc,deg*nep->n,&veps);
    283:     VecGetArray(veps,&epsarray);
    284:     for (i=0;i<deg;i++) {
    285:       if (i<-nep->nini) {
    286:         VecGetArray(nep->IS[i],&neparray);
    287:         PetscArraycpy(epsarray+i*nep->nloc,neparray,nep->nloc);
    288:         VecRestoreArray(nep->IS[i],&neparray);
    289:       } else {
    290:         if (!w) VecDuplicate(nep->IS[0],&w);
    291:         VecSetRandom(w,NULL);
    292:         VecGetArray(w,&neparray);
    293:         PetscArraycpy(epsarray+i*nep->nloc,neparray,nep->nloc);
    294:         VecRestoreArray(w,&neparray);
    295:       }
    296:     }
    297:     VecRestoreArray(veps,&epsarray);
    298:     EPSSetInitialSpace(ctx->eps,1,&veps);
    299:     VecDestroy(&veps);
    300:     VecDestroy(&w);
    301:     SlepcBasisDestroy_Private(&nep->nini,&nep->IS);
    302:   }

    304:   EPSSetUp(ctx->eps);
    305:   EPSGetDimensions(ctx->eps,NULL,&nep->ncv,&nep->mpd);
    306:   EPSGetTolerances(ctx->eps,NULL,&nep->max_it);
    307:   NEPAllocateSolution(nep,0);
    308:   return 0;
    309: }

    311: /*
    312:    NEPNLEIGSExtract_None - Extracts the first block of the basis
    313:    and normalizes the columns.
    314: */
    315: static PetscErrorCode NEPNLEIGSExtract_None(NEP nep,EPS eps)
    316: {
    317:   PetscInt          i,k,m,d;
    318:   const PetscScalar *px;
    319:   PetscScalar       sigma=nep->target,*b;
    320:   Mat               A;
    321:   Vec               xxr,xxi=NULL,w,t,xx;
    322:   PetscReal         norm;
    323:   NEP_NLEIGS        *ctx=(NEP_NLEIGS*)nep->data;

    325:   d = ctx->nmat-1;
    326:   EPSGetOperators(eps,&A,NULL);
    327:   MatCreateVecs(A,&xxr,NULL);
    328: #if !defined(PETSC_USE_COMPLEX)
    329:   VecDuplicate(xxr,&xxi);
    330: #endif
    331:   w = nep->work[0];
    332:   for (i=0;i<nep->nconv;i++) {
    333:     EPSGetEigenvector(eps,i,xxr,xxi);
    334:     VecGetArrayRead(xxr,&px);
    335:     VecPlaceArray(w,px);
    336:     BVInsertVec(nep->V,i,w);
    337:     BVNormColumn(nep->V,i,NORM_2,&norm);
    338:     BVScaleColumn(nep->V,i,1.0/norm);
    339:     VecResetArray(w);
    340:     VecRestoreArrayRead(xxr,&px);
    341:   }
    342:   if (nep->twosided) {
    343:     PetscMalloc1(ctx->nmat,&b);
    344:     NEPNLEIGSEvalNRTFunct(nep,d,sigma,b);
    345:     m = nep->nloc;
    346:     xx = ctx->w[0];
    347:     w = nep->work[0]; t = nep->work[1];
    348:     for (k=0;k<nep->nconv;k++) {
    349:       EPSGetLeftEigenvector(eps,k,xxr,xxi);
    350:       VecGetArrayRead(xxr,&px);
    351:       VecPlaceArray(xx,px+(d-1)*m);
    352:       VecCopy(xx,w);
    353:       VecScale(w,PetscConj(b[d-1]));
    354:       VecResetArray(xx);
    355:       for (i=0;i<d-1;i++) {
    356:         VecPlaceArray(xx,px+i*m);
    357:         VecAXPY(w,PetscConj(b[i]),xx);
    358:         VecResetArray(xx);
    359:       }
    360:       VecConjugate(w);
    361:       KSPSolveTranspose(ctx->ksp[0],w,t);
    362:       VecConjugate(t);
    363:       BVInsertVec(nep->W,k,t);
    364:       BVNormColumn(nep->W,k,NORM_2,&norm);
    365:       BVScaleColumn(nep->W,k,1.0/norm);
    366:       VecRestoreArrayRead(xxr,&px);
    367:     }
    368:     PetscFree(b);
    369:   }
    370:   VecDestroy(&xxr);
    371: #if !defined(PETSC_USE_COMPLEX)
    372:   VecDestroy(&xxi);
    373: #endif
    374:   return 0;
    375: }

    377: PetscErrorCode NEPSolve_NLEIGS_FullBasis(NEP nep)
    378: {
    379:   NEP_NLEIGS     *ctx = (NEP_NLEIGS*)nep->data;
    380:   PetscInt       i;
    381:   PetscScalar    eigi=0.0;

    383:   EPSSolve(ctx->eps);
    384:   EPSGetConverged(ctx->eps,&nep->nconv);
    385:   EPSGetIterationNumber(ctx->eps,&nep->its);
    386:   EPSGetConvergedReason(ctx->eps,(EPSConvergedReason*)&nep->reason);

    388:   /* recover eigenvalues */
    389:   for (i=0;i<nep->nconv;i++) {
    390:     EPSGetEigenpair(ctx->eps,i,&nep->eigr[i],&eigi,NULL,NULL);
    391: #if !defined(PETSC_USE_COMPLEX)
    393: #endif
    394:   }
    395:   NEPNLEIGSExtract_None(nep,ctx->eps);
    396:   return 0;
    397: }

    399: PetscErrorCode NEPNLEIGSSetEPS_NLEIGS(NEP nep,EPS eps)
    400: {
    401:   NEP_NLEIGS     *ctx=(NEP_NLEIGS*)nep->data;

    403:   PetscObjectReference((PetscObject)eps);
    404:   EPSDestroy(&ctx->eps);
    405:   ctx->eps = eps;
    406:   nep->state = NEP_STATE_INITIAL;
    407:   return 0;
    408: }

    410: /*@
    411:    NEPNLEIGSSetEPS - Associate an eigensolver object (EPS) to the NLEIGS solver.

    413:    Collective on nep

    415:    Input Parameters:
    416: +  nep - nonlinear eigenvalue solver
    417: -  eps - the eigensolver object

    419:    Level: advanced

    421: .seealso: NEPNLEIGSGetEPS()
    422: @*/
    423: PetscErrorCode NEPNLEIGSSetEPS(NEP nep,EPS eps)
    424: {
    428:   PetscTryMethod(nep,"NEPNLEIGSSetEPS_C",(NEP,EPS),(nep,eps));
    429:   return 0;
    430: }

    432: static PetscErrorCode EPSMonitor_NLEIGS(EPS eps,PetscInt its,PetscInt nconv,PetscScalar *eigr,PetscScalar *eigi,PetscReal *errest,PetscInt nest,void *ctx)
    433: {
    434:   NEP            nep = (NEP)ctx;
    435:   PetscInt       i,nv = PetscMin(nest,nep->ncv);

    437:   for (i=0;i<nv;i++) {
    438:     nep->eigr[i]   = eigr[i];
    439:     nep->eigi[i]   = eigi[i];
    440:     nep->errest[i] = errest[i];
    441:   }
    442:   NEPNLEIGSBackTransform((PetscObject)nep,nv,nep->eigr,nep->eigi);
    443:   NEPMonitor(nep,its,nconv,nep->eigr,nep->eigi,nep->errest,nest);
    444:   return 0;
    445: }

    447: PetscErrorCode NEPNLEIGSGetEPS_NLEIGS(NEP nep,EPS *eps)
    448: {
    449:   NEP_NLEIGS     *ctx=(NEP_NLEIGS*)nep->data;

    451:   if (!ctx->eps) {
    452:     EPSCreate(PetscObjectComm((PetscObject)nep),&ctx->eps);
    453:     PetscObjectIncrementTabLevel((PetscObject)ctx->eps,(PetscObject)nep,1);
    454:     EPSSetOptionsPrefix(ctx->eps,((PetscObject)nep)->prefix);
    455:     EPSAppendOptionsPrefix(ctx->eps,"nep_nleigs_");
    456:     PetscObjectSetOptions((PetscObject)ctx->eps,((PetscObject)nep)->options);
    457:     EPSMonitorSet(ctx->eps,EPSMonitor_NLEIGS,nep,NULL);
    458:   }
    459:   *eps = ctx->eps;
    460:   return 0;
    461: }

    463: /*@
    464:    NEPNLEIGSGetEPS - Retrieve the eigensolver object (EPS) associated
    465:    to the nonlinear eigenvalue solver.

    467:    Not Collective

    469:    Input Parameter:
    470: .  nep - nonlinear eigenvalue solver

    472:    Output Parameter:
    473: .  eps - the eigensolver object

    475:    Level: advanced

    477: .seealso: NEPNLEIGSSetEPS()
    478: @*/
    479: PetscErrorCode NEPNLEIGSGetEPS(NEP nep,EPS *eps)
    480: {
    483:   PetscUseMethod(nep,"NEPNLEIGSGetEPS_C",(NEP,EPS*),(nep,eps));
    484:   return 0;
    485: }