Actual source code: ciss.c

  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] T. Sakurai and H. Sugiura, "A projection method for generalized
 26:            eigenvalue problems", J. Comput. Appl. Math. 159:119-128, 2003.

 28:        [2] T. Sakurai and H. Tadano, "CIRR: a Rayleigh-Ritz type method with
 29:            contour integral for generalized eigenvalue problems", Hokkaido
 30:            Math. J. 36:745-757, 2007.
 31: */

 33: #include <slepc/private/epsimpl.h>
 34: #include <slepc/private/slepccontour.h>
 35: #include <slepcblaslapack.h>

 37: typedef struct {
 38:   /* user parameters */
 39:   PetscInt          N;          /* number of integration points (32) */
 40:   PetscInt          L;          /* block size (16) */
 41:   PetscInt          M;          /* moment degree (N/4 = 4) */
 42:   PetscReal         delta;      /* threshold of singular value (1e-12) */
 43:   PetscInt          L_max;      /* maximum number of columns of the source matrix V */
 44:   PetscReal         spurious_threshold; /* discard spurious eigenpairs */
 45:   PetscBool         isreal;     /* A and B are real */
 46:   PetscInt          npart;      /* number of partitions */
 47:   PetscInt          refine_inner;
 48:   PetscInt          refine_blocksize;
 49:   EPSCISSQuadRule   quad;
 50:   EPSCISSExtraction extraction;
 51:   PetscBool         usest;
 52:   /* private data */
 53:   SlepcContourData  contour;
 54:   PetscReal         *sigma;     /* threshold for numerical rank */
 55:   PetscScalar       *weight;
 56:   PetscScalar       *omega;
 57:   PetscScalar       *pp;
 58:   BV                V;
 59:   BV                S;
 60:   BV                pV;
 61:   BV                Y;
 62:   PetscBool         useconj;
 63:   PetscBool         usest_set;  /* whether the user set the usest flag or not */
 64:   PetscObjectId     rgid;
 65:   PetscObjectState  rgstate;
 66: } EPS_CISS;

 68: /*
 69:   Set up KSP solvers for every integration point, only called if !ctx->usest
 70: */
 71: static PetscErrorCode EPSCISSSetUp(EPS eps,Mat A,Mat B,Mat Pa,Mat Pb)
 72: {
 73:   EPS_CISS         *ctx = (EPS_CISS*)eps->data;
 74:   SlepcContourData contour;
 75:   PetscInt         i,p_id,nsplit;
 76:   Mat              Amat,Pmat;
 77:   MatStructure     str,strp;

 79:   PetscFunctionBegin;
 80:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(EPSCISSGetKSPs(eps,NULL,NULL));
 81:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)eps),PETSC_ERR_PLIB,"Something went wrong with EPSCISSGetKSPs()");
 82:   contour = ctx->contour;
 83:   PetscCall(STGetMatStructure(eps->st,&str));
 84:   PetscCall(STGetSplitPreconditionerInfo(eps->st,&nsplit,&strp));
 85:   for (i=0;i<contour->npoints;i++) {
 86:     p_id = i*contour->subcomm->n + contour->subcomm->color;
 87:     PetscCall(MatDuplicate(A,MAT_COPY_VALUES,&Amat));
 88:     if (B) PetscCall(MatAXPY(Amat,-ctx->omega[p_id],B,str));
 89:     else PetscCall(MatShift(Amat,-ctx->omega[p_id]));
 90:     if (nsplit) {
 91:       PetscCall(MatDuplicate(Pa,MAT_COPY_VALUES,&Pmat));
 92:       if (Pb) PetscCall(MatAXPY(Pmat,-ctx->omega[p_id],Pb,strp));
 93:       else PetscCall(MatShift(Pmat,-ctx->omega[p_id]));
 94:     } else Pmat = Amat;
 95:     PetscCall(EPS_KSPSetOperators(contour->ksp[i],Amat,Amat));
 96:     PetscCall(MatDestroy(&Amat));
 97:     if (nsplit) PetscCall(MatDestroy(&Pmat));
 98:   }
 99:   PetscFunctionReturn(PETSC_SUCCESS);
100: }

102: /*
103:   Y_i = (A-z_i B)^{-1}BV for every integration point, Y=[Y_i] is in the context
104: */
105: static PetscErrorCode EPSCISSSolve(EPS eps,Mat B,BV V,PetscInt L_start,PetscInt L_end)
106: {
107:   EPS_CISS         *ctx = (EPS_CISS*)eps->data;
108:   SlepcContourData contour;
109:   PetscInt         i,p_id;
110:   Mat              MV,BMV=NULL,MC;
111:   KSP              ksp;

113:   PetscFunctionBegin;
114:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(EPSCISSGetKSPs(eps,NULL,NULL));
115:   contour = ctx->contour;
116:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)eps),PETSC_ERR_PLIB,"Something went wrong with EPSCISSGetKSPs()");
117:   PetscCall(BVSetActiveColumns(V,L_start,L_end));
118:   PetscCall(BVGetMat(V,&MV));
119:   for (i=0;i<contour->npoints;i++) {
120:     p_id = i*contour->subcomm->n + contour->subcomm->color;
121:     if (ctx->usest)  {
122:       PetscCall(STSetShift(eps->st,ctx->omega[p_id]));
123:       PetscCall(STGetKSP(eps->st,&ksp));
124:     } else ksp = contour->ksp[i];
125:     PetscCall(BVSetActiveColumns(ctx->Y,i*ctx->L+L_start,i*ctx->L+L_end));
126:     PetscCall(BVGetMat(ctx->Y,&MC));
127:     if (B) {
128:       if (!i) {
129:         PetscCall(MatProductCreate(B,MV,NULL,&BMV));
130:         PetscCall(MatProductSetType(BMV,MATPRODUCT_AB));
131:         PetscCall(MatProductSetFromOptions(BMV));
132:         PetscCall(MatProductSymbolic(BMV));
133:       }
134:       PetscCall(MatProductNumeric(BMV));
135:       PetscCall(KSPMatSolve(ksp,BMV,MC));
136:     } else PetscCall(KSPMatSolve(ksp,MV,MC));
137:     PetscCall(BVRestoreMat(ctx->Y,&MC));
138:     if (ctx->usest && i<contour->npoints-1) PetscCall(KSPReset(ksp));
139:   }
140:   PetscCall(MatDestroy(&BMV));
141:   PetscCall(BVRestoreMat(V,&MV));
142:   PetscFunctionReturn(PETSC_SUCCESS);
143: }

145: static PetscErrorCode rescale_eig(EPS eps,PetscInt nv)
146: {
147:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
148:   PetscInt       i;
149:   PetscScalar    center;
150:   PetscReal      radius,a,b,c,d,rgscale;
151: #if defined(PETSC_USE_COMPLEX)
152:   PetscReal      start_ang,end_ang,vscale,theta;
153: #endif
154:   PetscBool      isring,isellipse,isinterval;

156:   PetscFunctionBegin;
157:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse));
158:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring));
159:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval));
160:   PetscCall(RGGetScale(eps->rg,&rgscale));
161:   if (isinterval) {
162:     PetscCall(RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d));
163:     if (c==d) {
164:       for (i=0;i<nv;i++) {
165: #if defined(PETSC_USE_COMPLEX)
166:         eps->eigr[i] = PetscRealPart(eps->eigr[i]);
167: #else
168:         eps->eigi[i] = 0;
169: #endif
170:       }
171:     }
172:   }
173:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
174:     if (isellipse) {
175:       PetscCall(RGEllipseGetParameters(eps->rg,&center,&radius,NULL));
176:       for (i=0;i<nv;i++) eps->eigr[i] = rgscale*(center + radius*eps->eigr[i]);
177:     } else if (isinterval) {
178:       PetscCall(RGIntervalGetEndpoints(eps->rg,&a,&b,&c,&d));
179:       if (ctx->quad == EPS_CISS_QUADRULE_CHEBYSHEV) {
180:         for (i=0;i<nv;i++) {
181:           if (c==d) eps->eigr[i] = ((eps->eigr[i]+1.0)*(b-a)/2.0+a)*rgscale;
182:           if (a==b) {
183: #if defined(PETSC_USE_COMPLEX)
184:             eps->eigr[i] = ((eps->eigr[i]+1.0)*(d-c)/2.0+c)*rgscale*PETSC_i;
185: #else
186:             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Integration points on a vertical line require complex arithmetic");
187: #endif
188:           }
189:         }
190:       } else {
191:         center = (b+a)/2.0+(d+c)/2.0*PETSC_PI;
192:         radius = PetscSqrtReal(PetscPowRealInt((b-a)/2.0,2)+PetscPowRealInt((d-c)/2.0,2));
193:         for (i=0;i<nv;i++) eps->eigr[i] = center + radius*eps->eigr[i];
194:       }
195:     } else if (isring) {  /* only supported in complex scalars */
196: #if defined(PETSC_USE_COMPLEX)
197:       PetscCall(RGRingGetParameters(eps->rg,&center,&radius,&vscale,&start_ang,&end_ang,NULL));
198:       if (ctx->quad == EPS_CISS_QUADRULE_CHEBYSHEV) {
199:         for (i=0;i<nv;i++) {
200:           theta = (start_ang*2.0+(end_ang-start_ang)*(PetscRealPart(eps->eigr[i])+1.0))*PETSC_PI;
201:           eps->eigr[i] = rgscale*center + (rgscale*radius+PetscImaginaryPart(eps->eigr[i]))*PetscCMPLX(PetscCosReal(theta),vscale*PetscSinReal(theta));
202:         }
203:       } else {
204:         for (i=0;i<nv;i++) eps->eigr[i] = rgscale*(center + radius*eps->eigr[i]);
205:       }
206: #endif
207:     }
208:   }
209:   PetscFunctionReturn(PETSC_SUCCESS);
210: }

