Actual source code: ciss.c

slepc-3.13.0 2020-03-31
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2020, 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>                /*I "slepceps.h" I*/
 34: #include <slepcblaslapack.h>

 36: typedef struct {
 37:   /* parameters */
 38:   PetscInt          N;          /* number of integration points (32) */
 39:   PetscInt          L;          /* block size (16) */
 40:   PetscInt          M;          /* moment degree (N/4 = 4) */
 41:   PetscReal         delta;      /* threshold of singular value (1e-12) */
 42:   PetscInt          L_max;      /* maximum number of columns of the source matrix V */
 43:   PetscReal         spurious_threshold; /* discard spurious eigenpairs */
 44:   PetscBool         isreal;     /* A and B are real */
 45:   PetscInt          npart;      /* number of partitions */
 46:   PetscInt          refine_inner;
 47:   PetscInt          refine_blocksize;
 48:   /* private data */
 49:   PetscReal         *sigma;     /* threshold for numerical rank */
 50:   PetscInt          subcomm_id;
 51:   PetscInt          num_solve_point;
 52:   PetscScalar       *weight;
 53:   PetscScalar       *omega;
 54:   PetscScalar       *pp;
 55:   BV                V;
 56:   BV                S;
 57:   BV                pV;
 58:   BV                Y;
 59:   Vec               xsub;
 60:   Vec               xdup;
 61:   KSP               *ksp;       /* ksp array for storing factorizations at integration points */
 62:   PetscBool         useconj;
 63:   PetscReal         est_eig;
 64:   VecScatter        scatterin;
 65:   Mat               pA,pB;
 66:   PetscSubcomm      subcomm;
 67:   PetscBool         usest;
 68:   PetscBool         usest_set;  /* whether the user set the usest flag or not */
 69:   EPSCISSQuadRule   quad;
 70:   EPSCISSExtraction extraction;
 71: } EPS_CISS;

 73: /* destroy KSP objects when the number of solve points changes */
 74: PETSC_STATIC_INLINE PetscErrorCode EPSCISSResetSolvers(EPS eps)
 75: {
 77:   PetscInt       i;
 78:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

 81:   if (ctx->ksp) {
 82:     for (i=0;i<ctx->num_solve_point;i++) {
 83:       KSPDestroy(&ctx->ksp[i]);
 84:     }
 85:     PetscFree(ctx->ksp);
 86:   }
 87:   return(0);
 88: }

 90: /* clean PetscSubcomm object when the number of partitions changes */
 91: PETSC_STATIC_INLINE PetscErrorCode EPSCISSResetSubcomm(EPS eps)
 92: {
 94:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

 97:   EPSCISSResetSolvers(eps);
 98:   PetscSubcommDestroy(&ctx->subcomm);
 99:   return(0);
100: }

102: /* determine whether half of integration points can be avoided (use its conjugates);
103:    depends on isreal and the center of the region */
104: PETSC_STATIC_INLINE PetscErrorCode EPSCISSSetUseConj(EPS eps,PetscBool *useconj)
105: {
107:   PetscScalar    center;
108:   PetscReal      c,d;
109:   PetscBool      isellipse,isinterval;
110: #if defined(PETSC_USE_COMPLEX)
111:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
112: #endif

115:   *useconj = PETSC_FALSE;
116:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
117:   if (isellipse) {
118:     RGEllipseGetParameters(eps->rg,&center,NULL,NULL);
119: #if defined(PETSC_USE_COMPLEX)
120:     *useconj = (ctx->isreal && PetscImaginaryPart(center) == 0.0)? PETSC_TRUE: PETSC_FALSE;
121: #endif
122:   } else {
123:     PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval);
124:     if (isinterval) {
125:       RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d);
126: #if defined(PETSC_USE_COMPLEX)
127:       *useconj = (ctx->isreal && c==d)? PETSC_TRUE: PETSC_FALSE;
128: #endif
129:     }
130:   }
131:   return(0);
132: }

134: /* create PetscSubcomm object and determine num_solve_point (depends on npart, N, useconj) */
135: PETSC_STATIC_INLINE PetscErrorCode EPSCISSSetUpSubComm(EPS eps,PetscInt *num_solve_point)
136: {
138:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
139:   PetscInt       N = ctx->N;

142:   PetscSubcommCreate(PetscObjectComm((PetscObject)eps),&ctx->subcomm);
143:   PetscSubcommSetNumber(ctx->subcomm,ctx->npart);
144:   PetscSubcommSetType(ctx->subcomm,PETSC_SUBCOMM_INTERLACED);
145:   PetscLogObjectMemory((PetscObject)eps,sizeof(PetscSubcomm));
146:   ctx->subcomm_id = ctx->subcomm->color;
147:   EPSCISSSetUseConj(eps,&ctx->useconj);
148:   if (ctx->useconj) N = N/2;
149:   *num_solve_point = N / ctx->npart;
150:   if (N%ctx->npart > ctx->subcomm_id) (*num_solve_point)++;
151:   return(0);
152: }

154: static PetscErrorCode CISSRedundantMat(EPS eps)
155: {
157:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
158:   Mat            A,B;
159:   PetscInt       nmat;

162:   STGetNumMatrices(eps->st,&nmat);
163:   if (ctx->subcomm->n != 1) {
164:     STGetMatrix(eps->st,0,&A);
165:     MatDestroy(&ctx->pA);
166:     MatCreateRedundantMatrix(A,ctx->subcomm->n,PetscSubcommChild(ctx->subcomm),MAT_INITIAL_MATRIX,&ctx->pA);
167:     if (nmat>1) {
168:       STGetMatrix(eps->st,1,&B);
169:       MatDestroy(&ctx->pB);
170:       MatCreateRedundantMatrix(B,ctx->subcomm->n,PetscSubcommChild(ctx->subcomm),MAT_INITIAL_MATRIX,&ctx->pB);
171:     } else ctx->pB = NULL;
172:   } else {
173:     ctx->pA = NULL;
174:     ctx->pB = NULL;
175:   }
176:   return(0);
177: }

179: static PetscErrorCode CISSScatterVec(EPS eps)
180: {
182:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
183:   IS             is1,is2;
184:   Vec            v0;
185:   PetscInt       i,j,k,mstart,mend,mlocal;
186:   PetscInt       *idx1,*idx2,mloc_sub;

189:   VecDestroy(&ctx->xsub);
190:   MatCreateVecs(ctx->pA,&ctx->xsub,NULL);

192:   VecDestroy(&ctx->xdup);
193:   MatGetLocalSize(ctx->pA,&mloc_sub,NULL);
194:   VecCreateMPI(PetscSubcommContiguousParent(ctx->subcomm),mloc_sub,PETSC_DECIDE,&ctx->xdup);

196:   VecScatterDestroy(&ctx->scatterin);
197:   BVGetColumn(ctx->V,0,&v0);
198:   VecGetOwnershipRange(v0,&mstart,&mend);
199:   mlocal = mend - mstart;
200:   PetscMalloc2(ctx->subcomm->n*mlocal,&idx1,ctx->subcomm->n*mlocal,&idx2);
201:   j = 0;
202:   for (k=0;k<ctx->subcomm->n;k++) {
203:     for (i=mstart;i<mend;i++) {
204:       idx1[j]   = i;
205:       idx2[j++] = i + eps->n*k;
206:     }
207:   }
208:   ISCreateGeneral(PetscObjectComm((PetscObject)eps),ctx->subcomm->n*mlocal,idx1,PETSC_COPY_VALUES,&is1);
209:   ISCreateGeneral(PetscObjectComm((PetscObject)eps),ctx->subcomm->n*mlocal,idx2,PETSC_COPY_VALUES,&is2);
210:   VecScatterCreate(v0,is1,ctx->xdup,is2,&ctx->scatterin);
211:   ISDestroy(&is1);
212:   ISDestroy(&is2);
213:   PetscFree2(idx1,idx2);
214:   BVRestoreColumn(ctx->V,0,&v0);
215:   return(0);
216: }

218: static PetscErrorCode SetPathParameter(EPS eps)
219: {
221:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
222:   PetscInt       i,j;
223:   PetscScalar    center=0.0,tmp,tmp2,*omegai;
224:   PetscReal      theta,radius=1.0,vscale,a,b,c,d,max_w=0.0,rgscale;
225: #if defined(PETSC_USE_COMPLEX)
226:   PetscReal      start_ang,end_ang;
227: #endif
228:   PetscBool      isring=PETSC_FALSE,isellipse=PETSC_FALSE,isinterval=PETSC_FALSE;

231:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
232:   PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring);
233:   PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval);
234:   RGGetScale(eps->rg,&rgscale);
235:   PetscMalloc1(ctx->N+1l,&omegai);
236:   RGComputeContour(eps->rg,ctx->N,ctx->omega,omegai);
237:   if (isellipse) {
238:     RGEllipseGetParameters(eps->rg,&center,&radius,&vscale);
239:     for (i=0;i<ctx->N;i++) {
240: #if defined(PETSC_USE_COMPLEX)
241:       theta = 2.0*PETSC_PI*(i+0.5)/ctx->N;
242:       ctx->pp[i] = PetscCMPLX(PetscCosReal(theta),vscale*PetscSinReal(theta));
243:       ctx->weight[i] = rgscale*radius*(PetscCMPLX(vscale*PetscCosReal(theta),PetscSinReal(theta)))/(PetscReal)ctx->N;
244: #else
245:       theta = (PETSC_PI/ctx->N)*(i+0.5);
246:       ctx->pp[i] = PetscCosReal(theta);
247:       ctx->weight[i] = PetscCosReal((ctx->N-1)*theta)/ctx->N;
248:       ctx->omega[i] = rgscale*(center + radius*ctx->pp[i]);
249: #endif
250:     }
251:   } else if (ctx->quad == EPS_CISS_QUADRULE_CHEBYSHEV) {
252:     for (i=0;i<ctx->N;i++) {
253:       theta = (PETSC_PI/ctx->N)*(i+0.5);
254:       ctx->pp[i] = PetscCosReal(theta);
255:       ctx->weight[i] = PetscCosReal((ctx->N-1)*theta)/ctx->N;
256:     }
257:     if (isinterval) {
258:       RGIntervalGetEndpoints(eps->rg,&a,&b,&c,&d);
259:       if ((c!=d || c!=0.0) && (a!=b || a!=0.0)) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Endpoints of the imaginary axis or the real axis must be both zero");
260:       for (i=0;i<ctx->N;i++) {
261:         if (c==d) ctx->omega[i] = ((b-a)*(ctx->pp[i]+1.0)/2.0+a)*rgscale;
262:         if (a==b) {
263: #if defined(PETSC_USE_COMPLEX)
264:           ctx->omega[i] = ((d-c)*(ctx->pp[i]+1.0)/2.0+c)*rgscale*PETSC_i;
265: #else
266:           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Integration points on a vertical line require complex arithmetic");
267: #endif
268:         }
269:       }
270:     }
271:     if (isring) {  /* only supported in complex scalars */
272: #if defined(PETSC_USE_COMPLEX)
273:       RGRingGetParameters(eps->rg,&center,&radius,&vscale,&start_ang,&end_ang,NULL);
274:       for (i=0;i<ctx->N;i++) {
275:         theta = (start_ang*2.0+(end_ang-start_ang)*(PetscRealPart(ctx->pp[i])+1.0))*PETSC_PI;
276:         ctx->omega[i] = rgscale*(center + radius*PetscCMPLX(PetscCosReal(theta),vscale*PetscSinReal(theta)));
277:       }
278: #endif
279:     }
280:   } else {
281:     if (isinterval) {
282:       RGIntervalGetEndpoints(eps->rg,&a,&b,&c,&d);
283:       center = rgscale*((b+a)/2.0+(d+c)/2.0*PETSC_PI);
284:       radius = PetscSqrtReal(PetscPowRealInt(rgscale*(b-a)/2.0,2)+PetscPowRealInt(rgscale*(d-c)/2.0,2));
285:     } else if (isring) {
286:       RGRingGetParameters(eps->rg,&center,&radius,NULL,NULL,NULL,NULL);
287:       center *= rgscale;
288:       radius *= rgscale;
289:     }
290:     for (i=0;i<ctx->N;i++) {
291:       ctx->pp[i] = (ctx->omega[i]-center)/radius;
292:       tmp = 1; tmp2 = 1;
293:       for (j=0;j<ctx->N;j++) {
294:         tmp *= ctx->omega[j];
295:         if (i != j) tmp2 *= ctx->omega[j]-ctx->omega[i];
296:       }
297:       ctx->weight[i] = tmp/tmp2;
298:       max_w = PetscMax(PetscAbsScalar(ctx->weight[i]),max_w);
299:     }
300:     for (i=0;i<ctx->N;i++) ctx->weight[i] /= (PetscScalar)max_w;
301:   }
302:   PetscFree(omegai);
303:   return(0);
304: }

