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 29*3a692a50SPatrick Sanan /* context for default matrix-free SNES */ 30*3a692a50SPatrick Sanan struct _p_MatMFFD { 31d52ab7bdSBarry Smith PETSCHEADER(struct _MFOps); 32d52ab7bdSBarry Smith Vec w; /* work vector */ 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 */ 54c51ad4d4SStefano Zampini Vec dlscale,drscale; /* diagonal scale */ 55c51ad4d4SStefano Zampini Vec dshift,dshiftw; /* shift by vectors */ 565eb111a0SBarry Smith void *ctx; /* this is used by MatCreateSNESMF() to store the SNES object */ 57d52ab7bdSBarry Smith }; 58d52ab7bdSBarry Smith 595a576424SJed Brown PETSC_EXTERN PetscFunctionList MatMFFDList; 605a576424SJed Brown PETSC_EXTERN PetscBool MatMFFDRegisterAllCalled; 6114acc2feSToby Isaac PETSC_EXTERN PetscErrorCode MatMFFDRegisterAll(void); 62d52ab7bdSBarry Smith 63d52ab7bdSBarry Smith #endif 64