xref: /petsc/src/mat/impls/mffd/mffd.c (revision 0700a8246d308f50502909ba325e6169d3ee27eb)
1e884886fSBarry Smith #define PETSCMAT_DLL
2e884886fSBarry Smith 
37c4f633dSBarry Smith #include "private/matimpl.h"
47c4f633dSBarry Smith #include "../src/mat/impls/mffd/mffdimpl.h"   /*I  "petscmat.h"   I*/
5e884886fSBarry Smith 
6b022a5c1SBarry Smith PetscFList MatMFFDList        = 0;
7e884886fSBarry Smith PetscTruth MatMFFDRegisterAllCalled = PETSC_FALSE;
8e884886fSBarry Smith 
9*0700a824SBarry Smith PetscClassId PETSCMAT_DLLEXPORT MATMFFD_CLASSID;
10166c7f25SBarry Smith PetscLogEvent  MATMFFD_Mult;
11e884886fSBarry Smith 
12b022a5c1SBarry Smith static PetscTruth MatMFFDPackageInitialized = PETSC_FALSE;
13b022a5c1SBarry Smith #undef __FUNCT__
14b022a5c1SBarry Smith #define __FUNCT__ "MatMFFDFinalizePackage"
15b022a5c1SBarry Smith /*@C
162390153bSJed Brown   MatMFFDFinalizePackage - This function destroys everything in the MatMFFD package. It is
17b022a5c1SBarry Smith   called from PetscFinalize().
18b022a5c1SBarry Smith 
19b022a5c1SBarry Smith   Level: developer
20b022a5c1SBarry Smith 
212390153bSJed Brown .keywords: Petsc, destroy, package
22b022a5c1SBarry Smith .seealso: PetscFinalize()
23b022a5c1SBarry Smith @*/
24b022a5c1SBarry Smith PetscErrorCode PETSC_DLLEXPORT MatMFFDFinalizePackage(void)
25b022a5c1SBarry Smith {
26b022a5c1SBarry Smith   PetscFunctionBegin;
27b022a5c1SBarry Smith   MatMFFDPackageInitialized = PETSC_FALSE;
28b022a5c1SBarry Smith   MatMFFDRegisterAllCalled  = PETSC_FALSE;
29b022a5c1SBarry Smith   MatMFFDList               = PETSC_NULL;
30b022a5c1SBarry Smith   PetscFunctionReturn(0);
31b022a5c1SBarry Smith }
32b022a5c1SBarry Smith 
33e884886fSBarry Smith #undef __FUNCT__
343ec795f1SBarry Smith #define __FUNCT__ "MatMFFDInitializePackage"
353ec795f1SBarry Smith /*@C
363ec795f1SBarry Smith   MatMFFDInitializePackage - This function initializes everything in the MatMFFD package. It is called
373ec795f1SBarry Smith   from PetscDLLibraryRegister() when using dynamic libraries, and on the first call to MatCreate_MFFD()
383ec795f1SBarry Smith   when using static libraries.
393ec795f1SBarry Smith 
403ec795f1SBarry Smith   Input Parameter:
413ec795f1SBarry Smith . path - The dynamic library path, or PETSC_NULL
423ec795f1SBarry Smith 
433ec795f1SBarry Smith   Level: developer
443ec795f1SBarry Smith 
453ec795f1SBarry Smith .keywords: Vec, initialize, package
463ec795f1SBarry Smith .seealso: PetscInitialize()
473ec795f1SBarry Smith @*/
483ec795f1SBarry Smith PetscErrorCode PETSCVEC_DLLEXPORT MatMFFDInitializePackage(const char path[])
493ec795f1SBarry Smith {
503ec795f1SBarry Smith   char              logList[256];
513ec795f1SBarry Smith   char              *className;
523ec795f1SBarry Smith   PetscTruth        opt;
533ec795f1SBarry Smith   PetscErrorCode    ierr;
543ec795f1SBarry Smith 
553ec795f1SBarry Smith   PetscFunctionBegin;
56b022a5c1SBarry Smith   if (MatMFFDPackageInitialized) PetscFunctionReturn(0);
57b022a5c1SBarry Smith   MatMFFDPackageInitialized = PETSC_TRUE;
583ec795f1SBarry Smith   /* Register Classes */
59*0700a824SBarry Smith   ierr = PetscClassIdRegister("MatMFFD",&MATMFFD_CLASSID);CHKERRQ(ierr);
603ec795f1SBarry Smith   /* Register Constructors */
613ec795f1SBarry Smith   ierr = MatMFFDRegisterAll(path);CHKERRQ(ierr);
623ec795f1SBarry Smith   /* Register Events */
63*0700a824SBarry Smith   ierr = PetscLogEventRegister("MatMult MF",          MATMFFD_CLASSID,&MATMFFD_Mult);CHKERRQ(ierr);
643ec795f1SBarry Smith 
653ec795f1SBarry Smith   /* Process info exclusions */
663ec795f1SBarry Smith   ierr = PetscOptionsGetString(PETSC_NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
673ec795f1SBarry Smith   if (opt) {
683ec795f1SBarry Smith     ierr = PetscStrstr(logList, "matmffd", &className);CHKERRQ(ierr);
693ec795f1SBarry Smith     if (className) {
70*0700a824SBarry Smith       ierr = PetscInfoDeactivateClass(MATMFFD_CLASSID);CHKERRQ(ierr);
713ec795f1SBarry Smith     }
723ec795f1SBarry Smith   }
733ec795f1SBarry Smith   /* Process summary exclusions */
743ec795f1SBarry Smith   ierr = PetscOptionsGetString(PETSC_NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
753ec795f1SBarry Smith   if (opt) {
763ec795f1SBarry Smith     ierr = PetscStrstr(logList, "matmffd", &className);CHKERRQ(ierr);
773ec795f1SBarry Smith     if (className) {
78*0700a824SBarry Smith       ierr = PetscLogEventDeactivateClass(MATMFFD_CLASSID);CHKERRQ(ierr);
793ec795f1SBarry Smith     }
803ec795f1SBarry Smith   }
81b022a5c1SBarry Smith   ierr = PetscRegisterFinalize(MatMFFDFinalizePackage);CHKERRQ(ierr);
823ec795f1SBarry Smith   PetscFunctionReturn(0);
833ec795f1SBarry Smith }
843ec795f1SBarry Smith 
853ec795f1SBarry Smith #undef __FUNCT__
86e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetType"
87e884886fSBarry Smith /*@C
88e884886fSBarry Smith     MatMFFDSetType - Sets the method that is used to compute the
89e884886fSBarry Smith     differencing parameter for finite differene matrix-free formulations.
90e884886fSBarry Smith 
91e884886fSBarry Smith     Input Parameters:
92e884886fSBarry Smith +   mat - the "matrix-free" matrix created via MatCreateSNESMF(), or MatCreateMFFD()
93e884886fSBarry Smith           or MatSetType(mat,MATMFFD);
94e884886fSBarry Smith -   ftype - the type requested, either MATMFFD_WP or MATMFFD_DS
95e884886fSBarry Smith 
96e884886fSBarry Smith     Level: advanced
97e884886fSBarry Smith 
98e884886fSBarry Smith     Notes:
99e884886fSBarry Smith     For example, such routines can compute h for use in
100e884886fSBarry Smith     Jacobian-vector products of the form
101e884886fSBarry Smith 
102e884886fSBarry Smith                         F(x+ha) - F(x)
103e884886fSBarry Smith           F'(u)a  ~=  ----------------
104e884886fSBarry Smith                               h
105e884886fSBarry Smith 
106d2d6cebeSBarry Smith .seealso: MatCreateSNESMF(), MatMFFDRegisterDynamic(), MatMFFDSetFunction()
107e884886fSBarry Smith @*/
108a313700dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetType(Mat mat,const MatMFFDType ftype)
109e884886fSBarry Smith {
110e884886fSBarry Smith   PetscErrorCode ierr,(*r)(MatMFFD);
111e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)mat->data;
112e884886fSBarry Smith   PetscTruth     match;
113e884886fSBarry Smith 
114e884886fSBarry Smith   PetscFunctionBegin;
115*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
116e884886fSBarry Smith   PetscValidCharPointer(ftype,2);
117e884886fSBarry Smith 
118e884886fSBarry Smith   /* already set, so just return */
119e884886fSBarry Smith   ierr = PetscTypeCompare((PetscObject)ctx,ftype,&match);CHKERRQ(ierr);
120e884886fSBarry Smith   if (match) PetscFunctionReturn(0);
121e884886fSBarry Smith 
122e884886fSBarry Smith   /* destroy the old one if it exists */
123e884886fSBarry Smith   if (ctx->ops->destroy) {
124e884886fSBarry Smith     ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);
125e884886fSBarry Smith   }
126e884886fSBarry Smith 
127b022a5c1SBarry Smith   ierr =  PetscFListFind(MatMFFDList,((PetscObject)ctx)->comm,ftype,(void (**)(void)) &r);CHKERRQ(ierr);
128e884886fSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown MatMFFD type %s given",ftype);
129e884886fSBarry Smith   ierr = (*r)(ctx);CHKERRQ(ierr);
130e884886fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ctx,ftype);CHKERRQ(ierr);
131e884886fSBarry Smith   PetscFunctionReturn(0);
132e884886fSBarry Smith }
133e884886fSBarry Smith 
134e884886fSBarry Smith typedef PetscErrorCode (*FCN1)(void*,Vec); /* force argument to next function to not be extern C*/
135e884886fSBarry Smith EXTERN_C_BEGIN
136e884886fSBarry Smith #undef __FUNCT__
137c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase_MFFD"
138c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase_MFFD(Mat mat,FCN1 func)
139e884886fSBarry Smith {
140e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
141e884886fSBarry Smith 
142e884886fSBarry Smith   PetscFunctionBegin;
143e884886fSBarry Smith   ctx->funcisetbase = func;
144e884886fSBarry Smith   PetscFunctionReturn(0);
145e884886fSBarry Smith }
146e884886fSBarry Smith EXTERN_C_END
147e884886fSBarry Smith 
148e884886fSBarry Smith typedef PetscErrorCode (*FCN2)(void*,PetscInt,Vec,PetscScalar*); /* force argument to next function to not be extern C*/
149e884886fSBarry Smith EXTERN_C_BEGIN
150e884886fSBarry Smith #undef __FUNCT__
151c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni_MFFD"
152c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni_MFFD(Mat mat,FCN2 funci)
153e884886fSBarry Smith {
154e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
155e884886fSBarry Smith 
156e884886fSBarry Smith   PetscFunctionBegin;
157e884886fSBarry Smith   ctx->funci = funci;
158e884886fSBarry Smith   PetscFunctionReturn(0);
159e884886fSBarry Smith }
160e884886fSBarry Smith EXTERN_C_END
161e884886fSBarry Smith 
162e884886fSBarry Smith 
163e884886fSBarry Smith #undef __FUNCT__
164e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegister"
165e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(MatMFFD))
166e884886fSBarry Smith {
167e884886fSBarry Smith   PetscErrorCode ierr;
168e884886fSBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
169e884886fSBarry Smith 
170e884886fSBarry Smith   PetscFunctionBegin;
171e884886fSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
172b022a5c1SBarry Smith   ierr = PetscFListAdd(&MatMFFDList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
173e884886fSBarry Smith   PetscFunctionReturn(0);
174e884886fSBarry Smith }
175e884886fSBarry Smith 
176e884886fSBarry Smith 
177e884886fSBarry Smith #undef __FUNCT__
178e884886fSBarry Smith #define __FUNCT__ "MatMFFDRegisterDestroy"
179e884886fSBarry Smith /*@C
180e884886fSBarry Smith    MatMFFDRegisterDestroy - Frees the list of MatMFFD methods that were
181e884886fSBarry Smith    registered by MatMFFDRegisterDynamic).
182e884886fSBarry Smith 
183e884886fSBarry Smith    Not Collective
184e884886fSBarry Smith 
185e884886fSBarry Smith    Level: developer
186e884886fSBarry Smith 
187e884886fSBarry Smith .keywords: MatMFFD, register, destroy
188e884886fSBarry Smith 
189e884886fSBarry Smith .seealso: MatMFFDRegisterDynamic), MatMFFDRegisterAll()
190e884886fSBarry Smith @*/
191e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDRegisterDestroy(void)
192e884886fSBarry Smith {
193e884886fSBarry Smith   PetscErrorCode ierr;
194e884886fSBarry Smith 
195e884886fSBarry Smith   PetscFunctionBegin;
196b022a5c1SBarry Smith   ierr = PetscFListDestroy(&MatMFFDList);CHKERRQ(ierr);
197e884886fSBarry Smith   MatMFFDRegisterAllCalled = PETSC_FALSE;
198e884886fSBarry Smith   PetscFunctionReturn(0);
199e884886fSBarry Smith }
200e884886fSBarry Smith 
201e884886fSBarry Smith /* ----------------------------------------------------------------------------------------*/
202e884886fSBarry Smith #undef __FUNCT__
203e884886fSBarry Smith #define __FUNCT__ "MatDestroy_MFFD"
204e884886fSBarry Smith PetscErrorCode MatDestroy_MFFD(Mat mat)
205e884886fSBarry Smith {
206e884886fSBarry Smith   PetscErrorCode ierr;
207e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)mat->data;
208e884886fSBarry Smith 
209e884886fSBarry Smith   PetscFunctionBegin;
210e884886fSBarry Smith   if (ctx->w) {
211e884886fSBarry Smith     ierr = VecDestroy(ctx->w);CHKERRQ(ierr);
212e884886fSBarry Smith   }
213cfe22f5eSBarry Smith   if (ctx->current_f_allocated) {
214cfe22f5eSBarry Smith     ierr = VecDestroy(ctx->current_f);
215cfe22f5eSBarry Smith   }
216e884886fSBarry Smith   if (ctx->ops->destroy) {ierr = (*ctx->ops->destroy)(ctx);CHKERRQ(ierr);}
217e884886fSBarry Smith   if (ctx->sp) {ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr);}
218e884886fSBarry Smith   ierr = PetscHeaderDestroy(ctx);CHKERRQ(ierr);
219e884886fSBarry Smith 
220e884886fSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetBase_C","",PETSC_NULL);CHKERRQ(ierr);
221e884886fSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C","",PETSC_NULL);CHKERRQ(ierr);
222e884886fSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetFunctioni_C","",PETSC_NULL);CHKERRQ(ierr);
223e884886fSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)mat,"MatMFFDSetCheckh_C","",PETSC_NULL);CHKERRQ(ierr);
224e884886fSBarry Smith 
225e884886fSBarry Smith   PetscFunctionReturn(0);
226e884886fSBarry Smith }
227e884886fSBarry Smith 
228e884886fSBarry Smith #undef __FUNCT__
229e884886fSBarry Smith #define __FUNCT__ "MatView_MFFD"
230e884886fSBarry Smith /*
231e884886fSBarry Smith    MatMFFDView_MFFD - Views matrix-free parameters.
232e884886fSBarry Smith 
233e884886fSBarry Smith */
234e884886fSBarry Smith PetscErrorCode MatView_MFFD(Mat J,PetscViewer viewer)
235e884886fSBarry Smith {
236e884886fSBarry Smith   PetscErrorCode ierr;
237e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)J->data;
238e884886fSBarry Smith   PetscTruth     iascii;
239e884886fSBarry Smith 
240e884886fSBarry Smith   PetscFunctionBegin;
241e884886fSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
242e884886fSBarry Smith   if (iascii) {
243e884886fSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"  matrix-free approximation:\n");CHKERRQ(ierr);
244e884886fSBarry Smith      ierr = PetscViewerASCIIPrintf(viewer,"    err=%G (relative error in function evaluation)\n",ctx->error_rel);CHKERRQ(ierr);
2457adad957SLisandro Dalcin      if (!((PetscObject)ctx)->type_name) {
246e884886fSBarry Smith        ierr = PetscViewerASCIIPrintf(viewer,"    The compute h routine has not yet been set\n");CHKERRQ(ierr);
247e884886fSBarry Smith      } else {
2487adad957SLisandro Dalcin        ierr = PetscViewerASCIIPrintf(viewer,"    Using %s compute h routine\n",((PetscObject)ctx)->type_name);CHKERRQ(ierr);
249e884886fSBarry Smith      }
250e884886fSBarry Smith      if (ctx->ops->view) {
251e884886fSBarry Smith        ierr = (*ctx->ops->view)(ctx,viewer);CHKERRQ(ierr);
252e884886fSBarry Smith      }
253e884886fSBarry Smith   } else {
254e884886fSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for matrix-free matrix",((PetscObject)viewer)->type_name);
255e884886fSBarry Smith   }
256e884886fSBarry Smith   PetscFunctionReturn(0);
257e884886fSBarry Smith }
258e884886fSBarry Smith 
259e884886fSBarry Smith #undef __FUNCT__
260e884886fSBarry Smith #define __FUNCT__ "MatAssemblyEnd_MFFD"
261e884886fSBarry Smith /*
262e884886fSBarry Smith    MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This
263e884886fSBarry Smith    allows the user to indicate the beginning of a new linear solve by calling
264e884886fSBarry Smith    MatAssemblyXXX() on the matrix free matrix. This then allows the
265e884886fSBarry Smith    MatMFFDCreate_WP() to properly compute ||U|| only the first time
266e884886fSBarry Smith    in the linear solver rather than every time.
267e884886fSBarry Smith */
268e884886fSBarry Smith PetscErrorCode MatAssemblyEnd_MFFD(Mat J,MatAssemblyType mt)
269e884886fSBarry Smith {
270e884886fSBarry Smith   PetscErrorCode ierr;
271e884886fSBarry Smith   MatMFFD        j = (MatMFFD)J->data;
272e884886fSBarry Smith 
273e884886fSBarry Smith   PetscFunctionBegin;
274e884886fSBarry Smith   ierr      = MatMFFDResetHHistory(J);CHKERRQ(ierr);
275e884886fSBarry Smith   j->vshift = 0.0;
276e884886fSBarry Smith   j->vscale = 1.0;
277e884886fSBarry Smith   PetscFunctionReturn(0);
278e884886fSBarry Smith }
279e884886fSBarry Smith 
280e884886fSBarry Smith #undef __FUNCT__
281e884886fSBarry Smith #define __FUNCT__ "MatMult_MFFD"
282e884886fSBarry Smith /*
283e884886fSBarry Smith   MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
284e884886fSBarry Smith 
285e884886fSBarry Smith         y ~= (F(u + ha) - F(u))/h,
286e884886fSBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
287e884886fSBarry Smith         u = current iterate
288e884886fSBarry Smith         h = difference interval
289e884886fSBarry Smith */
290e884886fSBarry Smith PetscErrorCode MatMult_MFFD(Mat mat,Vec a,Vec y)
291e884886fSBarry Smith {
292e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)mat->data;
293e884886fSBarry Smith   PetscScalar    h;
294e884886fSBarry Smith   Vec            w,U,F;
295e884886fSBarry Smith   PetscErrorCode ierr;
296e884886fSBarry Smith   PetscTruth     zeroa;
297e884886fSBarry Smith 
298e884886fSBarry Smith   PetscFunctionBegin;
299e884886fSBarry Smith   /* We log matrix-free matrix-vector products separately, so that we can
300e884886fSBarry Smith      separate the performance monitoring from the cases that use conventional
301e884886fSBarry Smith      storage.  We may eventually modify event logging to associate events
302e884886fSBarry Smith      with particular objects, hence alleviating the more general problem. */
303e884886fSBarry Smith   ierr = PetscLogEventBegin(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr);
304e884886fSBarry Smith 
305e884886fSBarry Smith   w    = ctx->w;
306e884886fSBarry Smith   U    = ctx->current_u;
3073ec795f1SBarry Smith   F    = ctx->current_f;
308e884886fSBarry Smith   /*
309e884886fSBarry Smith       Compute differencing parameter
310e884886fSBarry Smith   */
311e884886fSBarry Smith   if (!ctx->ops->compute) {
312e884886fSBarry Smith     ierr = MatMFFDSetType(mat,MATMFFD_WP);CHKERRQ(ierr);
313e884886fSBarry Smith     ierr = MatMFFDSetFromOptions(mat);CHKERRQ(ierr);
314e884886fSBarry Smith   }
315e884886fSBarry Smith   ierr = (*ctx->ops->compute)(ctx,U,a,&h,&zeroa);CHKERRQ(ierr);
316e884886fSBarry Smith   if (zeroa) {
317e884886fSBarry Smith     ierr = VecSet(y,0.0);CHKERRQ(ierr);
318e884886fSBarry Smith     PetscFunctionReturn(0);
319e884886fSBarry Smith   }
320e884886fSBarry Smith 
32151a79602SBarry Smith   if (PetscIsInfOrNanScalar(h)) SETERRQ(PETSC_ERR_PLIB,"Computed Nan differencing parameter h");
322e884886fSBarry Smith   if (ctx->checkh) {
323e884886fSBarry Smith     ierr = (*ctx->checkh)(ctx->checkhctx,U,a,&h);CHKERRQ(ierr);
324e884886fSBarry Smith   }
325e884886fSBarry Smith 
326e884886fSBarry Smith   /* keep a record of the current differencing parameter h */
327e884886fSBarry Smith   ctx->currenth = h;
328e884886fSBarry Smith #if defined(PETSC_USE_COMPLEX)
329e884886fSBarry Smith   ierr = PetscInfo2(mat,"Current differencing parameter: %G + %G i\n",PetscRealPart(h),PetscImaginaryPart(h));CHKERRQ(ierr);
330e884886fSBarry Smith #else
331e884886fSBarry Smith   ierr = PetscInfo1(mat,"Current differencing parameter: %15.12e\n",h);CHKERRQ(ierr);
332e884886fSBarry Smith #endif
333e884886fSBarry Smith   if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) {
334e884886fSBarry Smith     ctx->historyh[ctx->ncurrenth] = h;
335e884886fSBarry Smith   }
336e884886fSBarry Smith   ctx->ncurrenth++;
337e884886fSBarry Smith 
338e884886fSBarry Smith   /* w = u + ha */
339e884886fSBarry Smith   ierr = VecWAXPY(w,h,a,U);CHKERRQ(ierr);
340e884886fSBarry Smith 
341bcddec3dSBarry Smith   /* compute func(U) as base for differencing; only needed first time in and not when provided by user */
342bcddec3dSBarry Smith   if (ctx->ncurrenth == 1 && ctx->current_f_allocated) {
343e884886fSBarry Smith     ierr = (*ctx->func)(ctx->funcctx,U,F);CHKERRQ(ierr);
34452121784SBarry Smith   }
345e884886fSBarry Smith   ierr = (*ctx->func)(ctx->funcctx,w,y);CHKERRQ(ierr);
346e884886fSBarry Smith 
347e884886fSBarry Smith   ierr = VecAXPY(y,-1.0,F);CHKERRQ(ierr);
348e884886fSBarry Smith   ierr = VecScale(y,1.0/h);CHKERRQ(ierr);
349e884886fSBarry Smith 
350e884886fSBarry Smith   ierr = VecAXPBY(y,ctx->vshift,ctx->vscale,a);CHKERRQ(ierr);
351e884886fSBarry Smith 
352e884886fSBarry Smith   if (ctx->sp) {ierr = MatNullSpaceRemove(ctx->sp,y,PETSC_NULL);CHKERRQ(ierr);}
353e884886fSBarry Smith 
354e884886fSBarry Smith   ierr = PetscLogEventEnd(MATMFFD_Mult,a,y,0,0);CHKERRQ(ierr);
355e884886fSBarry Smith   PetscFunctionReturn(0);
356e884886fSBarry Smith }
357e884886fSBarry Smith 
358e884886fSBarry Smith #undef __FUNCT__
359e884886fSBarry Smith #define __FUNCT__ "MatGetDiagonal_MFFD"
360e884886fSBarry Smith /*
361e884886fSBarry Smith   MatGetDiagonal_MFFD - Gets the diagonal for a matrix free matrix
362e884886fSBarry Smith 
363e884886fSBarry Smith         y ~= (F(u + ha) - F(u))/h,
364e884886fSBarry Smith   where F = nonlinear function, as set by SNESSetFunction()
365e884886fSBarry Smith         u = current iterate
366e884886fSBarry Smith         h = difference interval
367e884886fSBarry Smith */
368e884886fSBarry Smith PetscErrorCode MatGetDiagonal_MFFD(Mat mat,Vec a)
369e884886fSBarry Smith {
370e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)mat->data;
371e884886fSBarry Smith   PetscScalar    h,*aa,*ww,v;
372e884886fSBarry Smith   PetscReal      epsilon = PETSC_SQRT_MACHINE_EPSILON,umin = 100.0*PETSC_SQRT_MACHINE_EPSILON;
373e884886fSBarry Smith   Vec            w,U;
374e884886fSBarry Smith   PetscErrorCode ierr;
375e884886fSBarry Smith   PetscInt       i,rstart,rend;
376e884886fSBarry Smith 
377e884886fSBarry Smith   PetscFunctionBegin;
378e884886fSBarry Smith   if (!ctx->funci) {
379e884886fSBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Requires calling MatMFFDSetFunctioni() first");
380e884886fSBarry Smith   }
381e884886fSBarry Smith 
382e884886fSBarry Smith   w    = ctx->w;
383e884886fSBarry Smith   U    = ctx->current_u;
384e884886fSBarry Smith   ierr = (*ctx->func)(ctx->funcctx,U,a);CHKERRQ(ierr);
385e884886fSBarry Smith   ierr = (*ctx->funcisetbase)(ctx->funcctx,U);CHKERRQ(ierr);
386e884886fSBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
387e884886fSBarry Smith 
388e884886fSBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
389e884886fSBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
390e884886fSBarry Smith   for (i=rstart; i<rend; i++) {
391e884886fSBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
392e884886fSBarry Smith     h  = ww[i-rstart];
393e884886fSBarry Smith     if (h == 0.0) h = 1.0;
394e884886fSBarry Smith #if !defined(PETSC_USE_COMPLEX)
395e884886fSBarry Smith     if (h < umin && h >= 0.0)      h = umin;
396e884886fSBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
397e884886fSBarry Smith #else
398e884886fSBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
399e884886fSBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
400e884886fSBarry Smith #endif
401e884886fSBarry Smith     h     *= epsilon;
402e884886fSBarry Smith 
403e884886fSBarry Smith     ww[i-rstart] += h;
404e884886fSBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
405e884886fSBarry Smith     ierr          = (*ctx->funci)(ctx->funcctx,i,w,&v);CHKERRQ(ierr);
406e884886fSBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
407e884886fSBarry Smith 
408e884886fSBarry Smith     /* possibly shift and scale result */
409e884886fSBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
410e884886fSBarry Smith 
411e884886fSBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
412e884886fSBarry Smith     ww[i-rstart] -= h;
413e884886fSBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
414e884886fSBarry Smith   }
415e884886fSBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
416e884886fSBarry Smith   PetscFunctionReturn(0);
417e884886fSBarry Smith }
418e884886fSBarry Smith 
419e884886fSBarry Smith #undef __FUNCT__
420e884886fSBarry Smith #define __FUNCT__ "MatShift_MFFD"
421e884886fSBarry Smith PetscErrorCode MatShift_MFFD(Mat Y,PetscScalar a)
422e884886fSBarry Smith {
423e884886fSBarry Smith   MatMFFD shell = (MatMFFD)Y->data;
424e884886fSBarry Smith   PetscFunctionBegin;
425e884886fSBarry Smith   shell->vshift += a;
426e884886fSBarry Smith   PetscFunctionReturn(0);
427e884886fSBarry Smith }
428e884886fSBarry Smith 
429e884886fSBarry Smith #undef __FUNCT__
430e884886fSBarry Smith #define __FUNCT__ "MatScale_MFFD"
431e884886fSBarry Smith PetscErrorCode MatScale_MFFD(Mat Y,PetscScalar a)
432e884886fSBarry Smith {
433e884886fSBarry Smith   MatMFFD shell = (MatMFFD)Y->data;
434e884886fSBarry Smith   PetscFunctionBegin;
435e884886fSBarry Smith   shell->vscale *= a;
436e884886fSBarry Smith   PetscFunctionReturn(0);
437e884886fSBarry Smith }
438e884886fSBarry Smith 
439e884886fSBarry Smith EXTERN_C_BEGIN
440e884886fSBarry Smith #undef __FUNCT__
441c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetBase_MFFD"
442c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_MFFD(Mat J,Vec U,Vec F)
443e884886fSBarry Smith {
444e884886fSBarry Smith   PetscErrorCode ierr;
445e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)J->data;
446e884886fSBarry Smith 
447e884886fSBarry Smith   PetscFunctionBegin;
448e884886fSBarry Smith   ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr);
449e884886fSBarry Smith   ctx->current_u = U;
45052121784SBarry Smith   if (F) {
451cfe22f5eSBarry Smith     if (ctx->current_f_allocated) {ierr = VecDestroy(ctx->current_f);CHKERRQ(ierr);}
4523ec795f1SBarry Smith     ctx->current_f           = F;
453cfe22f5eSBarry Smith     ctx->current_f_allocated = PETSC_FALSE;
454cfe22f5eSBarry Smith   } else if (!ctx->current_f_allocated) {
45552121784SBarry Smith     ierr = VecDuplicate(ctx->current_u, &ctx->current_f);CHKERRQ(ierr);
456cfe22f5eSBarry Smith     ctx->current_f_allocated = PETSC_TRUE;
45752121784SBarry Smith   }
458e884886fSBarry Smith   if (!ctx->w) {
459e884886fSBarry Smith     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
460e884886fSBarry Smith   }
461e884886fSBarry Smith   J->assembled = PETSC_TRUE;
462e884886fSBarry Smith   PetscFunctionReturn(0);
463e884886fSBarry Smith }
464e884886fSBarry Smith EXTERN_C_END
465e884886fSBarry Smith typedef PetscErrorCode (*FCN3)(void*,Vec,Vec,PetscScalar*); /* force argument to next function to not be extern C*/
466e884886fSBarry Smith EXTERN_C_BEGIN
467e884886fSBarry Smith #undef __FUNCT__
468c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh_MFFD"
469c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh_MFFD(Mat J,FCN3 fun,void*ectx)
470e884886fSBarry Smith {
471e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
472e884886fSBarry Smith 
473e884886fSBarry Smith   PetscFunctionBegin;
474e884886fSBarry Smith   ctx->checkh    = fun;
475e884886fSBarry Smith   ctx->checkhctx = ectx;
476e884886fSBarry Smith   PetscFunctionReturn(0);
477e884886fSBarry Smith }
478e884886fSBarry Smith EXTERN_C_END
479e884886fSBarry Smith 
480e884886fSBarry Smith #undef __FUNCT__
4816aa9148fSLisandro Dalcin #define __FUNCT__ "MatMFFDSetOptionsPrefix"
4826aa9148fSLisandro Dalcin /*@C
4836aa9148fSLisandro Dalcin    MatMFFDSetOptionsPrefix - Sets the prefix used for searching for all
4846aa9148fSLisandro Dalcin    MatMFFD options in the database.
4856aa9148fSLisandro Dalcin 
4866aa9148fSLisandro Dalcin    Collective on Mat
4876aa9148fSLisandro Dalcin 
4886aa9148fSLisandro Dalcin    Input Parameter:
4896aa9148fSLisandro Dalcin +  A - the Mat context
4906aa9148fSLisandro Dalcin -  prefix - the prefix to prepend to all option names
4916aa9148fSLisandro Dalcin 
4926aa9148fSLisandro Dalcin    Notes:
4936aa9148fSLisandro Dalcin    A hyphen (-) must NOT be given at the beginning of the prefix name.
4946aa9148fSLisandro Dalcin    The first character of all runtime options is AUTOMATICALLY the hyphen.
4956aa9148fSLisandro Dalcin 
4966aa9148fSLisandro Dalcin    Level: advanced
4976aa9148fSLisandro Dalcin 
4986aa9148fSLisandro Dalcin .keywords: SNES, matrix-free, parameters
4996aa9148fSLisandro Dalcin 
5006aa9148fSLisandro Dalcin .seealso: MatMFFDSetFromOptions(), MatCreateSNESMF()
5016aa9148fSLisandro Dalcin @*/
5026aa9148fSLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetOptionsPrefix(Mat mat,const char prefix[])
5036aa9148fSLisandro Dalcin 
5046aa9148fSLisandro Dalcin {
5056aa9148fSLisandro Dalcin   MatMFFD        mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL;
5066aa9148fSLisandro Dalcin   PetscErrorCode ierr;
5076aa9148fSLisandro Dalcin   PetscFunctionBegin;
508*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
509*0700a824SBarry Smith   PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1);
5106aa9148fSLisandro Dalcin   ierr = PetscObjectSetOptionsPrefix((PetscObject)mfctx,prefix);CHKERRQ(ierr);
5116aa9148fSLisandro Dalcin   PetscFunctionReturn(0);
5126aa9148fSLisandro Dalcin }
5136aa9148fSLisandro Dalcin 
5146aa9148fSLisandro Dalcin #undef __FUNCT__
515e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFromOptions"
516e884886fSBarry Smith /*@
517e884886fSBarry Smith    MatMFFDSetFromOptions - Sets the MatMFFD options from the command line
518e884886fSBarry Smith    parameter.
519e884886fSBarry Smith 
520e884886fSBarry Smith    Collective on Mat
521e884886fSBarry Smith 
522e884886fSBarry Smith    Input Parameters:
523e884886fSBarry Smith .  mat - the matrix obtained with MatCreateMFFD() or MatCreateSNESMF()
524e884886fSBarry Smith 
525e884886fSBarry Smith    Options Database Keys:
526e884886fSBarry Smith +  -mat_mffd_type - wp or ds (see MATMFFD_WP or MATMFFD_DS)
527e884886fSBarry Smith -  -mat_mffd_err - square root of estimated relative error in function evaluation
528e884886fSBarry Smith -  -mat_mffd_period - how often h is recomputed, defaults to 1, everytime
529e884886fSBarry Smith 
530e884886fSBarry Smith    Level: advanced
531e884886fSBarry Smith 
532e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
533e884886fSBarry Smith 
534e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(),
535e884886fSBarry Smith           MatMFFDResetHHistory(), MatMFFDKSPMonitor()
536e884886fSBarry Smith @*/
537e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFromOptions(Mat mat)
538e884886fSBarry Smith {
5396aa9148fSLisandro Dalcin   MatMFFD        mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL;
540e884886fSBarry Smith   PetscErrorCode ierr;
541e884886fSBarry Smith   PetscTruth     flg;
542e884886fSBarry Smith   char           ftype[256];
543e884886fSBarry Smith 
544e884886fSBarry Smith   PetscFunctionBegin;
545*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
546*0700a824SBarry Smith   PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1);
5477adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)mfctx)->comm,((PetscObject)mfctx)->prefix,"Set matrix free computation parameters","MatMFFD");CHKERRQ(ierr);
548b022a5c1SBarry Smith   ierr = PetscOptionsList("-mat_mffd_type","Matrix free type","MatMFFDSetType",MatMFFDList,((PetscObject)mfctx)->type_name,ftype,256,&flg);CHKERRQ(ierr);
549e884886fSBarry Smith   if (flg) {
550e884886fSBarry Smith     ierr = MatMFFDSetType(mat,ftype);CHKERRQ(ierr);
551e884886fSBarry Smith   }
552e884886fSBarry Smith 
553e884886fSBarry Smith   ierr = PetscOptionsReal("-mat_mffd_err","set sqrt relative error in function","MatMFFDSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
554e884886fSBarry Smith   ierr = PetscOptionsInt("-mat_mffd_period","how often h is recomputed","MatMFFDSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
555e884886fSBarry Smith 
55690d69ab7SBarry Smith   flg  = PETSC_FALSE;
55790d69ab7SBarry Smith   ierr = PetscOptionsTruth("-mat_mffd_check_positivity","Insure that U + h*a is nonnegative","MatMFFDSetCheckh",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
558e884886fSBarry Smith   if (flg) {
559e884886fSBarry Smith     ierr = MatMFFDSetCheckh(mat,MatMFFDCheckPositivity,0);CHKERRQ(ierr);
560e884886fSBarry Smith   }
561e884886fSBarry Smith   if (mfctx->ops->setfromoptions) {
562e884886fSBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
563e884886fSBarry Smith   }
564e884886fSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
565e884886fSBarry Smith   PetscFunctionReturn(0);
566e884886fSBarry Smith }
567e884886fSBarry Smith 
568e884886fSBarry Smith /*MC
569e884886fSBarry Smith   MATMFFD - MATMFFD = "mffd" - A matrix free matrix type.
570e884886fSBarry Smith 
571e884886fSBarry Smith   Level: advanced
572e884886fSBarry Smith 
573d2d6cebeSBarry Smith .seealso: MatCreateMFFD(), MatCreateSNESMF(), MatMFFDSetFunction()
574e884886fSBarry Smith M*/
575e884886fSBarry Smith EXTERN_C_BEGIN
576e884886fSBarry Smith #undef __FUNCT__
577e884886fSBarry Smith #define __FUNCT__ "MatCreate_MFFD"
578e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MFFD(Mat A)
579e884886fSBarry Smith {
580e884886fSBarry Smith   MatMFFD         mfctx;
581e884886fSBarry Smith   PetscErrorCode  ierr;
582e884886fSBarry Smith 
583e884886fSBarry Smith   PetscFunctionBegin;
5843ec795f1SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5853ec795f1SBarry Smith   ierr = MatMFFDInitializePackage(PETSC_NULL);CHKERRQ(ierr);
5863ec795f1SBarry Smith #endif
587e884886fSBarry Smith 
588*0700a824SBarry Smith   ierr = PetscHeaderCreate(mfctx,_p_MatMFFD,struct _MFOps,MATMFFD_CLASSID,0,"MatMFFD",((PetscObject)A)->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr);
589e884886fSBarry Smith   mfctx->sp              = 0;
590e884886fSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
591e884886fSBarry Smith   mfctx->recomputeperiod = 1;
592e884886fSBarry Smith   mfctx->count           = 0;
593e884886fSBarry Smith   mfctx->currenth        = 0.0;
594e884886fSBarry Smith   mfctx->historyh        = PETSC_NULL;
595e884886fSBarry Smith   mfctx->ncurrenth       = 0;
596e884886fSBarry Smith   mfctx->maxcurrenth     = 0;
5977adad957SLisandro Dalcin   ((PetscObject)mfctx)->type_name       = 0;
598e884886fSBarry Smith 
599e884886fSBarry Smith   mfctx->vshift          = 0.0;
600e884886fSBarry Smith   mfctx->vscale          = 1.0;
601e884886fSBarry Smith 
602e884886fSBarry Smith   /*
603e884886fSBarry Smith      Create the empty data structure to contain compute-h routines.
604e884886fSBarry Smith      These will be filled in below from the command line options or
605e884886fSBarry Smith      a later call with MatMFFDSetType() or if that is not called
606e884886fSBarry Smith      then it will default in the first use of MatMult_MFFD()
607e884886fSBarry Smith   */
608e884886fSBarry Smith   mfctx->ops->compute        = 0;
609e884886fSBarry Smith   mfctx->ops->destroy        = 0;
610e884886fSBarry Smith   mfctx->ops->view           = 0;
611e884886fSBarry Smith   mfctx->ops->setfromoptions = 0;
612e884886fSBarry Smith   mfctx->hctx                = 0;
613e884886fSBarry Smith 
614e884886fSBarry Smith   mfctx->func                = 0;
615e884886fSBarry Smith   mfctx->funcctx             = 0;
616e884886fSBarry Smith   mfctx->w                   = PETSC_NULL;
617e884886fSBarry Smith 
618e884886fSBarry Smith   A->data                = mfctx;
619e884886fSBarry Smith 
620e884886fSBarry Smith   A->ops->mult           = MatMult_MFFD;
621e884886fSBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
622e884886fSBarry Smith   A->ops->view           = MatView_MFFD;
623e884886fSBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
624e884886fSBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
625e884886fSBarry Smith   A->ops->scale          = MatScale_MFFD;
626e884886fSBarry Smith   A->ops->shift          = MatShift_MFFD;
627e884886fSBarry Smith   A->ops->setfromoptions = MatMFFDSetFromOptions;
628e884886fSBarry Smith   A->assembled = PETSC_TRUE;
629e884886fSBarry Smith 
63026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(A->rmap,1);CHKERRQ(ierr);
63126283091SBarry Smith   ierr = PetscLayoutSetBlockSize(A->cmap,1);CHKERRQ(ierr);
63226283091SBarry Smith   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
63326283091SBarry Smith   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
634ee1cef2cSJed Brown 
635c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetBase_C","MatMFFDSetBase_MFFD",MatMFFDSetBase_MFFD);CHKERRQ(ierr);
636c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioniBase_C","MatMFFDSetFunctioniBase_MFFD",MatMFFDSetFunctioniBase_MFFD);CHKERRQ(ierr);
637c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioni_C","MatMFFDSetFunctioni_MFFD",MatMFFDSetFunctioni_MFFD);CHKERRQ(ierr);
638c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetCheckh_C","MatMFFDSetCheckh_MFFD",MatMFFDSetCheckh_MFFD);CHKERRQ(ierr);
639e884886fSBarry Smith   mfctx->mat = A;
640e884886fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)A,MATMFFD);CHKERRQ(ierr);
641e884886fSBarry Smith   PetscFunctionReturn(0);
642e884886fSBarry Smith }
643e884886fSBarry Smith EXTERN_C_END
644e884886fSBarry Smith 
645e884886fSBarry Smith #undef __FUNCT__
646e884886fSBarry Smith #define __FUNCT__ "MatCreateMFFD"
647e884886fSBarry Smith /*@
648e884886fSBarry Smith    MatCreateMFFD - Creates a matrix-free matrix. See also MatCreateSNESMF()
649e884886fSBarry Smith 
650e884886fSBarry Smith    Collective on Vec
651e884886fSBarry Smith 
652e884886fSBarry Smith    Input Parameters:
653fef1beadSBarry Smith +  comm - MPI communicator
654fef1beadSBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
655fef1beadSBarry Smith            This value should be the same as the local size used in creating the
656fef1beadSBarry Smith            y vector for the matrix-vector product y = Ax.
657fef1beadSBarry Smith .  n - This value should be the same as the local size used in creating the
658fef1beadSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
659fef1beadSBarry Smith        calculated if N is given) For square matrices n is almost always m.
660fef1beadSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
661fef1beadSBarry Smith -  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
662fef1beadSBarry Smith 
663e884886fSBarry Smith 
664e884886fSBarry Smith    Output Parameter:
665e884886fSBarry Smith .  J - the matrix-free matrix
666e884886fSBarry Smith 
667e884886fSBarry Smith    Level: advanced
668e884886fSBarry Smith 
669e884886fSBarry Smith    Notes:
670e884886fSBarry Smith    The matrix-free matrix context merely contains the function pointers
671e884886fSBarry Smith    and work space for performing finite difference approximations of
672e884886fSBarry Smith    Jacobian-vector products, F'(u)*a,
673e884886fSBarry Smith 
674e884886fSBarry Smith    The default code uses the following approach to compute h
675e884886fSBarry Smith 
676e884886fSBarry Smith .vb
677e884886fSBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
678e884886fSBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
679e884886fSBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
680e884886fSBarry Smith  where
681e884886fSBarry Smith      error_rel = square root of relative error in function evaluation
682e884886fSBarry Smith      umin = minimum iterate parameter
683e884886fSBarry Smith .ve
684e884886fSBarry Smith 
685e884886fSBarry Smith    The user can set the error_rel via MatMFFDSetFunctionError() and
686e884886fSBarry Smith    umin via MatMFFDDefaultSetUmin(); see the nonlinear solvers chapter
687e884886fSBarry Smith    of the users manual for details.
688e884886fSBarry Smith 
689e884886fSBarry Smith    The user should call MatDestroy() when finished with the matrix-free
690e884886fSBarry Smith    matrix context.
691e884886fSBarry Smith 
692e884886fSBarry Smith    Options Database Keys:
693e884886fSBarry Smith +  -mat_mffd_err <error_rel> - Sets error_rel
694e884886fSBarry Smith .  -mat_mffd_unim <umin> - Sets umin (for default PETSc routine that computes h only)
695e884886fSBarry Smith .  -mat_mffd_ksp_monitor - KSP monitor routine that prints differencing h
696e884886fSBarry Smith -  -mat_mffd_check_positivity
697e884886fSBarry Smith 
698e884886fSBarry Smith .keywords: default, matrix-free, create, matrix
699e884886fSBarry Smith 
700e884886fSBarry Smith .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDefaultSetUmin(), MatMFFDSetFunction()
701e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateSNESMF(),
702e884886fSBarry Smith           MatMFFDGetH(),MatMFFDKSPMonitor(), MatMFFDRegisterDynamic),, MatMFFDComputeJacobian()
703e884886fSBarry Smith 
704e884886fSBarry Smith @*/
705fef1beadSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMFFD(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,Mat *J)
706e884886fSBarry Smith {
707e884886fSBarry Smith   PetscErrorCode ierr;
708e884886fSBarry Smith 
709e884886fSBarry Smith   PetscFunctionBegin;
710e884886fSBarry Smith   ierr = MatCreate(comm,J);CHKERRQ(ierr);
711fef1beadSBarry Smith   ierr = MatSetSizes(*J,m,n,M,N);CHKERRQ(ierr);
712e884886fSBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
713e884886fSBarry Smith   PetscFunctionReturn(0);
714e884886fSBarry Smith }
715e884886fSBarry Smith 
716e884886fSBarry Smith 
717e884886fSBarry Smith #undef __FUNCT__
718e884886fSBarry Smith #define __FUNCT__ "MatMFFDGetH"
719e884886fSBarry Smith /*@
720e884886fSBarry Smith    MatMFFDGetH - Gets the last value that was used as the differencing
721e884886fSBarry Smith    parameter.
722e884886fSBarry Smith 
723e884886fSBarry Smith    Not Collective
724e884886fSBarry Smith 
725e884886fSBarry Smith    Input Parameters:
726e884886fSBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
727e884886fSBarry Smith 
728e884886fSBarry Smith    Output Paramter:
729e884886fSBarry Smith .  h - the differencing step size
730e884886fSBarry Smith 
731e884886fSBarry Smith    Level: advanced
732e884886fSBarry Smith 
733e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
734e884886fSBarry Smith 
735e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatCreateMFFD(), MATMFFD
736e884886fSBarry Smith           MatMFFDResetHHistory(),MatMFFDKSPMonitor()
737e884886fSBarry Smith @*/
738e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDGetH(Mat mat,PetscScalar *h)
739e884886fSBarry Smith {
740e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
741e884886fSBarry Smith 
742e884886fSBarry Smith   PetscFunctionBegin;
743e884886fSBarry Smith   *h = ctx->currenth;
744e884886fSBarry Smith   PetscFunctionReturn(0);
745e884886fSBarry Smith }
746e884886fSBarry Smith 
747e884886fSBarry Smith 
748e884886fSBarry Smith #undef __FUNCT__
749e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunction"
750e884886fSBarry Smith /*@C
751e884886fSBarry Smith    MatMFFDSetFunction - Sets the function used in applying the matrix free.
752e884886fSBarry Smith 
753e884886fSBarry Smith    Collective on Mat
754e884886fSBarry Smith 
755e884886fSBarry Smith    Input Parameters:
756e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
757e884886fSBarry Smith .  func - the function to use
758e884886fSBarry Smith -  funcctx - optional function context passed to function
759e884886fSBarry Smith 
760e884886fSBarry Smith    Level: advanced
761e884886fSBarry Smith 
762e884886fSBarry Smith    Notes:
763e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
764e884886fSBarry Smith     matrix inside your compute Jacobian routine
765e884886fSBarry Smith 
7663ec795f1SBarry Smith     If this is not set then it will use the function set with SNESSetFunction() if MatCreateSNESMF() was used.
767e884886fSBarry Smith 
768e884886fSBarry Smith .keywords: SNES, matrix-free, function
769e884886fSBarry Smith 
770e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD
771e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
772e884886fSBarry Smith           MatMFFDKSPMonitor(), SNESetFunction()
773e884886fSBarry Smith @*/
7743ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunction(Mat mat,PetscErrorCode (*func)(void*,Vec,Vec),void *funcctx)
775e884886fSBarry Smith {
776e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
777e884886fSBarry Smith 
778e884886fSBarry Smith   PetscFunctionBegin;
779e884886fSBarry Smith   ctx->func    = func;
780e884886fSBarry Smith   ctx->funcctx = funcctx;
781e884886fSBarry Smith   PetscFunctionReturn(0);
782e884886fSBarry Smith }
783e884886fSBarry Smith 
784e884886fSBarry Smith #undef __FUNCT__
785e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni"
786e884886fSBarry Smith /*@C
787e884886fSBarry Smith    MatMFFDSetFunctioni - Sets the function for a single component
788e884886fSBarry Smith 
789e884886fSBarry Smith    Collective on Mat
790e884886fSBarry Smith 
791e884886fSBarry Smith    Input Parameters:
792e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
793e884886fSBarry Smith -  funci - the function to use
794e884886fSBarry Smith 
795e884886fSBarry Smith    Level: advanced
796e884886fSBarry Smith 
797e884886fSBarry Smith    Notes:
798e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
799e884886fSBarry Smith     matrix inside your compute Jacobian routine
800e884886fSBarry Smith 
801e884886fSBarry Smith 
802e884886fSBarry Smith .keywords: SNES, matrix-free, function
803e884886fSBarry Smith 
804e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(),
805e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
806e884886fSBarry Smith           MatMFFDKSPMonitor(), SNESetFunction()
807e884886fSBarry Smith @*/
808e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni(Mat mat,PetscErrorCode (*funci)(void*,PetscInt,Vec,PetscScalar*))
809e884886fSBarry Smith {
810e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,PetscInt,Vec,PetscScalar*));
811e884886fSBarry Smith 
812e884886fSBarry Smith   PetscFunctionBegin;
813*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
814e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
815e884886fSBarry Smith   if (f) {
816e884886fSBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
817e884886fSBarry Smith   }
818e884886fSBarry Smith   PetscFunctionReturn(0);
819e884886fSBarry Smith }
820e884886fSBarry Smith 
821e884886fSBarry Smith 
822e884886fSBarry Smith #undef __FUNCT__
823e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase"
824e884886fSBarry Smith /*@C
825e884886fSBarry Smith    MatMFFDSetFunctioniBase - Sets the base vector for a single component function evaluation
826e884886fSBarry Smith 
827e884886fSBarry Smith    Collective on Mat
828e884886fSBarry Smith 
829e884886fSBarry Smith    Input Parameters:
830e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
831e884886fSBarry Smith -  func - the function to use
832e884886fSBarry Smith 
833e884886fSBarry Smith    Level: advanced
834e884886fSBarry Smith 
835e884886fSBarry Smith    Notes:
836e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
837e884886fSBarry Smith     matrix inside your compute Jacobian routine
838e884886fSBarry Smith 
839e884886fSBarry Smith 
840e884886fSBarry Smith .keywords: SNES, matrix-free, function
841e884886fSBarry Smith 
842e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD
843e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
844e884886fSBarry Smith           MatMFFDKSPMonitor(), SNESetFunction()
845e884886fSBarry Smith @*/
846e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase(Mat mat,PetscErrorCode (*func)(void*,Vec))
847e884886fSBarry Smith {
848e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec));
849e884886fSBarry Smith 
850e884886fSBarry Smith   PetscFunctionBegin;
851*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
852e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
853e884886fSBarry Smith   if (f) {
854e884886fSBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
855e884886fSBarry Smith   }
856e884886fSBarry Smith   PetscFunctionReturn(0);
857e884886fSBarry Smith }
858e884886fSBarry Smith 
859e884886fSBarry Smith 
860e884886fSBarry Smith #undef __FUNCT__
861e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetPeriod"
862e884886fSBarry Smith /*@
863e884886fSBarry Smith    MatMFFDSetPeriod - Sets how often h is recomputed, by default it is everytime
864e884886fSBarry Smith 
865e884886fSBarry Smith    Collective on Mat
866e884886fSBarry Smith 
867e884886fSBarry Smith    Input Parameters:
868e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
869e884886fSBarry Smith -  period - 1 for everytime, 2 for every second etc
870e884886fSBarry Smith 
871e884886fSBarry Smith    Options Database Keys:
872e884886fSBarry Smith +  -mat_mffd_period <period>
873e884886fSBarry Smith 
874e884886fSBarry Smith    Level: advanced
875e884886fSBarry Smith 
876e884886fSBarry Smith 
877e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
878e884886fSBarry Smith 
879e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(),
880e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
881e884886fSBarry Smith           MatMFFDKSPMonitor()
882e884886fSBarry Smith @*/
883e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetPeriod(Mat mat,PetscInt period)
884e884886fSBarry Smith {
885e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
886e884886fSBarry Smith 
887e884886fSBarry Smith   PetscFunctionBegin;
888e884886fSBarry Smith   ctx->recomputeperiod = period;
889e884886fSBarry Smith   PetscFunctionReturn(0);
890e884886fSBarry Smith }
891e884886fSBarry Smith 
892e884886fSBarry Smith #undef __FUNCT__
893e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctionError"
894e884886fSBarry Smith /*@
895e884886fSBarry Smith    MatMFFDSetFunctionError - Sets the error_rel for the approximation of
896e884886fSBarry Smith    matrix-vector products using finite differences.
897e884886fSBarry Smith 
898e884886fSBarry Smith    Collective on Mat
899e884886fSBarry Smith 
900e884886fSBarry Smith    Input Parameters:
901e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateMFFD() or MatCreateSNESMF()
902e884886fSBarry Smith -  error_rel - relative error (should be set to the square root of
903e884886fSBarry Smith                the relative error in the function evaluations)
904e884886fSBarry Smith 
905e884886fSBarry Smith    Options Database Keys:
906e884886fSBarry Smith +  -mat_mffd_err <error_rel> - Sets error_rel
907e884886fSBarry Smith 
908e884886fSBarry Smith    Level: advanced
909e884886fSBarry Smith 
910e884886fSBarry Smith    Notes:
911e884886fSBarry Smith    The default matrix-free matrix-vector product routine computes
912e884886fSBarry Smith .vb
913e884886fSBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
914e884886fSBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
915e884886fSBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
916e884886fSBarry Smith .ve
917e884886fSBarry Smith 
918e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
919e884886fSBarry Smith 
920e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD
921e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
922e884886fSBarry Smith           MatMFFDKSPMonitor()
923e884886fSBarry Smith @*/
924e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctionError(Mat mat,PetscReal error)
925e884886fSBarry Smith {
926e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
927e884886fSBarry Smith 
928e884886fSBarry Smith   PetscFunctionBegin;
929e884886fSBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
930e884886fSBarry Smith   PetscFunctionReturn(0);
931e884886fSBarry Smith }
932e884886fSBarry Smith 
933e884886fSBarry Smith #undef __FUNCT__
934e884886fSBarry Smith #define __FUNCT__ "MatMFFDAddNullSpace"
935e884886fSBarry Smith /*@
936e884886fSBarry Smith    MatMFFDAddNullSpace - Provides a null space that an operator is
937e884886fSBarry Smith    supposed to have.  Since roundoff will create a small component in
938e884886fSBarry Smith    the null space, if you know the null space you may have it
939e884886fSBarry Smith    automatically removed.
940e884886fSBarry Smith 
941e884886fSBarry Smith    Collective on Mat
942e884886fSBarry Smith 
943e884886fSBarry Smith    Input Parameters:
944e884886fSBarry Smith +  J - the matrix-free matrix context
945e884886fSBarry Smith -  nullsp - object created with MatNullSpaceCreate()
946e884886fSBarry Smith 
947e884886fSBarry Smith    Level: advanced
948e884886fSBarry Smith 
949e884886fSBarry Smith .keywords: SNES, matrix-free, null space
950e884886fSBarry Smith 
951e884886fSBarry Smith .seealso: MatNullSpaceCreate(), MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD
952e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(),
953e884886fSBarry Smith           MatMFFDKSPMonitor(), MatMFFDErrorRel()
954e884886fSBarry Smith @*/
955e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDAddNullSpace(Mat J,MatNullSpace nullsp)
956e884886fSBarry Smith {
957e884886fSBarry Smith   PetscErrorCode ierr;
958e884886fSBarry Smith   MatMFFD      ctx = (MatMFFD)J->data;
959e884886fSBarry Smith 
960e884886fSBarry Smith   PetscFunctionBegin;
961e884886fSBarry Smith   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
962e884886fSBarry Smith   if (ctx->sp) { ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr); }
963e884886fSBarry Smith   ctx->sp = nullsp;
964e884886fSBarry Smith   PetscFunctionReturn(0);
965e884886fSBarry Smith }
966e884886fSBarry Smith 
967e884886fSBarry Smith #undef __FUNCT__
968e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetHHistory"
969e884886fSBarry Smith /*@
970e884886fSBarry Smith    MatMFFDSetHHistory - Sets an array to collect a history of the
971e884886fSBarry Smith    differencing values (h) computed for the matrix-free product.
972e884886fSBarry Smith 
973e884886fSBarry Smith    Collective on Mat
974e884886fSBarry Smith 
975e884886fSBarry Smith    Input Parameters:
976e884886fSBarry Smith +  J - the matrix-free matrix context
977e884886fSBarry Smith .  histroy - space to hold the history
978e884886fSBarry Smith -  nhistory - number of entries in history, if more entries are generated than
979e884886fSBarry Smith               nhistory, then the later ones are discarded
980e884886fSBarry Smith 
981e884886fSBarry Smith    Level: advanced
982e884886fSBarry Smith 
983e884886fSBarry Smith    Notes:
984e884886fSBarry Smith    Use MatMFFDResetHHistory() to reset the history counter and collect
985e884886fSBarry Smith    a new batch of differencing parameters, h.
986e884886fSBarry Smith 
987e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history
988e884886fSBarry Smith 
989e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(),
990e884886fSBarry Smith           MatMFFDResetHHistory(),
991e884886fSBarry Smith           MatMFFDKSPMonitor(), MatMFFDSetFunctionError()
992e884886fSBarry Smith 
993e884886fSBarry Smith @*/
994e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory)
995e884886fSBarry Smith {
996e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
997e884886fSBarry Smith 
998e884886fSBarry Smith   PetscFunctionBegin;
999e884886fSBarry Smith   ctx->historyh    = history;
1000e884886fSBarry Smith   ctx->maxcurrenth = nhistory;
100175567043SBarry Smith   ctx->currenth    = 0.;
1002e884886fSBarry Smith   PetscFunctionReturn(0);
1003e884886fSBarry Smith }
1004e884886fSBarry Smith 
1005e884886fSBarry Smith #undef __FUNCT__
1006e884886fSBarry Smith #define __FUNCT__ "MatMFFDResetHHistory"
1007e884886fSBarry Smith /*@
1008e884886fSBarry Smith    MatMFFDResetHHistory - Resets the counter to zero to begin
1009e884886fSBarry Smith    collecting a new set of differencing histories.
1010e884886fSBarry Smith 
1011e884886fSBarry Smith    Collective on Mat
1012e884886fSBarry Smith 
1013e884886fSBarry Smith    Input Parameters:
1014e884886fSBarry Smith .  J - the matrix-free matrix context
1015e884886fSBarry Smith 
1016e884886fSBarry Smith    Level: advanced
1017e884886fSBarry Smith 
1018e884886fSBarry Smith    Notes:
1019e884886fSBarry Smith    Use MatMFFDSetHHistory() to create the original history counter.
1020e884886fSBarry Smith 
1021e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1022e884886fSBarry Smith 
1023e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(),
1024e884886fSBarry Smith           MatMFFDSetHHistory(),
1025e884886fSBarry Smith           MatMFFDKSPMonitor(), MatMFFDSetFunctionError()
1026e884886fSBarry Smith 
1027e884886fSBarry Smith @*/
1028e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDResetHHistory(Mat J)
1029e884886fSBarry Smith {
1030e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
1031e884886fSBarry Smith 
1032e884886fSBarry Smith   PetscFunctionBegin;
1033e884886fSBarry Smith   ctx->ncurrenth    = 0;
1034e884886fSBarry Smith   PetscFunctionReturn(0);
1035e884886fSBarry Smith }
1036e884886fSBarry Smith 
1037e884886fSBarry Smith 
1038e884886fSBarry Smith #undef __FUNCT__
1039e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetBase"
1040e884886fSBarry Smith /*@
1041e884886fSBarry Smith     MatMFFDSetBase - Sets the vector U at which matrix vector products of the
1042e884886fSBarry Smith         Jacobian are computed
1043e884886fSBarry Smith 
1044e884886fSBarry Smith     Collective on Mat
1045e884886fSBarry Smith 
1046e884886fSBarry Smith     Input Parameters:
1047e884886fSBarry Smith +   J - the MatMFFD matrix
10483ec795f1SBarry Smith .   U - the vector
1049bcddec3dSBarry Smith -   F - (optional) vector that contains F(u) if it has been already computed
1050e884886fSBarry Smith 
1051e884886fSBarry Smith     Notes: This is rarely used directly
1052e884886fSBarry Smith 
10538af5ae88SBarry Smith     If F is provided then it is not recomputed. Otherwise the function is evaluated at the base
10548af5ae88SBarry Smith     point during the first MatMult() after each call to MatMFFDSetBase().
1055dff2f722SBarry Smith 
1056e884886fSBarry Smith     Level: advanced
1057e884886fSBarry Smith 
1058e884886fSBarry Smith @*/
10593ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase(Mat J,Vec U,Vec F)
1060e884886fSBarry Smith {
10613ec795f1SBarry Smith   PetscErrorCode ierr,(*f)(Mat,Vec,Vec);
1062e884886fSBarry Smith 
1063e884886fSBarry Smith   PetscFunctionBegin;
1064*0700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
1065*0700a824SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
1066*0700a824SBarry Smith   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,3);
1067e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1068e884886fSBarry Smith   if (f) {
10693ec795f1SBarry Smith     ierr = (*f)(J,U,F);CHKERRQ(ierr);
1070e884886fSBarry Smith   }
1071e884886fSBarry Smith   PetscFunctionReturn(0);
1072e884886fSBarry Smith }
1073e884886fSBarry Smith 
1074e884886fSBarry Smith #undef __FUNCT__
1075e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh"
1076e884886fSBarry Smith /*@C
1077e884886fSBarry Smith     MatMFFDSetCheckh - Sets a function that checks the computed h and adjusts
1078e884886fSBarry Smith         it to satisfy some criteria
1079e884886fSBarry Smith 
1080e884886fSBarry Smith     Collective on Mat
1081e884886fSBarry Smith 
1082e884886fSBarry Smith     Input Parameters:
1083e884886fSBarry Smith +   J - the MatMFFD matrix
1084e884886fSBarry Smith .   fun - the function that checks h
1085e884886fSBarry Smith -   ctx - any context needed by the function
1086e884886fSBarry Smith 
1087e884886fSBarry Smith     Options Database Keys:
1088e884886fSBarry Smith .   -mat_mffd_check_positivity
1089e884886fSBarry Smith 
1090e884886fSBarry Smith     Level: advanced
1091e884886fSBarry Smith 
1092e884886fSBarry Smith     Notes: For example, MatMFFDSetCheckPositivity() insures that all entries
1093e884886fSBarry Smith        of U + h*a are non-negative
1094e884886fSBarry Smith 
1095e884886fSBarry Smith .seealso:  MatMFFDSetCheckPositivity()
1096e884886fSBarry Smith @*/
1097e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh(Mat J,PetscErrorCode (*fun)(void*,Vec,Vec,PetscScalar*),void* ctx)
1098e884886fSBarry Smith {
1099e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec,Vec,PetscScalar*),void*);
1100e884886fSBarry Smith 
1101e884886fSBarry Smith   PetscFunctionBegin;
1102*0700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
1103e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
1104e884886fSBarry Smith   if (f) {
1105e884886fSBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
1106e884886fSBarry Smith   }
1107e884886fSBarry Smith   PetscFunctionReturn(0);
1108e884886fSBarry Smith }
1109e884886fSBarry Smith 
1110e884886fSBarry Smith #undef __FUNCT__
1111e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckPositivity"
1112e884886fSBarry Smith /*@
1113e884886fSBarry Smith     MatMFFDCheckPositivity - Checks that all entries in U + h*a are positive or
1114e884886fSBarry Smith         zero, decreases h until this is satisfied.
1115e884886fSBarry Smith 
1116e884886fSBarry Smith     Collective on Vec
1117e884886fSBarry Smith 
1118e884886fSBarry Smith     Input Parameters:
1119e884886fSBarry Smith +   U - base vector that is added to
1120e884886fSBarry Smith .   a - vector that is added
1121e884886fSBarry Smith .   h - scaling factor on a
1122e884886fSBarry Smith -   dummy - context variable (unused)
1123e884886fSBarry Smith 
1124e884886fSBarry Smith     Options Database Keys:
1125e884886fSBarry Smith .   -mat_mffd_check_positivity
1126e884886fSBarry Smith 
1127e884886fSBarry Smith     Level: advanced
1128e884886fSBarry Smith 
1129e884886fSBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
1130e884886fSBarry Smith            MatMFFDSetCheckh()
1131e884886fSBarry Smith 
1132e884886fSBarry Smith .seealso:  MatMFFDSetCheckh()
1133e884886fSBarry Smith @*/
1134e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDCheckPositivity(void* dummy,Vec U,Vec a,PetscScalar *h)
1135e884886fSBarry Smith {
1136e884886fSBarry Smith   PetscReal      val, minval;
1137e884886fSBarry Smith   PetscScalar    *u_vec, *a_vec;
1138e884886fSBarry Smith   PetscErrorCode ierr;
1139e884886fSBarry Smith   PetscInt       i,n;
1140e884886fSBarry Smith   MPI_Comm       comm;
1141e884886fSBarry Smith 
1142e884886fSBarry Smith   PetscFunctionBegin;
1143e884886fSBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
1144e884886fSBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
1145e884886fSBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
1146e884886fSBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
1147e884886fSBarry Smith   minval = PetscAbsScalar(*h*1.01);
1148e884886fSBarry Smith   for(i=0;i<n;i++) {
1149e884886fSBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
1150e884886fSBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
1151e884886fSBarry Smith       if (val < minval) minval = val;
1152e884886fSBarry Smith     }
1153e884886fSBarry Smith   }
1154e884886fSBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
1155e884886fSBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
1156e884886fSBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
1157e884886fSBarry Smith   if (val <= PetscAbsScalar(*h)) {
1158e884886fSBarry Smith     ierr = PetscInfo2(U,"Scaling back h from %G to %G\n",PetscRealPart(*h),.99*val);CHKERRQ(ierr);
1159e884886fSBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
1160e884886fSBarry Smith     else                         *h = -0.99*val;
1161e884886fSBarry Smith   }
1162e884886fSBarry Smith   PetscFunctionReturn(0);
1163e884886fSBarry Smith }
1164e884886fSBarry Smith 
1165e884886fSBarry Smith 
1166e884886fSBarry Smith 
1167e884886fSBarry Smith 
1168e884886fSBarry Smith 
1169e884886fSBarry Smith 
1170e884886fSBarry Smith 
1171