306: static PetscErrorCode CISSVecSetRandom(BV V,PetscInt i0,PetscInt i1)
307: {
309:   PetscInt       i,j,nlocal;
310:   PetscScalar    *vdata;
311:   Vec            x;

314:   BVGetSizes(V,&nlocal,NULL,NULL);
315:   for (i=i0;i<i1;i++) {
316:     BVSetRandomColumn(V,i);
317:     BVGetColumn(V,i,&x);
318:     VecGetArray(x,&vdata);
319:     for (j=0;j<nlocal;j++) {
320:       vdata[j] = PetscRealPart(vdata[j]);
321:       if (PetscRealPart(vdata[j]) < 0.5) vdata[j] = -1.0;
322:       else vdata[j] = 1.0;
323:     }
324:     VecRestoreArray(x,&vdata);
325:     BVRestoreColumn(V,i,&x);
326:   }
327:   return(0);
328: }

330: static PetscErrorCode VecScatterVecs(EPS eps,BV Vin,PetscInt n)
331: {
332:   PetscErrorCode    ierr;
333:   EPS_CISS          *ctx = (EPS_CISS*)eps->data;
334:   PetscInt          i;
335:   Vec               vi,pvi;
336:   const PetscScalar *array;

339:   for (i=0;i<n;i++) {
340:     BVGetColumn(Vin,i,&vi);
341:     VecScatterBegin(ctx->scatterin,vi,ctx->xdup,INSERT_VALUES,SCATTER_FORWARD);
342:     VecScatterEnd(ctx->scatterin,vi,ctx->xdup,INSERT_VALUES,SCATTER_FORWARD);
343:     BVRestoreColumn(Vin,i,&vi);
344:     VecGetArrayRead(ctx->xdup,&array);
345:     VecPlaceArray(ctx->xsub,array);
346:     BVGetColumn(ctx->pV,i,&pvi);
347:     VecCopy(ctx->xsub,pvi);
348:     BVRestoreColumn(ctx->pV,i,&pvi);
349:     VecResetArray(ctx->xsub);
350:     VecRestoreArrayRead(ctx->xdup,&array);
351:   }
352:   return(0);
353: }

355: static PetscErrorCode SolveLinearSystem(EPS eps,Mat A,Mat B,BV V,PetscInt L_start,PetscInt L_end,PetscBool initksp)
356: {
358:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
359:   PetscInt       i,j,p_id;
360:   Mat            Fz,kspMat;
361:   Vec            Bvj,vj,yj;
362:   KSP            ksp;

365:   if (!ctx->ksp) { EPSCISSGetKSPs(eps,&ctx->num_solve_point,&ctx->ksp); }
366:   BVCreateVec(V,&Bvj);
367:   if (ctx->usest) {
368:     MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Fz);
369:   }
370:   for (i=0;i<ctx->num_solve_point;i++) {
371:     p_id = i*ctx->subcomm->n + ctx->subcomm_id;
372:     if (!ctx->usest && initksp) {
373:       MatDuplicate(A,MAT_COPY_VALUES,&kspMat);
374:       if (B) {
375:         MatAXPY(kspMat,-ctx->omega[p_id],B,DIFFERENT_NONZERO_PATTERN);
376:       } else {
377:         MatShift(kspMat,-ctx->omega[p_id]);
378:       }
379:       KSPSetOperators(ctx->ksp[i],kspMat,kspMat);
380:       MatDestroy(&kspMat);
381:     } else if (ctx->usest) {
382:       STSetShift(eps->st,ctx->omega[p_id]);
383:       STGetKSP(eps->st,&ksp);
384:     }
385:     for (j=L_start;j<L_end;j++) {
386:       BVGetColumn(V,j,&vj);
387:       BVGetColumn(ctx->Y,i*ctx->L_max+j,&yj);
388:       if (B) {
389:         MatMult(B,vj,Bvj);
390:         if (ctx->usest) {
391:           KSPSolve(ksp,Bvj,yj);
392:         } else {
393:           KSPSolve(ctx->ksp[i],Bvj,yj);
394:         }
395:       } else {
396:         if (ctx->usest) {
397:           KSPSolve(ksp,vj,yj);
398:         } else {
399:           KSPSolve(ctx->ksp[i],vj,yj);
400:         }
401:       }
402:       BVRestoreColumn(V,j,&vj);
403:       BVRestoreColumn(ctx->Y,i*ctx->L_max+j,&yj);
404:     }
405:     if (ctx->usest && i<ctx->num_solve_point-1) { KSPReset(ksp); }
406:   }
407:   if (ctx->usest) { MatDestroy(&Fz); }
408:   VecDestroy(&Bvj);
409:   return(0);
410: }

412: #if defined(PETSC_USE_COMPLEX)
413: static PetscErrorCode EstimateNumberEigs(EPS eps,PetscInt *L_add)
414: {
416:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
417:   PetscInt       i,j,p_id;
418:   PetscScalar    tmp,m = 1,sum = 0.0;
419:   PetscReal      eta;
420:   Vec            v,vtemp,vj,yj;

423:   BVGetColumn(ctx->Y,0,&yj);
424:   VecDuplicate(yj,&v);
425:   BVRestoreColumn(ctx->Y,0,&yj);
426:   BVCreateVec(ctx->V,&vtemp);
427:   for (j=0;j<ctx->L;j++) {
428:     VecSet(v,0);
429:     for (i=0;i<ctx->num_solve_point; i++) {
430:       p_id = i*ctx->subcomm->n + ctx->subcomm_id;
431:       BVSetActiveColumns(ctx->Y,i*ctx->L_max+j,i*ctx->L_max+j+1);
432:       BVMultVec(ctx->Y,ctx->weight[p_id],1,v,&m);
433:     }
434:     BVGetColumn(ctx->V,j,&vj);
435:     if (ctx->pA) {
436:       VecSet(vtemp,0);
437:       VecScatterBegin(ctx->scatterin,v,vtemp,ADD_VALUES,SCATTER_REVERSE);
438:       VecScatterEnd(ctx->scatterin,v,vtemp,ADD_VALUES,SCATTER_REVERSE);
439:       VecDot(vj,vtemp,&tmp);
440:     } else {
441:       VecDot(vj,v,&tmp);
442:     }
443:     BVRestoreColumn(ctx->V,j,&vj);
444:     if (ctx->useconj) sum += PetscRealPart(tmp)*2;
445:     else sum += tmp;
446:   }
447:   ctx->est_eig = PetscAbsScalar(sum/(PetscReal)ctx->L);
448:   eta = PetscPowReal(10.0,-PetscLog10Real(eps->tol)/ctx->N);
449:   PetscInfo1(eps,"Estimation_#Eig %f\n",(double)ctx->est_eig);
450:   *L_add = (PetscInt)PetscCeilReal((ctx->est_eig*eta)/ctx->M) - ctx->L;
451:   if (*L_add < 0) *L_add = 0;
452:   if (*L_add>ctx->L_max-ctx->L) {
453:     PetscInfo(eps,"Number of eigenvalues around the contour path may be too large\n");
454:     *L_add = ctx->L_max-ctx->L;
455:   }
456:   VecDestroy(&v);
457:   VecDestroy(&vtemp);
458:   return(0);
459: }
460: #endif

