Actual source code: qslice.c

slepc-3.14.0 2020-09-30
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 polynomial eigensolver: "stoar"

 13:    Method: S-TOAR with spectrum slicing for symmetric quadratic eigenproblems

 15:    Algorithm:

 17:        Symmetric Two-Level Orthogonal Arnoldi.

 19:    References:

 21:        [1] C. Campos and J.E. Roman, "Inertia-based spectrum slicing
 22:            for symmetric quadratic eigenvalue problems", Numer. Linear
 23:            Algebra Appl. 27(4):e2293, 2020.
 24: */

 26: #include <slepc/private/pepimpl.h>
 27: #include "../src/pep/impls/krylov/pepkrylov.h"
 28: #include <slepcblaslapack.h>

 30: static PetscBool  cited = PETSC_FALSE;
 31: static const char citation[] =
 32:   "@Article{slepc-slice-qep,\n"
 33:   "   author = \"C. Campos and J. E. Roman\",\n"
 34:   "   title = \"Inertia-based spectrum slicing for symmetric quadratic eigenvalue problems\",\n"
 35:   "   journal = \"Numer. Linear Algebra Appl.\",\n"
 36:   "   volume = \"27\",\n"
 37:   "   number = \"4\",\n"
 38:   "   pages = \"e2293\",\n"
 39:   "   year = \"2020,\"\n"
 40:   "   doi = \"https://doi.org/10.1002/nla.2293\"\n"
 41:   "}\n";

 43: #define SLICE_PTOL PETSC_SQRT_MACHINE_EPSILON

 45: static PetscErrorCode PEPQSliceResetSR(PEP pep)
 46: {
 48:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
 49:   PEP_SR         sr=ctx->sr;
 50:   PEP_shift      s;
 51:   PetscInt       i;

 54:   if (sr) {
 55:     /* Reviewing list of shifts to free memory */
 56:     s = sr->s0;
 57:     if (s) {
 58:       while (s->neighb[1]) {
 59:         s = s->neighb[1];
 60:         PetscFree(s->neighb[0]);
 61:       }
 62:       PetscFree(s);
 63:     }
 64:     PetscFree(sr->S);
 65:     for (i=0;i<pep->nconv;i++) {PetscFree(sr->qinfo[i].q);}
 66:     PetscFree(sr->qinfo);
 67:     for (i=0;i<3;i++) {VecDestroy(&sr->v[i]);}
 68:     EPSDestroy(&sr->eps);
 69:     PetscFree(sr);
 70:   }
 71:   ctx->sr = NULL;
 72:   return(0);
 73: }

 75: PetscErrorCode PEPReset_STOAR_QSlice(PEP pep)
 76: {
 78:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;

 81:   PEPQSliceResetSR(pep);
 82:   PetscFree(ctx->inertias);
 83:   PetscFree(ctx->shifts);
 84:   return(0);
 85: }

 87: /*
 88:   PEPQSliceAllocateSolution - Allocate memory storage for common variables such
 89:   as eigenvalues and eigenvectors.
 90: */
 91: static PetscErrorCode PEPQSliceAllocateSolution(PEP pep)
 92: {
 94:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
 95:   PetscInt       k;
 96:   PetscLogDouble cnt;
 97:   BVType         type;
 98:   Vec            t;
 99:   PEP_SR         sr = ctx->sr;

102:   /* allocate space for eigenvalues and friends */
103:   k = PetscMax(1,sr->numEigs);
104:   PetscFree4(sr->eigr,sr->eigi,sr->errest,sr->perm);
105:   PetscCalloc4(k,&sr->eigr,k,&sr->eigi,k,&sr->errest,k,&sr->perm);
106:   PetscFree(sr->qinfo);
107:   PetscCalloc1(k,&sr->qinfo);
108:   cnt = 2*k*sizeof(PetscScalar) + 2*k*sizeof(PetscReal) + k*sizeof(PetscInt);
109:   PetscLogObjectMemory((PetscObject)pep,cnt);

111:   /* allocate sr->V and transfer options from pep->V */
112:   BVDestroy(&sr->V);
113:   BVCreate(PetscObjectComm((PetscObject)pep),&sr->V);
114:   PetscLogObjectParent((PetscObject)pep,(PetscObject)sr->V);
115:   if (!pep->V) { PEPGetBV(pep,&pep->V); }
116:   if (!((PetscObject)(pep->V))->type_name) {
117:     BVSetType(sr->V,BVSVEC);
118:   } else {
119:     BVGetType(pep->V,&type);
120:     BVSetType(sr->V,type);
121:   }
122:   STMatCreateVecsEmpty(pep->st,&t,NULL);
123:   BVSetSizesFromVec(sr->V,t,k+1);
124:   VecDestroy(&t);
125:   sr->ld = k;
126:   PetscFree(sr->S);
127:   PetscMalloc1((k+1)*sr->ld*(pep->nmat-1),&sr->S);
128:   return(0);
129: }

131: /* Convergence test to compute positive Ritz values */
132: static PetscErrorCode ConvergedPositive(EPS eps,PetscScalar eigr,PetscScalar eigi,PetscReal res,PetscReal *errest,void *ctx)
133: {
135:   *errest = (PetscRealPart(eigr)>0.0)?0.0:res;
136:   return(0);
137: }

139: static PetscErrorCode PEPQSliceMatGetInertia(PEP pep,PetscReal shift,PetscInt *inertia,PetscInt *zeros)
140: {
141:   KSP            ksp,kspr;
142:   PC             pc;
143:   Mat            F;
144:   PetscBool      flg;

148:   if (!pep->solvematcoeffs) {
149:     PetscMalloc1(pep->nmat,&pep->solvematcoeffs);
150:   }
151:   if (shift==PETSC_MAX_REAL) { /* Inertia of matrix A[2] */
152:     pep->solvematcoeffs[0] = 0.0; pep->solvematcoeffs[1] = 0.0; pep->solvematcoeffs[2] = 1.0;
153:   } else {
154:     PEPEvaluateBasis(pep,shift,0,pep->solvematcoeffs,NULL);
155:   }
156:   STMatSetUp(pep->st,pep->sfactor,pep->solvematcoeffs);
157:   STGetKSP(pep->st,&ksp);
158:   KSPGetPC(ksp,&pc);
159:   PetscObjectTypeCompare((PetscObject)pc,PCREDUNDANT,&flg);
160:   if (flg) {
161:     PCRedundantGetKSP(pc,&kspr);
162:     KSPGetPC(kspr,&pc);
163:   }
164:   PCFactorGetMatrix(pc,&F);
165:   MatGetInertia(F,inertia,zeros,NULL);
166:   return(0);
167: }

169: static PetscErrorCode PEPQSliceGetInertia(PEP pep,PetscReal shift,PetscInt *inertia,PetscInt *zeros,PetscInt correction)
170: {
172:   KSP            ksp;
173:   Mat            P;
174:   PetscReal      nzshift=0.0;
175:   PetscScalar    dot;
176:   PetscRandom    rand;
177:   PetscInt       nconv;
178:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
179:   PEP_SR         sr=ctx->sr;

182:   if (shift >= PETSC_MAX_REAL) { /* Right-open interval */
183:     *inertia = 0;
184:   } else if (shift <= PETSC_MIN_REAL) {
185:     *inertia = 0;
186:     if (zeros) *zeros = 0;
187:   } else {
188:     /* If the shift is zero, perturb it to a very small positive value.
189:        The goal is that the nonzero pattern is the same in all cases and reuse
190:        the symbolic factorizations */
191:     nzshift = (shift==0.0)? 10.0/PETSC_MAX_REAL: shift;
192:     PEPQSliceMatGetInertia(pep,nzshift,inertia,zeros);
193:     STSetShift(pep->st,nzshift);
194:   }
195:   if (!correction) {
196:     if (shift >= PETSC_MAX_REAL) *inertia = 2*pep->n;
197:     else if (shift>PETSC_MIN_REAL) {
198:       STGetKSP(pep->st,&ksp);
199:       KSPGetOperators(ksp,&P,NULL);
200:       if (*inertia!=pep->n && !sr->v[0]) {
201:         MatCreateVecs(P,&sr->v[0],NULL);
202:         VecDuplicate(sr->v[0],&sr->v[1]);
203:         VecDuplicate(sr->v[0],&sr->v[2]);
204:         BVGetRandomContext(pep->V,&rand);
205:         VecSetRandom(sr->v[0],rand);
206:       }
207:       if (*inertia<pep->n && *inertia>0) {
208:         if (!sr->eps) {
209:           EPSCreate(PetscObjectComm((PetscObject)pep),&sr->eps);
210:           EPSSetProblemType(sr->eps,EPS_HEP);
211:           EPSSetWhichEigenpairs(sr->eps,EPS_LARGEST_REAL);
212:         }
213:         EPSSetConvergenceTestFunction(sr->eps,ConvergedPositive,NULL,NULL);
214:         EPSSetOperators(sr->eps,P,NULL);
215:         EPSSolve(sr->eps);
216:         EPSGetConverged(sr->eps,&nconv);
217:         if (!nconv) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",nzshift);
218:         EPSGetEigenpair(sr->eps,0,NULL,NULL,sr->v[0],sr->v[1]);
219:       }
220:       if (*inertia!=pep->n) {
221:         MatMult(pep->A[1],sr->v[0],sr->v[1]);
222:         MatMult(pep->A[2],sr->v[0],sr->v[2]);
223:         VecAXPY(sr->v[1],2*nzshift,sr->v[2]);
224:         VecDot(sr->v[1],sr->v[0],&dot);
225:         if (PetscRealPart(dot)>0.0) *inertia = 2*pep->n-*inertia;
226:       }
227:     }
228:   } else if (correction<0) *inertia = 2*pep->n-*inertia;
229:   return(0);
230: }

