xref: /petsc/src/snes/mf/snesmfj.c (revision 5a655dc69978bfa5d89a399bdbc880d673ee91a3)
19a6cb015SBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*5a655dc6SBarry Smith static char vcid[] = "$Id: snesmfj.c,v 1.80 1999/03/17 23:24:23 bsmith Exp bsmith $";
481e6777dSBarry Smith #endif
581e6777dSBarry Smith 
69a6cb015SBarry Smith #include "src/snes/snesimpl.h"
79a6cb015SBarry Smith #include "src/snes/mf/snesmfj.h"   /*I  "snes.h"   I*/
881e6777dSBarry Smith 
9*5a655dc6SBarry Smith FList MatSNESMFList              = 0;
10*5a655dc6SBarry Smith int   MatSNESMFRegisterAllCalled = 0;
11a4d4d686SBarry Smith 
1239e2f89bSBarry Smith 
135615d1e5SSatish Balay #undef __FUNC__
14*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetType"
159a6cb015SBarry Smith /*@
16*5a655dc6SBarry Smith       MatSNESMFSetType - Sets the method that is used to compute the h in the
179a6cb015SBarry Smith             finite difference matrix free formulation.
189a6cb015SBarry Smith 
199a6cb015SBarry Smith    Input Parameters:
20*5a655dc6SBarry Smith +     mat - the matrix free matrix created via MatCreateSNESMF()
219a6cb015SBarry Smith -     ftype - the type requested
229a6cb015SBarry Smith 
2315091d37SBarry Smith    Level: advanced
2415091d37SBarry Smith 
25*5a655dc6SBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegister()
269a6cb015SBarry Smith @*/
27*5a655dc6SBarry Smith int MatSNESMFSetType(Mat mat,char *ftype)
28b9fa9cd0SBarry Smith {
29*5a655dc6SBarry Smith   int          ierr, (*r)(MatSNESMFCtx);
30*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
31a4d4d686SBarry Smith 
32a4d4d686SBarry Smith   PetscFunctionBegin;
33a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
34a4d4d686SBarry Smith 
359a6cb015SBarry Smith   /* already set, so just return */
363f1db9ecSBarry Smith   if (PetscTypeCompare(ctx->type_name,ftype)) PetscFunctionReturn(0);
37a4d4d686SBarry Smith 
389a6cb015SBarry Smith   /* destroy the old one if it exists */
399a6cb015SBarry Smith   if (ctx->ops->destroy) {
409a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
419a6cb015SBarry Smith   }
429a6cb015SBarry Smith 
439a6cb015SBarry Smith   /* Get the function pointers for the iterative method requested */
44*5a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
459a6cb015SBarry Smith 
46*5a655dc6SBarry Smith   ierr =  FListFind(ctx->comm, MatSNESMFList, ftype,(int (**)(void *)) &r );CHKERRQ(ierr);
479a6cb015SBarry Smith 
48*5a655dc6SBarry Smith   if (!r) SETERRQ(1,1,"Unknown MatSNESMF type given");
499a6cb015SBarry Smith 
509a6cb015SBarry Smith   ierr = (*r)(ctx); CHKERRQ(ierr);
519a6cb015SBarry Smith   ierr = PetscStrncpy(ctx->type_name,ftype,256);CHKERRQ(ierr);
529a6cb015SBarry Smith 
539a6cb015SBarry Smith   PetscFunctionReturn(0);
549a6cb015SBarry Smith }
559a6cb015SBarry Smith 
569a6cb015SBarry Smith /*MC
57*5a655dc6SBarry Smith    MatSNESMFRegister - Adds a method to the MatSNESMF registry
589a6cb015SBarry Smith 
599a6cb015SBarry Smith    Synopsis:
60*5a655dc6SBarry Smith    MatSNESMFRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
619a6cb015SBarry Smith 
629a6cb015SBarry Smith    Not Collective
639a6cb015SBarry Smith 
649a6cb015SBarry Smith    Input Parameters:
659a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
669a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
679a6cb015SBarry Smith .  name_create - name of routine to create method context
689a6cb015SBarry Smith -  routine_create - routine to create method context
699a6cb015SBarry Smith 
7015091d37SBarry Smith    Level: developer
7115091d37SBarry Smith 
729a6cb015SBarry Smith    Notes:
73*5a655dc6SBarry Smith    MatSNESMFRegister() may be called multiple times to add several user-defined solvers.
749a6cb015SBarry Smith 
759a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
769a6cb015SBarry Smith    is ignored.
779a6cb015SBarry Smith 
789a6cb015SBarry Smith    Sample usage:
799a6cb015SBarry Smith .vb
80*5a655dc6SBarry Smith    MatSNESMFRegister("my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
819a6cb015SBarry Smith                "MyHCreate",MyHCreate);
829a6cb015SBarry Smith .ve
839a6cb015SBarry Smith 
849a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
85*5a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
869a6cb015SBarry Smith    or at runtime via the option
879a6cb015SBarry Smith $     -snes_mf_type my_h
889a6cb015SBarry Smith 
89*5a655dc6SBarry Smith .keywords: MatSNESMF, register
909a6cb015SBarry Smith 
91*5a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
929a6cb015SBarry Smith M*/
939a6cb015SBarry Smith 
949a6cb015SBarry Smith #undef __FUNC__
95*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegister_Private"
96*5a655dc6SBarry Smith int MatSNESMFRegister_Private(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
979a6cb015SBarry Smith {
989a6cb015SBarry Smith   int ierr;
999a6cb015SBarry Smith   char fullname[256];
1009a6cb015SBarry Smith 
1019a6cb015SBarry Smith   PetscFunctionBegin;
1029a6cb015SBarry Smith   PetscStrcpy(fullname,path); PetscStrcat(fullname,":");PetscStrcat(fullname,name);
103*5a655dc6SBarry Smith   ierr = FListAdd_Private(&MatSNESMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
1049a6cb015SBarry Smith   PetscFunctionReturn(0);
1059a6cb015SBarry Smith }
1069a6cb015SBarry Smith 
1079a6cb015SBarry Smith 
1089a6cb015SBarry Smith #undef __FUNC__
109*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFRegisterDestroy"
1109a6cb015SBarry Smith /*@C
111*5a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
112*5a655dc6SBarry Smith    registered by MatSNESMFRegister().
1139a6cb015SBarry Smith 
1149a6cb015SBarry Smith    Not Collective
1159a6cb015SBarry Smith 
11615091d37SBarry Smith    Level: developer
11715091d37SBarry Smith 
118*5a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1199a6cb015SBarry Smith 
120*5a655dc6SBarry Smith .seealso: MatSNESMFRegister(), MatSNESMFRegisterAll()
1219a6cb015SBarry Smith @*/
122*5a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1239a6cb015SBarry Smith {
1249a6cb015SBarry Smith   int ierr;
1259a6cb015SBarry Smith 
1269a6cb015SBarry Smith   PetscFunctionBegin;
127*5a655dc6SBarry Smith   if (MatSNESMFList) {
128*5a655dc6SBarry Smith     ierr = FListDestroy( MatSNESMFList );CHKERRQ(ierr);
129*5a655dc6SBarry Smith     MatSNESMFList = 0;
1309a6cb015SBarry Smith   }
131*5a655dc6SBarry Smith   MatSNESMFRegisterAllCalled = 0;
1329a6cb015SBarry Smith   PetscFunctionReturn(0);
1339a6cb015SBarry Smith }
1349a6cb015SBarry Smith 
1359a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
136a4d4d686SBarry Smith #undef __FUNC__
137*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFDestroy_Private"
138*5a655dc6SBarry Smith int MatSNESMFDestroy_Private(Mat mat)
139a4d4d686SBarry Smith {
140a4d4d686SBarry Smith   int            ierr;
141*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
142fae171e0SBarry Smith 
1433a40ed3dSBarry Smith   PetscFunctionBegin;
1440a25c783SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr);
145b9fa9cd0SBarry Smith   ierr = VecDestroy(ctx->w); CHKERRQ(ierr);
1469a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
147b4fd4287SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
1489a6cb015SBarry Smith   PetscFree(ctx->ops);
149fae171e0SBarry Smith   PetscFree(ctx);
1503a40ed3dSBarry Smith   PetscFunctionReturn(0);
151b9fa9cd0SBarry Smith }
15250361f65SLois Curfman McInnes 
1535615d1e5SSatish Balay #undef __FUNC__
154*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFView_Private"
15539e2f89bSBarry Smith /*
156*5a655dc6SBarry Smith    MatSNESMFView_Private - Views matrix-free parameters.
1578f6e3e37SBarry Smith 
15839e2f89bSBarry Smith */
159*5a655dc6SBarry Smith int MatSNESMFView_Private(Mat J,Viewer viewer)
160eb9086c3SLois Curfman McInnes {
161eb9086c3SLois Curfman McInnes   int            ierr;
162*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
163eb9086c3SLois Curfman McInnes   MPI_Comm       comm;
164eb9086c3SLois Curfman McInnes   FILE           *fd;
165eb9086c3SLois Curfman McInnes   ViewerType     vtype;
166eb9086c3SLois Curfman McInnes 
1673a40ed3dSBarry Smith   PetscFunctionBegin;
1682d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
169eb9086c3SLois Curfman McInnes   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
170eb9086c3SLois Curfman McInnes   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
171eb9086c3SLois Curfman McInnes   ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
1723f1db9ecSBarry Smith   if (PetscTypeCompare(vtype,ASCII_VIEWER)) {
173eb9086c3SLois Curfman McInnes      PetscFPrintf(comm,fd,"  SNES matrix-free approximation:\n");
174eb9086c3SLois Curfman McInnes      PetscFPrintf(comm,fd,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);
1759a6cb015SBarry Smith      PetscFPrintf(ctx->comm,fd,"    Using %s compute h routine\n",ctx->type_name);
1769a6cb015SBarry Smith      if (ctx->ops->view) {
1779a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1789a6cb015SBarry Smith      }
1795cd90555SBarry Smith   } else {
1805cd90555SBarry Smith     SETERRQ(1,1,"Viewer type not supported for this object");
181eb9086c3SLois Curfman McInnes   }
1823a40ed3dSBarry Smith   PetscFunctionReturn(0);
183eb9086c3SLois Curfman McInnes }
184eb9086c3SLois Curfman McInnes 
185be726c96SBarry Smith #undef __FUNC__
186*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAssemblyEnd_Private"
187be726c96SBarry Smith /*
188*5a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
189be726c96SBarry Smith      allows the user to indicate the beginning of a new linear solve by call
190be726c96SBarry Smith      MatAssemblyXXX() on the matrix free matrix. This then allows the
191*5a655dc6SBarry Smith      MatSNESMFCreate_WP() to properly compute the || U|| only the first
192be726c96SBarry Smith      time in the linear solver rather than every time
193be726c96SBarry Smith 
194be726c96SBarry Smith */
195*5a655dc6SBarry Smith int MatSNESMFAssemblyEnd_Private(Mat J)
196be726c96SBarry Smith {
197be726c96SBarry Smith   int            ierr;
198be726c96SBarry Smith 
199be726c96SBarry Smith   PetscFunctionBegin;
200*5a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
201be726c96SBarry Smith   PetscFunctionReturn(0);
202be726c96SBarry Smith }
203be726c96SBarry Smith 
204c481317fSBarry Smith 
2055615d1e5SSatish Balay #undef __FUNC__
206*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFMult_Private"
207eb9086c3SLois Curfman McInnes /*
208*5a655dc6SBarry Smith   MatSNESMFMult_Private - Default matrix-free form for Jacobian-vector
209eb9086c3SLois Curfman McInnes   product, y = F'(u)*a:
210a4d4d686SBarry Smith 
2119a6cb015SBarry Smith         y ~= ( F(u + ha) - F(u) )/h,
212eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
213eb9086c3SLois Curfman McInnes         u = current iterate
214eb9086c3SLois Curfman McInnes         h = difference interval
215eb9086c3SLois Curfman McInnes */
216*5a655dc6SBarry Smith int MatSNESMFMult_Private(Mat mat,Vec a,Vec y)
21739e2f89bSBarry Smith {
218*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
219fae171e0SBarry Smith   SNES           snes;
220a4d4d686SBarry Smith   Scalar         h, mone = -1.0;
221fae171e0SBarry Smith   Vec            w,U,F;
222a305c92eSSatish Balay   int            ierr, (*eval_fct)(SNES,Vec,Vec)=0;
22339e2f89bSBarry Smith 
2243a40ed3dSBarry Smith   PetscFunctionBegin;
2259a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2269a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2279a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2289a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
22956cd22aeSBarry Smith   PLogEventBegin(MAT_MatrixFreeMult,a,y,0,0);
23056cd22aeSBarry Smith 
2316e38a044SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
232fae171e0SBarry Smith   snes = ctx->snes;
233fae171e0SBarry Smith   w    = ctx->w;
234fae171e0SBarry Smith 
23578b31e54SBarry Smith   ierr = SNESGetSolution(snes,&U); CHKERRQ(ierr);
23683e56afdSLois Curfman McInnes   if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
23783e56afdSLois Curfman McInnes     eval_fct = SNESComputeFunction;
23878b31e54SBarry Smith     ierr = SNESGetFunction(snes,&F); CHKERRQ(ierr);
239a4d4d686SBarry Smith   } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
24083e56afdSLois Curfman McInnes     eval_fct = SNESComputeGradient;
24183e56afdSLois Curfman McInnes     ierr = SNESGetGradient(snes,&F); CHKERRQ(ierr);
242a4d4d686SBarry Smith   } else SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Invalid method class");
24350361f65SLois Curfman McInnes 
2449a6cb015SBarry Smith   if (!ctx->ops->compute) {
245*5a655dc6SBarry Smith     ierr = MatSNESMFSetType(mat,"default");CHKERRQ(ierr);
246*5a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2479a6cb015SBarry Smith   }
2489a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h); CHKERRQ(ierr);
249a4d4d686SBarry Smith 
250a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
251a4d4d686SBarry Smith   ctx->currenth = h;
252a4d4d686SBarry Smith #if defined(USE_PETSC_COMPLEX)
253a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g + %g i\n",PetscReal(h),PetscImaginary(h));
254a4d4d686SBarry Smith #else
255a4d4d686SBarry Smith   PLogInfo(mat,"Current differencing parameter: %g\n",h);
256a4d4d686SBarry Smith #endif
257a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
258a4d4d686SBarry Smith     ctx->historyh[ctx->ncurrenth++] = h;
259be726c96SBarry Smith   } else {
260be726c96SBarry Smith     ctx->ncurrenth++;
261a4d4d686SBarry Smith   }
262a4d4d686SBarry Smith 
263a4d4d686SBarry Smith   /* Evaluate function at F(u + ha) */
264a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w); CHKERRQ(ierr);
265a4d4d686SBarry Smith   ierr = eval_fct(snes,w,y); CHKERRQ(ierr);
266a4d4d686SBarry Smith 
267a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y); CHKERRQ(ierr);
268a4d4d686SBarry Smith   h = 1.0/h;
269a4d4d686SBarry Smith   ierr = VecScale(&h,y); CHKERRQ(ierr);
270a4d4d686SBarry Smith   if (ctx->sp) {ierr = PCNullSpaceRemove(ctx->sp,y); CHKERRQ(ierr);}
271a4d4d686SBarry Smith 
272a4d4d686SBarry Smith   PLogEventEnd(MAT_MatrixFreeMult,a,y,0,0);
273a4d4d686SBarry Smith   PetscFunctionReturn(0);
274a4d4d686SBarry Smith }
275a4d4d686SBarry Smith 
276a4d4d686SBarry Smith #undef __FUNC__
277*5a655dc6SBarry Smith #define __FUNC__ "MatCreateSNESMF"
278a4d4d686SBarry Smith /*@C
279*5a655dc6SBarry Smith    MatCreateSNESMF - Creates a matrix-free matrix
280a4d4d686SBarry Smith    context for use with a SNES solver.  This matrix can be used as
281a4d4d686SBarry Smith    the Jacobian argument for the routine SNESSetJacobian().
282a4d4d686SBarry Smith 
283a4d4d686SBarry Smith    Collective on SNES and Vec
284a4d4d686SBarry Smith 
285a4d4d686SBarry Smith    Input Parameters:
286a4d4d686SBarry Smith +  snes - the SNES context
287a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
288a4d4d686SBarry Smith 
289a4d4d686SBarry Smith    Output Parameter:
290a4d4d686SBarry Smith .  J - the matrix-free matrix
291a4d4d686SBarry Smith 
29215091d37SBarry Smith    Level: advanced
29315091d37SBarry Smith 
294a4d4d686SBarry Smith    Notes:
295a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
296a4d4d686SBarry Smith    and work space for performing finite difference approximations of
2979a6cb015SBarry Smith    Jacobian-vector products, J(u)*a,
2989a6cb015SBarry Smith 
2999a6cb015SBarry Smith    The default code uses the following approach to compute h
300a4d4d686SBarry Smith 
301a4d4d686SBarry Smith .vb
302a4d4d686SBarry Smith      J(u)*a = [J(u+h*a) - J(u)]/h where
303a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
304a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
305a4d4d686SBarry Smith  where
306a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
307a4d4d686SBarry Smith      umin = minimum iterate parameter
308a4d4d686SBarry Smith .ve
309a4d4d686SBarry Smith 
310*5a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
311*5a655dc6SBarry Smith    umin via MatSNESMFDefaultSetUmin()
312a4d4d686SBarry Smith    See the nonlinear solvers chapter of the users manual for details.
313a4d4d686SBarry Smith 
314a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
315a4d4d686SBarry Smith    matrix context.
316a4d4d686SBarry Smith 
317a4d4d686SBarry Smith    Options Database Keys:
318a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
3199a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
320a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
321a4d4d686SBarry Smith 
322a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
323a4d4d686SBarry Smith 
324*5a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
325*5a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
326*5a655dc6SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegister()
327a4d4d686SBarry Smith 
328a4d4d686SBarry Smith @*/
329*5a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x, Mat *J)
330a4d4d686SBarry Smith {
331a4d4d686SBarry Smith   MPI_Comm       comm;
332*5a655dc6SBarry Smith   MatSNESMFCtx mfctx;
3339a6cb015SBarry Smith   int            n, nloc, ierr;
334a4d4d686SBarry Smith 
335a4d4d686SBarry Smith   PetscFunctionBegin;
336*5a655dc6SBarry Smith   mfctx = (MatSNESMFCtx) PetscMalloc(sizeof(struct _p_MatSNESMFCtx)); CHKPTRQ(mfctx);
337*5a655dc6SBarry Smith   PLogObjectMemory(snes,sizeof(MatSNESMFCtx));
3389a6cb015SBarry Smith   mfctx->comm         = snes->comm;
339a4d4d686SBarry Smith   mfctx->sp           = 0;
340a4d4d686SBarry Smith   mfctx->snes         = snes;
341a4d4d686SBarry Smith   mfctx->error_rel    = 1.e-8; /* assumes double precision */
342a4d4d686SBarry Smith   mfctx->currenth     = 0.0;
343a4d4d686SBarry Smith   mfctx->historyh     = PETSC_NULL;
344a4d4d686SBarry Smith   mfctx->ncurrenth    = 0;
345a4d4d686SBarry Smith   mfctx->maxcurrenth  = 0;
346a4d4d686SBarry Smith 
3479a6cb015SBarry Smith   /*
3489a6cb015SBarry Smith      Create the empty data structure to contain compute-h routines.
3499a6cb015SBarry Smith      These will be filled in below from the command line options or
350*5a655dc6SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
351*5a655dc6SBarry Smith      then it will default in the first use of MatSNESMFMult_private()
3529a6cb015SBarry Smith   */
3539a6cb015SBarry Smith   mfctx->ops                 = (MFOps *)PetscMalloc(sizeof(MFOps)); CHKPTRQ(mfctx->ops);
3549a6cb015SBarry Smith   mfctx->ops->compute        = 0;
3559a6cb015SBarry Smith   mfctx->ops->destroy        = 0;
3569a6cb015SBarry Smith   mfctx->ops->view           = 0;
3579a6cb015SBarry Smith   mfctx->ops->printhelp      = 0;
3589a6cb015SBarry Smith   mfctx->ops->setfromoptions = 0;
3599a6cb015SBarry Smith   mfctx->hctx                = 0;
3609a6cb015SBarry Smith 
361a4d4d686SBarry Smith   ierr = VecDuplicate(x,&mfctx->w); CHKERRQ(ierr);
362a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm); CHKERRQ(ierr);
363a4d4d686SBarry Smith   ierr = VecGetSize(x,&n); CHKERRQ(ierr);
364a4d4d686SBarry Smith   ierr = VecGetLocalSize(x,&nloc); CHKERRQ(ierr);
365ffa0fd9eSBarry Smith   ierr = MatCreateShell(comm,nloc,nloc,n,n,mfctx,J); CHKERRQ(ierr);
366*5a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESMFMult_Private);CHKERRQ(ierr);
367*5a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESMFDestroy_Private);CHKERRQ(ierr);
368*5a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESMFView_Private); CHKERRQ(ierr);
369*5a655dc6SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESMFAssemblyEnd_Private);CHKERRQ(ierr);
370a4d4d686SBarry Smith   PLogObjectParent(*J,mfctx->w);
371a4d4d686SBarry Smith   PLogObjectParent(snes,*J);
3729a6cb015SBarry Smith 
3739a6cb015SBarry Smith   mfctx->mat = *J;
3749a6cb015SBarry Smith 
3759a6cb015SBarry Smith 
3769a6cb015SBarry Smith   PetscFunctionReturn(0);
3779a6cb015SBarry Smith }
3789a6cb015SBarry Smith 
3799a6cb015SBarry Smith #undef __FUNC__
380*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFromOptions"
3819a6cb015SBarry Smith /*@
382*5a655dc6SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
3839a6cb015SBarry Smith      parameter.
3849a6cb015SBarry Smith 
3859a6cb015SBarry Smith    Collective on Mat
3869a6cb015SBarry Smith 
3879a6cb015SBarry Smith    Input Parameters:
388*5a655dc6SBarry Smith .   mat - the matrix obtained with MatCreateSNESMF()
389*5a655dc6SBarry Smith 
390*5a655dc6SBarry Smith       Options Database Keys:
391*5a655dc6SBarry Smith +   -snes_mf_type - <default,wp>
392*5a655dc6SBarry Smith -   -snes_mf_err - square root of estimated relative error in function evaluation
3939a6cb015SBarry Smith 
39415091d37SBarry Smith    Level: advanced
39515091d37SBarry Smith 
3969a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters
3979a6cb015SBarry Smith 
398*5a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
399*5a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
4009a6cb015SBarry Smith @*/
401*5a655dc6SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
4029a6cb015SBarry Smith {
403*5a655dc6SBarry Smith   MatSNESMFCtx mfctx;
4049a6cb015SBarry Smith   int            ierr,flg;
4059a6cb015SBarry Smith   char           ftype[256],p[64];
4069a6cb015SBarry Smith 
4079a6cb015SBarry Smith   PetscFunctionBegin;
4089a6cb015SBarry Smith   ierr = MatShellGetContext(mat,(void **)&mfctx); CHKERRQ(ierr);
4099a6cb015SBarry Smith   if (mfctx) {
4109a6cb015SBarry Smith     /* allow user to set the type */
4119a6cb015SBarry Smith     ierr = OptionsGetString(mfctx->snes->prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr);
4129a6cb015SBarry Smith     if (flg) {
413*5a655dc6SBarry Smith       ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
4149a6cb015SBarry Smith     }
4159a6cb015SBarry Smith 
4169a6cb015SBarry Smith     ierr = OptionsGetDouble(mfctx->snes->prefix,"-snes_mf_err",&mfctx->error_rel,&flg);CHKERRQ(ierr);
4179a6cb015SBarry Smith     if (mfctx->ops->setfromoptions) {
4189a6cb015SBarry Smith       ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
4199a6cb015SBarry Smith     }
4209a6cb015SBarry Smith 
4219a6cb015SBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
4229a6cb015SBarry Smith     PetscStrcpy(p,"-");
4239a6cb015SBarry Smith     if (mfctx->snes->prefix) PetscStrcat(p,mfctx->snes->prefix);
4249a6cb015SBarry Smith     if (flg) {
4259a6cb015SBarry Smith       (*PetscHelpPrintf)(mfctx->snes->comm,"   %ssnes_mf_err <err>: set sqrt rel error in function (default %g)\n",p,mfctx->error_rel);
4269a6cb015SBarry Smith       if (mfctx->ops->printhelp) {
4279a6cb015SBarry Smith         (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr);
4289a6cb015SBarry Smith       }
4299a6cb015SBarry Smith     }
4309a6cb015SBarry Smith   }
431a4d4d686SBarry Smith   PetscFunctionReturn(0);
432a4d4d686SBarry Smith }
433a4d4d686SBarry Smith 
434a4d4d686SBarry Smith #undef __FUNC__
435*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFGetH"
436a4d4d686SBarry Smith /*@
437*5a655dc6SBarry Smith    MatSNESMFGetH - Gets the last h that was used as the differencing
438a4d4d686SBarry Smith      parameter.
439a4d4d686SBarry Smith 
440a4d4d686SBarry Smith    Not Collective
441a4d4d686SBarry Smith 
442a4d4d686SBarry Smith    Input Parameters:
443*5a655dc6SBarry Smith .   mat - the matrix obtained with MatCreateSNESMF()
444a4d4d686SBarry Smith 
445a4d4d686SBarry Smith    Output Paramter:
446a4d4d686SBarry Smith .  h - the differencing step size
447a4d4d686SBarry Smith 
44815091d37SBarry Smith    Level: advanced
44915091d37SBarry Smith 
450a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
451a4d4d686SBarry Smith 
452*5a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
453*5a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
454a4d4d686SBarry Smith @*/
455*5a655dc6SBarry Smith int MatSNESMFGetH(Mat mat,Scalar *h)
456a4d4d686SBarry Smith {
457*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
458a4d4d686SBarry Smith   int            ierr;
459a4d4d686SBarry Smith 
460a4d4d686SBarry Smith   PetscFunctionBegin;
461a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
462a4d4d686SBarry Smith   if (ctx) {
463a4d4d686SBarry Smith     *h = ctx->currenth;
464a4d4d686SBarry Smith   }
465a4d4d686SBarry Smith   PetscFunctionReturn(0);
466a4d4d686SBarry Smith }
467a4d4d686SBarry Smith 
468a4d4d686SBarry Smith #undef __FUNC__
469*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFKSPMonitor"
470a4d4d686SBarry Smith /*
471*5a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
472a4d4d686SBarry Smith       SNES matrix free routines. Prints the h differencing parameter used at each
473a4d4d686SBarry Smith       timestep.
474a4d4d686SBarry Smith 
475a4d4d686SBarry Smith */
476*5a655dc6SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,double rnorm,void *dummy)
477a4d4d686SBarry Smith {
478a4d4d686SBarry Smith   PC             pc;
479*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
480a4d4d686SBarry Smith   int            ierr;
481a4d4d686SBarry Smith   Mat            mat;
482a4d4d686SBarry Smith   MPI_Comm       comm;
483a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
484a4d4d686SBarry Smith 
485a4d4d686SBarry Smith   PetscFunctionBegin;
486a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
487a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc); CHKERRQ(ierr);
488a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
489a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr);
490a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
4919a6cb015SBarry Smith   if (!ctx) {
4929a6cb015SBarry Smith     SETERRQ(1,1,"Matrix is not a matrix free shell matrix");
4939a6cb015SBarry Smith   }
494a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
495a4d4d686SBarry Smith #if defined(USE_PETSC_COMPLEX)
496a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
497a4d4d686SBarry Smith                 PetscReal(ctx->currenth),PetscImaginary(ctx->currenth));
498a4d4d686SBarry Smith #else
499a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);
500a4d4d686SBarry Smith #endif
501a4d4d686SBarry Smith   } else {
502a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);
503a4d4d686SBarry Smith   }
504a4d4d686SBarry Smith   PetscFunctionReturn(0);
505a4d4d686SBarry Smith }
506a4d4d686SBarry Smith 
507a4d4d686SBarry Smith #undef __FUNC__
508*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetFunctionError"
509a4d4d686SBarry Smith /*@
510*5a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
511a4d4d686SBarry Smith    matrix-vector products using finite differences.
512a4d4d686SBarry Smith 
513a4d4d686SBarry Smith    Collective on Mat
514a4d4d686SBarry Smith 
515a4d4d686SBarry Smith    Input Parameters:
516*5a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
5179a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
518a4d4d686SBarry Smith                the relative error in the function evaluations)
519a4d4d686SBarry Smith 
52015091d37SBarry Smith    Options Database Keys:
52115091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
52215091d37SBarry Smith 
52315091d37SBarry Smith    Level: advanced
52415091d37SBarry Smith 
525a4d4d686SBarry Smith    Notes:
526a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
527a4d4d686SBarry Smith .vb
528a4d4d686SBarry Smith      J(u)*a = [J(u+h*a) - J(u)]/h where
529a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
530a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
531a4d4d686SBarry Smith .ve
532a4d4d686SBarry Smith 
533a4d4d686SBarry Smith 
534a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
535a4d4d686SBarry Smith 
536*5a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
537*5a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
538*5a655dc6SBarry Smith           MatSNESMFKSPMonitor()
539a4d4d686SBarry Smith @*/
540*5a655dc6SBarry Smith int MatSNESMFSetFunctionError(Mat mat,double error)
541a4d4d686SBarry Smith {
542*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
543a4d4d686SBarry Smith   int            ierr;
544a4d4d686SBarry Smith 
545a4d4d686SBarry Smith   PetscFunctionBegin;
546a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
547a4d4d686SBarry Smith   if (ctx) {
548a4d4d686SBarry Smith     if (error != PETSC_DEFAULT) ctx->error_rel = error;
549a4d4d686SBarry Smith   }
550a4d4d686SBarry Smith   PetscFunctionReturn(0);
551a4d4d686SBarry Smith }
552a4d4d686SBarry Smith 
553a4d4d686SBarry Smith #undef __FUNC__
554*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFAddNullSpace"
555a4d4d686SBarry Smith /*@
556*5a655dc6SBarry Smith    MatSNESMFAddNullSpace - Provides a null space that
557a4d4d686SBarry Smith    an operator is supposed to have.  Since roundoff will create a
558a4d4d686SBarry Smith    small component in the null space, if you know the null space
559a4d4d686SBarry Smith    you may have it automatically removed.
560a4d4d686SBarry Smith 
561a4d4d686SBarry Smith    Collective on Mat
562a4d4d686SBarry Smith 
563a4d4d686SBarry Smith    Input Parameters:
564a4d4d686SBarry Smith +  J - the matrix-free matrix context
565a4d4d686SBarry Smith .  has_cnst - PETSC_TRUE or PETSC_FALSE, indicating if null space has constants
566a4d4d686SBarry Smith .  n - number of vectors (excluding constant vector) in null space
567a4d4d686SBarry Smith -  vecs - the vectors that span the null space (excluding the constant vector);
568a4d4d686SBarry Smith           these vectors must be orthonormal
569a4d4d686SBarry Smith 
57015091d37SBarry Smith    Level: advanced
57115091d37SBarry Smith 
572a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
573a4d4d686SBarry Smith 
574*5a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
575*5a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
576*5a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
577a4d4d686SBarry Smith 
578a4d4d686SBarry Smith @*/
579*5a655dc6SBarry Smith int MatSNESMFAddNullSpace(Mat J,int has_cnst,int n,Vec *vecs)
580a4d4d686SBarry Smith {
581a4d4d686SBarry Smith   int            ierr;
582*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
583a4d4d686SBarry Smith   MPI_Comm       comm;
584a4d4d686SBarry Smith 
585a4d4d686SBarry Smith   PetscFunctionBegin;
5862d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
587a4d4d686SBarry Smith 
588a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
589a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
590a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
591a4d4d686SBarry Smith   ierr = PCNullSpaceCreate(comm,has_cnst,n,vecs,&ctx->sp); CHKERRQ(ierr);
592a4d4d686SBarry Smith   PetscFunctionReturn(0);
593a4d4d686SBarry Smith }
594a4d4d686SBarry Smith 
595a4d4d686SBarry Smith #undef __FUNC__
596*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFSetHHistory"
597a4d4d686SBarry Smith /*@
598*5a655dc6SBarry Smith    MatSNESMFSetHHistory - Sets an array to collect a history
599a4d4d686SBarry Smith       of the differencing values h computed for the matrix free product
600a4d4d686SBarry Smith 
601a4d4d686SBarry Smith    Collective on Mat
602a4d4d686SBarry Smith 
603a4d4d686SBarry Smith    Input Parameters:
604a4d4d686SBarry Smith +  J - the matrix-free matrix context
605a4d4d686SBarry Smith .  histroy - space to hold the h history
606a4d4d686SBarry Smith -  nhistory - number of entries in history, if more h are generated than
607a4d4d686SBarry Smith               nhistory the later ones are discarded
608a4d4d686SBarry Smith 
60915091d37SBarry Smith    Level: advanced
61015091d37SBarry Smith 
611a4d4d686SBarry Smith    Notes:
612*5a655dc6SBarry Smith     Use MatSNESMFResetHHistory() to reset the history counter
613a4d4d686SBarry Smith     and collect a new batch of h.
614a4d4d686SBarry Smith 
615a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
616a4d4d686SBarry Smith 
617*5a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
618*5a655dc6SBarry Smith           MatSNESMFResetHHistory(),
619*5a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
620a4d4d686SBarry Smith 
621a4d4d686SBarry Smith @*/
622*5a655dc6SBarry Smith int MatSNESMFSetHHistory(Mat J,Scalar *history,int nhistory)
623a4d4d686SBarry Smith {
624a4d4d686SBarry Smith   int            ierr;
625*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
626a4d4d686SBarry Smith 
627a4d4d686SBarry Smith   PetscFunctionBegin;
628a4d4d686SBarry Smith 
629a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
630a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
631a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
632a4d4d686SBarry Smith   ctx->historyh    = history;
633a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
634a4d4d686SBarry Smith   ctx->currenth    = 0;
635a4d4d686SBarry Smith 
636a4d4d686SBarry Smith   PetscFunctionReturn(0);
637a4d4d686SBarry Smith }
638a4d4d686SBarry Smith 
639a4d4d686SBarry Smith #undef __FUNC__
640*5a655dc6SBarry Smith #define __FUNC__ "MatSNESMFResetHHistory"
641a4d4d686SBarry Smith /*@
642*5a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
643a4d4d686SBarry Smith       collecting a new set of differencing histories.
644a4d4d686SBarry Smith 
645a4d4d686SBarry Smith    Collective on Mat
646a4d4d686SBarry Smith 
647a4d4d686SBarry Smith    Input Parameters:
648a4d4d686SBarry Smith .  J - the matrix-free matrix context
649a4d4d686SBarry Smith 
65015091d37SBarry Smith    Level: advanced
65115091d37SBarry Smith 
652a4d4d686SBarry Smith    Notes:
653*5a655dc6SBarry Smith     Use MatSNESMFSetHHistory() to create the original history counter
654a4d4d686SBarry Smith 
655a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
656a4d4d686SBarry Smith 
657*5a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
658*5a655dc6SBarry Smith           MatSNESMFSetHHistory(),
659*5a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
660a4d4d686SBarry Smith 
661a4d4d686SBarry Smith @*/
662*5a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
663a4d4d686SBarry Smith {
664a4d4d686SBarry Smith   int            ierr;
665*5a655dc6SBarry Smith   MatSNESMFCtx ctx;
666a4d4d686SBarry Smith 
667a4d4d686SBarry Smith   PetscFunctionBegin;
668a4d4d686SBarry Smith 
669a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
670a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
671a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
672be726c96SBarry Smith   ctx->ncurrenth    = 0;
673a4d4d686SBarry Smith 
674a4d4d686SBarry Smith   PetscFunctionReturn(0);
675a4d4d686SBarry Smith }
676a4d4d686SBarry Smith 
677