212: static PetscErrorCode EPSSetUp_CISS(EPS eps)
213: {
214:   EPS_CISS         *ctx = (EPS_CISS*)eps->data;
215:   SlepcContourData contour;
216:   PetscBool        istrivial,isring,isellipse,isinterval,flg;
217:   PetscReal        c,d;
218:   PetscInt         nsplit;
219:   PetscRandom      rand;
220:   PetscObjectId    id;
221:   PetscObjectState state;
222:   Mat              A[2],Psplit[2];
223:   Vec              v0;

225:   PetscFunctionBegin;
226:   EPSCheckNotStructured(eps);
227:   if (eps->ncv==PETSC_DETERMINE) {
228:     eps->ncv = ctx->L_max*ctx->M;
229:     if (eps->ncv>eps->n) {
230:       eps->ncv = eps->n;
231:       ctx->L_max = eps->ncv/ctx->M;
232:       PetscCheck(ctx->L_max,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Cannot adjust solver parameters, try setting a smaller value of M (moment size)");
233:     }
234:   } else {
235:     PetscCall(EPSSetDimensions_Default(eps,&eps->nev,&eps->ncv,&eps->mpd));
236:     ctx->L_max = eps->ncv/ctx->M;
237:     if (!ctx->L_max) {
238:       ctx->L_max = 1;
239:       eps->ncv = ctx->L_max*ctx->M;
240:     }
241:   }
242:   ctx->L = PetscMin(ctx->L,ctx->L_max);
243:   if (eps->max_it==PETSC_DETERMINE) eps->max_it = 5;
244:   if (eps->mpd==PETSC_DETERMINE) eps->mpd = eps->ncv;
245:   if (!eps->which) eps->which = EPS_ALL;
246:   PetscCheck(eps->which==EPS_ALL,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver supports only computing all eigenvalues");
247:   EPSCheckUnsupported(eps,EPS_FEATURE_BALANCE | EPS_FEATURE_ARBITRARY | EPS_FEATURE_EXTRACTION | EPS_FEATURE_STOPPING | EPS_FEATURE_TWOSIDED);

249:   /* check region */
250:   PetscCall(RGIsTrivial(eps->rg,&istrivial));
251:   PetscCheck(!istrivial,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"CISS requires a nontrivial region, e.g. -rg_type ellipse ...");
252:   PetscCall(RGGetComplement(eps->rg,&flg));
253:   PetscCheck(!flg,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"A region with complement flag set is not allowed");
254:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse));
255:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring));
256:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval));
257:   PetscCheck(isellipse || isring || isinterval,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Currently only implemented for interval, elliptic or ring regions");

259:   /* if the region has changed, then reset contour data */
260:   PetscCall(PetscObjectGetId((PetscObject)eps->rg,&id));
261:   PetscCall(PetscObjectStateGet((PetscObject)eps->rg,&state));
262:   if (ctx->rgid && (id != ctx->rgid || state != ctx->rgstate)) {
263:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
264:     PetscCall(PetscInfo(eps,"Resetting the contour data structure due to a change of region\n"));
265:     ctx->rgid = id; ctx->rgstate = state;
266:   }

268: #if !defined(PETSC_USE_COMPLEX)
269:   PetscCheck(!isring,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Ring region only supported for complex scalars");
270: #endif
271:   if (isinterval) {
272:     PetscCall(RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d));
273: #if !defined(PETSC_USE_COMPLEX)
274:     PetscCheck(c==d && c==0.0,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"In real scalars, endpoints of the imaginary axis must be both zero");
275: #endif
276:     if (!ctx->quad && c==d) ctx->quad = EPS_CISS_QUADRULE_CHEBYSHEV;
277:   }
278:   if (!ctx->quad) ctx->quad = EPS_CISS_QUADRULE_TRAPEZOIDAL;

280:   /* create contour data structure */
281:   if (!ctx->contour) {
282:     PetscCall(RGCanUseConjugates(eps->rg,ctx->isreal,&ctx->useconj));
283:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)eps,&ctx->contour));
284:   }

286:   PetscCall(EPSAllocateSolution(eps,0));
287:   PetscCall(BVGetRandomContext(eps->V,&rand));  /* make sure the random context is available when duplicating */
288:   if (ctx->weight) PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
289:   PetscCall(PetscMalloc4(ctx->N,&ctx->weight,ctx->N+1,&ctx->omega,ctx->N,&ctx->pp,ctx->L_max*ctx->M,&ctx->sigma));

291:   /* allocate basis vectors */
292:   PetscCall(BVDestroy(&ctx->S));
293:   PetscCall(BVDuplicateResize(eps->V,ctx->L*ctx->M,&ctx->S));
294:   PetscCall(BVDestroy(&ctx->V));
295:   PetscCall(BVDuplicateResize(eps->V,ctx->L,&ctx->V));

297:   PetscCall(STGetMatrix(eps->st,0,&A[0]));
298:   PetscCall(MatIsShell(A[0],&flg));
299:   PetscCheck(!flg,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Matrix type shell is not supported in this solver");
300:   if (eps->isgeneralized) PetscCall(STGetMatrix(eps->st,1,&A[1]));

302:   if (!ctx->usest_set) ctx->usest = (ctx->npart>1)? PETSC_FALSE: PETSC_TRUE;
303:   PetscCheck(!ctx->usest || ctx->npart==1,PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The usest flag is not supported when partitions > 1");

305:   /* check if a user-defined split preconditioner has been set */
306:   PetscCall(STGetSplitPreconditionerInfo(eps->st,&nsplit,NULL));
307:   if (nsplit) {
308:     PetscCall(STGetSplitPreconditionerTerm(eps->st,0,&Psplit[0]));
309:     if (eps->isgeneralized) PetscCall(STGetSplitPreconditionerTerm(eps->st,1,&Psplit[1]));
310:   }

312:   contour = ctx->contour;
313:   PetscCall(SlepcContourRedundantMat(contour,eps->isgeneralized?2:1,A,nsplit?Psplit:NULL));
314:   if (contour->pA) {
315:     PetscCall(BVGetColumn(ctx->V,0,&v0));
316:     PetscCall(SlepcContourScatterCreate(contour,v0));
317:     PetscCall(BVRestoreColumn(ctx->V,0,&v0));
318:     PetscCall(BVDestroy(&ctx->pV));
319:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->pV));
320:     PetscCall(BVSetSizesFromVec(ctx->pV,contour->xsub,eps->n));
321:     PetscCall(BVSetFromOptions(ctx->pV));
322:     PetscCall(BVResize(ctx->pV,ctx->L,PETSC_FALSE));
323:   }

325:   EPSCheckDefinite(eps);
326:   EPSCheckSinvertCondition(eps,ctx->usest," (with the usest flag set)");

328:   PetscCall(BVDestroy(&ctx->Y));
329:   if (contour->pA) {
330:     PetscCall(BVCreate(PetscObjectComm((PetscObject)contour->xsub),&ctx->Y));
331:     PetscCall(BVSetSizesFromVec(ctx->Y,contour->xsub,eps->n));
332:     PetscCall(BVSetFromOptions(ctx->Y));
333:     PetscCall(BVResize(ctx->Y,contour->npoints*ctx->L,PETSC_FALSE));
334:   } else PetscCall(BVDuplicateResize(eps->V,contour->npoints*ctx->L,&ctx->Y));

336:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) PetscCall(DSSetType(eps->ds,DSGNHEP));
337:   else if (eps->isgeneralized) {
338:     if (eps->ishermitian && eps->ispositive) PetscCall(DSSetType(eps->ds,DSGHEP));
339:     else PetscCall(DSSetType(eps->ds,DSGNHEP));
340:   } else {
341:     if (eps->ishermitian) PetscCall(DSSetType(eps->ds,DSHEP));
342:     else PetscCall(DSSetType(eps->ds,DSNHEP));
343:   }
344:   PetscCall(DSAllocate(eps->ds,eps->ncv));

346: #if !defined(PETSC_USE_COMPLEX)
347:   PetscCall(EPSSetWorkVecs(eps,3));
348:   if (!eps->ishermitian) PetscCall(PetscInfo(eps,"Warning: complex eigenvalues are not calculated exactly without --with-scalar-type=complex in PETSc\n"));
349: #else
350:   PetscCall(EPSSetWorkVecs(eps,2));
351: #endif
352:   PetscFunctionReturn(PETSC_SUCCESS);
353: }

