Actual source code: ciss.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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] = PetscCosReal(theta)+vscale*PetscSinReal(theta)*PETSC_i;
243:       ctx->weight[i] = rgscale*radius*(vscale*PetscCosReal(theta)+PetscSinReal(theta)*PETSC_i)/(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*(PetscCosReal(theta)+PETSC_i*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: {
530: #if defined(PETSC_MISSING_LAPACK_GESVD)
532:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GESVD - Lapack routine is unavailable");
533: #else
535:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
536:   PetscInt       i,ml=ctx->L*ctx->M;
537:   PetscBLASInt   m,n,lda,ldu,ldvt,lwork,info;
538:   PetscScalar    *work;
539: #if defined(PETSC_USE_COMPLEX)
540:   PetscReal      *rwork;
541: #endif

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

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

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

618: static PetscErrorCode SVD_S(BV S,PetscInt ml,PetscReal delta,PetscReal *sigma,PetscInt *K)
619: {
620: #if defined(PETSC_MISSING_LAPACK_GESVD)
622:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GESVD - Lapack routine is unavailable");
623: #else
625:   PetscInt       i,j,k,local_size;
626:   PetscMPIInt    len;
627:   PetscScalar    *work,*temp,*B,*tempB,*s_data,*Q1,*Q2,*temp2,alpha=1,beta=0;
628:   PetscBLASInt   l,m,n,lda,ldu,ldvt,lwork,info,ldb,ldc;
629: #if defined(PETSC_USE_COMPLEX)
630:   PetscReal      *rwork;
631: #endif

634:   BVGetSizes(S,&local_size,NULL,NULL);
635:   BVGetArray(S,&s_data);
636:   PetscMalloc7(ml*ml,&temp,ml*ml,&temp2,local_size*ml,&Q1,local_size*ml,&Q2,ml*ml,&B,ml*ml,&tempB,5*ml,&work);
637:   PetscMemzero(B,ml*ml*sizeof(PetscScalar));
638: #if defined(PETSC_USE_COMPLEX)
639:   PetscMalloc1(5*ml,&rwork);
640: #endif
641:   PetscFPTrapPush(PETSC_FP_TRAP_OFF);

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

645:   for (k=0;k<2;k++) {
646:     PetscBLASIntCast(local_size,&m);
647:     PetscBLASIntCast(ml,&l);
648:     n = l; lda = m; ldb = m; ldc = l;
649:     if (k == 0) {
650:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,s_data,&lda,s_data,&ldb,&beta,temp,&ldc));
651:     } else if ((k%2)==1) {
652:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,Q1,&lda,Q1,&ldb,&beta,temp,&ldc));
653:     } else {
654:       PetscStackCallBLAS("BLASgemm",BLASgemm_("C","N",&l,&n,&m,&alpha,Q2,&lda,Q2,&ldb,&beta,temp,&ldc));
655:     }
656:     PetscMemzero(temp2,ml*ml*sizeof(PetscScalar));
657:     PetscMPIIntCast(ml*ml,&len);
658:     MPI_Allreduce(temp,temp2,len,MPIU_SCALAR,MPIU_SUM,PetscObjectComm((PetscObject)S));

660:     PetscBLASIntCast(ml,&m);
661:     n = m; lda = m; lwork = 5*m, ldu = 1; ldvt = 1;
662: #if defined(PETSC_USE_COMPLEX)
663:     PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&m,&n,temp2,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,rwork,&info));
664: #else
665:     PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("O","N",&m,&n,temp2,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,&info));
666: #endif
667:     SlepcCheckLapackInfo("gesvd",info);

669:     PetscBLASIntCast(local_size,&l);
670:     PetscBLASIntCast(ml,&n);
671:     m = n; lda = l; ldb = m; ldc = l;
672:     if (k==0) {
673:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,s_data,&lda,temp2,&ldb,&beta,Q1,&ldc));
674:     } else if ((k%2)==1) {
675:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,Q1,&lda,temp2,&ldb,&beta,Q2,&ldc));
676:     } else {
677:       PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,Q2,&lda,temp2,&ldb,&beta,Q1,&ldc));
678:     }

680:     PetscBLASIntCast(ml,&l);
681:     m = l; n = l; lda = l; ldb = m; ldc = l;
682:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","N",&l,&n,&m,&alpha,B,&lda,temp2,&ldb,&beta,tempB,&ldc));
683:     for (i=0;i<ml;i++) {
684:       sigma[i] = sqrt(sigma[i]);
685:       for (j=0;j<local_size;j++) {
686:         if ((k%2)==1) Q2[j+i*local_size]/=sigma[i];
687:         else Q1[j+i*local_size]/=sigma[i];
688:       }
689:       for (j=0;j<ml;j++) {
690:         B[j+i*ml]=tempB[j+i*ml]*sigma[i];
691:       }
692:     }
693:   }

695:   PetscBLASIntCast(ml,&m);
696:   n = m; lda = m; ldu=1; ldvt=1;
697: #if defined(PETSC_USE_COMPLEX)
698:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&m,&n,B,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,rwork,&info));
699: #else
700:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&m,&n,B,&lda,sigma,NULL,&ldu,NULL,&ldvt,work,&lwork,&info));
701: #endif
702:   SlepcCheckLapackInfo("gesvd",info);

704:   PetscBLASIntCast(local_size,&l);
705:   PetscBLASIntCast(ml,&n);
706:   m = n; lda = l; ldb = m; ldc = l;
707:   if ((k%2)==1) {
708:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&l,&n,&m,&alpha,Q1,&lda,B,&ldb,&beta,s_data,&ldc));
709:   } else {
710:     PetscStackCallBLAS("BLASgemm",BLASgemm_("N","T",&l,&n,&m,&alpha,Q2,&lda,B,&ldb,&beta,s_data,&ldc));
711:   }

