Actual source code: kspimpl.h

  2: #ifndef _KSPIMPL_H
  3: #define _KSPIMPL_H

 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:   PetscTruth defaultconvergedinitialrtol; /* default relative residual decrease is computing from initial residual, not rhs */
 48:   PetscTruth defaultconvergedmininitialrtol; /* default relative residual decrease is computing from min of initial residual and rhs */
 49:   PetscReal rnorm0;                   /* initial residual norm (used for divergence testing) */
 50:   PetscReal rnorm;                    /* current residual norm */
 51:   KSPConvergedReason reason;
 52:   PetscTruth         printreason;     /* prints converged reason after solve */

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

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

 69:   PetscErrorCode (*converged)(KSP,PetscInt,PetscReal,KSPConvergedReason*,void*);
 70:   void       *cnvP;

 72:   PC         pc;

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

 77:   /* ----------------Default work-area management -------------------- */
 78:   PetscInt    nwork;
 79:   Vec         *work;

 81:   PetscInt    setupcalled;

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

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

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

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

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

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

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

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

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

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

122: #define KSP_MatMult(ksp,A,x,y)          (!ksp->transpose_solve) ? MatMult(A,x,y)                                                            : MatMultTranspose(A,x,y) 
123: #define KSP_MatMultTranspose(ksp,A,x,y) (!ksp->transpose_solve) ? MatMultTranspose(A,x,y)                                                   : MatMult(A,x,y) 
124: #define KSP_PCApply(ksp,x,y)            (!ksp->transpose_solve) ? (PCApply(ksp->pc,x,y) || KSP_RemoveNullSpace(ksp,y))                      : PCApplyTranspose(ksp->pc,x,y) 
125: #define KSP_PCApplyTranspose(ksp,x,y)   (!ksp->transpose_solve) ? PCApplyTranspose(ksp->pc,x,y)                                             : (PCApply(ksp->pc,x,y) || KSP_RemoveNullSpace(ksp,y)) 
126: #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)


130: #endif