355: static PetscErrorCode EPSSetUpSort_CISS(EPS eps)
356: {
357:   SlepcSC        sc;

359:   PetscFunctionBegin;
360:   /* fill sorting criterion context */
361:   eps->sc->comparison    = SlepcCompareSmallestReal;
362:   eps->sc->comparisonctx = NULL;
363:   eps->sc->map           = NULL;
364:   eps->sc->mapobj        = NULL;

366:   /* fill sorting criterion for DS */
367:   PetscCall(DSGetSlepcSC(eps->ds,&sc));
368:   sc->comparison    = SlepcCompareLargestMagnitude;
369:   sc->comparisonctx = NULL;
370:   sc->map           = NULL;
371:   sc->mapobj        = NULL;
372:   PetscFunctionReturn(PETSC_SUCCESS);
373: }

375: static PetscErrorCode EPSSolve_CISS(EPS eps)
376: {
377:   EPS_CISS         *ctx = (EPS_CISS*)eps->data;
378:   SlepcContourData contour = ctx->contour;
379:   Mat              A,B,X,M,pA,pB,T,J,Pa=NULL,Pb=NULL;
380:   BV               V;
381:   PetscInt         i,j,ld,nmat,L_add=0,nv=0,L_base=ctx->L,inner,nlocal,*inside,nsplit;
382:   PetscScalar      *Mu,*H0,*H1=NULL,*rr,*temp;
383:   PetscReal        error,max_error,norm;
384:   PetscBool        *fl1;
385:   Vec              si,si1=NULL,w[3];
386:   PetscRandom      rand;
387: #if defined(PETSC_USE_COMPLEX)
388:   PetscBool        isellipse;
389:   PetscReal        est_eig,eta;
390: #else
391:   PetscReal        normi;
392: #endif

394:   PetscFunctionBegin;
395:   w[0] = eps->work[0];
396: #if defined(PETSC_USE_COMPLEX)
397:   w[1] = NULL;
398: #else
399:   w[1] = eps->work[2];
400: #endif
401:   w[2] = eps->work[1];
402:   PetscCall(VecGetLocalSize(w[0],&nlocal));
403:   PetscCall(DSGetLeadingDimension(eps->ds,&ld));
404:   PetscCall(RGComputeQuadrature(eps->rg,ctx->quad==EPS_CISS_QUADRULE_CHEBYSHEV?RG_QUADRULE_CHEBYSHEV:RG_QUADRULE_TRAPEZOIDAL,ctx->N,ctx->omega,ctx->pp,ctx->weight));
405:   PetscCall(STGetNumMatrices(eps->st,&nmat));
406:   PetscCall(STGetMatrix(eps->st,0,&A));
407:   if (nmat>1) PetscCall(STGetMatrix(eps->st,1,&B));
408:   else B = NULL;
409:   J = (contour->pA && nmat>1)? contour->pA[1]: B;
410:   V = contour->pA? ctx->pV: ctx->V;
411:   if (!ctx->usest) {
412:     T = contour->pA? contour->pA[0]: A;
413:     PetscCall(STGetSplitPreconditionerInfo(eps->st,&nsplit,NULL));
414:     if (nsplit) {
415:       if (contour->pA) {
416:         Pa = contour->pP[0];
417:         if (nsplit>1) Pb = contour->pP[1];
418:       } else {
419:         PetscCall(STGetSplitPreconditionerTerm(eps->st,0,&Pa));
420:         if (nsplit>1) PetscCall(STGetSplitPreconditionerTerm(eps->st,1,&Pb));
421:       }
422:     }
423:     PetscCall(EPSCISSSetUp(eps,T,J,Pa,Pb));
424:   }
425:   PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
426:   PetscCall(BVSetRandomSign(ctx->V));
427:   PetscCall(BVGetRandomContext(ctx->V,&rand));

429:   if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
430:   PetscCall(EPSCISSSolve(eps,J,V,0,ctx->L));
431: #if defined(PETSC_USE_COMPLEX)
432:   PetscCall(PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse));
433:   if (isellipse) {
434:     PetscCall(BVTraceQuadrature(ctx->Y,ctx->V,ctx->L,ctx->L,ctx->weight,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj,&est_eig));
435:     PetscCall(PetscInfo(eps,"Estimated eigenvalue count: %f\n",(double)est_eig));
436:     eta = PetscPowReal(10.0,-PetscLog10Real(eps->tol)/ctx->N);
437:     L_add = PetscMax(0,(PetscInt)PetscCeilReal((est_eig*eta)/ctx->M)-ctx->L);
438:     if (L_add>ctx->L_max-ctx->L) {
439:       PetscCall(PetscInfo(eps,"Number of eigenvalues inside the contour path may be too large\n"));
440:       L_add = ctx->L_max-ctx->L;
441:     }
442:   }
443: #endif
444:   if (L_add>0) {
445:     PetscCall(PetscInfo(eps,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by Estimate #Eig\n",ctx->L,ctx->L+L_add));
446:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
447:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
448:     PetscCall(BVSetRandomSign(ctx->V));
449:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
450:     ctx->L += L_add;
451:     PetscCall(EPSCISSSolve(eps,J,V,ctx->L-L_add,ctx->L));
452:   }
453:   PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
454:   for (i=0;i<ctx->refine_blocksize;i++) {
455:     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));
456:     PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
457:     PetscCall(PetscLogEventBegin(EPS_CISS_SVD,eps,0,0,0));
458:     PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
459:     PetscCall(PetscLogEventEnd(EPS_CISS_SVD,eps,0,0,0));
460:     if (ctx->sigma[0]<=ctx->delta || nv < ctx->L*ctx->M || ctx->L == ctx->L_max) break;
461:     L_add = L_base;
462:     if (ctx->L+L_add>ctx->L_max) L_add = ctx->L_max-ctx->L;
463:     PetscCall(PetscInfo(eps,"Changing L %" PetscInt_FMT " -> %" PetscInt_FMT " by SVD(H0)\n",ctx->L,ctx->L+L_add));
464:     PetscCall(BVCISSResizeBases(ctx->S,contour->pA?ctx->pV:ctx->V,ctx->Y,ctx->L,ctx->L+L_add,ctx->M,contour->npoints));
465:     PetscCall(BVSetActiveColumns(ctx->V,ctx->L,ctx->L+L_add));
466:     PetscCall(BVSetRandomSign(ctx->V));
467:     if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
468:     ctx->L += L_add;
469:     PetscCall(EPSCISSSolve(eps,J,V,ctx->L-L_add,ctx->L));
470:     if (L_add) {
471:       PetscCall(PetscFree2(Mu,H0));
472:       PetscCall(PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0));
473:     }
474:   }
475:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) PetscCall(PetscMalloc1(ctx->L*ctx->M*ctx->L*ctx->M,&H1));

477:   while (eps->reason == EPS_CONVERGED_ITERATING) {
478:     eps->its++;
479:     for (inner=0;inner<=ctx->refine_inner;inner++) {
480:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
481:         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));
482:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
483:         PetscCall(PetscLogEventBegin(EPS_CISS_SVD,eps,0,0,0));
484:         PetscCall(SlepcCISS_BH_SVD(H0,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv));
485:         PetscCall(PetscLogEventEnd(EPS_CISS_SVD,eps,0,0,0));
486:         break;
487:       } else {
488:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
489:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
490:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
491:         PetscCall(BVCopy(ctx->S,ctx->V));
492:         PetscCall(BVSVDAndRank(ctx->S,ctx->M,ctx->L,ctx->delta,BV_SVD_METHOD_REFINE,H0,ctx->sigma,&nv));
493:         if (ctx->sigma[0]>ctx->delta && nv==ctx->L*ctx->M && inner!=ctx->refine_inner) {
494:           if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
495:           PetscCall(EPSCISSSolve(eps,J,V,0,ctx->L));
496:         } else break;
497:       }
498:     }
499:     eps->nconv = 0;
500:     if (nv == 0) eps->reason = EPS_CONVERGED_TOL;
501:     else {
502:       PetscCall(DSSetDimensions(eps->ds,nv,0,0));
503:       PetscCall(DSSetState(eps->ds,DS_STATE_RAW));

505:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
506:         PetscCall(CISS_BlockHankel(Mu,0,ctx->L,ctx->M,H0));
507:         PetscCall(CISS_BlockHankel(Mu,1,ctx->L,ctx->M,H1));
508:         PetscCall(DSGetArray(eps->ds,DS_MAT_A,&temp));
509:         for (j=0;j<nv;j++) {
510:           for (i=0;i<nv;i++) {
511:             temp[i+j*ld] = H1[i+j*ctx->L*ctx->M];
512:           }
513:         }
514:         PetscCall(DSRestoreArray(eps->ds,DS_MAT_A,&temp));
515:         PetscCall(DSGetArray(eps->ds,DS_MAT_B,&temp));
516:         for (j=0;j<nv;j++) {
517:           for (i=0;i<nv;i++) {
518:             temp[i+j*ld] = H0[i+j*ctx->L*ctx->M];
519:           }
520:         }
521:         PetscCall(DSRestoreArray(eps->ds,DS_MAT_B,&temp));
522:       } else {
523:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
524:         PetscCall(DSGetMat(eps->ds,DS_MAT_A,&pA));
525:         PetscCall(MatZeroEntries(pA));
526:         PetscCall(BVMatProject(ctx->S,A,ctx->S,pA));
527:         PetscCall(DSRestoreMat(eps->ds,DS_MAT_A,&pA));
528:         if (B) {
529:           PetscCall(DSGetMat(eps->ds,DS_MAT_B,&pB));
530:           PetscCall(MatZeroEntries(pB));
531:           PetscCall(BVMatProject(ctx->S,B,ctx->S,pB));
532:           PetscCall(DSRestoreMat(eps->ds,DS_MAT_B,&pB));
533:         }
534:       }

