xref: /petsc/src/snes/mf/snesmfj.c (revision 52baeb724224a860a1ffc4fe94285b3c1fa7ab9a)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
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 
963dd3a1aSKris Buschelman PetscCookie PETSCSNES_DLLEXPORT MATSNESMFCTX_COOKIE = 0;
1046129b97SKris Buschelman PetscEvent  MATSNESMF_Mult = 0;
1146129b97SKris Buschelman 
124a2ae208SSatish Balay #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetType"
14fd4bdd07SBarry Smith /*@C
1565f2ba5bSLois Curfman McInnes     MatSNESMFSetType - Sets the method that is used to compute the
16b0a32e0cSBarry Smith     differencing parameter for finite differene matrix-free formulations.
179a6cb015SBarry Smith 
189a6cb015SBarry Smith     Input Parameters:
197e9d5209SBarry Smith +   mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMF()
207e9d5209SBarry Smith           or MatSetType(mat,MATMFFD);
219a6cb015SBarry Smith -   ftype - the type requested
229a6cb015SBarry Smith 
2315091d37SBarry Smith     Level: advanced
2415091d37SBarry Smith 
2565f2ba5bSLois Curfman McInnes     Notes:
2665f2ba5bSLois Curfman McInnes     For example, such routines can compute h for use in
2765f2ba5bSLois Curfman McInnes     Jacobian-vector products of the form
2865f2ba5bSLois Curfman McInnes 
2965f2ba5bSLois Curfman McInnes                         F(x+ha) - F(x)
30ef4ad1fdSLois Curfman McInnes           F'(u)a  ~=  ----------------
3165f2ba5bSLois Curfman McInnes                               h
3265f2ba5bSLois Curfman McInnes 
33f1af5d2fSBarry Smith .seealso: MatCreateSNESMF(), MatSNESMFRegisterDynamic)
349a6cb015SBarry Smith @*/
3563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetType(Mat mat,const MatSNESMFType ftype)
36b9fa9cd0SBarry Smith {
37dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(MatSNESMFCtx);
387e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
396831982aSBarry Smith   PetscTruth     match;
40a4d4d686SBarry Smith 
41a4d4d686SBarry Smith   PetscFunctionBegin;
424482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
434482741eSBarry Smith   PetscValidCharPointer(ftype,2);
440f5bd95cSBarry Smith 
459a6cb015SBarry Smith   /* already set, so just return */
466831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr);
470f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
48a4d4d686SBarry Smith 
499a6cb015SBarry Smith   /* destroy the old one if it exists */
509a6cb015SBarry Smith   if (ctx->ops->destroy) {
519a6cb015SBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
529a6cb015SBarry Smith   }
539a6cb015SBarry Smith 
5465f2ba5bSLois Curfman McInnes   /* Get the function pointers for the requrested method */
555a655dc6SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
56b9617806SBarry Smith   ierr =  PetscFListFind(ctx->comm,MatSNESMPetscFList,ftype,(void (**)(void)) &r);CHKERRQ(ierr);
57958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatSNESMF type %s given",ftype);
589a6cb015SBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
596831982aSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
609a6cb015SBarry Smith   PetscFunctionReturn(0);
619a6cb015SBarry Smith }
629a6cb015SBarry Smith 
636849ba73SBarry Smith typedef PetscErrorCode (*FCN1)(Vec,void*); /* force argument to next function to not be extern C*/
64c5c390f1SBarry Smith EXTERN_C_BEGIN
6587828ca2SBarry Smith #undef __FUNCT__
6687828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase_FD"
6763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunctioniBase_FD(Mat mat,FCN1 func)
6887828ca2SBarry Smith {
6987828ca2SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
7087828ca2SBarry Smith 
7187828ca2SBarry Smith   PetscFunctionBegin;
7287828ca2SBarry Smith   ctx->funcisetbase = func;
7387828ca2SBarry Smith   PetscFunctionReturn(0);
7487828ca2SBarry Smith }
75c5c390f1SBarry Smith EXTERN_C_END
7687828ca2SBarry Smith 
77a7cc72afSBarry Smith typedef PetscErrorCode (*FCN2)(PetscInt,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
78c5c390f1SBarry Smith EXTERN_C_BEGIN
7987828ca2SBarry Smith #undef __FUNCT__
8087828ca2SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni_FD"
8163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunctioni_FD(Mat mat,FCN2 funci)
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 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegister"
9463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatSNESMFCtx))
959a6cb015SBarry Smith {
96dfbe8321SBarry Smith   PetscErrorCode ierr;
97e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
989a6cb015SBarry Smith 
999a6cb015SBarry Smith   PetscFunctionBegin;
100b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
101c134de8dSSatish Balay   ierr = PetscFListAdd(&MatSNESMPetscFList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
1029a6cb015SBarry Smith   PetscFunctionReturn(0);
1039a6cb015SBarry Smith }
1049a6cb015SBarry Smith 
1059a6cb015SBarry Smith 
1064a2ae208SSatish Balay #undef __FUNCT__
1074a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFRegisterDestroy"
1089a6cb015SBarry Smith /*@C
1095a655dc6SBarry Smith    MatSNESMFRegisterDestroy - Frees the list of MatSNESMF methods that were
110f1af5d2fSBarry Smith    registered by MatSNESMFRegisterDynamic).
1119a6cb015SBarry Smith 
1129a6cb015SBarry Smith    Not Collective
1139a6cb015SBarry Smith 
11415091d37SBarry Smith    Level: developer
11515091d37SBarry Smith 
1165a655dc6SBarry Smith .keywords: MatSNESMF, register, destroy
1179a6cb015SBarry Smith 
118f1af5d2fSBarry Smith .seealso: MatSNESMFRegisterDynamic), MatSNESMFRegisterAll()
1199a6cb015SBarry Smith @*/
12063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFRegisterDestroy(void)
1219a6cb015SBarry Smith {
122dfbe8321SBarry Smith   PetscErrorCode ierr;
1239a6cb015SBarry Smith 
1249a6cb015SBarry Smith   PetscFunctionBegin;
125b0a32e0cSBarry Smith   if (MatSNESMPetscFList) {
126b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&MatSNESMPetscFList);CHKERRQ(ierr);
127b0a32e0cSBarry Smith     MatSNESMPetscFList = 0;
1289a6cb015SBarry Smith   }
1294c49b128SBarry Smith   MatSNESMFRegisterAllCalled = PETSC_FALSE;
1309a6cb015SBarry Smith   PetscFunctionReturn(0);
1319a6cb015SBarry Smith }
1329a6cb015SBarry Smith 
1339a6cb015SBarry Smith /* ----------------------------------------------------------------------------------------*/
1344a2ae208SSatish Balay #undef __FUNCT__
1358a124369SBarry Smith #define __FUNCT__ "MatDestroy_MFFD"
136dfbe8321SBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat)
137a4d4d686SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
1397e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
140fae171e0SBarry Smith 
1413a40ed3dSBarry Smith   PetscFunctionBegin;
142abc0a331SBarry Smith   if (ctx->w) {
143b9fa9cd0SBarry Smith     ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
144ba6a83e5SMatthew Knepley   }
1459a6cb015SBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
14674637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
147d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(ctx);CHKERRQ(ierr);
148901853e0SKris Buschelman 
149901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetBase_C","",PETSC_NULL);CHKERRQ(ierr);
150901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C","",PETSC_NULL);CHKERRQ(ierr);
151901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C","",PETSC_NULL);CHKERRQ(ierr);
152901853e0SKris Buschelman   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatSNESMFSetCheckh_C","",PETSC_NULL);CHKERRQ(ierr);
153901853e0SKris Buschelman 
1543a40ed3dSBarry Smith   PetscFunctionReturn(0);
155b9fa9cd0SBarry Smith }
15650361f65SLois Curfman McInnes 
1574a2ae208SSatish Balay #undef __FUNCT__
1588a124369SBarry Smith #define __FUNCT__ "MatView_MFFD"
15939e2f89bSBarry Smith /*
1608a124369SBarry Smith    MatSNESMFView_MFFD - Views matrix-free parameters.
1618f6e3e37SBarry Smith 
16239e2f89bSBarry Smith */
163dfbe8321SBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer)
164eb9086c3SLois Curfman McInnes {
165dfbe8321SBarry Smith   PetscErrorCode ierr;
1667e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
16732077d6dSBarry Smith   PetscTruth     iascii;
168eb9086c3SLois Curfman McInnes 
1693a40ed3dSBarry Smith   PetscFunctionBegin;
17032077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
17132077d6dSBarry Smith   if (iascii) {
172b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  SNES matrix-free approximation:\n");CHKERRQ(ierr);
173b0a32e0cSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%g (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
174473c83c3SBarry Smith      if (!ctx->type_name) {
175b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
176473c83c3SBarry Smith      } else {
177b0a32e0cSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",ctx->type_name);CHKERRQ(ierr);
178473c83c3SBarry Smith      }
1799a6cb015SBarry Smith      if (ctx->ops->view) {
1809a6cb015SBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
1819a6cb015SBarry Smith      }
1825cd90555SBarry Smith   } else {
18379a5c55eSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for SNES matrix free matrix",((PetscObject)viewer)->type_name);
184eb9086c3SLois Curfman McInnes   }
1853a40ed3dSBarry Smith   PetscFunctionReturn(0);
186eb9086c3SLois Curfman McInnes }
187eb9086c3SLois Curfman McInnes 
1884a2ae208SSatish Balay #undef __FUNCT__
1898a124369SBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD"
190be726c96SBarry Smith /*
19132dfb669SBarry Smith    MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This
19265f2ba5bSLois Curfman McInnes    allows the user to indicate the beginning of a new linear solve by calling
193be726c96SBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
19465f2ba5bSLois Curfman McInnes    MatSNESMFCreate_WP() to properly compute ||U|| only the first time
19565f2ba5bSLois Curfman McInnes    in the linear solver rather than every time.
196be726c96SBarry Smith */
197dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt)
198be726c96SBarry Smith {
199dfbe8321SBarry Smith   PetscErrorCode ierr;
2007e9d5209SBarry Smith   MatSNESMFCtx   j = (MatSNESMFCtx)J->data;
201be726c96SBarry Smith 
202be726c96SBarry Smith   PetscFunctionBegin;
2035a655dc6SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
204b0a32e0cSBarry Smith   if (j->usesnes) {
2051d1367b7SBarry Smith     ierr = SNESGetSolution(j->snes,&j->current_u);CHKERRQ(ierr);
2061d1367b7SBarry Smith     ierr = SNESGetFunction(j->snes,&j->current_f,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
207958c9bccSBarry Smith     if (!j->w) {
2082740c1caSMatthew Knepley       ierr = VecDuplicate(j->current_u, &j->w);CHKERRQ(ierr);
2092740c1caSMatthew Knepley     }
2101d1367b7SBarry Smith   }
211c5c390f1SBarry Smith   j->vshift = 0.0;
212c5c390f1SBarry Smith   j->vscale = 1.0;
213be726c96SBarry Smith   PetscFunctionReturn(0);
214be726c96SBarry Smith }
215be726c96SBarry Smith 
2164a2ae208SSatish Balay #undef __FUNCT__
2178a124369SBarry Smith #define __FUNCT__ "MatMult_MFFD"
218eb9086c3SLois Curfman McInnes /*
219adb62b0dSMatthew Knepley   MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
220a4d4d686SBarry Smith 
2219a6cb015SBarry Smith         y ~= (F(u + ha) - F(u))/h,
222eb9086c3SLois Curfman McInnes   where F = nonlinear function, as set by SNESSetFunction()
223eb9086c3SLois Curfman McInnes         u = current iterate
224eb9086c3SLois Curfman McInnes         h = difference interval
225eb9086c3SLois Curfman McInnes */
226dfbe8321SBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y)
22739e2f89bSBarry Smith {
2287e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
229fae171e0SBarry Smith   SNES           snes;
230ea709b57SSatish Balay   PetscScalar    h,mone = -1.0;
231fae171e0SBarry Smith   Vec            w,U,F;
232dfbe8321SBarry Smith   PetscErrorCode ierr,(*eval_fct)(SNES,Vec,Vec)=0;
2334dc4c822SBarry Smith   PetscTruth     zeroa;
23439e2f89bSBarry Smith 
2353a40ed3dSBarry Smith   PetscFunctionBegin;
2369a6cb015SBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
2379a6cb015SBarry Smith      separate the performance monitoring from the cases that use conventional
2389a6cb015SBarry Smith      storage.  We may eventually modify event logging to associate events
2399a6cb015SBarry Smith      with particular objects, hence alleviating the more general problem. */
24046129b97SKris Buschelman   ierr = PetscLogEventBegin(MATSNESMF_Mult,a,y,0,0);CHKERRQ(ierr);
24156cd22aeSBarry Smith 
242fae171e0SBarry Smith   snes = ctx->snes;
243fae171e0SBarry Smith   w    = ctx->w;
2441d1367b7SBarry Smith   U    = ctx->current_u;
24550361f65SLois Curfman McInnes 
24685614651SBarry Smith   /*
24785614651SBarry Smith       Compute differencing parameter
24885614651SBarry Smith   */
2499a6cb015SBarry Smith   if (!ctx->ops->compute) {
2502f859189SBarry Smith     ierr = MatSNESMFSetType(mat,MATSNESMF_WP);CHKERRQ(ierr);
2515a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(mat);CHKERRQ(ierr);
2529a6cb015SBarry Smith   }
2534dc4c822SBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h,&zeroa);CHKERRQ(ierr);
2544dc4c822SBarry Smith   if (zeroa) {
2554dc4c822SBarry Smith     PetscScalar zero = 0.0;
2564dc4c822SBarry Smith     ierr = VecSet(y,zero);CHKERRQ(ierr);
2574dc4c822SBarry Smith     PetscFunctionReturn(0);
2584dc4c822SBarry Smith   }
259a4d4d686SBarry Smith 
2605b7f0c42SBarry Smith   if (ctx->checkh) {
2615b7f0c42SBarry Smith     ierr = (*ctx->checkh)(U,a,&h,ctx->checkhctx);CHKERRQ(ierr);
2625b7f0c42SBarry Smith   }
2635b7f0c42SBarry Smith 
264a4d4d686SBarry Smith   /* keep a record of the current differencing parameter h */
265a4d4d686SBarry Smith   ctx->currenth = h;
266aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
26763ba0a88SBarry Smith   ierr = PetscLogInfo((mat,"MatMult_MFFD:Current differencing parameter: %g + %g i\n",PetscRealPart(h),PetscImaginaryPart(h)));CHKERRQ(ierr);
268a4d4d686SBarry Smith #else
26963ba0a88SBarry Smith   ierr = PetscLogInfo((mat,"MatMult_MFFD:Current differencing parameter: %15.12e\n",h));CHKERRQ(ierr);
270a4d4d686SBarry Smith #endif
271a4d4d686SBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
27285614651SBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
273a4d4d686SBarry Smith   }
27485614651SBarry Smith   ctx->ncurrenth++;
275a4d4d686SBarry Smith 
27685614651SBarry Smith   /* w = u + ha */
2772dcb1b2aSMatthew Knepley   ierr = VecWAXPY(w,h,a,U);CHKERRQ(ierr);
27885614651SBarry Smith 
279b0a32e0cSBarry Smith   if (ctx->usesnes) {
28085614651SBarry Smith     eval_fct = SNESComputeFunction;
2811d1367b7SBarry Smith     F    = ctx->current_f;
2821302d50aSBarry Smith     if (!F) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"You must call MatAssembly() even on matrix-free matrices");
28339903ad8SBarry Smith     ierr = (*eval_fct)(snes,w,y);CHKERRQ(ierr);
28485614651SBarry Smith   } else {
28585614651SBarry Smith     F = ctx->funcvec;
28685614651SBarry Smith     /* compute func(U) as base for differencing */
28785614651SBarry Smith     if (ctx->ncurrenth == 1) {
28885614651SBarry Smith       ierr = (*ctx->func)(snes,U,F,ctx->funcctx);CHKERRQ(ierr);
28985614651SBarry Smith     }
29085614651SBarry Smith     ierr = (*ctx->func)(snes,w,y,ctx->funcctx);CHKERRQ(ierr);
29185614651SBarry Smith   }
292a4d4d686SBarry Smith 
2932dcb1b2aSMatthew Knepley   ierr = VecAXPY(y,mone,F);CHKERRQ(ierr);
294a4d4d686SBarry Smith   h    = 1.0/h;
2952dcb1b2aSMatthew Knepley   ierr = VecScale(y,h);CHKERRQ(ierr);
296c5c390f1SBarry Smith 
2972dcb1b2aSMatthew Knepley   ierr = VecAXPBY(y,ctx->vshift,ctx->vscale,a);CHKERRQ(ierr);
298c5c390f1SBarry Smith 
29974637425SBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
300a4d4d686SBarry Smith 
30146129b97SKris Buschelman   ierr = PetscLogEventEnd(MATSNESMF_Mult,a,y,0,0);CHKERRQ(ierr);
302a4d4d686SBarry Smith   PetscFunctionReturn(0);
303a4d4d686SBarry Smith }
304a4d4d686SBarry Smith 
3054a2ae208SSatish Balay #undef __FUNCT__
3068a124369SBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD"
307cf57b110SBarry Smith /*
3088a124369SBarry Smith   MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix
309cf57b110SBarry Smith 
310cf57b110SBarry Smith         y ~= (F(u + ha) - F(u))/h,
311cf57b110SBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
312cf57b110SBarry Smith         u = current iterate
313cf57b110SBarry Smith         h = difference interval
314cf57b110SBarry Smith */
315dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a)
316cf57b110SBarry Smith {
3177e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)mat->data;
318ea709b57SSatish Balay   PetscScalar    h,*aa,*ww,v;
31977d8c4bbSBarry Smith   PetscReal      epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON;
32065df01d8SBarry Smith   Vec            w,U;
3216849ba73SBarry Smith   PetscErrorCode ierr;
322a7cc72afSBarry Smith   PetscInt       i,rstart,rend;
323cf57b110SBarry Smith 
324cf57b110SBarry Smith   PetscFunctionBegin;
325cf57b110SBarry Smith   if (!ctx->funci) {
3261302d50aSBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Requires calling MatSNESMFSetFunctioni() first");
327cf57b110SBarry Smith   }
328cf57b110SBarry Smith 
329cf57b110SBarry Smith   w    = ctx->w;
330cf57b110SBarry Smith   U    = ctx->current_u;
331cf57b110SBarry Smith   ierr = (*ctx->func)(0,U,a,ctx->funcctx);CHKERRQ(ierr);
332cf57b110SBarry Smith   ierr = (*ctx->funcisetbase)(U,ctx->funcctx);CHKERRQ(ierr);
333cf57b110SBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
334cf57b110SBarry Smith 
335cf57b110SBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
336cf57b110SBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
337cf57b110SBarry Smith   for (i=rstart; i<rend; i++) {
338cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
339cf57b110SBarry Smith     h  = ww[i-rstart];
340cf57b110SBarry Smith     if (h == 0.0) h = 1.0;
341cf57b110SBarry Smith #if !defined(PETSC_USE_COMPLEX)
342cf57b110SBarry Smith     if (h < umin && h >= 0.0)      h = umin;
343cf57b110SBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
344cf57b110SBarry Smith #else
345cf57b110SBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
346cf57b110SBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
347cf57b110SBarry Smith #endif
348cf57b110SBarry Smith     h     *= epsilon;
349cf57b110SBarry Smith 
350cf57b110SBarry Smith     ww[i-rstart] += h;
351cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
352cf57b110SBarry Smith     ierr          = (*ctx->funci)(i,w,&v,ctx->funcctx);CHKERRQ(ierr);
353cf57b110SBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
354c5c390f1SBarry Smith 
355c5c390f1SBarry Smith     /* possibly shift and scale result */
356c5c390f1SBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
357c5c390f1SBarry Smith 
358cf57b110SBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
359cf57b110SBarry Smith     ww[i-rstart] -= h;
360cf57b110SBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
361cf57b110SBarry Smith   }
362cf57b110SBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
363cf57b110SBarry Smith   PetscFunctionReturn(0);
364cf57b110SBarry Smith }
365cf57b110SBarry Smith 
366cf57b110SBarry Smith #undef __FUNCT__
367c5c390f1SBarry Smith #define __FUNCT__ "MatShift_MFFD"
368f4df32b1SMatthew Knepley PetscErrorCode MatShift_MFFD(Mat Y,PetscScalar a)
369c5c390f1SBarry Smith {
370c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
371c5c390f1SBarry Smith   PetscFunctionBegin;
372f4df32b1SMatthew Knepley   shell->vshift += a;
373c5c390f1SBarry Smith   PetscFunctionReturn(0);
374c5c390f1SBarry Smith }
375c5c390f1SBarry Smith 
376c5c390f1SBarry Smith #undef __FUNCT__
377c5c390f1SBarry Smith #define __FUNCT__ "MatScale_MFFD"
378f4df32b1SMatthew Knepley PetscErrorCode MatScale_MFFD(Mat Y,PetscScalar a)
379c5c390f1SBarry Smith {
380c5c390f1SBarry Smith   MatSNESMFCtx shell = (MatSNESMFCtx)Y->data;
381c5c390f1SBarry Smith   PetscFunctionBegin;
382f4df32b1SMatthew Knepley   shell->vscale *= a;
383c5c390f1SBarry Smith   PetscFunctionReturn(0);
384c5c390f1SBarry Smith }
385c5c390f1SBarry Smith 
386c5c390f1SBarry Smith 
387c5c390f1SBarry Smith #undef __FUNCT__
3884a2ae208SSatish Balay #define __FUNCT__ "MatCreateSNESMF"
389*52baeb72SSatish Balay /*@
39065f2ba5bSLois Curfman McInnes    MatCreateSNESMF - Creates a matrix-free matrix context for use with
39165f2ba5bSLois Curfman McInnes    a SNES solver.  This matrix can be used as the Jacobian argument for
39265f2ba5bSLois Curfman McInnes    the routine SNESSetJacobian().
393a4d4d686SBarry Smith 
394a4d4d686SBarry Smith    Collective on SNES and Vec
395a4d4d686SBarry Smith 
396a4d4d686SBarry Smith    Input Parameters:
397a4d4d686SBarry Smith +  snes - the SNES context
398a4d4d686SBarry Smith -  x - vector where SNES solution is to be stored.
399a4d4d686SBarry Smith 
400a4d4d686SBarry Smith    Output Parameter:
401a4d4d686SBarry Smith .  J - the matrix-free matrix
402a4d4d686SBarry Smith 
40315091d37SBarry Smith    Level: advanced
40415091d37SBarry Smith 
405a4d4d686SBarry Smith    Notes:
406a4d4d686SBarry Smith    The matrix-free matrix context merely contains the function pointers
407a4d4d686SBarry Smith    and work space for performing finite difference approximations of
40865f2ba5bSLois Curfman McInnes    Jacobian-vector products, F'(u)*a,
4099a6cb015SBarry Smith 
4109a6cb015SBarry Smith    The default code uses the following approach to compute h
411a4d4d686SBarry Smith 
412a4d4d686SBarry Smith .vb
41365f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
414a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
415a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
416a4d4d686SBarry Smith  where
417a4d4d686SBarry Smith      error_rel = square root of relative error in function evaluation
418a4d4d686SBarry Smith      umin = minimum iterate parameter
419a4d4d686SBarry Smith .ve
420a4d4d686SBarry Smith 
4215a655dc6SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
42265f2ba5bSLois Curfman McInnes    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
42365f2ba5bSLois Curfman McInnes    of the users manual for details.
424a4d4d686SBarry Smith 
425a4d4d686SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
426a4d4d686SBarry Smith    matrix context.
427a4d4d686SBarry Smith 
428a4d4d686SBarry Smith    Options Database Keys:
429a4d4d686SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
4309a6cb015SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
431a4d4d686SBarry Smith -  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
432a4d4d686SBarry Smith 
433a4d4d686SBarry Smith .keywords: SNES, default, matrix-free, create, matrix
434a4d4d686SBarry Smith 
4355a655dc6SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
4361d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateMF(),
437fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic), MatSNESMFComputeJacobian()
438a4d4d686SBarry Smith 
439a4d4d686SBarry Smith @*/
44063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatCreateSNESMF(SNES snes,Vec x,Mat *J)
441a4d4d686SBarry Smith {
4421d1367b7SBarry Smith   MatSNESMFCtx   mfctx;
443dfbe8321SBarry Smith   PetscErrorCode ierr;
4441d1367b7SBarry Smith 
4451d1367b7SBarry Smith   PetscFunctionBegin;
4461d1367b7SBarry Smith   ierr = MatCreateMF(x,J);CHKERRQ(ierr);
4477e9d5209SBarry Smith 
4487e9d5209SBarry Smith   mfctx          = (MatSNESMFCtx)(*J)->data;
4491d1367b7SBarry Smith   mfctx->snes    = snes;
450b0a32e0cSBarry Smith   mfctx->usesnes = PETSC_TRUE;
45152e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,*J);CHKERRQ(ierr);
4521d1367b7SBarry Smith   PetscFunctionReturn(0);
4531d1367b7SBarry Smith }
4541d1367b7SBarry Smith 
455cf3bea43SBarry Smith EXTERN_C_BEGIN
456cf3bea43SBarry Smith #undef __FUNCT__
457cf3bea43SBarry Smith #define __FUNCT__ "MatSNESMFSetBase_FD"
45863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetBase_FD(Mat J,Vec U)
459cf3bea43SBarry Smith {
460dfbe8321SBarry Smith   PetscErrorCode ierr;
4617e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
462cf3bea43SBarry Smith 
463cf3bea43SBarry Smith   PetscFunctionBegin;
464cf3bea43SBarry Smith   ierr = MatSNESMFResetHHistory(J);CHKERRQ(ierr);
465cf3bea43SBarry Smith   ctx->current_u = U;
466cf3bea43SBarry Smith   ctx->usesnes   = PETSC_FALSE;
467958c9bccSBarry Smith   if (!ctx->w) {
468ba6a83e5SMatthew Knepley     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
469ba6a83e5SMatthew Knepley   }
47032dfb669SBarry Smith   J->assembled = PETSC_TRUE;
471cf3bea43SBarry Smith   PetscFunctionReturn(0);
472cf3bea43SBarry Smith }
473cf3bea43SBarry Smith EXTERN_C_END
474cf3bea43SBarry Smith 
4756849ba73SBarry Smith typedef PetscErrorCode (*FCN3)(Vec,Vec,PetscScalar*,void*); /* force argument to next function to not be extern C*/
4765b7f0c42SBarry Smith EXTERN_C_BEGIN
4775b7f0c42SBarry Smith #undef __FUNCT__
4785b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh_FD"
47963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetCheckh_FD(Mat J,FCN3 fun,void*ectx)
4805b7f0c42SBarry Smith {
4815b7f0c42SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
4825b7f0c42SBarry Smith 
4835b7f0c42SBarry Smith   PetscFunctionBegin;
4845b7f0c42SBarry Smith   ctx->checkh    = fun;
4855b7f0c42SBarry Smith   ctx->checkhctx = ectx;
4865b7f0c42SBarry Smith   PetscFunctionReturn(0);
4875b7f0c42SBarry Smith }
4885b7f0c42SBarry Smith EXTERN_C_END
4895b7f0c42SBarry Smith 
4904a2ae208SSatish Balay #undef __FUNCT__
4917e9d5209SBarry Smith #define __FUNCT__ "MatSNESMFSetFromOptions"
4927e9d5209SBarry Smith /*@
4937e9d5209SBarry Smith    MatSNESMFSetFromOptions - Sets the MatSNESMF options from the command line
4947e9d5209SBarry Smith    parameter.
4957e9d5209SBarry Smith 
4967e9d5209SBarry Smith    Collective on Mat
4977e9d5209SBarry Smith 
4987e9d5209SBarry Smith    Input Parameters:
4997e9d5209SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
5007e9d5209SBarry Smith 
5017e9d5209SBarry Smith    Options Database Keys:
5027e9d5209SBarry Smith +  -snes_mf_type - <default,wp>
5037e9d5209SBarry Smith -  -snes_mf_err - square root of estimated relative error in function evaluation
5047e9d5209SBarry Smith -  -snes_mf_period - how often h is recomputed, defaults to 1, everytime
5057e9d5209SBarry Smith 
5067e9d5209SBarry Smith    Level: advanced
5077e9d5209SBarry Smith 
5087e9d5209SBarry Smith .keywords: SNES, matrix-free, parameters
5097e9d5209SBarry Smith 
5107e9d5209SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
5117e9d5209SBarry Smith           MatSNESMFResetHHistory(), MatSNESMFKSPMonitor()
5127e9d5209SBarry Smith @*/
51363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFromOptions(Mat mat)
5147e9d5209SBarry Smith {
5157e9d5209SBarry Smith   MatSNESMFCtx   mfctx = (MatSNESMFCtx)mat->data;
516dfbe8321SBarry Smith   PetscErrorCode ierr;
5177e9d5209SBarry Smith   PetscTruth     flg;
5187e9d5209SBarry Smith   char           ftype[256];
5197e9d5209SBarry Smith 
5207e9d5209SBarry Smith   PetscFunctionBegin;
5217e9d5209SBarry Smith   if (!MatSNESMFRegisterAllCalled) {ierr = MatSNESMFRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
5227e9d5209SBarry Smith 
5237e9d5209SBarry Smith   ierr = PetscOptionsBegin(mfctx->comm,mfctx->prefix,"Set matrix free computation parameters","MatSNESMF");CHKERRQ(ierr);
5247e9d5209SBarry Smith   ierr = PetscOptionsList("-snes_mf_type","Matrix free type","MatSNESMFSetType",MatSNESMPetscFList,mfctx->type_name,ftype,256,&flg);CHKERRQ(ierr);
5257e9d5209SBarry Smith   if (flg) {
5267e9d5209SBarry Smith     ierr = MatSNESMFSetType(mat,ftype);CHKERRQ(ierr);
5277e9d5209SBarry Smith   }
5287e9d5209SBarry Smith 
52987828ca2SBarry Smith   ierr = PetscOptionsReal("-snes_mf_err","set sqrt relative error in function","MatSNESMFSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
5307e9d5209SBarry Smith   ierr = PetscOptionsInt("-snes_mf_period","how often h is recomputed","MatSNESMFSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
5317e9d5209SBarry Smith   if (mfctx->snes) {
5327e9d5209SBarry Smith     ierr = PetscOptionsName("-snes_mf_ksp_monitor","Monitor matrix-free parameters","MatSNESMFKSPMonitor",&flg);CHKERRQ(ierr);
5337e9d5209SBarry Smith     if (flg) {
5347e9d5209SBarry Smith       KSP ksp;
53594b7f48cSBarry Smith       ierr = SNESGetKSP(mfctx->snes,&ksp);CHKERRQ(ierr);
5367e9d5209SBarry Smith       ierr = KSPSetMonitor(ksp,MatSNESMFKSPMonitor,PETSC_NULL,0);CHKERRQ(ierr);
5377e9d5209SBarry Smith     }
5387e9d5209SBarry Smith   }
5395b7f0c42SBarry Smith   ierr = PetscOptionsName("-snes_mf_check_positivity","Insure that U + h*a is nonnegative","MatSNESMFSetCheckh",&flg);CHKERRQ(ierr);
5405b7f0c42SBarry Smith   if (flg) {
5415b7f0c42SBarry Smith     ierr = MatSNESMFSetCheckh(mat,MatSNESMFCheckPositivity,0);CHKERRQ(ierr);
5425b7f0c42SBarry Smith   }
5437e9d5209SBarry Smith   if (mfctx->ops->setfromoptions) {
5447e9d5209SBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
5457e9d5209SBarry Smith   }
5467e9d5209SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
5477e9d5209SBarry Smith   PetscFunctionReturn(0);
5487e9d5209SBarry Smith }
5497e9d5209SBarry Smith 
5500bad9183SKris Buschelman /*MC
551fafad747SKris Buschelman   MATMFFD - MATMFFD = "mffd" - A matrix free matrix type.
5520bad9183SKris Buschelman 
5530bad9183SKris Buschelman   Level: advanced
5540bad9183SKris Buschelman 
5558bc8193eSBarry Smith .seealso: MatCreateMF(), MatCreateSNESMF()
5560bad9183SKris Buschelman M*/
557fe93831dSBarry Smith EXTERN_C_BEGIN
5587e9d5209SBarry Smith #undef __FUNCT__
5597e9d5209SBarry Smith #define __FUNCT__ "MatCreate_MFFD"
56063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatCreate_MFFD(Mat A)
5617e9d5209SBarry Smith {
5627e9d5209SBarry Smith   MatSNESMFCtx mfctx;
563dfbe8321SBarry Smith   PetscErrorCode ierr;
5647e9d5209SBarry Smith 
5657e9d5209SBarry Smith   PetscFunctionBegin;
5666e087cb5SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5676e087cb5SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
5686e087cb5SMatthew Knepley #endif
5696e087cb5SMatthew Knepley 
57052e6d16bSBarry Smith   ierr = PetscHeaderCreate(mfctx,_p_MatSNESMFCtx,struct _MFOps,MATSNESMFCTX_COOKIE,0,"SNESMF",A->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr);
5717e9d5209SBarry Smith   mfctx->sp              = 0;
5727e9d5209SBarry Smith   mfctx->snes            = 0;
57377d8c4bbSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
5747e9d5209SBarry Smith   mfctx->recomputeperiod = 1;
5757e9d5209SBarry Smith   mfctx->count           = 0;
5767e9d5209SBarry Smith   mfctx->currenth        = 0.0;
5777e9d5209SBarry Smith   mfctx->historyh        = PETSC_NULL;
5787e9d5209SBarry Smith   mfctx->ncurrenth       = 0;
5797e9d5209SBarry Smith   mfctx->maxcurrenth     = 0;
5807e9d5209SBarry Smith   mfctx->type_name       = 0;
5817e9d5209SBarry Smith   mfctx->usesnes         = PETSC_FALSE;
5827e9d5209SBarry Smith 
583c5c390f1SBarry Smith   mfctx->vshift          = 0.0;
584c5c390f1SBarry Smith   mfctx->vscale          = 1.0;
585c5c390f1SBarry Smith 
5867e9d5209SBarry Smith   /*
5877e9d5209SBarry Smith      Create the empty data structure to contain compute-h routines.
5887e9d5209SBarry Smith      These will be filled in below from the command line options or
5897e9d5209SBarry Smith      a later call with MatSNESMFSetType() or if that is not called
5908a124369SBarry Smith      then it will default in the first use of MatMult_MFFD()
5917e9d5209SBarry Smith   */
5927e9d5209SBarry Smith   mfctx->ops->compute        = 0;
5937e9d5209SBarry Smith   mfctx->ops->destroy        = 0;
5947e9d5209SBarry Smith   mfctx->ops->view           = 0;
5957e9d5209SBarry Smith   mfctx->ops->setfromoptions = 0;
5967e9d5209SBarry Smith   mfctx->hctx                = 0;
5977e9d5209SBarry Smith 
5987e9d5209SBarry Smith   mfctx->func                = 0;
5997e9d5209SBarry Smith   mfctx->funcctx             = 0;
6007e9d5209SBarry Smith   mfctx->funcvec             = 0;
601ba6a83e5SMatthew Knepley   mfctx->w                   = PETSC_NULL;
6027e9d5209SBarry Smith 
60365df01d8SBarry Smith   A->data                = mfctx;
6047e9d5209SBarry Smith 
6058a124369SBarry Smith   A->ops->mult           = MatMult_MFFD;
6068a124369SBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
6078a124369SBarry Smith   A->ops->view           = MatView_MFFD;
6088a124369SBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
6098a124369SBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
610c5c390f1SBarry Smith   A->ops->scale          = MatScale_MFFD;
611c5c390f1SBarry Smith   A->ops->shift          = MatShift_MFFD;
61265df01d8SBarry Smith   A->ops->setfromoptions = MatSNESMFSetFromOptions;
61332dfb669SBarry Smith   A->assembled = PETSC_TRUE;
6147e9d5209SBarry Smith 
61565df01d8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetBase_C","MatSNESMFSetBase_FD",MatSNESMFSetBase_FD);CHKERRQ(ierr);
616c5c390f1SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioniBase_C","MatSNESMFSetFunctioniBase_FD",MatSNESMFSetFunctioniBase_FD);CHKERRQ(ierr);
61787828ca2SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetFunctioni_C","MatSNESMFSetFunctioni_FD",MatSNESMFSetFunctioni_FD);CHKERRQ(ierr);
6185b7f0c42SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSNESMFSetCheckh_C","MatSNESMFSetCheckh_FD",MatSNESMFSetCheckh_FD);CHKERRQ(ierr);
61965df01d8SBarry Smith   mfctx->mat = A;
6207e9d5209SBarry Smith 
6217e9d5209SBarry Smith   PetscFunctionReturn(0);
6227e9d5209SBarry Smith }
623fe93831dSBarry Smith EXTERN_C_END
6247e9d5209SBarry Smith 
6257e9d5209SBarry Smith #undef __FUNCT__
6264a2ae208SSatish Balay #define __FUNCT__ "MatCreateMF"
627*52baeb72SSatish Balay /*@
6281d1367b7SBarry Smith    MatCreateMF - Creates a matrix-free matrix. See also MatCreateSNESMF()
6291d1367b7SBarry Smith 
6301d1367b7SBarry Smith    Collective on Vec
6311d1367b7SBarry Smith 
6321d1367b7SBarry Smith    Input Parameters:
6331d1367b7SBarry Smith .  x - vector that defines layout of the vectors and matrices
6341d1367b7SBarry Smith 
6351d1367b7SBarry Smith    Output Parameter:
6361d1367b7SBarry Smith .  J - the matrix-free matrix
6371d1367b7SBarry Smith 
6381d1367b7SBarry Smith    Level: advanced
6391d1367b7SBarry Smith 
6401d1367b7SBarry Smith    Notes:
6411d1367b7SBarry Smith    The matrix-free matrix context merely contains the function pointers
6421d1367b7SBarry Smith    and work space for performing finite difference approximations of
6431d1367b7SBarry Smith    Jacobian-vector products, F'(u)*a,
6441d1367b7SBarry Smith 
6451d1367b7SBarry Smith    The default code uses the following approach to compute h
6461d1367b7SBarry Smith 
6471d1367b7SBarry Smith .vb
6481d1367b7SBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
6491d1367b7SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
6501d1367b7SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
6511d1367b7SBarry Smith  where
6521d1367b7SBarry Smith      error_rel = square root of relative error in function evaluation
6531d1367b7SBarry Smith      umin = minimum iterate parameter
6541d1367b7SBarry Smith .ve
6551d1367b7SBarry Smith 
6561d1367b7SBarry Smith    The user can set the error_rel via MatSNESMFSetFunctionError() and
6571d1367b7SBarry Smith    umin via MatSNESMFDefaultSetUmin(); see the nonlinear solvers chapter
6581d1367b7SBarry Smith    of the users manual for details.
6591d1367b7SBarry Smith 
6601d1367b7SBarry Smith    The user should call MatDestroy() when finished with the matrix-free
6611d1367b7SBarry Smith    matrix context.
6621d1367b7SBarry Smith 
6631d1367b7SBarry Smith    Options Database Keys:
6641d1367b7SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
6651d1367b7SBarry Smith .  -snes_mf_unim <umin> - Sets umin (for default PETSc routine that computes h only)
6665b7f0c42SBarry Smith .  -snes_mf_ksp_monitor - KSP monitor routine that prints differencing h
6675b7f0c42SBarry Smith -  -snes_mf_check_positivity
6681d1367b7SBarry Smith 
6691d1367b7SBarry Smith .keywords: default, matrix-free, create, matrix
6701d1367b7SBarry Smith 
6711d1367b7SBarry Smith .seealso: MatDestroy(), MatSNESMFSetFunctionError(), MatSNESMFDefaultSetUmin()
6721d1367b7SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(), MatCreateSNESMF(),
673fed8bd04SBarry Smith           MatSNESMFGetH(),MatSNESMFKSPMonitor(), MatSNESMFRegisterDynamic),, MatSNESMFComputeJacobian()
6741d1367b7SBarry Smith 
6751d1367b7SBarry Smith @*/
67663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatCreateMF(Vec x,Mat *J)
6771d1367b7SBarry Smith {
678a4d4d686SBarry Smith   MPI_Comm       comm;
6796849ba73SBarry Smith   PetscErrorCode ierr;
680a7cc72afSBarry Smith   PetscInt       n,nloc;
681a4d4d686SBarry Smith 
682a4d4d686SBarry Smith   PetscFunctionBegin;
6831d1367b7SBarry Smith   ierr = PetscObjectGetComm((PetscObject)x,&comm);CHKERRQ(ierr);
68465df01d8SBarry Smith   ierr = VecGetSize(x,&n);CHKERRQ(ierr);
68565df01d8SBarry Smith   ierr = VecGetLocalSize(x,&nloc);CHKERRQ(ierr);
686f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,J);CHKERRQ(ierr);
687f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*J,nloc,nloc,n,n);CHKERRQ(ierr);
688e56c5435SBarry Smith   ierr = MatRegisterDynamic(MATMFFD,0,"MatCreate_MFFD",MatCreate_MFFD);CHKERRQ(ierr);
68965df01d8SBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
6909a6cb015SBarry Smith   PetscFunctionReturn(0);
6919a6cb015SBarry Smith }
6929a6cb015SBarry Smith 
693a4d4d686SBarry Smith 
6944a2ae208SSatish Balay #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFGetH"
696a4d4d686SBarry Smith /*@
69765f2ba5bSLois Curfman McInnes    MatSNESMFGetH - Gets the last value that was used as the differencing
698a4d4d686SBarry Smith    parameter.
699a4d4d686SBarry Smith 
700a4d4d686SBarry Smith    Not Collective
701a4d4d686SBarry Smith 
702a4d4d686SBarry Smith    Input Parameters:
7035a655dc6SBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
704a4d4d686SBarry Smith 
705a4d4d686SBarry Smith    Output Paramter:
706a4d4d686SBarry Smith .  h - the differencing step size
707a4d4d686SBarry Smith 
70815091d37SBarry Smith    Level: advanced
70915091d37SBarry Smith 
710a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
711a4d4d686SBarry Smith 
7125a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFSetHHistory(),
7135a655dc6SBarry Smith           MatSNESMFResetHHistory(),MatSNESMFKSPMonitor()
714a4d4d686SBarry Smith @*/
71563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFGetH(Mat mat,PetscScalar *h)
716a4d4d686SBarry Smith {
7177e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
718a4d4d686SBarry Smith 
719a4d4d686SBarry Smith   PetscFunctionBegin;
720a4d4d686SBarry Smith   *h = ctx->currenth;
721a4d4d686SBarry Smith   PetscFunctionReturn(0);
722a4d4d686SBarry Smith }
723a4d4d686SBarry Smith 
7244a2ae208SSatish Balay #undef __FUNCT__
7254a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFKSPMonitor"
726a4d4d686SBarry Smith /*
7275a655dc6SBarry Smith    MatSNESMFKSPMonitor - A KSP monitor for use with the default PETSc
72865f2ba5bSLois Curfman McInnes    SNES matrix free routines. Prints the differencing parameter used at
72965f2ba5bSLois Curfman McInnes    each step.
730a4d4d686SBarry Smith */
73163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFKSPMonitor(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy)
732a4d4d686SBarry Smith {
733a4d4d686SBarry Smith   PC             pc;
7345a655dc6SBarry Smith   MatSNESMFCtx   ctx;
735dfbe8321SBarry Smith   PetscErrorCode ierr;
736a4d4d686SBarry Smith   Mat            mat;
737a4d4d686SBarry Smith   MPI_Comm       comm;
738a4d4d686SBarry Smith   PetscTruth     nonzeroinitialguess;
739a4d4d686SBarry Smith 
740a4d4d686SBarry Smith   PetscFunctionBegin;
741a4d4d686SBarry Smith   ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
742a4d4d686SBarry Smith   ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
743a4d4d686SBarry Smith   ierr = KSPGetInitialGuessNonzero(ksp,&nonzeroinitialguess);CHKERRQ(ierr);
744a4d4d686SBarry Smith   ierr = PCGetOperators(pc,&mat,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7457e9d5209SBarry Smith   ctx  = (MatSNESMFCtx)mat->data;
7467e9d5209SBarry Smith 
747a4d4d686SBarry Smith   if (n > 0 || nonzeroinitialguess) {
748aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
74977431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e h %g + %g i\n",n,rnorm,
750329f5518SBarry Smith                 PetscRealPart(ctx->currenth),PetscImaginaryPart(ctx->currenth));CHKERRQ(ierr);
751a4d4d686SBarry Smith #else
75277431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e h %g \n",n,rnorm,ctx->currenth);CHKERRQ(ierr);
753a4d4d686SBarry Smith #endif
754a4d4d686SBarry Smith   } else {
75577431f27SBarry Smith     ierr = PetscPrintf(comm,"%D KSP Residual norm %14.12e\n",n,rnorm);CHKERRQ(ierr);
756a4d4d686SBarry Smith   }
757a4d4d686SBarry Smith   PetscFunctionReturn(0);
758a4d4d686SBarry Smith }
759a4d4d686SBarry Smith 
7604a2ae208SSatish Balay #undef __FUNCT__
7614a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunction"
76285614651SBarry Smith /*@C
76385614651SBarry Smith    MatSNESMFSetFunction - Sets the function used in applying the matrix free.
76485614651SBarry Smith 
76585614651SBarry Smith    Collective on Mat
76685614651SBarry Smith 
76785614651SBarry Smith    Input Parameters:
76885614651SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
76985614651SBarry Smith .  v   - workspace vector
77085614651SBarry Smith .  func - the function to use
77185614651SBarry Smith -  funcctx - optional function context passed to function
77285614651SBarry Smith 
77385614651SBarry Smith    Level: advanced
77485614651SBarry Smith 
77585614651SBarry Smith    Notes:
77685614651SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
77785614651SBarry Smith     matrix inside your compute Jacobian routine
77885614651SBarry Smith 
77985614651SBarry Smith     If this is not set then it will use the function set with SNESSetFunction()
78085614651SBarry Smith 
78185614651SBarry Smith .keywords: SNES, matrix-free, function
78285614651SBarry Smith 
78385614651SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
78485614651SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
78585614651SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
78685614651SBarry Smith @*/
78763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunction(Mat mat,Vec v,PetscErrorCode (*func)(SNES,Vec,Vec,void *),void *funcctx)
78885614651SBarry Smith {
7897e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
79085614651SBarry Smith 
79185614651SBarry Smith   PetscFunctionBegin;
79285614651SBarry Smith   ctx->func    = func;
79385614651SBarry Smith   ctx->funcctx = funcctx;
79485614651SBarry Smith   ctx->funcvec = v;
79585614651SBarry Smith   PetscFunctionReturn(0);
79685614651SBarry Smith }
79785614651SBarry Smith 
798cf57b110SBarry Smith #undef __FUNCT__
799cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioni"
800cf57b110SBarry Smith /*@C
801cf57b110SBarry Smith    MatSNESMFSetFunctioni - Sets the function for a single component
802cf57b110SBarry Smith 
803cf57b110SBarry Smith    Collective on Mat
804cf57b110SBarry Smith 
805cf57b110SBarry Smith    Input Parameters:
806cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
807cf57b110SBarry Smith -  funci - the function to use
808cf57b110SBarry Smith 
809cf57b110SBarry Smith    Level: advanced
810cf57b110SBarry Smith 
811cf57b110SBarry Smith    Notes:
812cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
813cf57b110SBarry Smith     matrix inside your compute Jacobian routine
814cf57b110SBarry Smith 
815cf57b110SBarry Smith 
816cf57b110SBarry Smith .keywords: SNES, matrix-free, function
817cf57b110SBarry Smith 
818cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
819cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
820cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
821cf57b110SBarry Smith @*/
82263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunctioni(Mat mat,PetscErrorCode (*funci)(PetscInt,Vec,PetscScalar*,void *))
823cf57b110SBarry Smith {
824a7cc72afSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(PetscInt,Vec,PetscScalar*,void *));
825cf57b110SBarry Smith 
826cf57b110SBarry Smith   PetscFunctionBegin;
8274482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
828c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
82987828ca2SBarry Smith   if (f) {
83087828ca2SBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
83187828ca2SBarry Smith   }
832cf57b110SBarry Smith   PetscFunctionReturn(0);
833cf57b110SBarry Smith }
834cf57b110SBarry Smith 
83587828ca2SBarry Smith 
836cf57b110SBarry Smith #undef __FUNCT__
837cf57b110SBarry Smith #define __FUNCT__ "MatSNESMFSetFunctioniBase"
838cf57b110SBarry Smith /*@C
839cf57b110SBarry Smith    MatSNESMFSetFunctioniBase - Sets the base vector for a single component function evaluation
840cf57b110SBarry Smith 
841cf57b110SBarry Smith    Collective on Mat
842cf57b110SBarry Smith 
843cf57b110SBarry Smith    Input Parameters:
844cf57b110SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
845cf57b110SBarry Smith -  func - the function to use
846cf57b110SBarry Smith 
847cf57b110SBarry Smith    Level: advanced
848cf57b110SBarry Smith 
849cf57b110SBarry Smith    Notes:
850cf57b110SBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
851cf57b110SBarry Smith     matrix inside your compute Jacobian routine
852cf57b110SBarry Smith 
853cf57b110SBarry Smith 
854cf57b110SBarry Smith .keywords: SNES, matrix-free, function
855cf57b110SBarry Smith 
856cf57b110SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
857cf57b110SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
858cf57b110SBarry Smith           MatSNESMFKSPMonitor(), SNESetFunction()
859cf57b110SBarry Smith @*/
86063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunctioniBase(Mat mat,PetscErrorCode (*func)(Vec,void *))
861cf57b110SBarry Smith {
8626849ba73SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,void *));
863cf57b110SBarry Smith 
864cf57b110SBarry Smith   PetscFunctionBegin;
8654482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
866c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSNESMFSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
86787828ca2SBarry Smith   if (f) {
86887828ca2SBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
86987828ca2SBarry Smith   }
870cf57b110SBarry Smith   PetscFunctionReturn(0);
871cf57b110SBarry Smith }
872cf57b110SBarry Smith 
87385614651SBarry Smith 
8744a2ae208SSatish Balay #undef __FUNCT__
8754a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetPeriod"
876329f5518SBarry Smith /*@
877329f5518SBarry Smith    MatSNESMFSetPeriod - Sets how often h is recomputed, by default it is everytime
878329f5518SBarry Smith 
879329f5518SBarry Smith    Collective on Mat
880329f5518SBarry Smith 
881329f5518SBarry Smith    Input Parameters:
882329f5518SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
883329f5518SBarry Smith -  period - 1 for everytime, 2 for every second etc
884329f5518SBarry Smith 
885329f5518SBarry Smith    Options Database Keys:
886329f5518SBarry Smith +  -snes_mf_period <period>
887329f5518SBarry Smith 
888329f5518SBarry Smith    Level: advanced
889329f5518SBarry Smith 
890329f5518SBarry Smith 
891329f5518SBarry Smith .keywords: SNES, matrix-free, parameters
892329f5518SBarry Smith 
893329f5518SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
894329f5518SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
895329f5518SBarry Smith           MatSNESMFKSPMonitor()
896329f5518SBarry Smith @*/
89763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetPeriod(Mat mat,PetscInt period)
898329f5518SBarry Smith {
8997e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
900329f5518SBarry Smith 
901329f5518SBarry Smith   PetscFunctionBegin;
902329f5518SBarry Smith   ctx->recomputeperiod = period;
903329f5518SBarry Smith   PetscFunctionReturn(0);
904329f5518SBarry Smith }
905329f5518SBarry Smith 
9064a2ae208SSatish Balay #undef __FUNCT__
9074a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetFunctionError"
908a4d4d686SBarry Smith /*@
9095a655dc6SBarry Smith    MatSNESMFSetFunctionError - Sets the error_rel for the approximation of
910a4d4d686SBarry Smith    matrix-vector products using finite differences.
911a4d4d686SBarry Smith 
912a4d4d686SBarry Smith    Collective on Mat
913a4d4d686SBarry Smith 
914a4d4d686SBarry Smith    Input Parameters:
9155a655dc6SBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
9169a6cb015SBarry Smith -  error_rel - relative error (should be set to the square root of
917a4d4d686SBarry Smith                the relative error in the function evaluations)
918a4d4d686SBarry Smith 
91915091d37SBarry Smith    Options Database Keys:
92015091d37SBarry Smith +  -snes_mf_err <error_rel> - Sets error_rel
92115091d37SBarry Smith 
92215091d37SBarry Smith    Level: advanced
92315091d37SBarry Smith 
924a4d4d686SBarry Smith    Notes:
925a4d4d686SBarry Smith    The default matrix-free matrix-vector product routine computes
926a4d4d686SBarry Smith .vb
92765f2ba5bSLois Curfman McInnes      F'(u)*a = [F(u+h*a) - F(u)]/h where
928a4d4d686SBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
929a4d4d686SBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
930a4d4d686SBarry Smith .ve
931a4d4d686SBarry Smith 
932a4d4d686SBarry Smith .keywords: SNES, matrix-free, parameters
933a4d4d686SBarry Smith 
9345a655dc6SBarry Smith .seealso: MatCreateSNESMF(),MatSNESMFGetH(),
9355a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9365a655dc6SBarry Smith           MatSNESMFKSPMonitor()
937a4d4d686SBarry Smith @*/
93863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetFunctionError(Mat mat,PetscReal error)
939a4d4d686SBarry Smith {
9407e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)mat->data;
941a4d4d686SBarry Smith 
942a4d4d686SBarry Smith   PetscFunctionBegin;
943a4d4d686SBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
944a4d4d686SBarry Smith   PetscFunctionReturn(0);
945a4d4d686SBarry Smith }
946a4d4d686SBarry Smith 
9474a2ae208SSatish Balay #undef __FUNCT__
9484a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFAddNullSpace"
949a4d4d686SBarry Smith /*@
95065f2ba5bSLois Curfman McInnes    MatSNESMFAddNullSpace - Provides a null space that an operator is
95165f2ba5bSLois Curfman McInnes    supposed to have.  Since roundoff will create a small component in
95265f2ba5bSLois Curfman McInnes    the null space, if you know the null space you may have it
95365f2ba5bSLois Curfman McInnes    automatically removed.
954a4d4d686SBarry Smith 
955a4d4d686SBarry Smith    Collective on Mat
956a4d4d686SBarry Smith 
957a4d4d686SBarry Smith    Input Parameters:
958a4d4d686SBarry Smith +  J - the matrix-free matrix context
95974637425SBarry Smith -  nullsp - object created with MatNullSpaceCreate()
960a4d4d686SBarry Smith 
96115091d37SBarry Smith    Level: advanced
96215091d37SBarry Smith 
963a4d4d686SBarry Smith .keywords: SNES, matrix-free, null space
964a4d4d686SBarry Smith 
96574637425SBarry Smith .seealso: MatNullSpaceCreate(), MatSNESMFGetH(), MatCreateSNESMF(),
9665a655dc6SBarry Smith           MatSNESMFSetHHistory(), MatSNESMFResetHHistory(),
9675a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFErrorRel()
968a4d4d686SBarry Smith @*/
96963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFAddNullSpace(Mat J,MatNullSpace nullsp)
970a4d4d686SBarry Smith {
971dfbe8321SBarry Smith   PetscErrorCode ierr;
9727e9d5209SBarry Smith   MatSNESMFCtx   ctx = (MatSNESMFCtx)J->data;
973a4d4d686SBarry Smith   MPI_Comm       comm;
974a4d4d686SBarry Smith 
975a4d4d686SBarry Smith   PetscFunctionBegin;
9762d0c0e3bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)J,&comm);CHKERRQ(ierr);
977a4d4d686SBarry Smith 
97885614651SBarry Smith   ctx->sp = nullsp;
97985614651SBarry Smith   ierr    = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
980a4d4d686SBarry Smith   PetscFunctionReturn(0);
981a4d4d686SBarry Smith }
982a4d4d686SBarry Smith 
9834a2ae208SSatish Balay #undef __FUNCT__
9844a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetHHistory"
985a4d4d686SBarry Smith /*@
98665f2ba5bSLois Curfman McInnes    MatSNESMFSetHHistory - Sets an array to collect a history of the
98765f2ba5bSLois Curfman McInnes    differencing values (h) computed for the matrix-free product.
988a4d4d686SBarry Smith 
989a4d4d686SBarry Smith    Collective on Mat
990a4d4d686SBarry Smith 
991a4d4d686SBarry Smith    Input Parameters:
992a4d4d686SBarry Smith +  J - the matrix-free matrix context
99365f2ba5bSLois Curfman McInnes .  histroy - space to hold the history
99465f2ba5bSLois Curfman McInnes -  nhistory - number of entries in history, if more entries are generated than
99565f2ba5bSLois Curfman McInnes               nhistory, then the later ones are discarded
996a4d4d686SBarry Smith 
99715091d37SBarry Smith    Level: advanced
99815091d37SBarry Smith 
999a4d4d686SBarry Smith    Notes:
100065f2ba5bSLois Curfman McInnes    Use MatSNESMFResetHHistory() to reset the history counter and collect
100165f2ba5bSLois Curfman McInnes    a new batch of differencing parameters, h.
1002a4d4d686SBarry Smith 
1003a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1004a4d4d686SBarry Smith 
10055a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10065a655dc6SBarry Smith           MatSNESMFResetHHistory(),
10075a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1008a4d4d686SBarry Smith 
1009a4d4d686SBarry Smith @*/
101063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory)
1011a4d4d686SBarry Smith {
10127e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1013a4d4d686SBarry Smith 
1014a4d4d686SBarry Smith   PetscFunctionBegin;
1015a4d4d686SBarry Smith   ctx->historyh    = history;
1016a4d4d686SBarry Smith   ctx->maxcurrenth = nhistory;
1017a4d4d686SBarry Smith   ctx->currenth    = 0;
1018a4d4d686SBarry Smith   PetscFunctionReturn(0);
1019a4d4d686SBarry Smith }
1020a4d4d686SBarry Smith 
10214a2ae208SSatish Balay #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFResetHHistory"
1023a4d4d686SBarry Smith /*@
10245a655dc6SBarry Smith    MatSNESMFResetHHistory - Resets the counter to zero to begin
1025a4d4d686SBarry Smith    collecting a new set of differencing histories.
1026a4d4d686SBarry Smith 
1027a4d4d686SBarry Smith    Collective on Mat
1028a4d4d686SBarry Smith 
1029a4d4d686SBarry Smith    Input Parameters:
1030a4d4d686SBarry Smith .  J - the matrix-free matrix context
1031a4d4d686SBarry Smith 
103215091d37SBarry Smith    Level: advanced
103315091d37SBarry Smith 
1034a4d4d686SBarry Smith    Notes:
103565f2ba5bSLois Curfman McInnes    Use MatSNESMFSetHHistory() to create the original history counter.
1036a4d4d686SBarry Smith 
1037a4d4d686SBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1038a4d4d686SBarry Smith 
10395a655dc6SBarry Smith .seealso: MatSNESMFGetH(), MatCreateSNESMF(),
10405a655dc6SBarry Smith           MatSNESMFSetHHistory(),
10415a655dc6SBarry Smith           MatSNESMFKSPMonitor(), MatSNESMFSetFunctionError()
1042a4d4d686SBarry Smith 
1043a4d4d686SBarry Smith @*/
104463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFResetHHistory(Mat J)
1045a4d4d686SBarry Smith {
10467e9d5209SBarry Smith   MatSNESMFCtx ctx = (MatSNESMFCtx)J->data;
1047a4d4d686SBarry Smith 
1048a4d4d686SBarry Smith   PetscFunctionBegin;
1049be726c96SBarry Smith   ctx->ncurrenth    = 0;
1050a4d4d686SBarry Smith   PetscFunctionReturn(0);
1051a4d4d686SBarry Smith }
1052a4d4d686SBarry Smith 
10534a2ae208SSatish Balay #undef __FUNCT__
1054fed8bd04SBarry Smith #define __FUNCT__ "MatSNESMFComputeJacobian"
105563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFComputeJacobian(SNES snes,Vec x,Mat *jac,Mat *B,MatStructure *flag,void *dummy)
10561d1367b7SBarry Smith {
1057dfbe8321SBarry Smith   PetscErrorCode ierr;
10581d1367b7SBarry Smith   PetscFunctionBegin;
10591d1367b7SBarry Smith   ierr = MatAssemblyBegin(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10601d1367b7SBarry Smith   ierr = MatAssemblyEnd(*jac,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
10611d1367b7SBarry Smith   PetscFunctionReturn(0);
10621d1367b7SBarry Smith }
10631d1367b7SBarry Smith 
10644a2ae208SSatish Balay #undef __FUNCT__
10654a2ae208SSatish Balay #define __FUNCT__ "MatSNESMFSetBase"
10665b7f0c42SBarry Smith /*@
10675b7f0c42SBarry Smith     MatSNESMFSetBase - Sets the vector U at which matrix vector products of the
10685b7f0c42SBarry Smith         Jacobian are computed
10695b7f0c42SBarry Smith 
10705b7f0c42SBarry Smith     Collective on Mat
10715b7f0c42SBarry Smith 
10725b7f0c42SBarry Smith     Input Parameters:
10735b7f0c42SBarry Smith +   J - the MatSNESMF matrix
10745b7f0c42SBarry Smith -   U - the vector
10755b7f0c42SBarry Smith 
10765b7f0c42SBarry Smith     Notes: This is rarely used directly
10775b7f0c42SBarry Smith 
10785b7f0c42SBarry Smith     Level: advanced
10795b7f0c42SBarry Smith 
10805b7f0c42SBarry Smith @*/
108163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetBase(Mat J,Vec U)
10821d1367b7SBarry Smith {
1083dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat,Vec);
10841d1367b7SBarry Smith 
10851d1367b7SBarry Smith   PetscFunctionBegin;
10864482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
10874482741eSBarry Smith   PetscValidHeaderSpecific(U,VEC_COOKIE,2);
1088c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1089cf3bea43SBarry Smith   if (f) {
1090cf3bea43SBarry Smith     ierr = (*f)(J,U);CHKERRQ(ierr);
109149d4803aSBarry Smith   }
10921d1367b7SBarry Smith   PetscFunctionReturn(0);
10931d1367b7SBarry Smith }
1094cf57b110SBarry Smith 
10955b7f0c42SBarry Smith #undef __FUNCT__
10965b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckh"
109761860be5SBarry Smith /*@C
10985b7f0c42SBarry Smith     MatSNESMFSetCheckh - Sets a function that checks the computed h and adjusts
10995b7f0c42SBarry Smith         it to satisfy some criteria
1100cf57b110SBarry Smith 
11015b7f0c42SBarry Smith     Collective on Mat
11025b7f0c42SBarry Smith 
11035b7f0c42SBarry Smith     Input Parameters:
11045b7f0c42SBarry Smith +   J - the MatSNESMF matrix
11055b7f0c42SBarry Smith .   fun - the function that checks h
11065b7f0c42SBarry Smith -   ctx - any context needed by the function
11075b7f0c42SBarry Smith 
11085b7f0c42SBarry Smith     Options Database Keys:
11095b7f0c42SBarry Smith .   -snes_mf_check_positivity
11105b7f0c42SBarry Smith 
11115b7f0c42SBarry Smith     Level: advanced
11125b7f0c42SBarry Smith 
11135b7f0c42SBarry Smith     Notes: For example, MatSNESMFSetCheckPositivity() insures that all entries
11145b7f0c42SBarry Smith        of U + h*a are non-negative
11155b7f0c42SBarry Smith 
11165b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckPositivity()
11175b7f0c42SBarry Smith @*/
111863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFSetCheckh(Mat J,PetscErrorCode (*fun)(Vec,Vec,PetscScalar*,void*),void* ctx)
11195b7f0c42SBarry Smith {
11206849ba73SBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(Vec,Vec,PetscScalar*,void*),void*);
11215b7f0c42SBarry Smith 
11225b7f0c42SBarry Smith   PetscFunctionBegin;
11234482741eSBarry Smith   PetscValidHeaderSpecific(J,MAT_COOKIE,1);
11245b7f0c42SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatSNESMFSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
11255b7f0c42SBarry Smith   if (f) {
11265b7f0c42SBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
11275b7f0c42SBarry Smith   }
11285b7f0c42SBarry Smith   PetscFunctionReturn(0);
11295b7f0c42SBarry Smith }
11305b7f0c42SBarry Smith 
11315b7f0c42SBarry Smith #undef __FUNCT__
11325b7f0c42SBarry Smith #define __FUNCT__ "MatSNESMFSetCheckPositivity"
11335b7f0c42SBarry Smith /*@
11345b7f0c42SBarry Smith     MatSNESMFCheckPositivity - Checks that all entries in U + h*a are positive or
11355b7f0c42SBarry Smith         zero, decreases h until this is satisfied.
11365b7f0c42SBarry Smith 
11375b7f0c42SBarry Smith     Collective on Vec
11385b7f0c42SBarry Smith 
11395b7f0c42SBarry Smith     Input Parameters:
11405b7f0c42SBarry Smith +   U - base vector that is added to
11415b7f0c42SBarry Smith .   a - vector that is added
11425b7f0c42SBarry Smith .   h - scaling factor on a
11435b7f0c42SBarry Smith -   dummy - context variable (unused)
11445b7f0c42SBarry Smith 
11455b7f0c42SBarry Smith     Options Database Keys:
11465b7f0c42SBarry Smith .   -snes_mf_check_positivity
11475b7f0c42SBarry Smith 
11485b7f0c42SBarry Smith     Level: advanced
11495b7f0c42SBarry Smith 
11505b7f0c42SBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
11515b7f0c42SBarry Smith            MatSNESMFSetCheckh()
11525b7f0c42SBarry Smith 
11535b7f0c42SBarry Smith .seealso:  MatSNESMFSetCheckh()
11545b7f0c42SBarry Smith @*/
115563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT MatSNESMFCheckPositivity(Vec U,Vec a,PetscScalar *h,void *dummy)
11565b7f0c42SBarry Smith {
11575b7f0c42SBarry Smith   PetscReal      val, minval;
11585b7f0c42SBarry Smith   PetscScalar    *u_vec, *a_vec;
1159dfbe8321SBarry Smith   PetscErrorCode ierr;
1160a7cc72afSBarry Smith   PetscInt       i,n;
11615b7f0c42SBarry Smith   MPI_Comm       comm;
11625b7f0c42SBarry Smith 
11635b7f0c42SBarry Smith   PetscFunctionBegin;
11645b7f0c42SBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
11655b7f0c42SBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
11665b7f0c42SBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
1167a7cc72afSBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
116861860be5SBarry Smith   minval = PetscAbsScalar(*h*1.01);
1169a7cc72afSBarry Smith   for(i=0;i<n;i++) {
117061860be5SBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
117161860be5SBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
11725b7f0c42SBarry Smith       if (val < minval) minval = val;
11735b7f0c42SBarry Smith     }
11745b7f0c42SBarry Smith   }
11755b7f0c42SBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
11765b7f0c42SBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
11775b7f0c42SBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
117861860be5SBarry Smith   if (val <= PetscAbsScalar(*h)) {
117963ba0a88SBarry Smith     ierr = PetscLogInfo((U,"MatSNESMFCheckPositivity: Scaling back h from %g to %g\n",PetscRealPart(*h),.99*val));CHKERRQ(ierr);
118061860be5SBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
11815b7f0c42SBarry Smith     else                         *h = -0.99*val;
11825b7f0c42SBarry Smith   }
11835b7f0c42SBarry Smith   PetscFunctionReturn(0);
11845b7f0c42SBarry Smith }
1185cf57b110SBarry Smith 
1186cf57b110SBarry Smith 
1187cf57b110SBarry Smith 
1188cf57b110SBarry Smith 
1189cf57b110SBarry Smith 
1190cf57b110SBarry Smith 
1191cf57b110SBarry Smith 
1192