462: static PetscErrorCode CalcMu(EPS eps,PetscScalar *Mu)
463: {
465:   PetscMPIInt    sub_size,len;
466:   PetscInt       i,j,k,s;
467:   PetscScalar    *m,*temp,*temp2,*ppk,alp;
468:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
469:   Mat            M;

472:   MPI_Comm_size(PetscSubcommChild(ctx->subcomm),&sub_size);
473:   PetscMalloc3(ctx->num_solve_point*ctx->L*(ctx->L+1),&temp,2*ctx->M*ctx->L*ctx->L,&temp2,ctx->num_solve_point,&ppk);
474:   MatCreateSeqDense(PETSC_COMM_SELF,ctx->L,ctx->L_max*ctx->num_solve_point,NULL,&M);
475:   for (i=0;i<2*ctx->M*ctx->L*ctx->L;i++) temp2[i] = 0;
476:   BVSetActiveColumns(ctx->Y,0,ctx->L_max*ctx->num_solve_point);
477:   if (ctx->pA) {
478:     BVSetActiveColumns(ctx->pV,0,ctx->L);
479:     BVDot(ctx->Y,ctx->pV,M);
480:   } else {
481:     BVSetActiveColumns(ctx->V,0,ctx->L);
482:     BVDot(ctx->Y,ctx->V,M);
483:   }
484:   MatDenseGetArray(M,&m);
485:   for (i=0;i<ctx->num_solve_point;i++) {
486:     for (j=0;j<ctx->L;j++) {
487:       for (k=0;k<ctx->L;k++) {
488:         temp[k+j*ctx->L+i*ctx->L*ctx->L]=m[k+j*ctx->L+i*ctx->L*ctx->L_max];
489:       }
490:     }
491:   }
492:   MatDenseRestoreArray(M,&m);
493:   for (i=0;i<ctx->num_solve_point;i++) ppk[i] = 1;
494:   for (k=0;k<2*ctx->M;k++) {
495:     for (j=0;j<ctx->L;j++) {
496:       for (i=0;i<ctx->num_solve_point;i++) {
497:         alp = ppk[i]*ctx->weight[i*ctx->subcomm->n + ctx->subcomm_id];
498:         for (s=0;s<ctx->L;s++) {
499:           if (ctx->useconj) temp2[s+(j+k*ctx->L)*ctx->L] += PetscRealPart(alp*temp[s+(j+i*ctx->L)*ctx->L])*2;
500:           else temp2[s+(j+k*ctx->L)*ctx->L] += alp*temp[s+(j+i*ctx->L)*ctx->L];
501:         }
502:       }
503:     }
504:     for (i=0;i<ctx->num_solve_point;i++)
505:       ppk[i] *= ctx->pp[i*ctx->subcomm->n + ctx->subcomm_id];
506:   }
507:   for (i=0;i<2*ctx->M*ctx->L*ctx->L;i++) temp2[i] /= sub_size;
508:   PetscMPIIntCast(2*ctx->M*ctx->L*ctx->L,&len);
509:   MPI_Allreduce(temp2,Mu,len,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)eps));
510:   PetscFree3(temp,temp2,ppk);
511:   MatDestroy(&M);
512:   return(0);
513: }

515: static PetscErrorCode BlockHankel(EPS eps,PetscScalar *Mu,PetscInt s,PetscScalar *H)
516: {
517:   EPS_CISS *ctx = (EPS_CISS*)eps->data;
518:   PetscInt i,j,k,L=ctx->L,M=ctx->M;

521:   for (k=0;k<L*M;k++)
522:     for (j=0;j<M;j++)
523:       for (i=0;i<L;i++)
524:         H[j*L+i+k*L*M] = Mu[i+k*L+(j+s)*L*L];
525:   return(0);
526: }

528: static PetscErrorCode SVD_H0(EPS eps,PetscScalar *S,PetscInt *K)
529: {
531:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
532:   PetscInt       i,ml=ctx->L*ctx->M;
533:   PetscBLASInt   m,n,lda,ldu,ldvt,lwork,info;
534:   PetscScalar    *work;
535: #if defined(PETSC_USE_COMPLEX)
536:   PetscReal      *rwork;
537: #endif

540:   PetscMalloc1(5*ml,&work);
541: #if defined(PETSC_USE_COMPLEX)
542:   PetscMalloc1(5*ml,&rwork);
543: #endif
544:   PetscBLASIntCast(ml,&m);
545:   n = m; lda = m; ldu = m; ldvt = m; lwork = 5*m;
546:   PetscFPTrapPush(PETSC_FP_TRAP_OFF);
547: #if defined(PETSC_USE_COMPLEX)
548:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","N",&m,&n,S,&lda,ctx->sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,rwork,&info));
549: #else
550:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","N",&m,&n,S,&lda,ctx->sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,&info));
551: #endif
552:   SlepcCheckLapackInfo("gesvd",info);
553:   PetscFPTrapPop();
554:   (*K) = 0;
555:   for (i=0;i<ml;i++) {
556:     if (ctx->sigma[i]/PetscMax(ctx->sigma[0],1)>ctx->delta) (*K)++;
557:   }
558:   PetscFree(work);
559: #if defined(PETSC_USE_COMPLEX)
560:   PetscFree(rwork);
561: #endif
562:   return(0);
563: }

565: static PetscErrorCode ConstructS(EPS eps)
566: {
568:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
569:   PetscInt       i,j,k,vec_local_size,p_id;
570:   Vec            v,sj,yj;
571:   PetscScalar    *ppk, *v_data, m = 1;

574:   BVGetSizes(ctx->Y,&vec_local_size,NULL,NULL);
575:   PetscMalloc1(ctx->num_solve_point,&ppk);
576:   for (i=0;i<ctx->num_solve_point;i++) ppk[i] = 1;
577:   BVGetColumn(ctx->Y,0,&yj);
578:   VecDuplicate(yj,&v);
579:   BVRestoreColumn(ctx->Y,0,&yj);
580:   for (k=0;k<ctx->M;k++) {
581:     for (j=0;j<ctx->L;j++) {
582:       VecSet(v,0);
583:       for (i=0;i<ctx->num_solve_point;i++) {
584:         p_id = i*ctx->subcomm->n + ctx->subcomm_id;
585:         BVSetActiveColumns(ctx->Y,i*ctx->L_max+j,i*ctx->L_max+j+1);
586:         BVMultVec(ctx->Y,ppk[i]*ctx->weight[p_id],1.0,v,&m);
587:       }
588:       if (ctx->useconj) {
589:         VecGetArray(v,&v_data);
590:         for (i=0;i<vec_local_size;i++) v_data[i] = PetscRealPart(v_data[i])*2;
591:         VecRestoreArray(v,&v_data);
592:       }
593:       BVGetColumn(ctx->S,k*ctx->L+j,&sj);
594:       if (ctx->pA) {
595:         VecSet(sj,0);
596:         VecScatterBegin(ctx->scatterin,v,sj,ADD_VALUES,SCATTER_REVERSE);
597:         VecScatterEnd(ctx->scatterin,v,sj,ADD_VALUES,SCATTER_REVERSE);
598:       } else {
599:         VecCopy(v,sj);
600:       }
601:       BVRestoreColumn(ctx->S,k*ctx->L+j,&sj);
602:     }
603:     for (i=0;i<ctx->num_solve_point;i++) {
604:       p_id = i*ctx->subcomm->n + ctx->subcomm_id;
605:       ppk[i] *= ctx->pp[p_id];
606:     }
607:   }
608:   PetscFree(ppk);
609:   VecDestroy(&v);
610:   return(0);
611: }

613: static PetscErrorCode SVD_S(BV S,PetscInt ml,PetscReal delta,PetscReal *sigma,PetscInt *K)
614: {
616:   PetscInt       i,j,k,local_size;
617:   PetscMPIInt    len;
618:   PetscScalar    *work,*temp,*B,*tempB,*s_data,*Q1,*Q2,*temp2,alpha=1,beta=0;
619:   PetscBLASInt   l,m,n,lda,ldu,ldvt,lwork,info,ldb,ldc;
620: #if defined(PETSC_USE_COMPLEX)
621:   PetscReal      *rwork;
622: #endif

625:   BVGetSizes(S,&local_size,NULL,NULL);
626:   BVGetArray(S,&s_data);
627:   PetscMalloc7(ml*ml,&temp,ml*ml,&temp2,local_size*ml,&Q1,local_size*ml,&Q2,ml*ml,&B,ml*ml,&tempB,5*ml,&work);
628:   PetscArrayzero(B,ml*ml);
629: #if defined(PETSC_USE_COMPLEX)
630:   PetscMalloc1(5*ml,&rwork);
631: #endif
632:   PetscFPTrapPush(PETSC_FP_TRAP_OFF);

634:   for (i=0;i<ml;i++) B[i*ml+i]=1;

636:   for (k=0;k<2;k++) {
637:     PetscBLASIntCast(local_size,&m);
638:     PetscBLASIntCast(ml,&l);
639:     n = l; lda = m; ldb = m; ldc = l;
640:     if (k == 0) {
641:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,s_data,&lda,s_data,&ldb,&beta,temp,&ldc));
642:     } else if ((k%2)==1) {
643:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,Q1,&lda,Q1,&ldb,&beta,temp,&ldc));
644:     } else {
645:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,Q2,&lda,Q2,&ldb,&beta,temp,&ldc));
646:     }
647:     PetscArrayzero(temp2,ml*ml);
648:     PetscMPIIntCast(ml*ml,&len);
649:     MPI_Allreduce(temp,temp2,len,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)S));

651:     PetscBLASIntCast(ml,&m);
652:     n = m; lda = m; lwork = 5*m, ldu = 1; ldvt = 1;
653: #if defined(PETSC_USE_COMPLEX)
654:     PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&m,&n,temp2,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,rwork,&info));
655: #else
656:     PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&m,&n,temp2,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,&info));
657: #endif
658:     SlepcCheckLapackInfo("gesvd",info);

660:     PetscBLASIntCast(local_size,&l);
661:     PetscBLASIntCast(ml,&n);
662:     m = n; lda = l; ldb = m; ldc = l;
663:     if (k==0) {
664:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,s_data,&lda,temp2,&ldb,&beta,Q1,&ldc));
665:     } else if ((k%2)==1) {
666:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,Q1,&lda,temp2,&ldb,&beta,Q2,&ldc));
667:     } else {
668:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,Q2,&lda,temp2,&ldb,&beta,Q1,&ldc));
669:     }

671:     PetscBLASIntCast(ml,&l);
672:     m = l; n = l; lda = l; ldb = m; ldc = l;
673:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,B,&lda,temp2,&ldb,&beta,tempB,&ldc));
674:     for (i=0;i<ml;i++) {
675:       sigma[i] = sqrt(sigma[i]);
676:       for (j=0;j<local_size;j++) {
677:         if ((k%2)==1) Q2[j+i*local_size]/=sigma[i];
678:         else Q1[j+i*local_size]/=sigma[i];
679:       }
680:       for (j=0;j<ml;j++) {
681:         B[j+i*ml]=tempB[j+i*ml]*sigma[i];
682:       }
683:     }
684:   }

686:   PetscBLASIntCast(ml,&m);
687:   n = m; lda = m; ldu=1; ldvt=1;
688: #if defined(PETSC_USE_COMPLEX)
689:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&m,&n,B,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,rwork,&info));
690: #else
691:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&m,&n,B,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,&info));
692: #endif
693:   SlepcCheckLapackInfo("gesvd",info);

695:   PetscBLASIntCast(local_size,&l);
696:   PetscBLASIntCast(ml,&n);
697:   m = n; lda = l; ldb = m; ldc = l;
698:   if ((k%2)==1) {
699:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&l,&n,&m,&alpha,Q1,&lda,B,&ldb,&beta,s_data,&ldc));
700:   } else {
701:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&l,&n,&m,&alpha,Q2,&lda,B,&ldb,&beta,s_data,&ldc));
702:   }