232: /*
233:    Check eigenvalue type - used only in non-hyperbolic problems.
234:    All computed eigenvalues must have the same definite type (positive or negative).
235:    If ini=TRUE the type is available in omega, otherwise we compute an eigenvalue
236:    closest to shift and determine its type.
237:  */
238: static PetscErrorCode PEPQSliceCheckEigenvalueType(PEP pep,PetscReal shift,PetscReal omega,PetscBool ini)
239: {
241:   PEP            pep2;
242:   ST             st;
243:   PetscInt       nconv;
244:   PetscScalar    lambda,dot;
245:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
246:   PEP_SR         sr=ctx->sr;

249:   if (!ini) {
250:     if (-(omega/(shift*ctx->alpha+ctx->beta))*sr->type<0) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Different positive/negative type detected in eigenvalue %g",(double)shift);
251:   } else {
252:     PEPCreate(PetscObjectComm((PetscObject)pep),&pep2);
253:     PEPSetOptionsPrefix(pep2,((PetscObject)pep)->prefix);
254:     PEPAppendOptionsPrefix(pep2,"pep_eigenvalue_type_");
255:     PEPSetTolerances(pep2,PETSC_DEFAULT,pep->max_it/4);
256:     PEPSetType(pep2,PEPTOAR);
257:     PEPSetOperators(pep2,pep->nmat,pep->A);
258:     PEPSetWhichEigenpairs(pep2,PEP_TARGET_MAGNITUDE);
259:     PEPGetRG(pep2,&pep2->rg);
260:     RGSetType(pep2->rg,RGINTERVAL);
261: #if defined(PETSC_USE_COMPLEX)
262:     RGIntervalSetEndpoints(pep2->rg,pep->inta,pep->intb,-PETSC_SQRT_MACHINE_EPSILON,PETSC_SQRT_MACHINE_EPSILON);
263: #else
264:     RGIntervalSetEndpoints(pep2->rg,pep->inta,pep->intb,0.0,0.0);
265: #endif
266:     pep2->target = shift;
267:     st = pep2->st;
268:     pep2->st = pep->st;
269:     PEPSolve(pep2);
270:     PEPGetConverged(pep2,&nconv);
271:     if (nconv) {
272:       PEPGetEigenpair(pep2,0,&lambda,NULL,pep2->work[0],NULL);
273:       MatMult(pep->A[1],pep2->work[0],pep2->work[1]);
274:       MatMult(pep->A[2],pep2->work[0],pep2->work[2]);
275:       VecAXPY(pep2->work[1],2.0*lambda*pep->sfactor,pep2->work[2]);
276:       VecDot(pep2->work[1],pep2->work[0],&dot);
277:       PetscInfo2(pep,"lambda=%g, %s type\n",(double)PetscRealPart(lambda),(PetscRealPart(dot)>0.0)?"positive":"negative");
278:       if (!sr->type) sr->type = (PetscRealPart(dot)>0.0)?1:-1;
279:       else {
280:         if (sr->type*PetscRealPart(dot)<0.0) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Different positive/negative type detected in eigenvalue %g",(double)PetscRealPart(lambda));
281:       }
282:     }
283:     pep2->st = st;
284:     PEPDestroy(&pep2);
285:   }
286:   return(0);
287: }

289: PETSC_STATIC_INLINE PetscErrorCode PEPQSliceDiscriminant(PEP pep,Vec u,Vec w,PetscReal *d,PetscReal *smas,PetscReal *smenos)
290: {
291:   PetscReal      ap,bp,cp,dis;
292:   PetscScalar    ts;

296:   MatMult(pep->A[0],u,w);
297:   VecDot(w,u,&ts);
298:   cp = PetscRealPart(ts);
299:   MatMult(pep->A[1],u,w);
300:   VecDot(w,u,&ts);
301:   bp = PetscRealPart(ts);
302:   MatMult(pep->A[2],u,w);
303:   VecDot(w,u,&ts);
304:   ap = PetscRealPart(ts);
305:   dis = bp*bp-4*ap*cp;
306:   if (dis>=0.0 && smas) {
307:     if (ap>0) *smas = (-bp+PetscSqrtReal(dis))/(2*ap);
308:     else if (ap<0) *smas = (-bp-PetscSqrtReal(dis))/(2*ap);
309:     else {
310:       if (bp >0) *smas = -cp/bp;
311:       else *smas = PETSC_MAX_REAL;
312:     }
313:   }
314:   if (dis>=0.0 && smenos) {
315:     if (ap>0) *smenos = (-bp-PetscSqrtReal(dis))/(2*ap);
316:     else if (ap<0) *smenos = (-bp+PetscSqrtReal(dis))/(2*ap);
317:     else {
318:       if (bp<0) *smenos = -cp/bp;
319:       else *smenos = PETSC_MAX_REAL;
320:     }
321:   }
322:   if (d) *d = dis;
323:   return(0);
324: }

326: PETSC_STATIC_INLINE PetscErrorCode PEPQSliceEvaluateQEP(PEP pep,PetscScalar x,Mat M,MatStructure str)
327: {

331:   MatCopy(pep->A[0],M,SAME_NONZERO_PATTERN);
332:   MatAXPY(M,x,pep->A[1],str);
333:   MatAXPY(M,x*x,pep->A[2],str);
334:   return(0);
335: }

337: /*@
338:    PEPCheckDefiniteQEP - Determines if a symmetric/Hermitian quadratic eigenvalue problem
339:    is definite or not.

341:    Logically Collective on pep

343:    Input Parameter:
344: .  pep  - eigensolver context

346:    Output Parameters:
347: +  xi - first computed parameter
348: .  mu - second computed parameter
349: .  definite - flag indicating that the problem is definite
350: -  hyperbolic - flag indicating that the problem is hyperbolic

352:    Notes:
353:    This function is intended for quadratic eigenvalue problems, Q(lambda)=A*lambda^2+B*lambda+C,
354:    with symmetric (or Hermitian) coefficient matrices A,B,C.

356:    On output, the flag 'definite' may have the values -1 (meaning that the QEP is not
357:    definite), 1 (if the problem is definite), or 0 if the algorithm was not able to
358:    determine whether the problem is definite or not.

360:    If definite=1, the output flag 'hyperbolic' informs in a similar way about whether the
361:    problem is hyperbolic or not.

363:    If definite=1, the computed values xi and mu satisfy Q(xi)<0 and Q(mu)>0, as
364:    obtained via the method proposed in [Niendorf and Voss, LAA 2010]. Furthermore, if
365:    hyperbolic=1 then only xi is computed.

367:    Level: advanced
368: @*/
369: PetscErrorCode PEPCheckDefiniteQEP(PEP pep,PetscReal *xi,PetscReal *mu,PetscInt *definite,PetscInt *hyperbolic)
370: {
372:   PetscRandom    rand;
373:   Vec            u,w;
374:   PetscReal      d=0.0,s=0.0,sp,mut=0.0,omg=0.0,omgp;
375:   PetscInt       k,its=10,hyp=0,check=0,nconv,inertia,n;
376:   Mat            M=NULL;
377:   MatStructure   str;
378:   EPS            eps;
379:   PetscBool      transform,ptypehyp;

382:   if (pep->problem_type!=PEP_HERMITIAN && pep->problem_type!=PEP_HYPERBOLIC) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"Only available for Hermitian (or hyperbolic) problems");
383:   ptypehyp = (pep->problem_type==PEP_HYPERBOLIC)? PETSC_TRUE: PETSC_FALSE;
384:   if (!pep->st) { PEPGetST(pep,&pep->st); }
385:   PEPSetDefaultST(pep);
386:   STSetMatrices(pep->st,pep->nmat,pep->A);
387:   MatGetSize(pep->A[0],&n,NULL);
388:   STGetTransform(pep->st,&transform);
389:   STSetTransform(pep->st,PETSC_FALSE);
390:   STSetUp(pep->st);
391:   MatCreateVecs(pep->A[0],&u,&w);
392:   PEPGetBV(pep,&pep->V);
393:   BVGetRandomContext(pep->V,&rand);
394:   VecSetRandom(u,rand);
395:   VecNormalize(u,NULL);
396:   PEPQSliceDiscriminant(pep,u,w,&d,&s,NULL);
397:   if (d<0.0) check = -1;
398:   if (!check) {
399:     EPSCreate(PetscObjectComm((PetscObject)pep),&eps);
400:     EPSSetProblemType(eps,EPS_HEP);
401:     EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL);
402:     EPSSetTolerances(eps,PetscSqrtReal(PETSC_SQRT_MACHINE_EPSILON),PETSC_DECIDE);
403:     MatDuplicate(pep->A[0],MAT_DO_NOT_COPY_VALUES,&M);
404:     STGetMatStructure(pep->st,&str);
405:   }
406:   for (k=0;k<its&&!check;k++) {
407:     PEPQSliceEvaluateQEP(pep,s,M,str);
408:     EPSSetOperators(eps,M,NULL);
409:     EPSSolve(eps);
410:     EPSGetConverged(eps,&nconv);
411:     if (!nconv) break;
412:     EPSGetEigenpair(eps,0,NULL,NULL,u,w);
413:     sp = s;
414:     PEPQSliceDiscriminant(pep,u,w,&d,&s,&omg);
415:     if (d<0.0) {check = -1; break;}
416:     if (PetscAbsReal((s-sp)/s)<100*PETSC_MACHINE_EPSILON) break;
417:     if (s>sp) {hyp = -1;}
418:     mut = 2*s-sp;
419:      PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
420:     if (inertia == n) {check = 1; break;}
421:   }
422:   for (;k<its&&!check;k++) {
423:     mut = (s-omg)/2;
424:      PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
425:     if (inertia == n) {check = 1; break;}
426:     if (PetscAbsReal((s-omg)/omg)<100*PETSC_MACHINE_EPSILON) break;
427:     PEPQSliceEvaluateQEP(pep,omg,M,str);
428:     EPSSetOperators(eps,M,NULL);
429:     EPSSolve(eps);
430:     EPSGetConverged(eps,&nconv);
431:     if (!nconv) break;
432:     EPSGetEigenpair(eps,0,NULL,NULL,u,w);
433:     omgp = omg;
434:     PEPQSliceDiscriminant(pep,u,w,&d,NULL,&omg);
435:     if (d<0.0) {check = -1; break;}
436:     if (omg<omgp) hyp = -1;
437:   }
438:   if (check==1) *xi = mut;
439:   if (hyp==-1 && ptypehyp) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Problem does not satisfy hyperbolic test; consider removing the hyperbolicity flag");
440:   if (check==1 && hyp==0) {
441:      PEPQSliceMatGetInertia(pep,PETSC_MAX_REAL,&inertia,NULL);
442:     if (inertia == 0) hyp = 1;
443:     else hyp = -1;
444:   }
445:   if (check==1 && hyp!=1) {
446:     check = 0;
447:     EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
448:     for (;k<its&&!check;k++) {
449:       PEPQSliceEvaluateQEP(pep,s,M,str);
450:       EPSSetOperators(eps,M,NULL);
451:       EPSSolve(eps);
452:       EPSGetConverged(eps,&nconv);
453:       if (!nconv) break;
454:       EPSGetEigenpair(eps,0,NULL,NULL,u,w);
455:       sp = s;
456:       PEPQSliceDiscriminant(pep,u,w,&d,&s,&omg);
457:       if (d<0.0) {check = -1; break;}
458:       if (PetscAbsReal((s-sp)/s)<100*PETSC_MACHINE_EPSILON) break;
459:       mut = 2*s-sp;
460:        PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
461:       if (inertia == 0) {check = 1; break;}
462:     }
463:     for (;k<its&&!check;k++) {
464:       mut = (s-omg)/2;
465:        PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
466:       if (inertia == 0) {check = 1; break;}
467:       if (PetscAbsReal((s-omg)/omg)<100*PETSC_MACHINE_EPSILON) break;
468:       PEPQSliceEvaluateQEP(pep,omg,M,str);
469:       EPSSetOperators(eps,M,NULL);
470:       EPSSolve(eps);
471:       EPSGetConverged(eps,&nconv);
472:       if (!nconv) break;
473:       EPSGetEigenpair(eps,0,NULL,NULL,u,w);
474:       PEPQSliceDiscriminant(pep,u,w,&d,NULL,&omg);
475:       if (d<0.0) {check = -1; break;}
476:     }
477:   }
478:   if (check==1) *mu = mut;
479:   *definite = check;
480:   *hyperbolic = hyp;
481:   if (M) { MatDestroy(&M); }
482:   VecDestroy(&u);
483:   VecDestroy(&w);
484:   EPSDestroy(&eps);
485:   STSetTransform(pep->st,transform);
486:   return(0);
487: }

