Actual source code: cayley.c

slepc-3.12.2 2020-01-13
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, 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:    Implements the Cayley spectral transform
 12: */

 14: #include <slepc/private/stimpl.h>          /*I "slepcst.h" I*/

 16: typedef struct {
 17:   PetscScalar nu;
 18:   PetscBool   nu_set;
 19: } ST_CAYLEY;

 21: PetscErrorCode STApply_Cayley(ST st,Vec x,Vec y)
 22: {

 26:   /* standard eigenproblem: y = (A - sI)^-1 (A + tI)x */
 27:   /* generalized eigenproblem: y = (A - sB)^-1 (A + tB)x */
 28:   MatMult(st->T[0],x,st->work[0]);
 29:   STMatSolve(st,st->work[0],y);
 30:   return(0);
 31: }

 33: PetscErrorCode STApplyTranspose_Cayley(ST st,Vec x,Vec y)
 34: {

 38:   /* standard eigenproblem: y =  (A + tI)^T (A - sI)^-T x */
 39:   /* generalized eigenproblem: y = (A + tB)^T (A - sB)^-T x */
 40:   STMatSolveTranspose(st,x,st->work[0]);
 41:   MatMultTranspose(st->T[0],st->work[0],y);
 42:   return(0);
 43: }

 45: static PetscErrorCode MatMult_Cayley(Mat B,Vec x,Vec y)
 46: {
 48:   ST             st;
 49:   ST_CAYLEY      *ctx;
 50:   PetscScalar    nu;

 53:   MatShellGetContext(B,(void**)&st);
 54:   ctx = (ST_CAYLEY*)st->data;
 55:   nu = ctx->nu;

 57:   if (st->matmode == ST_MATMODE_INPLACE) { nu = nu + st->sigma; };

 59:   if (st->nmat>1) {
 60:     /* generalized eigenproblem: y = (A + tB)x */
 61:     MatMult(st->A[0],x,y);
 62:     MatMult(st->A[1],x,st->work[1]);
 63:     VecAXPY(y,nu,st->work[1]);
 64:   } else {
 65:     /* standard eigenproblem: y = (A + tI)x */
 66:     MatMult(st->A[0],x,y);
 67:     VecAXPY(y,nu,x);
 68:   }
 69:   return(0);
 70: }

 72: static PetscErrorCode MatMultTranspose_Cayley(Mat B,Vec x,Vec y)
 73: {
 75:   ST             st;
 76:   ST_CAYLEY      *ctx;
 77:   PetscScalar    nu;

 80:   MatShellGetContext(B,(void**)&st);
 81:   ctx = (ST_CAYLEY*)st->data;
 82:   nu = ctx->nu;

 84:   if (st->matmode == ST_MATMODE_INPLACE) { nu = nu + st->sigma; };
 85:   nu = PetscConj(nu);

 87:   if (st->nmat>1) {
 88:     /* generalized eigenproblem: y = (A + tB)x */
 89:     MatMultTranspose(st->A[0],x,y);
 90:     MatMultTranspose(st->A[1],x,st->work[1]);
 91:     VecAXPY(y,nu,st->work[1]);
 92:   } else {
 93:     /* standard eigenproblem: y = (A + tI)x */
 94:     MatMultTranspose(st->A[0],x,y);
 95:     VecAXPY(y,nu,x);
 96:   }
 97:   return(0);
 98: }

100: PetscErrorCode STGetBilinearForm_Cayley(ST st,Mat *B)
101: {

105:   STSetUp(st);
106:   *B = st->T[0];
107:   PetscObjectReference((PetscObject)*B);
108:   return(0);
109: }

