Actual source code: cayley.c

slepc-3.7.0 2016-05-16
Report Typos and Errors
  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:   /* if the user did not set the shift, use the target value */
163:   if (!st->sigma_set) st->sigma = st->defsigma;

165:   if (!ctx->nu_set) ctx->nu = st->sigma;
166:   if (ctx->nu == 0.0 && st->sigma == 0.0) SETERRQ(PetscObjectComm((PetscObject)st),1,"Values of shift and antishift cannot be zero simultaneously");

168:   /* T[0] = A+nu*B */
169:   if (st->shift_matrix==ST_MATMODE_INPLACE) {
170:     MatGetLocalSize(st->A[0],&n,&m);
171:     MatCreateShell(PetscObjectComm((PetscObject)st),n,m,PETSC_DETERMINE,PETSC_DETERMINE,st,&st->T[0]);
172:     MatShellSetOperation(st->T[0],MATOP_MULT,(void(*)(void))MatMult_Cayley);
173:     PetscLogObjectParent((PetscObject)st,(PetscObject)st->T[0]);
174:   } else {
175:     STMatMAXPY_Private(st,ctx->nu,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[0]);
176:   }

178:   /* T[1] = A-sigma*B */
179:   STMatMAXPY_Private(st,-st->sigma,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[1]);
180:   PetscObjectReference((PetscObject)st->T[1]);
181:   MatDestroy(&st->P);
182:   st->P = st->T[1];
183:   if (st->nmat>1) {
184:     VecDestroy(&ctx->w2);
185:     MatCreateVecs(st->A[1],&ctx->w2,NULL);
186:     PetscLogObjectParent((PetscObject)st,(PetscObject)ctx->w2);
187:   }
188:   if (!st->ksp) { STGetKSP(st,&st->ksp); }
189:   STCheckFactorPackage(st);
190:   KSPSetOperators(st->ksp,st->P,st->P);
191:   KSPSetUp(st->ksp);
192:   return(0);
193: }

197: PetscErrorCode STSetShift_Cayley(ST st,PetscScalar newshift)
198: {
200:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;

203:   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");

205:   if (!ctx->nu_set) {
206:     if (st->shift_matrix!=ST_MATMODE_INPLACE) {
207:       STMatMAXPY_Private(st,newshift,ctx->nu,0,NULL,PETSC_FALSE,&st->T[0]);
208:     }
209:     ctx->nu = newshift;
210:   }
211:   STMatMAXPY_Private(st,-newshift,-st->sigma,0,NULL,PETSC_FALSE,&st->T[1]);
212:   if (st->P!=st->T[1]) {
213:     MatDestroy(&st->P);
214:     st->P = st->T[1];
215:     PetscObjectReference((PetscObject)st->P);
216:   }
217:   KSPSetOperators(st->ksp,st->P,st->P);
218:   KSPSetUp(st->ksp);
219:   return(0);
220: }

224: PetscErrorCode STSetFromOptions_Cayley(PetscOptionItems *PetscOptionsObject,ST st)
225: {
227:   PetscScalar    nu;
228:   PetscBool      flg;
229:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;
230:   PC             pc;
231:   PCType         pctype;
232:   KSPType        ksptype;

235:   if (!st->ksp) { STGetKSP(st,&st->ksp); }
236:   KSPGetPC(st->ksp,&pc);
237:   KSPGetType(st->ksp,&ksptype);
238:   PCGetType(pc,&pctype);
239:   if (!pctype && !ksptype) {
240:     if (st->shift_matrix == ST_MATMODE_SHELL) {
241:       /* in shell mode use GMRES with Jacobi as the default */
242:       KSPSetType(st->ksp,KSPGMRES);
243:       PCSetType(pc,PCJACOBI);
244:     } else {
245:       /* use direct solver as default */
246:       KSPSetType(st->ksp,KSPPREONLY);
247:       PCSetType(pc,PCLU);
248:     }
249:   }

251:   PetscOptionsHead(PetscOptionsObject,"ST Cayley Options");
252:   PetscOptionsScalar("-st_cayley_antishift","Value of the antishift","STCayleySetAntishift",ctx->nu,&nu,&flg);
253:   if (flg) {
254:     STCayleySetAntishift(st,nu);
255:   }
256:   PetscOptionsTail();
257:   return(0);
258: }