489: /*
490:    Dummy backtransform operation
491:  */
492: static PetscErrorCode PEPBackTransform_Skip(PEP pep)
493: {
495:   return(0);
496: }

498: PetscErrorCode PEPSetUp_STOAR_QSlice(PEP pep)
499: {
501:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
502:   PEP_SR         sr;
503:   PetscInt       ld,i,zeros=0;
504:   SlepcSC        sc;
505:   PetscReal      r;

508:   PEPCheckSinvertCayley(pep);
509:   if (pep->inta==pep->intb) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"This solver does not support computing all eigenvalues unless you provide a computational interval with PEPSetInterval()");
510:   if (pep->intb >= PETSC_MAX_REAL && pep->inta <= PETSC_MIN_REAL) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONG,"The defined computational interval should have at least one of their sides bounded");
511:   PEPCheckUnsupportedCondition(pep,PEP_FEATURE_STOPPING,PETSC_TRUE," (with spectrum slicing)");
512:   if (pep->tol==PETSC_DEFAULT) {
513: #if defined(PETSC_USE_REAL_SINGLE)
514:     pep->tol = SLEPC_DEFAULT_TOL;
515: #else
516:     /* use tighter tolerance */
517:     pep->tol = SLEPC_DEFAULT_TOL*1e-2;
518: #endif
519:   }
520:   if (ctx->nev==1) ctx->nev = PetscMin(20,pep->n);  /* nev not set, use default value */
521:   if (pep->n>10 && ctx->nev<10) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONG,"nev cannot be less than 10 in spectrum slicing runs");
522:   pep->ops->backtransform = PEPBackTransform_Skip;
523:   if (pep->max_it==PETSC_DEFAULT) pep->max_it = 100;

525:   /* create spectrum slicing context and initialize it */
526:   PEPQSliceResetSR(pep);
527:   PetscNewLog(pep,&sr);
528:   ctx->sr   = sr;
529:   sr->itsKs = 0;
530:   sr->nleap = 0;
531:   sr->sPres = NULL;

533:   if (pep->solvematcoeffs) { PetscFree(pep->solvematcoeffs); }
534:   PetscMalloc1(pep->nmat,&pep->solvematcoeffs);
535:   if (!pep->st) { PEPGetST(pep,&pep->st); }
536:   STSetTransform(pep->st,PETSC_FALSE);
537:   STSetUp(pep->st);

539:   ctx->hyperbolic = (pep->problem_type==PEP_HYPERBOLIC)? PETSC_TRUE: PETSC_FALSE;

541:   /* check presence of ends and finding direction */
542:   if (pep->inta > PETSC_MIN_REAL || pep->intb >= PETSC_MAX_REAL) {
543:     sr->int0 = pep->inta;
544:     sr->int1 = pep->intb;
545:     sr->dir = 1;
546:     if (pep->intb >= PETSC_MAX_REAL) { /* Right-open interval */
547:       sr->hasEnd = PETSC_FALSE;
548:     } else sr->hasEnd = PETSC_TRUE;
549:   } else {
550:     sr->int0 = pep->intb;
551:     sr->int1 = pep->inta;
552:     sr->dir = -1;
553:     sr->hasEnd = PetscNot(pep->inta <= PETSC_MIN_REAL);
554:   }

556:   /* compute inertia0 */
557:   PEPQSliceGetInertia(pep,sr->int0,&sr->inertia0,ctx->detect?&zeros:NULL,ctx->hyperbolic?0:1);
558:   if (zeros && (sr->int0==pep->inta || sr->int0==pep->intb)) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_USER,"Found singular matrix for the transformed problem in the interval endpoint");
559:   if (!ctx->hyperbolic && ctx->checket) {
560:     PEPQSliceCheckEigenvalueType(pep,sr->int0,0.0,PETSC_TRUE);
561:   }

563:   /* compute inertia1 */
564:   PEPQSliceGetInertia(pep,sr->int1,&sr->inertia1,ctx->detect?&zeros:NULL,ctx->hyperbolic?0:1);
565:   if (zeros) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_USER,"Found singular matrix for the transformed problem in an interval endpoint defined by user");
566:   if (!ctx->hyperbolic && ctx->checket && sr->hasEnd) {
567:     PEPQSliceCheckEigenvalueType(pep,sr->int1,0.0,PETSC_TRUE);
568:     if (!sr->type && (sr->inertia1-sr->inertia0)) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"No information of eigenvalue type in Interval");
569:     if (sr->type && !(sr->inertia1-sr->inertia0)) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Different positive/negative type detected");
570:     if (sr->dir*(sr->inertia1-sr->inertia0)<0) {
571:       sr->intcorr = -1;
572:       sr->inertia0 = 2*pep->n-sr->inertia0;
573:       sr->inertia1 = 2*pep->n-sr->inertia1;
574:     } else sr->intcorr = 1;
575:   } else {
576:     if (sr->inertia0<=pep->n && sr->inertia1<=pep->n) sr->intcorr = 1;
577:     else if (sr->inertia0>=pep->n && sr->inertia1>=pep->n) sr->intcorr = -1;
578:   }

580:   if (sr->hasEnd) {
581:     sr->dir = -sr->dir; r = sr->int0; sr->int0 = sr->int1; sr->int1 = r;
582:     i = sr->inertia0; sr->inertia0 = sr->inertia1; sr->inertia1 = i;
583:   }

585:   /* number of eigenvalues in interval */
586:   sr->numEigs = (sr->dir)*(sr->inertia1 - sr->inertia0);
587:   PetscInfo3(pep,"QSlice setup: allocating for %D eigenvalues in [%g,%g]\n",sr->numEigs,(double)pep->inta,(double)pep->intb);
588:   if (sr->numEigs) {
589:     PEPQSliceAllocateSolution(pep);
590:     PEPSetDimensions_Default(pep,ctx->nev,&ctx->ncv,&ctx->mpd);
591:     pep->nev = ctx->nev; pep->ncv = ctx->ncv; pep->mpd = ctx->mpd;
592:     ld   = ctx->ncv+2;
593:     DSSetType(pep->ds,DSGHIEP);
594:     DSSetCompact(pep->ds,PETSC_TRUE);
595:     DSAllocate(pep->ds,ld);
596:     DSGetSlepcSC(pep->ds,&sc);
597:     sc->rg            = NULL;
598:     sc->comparison    = SlepcCompareLargestMagnitude;
599:     sc->comparisonctx = NULL;
600:     sc->map           = NULL;
601:     sc->mapobj        = NULL;
602:   }
603:   return(0);
604: }

606: /*
607:    Fills the fields of a shift structure
608: */
609: static PetscErrorCode PEPCreateShift(PEP pep,PetscReal val,PEP_shift neighb0,PEP_shift neighb1)
610: {
612:   PEP_shift      s,*pending2;
613:   PetscInt       i;
614:   PEP_SR         sr;
615:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;

618:   sr = ctx->sr;
619:   PetscNewLog(pep,&s);
620:   s->value = val;
621:   s->neighb[0] = neighb0;
622:   if (neighb0) neighb0->neighb[1] = s;
623:   s->neighb[1] = neighb1;
624:   if (neighb1) neighb1->neighb[0] = s;
625:   s->comp[0] = PETSC_FALSE;
626:   s->comp[1] = PETSC_FALSE;
627:   s->index = -1;
628:   s->neigs = 0;
629:   s->nconv[0] = s->nconv[1] = 0;
630:   s->nsch[0] = s->nsch[1]=0;
631:   /* Inserts in the stack of pending shifts */
632:   /* If needed, the array is resized */
633:   if (sr->nPend >= sr->maxPend) {
634:     sr->maxPend *= 2;
635:     PetscMalloc1(sr->maxPend,&pending2);
636:     PetscLogObjectMemory((PetscObject)pep,sizeof(PEP_shift));
637:     for (i=0;i<sr->nPend;i++) pending2[i] = sr->pending[i];
638:     PetscFree(sr->pending);
639:     sr->pending = pending2;
640:   }
641:   sr->pending[sr->nPend++]=s;
642:   return(0);
643: }

