xref: /petsc/src/snes/mf/snesmfj.c (revision 15091d3721b14bd18f7efa625bb8738e103dca31)
19a6cb015SBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*15091d37SBarry Smith static char vcid[] = "$Id: snesmfj.c,v 1.79 1999/03/07 17:29:26 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 
99a6cb015SBarry Smith FList MatSNESFDMFList              = 0;
109a6cb015SBarry Smith int   MatSNESFDMFRegisterAllCalled = 0;
11a4d4d686SBarry Smith 
1239e2f89bSBarry Smith 
135615d1e5SSatish Balay #undef __FUNC__
14a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFSetType"
159a6cb015SBarry Smith /*@
169a6cb015SBarry Smith       MatSNESFDMFSetType - 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:
209a6cb015SBarry Smith +     mat - the matrix free matrix created via MatCreateSNESFDMF()
219a6cb015SBarry Smith -     ftype - the type requested
229a6cb015SBarry Smith 
23*15091d37SBarry Smith    Level: advanced
24*15091d37SBarry Smith 
259a6cb015SBarry Smith .seealso: MatCreateSNESFDMF(), MatSNESFDMFRegister()
269a6cb015SBarry Smith @*/
279a6cb015SBarry Smith int MatSNESFDMFSetType(Mat mat,char *ftype)
28b9fa9cd0SBarry Smith {
299a6cb015SBarry Smith   int            ierr, (*r)(MatSNESFDMFCtx);
30a4d4d686SBarry Smith   MatSNESFDMFCtx 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 */
449a6cb015SBarry Smith   if (!MatSNESFDMFRegisterAllCalled) {ierr = MatSNESFDMFRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
459a6cb015SBarry Smith 
469a6cb015SBarry Smith   ierr =  FListFind(ctx->comm, MatSNESFDMFList, ftype,(int (**)(void *)) &r );CHKERRQ(ierr);
479a6cb015SBarry Smith 
489a6cb015SBarry Smith   if (!r) SETERRQ(1,1,"Unknown MatSNESFDMF 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
579a6cb015SBarry Smith    MatSNESFDMFRegister - Adds a method to the MatSNESFDMF registry
589a6cb015SBarry Smith 
599a6cb015SBarry Smith    Synopsis:
609a6cb015SBarry Smith    MatSNESFDMFRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESFDMF))
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 
70*15091d37SBarry Smith    Level: developer
71*15091d37SBarry Smith 
729a6cb015SBarry Smith    Notes:
739a6cb015SBarry Smith    MatSNESFDMFRegister() 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
809a6cb015SBarry Smith    MatSNESFDMFRegister("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
859a6cb015SBarry Smith $     MatSNESFDMFSetType(mfctx,"my_h")
869a6cb015SBarry Smith    or at runtime via the option
879a6cb015SBarry Smith $     -snes_mf_type my_h
889a6cb015SBarry Smith 
899a6cb015SBarry Smith .keywords: MatSNESFDMF, register
909a6cb015SBarry Smith 
919a6cb015SBarry Smith .seealso: MatSNESFDMFRegisterAll(), MatSNESFDMFRegisterDestroy()
929a6cb015SBarry Smith M*/
939a6cb015SBarry Smith 
949a6cb015SBarry Smith #undef __FUNC__
959a6cb015SBarry Smith #define __FUNC__ "MatSNESFDMFRegister_Private"
969a6cb015SBarry Smith int MatSNESFDMFRegister_Private(char *sname,char *path,char *name,int (*function)(MatSNESFDMFCtx))
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);
1039a6cb015SBarry Smith   ierr = FListAdd_Private(&MatSNESFDMFList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
1049a6cb015SBarry Smith   PetscFunctionReturn(0);
1059a6cb015SBarry Smith }
1069a6cb015SBarry Smith 
1079a6cb015SBarry Smith 
1089a6cb015SBarry Smith #undef __FUNC__
1099a6cb015SBarry Smith #define __FUNC__ "MatSNESFDMFRegisterDestroy"
1109a6cb015SBarry Smith /*@C
1119a6cb015SBarry Smith    MatSNESFDMFRegisterDestroy - Frees the list of MatSNESFDMF methods that were
1129a6cb015SBarry Smith    registered by MatSNESFDMFRegister().
1139a6cb015SBarry Smith 
1149a6cb015SBarry Smith    Not Collective
1159a6cb015SBarry Smith 
116*15091d37SBarry Smith    Level: developer
117*15091d37SBarry Smith 
1189a6cb015SBarry Smith .keywords: MatSNESFDMF, register, destroy
1199a6cb015SBarry Smith 
1209a6cb015SBarry Smith .seealso: MatSNESFDMFRegister(), MatSNESFDMFRegisterAll()
1219a6cb015SBarry Smith @*/
1229a6cb015SBarry Smith int MatSNESFDMFRegisterDestroy(void)
1239a6cb015SBarry Smith {
1249a6cb015SBarry Smith   int ierr;
1259a6cb015SBarry Smith 
1269a6cb015SBarry Smith   PetscFunctionBegin;
1279a6cb015SBarry Smith   if (MatSNESFDMFList) {
1289a6cb015SBarry Smith     ierr = FListDestroy( MatSNESFDMFList );CHKERRQ(ierr);
1299a6cb015SBarry Smith     MatSNESFDMFList = 0;
1309a6cb015SBarry Smith   }
1319a6cb015SBarry Smith   MatSNESFDMFRegisterAllCalled = 0;
1329a6cb015SBarry Smith   PetscFunctionReturn(0);
1339a6cb015SBarry Smith }
1349a6cb015SBarry Smith 
1359a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
136a4d4d686SBarry Smith #undef __FUNC__
137a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFDestroy_Private"
138a4d4d686SBarry Smith int MatSNESFDMFDestroy_Private(Mat mat)
139a4d4d686SBarry Smith {
140a4d4d686SBarry Smith   int            ierr;
141a4d4d686SBarry Smith   MatSNESFDMFCtx 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__
154a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFView_Private"
15539e2f89bSBarry Smith /*
156a4d4d686SBarry Smith    MatSNESFDMFView_Private - Views matrix-free parameters.
1578f6e3e37SBarry Smith 
15839e2f89bSBarry Smith */
159a4d4d686SBarry Smith int MatSNESFDMFView_Private(Mat J,Viewer viewer)
160eb9086c3SLois Curfman McInnes {
161eb9086c3SLois Curfman McInnes   int            ierr;
162a4d4d686SBarry Smith   MatSNESFDMFCtx 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__
186be726c96SBarry Smith #define __FUNC__ "MatSNESFDMFAssemblyEnd_Private"
187be726c96SBarry Smith /*
188be726c96SBarry Smith    MatSNESFDMFAssemblyEnd_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
191be726c96SBarry Smith      MatSNESFDMFCreate_WP() to properly compute the || U|| only the first
192be726c96SBarry Smith      time in the linear solver rather than every time
193be726c96SBarry Smith 
194be726c96SBarry Smith */
195be726c96SBarry Smith int MatSNESFDMFAssemblyEnd_Private(Mat J)
196be726c96SBarry Smith {
197be726c96SBarry Smith   int            ierr;
198be726c96SBarry Smith 
199be726c96SBarry Smith   PetscFunctionBegin;
200be726c96SBarry Smith   ierr = MatSNESFDMFResetHHistory(J);CHKERRQ(ierr);
201be726c96SBarry Smith   PetscFunctionReturn(0);
202be726c96SBarry Smith }
203be726c96SBarry Smith 
204c481317fSBarry Smith 
2055615d1e5SSatish Balay #undef __FUNC__
206a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFMult_Private"
207eb9086c3SLois Curfman McInnes /*
208a4d4d686SBarry Smith   MatSNESFDMFMult_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 */
216a4d4d686SBarry Smith int MatSNESFDMFMult_Private(Mat mat,Vec a,Vec y)
21739e2f89bSBarry Smith {
218a4d4d686SBarry Smith   MatSNESFDMFCtx 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) {
2459a6cb015SBarry Smith     ierr = MatSNESFDMFSetType(mat,"default");CHKERRQ(ierr);
2469a6cb015SBarry Smith     ierr = MatSNESFDMFSetFromOptions(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__
277a4d4d686SBarry Smith #define __FUNC__ "MatCreateSNESFDMF"
278a4d4d686SBarry Smith /*@C
279a4d4d686SBarry Smith    MatCreateSNESFDMF - 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 
292*15091d37SBarry Smith    Level: advanced
293*15091d37SBarry 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 
3109a6cb015SBarry Smith    The user can set the error_rel via MatSNESFDMFSetFunctionError() and
3119a6cb015SBarry Smith    umin via MatSNESFDMFDefaultSetUmin()
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 
3249a6cb015SBarry Smith .seealso: MatDestroy(), MatSNESFDMFSetFunctionError(), MatSNESFDMFDefaultSetUmin()
325a4d4d686SBarry Smith           MatSNESFDMFSetHHistory(), MatSNESFDMFResetHHistory(),
3269a6cb015SBarry Smith           MatSNESFDMFGetH(),MatSNESFDMFKSPMonitor(), MatSNESFDMFRegister()
327a4d4d686SBarry Smith 
328a4d4d686SBarry Smith @*/
329a4d4d686SBarry Smith int MatCreateSNESFDMF(SNES snes,Vec x, Mat *J)
330a4d4d686SBarry Smith {
331a4d4d686SBarry Smith   MPI_Comm       comm;
332a4d4d686SBarry Smith   MatSNESFDMFCtx mfctx;
3339a6cb015SBarry Smith   int            n, nloc, ierr;
334a4d4d686SBarry Smith 
335a4d4d686SBarry Smith   PetscFunctionBegin;
336a4d4d686SBarry Smith   mfctx = (MatSNESFDMFCtx) PetscMalloc(sizeof(struct _p_MatSNESFDMFCtx)); CHKPTRQ(mfctx);
337a4d4d686SBarry Smith   PLogObjectMemory(snes,sizeof(MatSNESFDMFCtx));
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
3509a6cb015SBarry Smith      a later call with MatSNESFDMFSetType() or if that is not called
3519a6cb015SBarry Smith      then it will default in the first use of MatSNESFDMFMult_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);
366a4d4d686SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_MULT,(void*)MatSNESFDMFMult_Private);CHKERRQ(ierr);
367a4d4d686SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_DESTROY,(void *)MatSNESFDMFDestroy_Private);CHKERRQ(ierr);
368a4d4d686SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_VIEW,(void *)MatSNESFDMFView_Private); CHKERRQ(ierr);
369be726c96SBarry Smith   ierr = MatShellSetOperation(*J,MATOP_ASSEMBLY_END,(void *)MatSNESFDMFAssemblyEnd_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__
3809a6cb015SBarry Smith #define __FUNC__ "MatSNESFDMFGetH"
3819a6cb015SBarry Smith /*@
3829a6cb015SBarry Smith    MatSNESFDMFSetFromOptions - Sets the MatSNESFDMF options from the command line
3839a6cb015SBarry Smith      parameter.
3849a6cb015SBarry Smith 
3859a6cb015SBarry Smith    Collective on Mat
3869a6cb015SBarry Smith 
3879a6cb015SBarry Smith    Input Parameters:
3889a6cb015SBarry Smith .   mat - the matrix obtained with MatCreateSNESFDMF()
3899a6cb015SBarry Smith 
390*15091d37SBarry Smith    Level: advanced
391*15091d37SBarry Smith 
3929a6cb015SBarry Smith .keywords: SNES, matrix-free, parameters
3939a6cb015SBarry Smith 
3949a6cb015SBarry Smith .seealso: MatCreateSNESFDMF(),MatSNESFDMFSetHHistory(),
3959a6cb015SBarry Smith           MatSNESFDMFResetHHistory(),MatSNESFDMFKSPMonitor()
3969a6cb015SBarry Smith @*/
3979a6cb015SBarry Smith int MatSNESFDMFSetFromOptions(Mat mat)
3989a6cb015SBarry Smith {
3999a6cb015SBarry Smith   MatSNESFDMFCtx mfctx;
4009a6cb015SBarry Smith   int            ierr,flg;
4019a6cb015SBarry Smith   char           ftype[256],p[64];
4029a6cb015SBarry Smith 
4039a6cb015SBarry Smith   PetscFunctionBegin;
4049a6cb015SBarry Smith   ierr = MatShellGetContext(mat,(void **)&mfctx); CHKERRQ(ierr);
4059a6cb015SBarry Smith   if (mfctx) {
4069a6cb015SBarry Smith     /* allow user to set the type */
4079a6cb015SBarry Smith     ierr = OptionsGetString(mfctx->snes->prefix,"-snes_mf_type",ftype,256,&flg);CHKERRQ(ierr);
4089a6cb015SBarry Smith     if (flg) {
4099a6cb015SBarry Smith       ierr = MatSNESFDMFSetType(mat,ftype);CHKERRQ(ierr);
4109a6cb015SBarry Smith     }
4119a6cb015SBarry Smith 
4129a6cb015SBarry Smith     ierr = OptionsGetDouble(mfctx->snes->prefix,"-snes_mf_err",&mfctx->error_rel,&flg);CHKERRQ(ierr);
4139a6cb015SBarry Smith     if (mfctx->ops->setfromoptions) {
4149a6cb015SBarry Smith       ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
4159a6cb015SBarry Smith     }
4169a6cb015SBarry Smith 
4179a6cb015SBarry Smith     ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
4189a6cb015SBarry Smith     PetscStrcpy(p,"-");
4199a6cb015SBarry Smith     if (mfctx->snes->prefix) PetscStrcat(p,mfctx->snes->prefix);
4209a6cb015SBarry Smith     if (flg) {
4219a6cb015SBarry Smith       (*PetscHelpPrintf)(mfctx->snes->comm,"   %ssnes_mf_err <err>: set sqrt rel error in function (default %g)\n",p,mfctx->error_rel);
4229a6cb015SBarry Smith       if (mfctx->ops->printhelp) {
4239a6cb015SBarry Smith         (*mfctx->ops->printhelp)(mfctx);CHKERRQ(ierr);
4249a6cb015SBarry Smith       }
4259a6cb015SBarry Smith     }
4269a6cb015SBarry Smith   }
427a4d4d686SBarry Smith   PetscFunctionReturn(0);
428a4d4d686SBarry Smith }
429a4d4d686SBarry Smith 
430a4d4d686SBarry Smith #undef __FUNC__
431a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFGetH"
432a4d4d686SBarry Smith /*@
433a4d4d686SBarry Smith    MatSNESFDMFGetH - Gets the last h that was used as the differencing
434a4d4d686SBarry Smith      parameter.
435a4d4d686SBarry Smith 
436a4d4d686SBarry Smith    Not Collective
437a4d4d686SBarry Smith 
438a4d4d686SBarry Smith    Input Parameters:
439a4d4d686SBarry Smith .   mat - the matrix obtained with MatCreateSNESFDMF()
440a4d4d686SBarry Smith 
441a4d4d686SBarry Smith    Output Paramter:
442a4d4d686SBarry Smith .  h - the differencing step size
443a4d4d686SBarry Smith 
444*15091d37SBarry Smith    Level: advanced
445*15091d37SBarry Smith 
446a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
447a4d4d686SBarry Smith 
448a4d4d686SBarry Smith .seealso: MatCreateSNESFDMF(),MatSNESFDMFSetHHistory(),
449a4d4d686SBarry Smith           MatSNESFDMFResetHHistory(),MatSNESFDMFKSPMonitor()
450a4d4d686SBarry Smith @*/
451a4d4d686SBarry Smith int MatSNESFDMFGetH(Mat mat,Scalar *h)
452a4d4d686SBarry Smith {
453a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
454a4d4d686SBarry Smith   int            ierr;
455a4d4d686SBarry Smith 
456a4d4d686SBarry Smith   PetscFunctionBegin;
457a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
458a4d4d686SBarry Smith   if (ctx) {
459a4d4d686SBarry Smith     *h = ctx->currenth;
460a4d4d686SBarry Smith   }
461a4d4d686SBarry Smith   PetscFunctionReturn(0);
462a4d4d686SBarry Smith }
463a4d4d686SBarry Smith 
464a4d4d686SBarry Smith #undef __FUNC__
465a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFKSPMonitor"
466a4d4d686SBarry Smith /*
467a4d4d686SBarry Smith    MatSNESFDMFKSPMonitor - A KSP monitor for use with the default PETSc
468a4d4d686SBarry Smith       SNES matrix free routines. Prints the h differencing parameter used at each
469a4d4d686SBarry Smith       timestep.
470a4d4d686SBarry Smith 
471a4d4d686SBarry Smith */
472a4d4d686SBarry Smith int MatSNESFDMFKSPMonitor(KSP ksp,int n,double rnorm,void *dummy)
473a4d4d686SBarry Smith {
474a4d4d686SBarry Smith   PC             pc;
475a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
476a4d4d686SBarry Smith   int            ierr;
477a4d4d686SBarry Smith   Mat            mat;
478a4d4d686SBarry Smith   MPI_Comm       comm;
479a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
480a4d4d686SBarry Smith 
481a4d4d686SBarry Smith   PetscFunctionBegin;
482a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
483a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc); CHKERRQ(ierr);
484a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
485a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr);
486a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
4879a6cb015SBarry Smith   if (!ctx) {
4889a6cb015SBarry Smith     SETERRQ(1,1,"Matrix is not a matrix free shell matrix");
4899a6cb015SBarry Smith   }
490a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
491a4d4d686SBarry Smith #if defined(USE_PETSC_COMPLEX)
492a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
493a4d4d686SBarry Smith                 PetscReal(ctx->currenth),PetscImaginary(ctx->currenth));
494a4d4d686SBarry Smith #else
495a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);
496a4d4d686SBarry Smith #endif
497a4d4d686SBarry Smith   } else {
498a4d4d686SBarry Smith     PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);
499a4d4d686SBarry Smith   }
500a4d4d686SBarry Smith   PetscFunctionReturn(0);
501a4d4d686SBarry Smith }
502a4d4d686SBarry Smith 
503a4d4d686SBarry Smith #undef __FUNC__
5049a6cb015SBarry Smith #define __FUNC__ "MatSNESFDMFSetFunctionError"
505a4d4d686SBarry Smith /*@
5069a6cb015SBarry Smith    MatSNESFDMFSetFunctionError - Sets the error_rel for the approximation of
507a4d4d686SBarry Smith    matrix-vector products using finite differences.
508a4d4d686SBarry Smith 
509a4d4d686SBarry Smith    Collective on Mat
510a4d4d686SBarry Smith 
511a4d4d686SBarry Smith    Input Parameters:
512a4d4d686SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESFDMF()
5139a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
514a4d4d686SBarry Smith                the relative error in the function evaluations)
515a4d4d686SBarry Smith 
516*15091d37SBarry Smith    Options Database Keys:
517*15091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
518*15091d37SBarry Smith 
519*15091d37SBarry Smith    Level: advanced
520*15091d37SBarry Smith 
521a4d4d686SBarry Smith    Notes:
522a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
523a4d4d686SBarry Smith .vb
524a4d4d686SBarry Smith      J(u)*a = [J(u+h*a) - J(u)]/h where
525a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
526a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
527a4d4d686SBarry Smith .ve
528a4d4d686SBarry Smith 
529a4d4d686SBarry Smith 
530a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
531a4d4d686SBarry Smith 
532a4d4d686SBarry Smith .seealso: MatCreateSNESFDMF(),MatSNESFDMFGetH(),
533a4d4d686SBarry Smith           MatSNESFDMFSetHHistory(), MatSNESFDMFResetHHistory(),
534a4d4d686SBarry Smith           MatSNESFDMFKSPMonitor()
535a4d4d686SBarry Smith @*/
5369a6cb015SBarry Smith int MatSNESFDMFSetFunctionError(Mat mat,double error)
537a4d4d686SBarry Smith {
538a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
539a4d4d686SBarry Smith   int            ierr;
540a4d4d686SBarry Smith 
541a4d4d686SBarry Smith   PetscFunctionBegin;
542a4d4d686SBarry Smith   ierr = MatShellGetContext(mat,(void **)&ctx); CHKERRQ(ierr);
543a4d4d686SBarry Smith   if (ctx) {
544a4d4d686SBarry Smith     if (error != PETSC_DEFAULT) ctx->error_rel = error;
545a4d4d686SBarry Smith   }
546a4d4d686SBarry Smith   PetscFunctionReturn(0);
547a4d4d686SBarry Smith }
548a4d4d686SBarry Smith 
549a4d4d686SBarry Smith #undef __FUNC__
550a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFAddNullSpace"
551a4d4d686SBarry Smith /*@
552a4d4d686SBarry Smith    MatSNESFDMFAddNullSpace - Provides a null space that
553a4d4d686SBarry Smith    an operator is supposed to have.  Since roundoff will create a
554a4d4d686SBarry Smith    small component in the null space, if you know the null space
555a4d4d686SBarry Smith    you may have it automatically removed.
556a4d4d686SBarry Smith 
557a4d4d686SBarry Smith    Collective on Mat
558a4d4d686SBarry Smith 
559a4d4d686SBarry Smith    Input Parameters:
560a4d4d686SBarry Smith +  J - the matrix-free matrix context
561a4d4d686SBarry Smith .  has_cnst - PETSC_TRUE or PETSC_FALSE, indicating if null space has constants
562a4d4d686SBarry Smith .  n - number of vectors (excluding constant vector) in null space
563a4d4d686SBarry Smith -  vecs - the vectors that span the null space (excluding the constant vector);
564a4d4d686SBarry Smith           these vectors must be orthonormal
565a4d4d686SBarry Smith 
566*15091d37SBarry Smith    Level: advanced
567*15091d37SBarry Smith 
568a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
569a4d4d686SBarry Smith 
570a4d4d686SBarry Smith .seealso: MatSNESFDMFGetH(), MatCreateSNESFDMF(),
571a4d4d686SBarry Smith           MatSNESFDMFSetHHistory(), MatSNESFDMFResetHHistory(),
5729a6cb015SBarry Smith           MatSNESFDMFKSPMonitor(), MatSNESFDMFErrorRel()
573a4d4d686SBarry Smith 
574a4d4d686SBarry Smith @*/
575a4d4d686SBarry Smith int MatSNESFDMFAddNullSpace(Mat J,int has_cnst,int n,Vec *vecs)
576a4d4d686SBarry Smith {
577a4d4d686SBarry Smith   int            ierr;
578a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
579a4d4d686SBarry Smith   MPI_Comm       comm;
580a4d4d686SBarry Smith 
581a4d4d686SBarry Smith   PetscFunctionBegin;
5822d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
583a4d4d686SBarry Smith 
584a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
585a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
586a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
587a4d4d686SBarry Smith   ierr = PCNullSpaceCreate(comm,has_cnst,n,vecs,&ctx->sp); CHKERRQ(ierr);
588a4d4d686SBarry Smith   PetscFunctionReturn(0);
589a4d4d686SBarry Smith }
590a4d4d686SBarry Smith 
591a4d4d686SBarry Smith #undef __FUNC__
592a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFSetHHistory"
593a4d4d686SBarry Smith /*@
594a4d4d686SBarry Smith    MatSNESFDMFSetHHistory - Sets an array to collect a history
595a4d4d686SBarry Smith       of the differencing values h computed for the matrix free product
596a4d4d686SBarry Smith 
597a4d4d686SBarry Smith    Collective on Mat
598a4d4d686SBarry Smith 
599a4d4d686SBarry Smith    Input Parameters:
600a4d4d686SBarry Smith +  J - the matrix-free matrix context
601a4d4d686SBarry Smith .  histroy - space to hold the h history
602a4d4d686SBarry Smith -  nhistory - number of entries in history, if more h are generated than
603a4d4d686SBarry Smith               nhistory the later ones are discarded
604a4d4d686SBarry Smith 
605*15091d37SBarry Smith    Level: advanced
606*15091d37SBarry Smith 
607a4d4d686SBarry Smith    Notes:
608a4d4d686SBarry Smith     Use MatSNESFDMFResetHHistory() to reset the history counter
609a4d4d686SBarry Smith     and collect a new batch of h.
610a4d4d686SBarry Smith 
611a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
612a4d4d686SBarry Smith 
613a4d4d686SBarry Smith .seealso: MatSNESFDMFGetH(), MatCreateSNESFDMF(),
614a4d4d686SBarry Smith           MatSNESFDMFResetHHistory(),
6159a6cb015SBarry Smith           MatSNESFDMFKSPMonitor(), MatSNESFDMFSetFunctionError()
616a4d4d686SBarry Smith 
617a4d4d686SBarry Smith @*/
618a4d4d686SBarry Smith int MatSNESFDMFSetHHistory(Mat J,Scalar *history,int nhistory)
619a4d4d686SBarry Smith {
620a4d4d686SBarry Smith   int            ierr;
621a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
622a4d4d686SBarry Smith 
623a4d4d686SBarry Smith   PetscFunctionBegin;
624a4d4d686SBarry Smith 
625a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
626a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
627a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
628a4d4d686SBarry Smith   ctx->historyh    = history;
629a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
630a4d4d686SBarry Smith   ctx->currenth    = 0;
631a4d4d686SBarry Smith 
632a4d4d686SBarry Smith   PetscFunctionReturn(0);
633a4d4d686SBarry Smith }
634a4d4d686SBarry Smith 
635a4d4d686SBarry Smith #undef __FUNC__
636a4d4d686SBarry Smith #define __FUNC__ "MatSNESFDMFResetHHistory"
637a4d4d686SBarry Smith /*@
638a4d4d686SBarry Smith    MatSNESFDMFResetHHistory - Resets the counter to zero to begin
639a4d4d686SBarry Smith       collecting a new set of differencing histories.
640a4d4d686SBarry Smith 
641a4d4d686SBarry Smith    Collective on Mat
642a4d4d686SBarry Smith 
643a4d4d686SBarry Smith    Input Parameters:
644a4d4d686SBarry Smith .  J - the matrix-free matrix context
645a4d4d686SBarry Smith 
646*15091d37SBarry Smith    Level: advanced
647*15091d37SBarry Smith 
648a4d4d686SBarry Smith    Notes:
649a4d4d686SBarry Smith     Use MatSNESFDMFSetHHistory() to create the original history counter
650a4d4d686SBarry Smith 
651a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
652a4d4d686SBarry Smith 
653a4d4d686SBarry Smith .seealso: MatSNESFDMFGetH(), MatCreateSNESFDMF(),
654a4d4d686SBarry Smith           MatSNESFDMFSetHHistory(),
6559a6cb015SBarry Smith           MatSNESFDMFKSPMonitor(), MatSNESFDMFSetFunctionError()
656a4d4d686SBarry Smith 
657a4d4d686SBarry Smith @*/
658a4d4d686SBarry Smith int MatSNESFDMFResetHHistory(Mat J)
659a4d4d686SBarry Smith {
660a4d4d686SBarry Smith   int            ierr;
661a4d4d686SBarry Smith   MatSNESFDMFCtx ctx;
662a4d4d686SBarry Smith 
663a4d4d686SBarry Smith   PetscFunctionBegin;
664a4d4d686SBarry Smith 
665a4d4d686SBarry Smith   ierr = MatShellGetContext(J,(void **)&ctx); CHKERRQ(ierr);
666a4d4d686SBarry Smith   /* no context indicates that it is not the "matrix free" matrix type */
667a4d4d686SBarry Smith   if (!ctx) PetscFunctionReturn(0);
668be726c96SBarry Smith   ctx->ncurrenth    = 0;
669a4d4d686SBarry Smith 
670a4d4d686SBarry Smith   PetscFunctionReturn(0);
671a4d4d686SBarry Smith }
672a4d4d686SBarry Smith 
673