Actual source code: stimpl.h

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, 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: */

     11: #pragma once

     13: #include <slepcst.h>
     14: #include <slepc/private/slepcimpl.h>

     16: /* SUBMANSEC = ST */

     18: SLEPC_EXTERN PetscBool STRegisterAllCalled;
     19: SLEPC_EXTERN PetscErrorCode STRegisterAll(void);
     20: SLEPC_EXTERN PetscLogEvent ST_SetUp,ST_ComputeOperator,ST_Apply,ST_ApplyTranspose,ST_ApplyHermitianTranspose,ST_MatSetUp,ST_MatMult,ST_MatMultTranspose,ST_MatSolve,ST_MatSolveTranspose;

     22: typedef struct _STOps *STOps;

     24: struct _STOps {
     25:   PetscErrorCode (*apply)(ST,Vec,Vec);
     26:   PetscErrorCode (*applymat)(ST,Mat,Mat);
     27:   PetscErrorCode (*applytrans)(ST,Vec,Vec);
     28:   PetscErrorCode (*applyhermtrans)(ST,Vec,Vec);
     29:   PetscErrorCode (*backtransform)(ST,PetscInt,PetscScalar*,PetscScalar*);
     30:   PetscErrorCode (*setshift)(ST,PetscScalar);
     31:   PetscErrorCode (*getbilinearform)(ST,Mat*);
     32:   PetscErrorCode (*setup)(ST);
     33:   PetscErrorCode (*computeoperator)(ST);
     34:   PetscErrorCode (*setfromoptions)(ST,PetscOptionItems);
     35:   PetscErrorCode (*postsolve)(ST);
     36:   PetscErrorCode (*destroy)(ST);
     37:   PetscErrorCode (*reset)(ST);
     38:   PetscErrorCode (*view)(ST,PetscViewer);
     39:   PetscErrorCode (*checknullspace)(ST,BV);
     40:   PetscErrorCode (*setdefaultksp)(ST);
     41: };

     43: /*
     44:      'Updated' state means STSetUp must be called because matrices have been
     45:      modified, but the pattern is the same (hence reuse symbolic factorization)
     46: */
     47: typedef enum { ST_STATE_INITIAL,
     48:                ST_STATE_SETUP,
     49:                ST_STATE_UPDATED } STStateType;

     51: struct _p_ST {
     52:   PETSCHEADER(struct _STOps);
     53:   /*------------------------- User parameters --------------------------*/
     54:   Mat              *A;               /* matrices that define the eigensystem */
     55:   PetscInt         nmat;             /* number of user-provided matrices */
     56:   PetscScalar      sigma;            /* value of the shift */
     57:   PetscScalar      defsigma;         /* default value of the shift */
     58:   STMatMode        matmode;          /* how the transformation matrix is handled */
     59:   MatStructure     str;              /* whether matrices have the same pattern or not */
     60:   PetscBool        transform;        /* whether transformed matrices are computed */
     61:   PetscBool        structured;       /* whether the operator is structured or not */
     62:   Vec              D;                /* diagonal matrix for balancing */
     63:   Mat              Pmat;             /* user-provided preconditioner matrix */
     64:   PetscBool        Pmat_set;         /* whether the user provided a preconditioner matrix or not  */
     65:   Mat              *Psplit;          /* matrices for the split preconditioner */
     66:   PetscInt         nsplit;           /* number of split preconditioner matrices */
     67:   MatStructure     strp;             /* pattern of split preconditioner matrices */

     69:   /*------------------------- Misc data --------------------------*/
     70:   KSP              ksp;              /* linear solver used in some ST's */
     71:   PetscBool        usesksp;          /* whether the KSP object is used or not */
     72:   PetscInt         nwork;            /* number of work vectors */
     73:   Vec              *work;            /* work vectors */
     74:   Vec              wb;               /* balancing requires an extra work vector */
     75:   Vec              wht;              /* extra work vector for hermitian transpose apply */
     76:   STStateType      state;            /* initial -> setup -> with updated matrices */
     77:   PetscObjectState *Astate;          /* matrix state (to identify the original matrices) */
     78:   Mat              *T;               /* matrices resulting from transformation */
     79:   Mat              Op;               /* shell matrix for operator = alpha*D*inv(P)*M*inv(D) */
     80:   PetscBool        opseized;         /* whether Op has been seized by user */
     81:   PetscBool        opready;          /* whether Op is up-to-date or need be computed  */
     82:   Mat              P;                /* matrix from which preconditioner is built */
     83:   Mat              M;                /* matrix corresponding to the non-inverted part of the operator */
     84:   PetscBool        sigma_set;        /* whether the user provided the shift or not */
     85:   PetscBool        asymm;            /* the user matrices are all symmetric */
     86:   PetscBool        aherm;            /* the user matrices are all hermitian */
     87:   void             *data;
     88: };

     90: /*
     91:     Macros to test valid ST arguments
     92: */
     93: #if !defined(PETSC_USE_DEBUG)

     95: #define STCheckMatrices(h,arg) do {(void)(h);} while (0)
     96: #define STCheckNotSeized(h,arg) do {(void)(h);} while (0)

     98: #else

    100: #define STCheckMatrices(h,arg) \
    101:   do { \
    102:     PetscCheck((h)->A,PetscObjectComm((PetscObject)(h)),PETSC_ERR_ARG_WRONGSTATE,"ST matrices have not been set: Parameter #%d",arg); \
    103:   } while (0)
    104: #define STCheckNotSeized(h,arg) \
    105:   do { \
    106:     PetscCheck(!(h)->opseized,PetscObjectComm((PetscObject)(h)),PETSC_ERR_ARG_WRONGSTATE,"Must call STRestoreOperator() first: Parameter #%d",arg); \
    107:   } while (0)

    109: #endif

    111: SLEPC_INTERN PetscErrorCode STGetBilinearForm_Default(ST,Mat*);
    112: SLEPC_INTERN PetscErrorCode STCheckNullSpace_Default(ST,BV);
    113: SLEPC_INTERN PetscErrorCode STMatShellCreate(ST,PetscScalar,PetscInt,PetscInt*,PetscScalar*,Mat*);
    114: SLEPC_INTERN PetscErrorCode STMatShellShift(Mat,PetscScalar);
    115: SLEPC_INTERN PetscErrorCode STCheckFactorPackage(ST);
    116: SLEPC_INTERN PetscErrorCode STMatMAXPY_Private(ST,PetscScalar,PetscScalar,PetscInt,PetscScalar*,PetscBool,PetscBool,Mat*);
    117: SLEPC_INTERN PetscErrorCode STCoeffs_Monomial(ST,PetscScalar*);
    118: SLEPC_INTERN PetscErrorCode STSetDefaultKSP(ST);
    119: SLEPC_INTERN PetscErrorCode STSetDefaultKSP_Default(ST);
    120: SLEPC_INTERN PetscErrorCode STIsInjective_Shell(ST,PetscBool*);
    121: SLEPC_INTERN PetscErrorCode STComputeOperator(ST);
    122: SLEPC_INTERN PetscErrorCode STGetOperator_Private(ST,Mat*);
    123: SLEPC_INTERN PetscErrorCode STApply_Generic(ST,Vec,Vec);
    124: SLEPC_INTERN PetscErrorCode STApplyMat_Generic(ST,Mat,Mat);
    125: SLEPC_INTERN PetscErrorCode STApplyTranspose_Generic(ST,Vec,Vec);
    126: SLEPC_INTERN PetscErrorCode STApplyHermitianTranspose_Generic(ST,Vec,Vec);

    128: /*
    129:   ST_KSPSetOperators - Sets the KSP matrices
    130: */
    131: static inline PetscErrorCode ST_KSPSetOperators(ST st,Mat A,Mat B)
    132: {
    133:   const char     *prefix;

    135:   PetscFunctionBegin;
    136:   if (!st->ksp) PetscCall(STGetKSP(st,&st->ksp));
    137:   PetscCall(STCheckFactorPackage(st));
    138:   PetscCall(KSPSetOperators(st->ksp,A,B));
    139:   PetscCall(MatGetOptionsPrefix(B,&prefix));
    140:   if (!prefix) {
    141:     /* set Mat prefix to be the same as KSP to enable setting command-line options (e.g. MUMPS)
    142:        only applies if the Mat has no user-defined prefix */
    143:     PetscCall(KSPGetOptionsPrefix(st->ksp,&prefix));
    144:     PetscCall(MatSetOptionsPrefix(B,prefix));
    145:   }
    146:   PetscFunctionReturn(PETSC_SUCCESS);
    147: }