xref: /petsc/src/snes/mf/snesmfj.c (revision e2d1d2b73d857c2061120f42f5153ba627fcaad7)
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 @*/
310e33f6ddSBarry Smith int MatSNESMFSetType(Mat mat,const MatSNESMFType ftype)
32b9fa9cd0SBarry Smith {
335a655dc6SBarry Smith   int          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);}
529a6cb015SBarry Smith 
53b9617806SBarry Smith   ierr =  PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr);
549a6cb015SBarry Smith 
5529bbc08cSBarry Smith   if (!r) SETERRQ(1,"Unknown MatSNESMF type given");
569a6cb015SBarry Smith 
579a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
586831982aSBarry Smith 
596831982aSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
609a6cb015SBarry Smith 
619a6cb015SBarry Smith   PetscFunctionReturn(0);
629a6cb015SBarry Smith }
639a6cb015SBarry Smith 
648e019c35SBarry Smith typedef int (*FCN1)(Vec,void*); /* force argument to next function to not be extern C*/
65c5c390f1SBarry Smith EXTERN_C_BEGIN
6687828ca2SBarry Smith #undef __FUNCT__
6787828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD"
688e019c35SBarry Smith int MatSNESMFSetFunctioniBase_FD(Mat mat,FCN1 func)
6987828ca2SBarry Smith {
7087828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
7187828ca2SBarry Smith 
7287828ca2SBarry Smith   PetscFunctionBegin;
7387828ca2SBarry Smith   ctx->funcisetbase = func;
7487828ca2SBarry Smith   PetscFunctionReturn(0);
7587828ca2SBarry Smith }
76c5c390f1SBarry Smith EXTERN_C_END
7787828ca2SBarry Smith 
788e019c35SBarry Smith typedef int (*FCN2)(int,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
79c5c390f1SBarry Smith EXTERN_C_BEGIN
8087828ca2SBarry Smith #undef __FUNCT__
8187828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD"
828e019c35SBarry Smith int MatSNESMFSetFunctioni_FD(Mat mat,FCN2 funci)
8387828ca2SBarry Smith {
8487828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
8587828ca2SBarry Smith 
8687828ca2SBarry Smith   PetscFunctionBegin;
8787828ca2SBarry Smith   ctx->funci = funci;
8887828ca2SBarry Smith   PetscFunctionReturn(0);
8987828ca2SBarry Smith }
90c5c390f1SBarry Smith EXTERN_C_END
9187828ca2SBarry Smith 
929a6cb015SBarry Smith 
934a2ae208SSatish Balay #undef __FUNCT__
944a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
95899d7b4fSKris Buschelman int MatSNESMFRegister(const char sname[],const char path[],const char name[],int (*function)(MatSNESMFCtx))
969a6cb015SBarry Smith {
979a6cb015SBarry Smith   int ierr;
98*e2d1d2b7SBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
999a6cb015SBarry Smith 
1009a6cb015SBarry Smith   PetscFunctionBegin;
101b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
102c134de8dSSatish Balay   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
1039a6cb015SBarry Smith   PetscFunctionReturn(0);
1049a6cb015SBarry Smith }
1059a6cb015SBarry Smith 
1069a6cb015SBarry Smith 
1074a2ae208SSatish Balay #undef __FUNCT__
1084a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1099a6cb015SBarry Smith /*@C
1105a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
111f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1129a6cb015SBarry Smith 
1139a6cb015SBarry Smith    Not Collective
1149a6cb015SBarry Smith 
11515091d37SBarry Smith    Level: developer
11615091d37SBarry Smith 
1175a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1189a6cb015SBarry Smith 
119f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1209a6cb015SBarry Smith @*/
1215a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1229a6cb015SBarry Smith {
1239a6cb015SBarry Smith   int ierr;
1249a6cb015SBarry Smith 
1259a6cb015SBarry Smith   PetscFunctionBegin;
126b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
127b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
128b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1299a6cb015SBarry Smith   }
1304c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1319a6cb015SBarry Smith   PetscFunctionReturn(0);
1329a6cb015SBarry Smith }
1339a6cb015SBarry Smith 
1349a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
1354a2ae208SSatish Balay #undef __FUNCT__
1368a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD"
1378a124369SBarry Smith int MatDestroy_MFFD(Mat mat)
138a4d4d686SBarry Smith {
139a4d4d686SBarry Smith   int          ierr;
1407e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
141fae171e0SBarry Smith 
1423a40ed3dSBarry Smith   PetscFunctionBegin;
143ba6a83e5SMatthew Knepley   if (ctx->w != PETSC_NULL) {
144b9fa9cd0SBarry Smith     ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
145ba6a83e5SMatthew Knepley   }
1469a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
14774637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
1486831982aSBarry Smith   PetscHeaderDestroy(ctx);
1493a40ed3dSBarry Smith   PetscFunctionReturn(0);
150b9fa9cd0SBarry Smith }
15150361f65SLois Curfman McInnes 
1524a2ae208SSatish Balay #undef __FUNCT__
1538a124369SBarry Smith #define __FUNCT__ "MatView_MFFD"
15439e2f89bSBarry Smith /*
1558a124369SBarry Smith    MatSNESMFView_MFFD - Views matrix-free parameters.
1568f6e3e37SBarry Smith 
15739e2f89bSBarry Smith */
1588a124369SBarry Smith int MatView_MFFD(Mat J,PetscViewer viewer)
159eb9086c3SLois Curfman McInnes {
160eb9086c3SLois Curfman McInnes   int          ierr;
1617e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
16232077d6dSBarry Smith   PetscTruth   iascii;
163eb9086c3SLois Curfman McInnes 
1643a40ed3dSBarry Smith   PetscFunctionBegin;
16532077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
16632077d6dSBarry Smith   if (iascii) {
167b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
168b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
169473c83c3SBarry Smith      if (!ctx->type_name) {
170b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
171473c83c3SBarry Smith      } else {
172b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
173473c83c3SBarry Smith      }
1749a6cb015SBarry Smith      if (ctx->ops->view) {
1759a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1769a6cb015SBarry Smith      }
1775cd90555SBarry Smith   } else {
17829bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
179eb9086c3SLois Curfman McInnes   }
1803a40ed3dSBarry Smith   PetscFunctionReturn(0);
181eb9086c3SLois Curfman McInnes }
182eb9086c3SLois Curfman McInnes 
1834a2ae208SSatish Balay #undef __FUNCT__
1848a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD"
185be726c96SBarry Smith /*
1865a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
18765f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
188be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
18965f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
19065f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
191be726c96SBarry Smith */
1928a124369SBarry Smith int MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt)
193be726c96SBarry Smith {
194be726c96SBarry Smith   int             ierr;
1957e9d5209SBarry Smith   MatSNESMFCtx    j = (MatSNESMFCtx)J->data;
196be726c96SBarry Smith 
197be726c96SBarry Smith   PetscFunctionBegin;
1985a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
199b0a32e0cSBarry Smith   if (j->usesnes) {
2001d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
2011d1367b7SBarry Smith     ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2022740c1caSMatthew Knepley     if (j->w == PETSC_NULL) {
2032740c1caSMatthew Knepley       ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr);
2042740c1caSMatthew Knepley     }
2051d1367b7SBarry Smith   }
206c5c390f1SBarry Smith   j->vshift = 0.0;
207c5c390f1SBarry Smith   j->vscale = 1.0;
208be726c96SBarry Smith   PetscFunctionReturn(0);
209be726c96SBarry Smith }
210be726c96SBarry Smith 
2114a2ae208SSatish Balay #undef __FUNCT__
2128a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD"
213eb9086c3SLois Curfman McInnes /*
214adb62b0dSMatthew Knepley   MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
215a4d4d686SBarry Smith 
2169a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
217eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
218eb9086c3SLois Curfman McInnes         u = current iterate
219eb9086c3SLois Curfman McInnes         h = difference interval
220eb9086c3SLois Curfman McInnes */
2218a124369SBarry Smith int MatMult_MFFD(Mat mat,Vec a,Vec y)
22239e2f89bSBarry Smith {
2237e9d5209SBarry Smith   MatSNESMFCtx    ctx = (MatSNESMFCtx)mat->data;
224fae171e0SBarry Smith   SNES            snes;
225ea709b57SSatish Balay   PetscScalar     h,mone = -1.0;
226fae171e0SBarry Smith   Vec             w,U,F;
227a305c92eSSatish Balay   int             ierr,(*eval_fct)(SNES,Vec,Vec)=0;
22839e2f89bSBarry Smith 
2293a40ed3dSBarry Smith   PetscFunctionBegin;
2309a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2319a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2329a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2339a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
234d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
23556cd22aeSBarry Smith 
236fae171e0SBarry Smith   snes = ctx->snes;
237fae171e0SBarry Smith   w    = ctx->w;
2381d1367b7SBarry Smith   U    = ctx->current_u;
23950361f65SLois Curfman McInnes 
24085614651SBarry Smith   /*
24185614651SBarry Smith       Compute differencing parameter
24285614651SBarry Smith   */
2439a6cb015SBarry Smith   if (!ctx->ops->compute) {
2442f859189SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_WP);CHKERRQ(ierr);
2455a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2469a6cb015SBarry Smith   }
2479a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
248a4d4d686SBarry Smith 
2495b7f0c42SBarry Smith   if (ctx->checkh) {
2505b7f0c42SBarry Smith     ierr = (*ctx->checkh)(U,a,&h,ctx->checkhctx);CHKERRQ(ierr);
2515b7f0c42SBarry Smith   }
2525b7f0c42SBarry Smith 
253a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
254a4d4d686SBarry Smith   ctx->currenth = h;
255aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
2568a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h));
257a4d4d686SBarry Smith #else
2588a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h);
259a4d4d686SBarry Smith #endif
260a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
26185614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
262a4d4d686SBarry Smith   }
26385614651SBarry Smith   ctx->ncurrenth++;
264a4d4d686SBarry Smith 
26585614651SBarry Smith   /* w = u + ha */
266a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
26785614651SBarry Smith 
268b0a32e0cSBarry Smith   if (ctx->usesnes) {
26985614651SBarry Smith     eval_fct = SNESComputeFunction;
2701d1367b7SBarry Smith     F    = ctx->current_f;
27129bbc08cSBarry Smith     if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices");
27239903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
27385614651SBarry Smith   } else {
27485614651SBarry Smith     F = ctx->funcvec;
27585614651SBarry Smith     /* compute func(U) as base for differencing */
27685614651SBarry Smith     if (ctx->ncurrenth == 1) {
27785614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
27885614651SBarry Smith     }
27985614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
28085614651SBarry Smith   }
281a4d4d686SBarry Smith 
282a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
283a4d4d686SBarry Smith   h    = 1.0/h;
284a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
285c5c390f1SBarry Smith 
286c5c390f1SBarry Smith 
287c5c390f1SBarry Smith   if (ctx->vshift != 0.0 && ctx->vscale != 1.0) {
288c5c390f1SBarry Smith     ierr = VecAXPBY(&ctx->vshift,&ctx->vscale,a,y);CHKERRQ(ierr);
289c5c390f1SBarry Smith   } else if (ctx->vscale != 1.0) {
290c5c390f1SBarry Smith     ierr = VecScale(&ctx->vscale,y);CHKERRQ(ierr);
291c5c390f1SBarry Smith   } else if (ctx->vshift != 0.0) {
292c5c390f1SBarry Smith     ierr = VecAXPY(&ctx->vshift,a,y);CHKERRQ(ierr);
293c5c390f1SBarry Smith   }
294c5c390f1SBarry Smith 
29574637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
296a4d4d686SBarry Smith 
297d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
298a4d4d686SBarry Smith   PetscFunctionReturn(0);
299a4d4d686SBarry Smith }
300a4d4d686SBarry Smith 
3014a2ae208SSatish Balay #undef __FUNCT__
3028a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD"
303cf57b110SBarry Smith /*
3048a124369SBarry Smith   MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix
305cf57b110SBarry Smith 
306cf57b110SBarry Smith         y ~= (F(u + ha) - F(u))/h,
307cf57b110SBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
308cf57b110SBarry Smith         u = current iterate
309cf57b110SBarry Smith         h = difference interval
310cf57b110SBarry Smith */
3118a124369SBarry Smith int MatGetDiagonal_MFFD(Mat mat,Vec a)
312cf57b110SBarry Smith {
3137e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
314ea709b57SSatish Balay   PetscScalar  h,*aa,*ww,v;
31577d8c4bbSBarry Smith   PetscReal    epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON;
31665df01d8SBarry Smith   Vec          w,U;
317cf57b110SBarry Smith   int          i,ierr,rstart,rend;
318cf57b110SBarry Smith 
319cf57b110SBarry Smith   PetscFunctionBegin;
320cf57b110SBarry Smith   if (!ctx->funci) {
321cf57b110SBarry Smith     SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first");
322cf57b110SBarry Smith   }
323cf57b110SBarry Smith 
324cf57b110SBarry Smith   w    = ctx->w;
325cf57b110SBarry Smith   U    = ctx->current_u;
326cf57b110SBarry Smith   ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr);
327cf57b110SBarry Smith   ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr);
328cf57b110SBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
329cf57b110SBarry Smith 
330cf57b110SBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
331cf57b110SBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
332cf57b110SBarry Smith   for (i=rstart; i<rend; i++) {
333cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
334cf57b110SBarry Smith     h  = ww[i-rstart];
335cf57b110SBarry Smith     if (h == 0.0) h = 1.0;
336cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX)
337cf57b110SBarry Smith     if (h < umin && h >= 0.0)      h = umin;
338cf57b110SBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
339cf57b110SBarry Smith #else
340cf57b110SBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
341cf57b110SBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
342cf57b110SBarry Smith #endif
343cf57b110SBarry Smith     h     *= epsilon;
344cf57b110SBarry Smith 
345cf57b110SBarry Smith     ww[i-rstart] += h;
346cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
347cf57b110SBarry Smith     ierr          = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr);
348cf57b110SBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
349c5c390f1SBarry Smith 
350c5c390f1SBarry Smith     /* possibly shift and scale result */
351c5c390f1SBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
352c5c390f1SBarry Smith 
353cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
354cf57b110SBarry Smith     ww[i-rstart] -= h;
355cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
356cf57b110SBarry Smith   }
357cf57b110SBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
358cf57b110SBarry Smith   PetscFunctionReturn(0);
359cf57b110SBarry Smith }
360cf57b110SBarry Smith 
361cf57b110SBarry Smith #undef __FUNCT__
362c5c390f1SBarry Smith #define __FUNCT__ "MatShift_MFFD"
363268466fbSBarry Smith int MatShift_MFFD(const PetscScalar *a,Mat Y)
364c5c390f1SBarry Smith {
365c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
366c5c390f1SBarry Smith   PetscFunctionBegin;
367c5c390f1SBarry Smith   shell->vshift += *a;
368c5c390f1SBarry Smith   PetscFunctionReturn(0);
369c5c390f1SBarry Smith }
370c5c390f1SBarry Smith 
371c5c390f1SBarry Smith #undef __FUNCT__
372c5c390f1SBarry Smith #define __FUNCT__ "MatScale_MFFD"
373268466fbSBarry Smith int MatScale_MFFD(const PetscScalar *a,Mat Y)
374c5c390f1SBarry Smith {
375c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
376c5c390f1SBarry Smith   PetscFunctionBegin;
377c5c390f1SBarry Smith   shell->vscale *= *a;
378c5c390f1SBarry Smith   PetscFunctionReturn(0);
379c5c390f1SBarry Smith }
380c5c390f1SBarry Smith 
381c5c390f1SBarry Smith 
382c5c390f1SBarry Smith #undef __FUNCT__
3834a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
384a4d4d686SBarry Smith /*@C
38565f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
38665f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
38765f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
388a4d4d686SBarry Smith 
389a4d4d686SBarry Smith    Collective on SNES and Vec
390a4d4d686SBarry Smith 
391a4d4d686SBarry Smith    Input Parameters:
392a4d4d686SBarry Smith +  snes - the SNES context
393a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
394a4d4d686SBarry Smith 
395a4d4d686SBarry Smith    Output Parameter:
396a4d4d686SBarry Smith .  J - the matrix-free matrix
397a4d4d686SBarry Smith 
39815091d37SBarry Smith    Level: advanced
39915091d37SBarry Smith 
400a4d4d686SBarry Smith    Notes:
401a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
402a4d4d686SBarry Smith    and work space for performing finite difference approximations of
40365f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
4049a6cb015SBarry Smith 
4059a6cb015SBarry Smith    The default code uses the following approach to compute h
406a4d4d686SBarry Smith 
407a4d4d686SBarry Smith .vb
40865f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
409a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
410a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
411a4d4d686SBarry Smith  where
412a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
413a4d4d686SBarry Smith      umin = minimum iterate parameter
414a4d4d686SBarry Smith .ve
415a4d4d686SBarry Smith 
4165a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
41765f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
41865f2ba5bSLois Curfman McInnes    of the users manual for details.
419a4d4d686SBarry Smith 
420a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
421a4d4d686SBarry Smith    matrix context.
422a4d4d686SBarry Smith 
423a4d4d686SBarry Smith    Options Database Keys:
424a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4259a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
426a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
427a4d4d686SBarry Smith 
428a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
429a4d4d686SBarry Smith 
4305a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4311d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
432fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
433a4d4d686SBarry Smith 
434a4d4d686SBarry Smith @*/
4355a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J)
436a4d4d686SBarry Smith {
4371d1367b7SBarry Smith   MatSNESMFCtx mfctx;
4381d1367b7SBarry Smith   int          ierr;
4391d1367b7SBarry Smith 
4401d1367b7SBarry Smith   PetscFunctionBegin;
4411d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
4427e9d5209SBarry Smith 
4437e9d5209SBarry Smith   mfctx          = (MatSNESMFCtx)(*J)->data;
4441d1367b7SBarry Smith   mfctx->snes    = snes;
445b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
446b0a32e0cSBarry Smith   PetscLogObjectParent(snes,*J);
4471d1367b7SBarry Smith   PetscFunctionReturn(0);
4481d1367b7SBarry Smith }
4491d1367b7SBarry Smith 
450cf3bea43SBarry Smith EXTERN_C_BEGIN
451cf3bea43SBarry Smith #undef __FUNCT__
452cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD"
453cf3bea43SBarry Smith int MatSNESMFSetBase_FD(Mat J,Vec U)
454cf3bea43SBarry Smith {
455cf3bea43SBarry Smith   int          ierr;
4567e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
457cf3bea43SBarry Smith 
458cf3bea43SBarry Smith   PetscFunctionBegin;
459cf3bea43SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
460cf3bea43SBarry Smith   ctx->current_u = U;
461cf3bea43SBarry Smith   ctx->usesnes   = PETSC_FALSE;
462ba6a83e5SMatthew Knepley   if (ctx->w == PETSC_NULL) {
463ba6a83e5SMatthew Knepley     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
464ba6a83e5SMatthew Knepley   }
465cf3bea43SBarry Smith   PetscFunctionReturn(0);
466cf3bea43SBarry Smith }
467cf3bea43SBarry Smith EXTERN_C_END
468cf3bea43SBarry Smith 
4698e019c35SBarry Smith typedef int (*FCN3)(Vec,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
4705b7f0c42SBarry Smith EXTERN_C_BEGIN
4715b7f0c42SBarry Smith #undef __FUNCT__
4725b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh_FD"
4738e019c35SBarry Smith int MatSNESMFSetCheckh_FD(Mat J,FCN3 fun,void*ectx)
4745b7f0c42SBarry Smith {
4755b7f0c42SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
4765b7f0c42SBarry Smith 
4775b7f0c42SBarry Smith   PetscFunctionBegin;
4785b7f0c42SBarry Smith   ctx->checkh    = fun;
4795b7f0c42SBarry Smith   ctx->checkhctx = ectx;
4805b7f0c42SBarry Smith   PetscFunctionReturn(0);
4815b7f0c42SBarry Smith }
4825b7f0c42SBarry Smith EXTERN_C_END
4835b7f0c42SBarry Smith 
4844a2ae208SSatish Balay #undef __FUNCT__
4857e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions"
4867e9d5209SBarry Smith /*@
4877e9d5209SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4887e9d5209SBarry Smith    parameter.
4897e9d5209SBarry Smith 
4907e9d5209SBarry Smith    Collective on Mat
4917e9d5209SBarry Smith 
4927e9d5209SBarry Smith    Input Parameters:
4937e9d5209SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4947e9d5209SBarry Smith 
4957e9d5209SBarry Smith    Options Database Keys:
4967e9d5209SBarry Smith +  -snes_mf_type - <default,wp>
4977e9d5209SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
4987e9d5209SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
4997e9d5209SBarry Smith 
5007e9d5209SBarry Smith    Level: advanced
5017e9d5209SBarry Smith 
5027e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters
5037e9d5209SBarry Smith 
5047e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
5057e9d5209SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
5067e9d5209SBarry Smith @*/
5077e9d5209SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
5087e9d5209SBarry Smith {
5097e9d5209SBarry Smith   MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data;
5107e9d5209SBarry Smith   int          ierr;
5117e9d5209SBarry Smith   PetscTruth   flg;
5127e9d5209SBarry Smith   char         ftype[256];
5137e9d5209SBarry Smith 
5147e9d5209SBarry Smith   PetscFunctionBegin;
5157e9d5209SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
5167e9d5209SBarry Smith 
5177e9d5209SBarry Smith   ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
5187e9d5209SBarry Smith   ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
5197e9d5209SBarry Smith   if (flg) {
5207e9d5209SBarry Smith     ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
5217e9d5209SBarry Smith   }
5227e9d5209SBarry Smith 
52387828ca2SBarry Smith   ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
5247e9d5209SBarry Smith   ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
5257e9d5209SBarry Smith   if (mfctx->snes) {
5267e9d5209SBarry Smith     ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
5277e9d5209SBarry Smith     if (flg) {
5287e9d5209SBarry Smith       KSP ksp;
52994b7f48cSBarry Smith       ierr = SNESGetKSP(mfctx->snes,&ksp);CHKERRQ(ierr);
5307e9d5209SBarry Smith       ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
5317e9d5209SBarry Smith     }
5327e9d5209SBarry Smith   }
5335b7f0c42SBarry Smith   ierr = PetscOptionsName("-snes_mf_check_positivity","Insure that U + h*a is nonnegative","MatSNESMFSetCheckh",&flg);CHKERRQ(ierr);
5345b7f0c42SBarry Smith   if (flg) {
5355b7f0c42SBarry Smith     ierr = MatSNESMFSetCheckh(mat,MatSNESMFCheckPositivity,0);CHKERRQ(ierr);
5365b7f0c42SBarry Smith   }
5377e9d5209SBarry Smith   if (mfctx->ops->setfromoptions) {
5387e9d5209SBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
5397e9d5209SBarry Smith   }
5407e9d5209SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5417e9d5209SBarry Smith   PetscFunctionReturn(0);
5427e9d5209SBarry Smith }
5437e9d5209SBarry Smith 
5440bad9183SKris Buschelman /*MC
545fafad747SKris Buschelman   MATMFFD - MATMFFD = "mffd" - A matrix free matrix type.
5460bad9183SKris Buschelman 
5470bad9183SKris Buschelman   Level: advanced
5480bad9183SKris Buschelman 
5490bad9183SKris Buschelman .seealso: MatCreateMF, MatCreateSNESMF
5500bad9183SKris Buschelman M*/
551fe93831dSBarry Smith EXTERN_C_BEGIN
5527e9d5209SBarry Smith #undef __FUNCT__
5537e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD"
5547e9d5209SBarry Smith int MatCreate_MFFD(Mat A)
5557e9d5209SBarry Smith {
5567e9d5209SBarry Smith   MatSNESMFCtx mfctx;
55765df01d8SBarry Smith   int          ierr;
5587e9d5209SBarry Smith 
5597e9d5209SBarry Smith   PetscFunctionBegin;
5606e087cb5SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5616e087cb5SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
5626e087cb5SMatthew Knepley #endif
5636e087cb5SMatthew Knepley 
5648a124369SBarry Smith   PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD);
5657e9d5209SBarry Smith   PetscLogObjectCreate(mfctx);
5667e9d5209SBarry Smith   mfctx->sp              = 0;
5677e9d5209SBarry Smith   mfctx->snes            = 0;
56877d8c4bbSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
5697e9d5209SBarry Smith   mfctx->recomputeperiod = 1;
5707e9d5209SBarry Smith   mfctx->count           = 0;
5717e9d5209SBarry Smith   mfctx->currenth        = 0.0;
5727e9d5209SBarry Smith   mfctx->historyh        = PETSC_NULL;
5737e9d5209SBarry Smith   mfctx->ncurrenth       = 0;
5747e9d5209SBarry Smith   mfctx->maxcurrenth     = 0;
5757e9d5209SBarry Smith   mfctx->type_name       = 0;
5767e9d5209SBarry Smith   mfctx->usesnes         = PETSC_FALSE;
5777e9d5209SBarry Smith 
578c5c390f1SBarry Smith   mfctx->vshift          = 0.0;
579c5c390f1SBarry Smith   mfctx->vscale          = 1.0;
580c5c390f1SBarry Smith 
5817e9d5209SBarry Smith   /*
5827e9d5209SBarry Smith      Create the empty data structure to contain compute-h routines.
5837e9d5209SBarry Smith      These will be filled in below from the command line options or
5847e9d5209SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
5858a124369SBarry Smith      then it will default in the first use of MatMult_MFFD()
5867e9d5209SBarry Smith   */
5877e9d5209SBarry Smith   mfctx->ops->compute        = 0;
5887e9d5209SBarry Smith   mfctx->ops->destroy        = 0;
5897e9d5209SBarry Smith   mfctx->ops->view           = 0;
5907e9d5209SBarry Smith   mfctx->ops->setfromoptions = 0;
5917e9d5209SBarry Smith   mfctx->hctx                = 0;
5927e9d5209SBarry Smith 
5937e9d5209SBarry Smith   mfctx->func                = 0;
5947e9d5209SBarry Smith   mfctx->funcctx             = 0;
5957e9d5209SBarry Smith   mfctx->funcvec             = 0;
596ba6a83e5SMatthew Knepley   mfctx->w                   = PETSC_NULL;
5977e9d5209SBarry Smith 
59865df01d8SBarry Smith   A->data                = mfctx;
5997e9d5209SBarry Smith 
6008a124369SBarry Smith   A->ops->mult           = MatMult_MFFD;
6018a124369SBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
6028a124369SBarry Smith   A->ops->view           = MatView_MFFD;
6038a124369SBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
6048a124369SBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
605c5c390f1SBarry Smith   A->ops->scale          = MatScale_MFFD;
606c5c390f1SBarry Smith   A->ops->shift          = MatShift_MFFD;
60765df01d8SBarry Smith   A->ops->setfromoptions = MatSNESMFSetFromOptions;
6087e9d5209SBarry Smith 
60965df01d8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr);
610c5c390f1SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBase_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr);
61187828ca2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr);
6125b7f0c42SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetCheckh_C","MatSNESMFSetCheckh_FD",MatSNESMFSetCheckh_FD);CHKERRQ(ierr);
61365df01d8SBarry Smith   mfctx->mat = A;
6147e9d5209SBarry Smith 
6157e9d5209SBarry Smith   PetscFunctionReturn(0);
6167e9d5209SBarry Smith }
617fe93831dSBarry Smith EXTERN_C_END
6187e9d5209SBarry Smith 
6197e9d5209SBarry Smith #undef __FUNCT__
6204a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
6211d1367b7SBarry Smith /*@C
6221d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
6231d1367b7SBarry Smith 
6241d1367b7SBarry Smith    Collective on Vec
6251d1367b7SBarry Smith 
6261d1367b7SBarry Smith    Input Parameters:
6271d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
6281d1367b7SBarry Smith 
6291d1367b7SBarry Smith    Output Parameter:
6301d1367b7SBarry Smith .  J - the matrix-free matrix
6311d1367b7SBarry Smith 
6321d1367b7SBarry Smith    Level: advanced
6331d1367b7SBarry Smith 
6341d1367b7SBarry Smith    Notes:
6351d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
6361d1367b7SBarry Smith    and work space for performing finite difference approximations of
6371d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
6381d1367b7SBarry Smith 
6391d1367b7SBarry Smith    The default code uses the following approach to compute h
6401d1367b7SBarry Smith 
6411d1367b7SBarry Smith .vb
6421d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
6431d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
6441d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
6451d1367b7SBarry Smith  where
6461d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
6471d1367b7SBarry Smith      umin = minimum iterate parameter
6481d1367b7SBarry Smith .ve
6491d1367b7SBarry Smith 
6501d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
6511d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
6521d1367b7SBarry Smith    of the users manual for details.
6531d1367b7SBarry Smith 
6541d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
6551d1367b7SBarry Smith    matrix context.
6561d1367b7SBarry Smith 
6571d1367b7SBarry Smith    Options Database Keys:
6581d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
6591d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
6605b7f0c42SBarry Smith .  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
6615b7f0c42SBarry Smith -  -snes_mf_check_positivity
6621d1367b7SBarry Smith 
6631d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
6641d1367b7SBarry Smith 
6651d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
6661d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
667fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian()
6681d1367b7SBarry Smith 
6691d1367b7SBarry Smith @*/
6701d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J)
6711d1367b7SBarry Smith {
672a4d4d686SBarry Smith   MPI_Comm     comm;
6739a6cb015SBarry Smith   int          n,nloc,ierr;
674a4d4d686SBarry Smith 
675a4d4d686SBarry Smith   PetscFunctionBegin;
6761d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
67765df01d8SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
67865df01d8SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
6797e9d5209SBarry Smith   ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr);
680e56c5435SBarry Smith   ierr = MatRegisterDynamic(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr);
68165df01d8SBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
6829a6cb015SBarry Smith   PetscFunctionReturn(0);
6839a6cb015SBarry Smith }
6849a6cb015SBarry Smith 
685a4d4d686SBarry Smith 
6864a2ae208SSatish Balay #undef __FUNCT__
6874a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
688a4d4d686SBarry Smith /*@
68965f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
690a4d4d686SBarry Smith    parameter.
691a4d4d686SBarry Smith 
692a4d4d686SBarry Smith    Not Collective
693a4d4d686SBarry Smith 
694a4d4d686SBarry Smith    Input Parameters:
6955a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
696a4d4d686SBarry Smith 
697a4d4d686SBarry Smith    Output Paramter:
698a4d4d686SBarry Smith .  h - the differencing step size
699a4d4d686SBarry Smith 
70015091d37SBarry Smith    Level: advanced
70115091d37SBarry Smith 
702a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
703a4d4d686SBarry Smith 
7045a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
7055a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
706a4d4d686SBarry Smith @*/
70787828ca2SBarry Smith int MatSNESMFGetH(Mat mat,PetscScalar *h)
708a4d4d686SBarry Smith {
7097e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
710a4d4d686SBarry Smith 
711a4d4d686SBarry Smith   PetscFunctionBegin;
712a4d4d686SBarry Smith   *h = ctx->currenth;
713a4d4d686SBarry Smith   PetscFunctionReturn(0);
714a4d4d686SBarry Smith }
715a4d4d686SBarry Smith 
7164a2ae208SSatish Balay #undef __FUNCT__
7174a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
718a4d4d686SBarry Smith /*
7195a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
72065f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
72165f2ba5bSLois Curfman McInnes    each step.
722a4d4d686SBarry Smith */
723329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy)
724a4d4d686SBarry Smith {
725a4d4d686SBarry Smith   PC             pc;
7265a655dc6SBarry Smith   MatSNESMFCtx   ctx;
727a4d4d686SBarry Smith   int            ierr;
728a4d4d686SBarry Smith   Mat            mat;
729a4d4d686SBarry Smith   MPI_Comm       comm;
730a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
731a4d4d686SBarry Smith 
732a4d4d686SBarry Smith   PetscFunctionBegin;
733a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
734a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
735a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
736a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7377e9d5209SBarry Smith   ctx  = (MatSNESMFCtx)mat->data;
7387e9d5209SBarry Smith 
739a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
740aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
741d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
742329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
743a4d4d686SBarry Smith #else
744d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
745a4d4d686SBarry Smith #endif
746a4d4d686SBarry Smith   } else {
747d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
748a4d4d686SBarry Smith   }
749a4d4d686SBarry Smith   PetscFunctionReturn(0);
750a4d4d686SBarry Smith }
751a4d4d686SBarry Smith 
7524a2ae208SSatish Balay #undef __FUNCT__
7534a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
75485614651SBarry Smith /*@C
75585614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
75685614651SBarry Smith 
75785614651SBarry Smith    Collective on Mat
75885614651SBarry Smith 
75985614651SBarry Smith    Input Parameters:
76085614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
76185614651SBarry Smith .  v   - workspace vector
76285614651SBarry Smith .  func - the function to use
76385614651SBarry Smith -  funcctx - optional function context passed to function
76485614651SBarry Smith 
76585614651SBarry Smith    Level: advanced
76685614651SBarry Smith 
76785614651SBarry Smith    Notes:
76885614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
76985614651SBarry Smith     matrix inside your compute Jacobian routine
77085614651SBarry Smith 
77185614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
77285614651SBarry Smith 
77385614651SBarry Smith .keywords: SNES, matrix-free, function
77485614651SBarry Smith 
77585614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
77685614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
77785614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
77885614651SBarry Smith @*/
77985614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
78085614651SBarry Smith {
7817e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
78285614651SBarry Smith 
78385614651SBarry Smith   PetscFunctionBegin;
78485614651SBarry Smith   ctx->func    = func;
78585614651SBarry Smith   ctx->funcctx = funcctx;
78685614651SBarry Smith   ctx->funcvec = v;
78785614651SBarry Smith   PetscFunctionReturn(0);
78885614651SBarry Smith }
78985614651SBarry Smith 
790cf57b110SBarry Smith #undef __FUNCT__
791cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni"
792cf57b110SBarry Smith /*@C
793cf57b110SBarry Smith    MatSNESMFSetFunctioni - Sets the function for a single component
794cf57b110SBarry Smith 
795cf57b110SBarry Smith    Collective on Mat
796cf57b110SBarry Smith 
797cf57b110SBarry Smith    Input Parameters:
798cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
799cf57b110SBarry Smith -  funci - the function to use
800cf57b110SBarry Smith 
801cf57b110SBarry Smith    Level: advanced
802cf57b110SBarry Smith 
803cf57b110SBarry Smith    Notes:
804cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
805cf57b110SBarry Smith     matrix inside your compute Jacobian routine
806cf57b110SBarry Smith 
807cf57b110SBarry Smith 
808cf57b110SBarry Smith .keywords: SNES, matrix-free, function
809cf57b110SBarry Smith 
810cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
811cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
812cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
813cf57b110SBarry Smith @*/
81487828ca2SBarry Smith int MatSNESMFSetFunctioni(Mat mat,int (*funci)(int,Vec,PetscScalar*,void *))
815cf57b110SBarry Smith {
81687828ca2SBarry Smith   int  ierr,(*f)(Mat,int (*)(int,Vec,PetscScalar*,void *));
817cf57b110SBarry Smith 
818cf57b110SBarry Smith   PetscFunctionBegin;
8194482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
820c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
82187828ca2SBarry Smith   if (f) {
82287828ca2SBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
82387828ca2SBarry Smith   }
824cf57b110SBarry Smith   PetscFunctionReturn(0);
825cf57b110SBarry Smith }
826cf57b110SBarry Smith 
82787828ca2SBarry Smith 
828cf57b110SBarry Smith #undef __FUNCT__
829cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase"
830cf57b110SBarry Smith /*@C
831cf57b110SBarry Smith    MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation
832cf57b110SBarry Smith 
833cf57b110SBarry Smith    Collective on Mat
834cf57b110SBarry Smith 
835cf57b110SBarry Smith    Input Parameters:
836cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
837cf57b110SBarry Smith -  func - the function to use
838cf57b110SBarry Smith 
839cf57b110SBarry Smith    Level: advanced
840cf57b110SBarry Smith 
841cf57b110SBarry Smith    Notes:
842cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
843cf57b110SBarry Smith     matrix inside your compute Jacobian routine
844cf57b110SBarry Smith 
845cf57b110SBarry Smith 
846cf57b110SBarry Smith .keywords: SNES, matrix-free, function
847cf57b110SBarry Smith 
848cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
849cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
850cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
851cf57b110SBarry Smith @*/
852cf57b110SBarry Smith int MatSNESMFSetFunctioniBase(Mat mat,int (*func)(Vec,void *))
853cf57b110SBarry Smith {
85487828ca2SBarry Smith   int  ierr,(*f)(Mat,int (*)(Vec,void *));
855cf57b110SBarry Smith 
856cf57b110SBarry Smith   PetscFunctionBegin;
8574482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
858c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
85987828ca2SBarry Smith   if (f) {
86087828ca2SBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
86187828ca2SBarry Smith   }
862cf57b110SBarry Smith   PetscFunctionReturn(0);
863cf57b110SBarry Smith }
864cf57b110SBarry Smith 
86585614651SBarry Smith 
8664a2ae208SSatish Balay #undef __FUNCT__
8674a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
868329f5518SBarry Smith /*@
869329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
870329f5518SBarry Smith 
871329f5518SBarry Smith    Collective on Mat
872329f5518SBarry Smith 
873329f5518SBarry Smith    Input Parameters:
874329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
875329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
876329f5518SBarry Smith 
877329f5518SBarry Smith    Options Database Keys:
878329f5518SBarry Smith +  -snes_mf_period <period>
879329f5518SBarry Smith 
880329f5518SBarry Smith    Level: advanced
881329f5518SBarry Smith 
882329f5518SBarry Smith 
883329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
884329f5518SBarry Smith 
885329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
886329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
887329f5518SBarry Smith           MatSNESMFKSPMonitor()
888329f5518SBarry Smith @*/
889329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period)
890329f5518SBarry Smith {
8917e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
892329f5518SBarry Smith 
893329f5518SBarry Smith   PetscFunctionBegin;
894329f5518SBarry Smith   ctx->recomputeperiod = period;
895329f5518SBarry Smith   PetscFunctionReturn(0);
896329f5518SBarry Smith }
897329f5518SBarry Smith 
8984a2ae208SSatish Balay #undef __FUNCT__
8994a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
900a4d4d686SBarry Smith /*@
9015a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
902a4d4d686SBarry Smith    matrix-vector products using finite differences.
903a4d4d686SBarry Smith 
904a4d4d686SBarry Smith    Collective on Mat
905a4d4d686SBarry Smith 
906a4d4d686SBarry Smith    Input Parameters:
9075a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
9089a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
909a4d4d686SBarry Smith                the relative error in the function evaluations)
910a4d4d686SBarry Smith 
91115091d37SBarry Smith    Options Database Keys:
91215091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
91315091d37SBarry Smith 
91415091d37SBarry Smith    Level: advanced
91515091d37SBarry Smith 
916a4d4d686SBarry Smith    Notes:
917a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
918a4d4d686SBarry Smith .vb
91965f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
920a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
921a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
922a4d4d686SBarry Smith .ve
923a4d4d686SBarry Smith 
924a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
925a4d4d686SBarry Smith 
9265a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
9275a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9285a655dc6SBarry Smith           MatSNESMFKSPMonitor()
929a4d4d686SBarry Smith @*/
930329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error)
931a4d4d686SBarry Smith {
9327e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
933a4d4d686SBarry Smith 
934a4d4d686SBarry Smith   PetscFunctionBegin;
935a4d4d686SBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
936a4d4d686SBarry Smith   PetscFunctionReturn(0);
937a4d4d686SBarry Smith }
938a4d4d686SBarry Smith 
9394a2ae208SSatish Balay #undef __FUNCT__
9404a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
941a4d4d686SBarry Smith /*@
94265f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
94365f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
94465f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
94565f2ba5bSLois Curfman McInnes    automatically removed.
946a4d4d686SBarry Smith 
947a4d4d686SBarry Smith    Collective on Mat
948a4d4d686SBarry Smith 
949a4d4d686SBarry Smith    Input Parameters:
950a4d4d686SBarry Smith +  J - the matrix-free matrix context
95174637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
952a4d4d686SBarry Smith 
95315091d37SBarry Smith    Level: advanced
95415091d37SBarry Smith 
955a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
956a4d4d686SBarry Smith 
95774637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
9585a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9595a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
960a4d4d686SBarry Smith @*/
96174637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
962a4d4d686SBarry Smith {
963a4d4d686SBarry Smith   int          ierr;
9647e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
965a4d4d686SBarry Smith   MPI_Comm     comm;
966a4d4d686SBarry Smith 
967a4d4d686SBarry Smith   PetscFunctionBegin;
9682d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
969a4d4d686SBarry Smith 
97085614651SBarry Smith   ctx->sp = nullsp;
97185614651SBarry Smith   ierr    = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
972a4d4d686SBarry Smith   PetscFunctionReturn(0);
973a4d4d686SBarry Smith }
974a4d4d686SBarry Smith 
9754a2ae208SSatish Balay #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
977a4d4d686SBarry Smith /*@
97865f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
97965f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
980a4d4d686SBarry Smith 
981a4d4d686SBarry Smith    Collective on Mat
982a4d4d686SBarry Smith 
983a4d4d686SBarry Smith    Input Parameters:
984a4d4d686SBarry Smith +  J - the matrix-free matrix context
98565f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
98665f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
98765f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
988a4d4d686SBarry Smith 
98915091d37SBarry Smith    Level: advanced
99015091d37SBarry Smith 
991a4d4d686SBarry Smith    Notes:
99265f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
99365f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
994a4d4d686SBarry Smith 
995a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
996a4d4d686SBarry Smith 
9975a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
9985a655dc6SBarry Smith           MatSNESMFResetHHistory(),
9995a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1000a4d4d686SBarry Smith 
1001a4d4d686SBarry Smith @*/
10021836bdbcSSatish Balay int MatSNESMFSetHHistory(Mat J,PetscScalar history[],int nhistory)
1003a4d4d686SBarry Smith {
10047e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1005a4d4d686SBarry Smith 
1006a4d4d686SBarry Smith   PetscFunctionBegin;
1007a4d4d686SBarry Smith   ctx->historyh    = history;
1008a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
1009a4d4d686SBarry Smith   ctx->currenth    = 0;
1010a4d4d686SBarry Smith   PetscFunctionReturn(0);
1011a4d4d686SBarry Smith }
1012a4d4d686SBarry Smith 
10134a2ae208SSatish Balay #undef __FUNCT__
10144a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
1015a4d4d686SBarry Smith /*@
10165a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
1017a4d4d686SBarry Smith    collecting a new set of differencing histories.
1018a4d4d686SBarry Smith 
1019a4d4d686SBarry Smith    Collective on Mat
1020a4d4d686SBarry Smith 
1021a4d4d686SBarry Smith    Input Parameters:
1022a4d4d686SBarry Smith .  J - the matrix-free matrix context
1023a4d4d686SBarry Smith 
102415091d37SBarry Smith    Level: advanced
102515091d37SBarry Smith 
1026a4d4d686SBarry Smith    Notes:
102765f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
1028a4d4d686SBarry Smith 
1029a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1030a4d4d686SBarry Smith 
10315a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10325a655dc6SBarry Smith           MatSNESMFSetHHistory(),
10335a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1034a4d4d686SBarry Smith 
1035a4d4d686SBarry Smith @*/
10365a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
1037a4d4d686SBarry Smith {
10387e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1039a4d4d686SBarry Smith 
1040a4d4d686SBarry Smith   PetscFunctionBegin;
1041be726c96SBarry Smith   ctx->ncurrenth    = 0;
1042a4d4d686SBarry Smith   PetscFunctionReturn(0);
1043a4d4d686SBarry Smith }
1044a4d4d686SBarry Smith 
10454a2ae208SSatish Balay #undef __FUNCT__
1046fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian"
1047fed8bd04SBarry Smith int MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
10481d1367b7SBarry Smith {
10491d1367b7SBarry Smith   int ierr;
10501d1367b7SBarry Smith   PetscFunctionBegin;
10511d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10521d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10531d1367b7SBarry Smith   PetscFunctionReturn(0);
10541d1367b7SBarry Smith }
10551d1367b7SBarry Smith 
10564a2ae208SSatish Balay #undef __FUNCT__
10574a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
10585b7f0c42SBarry Smith /*@
10595b7f0c42SBarry Smith     MatSNESMFSetBase - Sets the vector U at which matrix vector products of the
10605b7f0c42SBarry Smith         Jacobian are computed
10615b7f0c42SBarry Smith 
10625b7f0c42SBarry Smith     Collective on Mat
10635b7f0c42SBarry Smith 
10645b7f0c42SBarry Smith     Input Parameters:
10655b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10665b7f0c42SBarry Smith -   U - the vector
10675b7f0c42SBarry Smith 
10685b7f0c42SBarry Smith     Notes: This is rarely used directly
10695b7f0c42SBarry Smith 
10705b7f0c42SBarry Smith     Level: advanced
10715b7f0c42SBarry Smith 
10725b7f0c42SBarry Smith @*/
10731d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U)
10741d1367b7SBarry Smith {
10753a7fca6bSBarry Smith   int  ierr,(*f)(Mat,Vec);
10761d1367b7SBarry Smith 
10771d1367b7SBarry Smith   PetscFunctionBegin;
10784482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
10794482741eSBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE,2);
1080c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1081cf3bea43SBarry Smith   if (f) {
1082cf3bea43SBarry Smith     ierr = (*f)(J,U);CHKERRQ(ierr);
108349d4803aSBarry Smith   }
10841d1367b7SBarry Smith   PetscFunctionReturn(0);
10851d1367b7SBarry Smith }
1086cf57b110SBarry Smith 
10875b7f0c42SBarry Smith #undef __FUNCT__
10885b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh"
108961860be5SBarry Smith /*@C
10905b7f0c42SBarry Smith     MatSNESMFSetCheckh - Sets a function that checks the computed h and adjusts
10915b7f0c42SBarry Smith         it to satisfy some criteria
1092cf57b110SBarry Smith 
10935b7f0c42SBarry Smith     Collective on Mat
10945b7f0c42SBarry Smith 
10955b7f0c42SBarry Smith     Input Parameters:
10965b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10975b7f0c42SBarry Smith .   fun - the function that checks h
10985b7f0c42SBarry Smith -   ctx - any context needed by the function
10995b7f0c42SBarry Smith 
11005b7f0c42SBarry Smith     Options Database Keys:
11015b7f0c42SBarry Smith .   -snes_mf_check_positivity
11025b7f0c42SBarry Smith 
11035b7f0c42SBarry Smith     Level: advanced
11045b7f0c42SBarry Smith 
11055b7f0c42SBarry Smith     Notes: For example, MatSNESMFSetCheckPositivity() insures that all entries
11065b7f0c42SBarry Smith        of U + h*a are non-negative
11075b7f0c42SBarry Smith 
11085b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckPositivity()
11095b7f0c42SBarry Smith @*/
111061860be5SBarry Smith int MatSNESMFSetCheckh(Mat J,int (*fun)(Vec,Vec,PetscScalar*,void*),void* ctx)
11115b7f0c42SBarry Smith {
111261860be5SBarry Smith   int  ierr,(*f)(Mat,int (*)(Vec,Vec,PetscScalar*,void*),void*);
11135b7f0c42SBarry Smith 
11145b7f0c42SBarry Smith   PetscFunctionBegin;
11154482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
11165b7f0c42SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
11175b7f0c42SBarry Smith   if (f) {
11185b7f0c42SBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
11195b7f0c42SBarry Smith   }
11205b7f0c42SBarry Smith   PetscFunctionReturn(0);
11215b7f0c42SBarry Smith }
11225b7f0c42SBarry Smith 
11235b7f0c42SBarry Smith #undef __FUNCT__
11245b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckPositivity"
11255b7f0c42SBarry Smith /*@
11265b7f0c42SBarry Smith     MatSNESMFCheckPositivity - Checks that all entries in U + h*a are positive or
11275b7f0c42SBarry Smith         zero, decreases h until this is satisfied.
11285b7f0c42SBarry Smith 
11295b7f0c42SBarry Smith     Collective on Vec
11305b7f0c42SBarry Smith 
11315b7f0c42SBarry Smith     Input Parameters:
11325b7f0c42SBarry Smith +   U - base vector that is added to
11335b7f0c42SBarry Smith .   a - vector that is added
11345b7f0c42SBarry Smith .   h - scaling factor on a
11355b7f0c42SBarry Smith -   dummy - context variable (unused)
11365b7f0c42SBarry Smith 
11375b7f0c42SBarry Smith     Options Database Keys:
11385b7f0c42SBarry Smith .   -snes_mf_check_positivity
11395b7f0c42SBarry Smith 
11405b7f0c42SBarry Smith     Level: advanced
11415b7f0c42SBarry Smith 
11425b7f0c42SBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
11435b7f0c42SBarry Smith            MatSNESMFSetCheckh()
11445b7f0c42SBarry Smith 
11455b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckh()
11465b7f0c42SBarry Smith @*/
11475b7f0c42SBarry Smith int MatSNESMFCheckPositivity(Vec U,Vec a,PetscScalar *h,void *dummy)
11485b7f0c42SBarry Smith {
11495b7f0c42SBarry Smith   PetscReal     val, minval;
11505b7f0c42SBarry Smith   PetscScalar   *u_vec, *a_vec;
11515b7f0c42SBarry Smith   int           ierr, i, size;
11525b7f0c42SBarry Smith   MPI_Comm      comm;
11535b7f0c42SBarry Smith 
11545b7f0c42SBarry Smith   PetscFunctionBegin;
11555b7f0c42SBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
11565b7f0c42SBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
11575b7f0c42SBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
11585b7f0c42SBarry Smith   ierr = VecGetLocalSize(U,&size);CHKERRQ(ierr);
115961860be5SBarry Smith   minval = PetscAbsScalar(*h*1.01);
11605b7f0c42SBarry Smith   for(i=0;i<size;i++) {
116161860be5SBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
116261860be5SBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
11635b7f0c42SBarry Smith       if (val < minval) minval = val;
11645b7f0c42SBarry Smith     }
11655b7f0c42SBarry Smith   }
11665b7f0c42SBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
11675b7f0c42SBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
11685b7f0c42SBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
116961860be5SBarry Smith   if (val <= PetscAbsScalar(*h)) {
117061860be5SBarry Smith     PetscLogInfo(U,"MatSNESMFCheckPositivity: Scaling back h from %g to %g\n",PetscRealPart(*h),.99*val);
117161860be5SBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
11725b7f0c42SBarry Smith     else                         *h = -0.99*val;
11735b7f0c42SBarry Smith   }
11745b7f0c42SBarry Smith   PetscFunctionReturn(0);
11755b7f0c42SBarry Smith }
1176cf57b110SBarry Smith 
1177cf57b110SBarry Smith 
1178cf57b110SBarry Smith 
1179cf57b110SBarry Smith 
1180cf57b110SBarry Smith 
1181cf57b110SBarry Smith 
1182cf57b110SBarry Smith 
1183