Actual source code: kspimpl.h

      2: #ifndef _KSPIMPL
      3: #define _KSPIMPL

     5:  #include petscksp.h

      7: typedef struct _KSPOps *KSPOps;

      9: struct _KSPOps {
     10:   PetscErrorCode (*buildsolution)(KSP,Vec,Vec*);       /* Returns a pointer to the solution, or
     11:                                                 calculates the solution in a
     12:                                                 user-provided area. */
     13:   PetscErrorCode (*buildresidual)(KSP,Vec,Vec,Vec*);   /* Returns a pointer to the residual, or
     14:                                                 calculates the residual in a
     15:                                                 user-provided area.  */
     16:   PetscErrorCode (*solve)(KSP);                        /* actual solver */
     17:   PetscErrorCode (*setup)(KSP);
     18:   PetscErrorCode (*setfromoptions)(KSP);
     19:   PetscErrorCode (*publishoptions)(KSP);
     20:   PetscErrorCode (*computeextremesingularvalues)(KSP,PetscReal*,PetscReal*);
     21:   PetscErrorCode (*computeeigenvalues)(KSP,PetscInt,PetscReal*,PetscReal*,PetscInt *);
     22:   PetscErrorCode (*destroy)(KSP);
     23:   PetscErrorCode (*view)(KSP,PetscViewer);
     24: };

     26: /*
     27:      Maximum number of monitors you can run with a single KSP
     28: */
     29: #define MAXKSPMONITORS 5

     31: /*
     32:    Defines the KSP data structure.
     33: */
     34: struct _p_KSP {
     35:   PETSCHEADER(struct _KSPOps);
     36:   /*------------------------- User parameters--------------------------*/
     37:   PetscInt max_it;                     /* maximum number of iterations */
     38:   PetscTruth    guess_zero,                  /* flag for whether initial guess is 0 */
     39:                 calc_sings,                  /* calculate extreme Singular Values */
     40:                 guess_knoll;                /* use initial guess of PCApply(ksp->B,b */
     41:   PCSide pc_side;                  /* flag for left, right, or symmetric
     42:                                       preconditioning */
     43:   PetscReal rtol,                     /* relative tolerance */
     44:             abstol,                     /* absolute tolerance */
     45:             ttol,                     /* (not set by user)  */
     46:             divtol;                   /* divergence tolerance */
     47:   PetscReal rnorm0;                   /* initial residual norm (used for divergence testing) */
     48:   PetscReal rnorm;                    /* current residual norm */
     49:   KSPConvergedReason reason;
     50:   PetscTruth         printreason;     /* prints converged reason after solve */

     52:   Vec vec_sol,vec_rhs;            /* pointer to where user has stashed
     53:                                       the solution and rhs, these are
     54:                                       never touched by the code, only
     55:                                       passed back to the user */
     56:   PetscReal     *res_hist;            /* If !0 stores residual at iterations*/
     57:   PetscInt           res_hist_len;         /* current size of residual history array */
     58:   PetscInt           res_hist_max;         /* actual amount of data in residual_history */
     59:   PetscTruth    res_hist_reset;       /* reset history to size zero for each new solve */

     61:   /* --------User (or default) routines (most return -1 on error) --------*/
     62:   PetscErrorCode (*monitor[MAXKSPMONITORS])(KSP,PetscInt,PetscReal,void*); /* returns control to user after */
     63:   PetscErrorCode (*monitordestroy[MAXKSPMONITORS])(void*);         /* */
     64:   void *monitorcontext[MAXKSPMONITORS];                  /* residual calculation, allows user */
     65:   PetscInt  numbermonitors;                                   /* to, for instance, print residual norm, etc. */

     67:   PetscErrorCode (*converged)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*);
     68:   void       *cnvP;

     70:   PC         pc;

     72:   void       *data;                      /* holder for misc stuff associated
     73:                                    with a particular iterative solver */

     75:   /* ----------------Default work-area management -------------------- */
     76:   PetscInt    nwork;
     77:   Vec         *work;

     79:   PetscInt    setupcalled;

     81:   PetscInt    its;       /* number of iterations so far computed */

     83:   PetscTruth  transpose_solve;    /* solve transpose system instead */

     85:   KSPNormType normtype;          /* type of norm used for convergence tests */

     87:   /*   Allow diagonally scaling the matrix before computing the preconditioner or using
     88:        the Krylov method. Note this is NOT just Jacobi preconditioning */

     90:   PetscTruth   dscale;       /* diagonal scale system; used with KSPSetDiagonalScale() */
     91:   PetscTruth   dscalefix;    /* unscale system after solve */
     92:   PetscTruth   dscalefix2;   /* system has been unscaled */
     93:   Vec          diagonal;     /* 1/sqrt(diag of matrix) */
     94:   Vec          truediagonal;

     96:   MatNullSpace nullsp;      /* Null space of the operator, removed from Krylov space */
     97: };

     99: #define KSPLogResidualHistory(ksp,norm) \
    100:     {if (ksp->res_hist && ksp->res_hist_max > ksp->res_hist_len) \
    101:      ksp->res_hist[ksp->res_hist_len++] = norm;}

    103: #define KSPMonitor(ksp,it,rnorm) \
    104:         { PetscErrorCode _ierr; PetscInt _i,_im = ksp->numbermonitors; \
    105:           for (_i=0; _i<_im; _i++) {\
    106:             _(*ksp->monitor[_i])(ksp,it,rnorm,ksp->monitorcontext[_i]);CHKERRQ(_ierr); \
    107:           } \
    108:         }

    110: EXTERN PetscErrorCode KSPDefaultDestroy(KSP);
    111: EXTERN PetscErrorCode KSPGetVecs(KSP,PetscInt,Vec**);
    112: EXTERN PetscErrorCode KSPDefaultGetWork(KSP,PetscInt);
    113: EXTERN PetscErrorCode KSPDefaultFreeWork(KSP);

    115: /*
    116:        These allow the various Krylov methods to apply to either the linear system or its transpose.
    117: */
    118: #define KSP_RemoveNullSpace(ksp,y) ((ksp->nullsp && ksp->pc_side == PC_LEFT) ? MatNullSpaceRemove(ksp->nullsp,y,PETSC_NULL) : 0)

    120: #define KSP_MatMult(ksp,A,x,y)          (!ksp->transpose_solve) ? MatMult(A,x,y)                                                            : MatMultTranspose(A,x,y)
    121: #define KSP_MatMultTranspose(ksp,A,x,y) (!ksp->transpose_solve) ? MatMultTranspose(A,x,y)                                                   : MatMult(A,x,y)
    122: #define KSP_PCApply(ksp,x,y)            (!ksp->transpose_solve) ? (PCApply(ksp->pc,x,y) || KSP_RemoveNullSpace(ksp,y))                      : PCApplyTranspose(ksp->pc,x,y)
    123: #define KSP_PCApplyTranspose(ksp,x,y)   (!ksp->transpose_solve) ? PCApplyTranspose(ksp->pc,x,y)                                             : (PCApply(ksp->pc,x,y) || KSP_RemoveNullSpace(ksp,y))
    124: #define KSP_PCApplyBAorAB(ksp,x,y,w)    (!ksp->transpose_solve) ? (PCApplyBAorAB(ksp->pc,ksp->pc_side,x,y,w) || KSP_RemoveNullSpace(ksp,y)) : PCApplyBAorABTranspose(ksp->pc,ksp->pc_side,x,y,w)

    126: #endif