645: /* Provides next shift to be computed */
646: static PetscErrorCode PEPExtractShift(PEP pep)
647: {
649:   PetscInt       iner,zeros=0;
650:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
651:   PEP_SR         sr;
652:   PetscReal      newShift,aux;
653:   PEP_shift      sPres;

656:   sr = ctx->sr;
657:   if (sr->nPend > 0) {
658:     if (sr->dirch) {
659:       aux = sr->int1; sr->int1 = sr->int0; sr->int0 = aux;
660:       iner = sr->inertia1; sr->inertia1 = sr->inertia0; sr->inertia0 = iner;
661:       sr->dir *= -1;
662:       PetscFree(sr->s0->neighb[1]);
663:       PetscFree(sr->s0);
664:       sr->nPend--;
665:       PEPCreateShift(pep,sr->int0,NULL,NULL);
666:       sr->sPrev = NULL;
667:       sr->sPres = sr->pending[--sr->nPend];
668:       pep->target = sr->sPres->value;
669:       sr->s0 = sr->sPres;
670:       pep->reason = PEP_CONVERGED_ITERATING;
671:     } else {
672:       sr->sPrev = sr->sPres;
673:       sr->sPres = sr->pending[--sr->nPend];
674:     }
675:     sPres = sr->sPres;
676:     PEPQSliceGetInertia(pep,sPres->value,&iner,ctx->detect?&zeros:NULL,sr->intcorr);
677:     if (zeros) {
678:       newShift = sPres->value*(1.0+SLICE_PTOL);
679:       if (sr->dir*(sPres->neighb[0] && newShift-sPres->neighb[0]->value) < 0) newShift = (sPres->value+sPres->neighb[0]->value)/2;
680:       else if (sPres->neighb[1] && sr->dir*(sPres->neighb[1]->value-newShift) < 0) newShift = (sPres->value+sPres->neighb[1]->value)/2;
681:       PEPQSliceGetInertia(pep,newShift,&iner,&zeros,sr->intcorr);
682:       if (zeros) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",newShift);
683:       sPres->value = newShift;
684:     }
685:     sr->sPres->inertia = iner;
686:     pep->target = sr->sPres->value;
687:     pep->reason = PEP_CONVERGED_ITERATING;
688:     pep->its = 0;
689:   } else sr->sPres = NULL;
690:   return(0);
691: }

693: /*
694:   Obtains value of subsequent shift
695: */
696: static PetscErrorCode PEPGetNewShiftValue(PEP pep,PetscInt side,PetscReal *newS)
697: {
698:   PetscReal lambda,d_prev;
699:   PetscInt  i,idxP;
700:   PEP_SR    sr;
701:   PEP_shift sPres,s;
702:   PEP_STOAR *ctx=(PEP_STOAR*)pep->data;

705:   sr = ctx->sr;
706:   sPres = sr->sPres;
707:   if (sPres->neighb[side]) {
708:   /* Completing a previous interval */
709:     if (!sPres->neighb[side]->neighb[side] && sPres->neighb[side]->nconv[side]==0) { /* One of the ends might be too far from eigenvalues */
710:       if (side) *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[sr->indexEig-1]]))/2;
711:       else *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[0]]))/2;
712:     } else *newS=(sPres->value + sPres->neighb[side]->value)/2;
713:   } else { /* (Only for side=1). Creating a new interval. */
714:     if (sPres->neigs==0) {/* No value has been accepted*/
715:       if (sPres->neighb[0]) {
716:         /* Multiplying by 10 the previous distance */
717:         *newS = sPres->value + 10*(sr->dir)*PetscAbsReal(sPres->value - sPres->neighb[0]->value);
718:         sr->nleap++;
719:         /* Stops when the interval is open and no values are found in the last 5 shifts (there might be infinite eigenvalues) */
720:         if (!sr->hasEnd && sr->nleap > 5) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Unable to compute the wanted eigenvalues with open interval");
721:       } else { /* First shift */
722:         if (pep->nconv != 0) {
723:           /* Unaccepted values give information for next shift */
724:           idxP=0;/* Number of values left from shift */
725:           for (i=0;i<pep->nconv;i++) {
726:             lambda = PetscRealPart(pep->eigr[i]);
727:             if ((sr->dir)*(lambda - sPres->value) <0) idxP++;
728:             else break;
729:           }
730:           /* Avoiding subtraction of eigenvalues (might be the same).*/
731:           if (idxP>0) {
732:             d_prev = PetscAbsReal(sPres->value - PetscRealPart(pep->eigr[0]))/(idxP+0.3);
733:           } else {
734:             d_prev = PetscAbsReal(sPres->value - PetscRealPart(pep->eigr[pep->nconv-1]))/(pep->nconv+0.3);
735:           }
736:           *newS = sPres->value + ((sr->dir)*d_prev*pep->nev)/2;
737:           sr->dirch = PETSC_FALSE;
738:         } else { /* No values found, no information for next shift */
739:           if (!sr->dirch) {
740:             sr->dirch = PETSC_TRUE;
741:             *newS = sr->int1;
742:           } else SETERRQ(PetscObjectComm((PetscObject)pep),1,"First shift renders no information");
743:         }
744:       }
745:     } else { /* Accepted values found */
746:       sr->dirch = PETSC_FALSE;
747:       sr->nleap = 0;
748:       /* Average distance of values in previous subinterval */
749:       s = sPres->neighb[0];
750:       while (s && PetscAbs(s->inertia - sPres->inertia)==0) {
751:         s = s->neighb[0];/* Looking for previous shifts with eigenvalues within */
752:       }
753:       if (s) {
754:         d_prev = PetscAbsReal((sPres->value - s->value)/(sPres->inertia - s->inertia));
755:       } else { /* First shift. Average distance obtained with values in this shift */
756:         /* first shift might be too far from first wanted eigenvalue (no values found outside the interval)*/
757:         if ((sr->dir)*(PetscRealPart(sr->eigr[0])-sPres->value)>0 && PetscAbsReal((PetscRealPart(sr->eigr[sr->indexEig-1]) - PetscRealPart(sr->eigr[0]))/PetscRealPart(sr->eigr[0])) > PetscSqrtReal(pep->tol)) {
758:           d_prev =  PetscAbsReal((PetscRealPart(sr->eigr[sr->indexEig-1]) - PetscRealPart(sr->eigr[0])))/(sPres->neigs+0.3);
759:         } else {
760:           d_prev = PetscAbsReal(PetscRealPart(sr->eigr[sr->indexEig-1]) - sPres->value)/(sPres->neigs+0.3);
761:         }
762:       }
763:       /* Average distance is used for next shift by adding it to value on the right or to shift */
764:       if ((sr->dir)*(PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1]) - sPres->value)>0) {
765:         *newS = PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1])+ ((sr->dir)*d_prev*(pep->nev))/2;
766:       } else { /* Last accepted value is on the left of shift. Adding to shift */
767:         *newS = sPres->value + ((sr->dir)*d_prev*(pep->nev))/2;
768:       }
769:     }
770:     /* End of interval can not be surpassed */
771:     if ((sr->dir)*(sr->int1 - *newS) < 0) *newS = sr->int1;
772:   }/* of neighb[side]==null */
773:   return(0);
774: }

776: /*
777:   Function for sorting an array of real values
778: */
779: static PetscErrorCode sortRealEigenvalues(PetscScalar *r,PetscInt *perm,PetscInt nr,PetscBool prev,PetscInt dir)
780: {
781:   PetscReal re;
782:   PetscInt  i,j,tmp;

785:   if (!prev) for (i=0;i<nr;i++) perm[i] = i;
786:   /* Insertion sort */
787:   for (i=1;i<nr;i++) {
788:     re = PetscRealPart(r[perm[i]]);
789:     j = i-1;
790:     while (j>=0 && dir*(re - PetscRealPart(r[perm[j]])) <= 0) {
791:       tmp = perm[j]; perm[j] = perm[j+1]; perm[j+1] = tmp; j--;
792:     }
793:   }
794:   return(0);
795: }

