Actual source code: cayley.c
slepc-3.7.2 2016-07-19
1: /*
2: Implements the Cayley spectral transform.
4: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
5: SLEPc - Scalable Library for Eigenvalue Problem Computations
6: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
8: This file is part of SLEPc.
10: SLEPc is free software: you can redistribute it and/or modify it under the
11: terms of version 3 of the GNU Lesser General Public License as published by
12: the Free Software Foundation.
14: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
15: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
17: more details.
19: You should have received a copy of the GNU Lesser General Public License
20: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
21: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
22: */
24: #include <slepc/private/stimpl.h> /*I "slepcst.h" I*/
26: typedef struct {
27: PetscScalar nu;
28: PetscBool nu_set;
29: Vec w2;
30: } ST_CAYLEY;
34: PetscErrorCode STApply_Cayley(ST st,Vec x,Vec y)
35: {
39: /* standard eigenproblem: y = (A - sI)^-1 (A + tI)x */
40: /* generalized eigenproblem: y = (A - sB)^-1 (A + tB)x */
41: MatMult(st->T[0],x,st->w);
42: STMatSolve(st,st->w,y);
43: return(0);
44: }
48: PetscErrorCode STApplyTranspose_Cayley(ST st,Vec x,Vec y)
49: {
53: /* standard eigenproblem: y = (A + tI)^T (A - sI)^-T x */
54: /* generalized eigenproblem: y = (A + tB)^T (A - sB)^-T x */
55: STMatSolveTranspose(st,x,st->w);
56: MatMultTranspose(st->T[0],st->w,y);
57: return(0);
58: }
62: static PetscErrorCode MatMult_Cayley(Mat B,Vec x,Vec y)
63: {
65: ST st;
66: ST_CAYLEY *ctx;
67: PetscScalar nu;
70: MatShellGetContext(B,(void**)&st);
71: ctx = (ST_CAYLEY*)st->data;
72: nu = ctx->nu;
74: if (st->shift_matrix == ST_MATMODE_INPLACE) { nu = nu + st->sigma; };
76: if (st->nmat>1) {
77: /* generalized eigenproblem: y = (A + tB)x */
78: MatMult(st->A[0],x,y);
79: MatMult(st->A[1],x,ctx->w2);
80: VecAXPY(y,nu,ctx->w2);
81: } else {
82: /* standard eigenproblem: y = (A + tI)x */
83: MatMult(st->A[0],x,y);
84: VecAXPY(y,nu,x);
85: }
86: return(0);
87: }
91: PetscErrorCode STGetBilinearForm_Cayley(ST st,Mat *B)
92: {
96: STSetUp(st);
97: *B = st->T[0];
98: PetscObjectReference((PetscObject)*B);
99: return(0);
100: }
104: PetscErrorCode STBackTransform_Cayley(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
105: {
106: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
107: PetscInt j;
108: #if !defined(PETSC_USE_COMPLEX)
109: PetscScalar t,i,r;
110: #endif
113: #if !defined(PETSC_USE_COMPLEX)
114: for (j=0;j<n;j++) {
115: if (eigi[j] == 0.0) eigr[j] = (ctx->nu + eigr[j] * st->sigma) / (eigr[j] - 1.0);
116: else {
117: r = eigr[j];
118: i = eigi[j];
119: r = st->sigma * (r * r + i * i - r) + ctx->nu * (r - 1);
120: i = - st->sigma * i - ctx->nu * i;
121: t = i * i + r * (r - 2.0) + 1.0;
122: eigr[j] = r / t;
123: eigi[j] = i / t;
124: }
125: }
126: #else
127: for (j=0;j<n;j++) {
128: eigr[j] = (ctx->nu + eigr[j] * st->sigma) / (eigr[j] - 1.0);
129: }
130: #endif
131: return(0);
132: }
136: PetscErrorCode STPostSolve_Cayley(ST st)
137: {
141: if (st->shift_matrix == ST_MATMODE_INPLACE) {
142: if (st->nmat>1) {
143: MatAXPY(st->A[0],st->sigma,st->A[1],st->str);
144: } else {
145: MatShift(st->A[0],st->sigma);
146: }
147: st->Astate[0] = ((PetscObject)st->A[0])->state;
148: st->state = ST_STATE_INITIAL;
149: }
150: return(0);
151: }
155: PetscErrorCode STSetUp_Cayley(ST st)
156: {
158: PetscInt n,m;
159: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
162: ST_AllocateWorkVec(st);
164: /* if the user did not set the shift, use the target value */
165: if (!st->sigma_set) st->sigma = st->defsigma;
167: if (!ctx->nu_set) ctx->nu = st->sigma;
168: if (ctx->nu == 0.0 && st->sigma == 0.0) SETERRQ(PetscObjectComm((PetscObject)st),1,"Values of shift and antishift cannot be zero simultaneously");
170: /* T[0] = A+nu*B */
171: if (st->shift_matrix==ST_MATMODE_INPLACE) {
172: MatGetLocalSize(st->A[0],&n,&m);
173: MatCreateShell(PetscObjectComm((PetscObject)st),n,m,PETSC_DETERMINE,PETSC_DETERMINE,st,&st->T[0]);
174: MatShellSetOperation(st->T[0],MATOP_MULT,(void(*)(void))MatMult_Cayley);
175: PetscLogObjectParent((PetscObject)st,(PetscObject)st->T[0]);
176: } else {
177: STMatMAXPY_Private(st,ctx->nu,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[0]);
178: }
180: /* T[1] = A-sigma*B */
181: STMatMAXPY_Private(st,-st->sigma,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[1]);
182: PetscObjectReference((PetscObject)st->T[1]);
183: MatDestroy(&st->P);
184: st->P = st->T[1];
185: if (st->nmat>1) {
186: VecDestroy(&ctx->w2);
187: MatCreateVecs(st->A[1],&ctx->w2,NULL);
188: PetscLogObjectParent((PetscObject)st,(PetscObject)ctx->w2);
189: }
190: if (!st->ksp) { STGetKSP(st,&st->ksp); }
191: STCheckFactorPackage(st);
192: KSPSetOperators(st->ksp,st->P,st->P);
193: KSPSetErrorIfNotConverged(st->ksp,PETSC_TRUE);
194: KSPSetUp(st->ksp);
195: return(0);
196: }
200: PetscErrorCode STSetShift_Cayley(ST st,PetscScalar newshift)
201: {
203: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
206: if (newshift==0.0 && (!ctx->nu_set || (ctx->nu_set && ctx->nu==0.0))) SETERRQ(PetscObjectComm((PetscObject)st),1,"Values of shift and antishift cannot be zero simultaneously");
208: if (!ctx->nu_set) {
209: if (st->shift_matrix!=ST_MATMODE_INPLACE) {
210: STMatMAXPY_Private(st,newshift,ctx->nu,0,NULL,PETSC_FALSE,&st->T[0]);
211: }
212: ctx->nu = newshift;
213: }
214: STMatMAXPY_Private(st,-newshift,-st->sigma,0,NULL,PETSC_FALSE,&st->T[1]);
215: if (st->P!=st->T[1]) {
216: MatDestroy(&st->P);
217: st->P = st->T[1];
218: PetscObjectReference((PetscObject)st->P);
219: }
220: KSPSetOperators(st->ksp,st->P,st->P);
221: KSPSetUp(st->ksp);
222: return(0);
223: }
227: PetscErrorCode STSetFromOptions_Cayley(PetscOptionItems *PetscOptionsObject,ST st)
228: {
230: PetscScalar nu;
231: PetscBool flg;
232: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
233: PC pc;
234: PCType pctype;
235: KSPType ksptype;
238: if (!st->ksp) { STGetKSP(st,&st->ksp); }
239: KSPGetPC(st->ksp,&pc);
240: KSPGetType(st->ksp,&ksptype);
241: PCGetType(pc,&pctype);
242: if (!pctype && !ksptype) {
243: if (st->shift_matrix == ST_MATMODE_SHELL) {
244: /* in shell mode use GMRES with Jacobi as the default */
245: KSPSetType(st->ksp,KSPGMRES);
246: PCSetType(pc,PCJACOBI);
247: } else {
248: /* use direct solver as default */
249: KSPSetType(st->ksp,KSPPREONLY);
250: PCSetType(pc,PCLU);
251: }
252: }
254: PetscOptionsHead(PetscOptionsObject,"ST Cayley Options");
255: PetscOptionsScalar("-st_cayley_antishift","Value of the antishift","STCayleySetAntishift",ctx->nu,&nu,&flg);
256: if (flg) {
257: STCayleySetAntishift(st,nu);
258: }
259: PetscOptionsTail();
260: return(0);
261: }
265: static PetscErrorCode STCayleySetAntishift_Cayley(ST st,PetscScalar newshift)
266: {
268: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
271: if (st->state && st->shift_matrix!=ST_MATMODE_INPLACE) {
272: STMatMAXPY_Private(st,newshift,ctx->nu,0,NULL,PETSC_FALSE,&st->T[0]);
273: }
274: ctx->nu = newshift;
275: ctx->nu_set = PETSC_TRUE;
276: return(0);
277: }
281: /*@
282: STCayleySetAntishift - Sets the value of the anti-shift for the Cayley
283: spectral transformation.
285: Logically Collective on ST
287: Input Parameters:
288: + st - the spectral transformation context
289: - nu - the anti-shift
291: Options Database Key:
292: . -st_cayley_antishift - Sets the value of the anti-shift
294: Level: intermediate
296: Note:
297: In the generalized Cayley transform, the operator can be expressed as
298: OP = inv(A - sigma B)*(A + nu B). This function sets the value of nu.
299: Use STSetShift() for setting sigma.
301: .seealso: STSetShift(), STCayleyGetAntishift()
302: @*/
303: PetscErrorCode STCayleySetAntishift(ST st,PetscScalar nu)
304: {
310: PetscTryMethod(st,"STCayleySetAntishift_C",(ST,PetscScalar),(st,nu));
311: return(0);
312: }
315: static PetscErrorCode STCayleyGetAntishift_Cayley(ST st,PetscScalar *nu)
316: {
317: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
320: *nu = ctx->nu;
321: return(0);
322: }
326: /*@
327: STCayleyGetAntishift - Gets the value of the anti-shift used in the Cayley
328: spectral transformation.
330: Not Collective
332: Input Parameter:
333: . st - the spectral transformation context
335: Output Parameter:
336: . nu - the anti-shift
338: Level: intermediate
340: .seealso: STGetShift(), STCayleySetAntishift()
341: @*/
342: PetscErrorCode STCayleyGetAntishift(ST st,PetscScalar *nu)
343: {
349: PetscUseMethod(st,"STCayleyGetAntishift_C",(ST,PetscScalar*),(st,nu));
350: return(0);
351: }
355: PetscErrorCode STView_Cayley(ST st,PetscViewer viewer)
356: {
358: char str[50];
359: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
360: PetscBool isascii;
363: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
364: if (isascii) {
365: SlepcSNPrintfScalar(str,50,ctx->nu,PETSC_FALSE);
366: PetscViewerASCIIPrintf(viewer," Cayley: antishift: %s\n",str);
367: }
368: return(0);
369: }
373: PetscErrorCode STReset_Cayley(ST st)
374: {
376: ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;
379: VecDestroy(&ctx->w2);
380: return(0);
381: }
385: PetscErrorCode STDestroy_Cayley(ST st)
386: {
390: PetscFree(st->data);
391: PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",NULL);
392: PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",NULL);
393: return(0);
394: }
398: PETSC_EXTERN PetscErrorCode STCreate_Cayley(ST st)
399: {
401: ST_CAYLEY *ctx;
404: PetscNewLog(st,&ctx);
405: st->data = (void*)ctx;
407: st->ops->apply = STApply_Cayley;
408: st->ops->getbilinearform = STGetBilinearForm_Cayley;
409: st->ops->applytrans = STApplyTranspose_Cayley;
410: st->ops->postsolve = STPostSolve_Cayley;
411: st->ops->backtransform = STBackTransform_Cayley;
412: st->ops->setfromoptions = STSetFromOptions_Cayley;
413: st->ops->setup = STSetUp_Cayley;
414: st->ops->setshift = STSetShift_Cayley;
415: st->ops->destroy = STDestroy_Cayley;
416: st->ops->reset = STReset_Cayley;
417: st->ops->view = STView_Cayley;
418: st->ops->checknullspace = STCheckNullSpace_Default;
419: PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",STCayleySetAntishift_Cayley);
420: PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",STCayleyGetAntishift_Cayley);
421: return(0);
422: }