536:       PetscCall(DSSolve(eps->ds,eps->eigr,eps->eigi));
537:       PetscCall(DSSynchronize(eps->ds,eps->eigr,eps->eigi));

539:       PetscCall(PetscMalloc3(nv,&fl1,nv,&inside,nv,&rr));
540:       PetscCall(rescale_eig(eps,nv));
541:       PetscCall(DSVectors(eps->ds,DS_MAT_X,NULL,NULL));
542:       PetscCall(DSGetMat(eps->ds,DS_MAT_X,&X));
543:       PetscCall(SlepcCISS_isGhost(X,nv,ctx->sigma,ctx->spurious_threshold,fl1));
544:       PetscCall(DSRestoreMat(eps->ds,DS_MAT_X,&X));
545:       PetscCall(RGCheckInside(eps->rg,nv,eps->eigr,eps->eigi,inside));
546:       for (i=0;i<nv;i++) {
547:         if (fl1[i] && inside[i]>=0) {
548:           rr[i] = 1.0;
549:           eps->nconv++;
550:         } else rr[i] = 0.0;
551:       }
552:       PetscCall(DSSort(eps->ds,eps->eigr,eps->eigi,rr,NULL,&eps->nconv));
553:       PetscCall(DSSynchronize(eps->ds,eps->eigr,eps->eigi));
554:       PetscCall(rescale_eig(eps,nv));
555:       PetscCall(PetscFree3(fl1,inside,rr));
556:       PetscCall(BVSetActiveColumns(eps->V,0,nv));
557:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
558:         PetscCall(BVSumQuadrature(ctx->S,ctx->Y,ctx->M,ctx->L,ctx->L,ctx->weight,ctx->pp,contour->scatterin,contour->subcomm,contour->npoints,ctx->useconj));
559:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
560:         PetscCall(BVCopy(ctx->S,ctx->V));
561:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
562:       }
563:       PetscCall(BVCopy(ctx->S,eps->V));

565:       PetscCall(DSVectors(eps->ds,DS_MAT_X,NULL,NULL));
566:       PetscCall(DSGetMat(eps->ds,DS_MAT_X,&X));
567:       PetscCall(BVMultInPlace(ctx->S,X,0,eps->nconv));
568:       if (eps->ishermitian) PetscCall(BVMultInPlace(eps->V,X,0,eps->nconv));
569:       PetscCall(DSRestoreMat(eps->ds,DS_MAT_X,&X));
570:       max_error = 0.0;
571:       for (i=0;i<eps->nconv;i++) {
572:         PetscCall(BVGetColumn(ctx->S,i,&si));
573: #if !defined(PETSC_USE_COMPLEX)
574:         if (eps->eigi[i]!=0.0) PetscCall(BVGetColumn(ctx->S,i+1,&si1));
575: #endif
576:         PetscCall(EPSComputeResidualNorm_Private(eps,PETSC_FALSE,eps->eigr[i],eps->eigi[i],si,si1,w,&error));
577:         if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {  /* vector is not normalized */
578:           PetscCall(VecNorm(si,NORM_2,&norm));
579: #if !defined(PETSC_USE_COMPLEX)
580:           if (eps->eigi[i]!=0.0) {
581:             PetscCall(VecNorm(si1,NORM_2,&normi));
582:             norm = SlepcAbsEigenvalue(norm,normi);
583:           }
584: #endif
585:           error /= norm;
586:         }
587:         PetscCall((*eps->converged)(eps,eps->eigr[i],eps->eigi[i],error,&error,eps->convergedctx));
588:         PetscCall(BVRestoreColumn(ctx->S,i,&si));
589: #if !defined(PETSC_USE_COMPLEX)
590:         if (eps->eigi[i]!=0.0) {
591:           PetscCall(BVRestoreColumn(ctx->S,i+1,&si1));
592:           i++;
593:         }
594: #endif
595:         max_error = PetscMax(max_error,error);
596:       }

598:       if (max_error <= eps->tol) eps->reason = EPS_CONVERGED_TOL;
599:       else if (eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
600:       else {
601:         if (eps->nconv > ctx->L) nv = eps->nconv;
602:         else if (ctx->L > nv) nv = ctx->L;
603:         nv = PetscMin(nv,ctx->L*ctx->M);
604:         PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,nv,ctx->L,NULL,&M));
605:         PetscCall(MatSetRandom(M,rand));
606:         PetscCall(BVSetActiveColumns(ctx->S,0,nv));
607:         PetscCall(BVMultInPlace(ctx->S,M,0,ctx->L));
608:         PetscCall(MatDestroy(&M));
609:         PetscCall(BVSetActiveColumns(ctx->S,0,ctx->L));
610:         PetscCall(BVSetActiveColumns(ctx->V,0,ctx->L));
611:         PetscCall(BVCopy(ctx->S,ctx->V));
612:         if (contour->pA) PetscCall(BVScatter(ctx->V,ctx->pV,contour->scatterin,contour->xdup));
613:         PetscCall(EPSCISSSolve(eps,J,V,0,ctx->L));
614:       }
615:     }
616:   }
617:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) PetscCall(PetscFree(H1));
618:   PetscCall(PetscFree2(Mu,H0));
619:   PetscFunctionReturn(PETSC_SUCCESS);
620: }

622: static PetscErrorCode EPSComputeVectors_CISS(EPS eps)
623: {
624:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
625:   PetscInt       n;
626:   Mat            Z,B=NULL;

628:   PetscFunctionBegin;
629:   if (eps->ishermitian) {
630:     if (eps->isgeneralized && !eps->ispositive) PetscCall(EPSComputeVectors_Indefinite(eps));
631:     else PetscCall(EPSComputeVectors_Hermitian(eps));
632:     if (eps->isgeneralized && eps->ispositive && ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
633:       /* normalize to have unit B-norm */
634:       PetscCall(STGetMatrix(eps->st,1,&B));
635:       PetscCall(BVSetMatrix(eps->V,B,PETSC_FALSE));
636:       PetscCall(BVNormalize(eps->V,NULL));
637:       PetscCall(BVSetMatrix(eps->V,NULL,PETSC_FALSE));
638:     }
639:     PetscFunctionReturn(PETSC_SUCCESS);
640:   }
641:   PetscCall(DSGetDimensions(eps->ds,&n,NULL,NULL,NULL));
642:   PetscCall(BVSetActiveColumns(eps->V,0,n));

644:   /* right eigenvectors */
645:   PetscCall(DSVectors(eps->ds,DS_MAT_X,NULL,NULL));

647:   /* V = V * Z */
648:   PetscCall(DSGetMat(eps->ds,DS_MAT_X,&Z));
649:   PetscCall(BVMultInPlace(eps->V,Z,0,n));
650:   PetscCall(DSRestoreMat(eps->ds,DS_MAT_X,&Z));
651:   PetscCall(BVSetActiveColumns(eps->V,0,eps->nconv));

653:   /* normalize */
654:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) PetscCall(BVNormalize(eps->V,NULL));
655:   PetscFunctionReturn(PETSC_SUCCESS);
656: }