111: PetscErrorCode STBackTransform_Cayley(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
112: {
113:   ST_CAYLEY   *ctx = (ST_CAYLEY*)st->data;
114:   PetscInt    j;
115: #if !defined(PETSC_USE_COMPLEX)
116:   PetscScalar t,i,r;
117: #endif

120: #if !defined(PETSC_USE_COMPLEX)
121:   for (j=0;j<n;j++) {
122:     if (eigi[j] == 0.0) eigr[j] = (ctx->nu + eigr[j] * st->sigma) / (eigr[j] - 1.0);
123:     else {
124:       r = eigr[j];
125:       i = eigi[j];
126:       r = st->sigma * (r * r + i * i - r) + ctx->nu * (r - 1);
127:       i = - st->sigma * i - ctx->nu * i;
128:       t = i * i + r * (r - 2.0) + 1.0;
129:       eigr[j] = r / t;
130:       eigi[j] = i / t;
131:     }
132:   }
133: #else
134:   for (j=0;j<n;j++) {
135:     eigr[j] = (ctx->nu + eigr[j] * st->sigma) / (eigr[j] - 1.0);
136:   }
137: #endif
138:   return(0);
139: }

141: PetscErrorCode STPostSolve_Cayley(ST st)
142: {

146:   if (st->matmode == ST_MATMODE_INPLACE) {
147:     if (st->nmat>1) {
148:       MatAXPY(st->A[0],st->sigma,st->A[1],st->str);
149:     } else {
150:       MatShift(st->A[0],st->sigma);
151:     }
152:     st->Astate[0] = ((PetscObject)st->A[0])->state;
153:     st->state = ST_STATE_INITIAL;
154:   }
155:   return(0);
156: }

158: PetscErrorCode STSetUp_Cayley(ST st)
159: {
161:   PetscInt       n,m;
162:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;

165:   STSetWorkVecs(st,2);

167:   /* if the user did not set the shift, use the target value */
168:   if (!st->sigma_set) st->sigma = st->defsigma;

170:   if (!ctx->nu_set) ctx->nu = st->sigma;
171:   if (ctx->nu == 0.0 && st->sigma == 0.0) SETERRQ(PetscObjectComm((PetscObject)st),1,"Values of shift and antishift cannot be zero simultaneously");
172:   if (ctx->nu == -st->sigma) SETERRQ(PetscObjectComm((PetscObject)st),1,"It is not allowed to set the antishift equal to minus the shift (the target)");

174:   /* T[0] = A+nu*B */
175:   if (st->matmode==ST_MATMODE_INPLACE) {
176:     MatGetLocalSize(st->A[0],&n,&m);
177:     MatCreateShell(PetscObjectComm((PetscObject)st),n,m,PETSC_DETERMINE,PETSC_DETERMINE,st,&st->T[0]);
178:     MatShellSetOperation(st->T[0],MATOP_MULT,(void(*)(void))MatMult_Cayley);
179:     MatShellSetOperation(st->T[0],MATOP_MULT_TRANSPOSE,(void(*)(void))MatMultTranspose_Cayley);
180:     PetscLogObjectParent((PetscObject)st,(PetscObject)st->T[0]);
181:   } else {
182:     STMatMAXPY_Private(st,ctx->nu,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[0]);
183:   }

185:   /* T[1] = A-sigma*B */
186:   STMatMAXPY_Private(st,-st->sigma,0.0,0,NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[1]);
187:   PetscObjectReference((PetscObject)st->T[1]);
188:   MatDestroy(&st->P);
189:   st->P = st->T[1];
190:   if (!st->ksp) { STGetKSP(st,&st->ksp); }
191:   STCheckFactorPackage(st);
192:   KSPSetOperators(st->ksp,st->P,st->P);
193:   KSPSetUp(st->ksp);
194:   return(0);
195: }

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");
204:   if (ctx->nu == -newshift) SETERRQ(PetscObjectComm((PetscObject)st),1,"It is not allowed to set the shift equal to minus the antishift");

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

223: PetscErrorCode STSetFromOptions_Cayley(PetscOptionItems *PetscOptionsObject,ST st)
224: {
226:   PetscScalar    nu;
227:   PetscBool      flg;
228:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;

231:   PetscOptionsHead(PetscOptionsObject,"ST Cayley Options");

233:     PetscOptionsScalar("-st_cayley_antishift","Value of the antishift","STCayleySetAntishift",ctx->nu,&nu,&flg);
234:     if (flg) { STCayleySetAntishift(st,nu); }

236:   PetscOptionsTail();
237:   return(0);
238: }

240: static PetscErrorCode STCayleySetAntishift_Cayley(ST st,PetscScalar newshift)
241: {
243:   ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;

246:   if (st->state && st->matmode!=ST_MATMODE_INPLACE) {
247:     STMatMAXPY_Private(st,newshift,ctx->nu,0,NULL,PETSC_FALSE,&st->T[0]);
248:   }
249:   ctx->nu     = newshift;
250:   ctx->nu_set = PETSC_TRUE;
251:   return(0);
252: }

254: /*@
255:    STCayleySetAntishift - Sets the value of the anti-shift for the Cayley
256:    spectral transformation.

258:    Logically Collective on st

260:    Input Parameters:
261: +  st  - the spectral transformation context
262: -  nu  - the anti-shift

264:    Options Database Key:
265: .  -st_cayley_antishift - Sets the value of the anti-shift

267:    Level: intermediate

269:    Note:
270:    In the generalized Cayley transform, the operator can be expressed as
271:    OP = inv(A - sigma B)*(A + nu B). This function sets the value of nu.
272:    Use STSetShift() for setting sigma. The value nu=-sigma is not allowed.

274: .seealso: STSetShift(), STCayleyGetAntishift()
275: @*/
276: PetscErrorCode STCayleySetAntishift(ST st,PetscScalar nu)
277: {

283:   PetscTryMethod(st,"STCayleySetAntishift_C",(ST,PetscScalar),(st,nu));
284:   return(0);
285: }

287: static PetscErrorCode STCayleyGetAntishift_Cayley(ST st,PetscScalar *nu)
288: {
289:   ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;

292:   *nu = ctx->nu;
293:   return(0);
294: }

296: /*@
297:    STCayleyGetAntishift - Gets the value of the anti-shift used in the Cayley
298:    spectral transformation.

300:    Not Collective

302:    Input Parameter:
303: .  st  - the spectral transformation context

305:    Output Parameter:
306: .  nu  - the anti-shift

308:    Level: intermediate

310: .seealso: STGetShift(), STCayleySetAntishift()
311: @*/
312: PetscErrorCode STCayleyGetAntishift(ST st,PetscScalar *nu)
313: {

319:   PetscUseMethod(st,"STCayleyGetAntishift_C",(ST,PetscScalar*),(st,nu));
320:   return(0);
321: }

323: PetscErrorCode STView_Cayley(ST st,PetscViewer viewer)
324: {
326:   char           str[50];
327:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;
328:   PetscBool      isascii;

331:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
332:   if (isascii) {
333:     SlepcSNPrintfScalar(str,50,ctx->nu,PETSC_FALSE);
334:     PetscViewerASCIIPrintf(viewer,"  antishift: %s\n",str);
335:   }
336:   return(0);
337: }

339: PetscErrorCode STDestroy_Cayley(ST st)
340: {

344:   PetscFree(st->data);
345:   PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",NULL);
346:   PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",NULL);
347:   return(0);
348: }

350: SLEPC_EXTERN PetscErrorCode STCreate_Cayley(ST st)
351: {
353:   ST_CAYLEY      *ctx;

356:   PetscNewLog(st,&ctx);
357:   st->data = (void*)ctx;

359:   st->usesksp = PETSC_TRUE;

361:   st->ops->apply           = STApply_Cayley;
362:   st->ops->getbilinearform = STGetBilinearForm_Cayley;
363:   st->ops->applytrans      = STApplyTranspose_Cayley;
364:   st->ops->postsolve       = STPostSolve_Cayley;
365:   st->ops->backtransform   = STBackTransform_Cayley;
366:   st->ops->setfromoptions  = STSetFromOptions_Cayley;
367:   st->ops->setup           = STSetUp_Cayley;
368:   st->ops->setshift        = STSetShift_Cayley;
369:   st->ops->destroy         = STDestroy_Cayley;
370:   st->ops->view            = STView_Cayley;
371:   st->ops->checknullspace  = STCheckNullSpace_Default;
372:   st->ops->setdefaultksp   = STSetDefaultKSP_Default;
373:   PetscObjectComposeFunction((PetscObject)st,"STCayleySetAntishift_C",STCayleySetAntishift_Cayley);
374:   PetscObjectComposeFunction((PetscObject)st,"STCayleyGetAntishift_C",STCayleyGetAntishift_Cayley);
375:   return(0);
376: }