xref: /petsc/src/mat/impls/mffd/mffdimpl.h (revision c51ad4d444e46f231c316650da19e5770c6fae2b)
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*/
16af0996ceSBarry Smith #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);
264416b707SBarry Smith   PetscErrorCode (*setfromoptions)(PetscOptionItems*,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   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 
52c3bb7e23SBarry Smith   PetscScalar vscale,vshift;   /* diagonal scale and shift by scalars */
53*c51ad4d4SStefano Zampini   Vec         dlscale,drscale; /* diagonal scale */
54*c51ad4d4SStefano Zampini   Vec         dshift,dshiftw;  /* shift by vectors */
555eb111a0SBarry 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;
6014acc2feSToby Isaac PETSC_EXTERN PetscErrorCode    MatMFFDRegisterAll(void);
61d52ab7bdSBarry Smith 
62d52ab7bdSBarry Smith #endif
63