Actual source code: nciss.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: "ciss"

     13:    Method: Contour Integral Spectral Slicing

     15:    Algorithm:

     17:        Contour integral based on Sakurai-Sugiura method to construct a
     18:        subspace, with various eigenpair extractions (Rayleigh-Ritz,
     19:        explicit moment).

     21:    Based on code contributed by Y. Maeda, T. Sakurai.

     23:    References:

     25:        [1] J. Asakura, T. Sakurai, H. Tadano, T. Ikegami, K. Kimura, "A
     26:            numerical method for nonlinear eigenvalue problems using contour
     27:            integrals", JSIAM Lett. 1:52-55, 2009.

     29:        [2] S. Yokota and T. Sakurai, "A projection method for nonlinear
     30:            eigenvalue problems using contour integrals", JSIAM Lett.
     31:            5:41-44, 2013.
     32: */

     34: #include <slepc/private/nepimpl.h>
     35: #include <slepc/private/slepccontour.h>

     37: typedef struct _n_nep_ciss_project *NEP_CISS_PROJECT;
     38: typedef struct {
     39:   /* parameters */
     40:   PetscInt          N;             /* number of integration points (32) */
     41:   PetscInt          L;             /* block size (16) */
     42:   PetscInt          M;             /* moment degree (N/4 = 4) */
     43:   PetscReal         delta;         /* threshold of singular value (1e-12) */
     44:   PetscInt          L_max;         /* maximum number of columns of the source matrix V */
     45:   PetscReal         spurious_threshold; /* discard spurious eigenpairs */
     46:   PetscBool         isreal;        /* T(z) is real for real z */
     47:   PetscInt          npart;         /* number of partitions */
     48:   PetscInt          refine_inner;
     49:   PetscInt          refine_blocksize;
     50:   NEPCISSExtraction extraction;
     51:   /* private data */
     52:   SlepcContourData  contour;
     53:   PetscReal         *sigma;        /* threshold for numerical rank */
     54:   PetscScalar       *weight;
     55:   PetscScalar       *omega;
     56:   PetscScalar       *pp;
     57:   BV                V;
     58:   BV                S;
     59:   BV                Y;
     60:   PetscBool         useconj;
     61:   Mat               J;             /* auxiliary matrix when using subcomm */
     62:   BV                pV;
     63:   NEP_CISS_PROJECT  dsctxf;
     64:   PetscObjectId     rgid;
     65:   PetscObjectState  rgstate;
     66: } NEP_CISS;

     68: struct _n_nep_ciss_project {
     69:   NEP  nep;
     70:   BV   Q;
     71: };

     73: static PetscErrorCode NEPContourDSComputeMatrix(DS ds,PetscScalar lambda,PetscBool deriv,DSMatType mat,void *ctx)
     74: {
     75:   NEP_CISS_PROJECT proj = (NEP_CISS_PROJECT)ctx;
     76:   Mat              M,fun;

     78:   PetscFunctionBegin;
     79:   if (!deriv) {
     80:     PetscCall(NEPComputeFunction(proj->nep,lambda,proj->nep->function,proj->nep->function));
     81:     fun = proj->nep->function;
     82:   } else {
     83:     PetscCall(NEPComputeJacobian(proj->nep,lambda,proj->nep->jacobian));
     84:     fun = proj->nep->jacobian;
     85:   }
     86:   PetscCall(DSGetMat(ds,mat,&M));
     87:   PetscCall(BVMatProject(proj->Q,fun,proj->Q,M));
     88:   PetscCall(DSRestoreMat(ds,mat,&M));
     89:   PetscFunctionReturn(PETSC_SUCCESS);
     90: }

     92: static PetscErrorCode NEPComputeFunctionSubcomm(NEP nep,PetscScalar lambda,Mat T,Mat P,PetscBool deriv)
     93: {
     94:   PetscInt       i;
     95:   PetscScalar    alpha;
     96:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;

     98:   PetscFunctionBegin;
     99:   PetscAssert(nep->fui!=NEP_USER_INTERFACE_CALLBACK,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_WRONGSTATE,"Should not arrive here with callbacks");
    100:   PetscCall(MatZeroEntries(T));
    101:   if (!deriv && T != P) PetscCall(MatZeroEntries(P));
    102:   for (i=0;i<nep->nt;i++) {
    103:     if (!deriv) PetscCall(FNEvaluateFunction(nep->f[i],lambda,&alpha));
    104:     else PetscCall(FNEvaluateDerivative(nep->f[i],lambda,&alpha));
    105:     PetscCall(MatAXPY(T,alpha,ctx->contour->pA[i],nep->mstr));
    106:     if (!deriv && T != P) PetscCall(MatAXPY(P,alpha,ctx->contour->pP[i],nep->mstrp));
    107:   }
    108:   PetscFunctionReturn(PETSC_SUCCESS);
    109: }

    111: /*
    112:   Set up KSP solvers for every integration point
    113: */
    114: static PetscErrorCode NEPCISSSetUp(NEP nep,Mat T,Mat P)
    115: {
    116:   NEP_CISS         *ctx = (NEP_CISS*)nep->data;
    117:   SlepcContourData contour;
    118:   PetscInt         i,p_id;
    119:   Mat              Amat,Pmat;

    121:   PetscFunctionBegin;
    122:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(NEPCISSGetKSPs(nep,NULL,NULL));
    123:   contour = ctx->contour;
    124:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)nep),PETSC_ERR_PLIB,"Something went wrong with NEPCISSGetKSPs()");
    125:   for (i=0;i<contour->npoints;i++) {
    126:     p_id = i*contour->subcomm->n + contour->subcomm->color;
    127:     PetscCall(MatDuplicate(T,MAT_DO_NOT_COPY_VALUES,&Amat));
    128:     if (T != P) PetscCall(MatDuplicate(P,MAT_DO_NOT_COPY_VALUES,&Pmat)); else Pmat = Amat;
    129:     if (contour->subcomm->n == 1 || nep->fui==NEP_USER_INTERFACE_CALLBACK) PetscCall(NEPComputeFunction(nep,ctx->omega[p_id],Amat,Pmat));
    130:     else PetscCall(NEPComputeFunctionSubcomm(nep,ctx->omega[p_id],Amat,Pmat,PETSC_FALSE));
    131:     PetscCall(NEP_KSPSetOperators(contour->ksp[i],Amat,Pmat));
    132:     PetscCall(MatDestroy(&Amat));
    133:     if (T != P) PetscCall(MatDestroy(&Pmat));
    134:   }
    135:   PetscFunctionReturn(PETSC_SUCCESS);
    136: }

    138: /*
    139:   Y_i = F(z_i)^{-1}Fp(z_i)V for every integration point, Y=[Y_i] is in the context
    140: */
    141: static PetscErrorCode NEPCISSSolve(NEP nep,Mat dT,BV V,PetscInt L_start,PetscInt L_end)
    142: {
    143:   NEP_CISS         *ctx = (NEP_CISS*)nep->data;
    144:   SlepcContourData contour = ctx->contour;
    145:   PetscInt         i,p_id;
    146:   Mat              MV,BMV=NULL,MC;

    148:   PetscFunctionBegin;
    149:   PetscCall(BVSetActiveColumns(V,L_start,L_end));
    150:   PetscCall(BVGetMat(V,&MV));
    151:   for (i=0;i<contour->npoints;i++) {
    152:     p_id = i*contour->subcomm->n + contour->subcomm->color;
    153:     if (contour->subcomm->n==1 || nep->fui==NEP_USER_INTERFACE_CALLBACK) PetscCall(NEPComputeJacobian(nep,ctx->omega[p_id],dT));
    154:     else PetscCall(NEPComputeFunctionSubcomm(nep,ctx->omega[p_id],dT,NULL,PETSC_TRUE));
    155:     PetscCall(BVSetActiveColumns(ctx->Y,i*ctx->L+L_start,i*ctx->L+L_end));
    156:     PetscCall(BVGetMat(ctx->Y,&MC));
    157:     if (!i) {
    158:       PetscCall(MatProductCreate(dT,MV,NULL,&BMV));
    159:       PetscCall(MatProductSetType(BMV,MATPRODUCT_AB));
    160:       PetscCall(MatProductSetFromOptions(BMV));
    161:       PetscCall(MatProductSymbolic(BMV));
    162:     }
    163:     PetscCall(MatProductNumeric(BMV));
    164:     PetscCall(KSPMatSolve(contour->ksp[i],BMV,MC));
    165:     PetscCall(BVRestoreMat(ctx->Y,&MC));
    166:   }
    167:   PetscCall(MatDestroy(&BMV));
    168:   PetscCall(BVRestoreMat(V,&MV));
    169:   PetscFunctionReturn(PETSC_SUCCESS);
    170: }

    172: static PetscErrorCode NEPSetUp_CISS(NEP nep)
    173: {
    174:   NEP_CISS         *ctx = (NEP_CISS*)nep->data;
    175:   SlepcContourData contour;
    176:   PetscInt         nwork;
    177:   PetscBool        istrivial,isellipse,flg;
    178:   NEP_CISS_PROJECT dsctxf;
    179:   PetscObjectId    id;
    180:   PetscObjectState state;
    181:   Vec              v0;

    183:   PetscFunctionBegin;
    184:   if (nep->ncv==PETSC_DEFAULT) nep->ncv = ctx->L_max*ctx->M;
    185:   else {
    186:     ctx->L_max = nep->ncv/ctx->M;
    187:     if (!ctx->L_max) {
    188:       ctx->L_max = 1;
    189:       nep->ncv = ctx->L_max*ctx->M;
    190:     }
    191:   }
    192:   ctx->L = PetscMin(ctx->L,ctx->L_max);
    193:   if (nep->max_it==PETSC_DEFAULT) nep->max_it = 5;
    194:   if (nep->mpd==PETSC_DEFAULT) nep->mpd = nep->ncv;
    195:   if (!nep->which) nep->which = NEP_ALL;
    196:   PetscCheck(nep->which==NEP_ALL,PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"This solver supports only computing all eigenvalues");
    197:   NEPCheckUnsupported(nep,NEP_FEATURE_STOPPING | NEP_FEATURE_TWOSIDED);

    199:   /* check region */
    200:   PetscCall(RGIsTrivial(nep->rg,&istrivial));
    201:   PetscCheck(!istrivial,PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"CISS requires a nontrivial region, e.g. -rg_type ellipse ...");
    202:   PetscCall(RGGetComplement(nep->rg,&flg));
    203:   PetscCheck(!flg,PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"A region with complement flag set is not allowed");
    204:   PetscCall(PetscObjectTypeCompare((PetscObject)nep->rg,RGELLIPSE,&isellipse));
    205:   PetscCheck(isellipse,PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"Currently only implemented for elliptic regions");

    207:   /* if the region has changed, then reset contour data */
    208:   PetscCall(PetscObjectGetId((PetscObject)nep->rg,&id));
    209:   PetscCall(PetscObjectStateGet((PetscObject)nep->rg,&state));
    210:   if (ctx->rgid && (id != ctx->rgid || state != ctx->rgstate)) {
    211:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
    212:     PetscCall(PetscInfo(nep,"Resetting the contour data structure due to a change of region\n"));
    213:     ctx->rgid = id; ctx->rgstate = state;
    214:   }

    216:   /* create contour data structure */
    217:   if (!ctx->contour) {
    218:     PetscCall(RGCanUseConjugates(nep->rg,ctx->isreal,&ctx->useconj));
    219:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)nep,&ctx->contour));
    220:   }

    222:   PetscCall(NEPAllocateSolution(nep,0));
    223:   if (ctx->weight) PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
    224:   PetscCall(PetscMalloc4(ctx->N,&ctx->weight,ctx->N,&ctx->omega,ctx->N,&ctx->pp,ctx->L_max*ctx->M,&ctx->sigma));

    226:   /* allocate basis vectors */
    227:   PetscCall(BVDestroy(&ctx->S));
    228:   PetscCall(BVDuplicateResize(nep->V,ctx->L*ctx->M,&ctx->S));
    229:   PetscCall(BVDestroy(&ctx->V));
    230:   PetscCall(BVDuplicateResize(nep->V,ctx->L,&ctx->V));

    232:   contour = ctx->contour;
    233:   if (contour->subcomm && contour->subcomm->n != 1 && nep->fui==NEP_USER_INTERFACE_CALLBACK) {
    234:     PetscCall(NEPComputeFunction(nep,0,nep->function,nep->function_pre));
    235:     PetscCall(SlepcContourRedundantMat(contour,1,&nep->function,(nep->function!=nep->function_pre)?&nep->function_pre:NULL));
    236:   } else PetscCall(SlepcContourRedundantMat(contour,nep->nt,nep->A,nep->P));
    237:   if (contour->pA) {
    238:     if (!ctx->J) PetscCall(MatDuplicate(contour->pA[0],MAT_DO_NOT_COPY_VALUES,&ctx->J));
    239:     PetscCall(BVGetColumn(ctx->V,0,&v0));
    240:     PetscCall(SlepcContourScatterCreate(contour,v0));
    241:     PetscCall(BVRestoreColumn(ctx->V,0,&v0));
    242:     PetscCall(BVDestroy(&ctx->pV));
    243:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->pV));
    244:     PetscCall(BVSetSizesFromVec(ctx->pV,contour->xsub,nep->n));
    245:     PetscCall(BVSetFromOptions(ctx->pV));
    246:     PetscCall(BVResize(ctx->pV,ctx->L,PETSC_FALSE));
    247:   }

    249:   PetscCall(BVDestroy(&ctx->Y));
    250:   if (contour->pA) {
    251:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->Y));
    252:     PetscCall(BVSetSizesFromVec(ctx->Y,contour->xsub,nep->n));
    253:     PetscCall(BVSetFromOptions(ctx->Y));
    254:     PetscCall(BVResize(ctx->Y,contour->npoints*ctx->L,PETSC_FALSE));
    255:   } else PetscCall(BVDuplicateResize(nep->V,contour->npoints*ctx->L,&ctx->Y));

    257:   if (ctx->extraction == NEP_CISS_EXTRACTION_RITZ)  {
    258:     PetscCall(DSSetMethod(nep->ds,1));
    259:     PetscCall(DSNEPSetRG(nep->ds,nep->rg));
    260:     if (nep->fui==NEP_USER_INTERFACE_SPLIT) PetscCall(DSNEPSetFN(nep->ds,nep->nt,nep->f));
    261:     else {
    262:       PetscCall(PetscNew(&dsctxf));
    263:       PetscCall(DSNEPSetComputeMatrixFunction(nep->ds,NEPContourDSComputeMatrix,dsctxf));
    264:       dsctxf->nep = nep;
    265:       ctx->dsctxf = dsctxf;
    266:     }
    267:   }
    268:   PetscCall(DSAllocate(nep->ds,nep->ncv));
    269:   nwork = (nep->fui==NEP_USER_INTERFACE_SPLIT)? 2: 1;
    270:   PetscCall(NEPSetWorkVecs(nep,nwork));
    271:   PetscFunctionReturn(PETSC_SUCCESS);
    272: }

    274: static PetscErrorCode NEPSolve_CISS(NEP nep)
    275: {
    276:   NEP_CISS         *ctx = (NEP_CISS*)nep->data;
    277:   SlepcContourData contour = ctx->contour;
    278:   Mat              X,M,E,T,P,J;
    279:   BV               V;
    280:   PetscInt         i,j,ld,L_add=0,nv=0,L_base=ctx->L,inner,*inside;
    281:   PetscScalar      *Mu,*H0,*H1,*rr,*temp,center;
    282:   PetscReal        error,max_error,radius,rgscale,est_eig,eta;
    283:   PetscBool        isellipse,*fl1;
    284:   Vec              si;
    285:   SlepcSC          sc;
    286:   PetscRandom      rand;

    288:   PetscFunctionBegin;
    289:   PetscCall(DSGetSlepcSC(nep->ds,&sc));
    290:   sc->comparison    = SlepcCompareLargestMagnitude;
    291:   sc->comparisonctx = NULL;
    292:   sc->map           = NULL;
    293:   sc->mapobj        = NULL;
    294:   PetscCall(DSGetLeadingDimension(nep->ds,&ld));
    295:   PetscCall(RGComputeQuadrature(nep->rg,RG_QUADRULE_TRAPEZOIDAL,ctx->N,ctx->omega,ctx->pp,ctx->weight));
    296:   if (contour->pA) {
    297:     T = contour->pA[0];
    298:     P = ((nep->fui==NEP_USER_INTERFACE_SPLIT && nep->P) || (nep->fui==NEP_USER_INTERFACE_CALLBACK && contour->pP))? contour->pP[0]: T;
    299:   } else {
    300:     T = nep->function;
    301:     P = nep->function_pre? nep->function_pre: nep->function;
    302:   }
    303:   PetscCall(NEPCISSSetUp(nep,T,P));
    304:   PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    305:   PetscCall(BVSetRandomSign(ctx->V));
    306:   PetscCall(BVGetRandomContext(ctx->V,&rand));
    307:   if (contour->pA) {
    308:     J = ctx->J;
    309:     V = ctx->pV;
    310:     PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    311:   } else {
    312:     J = nep->jacobian;
    313:     V = ctx->V;
    314:   }
    315:   PetscCall(NEPCISSSolve(nep,J,V,0,ctx->L));
    316:   PetscCall(PetscObjectTypeCompare((PetscObject)nep->rg,RGELLIPSE,&isellipse));
    317:   if (isellipse) {
    318:     PetscCall(BVTraceQuadrature(ctx->Y,ctx->V,ctx->L,ctx->L,ctx->weight,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj,&est_eig));
    319:     PetscCall(PetscInfo(nep,"Estimated eigenvalue count: %f\n",(double)est_eig));
    320:     eta = PetscPowReal(10.0,-PetscLog10Real(nep->tol)/ctx->N);
    321:     L_add = PetscMax(0,(PetscInt)PetscCeilReal((est_eig*eta)/ctx->M)-ctx->L);
    322:     if (L_add>ctx->L_max-ctx->L) {
    323:       PetscCall(PetscInfo(nep,"Number of eigenvalues inside the contour path may be too large\n"));
    324:       L_add = ctx->L_max-ctx->L;
    325:     }
    326:   }
    327:   /* Updates L after estimate the number of eigenvalue */
    328:   if (L_add>0) {
    329:     PetscCall(PetscInfo(nep,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by Estimate #Eig\n",ctx->L,ctx->L+L_add));
    330:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
    331:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
    332:     PetscCall(BVSetRandomSign(ctx->V));
    333:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    334:     ctx->L += L_add;
    335:     PetscCall(NEPCISSSolve(nep,J,V,ctx->L-L_add,ctx->L));
    336:   }

    338:   PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
    339:   for (i=0;i<ctx->refine_blocksize;i++) {
    340:     PetscCall(BVDotQuadrature(ctx->Y,V,Mu,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->subcomm,contour->npoints,ctx->useconj));
    341:     PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    342:     PetscCall(PetscLogEventBegin(NEP_CISS_SVD,nep,0,0,0));
    343:     PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
    344:     PetscCall(PetscLogEventEnd(NEP_CISS_SVD,nep,0,0,0));
    345:     if (ctx->sigma[0]<=ctx->delta || nv < ctx->L*ctx->M || ctx->L == ctx->L_max) break;
    346:     L_add = L_base;
    347:     if (ctx->L+L_add>ctx->L_max) L_add = ctx->L_max-ctx->L;
    348:     PetscCall(PetscInfo(nep,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by SVD(H0)\n",ctx->L,ctx->L+L_add));
    349:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
    350:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
    351:     PetscCall(BVSetRandomSign(ctx->V));
    352:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    353:     ctx->L += L_add;
    354:     PetscCall(NEPCISSSolve(nep,J,V,ctx->L-L_add,ctx->L));
    355:     if (L_add) {
    356:       PetscCall(PetscFree2(Mu,H0));
    357:       PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
    358:     }
    359:   }

    361:   PetscCall(RGGetScale(nep->rg,&rgscale));
    362:   PetscCall(RGEllipseGetParameters(nep->rg,&center,&radius,NULL));

    364:   if (ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) PetscCall(PetscMalloc1(ctx->L*ctx->M*ctx->L*ctx->M,&H1));

    366:   while (nep->reason == NEP_CONVERGED_ITERATING) {
    367:     nep->its++;
    368:     for (inner=0;inner<=ctx->refine_inner;inner++) {
    369:       if (ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) {
    370:         PetscCall(BVDotQuadrature(ctx->Y,V,Mu,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->subcomm,contour->npoints,ctx->useconj));
    371:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    372:         PetscCall(PetscLogEventBegin(NEP_CISS_SVD,nep,0,0,0));
    373:         PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
    374:         PetscCall(PetscLogEventEnd(NEP_CISS_SVD,nep,0,0,0));
    375:       } else {
    376:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    377:         /* compute SVD of S */
    378:         PetscCall(BVSVDAndRank(ctx->S,ctx->M,ctx->L,ctx->delta,(ctx->extraction==NEP_CISS_EXTRACTION_CAA)?BV_SVD_METHOD_QR_CAA:BV_SVD_METHOD_QR,H0,ctx->sigma,&nv));
    379:       }
    380:       PetscCall(PetscInfo(nep,"Estimated rank: nv = %" PetscInt_FMT "\n",nv));
    381:       if (ctx->sigma[0]>ctx->delta && nv==ctx->L*ctx->M && inner!=ctx->refine_inner) {
    382:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    383:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
    384:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    385:         PetscCall(BVCopy(ctx->S,ctx->V));
    386:         if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    387:         PetscCall(NEPCISSSolve(nep,J,V,0,ctx->L));
    388:       } else break;
    389:     }
    390:     nep->nconv = 0;
    391:     if (nv == 0) { nep->reason = NEP_CONVERGED_TOL; break; }
    392:     else {
    393:       /* Extracting eigenpairs */
    394:       PetscCall(DSSetDimensions(nep->ds,nv,0,0));
    395:       PetscCall(DSSetState(nep->ds,DS_STATE_RAW));
    396:       if (ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) {
    397:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    398:         PetscCall(CISS_BlockHankel(Mu,1,ctx->L,ctx->M,H1));
    399:         PetscCall(DSGetArray(nep->ds,DS_MAT_A,&temp));
    400:         for (j=0;j<nv;j++)
    401:           for (i=0;i<nv;i++)
    402:             temp[i+j*ld] = H1[i+j*ctx->L*ctx->M];
    403:         PetscCall(DSRestoreArray(nep->ds,DS_MAT_A,&temp));
    404:         PetscCall(DSGetArray(nep->ds,DS_MAT_B,&temp));
    405:         for (j=0;j<nv;j++)
    406:           for (i=0;i<nv;i++)
    407:             temp[i+j*ld] = H0[i+j*ctx->L*ctx->M];
    408:         PetscCall(DSRestoreArray(nep->ds,DS_MAT_B,&temp));
    409:       } else if (ctx->extraction == NEP_CISS_EXTRACTION_CAA) {
    410:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    411:         PetscCall(DSGetArray(nep->ds,DS_MAT_A,&temp));
    412:         for (i=0;i<nv;i++) PetscCall(PetscArraycpy(temp+i*ld,H0+i*nv,nv));
    413:         PetscCall(DSRestoreArray(nep->ds,DS_MAT_A,&temp));
    414:       } else {
    415:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    416:         if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
    417:           for (i=0;i<nep->nt;i++) {
    418:             PetscCall(DSGetMat(nep->ds,DSMatExtra[i],&E));
    419:             PetscCall(BVMatProject(ctx->S,nep->A[i],ctx->S,E));
    420:             PetscCall(DSRestoreMat(nep->ds,DSMatExtra[i],&E));
    421:           }
    422:         } else { ctx->dsctxf->Q = ctx->S; }
    423:       }
    424:       PetscCall(DSSolve(nep->ds,nep->eigr,nep->eigi));
    425:       PetscCall(DSSynchronize(nep->ds,nep->eigr,nep->eigi));
    426:       PetscCall(DSGetDimensions(nep->ds,NULL,NULL,NULL,&nv));
    427:       if (ctx->extraction == NEP_CISS_EXTRACTION_CAA || ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) {
    428:         for (i=0;i<nv;i++) {
    429:           nep->eigr[i] = (nep->eigr[i]*radius+center)*rgscale;
    430:         }
    431:       }
    432:       PetscCall(PetscMalloc3(nv,&fl1,nv,&inside,nv,&rr));
    433:       PetscCall(DSVectors(nep->ds,DS_MAT_X,NULL,NULL));
    434:       PetscCall(DSGetMat(nep->ds,DS_MAT_X,&X));
    435:       PetscCall(SlepcCISS_isGhost(X,nv,ctx->sigma,ctx->spurious_threshold,fl1));
    436:       PetscCall(DSRestoreMat(nep->ds,DS_MAT_X,&X));
    437:       PetscCall(RGCheckInside(nep->rg,nv,nep->eigr,nep->eigi,inside));
    438:       for (i=0;i<nv;i++) {
    439:         if (fl1[i] && inside[i]>=0) {
    440:           rr[i] = 1.0;
    441:           nep->nconv++;
    442:         } else rr[i] = 0.0;
    443:       }
    444:       PetscCall(DSSort(nep->ds,nep->eigr,nep->eigi,rr,NULL,&nep->nconv));
    445:       PetscCall(DSSynchronize(nep->ds,nep->eigr,nep->eigi));
    446:       if (ctx->extraction == NEP_CISS_EXTRACTION_CAA || ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) {
    447:         for (i=0;i<nv;i++) nep->eigr[i] = (nep->eigr[i]*radius+center)*rgscale;
    448:       }
    449:       PetscCall(PetscFree3(fl1,inside,rr));
    450:       PetscCall(BVSetActiveColumns(nep->V,0,nv));
    451:       PetscCall(DSVectors(nep->ds,DS_MAT_X,NULL,NULL));
    452:       if (ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) {
    453:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    454:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    455:         PetscCall(BVCopy(ctx->S,nep->V));
    456:         PetscCall(DSGetMat(nep->ds,DS_MAT_X,&X));
    457:         PetscCall(BVMultInPlace(ctx->S,X,0,nep->nconv));
    458:         PetscCall(BVMultInPlace(nep->V,X,0,nep->nconv));
    459:         PetscCall(DSRestoreMat(nep->ds,DS_MAT_X,&X));
    460:       } else {
    461:         PetscCall(DSGetMat(nep->ds,DS_MAT_X,&X));
    462:         PetscCall(BVMultInPlace(ctx->S,X,0,nep->nconv));
    463:         PetscCall(DSRestoreMat(nep->ds,DS_MAT_X,&X));
    464:         PetscCall(BVSetActiveColumns(ctx->S,0,nep->nconv));
    465:         PetscCall(BVCopy(ctx->S,nep->V));
    466:       }
    467:       max_error = 0.0;
    468:       for (i=0;i<nep->nconv;i++) {
    469:         PetscCall(BVGetColumn(nep->V,i,&si));
    470:         PetscCall(VecNormalize(si,NULL));
    471:         PetscCall(NEPComputeResidualNorm_Private(nep,PETSC_FALSE,nep->eigr[i],si,nep->work,&error));
    472:         PetscCall((*nep->converged)(nep,nep->eigr[i],0,error,&error,nep->convergedctx));
    473:         PetscCall(BVRestoreColumn(nep->V,i,&si));
    474:         max_error = PetscMax(max_error,error);
    475:       }
    476:       if (max_error <= nep->tol) nep->reason = NEP_CONVERGED_TOL;
    477:       else if (nep->its > nep->max_it) nep->reason = NEP_DIVERGED_ITS;
    478:       else {
    479:         if (nep->nconv > ctx->L) nv = nep->nconv;
    480:         else if (ctx->L > nv) nv = ctx->L;
    481:         nv = PetscMin(nv,ctx->L*ctx->M);
    482:         PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,nv,ctx->L,NULL,&M));
    483:         PetscCall(MatSetRandom(M,rand));
    484:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    485:         PetscCall(BVMultInPlace(ctx->S,M,0,ctx->L));
    486:         PetscCall(MatDestroy(&M));
    487:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
    488:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    489:         PetscCall(BVCopy(ctx->S,ctx->V));
    490:         if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    491:         PetscCall(NEPCISSSolve(nep,J,V,0,ctx->L));
    492:       }
    493:     }
    494:   }
    495:   PetscCall(PetscFree2(Mu,H0));
    496:   if (ctx->extraction == NEP_CISS_EXTRACTION_HANKEL) PetscCall(PetscFree(H1));
    497:   PetscFunctionReturn(PETSC_SUCCESS);
    498: }

    500: static PetscErrorCode NEPCISSSetSizes_CISS(NEP nep,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
    501: {
    502:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;
    503:   PetscInt       oN,oL,oM,oLmax,onpart;
    504:   PetscMPIInt    size;

    506:   PetscFunctionBegin;
    507:   oN = ctx->N;
    508:   if (ip == PETSC_DECIDE || ip == PETSC_DEFAULT) {
    509:     if (ctx->N!=32) { ctx->N =32; ctx->M = ctx->N/4; }
    510:   } else {
    511:     PetscCheck(ip>0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be > 0");
    512:     PetscCheck(ip%2==0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be an even number");
    513:     if (ctx->N!=ip) { ctx->N = ip; ctx->M = ctx->N/4; }
    514:   }
    515:   oL = ctx->L;
    516:   if (bs == PETSC_DECIDE || bs == PETSC_DEFAULT) {
    517:     ctx->L = 16;
    518:   } else {
    519:     PetscCheck(bs>0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The bs argument must be > 0");
    520:     ctx->L = bs;
    521:   }
    522:   oM = ctx->M;
    523:   if (ms == PETSC_DECIDE || ms == PETSC_DEFAULT) {
    524:     ctx->M = ctx->N/4;
    525:   } else {
    526:     PetscCheck(ms>0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be > 0");
    527:     PetscCheck(ms<=ctx->N,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be less than or equal to the number of integration points");
    528:     ctx->M = PetscMax(ms,2);
    529:   }
    530:   onpart = ctx->npart;
    531:   if (npart == PETSC_DECIDE || npart == PETSC_DEFAULT) {
    532:     ctx->npart = 1;
    533:   } else {
    534:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)nep),&size));
    535:     PetscCheck(npart>0 && npart<=size,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of npart");
    536:     ctx->npart = npart;
    537:   }
    538:   oLmax = ctx->L_max;
    539:   if (bsmax == PETSC_DECIDE || bsmax == PETSC_DEFAULT) {
    540:     ctx->L_max = 64;
    541:   } else {
    542:     PetscCheck(bsmax>0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The bsmax argument must be > 0");
    543:     ctx->L_max = PetscMax(bsmax,ctx->L);
    544:   }
    545:   if (onpart != ctx->npart || oN != ctx->N || realmats != ctx->isreal) {
    546:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
    547:     PetscCall(PetscInfo(nep,"Resetting the contour data structure due to a change of parameters\n"));
    548:     nep->state = NEP_STATE_INITIAL;
    549:   }
    550:   ctx->isreal = realmats;
    551:   if (oL != ctx->L || oM != ctx->M || oLmax != ctx->L_max) nep->state = NEP_STATE_INITIAL;
    552:   PetscFunctionReturn(PETSC_SUCCESS);
    553: }

    555: /*@
    556:    NEPCISSSetSizes - Sets the values of various size parameters in the CISS solver.

    558:    Logically Collective

    560:    Input Parameters:
    561: +  nep   - the nonlinear eigensolver context
    562: .  ip    - number of integration points
    563: .  bs    - block size
    564: .  ms    - moment size
    565: .  npart - number of partitions when splitting the communicator
    566: .  bsmax - max block size
    567: -  realmats - T(z) is real for real z

    569:    Options Database Keys:
    570: +  -nep_ciss_integration_points - Sets the number of integration points
    571: .  -nep_ciss_blocksize - Sets the block size
    572: .  -nep_ciss_moments - Sets the moment size
    573: .  -nep_ciss_partitions - Sets the number of partitions
    574: .  -nep_ciss_maxblocksize - Sets the maximum block size
    575: -  -nep_ciss_realmats - T(z) is real for real z

    577:    Notes:
    578:    The default number of partitions is 1. This means the internal KSP object is shared
    579:    among all processes of the NEP communicator. Otherwise, the communicator is split
    580:    into npart communicators, so that npart KSP solves proceed simultaneously.

    582:    The realmats flag can be set to true when T(.) is guaranteed to be real
    583:    when the argument is a real value, for example, when all matrices in
    584:    the split form are real. When set to true, the solver avoids some computations.

    586:    Level: advanced

    588: .seealso: NEPCISSGetSizes()
    589: @*/
    590: PetscErrorCode NEPCISSSetSizes(NEP nep,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
    591: {
    592:   PetscFunctionBegin;
    600:   PetscTryMethod(nep,"NEPCISSSetSizes_C",(NEP,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool),(nep,ip,bs,ms,npart,bsmax,realmats));
    601:   PetscFunctionReturn(PETSC_SUCCESS);
    602: }

    604: static PetscErrorCode NEPCISSGetSizes_CISS(NEP nep,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
    605: {
    606:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    608:   PetscFunctionBegin;
    609:   if (ip) *ip = ctx->N;
    610:   if (bs) *bs = ctx->L;
    611:   if (ms) *ms = ctx->M;
    612:   if (npart) *npart = ctx->npart;
    613:   if (bsmax) *bsmax = ctx->L_max;
    614:   if (realmats) *realmats = ctx->isreal;
    615:   PetscFunctionReturn(PETSC_SUCCESS);
    616: }

    618: /*@
    619:    NEPCISSGetSizes - Gets the values of various size parameters in the CISS solver.

    621:    Not Collective

    623:    Input Parameter:
    624: .  nep - the nonlinear eigensolver context

    626:    Output Parameters:
    627: +  ip    - number of integration points
    628: .  bs    - block size
    629: .  ms    - moment size
    630: .  npart - number of partitions when splitting the communicator
    631: .  bsmax - max block size
    632: -  realmats - T(z) is real for real z

    634:    Level: advanced

    636: .seealso: NEPCISSSetSizes()
    637: @*/
    638: PetscErrorCode NEPCISSGetSizes(NEP nep,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
    639: {
    640:   PetscFunctionBegin;
    642:   PetscUseMethod(nep,"NEPCISSGetSizes_C",(NEP,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscBool*),(nep,ip,bs,ms,npart,bsmax,realmats));
    643:   PetscFunctionReturn(PETSC_SUCCESS);
    644: }

    646: static PetscErrorCode NEPCISSSetThreshold_CISS(NEP nep,PetscReal delta,PetscReal spur)
    647: {
    648:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    650:   PetscFunctionBegin;
    651:   if (delta == (PetscReal)PETSC_DEFAULT) {
    652:     ctx->delta = SLEPC_DEFAULT_TOL*1e-4;
    653:   } else {
    654:     PetscCheck(delta>0.0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The delta argument must be > 0.0");
    655:     ctx->delta = delta;
    656:   }
    657:   if (spur == (PetscReal)PETSC_DEFAULT) {
    658:     ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
    659:   } else {
    660:     PetscCheck(spur>0.0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The spurious threshold argument must be > 0.0");
    661:     ctx->spurious_threshold = spur;
    662:   }
    663:   PetscFunctionReturn(PETSC_SUCCESS);
    664: }

    666: /*@
    667:    NEPCISSSetThreshold - Sets the values of various threshold parameters in
    668:    the CISS solver.

    670:    Logically Collective

    672:    Input Parameters:
    673: +  nep   - the nonlinear eigensolver context
    674: .  delta - threshold for numerical rank
    675: -  spur  - spurious threshold (to discard spurious eigenpairs)

    677:    Options Database Keys:
    678: +  -nep_ciss_delta - Sets the delta
    679: -  -nep_ciss_spurious_threshold - Sets the spurious threshold

    681:    Level: advanced

    683: .seealso: NEPCISSGetThreshold()
    684: @*/
    685: PetscErrorCode NEPCISSSetThreshold(NEP nep,PetscReal delta,PetscReal spur)
    686: {
    687:   PetscFunctionBegin;
    691:   PetscTryMethod(nep,"NEPCISSSetThreshold_C",(NEP,PetscReal,PetscReal),(nep,delta,spur));
    692:   PetscFunctionReturn(PETSC_SUCCESS);
    693: }

    695: static PetscErrorCode NEPCISSGetThreshold_CISS(NEP nep,PetscReal *delta,PetscReal *spur)
    696: {
    697:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    699:   PetscFunctionBegin;
    700:   if (delta) *delta = ctx->delta;
    701:   if (spur)  *spur = ctx->spurious_threshold;
    702:   PetscFunctionReturn(PETSC_SUCCESS);
    703: }

    705: /*@
    706:    NEPCISSGetThreshold - Gets the values of various threshold parameters in
    707:    the CISS solver.

    709:    Not Collective

    711:    Input Parameter:
    712: .  nep - the nonlinear eigensolver context

    714:    Output Parameters:
    715: +  delta - threshold for numerical rank
    716: -  spur  - spurious threshold (to discard spurious eigenpairs)

    718:    Level: advanced

    720: .seealso: NEPCISSSetThreshold()
    721: @*/
    722: PetscErrorCode NEPCISSGetThreshold(NEP nep,PetscReal *delta,PetscReal *spur)
    723: {
    724:   PetscFunctionBegin;
    726:   PetscUseMethod(nep,"NEPCISSGetThreshold_C",(NEP,PetscReal*,PetscReal*),(nep,delta,spur));
    727:   PetscFunctionReturn(PETSC_SUCCESS);
    728: }

    730: static PetscErrorCode NEPCISSSetRefinement_CISS(NEP nep,PetscInt inner,PetscInt blsize)
    731: {
    732:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    734:   PetscFunctionBegin;
    735:   if (inner == PETSC_DEFAULT) {
    736:     ctx->refine_inner = 0;
    737:   } else {
    738:     PetscCheck(inner>=0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The refine inner argument must be >= 0");
    739:     ctx->refine_inner = inner;
    740:   }
    741:   if (blsize == PETSC_DEFAULT) {
    742:     ctx->refine_blocksize = 0;
    743:   } else {
    744:     PetscCheck(blsize>=0,PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The refine blocksize argument must be >= 0");
    745:     ctx->refine_blocksize = blsize;
    746:   }
    747:   PetscFunctionReturn(PETSC_SUCCESS);
    748: }

    750: /*@
    751:    NEPCISSSetRefinement - Sets the values of various refinement parameters
    752:    in the CISS solver.

    754:    Logically Collective

    756:    Input Parameters:
    757: +  nep    - the nonlinear eigensolver context
    758: .  inner  - number of iterative refinement iterations (inner loop)
    759: -  blsize - number of iterative refinement iterations (blocksize loop)

    761:    Options Database Keys:
    762: +  -nep_ciss_refine_inner - Sets number of inner iterations
    763: -  -nep_ciss_refine_blocksize - Sets number of blocksize iterations

    765:    Level: advanced

    767: .seealso: NEPCISSGetRefinement()
    768: @*/
    769: PetscErrorCode NEPCISSSetRefinement(NEP nep,PetscInt inner,PetscInt blsize)
    770: {
    771:   PetscFunctionBegin;
    775:   PetscTryMethod(nep,"NEPCISSSetRefinement_C",(NEP,PetscInt,PetscInt),(nep,inner,blsize));
    776:   PetscFunctionReturn(PETSC_SUCCESS);
    777: }

    779: static PetscErrorCode NEPCISSGetRefinement_CISS(NEP nep,PetscInt *inner,PetscInt *blsize)
    780: {
    781:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    783:   PetscFunctionBegin;
    784:   if (inner)  *inner = ctx->refine_inner;
    785:   if (blsize) *blsize = ctx->refine_blocksize;
    786:   PetscFunctionReturn(PETSC_SUCCESS);
    787: }

    789: /*@
    790:    NEPCISSGetRefinement - Gets the values of various refinement parameters
    791:    in the CISS solver.

    793:    Not Collective

    795:    Input Parameter:
    796: .  nep - the nonlinear eigensolver context

    798:    Output Parameters:
    799: +  inner  - number of iterative refinement iterations (inner loop)
    800: -  blsize - number of iterative refinement iterations (blocksize loop)

    802:    Level: advanced

    804: .seealso: NEPCISSSetRefinement()
    805: @*/
    806: PetscErrorCode NEPCISSGetRefinement(NEP nep, PetscInt *inner, PetscInt *blsize)
    807: {
    808:   PetscFunctionBegin;
    810:   PetscUseMethod(nep,"NEPCISSGetRefinement_C",(NEP,PetscInt*,PetscInt*),(nep,inner,blsize));
    811:   PetscFunctionReturn(PETSC_SUCCESS);
    812: }

    814: static PetscErrorCode NEPCISSSetExtraction_CISS(NEP nep,NEPCISSExtraction extraction)
    815: {
    816:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    818:   PetscFunctionBegin;
    819:   if (ctx->extraction != extraction) {
    820:     ctx->extraction = extraction;
    821:     nep->state      = NEP_STATE_INITIAL;
    822:   }
    823:   PetscFunctionReturn(PETSC_SUCCESS);
    824: }

    826: /*@
    827:    NEPCISSSetExtraction - Sets the extraction technique used in the CISS solver.

    829:    Logically Collective

    831:    Input Parameters:
    832: +  nep        - the nonlinear eigensolver context
    833: -  extraction - the extraction technique

    835:    Options Database Key:
    836: .  -nep_ciss_extraction - Sets the extraction technique (either 'ritz', 'hankel' or 'caa')

    838:    Notes:
    839:    By default, the Rayleigh-Ritz extraction is used (NEP_CISS_EXTRACTION_RITZ).

    841:    If the 'hankel' or the 'caa' option is specified (NEP_CISS_EXTRACTION_HANKEL or
    842:    NEP_CISS_EXTRACTION_CAA), then the Block Hankel method, or the Communication-avoiding
    843:    Arnoldi method, respectively, is used for extracting eigenpairs.

    845:    Level: advanced

    847: .seealso: NEPCISSGetExtraction(), NEPCISSExtraction
    848: @*/
    849: PetscErrorCode NEPCISSSetExtraction(NEP nep,NEPCISSExtraction extraction)
    850: {
    851:   PetscFunctionBegin;
    854:   PetscTryMethod(nep,"NEPCISSSetExtraction_C",(NEP,NEPCISSExtraction),(nep,extraction));
    855:   PetscFunctionReturn(PETSC_SUCCESS);
    856: }

    858: static PetscErrorCode NEPCISSGetExtraction_CISS(NEP nep,NEPCISSExtraction *extraction)
    859: {
    860:   NEP_CISS *ctx = (NEP_CISS*)nep->data;

    862:   PetscFunctionBegin;
    863:   *extraction = ctx->extraction;
    864:   PetscFunctionReturn(PETSC_SUCCESS);
    865: }

    867: /*@
    868:    NEPCISSGetExtraction - Gets the extraction technique used in the CISS solver.

    870:    Not Collective

    872:    Input Parameter:
    873: .  nep - the nonlinear eigensolver context

    875:    Output Parameters:
    876: .  extraction - extraction technique

    878:    Level: advanced

    880: .seealso: NEPCISSSetExtraction() NEPCISSExtraction
    881: @*/
    882: PetscErrorCode NEPCISSGetExtraction(NEP nep,NEPCISSExtraction *extraction)
    883: {
    884:   PetscFunctionBegin;
    886:   PetscAssertPointer(extraction,2);
    887:   PetscUseMethod(nep,"NEPCISSGetExtraction_C",(NEP,NEPCISSExtraction*),(nep,extraction));
    888:   PetscFunctionReturn(PETSC_SUCCESS);
    889: }

    891: static PetscErrorCode NEPCISSGetKSPs_CISS(NEP nep,PetscInt *nsolve,KSP **ksp)
    892: {
    893:   NEP_CISS         *ctx = (NEP_CISS*)nep->data;
    894:   SlepcContourData contour;
    895:   PetscInt         i;
    896:   PC               pc;
    897:   MPI_Comm         child;

    899:   PetscFunctionBegin;
    900:   if (!ctx->contour) {  /* initialize contour data structure first */
    901:     PetscCall(RGCanUseConjugates(nep->rg,ctx->isreal,&ctx->useconj));
    902:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)nep,&ctx->contour));
    903:   }
    904:   contour = ctx->contour;
    905:   if (!contour->ksp) {
    906:     PetscCall(PetscMalloc1(contour->npoints,&contour->ksp));
    907:     PetscCall(PetscSubcommGetChild(contour->subcomm,&child));
    908:     for (i=0;i<contour->npoints;i++) {
    909:       PetscCall(KSPCreate(child,&contour->ksp[i]));
    910:       PetscCall(PetscObjectIncrementTabLevel((PetscObject)contour->ksp[i],(PetscObject)nep,1));
    911:       PetscCall(KSPSetOptionsPrefix(contour->ksp[i],((PetscObject)nep)->prefix));
    912:       PetscCall(KSPAppendOptionsPrefix(contour->ksp[i],"nep_ciss_"));
    913:       PetscCall(PetscObjectSetOptions((PetscObject)contour->ksp[i],((PetscObject)nep)->options));
    914:       PetscCall(KSPSetErrorIfNotConverged(contour->ksp[i],PETSC_TRUE));
    915:       PetscCall(KSPSetTolerances(contour->ksp[i],SlepcDefaultTol(nep->tol),PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT));
    916:       PetscCall(KSPGetPC(contour->ksp[i],&pc));
    917:       if ((nep->fui==NEP_USER_INTERFACE_SPLIT && nep->P) || (nep->fui==NEP_USER_INTERFACE_CALLBACK && nep->function_pre!=nep->function)) {
    918:         PetscCall(KSPSetType(contour->ksp[i],KSPBCGS));
    919:         PetscCall(PCSetType(pc,PCBJACOBI));
    920:       } else {
    921:         PetscCall(KSPSetType(contour->ksp[i],KSPPREONLY));
    922:         PetscCall(PCSetType(pc,PCLU));
    923:       }
    924:     }
    925:   }
    926:   if (nsolve) *nsolve = contour->npoints;
    927:   if (ksp)    *ksp    = contour->ksp;
    928:   PetscFunctionReturn(PETSC_SUCCESS);
    929: }

    931: /*@C
    932:    NEPCISSGetKSPs - Retrieve the array of linear solver objects associated with
    933:    the CISS solver.

    935:    Collective

    937:    Input Parameter:
    938: .  nep - nonlinear eigenvalue solver

    940:    Output Parameters:
    941: +  nsolve - number of solver objects
    942: -  ksp - array of linear solver object

    944:    Notes:
    945:    The number of KSP solvers is equal to the number of integration points divided by
    946:    the number of partitions. This value is halved in the case of real matrices with
    947:    a region centered at the real axis.

    949:    Level: advanced

    951: .seealso: NEPCISSSetSizes()
    952: @*/
    953: PetscErrorCode NEPCISSGetKSPs(NEP nep,PetscInt *nsolve,KSP **ksp)
    954: {
    955:   PetscFunctionBegin;
    957:   PetscUseMethod(nep,"NEPCISSGetKSPs_C",(NEP,PetscInt*,KSP**),(nep,nsolve,ksp));
    958:   PetscFunctionReturn(PETSC_SUCCESS);
    959: }

    961: static PetscErrorCode NEPReset_CISS(NEP nep)
    962: {
    963:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;

    965:   PetscFunctionBegin;
    966:   PetscCall(BVDestroy(&ctx->S));
    967:   PetscCall(BVDestroy(&ctx->V));
    968:   PetscCall(BVDestroy(&ctx->Y));
    969:   PetscCall(SlepcContourDataReset(ctx->contour));
    970:   PetscCall(MatDestroy(&ctx->J));
    971:   PetscCall(BVDestroy(&ctx->pV));
    972:   if (ctx->extraction == NEP_CISS_EXTRACTION_RITZ && nep->fui==NEP_USER_INTERFACE_CALLBACK) PetscCall(PetscFree(ctx->dsctxf));
    973:   PetscFunctionReturn(PETSC_SUCCESS);
    974: }

    976: static PetscErrorCode NEPSetFromOptions_CISS(NEP nep,PetscOptionItems *PetscOptionsObject)
    977: {
    978:   NEP_CISS          *ctx = (NEP_CISS*)nep->data;
    979:   PetscReal         r1,r2;
    980:   PetscInt          i,i1,i2,i3,i4,i5,i6,i7;
    981:   PetscBool         b1,flg,flg2,flg3,flg4,flg5,flg6;
    982:   NEPCISSExtraction extraction;

    984:   PetscFunctionBegin;
    985:   PetscOptionsHeadBegin(PetscOptionsObject,"NEP CISS Options");

    987:     PetscCall(NEPCISSGetSizes(nep,&i1,&i2,&i3,&i4,&i5,&b1));
    988:     PetscCall(PetscOptionsInt("-nep_ciss_integration_points","Number of integration points","NEPCISSSetSizes",i1,&i1,&flg));
    989:     PetscCall(PetscOptionsInt("-nep_ciss_blocksize","Block size","NEPCISSSetSizes",i2,&i2,&flg2));
    990:     PetscCall(PetscOptionsInt("-nep_ciss_moments","Moment size","NEPCISSSetSizes",i3,&i3,&flg3));
    991:     PetscCall(PetscOptionsInt("-nep_ciss_partitions","Number of partitions","NEPCISSSetSizes",i4,&i4,&flg4));
    992:     PetscCall(PetscOptionsInt("-nep_ciss_maxblocksize","Maximum block size","NEPCISSSetSizes",i5,&i5,&flg5));
    993:     PetscCall(PetscOptionsBool("-nep_ciss_realmats","True if T(z) is real for real z","NEPCISSSetSizes",b1,&b1,&flg6));
    994:     if (flg || flg2 || flg3 || flg4 || flg5 || flg6) PetscCall(NEPCISSSetSizes(nep,i1,i2,i3,i4,i5,b1));

    996:     PetscCall(NEPCISSGetThreshold(nep,&r1,&r2));
    997:     PetscCall(PetscOptionsReal("-nep_ciss_delta","Threshold for numerical rank","NEPCISSSetThreshold",r1,&r1,&flg));
    998:     PetscCall(PetscOptionsReal("-nep_ciss_spurious_threshold","Threshold for the spurious eigenpairs","NEPCISSSetThreshold",r2,&r2,&flg2));
    999:     if (flg || flg2) PetscCall(NEPCISSSetThreshold(nep,r1,r2));

    1001:     PetscCall(NEPCISSGetRefinement(nep,&i6,&i7));
    1002:     PetscCall(PetscOptionsInt("-nep_ciss_refine_inner","Number of inner iterative refinement iterations","NEPCISSSetRefinement",i6,&i6,&flg));
    1003:     PetscCall(PetscOptionsInt("-nep_ciss_refine_blocksize","Number of blocksize iterative refinement iterations","NEPCISSSetRefinement",i7,&i7,&flg2));
    1004:     if (flg || flg2) PetscCall(NEPCISSSetRefinement(nep,i6,i7));

    1006:     PetscCall(PetscOptionsEnum("-nep_ciss_extraction","Extraction technique","NEPCISSSetExtraction",NEPCISSExtractions,(PetscEnum)ctx->extraction,(PetscEnum*)&extraction,&flg));
    1007:     if (flg) PetscCall(NEPCISSSetExtraction(nep,extraction));

    1009:   PetscOptionsHeadEnd();

    1011:   if (!nep->rg) PetscCall(NEPGetRG(nep,&nep->rg));
    1012:   PetscCall(RGSetFromOptions(nep->rg)); /* this is necessary here to set useconj */
    1013:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(NEPCISSGetKSPs(nep,NULL,NULL));
    1014:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)nep),PETSC_ERR_PLIB,"Something went wrong with NEPCISSGetKSPs()");
    1015:   for (i=0;i<ctx->contour->npoints;i++) PetscCall(KSPSetFromOptions(ctx->contour->ksp[i]));
    1016:   PetscCall(PetscSubcommSetFromOptions(ctx->contour->subcomm));
    1017:   PetscFunctionReturn(PETSC_SUCCESS);
    1018: }

    1020: static PetscErrorCode NEPDestroy_CISS(NEP nep)
    1021: {
    1022:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;

    1024:   PetscFunctionBegin;
    1025:   PetscCall(SlepcContourDataDestroy(&ctx->contour));
    1026:   PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
    1027:   PetscCall(PetscFree(nep->data));
    1028:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetSizes_C",NULL));
    1029:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetSizes_C",NULL));
    1030:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetThreshold_C",NULL));
    1031:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetThreshold_C",NULL));
    1032:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetRefinement_C",NULL));
    1033:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetRefinement_C",NULL));
    1034:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetExtraction_C",NULL));
    1035:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetExtraction_C",NULL));
    1036:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetKSPs_C",NULL));
    1037:   PetscFunctionReturn(PETSC_SUCCESS);
    1038: }

    1040: static PetscErrorCode NEPView_CISS(NEP nep,PetscViewer viewer)
    1041: {
    1042:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;
    1043:   PetscBool      isascii;
    1044:   PetscViewer    sviewer;

    1046:   PetscFunctionBegin;
    1047:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
    1048:   if (isascii) {
    1049:     PetscCall(PetscViewerASCIIPrintf(viewer,"  sizes { integration points: %" PetscInt_FMT ", block size: %" PetscInt_FMT ", moment size: %" PetscInt_FMT ", partitions: %" PetscInt_FMT ", maximum block size: %" PetscInt_FMT " }\n",ctx->N,ctx->L,ctx->M,ctx->npart,ctx->L_max));
    1050:     if (ctx->isreal) PetscCall(PetscViewerASCIIPrintf(viewer,"  exploiting symmetry of integration points\n"));
    1051:     PetscCall(PetscViewerASCIIPrintf(viewer,"  threshold { delta: %g, spurious threshold: %g }\n",(double)ctx->delta,(double)ctx->spurious_threshold));
    1052:     PetscCall(PetscViewerASCIIPrintf(viewer,"  iterative refinement  { inner: %" PetscInt_FMT ", blocksize: %" PetscInt_FMT " }\n",ctx->refine_inner, ctx->refine_blocksize));
    1053:     PetscCall(PetscViewerASCIIPrintf(viewer,"  extraction: %s\n",NEPCISSExtractions[ctx->extraction]));
    1054:     if (!ctx->contour || !ctx->contour->ksp) PetscCall(NEPCISSGetKSPs(nep,NULL,NULL));
    1055:     PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)nep),PETSC_ERR_PLIB,"Something went wrong with NEPCISSGetKSPs()");
    1056:     PetscCall(PetscViewerASCIIPushTab(viewer));
    1057:     if (ctx->npart>1 && ctx->contour->subcomm) {
    1058:       PetscCall(PetscViewerGetSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
    1059:       if (!ctx->contour->subcomm->color) PetscCall(KSPView(ctx->contour->ksp[0],sviewer));
    1060:       PetscCall(PetscViewerFlush(sviewer));
    1061:       PetscCall(PetscViewerRestoreSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
    1062:       /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
    1063:       PetscCall(PetscViewerASCIIPopSynchronized(viewer));
    1064:     } else PetscCall(KSPView(ctx->contour->ksp[0],viewer));
    1065:     PetscCall(PetscViewerASCIIPopTab(viewer));
    1066:   }
    1067:   PetscFunctionReturn(PETSC_SUCCESS);
    1068: }

    1070: static PetscErrorCode NEPSetDSType_CISS(NEP nep)
    1071: {
    1072:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;

    1074:   PetscFunctionBegin;
    1075:   switch (ctx->extraction) {
    1076:     case NEP_CISS_EXTRACTION_RITZ:   PetscCall(DSSetType(nep->ds,DSNEP)); break;
    1077:     case NEP_CISS_EXTRACTION_HANKEL: PetscCall(DSSetType(nep->ds,DSGNHEP)); break;
    1078:     case NEP_CISS_EXTRACTION_CAA:    PetscCall(DSSetType(nep->ds,DSNHEP)); break;
    1079:   }
    1080:   PetscFunctionReturn(PETSC_SUCCESS);
    1081: }

    1083: SLEPC_EXTERN PetscErrorCode NEPCreate_CISS(NEP nep)
    1084: {
    1085:   NEP_CISS       *ctx = (NEP_CISS*)nep->data;

    1087:   PetscFunctionBegin;
    1088:   PetscCall(PetscNew(&ctx));
    1089:   nep->data = ctx;
    1090:   /* set default values of parameters */
    1091:   ctx->N                  = 32;
    1092:   ctx->L                  = 16;
    1093:   ctx->M                  = ctx->N/4;
    1094:   ctx->delta              = SLEPC_DEFAULT_TOL*1e-4;
    1095:   ctx->L_max              = 64;
    1096:   ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
    1097:   ctx->isreal             = PETSC_FALSE;
    1098:   ctx->npart              = 1;

    1100:   nep->useds = PETSC_TRUE;

    1102:   nep->ops->solve          = NEPSolve_CISS;
    1103:   nep->ops->setup          = NEPSetUp_CISS;
    1104:   nep->ops->setfromoptions = NEPSetFromOptions_CISS;
    1105:   nep->ops->reset          = NEPReset_CISS;
    1106:   nep->ops->destroy        = NEPDestroy_CISS;
    1107:   nep->ops->view           = NEPView_CISS;
    1108:   nep->ops->setdstype      = NEPSetDSType_CISS;

    1110:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetSizes_C",NEPCISSSetSizes_CISS));
    1111:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetSizes_C",NEPCISSGetSizes_CISS));
    1112:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetThreshold_C",NEPCISSSetThreshold_CISS));
    1113:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetThreshold_C",NEPCISSGetThreshold_CISS));
    1114:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetRefinement_C",NEPCISSSetRefinement_CISS));
    1115:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetRefinement_C",NEPCISSGetRefinement_CISS));
    1116:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSSetExtraction_C",NEPCISSSetExtraction_CISS));
    1117:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetExtraction_C",NEPCISSGetExtraction_CISS));
    1118:   PetscCall(PetscObjectComposeFunction((PetscObject)nep,"NEPCISSGetKSPs_C",NEPCISSGetKSPs_CISS));
    1119:   PetscFunctionReturn(PETSC_SUCCESS);
    1120: }