797: /* Stores the pairs obtained since the last shift in the global arrays */
798: static PetscErrorCode PEPStoreEigenpairs(PEP pep)
799: {
801:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
802:   PetscReal      lambda,err,*errest;
803:   PetscInt       i,*aux,count=0,ndef,ld,nconv=pep->nconv,d=pep->nmat-1,idx;
804:   PetscBool      iscayley,divide=PETSC_FALSE;
805:   PEP_SR         sr = ctx->sr;
806:   PEP_shift      sPres;
807:   Vec            w,vomega;
808:   Mat            MS;
809:   BV             tV;
810:   PetscScalar    *S,*eigr,*tS,*omega;

813:   sPres = sr->sPres;
814:   sPres->index = sr->indexEig;

816:   if (nconv>sr->ndef0+sr->ndef1) {
817:     /* Back-transform */
818:     STBackTransform(pep->st,nconv,pep->eigr,pep->eigi);
819:     for (i=0;i<nconv;i++) {
820: #if defined(PETSC_USE_COMPLEX)
821:       if (PetscImaginaryPart(pep->eigr[i])) pep->eigr[i] = sr->int0-sr->dir;
822: #else
823:       if (pep->eigi[i]) pep->eigr[i] = sr->int0-sr->dir;
824: #endif
825:     }
826:     PetscObjectTypeCompare((PetscObject)pep->st,STCAYLEY,&iscayley);
827:     /* Sort eigenvalues */
828:     sortRealEigenvalues(pep->eigr,pep->perm,nconv,PETSC_FALSE,sr->dir);
829:     VecCreateSeq(PETSC_COMM_SELF,nconv,&vomega);
830:     BVGetSignature(ctx->V,vomega);
831:     VecGetArray(vomega,&omega);
832:     BVGetSizes(pep->V,NULL,NULL,&ld);
833:     BVTensorGetFactors(ctx->V,NULL,&MS);
834:     MatDenseGetArray(MS,&S);
835:     /* Values stored in global array */
836:     PetscCalloc4(nconv,&eigr,nconv,&errest,nconv*nconv*d,&tS,nconv,&aux);
837:     ndef = sr->ndef0+sr->ndef1;
838:     for (i=0;i<nconv;i++) {
839:       lambda = PetscRealPart(pep->eigr[pep->perm[i]]);
840:       err = pep->errest[pep->perm[i]];
841:       if ((sr->dir)*(lambda - sPres->ext[0]) > 0 && (sr->dir)*(sPres->ext[1] - lambda) > 0) {/* Valid value */
842:         if (sr->indexEig+count-ndef>=sr->numEigs) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Unexpected error in Spectrum Slicing");
843:         PEPQSliceCheckEigenvalueType(pep,lambda,PetscRealPart(omega[pep->perm[i]]),PETSC_FALSE);
844:         eigr[count] = lambda;
845:         errest[count] = err;
846:         if (((sr->dir)*(sPres->value - lambda) > 0) && ((sr->dir)*(lambda - sPres->ext[0]) > 0)) sPres->nconv[0]++;
847:         if (((sr->dir)*(lambda - sPres->value) > 0) && ((sr->dir)*(sPres->ext[1] - lambda) > 0)) sPres->nconv[1]++;
848:         PetscArraycpy(tS+count*(d*nconv),S+pep->perm[i]*(d*ld),nconv);
849:         PetscArraycpy(tS+count*(d*nconv)+nconv,S+pep->perm[i]*(d*ld)+ld,nconv);
850:         count++;
851:       }
852:     }
853:     VecRestoreArray(vomega,&omega);
854:     VecDestroy(&vomega);
855:     for (i=0;i<count;i++) {
856:       PetscArraycpy(S+i*(d*ld),tS+i*nconv*d,nconv);
857:       PetscArraycpy(S+i*(d*ld)+ld,tS+i*nconv*d+nconv,nconv);
858:     }
859:     MatDenseRestoreArray(MS,&S);
860:     BVTensorRestoreFactors(ctx->V,NULL,&MS);
861:     BVSetActiveColumns(ctx->V,0,count);
862:     BVTensorCompress(ctx->V,count);
863:     if (sr->sPres->nconv[0] && sr->sPres->nconv[1]) {
864:       divide = PETSC_TRUE;
865:       BVTensorGetFactors(ctx->V,NULL,&MS);
866:       MatDenseGetArray(MS,&S);
867:       PetscArrayzero(tS,nconv*nconv*d);
868:       for (i=0;i<count;i++) {
869:         PetscArraycpy(tS+i*nconv*d,S+i*(d*ld),count);
870:         PetscArraycpy(tS+i*nconv*d+nconv,S+i*(d*ld)+ld,count);
871:       }
872:       MatDenseRestoreArray(MS,&S);
873:       BVTensorRestoreFactors(ctx->V,NULL,&MS);
874:       BVSetActiveColumns(pep->V,0,count);
875:       BVDuplicateResize(pep->V,count,&tV);
876:       BVCopy(pep->V,tV);
877:     }
878:     if (sr->sPres->nconv[0]) {
879:       if (divide) {
880:         BVSetActiveColumns(ctx->V,0,sr->sPres->nconv[0]);
881:         BVTensorCompress(ctx->V,sr->sPres->nconv[0]);
882:       }
883:       for (i=0;i<sr->ndef0;i++) aux[i] = sr->idxDef0[i];
884:       for (i=sr->ndef0;i<sr->sPres->nconv[0];i++) aux[i] = sr->indexEig+i-sr->ndef0;
885:       BVTensorGetFactors(ctx->V,NULL,&MS);
886:       MatDenseGetArray(MS,&S);
887:       for (i=0;i<sr->sPres->nconv[0];i++) {
888:         sr->eigr[aux[i]] = eigr[i];
889:         sr->errest[aux[i]] = errest[i];
890:         BVGetColumn(pep->V,i,&w);
891:         BVInsertVec(sr->V,aux[i],w);
892:         BVRestoreColumn(pep->V,i,&w);
893:         idx = sr->ld*d*aux[i];
894:         PetscArrayzero(sr->S+idx,sr->ld*d);
895:         PetscArraycpy(sr->S+idx,S+i*(ld*d),sr->sPres->nconv[0]);
896:         PetscArraycpy(sr->S+idx+sr->ld,S+i*(ld*d)+ld,sr->sPres->nconv[0]);
897:         PetscFree(sr->qinfo[aux[i]].q);
898:         PetscMalloc1(sr->sPres->nconv[0],&sr->qinfo[aux[i]].q);
899:         PetscArraycpy(sr->qinfo[aux[i]].q,aux,sr->sPres->nconv[0]);
900:         sr->qinfo[aux[i]].nq = sr->sPres->nconv[0];
901:       }
902:       MatDenseRestoreArray(MS,&S);
903:       BVTensorRestoreFactors(ctx->V,NULL,&MS);
904:     }

906:     if (sr->sPres->nconv[1]) {
907:       if (divide) {
908:         BVTensorGetFactors(ctx->V,NULL,&MS);
909:         MatDenseGetArray(MS,&S);
910:         for (i=0;i<sr->sPres->nconv[1];i++) {
911:           PetscArraycpy(S+i*(d*ld),tS+(sr->sPres->nconv[0]+i)*nconv*d,count);
912:           PetscArraycpy(S+i*(d*ld)+ld,tS+(sr->sPres->nconv[0]+i)*nconv*d+nconv,count);
913:         }
914:         MatDenseRestoreArray(MS,&S);
915:         BVTensorRestoreFactors(ctx->V,NULL,&MS);
916:         BVSetActiveColumns(pep->V,0,count);
917:         BVCopy(tV,pep->V);
918:         BVSetActiveColumns(ctx->V,0,sr->sPres->nconv[1]);
919:         BVTensorCompress(ctx->V,sr->sPres->nconv[1]);
920:       }
921:       for (i=0;i<sr->ndef1;i++) aux[i] = sr->idxDef1[i];
922:       for (i=sr->ndef1;i<sr->sPres->nconv[1];i++) aux[i] = sr->indexEig+sr->sPres->nconv[0]-sr->ndef0+i-sr->ndef1;
923:       BVTensorGetFactors(ctx->V,NULL,&MS);
924:       MatDenseGetArray(MS,&S);
925:       for (i=0;i<sr->sPres->nconv[1];i++) {
926:         sr->eigr[aux[i]] = eigr[sr->sPres->nconv[0]+i];
927:         sr->errest[aux[i]] = errest[sr->sPres->nconv[0]+i];
928:         BVGetColumn(pep->V,i,&w);
929:         BVInsertVec(sr->V,aux[i],w);
930:         BVRestoreColumn(pep->V,i,&w);
931:         idx = sr->ld*d*aux[i];
932:         PetscArrayzero(sr->S+idx,sr->ld*d);
933:         PetscArraycpy(sr->S+idx,S+i*(ld*d),sr->sPres->nconv[1]);
934:         PetscArraycpy(sr->S+idx+sr->ld,S+i*(ld*d)+ld,sr->sPres->nconv[1]);
935:         PetscFree(sr->qinfo[aux[i]].q);
936:         PetscMalloc1(sr->sPres->nconv[1],&sr->qinfo[aux[i]].q);
937:         PetscArraycpy(sr->qinfo[aux[i]].q,aux,sr->sPres->nconv[1]);
938:         sr->qinfo[aux[i]].nq = sr->sPres->nconv[1];
939:       }
940:       MatDenseRestoreArray(MS,&S);
941:       BVTensorRestoreFactors(ctx->V,NULL,&MS);
942:     }
943:     sPres->neigs = count-sr->ndef0-sr->ndef1;
944:     sr->indexEig += sPres->neigs;
945:     sPres->nconv[0]-= sr->ndef0;
946:     sPres->nconv[1]-= sr->ndef1;
947:     PetscFree4(eigr,errest,tS,aux);
948:   } else {
949:     sPres->neigs = 0;
950:     sPres->nconv[0]= 0;
951:     sPres->nconv[1]= 0;
952:   }
953:   /* Global ordering array updating */
954:   sortRealEigenvalues(sr->eigr,sr->perm,sr->indexEig,PETSC_FALSE,sr->dir);
955:   /* Check for completion */
956:   sPres->comp[0] = PetscNot(sPres->nconv[0] < sPres->nsch[0]);
957:   sPres->comp[1] = PetscNot(sPres->nconv[1] < sPres->nsch[1]);
958:   if (sPres->nconv[0] > sPres->nsch[0] || sPres->nconv[1] > sPres->nsch[1]) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Mismatch between number of values found and information from inertia");
959:   if (divide) { BVDestroy(&tV); }
960:   return(0);
961: }

963: static PetscErrorCode PEPLookForDeflation(PEP pep)
964: {
965:   PetscReal val;
966:   PetscInt  i,count0=0,count1=0;
967:   PEP_shift sPres;
968:   PetscInt  ini,fin;
969:   PEP_SR    sr;
970:   PEP_STOAR *ctx=(PEP_STOAR*)pep->data;

973:   sr = ctx->sr;
974:   sPres = sr->sPres;

976:   if (sPres->neighb[0]) ini = (sr->dir)*(sPres->neighb[0]->inertia - sr->inertia0);
977:   else ini = 0;
978:   fin = sr->indexEig;
979:   /* Selection of ends for searching new values */
980:   if (!sPres->neighb[0]) sPres->ext[0] = sr->int0;/* First shift */
981:   else sPres->ext[0] = sPres->neighb[0]->value;
982:   if (!sPres->neighb[1]) {
983:     if (sr->hasEnd) sPres->ext[1] = sr->int1;
984:     else sPres->ext[1] = (sr->dir > 0)?PETSC_MAX_REAL:PETSC_MIN_REAL;
985:   } else sPres->ext[1] = sPres->neighb[1]->value;
986:   /* Selection of values between right and left ends */
987:   for (i=ini;i<fin;i++) {
988:     val=PetscRealPart(sr->eigr[sr->perm[i]]);
989:     /* Values to the right of left shift */
990:     if ((sr->dir)*(val - sPres->ext[1]) < 0) {
991:       if ((sr->dir)*(val - sPres->value) < 0) count0++;
992:       else count1++;
993:     } else break;
994:   }
995:   /* The number of values on each side are found */
996:   if (sPres->neighb[0]) {
997:     sPres->nsch[0] = (sr->dir)*(sPres->inertia - sPres->neighb[0]->inertia)-count0;
998:     if (sPres->nsch[0]<0) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Mismatch between number of values found and information from inertia");
999:   } else sPres->nsch[0] = 0;

1001:   if (sPres->neighb[1]) {
1002:     sPres->nsch[1] = (sr->dir)*(sPres->neighb[1]->inertia - sPres->inertia) - count1;
1003:     if (sPres->nsch[1]<0) SETERRQ(PetscObjectComm((PetscObject)pep),1,"Mismatch between number of values found and information from inertia");
1004:   } else sPres->nsch[1] = (sr->dir)*(sr->inertia1 - sPres->inertia);

1006:   /* Completing vector of indexes for deflation */
1007:   for (i=0;i<count0;i++) sr->idxDef0[i] = sr->perm[ini+i];
1008:   sr->ndef0 = count0;
1009:   for (i=0;i<count1;i++) sr->idxDef1[i] = sr->perm[ini+count0+i];
1010:   sr->ndef1 = count1;
1011:   return(0);
1012: }

