Actual source code: qslice.c
slepc-3.16.2 2022-02-01
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2021, 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,dot;
175: PetscRandom rand;
176: PetscInt nconv;
177: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
178: PEP_SR sr=ctx->sr;
181: if (shift >= PETSC_MAX_REAL) { /* Right-open interval */
182: *inertia = 0;
183: } else if (shift <= PETSC_MIN_REAL) {
184: *inertia = 0;
185: if (zeros) *zeros = 0;
186: } else {
187: /* If the shift is zero, perturb it to a very small positive value.
188: The goal is that the nonzero pattern is the same in all cases and reuse
189: the symbolic factorizations */
190: nzshift = (shift==0.0)? 10.0/PETSC_MAX_REAL: shift;
191: PEPQSliceMatGetInertia(pep,nzshift,inertia,zeros);
192: STSetShift(pep->st,nzshift);
193: }
194: if (!correction) {
195: if (shift >= PETSC_MAX_REAL) *inertia = 2*pep->n;
196: else if (shift>PETSC_MIN_REAL) {
197: STGetKSP(pep->st,&ksp);
198: KSPGetOperators(ksp,&P,NULL);
199: if (*inertia!=pep->n && !sr->v[0]) {
200: MatCreateVecs(P,&sr->v[0],NULL);
201: VecDuplicate(sr->v[0],&sr->v[1]);
202: VecDuplicate(sr->v[0],&sr->v[2]);
203: BVGetRandomContext(pep->V,&rand);
204: VecSetRandom(sr->v[0],rand);
205: }
206: if (*inertia<pep->n && *inertia>0) {
207: if (!sr->eps) {
208: EPSCreate(PetscObjectComm((PetscObject)pep),&sr->eps);
209: EPSSetProblemType(sr->eps,EPS_HEP);
210: EPSSetWhichEigenpairs(sr->eps,EPS_LARGEST_REAL);
211: }
212: EPSSetConvergenceTestFunction(sr->eps,ConvergedPositive,NULL,NULL);
213: EPSSetOperators(sr->eps,P,NULL);
214: EPSSolve(sr->eps);
215: EPSGetConverged(sr->eps,&nconv);
216: if (!nconv) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",nzshift);
217: EPSGetEigenpair(sr->eps,0,NULL,NULL,sr->v[0],sr->v[1]);
218: }
219: if (*inertia!=pep->n) {
220: MatMult(pep->A[1],sr->v[0],sr->v[1]);
221: MatMult(pep->A[2],sr->v[0],sr->v[2]);
222: VecAXPY(sr->v[1],2*nzshift,sr->v[2]);
223: VecDotRealPart(sr->v[1],sr->v[0],&dot);
224: if (dot>0.0) *inertia = 2*pep->n-*inertia;
225: }
226: }
227: } else if (correction<0) *inertia = 2*pep->n-*inertia;
228: return(0);
229: }
231: /*
232: Check eigenvalue type - used only in non-hyperbolic problems.
233: All computed eigenvalues must have the same definite type (positive or negative).
234: If ini=TRUE the type is available in omega, otherwise we compute an eigenvalue
235: closest to shift and determine its type.
236: */
237: static PetscErrorCode PEPQSliceCheckEigenvalueType(PEP pep,PetscReal shift,PetscReal omega,PetscBool ini)
238: {
240: PEP pep2;
241: ST st;
242: PetscInt nconv;
243: PetscScalar lambda;
244: PetscReal 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: VecDotRealPart(pep2->work[1],pep2->work[0],&dot);
277: PetscInfo2(pep,"lambda=%g, %s type\n",(double)PetscRealPart(lambda),(dot>0.0)?"positive":"negative");
278: if (!sr->type) sr->type = (dot>0.0)?1:-1;
279: else {
280: if (sr->type*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;
295: MatMult(pep->A[0],u,w);
296: VecDotRealPart(w,u,&cp);
297: MatMult(pep->A[1],u,w);
298: VecDotRealPart(w,u,&bp);
299: MatMult(pep->A[2],u,w);
300: VecDotRealPart(w,u,&ap);
301: dis = bp*bp-4*ap*cp;
302: if (dis>=0.0 && smas) {
303: if (ap>0) *smas = (-bp+PetscSqrtReal(dis))/(2*ap);
304: else if (ap<0) *smas = (-bp-PetscSqrtReal(dis))/(2*ap);
305: else {
306: if (bp >0) *smas = -cp/bp;
307: else *smas = PETSC_MAX_REAL;
308: }
309: }
310: if (dis>=0.0 && smenos) {
311: if (ap>0) *smenos = (-bp-PetscSqrtReal(dis))/(2*ap);
312: else if (ap<0) *smenos = (-bp+PetscSqrtReal(dis))/(2*ap);
313: else {
314: if (bp<0) *smenos = -cp/bp;
315: else *smenos = PETSC_MAX_REAL;
316: }
317: }
318: if (d) *d = dis;
319: return(0);
320: }
322: PETSC_STATIC_INLINE PetscErrorCode PEPQSliceEvaluateQEP(PEP pep,PetscScalar x,Mat M,MatStructure str)
323: {
327: MatCopy(pep->A[0],M,SAME_NONZERO_PATTERN);
328: MatAXPY(M,x,pep->A[1],str);
329: MatAXPY(M,x*x,pep->A[2],str);
330: return(0);
331: }
333: /*@
334: PEPCheckDefiniteQEP - Determines if a symmetric/Hermitian quadratic eigenvalue problem
335: is definite or not.
337: Logically Collective on pep
339: Input Parameter:
340: . pep - eigensolver context
342: Output Parameters:
343: + xi - first computed parameter
344: . mu - second computed parameter
345: . definite - flag indicating that the problem is definite
346: - hyperbolic - flag indicating that the problem is hyperbolic
348: Notes:
349: This function is intended for quadratic eigenvalue problems, Q(lambda)=A*lambda^2+B*lambda+C,
350: with symmetric (or Hermitian) coefficient matrices A,B,C.
352: On output, the flag 'definite' may have the values -1 (meaning that the QEP is not
353: definite), 1 (if the problem is definite), or 0 if the algorithm was not able to
354: determine whether the problem is definite or not.
356: If definite=1, the output flag 'hyperbolic' informs in a similar way about whether the
357: problem is hyperbolic or not.
359: If definite=1, the computed values xi and mu satisfy Q(xi)<0 and Q(mu)>0, as
360: obtained via the method proposed in [Niendorf and Voss, LAA 2010]. Furthermore, if
361: hyperbolic=1 then only xi is computed.
363: Level: advanced
364: @*/
365: PetscErrorCode PEPCheckDefiniteQEP(PEP pep,PetscReal *xi,PetscReal *mu,PetscInt *definite,PetscInt *hyperbolic)
366: {
368: PetscRandom rand;
369: Vec u,w;
370: PetscReal d=0.0,s=0.0,sp,mut=0.0,omg=0.0,omgp;
371: PetscInt k,its=10,hyp=0,check=0,nconv,inertia,n;
372: Mat M=NULL;
373: MatStructure str;
374: EPS eps;
375: PetscBool transform,ptypehyp;
378: 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");
379: ptypehyp = (pep->problem_type==PEP_HYPERBOLIC)? PETSC_TRUE: PETSC_FALSE;
380: if (!pep->st) { PEPGetST(pep,&pep->st); }
381: PEPSetDefaultST(pep);
382: STSetMatrices(pep->st,pep->nmat,pep->A);
383: MatGetSize(pep->A[0],&n,NULL);
384: STGetTransform(pep->st,&transform);
385: STSetTransform(pep->st,PETSC_FALSE);
386: STSetUp(pep->st);
387: MatCreateVecs(pep->A[0],&u,&w);
388: PEPGetBV(pep,&pep->V);
389: BVGetRandomContext(pep->V,&rand);
390: VecSetRandom(u,rand);
391: VecNormalize(u,NULL);
392: PEPQSliceDiscriminant(pep,u,w,&d,&s,NULL);
393: if (d<0.0) check = -1;
394: if (!check) {
395: EPSCreate(PetscObjectComm((PetscObject)pep),&eps);
396: EPSSetProblemType(eps,EPS_HEP);
397: EPSSetWhichEigenpairs(eps,EPS_LARGEST_REAL);
398: EPSSetTolerances(eps,PetscSqrtReal(PETSC_SQRT_MACHINE_EPSILON),PETSC_DECIDE);
399: MatDuplicate(pep->A[0],MAT_DO_NOT_COPY_VALUES,&M);
400: STGetMatStructure(pep->st,&str);
401: }
402: for (k=0;k<its&&!check;k++) {
403: PEPQSliceEvaluateQEP(pep,s,M,str);
404: EPSSetOperators(eps,M,NULL);
405: EPSSolve(eps);
406: EPSGetConverged(eps,&nconv);
407: if (!nconv) break;
408: EPSGetEigenpair(eps,0,NULL,NULL,u,w);
409: sp = s;
410: PEPQSliceDiscriminant(pep,u,w,&d,&s,&omg);
411: if (d<0.0) {check = -1; break;}
412: if (PetscAbsReal((s-sp)/s)<100*PETSC_MACHINE_EPSILON) break;
413: if (s>sp) {hyp = -1;}
414: mut = 2*s-sp;
415: PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
416: if (inertia == n) {check = 1; break;}
417: }
418: for (;k<its&&!check;k++) {
419: mut = (s-omg)/2;
420: PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
421: if (inertia == n) {check = 1; break;}
422: if (PetscAbsReal((s-omg)/omg)<100*PETSC_MACHINE_EPSILON) break;
423: PEPQSliceEvaluateQEP(pep,omg,M,str);
424: EPSSetOperators(eps,M,NULL);
425: EPSSolve(eps);
426: EPSGetConverged(eps,&nconv);
427: if (!nconv) break;
428: EPSGetEigenpair(eps,0,NULL,NULL,u,w);
429: omgp = omg;
430: PEPQSliceDiscriminant(pep,u,w,&d,NULL,&omg);
431: if (d<0.0) {check = -1; break;}
432: if (omg<omgp) hyp = -1;
433: }
434: if (check==1) *xi = mut;
435: if (hyp==-1 && ptypehyp) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Problem does not satisfy hyperbolic test; consider removing the hyperbolicity flag");
436: if (check==1 && hyp==0) {
437: PEPQSliceMatGetInertia(pep,PETSC_MAX_REAL,&inertia,NULL);
438: if (inertia == 0) hyp = 1;
439: else hyp = -1;
440: }
441: if (check==1 && hyp!=1) {
442: check = 0;
443: EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);
444: for (;k<its&&!check;k++) {
445: PEPQSliceEvaluateQEP(pep,s,M,str);
446: EPSSetOperators(eps,M,NULL);
447: EPSSolve(eps);
448: EPSGetConverged(eps,&nconv);
449: if (!nconv) break;
450: EPSGetEigenpair(eps,0,NULL,NULL,u,w);
451: sp = s;
452: PEPQSliceDiscriminant(pep,u,w,&d,&s,&omg);
453: if (d<0.0) {check = -1; break;}
454: if (PetscAbsReal((s-sp)/s)<100*PETSC_MACHINE_EPSILON) break;
455: mut = 2*s-sp;
456: PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
457: if (inertia == 0) {check = 1; break;}
458: }
459: for (;k<its&&!check;k++) {
460: mut = (s-omg)/2;
461: PEPQSliceMatGetInertia(pep,mut,&inertia,NULL);
462: if (inertia == 0) {check = 1; break;}
463: if (PetscAbsReal((s-omg)/omg)<100*PETSC_MACHINE_EPSILON) break;
464: PEPQSliceEvaluateQEP(pep,omg,M,str);
465: EPSSetOperators(eps,M,NULL);
466: EPSSolve(eps);
467: EPSGetConverged(eps,&nconv);
468: if (!nconv) break;
469: EPSGetEigenpair(eps,0,NULL,NULL,u,w);
470: PEPQSliceDiscriminant(pep,u,w,&d,NULL,&omg);
471: if (d<0.0) {check = -1; break;}
472: }
473: }
474: if (check==1) *mu = mut;
475: *definite = check;
476: *hyperbolic = hyp;
477: if (M) { MatDestroy(&M); }
478: VecDestroy(&u);
479: VecDestroy(&w);
480: EPSDestroy(&eps);
481: STSetTransform(pep->st,transform);
482: return(0);
483: }
485: /*
486: Dummy backtransform operation
487: */
488: static PetscErrorCode PEPBackTransform_Skip(PEP pep)
489: {
491: return(0);
492: }
494: PetscErrorCode PEPSetUp_STOAR_QSlice(PEP pep)
495: {
497: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
498: PEP_SR sr;
499: PetscInt ld,i,zeros=0;
500: SlepcSC sc;
501: PetscReal r;
504: PEPCheckSinvertCayley(pep);
505: 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()");
506: 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");
507: PEPCheckUnsupportedCondition(pep,PEP_FEATURE_STOPPING,PETSC_TRUE," (with spectrum slicing)");
508: if (pep->tol==PETSC_DEFAULT) {
509: #if defined(PETSC_USE_REAL_SINGLE)
510: pep->tol = SLEPC_DEFAULT_TOL;
511: #else
512: /* use tighter tolerance */
513: pep->tol = SLEPC_DEFAULT_TOL*1e-2;
514: #endif
515: }
516: if (ctx->nev==1) ctx->nev = PetscMin(20,pep->n); /* nev not set, use default value */
517: 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");
518: pep->ops->backtransform = PEPBackTransform_Skip;
519: if (pep->max_it==PETSC_DEFAULT) pep->max_it = 100;
521: /* create spectrum slicing context and initialize it */
522: PEPQSliceResetSR(pep);
523: PetscNewLog(pep,&sr);
524: ctx->sr = sr;
525: sr->itsKs = 0;
526: sr->nleap = 0;
527: sr->sPres = NULL;
529: if (pep->solvematcoeffs) { PetscFree(pep->solvematcoeffs); }
530: PetscMalloc1(pep->nmat,&pep->solvematcoeffs);
531: if (!pep->st) { PEPGetST(pep,&pep->st); }
532: STSetTransform(pep->st,PETSC_FALSE);
533: STSetUp(pep->st);
535: ctx->hyperbolic = (pep->problem_type==PEP_HYPERBOLIC)? PETSC_TRUE: PETSC_FALSE;
537: /* check presence of ends and finding direction */
538: if (pep->inta > PETSC_MIN_REAL || pep->intb >= PETSC_MAX_REAL) {
539: sr->int0 = pep->inta;
540: sr->int1 = pep->intb;
541: sr->dir = 1;
542: if (pep->intb >= PETSC_MAX_REAL) { /* Right-open interval */
543: sr->hasEnd = PETSC_FALSE;
544: } else sr->hasEnd = PETSC_TRUE;
545: } else {
546: sr->int0 = pep->intb;
547: sr->int1 = pep->inta;
548: sr->dir = -1;
549: sr->hasEnd = PetscNot(pep->inta <= PETSC_MIN_REAL);
550: }
552: /* compute inertia0 */
553: PEPQSliceGetInertia(pep,sr->int0,&sr->inertia0,ctx->detect?&zeros:NULL,ctx->hyperbolic?0:1);
554: 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");
555: if (!ctx->hyperbolic && ctx->checket) {
556: PEPQSliceCheckEigenvalueType(pep,sr->int0,0.0,PETSC_TRUE);
557: }
559: /* compute inertia1 */
560: PEPQSliceGetInertia(pep,sr->int1,&sr->inertia1,ctx->detect?&zeros:NULL,ctx->hyperbolic?0:1);
561: if (zeros) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_USER,"Found singular matrix for the transformed problem in an interval endpoint defined by user");
562: if (!ctx->hyperbolic && ctx->checket && sr->hasEnd) {
563: PEPQSliceCheckEigenvalueType(pep,sr->int1,0.0,PETSC_TRUE);
564: if (!sr->type && (sr->inertia1-sr->inertia0)) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"No information of eigenvalue type in Interval");
565: if (sr->type && !(sr->inertia1-sr->inertia0)) SETERRQ(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Different positive/negative type detected");
566: if (sr->dir*(sr->inertia1-sr->inertia0)<0) {
567: sr->intcorr = -1;
568: sr->inertia0 = 2*pep->n-sr->inertia0;
569: sr->inertia1 = 2*pep->n-sr->inertia1;
570: } else sr->intcorr = 1;
571: } else {
572: if (sr->inertia0<=pep->n && sr->inertia1<=pep->n) sr->intcorr = 1;
573: else if (sr->inertia0>=pep->n && sr->inertia1>=pep->n) sr->intcorr = -1;
574: }
576: if (sr->hasEnd) {
577: sr->dir = -sr->dir; r = sr->int0; sr->int0 = sr->int1; sr->int1 = r;
578: i = sr->inertia0; sr->inertia0 = sr->inertia1; sr->inertia1 = i;
579: }
581: /* number of eigenvalues in interval */
582: sr->numEigs = (sr->dir)*(sr->inertia1 - sr->inertia0);
583: PetscInfo3(pep,"QSlice setup: allocating for %D eigenvalues in [%g,%g]\n",sr->numEigs,(double)pep->inta,(double)pep->intb);
584: if (sr->numEigs) {
585: PEPQSliceAllocateSolution(pep);
586: PEPSetDimensions_Default(pep,ctx->nev,&ctx->ncv,&ctx->mpd);
587: pep->nev = ctx->nev; pep->ncv = ctx->ncv; pep->mpd = ctx->mpd;
588: ld = ctx->ncv+2;
589: DSSetType(pep->ds,DSGHIEP);
590: DSSetCompact(pep->ds,PETSC_TRUE);
591: DSSetExtraRow(pep->ds,PETSC_TRUE);
592: DSAllocate(pep->ds,ld);
593: DSGetSlepcSC(pep->ds,&sc);
594: sc->rg = NULL;
595: sc->comparison = SlepcCompareLargestMagnitude;
596: sc->comparisonctx = NULL;
597: sc->map = NULL;
598: sc->mapobj = NULL;
599: } else {pep->ncv = 0; pep->nev = 0; pep->mpd = 0;}
600: return(0);
601: }
603: /*
604: Fills the fields of a shift structure
605: */
606: static PetscErrorCode PEPCreateShift(PEP pep,PetscReal val,PEP_shift neighb0,PEP_shift neighb1)
607: {
609: PEP_shift s,*pending2;
610: PetscInt i;
611: PEP_SR sr;
612: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
615: sr = ctx->sr;
616: PetscNewLog(pep,&s);
617: s->value = val;
618: s->neighb[0] = neighb0;
619: if (neighb0) neighb0->neighb[1] = s;
620: s->neighb[1] = neighb1;
621: if (neighb1) neighb1->neighb[0] = s;
622: s->comp[0] = PETSC_FALSE;
623: s->comp[1] = PETSC_FALSE;
624: s->index = -1;
625: s->neigs = 0;
626: s->nconv[0] = s->nconv[1] = 0;
627: s->nsch[0] = s->nsch[1]=0;
628: /* Inserts in the stack of pending shifts */
629: /* If needed, the array is resized */
630: if (sr->nPend >= sr->maxPend) {
631: sr->maxPend *= 2;
632: PetscMalloc1(sr->maxPend,&pending2);
633: PetscLogObjectMemory((PetscObject)pep,sr->maxPend*sizeof(PEP_shift*));
634: for (i=0;i<sr->nPend;i++) pending2[i] = sr->pending[i];
635: PetscFree(sr->pending);
636: sr->pending = pending2;
637: }
638: sr->pending[sr->nPend++]=s;
639: return(0);
640: }
642: /* Provides next shift to be computed */
643: static PetscErrorCode PEPExtractShift(PEP pep)
644: {
646: PetscInt iner,zeros=0;
647: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
648: PEP_SR sr;
649: PetscReal newShift,aux;
650: PEP_shift sPres;
653: sr = ctx->sr;
654: if (sr->nPend > 0) {
655: if (sr->dirch) {
656: aux = sr->int1; sr->int1 = sr->int0; sr->int0 = aux;
657: iner = sr->inertia1; sr->inertia1 = sr->inertia0; sr->inertia0 = iner;
658: sr->dir *= -1;
659: PetscFree(sr->s0->neighb[1]);
660: PetscFree(sr->s0);
661: sr->nPend--;
662: PEPCreateShift(pep,sr->int0,NULL,NULL);
663: sr->sPrev = NULL;
664: sr->sPres = sr->pending[--sr->nPend];
665: pep->target = sr->sPres->value;
666: sr->s0 = sr->sPres;
667: pep->reason = PEP_CONVERGED_ITERATING;
668: } else {
669: sr->sPrev = sr->sPres;
670: sr->sPres = sr->pending[--sr->nPend];
671: }
672: sPres = sr->sPres;
673: PEPQSliceGetInertia(pep,sPres->value,&iner,ctx->detect?&zeros:NULL,sr->intcorr);
674: if (zeros) {
675: newShift = sPres->value*(1.0+SLICE_PTOL);
676: if (sr->dir*(sPres->neighb[0] && newShift-sPres->neighb[0]->value) < 0) newShift = (sPres->value+sPres->neighb[0]->value)/2;
677: else if (sPres->neighb[1] && sr->dir*(sPres->neighb[1]->value-newShift) < 0) newShift = (sPres->value+sPres->neighb[1]->value)/2;
678: PEPQSliceGetInertia(pep,newShift,&iner,&zeros,sr->intcorr);
679: if (zeros) SETERRQ1(((PetscObject)pep)->comm,PETSC_ERR_CONV_FAILED,"Inertia computation fails in %g",newShift);
680: sPres->value = newShift;
681: }
682: sr->sPres->inertia = iner;
683: pep->target = sr->sPres->value;
684: pep->reason = PEP_CONVERGED_ITERATING;
685: pep->its = 0;
686: } else sr->sPres = NULL;
687: return(0);
688: }
690: /*
691: Obtains value of subsequent shift
692: */
693: static PetscErrorCode PEPGetNewShiftValue(PEP pep,PetscInt side,PetscReal *newS)
694: {
695: PetscReal lambda,d_prev;
696: PetscInt i,idxP;
697: PEP_SR sr;
698: PEP_shift sPres,s;
699: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
702: sr = ctx->sr;
703: sPres = sr->sPres;
704: if (sPres->neighb[side]) {
705: /* Completing a previous interval */
706: if (!sPres->neighb[side]->neighb[side] && sPres->neighb[side]->nconv[side]==0) { /* One of the ends might be too far from eigenvalues */
707: if (side) *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[sr->indexEig-1]]))/2;
708: else *newS = (sPres->value + PetscRealPart(sr->eigr[sr->perm[0]]))/2;
709: } else *newS=(sPres->value + sPres->neighb[side]->value)/2;
710: } else { /* (Only for side=1). Creating a new interval. */
711: if (sPres->neigs==0) {/* No value has been accepted*/
712: if (sPres->neighb[0]) {
713: /* Multiplying by 10 the previous distance */
714: *newS = sPres->value + 10*(sr->dir)*PetscAbsReal(sPres->value - sPres->neighb[0]->value);
715: sr->nleap++;
716: /* Stops when the interval is open and no values are found in the last 5 shifts (there might be infinite eigenvalues) */
717: if (!sr->hasEnd && sr->nleap > 5) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_CONV_FAILED,"Unable to compute the wanted eigenvalues with open interval");
718: } else { /* First shift */
719: if (pep->nconv != 0) {
720: /* Unaccepted values give information for next shift */
721: idxP=0;/* Number of values left from shift */
722: for (i=0;i<pep->nconv;i++) {
723: lambda = PetscRealPart(pep->eigr[i]);
724: if ((sr->dir)*(lambda - sPres->value) <0) idxP++;
725: else break;
726: }
727: /* Avoiding subtraction of eigenvalues (might be the same).*/
728: if (idxP>0) {
729: d_prev = PetscAbsReal(sPres->value - PetscRealPart(pep->eigr[0]))/(idxP+0.3);
730: } else {
731: d_prev = PetscAbsReal(sPres->value - PetscRealPart(pep->eigr[pep->nconv-1]))/(pep->nconv+0.3);
732: }
733: *newS = sPres->value + ((sr->dir)*d_prev*pep->nev)/2;
734: sr->dirch = PETSC_FALSE;
735: } else { /* No values found, no information for next shift */
736: if (!sr->dirch) {
737: sr->dirch = PETSC_TRUE;
738: *newS = sr->int1;
739: } else SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"First shift renders no information");
740: }
741: }
742: } else { /* Accepted values found */
743: sr->dirch = PETSC_FALSE;
744: sr->nleap = 0;
745: /* Average distance of values in previous subinterval */
746: s = sPres->neighb[0];
747: while (s && PetscAbs(s->inertia - sPres->inertia)==0) {
748: s = s->neighb[0];/* Looking for previous shifts with eigenvalues within */
749: }
750: if (s) {
751: d_prev = PetscAbsReal((sPres->value - s->value)/(sPres->inertia - s->inertia));
752: } else { /* First shift. Average distance obtained with values in this shift */
753: /* first shift might be too far from first wanted eigenvalue (no values found outside the interval)*/
754: 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)) {
755: d_prev = PetscAbsReal((PetscRealPart(sr->eigr[sr->indexEig-1]) - PetscRealPart(sr->eigr[0])))/(sPres->neigs+0.3);
756: } else {
757: d_prev = PetscAbsReal(PetscRealPart(sr->eigr[sr->indexEig-1]) - sPres->value)/(sPres->neigs+0.3);
758: }
759: }
760: /* Average distance is used for next shift by adding it to value on the right or to shift */
761: if ((sr->dir)*(PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1]) - sPres->value)>0) {
762: *newS = PetscRealPart(sr->eigr[sPres->index + sPres->neigs -1])+ ((sr->dir)*d_prev*(pep->nev))/2;
763: } else { /* Last accepted value is on the left of shift. Adding to shift */
764: *newS = sPres->value + ((sr->dir)*d_prev*(pep->nev))/2;
765: }
766: }
767: /* End of interval can not be surpassed */
768: if ((sr->dir)*(sr->int1 - *newS) < 0) *newS = sr->int1;
769: }/* of neighb[side]==null */
770: return(0);
771: }
773: /*
774: Function for sorting an array of real values
775: */
776: static PetscErrorCode sortRealEigenvalues(PetscScalar *r,PetscInt *perm,PetscInt nr,PetscBool prev,PetscInt dir)
777: {
778: PetscReal re;
779: PetscInt i,j,tmp;
782: if (!prev) for (i=0;i<nr;i++) perm[i] = i;
783: /* Insertion sort */
784: for (i=1;i<nr;i++) {
785: re = PetscRealPart(r[perm[i]]);
786: j = i-1;
787: while (j>=0 && dir*(re - PetscRealPart(r[perm[j]])) <= 0) {
788: tmp = perm[j]; perm[j] = perm[j+1]; perm[j+1] = tmp; j--;
789: }
790: }
791: return(0);
792: }
794: /* Stores the pairs obtained since the last shift in the global arrays */
795: static PetscErrorCode PEPStoreEigenpairs(PEP pep)
796: {
798: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
799: PetscReal lambda,err,*errest;
800: PetscInt i,*aux,count=0,ndef,ld,nconv=pep->nconv,d=pep->nmat-1,idx;
801: PetscBool iscayley,divide=PETSC_FALSE;
802: PEP_SR sr = ctx->sr;
803: PEP_shift sPres;
804: Vec w,vomega;
805: Mat MS;
806: BV tV;
807: PetscScalar *S,*eigr,*tS,*omega;
810: sPres = sr->sPres;
811: sPres->index = sr->indexEig;
813: if (nconv>sr->ndef0+sr->ndef1) {
814: /* Back-transform */
815: STBackTransform(pep->st,nconv,pep->eigr,pep->eigi);
816: for (i=0;i<nconv;i++) {
817: #if defined(PETSC_USE_COMPLEX)
818: if (PetscImaginaryPart(pep->eigr[i])) pep->eigr[i] = sr->int0-sr->dir;
819: #else
820: if (pep->eigi[i]) pep->eigr[i] = sr->int0-sr->dir;
821: #endif
822: }
823: PetscObjectTypeCompare((PetscObject)pep->st,STCAYLEY,&iscayley);
824: /* Sort eigenvalues */
825: sortRealEigenvalues(pep->eigr,pep->perm,nconv,PETSC_FALSE,sr->dir);
826: VecCreateSeq(PETSC_COMM_SELF,nconv,&vomega);
827: BVGetSignature(ctx->V,vomega);
828: VecGetArray(vomega,&omega);
829: BVGetSizes(pep->V,NULL,NULL,&ld);
830: BVTensorGetFactors(ctx->V,NULL,&MS);
831: MatDenseGetArray(MS,&S);
832: /* Values stored in global array */
833: PetscCalloc4(nconv,&eigr,nconv,&errest,nconv*nconv*d,&tS,nconv,&aux);
834: ndef = sr->ndef0+sr->ndef1;
835: for (i=0;i<nconv;i++) {
836: lambda = PetscRealPart(pep->eigr[pep->perm[i]]);
837: err = pep->errest[pep->perm[i]];
838: if ((sr->dir)*(lambda - sPres->ext[0]) > 0 && (sr->dir)*(sPres->ext[1] - lambda) > 0) {/* Valid value */
839: if (sr->indexEig+count-ndef>=sr->numEigs) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Unexpected error in Spectrum Slicing");
840: PEPQSliceCheckEigenvalueType(pep,lambda,PetscRealPart(omega[pep->perm[i]]),PETSC_FALSE);
841: eigr[count] = lambda;
842: errest[count] = err;
843: if (((sr->dir)*(sPres->value - lambda) > 0) && ((sr->dir)*(lambda - sPres->ext[0]) > 0)) sPres->nconv[0]++;
844: if (((sr->dir)*(lambda - sPres->value) > 0) && ((sr->dir)*(sPres->ext[1] - lambda) > 0)) sPres->nconv[1]++;
845: PetscArraycpy(tS+count*(d*nconv),S+pep->perm[i]*(d*ld),nconv);
846: PetscArraycpy(tS+count*(d*nconv)+nconv,S+pep->perm[i]*(d*ld)+ld,nconv);
847: count++;
848: }
849: }
850: VecRestoreArray(vomega,&omega);
851: VecDestroy(&vomega);
852: for (i=0;i<count;i++) {
853: PetscArraycpy(S+i*(d*ld),tS+i*nconv*d,nconv);
854: PetscArraycpy(S+i*(d*ld)+ld,tS+i*nconv*d+nconv,nconv);
855: }
856: MatDenseRestoreArray(MS,&S);
857: BVTensorRestoreFactors(ctx->V,NULL,&MS);
858: BVSetActiveColumns(ctx->V,0,count);
859: BVTensorCompress(ctx->V,count);
860: if (sr->sPres->nconv[0] && sr->sPres->nconv[1]) {
861: divide = PETSC_TRUE;
862: BVTensorGetFactors(ctx->V,NULL,&MS);
863: MatDenseGetArray(MS,&S);
864: PetscArrayzero(tS,nconv*nconv*d);
865: for (i=0;i<count;i++) {
866: PetscArraycpy(tS+i*nconv*d,S+i*(d*ld),count);
867: PetscArraycpy(tS+i*nconv*d+nconv,S+i*(d*ld)+ld,count);
868: }
869: MatDenseRestoreArray(MS,&S);
870: BVTensorRestoreFactors(ctx->V,NULL,&MS);
871: BVSetActiveColumns(pep->V,0,count);
872: BVDuplicateResize(pep->V,count,&tV);
873: BVCopy(pep->V,tV);
874: }
875: if (sr->sPres->nconv[0]) {
876: if (divide) {
877: BVSetActiveColumns(ctx->V,0,sr->sPres->nconv[0]);
878: BVTensorCompress(ctx->V,sr->sPres->nconv[0]);
879: }
880: for (i=0;i<sr->ndef0;i++) aux[i] = sr->idxDef0[i];
881: for (i=sr->ndef0;i<sr->sPres->nconv[0];i++) aux[i] = sr->indexEig+i-sr->ndef0;
882: BVTensorGetFactors(ctx->V,NULL,&MS);
883: MatDenseGetArray(MS,&S);
884: for (i=0;i<sr->sPres->nconv[0];i++) {
885: sr->eigr[aux[i]] = eigr[i];
886: sr->errest[aux[i]] = errest[i];
887: BVGetColumn(pep->V,i,&w);
888: BVInsertVec(sr->V,aux[i],w);
889: BVRestoreColumn(pep->V,i,&w);
890: idx = sr->ld*d*aux[i];
891: PetscArrayzero(sr->S+idx,sr->ld*d);
892: PetscArraycpy(sr->S+idx,S+i*(ld*d),sr->sPres->nconv[0]);
893: PetscArraycpy(sr->S+idx+sr->ld,S+i*(ld*d)+ld,sr->sPres->nconv[0]);
894: PetscFree(sr->qinfo[aux[i]].q);
895: PetscMalloc1(sr->sPres->nconv[0],&sr->qinfo[aux[i]].q);
896: PetscArraycpy(sr->qinfo[aux[i]].q,aux,sr->sPres->nconv[0]);
897: sr->qinfo[aux[i]].nq = sr->sPres->nconv[0];
898: }
899: MatDenseRestoreArray(MS,&S);
900: BVTensorRestoreFactors(ctx->V,NULL,&MS);
901: }
903: if (sr->sPres->nconv[1]) {
904: if (divide) {
905: BVTensorGetFactors(ctx->V,NULL,&MS);
906: MatDenseGetArray(MS,&S);
907: for (i=0;i<sr->sPres->nconv[1];i++) {
908: PetscArraycpy(S+i*(d*ld),tS+(sr->sPres->nconv[0]+i)*nconv*d,count);
909: PetscArraycpy(S+i*(d*ld)+ld,tS+(sr->sPres->nconv[0]+i)*nconv*d+nconv,count);
910: }
911: MatDenseRestoreArray(MS,&S);
912: BVTensorRestoreFactors(ctx->V,NULL,&MS);
913: BVSetActiveColumns(pep->V,0,count);
914: BVCopy(tV,pep->V);
915: BVSetActiveColumns(ctx->V,0,sr->sPres->nconv[1]);
916: BVTensorCompress(ctx->V,sr->sPres->nconv[1]);
917: }
918: for (i=0;i<sr->ndef1;i++) aux[i] = sr->idxDef1[i];
919: for (i=sr->ndef1;i<sr->sPres->nconv[1];i++) aux[i] = sr->indexEig+sr->sPres->nconv[0]-sr->ndef0+i-sr->ndef1;
920: BVTensorGetFactors(ctx->V,NULL,&MS);
921: MatDenseGetArray(MS,&S);
922: for (i=0;i<sr->sPres->nconv[1];i++) {
923: sr->eigr[aux[i]] = eigr[sr->sPres->nconv[0]+i];
924: sr->errest[aux[i]] = errest[sr->sPres->nconv[0]+i];
925: BVGetColumn(pep->V,i,&w);
926: BVInsertVec(sr->V,aux[i],w);
927: BVRestoreColumn(pep->V,i,&w);
928: idx = sr->ld*d*aux[i];
929: PetscArrayzero(sr->S+idx,sr->ld*d);
930: PetscArraycpy(sr->S+idx,S+i*(ld*d),sr->sPres->nconv[1]);
931: PetscArraycpy(sr->S+idx+sr->ld,S+i*(ld*d)+ld,sr->sPres->nconv[1]);
932: PetscFree(sr->qinfo[aux[i]].q);
933: PetscMalloc1(sr->sPres->nconv[1],&sr->qinfo[aux[i]].q);
934: PetscArraycpy(sr->qinfo[aux[i]].q,aux,sr->sPres->nconv[1]);
935: sr->qinfo[aux[i]].nq = sr->sPres->nconv[1];
936: }
937: MatDenseRestoreArray(MS,&S);
938: BVTensorRestoreFactors(ctx->V,NULL,&MS);
939: }
940: sPres->neigs = count-sr->ndef0-sr->ndef1;
941: sr->indexEig += sPres->neigs;
942: sPres->nconv[0]-= sr->ndef0;
943: sPres->nconv[1]-= sr->ndef1;
944: PetscFree4(eigr,errest,tS,aux);
945: } else {
946: sPres->neigs = 0;
947: sPres->nconv[0]= 0;
948: sPres->nconv[1]= 0;
949: }
950: /* Global ordering array updating */
951: sortRealEigenvalues(sr->eigr,sr->perm,sr->indexEig,PETSC_FALSE,sr->dir);
952: /* Check for completion */
953: sPres->comp[0] = PetscNot(sPres->nconv[0] < sPres->nsch[0]);
954: sPres->comp[1] = PetscNot(sPres->nconv[1] < sPres->nsch[1]);
955: if (sPres->nconv[0] > sPres->nsch[0] || sPres->nconv[1] > sPres->nsch[1]) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Mismatch between number of values found and information from inertia");
956: if (divide) { BVDestroy(&tV); }
957: return(0);
958: }
960: static PetscErrorCode PEPLookForDeflation(PEP pep)
961: {
962: PetscReal val;
963: PetscInt i,count0=0,count1=0;
964: PEP_shift sPres;
965: PetscInt ini,fin;
966: PEP_SR sr;
967: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
970: sr = ctx->sr;
971: sPres = sr->sPres;
973: if (sPres->neighb[0]) ini = (sr->dir)*(sPres->neighb[0]->inertia - sr->inertia0);
974: else ini = 0;
975: fin = sr->indexEig;
976: /* Selection of ends for searching new values */
977: if (!sPres->neighb[0]) sPres->ext[0] = sr->int0;/* First shift */
978: else sPres->ext[0] = sPres->neighb[0]->value;
979: if (!sPres->neighb[1]) {
980: if (sr->hasEnd) sPres->ext[1] = sr->int1;
981: else sPres->ext[1] = (sr->dir > 0)?PETSC_MAX_REAL:PETSC_MIN_REAL;
982: } else sPres->ext[1] = sPres->neighb[1]->value;
983: /* Selection of values between right and left ends */
984: for (i=ini;i<fin;i++) {
985: val=PetscRealPart(sr->eigr[sr->perm[i]]);
986: /* Values to the right of left shift */
987: if ((sr->dir)*(val - sPres->ext[1]) < 0) {
988: if ((sr->dir)*(val - sPres->value) < 0) count0++;
989: else count1++;
990: } else break;
991: }
992: /* The number of values on each side are found */
993: if (sPres->neighb[0]) {
994: sPres->nsch[0] = (sr->dir)*(sPres->inertia - sPres->neighb[0]->inertia)-count0;
995: if (sPres->nsch[0]<0) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Mismatch between number of values found and information from inertia");
996: } else sPres->nsch[0] = 0;
998: if (sPres->neighb[1]) {
999: sPres->nsch[1] = (sr->dir)*(sPres->neighb[1]->inertia - sPres->inertia) - count1;
1000: if (sPres->nsch[1]<0) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Mismatch between number of values found and information from inertia");
1001: } else sPres->nsch[1] = (sr->dir)*(sr->inertia1 - sPres->inertia);
1003: /* Completing vector of indexes for deflation */
1004: for (i=0;i<count0;i++) sr->idxDef0[i] = sr->perm[ini+i];
1005: sr->ndef0 = count0;
1006: for (i=0;i<count1;i++) sr->idxDef1[i] = sr->perm[ini+count0+i];
1007: sr->ndef1 = count1;
1008: return(0);
1009: }
1011: /*
1012: Compute a run of Lanczos iterations
1013: */
1014: static PetscErrorCode PEPSTOARrun_QSlice(PEP pep,PetscReal *a,PetscReal *b,PetscReal *omega,PetscInt k,PetscInt *M,PetscBool *breakdown,PetscBool *symmlost,Vec *t_)
1015: {
1017: PEP_STOAR *ctx = (PEP_STOAR*)pep->data;
1018: PetscInt i,j,m=*M,l,lock;
1019: PetscInt lds,d,ld,offq,nqt,ldds;
1020: Vec v=t_[0],t=t_[1],q=t_[2];
1021: PetscReal norm,sym=0.0,fro=0.0,*f;
1022: PetscScalar *y,*S,sigma;
1023: PetscBLASInt j_,one=1;
1024: PetscBool lindep;
1025: Mat MS;
1028: PetscMalloc1(*M,&y);
1029: BVGetSizes(pep->V,NULL,NULL,&ld);
1030: BVTensorGetDegree(ctx->V,&d);
1031: BVGetActiveColumns(pep->V,&lock,&nqt);
1032: lds = d*ld;
1033: offq = ld;
1034: DSGetLeadingDimension(pep->ds,&ldds);
1036: *breakdown = PETSC_FALSE; /* ----- */
1037: STGetShift(pep->st,&sigma);
1038: DSGetDimensions(pep->ds,NULL,&l,NULL,NULL);
1039: BVSetActiveColumns(ctx->V,0,m);
1040: BVSetActiveColumns(pep->V,0,nqt);
1041: for (j=k;j<m;j++) {
1042: /* apply operator */
1043: BVTensorGetFactors(ctx->V,NULL,&MS);
1044: MatDenseGetArray(MS,&S);
1045: BVGetColumn(pep->V,nqt,&t);
1046: BVMultVec(pep->V,1.0,0.0,v,S+j*lds);
1047: MatMult(pep->A[1],v,q);
1048: MatMult(pep->A[2],v,t);
1049: VecAXPY(q,sigma*pep->sfactor,t);
1050: VecScale(q,pep->sfactor);
1051: BVMultVec(pep->V,1.0,0.0,v,S+offq+j*lds);
1052: MatMult(pep->A[2],v,t);
1053: VecAXPY(q,pep->sfactor*pep->sfactor,t);
1054: STMatSolve(pep->st,q,t);
1055: VecScale(t,-1.0);
1056: BVRestoreColumn(pep->V,nqt,&t);
1058: /* orthogonalize */
1059: BVOrthogonalizeColumn(pep->V,nqt,S+(j+1)*lds,&norm,&lindep);
1060: if (!lindep) {
1061: *(S+(j+1)*lds+nqt) = norm;
1062: BVScaleColumn(pep->V,nqt,1.0/norm);
1063: nqt++;
1064: }
1065: for (i=0;i<nqt;i++) *(S+(j+1)*lds+offq+i) = *(S+j*lds+i)+sigma*(*(S+(j+1)*lds+i));
1066: BVSetActiveColumns(pep->V,0,nqt);
1067: MatDenseRestoreArray(MS,&S);
1068: BVTensorRestoreFactors(ctx->V,NULL,&MS);
1070: /* level-2 orthogonalization */
1071: BVOrthogonalizeColumn(ctx->V,j+1,y,&norm,&lindep);
1072: a[j] = PetscRealPart(y[j]);
1073: omega[j+1] = (norm > 0)?1.0:-1.0;
1074: BVScaleColumn(ctx->V,j+1,1.0/norm);
1075: b[j] = PetscAbsReal(norm);
1077: /* check symmetry */
1078: DSGetArrayReal(pep->ds,DS_MAT_T,&f);
1079: if (j==k) {
1080: for (i=l;i<j-1;i++) y[i] = PetscAbsScalar(y[i])-PetscAbsReal(f[2*ldds+i]);
1081: for (i=0;i<l;i++) y[i] = 0.0;
1082: }
1083: DSRestoreArrayReal(pep->ds,DS_MAT_T,&f);
1084: if (j>0) y[j-1] = PetscAbsScalar(y[j-1])-PetscAbsReal(b[j-1]);
1085: PetscBLASIntCast(j,&j_);
1086: sym = SlepcAbs(BLASnrm2_(&j_,y,&one),sym);
1087: fro = SlepcAbs(fro,SlepcAbs(a[j],b[j]));
1088: if (j>0) fro = SlepcAbs(fro,b[j-1]);
1089: if (sym/fro>PetscMax(PETSC_SQRT_MACHINE_EPSILON,10*pep->tol)) {
1090: *symmlost = PETSC_TRUE;
1091: *M=j;
1092: break;
1093: }
1094: }
1095: BVSetActiveColumns(pep->V,lock,nqt);
1096: BVSetActiveColumns(ctx->V,0,*M);
1097: PetscFree(y);
1098: return(0);
1099: }
1101: static PetscErrorCode PEPSTOAR_QSlice(PEP pep,Mat B)
1102: {
1104: PEP_STOAR *ctx = (PEP_STOAR*)pep->data;
1105: PetscInt j,k,l,nv=0,ld,ldds,t,nq=0,idx;
1106: PetscInt nconv=0,deg=pep->nmat-1,count0=0,count1=0;
1107: PetscScalar *om,sigma,*back,*S,*pQ;
1108: PetscReal beta,norm=1.0,*omega,*a,*b,eta,lambda;
1109: PetscBool breakdown,symmlost=PETSC_FALSE,sinv,falselock=PETSC_TRUE;
1110: Mat MS,MQ;
1111: Vec v,vomega;
1112: PEP_SR sr;
1113: BVOrthogType otype;
1114: BVOrthogBlockType obtype;
1117: /* Resize if needed for deflating vectors */
1118: sr = ctx->sr;
1119: sigma = sr->sPres->value;
1120: k = sr->ndef0+sr->ndef1;
1121: pep->ncv = ctx->ncv+k;
1122: pep->nev = ctx->nev+k;
1123: PEPAllocateSolution(pep,3);
1124: BVDestroy(&ctx->V);
1125: BVCreateTensor(pep->V,pep->nmat-1,&ctx->V);
1126: BVGetOrthogonalization(pep->V,&otype,NULL,&eta,&obtype);
1127: BVSetOrthogonalization(ctx->V,otype,BV_ORTHOG_REFINE_ALWAYS,eta,obtype);
1128: DSAllocate(pep->ds,pep->ncv+2);
1129: PetscMalloc1(pep->ncv,&back);
1130: DSGetLeadingDimension(pep->ds,&ldds);
1131: BVSetMatrix(ctx->V,B,PETSC_TRUE);
1132: if (ctx->lock) {
1133: /* undocumented option to use a cheaper locking instead of the true locking */
1134: PetscOptionsGetBool(NULL,NULL,"-pep_stoar_falselocking",&falselock,NULL);
1135: } else SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_SUP,"A locking variant is needed for spectrum slicing");
1136: PetscObjectTypeCompare((PetscObject)pep->st,STSINVERT,&sinv);
1137: RGPushScale(pep->rg,sinv?pep->sfactor:1.0/pep->sfactor);
1138: STScaleShift(pep->st,sinv?pep->sfactor:1.0/pep->sfactor);
1140: /* Get the starting Arnoldi vector */
1141: BVSetActiveColumns(pep->V,0,1);
1142: BVTensorBuildFirstColumn(ctx->V,pep->nini);
1143: BVSetActiveColumns(ctx->V,0,1);
1144: if (k) {
1145: /* Insert deflated vectors */
1146: BVSetActiveColumns(pep->V,0,0);
1147: idx = sr->ndef0?sr->idxDef0[0]:sr->idxDef1[0];
1148: for (j=0;j<k;j++) {
1149: BVGetColumn(pep->V,j,&v);
1150: BVCopyVec(sr->V,sr->qinfo[idx].q[j],v);
1151: BVRestoreColumn(pep->V,j,&v);
1152: }
1153: /* Update innerproduct matrix */
1154: BVSetActiveColumns(ctx->V,0,0);
1155: BVTensorGetFactors(ctx->V,NULL,&MS);
1156: BVSetActiveColumns(pep->V,0,k);
1157: BVTensorRestoreFactors(ctx->V,NULL,&MS);
1159: BVGetSizes(pep->V,NULL,NULL,&ld);
1160: BVTensorGetFactors(ctx->V,NULL,&MS);
1161: MatDenseGetArray(MS,&S);
1162: for (j=0;j<sr->ndef0;j++) {
1163: PetscArrayzero(S+j*ld*deg,ld*deg);
1164: PetscArraycpy(S+j*ld*deg,sr->S+sr->idxDef0[j]*sr->ld*deg,k);
1165: PetscArraycpy(S+j*ld*deg+ld,sr->S+sr->idxDef0[j]*sr->ld*deg+sr->ld,k);
1166: pep->eigr[j] = sr->eigr[sr->idxDef0[j]];
1167: pep->errest[j] = sr->errest[sr->idxDef0[j]];
1168: }
1169: for (j=0;j<sr->ndef1;j++) {
1170: PetscArrayzero(S+(j+sr->ndef0)*ld*deg,ld*deg);
1171: PetscArraycpy(S+(j+sr->ndef0)*ld*deg,sr->S+sr->idxDef1[j]*sr->ld*deg,k);
1172: PetscArraycpy(S+(j+sr->ndef0)*ld*deg+ld,sr->S+sr->idxDef1[j]*sr->ld*deg+sr->ld,k);
1173: pep->eigr[j+sr->ndef0] = sr->eigr[sr->idxDef1[j]];
1174: pep->errest[j+sr->ndef0] = sr->errest[sr->idxDef1[j]];
1175: }
1176: MatDenseRestoreArray(MS,&S);
1177: BVTensorRestoreFactors(ctx->V,NULL,&MS);
1178: BVSetActiveColumns(ctx->V,0,k+1);
1179: VecCreateSeq(PETSC_COMM_SELF,k+1,&vomega);
1180: VecGetArray(vomega,&om);
1181: for (j=0;j<k;j++) {
1182: BVOrthogonalizeColumn(ctx->V,j,NULL,&norm,NULL);
1183: BVScaleColumn(ctx->V,j,1/norm);
1184: om[j] = (norm>=0.0)?1.0:-1.0;
1185: }
1186: BVTensorGetFactors(ctx->V,NULL,&MS);
1187: MatDenseGetArray(MS,&S);
1188: for (j=0;j<deg;j++) {
1189: BVSetRandomColumn(pep->V,k+j);
1190: BVOrthogonalizeColumn(pep->V,k+j,S+k*ld*deg+j*ld,&norm,NULL);
1191: BVScaleColumn(pep->V,k+j,1.0/norm);
1192: S[k*ld*deg+j*ld+k+j] = norm;
1193: }
1194: MatDenseRestoreArray(MS,&S);
1195: BVSetActiveColumns(pep->V,0,k+deg);
1196: BVTensorRestoreFactors(ctx->V,NULL,&MS);
1197: BVOrthogonalizeColumn(ctx->V,k,NULL,&norm,NULL);
1198: BVScaleColumn(ctx->V,k,1.0/norm);
1199: om[k] = (norm>=0.0)?1.0:-1.0;
1200: VecRestoreArray(vomega,&om);
1201: BVSetSignature(ctx->V,vomega);
1202: DSGetArrayReal(pep->ds,DS_MAT_T,&a);
1203: VecGetArray(vomega,&om);
1204: for (j=0;j<k;j++) a[j] = PetscRealPart(om[j]/(pep->eigr[j]-sigma));
1205: VecRestoreArray(vomega,&om);
1206: VecDestroy(&vomega);
1207: DSRestoreArrayReal(pep->ds,DS_MAT_T,&a);
1208: DSGetArray(pep->ds,DS_MAT_Q,&pQ);
1209: PetscArrayzero(pQ,ldds*k);
1210: for (j=0;j<k;j++) pQ[j+j*ldds] = 1.0;
1211: DSRestoreArray(pep->ds,DS_MAT_Q,&pQ);
1212: }
1213: BVSetActiveColumns(ctx->V,0,k+1);
1214: DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1215: VecCreateSeq(PETSC_COMM_SELF,k+1,&vomega);
1216: BVGetSignature(ctx->V,vomega);
1217: VecGetArray(vomega,&om);
1218: for (j=0;j<k+1;j++) omega[j] = PetscRealPart(om[j]);
1219: VecRestoreArray(vomega,&om);
1220: DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1221: VecDestroy(&vomega);
1223: 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]);
1225: /* Restart loop */
1226: l = 0;
1227: pep->nconv = k;
1228: while (pep->reason == PEP_CONVERGED_ITERATING) {
1229: pep->its++;
1230: DSGetArrayReal(pep->ds,DS_MAT_T,&a);
1231: b = a+ldds;
1232: DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1234: /* Compute an nv-step Lanczos factorization */
1235: nv = PetscMin(pep->nconv+pep->mpd,pep->ncv);
1236: PEPSTOARrun_QSlice(pep,a,b,omega,pep->nconv+l,&nv,&breakdown,&symmlost,pep->work);
1237: beta = b[nv-1];
1238: if (symmlost && nv==pep->nconv+l) {
1239: pep->reason = PEP_DIVERGED_SYMMETRY_LOST;
1240: pep->nconv = nconv;
1241: PetscInfo2(pep,"Symmetry lost in STOAR sigma=%g nconv=%D\n",(double)sr->sPres->value,nconv);
1242: if (falselock || !ctx->lock) {
1243: BVSetActiveColumns(ctx->V,0,pep->nconv);
1244: BVTensorCompress(ctx->V,0);
1245: }
1246: break;
1247: }
1248: DSRestoreArrayReal(pep->ds,DS_MAT_T,&a);
1249: DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1250: DSSetDimensions(pep->ds,nv,pep->nconv,pep->nconv+l);
1251: if (l==0) {
1252: DSSetState(pep->ds,DS_STATE_INTERMEDIATE);
1253: } else {
1254: DSSetState(pep->ds,DS_STATE_RAW);
1255: }
1257: /* Solve projected problem */
1258: DSSolve(pep->ds,pep->eigr,pep->eigi);
1259: DSSort(pep->ds,pep->eigr,pep->eigi,NULL,NULL,NULL);
1260: DSUpdateExtraRow(pep->ds);
1261: DSSynchronize(pep->ds,pep->eigr,pep->eigi);
1263: /* Check convergence */
1264: /* PEPSTOARpreKConvergence(pep,nv,&norm,pep->work);*/
1265: norm = 1.0;
1266: DSGetDimensions(pep->ds,NULL,NULL,NULL,&t);
1267: PEPKrylovConvergence(pep,PETSC_FALSE,pep->nconv,t-pep->nconv,PetscAbsReal(beta)*norm,&k);
1268: (*pep->stopping)(pep,pep->its,pep->max_it,k,pep->nev,&pep->reason,pep->stoppingctx);
1269: for (j=0;j<k;j++) back[j] = pep->eigr[j];
1270: STBackTransform(pep->st,k,back,pep->eigi);
1271: count0=count1=0;
1272: for (j=0;j<k;j++) {
1273: lambda = PetscRealPart(back[j]);
1274: if (((sr->dir)*(sr->sPres->value - lambda) > 0) && ((sr->dir)*(lambda - sr->sPres->ext[0]) > 0)) count0++;
1275: if (((sr->dir)*(lambda - sr->sPres->value) > 0) && ((sr->dir)*(sr->sPres->ext[1] - lambda) > 0)) count1++;
1276: }
1277: if ((count0-sr->ndef0 >= sr->sPres->nsch[0]) && (count1-sr->ndef1 >= sr->sPres->nsch[1])) pep->reason = PEP_CONVERGED_TOL;
1278: /* Update l */
1279: if (pep->reason != PEP_CONVERGED_ITERATING || breakdown) l = 0;
1280: else {
1281: l = PetscMax(1,(PetscInt)((nv-k)/2));
1282: l = PetscMin(l,t);
1283: DSGetTruncateSize(pep->ds,k,t,&l);
1284: if (!breakdown) {
1285: /* Prepare the Rayleigh quotient for restart */
1286: DSTruncate(pep->ds,k+l,PETSC_FALSE);
1287: }
1288: }
1289: nconv = k;
1290: if (!ctx->lock && pep->reason == PEP_CONVERGED_ITERATING && !breakdown) { l += k; k = 0; } /* non-locking variant: reset no. of converged pairs */
1291: if (l) { PetscInfo1(pep,"Preparing to restart keeping l=%D vectors\n",l); }
1293: /* Update S */
1294: DSGetMat(pep->ds,DS_MAT_Q,&MQ);
1295: BVMultInPlace(ctx->V,MQ,pep->nconv,k+l);
1296: MatDestroy(&MQ);
1298: /* Copy last column of S */
1299: BVCopyColumn(ctx->V,nv,k+l);
1300: DSGetArrayReal(pep->ds,DS_MAT_D,&omega);
1301: VecCreateSeq(PETSC_COMM_SELF,k+l,&vomega);
1302: VecGetArray(vomega,&om);
1303: for (j=0;j<k+l;j++) om[j] = omega[j];
1304: VecRestoreArray(vomega,&om);
1305: BVSetActiveColumns(ctx->V,0,k+l);
1306: BVSetSignature(ctx->V,vomega);
1307: VecDestroy(&vomega);
1308: DSRestoreArrayReal(pep->ds,DS_MAT_D,&omega);
1310: if (breakdown && pep->reason == PEP_CONVERGED_ITERATING) {
1311: /* stop if breakdown */
1312: PetscInfo2(pep,"Breakdown TOAR method (it=%D norm=%g)\n",pep->its,(double)beta);
1313: pep->reason = PEP_DIVERGED_BREAKDOWN;
1314: }
1315: if (pep->reason != PEP_CONVERGED_ITERATING) l--;
1316: BVGetActiveColumns(pep->V,NULL,&nq);
1317: if (k+l+deg<=nq) {
1318: BVSetActiveColumns(ctx->V,pep->nconv,k+l+1);
1319: if (!falselock && ctx->lock) {
1320: BVTensorCompress(ctx->V,k-pep->nconv);
1321: } else {
1322: BVTensorCompress(ctx->V,0);
1323: }
1324: }
1325: pep->nconv = k;
1326: PEPMonitor(pep,pep->its,nconv,pep->eigr,pep->eigi,pep->errest,nv);
1327: }
1328: sr->itsKs += pep->its;
1329: if (pep->nconv>0) {
1330: BVSetActiveColumns(ctx->V,0,pep->nconv);
1331: BVGetActiveColumns(pep->V,NULL,&nq);
1332: BVSetActiveColumns(pep->V,0,nq);
1333: if (nq>pep->nconv) {
1334: BVTensorCompress(ctx->V,pep->nconv);
1335: BVSetActiveColumns(pep->V,0,pep->nconv);
1336: }
1337: for (j=0;j<pep->nconv;j++) {
1338: pep->eigr[j] *= pep->sfactor;
1339: pep->eigi[j] *= pep->sfactor;
1340: }
1341: }
1342: 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);
1343: STScaleShift(pep->st,sinv?1.0/pep->sfactor:pep->sfactor);
1344: RGPopScale(pep->rg);
1346: if (pep->reason == PEP_DIVERGED_SYMMETRY_LOST && nconv<sr->ndef0+sr->ndef1) SETERRQ1(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Symmetry lost at sigma=%g",(double)sr->sPres->value);
1347: if (pep->reason == PEP_DIVERGED_SYMMETRY_LOST && nconv==sr->ndef0+sr->ndef1) {
1348: if (++sr->symmlost>10) SETERRQ1(PetscObjectComm((PetscObject)pep),PETSC_ERR_PLIB,"Symmetry lost at sigma=%g",(double)sr->sPres->value);
1349: } else sr->symmlost = 0;
1351: DSTruncate(pep->ds,pep->nconv,PETSC_TRUE);
1352: PetscFree(back);
1353: return(0);
1354: }
1356: #define SWAP(a,b,t) {t=a;a=b;b=t;}
1358: static PetscErrorCode PEPQSliceGetInertias(PEP pep,PetscInt *n,PetscReal **shifts,PetscInt **inertias)
1359: {
1360: PetscErrorCode ierr;
1361: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
1362: PEP_SR sr=ctx->sr;
1363: PetscInt i=0,j,tmpi;
1364: PetscReal v,tmpr;
1365: PEP_shift s;
1368: if (!pep->state) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONGSTATE,"Must call PEPSetUp() first");
1369: if (!sr) SETERRQ(PetscObjectComm((PetscObject)pep),PETSC_ERR_ARG_WRONGSTATE,"Only available in interval computations, see PEPSetInterval()");
1370: if (!sr->s0) { /* PEPSolve not called yet */
1371: *n = 2;
1372: } else {
1373: *n = 1;
1374: s = sr->s0;
1375: while (s) {
1376: (*n)++;
1377: s = s->neighb[1];
1378: }
1379: }
1380: PetscMalloc1(*n,shifts);
1381: PetscMalloc1(*n,inertias);
1382: if (!sr->s0) { /* PEPSolve not called yet */
1383: (*shifts)[0] = sr->int0;
1384: (*shifts)[1] = sr->int1;
1385: (*inertias)[0] = sr->inertia0;
1386: (*inertias)[1] = sr->inertia1;
1387: } else {
1388: s = sr->s0;
1389: while (s) {
1390: (*shifts)[i] = s->value;
1391: (*inertias)[i++] = s->inertia;
1392: s = s->neighb[1];
1393: }
1394: (*shifts)[i] = sr->int1;
1395: (*inertias)[i] = sr->inertia1;
1396: }
1397: /* remove possible duplicate in last position */
1398: if ((*shifts)[(*n)-1]==(*shifts)[(*n)-2]) (*n)--;
1399: /* sort result */
1400: for (i=0;i<*n;i++) {
1401: v = (*shifts)[i];
1402: for (j=i+1;j<*n;j++) {
1403: if (v > (*shifts)[j]) {
1404: SWAP((*shifts)[i],(*shifts)[j],tmpr);
1405: SWAP((*inertias)[i],(*inertias)[j],tmpi);
1406: v = (*shifts)[i];
1407: }
1408: }
1409: }
1410: return(0);
1411: }
1413: PetscErrorCode PEPSolve_STOAR_QSlice(PEP pep)
1414: {
1416: PetscInt i,j,ti,deg=pep->nmat-1;
1417: PetscReal newS;
1418: PEP_STOAR *ctx=(PEP_STOAR*)pep->data;
1419: PEP_SR sr=ctx->sr;
1420: Mat S,B;
1421: PetscScalar *pS;
1424: PetscCitationsRegister(citation,&cited);
1426: /* Only with eigenvalues present in the interval ...*/
1427: if (sr->numEigs==0) {
1428: pep->reason = PEP_CONVERGED_TOL;
1429: return(0);
1430: }
1432: /* Inner product matrix */
1433: PEPSTOARSetUpInnerMatrix(pep,&B);
1435: /* Array of pending shifts */
1436: sr->maxPend = 100; /* Initial size */
1437: sr->nPend = 0;
1438: PetscMalloc1(sr->maxPend,&sr->pending);
1439: PetscLogObjectMemory((PetscObject)pep,sr->maxPend*sizeof(PEP_shift*));
1440: PEPCreateShift(pep,sr->int0,NULL,NULL);
1441: /* extract first shift */
1442: sr->sPrev = NULL;
1443: sr->sPres = sr->pending[--sr->nPend];
1444: sr->sPres->inertia = sr->inertia0;
1445: pep->target = sr->sPres->value;
1446: sr->s0 = sr->sPres;
1447: sr->indexEig = 0;
1449: for (i=0;i<sr->numEigs;i++) {
1450: sr->eigr[i] = 0.0;
1451: sr->eigi[i] = 0.0;
1452: sr->errest[i] = 0.0;
1453: sr->perm[i] = i;
1454: }
1455: /* Vectors for deflation */
1456: PetscMalloc2(sr->numEigs,&sr->idxDef0,sr->numEigs,&sr->idxDef1);
1457: PetscLogObjectMemory((PetscObject)pep,2*sr->numEigs*sizeof(PetscInt));
1458: sr->indexEig = 0;
1459: while (sr->sPres) {
1460: /* Search for deflation */
1461: PEPLookForDeflation(pep);
1462: /* KrylovSchur */
1463: PEPSTOAR_QSlice(pep,B);
1465: PEPStoreEigenpairs(pep);
1466: /* Select new shift */
1467: if (!sr->sPres->comp[1]) {
1468: PEPGetNewShiftValue(pep,1,&newS);
1469: PEPCreateShift(pep,newS,sr->sPres,sr->sPres->neighb[1]);
1470: }
1471: if (!sr->sPres->comp[0]) {
1472: /* Completing earlier interval */
1473: PEPGetNewShiftValue(pep,0,&newS);
1474: PEPCreateShift(pep,newS,sr->sPres->neighb[0],sr->sPres);
1475: }
1476: /* Preparing for a new search of values */
1477: PEPExtractShift(pep);
1478: }
1480: /* Updating pep values prior to exit */
1481: PetscFree2(sr->idxDef0,sr->idxDef1);
1482: PetscFree(sr->pending);
1483: pep->nconv = sr->indexEig;
1484: pep->reason = PEP_CONVERGED_TOL;
1485: pep->its = sr->itsKs;
1486: pep->nev = sr->indexEig;
1487: MatCreateSeqDense(PETSC_COMM_SELF,pep->nconv,pep->nconv,NULL,&S);
1488: MatDenseGetArray(S,&pS);
1489: for (i=0;i<pep->nconv;i++) {
1490: 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);
1491: }
1492: MatDenseRestoreArray(S,&pS);
1493: BVSetActiveColumns(sr->V,0,pep->nconv);
1494: BVMultInPlace(sr->V,S,0,pep->nconv);
1495: MatDestroy(&S);
1496: BVDestroy(&pep->V);
1497: pep->V = sr->V;
1498: PetscFree4(pep->eigr,pep->eigi,pep->errest,pep->perm);
1499: pep->eigr = sr->eigr;
1500: pep->eigi = sr->eigi;
1501: pep->perm = sr->perm;
1502: pep->errest = sr->errest;
1503: if (sr->dir<0) {
1504: for (i=0;i<pep->nconv/2;i++) {
1505: ti = sr->perm[i]; sr->perm[i] = sr->perm[pep->nconv-1-i]; sr->perm[pep->nconv-1-i] = ti;
1506: }
1507: }
1508: PetscFree(ctx->inertias);
1509: PetscFree(ctx->shifts);
1510: MatDestroy(&B);
1511: PEPQSliceGetInertias(pep,&ctx->nshifts,&ctx->shifts,&ctx->inertias);
1512: return(0);
1513: }