xref: /petsc/src/mat/impls/mffd/mffdimpl.h (revision 5eb111a03e391458e3ba05c647d400a4171c441a)
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 
15c6db04a5SJed Brown #include <petscmat.h>         /*I  "petscmat.h"   I*/
16afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>
17d52ab7bdSBarry Smith 
18d52ab7bdSBarry Smith /*
19d52ab7bdSBarry Smith     Table of functions that manage the computation and understanding
20d52ab7bdSBarry Smith     of the parameter for finite difference based matrix-free computations
21d52ab7bdSBarry Smith */
22d52ab7bdSBarry Smith struct _MFOps {
23ace3abfcSBarry Smith   PetscErrorCode (*compute)(MatMFFD,Vec,Vec,PetscScalar*,PetscBool * zeroa);
24d52ab7bdSBarry Smith   PetscErrorCode (*view)(MatMFFD,PetscViewer);
25d52ab7bdSBarry Smith   PetscErrorCode (*destroy)(MatMFFD);
26d52ab7bdSBarry Smith   PetscErrorCode (*setfromoptions)(MatMFFD);
27d52ab7bdSBarry Smith };
28d52ab7bdSBarry Smith 
29d52ab7bdSBarry Smith struct _p_MatMFFD {    /* context for default matrix-free SNES */
30d52ab7bdSBarry Smith   PETSCHEADER(struct _MFOps);
31d52ab7bdSBarry Smith   Vec            w;                        /* work vector */
32d52ab7bdSBarry Smith   MatNullSpace   sp;                       /* null space context */
33d52ab7bdSBarry Smith   PetscReal      error_rel;                /* square root of relative error in computing function */
34d52ab7bdSBarry Smith   PetscScalar    currenth;                 /* last differencing parameter h used */
35d52ab7bdSBarry Smith   PetscScalar    *historyh;                /* history of differencing parameter h */
36d52ab7bdSBarry Smith   PetscInt       ncurrenth,maxcurrenth;
37d52ab7bdSBarry Smith   void           *hctx;
38d52ab7bdSBarry Smith   Mat            mat;                      /* back reference to shell matrix that contains this */
39d52ab7bdSBarry Smith   PetscInt       recomputeperiod;          /* how often the h is recomputed; default to 1 */
40d52ab7bdSBarry Smith   PetscInt       count;                    /* used by recomputeperiod */
41d52ab7bdSBarry Smith   PetscErrorCode (*checkh)(void*,Vec,Vec,PetscScalar*);
42d52ab7bdSBarry Smith   void           *checkhctx;               /* optional context used by MatMFFDSetCheckh() */
43d52ab7bdSBarry Smith 
44d52ab7bdSBarry Smith   PetscErrorCode (*func)(void*,Vec,Vec);    /* function used for matrix free */
45d52ab7bdSBarry Smith   void           *funcctx;                       /* the context for the function */
46d52ab7bdSBarry Smith   Vec            current_f;                      /* location of F(u); used with F(u+h) */
47ace3abfcSBarry Smith   PetscBool      current_f_allocated;
48d52ab7bdSBarry Smith   Vec            current_u;                      /* location of u; used with F(u+h) */
49d52ab7bdSBarry Smith 
50d52ab7bdSBarry Smith   PetscErrorCode (*funci)(void*,PetscInt,Vec,PetscScalar*);    /* Evaluates func_[i]() */
51d52ab7bdSBarry Smith   PetscErrorCode (*funcisetbase)(void*,Vec);              /* Sets base for future evaluations of func_[i]() */
52d52ab7bdSBarry Smith 
53c3bb7e23SBarry Smith   PetscScalar vscale,vshift;                   /* diagonal scale and shift by scalars */
54c3bb7e23SBarry Smith   Vec         dlscale,drscale,dshift;                   /* diagonal scale and shift by vectors */
55*5eb111a0SBarry Smith   void        *ctx;   /* this is used by MatCreateSNESMF() to store the SNES object */
56d52ab7bdSBarry Smith };
57d52ab7bdSBarry Smith 
585a576424SJed Brown PETSC_EXTERN PetscFunctionList MatMFFDList;
595a576424SJed Brown PETSC_EXTERN PetscBool         MatMFFDRegisterAllCalled;
60d52ab7bdSBarry Smith 
61d52ab7bdSBarry Smith #endif
62