1014: /*
1015:   Compute a run of Lanczos iterations
1016: */
1017: static PetscErrorCode PEPSTOARrun_QSlice(PEP pep,PetscReal *a,PetscReal *b,PetscReal *omega,PetscInt k,PetscInt *M,PetscBool *breakdown,PetscBool *symmlost,Vec *t_)
1018: {
1020:   PEP_STOAR      *ctx = (PEP_STOAR*)pep->data;
1021:   PetscInt       i,j,m=*M,l,lock;
1022:   PetscInt       lds,d,ld,offq,nqt,ldds;
1023:   Vec            v=t_[0],t=t_[1],q=t_[2];
1024:   PetscReal      norm,sym=0.0,fro=0.0,*f;
1025:   PetscScalar    *y,*S,sigma;
1026:   PetscBLASInt   j_,one=1;
1027:   PetscBool      lindep;
1028:   Mat            MS;

1031:   PetscMalloc1(*M,&y);
1032:   BVGetSizes(pep->V,NULL,NULL,&ld);
1033:   BVTensorGetDegree(ctx->V,&d);
1034:   BVGetActiveColumns(pep->V,&lock,&nqt);
1035:   lds = d*ld;
1036:   offq = ld;
1037:   DSGetLeadingDimension(pep->ds,&ldds);

1039:   *breakdown = PETSC_FALSE; /* ----- */
1040:   STGetShift(pep->st,&sigma);
1041:   DSGetDimensions(pep->ds,NULL,NULL,&l,NULL,NULL);
1042:   BVSetActiveColumns(ctx->V,0,m);
1043:   BVSetActiveColumns(pep->V,0,nqt);
1044:   for (j=k;j<m;j++) {
1045:     /* apply operator */
1046:     BVTensorGetFactors(ctx->V,NULL,&MS);
1047:     MatDenseGetArray(MS,&S);
1048:     BVGetColumn(pep->V,nqt,&t);
1049:     BVMultVec(pep->V,1.0,0.0,v,S+j*lds);
1050:     MatMult(pep->A[1],v,q);
1051:     MatMult(pep->A[2],v,t);
1052:     VecAXPY(q,sigma*pep->sfactor,t);
1053:     VecScale(q,pep->sfactor);
1054:     BVMultVec(pep->V,1.0,0.0,v,S+offq+j*lds);
1055:     MatMult(pep->A[2],v,t);
1056:     VecAXPY(q,pep->sfactor*pep->sfactor,t);
1057:     STMatSolve(pep->st,q,t);
1058:     VecScale(t,-1.0);
1059:     BVRestoreColumn(pep->V,nqt,&t);

1061:     /* orthogonalize */
1062:     BVOrthogonalizeColumn(pep->V,nqt,S+(j+1)*lds,&norm,&lindep);
1063:     if (!lindep) {
1064:       *(S+(j+1)*lds+nqt) = norm;
1065:       BVScaleColumn(pep->V,nqt,1.0/norm);
1066:       nqt++;
1067:     }
1068:     for (i=0;i<nqt;i++) *(S+(j+1)*lds+offq+i) = *(S+j*lds+i)+sigma*(*(S+(j+1)*lds+i));
1069:     BVSetActiveColumns(pep->V,0,nqt);
1070:     MatDenseRestoreArray(MS,&S);
1071:     BVTensorRestoreFactors(ctx->V,NULL,&MS);

1073:     /* level-2 orthogonalization */
1074:     BVOrthogonalizeColumn(ctx->V,j+1,y,&norm,&lindep);
1075:     a[j] = PetscRealPart(y[j]);
1076:     omega[j+1] = (norm > 0)?1.0:-1.0;
1077:     BVScaleColumn(ctx->V,j+1,1.0/norm);
1078:     b[j] = PetscAbsReal(norm);

1080:     /* check symmetry */
1081:     DSGetArrayReal(pep->ds,DS_MAT_T,&f);
1082:     if (j==k) {
1083:       for (i=l;i<j-1;i++) y[i] = PetscAbsScalar(y[i])-PetscAbsReal(f[2*ldds+i]);
1084:       for (i=0;i<l;i++) y[i] = 0.0;
1085:     }
1086:     DSRestoreArrayReal(pep->ds,DS_MAT_T,&f);
1087:     if (j>0) y[j-1] = PetscAbsScalar(y[j-1])-PetscAbsReal(b[j-1]);
1088:     PetscBLASIntCast(j,&j_);
1089:     sym = SlepcAbs(BLASnrm2_(&j_,y,&one),sym);
1090:     fro = SlepcAbs(fro,SlepcAbs(a[j],b[j]));
1091:     if (j>0) fro = SlepcAbs(fro,b[j-1]);
1092:     if (sym/fro>PetscMax(PETSC_SQRT_MACHINE_EPSILON,10*pep->tol)) {
1093:       *symmlost = PETSC_TRUE;
1094:       *M=j;
1095:       break;
1096:     }
1097:   }
1098:   BVSetActiveColumns(pep->V,lock,nqt);
1099:   BVSetActiveColumns(ctx->V,0,*M);
1100:   PetscFree(y);
1101:   return(0);
1102: }

1104: static PetscErrorCode PEPSTOAR_QSlice(PEP pep,Mat B)
1105: {
1107:   PEP_STOAR      *ctx = (PEP_STOAR*)pep->data;
1108:   PetscInt       j,k,l,nv=0,ld,ldds,t,nq=0,idx;
1109:   PetscInt       nconv=0,deg=pep->nmat-1,count0=0,count1=0;
1110:   PetscScalar    *Q,*om,sigma,*back,*S,*pQ;
1111:   PetscReal      beta,norm=1.0,*omega,*a,*b,*r,eta,lambda;
1112:   PetscBool      breakdown,symmlost=PETSC_FALSE,sinv,falselock=PETSC_TRUE;
1113:   Mat            MS,MQ;
1114:   Vec            v,vomega;
1115:   PEP_SR         sr;
1116:   BVOrthogType   otype;
1117:   BVOrthogBlockType obtype;

1120:   /* Resize if needed for deflating vectors  */
1121:   sr = ctx->sr;
1122:   sigma = sr->sPres->value;
1123:   k = sr->ndef0+sr->ndef1;
1124:   pep->ncv = ctx->ncv+k;
1125:   pep->nev = ctx->nev+k;
1126:   PEPAllocateSolution(pep,3);
1127:   BVDestroy(&ctx->V);
1128:   BVCreateTensor(pep->V,pep->nmat-1,&ctx->V);
1129:   BVGetOrthogonalization(pep->V,&otype,NULL,&eta,&obtype);
1130:   BVSetOrthogonalization(ctx->V,otype,BV_ORTHOG_REFINE_ALWAYS,eta,obtype);
1131:   DSAllocate(pep->ds,pep->ncv+2);
1132:   PetscMalloc1(pep->ncv,&back);
1133:   DSGetLeadingDimension(pep->ds,&ldds);
1134:   BVSetMatrix(ctx->V,B,PETSC_TRUE);
1135:   if (ctx->lock) {
1136:     /* undocumented option to use a cheaper locking instead of the true locking */
1137:     PetscOptionsGetBool(NULL,NULL,"-pep_stoar_falselocking",&falselock,NULL);
1138:   } else SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"A locking variant is needed for spectrum slicing");
1139:   PetscObjectTypeCompare((PetscObject)pep->st,STSINVERT,&sinv);
1140:   RGPushScale(pep->rg,sinv?pep->sfactor:1.0/pep->sfactor);
1141:   STScaleShift(pep->st,sinv?pep->sfactor:1.0/pep->sfactor);

