xref: /petsc/src/snes/mf/snesmfj.c (revision 2740c1cae2c29d174e88bee2784e05021cbdaee1)
173f4d377SMatthew Knepley /*$Id: snesmfj.c,v 1.131 2001/09/05 18:45:40 bsmith Exp $*/
281e6777dSBarry Smith 
37e9d5209SBarry Smith #include "src/mat/matimpl.h"
4325e03aeSBarry Smith #include "src/snes/mf/snesmfj.h"   /*I  "petscsnes.h"   I*/
581e6777dSBarry Smith 
6b0a32e0cSBarry Smith PetscFList      MatSNESMPetscFList              = 0;
74c49b128SBarry Smith PetscTruth MatSNESMFRegisterAllCalled = PETSC_FALSE;
8a4d4d686SBarry Smith 
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 
65c5c390f1SBarry Smith EXTERN_C_BEGIN
6687828ca2SBarry Smith #undef __FUNCT__
6787828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD"
6887828ca2SBarry Smith int MatSNESMFSetFunctioniBase_FD(Mat mat,int (*func)(Vec,void *))
6987828ca2SBarry Smith {
7087828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
7187828ca2SBarry Smith 
7287828ca2SBarry Smith   PetscFunctionBegin;
7387828ca2SBarry Smith   ctx->funcisetbase = func;
7487828ca2SBarry Smith   PetscFunctionReturn(0);
7587828ca2SBarry Smith }
76c5c390f1SBarry Smith EXTERN_C_END
7787828ca2SBarry Smith 
78c5c390f1SBarry Smith EXTERN_C_BEGIN
7987828ca2SBarry Smith #undef __FUNCT__
8087828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD"
8187828ca2SBarry Smith int MatSNESMFSetFunctioni_FD(Mat mat,int (*funci)(int,Vec,PetscScalar*,void *))
8287828ca2SBarry Smith {
8387828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
8487828ca2SBarry Smith 
8587828ca2SBarry Smith   PetscFunctionBegin;
8687828ca2SBarry Smith   ctx->funci = funci;
8787828ca2SBarry Smith   PetscFunctionReturn(0);
8887828ca2SBarry Smith }
89c5c390f1SBarry Smith EXTERN_C_END
9087828ca2SBarry Smith 
919a6cb015SBarry Smith /*MC
92f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic - Adds a method to the MatSNESMF registry.
939a6cb015SBarry Smith 
949a6cb015SBarry Smith    Synopsis:
95fed8bd04SBarry Smith    int MatSNESMFRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(MatSNESMF))
969a6cb015SBarry Smith 
979a6cb015SBarry Smith    Not Collective
989a6cb015SBarry Smith 
999a6cb015SBarry Smith    Input Parameters:
1009a6cb015SBarry Smith +  name_solver - name of a new user-defined compute-h module
1019a6cb015SBarry Smith .  path - path (either absolute or relative) the library containing this solver
1029a6cb015SBarry Smith .  name_create - name of routine to create method context
1039a6cb015SBarry Smith -  routine_create - routine to create method context
1049a6cb015SBarry Smith 
10515091d37SBarry Smith    Level: developer
10615091d37SBarry Smith 
1079a6cb015SBarry Smith    Notes:
108f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic) may be called multiple times to add several user-defined solvers.
1099a6cb015SBarry Smith 
1109a6cb015SBarry Smith    If dynamic libraries are used, then the fourth input argument (routine_create)
1119a6cb015SBarry Smith    is ignored.
1129a6cb015SBarry Smith 
1139a6cb015SBarry Smith    Sample usage:
1149a6cb015SBarry Smith .vb
115f1af5d2fSBarry Smith    MatSNESMFRegisterDynamic"my_h",/home/username/my_lib/lib/libO/solaris/mylib.a,
1169a6cb015SBarry Smith                "MyHCreate",MyHCreate);
1179a6cb015SBarry Smith .ve
1189a6cb015SBarry Smith 
1199a6cb015SBarry Smith    Then, your solver can be chosen with the procedural interface via
1205a655dc6SBarry Smith $     MatSNESMFSetType(mfctx,"my_h")
1219a6cb015SBarry Smith    or at runtime via the option
1229a6cb015SBarry Smith $     -snes_mf_type my_h
1239a6cb015SBarry Smith 
1245a655dc6SBarry Smith .keywords: MatSNESMF, register
1259a6cb015SBarry Smith 
1265a655dc6SBarry Smith .seealso: MatSNESMFRegisterAll(), MatSNESMFRegisterDestroy()
1279a6cb015SBarry Smith M*/
1289a6cb015SBarry Smith 
1294a2ae208SSatish Balay #undef __FUNCT__
1304a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
131f1af5d2fSBarry Smith int MatSNESMFRegister(char *sname,char *path,char *name,int (*function)(MatSNESMFCtx))
1329a6cb015SBarry Smith {
1339a6cb015SBarry Smith   int ierr;
1349a6cb015SBarry Smith   char fullname[256];
1359a6cb015SBarry Smith 
1369a6cb015SBarry Smith   PetscFunctionBegin;
137b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
138c134de8dSSatish Balay   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
1399a6cb015SBarry Smith   PetscFunctionReturn(0);
1409a6cb015SBarry Smith }
1419a6cb015SBarry Smith 
1429a6cb015SBarry Smith 
1434a2ae208SSatish Balay #undef __FUNCT__
1444a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1459a6cb015SBarry Smith /*@C
1465a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
147f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1489a6cb015SBarry Smith 
1499a6cb015SBarry Smith    Not Collective
1509a6cb015SBarry Smith 
15115091d37SBarry Smith    Level: developer
15215091d37SBarry Smith 
1535a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1549a6cb015SBarry Smith 
155f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1569a6cb015SBarry Smith @*/
1575a655dc6SBarry Smith int MatSNESMFRegisterDestroy(void)
1589a6cb015SBarry Smith {
1599a6cb015SBarry Smith   int ierr;
1609a6cb015SBarry Smith 
1619a6cb015SBarry Smith   PetscFunctionBegin;
162b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
163b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
164b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1659a6cb015SBarry Smith   }
1664c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1679a6cb015SBarry Smith   PetscFunctionReturn(0);
1689a6cb015SBarry Smith }
1699a6cb015SBarry Smith 
1709a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
1714a2ae208SSatish Balay #undef __FUNCT__
1728a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD"
1738a124369SBarry Smith int MatDestroy_MFFD(Mat mat)
174a4d4d686SBarry Smith {
175a4d4d686SBarry Smith   int          ierr;
1767e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
177fae171e0SBarry Smith 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
179ba6a83e5SMatthew Knepley   if (ctx->w != PETSC_NULL) {
180b9fa9cd0SBarry Smith     ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
181ba6a83e5SMatthew Knepley   }
1829a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
18374637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
1846831982aSBarry Smith   PetscHeaderDestroy(ctx);
1853a40ed3dSBarry Smith   PetscFunctionReturn(0);
186b9fa9cd0SBarry Smith }
18750361f65SLois Curfman McInnes 
1884a2ae208SSatish Balay #undef __FUNCT__
1898a124369SBarry Smith #define __FUNCT__ "MatView_MFFD"
19039e2f89bSBarry Smith /*
1918a124369SBarry Smith    MatSNESMFView_MFFD - Views matrix-free parameters.
1928f6e3e37SBarry Smith 
19339e2f89bSBarry Smith */
1948a124369SBarry Smith int MatView_MFFD(Mat J,PetscViewer viewer)
195eb9086c3SLois Curfman McInnes {
196eb9086c3SLois Curfman McInnes   int          ierr;
1977e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1986831982aSBarry Smith   PetscTruth   isascii;
199eb9086c3SLois Curfman McInnes 
2003a40ed3dSBarry Smith   PetscFunctionBegin;
201b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
2020f5bd95cSBarry Smith   if (isascii) {
203b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
204b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
205473c83c3SBarry Smith      if (!ctx->type_name) {
206b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
207473c83c3SBarry Smith      } else {
208b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
209473c83c3SBarry Smith      }
2109a6cb015SBarry Smith      if (ctx->ops->view) {
2119a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
2129a6cb015SBarry Smith      }
2135cd90555SBarry Smith   } else {
21429bbc08cSBarry Smith     SETERRQ1(1,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
215eb9086c3SLois Curfman McInnes   }
2163a40ed3dSBarry Smith   PetscFunctionReturn(0);
217eb9086c3SLois Curfman McInnes }
218eb9086c3SLois Curfman McInnes 
2194a2ae208SSatish Balay #undef __FUNCT__
2208a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD"
221be726c96SBarry Smith /*
2225a655dc6SBarry Smith    MatSNESMFAssemblyEnd_Private - Resets the ctx->ncurrenth to zero. This
22365f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
224be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
22565f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
22665f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
227be726c96SBarry Smith */
2288a124369SBarry Smith int MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt)
229be726c96SBarry Smith {
230be726c96SBarry Smith   int             ierr;
2317e9d5209SBarry Smith   MatSNESMFCtx    j = (MatSNESMFCtx)J->data;
232be726c96SBarry Smith 
233be726c96SBarry Smith   PetscFunctionBegin;
2345a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
235b0a32e0cSBarry Smith   if (j->usesnes) {
2361d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
2371d1367b7SBarry Smith     ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
238*2740c1caSMatthew Knepley     if (j->w == PETSC_NULL) {
239*2740c1caSMatthew Knepley       ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr);
240*2740c1caSMatthew Knepley     }
2411d1367b7SBarry Smith   }
242c5c390f1SBarry Smith   j->vshift = 0.0;
243c5c390f1SBarry Smith   j->vscale = 1.0;
244be726c96SBarry Smith   PetscFunctionReturn(0);
245be726c96SBarry Smith }
246be726c96SBarry Smith 
2474a2ae208SSatish Balay #undef __FUNCT__
2488a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD"
249eb9086c3SLois Curfman McInnes /*
250adb62b0dSMatthew Knepley   MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
251a4d4d686SBarry Smith 
2529a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
253eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
254eb9086c3SLois Curfman McInnes         u = current iterate
255eb9086c3SLois Curfman McInnes         h = difference interval
256eb9086c3SLois Curfman McInnes */
2578a124369SBarry Smith int MatMult_MFFD(Mat mat,Vec a,Vec y)
25839e2f89bSBarry Smith {
2597e9d5209SBarry Smith   MatSNESMFCtx    ctx = (MatSNESMFCtx)mat->data;
260fae171e0SBarry Smith   SNES            snes;
261ea709b57SSatish Balay   PetscScalar     h,mone = -1.0;
262fae171e0SBarry Smith   Vec             w,U,F;
263a305c92eSSatish Balay   int             ierr,(*eval_fct)(SNES,Vec,Vec)=0;
26439e2f89bSBarry Smith 
2653a40ed3dSBarry Smith   PetscFunctionBegin;
2669a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2679a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2689a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2699a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
270d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
27156cd22aeSBarry Smith 
272fae171e0SBarry Smith   snes = ctx->snes;
273fae171e0SBarry Smith   w    = ctx->w;
2741d1367b7SBarry Smith   U    = ctx->current_u;
27550361f65SLois Curfman McInnes 
27685614651SBarry Smith   /*
27785614651SBarry Smith       Compute differencing parameter
27885614651SBarry Smith   */
2799a6cb015SBarry Smith   if (!ctx->ops->compute) {
280b7fd4e64SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_DEFAULT);CHKERRQ(ierr);
2815a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2829a6cb015SBarry Smith   }
2839a6cb015SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h);CHKERRQ(ierr);
284a4d4d686SBarry Smith 
2855b7f0c42SBarry Smith   if (ctx->checkh) {
2865b7f0c42SBarry Smith     ierr = (*ctx->checkh)(U,a,&h,ctx->checkhctx);CHKERRQ(ierr);
2875b7f0c42SBarry Smith   }
2885b7f0c42SBarry Smith 
289a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
290a4d4d686SBarry Smith   ctx->currenth = h;
291aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
2928a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h));
293a4d4d686SBarry Smith #else
2948a124369SBarry Smith   PetscLogInfo(mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h);
295a4d4d686SBarry Smith #endif
296a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
29785614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
298a4d4d686SBarry Smith   }
29985614651SBarry Smith   ctx->ncurrenth++;
300a4d4d686SBarry Smith 
30185614651SBarry Smith   /* w = u + ha */
302a4d4d686SBarry Smith   ierr = VecWAXPY(&h,a,U,w);CHKERRQ(ierr);
30385614651SBarry Smith 
304b0a32e0cSBarry Smith   if (ctx->usesnes) {
30585614651SBarry Smith     eval_fct = SNESComputeFunction;
3061d1367b7SBarry Smith     F    = ctx->current_f;
30729bbc08cSBarry Smith     if (!F) SETERRQ(1,"You must call MatAssembly() even on matrix-free matrices");
30839903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
30985614651SBarry Smith   } else {
31085614651SBarry Smith     F = ctx->funcvec;
31185614651SBarry Smith     /* compute func(U) as base for differencing */
31285614651SBarry Smith     if (ctx->ncurrenth == 1) {
31385614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
31485614651SBarry Smith     }
31585614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
31685614651SBarry Smith   }
317a4d4d686SBarry Smith 
318a4d4d686SBarry Smith   ierr = VecAXPY(&mone,F,y);CHKERRQ(ierr);
319a4d4d686SBarry Smith   h    = 1.0/h;
320a4d4d686SBarry Smith   ierr = VecScale(&h,y);CHKERRQ(ierr);
321c5c390f1SBarry Smith 
322c5c390f1SBarry Smith 
323c5c390f1SBarry Smith   if (ctx->vshift != 0.0 && ctx->vscale != 1.0) {
324c5c390f1SBarry Smith     ierr = VecAXPBY(&ctx->vshift,&ctx->vscale,a,y);CHKERRQ(ierr);
325c5c390f1SBarry Smith   } else if (ctx->vscale != 1.0) {
326c5c390f1SBarry Smith     ierr = VecScale(&ctx->vscale,y);CHKERRQ(ierr);
327c5c390f1SBarry Smith   } else if (ctx->vshift != 0.0) {
328c5c390f1SBarry Smith     ierr = VecAXPY(&ctx->vshift,a,y);CHKERRQ(ierr);
329c5c390f1SBarry Smith   }
330c5c390f1SBarry Smith 
33174637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
332a4d4d686SBarry Smith 
333d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(MAT_MultMatrixFree,a,y,0,0);CHKERRQ(ierr);
334a4d4d686SBarry Smith   PetscFunctionReturn(0);
335a4d4d686SBarry Smith }
336a4d4d686SBarry Smith 
3374a2ae208SSatish Balay #undef __FUNCT__
3388a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD"
339cf57b110SBarry Smith /*
3408a124369SBarry Smith   MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix
341cf57b110SBarry Smith 
342cf57b110SBarry Smith         y ~= (F(u + ha) - F(u))/h,
343cf57b110SBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
344cf57b110SBarry Smith         u = current iterate
345cf57b110SBarry Smith         h = difference interval
346cf57b110SBarry Smith */
3478a124369SBarry Smith int MatGetDiagonal_MFFD(Mat mat,Vec a)
348cf57b110SBarry Smith {
3497e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
350ea709b57SSatish Balay   PetscScalar  h,*aa,*ww,v;
35177d8c4bbSBarry Smith   PetscReal    epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON;
35265df01d8SBarry Smith   Vec          w,U;
353cf57b110SBarry Smith   int          i,ierr,rstart,rend;
354cf57b110SBarry Smith 
355cf57b110SBarry Smith   PetscFunctionBegin;
356cf57b110SBarry Smith   if (!ctx->funci) {
357cf57b110SBarry Smith     SETERRQ(1,"Requirers calling MatSNESMFSetFunctioni() first");
358cf57b110SBarry Smith   }
359cf57b110SBarry Smith 
360cf57b110SBarry Smith   w    = ctx->w;
361cf57b110SBarry Smith   U    = ctx->current_u;
362cf57b110SBarry Smith   ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr);
363cf57b110SBarry Smith   ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr);
364cf57b110SBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
365cf57b110SBarry Smith 
366cf57b110SBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
367cf57b110SBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
368cf57b110SBarry Smith   for (i=rstart; i<rend; i++) {
369cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
370cf57b110SBarry Smith     h  = ww[i-rstart];
371cf57b110SBarry Smith     if (h == 0.0) h = 1.0;
372cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX)
373cf57b110SBarry Smith     if (h < umin && h >= 0.0)      h = umin;
374cf57b110SBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
375cf57b110SBarry Smith #else
376cf57b110SBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
377cf57b110SBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
378cf57b110SBarry Smith #endif
379cf57b110SBarry Smith     h     *= epsilon;
380cf57b110SBarry Smith 
381cf57b110SBarry Smith     ww[i-rstart] += h;
382cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
383cf57b110SBarry Smith     ierr          = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr);
384cf57b110SBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
385c5c390f1SBarry Smith 
386c5c390f1SBarry Smith     /* possibly shift and scale result */
387c5c390f1SBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
388c5c390f1SBarry Smith 
389cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
390cf57b110SBarry Smith     ww[i-rstart] -= h;
391cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
392cf57b110SBarry Smith   }
393cf57b110SBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
394cf57b110SBarry Smith   PetscFunctionReturn(0);
395cf57b110SBarry Smith }
396cf57b110SBarry Smith 
397cf57b110SBarry Smith #undef __FUNCT__
398c5c390f1SBarry Smith #define __FUNCT__ "MatShift_MFFD"
399c5c390f1SBarry Smith int MatShift_MFFD(PetscScalar *a,Mat Y)
400c5c390f1SBarry Smith {
401c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
402c5c390f1SBarry Smith   PetscFunctionBegin;
403c5c390f1SBarry Smith   shell->vshift += *a;
404c5c390f1SBarry Smith   PetscFunctionReturn(0);
405c5c390f1SBarry Smith }
406c5c390f1SBarry Smith 
407c5c390f1SBarry Smith #undef __FUNCT__
408c5c390f1SBarry Smith #define __FUNCT__ "MatScale_MFFD"
409c5c390f1SBarry Smith int MatScale_MFFD(PetscScalar *a,Mat Y)
410c5c390f1SBarry Smith {
411c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
412c5c390f1SBarry Smith   PetscFunctionBegin;
413c5c390f1SBarry Smith   shell->vscale *= *a;
414c5c390f1SBarry Smith   PetscFunctionReturn(0);
415c5c390f1SBarry Smith }
416c5c390f1SBarry Smith 
417c5c390f1SBarry Smith 
418c5c390f1SBarry Smith #undef __FUNCT__
4194a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
420a4d4d686SBarry Smith /*@C
42165f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
42265f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
42365f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
424a4d4d686SBarry Smith 
425a4d4d686SBarry Smith    Collective on SNES and Vec
426a4d4d686SBarry Smith 
427a4d4d686SBarry Smith    Input Parameters:
428a4d4d686SBarry Smith +  snes - the SNES context
429a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
430a4d4d686SBarry Smith 
431a4d4d686SBarry Smith    Output Parameter:
432a4d4d686SBarry Smith .  J - the matrix-free matrix
433a4d4d686SBarry Smith 
43415091d37SBarry Smith    Level: advanced
43515091d37SBarry Smith 
436a4d4d686SBarry Smith    Notes:
437a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
438a4d4d686SBarry Smith    and work space for performing finite difference approximations of
43965f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
4409a6cb015SBarry Smith 
4419a6cb015SBarry Smith    The default code uses the following approach to compute h
442a4d4d686SBarry Smith 
443a4d4d686SBarry Smith .vb
44465f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
445a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
446a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
447a4d4d686SBarry Smith  where
448a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
449a4d4d686SBarry Smith      umin = minimum iterate parameter
450a4d4d686SBarry Smith .ve
451a4d4d686SBarry Smith 
4525a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
45365f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
45465f2ba5bSLois Curfman McInnes    of the users manual for details.
455a4d4d686SBarry Smith 
456a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
457a4d4d686SBarry Smith    matrix context.
458a4d4d686SBarry Smith 
459a4d4d686SBarry Smith    Options Database Keys:
460a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4619a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
462a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
463a4d4d686SBarry Smith 
464a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
465a4d4d686SBarry Smith 
4665a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4671d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
468fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
469a4d4d686SBarry Smith 
470a4d4d686SBarry Smith @*/
4715a655dc6SBarry Smith int MatCreateSNESMF(SNES snes,Vec x,Mat *J)
472a4d4d686SBarry Smith {
4731d1367b7SBarry Smith   MatSNESMFCtx mfctx;
4741d1367b7SBarry Smith   int          ierr;
4751d1367b7SBarry Smith 
4761d1367b7SBarry Smith   PetscFunctionBegin;
4771d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
4787e9d5209SBarry Smith 
4797e9d5209SBarry Smith   mfctx          = (MatSNESMFCtx)(*J)->data;
4801d1367b7SBarry Smith   mfctx->snes    = snes;
481b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
482b0a32e0cSBarry Smith   PetscLogObjectParent(snes,*J);
4831d1367b7SBarry Smith   PetscFunctionReturn(0);
4841d1367b7SBarry Smith }
4851d1367b7SBarry Smith 
486cf3bea43SBarry Smith EXTERN_C_BEGIN
487cf3bea43SBarry Smith #undef __FUNCT__
488cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD"
489cf3bea43SBarry Smith int MatSNESMFSetBase_FD(Mat J,Vec U)
490cf3bea43SBarry Smith {
491cf3bea43SBarry Smith   int          ierr;
4927e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
493cf3bea43SBarry Smith 
494cf3bea43SBarry Smith   PetscFunctionBegin;
495cf3bea43SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
496cf3bea43SBarry Smith   ctx->current_u = U;
497cf3bea43SBarry Smith   ctx->usesnes   = PETSC_FALSE;
498ba6a83e5SMatthew Knepley   if (ctx->w == PETSC_NULL) {
499ba6a83e5SMatthew Knepley     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
500ba6a83e5SMatthew Knepley   }
501cf3bea43SBarry Smith   PetscFunctionReturn(0);
502cf3bea43SBarry Smith }
503cf3bea43SBarry Smith EXTERN_C_END
504cf3bea43SBarry Smith 
5055b7f0c42SBarry Smith EXTERN_C_BEGIN
5065b7f0c42SBarry Smith #undef __FUNCT__
5075b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh_FD"
50861860be5SBarry Smith int MatSNESMFSetCheckh_FD(Mat J,int (*fun)(Vec,Vec,PetscScalar*,void*),void*ectx)
5095b7f0c42SBarry Smith {
5105b7f0c42SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
5115b7f0c42SBarry Smith 
5125b7f0c42SBarry Smith   PetscFunctionBegin;
5135b7f0c42SBarry Smith   ctx->checkh    = fun;
5145b7f0c42SBarry Smith   ctx->checkhctx = ectx;
5155b7f0c42SBarry Smith   PetscFunctionReturn(0);
5165b7f0c42SBarry Smith }
5175b7f0c42SBarry Smith EXTERN_C_END
5185b7f0c42SBarry Smith 
5194a2ae208SSatish Balay #undef __FUNCT__
5207e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions"
5217e9d5209SBarry Smith /*@
5227e9d5209SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
5237e9d5209SBarry Smith    parameter.
5247e9d5209SBarry Smith 
5257e9d5209SBarry Smith    Collective on Mat
5267e9d5209SBarry Smith 
5277e9d5209SBarry Smith    Input Parameters:
5287e9d5209SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
5297e9d5209SBarry Smith 
5307e9d5209SBarry Smith    Options Database Keys:
5317e9d5209SBarry Smith +  -snes_mf_type - <default,wp>
5327e9d5209SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
5337e9d5209SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
5347e9d5209SBarry Smith 
5357e9d5209SBarry Smith    Level: advanced
5367e9d5209SBarry Smith 
5377e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters
5387e9d5209SBarry Smith 
5397e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
5407e9d5209SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
5417e9d5209SBarry Smith @*/
5427e9d5209SBarry Smith int MatSNESMFSetFromOptions(Mat mat)
5437e9d5209SBarry Smith {
5447e9d5209SBarry Smith   MatSNESMFCtx mfctx = (MatSNESMFCtx)mat->data;
5457e9d5209SBarry Smith   int          ierr;
5467e9d5209SBarry Smith   PetscTruth   flg;
5477e9d5209SBarry Smith   char         ftype[256];
5487e9d5209SBarry Smith 
5497e9d5209SBarry Smith   PetscFunctionBegin;
5507e9d5209SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
5517e9d5209SBarry Smith 
5527e9d5209SBarry Smith   ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
5537e9d5209SBarry Smith   ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
5547e9d5209SBarry Smith   if (flg) {
5557e9d5209SBarry Smith     ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
5567e9d5209SBarry Smith   }
5577e9d5209SBarry Smith 
55887828ca2SBarry Smith   ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
5597e9d5209SBarry Smith   ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
5607e9d5209SBarry Smith   if (mfctx->snes) {
5617e9d5209SBarry Smith     ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
5627e9d5209SBarry Smith     if (flg) {
5637e9d5209SBarry Smith       SLES sles;
5647e9d5209SBarry Smith       KSP  ksp;
5657e9d5209SBarry Smith       ierr = SNESGetSLES(mfctx->snes,&sles);CHKERRQ(ierr);
5667e9d5209SBarry Smith       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
5677e9d5209SBarry Smith       ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
5687e9d5209SBarry Smith     }
5697e9d5209SBarry Smith   }
5705b7f0c42SBarry Smith   ierr = PetscOptionsName("-snes_mf_check_positivity","Insure that U + h*a is nonnegative","MatSNESMFSetCheckh",&flg);CHKERRQ(ierr);
5715b7f0c42SBarry Smith   if (flg) {
5725b7f0c42SBarry Smith     ierr = MatSNESMFSetCheckh(mat,MatSNESMFCheckPositivity,0);CHKERRQ(ierr);
5735b7f0c42SBarry Smith   }
5747e9d5209SBarry Smith   if (mfctx->ops->setfromoptions) {
5757e9d5209SBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
5767e9d5209SBarry Smith   }
5777e9d5209SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5787e9d5209SBarry Smith   PetscFunctionReturn(0);
5797e9d5209SBarry Smith }
5807e9d5209SBarry Smith 
5817e9d5209SBarry Smith #undef __FUNCT__
5827e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD"
5837e9d5209SBarry Smith EXTERN_C_BEGIN
5847e9d5209SBarry Smith int MatCreate_MFFD(Mat A)
5857e9d5209SBarry Smith {
5867e9d5209SBarry Smith   MatSNESMFCtx mfctx;
58765df01d8SBarry Smith   int          ierr;
5887e9d5209SBarry Smith 
5897e9d5209SBarry Smith   PetscFunctionBegin;
5906e087cb5SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5916e087cb5SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
5926e087cb5SMatthew Knepley #endif
5936e087cb5SMatthew Knepley 
5948a124369SBarry Smith   PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD);
5957e9d5209SBarry Smith   PetscLogObjectCreate(mfctx);
5967e9d5209SBarry Smith   mfctx->sp              = 0;
5977e9d5209SBarry Smith   mfctx->snes            = 0;
59877d8c4bbSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
5997e9d5209SBarry Smith   mfctx->recomputeperiod = 1;
6007e9d5209SBarry Smith   mfctx->count           = 0;
6017e9d5209SBarry Smith   mfctx->currenth        = 0.0;
6027e9d5209SBarry Smith   mfctx->historyh        = PETSC_NULL;
6037e9d5209SBarry Smith   mfctx->ncurrenth       = 0;
6047e9d5209SBarry Smith   mfctx->maxcurrenth     = 0;
6057e9d5209SBarry Smith   mfctx->type_name       = 0;
6067e9d5209SBarry Smith   mfctx->usesnes         = PETSC_FALSE;
6077e9d5209SBarry Smith 
608c5c390f1SBarry Smith   mfctx->vshift          = 0.0;
609c5c390f1SBarry Smith   mfctx->vscale          = 1.0;
610c5c390f1SBarry Smith 
6117e9d5209SBarry Smith   /*
6127e9d5209SBarry Smith      Create the empty data structure to contain compute-h routines.
6137e9d5209SBarry Smith      These will be filled in below from the command line options or
6147e9d5209SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
6158a124369SBarry Smith      then it will default in the first use of MatMult_MFFD()
6167e9d5209SBarry Smith   */
6177e9d5209SBarry Smith   mfctx->ops->compute        = 0;
6187e9d5209SBarry Smith   mfctx->ops->destroy        = 0;
6197e9d5209SBarry Smith   mfctx->ops->view           = 0;
6207e9d5209SBarry Smith   mfctx->ops->setfromoptions = 0;
6217e9d5209SBarry Smith   mfctx->hctx                = 0;
6227e9d5209SBarry Smith 
6237e9d5209SBarry Smith   mfctx->func                = 0;
6247e9d5209SBarry Smith   mfctx->funcctx             = 0;
6257e9d5209SBarry Smith   mfctx->funcvec             = 0;
626ba6a83e5SMatthew Knepley   mfctx->w                   = PETSC_NULL;
6277e9d5209SBarry Smith 
62865df01d8SBarry Smith   A->data                = mfctx;
6297e9d5209SBarry Smith 
6308a124369SBarry Smith   A->ops->mult           = MatMult_MFFD;
6318a124369SBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
6328a124369SBarry Smith   A->ops->view           = MatView_MFFD;
6338a124369SBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
6348a124369SBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
635c5c390f1SBarry Smith   A->ops->scale          = MatScale_MFFD;
636c5c390f1SBarry Smith   A->ops->shift          = MatShift_MFFD;
63765df01d8SBarry Smith   A->ops->setfromoptions = MatSNESMFSetFromOptions;
6387e9d5209SBarry Smith 
63965df01d8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr);
640c5c390f1SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBase_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr);
64187828ca2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr);
6425b7f0c42SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetCheckh_C","MatSNESMFSetCheckh_FD",MatSNESMFSetCheckh_FD);CHKERRQ(ierr);
64365df01d8SBarry Smith   mfctx->mat = A;
6447e9d5209SBarry Smith 
6457e9d5209SBarry Smith   PetscFunctionReturn(0);
6467e9d5209SBarry Smith }
6477e9d5209SBarry Smith 
6487e9d5209SBarry Smith EXTERN_C_END
6497e9d5209SBarry Smith 
6507e9d5209SBarry Smith #undef __FUNCT__
6514a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
6521d1367b7SBarry Smith /*@C
6531d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
6541d1367b7SBarry Smith 
6551d1367b7SBarry Smith    Collective on Vec
6561d1367b7SBarry Smith 
6571d1367b7SBarry Smith    Input Parameters:
6581d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
6591d1367b7SBarry Smith 
6601d1367b7SBarry Smith    Output Parameter:
6611d1367b7SBarry Smith .  J - the matrix-free matrix
6621d1367b7SBarry Smith 
6631d1367b7SBarry Smith    Level: advanced
6641d1367b7SBarry Smith 
6651d1367b7SBarry Smith    Notes:
6661d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
6671d1367b7SBarry Smith    and work space for performing finite difference approximations of
6681d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
6691d1367b7SBarry Smith 
6701d1367b7SBarry Smith    The default code uses the following approach to compute h
6711d1367b7SBarry Smith 
6721d1367b7SBarry Smith .vb
6731d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
6741d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
6751d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
6761d1367b7SBarry Smith  where
6771d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
6781d1367b7SBarry Smith      umin = minimum iterate parameter
6791d1367b7SBarry Smith .ve
6801d1367b7SBarry Smith 
6811d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
6821d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
6831d1367b7SBarry Smith    of the users manual for details.
6841d1367b7SBarry Smith 
6851d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
6861d1367b7SBarry Smith    matrix context.
6871d1367b7SBarry Smith 
6881d1367b7SBarry Smith    Options Database Keys:
6891d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
6901d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
6915b7f0c42SBarry Smith .  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
6925b7f0c42SBarry Smith -  -snes_mf_check_positivity
6931d1367b7SBarry Smith 
6941d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
6951d1367b7SBarry Smith 
6961d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
6971d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
698fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian()
6991d1367b7SBarry Smith 
7001d1367b7SBarry Smith @*/
7011d1367b7SBarry Smith int MatCreateMF(Vec x,Mat *J)
7021d1367b7SBarry Smith {
703a4d4d686SBarry Smith   MPI_Comm     comm;
7049a6cb015SBarry Smith   int          n,nloc,ierr;
705a4d4d686SBarry Smith 
706a4d4d686SBarry Smith   PetscFunctionBegin;
7071d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
70865df01d8SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
70965df01d8SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
7107e9d5209SBarry Smith   ierr = MatCreate(comm,nloc,nloc,n,n,J);CHKERRQ(ierr);
71165df01d8SBarry Smith   ierr = MatRegister(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr);
71265df01d8SBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
7139a6cb015SBarry Smith   PetscFunctionReturn(0);
7149a6cb015SBarry Smith }
7159a6cb015SBarry Smith 
716a4d4d686SBarry Smith 
7174a2ae208SSatish Balay #undef __FUNCT__
7184a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
719a4d4d686SBarry Smith /*@
72065f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
721a4d4d686SBarry Smith    parameter.
722a4d4d686SBarry Smith 
723a4d4d686SBarry Smith    Not Collective
724a4d4d686SBarry Smith 
725a4d4d686SBarry Smith    Input Parameters:
7265a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
727a4d4d686SBarry Smith 
728a4d4d686SBarry Smith    Output Paramter:
729a4d4d686SBarry Smith .  h - the differencing step size
730a4d4d686SBarry Smith 
73115091d37SBarry Smith    Level: advanced
73215091d37SBarry Smith 
733a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
734a4d4d686SBarry Smith 
7355a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
7365a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
737a4d4d686SBarry Smith @*/
73887828ca2SBarry Smith int MatSNESMFGetH(Mat mat,PetscScalar *h)
739a4d4d686SBarry Smith {
7407e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
741a4d4d686SBarry Smith 
742a4d4d686SBarry Smith   PetscFunctionBegin;
743a4d4d686SBarry Smith   *h = ctx->currenth;
744a4d4d686SBarry Smith   PetscFunctionReturn(0);
745a4d4d686SBarry Smith }
746a4d4d686SBarry Smith 
7474a2ae208SSatish Balay #undef __FUNCT__
7484a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
749a4d4d686SBarry Smith /*
7505a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
75165f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
75265f2ba5bSLois Curfman McInnes    each step.
753a4d4d686SBarry Smith */
754329f5518SBarry Smith int MatSNESMFKSPMonitor(KSP ksp,int n,PetscReal rnorm,void *dummy)
755a4d4d686SBarry Smith {
756a4d4d686SBarry Smith   PC             pc;
7575a655dc6SBarry Smith   MatSNESMFCtx   ctx;
758a4d4d686SBarry Smith   int            ierr;
759a4d4d686SBarry Smith   Mat            mat;
760a4d4d686SBarry Smith   MPI_Comm       comm;
761a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
762a4d4d686SBarry Smith 
763a4d4d686SBarry Smith   PetscFunctionBegin;
764a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
765a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
766a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
767a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7687e9d5209SBarry Smith   ctx  = (MatSNESMFCtx)mat->data;
7697e9d5209SBarry Smith 
770a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
771aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
772d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
773329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
774a4d4d686SBarry Smith #else
775d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
776a4d4d686SBarry Smith #endif
777a4d4d686SBarry Smith   } else {
778d132466eSBarry Smith     ierr = PetscPrintf(comm,"%d KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
779a4d4d686SBarry Smith   }
780a4d4d686SBarry Smith   PetscFunctionReturn(0);
781a4d4d686SBarry Smith }
782a4d4d686SBarry Smith 
7834a2ae208SSatish Balay #undef __FUNCT__
7844a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
78585614651SBarry Smith /*@C
78685614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
78785614651SBarry Smith 
78885614651SBarry Smith    Collective on Mat
78985614651SBarry Smith 
79085614651SBarry Smith    Input Parameters:
79185614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
79285614651SBarry Smith .  v   - workspace vector
79385614651SBarry Smith .  func - the function to use
79485614651SBarry Smith -  funcctx - optional function context passed to function
79585614651SBarry Smith 
79685614651SBarry Smith    Level: advanced
79785614651SBarry Smith 
79885614651SBarry Smith    Notes:
79985614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
80085614651SBarry Smith     matrix inside your compute Jacobian routine
80185614651SBarry Smith 
80285614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
80385614651SBarry Smith 
80485614651SBarry Smith .keywords: SNES, matrix-free, function
80585614651SBarry Smith 
80685614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
80785614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
80885614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
80985614651SBarry Smith @*/
81085614651SBarry Smith int MatSNESMFSetFunction(Mat mat,Vec v,int (*func)(SNES,Vec,Vec,void *),void *funcctx)
81185614651SBarry Smith {
8127e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
81385614651SBarry Smith 
81485614651SBarry Smith   PetscFunctionBegin;
81585614651SBarry Smith   ctx->func    = func;
81685614651SBarry Smith   ctx->funcctx = funcctx;
81785614651SBarry Smith   ctx->funcvec = v;
81885614651SBarry Smith   PetscFunctionReturn(0);
81985614651SBarry Smith }
82085614651SBarry Smith 
821cf57b110SBarry Smith #undef __FUNCT__
822cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni"
823cf57b110SBarry Smith /*@C
824cf57b110SBarry Smith    MatSNESMFSetFunctioni - Sets the function for a single component
825cf57b110SBarry Smith 
826cf57b110SBarry Smith    Collective on Mat
827cf57b110SBarry Smith 
828cf57b110SBarry Smith    Input Parameters:
829cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
830cf57b110SBarry Smith -  funci - the function to use
831cf57b110SBarry Smith 
832cf57b110SBarry Smith    Level: advanced
833cf57b110SBarry Smith 
834cf57b110SBarry Smith    Notes:
835cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
836cf57b110SBarry Smith     matrix inside your compute Jacobian routine
837cf57b110SBarry Smith 
838cf57b110SBarry Smith 
839cf57b110SBarry Smith .keywords: SNES, matrix-free, function
840cf57b110SBarry Smith 
841cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
842cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
843cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
844cf57b110SBarry Smith @*/
84587828ca2SBarry Smith int MatSNESMFSetFunctioni(Mat mat,int (*funci)(int,Vec,PetscScalar*,void *))
846cf57b110SBarry Smith {
84787828ca2SBarry Smith   int  ierr,(*f)(Mat,int (*)(int,Vec,PetscScalar*,void *));
848cf57b110SBarry Smith 
849cf57b110SBarry Smith   PetscFunctionBegin;
85087828ca2SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
851c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
85287828ca2SBarry Smith   if (f) {
85387828ca2SBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
85487828ca2SBarry Smith   }
855cf57b110SBarry Smith   PetscFunctionReturn(0);
856cf57b110SBarry Smith }
857cf57b110SBarry Smith 
85887828ca2SBarry Smith 
859cf57b110SBarry Smith #undef __FUNCT__
860cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase"
861cf57b110SBarry Smith /*@C
862cf57b110SBarry Smith    MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation
863cf57b110SBarry Smith 
864cf57b110SBarry Smith    Collective on Mat
865cf57b110SBarry Smith 
866cf57b110SBarry Smith    Input Parameters:
867cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
868cf57b110SBarry Smith -  func - the function to use
869cf57b110SBarry Smith 
870cf57b110SBarry Smith    Level: advanced
871cf57b110SBarry Smith 
872cf57b110SBarry Smith    Notes:
873cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
874cf57b110SBarry Smith     matrix inside your compute Jacobian routine
875cf57b110SBarry Smith 
876cf57b110SBarry Smith 
877cf57b110SBarry Smith .keywords: SNES, matrix-free, function
878cf57b110SBarry Smith 
879cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
880cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
881cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
882cf57b110SBarry Smith @*/
883cf57b110SBarry Smith int MatSNESMFSetFunctioniBase(Mat mat,int (*func)(Vec,void *))
884cf57b110SBarry Smith {
88587828ca2SBarry Smith   int  ierr,(*f)(Mat,int (*)(Vec,void *));
886cf57b110SBarry Smith 
887cf57b110SBarry Smith   PetscFunctionBegin;
88887828ca2SBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE);
889c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
89087828ca2SBarry Smith   if (f) {
89187828ca2SBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
89287828ca2SBarry Smith   }
893cf57b110SBarry Smith   PetscFunctionReturn(0);
894cf57b110SBarry Smith }
895cf57b110SBarry Smith 
89685614651SBarry Smith 
8974a2ae208SSatish Balay #undef __FUNCT__
8984a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
899329f5518SBarry Smith /*@
900329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
901329f5518SBarry Smith 
902329f5518SBarry Smith    Collective on Mat
903329f5518SBarry Smith 
904329f5518SBarry Smith    Input Parameters:
905329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
906329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
907329f5518SBarry Smith 
908329f5518SBarry Smith    Options Database Keys:
909329f5518SBarry Smith +  -snes_mf_period <period>
910329f5518SBarry Smith 
911329f5518SBarry Smith    Level: advanced
912329f5518SBarry Smith 
913329f5518SBarry Smith 
914329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
915329f5518SBarry Smith 
916329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
917329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
918329f5518SBarry Smith           MatSNESMFKSPMonitor()
919329f5518SBarry Smith @*/
920329f5518SBarry Smith int MatSNESMFSetPeriod(Mat mat,int period)
921329f5518SBarry Smith {
9227e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
923329f5518SBarry Smith 
924329f5518SBarry Smith   PetscFunctionBegin;
925329f5518SBarry Smith   ctx->recomputeperiod = period;
926329f5518SBarry Smith   PetscFunctionReturn(0);
927329f5518SBarry Smith }
928329f5518SBarry Smith 
9294a2ae208SSatish Balay #undef __FUNCT__
9304a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
931a4d4d686SBarry Smith /*@
9325a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
933a4d4d686SBarry Smith    matrix-vector products using finite differences.
934a4d4d686SBarry Smith 
935a4d4d686SBarry Smith    Collective on Mat
936a4d4d686SBarry Smith 
937a4d4d686SBarry Smith    Input Parameters:
9385a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
9399a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
940a4d4d686SBarry Smith                the relative error in the function evaluations)
941a4d4d686SBarry Smith 
94215091d37SBarry Smith    Options Database Keys:
94315091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
94415091d37SBarry Smith 
94515091d37SBarry Smith    Level: advanced
94615091d37SBarry Smith 
947a4d4d686SBarry Smith    Notes:
948a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
949a4d4d686SBarry Smith .vb
95065f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
951a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
952a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
953a4d4d686SBarry Smith .ve
954a4d4d686SBarry Smith 
955a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
956a4d4d686SBarry Smith 
9575a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
9585a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9595a655dc6SBarry Smith           MatSNESMFKSPMonitor()
960a4d4d686SBarry Smith @*/
961329f5518SBarry Smith int MatSNESMFSetFunctionError(Mat mat,PetscReal error)
962a4d4d686SBarry Smith {
9637e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
964a4d4d686SBarry Smith 
965a4d4d686SBarry Smith   PetscFunctionBegin;
966a4d4d686SBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
967a4d4d686SBarry Smith   PetscFunctionReturn(0);
968a4d4d686SBarry Smith }
969a4d4d686SBarry Smith 
9704a2ae208SSatish Balay #undef __FUNCT__
9714a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
972a4d4d686SBarry Smith /*@
97365f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
97465f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
97565f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
97665f2ba5bSLois Curfman McInnes    automatically removed.
977a4d4d686SBarry Smith 
978a4d4d686SBarry Smith    Collective on Mat
979a4d4d686SBarry Smith 
980a4d4d686SBarry Smith    Input Parameters:
981a4d4d686SBarry Smith +  J - the matrix-free matrix context
98274637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
983a4d4d686SBarry Smith 
98415091d37SBarry Smith    Level: advanced
98515091d37SBarry Smith 
986a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
987a4d4d686SBarry Smith 
98874637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
9895a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9905a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
991a4d4d686SBarry Smith @*/
99274637425SBarry Smith int MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
993a4d4d686SBarry Smith {
994a4d4d686SBarry Smith   int          ierr;
9957e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
996a4d4d686SBarry Smith   MPI_Comm     comm;
997a4d4d686SBarry Smith 
998a4d4d686SBarry Smith   PetscFunctionBegin;
9992d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
1000a4d4d686SBarry Smith 
100185614651SBarry Smith   ctx->sp = nullsp;
100285614651SBarry Smith   ierr    = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
1003a4d4d686SBarry Smith   PetscFunctionReturn(0);
1004a4d4d686SBarry Smith }
1005a4d4d686SBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
1008a4d4d686SBarry Smith /*@
100965f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
101065f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
1011a4d4d686SBarry Smith 
1012a4d4d686SBarry Smith    Collective on Mat
1013a4d4d686SBarry Smith 
1014a4d4d686SBarry Smith    Input Parameters:
1015a4d4d686SBarry Smith +  J - the matrix-free matrix context
101665f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
101765f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
101865f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
1019a4d4d686SBarry Smith 
102015091d37SBarry Smith    Level: advanced
102115091d37SBarry Smith 
1022a4d4d686SBarry Smith    Notes:
102365f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
102465f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
1025a4d4d686SBarry Smith 
1026a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1027a4d4d686SBarry Smith 
10285a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10295a655dc6SBarry Smith           MatSNESMFResetHHistory(),
10305a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1031a4d4d686SBarry Smith 
1032a4d4d686SBarry Smith @*/
103387828ca2SBarry Smith int MatSNESMFSetHHistory(Mat J,PetscScalar *history,int nhistory)
1034a4d4d686SBarry Smith {
10357e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1036a4d4d686SBarry Smith 
1037a4d4d686SBarry Smith   PetscFunctionBegin;
1038a4d4d686SBarry Smith   ctx->historyh    = history;
1039a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
1040a4d4d686SBarry Smith   ctx->currenth    = 0;
1041a4d4d686SBarry Smith   PetscFunctionReturn(0);
1042a4d4d686SBarry Smith }
1043a4d4d686SBarry Smith 
10444a2ae208SSatish Balay #undef __FUNCT__
10454a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
1046a4d4d686SBarry Smith /*@
10475a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
1048a4d4d686SBarry Smith    collecting a new set of differencing histories.
1049a4d4d686SBarry Smith 
1050a4d4d686SBarry Smith    Collective on Mat
1051a4d4d686SBarry Smith 
1052a4d4d686SBarry Smith    Input Parameters:
1053a4d4d686SBarry Smith .  J - the matrix-free matrix context
1054a4d4d686SBarry Smith 
105515091d37SBarry Smith    Level: advanced
105615091d37SBarry Smith 
1057a4d4d686SBarry Smith    Notes:
105865f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
1059a4d4d686SBarry Smith 
1060a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1061a4d4d686SBarry Smith 
10625a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10635a655dc6SBarry Smith           MatSNESMFSetHHistory(),
10645a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1065a4d4d686SBarry Smith 
1066a4d4d686SBarry Smith @*/
10675a655dc6SBarry Smith int MatSNESMFResetHHistory(Mat J)
1068a4d4d686SBarry Smith {
10697e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1070a4d4d686SBarry Smith 
1071a4d4d686SBarry Smith   PetscFunctionBegin;
1072be726c96SBarry Smith   ctx->ncurrenth    = 0;
1073a4d4d686SBarry Smith   PetscFunctionReturn(0);
1074a4d4d686SBarry Smith }
1075a4d4d686SBarry Smith 
10764a2ae208SSatish Balay #undef __FUNCT__
1077fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian"
1078fed8bd04SBarry Smith int MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
10791d1367b7SBarry Smith {
10801d1367b7SBarry Smith   int ierr;
10811d1367b7SBarry Smith   PetscFunctionBegin;
10821d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10831d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10841d1367b7SBarry Smith   PetscFunctionReturn(0);
10851d1367b7SBarry Smith }
10861d1367b7SBarry Smith 
10874a2ae208SSatish Balay #undef __FUNCT__
10884a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
10895b7f0c42SBarry Smith /*@
10905b7f0c42SBarry Smith     MatSNESMFSetBase - Sets the vector U at which matrix vector products of the
10915b7f0c42SBarry Smith         Jacobian are computed
10925b7f0c42SBarry Smith 
10935b7f0c42SBarry Smith     Collective on Mat
10945b7f0c42SBarry Smith 
10955b7f0c42SBarry Smith     Input Parameters:
10965b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10975b7f0c42SBarry Smith -   U - the vector
10985b7f0c42SBarry Smith 
10995b7f0c42SBarry Smith     Notes: This is rarely used directly
11005b7f0c42SBarry Smith 
11015b7f0c42SBarry Smith     Level: advanced
11025b7f0c42SBarry Smith 
11035b7f0c42SBarry Smith @*/
11041d1367b7SBarry Smith int MatSNESMFSetBase(Mat J,Vec U)
11051d1367b7SBarry Smith {
11063a7fca6bSBarry Smith   int  ierr,(*f)(Mat,Vec);
11071d1367b7SBarry Smith 
11081d1367b7SBarry Smith   PetscFunctionBegin;
11091d1367b7SBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE);
11101d1367b7SBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE);
1111c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1112cf3bea43SBarry Smith   if (f) {
1113cf3bea43SBarry Smith     ierr = (*f)(J,U);CHKERRQ(ierr);
111449d4803aSBarry Smith   }
11151d1367b7SBarry Smith   PetscFunctionReturn(0);
11161d1367b7SBarry Smith }
1117cf57b110SBarry Smith 
11185b7f0c42SBarry Smith #undef __FUNCT__
11195b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh"
112061860be5SBarry Smith /*@C
11215b7f0c42SBarry Smith     MatSNESMFSetCheckh - Sets a function that checks the computed h and adjusts
11225b7f0c42SBarry Smith         it to satisfy some criteria
1123cf57b110SBarry Smith 
11245b7f0c42SBarry Smith     Collective on Mat
11255b7f0c42SBarry Smith 
11265b7f0c42SBarry Smith     Input Parameters:
11275b7f0c42SBarry Smith +   J - the MatSNESMF matrix
11285b7f0c42SBarry Smith .   fun - the function that checks h
11295b7f0c42SBarry Smith -   ctx - any context needed by the function
11305b7f0c42SBarry Smith 
11315b7f0c42SBarry Smith     Options Database Keys:
11325b7f0c42SBarry Smith .   -snes_mf_check_positivity
11335b7f0c42SBarry Smith 
11345b7f0c42SBarry Smith     Level: advanced
11355b7f0c42SBarry Smith 
11365b7f0c42SBarry Smith     Notes: For example, MatSNESMFSetCheckPositivity() insures that all entries
11375b7f0c42SBarry Smith        of U + h*a are non-negative
11385b7f0c42SBarry Smith 
11395b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckPositivity()
11405b7f0c42SBarry Smith @*/
114161860be5SBarry Smith int MatSNESMFSetCheckh(Mat J,int (*fun)(Vec,Vec,PetscScalar*,void*),void* ctx)
11425b7f0c42SBarry Smith {
114361860be5SBarry Smith   int  ierr,(*f)(Mat,int (*)(Vec,Vec,PetscScalar*,void*),void*);
11445b7f0c42SBarry Smith 
11455b7f0c42SBarry Smith   PetscFunctionBegin;
11465b7f0c42SBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE);
11475b7f0c42SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
11485b7f0c42SBarry Smith   if (f) {
11495b7f0c42SBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
11505b7f0c42SBarry Smith   }
11515b7f0c42SBarry Smith   PetscFunctionReturn(0);
11525b7f0c42SBarry Smith }
11535b7f0c42SBarry Smith 
11545b7f0c42SBarry Smith #undef __FUNCT__
11555b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckPositivity"
11565b7f0c42SBarry Smith /*@
11575b7f0c42SBarry Smith     MatSNESMFCheckPositivity - Checks that all entries in U + h*a are positive or
11585b7f0c42SBarry Smith         zero, decreases h until this is satisfied.
11595b7f0c42SBarry Smith 
11605b7f0c42SBarry Smith     Collective on Vec
11615b7f0c42SBarry Smith 
11625b7f0c42SBarry Smith     Input Parameters:
11635b7f0c42SBarry Smith +   U - base vector that is added to
11645b7f0c42SBarry Smith .   a - vector that is added
11655b7f0c42SBarry Smith .   h - scaling factor on a
11665b7f0c42SBarry Smith -   dummy - context variable (unused)
11675b7f0c42SBarry Smith 
11685b7f0c42SBarry Smith     Options Database Keys:
11695b7f0c42SBarry Smith .   -snes_mf_check_positivity
11705b7f0c42SBarry Smith 
11715b7f0c42SBarry Smith     Level: advanced
11725b7f0c42SBarry Smith 
11735b7f0c42SBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
11745b7f0c42SBarry Smith            MatSNESMFSetCheckh()
11755b7f0c42SBarry Smith 
11765b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckh()
11775b7f0c42SBarry Smith @*/
11785b7f0c42SBarry Smith int MatSNESMFCheckPositivity(Vec U,Vec a,PetscScalar *h,void *dummy)
11795b7f0c42SBarry Smith {
11805b7f0c42SBarry Smith   PetscReal     val, minval;
11815b7f0c42SBarry Smith   PetscScalar   *u_vec, *a_vec;
11825b7f0c42SBarry Smith   int           ierr, i, size;
11835b7f0c42SBarry Smith   MPI_Comm      comm;
11845b7f0c42SBarry Smith 
11855b7f0c42SBarry Smith   PetscFunctionBegin;
11865b7f0c42SBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
11875b7f0c42SBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
11885b7f0c42SBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
11895b7f0c42SBarry Smith   ierr = VecGetLocalSize(U,&size);CHKERRQ(ierr);
119061860be5SBarry Smith   minval = PetscAbsScalar(*h*1.01);
11915b7f0c42SBarry Smith   for(i=0;i<size;i++) {
119261860be5SBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
119361860be5SBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
11945b7f0c42SBarry Smith       if (val < minval) minval = val;
11955b7f0c42SBarry Smith     }
11965b7f0c42SBarry Smith   }
11975b7f0c42SBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
11985b7f0c42SBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
11995b7f0c42SBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
120061860be5SBarry Smith   if (val <= PetscAbsScalar(*h)) {
120161860be5SBarry Smith     PetscLogInfo(U,"MatSNESMFCheckPositivity: Scaling back h from %g to %g\n",PetscRealPart(*h),.99*val);
120261860be5SBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
12035b7f0c42SBarry Smith     else                         *h = -0.99*val;
12045b7f0c42SBarry Smith   }
12055b7f0c42SBarry Smith   PetscFunctionReturn(0);
12065b7f0c42SBarry Smith }
1207cf57b110SBarry Smith 
1208cf57b110SBarry Smith 
1209cf57b110SBarry Smith 
1210cf57b110SBarry Smith 
1211cf57b110SBarry Smith 
1212cf57b110SBarry Smith 
1213cf57b110SBarry Smith 
1214