xref: /petsc/src/snes/mf/snesmfj.c (revision 0f5bd95cca42d693ada6d048329ab39533680180)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*0f5bd95cSBarry Smith static char vcid[] = "$Id: snesmfj.c,v 1.95 1999/10/06 23:41:22 balay Exp bsmith $";
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;
37*0f5bd95cSBarry Smith   int          match;
38a4d4d686SBarry Smith 
39a4d4d686SBarry Smith   PetscFunctionBegin;
40*0f5bd95cSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
41*0f5bd95cSBarry Smith   PetscValidCharPointer(ftype);
42*0f5bd95cSBarry Smith 
43a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
44a4d4d686SBarry Smith 
459a6cb015SBarry Smith   /* already set, so just return */
46*0f5bd95cSBarry Smith   match = PetscTypeCompare(ctx,ftype);
47*0f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
48a4d4d686SBarry Smith 
499a6cb015SBarry Smith   /* destroy the old one if it exists */
509a6cb015SBarry Smith   if (ctx->ops->destroy) {
519a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
529a6cb015SBarry Smith   }
539a6cb015SBarry Smith 
5465f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
555a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
569a6cb015SBarry Smith 
575a655dc6SBarry Smith   ierr =  FListFind(ctx->comm, MatSNESMFList, ftype,(int (**)(void *)) &r );CHKERRQ(ierr);
589a6cb015SBarry Smith 
595a655dc6SBarry Smith   if (!r) SETERRQ(1,1,"Unknown MatSNESMF type given");
609a6cb015SBarry Smith 
619a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
629a6cb015SBarry Smith   ierr = PetscStrncpy(ctx->type_name,ftype,256);CHKERRQ(ierr);
639a6cb015SBarry Smith 
649a6cb015SBarry Smith   PetscFunctionReturn(0);
659a6cb015SBarry Smith }
669a6cb015SBarry Smith 
679a6cb015SBarry Smith /*MC
6865f2ba5bSLois Curfman McInnes    MatSNESMFRegister - Adds a method to the MatSNESMF registry.
699a6cb015SBarry Smith 
709a6cb015SBarry Smith    Synopsis:
715a655dc6SBarry Smith    MatSNESMFRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
729a6cb015SBarry Smith 
739a6cb015SBarry Smith    Not Collective
749a6cb015SBarry Smith 
759a6cb015SBarry Smith    Input Parameters:
769a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
779a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
789a6cb015SBarry Smith .  name_create - name of routine to create method context
799a6cb015SBarry Smith -  routine_create - routine to create method context
809a6cb015SBarry Smith 
8115091d37SBarry Smith    Level: developer
8215091d37SBarry Smith 
839a6cb015SBarry Smith    Notes:
845a655dc6SBarry Smith    MatSNESMFRegister() may be called multiple times to add several user-defined solvers.
859a6cb015SBarry Smith 
869a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
879a6cb015SBarry Smith    is ignored.
889a6cb015SBarry Smith 
899a6cb015SBarry Smith    Sample usage:
909a6cb015SBarry Smith .vb
915a655dc6SBarry Smith    MatSNESMFRegister("my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
929a6cb015SBarry Smith                "MyHCreate",MyHCreate);
939a6cb015SBarry Smith .ve
949a6cb015SBarry Smith 
959a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
965a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
979a6cb015SBarry Smith    or at runtime via the option
989a6cb015SBarry Smith $     -snes_mf_type my_h
999a6cb015SBarry Smith 
1005a655dc6SBarry Smith .keywords: MatSNESMF, register
1019a6cb015SBarry Smith 
1025a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
1039a6cb015SBarry Smith M*/
1049a6cb015SBarry Smith 
1059a6cb015SBarry Smith #undef __FUNC__
1065a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegister_Private"
1075a655dc6SBarry Smith int MatSNESMFRegister_Private(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
1089a6cb015SBarry Smith {
1099a6cb015SBarry Smith   int ierr;
1109a6cb015SBarry Smith   char fullname[256];
1119a6cb015SBarry Smith 
1129a6cb015SBarry Smith   PetscFunctionBegin;
113250ffb4eSSatish Balay   ierr = FListConcat_Private(path,name,fullname); CHKERRQ(ierr);
1145a655dc6SBarry Smith   ierr = FListAdd_Private(&MatSNESMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
1159a6cb015SBarry Smith   PetscFunctionReturn(0);
1169a6cb015SBarry Smith }
1179a6cb015SBarry Smith 
1189a6cb015SBarry Smith 
1199a6cb015SBarry Smith #undef __FUNC__
1205a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegisterDestroy"
1219a6cb015SBarry Smith /*@C
1225a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
1235a655dc6SBarry Smith    registered by MatSNESMFRegister().
1249a6cb015SBarry Smith 
1259a6cb015SBarry Smith    Not Collective
1269a6cb015SBarry Smith 
12715091d37SBarry Smith    Level: developer
12815091d37SBarry Smith 
1295a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1309a6cb015SBarry Smith 
1315a655dc6SBarry Smith .seealso: MatSNESMFRegister(), MatSNESMFRegisterAll()
1329a6cb015SBarry Smith @*/
1335a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1349a6cb015SBarry Smith {
1359a6cb015SBarry Smith   int ierr;
1369a6cb015SBarry Smith 
1379a6cb015SBarry Smith   PetscFunctionBegin;
1385a655dc6SBarry Smith   if (MatSNESMFList) {
1395a655dc6SBarry Smith     ierr = FListDestroy( MatSNESMFList );CHKERRQ(ierr);
1405a655dc6SBarry Smith     MatSNESMFList = 0;
1419a6cb015SBarry Smith   }
1425a655dc6SBarry Smith   MatSNESMFRegisterAllCalled = 0;
1439a6cb015SBarry Smith   PetscFunctionReturn(0);
1449a6cb015SBarry Smith }
1459a6cb015SBarry Smith 
1469a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
147a4d4d686SBarry Smith #undef __FUNC__
1485a655dc6SBarry Smith #define __FUNC__ "MatSNESMFDestroy_Private"
1495a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat)
150a4d4d686SBarry Smith {
151a4d4d686SBarry Smith   int          ierr;
1525a655dc6SBarry Smith   MatSNESMFCtx ctx;
153fae171e0SBarry Smith 
1543a40ed3dSBarry Smith   PetscFunctionBegin;
1550a25c783SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
156b9fa9cd0SBarry Smith   ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
1579a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
158b4fd4287SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
159606d414cSSatish Balay   ierr = PetscFree(ctx->ops);CHKERRQ(ierr);
160606d414cSSatish Balay   ierr = PetscFree(ctx);CHKERRQ(ierr);
1613a40ed3dSBarry Smith   PetscFunctionReturn(0);
162b9fa9cd0SBarry Smith }
16350361f65SLois Curfman McInnes 
1645615d1e5SSatish Balay #undef __FUNC__
1655a655dc6SBarry Smith #define __FUNC__ "MatSNESMFView_Private"
16639e2f89bSBarry Smith /*
1675a655dc6SBarry Smith    MatSNESMFView_Private - Views matrix-free parameters.
1688f6e3e37SBarry Smith 
16939e2f89bSBarry Smith */
1705a655dc6SBarry Smith int MatSNESMFView_Private(Mat J,Viewer viewer)
171eb9086c3SLois Curfman McInnes {
172eb9086c3SLois Curfman McInnes   int          ierr;
1735a655dc6SBarry Smith   MatSNESMFCtx ctx;
174eb9086c3SLois Curfman McInnes   MPI_Comm     comm;
175eb9086c3SLois Curfman McInnes   FILE         *fd;
176*0f5bd95cSBarry Smith   int          isascii;
177eb9086c3SLois Curfman McInnes 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
1792d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
180eb9086c3SLois Curfman McInnes   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
181eb9086c3SLois Curfman McInnes   ierr = ViewerASCIIGetPointer(viewer,&fd);CHKERRQ(ierr);
182*0f5bd95cSBarry Smith   isascii = PetscTypeCompare(viewer,ASCII_VIEWER);
183*0f5bd95cSBarry Smith   if (isascii) {
184d132466eSBarry Smith      ierr = PetscFPrintf(comm,fd,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
185d132466eSBarry Smith      ierr = PetscFPrintf(comm,fd,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
186d132466eSBarry Smith      ierr = PetscFPrintf(ctx->comm,fd,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
1879a6cb015SBarry Smith      if (ctx->ops->view) {
1889a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1899a6cb015SBarry Smith      }
1905cd90555SBarry Smith   } else {
191*0f5bd95cSBarry Smith     SETERRQ1(1,1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
192eb9086c3SLois Curfman McInnes   }
1933a40ed3dSBarry Smith   PetscFunctionReturn(0);
194eb9086c3SLois Curfman McInnes }
195eb9086c3SLois Curfman McInnes 
196be726c96SBarry Smith #undef __FUNC__
1975a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAssemblyEnd_Private"
198be726c96SBarry Smith /*
1995a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
20065f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
201be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
20265f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
20365f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
204be726c96SBarry Smith */
2055a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J)
206be726c96SBarry Smith {
207be726c96SBarry Smith   int            ierr;
208be726c96SBarry Smith 
209be726c96SBarry Smith   PetscFunctionBegin;
2105a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
211be726c96SBarry Smith   PetscFunctionReturn(0);
212be726c96SBarry Smith }
213be726c96SBarry Smith 
214c481317fSBarry Smith 
2155615d1e5SSatish Balay #undef __FUNC__
2165a655dc6SBarry Smith #define __FUNC__ "MatSNESMFMult_Private"
217eb9086c3SLois Curfman McInnes /*
2185a655dc6SBarry Smith   MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector
219eb9086c3SLois Curfman McInnes   product, y = F'(u)*a:
220a4d4d686SBarry Smith 
2219a6cb015SBarry Smith         y ~= ( F(u + ha) - F(u) )/h,
222eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
223eb9086c3SLois Curfman McInnes         u = current iterate
224eb9086c3SLois Curfman McInnes         h = difference interval
225eb9086c3SLois Curfman McInnes */
2265a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y)
22739e2f89bSBarry Smith {
2285a655dc6SBarry Smith   MatSNESMFCtx ctx;
229fae171e0SBarry Smith   SNES           snes;
230a4d4d686SBarry Smith   Scalar         h, mone = -1.0;
231fae171e0SBarry Smith   Vec            w,U,F;
232a305c92eSSatish Balay   int            ierr, (*eval_fct)(SNES,Vec,Vec)=0;
23339e2f89bSBarry Smith 
2343a40ed3dSBarry Smith   PetscFunctionBegin;
2359a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2369a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2379a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2389a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
23956cd22aeSBarry Smith   PLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);
24056cd22aeSBarry Smith 
2416e38a044SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
242fae171e0SBarry Smith   snes = ctx->snes;
243fae171e0SBarry Smith   w    = ctx->w;
24478b31e54SBarry Smith   ierr = SNESGetSolution(snes,&U);CHKERRQ(ierr);
24550361f65SLois Curfman McInnes 
24685614651SBarry Smith   /*
24785614651SBarry Smith       Compute differencing parameter
24885614651SBarry Smith   */
2499a6cb015SBarry Smith   if (!ctx->ops->compute) {
2505a655dc6SBarry Smith     ierr = MatSNESMFSetType(mat,"default");CHKERRQ(ierr);
2515a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2529a6cb015SBarry Smith   }
2539a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
254a4d4d686SBarry Smith 
255a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
256a4d4d686SBarry Smith   ctx->currenth = h;
257aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
258a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g + %g i\n",PetscReal(h),PetscImaginary(h));
259a4d4d686SBarry Smith #else
260a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g\n",h);
261a4d4d686SBarry Smith #endif
262a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
26385614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
264a4d4d686SBarry Smith   }
26585614651SBarry Smith   ctx->ncurrenth++;
266a4d4d686SBarry Smith 
26785614651SBarry Smith   /* w = u + ha */
268a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
26985614651SBarry Smith 
27085614651SBarry Smith 
27185614651SBarry Smith   if (!ctx->func) {
27285614651SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
27385614651SBarry Smith       eval_fct = SNESComputeFunction;
274184914b5SBarry Smith       ierr     = SNESGetFunction(snes,&F,PETSC_NULL);CHKERRQ(ierr);
27585614651SBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
27685614651SBarry Smith       eval_fct = SNESComputeGradient;
277184914b5SBarry Smith       ierr     = SNESGetGradient(snes,&F,PETSC_NULL);CHKERRQ(ierr);
27885614651SBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class");
279a4d4d686SBarry Smith     ierr = eval_fct(snes,w,y);CHKERRQ(ierr);
28085614651SBarry Smith   } else {
28185614651SBarry Smith     F = ctx->funcvec;
28285614651SBarry Smith     /* compute func(U) as base for differencing */
28385614651SBarry Smith     if (ctx->ncurrenth == 1) {
28485614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
28585614651SBarry Smith     }
28685614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
28785614651SBarry Smith   }
288a4d4d686SBarry Smith 
289a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
290a4d4d686SBarry Smith   h    = 1.0/h;
291a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
292a4d4d686SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceRemove(ctx->sp,y);CHKERRQ(ierr);}
293a4d4d686SBarry Smith 
294a4d4d686SBarry Smith   PLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);
295a4d4d686SBarry Smith   PetscFunctionReturn(0);
296a4d4d686SBarry Smith }
297a4d4d686SBarry Smith 
298a4d4d686SBarry Smith #undef __FUNC__
2995a655dc6SBarry Smith #define __FUNC__ "MatCreateSNESMF"
300a4d4d686SBarry Smith /*@C
30165f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
30265f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
30365f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
304a4d4d686SBarry Smith 
305a4d4d686SBarry Smith    Collective on SNES and Vec
306a4d4d686SBarry Smith 
307a4d4d686SBarry Smith    Input Parameters:
308a4d4d686SBarry Smith +  snes - the SNES context
309a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
310a4d4d686SBarry Smith 
311a4d4d686SBarry Smith    Output Parameter:
312a4d4d686SBarry Smith .  J - the matrix-free matrix
313a4d4d686SBarry Smith 
31415091d37SBarry Smith    Level: advanced
31515091d37SBarry Smith 
316a4d4d686SBarry Smith    Notes:
317a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
318a4d4d686SBarry Smith    and work space for performing finite difference approximations of
31965f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
3209a6cb015SBarry Smith 
3219a6cb015SBarry Smith    The default code uses the following approach to compute h
322a4d4d686SBarry Smith 
323a4d4d686SBarry Smith .vb
32465f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
325a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
326a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
327a4d4d686SBarry Smith  where
328a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
329a4d4d686SBarry Smith      umin = minimum iterate parameter
330a4d4d686SBarry Smith .ve
331a4d4d686SBarry Smith 
3325a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
33365f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
33465f2ba5bSLois Curfman McInnes    of the users manual for details.
335a4d4d686SBarry Smith 
336a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
337a4d4d686SBarry Smith    matrix context.
338a4d4d686SBarry Smith 
339a4d4d686SBarry Smith    Options Database Keys:
340a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
3419a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
342a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
343a4d4d686SBarry Smith 
344a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
345a4d4d686SBarry Smith 
3465a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
3475a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
3485a655dc6SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegister()
349a4d4d686SBarry Smith 
350a4d4d686SBarry Smith @*/
3515a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x, Mat *J)
352a4d4d686SBarry Smith {
353a4d4d686SBarry Smith   MPI_Comm     comm;
3545a655dc6SBarry Smith   MatSNESMFCtx mfctx;
3559a6cb015SBarry Smith   int          n, nloc, ierr;
356a4d4d686SBarry Smith 
357a4d4d686SBarry Smith   PetscFunctionBegin;
3585a655dc6SBarry Smith   mfctx = (MatSNESMFCtx) PetscMalloc(sizeof(struct _p_MatSNESMFCtx));CHKPTRQ(mfctx);
3595a655dc6SBarry Smith   PLogObjectMemory(snes,sizeof(MatSNESMFCtx));
3609a6cb015SBarry Smith   mfctx->comm         = snes->comm;
361a4d4d686SBarry Smith   mfctx->sp           = 0;
362a4d4d686SBarry Smith   mfctx->snes         = snes;
363a4d4d686SBarry Smith   mfctx->error_rel    = 1.e-8; /* assumes double precision */
364a4d4d686SBarry Smith   mfctx->currenth     = 0.0;
365a4d4d686SBarry Smith   mfctx->historyh     = PETSC_NULL;
366a4d4d686SBarry Smith   mfctx->ncurrenth    = 0;
367a4d4d686SBarry Smith   mfctx->maxcurrenth  = 0;
368fc3d5af4SSatish Balay   ierr = PetscMemzero(mfctx->type_name,256*sizeof(char));CHKERRQ(ierr);
369a4d4d686SBarry Smith 
3709a6cb015SBarry Smith   /*
3719a6cb015SBarry Smith      Create the empty data structure to contain compute-h routines.
3729a6cb015SBarry Smith      These will be filled in below from the command line options or
3735a655dc6SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
3745a655dc6SBarry Smith      then it will default in the first use of MatSNESMFMult_private()
3759a6cb015SBarry Smith   */
3769a6cb015SBarry Smith   mfctx->ops                 = (MFOps *)PetscMalloc(sizeof(MFOps));CHKPTRQ(mfctx->ops);
3779a6cb015SBarry Smith   mfctx->ops->compute        = 0;
3789a6cb015SBarry Smith   mfctx->ops->destroy        = 0;
3799a6cb015SBarry Smith   mfctx->ops->view           = 0;
3809a6cb015SBarry Smith   mfctx->ops->printhelp      = 0;
3819a6cb015SBarry Smith   mfctx->ops->setfromoptions = 0;
3829a6cb015SBarry Smith   mfctx->hctx                = 0;
3839a6cb015SBarry Smith 
38485614651SBarry Smith   mfctx->func                = 0;
38585614651SBarry Smith   mfctx->funcctx             = 0;
38685614651SBarry Smith   mfctx->funcvec             = 0;
38785614651SBarry Smith 
388a4d4d686SBarry Smith   ierr = VecDuplicate(x,&mfctx->w);CHKERRQ(ierr);
389a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
390a4d4d686SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
391a4d4d686SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
392ffa0fd9eSBarry Smith   ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J);CHKERRQ(ierr);
3935a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr);
3945a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr);
3955a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private);CHKERRQ(ierr);
3965a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr);
397a4d4d686SBarry Smith   PLogObjectParent(*J,mfctx->w);
398a4d4d686SBarry Smith   PLogObjectParent(snes,*J);
3999a6cb015SBarry Smith 
4009a6cb015SBarry Smith   mfctx->mat = *J;
4019a6cb015SBarry Smith 
4029a6cb015SBarry Smith 
4039a6cb015SBarry Smith   PetscFunctionReturn(0);
4049a6cb015SBarry Smith }
4059a6cb015SBarry Smith 
4069a6cb015SBarry Smith #undef __FUNC__
4075a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFromOptions"
4089a6cb015SBarry Smith /*@
4095a655dc6SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4109a6cb015SBarry Smith    parameter.
4119a6cb015SBarry Smith 
4129a6cb015SBarry Smith    Collective on Mat
4139a6cb015SBarry Smith 
4149a6cb015SBarry Smith    Input Parameters:
4155a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4165a655dc6SBarry Smith 
4175a655dc6SBarry Smith    Options Database Keys:
4185a655dc6SBarry Smith +  -snes_mf_type - <default,wp>
4195a655dc6SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
4209a6cb015SBarry Smith 
42115091d37SBarry Smith    Level: advanced
42215091d37SBarry Smith 
4239a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters
4249a6cb015SBarry Smith 
4255a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4265a655dc6SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
4279a6cb015SBarry Smith @*/
4285a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
4299a6cb015SBarry Smith {
4305a655dc6SBarry Smith   MatSNESMFCtx mfctx;
4319a6cb015SBarry Smith   int          ierr,flg;
4329a6cb015SBarry Smith   char         ftype[256],p[64];
4339a6cb015SBarry Smith 
4349a6cb015SBarry Smith   PetscFunctionBegin;
4359a6cb015SBarry Smith   ierr = MatShellGetContext(mat,(void **)&mfctx);CHKERRQ(ierr);
4369a6cb015SBarry Smith   if (mfctx) {
4379a6cb015SBarry Smith     /* allow user to set the type */
4389a6cb015SBarry Smith     ierr = OptionsGetString(mfctx->snes->prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr);
4399a6cb015SBarry Smith     if (flg) {
4405a655dc6SBarry Smith       ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
4419a6cb015SBarry Smith     }
4429a6cb015SBarry Smith 
4439a6cb015SBarry Smith     ierr = OptionsGetDouble(mfctx->snes->prefix,"-snes_mf_err",&mfctx->error_rel,&flg);CHKERRQ(ierr);
4449a6cb015SBarry Smith     if (mfctx->ops->setfromoptions) {
4459a6cb015SBarry Smith       ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
4469a6cb015SBarry Smith     }
4479a6cb015SBarry Smith 
4489a6cb015SBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
449d132466eSBarry Smith     ierr = PetscStrcpy(p,"-");CHKERRQ(ierr);
450d132466eSBarry Smith     if (mfctx->snes->prefix) {ierr = PetscStrcat(p,mfctx->snes->prefix);CHKERRQ(ierr);}
4519a6cb015SBarry Smith     if (flg) {
452d132466eSBarry 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);
4539a6cb015SBarry Smith       if (mfctx->ops->printhelp) {
454d132466eSBarry Smith         ierr = (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr);
4559a6cb015SBarry Smith       }
4569a6cb015SBarry Smith     }
4579a6cb015SBarry Smith   }
458a4d4d686SBarry Smith   PetscFunctionReturn(0);
459a4d4d686SBarry Smith }
460a4d4d686SBarry Smith 
461a4d4d686SBarry Smith #undef __FUNC__
4625a655dc6SBarry Smith #define __FUNC__ "MatSNESMFGetH"
463a4d4d686SBarry Smith /*@
46465f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
465a4d4d686SBarry Smith    parameter.
466a4d4d686SBarry Smith 
467a4d4d686SBarry Smith    Not Collective
468a4d4d686SBarry Smith 
469a4d4d686SBarry Smith    Input Parameters:
4705a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
471a4d4d686SBarry Smith 
472a4d4d686SBarry Smith    Output Paramter:
473a4d4d686SBarry Smith .  h - the differencing step size
474a4d4d686SBarry Smith 
47515091d37SBarry Smith    Level: advanced
47615091d37SBarry Smith 
477a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
478a4d4d686SBarry Smith 
4795a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4805a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
481a4d4d686SBarry Smith @*/
4825a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h)
483a4d4d686SBarry Smith {
4845a655dc6SBarry Smith   MatSNESMFCtx ctx;
485a4d4d686SBarry Smith   int          ierr;
486a4d4d686SBarry Smith 
487a4d4d686SBarry Smith   PetscFunctionBegin;
488a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
489a4d4d686SBarry Smith   if (ctx) {
490a4d4d686SBarry Smith     *h = ctx->currenth;
491a4d4d686SBarry Smith   }
492a4d4d686SBarry Smith   PetscFunctionReturn(0);
493a4d4d686SBarry Smith }
494a4d4d686SBarry Smith 
495a4d4d686SBarry Smith #undef __FUNC__
4965a655dc6SBarry Smith #define __FUNC__ "MatSNESMFKSPMonitor"
497a4d4d686SBarry Smith /*
4985a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
49965f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
50065f2ba5bSLois Curfman McInnes    each step.
501a4d4d686SBarry Smith */
5025a655dc6SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,double rnorm,void *dummy)
503a4d4d686SBarry Smith {
504a4d4d686SBarry Smith   PC             pc;
5055a655dc6SBarry Smith   MatSNESMFCtx   ctx;
506a4d4d686SBarry Smith   int            ierr;
507a4d4d686SBarry Smith   Mat            mat;
508a4d4d686SBarry Smith   MPI_Comm       comm;
509a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
510a4d4d686SBarry Smith 
511a4d4d686SBarry Smith   PetscFunctionBegin;
512a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
513a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
514a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
515a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
516a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
5179a6cb015SBarry Smith   if (!ctx) {
5189a6cb015SBarry Smith     SETERRQ(1,1,"Matrix is not a matrix free shell matrix");
5199a6cb015SBarry Smith   }
520a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
521aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
522d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
523d132466eSBarry Smith                 PetscReal(ctx->currenth),PetscImaginary(ctx->currenth));CHKERRQ(ierr);
524a4d4d686SBarry Smith #else
525d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
526a4d4d686SBarry Smith #endif
527a4d4d686SBarry Smith   } else {
528d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
529a4d4d686SBarry Smith   }
530a4d4d686SBarry Smith   PetscFunctionReturn(0);
531a4d4d686SBarry Smith }
532a4d4d686SBarry Smith 
533a4d4d686SBarry Smith #undef __FUNC__
53485614651SBarry Smith #define __FUNC__ "MatSNESMFSetFunction"
53585614651SBarry Smith /*@C
53685614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
53785614651SBarry Smith 
53885614651SBarry Smith    Collective on Mat
53985614651SBarry Smith 
54085614651SBarry Smith    Input Parameters:
54185614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
54285614651SBarry Smith .  v   - workspace vector
54385614651SBarry Smith .  func - the function to use
54485614651SBarry Smith -  funcctx - optional function context passed to function
54585614651SBarry Smith 
54685614651SBarry Smith    Level: advanced
54785614651SBarry Smith 
54885614651SBarry Smith    Notes:
54985614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
55085614651SBarry Smith     matrix inside your compute Jacobian routine
55185614651SBarry Smith 
55285614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
55385614651SBarry Smith 
55485614651SBarry Smith .keywords: SNES, matrix-free, function
55585614651SBarry Smith 
55685614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
55785614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
55885614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
55985614651SBarry Smith @*/
56085614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
56185614651SBarry Smith {
56285614651SBarry Smith   MatSNESMFCtx ctx;
56385614651SBarry Smith   int          ierr;
56485614651SBarry Smith 
56585614651SBarry Smith   PetscFunctionBegin;
56685614651SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
56785614651SBarry Smith   if (ctx) {
56885614651SBarry Smith     ctx->func    = func;
56985614651SBarry Smith     ctx->funcctx = funcctx;
57085614651SBarry Smith     ctx->funcvec = v;
57185614651SBarry Smith   }
57285614651SBarry Smith   PetscFunctionReturn(0);
57385614651SBarry Smith }
57485614651SBarry Smith 
57585614651SBarry Smith 
57685614651SBarry Smith #undef __FUNC__
5775a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFunctionError"
578a4d4d686SBarry Smith /*@
5795a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
580a4d4d686SBarry Smith    matrix-vector products using finite differences.
581a4d4d686SBarry Smith 
582a4d4d686SBarry Smith    Collective on Mat
583a4d4d686SBarry Smith 
584a4d4d686SBarry Smith    Input Parameters:
5855a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
5869a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
587a4d4d686SBarry Smith                the relative error in the function evaluations)
588a4d4d686SBarry Smith 
58915091d37SBarry Smith    Options Database Keys:
59015091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
59115091d37SBarry Smith 
59215091d37SBarry Smith    Level: advanced
59315091d37SBarry Smith 
594a4d4d686SBarry Smith    Notes:
595a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
596a4d4d686SBarry Smith .vb
59765f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
598a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
599a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
600a4d4d686SBarry Smith .ve
601a4d4d686SBarry Smith 
602a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
603a4d4d686SBarry Smith 
6045a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
6055a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
6065a655dc6SBarry Smith           MatSNESMFKSPMonitor()
607a4d4d686SBarry Smith @*/
6085a655dc6SBarry Smith int MatSNESMFSetFunctionError(Mat mat,double error)
609a4d4d686SBarry Smith {
6105a655dc6SBarry Smith   MatSNESMFCtx ctx;
611a4d4d686SBarry Smith   int          ierr;
612a4d4d686SBarry Smith 
613a4d4d686SBarry Smith   PetscFunctionBegin;
614a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
615a4d4d686SBarry Smith   if (ctx) {
616a4d4d686SBarry Smith     if (error != PETSC_DEFAULT) ctx->error_rel = error;
617a4d4d686SBarry Smith   }
618a4d4d686SBarry Smith   PetscFunctionReturn(0);
619a4d4d686SBarry Smith }
620a4d4d686SBarry Smith 
621a4d4d686SBarry Smith #undef __FUNC__
6225a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAddNullSpace"
623a4d4d686SBarry Smith /*@
62465f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
62565f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
62665f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
62765f2ba5bSLois Curfman McInnes    automatically removed.
628a4d4d686SBarry Smith 
629a4d4d686SBarry Smith    Collective on Mat
630a4d4d686SBarry Smith 
631a4d4d686SBarry Smith    Input Parameters:
632a4d4d686SBarry Smith +  J - the matrix-free matrix context
63385614651SBarry Smith -  nullsp - object created with PCNullSpaceCreate()
634a4d4d686SBarry Smith 
63515091d37SBarry Smith    Level: advanced
63615091d37SBarry Smith 
637a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
638a4d4d686SBarry Smith 
63953b79920SLois Curfman McInnes .seealso: PCNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
6405a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
6415a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
642a4d4d686SBarry Smith @*/
64385614651SBarry Smith int MatSNESMFAddNullSpace(Mat J,PCNullSpace nullsp)
644a4d4d686SBarry Smith {
645a4d4d686SBarry Smith   int          ierr;
6465a655dc6SBarry Smith   MatSNESMFCtx ctx;
647a4d4d686SBarry Smith   MPI_Comm     comm;
648a4d4d686SBarry Smith 
649a4d4d686SBarry Smith   PetscFunctionBegin;
6502d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
651a4d4d686SBarry Smith 
652a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
653a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
654a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
65585614651SBarry Smith   ctx->sp = nullsp;
65685614651SBarry Smith   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
657a4d4d686SBarry Smith   PetscFunctionReturn(0);
658a4d4d686SBarry Smith }
659a4d4d686SBarry Smith 
660a4d4d686SBarry Smith #undef __FUNC__
6615a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetHHistory"
662a4d4d686SBarry Smith /*@
66365f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
66465f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
665a4d4d686SBarry Smith 
666a4d4d686SBarry Smith    Collective on Mat
667a4d4d686SBarry Smith 
668a4d4d686SBarry Smith    Input Parameters:
669a4d4d686SBarry Smith +  J - the matrix-free matrix context
67065f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
67165f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
67265f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
673a4d4d686SBarry Smith 
67415091d37SBarry Smith    Level: advanced
67515091d37SBarry Smith 
676a4d4d686SBarry Smith    Notes:
67765f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
67865f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
679a4d4d686SBarry Smith 
680a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
681a4d4d686SBarry Smith 
6825a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
6835a655dc6SBarry Smith           MatSNESMFResetHHistory(),
6845a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
685a4d4d686SBarry Smith 
686a4d4d686SBarry Smith @*/
6875a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory)
688a4d4d686SBarry Smith {
689a4d4d686SBarry Smith   int          ierr;
6905a655dc6SBarry Smith   MatSNESMFCtx ctx;
691a4d4d686SBarry Smith 
692a4d4d686SBarry Smith   PetscFunctionBegin;
693a4d4d686SBarry Smith 
694a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
695a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
696a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
697a4d4d686SBarry Smith   ctx->historyh    = history;
698a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
699a4d4d686SBarry Smith   ctx->currenth    = 0;
700a4d4d686SBarry Smith 
701a4d4d686SBarry Smith   PetscFunctionReturn(0);
702a4d4d686SBarry Smith }
703a4d4d686SBarry Smith 
704a4d4d686SBarry Smith #undef __FUNC__
7055a655dc6SBarry Smith #define __FUNC__ "MatSNESMFResetHHistory"
706a4d4d686SBarry Smith /*@
7075a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
708a4d4d686SBarry Smith    collecting a new set of differencing histories.
709a4d4d686SBarry Smith 
710a4d4d686SBarry Smith    Collective on Mat
711a4d4d686SBarry Smith 
712a4d4d686SBarry Smith    Input Parameters:
713a4d4d686SBarry Smith .  J - the matrix-free matrix context
714a4d4d686SBarry Smith 
71515091d37SBarry Smith    Level: advanced
71615091d37SBarry Smith 
717a4d4d686SBarry Smith    Notes:
71865f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
719a4d4d686SBarry Smith 
720a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
721a4d4d686SBarry Smith 
7225a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
7235a655dc6SBarry Smith           MatSNESMFSetHHistory(),
7245a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
725a4d4d686SBarry Smith 
726a4d4d686SBarry Smith @*/
7275a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
728a4d4d686SBarry Smith {
729a4d4d686SBarry Smith   int          ierr;
7305a655dc6SBarry Smith   MatSNESMFCtx ctx;
731a4d4d686SBarry Smith 
732a4d4d686SBarry Smith   PetscFunctionBegin;
733a4d4d686SBarry Smith 
734a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
735a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
736a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
737be726c96SBarry Smith   ctx->ncurrenth    = 0;
738a4d4d686SBarry Smith 
739a4d4d686SBarry Smith   PetscFunctionReturn(0);
740a4d4d686SBarry Smith }
741a4d4d686SBarry Smith 
742