1143:   /* Get the starting Arnoldi vector */
1144:   BVSetActiveColumns(pep->V,0,1);
1145:   BVTensorBuildFirstColumn(ctx->V,pep->nini);
1146:   BVSetActiveColumns(ctx->V,0,1);
1147:   if (k) {
1148:     /* Insert deflated vectors */
1149:     BVSetActiveColumns(pep->V,0,0);
1150:     idx = sr->ndef0?sr->idxDef0[0]:sr->idxDef1[0];
1151:     for (j=0;j<k;j++) {
1152:       BVGetColumn(pep->V,j,&v);
1153:       BVCopyVec(sr->V,sr->qinfo[idx].q[j],v);
1154:       BVRestoreColumn(pep->V,j,&v);
1155:     }
1156:     /* Update innerproduct matrix */
1157:     BVSetActiveColumns(ctx->V,0,0);
1158:     BVTensorGetFactors(ctx->V,NULL,&MS);
1159:     BVSetActiveColumns(pep->V,0,k);
1160:     BVTensorRestoreFactors(ctx->V,NULL,&MS);

1162:     BVGetSizes(pep->V,NULL,NULL,&ld);
1163:     BVTensorGetFactors(ctx->V,NULL,&MS);
1164:     MatDenseGetArray(MS,&S);
1165:     for (j=0;j<sr->ndef0;j++) {
1166:       PetscArrayzero(S+j*ld*deg,ld*deg);
1167:       PetscArraycpy(S+j*ld*deg,sr->S+sr->idxDef0[j]*sr->ld*deg,k);
1168:       PetscArraycpy(S+j*ld*deg+ld,sr->S+sr->idxDef0[j]*sr->ld*deg+sr->ld,k);
1169:       pep->eigr[j] = sr->eigr[sr->idxDef0[j]];
1170:       pep->errest[j] = sr->errest[sr->idxDef0[j]];
1171:     }
1172:     for (j=0;j<sr->ndef1;j++) {
1173:       PetscArrayzero(S+(j+sr->ndef0)*ld*deg,ld*deg);
1174:       PetscArraycpy(S+(j+sr->ndef0)*ld*deg,sr->S+sr->idxDef1[j]*sr->ld*deg,k);
1175:       PetscArraycpy(S+(j+sr->ndef0)*ld*deg+ld,sr->S+sr->idxDef1[j]*sr->ld*deg+sr->ld,k);
1176:       pep->eigr[j+sr->ndef0] = sr->eigr[sr->idxDef1[j]];
1177:       pep->errest[j+sr->ndef0] = sr->errest[sr->idxDef1[j]];
1178:     }
1179:     MatDenseRestoreArray(MS,&S);
1180:     BVTensorRestoreFactors(ctx->V,NULL,&MS);
1181:     BVSetActiveColumns(ctx->V,0,k+1);
1182:     VecCreateSeq(PETSC_COMM_SELF,k+1,&vomega);
1183:     VecGetArray(vomega,&om);
1184:     for (j=0;j<k;j++) {
1185:       BVOrthogonalizeColumn(ctx->V,j,NULL,&norm,NULL);
1186:       BVScaleColumn(ctx->V,j,1/norm);
1187:       om[j] = (norm>=0.0)?1.0:-1.0;
1188:     }
1189:     BVTensorGetFactors(ctx->V,NULL,&MS);
1190:     MatDenseGetArray(MS,&S);
1191:     for (j=0;j<deg;j++) {
1192:       BVSetRandomColumn(pep->V,k+j);
1193:       BVOrthogonalizeColumn(pep->V,k+j,S+k*ld*deg+j*ld,&norm,NULL);
1194:       BVScaleColumn(pep->V,k+j,1.0/norm);
1195:       S[k*ld*deg+j*ld+k+j] = norm;
1196:     }
1197:     MatDenseRestoreArray(MS,&S);
1198:     BVSetActiveColumns(pep->V,0,k+deg);
1199:     BVTensorRestoreFactors(ctx->V,NULL,&MS);
1200:     BVOrthogonalizeColumn(ctx->V,k,NULL,&norm,NULL);
1201:     BVScaleColumn(ctx->V,k,1.0/norm);
1202:     om[k] = (norm>=0.0)?1.0:-1.0;
1203:     VecRestoreArray(vomega,&om);
1204:     BVSetSignature(ctx->V,vomega);
1205:     DSGetArrayReal(pep->ds,DS_MAT_T,&a);
1206:     VecGetArray(vomega,&om);
1207:     for (j=0;j<k;j++) a[j] = PetscRealPart(om[j]/(pep->eigr[j]-sigma));
1208:     VecRestoreArray(vomega,&om);
1209:     VecDestroy(&vomega);
1210:     DSRestoreArrayReal(pep->ds,DS_MAT_T,&a);
1211:     DSGetArray(pep->ds,DS_MAT_Q,&pQ);
1212:     PetscArrayzero(pQ,ldds*k);
1213:     for (j=0;j<k;j++) pQ[j+j*ldds] = 1.0;
1214:     DSRestoreArray(pep->ds,DS_MAT_Q,&pQ);
1215:   }
1216:   BVSetActiveColumns(ctx->V,0,k+1);
1217:   DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1218:   VecCreateSeq(PETSC_COMM_SELF,k+1,&vomega);
1219:   BVGetSignature(ctx->V,vomega);
1220:   VecGetArray(vomega,&om);
1221:   for (j=0;j<k+1;j++) omega[j] = PetscRealPart(om[j]);
1222:   VecRestoreArray(vomega,&om);
1223:   DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1224:   VecDestroy(&vomega);

1226:   PetscInfo7(pep,"Start STOAR: sigma=%g in [%g,%g], for deflation: left=%D right=%D, searching: left=%D right=%D\n",(double)sr->sPres->value,(double)(sr->sPres->neighb[0]?sr->sPres->neighb[0]->value:sr->int0),(double)(sr->sPres->neighb[1]?sr->sPres->neighb[1]->value:sr->int1),sr->ndef0,sr->ndef1,sr->sPres->nsch[0],sr->sPres->nsch[1]);

1228:   /* Restart loop */
1229:   l = 0;
1230:   pep->nconv = k;
1231:   while (pep->reason == PEP_CONVERGED_ITERATING) {
1232:     pep->its++;
1233:     DSGetArrayReal(pep->ds,DS_MAT_T,&a);
1234:     b = a+ldds;
1235:     DSGetArrayReal(pep->ds,DS_MAT_D,&omega);

1237:     /* Compute an nv-step Lanczos factorization */
1238:     nv = PetscMin(pep->nconv+pep->mpd,pep->ncv);
1239:     PEPSTOARrun_QSlice(pep,a,b,omega,pep->nconv+l,&nv,&breakdown,&symmlost,pep->work);
1240:     beta = b[nv-1];
1241:     if (symmlost && nv==pep->nconv+l) {
1242:       pep->reason = PEP_DIVERGED_SYMMETRY_LOST;
1243:       pep->nconv = nconv;
1244:       PetscInfo2(pep,"Symmetry lost in STOAR sigma=%g nconv=%D\n",(double)sr->sPres->value,nconv);
1245:       if (falselock || !ctx->lock) {
1246:         BVSetActiveColumns(ctx->V,0,pep->nconv);
1247:         BVTensorCompress(ctx->V,0);
1248:       }
1249:       break;
1250:     }
1251:     DSRestoreArrayReal(pep->ds,DS_MAT_T,&a);
1252:     DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1253:     DSSetDimensions(pep->ds,nv,0,pep->nconv,pep->nconv+l);
1254:     if (l==0) {
1255:       DSSetState(pep->ds,DS_STATE_INTERMEDIATE);
1256:     } else {
1257:       DSSetState(pep->ds,DS_STATE_RAW);
1258:     }

1260:     /* Solve projected problem */
1261:     DSSolve(pep->ds,pep->eigr,pep->eigi);
1262:     DSSort(pep->ds,pep->eigr,pep->eigi,NULL,NULL,NULL);
1263:     DSSynchronize(pep->ds,pep->eigr,pep->eigi);

1265:     /* Check convergence */
1266:     /* PEPSTOARpreKConvergence(pep,nv,&norm,pep->work);*/
1267:     norm = 1.0;
1268:     DSGetDimensions(pep->ds,NULL,NULL,NULL,NULL,&t);
1269:     PEPKrylovConvergence(pep,PETSC_FALSE,pep->nconv,t-pep->nconv,PetscAbsReal(beta)*norm,&k);
1270:     (*pep->stopping)(pep,pep->its,pep->max_it,k,pep->nev,&pep->reason,pep->stoppingctx);
1271:     for (j=0;j<k;j++) back[j] = pep->eigr[j];
1272:     STBackTransform(pep->st,k,back,pep->eigi);
1273:     count0=count1=0;
1274:     for (j=0;j<k;j++) {
1275:       lambda = PetscRealPart(back[j]);
1276:       if (((sr->dir)*(sr->sPres->value - lambda) > 0) && ((sr->dir)*(lambda - sr->sPres->ext[0]) > 0)) count0++;
1277:       if (((sr->dir)*(lambda - sr->sPres->value) > 0) && ((sr->dir)*(sr->sPres->ext[1] - lambda) > 0)) count1++;
1278:     }
1279:     if ((count0-sr->ndef0 >= sr->sPres->nsch[0]) && (count1-sr->ndef1 >= sr->sPres->nsch[1])) pep->reason = PEP_CONVERGED_TOL;
1280:     /* Update l */
1281:     if (pep->reason != PEP_CONVERGED_ITERATING || breakdown) l = 0;
1282:     else {
1283:       l = PetscMax(1,(PetscInt)((nv-k)/2));
1284:       l = PetscMin(l,t);
1285:       if (!breakdown) {
1286:         DSGetArrayReal(pep->ds,DS_MAT_T,&a);
1287:         if (*(a+ldds+k+l-1)!=0) {
1288:           if (k+l<nv-1) l = l+1;
1289:           else l = l-1;
1290:         }
1291:         /* Prepare the Rayleigh quotient for restart */
1292:         DSGetArray(pep->ds,DS_MAT_Q,&Q);
1293:         DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1294:         r = a + 2*ldds;
1295:         for (j=k;j<k+l;j++) {
1296:           r[j] = PetscRealPart(Q[nv-1+j*ldds]*beta);
1297:         }
1298:         b = a+ldds;
1299:         b[k+l-1] = r[k+l-1];
1300:         omega[k+l] = omega[nv];
1301:         DSRestoreArray(pep->ds,DS_MAT_Q,&Q);
1302:         DSRestoreArrayReal(pep->ds,DS_MAT_T,&a);
1303:         DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1304:       }
1305:     }
1306:     nconv = k;
1307:     if (!ctx->lock && pep->reason == PEP_CONVERGED_ITERATING && !breakdown) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */

1309:     /* Update S */
1310:     DSGetMat(pep->ds,DS_MAT_Q,&MQ);
1311:     BVMultInPlace(ctx->V,MQ,pep->nconv,k+l);
1312:     MatDestroy(&MQ);

1314:     /* Copy last column of S */
1315:     BVCopyColumn(ctx->V,nv,k+l);
1316:     DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1317:     VecCreateSeq(PETSC_COMM_SELF,k+l,&vomega);
1318:     VecGetArray(vomega,&om);
1319:     for (j=0;j<k+l;j++) om[j] = omega[j];
1320:     VecRestoreArray(vomega,&om);
1321:     BVSetActiveColumns(ctx->V,0,k+l);
1322:     BVSetSignature(ctx->V,vomega);
1323:     VecDestroy(&vomega);
1324:     DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);