262: static PetscErrorCode STCayleySetAntishift_Cayley(ST st,PetscScalar newshift)
263: {
265:   ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;

268:   if (st->state && st->shift_matrix!=ST_MATMODE_INPLACE) {
269:     STMatMAXPY_Private(st,newshift,ctx->nu,0,NULL,PETSC_FALSE,&st->T[0]);
270:   }
271:   ctx->nu     = newshift;
272:   ctx->nu_set = PETSC_TRUE;
273:   return(0);
274: }

278: /*@
279:    STCayleySetAntishift - Sets the value of the anti-shift for the Cayley
280:    spectral transformation.

282:    Logically Collective on ST

284:    Input Parameters:
285: +  st  - the spectral transformation context
286: -  nu  - the anti-shift

288:    Options Database Key:
289: .  -st_cayley_antishift - Sets the value of the anti-shift

291:    Level: intermediate

293:    Note:
294:    In the generalized Cayley transform, the operator can be expressed as
295:    OP = inv(A - sigma B)*(A + nu B). This function sets the value of nu.
296:    Use STSetShift() for setting sigma.

298: .seealso: STSetShift(), STCayleyGetAntishift()
299: @*/
300: PetscErrorCode STCayleySetAntishift(ST st,PetscScalar nu)
301: {

307:   PetscTryMethod(st,"STCayleySetAntishift_C",(ST,PetscScalar),(st,nu));
308:   return(0);
309: }
312: static PetscErrorCode STCayleyGetAntishift_Cayley(ST st,PetscScalar *nu)
313: {
314:   ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;

317:   *nu = ctx->nu;
318:   return(0);
319: }

323: /*@
324:    STCayleyGetAntishift - Gets the value of the anti-shift used in the Cayley
325:    spectral transformation.

327:    Not Collective

329:    Input Parameter:
330: .  st  - the spectral transformation context

332:    Output Parameter:
333: .  nu  - the anti-shift

335:    Level: intermediate

337: .seealso: STGetShift(), STCayleySetAntishift()
338: @*/
339: PetscErrorCode STCayleyGetAntishift(ST st,PetscScalar *nu)
340: {

346:   PetscUseMethod(st,"STCayleyGetAntishift_C",(ST,PetscScalar*),(st,nu));
347:   return(0);
348: }

352: PetscErrorCode STView_Cayley(ST st,PetscViewer viewer)
353: {
355:   char           str[50];
356:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;
357:   PetscBool      isascii;

360:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
361:   if (isascii) {
362:     SlepcSNPrintfScalar(str,50,ctx->nu,PETSC_FALSE);
363:     PetscViewerASCIIPrintf(viewer,"  Cayley: antishift: %s\n",str);
364:   }
365:   return(0);
366: }

370: PetscErrorCode STReset_Cayley(ST st)
371: {
373:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;

376:   VecDestroy(&ctx->w2);
377:   return(0);
378: }

382: PetscErrorCode STDestroy_Cayley(ST st)
383: {

387:   PetscFree(st->data);
388:   PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",NULL);
389:   PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",NULL);
390:   return(0);
391: }

395: PETSC_EXTERN PetscErrorCode STCreate_Cayley(ST st)
396: {
398:   ST_CAYLEY      *ctx;

401:   PetscNewLog(st,&ctx);
402:   st->data = (void*)ctx;

404:   st->ops->apply           = STApply_Cayley;
405:   st->ops->getbilinearform = STGetBilinearForm_Cayley;
406:   st->ops->applytrans      = STApplyTranspose_Cayley;
407:   st->ops->postsolve       = STPostSolve_Cayley;
408:   st->ops->backtransform   = STBackTransform_Cayley;
409:   st->ops->setfromoptions  = STSetFromOptions_Cayley;
410:   st->ops->setup           = STSetUp_Cayley;
411:   st->ops->setshift        = STSetShift_Cayley;
412:   st->ops->destroy         = STDestroy_Cayley;
413:   st->ops->reset           = STReset_Cayley;
414:   st->ops->view            = STView_Cayley;
415:   st->ops->checknullspace  = STCheckNullSpace_Default;
416:   PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",STCayleySetAntishift_Cayley);
417:   PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",STCayleyGetAntishift_Cayley);
418:   return(0);
419: }