xref: /petsc/src/snes/mf/snesmfj.c (revision 65df01d881ca7056b825e98f77f86b858fe255c3)
1*65df01d8SBarry Smith /*$Id: snesmfj.c,v 1.125 2001/07/11 03:34:07 bsmith Exp bsmith $*/
281e6777dSBarry Smith 
3e090d566SSatish Balay #include "src/snes/mf/snesmfj.h"   /*I  "petscsnes.h"   I*/
47e9d5209SBarry Smith #include "src/mat/matimpl.h"
581e6777dSBarry Smith 
6b0a32e0cSBarry Smith PetscFList      MatSNESMPetscFList              = 0;
74c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE;
8a4d4d686SBarry Smith 
94a2ae208SSatish Balay #undef __FUNCT__
104a2ae208SSatish 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:
167e9d5209SBarry Smith +   mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMF()
177e9d5209SBarry Smith           or MatSetType(mat,MATMFFD);
189a6cb015SBarry Smith -   ftype - the type requested
199a6cb015SBarry Smith 
2015091d37SBarry Smith     Level: advanced
2115091d37SBarry Smith 
2265f2ba5bSLois Curfman McInnes     Notes:
2365f2ba5bSLois Curfman McInnes     For example, such routines can compute h for use in
2465f2ba5bSLois Curfman McInnes     Jacobian-vector products of the form
2565f2ba5bSLois Curfman McInnes 
2665f2ba5bSLois Curfman McInnes                         F(x+ha) - F(x)
27ef4ad1fdSLois Curfman McInnes           F'(u)a  ~=  ----------------
2865f2ba5bSLois Curfman McInnes                               h
2965f2ba5bSLois Curfman McInnes 
30f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic)
319a6cb015SBarry Smith @*/
32f6a0df18SBarry Smith int MatSNESMFSetType(Mat mat,MatSNESMFType ftype)
33b9fa9cd0SBarry Smith {
345a655dc6SBarry Smith   int          ierr,(*r)(MatSNESMFCtx);
357e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
366831982aSBarry Smith   PetscTruth   match;
37a4d4d686SBarry Smith 
38a4d4d686SBarry Smith   PetscFunctionBegin;
390f5bd95cSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
400f5bd95cSBarry Smith   PetscValidCharPointer(ftype);
410f5bd95cSBarry Smith 
429a6cb015SBarry Smith   /* already set, so just return */
436831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr);
440f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
45a4d4d686SBarry Smith 
469a6cb015SBarry Smith   /* destroy the old one if it exists */
479a6cb015SBarry Smith   if (ctx->ops->destroy) {
489a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
499a6cb015SBarry Smith   }
509a6cb015SBarry Smith 
5165f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
525a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
539a6cb015SBarry Smith 
54b9617806SBarry Smith   ierr =  PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr);
559a6cb015SBarry Smith 
5629bbc08cSBarry Smith   if (!r) SETERRQ(1,"Unknown MatSNESMF type given");
579a6cb015SBarry Smith 
589a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
596831982aSBarry Smith 
606831982aSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
619a6cb015SBarry Smith 
629a6cb015SBarry Smith   PetscFunctionReturn(0);
639a6cb015SBarry Smith }
649a6cb015SBarry Smith 
659a6cb015SBarry Smith /*MC
66f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry.
679a6cb015SBarry Smith 
689a6cb015SBarry Smith    Synopsis:
69fed8bd04SBarry Smith    int MatSNESMFRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
709a6cb015SBarry Smith 
719a6cb015SBarry Smith    Not Collective
729a6cb015SBarry Smith 
739a6cb015SBarry Smith    Input Parameters:
749a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
759a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
769a6cb015SBarry Smith .  name_create - name of routine to create method context
779a6cb015SBarry Smith -  routine_create - routine to create method context
789a6cb015SBarry Smith 
7915091d37SBarry Smith    Level: developer
8015091d37SBarry Smith 
819a6cb015SBarry Smith    Notes:
82f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers.
839a6cb015SBarry Smith 
849a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
859a6cb015SBarry Smith    is ignored.
869a6cb015SBarry Smith 
879a6cb015SBarry Smith    Sample usage:
889a6cb015SBarry Smith .vb
89f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
909a6cb015SBarry Smith                "MyHCreate",MyHCreate);
919a6cb015SBarry Smith .ve
929a6cb015SBarry Smith 
939a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
945a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
959a6cb015SBarry Smith    or at runtime via the option
969a6cb015SBarry Smith $     -snes_mf_type my_h
979a6cb015SBarry Smith 
985a655dc6SBarry Smith .keywords: MatSNESMF, register
999a6cb015SBarry Smith 
1005a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
1019a6cb015SBarry Smith M*/
1029a6cb015SBarry Smith 
1034a2ae208SSatish Balay #undef __FUNCT__
1044a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
105f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
1069a6cb015SBarry Smith {
1079a6cb015SBarry Smith   int ierr;
1089a6cb015SBarry Smith   char fullname[256];
1099a6cb015SBarry Smith 
1109a6cb015SBarry Smith   PetscFunctionBegin;
111b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
112b9617806SBarry Smith   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)())function);CHKERRQ(ierr);
1139a6cb015SBarry Smith   PetscFunctionReturn(0);
1149a6cb015SBarry Smith }
1159a6cb015SBarry Smith 
1169a6cb015SBarry Smith 
1174a2ae208SSatish Balay #undef __FUNCT__
1184a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1199a6cb015SBarry Smith /*@C
1205a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
121f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1229a6cb015SBarry Smith 
1239a6cb015SBarry Smith    Not Collective
1249a6cb015SBarry Smith 
12515091d37SBarry Smith    Level: developer
12615091d37SBarry Smith 
1275a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1289a6cb015SBarry Smith 
129f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1309a6cb015SBarry Smith @*/
1315a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1329a6cb015SBarry Smith {
1339a6cb015SBarry Smith   int ierr;
1349a6cb015SBarry Smith 
1359a6cb015SBarry Smith   PetscFunctionBegin;
136b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
137b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
138b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1399a6cb015SBarry Smith   }
1404c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1419a6cb015SBarry Smith   PetscFunctionReturn(0);
1429a6cb015SBarry Smith }
1439a6cb015SBarry Smith 
1449a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
1454a2ae208SSatish Balay #undef __FUNCT__
1464a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFDestroy_Private"
1475a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat)
148a4d4d686SBarry Smith {
149a4d4d686SBarry Smith   int          ierr;
1507e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
151fae171e0SBarry Smith 
1523a40ed3dSBarry Smith   PetscFunctionBegin;
153b9fa9cd0SBarry Smith   ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
1549a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
15574637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
1566831982aSBarry Smith   PetscHeaderDestroy(ctx);
1573a40ed3dSBarry Smith   PetscFunctionReturn(0);
158b9fa9cd0SBarry Smith }
15950361f65SLois Curfman McInnes 
1604a2ae208SSatish Balay #undef __FUNCT__
1614a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFView_Private"
16239e2f89bSBarry Smith /*
1635a655dc6SBarry Smith    MatSNESMFView_Private - Views matrix-free parameters.
1648f6e3e37SBarry Smith 
16539e2f89bSBarry Smith */
166b0a32e0cSBarry Smith int MatSNESMFView_Private(Mat J,PetscViewer viewer)
167eb9086c3SLois Curfman McInnes {
168eb9086c3SLois Curfman McInnes   int          ierr;
1697e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1706831982aSBarry Smith   PetscTruth   isascii;
171eb9086c3SLois Curfman McInnes 
1723a40ed3dSBarry Smith   PetscFunctionBegin;
173b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
1740f5bd95cSBarry Smith   if (isascii) {
175b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
176b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
177473c83c3SBarry Smith      if (!ctx->type_name) {
178b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
179473c83c3SBarry Smith      } else {
180b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
181473c83c3SBarry Smith      }
1829a6cb015SBarry Smith      if (ctx->ops->view) {
1839a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1849a6cb015SBarry Smith      }
1855cd90555SBarry Smith   } else {
18629bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
187eb9086c3SLois Curfman McInnes   }
1883a40ed3dSBarry Smith   PetscFunctionReturn(0);
189eb9086c3SLois Curfman McInnes }
190eb9086c3SLois Curfman McInnes 
1914a2ae208SSatish Balay #undef __FUNCT__
1924a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAssemblyEnd_Private"
193be726c96SBarry Smith /*
1945a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
19565f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
196be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
19765f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
19865f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
199be726c96SBarry Smith */
2007e9d5209SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J,MatAssemblyType mt)
201be726c96SBarry Smith {
202be726c96SBarry Smith   int             ierr;
2037e9d5209SBarry Smith   MatSNESMFCtx    j = (MatSNESMFCtx)J->data;
204*65df01d8SBarry Smith   SNESProblemType type;
205be726c96SBarry Smith 
206be726c96SBarry Smith   PetscFunctionBegin;
2075a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
208b0a32e0cSBarry Smith   if (j->usesnes) {
2091d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
210*65df01d8SBarry Smith     ierr = SNESGetProblemType(j->snes,&type);CHKERRQ(ierr);
211*65df01d8SBarry Smith     if (type == SNES_NONLINEAR_EQUATIONS) {
2121d1367b7SBarry Smith       ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
213*65df01d8SBarry Smith     } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
2141d1367b7SBarry Smith       ierr = SNESGetGradient(j->snes,&j->current_f,PETSC_NULL);CHKERRQ(ierr);
21529bbc08cSBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class");
2161d1367b7SBarry Smith   }
217be726c96SBarry Smith   PetscFunctionReturn(0);
218be726c96SBarry Smith }
219be726c96SBarry Smith 
2204a2ae208SSatish Balay #undef __FUNCT__
2214a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFMult_Private"
222eb9086c3SLois Curfman McInnes /*
2235a655dc6SBarry Smith   MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector
224eb9086c3SLois Curfman McInnes   product, y = F'(u)*a:
225a4d4d686SBarry Smith 
2269a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
227eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
228eb9086c3SLois Curfman McInnes         u = current iterate
229eb9086c3SLois Curfman McInnes         h = difference interval
230eb9086c3SLois Curfman McInnes */
2315a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y)
23239e2f89bSBarry Smith {
2337e9d5209SBarry Smith   MatSNESMFCtx    ctx = (MatSNESMFCtx)mat->data;
234fae171e0SBarry Smith   SNES            snes;
235a4d4d686SBarry Smith   Scalar          h,mone = -1.0;
236fae171e0SBarry Smith   Vec             w,U,F;
237a305c92eSSatish Balay   int             ierr,(*eval_fct)(SNES,Vec,Vec)=0;
238*65df01d8SBarry Smith   SNESProblemType type;
23939e2f89bSBarry Smith 
2403a40ed3dSBarry Smith   PetscFunctionBegin;
2419a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2429a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2439a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2449a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
245b0a32e0cSBarry Smith   ierr = PetscLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr);
24656cd22aeSBarry Smith 
247fae171e0SBarry Smith   snes = ctx->snes;
248fae171e0SBarry Smith   w    = ctx->w;
2491d1367b7SBarry Smith   U    = ctx->current_u;
25050361f65SLois Curfman McInnes 
25185614651SBarry Smith   /*
25285614651SBarry Smith       Compute differencing parameter
25385614651SBarry Smith   */
2549a6cb015SBarry Smith   if (!ctx->ops->compute) {
255b7fd4e64SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr);
2565a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2579a6cb015SBarry Smith   }
2589a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
259a4d4d686SBarry Smith 
260a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
261a4d4d686SBarry Smith   ctx->currenth = h;
262aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
263b0a32e0cSBarry Smith   PetscLogInfo(mat,"MatSNESMFMult_Private:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h));
264a4d4d686SBarry Smith #else
265b0a32e0cSBarry Smith   PetscLogInfo(mat,"MatSNESMFMult_Private:Current differencing parameter: %15.12e\n",h);
266a4d4d686SBarry Smith #endif
267a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
26885614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
269a4d4d686SBarry Smith   }
27085614651SBarry Smith   ctx->ncurrenth++;
271a4d4d686SBarry Smith 
27285614651SBarry Smith   /* w = u + ha */
273a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
27485614651SBarry Smith 
275b0a32e0cSBarry Smith   if (ctx->usesnes) {
276*65df01d8SBarry Smith     ierr = SNESGetProblemType(snes,&type);CHKERRQ(ierr);
277*65df01d8SBarry Smith     if (type == SNES_NONLINEAR_EQUATIONS) {
27885614651SBarry Smith       eval_fct = SNESComputeFunction;
279*65df01d8SBarry Smith     } else if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
28085614651SBarry Smith       eval_fct = SNESComputeGradient;
28129bbc08cSBarry Smith     } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Invalid method class");
2821d1367b7SBarry Smith     F    = ctx->current_f;
28329bbc08cSBarry Smith     if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices");
28439903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
28585614651SBarry Smith   } else {
28685614651SBarry Smith     F = ctx->funcvec;
28785614651SBarry Smith     /* compute func(U) as base for differencing */
28885614651SBarry Smith     if (ctx->ncurrenth == 1) {
28985614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
29085614651SBarry Smith     }
29185614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
29285614651SBarry Smith   }
293a4d4d686SBarry Smith 
294a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
295a4d4d686SBarry Smith   h    = 1.0/h;
296a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
29774637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
298a4d4d686SBarry Smith 
299b0a32e0cSBarry Smith   ierr = PetscLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);CHKERRQ(ierr);
300a4d4d686SBarry Smith   PetscFunctionReturn(0);
301a4d4d686SBarry Smith }
302a4d4d686SBarry Smith 
3034a2ae208SSatish Balay #undef __FUNCT__
304cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFGetDiagonal_Private"
305cf57b110SBarry Smith /*
306cf57b110SBarry Smith   MatSNESMFGetDiagonal_Private - Gets the diagonal for a matrix free matrix
307cf57b110SBarry Smith 
308cf57b110SBarry Smith         y ~= (F(u + ha) - F(u))/h,
309cf57b110SBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
310cf57b110SBarry Smith         u = current iterate
311cf57b110SBarry Smith         h = difference interval
312cf57b110SBarry Smith */
313cf57b110SBarry Smith int MatSNESMFGetDiagonal_Private(Mat mat,Vec a)
314cf57b110SBarry Smith {
3157e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
316*65df01d8SBarry Smith   Scalar       h,*aa,*ww,v;
317*65df01d8SBarry Smith   double       epsilon = 1.e-8,umin = 1.e-6;
318*65df01d8SBarry Smith   Vec          w,U;
319cf57b110SBarry Smith   int          i,ierr,rstart,rend;
320cf57b110SBarry Smith 
321cf57b110SBarry Smith   PetscFunctionBegin;
322cf57b110SBarry Smith   if (!ctx->funci) {
323cf57b110SBarry Smith     SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first");
324cf57b110SBarry Smith   }
325cf57b110SBarry Smith 
326cf57b110SBarry Smith   w    = ctx->w;
327cf57b110SBarry Smith   U    = ctx->current_u;
328cf57b110SBarry Smith   ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr);
329cf57b110SBarry Smith   ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr);
330cf57b110SBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
331cf57b110SBarry Smith 
332cf57b110SBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
333cf57b110SBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
334cf57b110SBarry Smith   for (i=rstart; i<rend; i++) {
335cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
336cf57b110SBarry Smith     h  = ww[i-rstart];
337cf57b110SBarry Smith     if (h == 0.0) h = 1.0;
338cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX)
339cf57b110SBarry Smith     if (h < umin && h >= 0.0)      h = umin;
340cf57b110SBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
341cf57b110SBarry Smith #else
342cf57b110SBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
343cf57b110SBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
344cf57b110SBarry Smith #endif
345cf57b110SBarry Smith     h     *= epsilon;
346cf57b110SBarry Smith 
347cf57b110SBarry Smith     ww[i-rstart] += h;
348cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
349cf57b110SBarry Smith     ierr          = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr);
350cf57b110SBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
351cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
352cf57b110SBarry Smith     ww[i-rstart] -= h;
353cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
354cf57b110SBarry Smith   }
355cf57b110SBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
356cf57b110SBarry Smith   PetscFunctionReturn(0);
357cf57b110SBarry Smith }
358cf57b110SBarry Smith 
359cf57b110SBarry Smith #undef __FUNCT__
3604a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
361a4d4d686SBarry Smith /*@C
36265f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
36365f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
36465f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
365a4d4d686SBarry Smith 
366a4d4d686SBarry Smith    Collective on SNES and Vec
367a4d4d686SBarry Smith 
368a4d4d686SBarry Smith    Input Parameters:
369a4d4d686SBarry Smith +  snes - the SNES context
370a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
371a4d4d686SBarry Smith 
372a4d4d686SBarry Smith    Output Parameter:
373a4d4d686SBarry Smith .  J - the matrix-free matrix
374a4d4d686SBarry Smith 
37515091d37SBarry Smith    Level: advanced
37615091d37SBarry Smith 
377a4d4d686SBarry Smith    Notes:
378a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
379a4d4d686SBarry Smith    and work space for performing finite difference approximations of
38065f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
3819a6cb015SBarry Smith 
3829a6cb015SBarry Smith    The default code uses the following approach to compute h
383a4d4d686SBarry Smith 
384a4d4d686SBarry Smith .vb
38565f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
386a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
387a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
388a4d4d686SBarry Smith  where
389a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
390a4d4d686SBarry Smith      umin = minimum iterate parameter
391a4d4d686SBarry Smith .ve
392a4d4d686SBarry Smith 
3935a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
39465f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
39565f2ba5bSLois Curfman McInnes    of the users manual for details.
396a4d4d686SBarry Smith 
397a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
398a4d4d686SBarry Smith    matrix context.
399a4d4d686SBarry Smith 
400a4d4d686SBarry Smith    Options Database Keys:
401a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4029a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
403a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
404a4d4d686SBarry Smith 
405a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
406a4d4d686SBarry Smith 
4075a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4081d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
409fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
410a4d4d686SBarry Smith 
411a4d4d686SBarry Smith @*/
4125a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J)
413a4d4d686SBarry Smith {
4141d1367b7SBarry Smith   MatSNESMFCtx mfctx;
4151d1367b7SBarry Smith   int          ierr;
4161d1367b7SBarry Smith 
4171d1367b7SBarry Smith   PetscFunctionBegin;
4181d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
4197e9d5209SBarry Smith 
4207e9d5209SBarry Smith   mfctx          = (MatSNESMFCtx)(*J)->data;
4211d1367b7SBarry Smith   mfctx->snes    = snes;
422b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
423b0a32e0cSBarry Smith   PetscLogObjectParent(snes,*J);
4241d1367b7SBarry Smith   PetscFunctionReturn(0);
4251d1367b7SBarry Smith }
4261d1367b7SBarry Smith 
427cf3bea43SBarry Smith EXTERN_C_BEGIN
428cf3bea43SBarry Smith #undef __FUNCT__
429cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD"
430cf3bea43SBarry Smith int MatSNESMFSetBase_FD(Mat J,Vec U)
431cf3bea43SBarry Smith {
432cf3bea43SBarry Smith   int          ierr;
4337e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
434cf3bea43SBarry Smith 
435cf3bea43SBarry Smith   PetscFunctionBegin;
436cf3bea43SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
437cf3bea43SBarry Smith   ctx->current_u = U;
438cf3bea43SBarry Smith   ctx->usesnes   = PETSC_FALSE;
439cf3bea43SBarry Smith   PetscFunctionReturn(0);
440cf3bea43SBarry Smith }
441cf3bea43SBarry Smith EXTERN_C_END
442cf3bea43SBarry Smith 
4434a2ae208SSatish Balay #undef __FUNCT__
4447e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions"
4457e9d5209SBarry Smith /*@
4467e9d5209SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4477e9d5209SBarry Smith    parameter.
4487e9d5209SBarry Smith 
4497e9d5209SBarry Smith    Collective on Mat
4507e9d5209SBarry Smith 
4517e9d5209SBarry Smith    Input Parameters:
4527e9d5209SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
4537e9d5209SBarry Smith 
4547e9d5209SBarry Smith    Options Database Keys:
4557e9d5209SBarry Smith +  -snes_mf_type - <default,wp>
4567e9d5209SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
4577e9d5209SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
4587e9d5209SBarry Smith 
4597e9d5209SBarry Smith    Level: advanced
4607e9d5209SBarry Smith 
4617e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters
4627e9d5209SBarry Smith 
4637e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
4647e9d5209SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
4657e9d5209SBarry Smith @*/
4667e9d5209SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
4677e9d5209SBarry Smith {
4687e9d5209SBarry Smith   MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data;
4697e9d5209SBarry Smith   int          ierr;
4707e9d5209SBarry Smith   PetscTruth   flg;
4717e9d5209SBarry Smith   char         ftype[256];
4727e9d5209SBarry Smith 
4737e9d5209SBarry Smith   PetscFunctionBegin;
4747e9d5209SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
4757e9d5209SBarry Smith 
4767e9d5209SBarry Smith   ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
4777e9d5209SBarry Smith   ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
4787e9d5209SBarry Smith   if (flg) {
4797e9d5209SBarry Smith     ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
4807e9d5209SBarry Smith   }
4817e9d5209SBarry Smith 
4827e9d5209SBarry Smith   ierr = PetscOptionsDouble("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
4837e9d5209SBarry Smith   ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
4847e9d5209SBarry Smith   if (mfctx->snes) {
4857e9d5209SBarry Smith     ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
4867e9d5209SBarry Smith     if (flg) {
4877e9d5209SBarry Smith       SLES sles;
4887e9d5209SBarry Smith       KSP  ksp;
4897e9d5209SBarry Smith       ierr = SNESGetSLES(mfctx->snes,&sles);CHKERRQ(ierr);
4907e9d5209SBarry Smith       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
4917e9d5209SBarry Smith       ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
4927e9d5209SBarry Smith     }
4937e9d5209SBarry Smith   }
4947e9d5209SBarry Smith   if (mfctx->ops->setfromoptions) {
4957e9d5209SBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
4967e9d5209SBarry Smith   }
4977e9d5209SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4987e9d5209SBarry Smith   PetscFunctionReturn(0);
4997e9d5209SBarry Smith }
5007e9d5209SBarry Smith 
5017e9d5209SBarry Smith #undef __FUNCT__
5027e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD"
5037e9d5209SBarry Smith EXTERN_C_BEGIN
5047e9d5209SBarry Smith int MatCreate_MFFD(Mat A)
5057e9d5209SBarry Smith {
5067e9d5209SBarry Smith   MatSNESMFCtx mfctx;
507*65df01d8SBarry Smith   int          ierr;
5087e9d5209SBarry Smith 
5097e9d5209SBarry Smith   PetscFunctionBegin;
510*65df01d8SBarry Smith   PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatSNESMFDestroy_Private,MatSNESMFView_Private);
5117e9d5209SBarry Smith   PetscLogObjectCreate(mfctx);
5127e9d5209SBarry Smith   mfctx->sp              = 0;
5137e9d5209SBarry Smith   mfctx->snes            = 0;
5147e9d5209SBarry Smith   mfctx->error_rel       = 1.e-8; /* assumes PetscReal precision */
5157e9d5209SBarry Smith   mfctx->recomputeperiod = 1;
5167e9d5209SBarry Smith   mfctx->count           = 0;
5177e9d5209SBarry Smith   mfctx->currenth        = 0.0;
5187e9d5209SBarry Smith   mfctx->historyh        = PETSC_NULL;
5197e9d5209SBarry Smith   mfctx->ncurrenth       = 0;
5207e9d5209SBarry Smith   mfctx->maxcurrenth     = 0;
5217e9d5209SBarry Smith   mfctx->type_name       = 0;
5227e9d5209SBarry Smith   mfctx->usesnes         = PETSC_FALSE;
5237e9d5209SBarry Smith 
5247e9d5209SBarry Smith   /*
5257e9d5209SBarry Smith      Create the empty data structure to contain compute-h routines.
5267e9d5209SBarry Smith      These will be filled in below from the command line options or
5277e9d5209SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
5287e9d5209SBarry Smith      then it will default in the first use of MatSNESMFMult_private()
5297e9d5209SBarry Smith   */
5307e9d5209SBarry Smith   mfctx->ops->compute        = 0;
5317e9d5209SBarry Smith   mfctx->ops->destroy        = 0;
5327e9d5209SBarry Smith   mfctx->ops->view           = 0;
5337e9d5209SBarry Smith   mfctx->ops->setfromoptions = 0;
5347e9d5209SBarry Smith   mfctx->hctx                = 0;
5357e9d5209SBarry Smith 
5367e9d5209SBarry Smith   mfctx->func                = 0;
5377e9d5209SBarry Smith   mfctx->funcctx             = 0;
5387e9d5209SBarry Smith   mfctx->funcvec             = 0;
5397e9d5209SBarry Smith 
540*65df01d8SBarry Smith   A->data                = mfctx;
5417e9d5209SBarry Smith 
542*65df01d8SBarry Smith   A->ops->mult           = MatSNESMFMult_Private;
543*65df01d8SBarry Smith   A->ops->destroy        = MatSNESMFDestroy_Private;
544*65df01d8SBarry Smith   A->ops->view           = MatSNESMFView_Private;
545*65df01d8SBarry Smith   A->ops->assemblyend    = MatSNESMFAssemblyEnd_Private;
546*65df01d8SBarry Smith   A->ops->getdiagonal    = MatSNESMFGetDiagonal_Private;
547*65df01d8SBarry Smith   A->ops->setfromoptions = MatSNESMFSetFromOptions;
5487e9d5209SBarry Smith 
549*65df01d8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr);
550*65df01d8SBarry Smith   mfctx->mat = A;
551*65df01d8SBarry Smith   ierr = VecCreateMPI(A->comm,A->n,A->N,&mfctx->w);CHKERRQ(ierr);
5527e9d5209SBarry Smith 
5537e9d5209SBarry Smith   PetscFunctionReturn(0);
5547e9d5209SBarry Smith }
5557e9d5209SBarry Smith 
5567e9d5209SBarry Smith EXTERN_C_END
5577e9d5209SBarry Smith 
5587e9d5209SBarry Smith #undef __FUNCT__
5594a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
5601d1367b7SBarry Smith /*@C
5611d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
5621d1367b7SBarry Smith 
5631d1367b7SBarry Smith    Collective on Vec
5641d1367b7SBarry Smith 
5651d1367b7SBarry Smith    Input Parameters:
5661d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
5671d1367b7SBarry Smith 
5681d1367b7SBarry Smith    Output Parameter:
5691d1367b7SBarry Smith .  J - the matrix-free matrix
5701d1367b7SBarry Smith 
5711d1367b7SBarry Smith    Level: advanced
5721d1367b7SBarry Smith 
5731d1367b7SBarry Smith    Notes:
5741d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
5751d1367b7SBarry Smith    and work space for performing finite difference approximations of
5761d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
5771d1367b7SBarry Smith 
5781d1367b7SBarry Smith    The default code uses the following approach to compute h
5791d1367b7SBarry Smith 
5801d1367b7SBarry Smith .vb
5811d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
5821d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
5831d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
5841d1367b7SBarry Smith  where
5851d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
5861d1367b7SBarry Smith      umin = minimum iterate parameter
5871d1367b7SBarry Smith .ve
5881d1367b7SBarry Smith 
5891d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
5901d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
5911d1367b7SBarry Smith    of the users manual for details.
5921d1367b7SBarry Smith 
5931d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
5941d1367b7SBarry Smith    matrix context.
5951d1367b7SBarry Smith 
5961d1367b7SBarry Smith    Options Database Keys:
5971d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
5981d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
5991d1367b7SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
6001d1367b7SBarry Smith 
6011d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
6021d1367b7SBarry Smith 
6031d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
6041d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
605fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian()
6061d1367b7SBarry Smith 
6071d1367b7SBarry Smith @*/
6081d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J)
6091d1367b7SBarry Smith {
610a4d4d686SBarry Smith   MPI_Comm     comm;
6115a655dc6SBarry Smith   MatSNESMFCtx mfctx;
6129a6cb015SBarry Smith   int          n,nloc,ierr;
613a4d4d686SBarry Smith 
614a4d4d686SBarry Smith   PetscFunctionBegin;
6151d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
616*65df01d8SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
617*65df01d8SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
6187e9d5209SBarry Smith   ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr);
619*65df01d8SBarry Smith   ierr = MatRegister(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr);
620*65df01d8SBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
6219a6cb015SBarry Smith   PetscFunctionReturn(0);
6229a6cb015SBarry Smith }
6239a6cb015SBarry Smith 
624a4d4d686SBarry Smith 
6254a2ae208SSatish Balay #undef __FUNCT__
6264a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
627a4d4d686SBarry Smith /*@
62865f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
629a4d4d686SBarry Smith    parameter.
630a4d4d686SBarry Smith 
631a4d4d686SBarry Smith    Not Collective
632a4d4d686SBarry Smith 
633a4d4d686SBarry Smith    Input Parameters:
6345a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
635a4d4d686SBarry Smith 
636a4d4d686SBarry Smith    Output Paramter:
637a4d4d686SBarry Smith .  h - the differencing step size
638a4d4d686SBarry Smith 
63915091d37SBarry Smith    Level: advanced
64015091d37SBarry Smith 
641a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
642a4d4d686SBarry Smith 
6435a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
6445a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
645a4d4d686SBarry Smith @*/
6465a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h)
647a4d4d686SBarry Smith {
6487e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
649a4d4d686SBarry Smith   int          ierr;
650a4d4d686SBarry Smith 
651a4d4d686SBarry Smith   PetscFunctionBegin;
652a4d4d686SBarry Smith   *h = ctx->currenth;
653a4d4d686SBarry Smith   PetscFunctionReturn(0);
654a4d4d686SBarry Smith }
655a4d4d686SBarry Smith 
6564a2ae208SSatish Balay #undef __FUNCT__
6574a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
658a4d4d686SBarry Smith /*
6595a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
66065f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
66165f2ba5bSLois Curfman McInnes    each step.
662a4d4d686SBarry Smith */
663329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy)
664a4d4d686SBarry Smith {
665a4d4d686SBarry Smith   PC             pc;
6665a655dc6SBarry Smith   MatSNESMFCtx   ctx;
667a4d4d686SBarry Smith   int            ierr;
668a4d4d686SBarry Smith   Mat            mat;
669a4d4d686SBarry Smith   MPI_Comm       comm;
670a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
671a4d4d686SBarry Smith 
672a4d4d686SBarry Smith   PetscFunctionBegin;
673a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
674a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
675a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
676a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6777e9d5209SBarry Smith   ctx  = (MatSNESMFCtx)mat->data;
6787e9d5209SBarry Smith 
679a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
680aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
681d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
682329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
683a4d4d686SBarry Smith #else
684d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
685a4d4d686SBarry Smith #endif
686a4d4d686SBarry Smith   } else {
687d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
688a4d4d686SBarry Smith   }
689a4d4d686SBarry Smith   PetscFunctionReturn(0);
690a4d4d686SBarry Smith }
691a4d4d686SBarry Smith 
6924a2ae208SSatish Balay #undef __FUNCT__
6934a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
69485614651SBarry Smith /*@C
69585614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
69685614651SBarry Smith 
69785614651SBarry Smith    Collective on Mat
69885614651SBarry Smith 
69985614651SBarry Smith    Input Parameters:
70085614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
70185614651SBarry Smith .  v   - workspace vector
70285614651SBarry Smith .  func - the function to use
70385614651SBarry Smith -  funcctx - optional function context passed to function
70485614651SBarry Smith 
70585614651SBarry Smith    Level: advanced
70685614651SBarry Smith 
70785614651SBarry Smith    Notes:
70885614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
70985614651SBarry Smith     matrix inside your compute Jacobian routine
71085614651SBarry Smith 
71185614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
71285614651SBarry Smith 
71385614651SBarry Smith .keywords: SNES, matrix-free, function
71485614651SBarry Smith 
71585614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
71685614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
71785614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
71885614651SBarry Smith @*/
71985614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
72085614651SBarry Smith {
7217e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
72285614651SBarry Smith   int          ierr;
72385614651SBarry Smith 
72485614651SBarry Smith   PetscFunctionBegin;
72585614651SBarry Smith   ctx->func    = func;
72685614651SBarry Smith   ctx->funcctx = funcctx;
72785614651SBarry Smith   ctx->funcvec = v;
72885614651SBarry Smith   PetscFunctionReturn(0);
72985614651SBarry Smith }
73085614651SBarry Smith 
731cf57b110SBarry Smith #undef __FUNCT__
732cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni"
733cf57b110SBarry Smith /*@C
734cf57b110SBarry Smith    MatSNESMFSetFunctioni - Sets the function for a single component
735cf57b110SBarry Smith 
736cf57b110SBarry Smith    Collective on Mat
737cf57b110SBarry Smith 
738cf57b110SBarry Smith    Input Parameters:
739cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
740cf57b110SBarry Smith -  funci - the function to use
741cf57b110SBarry Smith 
742cf57b110SBarry Smith    Level: advanced
743cf57b110SBarry Smith 
744cf57b110SBarry Smith    Notes:
745cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
746cf57b110SBarry Smith     matrix inside your compute Jacobian routine
747cf57b110SBarry Smith 
748cf57b110SBarry Smith 
749cf57b110SBarry Smith .keywords: SNES, matrix-free, function
750cf57b110SBarry Smith 
751cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
752cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
753cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
754cf57b110SBarry Smith @*/
755cf57b110SBarry Smith int MatSNESMFSetFunctioni(Mat mat,int (*funci)(int,Vec,Scalar*,void *))
756cf57b110SBarry Smith {
7577e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
758cf57b110SBarry Smith   int          ierr;
759cf57b110SBarry Smith 
760cf57b110SBarry Smith   PetscFunctionBegin;
761cf57b110SBarry Smith   ctx->funci   = funci;
762cf57b110SBarry Smith   PetscFunctionReturn(0);
763cf57b110SBarry Smith }
764cf57b110SBarry Smith 
765cf57b110SBarry Smith #undef __FUNCT__
766cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase"
767cf57b110SBarry Smith /*@C
768cf57b110SBarry Smith    MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation
769cf57b110SBarry Smith 
770cf57b110SBarry Smith    Collective on Mat
771cf57b110SBarry Smith 
772cf57b110SBarry Smith    Input Parameters:
773cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
774cf57b110SBarry Smith -  func - the function to use
775cf57b110SBarry Smith 
776cf57b110SBarry Smith    Level: advanced
777cf57b110SBarry Smith 
778cf57b110SBarry Smith    Notes:
779cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
780cf57b110SBarry Smith     matrix inside your compute Jacobian routine
781cf57b110SBarry Smith 
782cf57b110SBarry Smith 
783cf57b110SBarry Smith .keywords: SNES, matrix-free, function
784cf57b110SBarry Smith 
785cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
786cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
787cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
788cf57b110SBarry Smith @*/
789cf57b110SBarry Smith int MatSNESMFSetFunctioniBase(Mat mat,int (*func)(Vec,void *))
790cf57b110SBarry Smith {
7917e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
792cf57b110SBarry Smith   int          ierr;
793cf57b110SBarry Smith 
794cf57b110SBarry Smith   PetscFunctionBegin;
795cf57b110SBarry Smith   ctx->funcisetbase   = func;
796cf57b110SBarry Smith   PetscFunctionReturn(0);
797cf57b110SBarry Smith }
798cf57b110SBarry Smith 
79985614651SBarry Smith 
8004a2ae208SSatish Balay #undef __FUNCT__
8014a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
802329f5518SBarry Smith /*@
803329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
804329f5518SBarry Smith 
805329f5518SBarry Smith    Collective on Mat
806329f5518SBarry Smith 
807329f5518SBarry Smith    Input Parameters:
808329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
809329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
810329f5518SBarry Smith 
811329f5518SBarry Smith    Options Database Keys:
812329f5518SBarry Smith +  -snes_mf_period <period>
813329f5518SBarry Smith 
814329f5518SBarry Smith    Level: advanced
815329f5518SBarry Smith 
816329f5518SBarry Smith 
817329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
818329f5518SBarry Smith 
819329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
820329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
821329f5518SBarry Smith           MatSNESMFKSPMonitor()
822329f5518SBarry Smith @*/
823329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period)
824329f5518SBarry Smith {
8257e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
826329f5518SBarry Smith   int          ierr;
827329f5518SBarry Smith 
828329f5518SBarry Smith   PetscFunctionBegin;
829329f5518SBarry Smith   ctx->recomputeperiod = period;
830329f5518SBarry Smith   PetscFunctionReturn(0);
831329f5518SBarry Smith }
832329f5518SBarry Smith 
8334a2ae208SSatish Balay #undef __FUNCT__
8344a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
835a4d4d686SBarry Smith /*@
8365a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
837a4d4d686SBarry Smith    matrix-vector products using finite differences.
838a4d4d686SBarry Smith 
839a4d4d686SBarry Smith    Collective on Mat
840a4d4d686SBarry Smith 
841a4d4d686SBarry Smith    Input Parameters:
8425a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
8439a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
844a4d4d686SBarry Smith                the relative error in the function evaluations)
845a4d4d686SBarry Smith 
84615091d37SBarry Smith    Options Database Keys:
84715091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
84815091d37SBarry Smith 
84915091d37SBarry Smith    Level: advanced
85015091d37SBarry Smith 
851a4d4d686SBarry Smith    Notes:
852a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
853a4d4d686SBarry Smith .vb
85465f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
855a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
856a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
857a4d4d686SBarry Smith .ve
858a4d4d686SBarry Smith 
859a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
860a4d4d686SBarry Smith 
8615a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
8625a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
8635a655dc6SBarry Smith           MatSNESMFKSPMonitor()
864a4d4d686SBarry Smith @*/
865329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error)
866a4d4d686SBarry Smith {
8677e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
868a4d4d686SBarry Smith   int          ierr;
869a4d4d686SBarry Smith 
870a4d4d686SBarry Smith   PetscFunctionBegin;
871a4d4d686SBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
872a4d4d686SBarry Smith   PetscFunctionReturn(0);
873a4d4d686SBarry Smith }
874a4d4d686SBarry Smith 
8754a2ae208SSatish Balay #undef __FUNCT__
8764a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
877a4d4d686SBarry Smith /*@
87865f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
87965f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
88065f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
88165f2ba5bSLois Curfman McInnes    automatically removed.
882a4d4d686SBarry Smith 
883a4d4d686SBarry Smith    Collective on Mat
884a4d4d686SBarry Smith 
885a4d4d686SBarry Smith    Input Parameters:
886a4d4d686SBarry Smith +  J - the matrix-free matrix context
88774637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
888a4d4d686SBarry Smith 
88915091d37SBarry Smith    Level: advanced
89015091d37SBarry Smith 
891a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
892a4d4d686SBarry Smith 
89374637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
8945a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
8955a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
896a4d4d686SBarry Smith @*/
89774637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
898a4d4d686SBarry Smith {
899a4d4d686SBarry Smith   int          ierr;
9007e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
901a4d4d686SBarry Smith   MPI_Comm     comm;
902a4d4d686SBarry Smith 
903a4d4d686SBarry Smith   PetscFunctionBegin;
9042d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
905a4d4d686SBarry Smith 
90685614651SBarry Smith   ctx->sp = nullsp;
90785614651SBarry Smith   ierr    = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
908a4d4d686SBarry Smith   PetscFunctionReturn(0);
909a4d4d686SBarry Smith }
910a4d4d686SBarry Smith 
9114a2ae208SSatish Balay #undef __FUNCT__
9124a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
913a4d4d686SBarry Smith /*@
91465f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
91565f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
916a4d4d686SBarry Smith 
917a4d4d686SBarry Smith    Collective on Mat
918a4d4d686SBarry Smith 
919a4d4d686SBarry Smith    Input Parameters:
920a4d4d686SBarry Smith +  J - the matrix-free matrix context
92165f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
92265f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
92365f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
924a4d4d686SBarry Smith 
92515091d37SBarry Smith    Level: advanced
92615091d37SBarry Smith 
927a4d4d686SBarry Smith    Notes:
92865f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
92965f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
930a4d4d686SBarry Smith 
931a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
932a4d4d686SBarry Smith 
9335a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
9345a655dc6SBarry Smith           MatSNESMFResetHHistory(),
9355a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
936a4d4d686SBarry Smith 
937a4d4d686SBarry Smith @*/
9385a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory)
939a4d4d686SBarry Smith {
940a4d4d686SBarry Smith   int          ierr;
9417e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
942a4d4d686SBarry Smith 
943a4d4d686SBarry Smith   PetscFunctionBegin;
944a4d4d686SBarry Smith   ctx->historyh    = history;
945a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
946a4d4d686SBarry Smith   ctx->currenth    = 0;
947a4d4d686SBarry Smith 
948a4d4d686SBarry Smith   PetscFunctionReturn(0);
949a4d4d686SBarry Smith }
950a4d4d686SBarry Smith 
9514a2ae208SSatish Balay #undef __FUNCT__
9524a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
953a4d4d686SBarry Smith /*@
9545a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
955a4d4d686SBarry Smith    collecting a new set of differencing histories.
956a4d4d686SBarry Smith 
957a4d4d686SBarry Smith    Collective on Mat
958a4d4d686SBarry Smith 
959a4d4d686SBarry Smith    Input Parameters:
960a4d4d686SBarry Smith .  J - the matrix-free matrix context
961a4d4d686SBarry Smith 
96215091d37SBarry Smith    Level: advanced
96315091d37SBarry Smith 
964a4d4d686SBarry Smith    Notes:
96565f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
966a4d4d686SBarry Smith 
967a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
968a4d4d686SBarry Smith 
9695a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
9705a655dc6SBarry Smith           MatSNESMFSetHHistory(),
9715a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
972a4d4d686SBarry Smith 
973a4d4d686SBarry Smith @*/
9745a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
975a4d4d686SBarry Smith {
976a4d4d686SBarry Smith   int          ierr;
9777e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
978a4d4d686SBarry Smith 
979a4d4d686SBarry Smith   PetscFunctionBegin;
980be726c96SBarry Smith   ctx->ncurrenth    = 0;
981a4d4d686SBarry Smith 
982a4d4d686SBarry Smith   PetscFunctionReturn(0);
983a4d4d686SBarry Smith }
984a4d4d686SBarry Smith 
9854a2ae208SSatish Balay #undef __FUNCT__
986fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian"
987fed8bd04SBarry Smith int MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
9881d1367b7SBarry Smith {
9891d1367b7SBarry Smith   int ierr;
9901d1367b7SBarry Smith   PetscFunctionBegin;
9911d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9921d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
9931d1367b7SBarry Smith   PetscFunctionReturn(0);
9941d1367b7SBarry Smith }
9951d1367b7SBarry Smith 
9964a2ae208SSatish Balay #undef __FUNCT__
9974a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
9981d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U)
9991d1367b7SBarry Smith {
10003a7fca6bSBarry Smith   int  ierr,(*f)(Mat,Vec);
10011d1367b7SBarry Smith 
10021d1367b7SBarry Smith   PetscFunctionBegin;
10031d1367b7SBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE);
10041d1367b7SBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE);
1005cf3bea43SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)())&f);CHKERRQ(ierr);
1006cf3bea43SBarry Smith   if (f) {
1007cf3bea43SBarry Smith     ierr = (*f)(J,U);CHKERRQ(ierr);
100849d4803aSBarry Smith   }
10091d1367b7SBarry Smith   PetscFunctionReturn(0);
10101d1367b7SBarry Smith }
1011cf57b110SBarry Smith 
1012cf57b110SBarry Smith 
1013cf57b110SBarry Smith 
1014cf57b110SBarry Smith 
1015cf57b110SBarry Smith 
1016cf57b110SBarry Smith 
1017cf57b110SBarry Smith 
1018cf57b110SBarry Smith 
1019cf57b110SBarry Smith 
1020