xref: /petsc/src/snes/mf/snesmfj.c (revision 4a2ae208534dc2960f861bf8a1e27d424854347f)
1*4a2ae208SSatish Balay /*$Id: snesmfj.c,v 1.116 2001/03/09 20:27:38 bsmith Exp balay $*/
281e6777dSBarry Smith 
39a6cb015SBarry Smith #include "src/snes/snesimpl.h"
4e090d566SSatish Balay #include "src/snes/mf/snesmfj.h"   /*I  "petscsnes.h"   I*/
581e6777dSBarry Smith 
6b0a32e0cSBarry Smith PetscFList      MatSNESMPetscFList              = 0;
74c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE;
8a4d4d686SBarry Smith 
9*4a2ae208SSatish Balay #undef __FUNCT__
10*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetType"
11fd4bdd07SBarry Smith /*@C
1265f2ba5bSLois Curfman McInnes     MatSNESMFSetType - Sets the method that is used to compute the
13b0a32e0cSBarry Smith     differencing parameter for finite differene matrix-free formulations.
149a6cb015SBarry Smith 
159a6cb015SBarry Smith     Input Parameters:
16ef4ad1fdSLois Curfman McInnes +   mat - the "matrix-free" matrix created via MatCreateSNESMF()
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 @*/
31f6a0df18SBarry Smith int MatSNESMFSetType(Mat mat,MatSNESMFType ftype)
32b9fa9cd0SBarry Smith {
335a655dc6SBarry Smith   int          ierr,(*r)(MatSNESMFCtx);
345a655dc6SBarry Smith   MatSNESMFCtx ctx;
356831982aSBarry Smith   PetscTruth   match;
36a4d4d686SBarry Smith 
37a4d4d686SBarry Smith   PetscFunctionBegin;
380f5bd95cSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
390f5bd95cSBarry Smith   PetscValidCharPointer(ftype);
400f5bd95cSBarry Smith 
41a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
42a4d4d686SBarry Smith 
439a6cb015SBarry Smith   /* already set, so just return */
446831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr);
450f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
46a4d4d686SBarry Smith 
479a6cb015SBarry Smith   /* destroy the old one if it exists */
489a6cb015SBarry Smith   if (ctx->ops->destroy) {
499a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
509a6cb015SBarry Smith   }
519a6cb015SBarry Smith 
5265f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
535a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
549a6cb015SBarry Smith 
55b0a32e0cSBarry Smith   ierr =  PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(int (**)(void *)) &r);CHKERRQ(ierr);
569a6cb015SBarry Smith 
5729bbc08cSBarry Smith   if (!r) SETERRQ(1,"Unknown MatSNESMF type given");
589a6cb015SBarry Smith 
599a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
606831982aSBarry Smith 
616831982aSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
629a6cb015SBarry Smith 
639a6cb015SBarry Smith   PetscFunctionReturn(0);
649a6cb015SBarry Smith }
659a6cb015SBarry Smith 
669a6cb015SBarry Smith /*MC
67f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry.
689a6cb015SBarry Smith 
699a6cb015SBarry Smith    Synopsis:
70f1af5d2fSBarry Smith    MatSNESMFRegisterDynamicchar *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
719a6cb015SBarry Smith 
729a6cb015SBarry Smith    Not Collective
739a6cb015SBarry Smith 
749a6cb015SBarry Smith    Input Parameters:
759a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
769a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
779a6cb015SBarry Smith .  name_create - name of routine to create method context
789a6cb015SBarry Smith -  routine_create - routine to create method context
799a6cb015SBarry Smith 
8015091d37SBarry Smith    Level: developer
8115091d37SBarry Smith 
829a6cb015SBarry Smith    Notes:
83f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers.
849a6cb015SBarry Smith 
859a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
869a6cb015SBarry Smith    is ignored.
879a6cb015SBarry Smith 
889a6cb015SBarry Smith    Sample usage:
899a6cb015SBarry Smith .vb
90f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
919a6cb015SBarry Smith                "MyHCreate",MyHCreate);
929a6cb015SBarry Smith .ve
939a6cb015SBarry Smith 
949a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
955a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
969a6cb015SBarry Smith    or at runtime via the option
979a6cb015SBarry Smith $     -snes_mf_type my_h
989a6cb015SBarry Smith 
995a655dc6SBarry Smith .keywords: MatSNESMF, register
1009a6cb015SBarry Smith 
1015a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
1029a6cb015SBarry Smith M*/
1039a6cb015SBarry Smith 
104*4a2ae208SSatish Balay #undef __FUNCT__
105*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
106f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
1079a6cb015SBarry Smith {
1089a6cb015SBarry Smith   int ierr;
1099a6cb015SBarry Smith   char fullname[256];
1109a6cb015SBarry Smith 
1119a6cb015SBarry Smith   PetscFunctionBegin;
112b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
113b0a32e0cSBarry Smith   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
1149a6cb015SBarry Smith   PetscFunctionReturn(0);
1159a6cb015SBarry Smith }
1169a6cb015SBarry Smith 
1179a6cb015SBarry Smith 
118*4a2ae208SSatish Balay #undef __FUNCT__
119*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1209a6cb015SBarry Smith /*@C
1215a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
122f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1239a6cb015SBarry Smith 
1249a6cb015SBarry Smith    Not Collective
1259a6cb015SBarry Smith 
12615091d37SBarry Smith    Level: developer
12715091d37SBarry Smith 
1285a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1299a6cb015SBarry Smith 
130f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1319a6cb015SBarry Smith @*/
1325a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1339a6cb015SBarry Smith {
1349a6cb015SBarry Smith   int ierr;
1359a6cb015SBarry Smith 
1369a6cb015SBarry Smith   PetscFunctionBegin;
137b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
138b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
139b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1409a6cb015SBarry Smith   }
1414c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1429a6cb015SBarry Smith   PetscFunctionReturn(0);
1439a6cb015SBarry Smith }
1449a6cb015SBarry Smith 
1459a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
146*4a2ae208SSatish Balay #undef __FUNCT__
147*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFDestroy_Private"
1485a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat)
149a4d4d686SBarry Smith {
150a4d4d686SBarry Smith   int          ierr;
1515a655dc6SBarry Smith   MatSNESMFCtx ctx;
152fae171e0SBarry Smith 
1533a40ed3dSBarry Smith   PetscFunctionBegin;
1540a25c783SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
155b9fa9cd0SBarry Smith   ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
1569a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
15774637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
1586831982aSBarry Smith   PetscHeaderDestroy(ctx);
1593a40ed3dSBarry Smith   PetscFunctionReturn(0);
160b9fa9cd0SBarry Smith }
16150361f65SLois Curfman McInnes 
162*4a2ae208SSatish Balay #undef __FUNCT__
163*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFView_Private"
16439e2f89bSBarry Smith /*
1655a655dc6SBarry Smith    MatSNESMFView_Private - Views matrix-free parameters.
1668f6e3e37SBarry Smith 
16739e2f89bSBarry Smith */
168b0a32e0cSBarry Smith int MatSNESMFView_Private(Mat J,PetscViewer viewer)
169eb9086c3SLois Curfman McInnes {
170eb9086c3SLois Curfman McInnes   int          ierr;
1715a655dc6SBarry Smith   MatSNESMFCtx ctx;
1726831982aSBarry Smith   PetscTruth   isascii;
173eb9086c3SLois Curfman McInnes 
1743a40ed3dSBarry Smith   PetscFunctionBegin;
175eb9086c3SLois Curfman McInnes   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
176b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
1770f5bd95cSBarry Smith   if (isascii) {
178b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
179b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
180473c83c3SBarry Smith      if (!ctx->type_name) {
181b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
182473c83c3SBarry Smith      } else {
183b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
184473c83c3SBarry Smith      }
1859a6cb015SBarry Smith      if (ctx->ops->view) {
1869a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1879a6cb015SBarry Smith      }
1885cd90555SBarry Smith   } else {
18929bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
190eb9086c3SLois Curfman McInnes   }
1913a40ed3dSBarry Smith   PetscFunctionReturn(0);
192eb9086c3SLois Curfman McInnes }
193eb9086c3SLois Curfman McInnes 
194*4a2ae208SSatish Balay #undef __FUNCT__
195*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAssemblyEnd_Private"
196be726c96SBarry Smith /*
1975a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
19865f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
199be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
20065f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
20165f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
202be726c96SBarry Smith */
2035a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J)
204be726c96SBarry Smith {
205be726c96SBarry Smith   int          ierr;
2061d1367b7SBarry Smith   MatSNESMFCtx j;
207be726c96SBarry Smith 
208be726c96SBarry Smith   PetscFunctionBegin;
2095a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
2101d1367b7SBarry Smith   ierr = MatShellGetContext(J,(void **)&j);CHKERRQ(ierr);
211b0a32e0cSBarry Smith   if (j->usesnes) {
2121d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
2131d1367b7SBarry Smith     if (j->snes->method_class == SNES_NONLINEAR_EQUATIONS) {
2141d1367b7SBarry Smith       ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2151d1367b7SBarry Smith     } else if (j->snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
2161d1367b7SBarry Smith       ierr = SNESGetGradient(j->snes,&j->current_f,PETSC_NULL);CHKERRQ(ierr);
21729bbc08cSBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class");
2181d1367b7SBarry Smith   }
219be726c96SBarry Smith   PetscFunctionReturn(0);
220be726c96SBarry Smith }
221be726c96SBarry Smith 
222c481317fSBarry Smith 
223*4a2ae208SSatish Balay #undef __FUNCT__
224*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFMult_Private"
225eb9086c3SLois Curfman McInnes /*
2265a655dc6SBarry Smith   MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector
227eb9086c3SLois Curfman McInnes   product, y = F'(u)*a:
228a4d4d686SBarry Smith 
2299a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
230eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
231eb9086c3SLois Curfman McInnes         u = current iterate
232eb9086c3SLois Curfman McInnes         h = difference interval
233eb9086c3SLois Curfman McInnes */
2345a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y)
23539e2f89bSBarry Smith {
2365a655dc6SBarry Smith   MatSNESMFCtx ctx;
237fae171e0SBarry Smith   SNES         snes;
238a4d4d686SBarry Smith   Scalar       h,mone = -1.0;
239fae171e0SBarry Smith   Vec          w,U,F;
240a305c92eSSatish Balay   int          ierr,(*eval_fct)(SNES,Vec,Vec)=0;
24139e2f89bSBarry Smith 
2423a40ed3dSBarry Smith   PetscFunctionBegin;
2439a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2449a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2459a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2469a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
247b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr);
24856cd22aeSBarry Smith 
2496e38a044SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
250fae171e0SBarry Smith   snes = ctx->snes;
251fae171e0SBarry Smith   w    = ctx->w;
2521d1367b7SBarry Smith   U    = ctx->current_u;
25350361f65SLois Curfman McInnes 
25485614651SBarry Smith   /*
25585614651SBarry Smith       Compute differencing parameter
25685614651SBarry Smith   */
2579a6cb015SBarry Smith   if (!ctx->ops->compute) {
258b7fd4e64SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr);
2595a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2609a6cb015SBarry Smith   }
2619a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
262a4d4d686SBarry Smith 
263a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
264a4d4d686SBarry Smith   ctx->currenth = h;
265aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
266b0a32e0cSBarry Smith   PetscLogInfo(mat,"MatSNESMFMult_Private:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h));
267a4d4d686SBarry Smith #else
268b0a32e0cSBarry Smith   PetscLogInfo(mat,"MatSNESMFMult_Private:Current differencing parameter: %15.12e\n",h);
269a4d4d686SBarry Smith #endif
270a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
27185614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
272a4d4d686SBarry Smith   }
27385614651SBarry Smith   ctx->ncurrenth++;
274a4d4d686SBarry Smith 
27585614651SBarry Smith   /* w = u + ha */
276a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
27785614651SBarry Smith 
278b0a32e0cSBarry Smith   if (ctx->usesnes) {
27985614651SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
28085614651SBarry Smith       eval_fct = SNESComputeFunction;
28185614651SBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
28285614651SBarry Smith       eval_fct = SNESComputeGradient;
28329bbc08cSBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class");
2841d1367b7SBarry Smith     F    = ctx->current_f;
28529bbc08cSBarry Smith     if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices");
28639903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
28785614651SBarry Smith   } else {
28885614651SBarry Smith     F = ctx->funcvec;
28985614651SBarry Smith     /* compute func(U) as base for differencing */
29085614651SBarry Smith     if (ctx->ncurrenth == 1) {
29185614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
29285614651SBarry Smith     }
29385614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
29485614651SBarry Smith   }
295a4d4d686SBarry Smith 
296a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
297a4d4d686SBarry Smith   h    = 1.0/h;
298a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
29974637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
300a4d4d686SBarry Smith 
301b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr);
302a4d4d686SBarry Smith   PetscFunctionReturn(0);
303a4d4d686SBarry Smith }
304a4d4d686SBarry Smith 
305*4a2ae208SSatish Balay #undef __FUNCT__
306*4a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
307a4d4d686SBarry Smith /*@C
30865f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
30965f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
31065f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
311a4d4d686SBarry Smith 
312a4d4d686SBarry Smith    Collective on SNES and Vec
313a4d4d686SBarry Smith 
314a4d4d686SBarry Smith    Input Parameters:
315a4d4d686SBarry Smith +  snes - the SNES context
316a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
317a4d4d686SBarry Smith 
318a4d4d686SBarry Smith    Output Parameter:
319a4d4d686SBarry Smith .  J - the matrix-free matrix
320a4d4d686SBarry Smith 
32115091d37SBarry Smith    Level: advanced
32215091d37SBarry Smith 
323a4d4d686SBarry Smith    Notes:
324a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
325a4d4d686SBarry Smith    and work space for performing finite difference approximations of
32665f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
3279a6cb015SBarry Smith 
3289a6cb015SBarry Smith    The default code uses the following approach to compute h
329a4d4d686SBarry Smith 
330a4d4d686SBarry Smith .vb
33165f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
332a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
333a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
334a4d4d686SBarry Smith  where
335a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
336a4d4d686SBarry Smith      umin = minimum iterate parameter
337a4d4d686SBarry Smith .ve
338a4d4d686SBarry Smith 
3395a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
34065f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
34165f2ba5bSLois Curfman McInnes    of the users manual for details.
342a4d4d686SBarry Smith 
343a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
344a4d4d686SBarry Smith    matrix context.
345a4d4d686SBarry Smith 
346a4d4d686SBarry Smith    Options Database Keys:
347a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
3489a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
349a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
350a4d4d686SBarry Smith 
351a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
352a4d4d686SBarry Smith 
3535a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
3541d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
3551d1367b7SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFFormJacobian()
356a4d4d686SBarry Smith 
357a4d4d686SBarry Smith @*/
3585a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J)
359a4d4d686SBarry Smith {
3601d1367b7SBarry Smith   MatSNESMFCtx mfctx;
3611d1367b7SBarry Smith   int          ierr;
3621d1367b7SBarry Smith 
3631d1367b7SBarry Smith   PetscFunctionBegin;
3641d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
3651d1367b7SBarry Smith   ierr = MatShellGetContext(*J,(void **)&mfctx);CHKERRQ(ierr);
3661d1367b7SBarry Smith   mfctx->snes    = snes;
367b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
368b0a32e0cSBarry Smith   PetscLogObjectParent(snes,*J);
3691d1367b7SBarry Smith   PetscFunctionReturn(0);
3701d1367b7SBarry Smith }
3711d1367b7SBarry Smith 
372*4a2ae208SSatish Balay #undef __FUNCT__
373*4a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
3741d1367b7SBarry Smith /*@C
3751d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
3761d1367b7SBarry Smith 
3771d1367b7SBarry Smith    Collective on Vec
3781d1367b7SBarry Smith 
3791d1367b7SBarry Smith    Input Parameters:
3801d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
3811d1367b7SBarry Smith 
3821d1367b7SBarry Smith    Output Parameter:
3831d1367b7SBarry Smith .  J - the matrix-free matrix
3841d1367b7SBarry Smith 
3851d1367b7SBarry Smith    Level: advanced
3861d1367b7SBarry Smith 
3871d1367b7SBarry Smith    Notes:
3881d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
3891d1367b7SBarry Smith    and work space for performing finite difference approximations of
3901d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
3911d1367b7SBarry Smith 
3921d1367b7SBarry Smith    The default code uses the following approach to compute h
3931d1367b7SBarry Smith 
3941d1367b7SBarry Smith .vb
3951d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
3961d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
3971d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
3981d1367b7SBarry Smith  where
3991d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
4001d1367b7SBarry Smith      umin = minimum iterate parameter
4011d1367b7SBarry Smith .ve
4021d1367b7SBarry Smith 
4031d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
4041d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
4051d1367b7SBarry Smith    of the users manual for details.
4061d1367b7SBarry Smith 
4071d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
4081d1367b7SBarry Smith    matrix context.
4091d1367b7SBarry Smith 
4101d1367b7SBarry Smith    Options Database Keys:
4111d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4121d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
4131d1367b7SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
4141d1367b7SBarry Smith 
4151d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
4161d1367b7SBarry Smith 
4171d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4181d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
4191d1367b7SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFFormJacobian()
4201d1367b7SBarry Smith 
4211d1367b7SBarry Smith @*/
4221d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J)
4231d1367b7SBarry Smith {
424a4d4d686SBarry Smith   MPI_Comm     comm;
4255a655dc6SBarry Smith   MatSNESMFCtx mfctx;
4269a6cb015SBarry Smith   int          n,nloc,ierr;
427a4d4d686SBarry Smith 
428a4d4d686SBarry Smith   PetscFunctionBegin;
4291d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
4301d1367b7SBarry Smith   PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",comm,MatSNESMFDestroy_Private,MatSNESMFView_Private);
431b0a32e0cSBarry Smith   PetscLogObjectCreate(mfctx);
432a4d4d686SBarry Smith   mfctx->sp              = 0;
4331d1367b7SBarry Smith   mfctx->snes            = 0;
434329f5518SBarry Smith   mfctx->error_rel       = 1.e-8; /* assumes PetscReal precision */
435329f5518SBarry Smith   mfctx->recomputeperiod = 1;
436329f5518SBarry Smith   mfctx->count           = 0;
437a4d4d686SBarry Smith   mfctx->currenth        = 0.0;
438a4d4d686SBarry Smith   mfctx->historyh        = PETSC_NULL;
439a4d4d686SBarry Smith   mfctx->ncurrenth       = 0;
440a4d4d686SBarry Smith   mfctx->maxcurrenth     = 0;
4416831982aSBarry Smith   mfctx->type_name       = 0;
442b0a32e0cSBarry Smith   mfctx->usesnes         = PETSC_FALSE;
443a4d4d686SBarry Smith 
4449a6cb015SBarry Smith   /*
4459a6cb015SBarry Smith      Create the empty data structure to contain compute-h routines.
4469a6cb015SBarry Smith      These will be filled in below from the command line options or
4475a655dc6SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
4485a655dc6SBarry Smith      then it will default in the first use of MatSNESMFMult_private()
4499a6cb015SBarry Smith   */
4509a6cb015SBarry Smith   mfctx->ops->compute        = 0;
4519a6cb015SBarry Smith   mfctx->ops->destroy        = 0;
4529a6cb015SBarry Smith   mfctx->ops->view           = 0;
4539a6cb015SBarry Smith   mfctx->ops->setfromoptions = 0;
4549a6cb015SBarry Smith   mfctx->hctx                = 0;
4559a6cb015SBarry Smith 
45685614651SBarry Smith   mfctx->func                = 0;
45785614651SBarry Smith   mfctx->funcctx             = 0;
45885614651SBarry Smith   mfctx->funcvec             = 0;
45985614651SBarry Smith 
460a4d4d686SBarry Smith   ierr = VecDuplicate(x,&mfctx->w);CHKERRQ(ierr);
4611d1367b7SBarry Smith   ierr = VecGetSize(mfctx->w,&n);CHKERRQ(ierr);
4621d1367b7SBarry Smith   ierr = VecGetLocalSize(mfctx->w,&nloc);CHKERRQ(ierr);
463ffa0fd9eSBarry Smith   ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J);CHKERRQ(ierr);
4645a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr);
4655a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr);
4665a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private);CHKERRQ(ierr);
4675a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr);
468b0a32e0cSBarry Smith   PetscLogObjectParent(*J,mfctx->w);
4699a6cb015SBarry Smith 
4709a6cb015SBarry Smith   mfctx->mat = *J;
4719a6cb015SBarry Smith   PetscFunctionReturn(0);
4729a6cb015SBarry Smith }
4739a6cb015SBarry Smith 
474*4a2ae208SSatish Balay #undef __FUNCT__
475*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFromOptions"
4769a6cb015SBarry Smith /*@
4775a655dc6SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4789a6cb015SBarry Smith    parameter.
4799a6cb015SBarry Smith 
4809a6cb015SBarry Smith    Collective on Mat
4819a6cb015SBarry Smith 
4829a6cb015SBarry Smith    Input Parameters:
4835a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4845a655dc6SBarry Smith 
4855a655dc6SBarry Smith    Options Database Keys:
4865a655dc6SBarry Smith +  -snes_mf_type - <default,wp>
4875a655dc6SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
488329f5518SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
4899a6cb015SBarry Smith 
49015091d37SBarry Smith    Level: advanced
49115091d37SBarry Smith 
4929a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters
4939a6cb015SBarry Smith 
4945a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4955a655dc6SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
4969a6cb015SBarry Smith @*/
4975a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
4989a6cb015SBarry Smith {
4995a655dc6SBarry Smith   MatSNESMFCtx mfctx;
500f1af5d2fSBarry Smith   int          ierr;
501f1af5d2fSBarry Smith   PetscTruth   flg;
502186905e3SBarry Smith   char         ftype[256];
5039a6cb015SBarry Smith 
5049a6cb015SBarry Smith   PetscFunctionBegin;
5059a6cb015SBarry Smith   ierr = MatShellGetContext(mat,(void **)&mfctx);CHKERRQ(ierr);
5069a6cb015SBarry Smith   if (mfctx) {
507186905e3SBarry Smith     if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
508186905e3SBarry Smith 
509b0a32e0cSBarry Smith     ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
510b0a32e0cSBarry Smith       ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
5119a6cb015SBarry Smith       if (flg) {
5125a655dc6SBarry Smith         ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
5139a6cb015SBarry Smith       }
5149a6cb015SBarry Smith 
515b0a32e0cSBarry Smith       ierr = PetscOptionsDouble("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
516b0a32e0cSBarry Smith       ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
517186905e3SBarry Smith       if (mfctx->snes) {
518b0a32e0cSBarry Smith         ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
519186905e3SBarry Smith         if (flg) {
520186905e3SBarry Smith           SLES sles;
521186905e3SBarry Smith           KSP  ksp;
522186905e3SBarry Smith           ierr = SNESGetSLES(mfctx->snes,&sles);CHKERRQ(ierr);
523186905e3SBarry Smith           ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
524186905e3SBarry Smith           ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
525186905e3SBarry Smith         }
526186905e3SBarry Smith       }
5279a6cb015SBarry Smith       if (mfctx->ops->setfromoptions) {
5289a6cb015SBarry Smith         ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
5299a6cb015SBarry Smith       }
530b0a32e0cSBarry Smith     ierr = PetscOptionsEnd();CHKERRQ(ierr);
5314bbc92c1SBarry Smith 
5329a6cb015SBarry Smith   }
533a4d4d686SBarry Smith   PetscFunctionReturn(0);
534a4d4d686SBarry Smith }
535a4d4d686SBarry Smith 
536*4a2ae208SSatish Balay #undef __FUNCT__
537*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
538a4d4d686SBarry Smith /*@
53965f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
540a4d4d686SBarry Smith    parameter.
541a4d4d686SBarry Smith 
542a4d4d686SBarry Smith    Not Collective
543a4d4d686SBarry Smith 
544a4d4d686SBarry Smith    Input Parameters:
5455a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
546a4d4d686SBarry Smith 
547a4d4d686SBarry Smith    Output Paramter:
548a4d4d686SBarry Smith .  h - the differencing step size
549a4d4d686SBarry Smith 
55015091d37SBarry Smith    Level: advanced
55115091d37SBarry Smith 
552a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
553a4d4d686SBarry Smith 
5545a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
5555a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
556a4d4d686SBarry Smith @*/
5575a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h)
558a4d4d686SBarry Smith {
5595a655dc6SBarry Smith   MatSNESMFCtx ctx;
560a4d4d686SBarry Smith   int          ierr;
561a4d4d686SBarry Smith 
562a4d4d686SBarry Smith   PetscFunctionBegin;
563a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
564a4d4d686SBarry Smith   if (ctx) {
565a4d4d686SBarry Smith     *h = ctx->currenth;
566a4d4d686SBarry Smith   }
567a4d4d686SBarry Smith   PetscFunctionReturn(0);
568a4d4d686SBarry Smith }
569a4d4d686SBarry Smith 
570*4a2ae208SSatish Balay #undef __FUNCT__
571*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
572a4d4d686SBarry Smith /*
5735a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
57465f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
57565f2ba5bSLois Curfman McInnes    each step.
576a4d4d686SBarry Smith */
577329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy)
578a4d4d686SBarry Smith {
579a4d4d686SBarry Smith   PC             pc;
5805a655dc6SBarry Smith   MatSNESMFCtx   ctx;
581a4d4d686SBarry Smith   int            ierr;
582a4d4d686SBarry Smith   Mat            mat;
583a4d4d686SBarry Smith   MPI_Comm       comm;
584a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
585a4d4d686SBarry Smith 
586a4d4d686SBarry Smith   PetscFunctionBegin;
587a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
588a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
589a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
590a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
591a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
5929a6cb015SBarry Smith   if (!ctx) {
59329bbc08cSBarry Smith     SETERRQ(1,"Matrix is not a matrix free shell matrix");
5949a6cb015SBarry Smith   }
595a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
596aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
597d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
598329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
599a4d4d686SBarry Smith #else
600d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
601a4d4d686SBarry Smith #endif
602a4d4d686SBarry Smith   } else {
603d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
604a4d4d686SBarry Smith   }
605a4d4d686SBarry Smith   PetscFunctionReturn(0);
606a4d4d686SBarry Smith }
607a4d4d686SBarry Smith 
608*4a2ae208SSatish Balay #undef __FUNCT__
609*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
61085614651SBarry Smith /*@C
61185614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
61285614651SBarry Smith 
61385614651SBarry Smith    Collective on Mat
61485614651SBarry Smith 
61585614651SBarry Smith    Input Parameters:
61685614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
61785614651SBarry Smith .  v   - workspace vector
61885614651SBarry Smith .  func - the function to use
61985614651SBarry Smith -  funcctx - optional function context passed to function
62085614651SBarry Smith 
62185614651SBarry Smith    Level: advanced
62285614651SBarry Smith 
62385614651SBarry Smith    Notes:
62485614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
62585614651SBarry Smith     matrix inside your compute Jacobian routine
62685614651SBarry Smith 
62785614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
62885614651SBarry Smith 
62985614651SBarry Smith .keywords: SNES, matrix-free, function
63085614651SBarry Smith 
63185614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
63285614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
63385614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
63485614651SBarry Smith @*/
63585614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
63685614651SBarry Smith {
63785614651SBarry Smith   MatSNESMFCtx ctx;
63885614651SBarry Smith   int          ierr;
63985614651SBarry Smith 
64085614651SBarry Smith   PetscFunctionBegin;
64185614651SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
64285614651SBarry Smith   if (ctx) {
64385614651SBarry Smith     ctx->func    = func;
64485614651SBarry Smith     ctx->funcctx = funcctx;
64585614651SBarry Smith     ctx->funcvec = v;
64685614651SBarry Smith   }
64785614651SBarry Smith   PetscFunctionReturn(0);
64885614651SBarry Smith }
64985614651SBarry Smith 
65085614651SBarry Smith 
651*4a2ae208SSatish Balay #undef __FUNCT__
652*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
653329f5518SBarry Smith /*@
654329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
655329f5518SBarry Smith 
656329f5518SBarry Smith    Collective on Mat
657329f5518SBarry Smith 
658329f5518SBarry Smith    Input Parameters:
659329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
660329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
661329f5518SBarry Smith 
662329f5518SBarry Smith    Options Database Keys:
663329f5518SBarry Smith +  -snes_mf_period <period>
664329f5518SBarry Smith 
665329f5518SBarry Smith    Level: advanced
666329f5518SBarry Smith 
667329f5518SBarry Smith 
668329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
669329f5518SBarry Smith 
670329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
671329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
672329f5518SBarry Smith           MatSNESMFKSPMonitor()
673329f5518SBarry Smith @*/
674329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period)
675329f5518SBarry Smith {
676329f5518SBarry Smith   MatSNESMFCtx ctx;
677329f5518SBarry Smith   int          ierr;
678329f5518SBarry Smith 
679329f5518SBarry Smith   PetscFunctionBegin;
680329f5518SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
681329f5518SBarry Smith   if (ctx) {
682329f5518SBarry Smith     ctx->recomputeperiod = period;
683329f5518SBarry Smith   }
684329f5518SBarry Smith   PetscFunctionReturn(0);
685329f5518SBarry Smith }
686329f5518SBarry Smith 
687*4a2ae208SSatish Balay #undef __FUNCT__
688*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
689a4d4d686SBarry Smith /*@
6905a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
691a4d4d686SBarry Smith    matrix-vector products using finite differences.
692a4d4d686SBarry Smith 
693a4d4d686SBarry Smith    Collective on Mat
694a4d4d686SBarry Smith 
695a4d4d686SBarry Smith    Input Parameters:
6965a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
6979a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
698a4d4d686SBarry Smith                the relative error in the function evaluations)
699a4d4d686SBarry Smith 
70015091d37SBarry Smith    Options Database Keys:
70115091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
70215091d37SBarry Smith 
70315091d37SBarry Smith    Level: advanced
70415091d37SBarry Smith 
705a4d4d686SBarry Smith    Notes:
706a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
707a4d4d686SBarry Smith .vb
70865f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
709a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
710a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
711a4d4d686SBarry Smith .ve
712a4d4d686SBarry Smith 
713a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
714a4d4d686SBarry Smith 
7155a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
7165a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
7175a655dc6SBarry Smith           MatSNESMFKSPMonitor()
718a4d4d686SBarry Smith @*/
719329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error)
720a4d4d686SBarry Smith {
7215a655dc6SBarry Smith   MatSNESMFCtx ctx;
722a4d4d686SBarry Smith   int          ierr;
723a4d4d686SBarry Smith 
724a4d4d686SBarry Smith   PetscFunctionBegin;
725a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
726a4d4d686SBarry Smith   if (ctx) {
727a4d4d686SBarry Smith     if (error != PETSC_DEFAULT) ctx->error_rel = error;
728a4d4d686SBarry Smith   }
729a4d4d686SBarry Smith   PetscFunctionReturn(0);
730a4d4d686SBarry Smith }
731a4d4d686SBarry Smith 
732*4a2ae208SSatish Balay #undef __FUNCT__
733*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
734a4d4d686SBarry Smith /*@
73565f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
73665f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
73765f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
73865f2ba5bSLois Curfman McInnes    automatically removed.
739a4d4d686SBarry Smith 
740a4d4d686SBarry Smith    Collective on Mat
741a4d4d686SBarry Smith 
742a4d4d686SBarry Smith    Input Parameters:
743a4d4d686SBarry Smith +  J - the matrix-free matrix context
74474637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
745a4d4d686SBarry Smith 
74615091d37SBarry Smith    Level: advanced
74715091d37SBarry Smith 
748a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
749a4d4d686SBarry Smith 
75074637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
7515a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
7525a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
753a4d4d686SBarry Smith @*/
75474637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
755a4d4d686SBarry Smith {
756a4d4d686SBarry Smith   int          ierr;
7575a655dc6SBarry Smith   MatSNESMFCtx ctx;
758a4d4d686SBarry Smith   MPI_Comm     comm;
759a4d4d686SBarry Smith 
760a4d4d686SBarry Smith   PetscFunctionBegin;
7612d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
762a4d4d686SBarry Smith 
763a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
764a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
765a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
76685614651SBarry Smith   ctx->sp = nullsp;
76785614651SBarry Smith   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
768a4d4d686SBarry Smith   PetscFunctionReturn(0);
769a4d4d686SBarry Smith }
770a4d4d686SBarry Smith 
771*4a2ae208SSatish Balay #undef __FUNCT__
772*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
773a4d4d686SBarry Smith /*@
77465f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
77565f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
776a4d4d686SBarry Smith 
777a4d4d686SBarry Smith    Collective on Mat
778a4d4d686SBarry Smith 
779a4d4d686SBarry Smith    Input Parameters:
780a4d4d686SBarry Smith +  J - the matrix-free matrix context
78165f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
78265f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
78365f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
784a4d4d686SBarry Smith 
78515091d37SBarry Smith    Level: advanced
78615091d37SBarry Smith 
787a4d4d686SBarry Smith    Notes:
78865f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
78965f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
790a4d4d686SBarry Smith 
791a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
792a4d4d686SBarry Smith 
7935a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
7945a655dc6SBarry Smith           MatSNESMFResetHHistory(),
7955a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
796a4d4d686SBarry Smith 
797a4d4d686SBarry Smith @*/
7985a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory)
799a4d4d686SBarry Smith {
800a4d4d686SBarry Smith   int          ierr;
8015a655dc6SBarry Smith   MatSNESMFCtx ctx;
802a4d4d686SBarry Smith 
803a4d4d686SBarry Smith   PetscFunctionBegin;
804a4d4d686SBarry Smith 
805a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
806a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
807a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
808a4d4d686SBarry Smith   ctx->historyh    = history;
809a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
810a4d4d686SBarry Smith   ctx->currenth    = 0;
811a4d4d686SBarry Smith 
812a4d4d686SBarry Smith   PetscFunctionReturn(0);
813a4d4d686SBarry Smith }
814a4d4d686SBarry Smith 
815*4a2ae208SSatish Balay #undef __FUNCT__
816*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
817a4d4d686SBarry Smith /*@
8185a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
819a4d4d686SBarry Smith    collecting a new set of differencing histories.
820a4d4d686SBarry Smith 
821a4d4d686SBarry Smith    Collective on Mat
822a4d4d686SBarry Smith 
823a4d4d686SBarry Smith    Input Parameters:
824a4d4d686SBarry Smith .  J - the matrix-free matrix context
825a4d4d686SBarry Smith 
82615091d37SBarry Smith    Level: advanced
82715091d37SBarry Smith 
828a4d4d686SBarry Smith    Notes:
82965f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
830a4d4d686SBarry Smith 
831a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
832a4d4d686SBarry Smith 
8335a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
8345a655dc6SBarry Smith           MatSNESMFSetHHistory(),
8355a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
836a4d4d686SBarry Smith 
837a4d4d686SBarry Smith @*/
8385a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
839a4d4d686SBarry Smith {
840a4d4d686SBarry Smith   int          ierr;
8415a655dc6SBarry Smith   MatSNESMFCtx ctx;
842a4d4d686SBarry Smith 
843a4d4d686SBarry Smith   PetscFunctionBegin;
844a4d4d686SBarry Smith 
845a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
846a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
847a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
848be726c96SBarry Smith   ctx->ncurrenth    = 0;
849a4d4d686SBarry Smith 
850a4d4d686SBarry Smith   PetscFunctionReturn(0);
851a4d4d686SBarry Smith }
852a4d4d686SBarry Smith 
853*4a2ae208SSatish Balay #undef __FUNCT__
854*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFFormJacobian"
8551d1367b7SBarry Smith int MatSNESMFFormJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
8561d1367b7SBarry Smith {
8571d1367b7SBarry Smith   int ierr;
8581d1367b7SBarry Smith   PetscFunctionBegin;
8591d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8601d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
8611d1367b7SBarry Smith   PetscFunctionReturn(0);
8621d1367b7SBarry Smith }
8631d1367b7SBarry Smith 
864*4a2ae208SSatish Balay #undef __FUNCT__
865*4a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
8661d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U)
8671d1367b7SBarry Smith {
8681d1367b7SBarry Smith   int          ierr;
8691d1367b7SBarry Smith   MatSNESMFCtx ctx;
8701d1367b7SBarry Smith 
8711d1367b7SBarry Smith   PetscFunctionBegin;
8721d1367b7SBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE);
8731d1367b7SBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE);
8741d1367b7SBarry Smith 
8751d1367b7SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx);CHKERRQ(ierr);
8761d1367b7SBarry Smith   ctx->current_u = U;
877b0a32e0cSBarry Smith   ctx->usesnes   = PETSC_FALSE;
8781d1367b7SBarry Smith   PetscFunctionReturn(0);
8791d1367b7SBarry Smith }
880