704:   PetscFPTrapPop();
705:   BVRestoreArray(S,&s_data);

707:   (*K) = 0;
708:   for (i=0;i<ml;i++) {
709:     if (sigma[i]/PetscMax(sigma[0],1)>delta) (*K)++;
710:   }
711:   PetscFree7(temp,temp2,Q1,Q2,B,tempB,work);
712: #if defined(PETSC_USE_COMPLEX)
713:   PetscFree(rwork);
714: #endif
715:   return(0);
716: }

718: static PetscErrorCode isGhost(EPS eps,PetscInt ld,PetscInt nv,PetscBool *fl)
719: {
721:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
722:   PetscInt       i,j;
723:   PetscScalar    *pX;
724:   PetscReal      *tau,s1,s2,tau_max=0.0;

727:   PetscMalloc1(nv,&tau);
728:   DSVectors(eps->ds,DS_MAT_X,NULL,NULL);
729:   DSGetArray(eps->ds,DS_MAT_X,&pX);

731:   for (i=0;i<nv;i++) {
732:     s1 = 0;
733:     s2 = 0;
734:     for (j=0;j<nv;j++) {
735:       s1 += PetscAbsScalar(PetscPowScalarInt(pX[i*ld+j],2));
736:       s2 += PetscPowRealInt(PetscAbsScalar(pX[i*ld+j]),2)/ctx->sigma[j];
737:     }
738:     tau[i] = s1/s2;
739:     tau_max = PetscMax(tau_max,tau[i]);
740:   }
741:   DSRestoreArray(eps->ds,DS_MAT_X,&pX);
742:   for (i=0;i<nv;i++) {
743:     tau[i] /= tau_max;
744:   }
745:   for (i=0;i<nv;i++) {
746:     if (tau[i]>=ctx->spurious_threshold) fl[i] = PETSC_TRUE;
747:     else fl[i] = PETSC_FALSE;
748:   }
749:   PetscFree(tau);
750:   return(0);
751: }

753: static PetscErrorCode rescale_eig(EPS eps,PetscInt nv)
754: {
756:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
757:   PetscInt       i;
758:   PetscScalar    center;
759:   PetscReal      radius,a,b,c,d,rgscale;
760: #if defined(PETSC_USE_COMPLEX)
761:   PetscReal      start_ang,end_ang,vscale,theta;
762: #endif
763:   PetscBool      isring,isellipse,isinterval;

766:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
767:   PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring);
768:   PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval);
769:   RGGetScale(eps->rg,&rgscale);
770:   if (isinterval) {
771:     RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d);
772:     if (c==d) {
773:       for (i=0;i<nv;i++) {
774: #if defined(PETSC_USE_COMPLEX)
775:         eps->eigr[i] = PetscRealPart(eps->eigr[i]);
776: #else
777:         eps->eigi[i] = 0;
778: #endif
779:       }
780:     }
781:   }
782:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
783:     if (isellipse) {
784:       RGEllipseGetParameters(eps->rg,&center,&radius,NULL);
785:       for (i=0;i<nv;i++) eps->eigr[i] = rgscale*(center + radius*eps->eigr[i]);
786:     } else if (isinterval) {
787:       RGIntervalGetEndpoints(eps->rg,&a,&b,&c,&d);
788:       if (ctx->quad == EPS_CISS_QUADRULE_CHEBYSHEV) {
789:         for (i=0;i<nv;i++) {
790:           if (c==d) eps->eigr[i] = ((b-a)*(eps->eigr[i]+1.0)/2.0+a)*rgscale;
791:           if (a==b) {
792: #if defined(PETSC_USE_COMPLEX)
793:             eps->eigr[i] = ((d-c)*(eps->eigr[i]+1.0)/2.0+c)*rgscale*PETSC_i;
794: #else
795:             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Integration points on a vertical line require complex arithmetic");
796: #endif
797:           }
798:         }
799:       } else {
800:         center = (b+a)/2.0+(d+c)/2.0*PETSC_PI;
801:         radius = PetscSqrtReal(PetscPowRealInt((b-a)/2.0,2)+PetscPowRealInt((d-c)/2.0,2));
802:         for (i=0;i<nv;i++) eps->eigr[i] = center + radius*eps->eigr[i];
803:       }
804:     } else if (isring) {  /* only supported in complex scalars */
805: #if defined(PETSC_USE_COMPLEX)
806:       RGRingGetParameters(eps->rg,&center,&radius,&vscale,&start_ang,&end_ang,NULL);
807:       if (ctx->quad == EPS_CISS_QUADRULE_CHEBYSHEV) {
808:         for (i=0;i<nv;i++) {
809:           theta = (start_ang*2.0+(end_ang-start_ang)*(PetscRealPart(eps->eigr[i])+1.0))*PETSC_PI;
810:           eps->eigr[i] = rgscale*center + (rgscale*radius+PetscImaginaryPart(eps->eigr[i]))*PetscCMPLX(PetscCosReal(theta),vscale*PetscSinReal(theta));
811:         }
812:       } else {
813:         for (i=0;i<nv;i++) eps->eigr[i] = rgscale*(center + radius*eps->eigr[i]);
814:       }
815: #endif
816:     }
817:   }
818:   return(0);
819: }

821: PetscErrorCode EPSSetUp_CISS(EPS eps)
822: {
824:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
825:   PetscBool      issinvert,istrivial,isring,isellipse,isinterval,flg,useconj;
826:   PetscReal      c,d;
827:   Mat            A;

830:   if (!eps->ncv) {
831:     eps->ncv = ctx->L_max*ctx->M;
832:     if (eps->ncv>eps->n) {
833:       eps->ncv = eps->n;
834:       ctx->L_max = eps->ncv/ctx->M;
835:       if (!ctx->L_max) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Cannot adjust solver parameters, try setting a smaller value of M (moment size)");
836:     }
837:   } else {
838:     EPSSetDimensions_Default(eps,eps->nev,&eps->ncv,&eps->mpd);
839:     ctx->L_max = eps->ncv/ctx->M;
840:     if (!ctx->L_max) {
841:       ctx->L_max = 1;
842:       eps->ncv = ctx->L_max*ctx->M;
843:     }
844:   }
845:   ctx->L = PetscMin(ctx->L,ctx->L_max);
846:   if (!eps->max_it) eps->max_it = 1;
847:   if (!eps->mpd) eps->mpd = eps->ncv;
848:   if (!eps->which) eps->which = EPS_ALL;
849:   if (!eps->extraction) { EPSSetExtraction(eps,EPS_RITZ); }
850:   else if (eps->extraction!=EPS_RITZ) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Unsupported extraction type");
851:   if (eps->arbitrary) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Arbitrary selection of eigenpairs not supported in this solver");
852:   if (eps->stopping!=EPSStoppingBasic) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"This solver does not support user-defined stopping test");

854:   /* check region */
855:   RGIsTrivial(eps->rg,&istrivial);
856:   if (istrivial) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"CISS requires a nontrivial region, e.g. -rg_type ellipse ...");
857:   RGGetComplement(eps->rg,&flg);
858:   if (flg) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"A region with complement flag set is not allowed");
859:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
860:   PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring);
861:   PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval);
862:   if (!isellipse && !isring && !isinterval) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Currently only implemented for interval, elliptic or ring regions");
863:   /* if useconj has changed, then reset subcomm data */
864:   EPSCISSSetUseConj(eps,&useconj);
865:   if (useconj!=ctx->useconj) { EPSCISSResetSubcomm(eps); }

867: #if !defined(PETSC_USE_COMPLEX)
868:   if (isring) {
869:     SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Ring region only supported for complex scalars");
870:   }
871: #endif
872:   if (isinterval) {
873:     RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d);
874: #if !defined(PETSC_USE_COMPLEX)
875:     if (c!=d || c!=0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"In real scalars, endpoints of the imaginary axis must be both zero");
876: #endif
877:     if (!ctx->quad && c==d) ctx->quad = EPS_CISS_QUADRULE_CHEBYSHEV;
878:   }
879:   if (!ctx->quad) ctx->quad = EPS_CISS_QUADRULE_TRAPEZOIDAL;

881:   /* create split comm */
882:   if (!ctx->subcomm) { EPSCISSSetUpSubComm(eps,&ctx->num_solve_point); }

884:   EPSAllocateSolution(eps,0);
885:   if (ctx->weight) { PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma); }
886:   PetscMalloc4(ctx->N,&ctx->weight,ctx->N+1,&ctx->omega,ctx->N,&ctx->pp,ctx->L_max*ctx->M,&ctx->sigma);
887:   PetscLogObjectMemory((PetscObject)eps,3*ctx->N*sizeof(PetscScalar)+ctx->L_max*ctx->N*sizeof(PetscReal));

889:   /* allocate basis vectors */
890:   BVDestroy(&ctx->S);
891:   BVDuplicateResize(eps->V,ctx->L_max*ctx->M,&ctx->S);
892:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->S);
893:   BVDestroy(&ctx->V);
894:   BVDuplicateResize(eps->V,ctx->L_max,&ctx->V);
895:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->V);

897:   STGetMatrix(eps->st,0,&A);
898:   PetscObjectTypeCompare((PetscObject)A,MATSHELL,&flg);
899:   if (flg) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Matrix type shell is not supported in this solver");

901:   if (!ctx->usest_set) ctx->usest = (ctx->npart>1)? PETSC_FALSE: PETSC_TRUE;
902:   if (ctx->usest && ctx->npart>1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"The usest flag is not supported when partitions > 1");

904:   CISSRedundantMat(eps);
905:   if (ctx->pA) {
906:     CISSScatterVec(eps);
907:     BVDestroy(&ctx->pV);
908:     BVCreate(PetscObjectComm((PetscObject)ctx->xsub),&ctx->pV);
909:     BVSetSizesFromVec(ctx->pV,ctx->xsub,eps->n);
910:     BVSetFromOptions(ctx->pV);
911:     BVResize(ctx->pV,ctx->L_max,PETSC_FALSE);
912:     PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->pV);
913:   }

915:   if (ctx->usest) {
916:     PetscObjectTypeCompare((PetscObject)eps->st,STSINVERT,&issinvert);
917:     if (!issinvert) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"If the usest flag is set, you must select the STSINVERT spectral transformation");
918:   }

920:   BVDestroy(&ctx->Y);
921:   if (ctx->pA) {
922:     BVCreate(PetscObjectComm((PetscObject)ctx->xsub),&ctx->Y);
923:     BVSetSizesFromVec(ctx->Y,ctx->xsub,eps->n);
924:     BVSetFromOptions(ctx->Y);
925:     BVResize(ctx->Y,ctx->num_solve_point*ctx->L_max,PETSC_FALSE);
926:   } else {
927:     BVDuplicateResize(eps->V,ctx->num_solve_point*ctx->L_max,&ctx->Y);
928:   }
929:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->Y);

931:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
932:     DSSetType(eps->ds,DSGNHEP);
933:   } else if (eps->isgeneralized) {
934:     if (eps->ishermitian && eps->ispositive) {
935:       DSSetType(eps->ds,DSGHEP);
936:     } else {
937:       DSSetType(eps->ds,DSGNHEP);
938:     }
939:   } else {
940:     if (eps->ishermitian) {
941:       DSSetType(eps->ds,DSHEP);
942:     } else {
943:       DSSetType(eps->ds,DSNHEP);
944:     }
945:   }
946:   DSAllocate(eps->ds,eps->ncv);
947:   EPSSetWorkVecs(eps,2);

949: #if !defined(PETSC_USE_COMPLEX)
950:   if (!eps->ishermitian) { PetscInfo(eps,"Warning: complex eigenvalues are not calculated exactly without --with-scalar-type=complex in PETSc\n"); }
951: #endif
952:   return(0);
953: }

955: PetscErrorCode EPSSolve_CISS(EPS eps)
956: {
958:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
959:   Mat            A,B,X,M,pA,pB;
960:   PetscInt       i,j,ld,nmat,L_add=0,nv=0,L_base=ctx->L,inner,nlocal,*inside;
961:   PetscScalar    *Mu,*H0,*H1=NULL,*rr,*temp;
962:   PetscReal      error,max_error;
963:   PetscBool      *fl1;
964:   Vec            si,w[3];
965:   SlepcSC        sc;
966:   PetscRandom    rand;
967: #if defined(PETSC_USE_COMPLEX)
968:   PetscBool      isellipse;
969: #endif

972:   w[0] = eps->work[0];
973:   w[1] = NULL;
974:   w[2] = eps->work[1];
975:   /* override SC settings */
976:   DSGetSlepcSC(eps->ds,&sc);
977:   sc->comparison    = SlepcCompareLargestMagnitude;
978:   sc->comparisonctx = NULL;
979:   sc->map           = NULL;
980:   sc->mapobj        = NULL;
981:   VecGetLocalSize(w[0],&nlocal);
982:   DSGetLeadingDimension(eps->ds,&ld);
983:   STGetNumMatrices(eps->st,&nmat);
984:   STGetMatrix(eps->st,0,&A);
985:   if (nmat>1) { STGetMatrix(eps->st,1,&B); }
986:   else B = NULL;
987:   SetPathParameter(eps);
988:   CISSVecSetRandom(ctx->V,0,ctx->L);
989:   BVGetRandomContext(ctx->V,&rand);

991:   if (ctx->pA) {
992:     VecScatterVecs(eps,ctx->V,ctx->L);
993:     SolveLinearSystem(eps,ctx->pA,ctx->pB,ctx->pV,0,ctx->L,PETSC_TRUE);
994:   } else {
995:     SolveLinearSystem(eps,A,B,ctx->V,0,ctx->L,PETSC_TRUE);
996:   }
997: #if defined(PETSC_USE_COMPLEX)
998:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
999:   if (isellipse) {
1000:     EstimateNumberEigs(eps,&L_add);
1001:   } else {
1002:     L_add = 0;
1003:   }
1004: #else
1005:   L_add = 0;
1006: #endif
1007:   if (L_add>0) {
1008:     PetscInfo2(eps,"Changing L %D -> %D by Estimate #Eig\n",ctx->L,ctx->L+L_add);
1009:     CISSVecSetRandom(ctx->V,ctx->L,ctx->L+L_add);
1010:     if (ctx->pA) {
1011:       VecScatterVecs(eps,ctx->V,ctx->L+L_add);
1012:       SolveLinearSystem(eps,ctx->pA,ctx->pB,ctx->pV,ctx->L,ctx->L+L_add,PETSC_FALSE);
1013:     } else {
1014:       SolveLinearSystem(eps,A,B,ctx->V,ctx->L,ctx->L+L_add,PETSC_FALSE);
1015:     }
1016:     ctx->L += L_add;
1017:   }
1018:   PetscMalloc2(ctx->L*ctx->L*ctx->M*2,&Mu,ctx->L*ctx->M*ctx->L*ctx->M,&H0);
1019:   for (i=0;i<ctx->refine_blocksize;i++) {
1020:     CalcMu(eps,Mu);
1021:     BlockHankel(eps,Mu,0,H0);
1022:     SVD_H0(eps,H0,&nv);
1023:     if (ctx->sigma[0]<=ctx->delta || nv < ctx->L*ctx->M || ctx->L == ctx->L_max) break;
1024:     L_add = L_base;
1025:     if (ctx->L+L_add>ctx->L_max) L_add = ctx->L_max-ctx->L;
1026:     PetscInfo2(eps,"Changing L %D -> %D by SVD(H0)\n",ctx->L,ctx->L+L_add);
1027:     CISSVecSetRandom(ctx->V,ctx->L,ctx->L+L_add);
1028:     if (ctx->pA) {
1029:       VecScatterVecs(eps,ctx->V,ctx->L+L_add);
1030:       SolveLinearSystem(eps,ctx->pA,ctx->pB,ctx->pV,ctx->L,ctx->L+L_add,PETSC_FALSE);
1031:     } else {
1032:       SolveLinearSystem(eps,A,B,ctx->V,ctx->L,ctx->L+L_add,PETSC_FALSE);
1033:     }
1034:     ctx->L += L_add;
1035:   }
1036:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1037:     PetscMalloc1(ctx->L*ctx->M*ctx->L*ctx->M,&H1);
1038:   }

1040:   while (eps->reason == EPS_CONVERGED_ITERATING) {
1041:     eps->its++;
1042:     for (inner=0;inner<=ctx->refine_inner;inner++) {
1043:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1044:         CalcMu(eps,Mu);
1045:         BlockHankel(eps,Mu,0,H0);
1046:         SVD_H0(eps,H0,&nv);
1047:         break;
1048:       } else {
1049:         ConstructS(eps);
1050:         BVSetActiveColumns(ctx->S,0,ctx->L);
1051:         BVCopy(ctx->S,ctx->V);
1052:         SVD_S(ctx->S,ctx->L*ctx->M,ctx->delta,ctx->sigma,&nv);
1053:         if (ctx->sigma[0]>ctx->delta && nv==ctx->L*ctx->M && inner!=ctx->refine_inner) {
1054:           if (ctx->pA) {
1055:             VecScatterVecs(eps,ctx->V,ctx->L);
1056:             SolveLinearSystem(eps,ctx->pA,ctx->pB,ctx->pV,0,ctx->L,PETSC_FALSE);
1057:           } else {
1058:             SolveLinearSystem(eps,A,B,ctx->V,0,ctx->L,PETSC_FALSE);
1059:           }
1060:         } else break;
1061:       }
1062:     }
1063:     eps->nconv = 0;
1064:     if (nv == 0) eps->reason = EPS_CONVERGED_TOL;
1065:     else {
1066:       DSSetDimensions(eps->ds,nv,0,0,0);
1067:       DSSetState(eps->ds,DS_STATE_RAW);

1069:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1070:         BlockHankel(eps,Mu,0,H0);
1071:         BlockHankel(eps,Mu,1,H1);
1072:         DSGetArray(eps->ds,DS_MAT_A,&temp);
1073:         for (j=0;j<nv;j++) {
1074:           for (i=0;i<nv;i++) {
1075:             temp[i+j*ld] = H1[i+j*ctx->L*ctx->M];
1076:           }
1077:         }
1078:         DSRestoreArray(eps->ds,DS_MAT_A,&temp);
1079:         DSGetArray(eps->ds,DS_MAT_B,&temp);
1080:         for (j=0;j<nv;j++) {
1081:           for (i=0;i<nv;i++) {
1082:             temp[i+j*ld] = H0[i+j*ctx->L*ctx->M];
1083:           }
1084:         }
1085:         DSRestoreArray(eps->ds,DS_MAT_B,&temp);
1086:       } else {
1087:         BVSetActiveColumns(ctx->S,0,nv);
1088:         DSGetMat(eps->ds,DS_MAT_A,&pA);
1089:         MatZeroEntries(pA);
1090:         BVMatProject(ctx->S,A,ctx->S,pA);
1091:         DSRestoreMat(eps->ds,DS_MAT_A,&pA);
1092:         if (B) {
1093:           DSGetMat(eps->ds,DS_MAT_B,&pB);
1094:           MatZeroEntries(pB);
1095:           BVMatProject(ctx->S,B,ctx->S,pB);
1096:           DSRestoreMat(eps->ds,DS_MAT_B,&pB);
1097:         }
1098:       }

1100:       DSSolve(eps->ds,eps->eigr,eps->eigi);
1101:       DSSynchronize(eps->ds,eps->eigr,eps->eigi);

1103:       PetscMalloc3(nv,&fl1,nv,&inside,nv,&rr);
1104:       rescale_eig(eps,nv);
1105:       isGhost(eps,ld,nv,fl1);
1106:       RGCheckInside(eps->rg,nv,eps->eigr,eps->eigi,inside);
1107:       for (i=0;i<nv;i++) {
1108:         if (fl1[i] && inside[i]>=0) {
1109:           rr[i] = 1.0;
1110:           eps->nconv++;
1111:         } else rr[i] = 0.0;
1112:       }
1113:       DSSort(eps->ds,eps->eigr,eps->eigi,rr,NULL,&eps->nconv);
1114:       DSSynchronize(eps->ds,eps->eigr,eps->eigi);
1115:       rescale_eig(eps,nv);
1116:       PetscFree3(fl1,inside,rr);
1117:       BVSetActiveColumns(eps->V,0,nv);
1118:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1119:         ConstructS(eps);
1120:         BVSetActiveColumns(ctx->S,0,ctx->L);
1121:         BVCopy(ctx->S,ctx->V);
1122:         BVSetActiveColumns(ctx->S,0,nv);
1123:       }
1124:       BVCopy(ctx->S,eps->V);

1126:       DSVectors(eps->ds,DS_MAT_X,NULL,NULL);
1127:       DSGetMat(eps->ds,DS_MAT_X,&X);
1128:       BVMultInPlace(ctx->S,X,0,eps->nconv);
1129:       if (eps->ishermitian) {
1130:         BVMultInPlace(eps->V,X,0,eps->nconv);
1131:       }
1132:       MatDestroy(&X);
1133:       max_error = 0.0;
1134:       for (i=0;i<eps->nconv;i++) {
1135:         BVGetColumn(ctx->S,i,&si);
1136:         EPSComputeResidualNorm_Private(eps,PETSC_FALSE,eps->eigr[i],eps->eigi[i],si,NULL,w,&error);
1137:         (*eps->converged)(eps,eps->eigr[i],eps->eigi[i],error,&error,eps->convergedctx);
1138:         BVRestoreColumn(ctx->S,i,&si);
1139:         max_error = PetscMax(max_error,error);
1140:       }

