xref: /petsc/src/mat/impls/mffd/mffdimpl.h (revision 1b37a2a7cc4a4fb30c3e967db1c694c0a1013f51)
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 #pragma once
12 
13 #include <petscmat.h> /*I  "petscmat.h"   I*/
14 #include <petsc/private/petscimpl.h>
15 
16 /*
17  Table of functions that manage the computation and understanding
18  of the parameter for finite difference based matrix-free computations
19 */
20 struct _MFOps {
21   PetscErrorCode (*compute)(MatMFFD, Vec, Vec, PetscScalar *, PetscBool *zeroa);
22   PetscErrorCode (*view)(MatMFFD, PetscViewer);
23   PetscErrorCode (*destroy)(MatMFFD);
24   PetscErrorCode (*setfromoptions)(MatMFFD, PetscOptionItems *);
25 };
26 
27 /* context for default matrix-free SNES */
28 struct _p_MatMFFD {
29   PETSCHEADER(struct _MFOps);
30   Vec          w;         /* work vector */
31   PetscReal    error_rel; /* square root of relative error in computing function */
32   PetscScalar  currenth;  /* last differencing parameter h used */
33   PetscScalar *historyh;  /* history of differencing parameter h */
34   PetscInt     ncurrenth, maxcurrenth;
35   void        *hctx;
36   Mat          mat;             /* back reference to shell matrix that contains this */
37   PetscInt     recomputeperiod; /* how often the h is recomputed; default to 1 */
38   PetscInt     count;           /* used by recomputeperiod */
39   PetscErrorCode (*checkh)(void *, Vec, Vec, PetscScalar *);
40   void *checkhctx; /* optional context used by MatMFFDSetCheckh() */
41 
42   PetscErrorCode (*func)(void *, Vec, Vec); /* function used for matrix-free */
43   void     *funcctx;                        /* the context for the function */
44   Vec       current_f;                      /* location of F(u); used with F(u+h) */
45   PetscBool current_f_allocated;
46   Vec       current_u; /* location of u; used with F(u+h) */
47 
48   PetscErrorCode (*funci)(void *, PetscInt, Vec, PetscScalar *); /* Evaluates func_[i]() */
49   PetscErrorCode (*funcisetbase)(void *, Vec);                   /* Sets base for future evaluations of func_[i]() */
50 
51   void *ctx; /* this is used by MatCreateSNESMF() to store the SNES object */
52 #if defined(PETSC_USE_COMPLEX)
53   PetscBool usecomplex; /* use Lyness complex number trick to compute the matrix-vector product */
54 #endif
55 };
56 
57 PETSC_EXTERN PetscFunctionList MatMFFDList;
58 PETSC_EXTERN PetscBool         MatMFFDRegisterAllCalled;
59 PETSC_EXTERN PetscErrorCode    MatMFFDRegisterAll(void);
60