Actual source code: nleigs.c
slepc-3.7.0 2016-05-16
1: /*
3: SLEPc nonlinear eigensolver: "nleigs"
5: Method: NLEIGS
7: Algorithm:
9: Fully rational Krylov method for nonlinear eigenvalue problems.
11: References:
13: [1] S. Guttel et al., "NLEIGS: A class of robust fully rational Krylov
14: method for nonlinear eigenvalue problems", SIAM J. Sci. Comput.
15: 36(6):A2842-A2864, 2014.
17: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18: SLEPc - Scalable Library for Eigenvalue Problem Computations
19: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
21: This file is part of SLEPc.
23: SLEPc is free software: you can redistribute it and/or modify it under the
24: terms of version 3 of the GNU Lesser General Public License as published by
25: the Free Software Foundation.
27: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
28: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
29: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
30: more details.
32: You should have received a copy of the GNU Lesser General Public License
33: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
34: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35: */
37: #include <slepc/private/nepimpl.h> /*I "slepcnep.h" I*/
38: #include <slepcblaslapack.h>
40: #define MAX_LBPOINTS 100
41: #define NDPOINTS 1e4
42: #define MAX_NSHIFTS 100
44: typedef struct {
45: PetscInt nmat; /* number of interpolation points */
46: PetscScalar *s,*xi; /* Leja-Bagby points */
47: PetscScalar *beta; /* scaling factors */
48: Mat *D; /* divided difference matrices */
49: PetscScalar *coeffD; /* coefficients for divided differences in split form */
50: PetscInt nshifts; /* provided number of shifts */
51: PetscScalar *shifts; /* user-provided shifts for the Rational Krylov variant */
52: PetscInt nshiftsw; /* actual number of shifts (1 if Krylov-Schur) */
53: PetscReal ddtol; /* tolerance for divided difference convergence */
54: PetscInt ddmaxit; /* maximum number of divided difference terms */
55: BV W; /* auxiliary BV object */
56: PetscReal keep; /* restart parameter */
57: PetscBool lock; /* locking/non-locking variant */
58: PetscBool trueres; /* whether the true residual norm must be computed */
59: PetscInt idxrk; /* index of next shift to use */
60: KSP *ksp; /* ksp array for storing shift factorizations */
61: Vec vrn; /* random vector with normally distributed value */
62: void *singularitiesctx;
63: PetscErrorCode (*computesingularities)(NEP,PetscInt*,PetscScalar*,void*);
64: } NEP_NLEIGS;
66: typedef struct {
67: PetscInt nmat;
68: PetscScalar coeff[MAX_NSHIFTS];
69: Mat A[MAX_NSHIFTS];
70: Vec t;
71: } ShellMatCtx;
75: PETSC_STATIC_INLINE PetscErrorCode NEPNLEIGSSetShifts(NEP nep)
76: {
77: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
80: if (!ctx->nshifts) {
81: ctx->shifts = &nep->target;
82: ctx->nshiftsw = 1;
83: } else ctx->nshiftsw = ctx->nshifts;
84: return(0);
85: }
89: static PetscErrorCode NEPNLEIGSBackTransform(PetscObject ob,PetscInt n,PetscScalar *valr,PetscScalar *vali)
90: {
91: NEP nep;
92: PetscInt j;
93: #if !defined(PETSC_USE_COMPLEX)
94: PetscScalar t;
95: #endif
98: nep = (NEP)ob;
99: #if !defined(PETSC_USE_COMPLEX)
100: for (j=0;j<n;j++) {
101: if (vali[j] == 0) valr[j] = 1.0 / valr[j] + nep->target;
102: else {
103: t = valr[j] * valr[j] + vali[j] * vali[j];
104: valr[j] = valr[j] / t + nep->target;
105: vali[j] = - vali[j] / t;
106: }
107: }
108: #else
109: for (j=0;j<n;j++) {
110: valr[j] = 1.0 / valr[j] + nep->target;
111: }
112: #endif
113: return(0);
114: }
118: static PetscErrorCode NEPNLEIGSLejaBagbyPoints(NEP nep)
119: {
121: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
122: PetscInt i,k,ndpt=NDPOINTS,ndptx=NDPOINTS;
123: PetscScalar *ds,*dsi,*dxi,*nrs,*nrxi,*s=ctx->s,*xi=ctx->xi,*beta=ctx->beta;
124: PetscReal maxnrs,minnrxi;
127: PetscMalloc5(ndpt+1,&ds,ndpt+1,&dsi,ndpt,&dxi,ndpt+1,&nrs,ndpt,&nrxi);
129: /* Discretize the target region boundary */
130: RGComputeContour(nep->rg,ndpt,ds,dsi);
131: #if !defined(PETSC_USE_COMPLEX)
132: for (i=0;i<ndpt;i++) if (dsi[i]!=0.0) break;
133: if (i<ndpt) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"NLEIGS with real arithmetic requires the target set to be included in the real axis");
134: #endif
135: /* Discretize the singularity region */
136: if (ctx->computesingularities) {
137: (ctx->computesingularities)(nep,&ndptx,dxi,ctx->singularitiesctx);
138: } else ndptx = 0;
140: /* Look for Leja-Bagby points in the discretization sets */
141: s[0] = ds[0];
142: xi[0] = (ndptx>0)?dxi[0]:PETSC_INFINITY;
143: beta[0] = 1.0; /* scaling factors are also computed here */
144: maxnrs = 0.0;
145: minnrxi = PETSC_MAX_REAL;
146: for (i=0;i<ndpt;i++) {
147: nrs[i] = (ds[i]-s[0])/(1.0-ds[i]/xi[0]);
148: if (PetscAbsScalar(nrs[i])>=maxnrs) {maxnrs = PetscAbsScalar(nrs[i]); s[1] = ds[i];}
149: }
150: for (i=1;i<ndptx;i++) {
151: nrxi[i] = (dxi[i]-s[0])/(1.0-dxi[i]/xi[0]);
152: if (PetscAbsScalar(nrxi[i])<=minnrxi) {minnrxi = PetscAbsScalar(nrxi[i]); xi[1] = dxi[i];}
153: }
154: if (ndptx<2) xi[1] = PETSC_INFINITY;
156: beta[1] = maxnrs;
157: for (k=2;k<ctx->ddmaxit;k++) {
158: maxnrs = 0.0;
159: minnrxi = PETSC_MAX_REAL;
160: for (i=0;i<ndpt;i++) {
161: nrs[i] *= ((ds[i]-s[k-1])/(1.0-ds[i]/xi[k-1]))/beta[k-1];
162: if (PetscAbsScalar(nrs[i])>maxnrs) {maxnrs = PetscAbsScalar(nrs[i]); s[k] = ds[i];}
163: }
164: if (ndptx>=k) {
165: for (i=1;i<ndptx;i++) {
166: nrxi[i] *= ((dxi[i]-s[k-1])/(1.0-dxi[i]/xi[k-1]))/beta[k-1];
167: if (PetscAbsScalar(nrxi[i])<minnrxi) {minnrxi = PetscAbsScalar(nrxi[i]); xi[k] = dxi[i];}
168: }
169: } else xi[k] = PETSC_INFINITY;
170: beta[k] = maxnrs;
171: }
172: PetscFree5(ds,dsi,dxi,nrs,nrxi);
173: return(0);
174: }
178: static PetscErrorCode NEPNLEIGSEvalNRTFunct(NEP nep,PetscInt k,PetscScalar sigma,PetscScalar *b)
179: {
180: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
181: PetscInt i;
182: PetscScalar *beta=ctx->beta,*s=ctx->s,*xi=ctx->xi;
185: b[0] = 1.0/beta[0];
186: for (i=0;i<k;i++) {
187: b[i+1] = ((sigma-s[i])*b[i])/(beta[i+1]*(1.0-sigma/xi[i]));
188: }
189: return(0);
190: }
194: static PetscErrorCode MatMult_Fun(Mat A,Vec x,Vec y)
195: {
197: ShellMatCtx *ctx;
198: PetscInt i;
201: MatShellGetContext(A,(void**)&ctx);
202: MatMult(ctx->A[0],x,y);
203: if (ctx->coeff[0]!=1.0) { VecScale(y,ctx->coeff[0]); }
204: for (i=1;i<ctx->nmat;i++) {
205: MatMult(ctx->A[i],x,ctx->t);
206: VecAXPY(y,ctx->coeff[i],ctx->t);
207: }
208: return(0);
209: }
213: static PetscErrorCode MatMultTranspose_Fun(Mat A,Vec x,Vec y)
214: {
216: ShellMatCtx *ctx;
217: PetscInt i;
220: MatShellGetContext(A,(void**)&ctx);
221: MatMultTranspose(ctx->A[0],x,y);
222: if (ctx->coeff[0]!=1.0) { VecScale(y,ctx->coeff[0]); }
223: for (i=1;i<ctx->nmat;i++) {
224: MatMultTranspose(ctx->A[i],x,ctx->t);
225: VecAXPY(y,ctx->coeff[i],ctx->t);
226: }
227: return(0);
228: }
232: static PetscErrorCode MatGetDiagonal_Fun(Mat A,Vec diag)
233: {
235: ShellMatCtx *ctx;
236: PetscInt i;
239: MatShellGetContext(A,(void**)&ctx);
240: MatGetDiagonal(ctx->A[0],diag);
241: if (ctx->coeff[0]!=1.0) { VecScale(diag,ctx->coeff[0]); }
242: for (i=1;i<ctx->nmat;i++) {
243: MatGetDiagonal(ctx->A[i],ctx->t);
244: VecAXPY(diag,ctx->coeff[i],ctx->t);
245: }
246: return(0);
247: }
251: static PetscErrorCode MatDuplicate_Fun(Mat A,MatDuplicateOption op,Mat *B)
252: {
253: PetscInt n,i;
254: ShellMatCtx *ctxnew,*ctx;
255: void (*fun)();
259: MatShellGetContext(A,(void**)&ctx);
260: PetscNew(&ctxnew);
261: ctxnew->nmat = ctx->nmat;
262: for (i=0;i<ctx->nmat;i++) {
263: PetscObjectReference((PetscObject)ctx->A[i]);
264: ctxnew->A[i] = ctx->A[i];
265: ctxnew->coeff[i] = ctx->coeff[i];
266: }
267: MatGetSize(ctx->A[0],&n,NULL);
268: VecDuplicate(ctx->t,&ctxnew->t);
269: MatCreateShell(PETSC_COMM_WORLD,n,n,n,n,(void*)ctxnew,B);
270: MatShellGetOperation(A,MATOP_MULT,&fun);
271: MatShellSetOperation(*B,MATOP_MULT,fun);
272: MatShellGetOperation(A,MATOP_MULT_TRANSPOSE,&fun);
273: MatShellSetOperation(*B,MATOP_MULT_TRANSPOSE,fun);
274: MatShellGetOperation(A,MATOP_GET_DIAGONAL,&fun);
275: MatShellSetOperation(*B,MATOP_GET_DIAGONAL,fun);
276: MatShellGetOperation(A,MATOP_DUPLICATE,&fun);
277: MatShellSetOperation(*B,MATOP_DUPLICATE,fun);
278: MatShellGetOperation(A,MATOP_DESTROY,&fun);
279: MatShellSetOperation(*B,MATOP_DESTROY,fun);
280: MatShellGetOperation(A,MATOP_AXPY,&fun);
281: MatShellSetOperation(*B,MATOP_AXPY,fun);
282: return(0);
283: }
287: static PetscErrorCode MatDestroy_Fun(Mat A)
288: {
289: ShellMatCtx *ctx;
291: PetscInt i;
294: if (A) {
295: MatShellGetContext(A,(void**)&ctx);
296: for (i=0;i<ctx->nmat;i++) {
297: MatDestroy(&ctx->A[i]);
298: }
299: VecDestroy(&ctx->t);
300: PetscFree(ctx);
301: }
302: return(0);
303: }
307: static PetscErrorCode MatAXPY_Fun(Mat Y,PetscScalar a,Mat X,MatStructure str)
308: {
309: ShellMatCtx *ctxY,*ctxX;
311: PetscInt i,j;
312: PetscBool found;
315: MatShellGetContext(Y,(void**)&ctxY);
316: MatShellGetContext(X,(void**)&ctxX);
317: for (i=0;i<ctxX->nmat;i++) {
318: found = PETSC_FALSE;
319: for (j=0;!found&&j<ctxY->nmat;j++) {
320: if (ctxX->A[i]==ctxY->A[j]) {
321: found = PETSC_TRUE;
322: ctxY->coeff[j] += a*ctxX->coeff[i];
323: }
324: }
325: if (!found) {
326: ctxY->coeff[ctxY->nmat] = a*ctxX->coeff[i];
327: ctxY->A[ctxY->nmat++] = ctxX->A[i];
328: PetscObjectReference((PetscObject)ctxX->A[i]);
329: }
330: }
331: return(0);
332: }
336: static PetscErrorCode MatScale_Fun(Mat M,PetscScalar a)
337: {
338: ShellMatCtx *ctx;
340: PetscInt i;
343: MatShellGetContext(M,(void**)&ctx);
344: for (i=0;i<ctx->nmat;i++) ctx->coeff[i] *= a;
345: return(0);
346: }
350: static PetscErrorCode NLEIGSMatToMatShellArray(Mat M,Mat *Ms)
351: {
353: ShellMatCtx *ctx;
354: PetscInt n;
355: PetscBool has;
356:
358: MatHasOperation(M,MATOP_DUPLICATE,&has);
359: if (!has) SETERRQ(PetscObjectComm((PetscObject)M),1,"MatDuplicate operation required");
360: PetscNew(&ctx);
361: MatDuplicate(M,MAT_COPY_VALUES,&ctx->A[0]);
362: ctx->nmat = 1;
363: ctx->coeff[0] = 1.0;
364: MatCreateVecs(M,&ctx->t,NULL);
365: MatGetSize(M,&n,NULL);
366: MatCreateShell(PetscObjectComm((PetscObject)M),n,n,n,n,(void*)ctx,Ms);
367: MatShellSetOperation(*Ms,MATOP_MULT,(void(*)())MatMult_Fun);
368: MatShellSetOperation(*Ms,MATOP_MULT_TRANSPOSE,(void(*)())MatMultTranspose_Fun);
369: MatShellSetOperation(*Ms,MATOP_GET_DIAGONAL,(void(*)())MatGetDiagonal_Fun);
370: MatShellSetOperation(*Ms,MATOP_DUPLICATE,(void(*)())MatDuplicate_Fun);
371: MatShellSetOperation(*Ms,MATOP_DESTROY,(void(*)())MatDestroy_Fun);
372: MatShellSetOperation(*Ms,MATOP_AXPY,(void(*)())MatAXPY_Fun);
373: MatShellSetOperation(*Ms,MATOP_SCALE,(void(*)())MatScale_Fun);
374: return(0);
375: }
379: static PetscErrorCode NEPNLEIGSNormEstimation(NEP nep,Mat M,PetscReal *norm,Vec *w)
380: {
381: PetscScalar *z,*x,*y;
382: PetscReal tr;
383: Vec X=w[0],Y=w[1];
384: PetscInt n,i;
385: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
386: PetscRandom rand;
388:
390: if (!ctx->vrn) {
391: /* generate a random vector with normally distributed entries with the Box-Muller transform */
392: BVGetRandomContext(nep->V,&rand);
393: MatCreateVecs(M,&ctx->vrn,NULL);
394: VecSetRandom(X,rand);
395: VecSetRandom(Y,rand);
396: VecGetLocalSize(ctx->vrn,&n);
397: VecGetArray(ctx->vrn,&z);
398: VecGetArray(X,&x);
399: VecGetArray(Y,&y);
400: for (i=0;i<n;i++) {
401: #if defined(PETSC_USE_COMPLEX)
402: z[i] = PetscSqrtReal(-2.0*PetscLogReal(PetscRealPart(x[i])))*PetscCosReal(2.0*PETSC_PI*PetscRealPart(y[i]));
403: z[i] += PETSC_i*(PetscSqrtReal(-2.0*PetscLogReal(PetscImaginaryPart(x[i])))*PetscCosReal(2.0*PETSC_PI*PetscImaginaryPart(y[i])));
404: #else
405: z[i] = PetscSqrtReal(-2.0*PetscLogReal(x[i]))*PetscCosReal(2.0*PETSC_PI*y[i]);
406: #endif
407: }
408: VecRestoreArray(ctx->vrn,&z);
409: VecRestoreArray(X,&x);
410: VecRestoreArray(Y,&y);
411: VecNorm(ctx->vrn,NORM_2,&tr);
412: VecScale(ctx->vrn,1/tr);
413: }
414: /* matrix-free norm estimator of Ipsen http://www4.ncsu.edu/~ipsen/ps/slides_ima.pdf */
415: MatGetSize(M,&n,NULL);
416: MatMult(M,ctx->vrn,X);
417: VecNorm(X,NORM_2,norm);
418: *norm *= PetscSqrtReal(n);
419: return(0);
420: }
424: static PetscErrorCode NEPNLEIGSDividedDifferences_split(NEP nep)
425: {
427: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
428: PetscInt k,j,i;
429: PetscReal norm0,norm,max;
430: PetscScalar *s=ctx->s,*beta=ctx->beta,*b,alpha,*coeffs;
431: Mat T,Ts;
432: PetscBool shell;
435: PetscMalloc1(nep->nt*ctx->ddmaxit,&ctx->coeffD);
436: PetscMalloc2(ctx->ddmaxit+1,&b,ctx->ddmaxit+1,&coeffs);
437: max = 0.0;
438: for (j=0;j<nep->nt;j++) {
439: FNEvaluateFunction(nep->f[j],s[0],ctx->coeffD+j);
440: ctx->coeffD[j] /= beta[0];
441: max = PetscMax(PetscAbsScalar(ctx->coeffD[j]),max);
442: }
443: norm0 = max;
444: ctx->nmat = ctx->ddmaxit;
445: for (k=1;k<ctx->ddmaxit;k++) {
446: NEPNLEIGSEvalNRTFunct(nep,k,s[k],b);
447: max = 0.0;
448: for (i=0;i<nep->nt;i++) {
449: FNEvaluateFunction(nep->f[i],s[k],ctx->coeffD+k*nep->nt+i);
450: for (j=0;j<k;j++) {
451: ctx->coeffD[k*nep->nt+i] -= b[j]*ctx->coeffD[i+nep->nt*j];
452: }
453: ctx->coeffD[k*nep->nt+i] /= b[k];
454: max = PetscMax(PetscAbsScalar(ctx->coeffD[k*nep->nt+i]),max);
455: }
456: norm = max;
457: if (norm/norm0 < ctx->ddtol) {
458: ctx->nmat = k+1;
459: break;
460: }
461: }
462: if (!ctx->ksp) { NEPNLEIGSGetKSPs(nep,&ctx->ksp); }
463: PetscObjectTypeCompare((PetscObject)nep->A[0],MATSHELL,&shell);
464: for (i=0;i<ctx->nshiftsw;i++) {
465: NEPNLEIGSEvalNRTFunct(nep,ctx->nmat,ctx->shifts[i],coeffs);
466: if (!shell) {
467: MatDuplicate(nep->A[0],MAT_COPY_VALUES,&T);
468: } else {
469: NLEIGSMatToMatShellArray(nep->A[0],&T);
470: }
471: alpha = 0.0;
472: for (j=0;j<ctx->nmat-1;j++) alpha += coeffs[j]*ctx->coeffD[j*nep->nt];
473: MatScale(T,alpha);
474: for (k=1;k<nep->nt;k++) {
475: alpha = 0.0;
476: for (j=0;j<ctx->nmat-1;j++) alpha += coeffs[j]*ctx->coeffD[j*nep->nt+k];
477: if (shell) {
478: NLEIGSMatToMatShellArray(nep->A[k],&Ts);
479: }
480: MatAXPY(T,alpha,shell?Ts:nep->A[k],nep->mstr);
481: if (shell) {
482: MatDestroy(&Ts);
483: }
484: }
485: KSPSetOperators(ctx->ksp[i],T,T);
486: KSPSetUp(ctx->ksp[i]);
487: MatDestroy(&T);
488: }
489: PetscFree2(b,coeffs);
490: return(0);
491: }
495: static PetscErrorCode NEPNLEIGSDividedDifferences_callback(NEP nep)
496: {
498: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
499: PetscInt k,j,i;
500: PetscReal norm0,norm;
501: PetscScalar *s=ctx->s,*beta=ctx->beta,*b,*coeffs;
502: Mat *D=ctx->D,T;
503: PetscBool shell,has,vec=PETSC_FALSE;
504: Vec w[2];
507: PetscMalloc2(ctx->ddmaxit+1,&b,ctx->ddmaxit+1,&coeffs);
508: T = nep->function;
509: NEPComputeFunction(nep,s[0],T,T);
510: PetscObjectTypeCompare((PetscObject)T,MATSHELL,&shell);
511: if (!shell) {
512: MatDuplicate(T,MAT_COPY_VALUES,&D[0]);
513: } else {
514: NLEIGSMatToMatShellArray(T,&D[0]);
515: }
516: if (beta[0]!=1.0) {
517: MatScale(D[0],1.0/beta[0]);
518: }
519: MatHasOperation(D[0],MATOP_NORM,&has);
520: if (has) {
521: MatNorm(D[0],NORM_FROBENIUS,&norm0);
522: } else {
523: MatCreateVecs(D[0],NULL,&w[0]);
524: VecDuplicate(w[0],&w[1]);
525: vec = PETSC_TRUE;
526: NEPNLEIGSNormEstimation(nep,D[0],&norm0,w);
527: }
528: ctx->nmat = ctx->ddmaxit;
529: for (k=1;k<ctx->ddmaxit;k++) {
530: NEPNLEIGSEvalNRTFunct(nep,k,s[k],b);
531: NEPComputeFunction(nep,s[k],T,T);
532: if (!shell) {
533: MatDuplicate(T,MAT_COPY_VALUES,&D[k]);
534: } else {
535: NLEIGSMatToMatShellArray(T,&D[k]);
536: }
537: for (j=0;j<k;j++) {
538: MatAXPY(D[k],-b[j],D[j],nep->mstr);
539: }
540: MatScale(D[k],1.0/b[k]);
541: MatHasOperation(D[k],MATOP_NORM,&has);
542: if (has) {
543: MatNorm(D[k],NORM_FROBENIUS,&norm);
544: } else {
545: if(!vec) {
546: MatCreateVecs(D[k],NULL,&w[0]);
547: VecDuplicate(w[0],&w[1]);
548: vec = PETSC_TRUE;
549: }
550: NEPNLEIGSNormEstimation(nep,D[k],&norm,w);
551: }
552: if (norm/norm0 < ctx->ddtol) {
553: ctx->nmat = k+1; /* increment (the last matrix is not used) */
554: MatDestroy(&D[k]);
555: break;
556: }
557: }
558: if (!ctx->ksp) { NEPNLEIGSGetKSPs(nep,&ctx->ksp); }
559: for (i=0;i<ctx->nshiftsw;i++) {
560: NEPNLEIGSEvalNRTFunct(nep,ctx->nmat,ctx->shifts[i],coeffs);
561: MatDuplicate(ctx->D[0],MAT_COPY_VALUES,&T);
562: if (coeffs[0]!=1.0) { MatScale(T,coeffs[0]); }
563: for (j=1;j<ctx->nmat-1;j++) {
564: MatAXPY(T,coeffs[j],ctx->D[j],nep->mstr);
565: }
566: KSPSetOperators(ctx->ksp[i],T,T);
567: KSPSetUp(ctx->ksp[i]);
568: MatDestroy(&T);
569: }
570: PetscFree2(b,coeffs);
571: if (vec) {
572: VecDestroy(&w[0]);
573: VecDestroy(&w[1]);
574: }
575: return(0);
576: }
580: static PetscErrorCode NEPNLEIGSRitzVector(NEP nep,PetscScalar *S,PetscInt ld,PetscInt nq,PetscScalar *H,PetscInt k,Vec t)
581: {
583: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
584: PetscInt deg=ctx->nmat-1,ldds,n;
585: PetscBLASInt nq_,lds_,n_,one=1,ldds_;
586: PetscScalar sone=1.0,zero=0.0,*x,*y,*X;
589: DSGetDimensions(nep->ds,&n,NULL,NULL,NULL,NULL);
590: PetscMalloc1(n+nq,&y);
591: x = y+nq;
592: DSGetLeadingDimension(nep->ds,&ldds);
593: PetscBLASIntCast(n,&n_);
594: PetscBLASIntCast(nq,&nq_);
595: PetscBLASIntCast(ldds,&ldds_);
596: PetscBLASIntCast(deg*ld,&lds_);
597: DSGetArray(nep->ds,DS_MAT_X,&X);
598: if (ctx->nshifts) PetscStackCall("BLASgemv",BLASgemv_("N",&n_,&n_,&sone,H,&ldds_,X+k*ldds,&one,&zero,x,&one));
599: else x = X+k*ldds;
600: PetscStackCall("BLASgemv",BLASgemv_("N",&nq_,&n_,&sone,S,&lds_,x,&one,&zero,y,&one));
601: DSRestoreArray(nep->ds,DS_MAT_X,&X);
602: BVSetActiveColumns(nep->V,0,nq);
603: BVMultVec(nep->V,1.0,0.0,t,y);
604: VecNormalize(t,NULL);
605: PetscFree(y);
606: return(0);
607: }
611: /*
612: NEPKrylovConvergence - This is the analogue to EPSKrylovConvergence.
613: */
614: static PetscErrorCode NEPNLEIGSKrylovConvergence(NEP nep,PetscScalar *S,PetscInt ld,PetscInt nq,PetscScalar *H,PetscBool getall,PetscInt kini,PetscInt nits,PetscScalar betak,PetscReal betah,PetscInt *kout,Vec *w)
615: {
617: PetscInt k,newk,marker,inside;
618: PetscScalar re,im;
619: PetscReal resnorm,tt;
620: PetscBool istrivial;
621: Vec t;
622: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
625: t = w[0];
626: RGIsTrivial(nep->rg,&istrivial);
627: marker = -1;
628: if (nep->trackall) getall = PETSC_TRUE;
629: for (k=kini;k<kini+nits;k++) {
630: /* eigenvalue */
631: re = nep->eigr[k];
632: im = nep->eigi[k];
633: if (!istrivial) {
634: if (!ctx->nshifts) {
635: NEPNLEIGSBackTransform((PetscObject)nep,1,&re,&im);
636: }
637: RGCheckInside(nep->rg,1,&re,&im,&inside);
638: if (marker==-1 && inside<0) marker = k;
639: }
640: newk = k;
641: DSVectors(nep->ds,DS_MAT_X,&newk,&resnorm);
642: tt = (ctx->nshifts)?SlepcAbsEigenvalue(betak-nep->eigr[k]*betah,nep->eigi[k]*betah):betah;
643: resnorm *= PetscAbsReal(tt);
644: /* error estimate */
645: (*nep->converged)(nep,nep->eigr[k],nep->eigi[k],resnorm,&nep->errest[k],nep->convergedctx);
646: if (ctx->trueres && (nep->errest[k] < nep->tol) ) {
647: /* check explicit residual */
648: NEPNLEIGSRitzVector(nep,S,ld,nq,H,k,t);
649: NEPComputeResidualNorm_Private(nep,re,t,w+1,&resnorm);
650: (*nep->converged)(nep,re,im,resnorm,&nep->errest[k],nep->convergedctx);
651: }
652: if (marker==-1 && nep->errest[k] >= nep->tol) marker = k;
653: if (newk==k+1) {
654: nep->errest[k+1] = nep->errest[k];
655: k++;
656: }
657: if (marker!=-1 && !getall) break;
658: }
659: if (marker!=-1) k = marker;
660: *kout = k;
661: return(0);
662: }
666: PetscErrorCode NEPSetUp_NLEIGS(NEP nep)
667: {
669: PetscInt k,in;
670: PetscScalar zero=0.0;
671: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
672: SlepcSC sc;
673: PetscBool istrivial;
676: NEPSetDimensions_Default(nep,nep->nev,&nep->ncv,&nep->mpd);
677: if (nep->ncv>nep->nev+nep->mpd) SETERRQ(PetscObjectComm((PetscObject)nep),1,"The value of ncv must not be larger than nev+mpd");
678: if (!nep->max_it) nep->max_it = PetscMax(5000,2*nep->n/nep->ncv);
679: if (!ctx->ddmaxit) ctx->ddmaxit = MAX_LBPOINTS;
680: RGIsTrivial(nep->rg,&istrivial);
681: if (istrivial) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"NEPNLEIGS requires a nontrivial region defining the target set");
682: RGCheckInside(nep->rg,1,&nep->target,&zero,&in);
683: if (in<0) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_SUP,"The target is not inside the target set");
684: if (!nep->which) nep->which = NEP_TARGET_MAGNITUDE;
686: /* Initialize the NLEIGS context structure */
687: k = ctx->ddmaxit;
688: PetscMalloc4(k,&ctx->s,k,&ctx->xi,k,&ctx->beta,k,&ctx->D);
689: nep->data = ctx;
690: if (nep->tol==PETSC_DEFAULT) nep->tol = SLEPC_DEFAULT_TOL;
691: if (ctx->ddtol==PETSC_DEFAULT) ctx->ddtol = nep->tol/10.0;
692: if (!ctx->keep) ctx->keep = 0.5;
694: /* Compute Leja-Bagby points and scaling values */
695: NEPNLEIGSLejaBagbyPoints(nep);
697: /* Compute the divided difference matrices */
698: if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
699: NEPNLEIGSDividedDifferences_split(nep);
700: } else {
701: NEPNLEIGSDividedDifferences_callback(nep);
702: }
703: NEPAllocateSolution(nep,ctx->nmat);
704: NEPSetWorkVecs(nep,4);
706: /* set-up DS and transfer split operator functions */
707: DSSetType(nep->ds,ctx->nshifts?DSGNHEP:DSNHEP);
708: DSAllocate(nep->ds,nep->ncv+1);
709: DSGetSlepcSC(nep->ds,&sc);
710: if (!ctx->nshifts) {
711: sc->map = NEPNLEIGSBackTransform;
712: DSSetExtraRow(nep->ds,PETSC_TRUE);
713: }
714: sc->mapobj = (PetscObject)nep;
715: sc->rg = nep->rg;
716: sc->comparison = nep->sc->comparison;
717: sc->comparisonctx = nep->sc->comparisonctx;
718: return(0);
719: }
723: /*
724: Norm of [sp;sq]
725: */
726: static PetscErrorCode NEPTOARSNorm2(PetscInt n,PetscScalar *S,PetscReal *norm)
727: {
729: PetscBLASInt n_,one=1;
732: PetscBLASIntCast(n,&n_);
733: *norm = BLASnrm2_(&n_,S,&one);
734: return(0);
735: }
739: /*
740: Computes GS orthogonalization [z;x] - [Sp;Sq]*y,
741: where y = ([Sp;Sq]'*[z;x]).
742: k: Column from S to be orthogonalized against previous columns.
743: Sq = Sp+ld
744: dim(work)=k;
745: */
746: static PetscErrorCode NEPTOAROrth2(NEP nep,PetscScalar *S,PetscInt ld,PetscInt deg,PetscInt k,PetscScalar *y,PetscReal *norm,PetscBool *lindep,PetscScalar *work)
747: {
749: PetscBLASInt n_,lds_,k_,one=1;
750: PetscScalar sonem=-1.0,sone=1.0,szero=0.0,*x0,*x,*c;
751: PetscInt i,lds=deg*ld,n;
752: PetscReal eta,onorm;
753:
755: BVGetOrthogonalization(nep->V,NULL,NULL,&eta,NULL);
756: n = k+deg-1;
757: PetscBLASIntCast(n,&n_);
758: PetscBLASIntCast(deg*ld,&lds_);
759: PetscBLASIntCast(k,&k_); /* Number of vectors to orthogonalize against them */
760: c = work;
761: x0 = S+k*lds;
762: PetscStackCall("BLASgemv",BLASgemv_("C",&n_,&k_,&sone,S,&lds_,x0,&one,&szero,y,&one));
763: for (i=1;i<deg;i++) {
764: x = S+i*ld+k*lds;
765: PetscStackCall("BLASgemv",BLASgemv_("C",&n_,&k_,&sone,S+i*ld,&lds_,x,&one,&sone,y,&one));
766: }
767: for (i=0;i<deg;i++) {
768: x= S+i*ld+k*lds;
769: PetscStackCall("BLASgemv",BLASgemv_("N",&n_,&k_,&sonem,S+i*ld,&lds_,y,&one,&sone,x,&one));
770: }
771: NEPTOARSNorm2(lds,S+k*lds,&onorm);
772: /* twice */
773: PetscStackCall("BLASgemv",BLASgemv_("C",&n_,&k_,&sone,S,&lds_,x0,&one,&szero,c,&one));
774: for (i=1;i<deg;i++) {
775: x = S+i*ld+k*lds;
776: PetscStackCall("BLASgemv",BLASgemv_("C",&n_,&k_,&sone,S+i*ld,&lds_,x,&one,&sone,c,&one));
777: }
778: for (i=0;i<deg;i++) {
779: x= S+i*ld+k*lds;
780: PetscStackCall("BLASgemv",BLASgemv_("N",&n_,&k_,&sonem,S+i*ld,&lds_,c,&one,&sone,x,&one));
781: }
782: for (i=0;i<k;i++) y[i] += c[i];
783: if (norm) {
784: NEPTOARSNorm2(lds,S+k*lds,norm);
785: if (lindep) *lindep = (*norm < eta * onorm)?PETSC_TRUE:PETSC_FALSE;
786: }
787: return(0);
788: }
792: /*
793: Extend the TOAR basis by applying the the matrix operator
794: over a vector which is decomposed on the TOAR way
795: Input:
796: - S,V: define the latest Arnoldi vector (nv vectors in V)
797: Output:
798: - t: new vector extending the TOAR basis
799: - r: temporally coefficients to compute the TOAR coefficients
800: for the new Arnoldi vector
801: Workspace: t_ (two vectors)
802: */
803: static PetscErrorCode NEPTOARExtendBasis(NEP nep,PetscInt idxrktg,PetscScalar *S,PetscInt ls,PetscInt nv,BV V,Vec t,PetscScalar *r,PetscInt lr,Vec *t_)
804: {
806: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
807: PetscInt deg=ctx->nmat-1,k,j;
808: Vec v=t_[0],q=t_[1],w;
809: PetscScalar *beta=ctx->beta,*s=ctx->s,*xi=ctx->xi,*coeffs,sigma;
812: if (!ctx->ksp) { NEPNLEIGSGetKSPs(nep,&ctx->ksp); }
813: sigma = ctx->shifts[idxrktg];
814: BVSetActiveColumns(nep->V,0,nv);
815: PetscMalloc1(ctx->nmat-1,&coeffs);
816: if (PetscAbsScalar(s[deg-2]-sigma)<100*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_SELF,1,"Breakdown in NLEIGS");
817: /* i-part stored in (i-1) position */
818: for (j=0;j<nv;j++) {
819: r[(deg-2)*lr+j] = (S[(deg-2)*ls+j]+(beta[deg-1]/xi[deg-2])*S[(deg-1)*ls+j])/(s[deg-2]-sigma);
820: }
821: BVSetActiveColumns(ctx->W,0,deg-1);
822: BVGetColumn(ctx->W,deg-2,&w);
823: BVMultVec(V,1.0,0.0,w,r+(deg-2)*lr);
824: BVRestoreColumn(ctx->W,deg-2,&w);
825: for (k=deg-2;k>0;k--) {
826: if (PetscAbsScalar(s[k-1]-sigma)<100*PETSC_MACHINE_EPSILON) SETERRQ(PETSC_COMM_SELF,1,"Breakdown in NLEIGS");
827: for (j=0;j<nv;j++) r[(k-1)*lr+j] = (S[(k-1)*ls+j]+(beta[k]/xi[k-1])*S[k*ls+j]-beta[k]*(1.0-sigma/xi[k-1])*r[(k)*lr+j])/(s[k-1]-sigma);
828: BVGetColumn(ctx->W,k-1,&w);
829: BVMultVec(V,1.0,0.0,w,r+(k-1)*lr);
830: BVRestoreColumn(ctx->W,k-1,&w);
831: }
832: if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
833: for (j=0;j<ctx->nmat-1;j++) coeffs[j] = ctx->coeffD[nep->nt*j];
834: BVMultVec(ctx->W,1.0,0.0,v,coeffs);
835: MatMult(nep->A[0],v,q);
836: for (k=1;k<nep->nt;k++) {
837: for (j=0;j<ctx->nmat-1;j++) coeffs[j] = ctx->coeffD[nep->nt*j+k];
838: BVMultVec(ctx->W,1.0,0,v,coeffs);
839: MatMult(nep->A[k],v,t);
840: VecAXPY(q,1.0,t);
841: }
842: KSPSolve(ctx->ksp[idxrktg],q,t);
843: VecScale(t,-1.0);
844: } else {
845: for (k=0;k<deg-1;k++) {
846: BVGetColumn(ctx->W,k,&w);
847: MatMult(ctx->D[k],w,q);
848: BVRestoreColumn(ctx->W,k,&w);
849: BVInsertVec(ctx->W,k,q);
850: }
851: for (j=0;j<ctx->nmat-1;j++) coeffs[j] = 1.0;
852: BVMultVec(ctx->W,1.0,0.0,q,coeffs);
853: KSPSolve(ctx->ksp[idxrktg],q,t);
854: VecScale(t,-1.0);
855: }
856: PetscFree(coeffs);
857: return(0);
858: }
862: /*
863: Compute TOAR coefficients of the blocks of the new Arnoldi vector computed
864: */
865: static PetscErrorCode NEPTOARCoefficients(NEP nep,PetscScalar sigma,PetscInt nv,PetscScalar *S,PetscInt ls,PetscScalar *r,PetscInt lr,PetscScalar *x,PetscScalar *work)
866: {
868: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
869: PetscInt k,j,d=ctx->nmat-1;
870: PetscScalar *t=work;
873: NEPNLEIGSEvalNRTFunct(nep,d-1,sigma,t);
874: for (k=0;k<d-1;k++) {
875: for (j=0;j<=nv;j++) r[k*lr+j] += t[k]*x[j];
876: }
877: for (j=0;j<=nv;j++) r[(d-1)*lr+j] = t[d-1]*x[j];
878: return(0);
879: }
883: /*
884: Compute continuation vector coefficients for the Rational-Krylov run.
885: dim(work) >= (end-ini)*(end-ini+1) + end+1 + 2*(end-ini+1), dim(t) = end.
886: */
887: static PetscErrorCode NEPNLEIGS_RKcontinuation(NEP nep,PetscInt ini,PetscInt end,PetscScalar *K,PetscScalar *H,PetscInt ld,PetscScalar sigma,PetscScalar *S,PetscInt lds,PetscScalar *cont,PetscScalar *t,PetscScalar *work)
888: {
889: #if defined(PETSC_MISSING_LAPACK_GEQRF) || defined(SLEPC_MISSING_LAPACK_LARF)
891: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"GEQRF/LARF - Lapack routines are unavailable");
892: #else
894: PetscScalar *x,*W,*tau,sone=1.0,szero=0.0;
895: PetscInt i,j,n1,n,nwu=0;
896: PetscBLASInt info,n_,n1_,one=1,dim,lds_;
897: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
900: if (!ctx->nshifts || !end) {
901: t[0] = 1;
902: PetscMemcpy(cont,S+end*lds,lds*sizeof(PetscScalar));
903: } else {
904: n = end-ini;
905: n1 = n+1;
906: x = work+nwu;
907: nwu += end+1;
908: tau = work+nwu;
909: nwu += n;
910: W = work+nwu;
911: nwu += n1*n;
912: for (j=ini;j<end;j++) {
913: for (i=ini;i<=end;i++) W[(j-ini)*n1+i-ini] = K[j*ld+i] -H[j*ld+i]*sigma;
914: }
915: PetscBLASIntCast(n,&n_);
916: PetscBLASIntCast(n1,&n1_);
917: PetscBLASIntCast(end+1,&dim);
918: PetscStackCallBLAS("LAPACKgeqrf",LAPACKgeqrf_(&n1_,&n_,W,&n1_,tau,work+nwu,&n1_,&info));
919: if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in Lapack xGEQRF %d",info);
920: for (i=0;i<end;i++) t[i] = 0.0;
921: t[end] = 1.0;
922: for (j=n-1;j>=0;j--) {
923: for (i=0;i<ini+j;i++) x[i] = 0.0;
924: x[ini+j] = 1.0;
925: for (i=j+1;i<n1;i++) x[i+ini] = W[i+n1*j];
926: tau[j] = PetscConj(tau[j]);
927: PetscStackCallBLAS("LAPACKlarf",LAPACKlarf_("L",&dim,&one,x,&one,tau+j,t,&dim,work+nwu));
928: }
929: PetscBLASIntCast(lds,&lds_);
930: PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&lds_,&n1_,&sone,S,&lds_,t,&one,&szero,cont,&one));
931: }
932: return(0);
933: #endif
934: }
938: /*
939: Compute a run of Arnoldi iterations
940: */
941: static PetscErrorCode NEPNLEIGSTOARrun(NEP nep,PetscInt *nq,PetscScalar *S,PetscInt ld,PetscScalar *K,PetscScalar *H,PetscInt ldh,BV V,PetscInt k,PetscInt *M,PetscBool *breakdown,Vec *t_)
942: {
944: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
945: PetscInt i,j,p,m=*M,lwa,deg=ctx->nmat-1,lds=ld*deg,nqt=*nq;
946: Vec t=t_[0];
947: PetscReal norm;
948: PetscScalar *x,*work,*tt,sigma,*cont;
949: PetscBool lindep;
952: lwa = PetscMax(ld,deg)+(m+1)*(m+1)+4*(m+1);
953: PetscMalloc4(ld,&x,lwa,&work,m+1,&tt,lds,&cont);
954: for (j=k;j<m;j++) {
955: sigma = ctx->shifts[(++(ctx->idxrk))%ctx->nshiftsw];
957: /* Continuation vector */
958: NEPNLEIGS_RKcontinuation(nep,0,j,K,H,ldh,sigma,S,lds,cont,tt,work);
959:
960: /* apply operator */
961: BVGetColumn(nep->V,nqt,&t);
962: NEPTOARExtendBasis(nep,(ctx->idxrk)%ctx->nshiftsw,cont,ld,nqt,V,t,S+(j+1)*lds,ld,t_+1);
963: BVRestoreColumn(nep->V,nqt,&t);
965: /* orthogonalize */
966: BVOrthogonalizeColumn(nep->V,nqt,x,&norm,&lindep);
967: if (!lindep) {
968: x[nqt] = norm;
969: BVScaleColumn(nep->V,nqt,1.0/norm);
970: nqt++;
971: } else x[nqt] = 0.0;
973: NEPTOARCoefficients(nep,sigma,*nq,cont,ld,S+(j+1)*lds,ld,x,work);
975: /* Level-2 orthogonalization */
976: NEPTOAROrth2(nep,S,ld,deg,j+1,H+j*ldh,&norm,breakdown,work);
977: H[j+1+ldh*j] = norm;
978: if (ctx->nshifts) {
979: for (i=0;i<=j;i++) K[i+ldh*j] = sigma*H[i+ldh*j] + tt[i];
980: K[j+1+ldh*j] = sigma*H[j+1+ldh*j];
981: }
982: *nq = nqt;
983: if (*breakdown) {
984: *M = j+1;
985: break;
986: }
987: for (p=0;p<deg;p++) {
988: for (i=0;i<=j+deg;i++) {
989: S[i+p*ld+(j+1)*lds] /= norm;
990: }
991: }
992: }
993: PetscFree4(x,work,tt,cont);
994: return(0);
995: }
999: /* dim(work)=5*ld*lds dim(rwork)=6*n */
1000: static PetscErrorCode NEPTOARTrunc(NEP nep,PetscScalar *S,PetscInt ld,PetscInt deg,PetscInt *nq,PetscInt cs1,PetscScalar *work,PetscReal *rwork)
1001: {
1003: PetscInt lwa,nwu=0,nrwu=0;
1004: PetscInt j,i,n,lds=deg*ld,rk=0,rs1;
1005: PetscScalar *M,*V,*pU,t;
1006: PetscReal *sg,tol;
1007: PetscBLASInt cs1_,rs1_,cs1tdeg,n_,info,lw_;
1008: Mat U;
1011: rs1 = *nq;
1012: n = (rs1>deg*cs1)?deg*cs1:rs1;
1013: lwa = 5*ld*lds;
1014: M = work+nwu;
1015: nwu += rs1*cs1*deg;
1016: sg = rwork+nrwu;
1017: nrwu += n;
1018: pU = work+nwu;
1019: nwu += rs1*n;
1020: V = work+nwu;
1021: nwu += deg*cs1*n;
1022: for (i=0;i<cs1;i++) {
1023: for (j=0;j<deg;j++) {
1024: PetscMemcpy(M+(i+j*cs1)*rs1,S+i*lds+j*ld,rs1*sizeof(PetscScalar));
1025: }
1026: }
1027: PetscBLASIntCast(n,&n_);
1028: PetscBLASIntCast(cs1,&cs1_);
1029: PetscBLASIntCast(rs1,&rs1_);
1030: PetscBLASIntCast(cs1*deg,&cs1tdeg);
1031: PetscBLASIntCast(lwa-nwu,&lw_);
1032: #if !defined (PETSC_USE_COMPLEX)
1033: PetscStackCall("LAPACKgesvd",LAPACKgesvd_("S","S",&rs1_,&cs1tdeg,M,&rs1_,sg,pU,&rs1_,V,&n_,work+nwu,&lw_,&info));
1034: #else
1035: PetscStackCall("LAPACKgesvd",LAPACKgesvd_("S","S",&rs1_,&cs1tdeg,M,&rs1_,sg,pU,&rs1_,V,&n_,work+nwu,&lw_,rwork+nrwu,&info));
1036: #endif
1037: if (info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in Lapack xGESVD %d",info);
1038:
1039: /* Update the corresponding vectors V(:,idx) = V*Q(:,idx) */
1040: MatCreateSeqDense(PETSC_COMM_SELF,rs1,cs1+deg-1,pU,&U);
1041: BVSetActiveColumns(nep->V,0,rs1);
1042: BVMultInPlace(nep->V,U,0,cs1+deg-1);
1043: BVSetActiveColumns(nep->V,0,cs1+deg-1);
1044: MatDestroy(&U);
1045: tol = PetscMax(rs1,deg*cs1)*PETSC_MACHINE_EPSILON*sg[0];
1046: for (i=0;i<PetscMin(n_,cs1tdeg);i++) if (sg[i]>tol) rk++;
1047: rk = PetscMin(cs1+deg-1,rk);
1048:
1049: /* Update S */
1050: PetscMemzero(S,lds*ld*sizeof(PetscScalar));
1051: for (i=0;i<rk;i++) {
1052: t = sg[i];
1053: PetscStackCall("BLASscal",BLASscal_(&cs1tdeg,&t,V+i,&n_));
1054: }
1055: for (j=0;j<cs1;j++) {
1056: for (i=0;i<deg;i++) {
1057: PetscMemcpy(S+j*lds+i*ld,V+(cs1*i+j)*n,rk*sizeof(PetscScalar));
1058: }
1059: }
1060: *nq = rk;
1061: return(0);
1062: }
1066: /*
1067: S <- S*Q
1068: columns s-s+ncu of S
1069: rows 0-sr of S
1070: size(Q) qr x ncu
1071: dim(work)=sr*ncu
1072: */
1073: static PetscErrorCode NEPTOARSupdate(PetscScalar *S,PetscInt ld,PetscInt deg,PetscInt sr,PetscInt s,PetscInt ncu,PetscInt qr,PetscScalar *Q,PetscInt ldq,PetscScalar *work)
1074: {
1076: PetscScalar a=1.0,b=0.0;
1077: PetscBLASInt sr_,ncu_,ldq_,lds_,qr_;
1078: PetscInt j,lds=deg*ld,i;
1081: PetscBLASIntCast(sr,&sr_);
1082: PetscBLASIntCast(qr,&qr_);
1083: PetscBLASIntCast(ncu,&ncu_);
1084: PetscBLASIntCast(lds,&lds_);
1085: PetscBLASIntCast(ldq,&ldq_);
1086: for (i=0;i<deg;i++) {
1087: PetscStackCall("BLASgemm",BLASgemm_("N","N",&sr_,&ncu_,&qr_,&a,S+i*ld,&lds_,Q,&ldq_,&b,work,&sr_));
1088: for (j=0;j<ncu;j++) {
1089: PetscMemcpy(S+lds*(s+j)+i*ld,work+j*sr,sr*sizeof(PetscScalar));
1090: }
1091: }
1092: return(0);
1093: }
1097: PetscErrorCode NEPSolve_NLEIGS(NEP nep)
1098: {
1100: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
1101: PetscInt i,j,k=0,l,nv=0,ld,lds,off,ldds,rs1,nq=0,newn;
1102: PetscInt lwa,lrwa,nwu=0,nrwu=0,deg=ctx->nmat-1,nconv=0;
1103: PetscScalar *S,*Q,*work,*H,*pU,*K,betak=0,*Hc;
1104: PetscReal betah,norm,*rwork;
1105: PetscBool breakdown=PETSC_FALSE,lindep;
1106: Mat U;
1109: ld = nep->ncv+deg;
1110: lds = deg*ld;
1111: lwa = (deg+6)*ld*lds;
1112: lrwa = 7*lds;
1113: DSGetLeadingDimension(nep->ds,&ldds);
1114: PetscMalloc4(lwa,&work,lrwa,&rwork,lds*ld,&S,ldds*ldds,&Hc);
1115: PetscMemzero(S,lds*ld*sizeof(PetscScalar));
1116: BVDuplicateResize(nep->V,PetscMax(nep->nt-1,ctx->nmat-1),&ctx->W);
1118: /* Get the starting vector */
1119: for (i=0;i<deg;i++) {
1120: BVSetRandomColumn(nep->V,i);
1121: BVOrthogonalizeColumn(nep->V,i,S+i*ld,&norm,&lindep);
1122: if (!lindep) {
1123: BVScaleColumn(nep->V,i,1/norm);
1124: S[i+i*ld] = norm;
1125: nq++;
1126: }
1127: }
1128: if (!nq) SETERRQ(PetscObjectComm((PetscObject)nep),1,"NEP: Problem with initial vector");
1129: NEPTOARSNorm2(lds,S,&norm);
1130: for (j=0;j<deg;j++) {
1131: for (i=0;i<=j;i++) S[i+j*ld] /= norm;
1132: }
1134: /* Restart loop */
1135: l = 0;
1136: while (nep->reason == NEP_CONVERGED_ITERATING) {
1137: nep->its++;
1138:
1139: /* Compute an nv-step Krylov relation */
1140: nv = PetscMin(nep->nconv+nep->mpd,nep->ncv);
1141: if (ctx->nshifts) { DSGetArray(nep->ds,DS_MAT_A,&K); }
1142: DSGetArray(nep->ds,ctx->nshifts?DS_MAT_B:DS_MAT_A,&H);
1143: NEPNLEIGSTOARrun(nep,&nq,S,ld,K,H,ldds,nep->V,nep->nconv+l,&nv,&breakdown,nep->work);
1144: betah = PetscAbsScalar(H[(nv-1)*ldds+nv]);
1145: DSRestoreArray(nep->ds,ctx->nshifts?DS_MAT_B:DS_MAT_A,&H);
1146: if (ctx->nshifts) {
1147: betak = K[(nv-1)*ldds+nv];
1148: DSRestoreArray(nep->ds,DS_MAT_A,&K);
1149: }
1150: DSSetDimensions(nep->ds,nv,0,nep->nconv,nep->nconv+l);
1151: if (l==0) {
1152: DSSetState(nep->ds,DS_STATE_INTERMEDIATE);
1153: } else {
1154: DSSetState(nep->ds,DS_STATE_RAW);
1155: }
1157: /* Solve projected problem */
1158: if (ctx->nshifts) {
1159: DSGetArray(nep->ds,DS_MAT_B,&H);
1160: PetscMemcpy(Hc,H,ldds*ldds*sizeof(PetscScalar));
1161: DSRestoreArray(nep->ds,DS_MAT_B,&H);
1162: }
1163: DSSolve(nep->ds,nep->eigr,nep->eigi);
1164: DSSort(nep->ds,nep->eigr,nep->eigi,NULL,NULL,NULL);
1165: if (!ctx->nshifts) {
1166: DSUpdateExtraRow(nep->ds);
1167: }
1169: /* Check convergence */
1170: NEPNLEIGSKrylovConvergence(nep,S,ld,nq,Hc,PETSC_FALSE,nep->nconv,nv-nep->nconv,betak,betah,&k,nep->work);
1171: (*nep->stopping)(nep,nep->its,nep->max_it,k,nep->nev,&nep->reason,nep->stoppingctx);
1172: nconv = k;
1174: /* Update l */
1175: if (nep->reason != NEP_CONVERGED_ITERATING || breakdown) l = 0;
1176: else {
1177: l = PetscMax(1,(PetscInt)((nv-k)*ctx->keep));
1178: if (!breakdown) {
1179: /* Prepare the Rayleigh quotient for restart */
1180: if (!ctx->nshifts) {
1181: DSTruncate(nep->ds,k+l);
1182: DSGetDimensions(nep->ds,&newn,NULL,NULL,NULL,NULL);
1183: l = newn-k;
1184: } else {
1185: DSGetArray(nep->ds,DS_MAT_Q,&Q);
1186: DSGetArray(nep->ds,DS_MAT_B,&H);
1187: DSGetArray(nep->ds,DS_MAT_A,&K);
1188: for (i=ctx->lock?k:0;i<k+l;i++) {
1189: H[k+l+i*ldds] = betah*Q[nv-1+i*ldds];
1190: K[k+l+i*ldds] = betak*Q[nv-1+i*ldds];
1191: }
1192: DSRestoreArray(nep->ds,DS_MAT_B,&H);
1193: DSRestoreArray(nep->ds,DS_MAT_A,&K);
1194: DSRestoreArray(nep->ds,DS_MAT_Q,&Q);
1195: DSSetDimensions(nep->ds,k+l,0,nep->nconv,0);
1196: }
1197: }
1198: }
1199: if (!ctx->lock && l>0) { l += k; k = 0; }
1201: /* Update S */
1202: off = nep->nconv*ldds;
1203: DSGetArray(nep->ds,ctx->nshifts?DS_MAT_Z:DS_MAT_Q,&Q);
1204: NEPTOARSupdate(S,ld,deg,nq,nep->nconv,k+l-nep->nconv,nv,Q+off,ldds,work+nwu);
1205: DSRestoreArray(nep->ds,ctx->nshifts?DS_MAT_Z:DS_MAT_Q,&Q);
1207: /* Copy last column of S */
1208: PetscMemcpy(S+lds*(k+l),S+lds*nv,lds*sizeof(PetscScalar));
1209: if (nep->reason == NEP_CONVERGED_ITERATING) {
1210: if (breakdown) {
1212: /* Stop if breakdown */
1213: PetscInfo2(nep,"Breakdown (it=%D norm=%g)\n",nep->its,(double)betah);
1214: nep->reason = NEP_DIVERGED_BREAKDOWN;
1215: } else {
1216: /* Truncate S */
1217: NEPTOARTrunc(nep,S,ld,deg,&nq,k+l+1,work+nwu,rwork+nrwu);
1218: }
1219: }
1220: nep->nconv = k;
1221: NEPMonitor(nep,nep->its,nconv,nep->eigr,nep->eigi,nep->errest,nv);
1222: }
1223: nep->nconv = nconv;
1224: if (nep->nconv>0) {
1225: /* Extract invariant pair */
1226: NEPTOARTrunc(nep,S,ld,deg,&nq,nep->nconv,work+nwu,rwork+nrwu);
1227: /* Update vectors V = V*S or V=V*S*H */
1228: rs1 = nep->nconv;
1229: if (ctx->nshifts) {
1230: DSGetArray(nep->ds,DS_MAT_B,&H);
1231: NEPTOARSupdate(S,ld,deg,rs1,0,nep->nconv,nep->nconv,H,ldds,work+nwu);
1232: DSRestoreArray(nep->ds,DS_MAT_B,&H);
1233: }
1234: PetscMalloc1(rs1*nep->nconv,&pU);
1235: for (i=0;i<nep->nconv;i++) {
1236: PetscMemcpy(pU+i*rs1,S+i*lds,rs1*sizeof(PetscScalar));
1237: }
1238: MatCreateSeqDense(PETSC_COMM_SELF,rs1,nep->nconv,pU,&U);
1239: BVSetActiveColumns(nep->V,0,rs1);
1240: BVMultInPlace(nep->V,U,0,nep->nconv);
1241: BVSetActiveColumns(nep->V,0,nep->nconv);
1242: MatDestroy(&U);
1243: PetscFree(pU);
1244: }
1245: /* truncate Schur decomposition and change the state to raw so that
1246: DSVectors() computes eigenvectors from scratch */
1247: DSSetDimensions(nep->ds,nep->nconv,0,0,0);
1248: DSSetState(nep->ds,DS_STATE_RAW);
1250: PetscFree4(work,rwork,S,Hc);
1251: /* Map eigenvalues back to the original problem */
1252: if (!ctx->nshifts) {
1253: NEPNLEIGSBackTransform((PetscObject)nep,nep->nconv,nep->eigr,nep->eigi);
1254: }
1255: BVDestroy(&ctx->W);
1256: return(0);
1257: }
1261: static PetscErrorCode NEPNLEIGSSetSingularitiesFunction_NLEIGS(NEP nep,PetscErrorCode (*fun)(NEP,PetscInt*,PetscScalar*,void*),void *ctx)
1262: {
1263: NEP_NLEIGS *nepctx=(NEP_NLEIGS*)nep->data;
1266: if (fun) nepctx->computesingularities = fun;
1267: if (ctx) nepctx->singularitiesctx = ctx;
1268: return(0);
1269: }
1273: /*@C
1274: NEPNLEIGSSetSingularitiesFunction - Sets a user function to compute a discretization
1275: of the singularity set (where T(.) is not analytic).
1277: Logically Collective on NEP
1279: Input Parameters:
1280: + nep - the NEP context
1281: . fun - user function (if NULL then NEP retains any previously set value)
1282: - ctx - [optional] user-defined context for private data for the function
1283: (may be NULL, in which case NEP retains any previously set value)
1285: Calling Sequence of fun:
1286: $ fun(NEP nep,PetscInt *maxnp,PetscScalar *xi,void *ctx)
1288: + nep - the NEP context
1289: . maxnp - on input number of requested points in the discretization (can be set)
1290: . xi - computed values of the discretization
1291: - ctx - optional context, as set by NEPNLEIGSSetSingularitiesFunction()
1293: Note:
1294: The user-defined function can set a smaller value of maxnp if necessary.
1296: Level: intermediate
1298: .seealso: NEPNLEIGSGetSingularitiesFunction()
1299: @*/
1300: PetscErrorCode NEPNLEIGSSetSingularitiesFunction(NEP nep,PetscErrorCode (*fun)(NEP,PetscInt*,PetscScalar*,void*),void *ctx)
1301: {
1306: PetscTryMethod(nep,"NEPNLEIGSSetSingularitiesFunction_C",(NEP,PetscErrorCode(*)(NEP,PetscInt*,PetscScalar*,void*),void*),(nep,fun,ctx));
1307: return(0);
1308: }
1312: static PetscErrorCode NEPNLEIGSGetSingularitiesFunction_NLEIGS(NEP nep,PetscErrorCode (**fun)(NEP,PetscInt*,PetscScalar*,void*),void **ctx)
1313: {
1314: NEP_NLEIGS *nepctx=(NEP_NLEIGS*)nep->data;
1317: if (fun) *fun = nepctx->computesingularities;
1318: if (ctx) *ctx = nepctx->singularitiesctx;
1319: return(0);
1320: }
1324: /*@C
1325: NEPNLEIGSGetSingularitiesFunction - Returns the Function and optionally the user
1326: provided context for computing a discretization of the singularity set.
1328: Not Collective
1330: Input Parameter:
1331: . nep - the nonlinear eigensolver context
1333: Output Parameters:
1334: + fun - location to put the function (or NULL)
1335: - ctx - location to stash the function context (or NULL)
1337: Level: advanced
1339: .seealso: NEPNLEIGSSetSingularitiesFunction()
1340: @*/
1341: PetscErrorCode NEPNLEIGSGetSingularitiesFunction(NEP nep,PetscErrorCode (**fun)(NEP,PetscInt*,PetscScalar*,void*),void **ctx)
1342: {
1347: PetscUseMethod(nep,"NEPNLEIGSGetSingularitiesFunction_C",(NEP,PetscErrorCode(**)(NEP,PetscInt*,PetscScalar*,void*),void**),(nep,fun,ctx));
1348: return(0);
1349: }
1353: static PetscErrorCode NEPNLEIGSSetRestart_NLEIGS(NEP nep,PetscReal keep)
1354: {
1355: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1358: if (keep==PETSC_DEFAULT) ctx->keep = 0.5;
1359: else {
1360: if (keep<0.1 || keep>0.9) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"The keep argument must be in the range [0.1,0.9]");
1361: ctx->keep = keep;
1362: }
1363: return(0);
1364: }
1368: /*@
1369: NEPNLEIGSSetRestart - Sets the restart parameter for the NLEIGS
1370: method, in particular the proportion of basis vectors that must be kept
1371: after restart.
1373: Logically Collective on NEP
1375: Input Parameters:
1376: + nep - the nonlinear eigensolver context
1377: - keep - the number of vectors to be kept at restart
1379: Options Database Key:
1380: . -nep_nleigs_restart - Sets the restart parameter
1382: Notes:
1383: Allowed values are in the range [0.1,0.9]. The default is 0.5.
1385: Level: advanced
1387: .seealso: NEPNLEIGSGetRestart()
1388: @*/
1389: PetscErrorCode NEPNLEIGSSetRestart(NEP nep,PetscReal keep)
1390: {
1396: PetscTryMethod(nep,"NEPNLEIGSSetRestart_C",(NEP,PetscReal),(nep,keep));
1397: return(0);
1398: }
1402: static PetscErrorCode NEPNLEIGSGetRestart_NLEIGS(NEP nep,PetscReal *keep)
1403: {
1404: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1407: *keep = ctx->keep;
1408: return(0);
1409: }
1413: /*@
1414: NEPNLEIGSGetRestart - Gets the restart parameter used in the NLEIGS method.
1416: Not Collective
1418: Input Parameter:
1419: . nep - the nonlinear eigensolver context
1421: Output Parameter:
1422: . keep - the restart parameter
1424: Level: advanced
1426: .seealso: NEPNLEIGSSetRestart()
1427: @*/
1428: PetscErrorCode NEPNLEIGSGetRestart(NEP nep,PetscReal *keep)
1429: {
1435: PetscUseMethod(nep,"NEPNLEIGSGetRestart_C",(NEP,PetscReal*),(nep,keep));
1436: return(0);
1437: }
1441: static PetscErrorCode NEPNLEIGSSetLocking_NLEIGS(NEP nep,PetscBool lock)
1442: {
1443: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1446: ctx->lock = lock;
1447: return(0);
1448: }
1452: /*@
1453: NEPNLEIGSSetLocking - Choose between locking and non-locking variants of
1454: the NLEIGS method.
1456: Logically Collective on NEP
1458: Input Parameters:
1459: + nep - the nonlinear eigensolver context
1460: - lock - true if the locking variant must be selected
1462: Options Database Key:
1463: . -nep_nleigs_locking - Sets the locking flag
1465: Notes:
1466: The default is to lock converged eigenpairs when the method restarts.
1467: This behaviour can be changed so that all directions are kept in the
1468: working subspace even if already converged to working accuracy (the
1469: non-locking variant).
1471: Level: advanced
1473: .seealso: NEPNLEIGSGetLocking()
1474: @*/
1475: PetscErrorCode NEPNLEIGSSetLocking(NEP nep,PetscBool lock)
1476: {
1482: PetscTryMethod(nep,"NEPNLEIGSSetLocking_C",(NEP,PetscBool),(nep,lock));
1483: return(0);
1484: }
1488: static PetscErrorCode NEPNLEIGSGetLocking_NLEIGS(NEP nep,PetscBool *lock)
1489: {
1490: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1493: *lock = ctx->lock;
1494: return(0);
1495: }
1499: /*@
1500: NEPNLEIGSGetLocking - Gets the locking flag used in the NLEIGS method.
1502: Not Collective
1504: Input Parameter:
1505: . nep - the nonlinear eigensolver context
1507: Output Parameter:
1508: . lock - the locking flag
1510: Level: advanced
1512: .seealso: NEPNLEIGSSetLocking()
1513: @*/
1514: PetscErrorCode NEPNLEIGSGetLocking(NEP nep,PetscBool *lock)
1515: {
1521: PetscUseMethod(nep,"NEPNLEIGSGetLocking_C",(NEP,PetscBool*),(nep,lock));
1522: return(0);
1523: }
1527: static PetscErrorCode NEPNLEIGSSetInterpolation_NLEIGS(NEP nep,PetscReal tol,PetscInt maxits)
1528: {
1529: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1532: if (tol == PETSC_DEFAULT) {
1533: ctx->ddtol = PETSC_DEFAULT;
1534: nep->state = NEP_STATE_INITIAL;
1535: } else {
1536: if (tol <= 0.0) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of tol. Must be > 0");
1537: ctx->ddtol = tol;
1538: }
1539: if (maxits == PETSC_DEFAULT || maxits == PETSC_DECIDE) {
1540: ctx->ddmaxit = 0;
1541: nep->state = NEP_STATE_INITIAL;
1542: } else {
1543: if (maxits <= 0) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_OUTOFRANGE,"Illegal value of maxits. Must be > 0");
1544: ctx->ddmaxit = maxits;
1545: }
1546: return(0);
1547: }
1551: /*@
1552: NEPNLEIGSSetInterpolation - Sets the tolerance and maximum iteration count used
1553: by the NLEIGS method when building the interpolation via divided differences.
1555: Logically Collective on NEP
1557: Input Parameters:
1558: + nep - the nonlinear eigensolver context
1559: . tol - the convergence tolerance
1560: - maxits - maximum number of iterations to use
1562: Options Database Key:
1563: + -nep_nleigs_interpolation_tol <tol> - Sets the convergence tolerance
1564: - -nep_nleigs_interpolation_max_it <maxits> - Sets the maximum number of iterations
1566: Notes:
1567: Use PETSC_DEFAULT for either argument to assign a reasonably good value.
1569: Level: advanced
1571: .seealso: NEPNLEIGSGetInterpolation()
1572: @*/
1573: PetscErrorCode NEPNLEIGSSetInterpolation(NEP nep,PetscReal tol,PetscInt maxits)
1574: {
1581: PetscTryMethod(nep,"NEPNLEIGSSetInterpolation_C",(NEP,PetscReal,PetscInt),(nep,tol,maxits));
1582: return(0);
1583: }
1587: static PetscErrorCode NEPNLEIGSGetInterpolation_NLEIGS(NEP nep,PetscReal *tol,PetscInt *maxits)
1588: {
1589: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1592: if (tol) *tol = ctx->ddtol;
1593: if (maxits) *maxits = ctx->ddmaxit;
1594: return(0);
1595: }
1599: /*@
1600: NEPNLEIGSGetInterpolation - Gets the tolerance and maximum iteration count used
1601: by the NLEIGS method when building the interpolation via divided differences.
1603: Not Collective
1605: Input Parameter:
1606: . nep - the nonlinear eigensolver context
1608: Output Parameter:
1609: + tol - the convergence tolerance
1610: - maxits - maximum number of iterations
1612: Level: advanced
1614: .seealso: NEPNLEIGSSetInterpolation()
1615: @*/
1616: PetscErrorCode NEPNLEIGSGetInterpolation(NEP nep,PetscReal *tol,PetscInt *maxits)
1617: {
1622: PetscTryMethod(nep,"NEPNLEIGSGetInterpolation_C",(NEP,PetscReal*,PetscInt*),(nep,tol,maxits));
1623: return(0);
1624: }
1628: static PetscErrorCode NEPNLEIGSSetTrueResidual_NLEIGS(NEP nep,PetscBool trueres)
1629: {
1630: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1633: ctx->trueres = trueres;
1634: return(0);
1635: }
1639: /*@
1640: NEPNLEIGSSetTrueResidual - Specifies if the solver must compute the true residual
1641: explicitly or not.
1643: Logically Collective on NEP
1645: Input Parameters:
1646: + nep - the nonlinear eigensolver context
1647: - trueres - whether true residuals are required or not
1649: Options Database Key:
1650: . -nep_nleigs_true_residual <boolean> - Sets/resets the boolean flag 'trueres'
1652: Notes:
1653: If the user sets trueres=PETSC_TRUE then the solver explicitly computes
1654: the true residual norm for each eigenpair approximation, and uses it for
1655: convergence testing. The default is to use the cheaper approximation
1656: available from the (rational) Krylov iteration.
1658: Level: advanced
1660: .seealso: NEPNLEIGSGetTrueResidual()
1661: @*/
1662: PetscErrorCode NEPNLEIGSSetTrueResidual(NEP nep,PetscBool trueres)
1663: {
1669: PetscTryMethod(nep,"NEPNLEIGSSetTrueResidual_C",(NEP,PetscBool),(nep,trueres));
1670: return(0);
1671: }
1675: static PetscErrorCode NEPNLEIGSGetTrueResidual_NLEIGS(NEP nep,PetscBool *trueres)
1676: {
1677: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1680: *trueres = ctx->trueres;
1681: return(0);
1682: }
1686: /*@
1687: NEPNLEIGSGetTrueResidual - Returns the flag indicating whether true
1688: residuals must be computed explicitly or not.
1690: Not Collective
1692: Input Parameter:
1693: . nep - the nonlinear eigensolver context
1695: Output Parameter:
1696: . trueres - the returned flag
1698: Level: advanced
1700: .seealso: NEPNLEIGSSetTrueResidual()
1701: @*/
1702: PetscErrorCode NEPNLEIGSGetTrueResidual(NEP nep,PetscBool *trueres)
1703: {
1709: PetscTryMethod(nep,"NEPNLEIGSGetTrueResidual_C",(NEP,PetscBool*),(nep,trueres));
1710: return(0);
1711: }
1715: static PetscErrorCode NEPNLEIGSSetRKShifts_NLEIGS(NEP nep,PetscInt ns,PetscScalar *shifts)
1716: {
1718: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1719: PetscInt i;
1722: if (ns<=0) SETERRQ(PetscObjectComm((PetscObject)nep),PETSC_ERR_ARG_WRONG,"Number of shifts must be positive");
1723: if (ctx->nshifts) { PetscFree(ctx->shifts); }
1724: for (i=0;i<ctx->nshiftsw;i++) { KSPDestroy(&ctx->ksp[i]); }
1725: PetscFree(ctx->ksp);
1726: ctx->ksp = NULL;
1727: PetscMalloc(ns,&ctx->shifts);
1728: for (i=0;i<ns;i++) ctx->shifts[i] = shifts[i];
1729: ctx->nshifts = ns;
1730: nep->state = NEP_STATE_INITIAL;
1731: return(0);
1732: }
1736: /*@C
1737: NEPNLEIGSSetRKShifts - Sets a list of shifts to be used in the Rational
1738: Krylov method.
1740: Logically Collective on NEP
1742: Input Parameters:
1743: + nep - the nonlinear eigensolver context
1744: . ns - number of shifts
1745: - shifts - array of scalar values specifying the shifts
1747: Options Database Key:
1748: . -nep_nleigs_rk_shifts - Sets the list of shifts
1750: Notes:
1751: If only one shift is provided, the subspace is built with the simpler
1752: shift-and-invert Krylov-Schur.
1754: In the case of real scalars, complex shifts are not allowed. In the
1755: command line, a comma-separated list of complex values can be provided with
1756: the format [+/-][realnumber][+/-]realnumberi with no spaces, e.g.
1757: -nep_nleigs_rk_shifts 1.0+2.0i,1.5+2.0i,1.0+1.5i
1759: Level: advanced
1761: .seealso: NEPNLEIGSGetRKShifts()
1762: @*/
1763: PetscErrorCode NEPNLEIGSSetRKShifts(NEP nep,PetscInt ns,PetscScalar *shifts)
1764: {
1771: PetscTryMethod(nep,"NEPNLEIGSSetRKShifts_C",(NEP,PetscInt,PetscScalar*),(nep,ns,shifts));
1772: return(0);
1773: }
1777: static PetscErrorCode NEPNLEIGSGetRKShifts_NLEIGS(NEP nep,PetscInt *ns,PetscScalar **shifts)
1778: {
1780: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1781: PetscInt i;
1784: *ns = ctx->nshifts;
1785: if (ctx->nshifts) {
1786: PetscMalloc1(ctx->nshifts,shifts);
1787: for (i=0;i<ctx->nshifts;i++) (*shifts)[i] = ctx->shifts[i];
1788: }
1789: return(0);
1790: }
1794: /*@C
1795: NEPNLEIGSGetRKShifts - Gets the list of shifts used in the Rational
1796: Krylov method.
1798: Not Collective
1800: Input Parameter:
1801: . nep - the nonlinear eigensolver context
1803: Output Parameter:
1804: + ns - number of shifts
1805: - shifts - array of shifts
1807: Level: advanced
1809: .seealso: NEPNLEIGSSetRKShifts()
1810: @*/
1811: PetscErrorCode NEPNLEIGSGetRKShifts(NEP nep,PetscInt *ns,PetscScalar **shifts)
1812: {
1819: PetscTryMethod(nep,"NEPNLEIGSGetRKShifts_C",(NEP,PetscInt*,PetscScalar**),(nep,ns,shifts));
1820: return(0);
1821: }
1823: #define SHIFTMAX 30
1827: PetscErrorCode NEPSetFromOptions_NLEIGS(PetscOptionItems *PetscOptionsObject,NEP nep)
1828: {
1830: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
1831: PetscInt i,k;
1832: PetscBool flg1,flg2,b;
1833: PetscReal r;
1834: PetscScalar array[SHIFTMAX];
1835: PC pc;
1836: PCType pctype;
1837: KSPType ksptype;
1840: PetscOptionsHead(PetscOptionsObject,"NEP NLEIGS Options");
1841: PetscOptionsReal("-nep_nleigs_restart","Proportion of vectors kept after restart","NEPNLEIGSSetRestart",0.5,&r,&flg1);
1842: if (flg1) {
1843: NEPNLEIGSSetRestart(nep,r);
1844: }
1845: PetscOptionsBool("-nep_nleigs_locking","Choose between locking and non-locking variants","NEPNLEIGSSetLocking",PETSC_FALSE,&b,&flg1);
1846: if (flg1) {
1847: NEPNLEIGSSetLocking(nep,b);
1848: }
1849: PetscOptionsBool("-nep_nleigs_true_residual","Compute true residuals explicitly","NEPNLEIGSSetTrueResidual",PETSC_FALSE,&b,&flg1);
1850: if (flg1) {
1851: NEPNLEIGSSetTrueResidual(nep,b);
1852: }
1853: NEPNLEIGSGetInterpolation(nep,&r,&i);
1854: if (!i) i = PETSC_DEFAULT;
1855: PetscOptionsInt("-nep_nleigs_interpolation_max_it","Maximum number of terms for interpolation via divided differences","NEPNLEIGSSetInterpolation",i,&i,&flg1);
1856: PetscOptionsReal("-nep_nleigs_interpolation_tol","Tolerance for interpolation via divided differences","NEPNLEIGSSetInterpolation",r,&r,&flg2);
1857: if (flg1 || flg2) {
1858: NEPNLEIGSSetInterpolation(nep,r,i);
1859: }
1860: k = SHIFTMAX;
1861: for (i=0;i<k;i++) array[i] = 0;
1862: PetscOptionsScalarArray("-nep_nleigs_rk_shifts","Shifts for Rational Krylov","NEPNLEIGSSetRKShifts",array,&k,&flg1);
1863: if (flg1) {
1864: NEPNLEIGSSetRKShifts(nep,k,array);
1865: }
1867: if (!ctx->ksp) { NEPNLEIGSGetKSPs(nep,&ctx->ksp); }
1868: for (i=0;i<ctx->nshiftsw;i++) {
1869: KSPGetPC(ctx->ksp[i],&pc);
1870: KSPGetType(ctx->ksp[i],&ksptype);
1871: PCGetType(pc,&pctype);
1872: if (!pctype && !ksptype) {
1873: KSPSetType(ctx->ksp[i],KSPPREONLY);
1874: PCSetType(pc,PCLU);
1875: }
1876: KSPSetOperators(ctx->ksp[i],nep->function,nep->function_pre);
1877: KSPSetFromOptions(ctx->ksp[i]);
1878: }
1879: PetscOptionsTail();
1880: return(0);
1881: }
1885: static PetscErrorCode NEPNLEIGSGetKSPs_NLEIGS(NEP nep,KSP **ksp)
1886: {
1888: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
1889: PetscInt i;
1892: if (!ctx->ksp) {
1893: NEPNLEIGSSetShifts(nep);
1894: PetscMalloc1(ctx->nshiftsw,&ctx->ksp);
1895: for (i=0;i<ctx->nshiftsw;i++) {
1896: KSPCreate(PetscObjectComm((PetscObject)nep),&ctx->ksp[i]);
1897: KSPSetOptionsPrefix(ctx->ksp[i],((PetscObject)nep)->prefix);
1898: KSPAppendOptionsPrefix(ctx->ksp[i],"nep_nleigs_");
1899: PetscObjectIncrementTabLevel((PetscObject)ctx->ksp[i],(PetscObject)nep,1);
1900: PetscLogObjectParent((PetscObject)nep,(PetscObject)ctx->ksp[i]);
1901: KSPSetErrorIfNotConverged(ctx->ksp[i],PETSC_TRUE);
1902: }
1903: }
1904: *ksp = ctx->ksp;
1905: return(0);
1906: }
1910: /*@C
1911: NEPNLEIGSGetKSPs - Retrieve the array of linear solver objects associated with
1912: the nonlinear eigenvalue solver.
1914: Not Collective
1916: Input Parameter:
1917: . nep - nonlinear eigenvalue solver
1919: Output Parameter:
1920: . ksp - array of linear solver object
1922: Level: advanced
1923: @*/
1924: PetscErrorCode NEPNLEIGSGetKSPs(NEP nep,KSP **ksp)
1925: {
1931: PetscUseMethod(nep,"NEPNLEIGSGetKSPs_C",(NEP,KSP**),(nep,ksp));
1932: return(0);
1933: }
1937: PetscErrorCode NEPView_NLEIGS(NEP nep,PetscViewer viewer)
1938: {
1940: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1941: PetscBool isascii;
1942: PetscInt i;
1943: char str[50];
1946: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1947: if (isascii) {
1948: PetscViewerASCIIPrintf(viewer," NLEIGS: %d%% of basis vectors kept after restart\n",(int)(100*ctx->keep));
1949: PetscViewerASCIIPrintf(viewer," NLEIGS: using the %slocking variant\n",ctx->lock?"":"non-");
1950: PetscViewerASCIIPrintf(viewer," NLEIGS: maximum number of divided difference terms: %D\n",ctx->ddmaxit);
1951: PetscViewerASCIIPrintf(viewer," NLEIGS: tolerance for divided difference convergence: %g\n",(double)ctx->ddtol);
1952: if (ctx->nshifts) {
1953: PetscViewerASCIIPrintf(viewer," NLEIGS: RK shifts: ");
1954: PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
1955: for (i=0;i<ctx->nshifts;i++) {
1956: SlepcSNPrintfScalar(str,50,ctx->shifts[i],PETSC_FALSE);
1957: PetscViewerASCIIPrintf(viewer,"%s%s",str,(i<ctx->nshifts-1)?",":"");
1958: }
1959: PetscViewerASCIIPrintf(viewer,"\n");
1960: PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1961: }
1962: if (ctx->trueres) { PetscViewerASCIIPrintf(viewer," NLEIGS: computing true residuals for convergence check\n"); }
1963: if (!ctx->ksp) { NEPNLEIGSGetKSPs(nep,&ctx->ksp); }
1964: PetscViewerASCIIPushTab(viewer);
1965: KSPView(ctx->ksp[0],viewer);
1966: PetscViewerASCIIPopTab(viewer);
1967: }
1968: return(0);
1969: }
1973: PetscErrorCode NEPReset_NLEIGS(NEP nep)
1974: {
1976: PetscInt k;
1977: NEP_NLEIGS *ctx=(NEP_NLEIGS*)nep->data;
1980: if (nep->fui==NEP_USER_INTERFACE_SPLIT) {
1981: PetscFree(ctx->coeffD);
1982: } else {
1983: for (k=0;k<ctx->nmat;k++) { MatDestroy(&ctx->D[k]); }
1984: }
1985: if (ctx->vrn) {
1986: VecDestroy(&ctx->vrn);
1987: }
1988: return(0);
1989: }
1993: PetscErrorCode NEPDestroy_NLEIGS(NEP nep)
1994: {
1996: PetscInt k;
1997: NEP_NLEIGS *ctx = (NEP_NLEIGS*)nep->data;
2000: for (k=0;k<ctx->nshiftsw;k++) { KSPDestroy(&ctx->ksp[k]); }
2001: PetscFree(ctx->ksp);
2002: if (ctx->nshifts) { PetscFree(ctx->shifts); }
2003: PetscFree4(ctx->s,ctx->xi,ctx->beta,ctx->D);
2004: PetscFree(nep->data);
2005: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetSingularitiesFunction_C",NULL);
2006: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetSingularitiesFunction_C",NULL);
2007: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetRestart_C",NULL);
2008: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetRestart_C",NULL);
2009: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetLocking_C",NULL);
2010: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetLocking_C",NULL);
2011: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetInterpolation_C",NULL);
2012: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetInterpolation_C",NULL);
2013: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetTrueResidual_C",NULL);
2014: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetTrueResidual_C",NULL);
2015: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetRKShifts_C",NULL);
2016: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetRKShifts_C",NULL);
2017: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetKSPs_C",NULL);
2018: return(0);
2019: }
2023: PETSC_EXTERN PetscErrorCode NEPCreate_NLEIGS(NEP nep)
2024: {
2026: NEP_NLEIGS *ctx;
2029: PetscNewLog(nep,&ctx);
2030: nep->data = (void*)ctx;
2031: ctx->lock = PETSC_TRUE;
2032: ctx->ddtol = PETSC_DEFAULT;
2033: ctx->ddmaxit = 0;
2034: ctx->trueres = PETSC_FALSE;
2035: ctx->nshifts = 0;
2037: nep->ops->solve = NEPSolve_NLEIGS;
2038: nep->ops->setup = NEPSetUp_NLEIGS;
2039: nep->ops->setfromoptions = NEPSetFromOptions_NLEIGS;
2040: nep->ops->view = NEPView_NLEIGS;
2041: nep->ops->destroy = NEPDestroy_NLEIGS;
2042: nep->ops->reset = NEPReset_NLEIGS;
2043: nep->ops->computevectors = NEPComputeVectors_Schur;
2044: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetSingularitiesFunction_C",NEPNLEIGSSetSingularitiesFunction_NLEIGS);
2045: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetSingularitiesFunction_C",NEPNLEIGSGetSingularitiesFunction_NLEIGS);
2046: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetRestart_C",NEPNLEIGSSetRestart_NLEIGS);
2047: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetRestart_C",NEPNLEIGSGetRestart_NLEIGS);
2048: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetLocking_C",NEPNLEIGSSetLocking_NLEIGS);
2049: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetLocking_C",NEPNLEIGSGetLocking_NLEIGS);
2050: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetInterpolation_C",NEPNLEIGSSetInterpolation_NLEIGS);
2051: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetInterpolation_C",NEPNLEIGSGetInterpolation_NLEIGS);
2052: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetTrueResidual_C",NEPNLEIGSSetTrueResidual_NLEIGS);
2053: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetTrueResidual_C",NEPNLEIGSGetTrueResidual_NLEIGS);
2054: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSSetRKShifts_C",NEPNLEIGSSetRKShifts_NLEIGS);
2055: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetRKShifts_C",NEPNLEIGSGetRKShifts_NLEIGS);
2056: PetscObjectComposeFunction((PetscObject)nep,"NEPNLEIGSGetKSPs_C",NEPNLEIGSGetKSPs_NLEIGS);
2057: return(0);
2058: }