713:   PetscFPTrapPop();
714:   BVRestoreArray(S,&s_data);

716:   (*K) = 0;
717:   for (i=0;i<ml;i++) {
718:     if (sigma[i]/PetscMax(sigma[0],1)>delta) (*K)++;
719:   }
720:   PetscFree7(temp,temp2,Q1,Q2,B,tempB,work);
721: #if defined(PETSC_USE_COMPLEX)
722:   PetscFree(rwork);
723: #endif
724:   return(0);
725: #endif
726: }

728: static PetscErrorCode isGhost(EPS eps,PetscInt ld,PetscInt nv,PetscBool *fl)
729: {
731:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
732:   PetscInt       i,j;
733:   PetscScalar    *pX;
734:   PetscReal      *tau,s1,s2,tau_max=0.0;

737:   PetscMalloc1(nv,&tau);
738:   DSVectors(eps->ds,DS_MAT_X,NULL,NULL);
739:   DSGetArray(eps->ds,DS_MAT_X,&pX);

741:   for (i=0;i<nv;i++) {
742:     s1 = 0;
743:     s2 = 0;
744:     for (j=0;j<nv;j++) {
745:       s1 += PetscAbsScalar(PetscPowScalarInt(pX[i*ld+j],2));
746:       s2 += PetscPowRealInt(PetscAbsScalar(pX[i*ld+j]),2)/ctx->sigma[j];
747:     }
748:     tau[i] = s1/s2;
749:     tau_max = PetscMax(tau_max,tau[i]);
750:   }
751:   DSRestoreArray(eps->ds,DS_MAT_X,&pX);
752:   for (i=0;i<nv;i++) {
753:     tau[i] /= tau_max;
754:   }
755:   for (i=0;i<nv;i++) {
756:     if (tau[i]>=ctx->spurious_threshold) fl[i] = PETSC_TRUE;
757:     else fl[i] = PETSC_FALSE;
758:   }
759:   PetscFree(tau);
760:   return(0);
761: }

763: static PetscErrorCode rescale_eig(EPS eps,PetscInt nv)
764: {
766:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
767:   PetscInt       i;
768:   PetscScalar    center;
769:   PetscReal      radius,a,b,c,d,rgscale;
770: #if defined(PETSC_USE_COMPLEX)
771:   PetscReal      start_ang,end_ang,vscale,theta;
772: #endif
773:   PetscBool      isring,isellipse,isinterval;

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

831: PetscErrorCode EPSSetUp_CISS(EPS eps)
832: {
834:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
835:   PetscBool      issinvert,istrivial,isring,isellipse,isinterval,flg,useconj;
836:   PetscReal      c,d;
837:   Mat            A;

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

864:   /* check region */
865:   RGIsTrivial(eps->rg,&istrivial);
866:   if (istrivial) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"CISS requires a nontrivial region, e.g. -rg_type ellipse ...");
867:   RGGetComplement(eps->rg,&flg);
868:   if (flg) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"A region with complement flag set is not allowed");
869:   PetscObjectTypeCompare((PetscObject)eps->rg,RGELLIPSE,&isellipse);
870:   PetscObjectTypeCompare((PetscObject)eps->rg,RGRING,&isring);
871:   PetscObjectTypeCompare((PetscObject)eps->rg,RGINTERVAL,&isinterval);
872:   if (!isellipse && !isring && !isinterval) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Currently only implemented for interval, elliptic or ring regions");
873:   /* if useconj has changed, then reset subcomm data */
874:   EPSCISSSetUseConj(eps,&useconj);
875:   if (useconj!=ctx->useconj) { EPSCISSResetSubcomm(eps); }

877: #if !defined(PETSC_USE_COMPLEX)
878:   if (isring) {
879:     SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_SUP,"Ring region only supported for complex scalars");
880:   }
881: #endif
882:   if (isinterval) {
883:     RGIntervalGetEndpoints(eps->rg,NULL,NULL,&c,&d);
884: #if !defined(PETSC_USE_COMPLEX)
885:     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");
886: #endif
887:     if (!ctx->quad && c==d) ctx->quad = EPS_CISS_QUADRULE_CHEBYSHEV;
888:   }
889:   if (!ctx->quad) ctx->quad = EPS_CISS_QUADRULE_TRAPEZOIDAL;

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

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

899:   /* allocate basis vectors */
900:   BVDestroy(&ctx->S);
901:   BVDuplicateResize(eps->V,ctx->L_max*ctx->M,&ctx->S);
902:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->S);
903:   BVDestroy(&ctx->V);
904:   BVDuplicateResize(eps->V,ctx->L_max,&ctx->V);
905:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->V);

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

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

914:   CISSRedundantMat(eps);
915:   if (ctx->pA) {
916:     CISSScatterVec(eps);
917:     BVDestroy(&ctx->pV);
918:     BVCreate(PetscObjectComm((PetscObject)ctx->xsub),&ctx->pV);
919:     BVSetSizesFromVec(ctx->pV,ctx->xsub,eps->n);
920:     BVSetFromOptions(ctx->pV);
921:     BVResize(ctx->pV,ctx->L_max,PETSC_FALSE);
922:     PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->pV);
923:   }

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

930:   BVDestroy(&ctx->Y);
931:   if (ctx->pA) {
932:     BVCreate(PetscObjectComm((PetscObject)ctx->xsub),&ctx->Y);
933:     BVSetSizesFromVec(ctx->Y,ctx->xsub,eps->n);
934:     BVSetFromOptions(ctx->Y);
935:     BVResize(ctx->Y,ctx->num_solve_point*ctx->L_max,PETSC_FALSE);
936:   } else {
937:     BVDuplicateResize(eps->V,ctx->num_solve_point*ctx->L_max,&ctx->Y);
938:   }
939:   PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->Y);

