1 /*
2     This file should be included in NEW routines that compute the
3     differencing parameter for finite difference based matrix-free
4     methods.  For example, such routines can compute h for use in
5     Jacobian-vector products of the form
6 
7                        F(x+ha) - F(x)
8           F'(u)a  ~=  ----------------
9                             h
10 */
11 
12 #if !defined(__MFFD_H__)
13 #define __MFFD_H__
14 
15 #include <petscmat.h>         /*I  "petscmat.h"   I*/
16 #include <petsc/private/petscimpl.h>
17 
18 /*
19     Table of functions that manage the computation and understanding
20     of the parameter for finite difference based matrix-free computations
21 */
22 struct _MFOps {
23   PetscErrorCode (*compute)(MatMFFD,Vec,Vec,PetscScalar*,PetscBool * zeroa);
24   PetscErrorCode (*view)(MatMFFD,PetscViewer);
25   PetscErrorCode (*destroy)(MatMFFD);
26   PetscErrorCode (*setfromoptions)(PetscOptionItems*,MatMFFD);
27 };
28 
29 /* context for default matrix-free SNES */
30 struct _p_MatMFFD {
31   PETSCHEADER(struct _MFOps);
32   Vec            w;                                     /* work vector */
33   PetscReal      error_rel;                             /* square root of relative error in computing function */
34   PetscScalar    currenth;                              /* last differencing parameter h used */
35   PetscScalar    *historyh;                             /* history of differencing parameter h */
36   PetscInt       ncurrenth,maxcurrenth;
37   void           *hctx;
38   Mat            mat;                                   /* back reference to shell matrix that contains this */
39   PetscInt       recomputeperiod;                       /* how often the h is recomputed; default to 1 */
40   PetscInt       count;                                 /* used by recomputeperiod */
41   PetscErrorCode (*checkh)(void*,Vec,Vec,PetscScalar*);
42   void           *checkhctx;                            /* optional context used by MatMFFDSetCheckh() */
43 
44   PetscErrorCode (*func)(void*,Vec,Vec); /* function used for matrix free */
45   void           *funcctx;               /* the context for the function */
46   Vec            current_f;              /* location of F(u); used with F(u+h) */
47   PetscBool      current_f_allocated;
48   Vec            current_u;              /* location of u; used with F(u+h) */
49 
50   PetscErrorCode (*funci)(void*,PetscInt,Vec,PetscScalar*); /* Evaluates func_[i]() */
51   PetscErrorCode (*funcisetbase)(void*,Vec);                /* Sets base for future evaluations of func_[i]() */
52 
53   void           *ctx;            /* this is used by MatCreateSNESMF() to store the SNES object */
54 #if defined(PETSC_USE_COMPLEX)
55   PetscBool      usecomplex;      /* use Lyness complex number trick to compute the matrix-vector product */
56 #endif
57 };
58 
59 PETSC_EXTERN PetscFunctionList MatMFFDList;
60 PETSC_EXTERN PetscBool         MatMFFDRegisterAllCalled;
61 PETSC_EXTERN PetscErrorCode    MatMFFDRegisterAll(void);
62 
63 #endif
64