1142:       if (max_error <= eps->tol) eps->reason = EPS_CONVERGED_TOL;
1143:       else if (eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
1144:       else {
1145:         if (eps->nconv > ctx->L) {
1146:           MatCreateSeqDense(PETSC_COMM_SELF,eps->nconv,ctx->L,NULL,&M);
1147:           MatDenseGetArray(M,&temp);
1148:           for (i=0;i<ctx->L*eps->nconv;i++) {
1149:             PetscRandomGetValue(rand,&temp[i]);
1150:             temp[i] = PetscRealPart(temp[i]);
1151:           }
1152:           MatDenseRestoreArray(M,&temp);
1153:           BVSetActiveColumns(ctx->S,0,eps->nconv);
1154:           BVMultInPlace(ctx->S,M,0,ctx->L);
1155:           MatDestroy(&M);
1156:           BVSetActiveColumns(ctx->S,0,ctx->L);
1157:           BVCopy(ctx->S,ctx->V);
1158:         }
1159:         if (ctx->pA) {
1160:           VecScatterVecs(eps,ctx->V,ctx->L);
1161:           SolveLinearSystem(eps,ctx->pA,ctx->pB,ctx->pV,0,ctx->L,PETSC_FALSE);
1162:         } else {
1163:           SolveLinearSystem(eps,A,B,ctx->V,0,ctx->L,PETSC_FALSE);
1164:         }
1165:       }
1166:     }
1167:   }
1168:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1169:     PetscFree(H1);
1170:   }
1171:   PetscFree2(Mu,H0);
1172:   return(0);
1173: }

