xref: /petsc/src/snes/mf/snesmfj.c (revision 52e6d16ba989af9362d0fcebb12e73dd7c9666ed)
181e6777dSBarry Smith 
27e9d5209SBarry Smith #include "src/mat/matimpl.h"
3325e03aeSBarry Smith #include "src/snes/mf/snesmfj.h"   /*I  "petscsnes.h"   I*/
481e6777dSBarry Smith 
5b0a32e0cSBarry Smith PetscFList MatSNESMPetscFList         = 0;
64c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE;
7a4d4d686SBarry Smith 
84a2ae208SSatish Balay #undef __FUNCT__
94a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetType"
10fd4bdd07SBarry Smith /*@C
1165f2ba5bSLois Curfman McInnes     MatSNESMFSetType - Sets the method that is used to compute the
12b0a32e0cSBarry Smith     differencing parameter for finite differene matrix-free formulations.
139a6cb015SBarry Smith 
149a6cb015SBarry Smith     Input Parameters:
157e9d5209SBarry Smith +   mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMF()
167e9d5209SBarry Smith           or MatSetType(mat,MATMFFD);
179a6cb015SBarry Smith -   ftype - the type requested
189a6cb015SBarry Smith 
1915091d37SBarry Smith     Level: advanced
2015091d37SBarry Smith 
2165f2ba5bSLois Curfman McInnes     Notes:
2265f2ba5bSLois Curfman McInnes     For example, such routines can compute h for use in
2365f2ba5bSLois Curfman McInnes     Jacobian-vector products of the form
2465f2ba5bSLois Curfman McInnes 
2565f2ba5bSLois Curfman McInnes                         F(x+ha) - F(x)
26ef4ad1fdSLois Curfman McInnes           F'(u)a  ~=  ----------------
2765f2ba5bSLois Curfman McInnes                               h
2865f2ba5bSLois Curfman McInnes 
29f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic)
309a6cb015SBarry Smith @*/
31dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetType(Mat mat,const MatSNESMFType ftype)
32b9fa9cd0SBarry Smith {
33dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(MatSNESMFCtx);
347e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
356831982aSBarry Smith   PetscTruth     match;
36a4d4d686SBarry Smith 
37a4d4d686SBarry Smith   PetscFunctionBegin;
384482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
394482741eSBarry Smith   PetscValidCharPointer(ftype,2);
400f5bd95cSBarry Smith 
419a6cb015SBarry Smith   /* already set, so just return */
426831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr);
430f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
44a4d4d686SBarry Smith 
459a6cb015SBarry Smith   /* destroy the old one if it exists */
469a6cb015SBarry Smith   if (ctx->ops->destroy) {
479a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
489a6cb015SBarry Smith   }
499a6cb015SBarry Smith 
5065f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
515a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
52b9617806SBarry Smith   ierr =  PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr);
53958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatSNESMF type %s given",ftype);
549a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
556831982aSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
569a6cb015SBarry Smith   PetscFunctionReturn(0);
579a6cb015SBarry Smith }
589a6cb015SBarry Smith 
596849ba73SBarry Smith typedef PetscErrorCode (*FCN1)(Vec,void*); /* force argument to next function to not be extern C*/
60c5c390f1SBarry Smith EXTERN_C_BEGIN
6187828ca2SBarry Smith #undef __FUNCT__
6287828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD"
63dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctioniBase_FD(Mat mat,FCN1 func)
6487828ca2SBarry Smith {
6587828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
6687828ca2SBarry Smith 
6787828ca2SBarry Smith   PetscFunctionBegin;
6887828ca2SBarry Smith   ctx->funcisetbase = func;
6987828ca2SBarry Smith   PetscFunctionReturn(0);
7087828ca2SBarry Smith }
71c5c390f1SBarry Smith EXTERN_C_END
7287828ca2SBarry Smith 
73a7cc72afSBarry Smith typedef PetscErrorCode (*FCN2)(PetscInt,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
74c5c390f1SBarry Smith EXTERN_C_BEGIN
7587828ca2SBarry Smith #undef __FUNCT__
7687828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD"
77dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctioni_FD(Mat mat,FCN2 funci)
7887828ca2SBarry Smith {
7987828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
8087828ca2SBarry Smith 
8187828ca2SBarry Smith   PetscFunctionBegin;
8287828ca2SBarry Smith   ctx->funci = funci;
8387828ca2SBarry Smith   PetscFunctionReturn(0);
8487828ca2SBarry Smith }
85c5c390f1SBarry Smith EXTERN_C_END
8687828ca2SBarry Smith 
879a6cb015SBarry Smith 
884a2ae208SSatish Balay #undef __FUNCT__
894a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
906849ba73SBarry Smith PetscErrorCode MatSNESMFRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatSNESMFCtx))
919a6cb015SBarry Smith {
92dfbe8321SBarry Smith   PetscErrorCode ierr;
93e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
949a6cb015SBarry Smith 
959a6cb015SBarry Smith   PetscFunctionBegin;
96b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
97c134de8dSSatish Balay   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
989a6cb015SBarry Smith   PetscFunctionReturn(0);
999a6cb015SBarry Smith }
1009a6cb015SBarry Smith 
1019a6cb015SBarry Smith 
1024a2ae208SSatish Balay #undef __FUNCT__
1034a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1049a6cb015SBarry Smith /*@C
1055a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
106f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1079a6cb015SBarry Smith 
1089a6cb015SBarry Smith    Not Collective
1099a6cb015SBarry Smith 
11015091d37SBarry Smith    Level: developer
11115091d37SBarry Smith 
1125a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1139a6cb015SBarry Smith 
114f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1159a6cb015SBarry Smith @*/
116dfbe8321SBarry Smith PetscErrorCode MatSNESMFRegisterDestroy(void)
1179a6cb015SBarry Smith {
118dfbe8321SBarry Smith   PetscErrorCode ierr;
1199a6cb015SBarry Smith 
1209a6cb015SBarry Smith   PetscFunctionBegin;
121b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
122b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
123b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1249a6cb015SBarry Smith   }
1254c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1269a6cb015SBarry Smith   PetscFunctionReturn(0);
1279a6cb015SBarry Smith }
1289a6cb015SBarry Smith 
1299a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
1304a2ae208SSatish Balay #undef __FUNCT__
1318a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD"
132dfbe8321SBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat)
133a4d4d686SBarry Smith {
134dfbe8321SBarry Smith   PetscErrorCode ierr;
1357e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
136fae171e0SBarry Smith 
1373a40ed3dSBarry Smith   PetscFunctionBegin;
138abc0a331SBarry Smith   if (ctx->w) {
139b9fa9cd0SBarry Smith     ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
140ba6a83e5SMatthew Knepley   }
1419a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
14274637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
143d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(ctx);CHKERRQ(ierr);
144901853e0SKris Buschelman 
145901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetBase_C","",PETSC_NULL);CHKERRQ(ierr);
146901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C","",PETSC_NULL);CHKERRQ(ierr);
147901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C","",PETSC_NULL);CHKERRQ(ierr);
148901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetCheckh_C","",PETSC_NULL);CHKERRQ(ierr);
149901853e0SKris Buschelman 
1503a40ed3dSBarry Smith   PetscFunctionReturn(0);
151b9fa9cd0SBarry Smith }
15250361f65SLois Curfman McInnes 
1534a2ae208SSatish Balay #undef __FUNCT__
1548a124369SBarry Smith #define __FUNCT__ "MatView_MFFD"
15539e2f89bSBarry Smith /*
1568a124369SBarry Smith    MatSNESMFView_MFFD - Views matrix-free parameters.
1578f6e3e37SBarry Smith 
15839e2f89bSBarry Smith */
159dfbe8321SBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer)
160eb9086c3SLois Curfman McInnes {
161dfbe8321SBarry Smith   PetscErrorCode ierr;
1627e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
16332077d6dSBarry Smith   PetscTruth     iascii;
164eb9086c3SLois Curfman McInnes 
1653a40ed3dSBarry Smith   PetscFunctionBegin;
16632077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
16732077d6dSBarry Smith   if (iascii) {
168b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
169b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
170473c83c3SBarry Smith      if (!ctx->type_name) {
171b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
172473c83c3SBarry Smith      } else {
173b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
174473c83c3SBarry Smith      }
1759a6cb015SBarry Smith      if (ctx->ops->view) {
1769a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1779a6cb015SBarry Smith      }
1785cd90555SBarry Smith   } else {
17979a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
180eb9086c3SLois Curfman McInnes   }
1813a40ed3dSBarry Smith   PetscFunctionReturn(0);
182eb9086c3SLois Curfman McInnes }
183eb9086c3SLois Curfman McInnes 
1844a2ae208SSatish Balay #undef __FUNCT__
1858a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD"
186be726c96SBarry Smith /*
18732dfb669SBarry Smith    MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This
18865f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
189be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
19065f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
19165f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
192be726c96SBarry Smith */
193dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt)
194be726c96SBarry Smith {
195dfbe8321SBarry Smith   PetscErrorCode ierr;
1967e9d5209SBarry Smith   MatSNESMFCtx   j = (MatSNESMFCtx)J->data;
197be726c96SBarry Smith 
198be726c96SBarry Smith   PetscFunctionBegin;
1995a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
200b0a32e0cSBarry Smith   if (j->usesnes) {
2011d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
2021d1367b7SBarry Smith     ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
203958c9bccSBarry Smith     if (!j->w) {
2042740c1caSMatthew Knepley       ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr);
2052740c1caSMatthew Knepley     }
2061d1367b7SBarry Smith   }
207c5c390f1SBarry Smith   j->vshift = 0.0;
208c5c390f1SBarry Smith   j->vscale = 1.0;
209be726c96SBarry Smith   PetscFunctionReturn(0);
210be726c96SBarry Smith }
211be726c96SBarry Smith 
2124a2ae208SSatish Balay #undef __FUNCT__
2138a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD"
214eb9086c3SLois Curfman McInnes /*
215adb62b0dSMatthew Knepley   MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
216a4d4d686SBarry Smith 
2179a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
218eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
219eb9086c3SLois Curfman McInnes         u = current iterate
220eb9086c3SLois Curfman McInnes         h = difference interval
221eb9086c3SLois Curfman McInnes */
222dfbe8321SBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y)
22339e2f89bSBarry Smith {
2247e9d5209SBarry Smith   MatSNESMFCtx    ctx = (MatSNESMFCtx)mat->data;
225fae171e0SBarry Smith   SNES            snes;
226ea709b57SSatish Balay   PetscScalar     h,mone = -1.0;
227fae171e0SBarry Smith   Vec             w,U,F;
228dfbe8321SBarry Smith   PetscErrorCode ierr,(*eval_fct)(SNES,Vec,Vec)=0;
22939e2f89bSBarry Smith 
2303a40ed3dSBarry Smith   PetscFunctionBegin;
2319a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2329a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2339a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2349a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
235d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
23656cd22aeSBarry Smith 
237fae171e0SBarry Smith   snes = ctx->snes;
238fae171e0SBarry Smith   w    = ctx->w;
2391d1367b7SBarry Smith   U    = ctx->current_u;
24050361f65SLois Curfman McInnes 
24185614651SBarry Smith   /*
24285614651SBarry Smith       Compute differencing parameter
24385614651SBarry Smith   */
2449a6cb015SBarry Smith   if (!ctx->ops->compute) {
2452f859189SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_WP);CHKERRQ(ierr);
2465a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2479a6cb015SBarry Smith   }
2489a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
249a4d4d686SBarry Smith 
2505b7f0c42SBarry Smith   if (ctx->checkh) {
2515b7f0c42SBarry Smith     ierr = (*ctx->checkh)(U,a,&h,ctx->checkhctx);CHKERRQ(ierr);
2525b7f0c42SBarry Smith   }
2535b7f0c42SBarry Smith 
254a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
255a4d4d686SBarry Smith   ctx->currenth = h;
256aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
2578a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h));
258a4d4d686SBarry Smith #else
2598a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h);
260a4d4d686SBarry Smith #endif
261a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
26285614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
263a4d4d686SBarry Smith   }
26485614651SBarry Smith   ctx->ncurrenth++;
265a4d4d686SBarry Smith 
26685614651SBarry Smith   /* w = u + ha */
267a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
26885614651SBarry Smith 
269b0a32e0cSBarry Smith   if (ctx->usesnes) {
27085614651SBarry Smith     eval_fct = SNESComputeFunction;
2711d1367b7SBarry Smith     F    = ctx->current_f;
2721302d50aSBarry Smith     if (!F) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You must call MatAssembly() even on matrix-free matrices");
27339903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
27485614651SBarry Smith   } else {
27585614651SBarry Smith     F = ctx->funcvec;
27685614651SBarry Smith     /* compute func(U) as base for differencing */
27785614651SBarry Smith     if (ctx->ncurrenth == 1) {
27885614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
27985614651SBarry Smith     }
28085614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
28185614651SBarry Smith   }
282a4d4d686SBarry Smith 
283a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
284a4d4d686SBarry Smith   h    = 1.0/h;
285a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
286c5c390f1SBarry Smith 
287c5c390f1SBarry Smith   ierr = VecAXPBY(&ctx->vshift,&ctx->vscale,a,y);CHKERRQ(ierr);
288c5c390f1SBarry Smith 
28974637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
290a4d4d686SBarry Smith 
291d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
292a4d4d686SBarry Smith   PetscFunctionReturn(0);
293a4d4d686SBarry Smith }
294a4d4d686SBarry Smith 
2954a2ae208SSatish Balay #undef __FUNCT__
2968a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD"
297cf57b110SBarry Smith /*
2988a124369SBarry Smith   MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix
299cf57b110SBarry Smith 
300cf57b110SBarry Smith         y ~= (F(u + ha) - F(u))/h,
301cf57b110SBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
302cf57b110SBarry Smith         u = current iterate
303cf57b110SBarry Smith         h = difference interval
304cf57b110SBarry Smith */
305dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a)
306cf57b110SBarry Smith {
3077e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
308ea709b57SSatish Balay   PetscScalar    h,*aa,*ww,v;
30977d8c4bbSBarry Smith   PetscReal      epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON;
31065df01d8SBarry Smith   Vec            w,U;
3116849ba73SBarry Smith   PetscErrorCode ierr;
312a7cc72afSBarry Smith   PetscInt       i,rstart,rend;
313cf57b110SBarry Smith 
314cf57b110SBarry Smith   PetscFunctionBegin;
315cf57b110SBarry Smith   if (!ctx->funci) {
3161302d50aSBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Requires calling MatSNESMFSetFunctioni() first");
317cf57b110SBarry Smith   }
318cf57b110SBarry Smith 
319cf57b110SBarry Smith   w    = ctx->w;
320cf57b110SBarry Smith   U    = ctx->current_u;
321cf57b110SBarry Smith   ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr);
322cf57b110SBarry Smith   ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr);
323cf57b110SBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
324cf57b110SBarry Smith 
325cf57b110SBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
326cf57b110SBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
327cf57b110SBarry Smith   for (i=rstart; i<rend; i++) {
328cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
329cf57b110SBarry Smith     h  = ww[i-rstart];
330cf57b110SBarry Smith     if (h == 0.0) h = 1.0;
331cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX)
332cf57b110SBarry Smith     if (h < umin && h >= 0.0)      h = umin;
333cf57b110SBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
334cf57b110SBarry Smith #else
335cf57b110SBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
336cf57b110SBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
337cf57b110SBarry Smith #endif
338cf57b110SBarry Smith     h     *= epsilon;
339cf57b110SBarry Smith 
340cf57b110SBarry Smith     ww[i-rstart] += h;
341cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
342cf57b110SBarry Smith     ierr          = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr);
343cf57b110SBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
344c5c390f1SBarry Smith 
345c5c390f1SBarry Smith     /* possibly shift and scale result */
346c5c390f1SBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
347c5c390f1SBarry Smith 
348cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
349cf57b110SBarry Smith     ww[i-rstart] -= h;
350cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
351cf57b110SBarry Smith   }
352cf57b110SBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
353cf57b110SBarry Smith   PetscFunctionReturn(0);
354cf57b110SBarry Smith }
355cf57b110SBarry Smith 
356cf57b110SBarry Smith #undef __FUNCT__
357c5c390f1SBarry Smith #define __FUNCT__ "MatShift_MFFD"
358dfbe8321SBarry Smith PetscErrorCode MatShift_MFFD(const PetscScalar *a,Mat Y)
359c5c390f1SBarry Smith {
360c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
361c5c390f1SBarry Smith   PetscFunctionBegin;
362c5c390f1SBarry Smith   shell->vshift += *a;
363c5c390f1SBarry Smith   PetscFunctionReturn(0);
364c5c390f1SBarry Smith }
365c5c390f1SBarry Smith 
366c5c390f1SBarry Smith #undef __FUNCT__
367c5c390f1SBarry Smith #define __FUNCT__ "MatScale_MFFD"
368dfbe8321SBarry Smith PetscErrorCode MatScale_MFFD(const PetscScalar *a,Mat Y)
369c5c390f1SBarry Smith {
370c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
371c5c390f1SBarry Smith   PetscFunctionBegin;
372c5c390f1SBarry Smith   shell->vscale *= *a;
373c5c390f1SBarry Smith   PetscFunctionReturn(0);
374c5c390f1SBarry Smith }
375c5c390f1SBarry Smith 
376c5c390f1SBarry Smith 
377c5c390f1SBarry Smith #undef __FUNCT__
3784a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
379a4d4d686SBarry Smith /*@C
38065f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
38165f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
38265f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
383a4d4d686SBarry Smith 
384a4d4d686SBarry Smith    Collective on SNES and Vec
385a4d4d686SBarry Smith 
386a4d4d686SBarry Smith    Input Parameters:
387a4d4d686SBarry Smith +  snes - the SNES context
388a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
389a4d4d686SBarry Smith 
390a4d4d686SBarry Smith    Output Parameter:
391a4d4d686SBarry Smith .  J - the matrix-free matrix
392a4d4d686SBarry Smith 
39315091d37SBarry Smith    Level: advanced
39415091d37SBarry Smith 
395a4d4d686SBarry Smith    Notes:
396a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
397a4d4d686SBarry Smith    and work space for performing finite difference approximations of
39865f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
3999a6cb015SBarry Smith 
4009a6cb015SBarry Smith    The default code uses the following approach to compute h
401a4d4d686SBarry Smith 
402a4d4d686SBarry Smith .vb
40365f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
404a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
405a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
406a4d4d686SBarry Smith  where
407a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
408a4d4d686SBarry Smith      umin = minimum iterate parameter
409a4d4d686SBarry Smith .ve
410a4d4d686SBarry Smith 
4115a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
41265f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
41365f2ba5bSLois Curfman McInnes    of the users manual for details.
414a4d4d686SBarry Smith 
415a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
416a4d4d686SBarry Smith    matrix context.
417a4d4d686SBarry Smith 
418a4d4d686SBarry Smith    Options Database Keys:
419a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4209a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
421a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
422a4d4d686SBarry Smith 
423a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
424a4d4d686SBarry Smith 
4255a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4261d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
427fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
428a4d4d686SBarry Smith 
429a4d4d686SBarry Smith @*/
430dfbe8321SBarry Smith PetscErrorCode MatCreateSNESMF(SNES snes,Vec x,Mat *J)
431a4d4d686SBarry Smith {
4321d1367b7SBarry Smith   MatSNESMFCtx   mfctx;
433dfbe8321SBarry Smith   PetscErrorCode ierr;
4341d1367b7SBarry Smith 
4351d1367b7SBarry Smith   PetscFunctionBegin;
4361d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
4377e9d5209SBarry Smith 
4387e9d5209SBarry Smith   mfctx          = (MatSNESMFCtx)(*J)->data;
4391d1367b7SBarry Smith   mfctx->snes    = snes;
440b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
441*52e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,*J);CHKERRQ(ierr);
4421d1367b7SBarry Smith   PetscFunctionReturn(0);
4431d1367b7SBarry Smith }
4441d1367b7SBarry Smith 
445cf3bea43SBarry Smith EXTERN_C_BEGIN
446cf3bea43SBarry Smith #undef __FUNCT__
447cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD"
448dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetBase_FD(Mat J,Vec U)
449cf3bea43SBarry Smith {
450dfbe8321SBarry Smith   PetscErrorCode ierr;
4517e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
452cf3bea43SBarry Smith 
453cf3bea43SBarry Smith   PetscFunctionBegin;
454cf3bea43SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
455cf3bea43SBarry Smith   ctx->current_u = U;
456cf3bea43SBarry Smith   ctx->usesnes   = PETSC_FALSE;
457958c9bccSBarry Smith   if (!ctx->w) {
458ba6a83e5SMatthew Knepley     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
459ba6a83e5SMatthew Knepley   }
46032dfb669SBarry Smith   J->assembled = PETSC_TRUE;
461cf3bea43SBarry Smith   PetscFunctionReturn(0);
462cf3bea43SBarry Smith }
463cf3bea43SBarry Smith EXTERN_C_END
464cf3bea43SBarry Smith 
4656849ba73SBarry Smith typedef PetscErrorCode (*FCN3)(Vec,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
4665b7f0c42SBarry Smith EXTERN_C_BEGIN
4675b7f0c42SBarry Smith #undef __FUNCT__
4685b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh_FD"
469dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetCheckh_FD(Mat J,FCN3 fun,void*ectx)
4705b7f0c42SBarry Smith {
4715b7f0c42SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
4725b7f0c42SBarry Smith 
4735b7f0c42SBarry Smith   PetscFunctionBegin;
4745b7f0c42SBarry Smith   ctx->checkh    = fun;
4755b7f0c42SBarry Smith   ctx->checkhctx = ectx;
4765b7f0c42SBarry Smith   PetscFunctionReturn(0);
4775b7f0c42SBarry Smith }
4785b7f0c42SBarry Smith EXTERN_C_END
4795b7f0c42SBarry Smith 
4804a2ae208SSatish Balay #undef __FUNCT__
4817e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions"
4827e9d5209SBarry Smith /*@
4837e9d5209SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4847e9d5209SBarry Smith    parameter.
4857e9d5209SBarry Smith 
4867e9d5209SBarry Smith    Collective on Mat
4877e9d5209SBarry Smith 
4887e9d5209SBarry Smith    Input Parameters:
4897e9d5209SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4907e9d5209SBarry Smith 
4917e9d5209SBarry Smith    Options Database Keys:
4927e9d5209SBarry Smith +  -snes_mf_type - <default,wp>
4937e9d5209SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
4947e9d5209SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
4957e9d5209SBarry Smith 
4967e9d5209SBarry Smith    Level: advanced
4977e9d5209SBarry Smith 
4987e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters
4997e9d5209SBarry Smith 
5007e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
5017e9d5209SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
5027e9d5209SBarry Smith @*/
503dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFromOptions(Mat mat)
5047e9d5209SBarry Smith {
5057e9d5209SBarry Smith   MatSNESMFCtx   mfctx = (MatSNESMFCtx)mat->data;
506dfbe8321SBarry Smith   PetscErrorCode ierr;
5077e9d5209SBarry Smith   PetscTruth     flg;
5087e9d5209SBarry Smith   char           ftype[256];
5097e9d5209SBarry Smith 
5107e9d5209SBarry Smith   PetscFunctionBegin;
5117e9d5209SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
5127e9d5209SBarry Smith 
5137e9d5209SBarry Smith   ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
5147e9d5209SBarry Smith   ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
5157e9d5209SBarry Smith   if (flg) {
5167e9d5209SBarry Smith     ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
5177e9d5209SBarry Smith   }
5187e9d5209SBarry Smith 
51987828ca2SBarry Smith   ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
5207e9d5209SBarry Smith   ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
5217e9d5209SBarry Smith   if (mfctx->snes) {
5227e9d5209SBarry Smith     ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
5237e9d5209SBarry Smith     if (flg) {
5247e9d5209SBarry Smith       KSP ksp;
52594b7f48cSBarry Smith       ierr = SNESGetKSP(mfctx->snes,&ksp);CHKERRQ(ierr);
5267e9d5209SBarry Smith       ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
5277e9d5209SBarry Smith     }
5287e9d5209SBarry Smith   }
5295b7f0c42SBarry Smith   ierr = PetscOptionsName("-snes_mf_check_positivity","Insure that U + h*a is nonnegative","MatSNESMFSetCheckh",&flg);CHKERRQ(ierr);
5305b7f0c42SBarry Smith   if (flg) {
5315b7f0c42SBarry Smith     ierr = MatSNESMFSetCheckh(mat,MatSNESMFCheckPositivity,0);CHKERRQ(ierr);
5325b7f0c42SBarry Smith   }
5337e9d5209SBarry Smith   if (mfctx->ops->setfromoptions) {
5347e9d5209SBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
5357e9d5209SBarry Smith   }
5367e9d5209SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5377e9d5209SBarry Smith   PetscFunctionReturn(0);
5387e9d5209SBarry Smith }
5397e9d5209SBarry Smith 
5400bad9183SKris Buschelman /*MC
541fafad747SKris Buschelman   MATMFFD - MATMFFD = "mffd" - A matrix free matrix type.
5420bad9183SKris Buschelman 
5430bad9183SKris Buschelman   Level: advanced
5440bad9183SKris Buschelman 
5458bc8193eSBarry Smith .seealso: MatCreateMF(), MatCreateSNESMF()
5460bad9183SKris Buschelman M*/
547fe93831dSBarry Smith EXTERN_C_BEGIN
5487e9d5209SBarry Smith #undef __FUNCT__
5497e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD"
550dfbe8321SBarry Smith PetscErrorCode MatCreate_MFFD(Mat A)
5517e9d5209SBarry Smith {
5527e9d5209SBarry Smith   MatSNESMFCtx mfctx;
553dfbe8321SBarry Smith   PetscErrorCode ierr;
5547e9d5209SBarry Smith 
5557e9d5209SBarry Smith   PetscFunctionBegin;
5566e087cb5SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5576e087cb5SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
5586e087cb5SMatthew Knepley #endif
5596e087cb5SMatthew Knepley 
560*52e6d16bSBarry Smith   ierr = PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr);
5617e9d5209SBarry Smith   mfctx->sp              = 0;
5627e9d5209SBarry Smith   mfctx->snes            = 0;
56377d8c4bbSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
5647e9d5209SBarry Smith   mfctx->recomputeperiod = 1;
5657e9d5209SBarry Smith   mfctx->count           = 0;
5667e9d5209SBarry Smith   mfctx->currenth        = 0.0;
5677e9d5209SBarry Smith   mfctx->historyh        = PETSC_NULL;
5687e9d5209SBarry Smith   mfctx->ncurrenth       = 0;
5697e9d5209SBarry Smith   mfctx->maxcurrenth     = 0;
5707e9d5209SBarry Smith   mfctx->type_name       = 0;
5717e9d5209SBarry Smith   mfctx->usesnes         = PETSC_FALSE;
5727e9d5209SBarry Smith 
573c5c390f1SBarry Smith   mfctx->vshift          = 0.0;
574c5c390f1SBarry Smith   mfctx->vscale          = 1.0;
575c5c390f1SBarry Smith 
5767e9d5209SBarry Smith   /*
5777e9d5209SBarry Smith      Create the empty data structure to contain compute-h routines.
5787e9d5209SBarry Smith      These will be filled in below from the command line options or
5797e9d5209SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
5808a124369SBarry Smith      then it will default in the first use of MatMult_MFFD()
5817e9d5209SBarry Smith   */
5827e9d5209SBarry Smith   mfctx->ops->compute        = 0;
5837e9d5209SBarry Smith   mfctx->ops->destroy        = 0;
5847e9d5209SBarry Smith   mfctx->ops->view           = 0;
5857e9d5209SBarry Smith   mfctx->ops->setfromoptions = 0;
5867e9d5209SBarry Smith   mfctx->hctx                = 0;
5877e9d5209SBarry Smith 
5887e9d5209SBarry Smith   mfctx->func                = 0;
5897e9d5209SBarry Smith   mfctx->funcctx             = 0;
5907e9d5209SBarry Smith   mfctx->funcvec             = 0;
591ba6a83e5SMatthew Knepley   mfctx->w                   = PETSC_NULL;
5927e9d5209SBarry Smith 
59365df01d8SBarry Smith   A->data                = mfctx;
5947e9d5209SBarry Smith 
5958a124369SBarry Smith   A->ops->mult           = MatMult_MFFD;
5968a124369SBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
5978a124369SBarry Smith   A->ops->view           = MatView_MFFD;
5988a124369SBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
5998a124369SBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
600c5c390f1SBarry Smith   A->ops->scale          = MatScale_MFFD;
601c5c390f1SBarry Smith   A->ops->shift          = MatShift_MFFD;
60265df01d8SBarry Smith   A->ops->setfromoptions = MatSNESMFSetFromOptions;
60332dfb669SBarry Smith   A->assembled = PETSC_TRUE;
6047e9d5209SBarry Smith 
60565df01d8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr);
606c5c390f1SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBase_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr);
60787828ca2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr);
6085b7f0c42SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetCheckh_C","MatSNESMFSetCheckh_FD",MatSNESMFSetCheckh_FD);CHKERRQ(ierr);
60965df01d8SBarry Smith   mfctx->mat = A;
6107e9d5209SBarry Smith 
6117e9d5209SBarry Smith   PetscFunctionReturn(0);
6127e9d5209SBarry Smith }
613fe93831dSBarry Smith EXTERN_C_END
6147e9d5209SBarry Smith 
6157e9d5209SBarry Smith #undef __FUNCT__
6164a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
6171d1367b7SBarry Smith /*@C
6181d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
6191d1367b7SBarry Smith 
6201d1367b7SBarry Smith    Collective on Vec
6211d1367b7SBarry Smith 
6221d1367b7SBarry Smith    Input Parameters:
6231d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
6241d1367b7SBarry Smith 
6251d1367b7SBarry Smith    Output Parameter:
6261d1367b7SBarry Smith .  J - the matrix-free matrix
6271d1367b7SBarry Smith 
6281d1367b7SBarry Smith    Level: advanced
6291d1367b7SBarry Smith 
6301d1367b7SBarry Smith    Notes:
6311d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
6321d1367b7SBarry Smith    and work space for performing finite difference approximations of
6331d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
6341d1367b7SBarry Smith 
6351d1367b7SBarry Smith    The default code uses the following approach to compute h
6361d1367b7SBarry Smith 
6371d1367b7SBarry Smith .vb
6381d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
6391d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
6401d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
6411d1367b7SBarry Smith  where
6421d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
6431d1367b7SBarry Smith      umin = minimum iterate parameter
6441d1367b7SBarry Smith .ve
6451d1367b7SBarry Smith 
6461d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
6471d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
6481d1367b7SBarry Smith    of the users manual for details.
6491d1367b7SBarry Smith 
6501d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
6511d1367b7SBarry Smith    matrix context.
6521d1367b7SBarry Smith 
6531d1367b7SBarry Smith    Options Database Keys:
6541d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
6551d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
6565b7f0c42SBarry Smith .  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
6575b7f0c42SBarry Smith -  -snes_mf_check_positivity
6581d1367b7SBarry Smith 
6591d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
6601d1367b7SBarry Smith 
6611d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
6621d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
663fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian()
6641d1367b7SBarry Smith 
6651d1367b7SBarry Smith @*/
666dfbe8321SBarry Smith PetscErrorCode MatCreateMF(Vec x,Mat *J)
6671d1367b7SBarry Smith {
668a4d4d686SBarry Smith   MPI_Comm       comm;
6696849ba73SBarry Smith   PetscErrorCode ierr;
670a7cc72afSBarry Smith   PetscInt       n,nloc;
671a4d4d686SBarry Smith 
672a4d4d686SBarry Smith   PetscFunctionBegin;
6731d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
67465df01d8SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
67565df01d8SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
6767e9d5209SBarry Smith   ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr);
677e56c5435SBarry Smith   ierr = MatRegisterDynamic(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr);
67865df01d8SBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
6799a6cb015SBarry Smith   PetscFunctionReturn(0);
6809a6cb015SBarry Smith }
6819a6cb015SBarry Smith 
682a4d4d686SBarry Smith 
6834a2ae208SSatish Balay #undef __FUNCT__
6844a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
685a4d4d686SBarry Smith /*@
68665f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
687a4d4d686SBarry Smith    parameter.
688a4d4d686SBarry Smith 
689a4d4d686SBarry Smith    Not Collective
690a4d4d686SBarry Smith 
691a4d4d686SBarry Smith    Input Parameters:
6925a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
693a4d4d686SBarry Smith 
694a4d4d686SBarry Smith    Output Paramter:
695a4d4d686SBarry Smith .  h - the differencing step size
696a4d4d686SBarry Smith 
69715091d37SBarry Smith    Level: advanced
69815091d37SBarry Smith 
699a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
700a4d4d686SBarry Smith 
7015a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
7025a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
703a4d4d686SBarry Smith @*/
704dfbe8321SBarry Smith PetscErrorCode MatSNESMFGetH(Mat mat,PetscScalar *h)
705a4d4d686SBarry Smith {
7067e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
707a4d4d686SBarry Smith 
708a4d4d686SBarry Smith   PetscFunctionBegin;
709a4d4d686SBarry Smith   *h = ctx->currenth;
710a4d4d686SBarry Smith   PetscFunctionReturn(0);
711a4d4d686SBarry Smith }
712a4d4d686SBarry Smith 
7134a2ae208SSatish Balay #undef __FUNCT__
7144a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
715a4d4d686SBarry Smith /*
7165a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
71765f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
71865f2ba5bSLois Curfman McInnes    each step.
719a4d4d686SBarry Smith */
720a7cc72afSBarry Smith PetscErrorCode MatSNESMFKSPMonitor(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy)
721a4d4d686SBarry Smith {
722a4d4d686SBarry Smith   PC             pc;
7235a655dc6SBarry Smith   MatSNESMFCtx   ctx;
724dfbe8321SBarry Smith   PetscErrorCode ierr;
725a4d4d686SBarry Smith   Mat            mat;
726a4d4d686SBarry Smith   MPI_Comm       comm;
727a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
728a4d4d686SBarry Smith 
729a4d4d686SBarry Smith   PetscFunctionBegin;
730a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
731a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
732a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
733a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7347e9d5209SBarry Smith   ctx  = (MatSNESMFCtx)mat->data;
7357e9d5209SBarry Smith 
736a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
737aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
73877431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
739329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
740a4d4d686SBarry Smith #else
74177431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
742a4d4d686SBarry Smith #endif
743a4d4d686SBarry Smith   } else {
74477431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
745a4d4d686SBarry Smith   }
746a4d4d686SBarry Smith   PetscFunctionReturn(0);
747a4d4d686SBarry Smith }
748a4d4d686SBarry Smith 
7494a2ae208SSatish Balay #undef __FUNCT__
7504a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
75185614651SBarry Smith /*@C
75285614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
75385614651SBarry Smith 
75485614651SBarry Smith    Collective on Mat
75585614651SBarry Smith 
75685614651SBarry Smith    Input Parameters:
75785614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
75885614651SBarry Smith .  v   - workspace vector
75985614651SBarry Smith .  func - the function to use
76085614651SBarry Smith -  funcctx - optional function context passed to function
76185614651SBarry Smith 
76285614651SBarry Smith    Level: advanced
76385614651SBarry Smith 
76485614651SBarry Smith    Notes:
76585614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
76685614651SBarry Smith     matrix inside your compute Jacobian routine
76785614651SBarry Smith 
76885614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
76985614651SBarry Smith 
77085614651SBarry Smith .keywords: SNES, matrix-free, function
77185614651SBarry Smith 
77285614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
77385614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
77485614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
77585614651SBarry Smith @*/
7766849ba73SBarry Smith PetscErrorCode MatSNESMFSetFunction(Mat mat,Vec v,PetscErrorCode (*func)(SNES,Vec,Vec,void *),void *funcctx)
77785614651SBarry Smith {
7787e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
77985614651SBarry Smith 
78085614651SBarry Smith   PetscFunctionBegin;
78185614651SBarry Smith   ctx->func    = func;
78285614651SBarry Smith   ctx->funcctx = funcctx;
78385614651SBarry Smith   ctx->funcvec = v;
78485614651SBarry Smith   PetscFunctionReturn(0);
78585614651SBarry Smith }
78685614651SBarry Smith 
787cf57b110SBarry Smith #undef __FUNCT__
788cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni"
789cf57b110SBarry Smith /*@C
790cf57b110SBarry Smith    MatSNESMFSetFunctioni - Sets the function for a single component
791cf57b110SBarry Smith 
792cf57b110SBarry Smith    Collective on Mat
793cf57b110SBarry Smith 
794cf57b110SBarry Smith    Input Parameters:
795cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
796cf57b110SBarry Smith -  funci - the function to use
797cf57b110SBarry Smith 
798cf57b110SBarry Smith    Level: advanced
799cf57b110SBarry Smith 
800cf57b110SBarry Smith    Notes:
801cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
802cf57b110SBarry Smith     matrix inside your compute Jacobian routine
803cf57b110SBarry Smith 
804cf57b110SBarry Smith 
805cf57b110SBarry Smith .keywords: SNES, matrix-free, function
806cf57b110SBarry Smith 
807cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
808cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
809cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
810cf57b110SBarry Smith @*/
811a7cc72afSBarry Smith PetscErrorCode MatSNESMFSetFunctioni(Mat mat,PetscErrorCode (*funci)(PetscInt,Vec,PetscScalar*,void *))
812cf57b110SBarry Smith {
813a7cc72afSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(PetscInt,Vec,PetscScalar*,void *));
814cf57b110SBarry Smith 
815cf57b110SBarry Smith   PetscFunctionBegin;
8164482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
817c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
81887828ca2SBarry Smith   if (f) {
81987828ca2SBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
82087828ca2SBarry Smith   }
821cf57b110SBarry Smith   PetscFunctionReturn(0);
822cf57b110SBarry Smith }
823cf57b110SBarry Smith 
82487828ca2SBarry Smith 
825cf57b110SBarry Smith #undef __FUNCT__
826cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase"
827cf57b110SBarry Smith /*@C
828cf57b110SBarry Smith    MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation
829cf57b110SBarry Smith 
830cf57b110SBarry Smith    Collective on Mat
831cf57b110SBarry Smith 
832cf57b110SBarry Smith    Input Parameters:
833cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
834cf57b110SBarry Smith -  func - the function to use
835cf57b110SBarry Smith 
836cf57b110SBarry Smith    Level: advanced
837cf57b110SBarry Smith 
838cf57b110SBarry Smith    Notes:
839cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
840cf57b110SBarry Smith     matrix inside your compute Jacobian routine
841cf57b110SBarry Smith 
842cf57b110SBarry Smith 
843cf57b110SBarry Smith .keywords: SNES, matrix-free, function
844cf57b110SBarry Smith 
845cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
846cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
847cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
848cf57b110SBarry Smith @*/
8496849ba73SBarry Smith PetscErrorCode MatSNESMFSetFunctioniBase(Mat mat,PetscErrorCode (*func)(Vec,void *))
850cf57b110SBarry Smith {
8516849ba73SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,void *));
852cf57b110SBarry Smith 
853cf57b110SBarry Smith   PetscFunctionBegin;
8544482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
855c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
85687828ca2SBarry Smith   if (f) {
85787828ca2SBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
85887828ca2SBarry Smith   }
859cf57b110SBarry Smith   PetscFunctionReturn(0);
860cf57b110SBarry Smith }
861cf57b110SBarry Smith 
86285614651SBarry Smith 
8634a2ae208SSatish Balay #undef __FUNCT__
8644a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
865329f5518SBarry Smith /*@
866329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
867329f5518SBarry Smith 
868329f5518SBarry Smith    Collective on Mat
869329f5518SBarry Smith 
870329f5518SBarry Smith    Input Parameters:
871329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
872329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
873329f5518SBarry Smith 
874329f5518SBarry Smith    Options Database Keys:
875329f5518SBarry Smith +  -snes_mf_period <period>
876329f5518SBarry Smith 
877329f5518SBarry Smith    Level: advanced
878329f5518SBarry Smith 
879329f5518SBarry Smith 
880329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
881329f5518SBarry Smith 
882329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
883329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
884329f5518SBarry Smith           MatSNESMFKSPMonitor()
885329f5518SBarry Smith @*/
886a7cc72afSBarry Smith PetscErrorCode MatSNESMFSetPeriod(Mat mat,PetscInt period)
887329f5518SBarry Smith {
8887e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
889329f5518SBarry Smith 
890329f5518SBarry Smith   PetscFunctionBegin;
891329f5518SBarry Smith   ctx->recomputeperiod = period;
892329f5518SBarry Smith   PetscFunctionReturn(0);
893329f5518SBarry Smith }
894329f5518SBarry Smith 
8954a2ae208SSatish Balay #undef __FUNCT__
8964a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
897a4d4d686SBarry Smith /*@
8985a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
899a4d4d686SBarry Smith    matrix-vector products using finite differences.
900a4d4d686SBarry Smith 
901a4d4d686SBarry Smith    Collective on Mat
902a4d4d686SBarry Smith 
903a4d4d686SBarry Smith    Input Parameters:
9045a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
9059a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
906a4d4d686SBarry Smith                the relative error in the function evaluations)
907a4d4d686SBarry Smith 
90815091d37SBarry Smith    Options Database Keys:
90915091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
91015091d37SBarry Smith 
91115091d37SBarry Smith    Level: advanced
91215091d37SBarry Smith 
913a4d4d686SBarry Smith    Notes:
914a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
915a4d4d686SBarry Smith .vb
91665f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
917a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
918a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
919a4d4d686SBarry Smith .ve
920a4d4d686SBarry Smith 
921a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
922a4d4d686SBarry Smith 
9235a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
9245a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9255a655dc6SBarry Smith           MatSNESMFKSPMonitor()
926a4d4d686SBarry Smith @*/
927dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetFunctionError(Mat mat,PetscReal error)
928a4d4d686SBarry Smith {
9297e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
930a4d4d686SBarry Smith 
931a4d4d686SBarry Smith   PetscFunctionBegin;
932a4d4d686SBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
933a4d4d686SBarry Smith   PetscFunctionReturn(0);
934a4d4d686SBarry Smith }
935a4d4d686SBarry Smith 
9364a2ae208SSatish Balay #undef __FUNCT__
9374a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
938a4d4d686SBarry Smith /*@
93965f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
94065f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
94165f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
94265f2ba5bSLois Curfman McInnes    automatically removed.
943a4d4d686SBarry Smith 
944a4d4d686SBarry Smith    Collective on Mat
945a4d4d686SBarry Smith 
946a4d4d686SBarry Smith    Input Parameters:
947a4d4d686SBarry Smith +  J - the matrix-free matrix context
94874637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
949a4d4d686SBarry Smith 
95015091d37SBarry Smith    Level: advanced
95115091d37SBarry Smith 
952a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
953a4d4d686SBarry Smith 
95474637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
9555a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9565a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
957a4d4d686SBarry Smith @*/
958dfbe8321SBarry Smith PetscErrorCode MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
959a4d4d686SBarry Smith {
960dfbe8321SBarry Smith   PetscErrorCode ierr;
9617e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
962a4d4d686SBarry Smith   MPI_Comm       comm;
963a4d4d686SBarry Smith 
964a4d4d686SBarry Smith   PetscFunctionBegin;
9652d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
966a4d4d686SBarry Smith 
96785614651SBarry Smith   ctx->sp = nullsp;
96885614651SBarry Smith   ierr    = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
969a4d4d686SBarry Smith   PetscFunctionReturn(0);
970a4d4d686SBarry Smith }
971a4d4d686SBarry Smith 
9724a2ae208SSatish Balay #undef __FUNCT__
9734a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
974a4d4d686SBarry Smith /*@
97565f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
97665f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
977a4d4d686SBarry Smith 
978a4d4d686SBarry Smith    Collective on Mat
979a4d4d686SBarry Smith 
980a4d4d686SBarry Smith    Input Parameters:
981a4d4d686SBarry Smith +  J - the matrix-free matrix context
98265f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
98365f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
98465f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
985a4d4d686SBarry Smith 
98615091d37SBarry Smith    Level: advanced
98715091d37SBarry Smith 
988a4d4d686SBarry Smith    Notes:
98965f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
99065f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
991a4d4d686SBarry Smith 
992a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
993a4d4d686SBarry Smith 
9945a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
9955a655dc6SBarry Smith           MatSNESMFResetHHistory(),
9965a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
997a4d4d686SBarry Smith 
998a4d4d686SBarry Smith @*/
999a7cc72afSBarry Smith PetscErrorCode MatSNESMFSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory)
1000a4d4d686SBarry Smith {
10017e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1002a4d4d686SBarry Smith 
1003a4d4d686SBarry Smith   PetscFunctionBegin;
1004a4d4d686SBarry Smith   ctx->historyh    = history;
1005a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
1006a4d4d686SBarry Smith   ctx->currenth    = 0;
1007a4d4d686SBarry Smith   PetscFunctionReturn(0);
1008a4d4d686SBarry Smith }
1009a4d4d686SBarry Smith 
10104a2ae208SSatish Balay #undef __FUNCT__
10114a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
1012a4d4d686SBarry Smith /*@
10135a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
1014a4d4d686SBarry Smith    collecting a new set of differencing histories.
1015a4d4d686SBarry Smith 
1016a4d4d686SBarry Smith    Collective on Mat
1017a4d4d686SBarry Smith 
1018a4d4d686SBarry Smith    Input Parameters:
1019a4d4d686SBarry Smith .  J - the matrix-free matrix context
1020a4d4d686SBarry Smith 
102115091d37SBarry Smith    Level: advanced
102215091d37SBarry Smith 
1023a4d4d686SBarry Smith    Notes:
102465f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
1025a4d4d686SBarry Smith 
1026a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1027a4d4d686SBarry Smith 
10285a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10295a655dc6SBarry Smith           MatSNESMFSetHHistory(),
10305a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1031a4d4d686SBarry Smith 
1032a4d4d686SBarry Smith @*/
1033dfbe8321SBarry Smith PetscErrorCode MatSNESMFResetHHistory(Mat J)
1034a4d4d686SBarry Smith {
10357e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1036a4d4d686SBarry Smith 
1037a4d4d686SBarry Smith   PetscFunctionBegin;
1038be726c96SBarry Smith   ctx->ncurrenth    = 0;
1039a4d4d686SBarry Smith   PetscFunctionReturn(0);
1040a4d4d686SBarry Smith }
1041a4d4d686SBarry Smith 
10424a2ae208SSatish Balay #undef __FUNCT__
1043fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian"
1044dfbe8321SBarry Smith PetscErrorCode MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
10451d1367b7SBarry Smith {
1046dfbe8321SBarry Smith   PetscErrorCode ierr;
10471d1367b7SBarry Smith   PetscFunctionBegin;
10481d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10491d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10501d1367b7SBarry Smith   PetscFunctionReturn(0);
10511d1367b7SBarry Smith }
10521d1367b7SBarry Smith 
10534a2ae208SSatish Balay #undef __FUNCT__
10544a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
10555b7f0c42SBarry Smith /*@
10565b7f0c42SBarry Smith     MatSNESMFSetBase - Sets the vector U at which matrix vector products of the
10575b7f0c42SBarry Smith         Jacobian are computed
10585b7f0c42SBarry Smith 
10595b7f0c42SBarry Smith     Collective on Mat
10605b7f0c42SBarry Smith 
10615b7f0c42SBarry Smith     Input Parameters:
10625b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10635b7f0c42SBarry Smith -   U - the vector
10645b7f0c42SBarry Smith 
10655b7f0c42SBarry Smith     Notes: This is rarely used directly
10665b7f0c42SBarry Smith 
10675b7f0c42SBarry Smith     Level: advanced
10685b7f0c42SBarry Smith 
10695b7f0c42SBarry Smith @*/
1070dfbe8321SBarry Smith PetscErrorCode MatSNESMFSetBase(Mat J,Vec U)
10711d1367b7SBarry Smith {
1072dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat,Vec);
10731d1367b7SBarry Smith 
10741d1367b7SBarry Smith   PetscFunctionBegin;
10754482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
10764482741eSBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE,2);
1077c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1078cf3bea43SBarry Smith   if (f) {
1079cf3bea43SBarry Smith     ierr = (*f)(J,U);CHKERRQ(ierr);
108049d4803aSBarry Smith   }
10811d1367b7SBarry Smith   PetscFunctionReturn(0);
10821d1367b7SBarry Smith }
1083cf57b110SBarry Smith 
10845b7f0c42SBarry Smith #undef __FUNCT__
10855b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh"
108661860be5SBarry Smith /*@C
10875b7f0c42SBarry Smith     MatSNESMFSetCheckh - Sets a function that checks the computed h and adjusts
10885b7f0c42SBarry Smith         it to satisfy some criteria
1089cf57b110SBarry Smith 
10905b7f0c42SBarry Smith     Collective on Mat
10915b7f0c42SBarry Smith 
10925b7f0c42SBarry Smith     Input Parameters:
10935b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10945b7f0c42SBarry Smith .   fun - the function that checks h
10955b7f0c42SBarry Smith -   ctx - any context needed by the function
10965b7f0c42SBarry Smith 
10975b7f0c42SBarry Smith     Options Database Keys:
10985b7f0c42SBarry Smith .   -snes_mf_check_positivity
10995b7f0c42SBarry Smith 
11005b7f0c42SBarry Smith     Level: advanced
11015b7f0c42SBarry Smith 
11025b7f0c42SBarry Smith     Notes: For example, MatSNESMFSetCheckPositivity() insures that all entries
11035b7f0c42SBarry Smith        of U + h*a are non-negative
11045b7f0c42SBarry Smith 
11055b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckPositivity()
11065b7f0c42SBarry Smith @*/
11076849ba73SBarry Smith PetscErrorCode MatSNESMFSetCheckh(Mat J,PetscErrorCode (*fun)(Vec,Vec,PetscScalar*,void*),void* ctx)
11085b7f0c42SBarry Smith {
11096849ba73SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,Vec,PetscScalar*,void*),void*);
11105b7f0c42SBarry Smith 
11115b7f0c42SBarry Smith   PetscFunctionBegin;
11124482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
11135b7f0c42SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
11145b7f0c42SBarry Smith   if (f) {
11155b7f0c42SBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
11165b7f0c42SBarry Smith   }
11175b7f0c42SBarry Smith   PetscFunctionReturn(0);
11185b7f0c42SBarry Smith }
11195b7f0c42SBarry Smith 
11205b7f0c42SBarry Smith #undef __FUNCT__
11215b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckPositivity"
11225b7f0c42SBarry Smith /*@
11235b7f0c42SBarry Smith     MatSNESMFCheckPositivity - Checks that all entries in U + h*a are positive or
11245b7f0c42SBarry Smith         zero, decreases h until this is satisfied.
11255b7f0c42SBarry Smith 
11265b7f0c42SBarry Smith     Collective on Vec
11275b7f0c42SBarry Smith 
11285b7f0c42SBarry Smith     Input Parameters:
11295b7f0c42SBarry Smith +   U - base vector that is added to
11305b7f0c42SBarry Smith .   a - vector that is added
11315b7f0c42SBarry Smith .   h - scaling factor on a
11325b7f0c42SBarry Smith -   dummy - context variable (unused)
11335b7f0c42SBarry Smith 
11345b7f0c42SBarry Smith     Options Database Keys:
11355b7f0c42SBarry Smith .   -snes_mf_check_positivity
11365b7f0c42SBarry Smith 
11375b7f0c42SBarry Smith     Level: advanced
11385b7f0c42SBarry Smith 
11395b7f0c42SBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
11405b7f0c42SBarry Smith            MatSNESMFSetCheckh()
11415b7f0c42SBarry Smith 
11425b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckh()
11435b7f0c42SBarry Smith @*/
1144dfbe8321SBarry Smith PetscErrorCode MatSNESMFCheckPositivity(Vec U,Vec a,PetscScalar *h,void *dummy)
11455b7f0c42SBarry Smith {
11465b7f0c42SBarry Smith   PetscReal      val, minval;
11475b7f0c42SBarry Smith   PetscScalar    *u_vec, *a_vec;
1148dfbe8321SBarry Smith   PetscErrorCode ierr;
1149a7cc72afSBarry Smith   PetscInt       i,n;
11505b7f0c42SBarry Smith   MPI_Comm       comm;
11515b7f0c42SBarry Smith 
11525b7f0c42SBarry Smith   PetscFunctionBegin;
11535b7f0c42SBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
11545b7f0c42SBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
11555b7f0c42SBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
1156a7cc72afSBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
115761860be5SBarry Smith   minval = PetscAbsScalar(*h*1.01);
1158a7cc72afSBarry Smith   for(i=0;i<n;i++) {
115961860be5SBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
116061860be5SBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
11615b7f0c42SBarry Smith       if (val < minval) minval = val;
11625b7f0c42SBarry Smith     }
11635b7f0c42SBarry Smith   }
11645b7f0c42SBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
11655b7f0c42SBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
11665b7f0c42SBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
116761860be5SBarry Smith   if (val <= PetscAbsScalar(*h)) {
116861860be5SBarry Smith     PetscLogInfo(U,"MatSNESMFCheckPositivity: Scaling back h from %g to %g\n",PetscRealPart(*h),.99*val);
116961860be5SBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
11705b7f0c42SBarry Smith     else                         *h = -0.99*val;
11715b7f0c42SBarry Smith   }
11725b7f0c42SBarry Smith   PetscFunctionReturn(0);
11735b7f0c42SBarry Smith }
1174cf57b110SBarry Smith 
1175cf57b110SBarry Smith 
1176cf57b110SBarry Smith 
1177cf57b110SBarry Smith 
1178cf57b110SBarry Smith 
1179cf57b110SBarry Smith 
1180cf57b110SBarry Smith 
1181