941:   if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
942:     DSSetType(eps->ds,DSGNHEP);
943:   } else if (eps->isgeneralized) {
944:     if (eps->ishermitian && eps->ispositive) {
945:       DSSetType(eps->ds,DSGHEP);
946:     } else {
947:       DSSetType(eps->ds,DSGNHEP);
948:     }
949:   } else {
950:     if (eps->ishermitian) {
951:       DSSetType(eps->ds,DSHEP);
952:     } else {
953:       DSSetType(eps->ds,DSNHEP);
954:     }
955:   }
956:   DSAllocate(eps->ds,eps->ncv);
957:   EPSSetWorkVecs(eps,2);

959: #if !defined(PETSC_USE_COMPLEX)
960:   if (!eps->ishermitian) { PetscInfo(eps,"Warning: complex eigenvalues are not calculated exactly without --with-scalar-type=complex in PETSc\n"); }
961: #endif
962:   return(0);
963: }

965: PetscErrorCode EPSSolve_CISS(EPS eps)
966: {
968:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
969:   Mat            A,B,X,M,pA,pB;
970:   PetscInt       i,j,ld,nmat,L_add=0,nv=0,L_base=ctx->L,inner,nlocal,*inside;
971:   PetscScalar    *Mu,*H0,*H1=NULL,*rr,*temp;
972:   PetscReal      error,max_error;
973:   PetscBool      *fl1;
974:   Vec            si,w[3];
975:   SlepcSC        sc;
976:   PetscRandom    rand;
977: #if defined(PETSC_USE_COMPLEX)
978:   PetscBool      isellipse;
979: #endif

982:   w[0] = eps->work[0];
983:   w[1] = NULL;
984:   w[2] = eps->work[1];
985:   /* override SC settings */
986:   DSGetSlepcSC(eps->ds,&sc);
987:   sc->comparison    = SlepcCompareLargestMagnitude;
988:   sc->comparisonctx = NULL;
989:   sc->map           = NULL;
990:   sc->mapobj        = NULL;
991:   VecGetLocalSize(w[0],&nlocal);
992:   DSGetLeadingDimension(eps->ds,&ld);
993:   STGetNumMatrices(eps->st,&nmat);
994:   STGetMatrix(eps->st,0,&A);
995:   if (nmat>1) { STGetMatrix(eps->st,1,&B); }
996:   else B = NULL;
997:   SetPathParameter(eps);
998:   CISSVecSetRandom(ctx->V,0,ctx->L);
999:   BVGetRandomContext(ctx->V,&rand);

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

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

1079:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1080:         BlockHankel(eps,Mu,0,H0);
1081:         BlockHankel(eps,Mu,1,H1);
1082:         DSGetArray(eps->ds,DS_MAT_A,&temp);
1083:         for (j=0;j<nv;j++) {
1084:           for (i=0;i<nv;i++) {
1085:             temp[i+j*ld] = H1[i+j*ctx->L*ctx->M];
1086:           }
1087:         }
1088:         DSRestoreArray(eps->ds,DS_MAT_A,&temp);
1089:         DSGetArray(eps->ds,DS_MAT_B,&temp);
1090:         for (j=0;j<nv;j++) {
1091:           for (i=0;i<nv;i++) {
1092:             temp[i+j*ld] = H0[i+j*ctx->L*ctx->M];
1093:           }
1094:         }
1095:         DSRestoreArray(eps->ds,DS_MAT_B,&temp);
1096:       } else {
1097:         BVSetActiveColumns(ctx->S,0,nv);
1098:         DSGetMat(eps->ds,DS_MAT_A,&pA);
1099:         MatZeroEntries(pA);
1100:         BVMatProject(ctx->S,A,ctx->S,pA);
1101:         DSRestoreMat(eps->ds,DS_MAT_A,&pA);
1102:         if (B) {
1103:           DSGetMat(eps->ds,DS_MAT_B,&pB);
1104:           MatZeroEntries(pB);
1105:           BVMatProject(ctx->S,B,ctx->S,pB);
1106:           DSRestoreMat(eps->ds,DS_MAT_B,&pB);
1107:         }
1108:       }

1110:       DSSolve(eps->ds,eps->eigr,eps->eigi);
1111:       DSSynchronize(eps->ds,eps->eigr,eps->eigi);

1113:       PetscMalloc3(nv,&fl1,nv,&inside,nv,&rr);
1114:       rescale_eig(eps,nv);
1115:       isGhost(eps,ld,nv,fl1);
1116:       RGCheckInside(eps->rg,nv,eps->eigr,eps->eigi,inside);
1117:       for (i=0;i<nv;i++) {
1118:         if (fl1[i] && inside[i]>=0) {
1119:           rr[i] = 1.0;
1120:           eps->nconv++;
1121:         } else rr[i] = 0.0;
1122:       }
1123:       DSSort(eps->ds,eps->eigr,eps->eigi,rr,NULL,&eps->nconv);
1124:       DSSynchronize(eps->ds,eps->eigr,eps->eigi);
1125:       rescale_eig(eps,nv);
1126:       PetscFree3(fl1,inside,rr);
1127:       BVSetActiveColumns(eps->V,0,nv);
1128:       if (ctx->extraction == EPS_CISS_EXTRACTION_HANKEL) {
1129:         ConstructS(eps);
1130:         BVSetActiveColumns(ctx->S,0,ctx->L);
1131:         BVCopy(ctx->S,ctx->V);
1132:         BVSetActiveColumns(ctx->S,0,nv);
1133:       }
1134:       BVCopy(ctx->S,eps->V);

