xref: /petsc/src/snes/mf/snesmfj.c (revision 250ffb4e3479f9821b9ca95e67da6826064da6a3)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*250ffb4eSSatish Balay static char vcid[] = "$Id: snesmfj.c,v 1.94 1999/10/04 23:23:03 balay Exp balay $";
381e6777dSBarry Smith #endif
481e6777dSBarry Smith 
59a6cb015SBarry Smith #include "src/snes/snesimpl.h"
69a6cb015SBarry Smith #include "src/snes/mf/snesmfj.h"   /*I  "snes.h"   I*/
781e6777dSBarry Smith 
85a655dc6SBarry Smith FList MatSNESMFList              = 0;
95a655dc6SBarry Smith int   MatSNESMFRegisterAllCalled = 0;
10a4d4d686SBarry Smith 
115615d1e5SSatish Balay #undef __FUNC__
125a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetType"
13fd4bdd07SBarry Smith /*@C
1465f2ba5bSLois Curfman McInnes     MatSNESMFSetType - Sets the method that is used to compute the
1565f2ba5bSLois Curfman McInnes     differencing parameter for finite difference matrix-free formulations.
169a6cb015SBarry Smith 
179a6cb015SBarry Smith     Input Parameters:
18ef4ad1fdSLois Curfman McInnes +   mat - the "matrix-free" matrix created via MatCreateSNESMF()
199a6cb015SBarry Smith -   ftype - the type requested
209a6cb015SBarry Smith 
2115091d37SBarry Smith     Level: advanced
2215091d37SBarry Smith 
2365f2ba5bSLois Curfman McInnes     Notes:
2465f2ba5bSLois Curfman McInnes     For example, such routines can compute h for use in
2565f2ba5bSLois Curfman McInnes     Jacobian-vector products of the form
2665f2ba5bSLois Curfman McInnes 
2765f2ba5bSLois Curfman McInnes                         F(x+ha) - F(x)
28ef4ad1fdSLois Curfman McInnes           F'(u)a  ~=  ----------------
2965f2ba5bSLois Curfman McInnes                               h
3065f2ba5bSLois Curfman McInnes 
315a655dc6SBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegister()
329a6cb015SBarry Smith @*/
335a655dc6SBarry Smith int MatSNESMFSetType(Mat mat,char *ftype)
34b9fa9cd0SBarry Smith {
355a655dc6SBarry Smith   int          ierr, (*r)(MatSNESMFCtx);
365a655dc6SBarry Smith   MatSNESMFCtx ctx;
37a4d4d686SBarry Smith 
38a4d4d686SBarry Smith   PetscFunctionBegin;
39a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
40a4d4d686SBarry Smith 
419a6cb015SBarry Smith   /* already set, so just return */
42454a90a3SBarry Smith   if (PetscTypeCompare(ctx,ftype)) PetscFunctionReturn(0);
43a4d4d686SBarry Smith 
449a6cb015SBarry Smith   /* destroy the old one if it exists */
459a6cb015SBarry Smith   if (ctx->ops->destroy) {
469a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
479a6cb015SBarry Smith   }
489a6cb015SBarry Smith 
4965f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
505a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
519a6cb015SBarry Smith 
525a655dc6SBarry Smith   ierr =  FListFind(ctx->comm, MatSNESMFList, ftype,(int (**)(void *)) &r );CHKERRQ(ierr);
539a6cb015SBarry Smith 
545a655dc6SBarry Smith   if (!r) SETERRQ(1,1,"Unknown MatSNESMF type given");
559a6cb015SBarry Smith 
569a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
579a6cb015SBarry Smith   ierr = PetscStrncpy(ctx->type_name,ftype,256);CHKERRQ(ierr);
589a6cb015SBarry Smith 
599a6cb015SBarry Smith   PetscFunctionReturn(0);
609a6cb015SBarry Smith }
619a6cb015SBarry Smith 
629a6cb015SBarry Smith /*MC
6365f2ba5bSLois Curfman McInnes    MatSNESMFRegister - Adds a method to the MatSNESMF registry.
649a6cb015SBarry Smith 
659a6cb015SBarry Smith    Synopsis:
665a655dc6SBarry Smith    MatSNESMFRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
679a6cb015SBarry Smith 
689a6cb015SBarry Smith    Not Collective
699a6cb015SBarry Smith 
709a6cb015SBarry Smith    Input Parameters:
719a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
729a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
739a6cb015SBarry Smith .  name_create - name of routine to create method context
749a6cb015SBarry Smith -  routine_create - routine to create method context
759a6cb015SBarry Smith 
7615091d37SBarry Smith    Level: developer
7715091d37SBarry Smith 
789a6cb015SBarry Smith    Notes:
795a655dc6SBarry Smith    MatSNESMFRegister() may be called multiple times to add several user-defined solvers.
809a6cb015SBarry Smith 
819a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
829a6cb015SBarry Smith    is ignored.
839a6cb015SBarry Smith 
849a6cb015SBarry Smith    Sample usage:
859a6cb015SBarry Smith .vb
865a655dc6SBarry Smith    MatSNESMFRegister("my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
879a6cb015SBarry Smith                "MyHCreate",MyHCreate);
889a6cb015SBarry Smith .ve
899a6cb015SBarry Smith 
909a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
915a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
929a6cb015SBarry Smith    or at runtime via the option
939a6cb015SBarry Smith $     -snes_mf_type my_h
949a6cb015SBarry Smith 
955a655dc6SBarry Smith .keywords: MatSNESMF, register
969a6cb015SBarry Smith 
975a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
989a6cb015SBarry Smith M*/
999a6cb015SBarry Smith 
1009a6cb015SBarry Smith #undef __FUNC__
1015a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegister_Private"
1025a655dc6SBarry Smith int MatSNESMFRegister_Private(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
1039a6cb015SBarry Smith {
1049a6cb015SBarry Smith   int ierr;
1059a6cb015SBarry Smith   char fullname[256];
1069a6cb015SBarry Smith 
1079a6cb015SBarry Smith   PetscFunctionBegin;
108*250ffb4eSSatish Balay   ierr = FListConcat_Private(path,name,fullname); CHKERRQ(ierr);
1095a655dc6SBarry Smith   ierr = FListAdd_Private(&MatSNESMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
1109a6cb015SBarry Smith   PetscFunctionReturn(0);
1119a6cb015SBarry Smith }
1129a6cb015SBarry Smith 
1139a6cb015SBarry Smith 
1149a6cb015SBarry Smith #undef __FUNC__
1155a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegisterDestroy"
1169a6cb015SBarry Smith /*@C
1175a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
1185a655dc6SBarry Smith    registered by MatSNESMFRegister().
1199a6cb015SBarry Smith 
1209a6cb015SBarry Smith    Not Collective
1219a6cb015SBarry Smith 
12215091d37SBarry Smith    Level: developer
12315091d37SBarry Smith 
1245a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1259a6cb015SBarry Smith 
1265a655dc6SBarry Smith .seealso: MatSNESMFRegister(), MatSNESMFRegisterAll()
1279a6cb015SBarry Smith @*/
1285a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1299a6cb015SBarry Smith {
1309a6cb015SBarry Smith   int ierr;
1319a6cb015SBarry Smith 
1329a6cb015SBarry Smith   PetscFunctionBegin;
1335a655dc6SBarry Smith   if (MatSNESMFList) {
1345a655dc6SBarry Smith     ierr = FListDestroy( MatSNESMFList );CHKERRQ(ierr);
1355a655dc6SBarry Smith     MatSNESMFList = 0;
1369a6cb015SBarry Smith   }
1375a655dc6SBarry Smith   MatSNESMFRegisterAllCalled = 0;
1389a6cb015SBarry Smith   PetscFunctionReturn(0);
1399a6cb015SBarry Smith }
1409a6cb015SBarry Smith 
1419a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
142a4d4d686SBarry Smith #undef __FUNC__
1435a655dc6SBarry Smith #define __FUNC__ "MatSNESMFDestroy_Private"
1445a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat)
145a4d4d686SBarry Smith {
146a4d4d686SBarry Smith   int          ierr;
1475a655dc6SBarry Smith   MatSNESMFCtx ctx;
148fae171e0SBarry Smith 
1493a40ed3dSBarry Smith   PetscFunctionBegin;
1500a25c783SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
151b9fa9cd0SBarry Smith   ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
1529a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
153b4fd4287SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
154606d414cSSatish Balay   ierr = PetscFree(ctx->ops);CHKERRQ(ierr);
155606d414cSSatish Balay   ierr = PetscFree(ctx);CHKERRQ(ierr);
1563a40ed3dSBarry Smith   PetscFunctionReturn(0);
157b9fa9cd0SBarry Smith }
15850361f65SLois Curfman McInnes 
1595615d1e5SSatish Balay #undef __FUNC__
1605a655dc6SBarry Smith #define __FUNC__ "MatSNESMFView_Private"
16139e2f89bSBarry Smith /*
1625a655dc6SBarry Smith    MatSNESMFView_Private - Views matrix-free parameters.
1638f6e3e37SBarry Smith 
16439e2f89bSBarry Smith */
1655a655dc6SBarry Smith int MatSNESMFView_Private(Mat J,Viewer viewer)
166eb9086c3SLois Curfman McInnes {
167eb9086c3SLois Curfman McInnes   int          ierr;
1685a655dc6SBarry Smith   MatSNESMFCtx ctx;
169eb9086c3SLois Curfman McInnes   MPI_Comm     comm;
170eb9086c3SLois Curfman McInnes   FILE         *fd;
171eb9086c3SLois Curfman McInnes 
1723a40ed3dSBarry Smith   PetscFunctionBegin;
1732d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
174eb9086c3SLois Curfman McInnes   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
175eb9086c3SLois Curfman McInnes   ierr = ViewerASCIIGetPointer(viewer,&fd);CHKERRQ(ierr);
176454a90a3SBarry Smith   if (PetscTypeCompare(viewer,ASCII_VIEWER)) {
177d132466eSBarry Smith      ierr = PetscFPrintf(comm,fd,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
178d132466eSBarry Smith      ierr = PetscFPrintf(comm,fd,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
179d132466eSBarry Smith      ierr = PetscFPrintf(ctx->comm,fd,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
1809a6cb015SBarry Smith      if (ctx->ops->view) {
1819a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1829a6cb015SBarry Smith      }
1835cd90555SBarry Smith   } else {
1845cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported for this object");
185eb9086c3SLois Curfman McInnes   }
1863a40ed3dSBarry Smith   PetscFunctionReturn(0);
187eb9086c3SLois Curfman McInnes }
188eb9086c3SLois Curfman McInnes 
189be726c96SBarry Smith #undef __FUNC__
1905a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAssemblyEnd_Private"
191be726c96SBarry Smith /*
1925a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
19365f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
194be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
19565f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
19665f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
197be726c96SBarry Smith */
1985a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J)
199be726c96SBarry Smith {
200be726c96SBarry Smith   int            ierr;
201be726c96SBarry Smith 
202be726c96SBarry Smith   PetscFunctionBegin;
2035a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
204be726c96SBarry Smith   PetscFunctionReturn(0);
205be726c96SBarry Smith }
206be726c96SBarry Smith 
207c481317fSBarry Smith 
2085615d1e5SSatish Balay #undef __FUNC__
2095a655dc6SBarry Smith #define __FUNC__ "MatSNESMFMult_Private"
210eb9086c3SLois Curfman McInnes /*
2115a655dc6SBarry Smith   MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector
212eb9086c3SLois Curfman McInnes   product, y = F'(u)*a:
213a4d4d686SBarry Smith 
2149a6cb015SBarry Smith         y ~= ( F(u + ha) - F(u) )/h,
215eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
216eb9086c3SLois Curfman McInnes         u = current iterate
217eb9086c3SLois Curfman McInnes         h = difference interval
218eb9086c3SLois Curfman McInnes */
2195a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y)
22039e2f89bSBarry Smith {
2215a655dc6SBarry Smith   MatSNESMFCtx ctx;
222fae171e0SBarry Smith   SNES           snes;
223a4d4d686SBarry Smith   Scalar         h, mone = -1.0;
224fae171e0SBarry Smith   Vec            w,U,F;
225a305c92eSSatish Balay   int            ierr, (*eval_fct)(SNES,Vec,Vec)=0;
22639e2f89bSBarry Smith 
2273a40ed3dSBarry Smith   PetscFunctionBegin;
2289a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2299a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2309a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2319a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
23256cd22aeSBarry Smith   PLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);
23356cd22aeSBarry Smith 
2346e38a044SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
235fae171e0SBarry Smith   snes = ctx->snes;
236fae171e0SBarry Smith   w    = ctx->w;
23778b31e54SBarry Smith   ierr = SNESGetSolution(snes,&U);CHKERRQ(ierr);
23850361f65SLois Curfman McInnes 
23985614651SBarry Smith   /*
24085614651SBarry Smith       Compute differencing parameter
24185614651SBarry Smith   */
2429a6cb015SBarry Smith   if (!ctx->ops->compute) {
2435a655dc6SBarry Smith     ierr = MatSNESMFSetType(mat,"default");CHKERRQ(ierr);
2445a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2459a6cb015SBarry Smith   }
2469a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
247a4d4d686SBarry Smith 
248a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
249a4d4d686SBarry Smith   ctx->currenth = h;
250aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
251a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g + %g i\n",PetscReal(h),PetscImaginary(h));
252a4d4d686SBarry Smith #else
253a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g\n",h);
254a4d4d686SBarry Smith #endif
255a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
25685614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
257a4d4d686SBarry Smith   }
25885614651SBarry Smith   ctx->ncurrenth++;
259a4d4d686SBarry Smith 
26085614651SBarry Smith   /* w = u + ha */
261a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
26285614651SBarry Smith 
26385614651SBarry Smith 
26485614651SBarry Smith   if (!ctx->func) {
26585614651SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
26685614651SBarry Smith       eval_fct = SNESComputeFunction;
267184914b5SBarry Smith       ierr     = SNESGetFunction(snes,&F,PETSC_NULL);CHKERRQ(ierr);
26885614651SBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
26985614651SBarry Smith       eval_fct = SNESComputeGradient;
270184914b5SBarry Smith       ierr     = SNESGetGradient(snes,&F,PETSC_NULL);CHKERRQ(ierr);
27185614651SBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class");
272a4d4d686SBarry 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);
285a4d4d686SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceRemove(ctx->sp,y);CHKERRQ(ierr);}
286a4d4d686SBarry Smith 
287a4d4d686SBarry Smith   PLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);
288a4d4d686SBarry Smith   PetscFunctionReturn(0);
289a4d4d686SBarry Smith }
290a4d4d686SBarry Smith 
291a4d4d686SBarry Smith #undef __FUNC__
2925a655dc6SBarry Smith #define __FUNC__ "MatCreateSNESMF"
293a4d4d686SBarry Smith /*@C
29465f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
29565f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
29665f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
297a4d4d686SBarry Smith 
298a4d4d686SBarry Smith    Collective on SNES and Vec
299a4d4d686SBarry Smith 
300a4d4d686SBarry Smith    Input Parameters:
301a4d4d686SBarry Smith +  snes - the SNES context
302a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
303a4d4d686SBarry Smith 
304a4d4d686SBarry Smith    Output Parameter:
305a4d4d686SBarry Smith .  J - the matrix-free matrix
306a4d4d686SBarry Smith 
30715091d37SBarry Smith    Level: advanced
30815091d37SBarry Smith 
309a4d4d686SBarry Smith    Notes:
310a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
311a4d4d686SBarry Smith    and work space for performing finite difference approximations of
31265f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
3139a6cb015SBarry Smith 
3149a6cb015SBarry Smith    The default code uses the following approach to compute h
315a4d4d686SBarry Smith 
316a4d4d686SBarry Smith .vb
31765f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
318a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
319a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
320a4d4d686SBarry Smith  where
321a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
322a4d4d686SBarry Smith      umin = minimum iterate parameter
323a4d4d686SBarry Smith .ve
324a4d4d686SBarry Smith 
3255a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
32665f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
32765f2ba5bSLois Curfman McInnes    of the users manual for details.
328a4d4d686SBarry Smith 
329a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
330a4d4d686SBarry Smith    matrix context.
331a4d4d686SBarry Smith 
332a4d4d686SBarry Smith    Options Database Keys:
333a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
3349a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
335a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
336a4d4d686SBarry Smith 
337a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
338a4d4d686SBarry Smith 
3395a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
3405a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
3415a655dc6SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegister()
342a4d4d686SBarry Smith 
343a4d4d686SBarry Smith @*/
3445a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x, Mat *J)
345a4d4d686SBarry Smith {
346a4d4d686SBarry Smith   MPI_Comm     comm;
3475a655dc6SBarry Smith   MatSNESMFCtx mfctx;
3489a6cb015SBarry Smith   int          n, nloc, ierr;
349a4d4d686SBarry Smith 
350a4d4d686SBarry Smith   PetscFunctionBegin;
3515a655dc6SBarry Smith   mfctx = (MatSNESMFCtx) PetscMalloc(sizeof(struct _p_MatSNESMFCtx));CHKPTRQ(mfctx);
3525a655dc6SBarry Smith   PLogObjectMemory(snes,sizeof(MatSNESMFCtx));
3539a6cb015SBarry Smith   mfctx->comm         = snes->comm;
354a4d4d686SBarry Smith   mfctx->sp           = 0;
355a4d4d686SBarry Smith   mfctx->snes         = snes;
356a4d4d686SBarry Smith   mfctx->error_rel    = 1.e-8; /* assumes double precision */
357a4d4d686SBarry Smith   mfctx->currenth     = 0.0;
358a4d4d686SBarry Smith   mfctx->historyh     = PETSC_NULL;
359a4d4d686SBarry Smith   mfctx->ncurrenth    = 0;
360a4d4d686SBarry Smith   mfctx->maxcurrenth  = 0;
361fc3d5af4SSatish Balay   ierr = PetscMemzero(mfctx->type_name,256*sizeof(char));CHKERRQ(ierr);
362a4d4d686SBarry Smith 
3639a6cb015SBarry Smith   /*
3649a6cb015SBarry Smith      Create the empty data structure to contain compute-h routines.
3659a6cb015SBarry Smith      These will be filled in below from the command line options or
3665a655dc6SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
3675a655dc6SBarry Smith      then it will default in the first use of MatSNESMFMult_private()
3689a6cb015SBarry Smith   */
3699a6cb015SBarry Smith   mfctx->ops                 = (MFOps *)PetscMalloc(sizeof(MFOps));CHKPTRQ(mfctx->ops);
3709a6cb015SBarry Smith   mfctx->ops->compute        = 0;
3719a6cb015SBarry Smith   mfctx->ops->destroy        = 0;
3729a6cb015SBarry Smith   mfctx->ops->view           = 0;
3739a6cb015SBarry Smith   mfctx->ops->printhelp      = 0;
3749a6cb015SBarry Smith   mfctx->ops->setfromoptions = 0;
3759a6cb015SBarry Smith   mfctx->hctx                = 0;
3769a6cb015SBarry Smith 
37785614651SBarry Smith   mfctx->func                = 0;
37885614651SBarry Smith   mfctx->funcctx             = 0;
37985614651SBarry Smith   mfctx->funcvec             = 0;
38085614651SBarry Smith 
381a4d4d686SBarry Smith   ierr = VecDuplicate(x,&mfctx->w);CHKERRQ(ierr);
382a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
383a4d4d686SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
384a4d4d686SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
385ffa0fd9eSBarry Smith   ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J);CHKERRQ(ierr);
3865a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr);
3875a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr);
3885a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private);CHKERRQ(ierr);
3895a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr);
390a4d4d686SBarry Smith   PLogObjectParent(*J,mfctx->w);
391a4d4d686SBarry Smith   PLogObjectParent(snes,*J);
3929a6cb015SBarry Smith 
3939a6cb015SBarry Smith   mfctx->mat = *J;
3949a6cb015SBarry Smith 
3959a6cb015SBarry Smith 
3969a6cb015SBarry Smith   PetscFunctionReturn(0);
3979a6cb015SBarry Smith }
3989a6cb015SBarry Smith 
3999a6cb015SBarry Smith #undef __FUNC__
4005a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFromOptions"
4019a6cb015SBarry Smith /*@
4025a655dc6SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4039a6cb015SBarry Smith    parameter.
4049a6cb015SBarry Smith 
4059a6cb015SBarry Smith    Collective on Mat
4069a6cb015SBarry Smith 
4079a6cb015SBarry Smith    Input Parameters:
4085a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4095a655dc6SBarry Smith 
4105a655dc6SBarry Smith    Options Database Keys:
4115a655dc6SBarry Smith +  -snes_mf_type - <default,wp>
4125a655dc6SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
4139a6cb015SBarry Smith 
41415091d37SBarry Smith    Level: advanced
41515091d37SBarry Smith 
4169a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters
4179a6cb015SBarry Smith 
4185a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4195a655dc6SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
4209a6cb015SBarry Smith @*/
4215a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
4229a6cb015SBarry Smith {
4235a655dc6SBarry Smith   MatSNESMFCtx mfctx;
4249a6cb015SBarry Smith   int          ierr,flg;
4259a6cb015SBarry Smith   char         ftype[256],p[64];
4269a6cb015SBarry Smith 
4279a6cb015SBarry Smith   PetscFunctionBegin;
4289a6cb015SBarry Smith   ierr = MatShellGetContext(mat,(void **)&mfctx);CHKERRQ(ierr);
4299a6cb015SBarry Smith   if (mfctx) {
4309a6cb015SBarry Smith     /* allow user to set the type */
4319a6cb015SBarry Smith     ierr = OptionsGetString(mfctx->snes->prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr);
4329a6cb015SBarry Smith     if (flg) {
4335a655dc6SBarry Smith       ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
4349a6cb015SBarry Smith     }
4359a6cb015SBarry Smith 
4369a6cb015SBarry Smith     ierr = OptionsGetDouble(mfctx->snes->prefix,"-snes_mf_err",&mfctx->error_rel,&flg);CHKERRQ(ierr);
4379a6cb015SBarry Smith     if (mfctx->ops->setfromoptions) {
4389a6cb015SBarry Smith       ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
4399a6cb015SBarry Smith     }
4409a6cb015SBarry Smith 
4419a6cb015SBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
442d132466eSBarry Smith     ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
443d132466eSBarry Smith     if (mfctx->snes->prefix) {ierr = PetscStrcat(p,mfctx->snes->prefix);CHKERRQ(ierr);}
4449a6cb015SBarry Smith     if (flg) {
445d132466eSBarry Smith       ierr = (*PetscHelpPrintf)(mfctx->snes->comm,"   %ssnes_mf_err <err>: set sqrt rel error in function (default %g)\n",p,mfctx->error_rel);CHKERRQ(ierr);
4469a6cb015SBarry Smith       if (mfctx->ops->printhelp) {
447d132466eSBarry Smith         ierr = (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr);
4489a6cb015SBarry Smith       }
4499a6cb015SBarry Smith     }
4509a6cb015SBarry Smith   }
451a4d4d686SBarry Smith   PetscFunctionReturn(0);
452a4d4d686SBarry Smith }
453a4d4d686SBarry Smith 
454a4d4d686SBarry Smith #undef __FUNC__
4555a655dc6SBarry Smith #define __FUNC__ "MatSNESMFGetH"
456a4d4d686SBarry Smith /*@
45765f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
458a4d4d686SBarry Smith    parameter.
459a4d4d686SBarry Smith 
460a4d4d686SBarry Smith    Not Collective
461a4d4d686SBarry Smith 
462a4d4d686SBarry Smith    Input Parameters:
4635a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
464a4d4d686SBarry Smith 
465a4d4d686SBarry Smith    Output Paramter:
466a4d4d686SBarry Smith .  h - the differencing step size
467a4d4d686SBarry Smith 
46815091d37SBarry Smith    Level: advanced
46915091d37SBarry Smith 
470a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
471a4d4d686SBarry Smith 
4725a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4735a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
474a4d4d686SBarry Smith @*/
4755a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h)
476a4d4d686SBarry Smith {
4775a655dc6SBarry Smith   MatSNESMFCtx ctx;
478a4d4d686SBarry Smith   int          ierr;
479a4d4d686SBarry Smith 
480a4d4d686SBarry Smith   PetscFunctionBegin;
481a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
482a4d4d686SBarry Smith   if (ctx) {
483a4d4d686SBarry Smith     *h = ctx->currenth;
484a4d4d686SBarry Smith   }
485a4d4d686SBarry Smith   PetscFunctionReturn(0);
486a4d4d686SBarry Smith }
487a4d4d686SBarry Smith 
488a4d4d686SBarry Smith #undef __FUNC__
4895a655dc6SBarry Smith #define __FUNC__ "MatSNESMFKSPMonitor"
490a4d4d686SBarry Smith /*
4915a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
49265f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
49365f2ba5bSLois Curfman McInnes    each step.
494a4d4d686SBarry Smith */
4955a655dc6SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,double rnorm,void *dummy)
496a4d4d686SBarry Smith {
497a4d4d686SBarry Smith   PC             pc;
4985a655dc6SBarry Smith   MatSNESMFCtx   ctx;
499a4d4d686SBarry Smith   int            ierr;
500a4d4d686SBarry Smith   Mat            mat;
501a4d4d686SBarry Smith   MPI_Comm       comm;
502a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
503a4d4d686SBarry Smith 
504a4d4d686SBarry Smith   PetscFunctionBegin;
505a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
506a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
507a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
508a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
509a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
5109a6cb015SBarry Smith   if (!ctx) {
5119a6cb015SBarry Smith     SETERRQ(1,1,"Matrix is not a matrix free shell matrix");
5129a6cb015SBarry Smith   }
513a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
514aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
515d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
516d132466eSBarry Smith                 PetscReal(ctx->currenth),PetscImaginary(ctx->currenth));CHKERRQ(ierr);
517a4d4d686SBarry Smith #else
518d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
519a4d4d686SBarry Smith #endif
520a4d4d686SBarry Smith   } else {
521d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
522a4d4d686SBarry Smith   }
523a4d4d686SBarry Smith   PetscFunctionReturn(0);
524a4d4d686SBarry Smith }
525a4d4d686SBarry Smith 
526a4d4d686SBarry Smith #undef __FUNC__
52785614651SBarry Smith #define __FUNC__ "MatSNESMFSetFunction"
52885614651SBarry Smith /*@C
52985614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
53085614651SBarry Smith 
53185614651SBarry Smith    Collective on Mat
53285614651SBarry Smith 
53385614651SBarry Smith    Input Parameters:
53485614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
53585614651SBarry Smith .  v   - workspace vector
53685614651SBarry Smith .  func - the function to use
53785614651SBarry Smith -  funcctx - optional function context passed to function
53885614651SBarry Smith 
53985614651SBarry Smith    Level: advanced
54085614651SBarry Smith 
54185614651SBarry Smith    Notes:
54285614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
54385614651SBarry Smith     matrix inside your compute Jacobian routine
54485614651SBarry Smith 
54585614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
54685614651SBarry Smith 
54785614651SBarry Smith .keywords: SNES, matrix-free, function
54885614651SBarry Smith 
54985614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
55085614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
55185614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
55285614651SBarry Smith @*/
55385614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
55485614651SBarry Smith {
55585614651SBarry Smith   MatSNESMFCtx ctx;
55685614651SBarry Smith   int          ierr;
55785614651SBarry Smith 
55885614651SBarry Smith   PetscFunctionBegin;
55985614651SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
56085614651SBarry Smith   if (ctx) {
56185614651SBarry Smith     ctx->func    = func;
56285614651SBarry Smith     ctx->funcctx = funcctx;
56385614651SBarry Smith     ctx->funcvec = v;
56485614651SBarry Smith   }
56585614651SBarry Smith   PetscFunctionReturn(0);
56685614651SBarry Smith }
56785614651SBarry Smith 
56885614651SBarry Smith 
56985614651SBarry Smith #undef __FUNC__
5705a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFunctionError"
571a4d4d686SBarry Smith /*@
5725a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
573a4d4d686SBarry Smith    matrix-vector products using finite differences.
574a4d4d686SBarry Smith 
575a4d4d686SBarry Smith    Collective on Mat
576a4d4d686SBarry Smith 
577a4d4d686SBarry Smith    Input Parameters:
5785a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
5799a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
580a4d4d686SBarry Smith                the relative error in the function evaluations)
581a4d4d686SBarry Smith 
58215091d37SBarry Smith    Options Database Keys:
58315091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
58415091d37SBarry Smith 
58515091d37SBarry Smith    Level: advanced
58615091d37SBarry Smith 
587a4d4d686SBarry Smith    Notes:
588a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
589a4d4d686SBarry Smith .vb
59065f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
591a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
592a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
593a4d4d686SBarry Smith .ve
594a4d4d686SBarry Smith 
595a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
596a4d4d686SBarry Smith 
5975a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
5985a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
5995a655dc6SBarry Smith           MatSNESMFKSPMonitor()
600a4d4d686SBarry Smith @*/
6015a655dc6SBarry Smith int MatSNESMFSetFunctionError(Mat mat,double error)
602a4d4d686SBarry Smith {
6035a655dc6SBarry Smith   MatSNESMFCtx ctx;
604a4d4d686SBarry Smith   int          ierr;
605a4d4d686SBarry Smith 
606a4d4d686SBarry Smith   PetscFunctionBegin;
607a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
608a4d4d686SBarry Smith   if (ctx) {
609a4d4d686SBarry Smith     if (error != PETSC_DEFAULT) ctx->error_rel = error;
610a4d4d686SBarry Smith   }
611a4d4d686SBarry Smith   PetscFunctionReturn(0);
612a4d4d686SBarry Smith }
613a4d4d686SBarry Smith 
614a4d4d686SBarry Smith #undef __FUNC__
6155a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAddNullSpace"
616a4d4d686SBarry Smith /*@
61765f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
61865f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
61965f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
62065f2ba5bSLois Curfman McInnes    automatically removed.
621a4d4d686SBarry Smith 
622a4d4d686SBarry Smith    Collective on Mat
623a4d4d686SBarry Smith 
624a4d4d686SBarry Smith    Input Parameters:
625a4d4d686SBarry Smith +  J - the matrix-free matrix context
62685614651SBarry Smith -  nullsp - object created with PCNullSpaceCreate()
627a4d4d686SBarry Smith 
62815091d37SBarry Smith    Level: advanced
62915091d37SBarry Smith 
630a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
631a4d4d686SBarry Smith 
63253b79920SLois Curfman McInnes .seealso: PCNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
6335a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
6345a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
635a4d4d686SBarry Smith @*/
63685614651SBarry Smith int MatSNESMFAddNullSpace(Mat J,PCNullSpace nullsp)
637a4d4d686SBarry Smith {
638a4d4d686SBarry Smith   int          ierr;
6395a655dc6SBarry Smith   MatSNESMFCtx ctx;
640a4d4d686SBarry Smith   MPI_Comm     comm;
641a4d4d686SBarry Smith 
642a4d4d686SBarry Smith   PetscFunctionBegin;
6432d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
644a4d4d686SBarry Smith 
645a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
646a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
647a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
64885614651SBarry Smith   ctx->sp = nullsp;
64985614651SBarry Smith   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
650a4d4d686SBarry Smith   PetscFunctionReturn(0);
651a4d4d686SBarry Smith }
652a4d4d686SBarry Smith 
653a4d4d686SBarry Smith #undef __FUNC__
6545a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetHHistory"
655a4d4d686SBarry Smith /*@
65665f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
65765f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
658a4d4d686SBarry Smith 
659a4d4d686SBarry Smith    Collective on Mat
660a4d4d686SBarry Smith 
661a4d4d686SBarry Smith    Input Parameters:
662a4d4d686SBarry Smith +  J - the matrix-free matrix context
66365f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
66465f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
66565f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
666a4d4d686SBarry Smith 
66715091d37SBarry Smith    Level: advanced
66815091d37SBarry Smith 
669a4d4d686SBarry Smith    Notes:
67065f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
67165f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
672a4d4d686SBarry Smith 
673a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
674a4d4d686SBarry Smith 
6755a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
6765a655dc6SBarry Smith           MatSNESMFResetHHistory(),
6775a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
678a4d4d686SBarry Smith 
679a4d4d686SBarry Smith @*/
6805a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory)
681a4d4d686SBarry Smith {
682a4d4d686SBarry Smith   int          ierr;
6835a655dc6SBarry Smith   MatSNESMFCtx ctx;
684a4d4d686SBarry Smith 
685a4d4d686SBarry Smith   PetscFunctionBegin;
686a4d4d686SBarry Smith 
687a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
688a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
689a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
690a4d4d686SBarry Smith   ctx->historyh    = history;
691a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
692a4d4d686SBarry Smith   ctx->currenth    = 0;
693a4d4d686SBarry Smith 
694a4d4d686SBarry Smith   PetscFunctionReturn(0);
695a4d4d686SBarry Smith }
696a4d4d686SBarry Smith 
697a4d4d686SBarry Smith #undef __FUNC__
6985a655dc6SBarry Smith #define __FUNC__ "MatSNESMFResetHHistory"
699a4d4d686SBarry Smith /*@
7005a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
701a4d4d686SBarry Smith    collecting a new set of differencing histories.
702a4d4d686SBarry Smith 
703a4d4d686SBarry Smith    Collective on Mat
704a4d4d686SBarry Smith 
705a4d4d686SBarry Smith    Input Parameters:
706a4d4d686SBarry Smith .  J - the matrix-free matrix context
707a4d4d686SBarry Smith 
70815091d37SBarry Smith    Level: advanced
70915091d37SBarry Smith 
710a4d4d686SBarry Smith    Notes:
71165f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
712a4d4d686SBarry Smith 
713a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
714a4d4d686SBarry Smith 
7155a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
7165a655dc6SBarry Smith           MatSNESMFSetHHistory(),
7175a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
718a4d4d686SBarry Smith 
719a4d4d686SBarry Smith @*/
7205a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
721a4d4d686SBarry Smith {
722a4d4d686SBarry Smith   int          ierr;
7235a655dc6SBarry Smith   MatSNESMFCtx ctx;
724a4d4d686SBarry Smith 
725a4d4d686SBarry Smith   PetscFunctionBegin;
726a4d4d686SBarry Smith 
727a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
728a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
729a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
730be726c96SBarry Smith   ctx->ncurrenth    = 0;
731a4d4d686SBarry Smith 
732a4d4d686SBarry Smith   PetscFunctionReturn(0);
733a4d4d686SBarry Smith }
734a4d4d686SBarry Smith 
735