xref: /petsc/src/mat/impls/mffd/mffdimpl.h (revision c3bb7e237024dedbcb903d02d33525e40bfdf812)
1d52ab7bdSBarry Smith /*
2d52ab7bdSBarry Smith     This file should be included in NEW routines that compute the
3d52ab7bdSBarry Smith     differencing parameter for finite difference based matrix-free
4d52ab7bdSBarry Smith     methods.  For example, such routines can compute h for use in
5d52ab7bdSBarry Smith     Jacobian-vector products of the form
6d52ab7bdSBarry Smith 
7d52ab7bdSBarry Smith                        F(x+ha) - F(x)
8d52ab7bdSBarry Smith           F'(u)a  ~=  ----------------
9d52ab7bdSBarry Smith                             h
10d52ab7bdSBarry Smith */
11d52ab7bdSBarry Smith 
12d52ab7bdSBarry Smith #if !defined(__MFFD_H__)
13d52ab7bdSBarry Smith #define __MFFD_H__
14d52ab7bdSBarry Smith 
15d52ab7bdSBarry Smith #include "petscmat.h"         /*I  "petscmat.h"   I*/
16d52ab7bdSBarry Smith 
17d52ab7bdSBarry Smith /*
18d52ab7bdSBarry Smith     Table of functions that manage the computation and understanding
19d52ab7bdSBarry Smith     of the parameter for finite difference based matrix-free computations
20d52ab7bdSBarry Smith */
21d52ab7bdSBarry Smith struct _MFOps {
22ace3abfcSBarry Smith   PetscErrorCode (*compute)(MatMFFD,Vec,Vec,PetscScalar *,PetscBool * zeroa);
23d52ab7bdSBarry Smith   PetscErrorCode (*view)(MatMFFD,PetscViewer);
24d52ab7bdSBarry Smith   PetscErrorCode (*destroy)(MatMFFD);
25d52ab7bdSBarry Smith   PetscErrorCode (*setfromoptions)(MatMFFD);
26d52ab7bdSBarry Smith };
27d52ab7bdSBarry Smith 
28d52ab7bdSBarry Smith struct _p_MatMFFD {    /* context for default matrix-free SNES */
29d52ab7bdSBarry Smith   PETSCHEADER(struct _MFOps);
30d52ab7bdSBarry Smith   Vec              w;                      /* work vector */
31d52ab7bdSBarry Smith   MatNullSpace     sp;                     /* null space context */
32d52ab7bdSBarry Smith   PetscReal        error_rel;              /* square root of relative error in computing function */
33d52ab7bdSBarry Smith   PetscScalar      currenth;               /* last differencing parameter h used */
34d52ab7bdSBarry Smith   PetscScalar      *historyh;              /* history of differencing parameter h */
35d52ab7bdSBarry Smith   PetscInt         ncurrenth,maxcurrenth;
36d52ab7bdSBarry Smith   void             *hctx;
37d52ab7bdSBarry Smith   Mat              mat;                    /* back reference to shell matrix that contains this */
38d52ab7bdSBarry Smith   PetscInt         recomputeperiod;        /* how often the h is recomputed; default to 1 */
39d52ab7bdSBarry Smith   PetscInt         count;                  /* used by recomputeperiod */
40d52ab7bdSBarry Smith   PetscErrorCode   (*checkh)(void*,Vec,Vec,PetscScalar*);
41d52ab7bdSBarry Smith   void             *checkhctx;             /* optional context used by MatMFFDSetCheckh() */
42d52ab7bdSBarry Smith 
43d52ab7bdSBarry Smith   PetscErrorCode   (*func)(void*,Vec,Vec);  /* function used for matrix free */
44d52ab7bdSBarry Smith   void             *funcctx;                     /* the context for the function */
45d52ab7bdSBarry Smith   Vec              current_f;                    /* location of F(u); used with F(u+h) */
46ace3abfcSBarry Smith   PetscBool        current_f_allocated;
47d52ab7bdSBarry Smith   Vec              current_u;                    /* location of u; used with F(u+h) */
48d52ab7bdSBarry Smith 
49d52ab7bdSBarry Smith   PetscErrorCode   (*funci)(void*,PetscInt,Vec,PetscScalar*);  /* Evaluates func_[i]() */
50d52ab7bdSBarry Smith   PetscErrorCode   (*funcisetbase)(void*,Vec);            /* Sets base for future evaluations of func_[i]() */
51d52ab7bdSBarry Smith 
52*c3bb7e23SBarry Smith   PetscScalar      vscale,vshift;              /* diagonal scale and shift by scalars */
53*c3bb7e23SBarry Smith   Vec              dlscale,drscale,dshift;              /* diagonal scale and shift by vectors */
54d52ab7bdSBarry Smith };
55d52ab7bdSBarry Smith 
56b022a5c1SBarry Smith EXTERN PetscFList MatMFFDList;
57ace3abfcSBarry Smith EXTERN PetscBool  MatMFFDRegisterAllCalled;
58d52ab7bdSBarry Smith 
59d52ab7bdSBarry Smith #endif
60