1175: static PetscErrorCode EPSCISSSetSizes_CISS(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
1176: {
1178:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1179:   PetscInt       oN,onpart;

1182:   oN = ctx->N;
1183:   if (ip == PETSC_DECIDE || ip == PETSC_DEFAULT) {
1184:     if (ctx->N!=32) { ctx->N =32; ctx->M = ctx->N/4; }
1185:   } else {
1186:     if (ip<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be > 0");
1187:     if (ip%2) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be an even number");
1188:     if (ctx->N!=ip) { ctx->N = ip; ctx->M = ctx->N/4; }
1189:   }
1190:   if (bs == PETSC_DECIDE || bs == PETSC_DEFAULT) {
1191:     ctx->L = 16;
1192:   } else {
1193:     if (bs<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bs argument must be > 0");
1194:     ctx->L = bs;
1195:   }
1196:   if (ms == PETSC_DECIDE || ms == PETSC_DEFAULT) {
1197:     ctx->M = ctx->N/4;
1198:   } else {
1199:     if (ms<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be > 0");
1200:     if (ms>ctx->N) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be less than or equal to the number of integration points");
1201:     ctx->M = ms;
1202:   }
1203:   onpart = ctx->npart;
1204:   if (npart == PETSC_DECIDE || npart == PETSC_DEFAULT) {
1205:     ctx->npart = 1;
1206:   } else {
1207:     if (npart<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The npart argument must be > 0");
1208:     ctx->npart = npart;
1209:   }
1210:   if (bsmax == PETSC_DECIDE || bsmax == PETSC_DEFAULT) {
1211:     ctx->L_max = 64;
1212:   } else {
1213:     if (bsmax<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bsmax argument must be > 0");
1214:     ctx->L_max = PetscMax(bsmax,ctx->L);
1215:   }
1216:   if (onpart != ctx->npart || oN != ctx->N || realmats != ctx->isreal) { EPSCISSResetSubcomm(eps); }
1217:   ctx->isreal = realmats;
1218:   eps->state = EPS_STATE_INITIAL;
1219:   return(0);
1220: }

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

1225:    Logically Collective on eps

1227:    Input Parameters:
1228: +  eps   - the eigenproblem solver context
1229: .  ip    - number of integration points
1230: .  bs    - block size
1231: .  ms    - moment size
1232: .  npart - number of partitions when splitting the communicator
1233: .  bsmax - max block size
1234: -  realmats - A and B are real

1236:    Options Database Keys:
1237: +  -eps_ciss_integration_points - Sets the number of integration points
1238: .  -eps_ciss_blocksize - Sets the block size
1239: .  -eps_ciss_moments - Sets the moment size
1240: .  -eps_ciss_partitions - Sets the number of partitions
1241: .  -eps_ciss_maxblocksize - Sets the maximum block size
1242: -  -eps_ciss_realmats - A and B are real

1244:    Note:
1245:    The default number of partitions is 1. This means the internal KSP object is shared
1246:    among all processes of the EPS communicator. Otherwise, the communicator is split
1247:    into npart communicators, so that npart KSP solves proceed simultaneously.

1249:    Level: advanced

1251: .seealso: EPSCISSGetSizes()
1252: @*/
1253: PetscErrorCode EPSCISSSetSizes(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
1254: {

1265:   PetscTryMethod(eps,"EPSCISSSetSizes_C",(EPS,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool),(eps,ip,bs,ms,npart,bsmax,realmats));
1266:   return(0);
1267: }

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

1274:   if (ip) *ip = ctx->N;
1275:   if (bs) *bs = ctx->L;
1276:   if (ms) *ms = ctx->M;
1277:   if (npart) *npart = ctx->npart;
1278:   if (bsmax) *bsmax = ctx->L_max;
1279:   if (realmats) *realmats = ctx->isreal;
1280:   return(0);
1281: }

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

1286:    Not Collective

1288:    Input Parameter:
1289: .  eps - the eigenproblem solver context

1291:    Output Parameters:
1292: +  ip    - number of integration points
1293: .  bs    - block size
1294: .  ms    - moment size
1295: .  npart - number of partitions when splitting the communicator
1296: .  bsmax - max block size
1297: -  realmats - A and B are real

1299:    Level: advanced

1301: .seealso: EPSCISSSetSizes()
1302: @*/
1303: PetscErrorCode EPSCISSGetSizes(EPS eps,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
1304: {

1309:   PetscUseMethod(eps,"EPSCISSGetSizes_C",(EPS,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscBool*),(eps,ip,bs,ms,npart,bsmax,realmats));
1310:   return(0);
1311: }

1313: static PetscErrorCode EPSCISSSetThreshold_CISS(EPS eps,PetscReal delta,PetscReal spur)
1314: {
1315:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1318:   if (delta == PETSC_DEFAULT) {
1319:     ctx->delta = 1e-12;
1320:   } else {
1321:     if (delta<=0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The delta argument must be > 0.0");
1322:     ctx->delta = delta;
1323:   }
1324:   if (spur == PETSC_DEFAULT) {
1325:     ctx->spurious_threshold = 1e-4;
1326:   } else {
1327:     if (spur<=0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The spurious threshold argument must be > 0.0");
1328:     ctx->spurious_threshold = spur;
1329:   }
1330:   return(0);
1331: }

1333: /*@
1334:    EPSCISSSetThreshold - Sets the values of various threshold parameters in
1335:    the CISS solver.

1337:    Logically Collective on eps

1339:    Input Parameters:
1340: +  eps   - the eigenproblem solver context
1341: .  delta - threshold for numerical rank
1342: -  spur  - spurious threshold (to discard spurious eigenpairs)

1344:    Options Database Keys:
1345: +  -eps_ciss_delta - Sets the delta
1346: -  -eps_ciss_spurious_threshold - Sets the spurious threshold

1348:    Level: advanced

1350: .seealso: EPSCISSGetThreshold()
1351: @*/
1352: PetscErrorCode EPSCISSSetThreshold(EPS eps,PetscReal delta,PetscReal spur)
1353: {

1360:   PetscTryMethod(eps,"EPSCISSSetThreshold_C",(EPS,PetscReal,PetscReal),(eps,delta,spur));
1361:   return(0);
1362: }

1364: static PetscErrorCode EPSCISSGetThreshold_CISS(EPS eps,PetscReal *delta,PetscReal *spur)
1365: {
1366:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1369:   if (delta) *delta = ctx->delta;
1370:   if (spur)  *spur = ctx->spurious_threshold;
1371:   return(0);
1372: }

1374: /*@
1375:    EPSCISSGetThreshold - Gets the values of various threshold parameters
1376:    in the CISS solver.

1378:    Not Collective

1380:    Input Parameter:
1381: .  eps - the eigenproblem solver context

1383:    Output Parameters:
1384: +  delta - threshold for numerical rank
1385: -  spur  - spurious threshold (to discard spurious eigenpairs)

1387:    Level: advanced

1389: .seealso: EPSCISSSetThreshold()
1390: @*/
1391: PetscErrorCode EPSCISSGetThreshold(EPS eps,PetscReal *delta,PetscReal *spur)
1392: {

1397:   PetscUseMethod(eps,"EPSCISSGetThreshold_C",(EPS,PetscReal*,PetscReal*),(eps,delta,spur));
1398:   return(0);
1399: }

1401: static PetscErrorCode EPSCISSSetRefinement_CISS(EPS eps,PetscInt inner,PetscInt blsize)
1402: {
1403:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1406:   if (inner == PETSC_DEFAULT) {
1407:     ctx->refine_inner = 0;
1408:   } else {
1409:     if (inner<0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine inner argument must be >= 0");
1410:     ctx->refine_inner = inner;
1411:   }
1412:   if (blsize == PETSC_DEFAULT) {
1413:     ctx->refine_blocksize = 0;
1414:   } else {
1415:     if (blsize<0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine blocksize argument must be >= 0");
1416:     ctx->refine_blocksize = blsize;
1417:   }
1418:   return(0);
1419: }

1421: /*@
1422:    EPSCISSSetRefinement - Sets the values of various refinement parameters
1423:    in the CISS solver.

1425:    Logically Collective on eps

1427:    Input Parameters:
1428: +  eps    - the eigenproblem solver context
1429: .  inner  - number of iterative refinement iterations (inner loop)
1430: -  blsize - number of iterative refinement iterations (blocksize loop)

1432:    Options Database Keys:
1433: +  -eps_ciss_refine_inner - Sets number of inner iterations
1434: -  -eps_ciss_refine_blocksize - Sets number of blocksize iterations

1436:    Level: advanced

1438: .seealso: EPSCISSGetRefinement()
1439: @*/
1440: PetscErrorCode EPSCISSSetRefinement(EPS eps,PetscInt inner,PetscInt blsize)
1441: {

1448:   PetscTryMethod(eps,"EPSCISSSetRefinement_C",(EPS,PetscInt,PetscInt),(eps,inner,blsize));
1449:   return(0);
1450: }

1452: static PetscErrorCode EPSCISSGetRefinement_CISS(EPS eps,PetscInt *inner,PetscInt *blsize)
1453: {
1454:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1457:   if (inner)  *inner = ctx->refine_inner;
1458:   if (blsize) *blsize = ctx->refine_blocksize;
1459:   return(0);
1460: }

1462: /*@
1463:    EPSCISSGetRefinement - Gets the values of various refinement parameters
1464:    in the CISS solver.

1466:    Not Collective

1468:    Input Parameter:
1469: .  eps - the eigenproblem solver context

1471:    Output Parameters:
1472: +  inner  - number of iterative refinement iterations (inner loop)
1473: -  blsize - number of iterative refinement iterations (blocksize loop)

1475:    Level: advanced

1477: .seealso: EPSCISSSetRefinement()
1478: @*/
1479: PetscErrorCode EPSCISSGetRefinement(EPS eps, PetscInt *inner, PetscInt *blsize)
1480: {

1485:   PetscUseMethod(eps,"EPSCISSGetRefinement_C",(EPS,PetscInt*,PetscInt*),(eps,inner,blsize));
1486:   return(0);
1487: }

1489: static PetscErrorCode EPSCISSSetUseST_CISS(EPS eps,PetscBool usest)
1490: {
1491:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1494:   ctx->usest     = usest;
1495:   ctx->usest_set = PETSC_TRUE;
1496:   eps->state     = EPS_STATE_INITIAL;
1497:   return(0);
1498: }

1500: /*@
1501:    EPSCISSSetUseST - Sets a flag indicating that the CISS solver will
1502:    use the ST object for the linear solves.

1504:    Logically Collective on eps

1506:    Input Parameters:
1507: +  eps    - the eigenproblem solver context
1508: -  usest  - boolean flag to use the ST object or not

1510:    Options Database Keys:
1511: .  -eps_ciss_usest <bool> - whether the ST object will be used or not

1513:    Level: advanced

1515: .seealso: EPSCISSGetUseST()
1516: @*/
1517: PetscErrorCode EPSCISSSetUseST(EPS eps,PetscBool usest)
1518: {

1524:   PetscTryMethod(eps,"EPSCISSSetUseST_C",(EPS,PetscBool),(eps,usest));
1525:   return(0);
1526: }

1528: static PetscErrorCode EPSCISSGetUseST_CISS(EPS eps,PetscBool *usest)
1529: {
1530:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1533:   *usest = ctx->usest;
1534:   return(0);
1535: }

1537: /*@
1538:    EPSCISSGetUseST - Gets the flag for using the ST object
1539:    in the CISS solver.

1541:    Not Collective

1543:    Input Parameter:
1544: .  eps - the eigenproblem solver context

1546:    Output Parameters:
1547: .  usest - boolean flag indicating if the ST object is being used

1549:    Level: advanced

1551: .seealso: EPSCISSSetUseST()
1552: @*/
1553: PetscErrorCode EPSCISSGetUseST(EPS eps,PetscBool *usest)
1554: {

1560:   PetscUseMethod(eps,"EPSCISSGetUseST_C",(EPS,PetscBool*),(eps,usest));
1561:   return(0);
1562: }

1564: static PetscErrorCode EPSCISSSetQuadRule_CISS(EPS eps,EPSCISSQuadRule quad)
1565: {
1566:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1569:   ctx->quad = quad;
1570:   return(0);
1571: }

1573: /*@
1574:    EPSCISSSetQuadRule - Sets the quadrature rule used in the CISS solver.

1576:    Logically Collective on eps

1578:    Input Parameters:
1579: +  eps  - the eigenproblem solver context
1580: -  quad - the quadrature rule

1582:    Options Database Key:
1583: .  -eps_ciss_quadrule - Sets the quadrature rule (either 'trapezoidal' or
1584:                            'chebyshev')

1586:    Notes:
1587:    By default, the trapezoidal rule is used (EPS_CISS_QUADRULE_TRAPEZOIDAL).

1589:    If the 'chebyshev' option is specified (EPS_CISS_QUADRULE_CHEBYSHEV), then
1590:    Chebyshev points are used as quadrature points.

1592:    Level: advanced

1594: .seealso: EPSCISSGetQuadRule(), EPSCISSQuadRule
1595: @*/
1596: PetscErrorCode EPSCISSSetQuadRule(EPS eps,EPSCISSQuadRule quad)
1597: {

1603:   PetscTryMethod(eps,"EPSCISSSetQuadRule_C",(EPS,EPSCISSQuadRule),(eps,quad));
1604:   return(0);
1605: }

1607: static PetscErrorCode EPSCISSGetQuadRule_CISS(EPS eps,EPSCISSQuadRule *quad)
1608: {
1609:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1612:   *quad = ctx->quad;
1613:   return(0);
1614: }

1616: /*@
1617:    EPSCISSGetQuadRule - Gets the quadrature rule used in the CISS solver.

1619:    Not Collective

1621:    Input Parameter:
1622: .  eps - the eigenproblem solver context

1624:    Output Parameters:
1625: .  quad - quadrature rule

1627:    Level: advanced

1629: .seealso: EPSCISSSetQuadRule() EPSCISSQuadRule
1630: @*/
1631: PetscErrorCode EPSCISSGetQuadRule(EPS eps,EPSCISSQuadRule *quad)
1632: {

1638:   PetscUseMethod(eps,"EPSCISSGetQuadRule_C",(EPS,EPSCISSQuadRule*),(eps,quad));
1639:   return(0);
1640: }

1642: static PetscErrorCode EPSCISSSetExtraction_CISS(EPS eps,EPSCISSExtraction extraction)
1643: {
1644:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1647:   ctx->extraction = extraction;
1648:   return(0);
1649: }

1651: /*@
1652:    EPSCISSSetExtraction - Sets the extraction technique used in the CISS solver.

1654:    Logically Collective on eps

1656:    Input Parameters:
1657: +  eps        - the eigenproblem solver context
1658: -  extraction - the extraction technique

1660:    Options Database Key:
1661: .  -eps_ciss_extraction - Sets the extraction technique (either 'ritz' or
1662:                            'hankel')

1664:    Notes:
1665:    By default, the Rayleigh-Ritz extraction is used (EPS_CISS_EXTRACTION_RITZ).

1667:    If the 'hankel' option is specified (EPS_CISS_EXTRACTION_HANKEL), then
1668:    the Block Hankel method is used for extracting eigenpairs.

1670:    Level: advanced

1672: .seealso: EPSCISSGetExtraction(), EPSCISSExtraction
1673: @*/
1674: PetscErrorCode EPSCISSSetExtraction(EPS eps,EPSCISSExtraction extraction)
1675: {

1681:   PetscTryMethod(eps,"EPSCISSSetExtraction_C",(EPS,EPSCISSExtraction),(eps,extraction));
1682:   return(0);
1683: }

1685: static PetscErrorCode EPSCISSGetExtraction_CISS(EPS eps,EPSCISSExtraction *extraction)
1686: {
1687:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1690:   *extraction = ctx->extraction;
1691:   return(0);
1692: }

1694: /*@
1695:    EPSCISSGetExtraction - Gets the extraction technique used in the CISS solver.

1697:    Not Collective

1699:    Input Parameter:
1700: .  eps - the eigenproblem solver context

1702:    Output Parameters:
1703: +  extraction - extraction technique

1705:    Level: advanced

1707: .seealso: EPSCISSSetExtraction() EPSCISSExtraction
1708: @*/
1709: PetscErrorCode EPSCISSGetExtraction(EPS eps,EPSCISSExtraction *extraction)
1710: {

1716:   PetscUseMethod(eps,"EPSCISSGetExtraction_C",(EPS,EPSCISSExtraction*),(eps,extraction));
1717:   return(0);
1718: }

1720: static PetscErrorCode EPSCISSGetKSPs_CISS(EPS eps,PetscInt *nsolve,KSP **ksp)
1721: {
1723:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1724:   PetscInt       i;
1725:   PC             pc;

1728:   if (!ctx->ksp) {
1729:     if (!ctx->subcomm) {  /* initialize subcomm first */
1730:       EPSCISSSetUseConj(eps,&ctx->useconj);
1731:       EPSCISSSetUpSubComm(eps,&ctx->num_solve_point);
1732:     }
1733:     PetscMalloc1(ctx->num_solve_point,&ctx->ksp);
1734:     for (i=0;i<ctx->num_solve_point;i++) {
1735:       KSPCreate(PetscSubcommChild(ctx->subcomm),&ctx->ksp[i]);
1736:       PetscObjectIncrementTabLevel((PetscObject)ctx->ksp[i],(PetscObject)eps,1);
1737:       KSPSetOptionsPrefix(ctx->ksp[i],((PetscObject)eps)->prefix);
1738:       KSPAppendOptionsPrefix(ctx->ksp[i],"eps_ciss_");
1739:       PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->ksp[i]);
1740:       PetscObjectSetOptions((PetscObject)ctx->ksp[i],((PetscObject)eps)->options);
1741:       KSPSetErrorIfNotConverged(ctx->ksp[i],PETSC_TRUE);
1742:       KSPSetTolerances(ctx->ksp[i],SLEPC_DEFAULT_TOL,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
1743:       KSPGetPC(ctx->ksp[i],&pc);
1744:       KSPSetType(ctx->ksp[i],KSPPREONLY);
1745:       PCSetType(pc,PCLU);
1746:     }
1747:   }
1748:   if (nsolve) *nsolve = ctx->num_solve_point;
1749:   if (ksp)    *ksp    = ctx->ksp;
1750:   return(0);
1751: }

1753: /*@C
1754:    EPSCISSGetKSPs - Retrieve the array of linear solver objects associated with
1755:    the CISS solver.

1757:    Not Collective

1759:    Input Parameter:
1760: .  eps - the eigenproblem solver solver

1762:    Output Parameters:
1763: +  nsolve - number of solver objects
1764: -  ksp - array of linear solver object

1766:    Notes:
1767:    The number of KSP solvers is equal to the number of integration points divided by
1768:    the number of partitions. This value is halved in the case of real matrices with
1769:    a region centered at the real axis.

1771:    Level: advanced

1773: .seealso: EPSCISSSetSizes()
1774: @*/
1775: PetscErrorCode EPSCISSGetKSPs(EPS eps,PetscInt *nsolve,KSP **ksp)
1776: {

1781:   PetscUseMethod(eps,"EPSCISSGetKSPs_C",(EPS,PetscInt*,KSP**),(eps,nsolve,ksp));
1782:   return(0);
1783: }

1785: PetscErrorCode EPSReset_CISS(EPS eps)
1786: {
1788:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1789:   PetscInt       i;

1792:   BVDestroy(&ctx->S);
1793:   BVDestroy(&ctx->V);
1794:   BVDestroy(&ctx->Y);
1795:   if (!ctx->usest) {
1796:     for (i=0;i<ctx->num_solve_point;i++) {
1797:       KSPReset(ctx->ksp[i]);
1798:     }
1799:   }
1800:   VecScatterDestroy(&ctx->scatterin);
1801:   VecDestroy(&ctx->xsub);
1802:   VecDestroy(&ctx->xdup);
1803:   if (ctx->pA) {
1804:     MatDestroy(&ctx->pA);
1805:     MatDestroy(&ctx->pB);
1806:     BVDestroy(&ctx->pV);
1807:   }
1808:   return(0);
1809: }

1811: PetscErrorCode EPSSetFromOptions_CISS(PetscOptionItems *PetscOptionsObject,EPS eps)
1812: {
1813:   PetscErrorCode    ierr;
1814:   PetscReal         r3,r4;
1815:   PetscInt          i,i1,i2,i3,i4,i5,i6,i7;
1816:   PetscBool         b1,b2,flg;
1817:   EPS_CISS          *ctx = (EPS_CISS*)eps->data;
1818:   EPSCISSQuadRule   quad;
1819:   EPSCISSExtraction extraction;

1822:   PetscOptionsHead(PetscOptionsObject,"EPS CISS Options");

1824:     EPSCISSGetSizes(eps,&i1,&i2,&i3,&i4,&i5,&b1);
1825:     PetscOptionsInt("-eps_ciss_integration_points","Number of integration points","EPSCISSSetSizes",i1,&i1,NULL);
1826:     PetscOptionsInt("-eps_ciss_blocksize","Block size","EPSCISSSetSizes",i2,&i2,NULL);
1827:     PetscOptionsInt("-eps_ciss_moments","Moment size","EPSCISSSetSizes",i3,&i3,NULL);
1828:     PetscOptionsInt("-eps_ciss_partitions","Number of partitions","EPSCISSSetSizes",i4,&i4,NULL);
1829:     PetscOptionsInt("-eps_ciss_maxblocksize","Maximum block size","EPSCISSSetSizes",i5,&i5,NULL);
1830:     PetscOptionsBool("-eps_ciss_realmats","True if A and B are real","EPSCISSSetSizes",b1,&b1,NULL);
1831:     EPSCISSSetSizes(eps,i1,i2,i3,i4,i5,b1);

1833:     EPSCISSGetThreshold(eps,&r3,&r4);
1834:     PetscOptionsReal("-eps_ciss_delta","Threshold for numerical rank","EPSCISSSetThreshold",r3,&r3,NULL);
1835:     PetscOptionsReal("-eps_ciss_spurious_threshold","Threshold for the spurious eigenpairs","EPSCISSSetThreshold",r4,&r4,NULL);
1836:     EPSCISSSetThreshold(eps,r3,r4);

1838:     EPSCISSGetRefinement(eps,&i6,&i7);
1839:     PetscOptionsInt("-eps_ciss_refine_inner","Number of inner iterative refinement iterations","EPSCISSSetRefinement",i6,&i6,NULL);
1840:     PetscOptionsInt("-eps_ciss_refine_blocksize","Number of blocksize iterative refinement iterations","EPSCISSSetRefinement",i7,&i7,NULL);
1841:     EPSCISSSetRefinement(eps,i6,i7);

1843:     EPSCISSGetUseST(eps,&b2);
1844:     PetscOptionsBool("-eps_ciss_usest","Use ST for linear solves","EPSCISSSetUseST",b2,&b2,&flg);
1845:     if (flg) { EPSCISSSetUseST(eps,b2); }

1847:     PetscOptionsEnum("-eps_ciss_quadrule","Quadrature rule","EPSCISSSetQuadRule",EPSCISSQuadRules,(PetscEnum)ctx->quad,(PetscEnum*)&quad,&flg);
1848:     if (flg) { EPSCISSSetQuadRule(eps,quad); }

1850:     PetscOptionsEnum("-eps_ciss_extraction","Extraction technique","EPSCISSSetExtraction",EPSCISSExtractions,(PetscEnum)ctx->extraction,(PetscEnum*)&extraction,&flg);
1851:     if (flg) { EPSCISSSetExtraction(eps,extraction); }

1853:   PetscOptionsTail();

1855:   if (!eps->rg) { EPSGetRG(eps,&eps->rg); }
1856:   RGSetFromOptions(eps->rg); /* this is necessary here to set useconj */
1857:   if (!ctx->ksp) { EPSCISSGetKSPs(eps,&ctx->num_solve_point,&ctx->ksp); }
1858:   for (i=0;i<ctx->num_solve_point;i++) {
1859:     KSPSetFromOptions(ctx->ksp[i]);
1860:   }
1861:   PetscSubcommSetFromOptions(ctx->subcomm);
1862:   return(0);
1863: }

1865: PetscErrorCode EPSDestroy_CISS(EPS eps)
1866: {
1868:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1871:   EPSCISSResetSubcomm(eps);
1872:   PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma);
1873:   PetscFree(eps->data);
1874:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetSizes_C",NULL);
1875:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetSizes_C",NULL);
1876:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetThreshold_C",NULL);
1877:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetThreshold_C",NULL);
1878:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetRefinement_C",NULL);
1879:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetRefinement_C",NULL);
1880:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetUseST_C",NULL);
1881:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetUseST_C",NULL);
1882:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetQuadRule_C",NULL);
1883:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetQuadRule_C",NULL);
1884:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetExtraction_C",NULL);
1885:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetExtraction_C",NULL);
1886:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetKSPs_C",NULL);
1887:   return(0);
1888: }

1890: PetscErrorCode EPSView_CISS(EPS eps,PetscViewer viewer)
1891: {
1893:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1894:   PetscBool      isascii;

1897:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1898:   if (isascii) {
1899:     PetscViewerASCIIPrintf(viewer,"  sizes { integration points: %D, block size: %D, moment size: %D, partitions: %D, maximum block size: %D }\n",ctx->N,ctx->L,ctx->M,ctx->npart,ctx->L_max);
1900:     if (ctx->isreal) {
1901:       PetscViewerASCIIPrintf(viewer,"  exploiting symmetry of integration points\n");
1902:     }
1903:     PetscViewerASCIIPrintf(viewer,"  threshold { delta: %g, spurious threshold: %g }\n",(double)ctx->delta,(double)ctx->spurious_threshold);
1904:     PetscViewerASCIIPrintf(viewer,"  iterative refinement { inner: %D, blocksize: %D }\n",ctx->refine_inner, ctx->refine_blocksize);
1905:     PetscViewerASCIIPrintf(viewer,"  extraction: %s\n",EPSCISSExtractions[ctx->extraction]);
1906:     PetscViewerASCIIPrintf(viewer,"  quadrature rule: %s\n",EPSCISSQuadRules[ctx->quad]);
1907:     if (ctx->usest) {
1908:       PetscViewerASCIIPrintf(viewer,"  using ST for linear solves\n");
1909:     } else {
1910:       if (!ctx->ksp) { EPSCISSGetKSPs(eps,&ctx->num_solve_point,&ctx->ksp); }
1911:       PetscViewerASCIIPushTab(viewer);
1912:       KSPView(ctx->ksp[0],viewer);
1913:       PetscViewerASCIIPopTab(viewer);
1914:     }
1915:   }
1916:   return(0);
1917: }

1919: PetscErrorCode EPSSetDefaultST_CISS(EPS eps)
1920: {
1922:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1923:   PetscBool      usest = ctx->usest;

1926:   if (!((PetscObject)eps->st)->type_name) {
1927:     if (!ctx->usest_set) usest = (ctx->npart>1)? PETSC_FALSE: PETSC_TRUE;
1928:     if (usest) {
1929:       STSetType(eps->st,STSINVERT);
1930:     } else {
1931:       /* we are not going to use ST, so avoid factorizing the matrix */
1932:       STSetType(eps->st,STSHIFT);
1933:     }
1934:   }
1935:   return(0);
1936: }

1938: SLEPC_EXTERN PetscErrorCode EPSCreate_CISS(EPS eps)
1939: {
1941:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1944:   PetscNewLog(eps,&ctx);
1945:   eps->data = ctx;

1947:   eps->useds = PETSC_TRUE;
1948:   eps->categ = EPS_CATEGORY_CONTOUR;

1950:   eps->ops->solve          = EPSSolve_CISS;
1951:   eps->ops->setup          = EPSSetUp_CISS;
1952:   eps->ops->setfromoptions = EPSSetFromOptions_CISS;
1953:   eps->ops->destroy        = EPSDestroy_CISS;
1954:   eps->ops->reset          = EPSReset_CISS;
1955:   eps->ops->view           = EPSView_CISS;
1956:   eps->ops->computevectors = EPSComputeVectors_Schur;
1957:   eps->ops->setdefaultst   = EPSSetDefaultST_CISS;

1959:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetSizes_C",EPSCISSSetSizes_CISS);
1960:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetSizes_C",EPSCISSGetSizes_CISS);
1961:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetThreshold_C",EPSCISSSetThreshold_CISS);
1962:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetThreshold_C",EPSCISSGetThreshold_CISS);
1963:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetRefinement_C",EPSCISSSetRefinement_CISS);
1964:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetRefinement_C",EPSCISSGetRefinement_CISS);
1965:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetUseST_C",EPSCISSSetUseST_CISS);
1966:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetUseST_C",EPSCISSGetUseST_CISS);
1967:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetQuadRule_C",EPSCISSSetQuadRule_CISS);
1968:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetQuadRule_C",EPSCISSGetQuadRule_CISS);
1969:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetExtraction_C",EPSCISSSetExtraction_CISS);
1970:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetExtraction_C",EPSCISSGetExtraction_CISS);
1971:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetKSPs_C",EPSCISSGetKSPs_CISS);
1972:   /* set default values of parameters */
1973:   ctx->N                  = 32;
1974:   ctx->L                  = 16;
1975:   ctx->M                  = ctx->N/4;
1976:   ctx->delta              = 1e-12;
1977:   ctx->L_max              = 64;
1978:   ctx->spurious_threshold = 1e-4;
1979:   ctx->usest              = PETSC_TRUE;
1980:   ctx->usest_set          = PETSC_FALSE;
1981:   ctx->isreal             = PETSC_FALSE;
1982:   ctx->refine_inner       = 0;
1983:   ctx->refine_blocksize   = 0;
1984:   ctx->npart              = 1;
1985:   ctx->quad               = (EPSCISSQuadRule)0;
1986:   ctx->extraction         = EPS_CISS_EXTRACTION_RITZ;
1987:   return(0);
1988: }