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