658: static PetscErrorCode EPSCISSSetSizes_CISS(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
659: {
660:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
661:   PetscInt       oN,oL,oM,oLmax,onpart;
662:   PetscMPIInt    size;

664:   PetscFunctionBegin;
665:   oN = ctx->N;
666:   if (ip == PETSC_DETERMINE) {
667:     if (ctx->N!=32) { ctx->N =32; ctx->M = ctx->N/4; }
668:   } else if (ip != PETSC_CURRENT) {
669:     PetscCheck(ip>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be > 0");
670:     PetscCheck(ip%2==0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be an even number");
671:     if (ctx->N!=ip) { ctx->N = ip; ctx->M = ctx->N/4; }
672:   }
673:   oL = ctx->L;
674:   if (bs == PETSC_DETERMINE) {
675:     ctx->L = 16;
676:   } else if (bs != PETSC_CURRENT) {
677:     PetscCheck(bs>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bs argument must be > 0");
678:     ctx->L = bs;
679:   }
680:   oM = ctx->M;
681:   if (ms == PETSC_DETERMINE) {
682:     ctx->M = ctx->N/4;
683:   } else if (ms != PETSC_CURRENT) {
684:     PetscCheck(ms>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be > 0");
685:     PetscCheck(ms<=ctx->N,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be less than or equal to the number of integration points");
686:     ctx->M = ms;
687:   }
688:   onpart = ctx->npart;
689:   if (npart == PETSC_DETERMINE) {
690:     ctx->npart = 1;
691:   } else if (npart != PETSC_CURRENT) {
692:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)eps),&size));
693:     PetscCheck(npart>0 && npart<=size,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of npart");
694:     ctx->npart = npart;
695:   }
696:   oLmax = ctx->L_max;
697:   if (bsmax == PETSC_DETERMINE) {
698:     ctx->L_max = 64;
699:   } else if (bsmax != PETSC_CURRENT) {
700:     PetscCheck(bsmax>0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bsmax argument must be > 0");
701:     ctx->L_max = PetscMax(bsmax,ctx->L);
702:   }
703:   if (onpart != ctx->npart || oN != ctx->N || realmats != ctx->isreal) {
704:     PetscCall(SlepcContourDataDestroy(&ctx->contour));
705:     PetscCall(PetscInfo(eps,"Resetting the contour data structure due to a change of parameters\n"));
706:     eps->state = EPS_STATE_INITIAL;
707:   }
708:   ctx->isreal = realmats;
709:   if (oL != ctx->L || oM != ctx->M || oLmax != ctx->L_max) eps->state = EPS_STATE_INITIAL;
710:   PetscFunctionReturn(PETSC_SUCCESS);
711: }

713: /*@
714:    EPSCISSSetSizes - Sets the values of various size parameters in the CISS solver.

716:    Logically Collective

718:    Input Parameters:
719: +  eps      - the linear eigensolver context
720: .  ip       - number of integration points
721: .  bs       - block size
722: .  ms       - moment size
723: .  npart    - number of partitions when splitting the communicator
724: .  bsmax    - max block size
725: -  realmats - $A$ and $B$ are real

727:    Options Database Keys:
728: +  -eps_ciss_integration_points ip - sets the number of integration points
729: .  -eps_ciss_blocksize bs          - sets the block size
730: .  -eps_ciss_moments ms            - sets the moment size
731: .  -eps_ciss_partitions npart      - sets the number of partitions
732: .  -eps_ciss_maxblocksize bsmax    - sets the maximum block size
733: -  -eps_ciss_realmats (true|false) - $A$ and $B$ are real

735:    Notes:
736:    For all integer arguments, you can use `PETSC_CURRENT` to keep the current value, and
737:    `PETSC_DETERMINE` to set them to a default value.

739:    The default number of partitions is 1. This means the internal `KSP` object is shared
740:    among all processes of the `EPS` communicator. Otherwise, the communicator is split
741:    into `npart` communicators, so that `npart` `KSP` solves proceed simultaneously.

743:    For a detailed description of the parameters see {cite:p}`Mae16`.

745:    Level: advanced

747: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetSizes()`, `EPSCISSSetThreshold()`, `EPSCISSSetRefinement()`
748: @*/
749: PetscErrorCode EPSCISSSetSizes(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
750: {
751:   PetscFunctionBegin;
759:   PetscTryMethod(eps,"EPSCISSSetSizes_C",(EPS,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool),(eps,ip,bs,ms,npart,bsmax,realmats));
760:   PetscFunctionReturn(PETSC_SUCCESS);
761: }

763: static PetscErrorCode EPSCISSGetSizes_CISS(EPS eps,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
764: {
765:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

767:   PetscFunctionBegin;
768:   if (ip) *ip = ctx->N;
769:   if (bs) *bs = ctx->L;
770:   if (ms) *ms = ctx->M;
771:   if (npart) *npart = ctx->npart;
772:   if (bsmax) *bsmax = ctx->L_max;
773:   if (realmats) *realmats = ctx->isreal;
774:   PetscFunctionReturn(PETSC_SUCCESS);
775: }

777: /*@
778:    EPSCISSGetSizes - Gets the values of various size parameters in the CISS solver.

780:    Not Collective

782:    Input Parameter:
783: .  eps - the linear eigensolver context

785:    Output Parameters:
786: +  ip    - number of integration points
787: .  bs    - block size
788: .  ms    - moment size
789: .  npart - number of partitions when splitting the communicator
790: .  bsmax - max block size
791: -  realmats - $A$ and $B$ are real

793:    Level: advanced

795: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetSizes()`
796: @*/
797: PetscErrorCode EPSCISSGetSizes(EPS eps,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
798: {
799:   PetscFunctionBegin;
801:   PetscUseMethod(eps,"EPSCISSGetSizes_C",(EPS,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscBool*),(eps,ip,bs,ms,npart,bsmax,realmats));
802:   PetscFunctionReturn(PETSC_SUCCESS);
803: }

805: static PetscErrorCode EPSCISSSetThreshold_CISS(EPS eps,PetscReal delta,PetscReal spur)
806: {
807:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

809:   PetscFunctionBegin;
810:   if (delta == (PetscReal)PETSC_DETERMINE) {
811:     ctx->delta = SLEPC_DEFAULT_TOL*1e-4;
812:   } else if (delta != (PetscReal)PETSC_CURRENT) {
813:     PetscCheck(delta>0.0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The delta argument must be > 0.0");
814:     ctx->delta = delta;
815:   }
816:   if (spur == (PetscReal)PETSC_DETERMINE) {
817:     ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
818:   } else if (spur != (PetscReal)PETSC_CURRENT) {
819:     PetscCheck(spur>0.0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The spurious threshold argument must be > 0.0");
820:     ctx->spurious_threshold = spur;
821:   }
822:   PetscFunctionReturn(PETSC_SUCCESS);
823: }

825: /*@
826:    EPSCISSSetThreshold - Sets the values of various threshold parameters in
827:    the CISS solver.

829:    Logically Collective

831:    Input Parameters:
832: +  eps   - the linear eigensolver context
833: .  delta - threshold for numerical rank
834: -  spur  - spurious threshold (to discard spurious eigenpairs)

836:    Options Database Keys:
837: +  -eps_ciss_delta delta             - sets the delta
838: -  -eps_ciss_spurious_threshold spur - sets the spurious threshold

840:    Notes:
841:    `PETSC_CURRENT` can be used to preserve the current value of any of the
842:    arguments, and `PETSC_DETERMINE` to set them to a default value.

844:    For a detailed description of the parameters see {cite:p}`Mae16`.

846:    Level: advanced

848: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetThreshold()`, `EPSCISSSetSizes()`, `EPSCISSSetRefinement()`
849: @*/
850: PetscErrorCode EPSCISSSetThreshold(EPS eps,PetscReal delta,PetscReal spur)
851: {
852:   PetscFunctionBegin;
856:   PetscTryMethod(eps,"EPSCISSSetThreshold_C",(EPS,PetscReal,PetscReal),(eps,delta,spur));
857:   PetscFunctionReturn(PETSC_SUCCESS);
858: }

860: static PetscErrorCode EPSCISSGetThreshold_CISS(EPS eps,PetscReal *delta,PetscReal *spur)
861: {
862:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

864:   PetscFunctionBegin;
865:   if (delta) *delta = ctx->delta;
866:   if (spur)  *spur = ctx->spurious_threshold;
867:   PetscFunctionReturn(PETSC_SUCCESS);
868: }

870: /*@
871:    EPSCISSGetThreshold - Gets the values of various threshold parameters
872:    in the CISS solver.

874:    Not Collective

876:    Input Parameter:
877: .  eps - the linear eigensolver context

879:    Output Parameters:
880: +  delta - threshold for numerical rank
881: -  spur  - spurious threshold (to discard spurious eigenpairs)

883:    Level: advanced

885: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetThreshold()`
886: @*/
887: PetscErrorCode EPSCISSGetThreshold(EPS eps,PetscReal *delta,PetscReal *spur)
888: {
889:   PetscFunctionBegin;
891:   PetscUseMethod(eps,"EPSCISSGetThreshold_C",(EPS,PetscReal*,PetscReal*),(eps,delta,spur));
892:   PetscFunctionReturn(PETSC_SUCCESS);
893: }

895: static PetscErrorCode EPSCISSSetRefinement_CISS(EPS eps,PetscInt inner,PetscInt blsize)
896: {
897:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

899:   PetscFunctionBegin;
900:   if (inner == PETSC_DETERMINE) {
901:     ctx->refine_inner = 0;
902:   } else if (inner != PETSC_CURRENT) {
903:     PetscCheck(inner>=0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine inner argument must be >= 0");
904:     ctx->refine_inner = inner;
905:   }
906:   if (blsize == PETSC_DETERMINE) {
907:     ctx->refine_blocksize = 0;
908:   } else if (blsize != PETSC_CURRENT) {
909:     PetscCheck(blsize>=0,PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine blocksize argument must be >= 0");
910:     ctx->refine_blocksize = blsize;
911:   }
912:   PetscFunctionReturn(PETSC_SUCCESS);
913: }

915: /*@
916:    EPSCISSSetRefinement - Sets the values of various refinement parameters
917:    in the CISS solver.

919:    Logically Collective

921:    Input Parameters:
922: +  eps    - the linear eigensolver context
923: .  inner  - number of iterative refinement iterations (inner loop)
924: -  blsize - number of iterative refinement iterations (blocksize loop)

926:    Options Database Keys:
927: +  -eps_ciss_refine_inner inner      - sets number of inner iterations
928: -  -eps_ciss_refine_blocksize blsize - sets number of blocksize iterations

930:    Notes:
931:    `PETSC_CURRENT` can be used to preserve the current value of any of the
932:    arguments, and `PETSC_DETERMINE` to set them to a default of 0 (no refinement).

934:    For a detailed description of the parameters see {cite:p}`Mae16`.

936:    Level: advanced

938: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetRefinement()`, `EPSCISSSetSizes()`, `EPSCISSSetThreshold()`
939: @*/
940: PetscErrorCode EPSCISSSetRefinement(EPS eps,PetscInt inner,PetscInt blsize)
941: {
942:   PetscFunctionBegin;
946:   PetscTryMethod(eps,"EPSCISSSetRefinement_C",(EPS,PetscInt,PetscInt),(eps,inner,blsize));
947:   PetscFunctionReturn(PETSC_SUCCESS);
948: }

950: static PetscErrorCode EPSCISSGetRefinement_CISS(EPS eps,PetscInt *inner,PetscInt *blsize)
951: {
952:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

954:   PetscFunctionBegin;
955:   if (inner)  *inner = ctx->refine_inner;
956:   if (blsize) *blsize = ctx->refine_blocksize;
957:   PetscFunctionReturn(PETSC_SUCCESS);
958: }

960: /*@
961:    EPSCISSGetRefinement - Gets the values of various refinement parameters
962:    in the CISS solver.

964:    Not Collective

966:    Input Parameter:
967: .  eps - the linear eigensolver context

969:    Output Parameters:
970: +  inner  - number of iterative refinement iterations (inner loop)
971: -  blsize - number of iterative refinement iterations (blocksize loop)

973:    Level: advanced

975: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetRefinement()`
976: @*/
977: PetscErrorCode EPSCISSGetRefinement(EPS eps, PetscInt *inner, PetscInt *blsize)
978: {
979:   PetscFunctionBegin;
981:   PetscUseMethod(eps,"EPSCISSGetRefinement_C",(EPS,PetscInt*,PetscInt*),(eps,inner,blsize));
982:   PetscFunctionReturn(PETSC_SUCCESS);
983: }

985: static PetscErrorCode EPSCISSSetUseST_CISS(EPS eps,PetscBool usest)
986: {
987:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

989:   PetscFunctionBegin;
990:   ctx->usest     = usest;
991:   ctx->usest_set = PETSC_TRUE;
992:   eps->state     = EPS_STATE_INITIAL;
993:   PetscFunctionReturn(PETSC_SUCCESS);
994: }

996: /*@
997:    EPSCISSSetUseST - Sets a flag indicating that the CISS solver will
998:    use the `ST` object for the linear solves.

1000:    Logically Collective

1002:    Input Parameters:
1003: +  eps    - the linear eigensolver context
1004: -  usest  - boolean flag to use the `ST` object or not

1006:    Options Database Key:
1007: .  -eps_ciss_usest (true|false) - whether the `ST` object will be used or not

1009:    Note:
1010:    When this option is set, the linear solves can be configured by
1011:    setting options for the `KSP` object obtained with `STGetKSP()`.
1012:    Otherwise, several `KSP` objects are created, which can be accessed
1013:    with `EPSCISSGetKSPs()`.

1015:    The default is to use the `ST`, unless several partitions have been
1016:    specified, see `EPSCISSSetSizes()`.

1018:    Level: advanced

1020: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetUseST()`, `EPSCISSSetSizes()`, `EPSCISSGetKSPs()`, `STGetKSP()`
1021: @*/
1022: PetscErrorCode EPSCISSSetUseST(EPS eps,PetscBool usest)
1023: {
1024:   PetscFunctionBegin;
1027:   PetscTryMethod(eps,"EPSCISSSetUseST_C",(EPS,PetscBool),(eps,usest));
1028:   PetscFunctionReturn(PETSC_SUCCESS);
1029: }

1031: static PetscErrorCode EPSCISSGetUseST_CISS(EPS eps,PetscBool *usest)
1032: {
1033:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1035:   PetscFunctionBegin;
1036:   *usest = ctx->usest;
1037:   PetscFunctionReturn(PETSC_SUCCESS);
1038: }

1040: /*@
1041:    EPSCISSGetUseST - Gets the flag for using the `ST` object
1042:    in the CISS solver.

1044:    Not Collective

1046:    Input Parameter:
1047: .  eps - the linear eigensolver context

1049:    Output Parameter:
1050: .  usest - boolean flag indicating if the `ST` object is being used

1052:    Level: advanced

1054: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetUseST()`
1055: @*/
1056: PetscErrorCode EPSCISSGetUseST(EPS eps,PetscBool *usest)
1057: {
1058:   PetscFunctionBegin;
1060:   PetscAssertPointer(usest,2);
1061:   PetscUseMethod(eps,"EPSCISSGetUseST_C",(EPS,PetscBool*),(eps,usest));
1062:   PetscFunctionReturn(PETSC_SUCCESS);
1063: }

1065: static PetscErrorCode EPSCISSSetQuadRule_CISS(EPS eps,EPSCISSQuadRule quad)
1066: {
1067:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1069:   PetscFunctionBegin;
1070:   if (ctx->quad != quad) {
1071:     ctx->quad  = quad;
1072:     eps->state = EPS_STATE_INITIAL;
1073:   }
1074:   PetscFunctionReturn(PETSC_SUCCESS);
1075: }

1077: /*@
1078:    EPSCISSSetQuadRule - Sets the quadrature rule used in the CISS solver.

1080:    Logically Collective

1082:    Input Parameters:
1083: +  eps  - the linear eigensolver context
1084: -  quad - the quadrature rule, see `EPSCISSQuadRule` for possible values

1086:    Options Database Key:
1087: .  -eps_ciss_quadrule (trapezoidal|chebyshev) - sets the quadrature rule

1089:    Notes:
1090:    By default, the trapezoidal rule is used (`EPS_CISS_QUADRULE_TRAPEZOIDAL`).

1092:    If the `chebyshev` option is specified (`EPS_CISS_QUADRULE_CHEBYSHEV`), then
1093:    Chebyshev points are used as quadrature points.

1095:    Level: advanced

1097: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetQuadRule()`, `EPSCISSQuadRule`
1098: @*/
1099: PetscErrorCode EPSCISSSetQuadRule(EPS eps,EPSCISSQuadRule quad)
1100: {
1101:   PetscFunctionBegin;
1104:   PetscTryMethod(eps,"EPSCISSSetQuadRule_C",(EPS,EPSCISSQuadRule),(eps,quad));
1105:   PetscFunctionReturn(PETSC_SUCCESS);
1106: }

1108: static PetscErrorCode EPSCISSGetQuadRule_CISS(EPS eps,EPSCISSQuadRule *quad)
1109: {
1110:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1112:   PetscFunctionBegin;
1113:   *quad = ctx->quad;
1114:   PetscFunctionReturn(PETSC_SUCCESS);
1115: }

1117: /*@
1118:    EPSCISSGetQuadRule - Gets the quadrature rule used in the CISS solver.

1120:    Not Collective

1122:    Input Parameter:
1123: .  eps - the linear eigensolver context

1125:    Output Parameter:
1126: .  quad - quadrature rule

1128:    Level: advanced

1130: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetQuadRule()`, `EPSCISSQuadRule`
1131: @*/
1132: PetscErrorCode EPSCISSGetQuadRule(EPS eps,EPSCISSQuadRule *quad)
1133: {
1134:   PetscFunctionBegin;
1136:   PetscAssertPointer(quad,2);
1137:   PetscUseMethod(eps,"EPSCISSGetQuadRule_C",(EPS,EPSCISSQuadRule*),(eps,quad));
1138:   PetscFunctionReturn(PETSC_SUCCESS);
1139: }

1141: static PetscErrorCode EPSCISSSetExtraction_CISS(EPS eps,EPSCISSExtraction extraction)
1142: {
1143:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1145:   PetscFunctionBegin;
1146:   if (ctx->extraction != extraction) {
1147:     ctx->extraction = extraction;
1148:     eps->state      = EPS_STATE_INITIAL;
1149:   }
1150:   PetscFunctionReturn(PETSC_SUCCESS);
1151: }

1153: /*@
1154:    EPSCISSSetExtraction - Sets the extraction technique used in the CISS solver.

1156:    Logically Collective

1158:    Input Parameters:
1159: +  eps        - the linear eigensolver context
1160: -  extraction - the extraction technique, see `EPSCISSExtraction` for possible values

1162:    Options Database Key:
1163: .  -eps_ciss_extraction (ritz|hankel) - sets the extraction technique

1165:    Notes:
1166:    By default, the Rayleigh-Ritz extraction is used (`EPS_CISS_EXTRACTION_RITZ`),
1167:    see {cite:p}`Sak07`.

1169:    If the `hankel` option is specified (`EPS_CISS_EXTRACTION_HANKEL`), then
1170:    the block Hankel method is used for extracting eigenpairs {cite:p}`Sak03`.

1172:    Level: advanced

1174: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSGetExtraction()`, `EPSCISSExtraction`
1175: @*/
1176: PetscErrorCode EPSCISSSetExtraction(EPS eps,EPSCISSExtraction extraction)
1177: {
1178:   PetscFunctionBegin;
1181:   PetscTryMethod(eps,"EPSCISSSetExtraction_C",(EPS,EPSCISSExtraction),(eps,extraction));
1182:   PetscFunctionReturn(PETSC_SUCCESS);
1183: }

1185: static PetscErrorCode EPSCISSGetExtraction_CISS(EPS eps,EPSCISSExtraction *extraction)
1186: {
1187:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1189:   PetscFunctionBegin;
1190:   *extraction = ctx->extraction;
1191:   PetscFunctionReturn(PETSC_SUCCESS);
1192: }

1194: /*@
1195:    EPSCISSGetExtraction - Gets the extraction technique used in the CISS solver.

1197:    Not Collective

1199:    Input Parameter:
1200: .  eps - the linear eigensolver context

1202:    Output Parameter:
1203: .  extraction - extraction technique

1205:    Level: advanced

1207: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetExtraction()`, `EPSCISSExtraction`
1208: @*/
1209: PetscErrorCode EPSCISSGetExtraction(EPS eps,EPSCISSExtraction *extraction)
1210: {
1211:   PetscFunctionBegin;
1213:   PetscAssertPointer(extraction,2);
1214:   PetscUseMethod(eps,"EPSCISSGetExtraction_C",(EPS,EPSCISSExtraction*),(eps,extraction));
1215:   PetscFunctionReturn(PETSC_SUCCESS);
1216: }

1218: static PetscErrorCode EPSCISSGetKSPs_CISS(EPS eps,PetscInt *nsolve,KSP **ksp)
1219: {
1220:   EPS_CISS         *ctx = (EPS_CISS*)eps->data;
1221:   SlepcContourData contour;
1222:   PetscInt         i,nsplit;
1223:   PC               pc;
1224:   MPI_Comm         child;

1226:   PetscFunctionBegin;
1227:   if (!ctx->contour) {  /* initialize contour data structure first */
1228:     PetscCall(RGCanUseConjugates(eps->rg,ctx->isreal,&ctx->useconj));
1229:     PetscCall(SlepcContourDataCreate(ctx->useconj?ctx->N/2:ctx->N,ctx->npart,(PetscObject)eps,&ctx->contour));
1230:   }
1231:   contour = ctx->contour;
1232:   if (!contour->ksp) {
1233:     PetscCall(PetscMalloc1(contour->npoints,&contour->ksp));
1234:     PetscCall(EPSGetST(eps,&eps->st));
1235:     PetscCall(STGetSplitPreconditionerInfo(eps->st,&nsplit,NULL));
1236:     PetscCall(PetscSubcommGetChild(contour->subcomm,&child));
1237:     for (i=0;i<contour->npoints;i++) {
1238:       PetscCall(KSPCreate(child,&contour->ksp[i]));
1239:       PetscCall(PetscObjectIncrementTabLevel((PetscObject)contour->ksp[i],(PetscObject)eps,1));
1240:       PetscCall(KSPSetOptionsPrefix(contour->ksp[i],((PetscObject)eps)->prefix));
1241:       PetscCall(KSPAppendOptionsPrefix(contour->ksp[i],"eps_ciss_"));
1242:       PetscCall(PetscObjectSetOptions((PetscObject)contour->ksp[i],((PetscObject)eps)->options));
1243:       PetscCall(KSPSetErrorIfNotConverged(contour->ksp[i],PETSC_TRUE));
1244:       PetscCall(KSPSetTolerances(contour->ksp[i],SlepcDefaultTol(eps->tol),PETSC_CURRENT,PETSC_CURRENT,PETSC_CURRENT));
1245:       PetscCall(KSPGetPC(contour->ksp[i],&pc));
1246:       if (nsplit) {
1247:         PetscCall(KSPSetType(contour->ksp[i],KSPBCGS));
1248:         PetscCall(PCSetType(pc,PCBJACOBI));
1249:       } else {
1250:         PetscCall(KSPSetType(contour->ksp[i],KSPPREONLY));
1251:         PetscCall(PCSetType(pc,PCLU));
1252:       }
1253:     }
1254:   }
1255:   if (nsolve) *nsolve = contour->npoints;
1256:   if (ksp)    *ksp    = contour->ksp;
1257:   PetscFunctionReturn(PETSC_SUCCESS);
1258: }

1260: /*@C
1261:    EPSCISSGetKSPs - Retrieve the array of linear solver objects associated with
1262:    the CISS solver.

1264:    Not Collective

1266:    Input Parameter:
1267: .  eps - the linear eigensolver context

1269:    Output Parameters:
1270: +  nsolve - number of solver objects
1271: -  ksp - array of linear solver object

1273:    Note:
1274:    The number of `KSP` solvers is equal to the number of integration points divided by
1275:    the number of partitions, see `EPSCISSSetSizes()`. This value is halved in the case
1276:    of real matrices with a region centered at the real axis.

1278:    Level: advanced

1280: .seealso: [](ch:eps), `EPSCISS`, `EPSCISSSetSizes()`
1281: @*/
1282: PetscErrorCode EPSCISSGetKSPs(EPS eps,PetscInt *nsolve,KSP **ksp)
1283: {
1284:   PetscFunctionBegin;
1286:   PetscUseMethod(eps,"EPSCISSGetKSPs_C",(EPS,PetscInt*,KSP**),(eps,nsolve,ksp));
1287:   PetscFunctionReturn(PETSC_SUCCESS);
1288: }

1290: static PetscErrorCode EPSReset_CISS(EPS eps)
1291: {
1292:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1294:   PetscFunctionBegin;
1295:   PetscCall(BVDestroy(&ctx->S));
1296:   PetscCall(BVDestroy(&ctx->V));
1297:   PetscCall(BVDestroy(&ctx->Y));
1298:   if (!ctx->usest) PetscCall(SlepcContourDataReset(ctx->contour));
1299:   PetscCall(BVDestroy(&ctx->pV));
1300:   PetscFunctionReturn(PETSC_SUCCESS);
1301: }

1303: static PetscErrorCode EPSSetFromOptions_CISS(EPS eps,PetscOptionItems PetscOptionsObject)
1304: {
1305:   PetscReal         r3,r4;
1306:   PetscInt          i,i1,i2,i3,i4,i5,i6,i7;
1307:   PetscBool         b1,b2,flg,flg2,flg3,flg4,flg5,flg6;
1308:   EPS_CISS          *ctx = (EPS_CISS*)eps->data;
1309:   EPSCISSQuadRule   quad;
1310:   EPSCISSExtraction extraction;

1312:   PetscFunctionBegin;
1313:   PetscOptionsHeadBegin(PetscOptionsObject,"EPS CISS Options");

1315:     PetscCall(EPSCISSGetSizes(eps,&i1,&i2,&i3,&i4,&i5,&b1));
1316:     PetscCall(PetscOptionsInt("-eps_ciss_integration_points","Number of integration points","EPSCISSSetSizes",i1,&i1,&flg));
1317:     PetscCall(PetscOptionsInt("-eps_ciss_blocksize","Block size","EPSCISSSetSizes",i2,&i2,&flg2));
1318:     PetscCall(PetscOptionsInt("-eps_ciss_moments","Moment size","EPSCISSSetSizes",i3,&i3,&flg3));
1319:     PetscCall(PetscOptionsInt("-eps_ciss_partitions","Number of partitions","EPSCISSSetSizes",i4,&i4,&flg4));
1320:     PetscCall(PetscOptionsInt("-eps_ciss_maxblocksize","Maximum block size","EPSCISSSetSizes",i5,&i5,&flg5));
1321:     PetscCall(PetscOptionsBool("-eps_ciss_realmats","True if A and B are real","EPSCISSSetSizes",b1,&b1,&flg6));
1322:     if (flg || flg2 || flg3 || flg4 || flg5 || flg6) PetscCall(EPSCISSSetSizes(eps,i1,i2,i3,i4,i5,b1));

1324:     PetscCall(EPSCISSGetThreshold(eps,&r3,&r4));
1325:     PetscCall(PetscOptionsReal("-eps_ciss_delta","Threshold for numerical rank","EPSCISSSetThreshold",r3,&r3,&flg));
1326:     PetscCall(PetscOptionsReal("-eps_ciss_spurious_threshold","Threshold for the spurious eigenpairs","EPSCISSSetThreshold",r4,&r4,&flg2));
1327:     if (flg || flg2) PetscCall(EPSCISSSetThreshold(eps,r3,r4));

1329:     PetscCall(EPSCISSGetRefinement(eps,&i6,&i7));
1330:     PetscCall(PetscOptionsInt("-eps_ciss_refine_inner","Number of inner iterative refinement iterations","EPSCISSSetRefinement",i6,&i6,&flg));
1331:     PetscCall(PetscOptionsInt("-eps_ciss_refine_blocksize","Number of blocksize iterative refinement iterations","EPSCISSSetRefinement",i7,&i7,&flg2));
1332:     if (flg || flg2) PetscCall(EPSCISSSetRefinement(eps,i6,i7));

1334:     PetscCall(EPSCISSGetUseST(eps,&b2));
1335:     PetscCall(PetscOptionsBool("-eps_ciss_usest","Use ST for linear solves","EPSCISSSetUseST",b2,&b2,&flg));
1336:     if (flg) PetscCall(EPSCISSSetUseST(eps,b2));

1338:     PetscCall(PetscOptionsEnum("-eps_ciss_quadrule","Quadrature rule","EPSCISSSetQuadRule",EPSCISSQuadRules,(PetscEnum)ctx->quad,(PetscEnum*)&quad,&flg));
1339:     if (flg) PetscCall(EPSCISSSetQuadRule(eps,quad));

1341:     PetscCall(PetscOptionsEnum("-eps_ciss_extraction","Extraction technique","EPSCISSSetExtraction",EPSCISSExtractions,(PetscEnum)ctx->extraction,(PetscEnum*)&extraction,&flg));
1342:     if (flg) PetscCall(EPSCISSSetExtraction(eps,extraction));

1344:   PetscOptionsHeadEnd();

1346:   if (!eps->rg) PetscCall(EPSGetRG(eps,&eps->rg));
1347:   PetscCall(RGSetFromOptions(eps->rg)); /* this is necessary here to set useconj */
1348:   if (!ctx->contour || !ctx->contour->ksp) PetscCall(EPSCISSGetKSPs(eps,NULL,NULL));
1349:   PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)eps),PETSC_ERR_PLIB,"Something went wrong with EPSCISSGetKSPs()");
1350:   for (i=0;i<ctx->contour->npoints;i++) PetscCall(KSPSetFromOptions(ctx->contour->ksp[i]));
1351:   PetscCall(PetscSubcommSetFromOptions(ctx->contour->subcomm));
1352:   PetscFunctionReturn(PETSC_SUCCESS);
1353: }

1355: static PetscErrorCode EPSDestroy_CISS(EPS eps)
1356: {
1357:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1359:   PetscFunctionBegin;
1360:   PetscCall(SlepcContourDataDestroy(&ctx->contour));
1361:   PetscCall(PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma));
1362:   PetscCall(PetscFree(eps->data));
1363:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetSizes_C",NULL));
1364:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetSizes_C",NULL));
1365:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetThreshold_C",NULL));
1366:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetThreshold_C",NULL));
1367:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetRefinement_C",NULL));
1368:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetRefinement_C",NULL));
1369:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetUseST_C",NULL));
1370:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetUseST_C",NULL));
1371:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetQuadRule_C",NULL));
1372:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetQuadRule_C",NULL));
1373:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetExtraction_C",NULL));
1374:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetExtraction_C",NULL));
1375:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetKSPs_C",NULL));
1376:   PetscFunctionReturn(PETSC_SUCCESS);
1377: }

1379: static PetscErrorCode EPSView_CISS(EPS eps,PetscViewer viewer)
1380: {
1381:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1382:   PetscBool      isascii;
1383:   PetscViewer    sviewer;

1385:   PetscFunctionBegin;
1386:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
1387:   if (isascii) {
1388:     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));
1389:     if (ctx->isreal) PetscCall(PetscViewerASCIIPrintf(viewer,"  exploiting symmetry of integration points\n"));
1390:     PetscCall(PetscViewerASCIIPrintf(viewer,"  threshold { delta: %g, spurious threshold: %g }\n",(double)ctx->delta,(double)ctx->spurious_threshold));
1391:     PetscCall(PetscViewerASCIIPrintf(viewer,"  iterative refinement { inner: %" PetscInt_FMT ", blocksize: %" PetscInt_FMT " }\n",ctx->refine_inner, ctx->refine_blocksize));
1392:     PetscCall(PetscViewerASCIIPrintf(viewer,"  extraction: %s\n",EPSCISSExtractions[ctx->extraction]));
1393:     PetscCall(PetscViewerASCIIPrintf(viewer,"  quadrature rule: %s\n",EPSCISSQuadRules[ctx->quad]));
1394:     if (ctx->usest) PetscCall(PetscViewerASCIIPrintf(viewer,"  using ST for linear solves\n"));
1395:     else {
1396:       if (!ctx->contour || !ctx->contour->ksp) PetscCall(EPSCISSGetKSPs(eps,NULL,NULL));
1397:       PetscAssert(ctx->contour && ctx->contour->ksp,PetscObjectComm((PetscObject)eps),PETSC_ERR_PLIB,"Something went wrong with EPSCISSGetKSPs()");
1398:       PetscCall(PetscViewerASCIIPushTab(viewer));
1399:       if (ctx->npart>1 && ctx->contour->subcomm) {
1400:         PetscCall(PetscViewerGetSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
1401:         if (!ctx->contour->subcomm->color) PetscCall(KSPView(ctx->contour->ksp[0],sviewer));
1402:         PetscCall(PetscViewerFlush(sviewer));
1403:         PetscCall(PetscViewerRestoreSubViewer(viewer,ctx->contour->subcomm->child,&sviewer));
1404:         /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
1405:         PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1406:       } else PetscCall(KSPView(ctx->contour->ksp[0],viewer));
1407:       PetscCall(PetscViewerASCIIPopTab(viewer));
1408:     }
1409:   }
1410:   PetscFunctionReturn(PETSC_SUCCESS);
1411: }

1413: static PetscErrorCode EPSSetDefaultST_CISS(EPS eps)
1414: {
1415:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1416:   PetscBool      usest = ctx->usest;
1417:   KSP            ksp;
1418:   PC             pc;

1420:   PetscFunctionBegin;
1421:   if (!((PetscObject)eps->st)->type_name) {
1422:     if (!ctx->usest_set) usest = (ctx->npart>1)? PETSC_FALSE: PETSC_TRUE;
1423:     if (usest) PetscCall(STSetType(eps->st,STSINVERT));
1424:     else {
1425:       /* we are not going to use ST, so avoid factorizing the matrix */
1426:       PetscCall(STSetType(eps->st,STSHIFT));
1427:       if (eps->isgeneralized) {
1428:         PetscCall(STGetKSP(eps->st,&ksp));
1429:         PetscCall(KSPGetPC(ksp,&pc));
1430:         PetscCall(PCSetType(pc,PCNONE));
1431:       }
1432:     }
1433:   }
1434:   PetscFunctionReturn(PETSC_SUCCESS);
1435: }

1437: /*MC
1438:    EPSCISS - EPSCISS = "ciss" - A contour integral eigensolver based on the
1439:    Sakurai-Sugiura scheme.

1441:    Notes:
1442:    This solver is based on the numerical contour integration idea
1443:    proposed initially by {cite:t}`Sak03` and improved later by adding
1444:    a Rayleigh-Ritz projection step {cite:p}`Sak07`.

1446:    Contour integral methods are able to compute all eigenvalues
1447:    lying inside a region of the complex plane. Use `EPSGetRG()` to
1448:    specify the region. However, the computational cost is usually high
1449:    because multiple linear systems must be solved. For this, we can
1450:    use the `KSP` object inside `ST`, or several independent `KSP`s,
1451:    see `EPSCISSSetUseST()`.

1453:    Details of the implementation in SLEPc can be found in {cite:p}`Mae16`.

1455:    Level: beginner

1457: .seealso: [](ch:eps), `EPS`, `EPSType`, `EPSSetType()`, `EPSGetRG()`
1458: M*/
1459: SLEPC_EXTERN PetscErrorCode EPSCreate_CISS(EPS eps)
1460: {
1461:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1463:   PetscFunctionBegin;
1464:   PetscCall(PetscNew(&ctx));
1465:   eps->data = ctx;

1467:   eps->useds = PETSC_TRUE;
1468:   eps->categ = EPS_CATEGORY_CONTOUR;

1470:   eps->ops->solve          = EPSSolve_CISS;
1471:   eps->ops->setup          = EPSSetUp_CISS;
1472:   eps->ops->setupsort      = EPSSetUpSort_CISS;
1473:   eps->ops->setfromoptions = EPSSetFromOptions_CISS;
1474:   eps->ops->destroy        = EPSDestroy_CISS;
1475:   eps->ops->reset          = EPSReset_CISS;
1476:   eps->ops->view           = EPSView_CISS;
1477:   eps->ops->computevectors = EPSComputeVectors_CISS;
1478:   eps->ops->setdefaultst   = EPSSetDefaultST_CISS;

1480:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetSizes_C",EPSCISSSetSizes_CISS));
1481:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetSizes_C",EPSCISSGetSizes_CISS));
1482:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetThreshold_C",EPSCISSSetThreshold_CISS));
1483:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetThreshold_C",EPSCISSGetThreshold_CISS));
1484:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetRefinement_C",EPSCISSSetRefinement_CISS));
1485:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetRefinement_C",EPSCISSGetRefinement_CISS));
1486:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetUseST_C",EPSCISSSetUseST_CISS));
1487:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetUseST_C",EPSCISSGetUseST_CISS));
1488:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetQuadRule_C",EPSCISSSetQuadRule_CISS));
1489:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetQuadRule_C",EPSCISSGetQuadRule_CISS));
1490:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetExtraction_C",EPSCISSSetExtraction_CISS));
1491:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetExtraction_C",EPSCISSGetExtraction_CISS));
1492:   PetscCall(PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetKSPs_C",EPSCISSGetKSPs_CISS));

1494:   /* set default values of parameters */
1495:   ctx->N                  = 32;
1496:   ctx->L                  = 16;
1497:   ctx->M                  = ctx->N/4;
1498:   ctx->delta              = SLEPC_DEFAULT_TOL*1e-4;
1499:   ctx->L_max              = 64;
1500:   ctx->spurious_threshold = PetscSqrtReal(SLEPC_DEFAULT_TOL);
1501:   ctx->usest              = PETSC_TRUE;
1502:   ctx->usest_set          = PETSC_FALSE;
1503:   ctx->isreal             = PETSC_FALSE;
1504:   ctx->refine_inner       = 0;
1505:   ctx->refine_blocksize   = 0;
1506:   ctx->npart              = 1;
1507:   ctx->quad               = (EPSCISSQuadRule)0;
1508:   ctx->extraction         = EPS_CISS_EXTRACTION_RITZ;
1509:   PetscFunctionReturn(PETSC_SUCCESS);
1510: }