Date: 2024-09-25T09:33:57+00:00

Actual source code: pciss.c
slepc-3.21.2 2024-09-25
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 polynomial eigenvalue problems using contour
     27:            integral", Japan J. Indust. Appl. Math. 27:73-90, 2010.
     28: */

     30: #include <slepc/private/pepimpl.h>
     31: #include <slepc/private/slepccontour.h>

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

     62: static PetscErrorCode PEPComputeFunction(PEP pep,PetscScalar lambda,Mat T,Mat P,PetscBool deriv)
     63: {
     64:   PetscInt         i;
     65:   PetscScalar      *coeff;
     66:   Mat              *A,*K;
     67:   MatStructure     str,strp;
     68:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
     69:   SlepcContourData contour = ctx->contour;

     71:   PetscFunctionBegin;
     72:   A = (contour->pA)?contour->pA:pep->A;
     73:   K = (contour->pP)?contour->pP:ctx->Psplit;
     74:   PetscCall(PetscMalloc1(pep->nmat,&coeff));
     75:   if (deriv) PetscCall(PEPEvaluateBasisDerivative(pep,lambda,0,coeff,NULL));
     76:   else PetscCall(PEPEvaluateBasis(pep,lambda,0,coeff,NULL));
     77:   PetscCall(STGetMatStructure(pep->st,&str));
     78:   PetscCall(MatZeroEntries(T));
     79:   if (!deriv && T != P) {
     80:     PetscCall(STGetSplitPreconditionerInfo(pep->st,NULL,&strp));
     81:     PetscCall(MatZeroEntries(P));
     82:   }
     83:   i = deriv?1:0;
     84:   for (;i<pep->nmat;i++) {
     85:     PetscCall(MatAXPY(T,coeff[i],A[i],str));
     86:     if (!deriv && T != P) PetscCall(MatAXPY(P,coeff[i],K[i],strp));
     87:   }
     88:   PetscCall(PetscFree(coeff));
     89:   PetscFunctionReturn(PETSC_SUCCESS);
     90: }

     92: /*
     93:   Set up KSP solvers for every integration point
     94: */
     95: static PetscErrorCode PEPCISSSetUp(PEP pep,Mat T,Mat P)
     96: {
     97:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
     98:   SlepcContourData contour;
     99:   PetscInt         i,p_id;
    100:   Mat              Amat,Pmat;

    102:   PetscFunctionBegin;
    103:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(PEPCISSGetKSPs(pep,NULL,NULL));
    104:   contour = ctx->contour;
    105:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Something went wrong with PEPCISSGetKSPs()");
    106:   for (i=0;i<contour->npoints;i++) {
    107:     p_id = i*contour->subcomm->n + contour->subcomm->color;
    108:     PetscCall(MatDuplicate(T,MAT_DO_NOT_COPY_VALUES,&Amat));
    109:     if (T != P) PetscCall(MatDuplicate(P,MAT_DO_NOT_COPY_VALUES,&Pmat)); else Pmat = Amat;
    110:     PetscCall(PEPComputeFunction(pep,ctx->omega[p_id],Amat,Pmat,PETSC_FALSE));
    111:     PetscCall(PEP_KSPSetOperators(contour->ksp[i],Amat,Pmat));
    112:     PetscCall(MatDestroy(&Amat));
    113:     if (T != P) PetscCall(MatDestroy(&Pmat));
    114:   }
    115:   PetscFunctionReturn(PETSC_SUCCESS);
    116: }

    118: /*
    119:   Y_i = F(z_i)^{-1}Fp(z_i)V for every integration point, Y=[Y_i] is in the context
    120: */
    121: static PetscErrorCode PEPCISSSolve(PEP pep,Mat dT,BV V,PetscInt L_start,PetscInt L_end)
    122: {
    123:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
    124:   SlepcContourData contour;
    125:   PetscInt         i,p_id;
    126:   Mat              MV,BMV=NULL,MC;

    128:   PetscFunctionBegin;
    129:   contour = ctx->contour;
    130:   PetscCall(BVSetActiveColumns(V,L_start,L_end));
    131:   PetscCall(BVGetMat(V,&MV));
    132:   for (i=0;i<contour->npoints;i++) {
    133:     p_id = i*contour->subcomm->n + contour->subcomm->color;
    134:     PetscCall(PEPComputeFunction(pep,ctx->omega[p_id],dT,NULL,PETSC_TRUE));
    135:     PetscCall(BVSetActiveColumns(ctx->Y,i*ctx->L+L_start,i*ctx->L+L_end));
    136:     PetscCall(BVGetMat(ctx->Y,&MC));
    137:     if (!i) {
    138:       PetscCall(MatProductCreate(dT,MV,NULL,&BMV));
    139:       PetscCall(MatProductSetType(BMV,MATPRODUCT_AB));
    140:       PetscCall(MatProductSetFromOptions(BMV));
    141:       PetscCall(MatProductSymbolic(BMV));
    142:     }
    143:     PetscCall(MatProductNumeric(BMV));
    144:     PetscCall(KSPMatSolve(contour->ksp[i],BMV,MC));
    145:     PetscCall(BVRestoreMat(ctx->Y,&MC));
    146:   }
    147:   PetscCall(MatDestroy(&BMV));
    148:   PetscCall(BVRestoreMat(V,&MV));
    149:   PetscFunctionReturn(PETSC_SUCCESS);
    150: }

    152: static PetscErrorCode PEPSetUp_CISS(PEP pep)
    153: {
    154:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
    155:   SlepcContourData contour;
    156:   PetscInt         i,nwork,nsplit;
    157:   PetscBool        istrivial,isellipse,flg;
    158:   PetscObjectId    id;
    159:   PetscObjectState state;
    160:   Vec              v0;

    162:   PetscFunctionBegin;
    163:   if (pep->ncv==PETSC_DEFAULT) pep->ncv = ctx->L_max*ctx->M;
    164:   else {
    165:     ctx->L_max = pep->ncv/ctx->M;
    166:     if (!ctx->L_max) {
    167:       ctx->L_max = 1;
    168:       pep->ncv = ctx->L_max*ctx->M;
    169:     }
    170:   }
    171:   ctx->L = PetscMin(ctx->L,ctx->L_max);
    172:   if (pep->max_it==PETSC_DEFAULT) pep->max_it = 5;
    173:   if (pep->mpd==PETSC_DEFAULT) pep->mpd = pep->ncv;
    174:   if (!pep->which) pep->which = PEP_ALL;
    175:   PetscCheck(pep->which==PEP_ALL,PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"This solver supports only computing all eigenvalues");
    176:   PEPCheckUnsupported(pep,PEP_FEATURE_STOPPING);
    177:   PEPCheckIgnored(pep,PEP_FEATURE_SCALE);

    179:   /* check region */
    180:   PetscCall(RGIsTrivial(pep->rg,&istrivial));
    181:   PetscCheck(!istrivial,PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"CISS requires a nontrivial region, e.g. -rg_type ellipse ...");
    182:   PetscCall(RGGetComplement(pep->rg,&flg));
    183:   PetscCheck(!flg,PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"A region with complement flag set is not allowed");
    184:   PetscCall(PetscObjectTypeCompare((PetscObject)pep->rg,RGELLIPSE,&isellipse));
    185:   PetscCheck(isellipse,PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"Currently only implemented for elliptic regions");

    187:   /* if the region has changed, then reset contour data */
    188:   PetscCall(PetscObjectGetId((PetscObject)pep->rg,&id));
    189:   PetscCall(PetscObjectStateGet((PetscObject)pep->rg,&state));
    190:   if (ctx->rgid && (id != ctx->rgid || state != ctx->rgstate)) {
    191:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
    192:     PetscCall(PetscInfo(pep,"Resetting the contour data structure due to a change of region\n"));
    193:     ctx->rgid = id; ctx->rgstate = state;
    194:   }

    196:   /* create contour data structure */
    197:   if (!ctx->contour) {
    198:     PetscCall(RGCanUseConjugates(pep->rg,ctx->isreal,&ctx->useconj));
    199:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)pep,&ctx->contour));
    200:   }

    202:   PetscCall(PEPAllocateSolution(pep,0));
    203:   if (ctx->weight) PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
    204:   PetscCall(PetscMalloc4(ctx->N,&ctx->weight,ctx->N,&ctx->omega,ctx->N,&ctx->pp,ctx->L_max*ctx->M,&ctx->sigma));

    206:   /* allocate basis vectors */
    207:   PetscCall(BVDestroy(&ctx->S));
    208:   PetscCall(BVDuplicateResize(pep->V,ctx->L*ctx->M,&ctx->S));
    209:   PetscCall(BVDestroy(&ctx->V));
    210:   PetscCall(BVDuplicateResize(pep->V,ctx->L,&ctx->V));

    212:   /* check if a user-defined split preconditioner has been set */
    213:   PetscCall(STGetSplitPreconditionerInfo(pep->st,&nsplit,NULL));
    214:   if (nsplit) {
    215:     PetscCall(PetscFree(ctx->Psplit));
    216:     PetscCall(PetscMalloc1(nsplit,&ctx->Psplit));
    217:     for (i=0;i<nsplit;i++) PetscCall(STGetSplitPreconditionerTerm(pep->st,i,&ctx->Psplit[i]));
    218:   }

    220:   contour = ctx->contour;
    221:   PetscCall(SlepcContourRedundantMat(contour,pep->nmat,pep->A,ctx->Psplit));
    222:   if (!ctx->J) PetscCall(MatDuplicate(contour->pA?contour->pA[0]:pep->A[0],MAT_DO_NOT_COPY_VALUES,&ctx->J));
    223:   if (contour->pA) {
    224:     PetscCall(BVGetColumn(ctx->V,0,&v0));
    225:     PetscCall(SlepcContourScatterCreate(contour,v0));
    226:     PetscCall(BVRestoreColumn(ctx->V,0,&v0));
    227:     PetscCall(BVDestroy(&ctx->pV));
    228:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->pV));
    229:     PetscCall(BVSetSizesFromVec(ctx->pV,contour->xsub,pep->n));
    230:     PetscCall(BVSetFromOptions(ctx->pV));
    231:     PetscCall(BVResize(ctx->pV,ctx->L,PETSC_FALSE));
    232:   }

    234:   PetscCall(BVDestroy(&ctx->Y));
    235:   if (contour->pA) {
    236:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->Y));
    237:     PetscCall(BVSetSizesFromVec(ctx->Y,contour->xsub,pep->n));
    238:     PetscCall(BVSetFromOptions(ctx->Y));
    239:     PetscCall(BVResize(ctx->Y,contour->npoints*ctx->L,PETSC_FALSE));
    240:   } else PetscCall(BVDuplicateResize(pep->V,contour->npoints*ctx->L,&ctx->Y));

    242:   if (ctx->extraction == PEP_CISS_EXTRACTION_RITZ) {
    243:     PetscCall(DSPEPSetDegree(pep->ds,pep->nmat-1));
    244:     PetscCall(DSPEPSetCoefficients(pep->ds,pep->pbc));
    245:   }
    246:   PetscCall(DSAllocate(pep->ds,pep->ncv));
    247:   nwork = 2;
    248:   PetscCall(PEPSetWorkVecs(pep,nwork));
    249:   PetscFunctionReturn(PETSC_SUCCESS);
    250: }

    252: static PetscErrorCode PEPSolve_CISS(PEP pep)
    253: {
    254:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
    255:   SlepcContourData contour = ctx->contour;
    256:   Mat              X,M,E,T,P;
    257:   PetscInt         i,j,ld,L_add=0,nv=0,L_base=ctx->L,inner,*inside,nsplit;
    258:   PetscScalar      *Mu,*H0,*H1,*rr,*temp,center;
    259:   PetscReal        error,max_error,radius,rgscale,est_eig,eta;
    260:   PetscBool        isellipse,*fl1;
    261:   Vec              si;
    262:   SlepcSC          sc;
    263:   PetscRandom      rand;

    265:   PetscFunctionBegin;
    266:   PetscCall(DSGetSlepcSC(pep->ds,&sc));
    267:   sc->comparison    = SlepcCompareLargestMagnitude;
    268:   sc->comparisonctx = NULL;
    269:   sc->map           = NULL;
    270:   sc->mapobj        = NULL;
    271:   PetscCall(DSGetLeadingDimension(pep->ds,&ld));
    272:   PetscCall(RGComputeQuadrature(pep->rg,RG_QUADRULE_TRAPEZOIDAL,ctx->N,ctx->omega,ctx->pp,ctx->weight));
    273:   PetscCall(STGetSplitPreconditionerInfo(pep->st,&nsplit,NULL));
    274:   if (contour->pA) {
    275:     T = contour->pA[0];
    276:     P = nsplit? contour->pP[0]: T;
    277:   } else {
    278:     T = pep->A[0];
    279:     P = nsplit? ctx->Psplit[0]: T;
    280:   }
    281:   PetscCall(PEPCISSSetUp(pep,T,P));
    282:   PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    283:   PetscCall(BVSetRandomSign(ctx->V));
    284:   PetscCall(BVGetRandomContext(ctx->V,&rand));
    285:   if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    286:   PetscCall(PEPCISSSolve(pep,ctx->J,(contour->pA)?ctx->pV:ctx->V,0,ctx->L));
    287:   PetscCall(PetscObjectTypeCompare((PetscObject)pep->rg,RGELLIPSE,&isellipse));
    288:   if (isellipse) {
    289:     PetscCall(BVTraceQuadrature(ctx->Y,ctx->V,ctx->L,ctx->L,ctx->weight,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj,&est_eig));
    290:     PetscCall(PetscInfo(pep,"Estimated eigenvalue count: %f\n",(double)est_eig));
    291:     eta = PetscPowReal(10.0,-PetscLog10Real(pep->tol)/ctx->N);
    292:     L_add = PetscMax(0,(PetscInt)PetscCeilReal((est_eig*eta)/ctx->M)-ctx->L);
    293:     if (L_add>ctx->L_max-ctx->L) {
    294:       PetscCall(PetscInfo(pep,"Number of eigenvalues inside the contour path may be too large\n"));
    295:       L_add = ctx->L_max-ctx->L;
    296:     }
    297:   }
    298:   /* Updates L after estimate the number of eigenvalue */
    299:   if (L_add>0) {
    300:     PetscCall(PetscInfo(pep,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by Estimate #Eig\n",ctx->L,ctx->L+L_add));
    301:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
    302:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
    303:     PetscCall(BVSetRandomSign(ctx->V));
    304:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    305:     ctx->L += L_add;
    306:     PetscCall(PEPCISSSolve(pep,ctx->J,(contour->pA)?ctx->pV:ctx->V,ctx->L-L_add,ctx->L));
    307:   }

    309:   PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
    310:   for (i=0;i<ctx->refine_blocksize;i++) {
    311:     PetscCall(BVDotQuadrature(ctx->Y,(contour->pA)?ctx->pV:ctx->V,Mu,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->subcomm,contour->npoints,ctx->useconj));
    312:     PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    313:     PetscCall(PetscLogEventBegin(PEP_CISS_SVD,pep,0,0,0));
    314:     PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
    315:     PetscCall(PetscLogEventEnd(PEP_CISS_SVD,pep,0,0,0));
    316:     if (ctx->sigma[0]<=ctx->delta || nv < ctx->L*ctx->M || ctx->L == ctx->L_max) break;
    317:     L_add = L_base;
    318:     if (ctx->L+L_add>ctx->L_max) L_add = ctx->L_max-ctx->L;
    319:     PetscCall(PetscInfo(pep,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by SVD(H0)\n",ctx->L,ctx->L+L_add));
    320:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
    321:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
    322:     PetscCall(BVSetRandomSign(ctx->V));
    323:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    324:     ctx->L += L_add;
    325:     PetscCall(PEPCISSSolve(pep,ctx->J,(contour->pA)?ctx->pV:ctx->V,ctx->L-L_add,ctx->L));
    326:     if (L_add) {
    327:       PetscCall(PetscFree2(Mu,H0));
    328:       PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
    329:     }
    330:   }

    332:   PetscCall(RGGetScale(pep->rg,&rgscale));
    333:   PetscCall(RGEllipseGetParameters(pep->rg,&center,&radius,NULL));

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

    337:   while (pep->reason == PEP_CONVERGED_ITERATING) {
    338:     pep->its++;
    339:     for (inner=0;inner<=ctx->refine_inner;inner++) {
    340:       if (ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) {
    341:         PetscCall(BVDotQuadrature(ctx->Y,(contour->pA)?ctx->pV:ctx->V,Mu,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->subcomm,contour->npoints,ctx->useconj));
    342:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    343:         PetscCall(PetscLogEventBegin(PEP_CISS_SVD,pep,0,0,0));
    344:         PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
    345:         PetscCall(PetscLogEventEnd(PEP_CISS_SVD,pep,0,0,0));
    346:       } else {
    347:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    348:         /* compute SVD of S */
    349:         PetscCall(BVSVDAndRank(ctx->S,ctx->M,ctx->L,ctx->delta,(ctx->extraction==PEP_CISS_EXTRACTION_CAA)?BV_SVD_METHOD_QR_CAA:BV_SVD_METHOD_QR,H0,ctx->sigma,&nv));
    350:       }
    351:       PetscCall(PetscInfo(pep,"Estimated rank: nv = %" PetscInt_FMT "\n",nv));
    352:       if (ctx->sigma[0]>ctx->delta && nv==ctx->L*ctx->M && inner!=ctx->refine_inner) {
    353:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    354:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
    355:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    356:         PetscCall(BVCopy(ctx->S,ctx->V));
    357:         if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    358:         PetscCall(PEPCISSSolve(pep,ctx->J,(contour->pA)?ctx->pV:ctx->V,0,ctx->L));
    359:       } else break;
    360:     }
    361:     pep->nconv = 0;
    362:     if (nv == 0) { pep->reason = PEP_CONVERGED_TOL; break; }
    363:     else {
    364:       /* Extracting eigenpairs */
    365:       PetscCall(DSSetDimensions(pep->ds,nv,0,0));
    366:       PetscCall(DSSetState(pep->ds,DS_STATE_RAW));
    367:       if (ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) {
    368:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
    369:         PetscCall(CISS_BlockHankel(Mu,1,ctx->L,ctx->M,H1));
    370:         PetscCall(DSGetArray(pep->ds,DS_MAT_A,&temp));
    371:         for (j=0;j<nv;j++)
    372:           for (i=0;i<nv;i++)
    373:             temp[i+j*ld] = H1[i+j*ctx->L*ctx->M];
    374:         PetscCall(DSRestoreArray(pep->ds,DS_MAT_A,&temp));
    375:         PetscCall(DSGetArray(pep->ds,DS_MAT_B,&temp));
    376:         for (j=0;j<nv;j++)
    377:           for (i=0;i<nv;i++)
    378:             temp[i+j*ld] = H0[i+j*ctx->L*ctx->M];
    379:         PetscCall(DSRestoreArray(pep->ds,DS_MAT_B,&temp));
    380:       } else if (ctx->extraction == PEP_CISS_EXTRACTION_CAA) {
    381:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    382:         PetscCall(DSGetArray(pep->ds,DS_MAT_A,&temp));
    383:         for (i=0;i<nv;i++) PetscCall(PetscArraycpy(temp+i*ld,H0+i*nv,nv));
    384:         PetscCall(DSRestoreArray(pep->ds,DS_MAT_A,&temp));
    385:       } else {
    386:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    387:         for (i=0;i<pep->nmat;i++) {
    388:           PetscCall(DSGetMat(pep->ds,DSMatExtra[i],&E));
    389:           PetscCall(BVMatProject(ctx->S,pep->A[i],ctx->S,E));
    390:           PetscCall(DSRestoreMat(pep->ds,DSMatExtra[i],&E));
    391:         }
    392:         nv = (pep->nmat-1)*nv;
    393:       }
    394:       PetscCall(DSSolve(pep->ds,pep->eigr,pep->eigi));
    395:       PetscCall(DSSynchronize(pep->ds,pep->eigr,pep->eigi));
    396:       if (ctx->extraction == PEP_CISS_EXTRACTION_CAA || ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) {
    397:         for (i=0;i<nv;i++) {
    398:           pep->eigr[i] = (pep->eigr[i]*radius+center)*rgscale;
    399:         }
    400:       }
    401:       PetscCall(PetscMalloc3(nv,&fl1,nv,&inside,nv,&rr));
    402:       PetscCall(DSVectors(pep->ds,DS_MAT_X,NULL,NULL));
    403:       PetscCall(DSGetMat(pep->ds,DS_MAT_X,&X));
    404:       PetscCall(SlepcCISS_isGhost(X,nv,ctx->sigma,ctx->spurious_threshold,fl1));
    405:       PetscCall(DSRestoreMat(pep->ds,DS_MAT_X,&X));
    406:       PetscCall(RGCheckInside(pep->rg,nv,pep->eigr,pep->eigi,inside));
    407:       for (i=0;i<nv;i++) {
    408:         if (fl1[i] && inside[i]>=0) {
    409:           rr[i] = 1.0;
    410:           pep->nconv++;
    411:         } else rr[i] = 0.0;
    412:       }
    413:       PetscCall(DSSort(pep->ds,pep->eigr,pep->eigi,rr,NULL,&pep->nconv));
    414:       PetscCall(DSSynchronize(pep->ds,pep->eigr,pep->eigi));
    415:       if (ctx->extraction == PEP_CISS_EXTRACTION_CAA || ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) {
    416:         for (i=0;i<nv;i++) pep->eigr[i] = (pep->eigr[i]*radius+center)*rgscale;
    417:       }
    418:       PetscCall(PetscFree3(fl1,inside,rr));
    419:       PetscCall(BVSetActiveColumns(pep->V,0,nv));
    420:       PetscCall(DSVectors(pep->ds,DS_MAT_X,NULL,NULL));
    421:       if (ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) {
    422:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
    423:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    424:         PetscCall(BVCopy(ctx->S,pep->V));
    425:         PetscCall(DSGetMat(pep->ds,DS_MAT_X,&X));
    426:         PetscCall(BVMultInPlace(ctx->S,X,0,pep->nconv));
    427:         PetscCall(BVMultInPlace(pep->V,X,0,pep->nconv));
    428:         PetscCall(DSRestoreMat(pep->ds,DS_MAT_X,&X));
    429:       } else {
    430:         PetscCall(DSGetMat(pep->ds,DS_MAT_X,&X));
    431:         PetscCall(BVMultInPlace(ctx->S,X,0,pep->nconv));
    432:         PetscCall(DSRestoreMat(pep->ds,DS_MAT_X,&X));
    433:         PetscCall(BVSetActiveColumns(ctx->S,0,pep->nconv));
    434:         PetscCall(BVCopy(ctx->S,pep->V));
    435:       }
    436:       max_error = 0.0;
    437:       for (i=0;i<pep->nconv;i++) {
    438:         PetscCall(BVGetColumn(pep->V,i,&si));
    439:         PetscCall(VecNormalize(si,NULL));
    440:         PetscCall(PEPComputeResidualNorm_Private(pep,pep->eigr[i],0,si,NULL,pep->work,&error));
    441:         PetscCall((*pep->converged)(pep,pep->eigr[i],0,error,&error,pep->convergedctx));
    442:         PetscCall(BVRestoreColumn(pep->V,i,&si));
    443:         max_error = PetscMax(max_error,error);
    444:       }
    445:       if (max_error <= pep->tol) pep->reason = PEP_CONVERGED_TOL;
    446:       else if (pep->its > pep->max_it) pep->reason = PEP_DIVERGED_ITS;
    447:       else {
    448:         if (pep->nconv > ctx->L) nv = pep->nconv;
    449:         else if (ctx->L > nv) nv = ctx->L;
    450:         nv = PetscMin(nv,ctx->L*ctx->M);
    451:         PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,nv,ctx->L,NULL,&M));
    452:         PetscCall(MatSetRandom(M,rand));
    453:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
    454:         PetscCall(BVMultInPlace(ctx->S,M,0,ctx->L));
    455:         PetscCall(MatDestroy(&M));
    456:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
    457:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
    458:         PetscCall(BVCopy(ctx->S,ctx->V));
    459:         if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
    460:         PetscCall(PEPCISSSolve(pep,ctx->J,(contour->pA)?ctx->pV:ctx->V,0,ctx->L));
    461:       }
    462:     }
    463:   }
    464:   PetscCall(PetscFree2(Mu,H0));
    465:   if (ctx->extraction == PEP_CISS_EXTRACTION_HANKEL) PetscCall(PetscFree(H1));
    466:   PetscFunctionReturn(PETSC_SUCCESS);
    467: }

    469: static PetscErrorCode PEPCISSSetSizes_CISS(PEP pep,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
    470: {
    471:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;
    472:   PetscInt       oN,oL,oM,oLmax,onpart;
    473:   PetscMPIInt    size;

    475:   PetscFunctionBegin;
    476:   oN = ctx->N;
    477:   if (ip == PETSC_DECIDE || ip == PETSC_DEFAULT) {
    478:     if (ctx->N!=32) { ctx->N =32; ctx->M = ctx->N/4; }
    479:   } else {
    480:     PetscCheck(ip>0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be > 0");
    481:     PetscCheck(ip%2==0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be an even number");
    482:     if (ctx->N!=ip) { ctx->N = ip; ctx->M = ctx->N/4; }
    483:   }
    484:   oL = ctx->L;
    485:   if (bs == PETSC_DECIDE || bs == PETSC_DEFAULT) {
    486:     ctx->L = 16;
    487:   } else {
    488:     PetscCheck(bs>0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The bs argument must be > 0");
    489:     ctx->L = bs;
    490:   }
    491:   oM = ctx->M;
    492:   if (ms == PETSC_DECIDE || ms == PETSC_DEFAULT) {
    493:     ctx->M = ctx->N/4;
    494:   } else {
    495:     PetscCheck(ms>0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be > 0");
    496:     PetscCheck(ms<=ctx->N,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be less than or equal to the number of integration points");
    497:     ctx->M = PetscMax(ms,2);
    498:   }
    499:   onpart = ctx->npart;
    500:   if (npart == PETSC_DECIDE || npart == PETSC_DEFAULT) {
    501:     ctx->npart = 1;
    502:   } else {
    503:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)pep),&size));
    504:     PetscCheck(npart>0 && npart<=size,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of npart");
    505:     ctx->npart = npart;
    506:   }
    507:   oLmax = ctx->L_max;
    508:   if (bsmax == PETSC_DECIDE || bsmax == PETSC_DEFAULT) {
    509:     ctx->L_max = 64;
    510:   } else {
    511:     PetscCheck(bsmax>0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The bsmax argument must be > 0");
    512:     ctx->L_max = PetscMax(bsmax,ctx->L);
    513:   }
    514:   if (onpart != ctx->npart || oN != ctx->N || realmats != ctx->isreal) {
    515:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
    516:     PetscCall(PetscInfo(pep,"Resetting the contour data structure due to a change of parameters\n"));
    517:     pep->state = PEP_STATE_INITIAL;
    518:   }
    519:   ctx->isreal = realmats;
    520:   if (oL != ctx->L || oM != ctx->M || oLmax != ctx->L_max) pep->state = PEP_STATE_INITIAL;
    521:   PetscFunctionReturn(PETSC_SUCCESS);
    522: }

    524: /*@
    525:    PEPCISSSetSizes - Sets the values of various size parameters in the CISS solver.

    527:    Logically Collective

    529:    Input Parameters:
    530: +  pep   - the polynomial eigensolver context
    531: .  ip    - number of integration points
    532: .  bs    - block size
    533: .  ms    - moment size
    534: .  npart - number of partitions when splitting the communicator
    535: .  bsmax - max block size
    536: -  realmats - all coefficient matrices of P(.) are real

    538:    Options Database Keys:
    539: +  -pep_ciss_integration_points - Sets the number of integration points
    540: .  -pep_ciss_blocksize - Sets the block size
    541: .  -pep_ciss_moments - Sets the moment size
    542: .  -pep_ciss_partitions - Sets the number of partitions
    543: .  -pep_ciss_maxblocksize - Sets the maximum block size
    544: -  -pep_ciss_realmats - all coefficient matrices of P(.) are real

    546:    Notes:
    547:    The default number of partitions is 1. This means the internal KSP object is shared
    548:    among all processes of the PEP communicator. Otherwise, the communicator is split
    549:    into npart communicators, so that npart KSP solves proceed simultaneously.

    551:    Level: advanced

    553: .seealso: PEPCISSGetSizes()
    554: @*/
    555: PetscErrorCode PEPCISSSetSizes(PEP pep,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
    556: {
    557:   PetscFunctionBegin;
    565:   PetscTryMethod(pep,"PEPCISSSetSizes_C",(PEP,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool),(pep,ip,bs,ms,npart,bsmax,realmats));
    566:   PetscFunctionReturn(PETSC_SUCCESS);
    567: }

    569: static PetscErrorCode PEPCISSGetSizes_CISS(PEP pep,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
    570: {
    571:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    573:   PetscFunctionBegin;
    574:   if (ip) *ip = ctx->N;
    575:   if (bs) *bs = ctx->L;
    576:   if (ms) *ms = ctx->M;
    577:   if (npart) *npart = ctx->npart;
    578:   if (bsmax) *bsmax = ctx->L_max;
    579:   if (realmats) *realmats = ctx->isreal;
    580:   PetscFunctionReturn(PETSC_SUCCESS);
    581: }

    583: /*@
    584:    PEPCISSGetSizes - Gets the values of various size parameters in the CISS solver.

    586:    Not Collective

    588:    Input Parameter:
    589: .  pep - the polynomial eigensolver context

    591:    Output Parameters:
    592: +  ip    - number of integration points
    593: .  bs    - block size
    594: .  ms    - moment size
    595: .  npart - number of partitions when splitting the communicator
    596: .  bsmax - max block size
    597: -  realmats - all coefficient matrices of P(.) are real

    599:    Level: advanced

    601: .seealso: PEPCISSSetSizes()
    602: @*/
    603: PetscErrorCode PEPCISSGetSizes(PEP pep,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
    604: {
    605:   PetscFunctionBegin;
    607:   PetscUseMethod(pep,"PEPCISSGetSizes_C",(PEP,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscBool*),(pep,ip,bs,ms,npart,bsmax,realmats));
    608:   PetscFunctionReturn(PETSC_SUCCESS);
    609: }

    611: static PetscErrorCode PEPCISSSetThreshold_CISS(PEP pep,PetscReal delta,PetscReal spur)
    612: {
    613:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    615:   PetscFunctionBegin;
    616:   if (delta == (PetscReal)PETSC_DEFAULT) {
    617:     ctx->delta = SLEPC_DEFAULT_TOL*1e-4;
    618:   } else {
    619:     PetscCheck(delta>0.0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The delta argument must be > 0.0");
    620:     ctx->delta = delta;
    621:   }
    622:   if (spur == (PetscReal)PETSC_DEFAULT) {
    623:     ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
    624:   } else {
    625:     PetscCheck(spur>0.0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The spurious threshold argument must be > 0.0");
    626:     ctx->spurious_threshold = spur;
    627:   }
    628:   PetscFunctionReturn(PETSC_SUCCESS);
    629: }

    631: /*@
    632:    PEPCISSSetThreshold - Sets the values of various threshold parameters in
    633:    the CISS solver.

    635:    Logically Collective

    637:    Input Parameters:
    638: +  pep   - the polynomial eigensolver context
    639: .  delta - threshold for numerical rank
    640: -  spur  - spurious threshold (to discard spurious eigenpairs)

    642:    Options Database Keys:
    643: +  -pep_ciss_delta - Sets the delta
    644: -  -pep_ciss_spurious_threshold - Sets the spurious threshold

    646:    Level: advanced

    648: .seealso: PEPCISSGetThreshold()
    649: @*/
    650: PetscErrorCode PEPCISSSetThreshold(PEP pep,PetscReal delta,PetscReal spur)
    651: {
    652:   PetscFunctionBegin;
    656:   PetscTryMethod(pep,"PEPCISSSetThreshold_C",(PEP,PetscReal,PetscReal),(pep,delta,spur));
    657:   PetscFunctionReturn(PETSC_SUCCESS);
    658: }

    660: static PetscErrorCode PEPCISSGetThreshold_CISS(PEP pep,PetscReal *delta,PetscReal *spur)
    661: {
    662:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    664:   PetscFunctionBegin;
    665:   if (delta) *delta = ctx->delta;
    666:   if (spur)  *spur = ctx->spurious_threshold;
    667:   PetscFunctionReturn(PETSC_SUCCESS);
    668: }

    670: /*@
    671:    PEPCISSGetThreshold - Gets the values of various threshold parameters in
    672:    the CISS solver.

    674:    Not Collective

    676:    Input Parameter:
    677: .  pep - the polynomial eigensolver context

    679:    Output Parameters:
    680: +  delta - threshold for numerical rank
    681: -  spur  - spurious threshold (to discard spurious eigenpairs)

    683:    Level: advanced

    685: .seealso: PEPCISSSetThreshold()
    686: @*/
    687: PetscErrorCode PEPCISSGetThreshold(PEP pep,PetscReal *delta,PetscReal *spur)
    688: {
    689:   PetscFunctionBegin;
    691:   PetscUseMethod(pep,"PEPCISSGetThreshold_C",(PEP,PetscReal*,PetscReal*),(pep,delta,spur));
    692:   PetscFunctionReturn(PETSC_SUCCESS);
    693: }

    695: static PetscErrorCode PEPCISSSetRefinement_CISS(PEP pep,PetscInt inner,PetscInt blsize)
    696: {
    697:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    699:   PetscFunctionBegin;
    700:   if (inner == PETSC_DEFAULT) {
    701:     ctx->refine_inner = 0;
    702:   } else {
    703:     PetscCheck(inner>=0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The refine inner argument must be >= 0");
    704:     ctx->refine_inner = inner;
    705:   }
    706:   if (blsize == PETSC_DEFAULT) {
    707:     ctx->refine_blocksize = 0;
    708:   } else {
    709:     PetscCheck(blsize>=0,PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_OUTOFRANGE,"The refine blocksize argument must be >= 0");
    710:     ctx->refine_blocksize = blsize;
    711:   }
    712:   PetscFunctionReturn(PETSC_SUCCESS);
    713: }

    715: /*@
    716:    PEPCISSSetRefinement - Sets the values of various refinement parameters
    717:    in the CISS solver.

    719:    Logically Collective

    721:    Input Parameters:
    722: +  pep    - the polynomial eigensolver context
    723: .  inner  - number of iterative refinement iterations (inner loop)
    724: -  blsize - number of iterative refinement iterations (blocksize loop)

    726:    Options Database Keys:
    727: +  -pep_ciss_refine_inner - Sets number of inner iterations
    728: -  -pep_ciss_refine_blocksize - Sets number of blocksize iterations

    730:    Level: advanced

    732: .seealso: PEPCISSGetRefinement()
    733: @*/
    734: PetscErrorCode PEPCISSSetRefinement(PEP pep,PetscInt inner,PetscInt blsize)
    735: {
    736:   PetscFunctionBegin;
    740:   PetscTryMethod(pep,"PEPCISSSetRefinement_C",(PEP,PetscInt,PetscInt),(pep,inner,blsize));
    741:   PetscFunctionReturn(PETSC_SUCCESS);
    742: }

    744: static PetscErrorCode PEPCISSGetRefinement_CISS(PEP pep,PetscInt *inner,PetscInt *blsize)
    745: {
    746:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    748:   PetscFunctionBegin;
    749:   if (inner)  *inner = ctx->refine_inner;
    750:   if (blsize) *blsize = ctx->refine_blocksize;
    751:   PetscFunctionReturn(PETSC_SUCCESS);
    752: }

    754: /*@
    755:    PEPCISSGetRefinement - Gets the values of various refinement parameters
    756:    in the CISS solver.

    758:    Not Collective

    760:    Input Parameter:
    761: .  pep - the polynomial eigensolver context

    763:    Output Parameters:
    764: +  inner  - number of iterative refinement iterations (inner loop)
    765: -  blsize - number of iterative refinement iterations (blocksize loop)

    767:    Level: advanced

    769: .seealso: PEPCISSSetRefinement()
    770: @*/
    771: PetscErrorCode PEPCISSGetRefinement(PEP pep, PetscInt *inner, PetscInt *blsize)
    772: {
    773:   PetscFunctionBegin;
    775:   PetscUseMethod(pep,"PEPCISSGetRefinement_C",(PEP,PetscInt*,PetscInt*),(pep,inner,blsize));
    776:   PetscFunctionReturn(PETSC_SUCCESS);
    777: }

    779: static PetscErrorCode PEPCISSSetExtraction_CISS(PEP pep,PEPCISSExtraction extraction)
    780: {
    781:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    783:   PetscFunctionBegin;
    784:   if (ctx->extraction != extraction) {
    785:     ctx->extraction = extraction;
    786:     pep->state      = PEP_STATE_INITIAL;
    787:   }
    788:   PetscFunctionReturn(PETSC_SUCCESS);
    789: }

    791: /*@
    792:    PEPCISSSetExtraction - Sets the extraction technique used in the CISS solver.

    794:    Logically Collective

    796:    Input Parameters:
    797: +  pep        - the polynomial eigensolver context
    798: -  extraction - the extraction technique

    800:    Options Database Key:
    801: .  -pep_ciss_extraction - Sets the extraction technique (either 'ritz', 'hankel' or 'caa')

    803:    Notes:
    804:    By default, the Rayleigh-Ritz extraction is used (PEP_CISS_EXTRACTION_RITZ).

    806:    If the 'hankel' or the 'caa' option is specified (PEP_CISS_EXTRACTION_HANKEL or
    807:    PEP_CISS_EXTRACTION_CAA), then the Block Hankel method, or the Communication-avoiding
    808:    Arnoldi method, respectively, is used for extracting eigenpairs.

    810:    Level: advanced

    812: .seealso: PEPCISSGetExtraction(), PEPCISSExtraction
    813: @*/
    814: PetscErrorCode PEPCISSSetExtraction(PEP pep,PEPCISSExtraction extraction)
    815: {
    816:   PetscFunctionBegin;
    819:   PetscTryMethod(pep,"PEPCISSSetExtraction_C",(PEP,PEPCISSExtraction),(pep,extraction));
    820:   PetscFunctionReturn(PETSC_SUCCESS);
    821: }

    823: static PetscErrorCode PEPCISSGetExtraction_CISS(PEP pep,PEPCISSExtraction *extraction)
    824: {
    825:   PEP_CISS *ctx = (PEP_CISS*)pep->data;

    827:   PetscFunctionBegin;
    828:   *extraction = ctx->extraction;
    829:   PetscFunctionReturn(PETSC_SUCCESS);
    830: }

    832: /*@
    833:    PEPCISSGetExtraction - Gets the extraction technique used in the CISS solver.

    835:    Not Collective

    837:    Input Parameter:
    838: .  pep - the polynomial eigensolver context

    840:    Output Parameters:
    841: .  extraction - extraction technique

    843:    Level: advanced

    845: .seealso: PEPCISSSetExtraction() PEPCISSExtraction
    846: @*/
    847: PetscErrorCode PEPCISSGetExtraction(PEP pep,PEPCISSExtraction *extraction)
    848: {
    849:   PetscFunctionBegin;
    851:   PetscAssertPointer(extraction,2);
    852:   PetscUseMethod(pep,"PEPCISSGetExtraction_C",(PEP,PEPCISSExtraction*),(pep,extraction));
    853:   PetscFunctionReturn(PETSC_SUCCESS);
    854: }

    856: static PetscErrorCode PEPCISSGetKSPs_CISS(PEP pep,PetscInt *nsolve,KSP **ksp)
    857: {
    858:   PEP_CISS         *ctx = (PEP_CISS*)pep->data;
    859:   SlepcContourData contour;
    860:   PetscInt         i,nsplit;
    861:   PC               pc;
    862:   MPI_Comm         child;

    864:   PetscFunctionBegin;
    865:   if (!ctx->contour) {  /* initialize contour data structure first */
    866:     PetscCall(RGCanUseConjugates(pep->rg,ctx->isreal,&ctx->useconj));
    867:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)pep,&ctx->contour));
    868:   }
    869:   contour = ctx->contour;
    870:   if (!contour->ksp) {
    871:     PetscCall(PetscMalloc1(contour->npoints,&contour->ksp));
    872:     PetscCall(PEPGetST(pep,&pep->st));
    873:     PetscCall(STGetSplitPreconditionerInfo(pep->st,&nsplit,NULL));
    874:     PetscCall(PetscSubcommGetChild(contour->subcomm,&child));
    875:     for (i=0;i<contour->npoints;i++) {
    876:       PetscCall(KSPCreate(child,&contour->ksp[i]));
    877:       PetscCall(PetscObjectIncrementTabLevel((PetscObject)contour->ksp[i],(PetscObject)pep,1));
    878:       PetscCall(KSPSetOptionsPrefix(contour->ksp[i],((PetscObject)pep)->prefix));
    879:       PetscCall(KSPAppendOptionsPrefix(contour->ksp[i],"pep_ciss_"));
    880:       PetscCall(PetscObjectSetOptions((PetscObject)contour->ksp[i],((PetscObject)pep)->options));
    881:       PetscCall(KSPSetErrorIfNotConverged(contour->ksp[i],PETSC_TRUE));
    882:       PetscCall(KSPSetTolerances(contour->ksp[i],SlepcDefaultTol(pep->tol),PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT));
    883:       PetscCall(KSPGetPC(contour->ksp[i],&pc));
    884:       if (nsplit) {
    885:         PetscCall(KSPSetType(contour->ksp[i],KSPBCGS));
    886:         PetscCall(PCSetType(pc,PCBJACOBI));
    887:       } else {
    888:         PetscCall(KSPSetType(contour->ksp[i],KSPPREONLY));
    889:         PetscCall(PCSetType(pc,PCLU));
    890:       }
    891:     }
    892:   }
    893:   if (nsolve) *nsolve = contour->npoints;
    894:   if (ksp)    *ksp    = contour->ksp;
    895:   PetscFunctionReturn(PETSC_SUCCESS);
    896: }

    898: /*@C
    899:    PEPCISSGetKSPs - Retrieve the array of linear solver objects associated with
    900:    the CISS solver.

    902:    Collective

    904:    Input Parameter:
    905: .  pep - polynomial eigenvalue solver

    907:    Output Parameters:
    908: +  nsolve - number of solver objects
    909: -  ksp - array of linear solver object

    911:    Notes:
    912:    The number of KSP solvers is equal to the number of integration points divided by
    913:    the number of partitions. This value is halved in the case of real matrices with
    914:    a region centered at the real axis.

    916:    Level: advanced

    918: .seealso: PEPCISSSetSizes()
    919: @*/
    920: PetscErrorCode PEPCISSGetKSPs(PEP pep,PetscInt *nsolve,KSP **ksp)
    921: {
    922:   PetscFunctionBegin;
    924:   PetscUseMethod(pep,"PEPCISSGetKSPs_C",(PEP,PetscInt*,KSP**),(pep,nsolve,ksp));
    925:   PetscFunctionReturn(PETSC_SUCCESS);
    926: }

    928: static PetscErrorCode PEPReset_CISS(PEP pep)
    929: {
    930:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;

    932:   PetscFunctionBegin;
    933:   PetscCall(BVDestroy(&ctx->S));
    934:   PetscCall(BVDestroy(&ctx->V));
    935:   PetscCall(BVDestroy(&ctx->Y));
    936:   PetscCall(SlepcContourDataReset(ctx->contour));
    937:   PetscCall(MatDestroy(&ctx->J));
    938:   PetscCall(BVDestroy(&ctx->pV));
    939:   PetscCall(PetscFree(ctx->Psplit));
    940:   PetscFunctionReturn(PETSC_SUCCESS);
    941: }

    943: static PetscErrorCode PEPSetFromOptions_CISS(PEP pep,PetscOptionItems *PetscOptionsObject)
    944: {
    945:   PEP_CISS          *ctx = (PEP_CISS*)pep->data;
    946:   PetscReal         r1,r2;
    947:   PetscInt          i,i1,i2,i3,i4,i5,i6,i7;
    948:   PetscBool         b1,flg,flg2,flg3,flg4,flg5,flg6;
    949:   PEPCISSExtraction extraction;

    951:   PetscFunctionBegin;
    952:   PetscOptionsHeadBegin(PetscOptionsObject,"PEP CISS Options");

    954:     PetscCall(PEPCISSGetSizes(pep,&i1,&i2,&i3,&i4,&i5,&b1));
    955:     PetscCall(PetscOptionsInt("-pep_ciss_integration_points","Number of integration points","PEPCISSSetSizes",i1,&i1,&flg));
    956:     PetscCall(PetscOptionsInt("-pep_ciss_blocksize","Block size","PEPCISSSetSizes",i2,&i2,&flg2));
    957:     PetscCall(PetscOptionsInt("-pep_ciss_moments","Moment size","PEPCISSSetSizes",i3,&i3,&flg3));
    958:     PetscCall(PetscOptionsInt("-pep_ciss_partitions","Number of partitions","PEPCISSSetSizes",i4,&i4,&flg4));
    959:     PetscCall(PetscOptionsInt("-pep_ciss_maxblocksize","Maximum block size","PEPCISSSetSizes",i5,&i5,&flg5));
    960:     PetscCall(PetscOptionsBool("-pep_ciss_realmats","True if all coefficient matrices of P(.) are real","PEPCISSSetSizes",b1,&b1,&flg6));
    961:     if (flg || flg2 || flg3 || flg4 || flg5 || flg6) PetscCall(PEPCISSSetSizes(pep,i1,i2,i3,i4,i5,b1));

    963:     PetscCall(PEPCISSGetThreshold(pep,&r1,&r2));
    964:     PetscCall(PetscOptionsReal("-pep_ciss_delta","Threshold for numerical rank","PEPCISSSetThreshold",r1,&r1,&flg));
    965:     PetscCall(PetscOptionsReal("-pep_ciss_spurious_threshold","Threshold for the spurious eigenpairs","PEPCISSSetThreshold",r2,&r2,&flg2));
    966:     if (flg || flg2) PetscCall(PEPCISSSetThreshold(pep,r1,r2));

    968:     PetscCall(PEPCISSGetRefinement(pep,&i6,&i7));
    969:     PetscCall(PetscOptionsInt("-pep_ciss_refine_inner","Number of inner iterative refinement iterations","PEPCISSSetRefinement",i6,&i6,&flg));
    970:     PetscCall(PetscOptionsInt("-pep_ciss_refine_blocksize","Number of blocksize iterative refinement iterations","PEPCISSSetRefinement",i7,&i7,&flg2));
    971:     if (flg || flg2) PetscCall(PEPCISSSetRefinement(pep,i6,i7));

    973:     PetscCall(PetscOptionsEnum("-pep_ciss_extraction","Extraction technique","PEPCISSSetExtraction",PEPCISSExtractions,(PetscEnum)ctx->extraction,(PetscEnum*)&extraction,&flg));
    974:     if (flg) PetscCall(PEPCISSSetExtraction(pep,extraction));

    976:   PetscOptionsHeadEnd();

    978:   if (!pep->rg) PetscCall(PEPGetRG(pep,&pep->rg));
    979:   PetscCall(RGSetFromOptions(pep->rg)); /* this is necessary here to set useconj */
    980:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(PEPCISSGetKSPs(pep,NULL,NULL));
    981:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Something went wrong with PEPCISSGetKSPs()");
    982:   for (i=0;i<ctx->contour->npoints;i++) PetscCall(KSPSetFromOptions(ctx->contour->ksp[i]));
    983:   PetscCall(PetscSubcommSetFromOptions(ctx->contour->subcomm));
    984:   PetscFunctionReturn(PETSC_SUCCESS);
    985: }

    987: static PetscErrorCode PEPDestroy_CISS(PEP pep)
    988: {
    989:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;

    991:   PetscFunctionBegin;
    992:   PetscCall(SlepcContourDataDestroy(&ctx->contour));
    993:   PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
    994:   PetscCall(PetscFree(pep->data));
    995:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetSizes_C",NULL));
    996:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetSizes_C",NULL));
    997:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetThreshold_C",NULL));
    998:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetThreshold_C",NULL));
    999:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetRefinement_C",NULL));
    1000:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetRefinement_C",NULL));
    1001:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetExtraction_C",NULL));
    1002:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetExtraction_C",NULL));
    1003:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetKSPs_C",NULL));
    1004:   PetscFunctionReturn(PETSC_SUCCESS);
    1005: }

    1007: static PetscErrorCode PEPView_CISS(PEP pep,PetscViewer viewer)
    1008: {
    1009:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;
    1010:   PetscBool      isascii;
    1011:   PetscViewer    sviewer;

    1013:   PetscFunctionBegin;
    1014:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
    1015:   if (isascii) {
    1016:     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));
    1017:     if (ctx->isreal) PetscCall(PetscViewerASCIIPrintf(viewer,"  exploiting symmetry of integration points\n"));
    1018:     PetscCall(PetscViewerASCIIPrintf(viewer,"  threshold { delta: %g, spurious threshold: %g }\n",(double)ctx->delta,(double)ctx->spurious_threshold));
    1019:     PetscCall(PetscViewerASCIIPrintf(viewer,"  iterative refinement  { inner: %" PetscInt_FMT ", blocksize: %" PetscInt_FMT " }\n",ctx->refine_inner, ctx->refine_blocksize));
    1020:     PetscCall(PetscViewerASCIIPrintf(viewer,"  extraction: %s\n",PEPCISSExtractions[ctx->extraction]));
    1021:     if (!ctx->contour || !ctx->contour->ksp) PetscCall(PEPCISSGetKSPs(pep,NULL,NULL));
    1022:     PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Something went wrong with PEPCISSGetKSPs()");
    1023:     PetscCall(PetscViewerASCIIPushTab(viewer));
    1024:     if (ctx->npart>1 && ctx->contour->subcomm) {
    1025:       PetscCall(PetscViewerGetSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
    1026:       if (!ctx->contour->subcomm->color) PetscCall(KSPView(ctx->contour->ksp[0],sviewer));
    1027:       PetscCall(PetscViewerFlush(sviewer));
    1028:       PetscCall(PetscViewerRestoreSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
    1029:       /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
    1030:       PetscCall(PetscViewerASCIIPopSynchronized(viewer));
    1031:     } else PetscCall(KSPView(ctx->contour->ksp[0],viewer));
    1032:     PetscCall(PetscViewerASCIIPopTab(viewer));
    1033:   }
    1034:   PetscFunctionReturn(PETSC_SUCCESS);
    1035: }

    1037: static PetscErrorCode PEPSetDSType_CISS(PEP pep)
    1038: {
    1039:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;

    1041:   PetscFunctionBegin;
    1042:   switch (ctx->extraction) {
    1043:     case PEP_CISS_EXTRACTION_RITZ:   PetscCall(DSSetType(pep->ds,DSPEP)); break;
    1044:     case PEP_CISS_EXTRACTION_HANKEL: PetscCall(DSSetType(pep->ds,DSGNHEP)); break;
    1045:     case PEP_CISS_EXTRACTION_CAA:    PetscCall(DSSetType(pep->ds,DSNHEP)); break;
    1046:   }
    1047:   PetscFunctionReturn(PETSC_SUCCESS);
    1048: }

    1050: SLEPC_EXTERN PetscErrorCode PEPCreate_CISS(PEP pep)
    1051: {
    1052:   PEP_CISS       *ctx = (PEP_CISS*)pep->data;

    1054:   PetscFunctionBegin;
    1055:   PetscCall(PetscNew(&ctx));
    1056:   pep->data = ctx;
    1057:   /* set default values of parameters */
    1058:   ctx->N                  = 32;
    1059:   ctx->L                  = 16;
    1060:   ctx->M                  = ctx->N/4;
    1061:   ctx->delta              = SLEPC_DEFAULT_TOL*1e-4;
    1062:   ctx->L_max              = 64;
    1063:   ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
    1064:   ctx->isreal             = PETSC_FALSE;
    1065:   ctx->npart              = 1;

    1067:   pep->ops->solve          = PEPSolve_CISS;
    1068:   pep->ops->setup          = PEPSetUp_CISS;
    1069:   pep->ops->setfromoptions = PEPSetFromOptions_CISS;
    1070:   pep->ops->reset          = PEPReset_CISS;
    1071:   pep->ops->destroy        = PEPDestroy_CISS;
    1072:   pep->ops->view           = PEPView_CISS;
    1073:   pep->ops->setdstype      = PEPSetDSType_CISS;

    1075:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetSizes_C",PEPCISSSetSizes_CISS));
    1076:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetSizes_C",PEPCISSGetSizes_CISS));
    1077:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetThreshold_C",PEPCISSSetThreshold_CISS));
    1078:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetThreshold_C",PEPCISSGetThreshold_CISS));
    1079:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetRefinement_C",PEPCISSSetRefinement_CISS));
    1080:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetRefinement_C",PEPCISSGetRefinement_CISS));
    1081:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSSetExtraction_C",PEPCISSSetExtraction_CISS));
    1082:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetExtraction_C",PEPCISSGetExtraction_CISS));
    1083:   PetscCall(PetscObjectComposeFunction((PetscObject)pep,"PEPCISSGetKSPs_C",PEPCISSGetKSPs_CISS));
    1084:   PetscFunctionReturn(PETSC_SUCCESS);
    1085: }