1326:     if (breakdown && pep->reason == PEP_CONVERGED_ITERATING) {
1327:       /* stop if breakdown */
1328:       PetscInfo2(pep,"Breakdown TOAR method (it=%D norm=%g)\n",pep->its,(double)beta);
1329:       pep->reason = PEP_DIVERGED_BREAKDOWN;
1330:     }
1331:     if (pep->reason != PEP_CONVERGED_ITERATING) l--;
1332:     BVGetActiveColumns(pep->V,NULL,&nq);
1333:     if (k+l+deg<=nq) {
1334:       BVSetActiveColumns(ctx->V,pep->nconv,k+l+1);
1335:       if (!falselock && ctx->lock) {
1336:         BVTensorCompress(ctx->V,k-pep->nconv);
1337:       } else {
1338:         BVTensorCompress(ctx->V,0);
1339:       }
1340:     }
1341:     pep->nconv = k;
1342:     PEPMonitor(pep,pep->its,nconv,pep->eigr,pep->eigi,pep->errest,nv);
1343:   }
1344:   sr->itsKs += pep->its;
1345:   if (pep->nconv>0) {
1346:     BVSetActiveColumns(ctx->V,0,pep->nconv);
1347:     BVGetActiveColumns(pep->V,NULL,&nq);
1348:     BVSetActiveColumns(pep->V,0,nq);
1349:     if (nq>pep->nconv) {
1350:       BVTensorCompress(ctx->V,pep->nconv);
1351:       BVSetActiveColumns(pep->V,0,pep->nconv);
1352:     }
1353:     for (j=0;j<pep->nconv;j++) {
1354:       pep->eigr[j] *= pep->sfactor;
1355:       pep->eigi[j] *= pep->sfactor;
1356:     }
1357:   }
1358:   PetscInfo4(pep,"Finished STOAR: nconv=%D (deflated=%D, left=%D, right=%D)\n",pep->nconv,sr->ndef0+sr->ndef1,count0-sr->ndef0,count1-sr->ndef1);
1359:   STScaleShift(pep->st,sinv?1.0/pep->sfactor:pep->sfactor);
1360:   RGPopScale(pep->rg);

1362:   if (pep->reason == PEP_DIVERGED_SYMMETRY_LOST && nconv<sr->ndef0+sr->ndef1) SETERRQ1(PetscObjectComm((PetscObject)pep),1,"Symmetry lost at sigma=%g",(double)sr->sPres->value);
1363:   if (pep->reason == PEP_DIVERGED_SYMMETRY_LOST && nconv==sr->ndef0+sr->ndef1) {
1364:     if (++sr->symmlost>10) SETERRQ1(PetscObjectComm((PetscObject)pep),1,"Symmetry lost at sigma=%g",(double)sr->sPres->value);
1365:   } else sr->symmlost = 0;

1367:   /* truncate Schur decomposition and change the state to raw so that
1368:      DSVectors() computes eigenvectors from scratch */
1369:   DSSetDimensions(pep->ds,pep->nconv,0,0,0);
1370:   DSSetState(pep->ds,DS_STATE_RAW);
1371:   PetscFree(back);
1372:   return(0);
1373: }

1375: #define SWAP(a,b,t) {t=a;a=b;b=t;}

1377: static PetscErrorCode PEPQSliceGetInertias(PEP pep,PetscInt *n,PetscReal **shifts,PetscInt **inertias)
1378: {
1379:   PetscErrorCode  ierr;
1380:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
1381:   PEP_SR          sr=ctx->sr;
1382:   PetscInt        i=0,j,tmpi;
1383:   PetscReal       v,tmpr;
1384:   PEP_shift       s;

1387:   if (!pep->state) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONGSTATE,"Must call PEPSetUp() first");
1388:   if (!sr) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONGSTATE,"Only available in interval computations, see PEPSetInterval()");
1389:   if (!sr->s0) {  /* PEPSolve not called yet */
1390:     *n = 2;
1391:   } else {
1392:     *n = 1;
1393:     s = sr->s0;
1394:     while (s) {
1395:       (*n)++;
1396:       s = s->neighb[1];
1397:     }
1398:   }
1399:   PetscMalloc1(*n,shifts);
1400:   PetscMalloc1(*n,inertias);
1401:   if (!sr->s0) {  /* PEPSolve not called yet */
1402:     (*shifts)[0]   = sr->int0;
1403:     (*shifts)[1]   = sr->int1;
1404:     (*inertias)[0] = sr->inertia0;
1405:     (*inertias)[1] = sr->inertia1;
1406:   } else {
1407:     s = sr->s0;
1408:     while (s) {
1409:       (*shifts)[i]     = s->value;
1410:       (*inertias)[i++] = s->inertia;
1411:       s = s->neighb[1];
1412:     }
1413:     (*shifts)[i]   = sr->int1;
1414:     (*inertias)[i] = sr->inertia1;
1415:   }
1416:   /* remove possible duplicate in last position */
1417:   if ((*shifts)[(*n)-1]==(*shifts)[(*n)-2]) (*n)--;
1418:   /* sort result */
1419:   for (i=0;i<*n;i++) {
1420:     v = (*shifts)[i];
1421:     for (j=i+1;j<*n;j++) {
1422:       if (v > (*shifts)[j]) {
1423:         SWAP((*shifts)[i],(*shifts)[j],tmpr);
1424:         SWAP((*inertias)[i],(*inertias)[j],tmpi);
1425:         v = (*shifts)[i];
1426:       }
1427:     }
1428:   }
1429:   return(0);
1430: }

1432: PetscErrorCode PEPSolve_STOAR_QSlice(PEP pep)
1433: {
1435:   PetscInt       i,j,ti,deg=pep->nmat-1;
1436:   PetscReal      newS;
1437:   PEP_STOAR      *ctx=(PEP_STOAR*)pep->data;
1438:   PEP_SR         sr=ctx->sr;
1439:   Mat            S,B;
1440:   PetscScalar    *pS;

1443:   PetscCitationsRegister(citation,&cited);

1445:   /* Only with eigenvalues present in the interval ...*/
1446:   if (sr->numEigs==0) {
1447:     pep->reason = PEP_CONVERGED_TOL;
1448:     return(0);
1449:   }

1451:   /* Inner product matrix */
1452:   PEPSTOARSetUpInnerMatrix(pep,&B);

1454:   /* Array of pending shifts */
1455:   sr->maxPend = 100; /* Initial size */
1456:   sr->nPend = 0;
1457:   PetscMalloc1(sr->maxPend,&sr->pending);
1458:   PetscLogObjectMemory((PetscObject)pep,(sr->maxPend)*sizeof(PEP_shift));
1459:   PEPCreateShift(pep,sr->int0,NULL,NULL);
1460:   /* extract first shift */
1461:   sr->sPrev = NULL;
1462:   sr->sPres = sr->pending[--sr->nPend];
1463:   sr->sPres->inertia = sr->inertia0;
1464:   pep->target = sr->sPres->value;
1465:   sr->s0 = sr->sPres;
1466:   sr->indexEig = 0;

1468:   /* Memory reservation for auxiliary variables */
1469:   PetscLogObjectMemory((PetscObject)pep,(sr->numEigs+2*pep->ncv)*sizeof(PetscScalar));
1470:   for (i=0;i<sr->numEigs;i++) {
1471:     sr->eigr[i]   = 0.0;
1472:     sr->eigi[i]   = 0.0;
1473:     sr->errest[i] = 0.0;
1474:     sr->perm[i]   = i;
1475:   }
1476:   /* Vectors for deflation */
1477:   PetscMalloc2(sr->numEigs,&sr->idxDef0,sr->numEigs,&sr->idxDef1);
1478:   PetscLogObjectMemory((PetscObject)pep,sr->numEigs*sizeof(PetscInt));
1479:   sr->indexEig = 0;
1480:   while (sr->sPres) {
1481:     /* Search for deflation */
1482:     PEPLookForDeflation(pep);
1483:     /* KrylovSchur */
1484:     PEPSTOAR_QSlice(pep,B);

1486:     PEPStoreEigenpairs(pep);
1487:     /* Select new shift */
1488:     if (!sr->sPres->comp[1]) {
1489:       PEPGetNewShiftValue(pep,1,&newS);
1490:       PEPCreateShift(pep,newS,sr->sPres,sr->sPres->neighb[1]);
1491:     }
1492:     if (!sr->sPres->comp[0]) {
1493:       /* Completing earlier interval */
1494:       PEPGetNewShiftValue(pep,0,&newS);
1495:       PEPCreateShift(pep,newS,sr->sPres->neighb[0],sr->sPres);
1496:     }
1497:     /* Preparing for a new search of values */
1498:     PEPExtractShift(pep);
1499:   }

1501:   /* Updating pep values prior to exit */
1502:   PetscFree2(sr->idxDef0,sr->idxDef1);
1503:   PetscFree(sr->pending);
1504:   pep->nconv  = sr->indexEig;
1505:   pep->reason = PEP_CONVERGED_TOL;
1506:   pep->its    = sr->itsKs;
1507:   pep->nev    = sr->indexEig;
1508:   MatCreateSeqDense(PETSC_COMM_SELF,pep->nconv,pep->nconv,NULL,&S);
1509:   MatDenseGetArray(S,&pS);
1510:   for (i=0;i<pep->nconv;i++) {
1511:     for (j=0;j<sr->qinfo[i].nq;j++) pS[i*pep->nconv+sr->qinfo[i].q[j]] = *(sr->S+i*sr->ld*deg+j);
1512:   }
1513:   MatDenseRestoreArray(S,&pS);
1514:   BVSetActiveColumns(sr->V,0,pep->nconv);
1515:   BVMultInPlace(sr->V,S,0,pep->nconv);
1516:   MatDestroy(&S);
1517:   BVDestroy(&pep->V);
1518:   pep->V = sr->V;
1519:   PetscFree4(pep->eigr,pep->eigi,pep->errest,pep->perm);
1520:   pep->eigr   = sr->eigr;
1521:   pep->eigi   = sr->eigi;
1522:   pep->perm   = sr->perm;
1523:   pep->errest = sr->errest;
1524:   if (sr->dir<0) {
1525:     for (i=0;i<pep->nconv/2;i++) {
1526:       ti = sr->perm[i]; sr->perm[i] = sr->perm[pep->nconv-1-i]; sr->perm[pep->nconv-1-i] = ti;
1527:     }
1528:   }
1529:   PetscFree(ctx->inertias);
1530:   PetscFree(ctx->shifts);
1531:   MatDestroy(&B);
1532:   PEPQSliceGetInertias(pep,&ctx->nshifts,&ctx->shifts,&ctx->inertias);
1533:   return(0);
1534: }