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 */ 11687625d7SJacob Faibussowitsch #ifndef PETSC_MFFDIMPL_H 12687625d7SJacob Faibussowitsch #define PETSC_MFFDIMPL_H 13d52ab7bdSBarry Smith 14c6db04a5SJed Brown #include <petscmat.h> /*I "petscmat.h" I*/ 15af0996ceSBarry Smith #include <petsc/private/petscimpl.h> 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); 25dbbe0bcdSBarry Smith PetscErrorCode (*setfromoptions)(MatMFFD, PetscOptionItems *); 26d52ab7bdSBarry Smith }; 27d52ab7bdSBarry Smith 283a692a50SPatrick Sanan /* context for default matrix-free SNES */ 293a692a50SPatrick Sanan struct _p_MatMFFD { 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 43*01c1178eSBarry 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 525eb111a0SBarry Smith void *ctx; /* this is used by MatCreateSNESMF() to store the SNES object */ 53c92e3469SBarry Smith #if defined(PETSC_USE_COMPLEX) 54c92e3469SBarry Smith PetscBool usecomplex; /* use Lyness complex number trick to compute the matrix-vector product */ 55c92e3469SBarry Smith #endif 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 62687625d7SJacob Faibussowitsch #endif // PETSC_MFFDIMPL_H 63