1136:       DSVectors(eps->ds,DS_MAT_X,NULL,NULL);
1137:       DSGetMat(eps->ds,DS_MAT_X,&X);
1138:       BVMultInPlace(ctx->S,X,0,eps->nconv);
1139:       if (eps->ishermitian) {
1140:         BVMultInPlace(eps->V,X,0,eps->nconv);
1141:       }
1142:       MatDestroy(&X);
1143:       max_error = 0.0;
1144:       for (i=0;i<eps->nconv;i++) {
1145:         BVGetColumn(ctx->S,i,&si);
1146:         EPSComputeResidualNorm_Private(eps,PETSC_FALSE,eps->eigr[i],eps->eigi[i],si,NULL,w,&error);
1147:         (*eps->converged)(eps,eps->eigr[i],eps->eigi[i],error,&error,eps->convergedctx);
1148:         BVRestoreColumn(ctx->S,i,&si);
1149:         max_error = PetscMax(max_error,error);
1150:       }

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

1185: static PetscErrorCode EPSCISSSetSizes_CISS(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
1186: {
1188:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1189:   PetscInt       oN,onpart;

1192:   oN = ctx->N;
1193:   if (ip == PETSC_DECIDE || ip == PETSC_DEFAULT) {
1194:     if (ctx->N!=32) { ctx->N =32; ctx->M = ctx->N/4; }
1195:   } else {
1196:     if (ip<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be > 0");
1197:     if (ip%2) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ip argument must be an even number");
1198:     if (ctx->N!=ip) { ctx->N = ip; ctx->M = ctx->N/4; }
1199:   }
1200:   if (bs == PETSC_DECIDE || bs == PETSC_DEFAULT) {
1201:     ctx->L = 16;
1202:   } else {
1203:     if (bs<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bs argument must be > 0");
1204:     ctx->L = bs;
1205:   }
1206:   if (ms == PETSC_DECIDE || ms == PETSC_DEFAULT) {
1207:     ctx->M = ctx->N/4;
1208:   } else {
1209:     if (ms<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The ms argument must be > 0");
1210:     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");
1211:     ctx->M = ms;
1212:   }
1213:   onpart = ctx->npart;
1214:   if (npart == PETSC_DECIDE || npart == PETSC_DEFAULT) {
1215:     ctx->npart = 1;
1216:   } else {
1217:     if (npart<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The npart argument must be > 0");
1218:     ctx->npart = npart;
1219:   }
1220:   if (bsmax == PETSC_DECIDE || bsmax == PETSC_DEFAULT) {
1221:     ctx->L_max = 64;
1222:   } else {
1223:     if (bsmax<1) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The bsmax argument must be > 0");
1224:     ctx->L_max = PetscMax(bsmax,ctx->L);
1225:   }
1226:   if (onpart != ctx->npart || oN != ctx->N || realmats != ctx->isreal) { EPSCISSResetSubcomm(eps); }
1227:   ctx->isreal = realmats;
1228:   eps->state = EPS_STATE_INITIAL;
1229:   return(0);
1230: }

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

1235:    Logically Collective on EPS

1237:    Input Parameters:
1238: +  eps   - the eigenproblem solver context
1239: .  ip    - number of integration points
1240: .  bs    - block size
1241: .  ms    - moment size
1242: .  npart - number of partitions when splitting the communicator
1243: .  bsmax - max block size
1244: -  realmats - A and B are real

1246:    Options Database Keys:
1247: +  -eps_ciss_integration_points - Sets the number of integration points
1248: .  -eps_ciss_blocksize - Sets the block size
1249: .  -eps_ciss_moments - Sets the moment size
1250: .  -eps_ciss_partitions - Sets the number of partitions
1251: .  -eps_ciss_maxblocksize - Sets the maximum block size
1252: -  -eps_ciss_realmats - A and B are real

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

1259:    Level: advanced

1261: .seealso: EPSCISSGetSizes()
1262: @*/
1263: PetscErrorCode EPSCISSSetSizes(EPS eps,PetscInt ip,PetscInt bs,PetscInt ms,PetscInt npart,PetscInt bsmax,PetscBool realmats)
1264: {

1275:   PetscTryMethod(eps,"EPSCISSSetSizes_C",(EPS,PetscInt,PetscInt,PetscInt,PetscInt,PetscInt,PetscBool),(eps,ip,bs,ms,npart,bsmax,realmats));
1276:   return(0);
1277: }

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

1284:   if (ip) *ip = ctx->N;
1285:   if (bs) *bs = ctx->L;
1286:   if (ms) *ms = ctx->M;
1287:   if (npart) *npart = ctx->npart;
1288:   if (bsmax) *bsmax = ctx->L_max;
1289:   if (realmats) *realmats = ctx->isreal;
1290:   return(0);
1291: }

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

1296:    Not Collective

1298:    Input Parameter:
1299: .  eps - the eigenproblem solver context

1301:    Output Parameters:
1302: +  ip    - number of integration points
1303: .  bs    - block size
1304: .  ms    - moment size
1305: .  npart - number of partitions when splitting the communicator
1306: .  bsmax - max block size
1307: -  realmats - A and B are real

1309:    Level: advanced

1311: .seealso: EPSCISSSetSizes()
1312: @*/
1313: PetscErrorCode EPSCISSGetSizes(EPS eps,PetscInt *ip,PetscInt *bs,PetscInt *ms,PetscInt *npart,PetscInt *bsmax,PetscBool *realmats)
1314: {

1319:   PetscUseMethod(eps,"EPSCISSGetSizes_C",(EPS,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscInt*,PetscBool*),(eps,ip,bs,ms,npart,bsmax,realmats));
1320:   return(0);
1321: }

1323: static PetscErrorCode EPSCISSSetThreshold_CISS(EPS eps,PetscReal delta,PetscReal spur)
1324: {
1325:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1328:   if (delta == PETSC_DEFAULT) {
1329:     ctx->delta = 1e-12;
1330:   } else {
1331:     if (delta<=0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The delta argument must be > 0.0");
1332:     ctx->delta = delta;
1333:   }
1334:   if (spur == PETSC_DEFAULT) {
1335:     ctx->spurious_threshold = 1e-4;
1336:   } else {
1337:     if (spur<=0.0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The spurious threshold argument must be > 0.0");
1338:     ctx->spurious_threshold = spur;
1339:   }
1340:   return(0);
1341: }

1343: /*@
1344:    EPSCISSSetThreshold - Sets the values of various threshold parameters in
1345:    the CISS solver.

1347:    Logically Collective on EPS

1349:    Input Parameters:
1350: +  eps   - the eigenproblem solver context
1351: .  delta - threshold for numerical rank
1352: -  spur  - spurious threshold (to discard spurious eigenpairs)

1354:    Options Database Keys:
1355: +  -eps_ciss_delta - Sets the delta
1356: -  -eps_ciss_spurious_threshold - Sets the spurious threshold

1358:    Level: advanced

1360: .seealso: EPSCISSGetThreshold()
1361: @*/
1362: PetscErrorCode EPSCISSSetThreshold(EPS eps,PetscReal delta,PetscReal spur)
1363: {

1370:   PetscTryMethod(eps,"EPSCISSSetThreshold_C",(EPS,PetscReal,PetscReal),(eps,delta,spur));
1371:   return(0);
1372: }

1374: static PetscErrorCode EPSCISSGetThreshold_CISS(EPS eps,PetscReal *delta,PetscReal *spur)
1375: {
1376:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1379:   if (delta) *delta = ctx->delta;
1380:   if (spur)  *spur = ctx->spurious_threshold;
1381:   return(0);
1382: }

1384: /*@
1385:    EPSCISSGetThreshold - Gets the values of various threshold parameters
1386:    in the CISS solver.

1388:    Not Collective

1390:    Input Parameter:
1391: .  eps - the eigenproblem solver context

1393:    Output Parameters:
1394: +  delta - threshold for numerical rank
1395: -  spur  - spurious threshold (to discard spurious eigenpairs)

1397:    Level: advanced

1399: .seealso: EPSCISSSetThreshold()
1400: @*/
1401: PetscErrorCode EPSCISSGetThreshold(EPS eps,PetscReal *delta,PetscReal *spur)
1402: {

1407:   PetscUseMethod(eps,"EPSCISSGetThreshold_C",(EPS,PetscReal*,PetscReal*),(eps,delta,spur));
1408:   return(0);
1409: }

1411: static PetscErrorCode EPSCISSSetRefinement_CISS(EPS eps,PetscInt inner,PetscInt blsize)
1412: {
1413:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1416:   if (inner == PETSC_DEFAULT) {
1417:     ctx->refine_inner = 0;
1418:   } else {
1419:     if (inner<0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine inner argument must be >= 0");
1420:     ctx->refine_inner = inner;
1421:   }
1422:   if (blsize == PETSC_DEFAULT) {
1423:     ctx->refine_blocksize = 0;
1424:   } else {
1425:     if (blsize<0) SETERRQ(PetscObjectComm((PetscObject)eps),PETSC_ERR_ARG_OUTOFRANGE,"The refine blocksize argument must be >= 0");
1426:     ctx->refine_blocksize = blsize;
1427:   }
1428:   return(0);
1429: }

1431: /*@
1432:    EPSCISSSetRefinement - Sets the values of various refinement parameters
1433:    in the CISS solver.

1435:    Logically Collective on EPS

1437:    Input Parameters:
1438: +  eps    - the eigenproblem solver context
1439: .  inner  - number of iterative refinement iterations (inner loop)
1440: -  blsize - number of iterative refinement iterations (blocksize loop)

1442:    Options Database Keys:
1443: +  -eps_ciss_refine_inner - Sets number of inner iterations
1444: -  -eps_ciss_refine_blocksize - Sets number of blocksize iterations

1446:    Level: advanced

1448: .seealso: EPSCISSGetRefinement()
1449: @*/
1450: PetscErrorCode EPSCISSSetRefinement(EPS eps,PetscInt inner,PetscInt blsize)
1451: {

1458:   PetscTryMethod(eps,"EPSCISSSetRefinement_C",(EPS,PetscInt,PetscInt),(eps,inner,blsize));
1459:   return(0);
1460: }

1462: static PetscErrorCode EPSCISSGetRefinement_CISS(EPS eps,PetscInt *inner,PetscInt *blsize)
1463: {
1464:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1467:   if (inner)  *inner = ctx->refine_inner;
1468:   if (blsize) *blsize = ctx->refine_blocksize;
1469:   return(0);
1470: }

1472: /*@
1473:    EPSCISSGetRefinement - Gets the values of various refinement parameters
1474:    in the CISS solver.

1476:    Not Collective

1478:    Input Parameter:
1479: .  eps - the eigenproblem solver context

1481:    Output Parameters:
1482: +  inner  - number of iterative refinement iterations (inner loop)
1483: -  blsize - number of iterative refinement iterations (blocksize loop)

1485:    Level: advanced

1487: .seealso: EPSCISSSetRefinement()
1488: @*/
1489: PetscErrorCode EPSCISSGetRefinement(EPS eps, PetscInt *inner, PetscInt *blsize)
1490: {

1495:   PetscUseMethod(eps,"EPSCISSGetRefinement_C",(EPS,PetscInt*,PetscInt*),(eps,inner,blsize));
1496:   return(0);
1497: }

1499: static PetscErrorCode EPSCISSSetUseST_CISS(EPS eps,PetscBool usest)
1500: {
1501:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1504:   ctx->usest     = usest;
1505:   ctx->usest_set = PETSC_TRUE;
1506:   eps->state     = EPS_STATE_INITIAL;
1507:   return(0);
1508: }

1510: /*@
1511:    EPSCISSSetUseST - Sets a flag indicating that the CISS solver will
1512:    use the ST object for the linear solves.

1514:    Logically Collective on EPS

1516:    Input Parameters:
1517: +  eps    - the eigenproblem solver context
1518: -  usest  - boolean flag to use the ST object or not

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

1523:    Level: advanced

1525: .seealso: EPSCISSGetUseST()
1526: @*/
1527: PetscErrorCode EPSCISSSetUseST(EPS eps,PetscBool usest)
1528: {

1534:   PetscTryMethod(eps,"EPSCISSSetUseST_C",(EPS,PetscBool),(eps,usest));
1535:   return(0);
1536: }

1538: static PetscErrorCode EPSCISSGetUseST_CISS(EPS eps,PetscBool *usest)
1539: {
1540:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1543:   *usest = ctx->usest;
1544:   return(0);
1545: }

1547: /*@
1548:    EPSCISSGetUseST - Gets the flag for using the ST object
1549:    in the CISS solver.

1551:    Not Collective

1553:    Input Parameter:
1554: .  eps - the eigenproblem solver context

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

1559:    Level: advanced

1561: .seealso: EPSCISSSetUseST()
1562: @*/
1563: PetscErrorCode EPSCISSGetUseST(EPS eps,PetscBool *usest)
1564: {

1570:   PetscUseMethod(eps,"EPSCISSGetUseST_C",(EPS,PetscBool*),(eps,usest));
1571:   return(0);
1572: }

1574: static PetscErrorCode EPSCISSSetQuadRule_CISS(EPS eps,EPSCISSQuadRule quad)
1575: {
1576:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1579:   ctx->quad = quad;
1580:   return(0);
1581: }

1583: /*@
1584:    EPSCISSSetQuadRule - Sets the quadrature rule used in the CISS solver.

1586:    Logically Collective on EPS

1588:    Input Parameters:
1589: +  eps  - the eigenproblem solver context
1590: -  quad - the quadrature rule

1592:    Options Database Key:
1593: .  -eps_ciss_quadrule - Sets the quadrature rule (either 'trapezoidal' or
1594:                            'chebyshev')

1596:    Notes:
1597:    By default, the trapezoidal rule is used (EPS_CISS_QUADRULE_TRAPEZOIDAL).

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

1602:    Level: advanced

1604: .seealso: EPSCISSGetQuadRule(), EPSCISSQuadRule
1605: @*/
1606: PetscErrorCode EPSCISSSetQuadRule(EPS eps,EPSCISSQuadRule quad)
1607: {

1613:   PetscTryMethod(eps,"EPSCISSSetQuadRule_C",(EPS,EPSCISSQuadRule),(eps,quad));
1614:   return(0);
1615: }

1617: static PetscErrorCode EPSCISSGetQuadRule_CISS(EPS eps,EPSCISSQuadRule *quad)
1618: {
1619:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1622:   *quad = ctx->quad;
1623:   return(0);
1624: }

1626: /*@
1627:    EPSCISSGetQuadRule - Gets the quadrature rule used in the CISS solver.

1629:    Not Collective

1631:    Input Parameter:
1632: .  eps - the eigenproblem solver context

1634:    Output Parameters:
1635: .  quad - quadrature rule

1637:    Level: advanced

1639: .seealso: EPSCISSSetQuadRule() EPSCISSQuadRule
1640: @*/
1641: PetscErrorCode EPSCISSGetQuadRule(EPS eps, EPSCISSQuadRule *quad)
1642: {

1648:   PetscUseMethod(eps,"EPSCISSGetQuadRule_C",(EPS,EPSCISSQuadRule*),(eps,quad));
1649:   return(0);
1650: }

1652: static PetscErrorCode EPSCISSSetExtraction_CISS(EPS eps,EPSCISSExtraction extraction)
1653: {
1654:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1657:   ctx->extraction = extraction;
1658:   return(0);
1659: }

1661: /*@
1662:    EPSCISSSetExtraction - Sets the extraction technique used in the CISS solver.

1664:    Logically Collective on EPS

1666:    Input Parameters:
1667: +  eps        - the eigenproblem solver context
1668: -  extraction - the extraction technique

1670:    Options Database Key:
1671: .  -eps_ciss_extraction - Sets the extraction technique (either 'ritz' or
1672:                            'hankel')

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

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

1680:    Level: advanced

1682: .seealso: EPSCISSGetExtraction(), EPSCISSExtraction
1683: @*/
1684: PetscErrorCode EPSCISSSetExtraction(EPS eps,EPSCISSExtraction extraction)
1685: {

1691:   PetscTryMethod(eps,"EPSCISSSetExtraction_C",(EPS,EPSCISSExtraction),(eps,extraction));
1692:   return(0);
1693: }

1695: static PetscErrorCode EPSCISSGetExtraction_CISS(EPS eps,EPSCISSExtraction *extraction)
1696: {
1697:   EPS_CISS *ctx = (EPS_CISS*)eps->data;

1700:   *extraction = ctx->extraction;
1701:   return(0);
1702: }

1704: /*@
1705:    EPSCISSGetExtraction - Gets the extraction technique used in the CISS solver.

1707:    Not Collective

1709:    Input Parameter:
1710: .  eps - the eigenproblem solver context

1712:    Output Parameters:
1713: +  extraction - extraction technique

1715:    Level: advanced

1717: .seealso: EPSCISSSetExtraction() EPSCISSExtraction
1718: @*/
1719: PetscErrorCode EPSCISSGetExtraction(EPS eps,EPSCISSExtraction *extraction)
1720: {

1726:   PetscUseMethod(eps,"EPSCISSGetExtraction_C",(EPS,EPSCISSExtraction*),(eps,extraction));
1727:   return(0);
1728: }

1730: static PetscErrorCode EPSCISSGetKSPs_CISS(EPS eps,PetscInt *nsolve,KSP **ksp)
1731: {
1733:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1734:   PetscInt       i;
1735:   PC             pc;

1738:   if (!ctx->ksp) {
1739:     if (!ctx->subcomm) {  /* initialize subcomm first */
1740:       EPSCISSSetUseConj(eps,&ctx->useconj);
1741:       EPSCISSSetUpSubComm(eps,&ctx->num_solve_point);
1742:     }
1743:     PetscMalloc1(ctx->num_solve_point,&ctx->ksp);
1744:     for (i=0;i<ctx->num_solve_point;i++) {
1745:       KSPCreate(PetscSubcommChild(ctx->subcomm),&ctx->ksp[i]);
1746:       PetscObjectIncrementTabLevel((PetscObject)ctx->ksp[i],(PetscObject)eps,1);
1747:       KSPSetOptionsPrefix(ctx->ksp[i],((PetscObject)eps)->prefix);
1748:       KSPAppendOptionsPrefix(ctx->ksp[i],"eps_ciss_");
1749:       PetscLogObjectParent((PetscObject)eps,(PetscObject)ctx->ksp[i]);
1750:       PetscObjectSetOptions((PetscObject)ctx->ksp[i],((PetscObject)eps)->options);
1751:       KSPSetErrorIfNotConverged(ctx->ksp[i],PETSC_TRUE);
1752:       KSPSetTolerances(ctx->ksp[i],SLEPC_DEFAULT_TOL,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
1753:       KSPGetPC(ctx->ksp[i],&pc);
1754:       KSPSetType(ctx->ksp[i],KSPPREONLY);
1755:       PCSetType(pc,PCLU);
1756:     }
1757:   }
1758:   if (nsolve) *nsolve = ctx->num_solve_point;
1759:   if (ksp)    *ksp    = ctx->ksp;
1760:   return(0);
1761: }

1763: /*@C
1764:    EPSCISSGetKSPs - Retrieve the array of linear solver objects associated with
1765:    the CISS solver.

1767:    Not Collective

1769:    Input Parameter:
1770: .  eps - the eigenproblem solver solver

1772:    Output Parameters:
1773: +  nsolve - number of solver objects
1774: -  ksp - array of linear solver object

1776:    Notes:
1777:    The number of KSP solvers is equal to the number of integration points divided by
1778:    the number of partitions. This value is halved in the case of real matrices with
1779:    a region centered at the real axis.

1781:    Level: advanced

1783: .seealso: EPSCISSSetSizes()
1784: @*/
1785: PetscErrorCode EPSCISSGetKSPs(EPS eps,PetscInt *nsolve,KSP **ksp)
1786: {

1791:   PetscUseMethod(eps,"EPSCISSGetKSPs_C",(EPS,PetscInt*,KSP**),(eps,nsolve,ksp));
1792:   return(0);
1793: }

1795: PetscErrorCode EPSReset_CISS(EPS eps)
1796: {
1798:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1799:   PetscInt       i;

1802:   BVDestroy(&ctx->S);
1803:   BVDestroy(&ctx->V);
1804:   BVDestroy(&ctx->Y);
1805:   if (!ctx->usest) {
1806:     for (i=0;i<ctx->num_solve_point;i++) {
1807:       KSPReset(ctx->ksp[i]);
1808:     }
1809:   }
1810:   VecScatterDestroy(&ctx->scatterin);
1811:   VecDestroy(&ctx->xsub);
1812:   VecDestroy(&ctx->xdup);
1813:   if (ctx->pA) {
1814:     MatDestroy(&ctx->pA);
1815:     MatDestroy(&ctx->pB);
1816:     BVDestroy(&ctx->pV);
1817:   }
1818:   return(0);
1819: }

1821: PetscErrorCode EPSSetFromOptions_CISS(PetscOptionItems *PetscOptionsObject,EPS eps)
1822: {
1823:   PetscErrorCode    ierr;
1824:   PetscReal         r3,r4;
1825:   PetscInt          i,i1,i2,i3,i4,i5,i6,i7;
1826:   PetscBool         b1,b2,flg;
1827:   EPS_CISS          *ctx = (EPS_CISS*)eps->data;
1828:   EPSCISSQuadRule   quad;
1829:   EPSCISSExtraction extraction;

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

1834:     EPSCISSGetSizes(eps,&i1,&i2,&i3,&i4,&i5,&b1);
1835:     PetscOptionsInt("-eps_ciss_integration_points","Number of integration points","EPSCISSSetSizes",i1,&i1,NULL);
1836:     PetscOptionsInt("-eps_ciss_blocksize","Block size","EPSCISSSetSizes",i2,&i2,NULL);
1837:     PetscOptionsInt("-eps_ciss_moments","Moment size","EPSCISSSetSizes",i3,&i3,NULL);
1838:     PetscOptionsInt("-eps_ciss_partitions","Number of partitions","EPSCISSSetSizes",i4,&i4,NULL);
1839:     PetscOptionsInt("-eps_ciss_maxblocksize","Maximum block size","EPSCISSSetSizes",i5,&i5,NULL);
1840:     PetscOptionsBool("-eps_ciss_realmats","True if A and B are real","EPSCISSSetSizes",b1,&b1,NULL);
1841:     EPSCISSSetSizes(eps,i1,i2,i3,i4,i5,b1);

1843:     EPSCISSGetThreshold(eps,&r3,&r4);
1844:     PetscOptionsReal("-eps_ciss_delta","Threshold for numerical rank","EPSCISSSetThreshold",r3,&r3,NULL);
1845:     PetscOptionsReal("-eps_ciss_spurious_threshold","Threshold for the spurious eigenpairs","EPSCISSSetThreshold",r4,&r4,NULL);
1846:     EPSCISSSetThreshold(eps,r3,r4);

1848:     EPSCISSGetRefinement(eps,&i6,&i7);
1849:     PetscOptionsInt("-eps_ciss_refine_inner","Number of inner iterative refinement iterations","EPSCISSSetRefinement",i6,&i6,NULL);
1850:     PetscOptionsInt("-eps_ciss_refine_blocksize","Number of blocksize iterative refinement iterations","EPSCISSSetRefinement",i7,&i7,NULL);
1851:     EPSCISSSetRefinement(eps,i6,i7);

1853:     EPSCISSGetUseST(eps,&b2);
1854:     PetscOptionsBool("-eps_ciss_usest","Use ST for linear solves","EPSCISSSetUseST",b2,&b2,&flg);
1855:     if (flg) { EPSCISSSetUseST(eps,b2); }

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

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

1863:   PetscOptionsTail();

1865:   if (!eps->rg) { EPSGetRG(eps,&eps->rg); }
1866:   RGSetFromOptions(eps->rg); /* this is necessary here to set useconj */
1867:   if (!ctx->ksp) { EPSCISSGetKSPs(eps,&ctx->num_solve_point,&ctx->ksp); }
1868:   for (i=0;i<ctx->num_solve_point;i++) {
1869:     KSPSetFromOptions(ctx->ksp[i]);
1870:   }
1871:   PetscSubcommSetFromOptions(ctx->subcomm);
1872:   return(0);
1873: }

1875: PetscErrorCode EPSDestroy_CISS(EPS eps)
1876: {
1878:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1881:   EPSCISSResetSubcomm(eps);
1882:   PetscFree4(ctx->weight,ctx->omega,ctx->pp,ctx->sigma);
1883:   PetscFree(eps->data);
1884:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetSizes_C",NULL);
1885:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetSizes_C",NULL);
1886:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetThreshold_C",NULL);
1887:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetThreshold_C",NULL);
1888:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetRefinement_C",NULL);
1889:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetRefinement_C",NULL);
1890:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetUseST_C",NULL);
1891:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetUseST_C",NULL);
1892:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetQuadRule_C",NULL);
1893:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetQuadRule_C",NULL);
1894:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSSetExtraction_C",NULL);
1895:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetExtraction_C",NULL);
1896:   PetscObjectComposeFunction((PetscObject)eps,"EPSCISSGetKSPs_C",NULL);
1897:   return(0);
1898: }

1900: PetscErrorCode EPSView_CISS(EPS eps,PetscViewer viewer)
1901: {
1903:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1904:   PetscBool      isascii;

1907:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1908:   if (isascii) {
1909:     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);
1910:     if (ctx->isreal) {
1911:       PetscViewerASCIIPrintf(viewer,"  exploiting symmetry of integration points\n");
1912:     }
1913:     PetscViewerASCIIPrintf(viewer,"  threshold { delta: %g, spurious threshold: %g }\n",(double)ctx->delta,(double)ctx->spurious_threshold);
1914:     PetscViewerASCIIPrintf(viewer,"  iterative refinement  { inner: %D, blocksize: %D }\n",ctx->refine_inner, ctx->refine_blocksize);
1915:     PetscViewerASCIIPrintf(viewer,"  extraction: %s\n",EPSCISSExtractions[ctx->extraction]);
1916:     PetscViewerASCIIPrintf(viewer,"  quadrature rule: %s\n",EPSCISSQuadRules[ctx->quad]);
1917:     if (ctx->usest) {
1918:       PetscViewerASCIIPrintf(viewer,"  using ST for linear solves\n");
1919:     } else {
1920:       if (!ctx->ksp) { EPSCISSGetKSPs(eps,&ctx->num_solve_point,&ctx->ksp); }
1921:       PetscViewerASCIIPushTab(viewer);
1922:       KSPView(ctx->ksp[0],viewer);
1923:       PetscViewerASCIIPopTab(viewer);
1924:     }
1925:   }
1926:   return(0);
1927: }

1929: PetscErrorCode EPSSetDefaultST_CISS(EPS eps)
1930: {
1932:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;
1933:   PetscBool      usest = ctx->usest;

1936:   if (!((PetscObject)eps->st)->type_name) {
1937:     if (!ctx->usest_set) usest = (ctx->npart>1)? PETSC_FALSE: PETSC_TRUE;
1938:     if (usest) {
1939:       STSetType(eps->st,STSINVERT);
1940:     } else {
1941:       /* we are not going to use ST, so avoid factorizing the matrix */
1942:       STSetType(eps->st,STSHIFT);
1943:     }
1944:   }
1945:   return(0);
1946: }

1948: SLEPC_EXTERN PetscErrorCode EPSCreate_CISS(EPS eps)
1949: {
1951:   EPS_CISS       *ctx = (EPS_CISS*)eps->data;

1954:   PetscNewLog(eps,&ctx);
1955:   eps->data = ctx;

1957:   eps->useds = PETSC_TRUE;
1958:   eps->categ = EPS_CATEGORY_CONTOUR;

1960:   eps->ops->solve          = EPSSolve_CISS;
1961:   eps->ops->setup          = EPSSetUp_CISS;
1962:   eps->ops->setfromoptions = EPSSetFromOptions_CISS;
1963:   eps->ops->destroy        = EPSDestroy_CISS;
1964:   eps->ops->reset          = EPSReset_CISS;
1965:   eps->ops->view           = EPSView_CISS;
1966:   eps->ops->computevectors = EPSComputeVectors_Schur;
1967:   eps->ops->setdefaultst   = EPSSetDefaultST_CISS;

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