Date: 2015-09-03T15:03:58+00:00

Actual source code: cayley.c
slepc-3.6.1 2015-09-03
Report Typos and Errors

      1: /*
      2:       Implements the Cayley spectral transform.

      4:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      5:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      6:    Copyright (c) 2002-2015, 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->setupcalled = 0;
    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,PETSC_TRUE,&st->T[0]);
    176:   }

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

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

    202:   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:   /* Nothing to be done if STSetUp has not been called yet */
    205:   if (!st->setupcalled) return(0);

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

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

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

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

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

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

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

    284:    Logically Collective on ST

    286:    Input Parameters:
    287: +  st  - the spectral transformation context
    288: -  nu  - the anti-shift

    290:    Options Database Key:
    291: .  -st_cayley_antishift - Sets the value of the anti-shift

    293:    Level: intermediate

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

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

    309:   PetscTryMethod(st,"STCayleySetAntishift_C",(ST,PetscScalar),(st,nu));
    310:   return(0);
    311: }
    314: static PetscErrorCode STCayleyGetAntishift_Cayley(ST st,PetscScalar *nu)
    315: {
    316:   ST_CAYLEY *ctx = (ST_CAYLEY*)st->data;

    319:   *nu = ctx->nu;
    320:   return(0);
    321: }

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

    329:    Not Collective

    331:    Input Parameter:
    332: .  st  - the spectral transformation context

    334:    Output Parameter:
    335: .  nu  - the anti-shift

    337:    Level: intermediate

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

    348:   PetscTryMethod(st,"STCayleyGetAntishift_C",(ST,PetscScalar*),(st,nu));
    349:   return(0);
    350: }

    354: PetscErrorCode STView_Cayley(ST st,PetscViewer viewer)
    355: {
    357:   char           str[50];
    358:   ST_CAYLEY      *ctx = (ST_CAYLEY*)st->data;

    361:   SlepcSNPrintfScalar(str,50,ctx->nu,PETSC_FALSE);
    362:   PetscViewerASCIIPrintf(viewer,"  Cayley: antishift: %s\n",str);
    363:   return(0);
    364: }

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

    374:   VecDestroy(&ctx->w2);
    375:   return(0);
    376: }

    380: PetscErrorCode STDestroy_Cayley(ST st)
    381: {

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

    393: PETSC_EXTERN PetscErrorCode STCreate_Cayley(ST st)
    394: {
    396:   ST_CAYLEY      *ctx;

    399:   PetscNewLog(st,&ctx);
    400:   st->data = (void*)ctx;

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