xref: /petsc/src/mat/impls/mffd/mffd.c (revision e7e72b3d0edcd0d15e7f68c03be08666507fc872)
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 
90700a824SBarry 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 */
590700a824SBarry Smith   ierr = PetscClassIdRegister("MatMFFD",&MATMFFD_CLASSID);CHKERRQ(ierr);
603ec795f1SBarry Smith   /* Register Constructors */
613ec795f1SBarry Smith   ierr = MatMFFDRegisterAll(path);CHKERRQ(ierr);
623ec795f1SBarry Smith   /* Register Events */
630700a824SBarry 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) {
700700a824SBarry 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) {
780700a824SBarry 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;
1150700a824SBarry 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);
128e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,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 {
254e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,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
2651d0fab5eSBarry Smith    MatCreateMFFD_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 
321e32f2f54SBarry Smith   if (PetscIsInfOrNanScalar(h)) SETERRQ(PETSC_COMM_SELF,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;
378*e7e72b3dSBarry Smith   if (!ctx->funci) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Requires calling MatMFFDSetFunctioni() first");
379e884886fSBarry Smith 
380e884886fSBarry Smith   w    = ctx->w;
381e884886fSBarry Smith   U    = ctx->current_u;
382e884886fSBarry Smith   ierr = (*ctx->func)(ctx->funcctx,U,a);CHKERRQ(ierr);
383e884886fSBarry Smith   ierr = (*ctx->funcisetbase)(ctx->funcctx,U);CHKERRQ(ierr);
384e884886fSBarry Smith   ierr = VecCopy(U,w);CHKERRQ(ierr);
385e884886fSBarry Smith 
386e884886fSBarry Smith   ierr = VecGetOwnershipRange(a,&rstart,&rend);CHKERRQ(ierr);
387e884886fSBarry Smith   ierr = VecGetArray(a,&aa);CHKERRQ(ierr);
388e884886fSBarry Smith   for (i=rstart; i<rend; i++) {
389e884886fSBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
390e884886fSBarry Smith     h  = ww[i-rstart];
391e884886fSBarry Smith     if (h == 0.0) h = 1.0;
392e884886fSBarry Smith #if !defined(PETSC_USE_COMPLEX)
393e884886fSBarry Smith     if (h < umin && h >= 0.0)      h = umin;
394e884886fSBarry Smith     else if (h < 0.0 && h > -umin) h = -umin;
395e884886fSBarry Smith #else
396e884886fSBarry Smith     if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0)     h = umin;
397e884886fSBarry Smith     else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
398e884886fSBarry Smith #endif
399e884886fSBarry Smith     h     *= epsilon;
400e884886fSBarry Smith 
401e884886fSBarry Smith     ww[i-rstart] += h;
402e884886fSBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
403e884886fSBarry Smith     ierr          = (*ctx->funci)(ctx->funcctx,i,w,&v);CHKERRQ(ierr);
404e884886fSBarry Smith     aa[i-rstart]  = (v - aa[i-rstart])/h;
405e884886fSBarry Smith 
406e884886fSBarry Smith     /* possibly shift and scale result */
407e884886fSBarry Smith     aa[i - rstart] = ctx->vshift + ctx->vscale*aa[i-rstart];
408e884886fSBarry Smith 
409e884886fSBarry Smith     ierr = VecGetArray(w,&ww);CHKERRQ(ierr);
410e884886fSBarry Smith     ww[i-rstart] -= h;
411e884886fSBarry Smith     ierr = VecRestoreArray(w,&ww);CHKERRQ(ierr);
412e884886fSBarry Smith   }
413e884886fSBarry Smith   ierr = VecRestoreArray(a,&aa);CHKERRQ(ierr);
414e884886fSBarry Smith   PetscFunctionReturn(0);
415e884886fSBarry Smith }
416e884886fSBarry Smith 
417e884886fSBarry Smith #undef __FUNCT__
418e884886fSBarry Smith #define __FUNCT__ "MatShift_MFFD"
419e884886fSBarry Smith PetscErrorCode MatShift_MFFD(Mat Y,PetscScalar a)
420e884886fSBarry Smith {
421e884886fSBarry Smith   MatMFFD shell = (MatMFFD)Y->data;
422e884886fSBarry Smith   PetscFunctionBegin;
423e884886fSBarry Smith   shell->vshift += a;
424e884886fSBarry Smith   PetscFunctionReturn(0);
425e884886fSBarry Smith }
426e884886fSBarry Smith 
427e884886fSBarry Smith #undef __FUNCT__
428e884886fSBarry Smith #define __FUNCT__ "MatScale_MFFD"
429e884886fSBarry Smith PetscErrorCode MatScale_MFFD(Mat Y,PetscScalar a)
430e884886fSBarry Smith {
431e884886fSBarry Smith   MatMFFD shell = (MatMFFD)Y->data;
432e884886fSBarry Smith   PetscFunctionBegin;
433e884886fSBarry Smith   shell->vscale *= a;
434e884886fSBarry Smith   PetscFunctionReturn(0);
435e884886fSBarry Smith }
436e884886fSBarry Smith 
437e884886fSBarry Smith EXTERN_C_BEGIN
438e884886fSBarry Smith #undef __FUNCT__
439c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetBase_MFFD"
440c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase_MFFD(Mat J,Vec U,Vec F)
441e884886fSBarry Smith {
442e884886fSBarry Smith   PetscErrorCode ierr;
443e884886fSBarry Smith   MatMFFD        ctx = (MatMFFD)J->data;
444e884886fSBarry Smith 
445e884886fSBarry Smith   PetscFunctionBegin;
446e884886fSBarry Smith   ierr = MatMFFDResetHHistory(J);CHKERRQ(ierr);
447e884886fSBarry Smith   ctx->current_u = U;
44852121784SBarry Smith   if (F) {
449cfe22f5eSBarry Smith     if (ctx->current_f_allocated) {ierr = VecDestroy(ctx->current_f);CHKERRQ(ierr);}
4503ec795f1SBarry Smith     ctx->current_f           = F;
451cfe22f5eSBarry Smith     ctx->current_f_allocated = PETSC_FALSE;
452cfe22f5eSBarry Smith   } else if (!ctx->current_f_allocated) {
45352121784SBarry Smith     ierr = VecDuplicate(ctx->current_u, &ctx->current_f);CHKERRQ(ierr);
454cfe22f5eSBarry Smith     ctx->current_f_allocated = PETSC_TRUE;
45552121784SBarry Smith   }
456e884886fSBarry Smith   if (!ctx->w) {
457e884886fSBarry Smith     ierr = VecDuplicate(ctx->current_u, &ctx->w);CHKERRQ(ierr);
458e884886fSBarry Smith   }
459e884886fSBarry Smith   J->assembled = PETSC_TRUE;
460e884886fSBarry Smith   PetscFunctionReturn(0);
461e884886fSBarry Smith }
462e884886fSBarry Smith EXTERN_C_END
463e884886fSBarry Smith typedef PetscErrorCode (*FCN3)(void*,Vec,Vec,PetscScalar*); /* force argument to next function to not be extern C*/
464e884886fSBarry Smith EXTERN_C_BEGIN
465e884886fSBarry Smith #undef __FUNCT__
466c879e56bSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh_MFFD"
467c879e56bSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh_MFFD(Mat J,FCN3 fun,void*ectx)
468e884886fSBarry Smith {
469e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
470e884886fSBarry Smith 
471e884886fSBarry Smith   PetscFunctionBegin;
472e884886fSBarry Smith   ctx->checkh    = fun;
473e884886fSBarry Smith   ctx->checkhctx = ectx;
474e884886fSBarry Smith   PetscFunctionReturn(0);
475e884886fSBarry Smith }
476e884886fSBarry Smith EXTERN_C_END
477e884886fSBarry Smith 
478e884886fSBarry Smith #undef __FUNCT__
4796aa9148fSLisandro Dalcin #define __FUNCT__ "MatMFFDSetOptionsPrefix"
4806aa9148fSLisandro Dalcin /*@C
4816aa9148fSLisandro Dalcin    MatMFFDSetOptionsPrefix - Sets the prefix used for searching for all
4826aa9148fSLisandro Dalcin    MatMFFD options in the database.
4836aa9148fSLisandro Dalcin 
4846aa9148fSLisandro Dalcin    Collective on Mat
4856aa9148fSLisandro Dalcin 
4866aa9148fSLisandro Dalcin    Input Parameter:
4876aa9148fSLisandro Dalcin +  A - the Mat context
4886aa9148fSLisandro Dalcin -  prefix - the prefix to prepend to all option names
4896aa9148fSLisandro Dalcin 
4906aa9148fSLisandro Dalcin    Notes:
4916aa9148fSLisandro Dalcin    A hyphen (-) must NOT be given at the beginning of the prefix name.
4926aa9148fSLisandro Dalcin    The first character of all runtime options is AUTOMATICALLY the hyphen.
4936aa9148fSLisandro Dalcin 
4946aa9148fSLisandro Dalcin    Level: advanced
4956aa9148fSLisandro Dalcin 
4966aa9148fSLisandro Dalcin .keywords: SNES, matrix-free, parameters
4976aa9148fSLisandro Dalcin 
4986aa9148fSLisandro Dalcin .seealso: MatMFFDSetFromOptions(), MatCreateSNESMF()
4996aa9148fSLisandro Dalcin @*/
5006aa9148fSLisandro Dalcin PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetOptionsPrefix(Mat mat,const char prefix[])
5016aa9148fSLisandro Dalcin 
5026aa9148fSLisandro Dalcin {
5036aa9148fSLisandro Dalcin   MatMFFD        mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL;
5046aa9148fSLisandro Dalcin   PetscErrorCode ierr;
5056aa9148fSLisandro Dalcin   PetscFunctionBegin;
5060700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5070700a824SBarry Smith   PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1);
5086aa9148fSLisandro Dalcin   ierr = PetscObjectSetOptionsPrefix((PetscObject)mfctx,prefix);CHKERRQ(ierr);
5096aa9148fSLisandro Dalcin   PetscFunctionReturn(0);
5106aa9148fSLisandro Dalcin }
5116aa9148fSLisandro Dalcin 
5126aa9148fSLisandro Dalcin #undef __FUNCT__
513e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFromOptions"
514e884886fSBarry Smith /*@
515e884886fSBarry Smith    MatMFFDSetFromOptions - Sets the MatMFFD options from the command line
516e884886fSBarry Smith    parameter.
517e884886fSBarry Smith 
518e884886fSBarry Smith    Collective on Mat
519e884886fSBarry Smith 
520e884886fSBarry Smith    Input Parameters:
521e884886fSBarry Smith .  mat - the matrix obtained with MatCreateMFFD() or MatCreateSNESMF()
522e884886fSBarry Smith 
523e884886fSBarry Smith    Options Database Keys:
524e884886fSBarry Smith +  -mat_mffd_type - wp or ds (see MATMFFD_WP or MATMFFD_DS)
525e884886fSBarry Smith -  -mat_mffd_err - square root of estimated relative error in function evaluation
526e884886fSBarry Smith -  -mat_mffd_period - how often h is recomputed, defaults to 1, everytime
527e884886fSBarry Smith 
528e884886fSBarry Smith    Level: advanced
529e884886fSBarry Smith 
530e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
531e884886fSBarry Smith 
5321d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatMFFDResetHHistory()
533e884886fSBarry Smith @*/
534e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFromOptions(Mat mat)
535e884886fSBarry Smith {
5366aa9148fSLisandro Dalcin   MatMFFD        mfctx = mat ? (MatMFFD)mat->data : PETSC_NULL;
537e884886fSBarry Smith   PetscErrorCode ierr;
538e884886fSBarry Smith   PetscTruth     flg;
539e884886fSBarry Smith   char           ftype[256];
540e884886fSBarry Smith 
541e884886fSBarry Smith   PetscFunctionBegin;
5420700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
5430700a824SBarry Smith   PetscValidHeaderSpecific(mfctx,MATMFFD_CLASSID,1);
5447adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)mfctx)->comm,((PetscObject)mfctx)->prefix,"Set matrix free computation parameters","MatMFFD");CHKERRQ(ierr);
545b022a5c1SBarry Smith   ierr = PetscOptionsList("-mat_mffd_type","Matrix free type","MatMFFDSetType",MatMFFDList,((PetscObject)mfctx)->type_name,ftype,256,&flg);CHKERRQ(ierr);
546e884886fSBarry Smith   if (flg) {
547e884886fSBarry Smith     ierr = MatMFFDSetType(mat,ftype);CHKERRQ(ierr);
548e884886fSBarry Smith   }
549e884886fSBarry Smith 
550e884886fSBarry Smith   ierr = PetscOptionsReal("-mat_mffd_err","set sqrt relative error in function","MatMFFDSetFunctionError",mfctx->error_rel,&mfctx->error_rel,0);CHKERRQ(ierr);
551e884886fSBarry Smith   ierr = PetscOptionsInt("-mat_mffd_period","how often h is recomputed","MatMFFDSetPeriod",mfctx->recomputeperiod,&mfctx->recomputeperiod,0);CHKERRQ(ierr);
552e884886fSBarry Smith 
55390d69ab7SBarry Smith   flg  = PETSC_FALSE;
55490d69ab7SBarry Smith   ierr = PetscOptionsTruth("-mat_mffd_check_positivity","Insure that U + h*a is nonnegative","MatMFFDSetCheckh",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
555e884886fSBarry Smith   if (flg) {
556e884886fSBarry Smith     ierr = MatMFFDSetCheckh(mat,MatMFFDCheckPositivity,0);CHKERRQ(ierr);
557e884886fSBarry Smith   }
558e884886fSBarry Smith   if (mfctx->ops->setfromoptions) {
559e884886fSBarry Smith     ierr = (*mfctx->ops->setfromoptions)(mfctx);CHKERRQ(ierr);
560e884886fSBarry Smith   }
561e884886fSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
562e884886fSBarry Smith   PetscFunctionReturn(0);
563e884886fSBarry Smith }
564e884886fSBarry Smith 
565e884886fSBarry Smith /*MC
566e884886fSBarry Smith   MATMFFD - MATMFFD = "mffd" - A matrix free matrix type.
567e884886fSBarry Smith 
568e884886fSBarry Smith   Level: advanced
569e884886fSBarry Smith 
570d2d6cebeSBarry Smith .seealso: MatCreateMFFD(), MatCreateSNESMF(), MatMFFDSetFunction()
571e884886fSBarry Smith M*/
572e884886fSBarry Smith EXTERN_C_BEGIN
573e884886fSBarry Smith #undef __FUNCT__
574e884886fSBarry Smith #define __FUNCT__ "MatCreate_MFFD"
575e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_MFFD(Mat A)
576e884886fSBarry Smith {
577e884886fSBarry Smith   MatMFFD         mfctx;
578e884886fSBarry Smith   PetscErrorCode  ierr;
579e884886fSBarry Smith 
580e884886fSBarry Smith   PetscFunctionBegin;
5813ec795f1SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
5823ec795f1SBarry Smith   ierr = MatMFFDInitializePackage(PETSC_NULL);CHKERRQ(ierr);
5833ec795f1SBarry Smith #endif
584e884886fSBarry Smith 
5850700a824SBarry Smith   ierr = PetscHeaderCreate(mfctx,_p_MatMFFD,struct _MFOps,MATMFFD_CLASSID,0,"MatMFFD",((PetscObject)A)->comm,MatDestroy_MFFD,MatView_MFFD);CHKERRQ(ierr);
586e884886fSBarry Smith   mfctx->sp              = 0;
587e884886fSBarry Smith   mfctx->error_rel       = PETSC_SQRT_MACHINE_EPSILON;
588e884886fSBarry Smith   mfctx->recomputeperiod = 1;
589e884886fSBarry Smith   mfctx->count           = 0;
590e884886fSBarry Smith   mfctx->currenth        = 0.0;
591e884886fSBarry Smith   mfctx->historyh        = PETSC_NULL;
592e884886fSBarry Smith   mfctx->ncurrenth       = 0;
593e884886fSBarry Smith   mfctx->maxcurrenth     = 0;
5947adad957SLisandro Dalcin   ((PetscObject)mfctx)->type_name       = 0;
595e884886fSBarry Smith 
596e884886fSBarry Smith   mfctx->vshift          = 0.0;
597e884886fSBarry Smith   mfctx->vscale          = 1.0;
598e884886fSBarry Smith 
599e884886fSBarry Smith   /*
600e884886fSBarry Smith      Create the empty data structure to contain compute-h routines.
601e884886fSBarry Smith      These will be filled in below from the command line options or
602e884886fSBarry Smith      a later call with MatMFFDSetType() or if that is not called
603e884886fSBarry Smith      then it will default in the first use of MatMult_MFFD()
604e884886fSBarry Smith   */
605e884886fSBarry Smith   mfctx->ops->compute        = 0;
606e884886fSBarry Smith   mfctx->ops->destroy        = 0;
607e884886fSBarry Smith   mfctx->ops->view           = 0;
608e884886fSBarry Smith   mfctx->ops->setfromoptions = 0;
609e884886fSBarry Smith   mfctx->hctx                = 0;
610e884886fSBarry Smith 
611e884886fSBarry Smith   mfctx->func                = 0;
612e884886fSBarry Smith   mfctx->funcctx             = 0;
613e884886fSBarry Smith   mfctx->w                   = PETSC_NULL;
614e884886fSBarry Smith 
615e884886fSBarry Smith   A->data                = mfctx;
616e884886fSBarry Smith 
617e884886fSBarry Smith   A->ops->mult           = MatMult_MFFD;
618e884886fSBarry Smith   A->ops->destroy        = MatDestroy_MFFD;
619e884886fSBarry Smith   A->ops->view           = MatView_MFFD;
620e884886fSBarry Smith   A->ops->assemblyend    = MatAssemblyEnd_MFFD;
621e884886fSBarry Smith   A->ops->getdiagonal    = MatGetDiagonal_MFFD;
622e884886fSBarry Smith   A->ops->scale          = MatScale_MFFD;
623e884886fSBarry Smith   A->ops->shift          = MatShift_MFFD;
624e884886fSBarry Smith   A->ops->setfromoptions = MatMFFDSetFromOptions;
625e884886fSBarry Smith   A->assembled = PETSC_TRUE;
626e884886fSBarry Smith 
62726283091SBarry Smith   ierr = PetscLayoutSetBlockSize(A->rmap,1);CHKERRQ(ierr);
62826283091SBarry Smith   ierr = PetscLayoutSetBlockSize(A->cmap,1);CHKERRQ(ierr);
62926283091SBarry Smith   ierr = PetscLayoutSetUp(A->rmap);CHKERRQ(ierr);
63026283091SBarry Smith   ierr = PetscLayoutSetUp(A->cmap);CHKERRQ(ierr);
631ee1cef2cSJed Brown 
632c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetBase_C","MatMFFDSetBase_MFFD",MatMFFDSetBase_MFFD);CHKERRQ(ierr);
633c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioniBase_C","MatMFFDSetFunctioniBase_MFFD",MatMFFDSetFunctioniBase_MFFD);CHKERRQ(ierr);
634c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetFunctioni_C","MatMFFDSetFunctioni_MFFD",MatMFFDSetFunctioni_MFFD);CHKERRQ(ierr);
635c879e56bSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatMFFDSetCheckh_C","MatMFFDSetCheckh_MFFD",MatMFFDSetCheckh_MFFD);CHKERRQ(ierr);
636e884886fSBarry Smith   mfctx->mat = A;
637e884886fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)A,MATMFFD);CHKERRQ(ierr);
638e884886fSBarry Smith   PetscFunctionReturn(0);
639e884886fSBarry Smith }
640e884886fSBarry Smith EXTERN_C_END
641e884886fSBarry Smith 
642e884886fSBarry Smith #undef __FUNCT__
643e884886fSBarry Smith #define __FUNCT__ "MatCreateMFFD"
644e884886fSBarry Smith /*@
645e884886fSBarry Smith    MatCreateMFFD - Creates a matrix-free matrix. See also MatCreateSNESMF()
646e884886fSBarry Smith 
647e884886fSBarry Smith    Collective on Vec
648e884886fSBarry Smith 
649e884886fSBarry Smith    Input Parameters:
650fef1beadSBarry Smith +  comm - MPI communicator
651fef1beadSBarry Smith .  m - number of local rows (or PETSC_DECIDE to have calculated if M is given)
652fef1beadSBarry Smith            This value should be the same as the local size used in creating the
653fef1beadSBarry Smith            y vector for the matrix-vector product y = Ax.
654fef1beadSBarry Smith .  n - This value should be the same as the local size used in creating the
655fef1beadSBarry Smith        x vector for the matrix-vector product y = Ax. (or PETSC_DECIDE to have
656fef1beadSBarry Smith        calculated if N is given) For square matrices n is almost always m.
657fef1beadSBarry Smith .  M - number of global rows (or PETSC_DETERMINE to have calculated if m is given)
658fef1beadSBarry Smith -  N - number of global columns (or PETSC_DETERMINE to have calculated if n is given)
659fef1beadSBarry Smith 
660e884886fSBarry Smith 
661e884886fSBarry Smith    Output Parameter:
662e884886fSBarry Smith .  J - the matrix-free matrix
663e884886fSBarry Smith 
664e884886fSBarry Smith    Level: advanced
665e884886fSBarry Smith 
666e884886fSBarry Smith    Notes:
667e884886fSBarry Smith    The matrix-free matrix context merely contains the function pointers
668e884886fSBarry Smith    and work space for performing finite difference approximations of
669e884886fSBarry Smith    Jacobian-vector products, F'(u)*a,
670e884886fSBarry Smith 
671e884886fSBarry Smith    The default code uses the following approach to compute h
672e884886fSBarry Smith 
673e884886fSBarry Smith .vb
674e884886fSBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
675e884886fSBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
676e884886fSBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   otherwise
677e884886fSBarry Smith  where
678e884886fSBarry Smith      error_rel = square root of relative error in function evaluation
679e884886fSBarry Smith      umin = minimum iterate parameter
680e884886fSBarry Smith .ve
681e884886fSBarry Smith 
682e884886fSBarry Smith    The user can set the error_rel via MatMFFDSetFunctionError() and
6831d0fab5eSBarry Smith    umin via MatMFFDDSSetUmin(); see the nonlinear solvers chapter
684e884886fSBarry Smith    of the users manual for details.
685e884886fSBarry Smith 
686e884886fSBarry Smith    The user should call MatDestroy() when finished with the matrix-free
687e884886fSBarry Smith    matrix context.
688e884886fSBarry Smith 
689e884886fSBarry Smith    Options Database Keys:
690e884886fSBarry Smith +  -mat_mffd_err <error_rel> - Sets error_rel
691e884886fSBarry Smith .  -mat_mffd_unim <umin> - Sets umin (for default PETSc routine that computes h only)
692e884886fSBarry Smith -  -mat_mffd_check_positivity
693e884886fSBarry Smith 
694e884886fSBarry Smith .keywords: default, matrix-free, create, matrix
695e884886fSBarry Smith 
6961d0fab5eSBarry Smith .seealso: MatDestroy(), MatMFFDSetFunctionError(), MatMFFDDSSetUmin(), MatMFFDSetFunction()
697e884886fSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(), MatCreateSNESMF(),
6981d0fab5eSBarry Smith           MatMFFDGetH(), MatMFFDRegisterDynamic), MatMFFDComputeJacobian()
699e884886fSBarry Smith 
700e884886fSBarry Smith @*/
701fef1beadSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateMFFD(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt M,PetscInt N,Mat *J)
702e884886fSBarry Smith {
703e884886fSBarry Smith   PetscErrorCode ierr;
704e884886fSBarry Smith 
705e884886fSBarry Smith   PetscFunctionBegin;
706e884886fSBarry Smith   ierr = MatCreate(comm,J);CHKERRQ(ierr);
707fef1beadSBarry Smith   ierr = MatSetSizes(*J,m,n,M,N);CHKERRQ(ierr);
708e884886fSBarry Smith   ierr = MatSetType(*J,MATMFFD);CHKERRQ(ierr);
709e884886fSBarry Smith   PetscFunctionReturn(0);
710e884886fSBarry Smith }
711e884886fSBarry Smith 
712e884886fSBarry Smith 
713e884886fSBarry Smith #undef __FUNCT__
714e884886fSBarry Smith #define __FUNCT__ "MatMFFDGetH"
715e884886fSBarry Smith /*@
716e884886fSBarry Smith    MatMFFDGetH - Gets the last value that was used as the differencing
717e884886fSBarry Smith    parameter.
718e884886fSBarry Smith 
719e884886fSBarry Smith    Not Collective
720e884886fSBarry Smith 
721e884886fSBarry Smith    Input Parameters:
722e884886fSBarry Smith .  mat - the matrix obtained with MatCreateSNESMF()
723e884886fSBarry Smith 
724e884886fSBarry Smith    Output Paramter:
725e884886fSBarry Smith .  h - the differencing step size
726e884886fSBarry Smith 
727e884886fSBarry Smith    Level: advanced
728e884886fSBarry Smith 
729e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
730e884886fSBarry Smith 
7311d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDSetHHistory(), MatCreateMFFD(), MATMFFD, MatMFFDResetHHistory()
732e884886fSBarry Smith @*/
733e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDGetH(Mat mat,PetscScalar *h)
734e884886fSBarry Smith {
735e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
736e884886fSBarry Smith 
737e884886fSBarry Smith   PetscFunctionBegin;
738e884886fSBarry Smith   *h = ctx->currenth;
739e884886fSBarry Smith   PetscFunctionReturn(0);
740e884886fSBarry Smith }
741e884886fSBarry Smith 
742e884886fSBarry Smith 
743e884886fSBarry Smith #undef __FUNCT__
744e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunction"
745e884886fSBarry Smith /*@C
746e884886fSBarry Smith    MatMFFDSetFunction - Sets the function used in applying the matrix free.
747e884886fSBarry Smith 
748e884886fSBarry Smith    Collective on Mat
749e884886fSBarry Smith 
750e884886fSBarry Smith    Input Parameters:
751e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
752e884886fSBarry Smith .  func - the function to use
753e884886fSBarry Smith -  funcctx - optional function context passed to function
754e884886fSBarry Smith 
755e884886fSBarry Smith    Level: advanced
756e884886fSBarry Smith 
757e884886fSBarry Smith    Notes:
758e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
759e884886fSBarry Smith     matrix inside your compute Jacobian routine
760e884886fSBarry Smith 
7613ec795f1SBarry Smith     If this is not set then it will use the function set with SNESSetFunction() if MatCreateSNESMF() was used.
762e884886fSBarry Smith 
763e884886fSBarry Smith .keywords: SNES, matrix-free, function
764e884886fSBarry Smith 
7651d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD,
7661d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction()
767e884886fSBarry Smith @*/
7683ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunction(Mat mat,PetscErrorCode (*func)(void*,Vec,Vec),void *funcctx)
769e884886fSBarry Smith {
770e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
771e884886fSBarry Smith 
772e884886fSBarry Smith   PetscFunctionBegin;
773e884886fSBarry Smith   ctx->func    = func;
774e884886fSBarry Smith   ctx->funcctx = funcctx;
775e884886fSBarry Smith   PetscFunctionReturn(0);
776e884886fSBarry Smith }
777e884886fSBarry Smith 
778e884886fSBarry Smith #undef __FUNCT__
779e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioni"
780e884886fSBarry Smith /*@C
781e884886fSBarry Smith    MatMFFDSetFunctioni - Sets the function for a single component
782e884886fSBarry Smith 
783e884886fSBarry Smith    Collective on Mat
784e884886fSBarry Smith 
785e884886fSBarry Smith    Input Parameters:
786e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
787e884886fSBarry Smith -  funci - the function to use
788e884886fSBarry Smith 
789e884886fSBarry Smith    Level: advanced
790e884886fSBarry Smith 
791e884886fSBarry Smith    Notes:
792e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
793e884886fSBarry Smith     matrix inside your compute Jacobian routine
794e884886fSBarry Smith 
795e884886fSBarry Smith 
796e884886fSBarry Smith .keywords: SNES, matrix-free, function
797e884886fSBarry Smith 
7981d0fab5eSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction()
7991d0fab5eSBarry Smith 
800e884886fSBarry Smith @*/
801e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioni(Mat mat,PetscErrorCode (*funci)(void*,PetscInt,Vec,PetscScalar*))
802e884886fSBarry Smith {
803e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,PetscInt,Vec,PetscScalar*));
804e884886fSBarry Smith 
805e884886fSBarry Smith   PetscFunctionBegin;
8060700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
807e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioni_C",(void (**)(void))&f);CHKERRQ(ierr);
808e884886fSBarry Smith   if (f) {
809e884886fSBarry Smith     ierr = (*f)(mat,funci);CHKERRQ(ierr);
810e884886fSBarry Smith   }
811e884886fSBarry Smith   PetscFunctionReturn(0);
812e884886fSBarry Smith }
813e884886fSBarry Smith 
814e884886fSBarry Smith 
815e884886fSBarry Smith #undef __FUNCT__
816e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctioniBase"
817e884886fSBarry Smith /*@C
818e884886fSBarry Smith    MatMFFDSetFunctioniBase - Sets the base vector for a single component function evaluation
819e884886fSBarry Smith 
820e884886fSBarry Smith    Collective on Mat
821e884886fSBarry Smith 
822e884886fSBarry Smith    Input Parameters:
823e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
824e884886fSBarry Smith -  func - the function to use
825e884886fSBarry Smith 
826e884886fSBarry Smith    Level: advanced
827e884886fSBarry Smith 
828e884886fSBarry Smith    Notes:
829e884886fSBarry Smith     If you use this you MUST call MatAssemblyBegin()/MatAssemblyEnd() on the matrix free
830e884886fSBarry Smith     matrix inside your compute Jacobian routine
831e884886fSBarry Smith 
832e884886fSBarry Smith 
833e884886fSBarry Smith .keywords: SNES, matrix-free, function
834e884886fSBarry Smith 
835e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD
8361d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory(), SNESetFunction()
837e884886fSBarry Smith @*/
838e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctioniBase(Mat mat,PetscErrorCode (*func)(void*,Vec))
839e884886fSBarry Smith {
840e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec));
841e884886fSBarry Smith 
842e884886fSBarry Smith   PetscFunctionBegin;
8430700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,1);
844e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatMFFDSetFunctioniBase_C",(void (**)(void))&f);CHKERRQ(ierr);
845e884886fSBarry Smith   if (f) {
846e884886fSBarry Smith     ierr = (*f)(mat,func);CHKERRQ(ierr);
847e884886fSBarry Smith   }
848e884886fSBarry Smith   PetscFunctionReturn(0);
849e884886fSBarry Smith }
850e884886fSBarry Smith 
851e884886fSBarry Smith 
852e884886fSBarry Smith #undef __FUNCT__
853e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetPeriod"
854e884886fSBarry Smith /*@
855e884886fSBarry Smith    MatMFFDSetPeriod - Sets how often h is recomputed, by default it is everytime
856e884886fSBarry Smith 
857e884886fSBarry Smith    Collective on Mat
858e884886fSBarry Smith 
859e884886fSBarry Smith    Input Parameters:
860e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateSNESMF()
861e884886fSBarry Smith -  period - 1 for everytime, 2 for every second etc
862e884886fSBarry Smith 
863e884886fSBarry Smith    Options Database Keys:
864e884886fSBarry Smith +  -mat_mffd_period <period>
865e884886fSBarry Smith 
866e884886fSBarry Smith    Level: advanced
867e884886fSBarry Smith 
868e884886fSBarry Smith 
869e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
870e884886fSBarry Smith 
871e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(),
8721d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory()
873e884886fSBarry Smith @*/
874e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetPeriod(Mat mat,PetscInt period)
875e884886fSBarry Smith {
876e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
877e884886fSBarry Smith 
878e884886fSBarry Smith   PetscFunctionBegin;
879e884886fSBarry Smith   ctx->recomputeperiod = period;
880e884886fSBarry Smith   PetscFunctionReturn(0);
881e884886fSBarry Smith }
882e884886fSBarry Smith 
883e884886fSBarry Smith #undef __FUNCT__
884e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetFunctionError"
885e884886fSBarry Smith /*@
886e884886fSBarry Smith    MatMFFDSetFunctionError - Sets the error_rel for the approximation of
887e884886fSBarry Smith    matrix-vector products using finite differences.
888e884886fSBarry Smith 
889e884886fSBarry Smith    Collective on Mat
890e884886fSBarry Smith 
891e884886fSBarry Smith    Input Parameters:
892e884886fSBarry Smith +  mat - the matrix free matrix created via MatCreateMFFD() or MatCreateSNESMF()
893e884886fSBarry Smith -  error_rel - relative error (should be set to the square root of
894e884886fSBarry Smith                the relative error in the function evaluations)
895e884886fSBarry Smith 
896e884886fSBarry Smith    Options Database Keys:
897e884886fSBarry Smith +  -mat_mffd_err <error_rel> - Sets error_rel
898e884886fSBarry Smith 
899e884886fSBarry Smith    Level: advanced
900e884886fSBarry Smith 
901e884886fSBarry Smith    Notes:
902e884886fSBarry Smith    The default matrix-free matrix-vector product routine computes
903e884886fSBarry Smith .vb
904e884886fSBarry Smith      F'(u)*a = [F(u+h*a) - F(u)]/h where
905e884886fSBarry Smith      h = error_rel*u'a/||a||^2                        if  |u'a| > umin*||a||_{1}
906e884886fSBarry Smith        = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2   else
907e884886fSBarry Smith .ve
908e884886fSBarry Smith 
909e884886fSBarry Smith .keywords: SNES, matrix-free, parameters
910e884886fSBarry Smith 
911e884886fSBarry Smith .seealso: MatCreateSNESMF(),MatMFFDGetH(), MatCreateMFFD(), MATMFFD
9121d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory()
913e884886fSBarry Smith @*/
914e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetFunctionError(Mat mat,PetscReal error)
915e884886fSBarry Smith {
916e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)mat->data;
917e884886fSBarry Smith 
918e884886fSBarry Smith   PetscFunctionBegin;
919e884886fSBarry Smith   if (error != PETSC_DEFAULT) ctx->error_rel = error;
920e884886fSBarry Smith   PetscFunctionReturn(0);
921e884886fSBarry Smith }
922e884886fSBarry Smith 
923e884886fSBarry Smith #undef __FUNCT__
924e884886fSBarry Smith #define __FUNCT__ "MatMFFDAddNullSpace"
925e884886fSBarry Smith /*@
926e884886fSBarry Smith    MatMFFDAddNullSpace - Provides a null space that an operator is
927e884886fSBarry Smith    supposed to have.  Since roundoff will create a small component in
928e884886fSBarry Smith    the null space, if you know the null space you may have it
929e884886fSBarry Smith    automatically removed.
930e884886fSBarry Smith 
931e884886fSBarry Smith    Collective on Mat
932e884886fSBarry Smith 
933e884886fSBarry Smith    Input Parameters:
934e884886fSBarry Smith +  J - the matrix-free matrix context
935e884886fSBarry Smith -  nullsp - object created with MatNullSpaceCreate()
936e884886fSBarry Smith 
937e884886fSBarry Smith    Level: advanced
938e884886fSBarry Smith 
939e884886fSBarry Smith .keywords: SNES, matrix-free, null space
940e884886fSBarry Smith 
941e884886fSBarry Smith .seealso: MatNullSpaceCreate(), MatMFFDGetH(), MatCreateSNESMF(), MatCreateMFFD(), MATMFFD
9421d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDResetHHistory()
943e884886fSBarry Smith @*/
944e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDAddNullSpace(Mat J,MatNullSpace nullsp)
945e884886fSBarry Smith {
946e884886fSBarry Smith   PetscErrorCode ierr;
947e884886fSBarry Smith   MatMFFD      ctx = (MatMFFD)J->data;
948e884886fSBarry Smith 
949e884886fSBarry Smith   PetscFunctionBegin;
950e884886fSBarry Smith   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
951e884886fSBarry Smith   if (ctx->sp) { ierr = MatNullSpaceDestroy(ctx->sp);CHKERRQ(ierr); }
952e884886fSBarry Smith   ctx->sp = nullsp;
953e884886fSBarry Smith   PetscFunctionReturn(0);
954e884886fSBarry Smith }
955e884886fSBarry Smith 
956e884886fSBarry Smith #undef __FUNCT__
957e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetHHistory"
958e884886fSBarry Smith /*@
959e884886fSBarry Smith    MatMFFDSetHHistory - Sets an array to collect a history of the
960e884886fSBarry Smith    differencing values (h) computed for the matrix-free product.
961e884886fSBarry Smith 
962e884886fSBarry Smith    Collective on Mat
963e884886fSBarry Smith 
964e884886fSBarry Smith    Input Parameters:
965e884886fSBarry Smith +  J - the matrix-free matrix context
966e884886fSBarry Smith .  histroy - space to hold the history
967e884886fSBarry Smith -  nhistory - number of entries in history, if more entries are generated than
968e884886fSBarry Smith               nhistory, then the later ones are discarded
969e884886fSBarry Smith 
970e884886fSBarry Smith    Level: advanced
971e884886fSBarry Smith 
972e884886fSBarry Smith    Notes:
973e884886fSBarry Smith    Use MatMFFDResetHHistory() to reset the history counter and collect
974e884886fSBarry Smith    a new batch of differencing parameters, h.
975e884886fSBarry Smith 
976e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history
977e884886fSBarry Smith 
978e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(),
9791d0fab5eSBarry Smith           MatMFFDResetHHistory(), MatMFFDSetFunctionError()
980e884886fSBarry Smith 
981e884886fSBarry Smith @*/
982e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetHHistory(Mat J,PetscScalar history[],PetscInt nhistory)
983e884886fSBarry Smith {
984e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
985e884886fSBarry Smith 
986e884886fSBarry Smith   PetscFunctionBegin;
987e884886fSBarry Smith   ctx->historyh    = history;
988e884886fSBarry Smith   ctx->maxcurrenth = nhistory;
98975567043SBarry Smith   ctx->currenth    = 0.;
990e884886fSBarry Smith   PetscFunctionReturn(0);
991e884886fSBarry Smith }
992e884886fSBarry Smith 
993e884886fSBarry Smith #undef __FUNCT__
994e884886fSBarry Smith #define __FUNCT__ "MatMFFDResetHHistory"
995e884886fSBarry Smith /*@
996e884886fSBarry Smith    MatMFFDResetHHistory - Resets the counter to zero to begin
997e884886fSBarry Smith    collecting a new set of differencing histories.
998e884886fSBarry Smith 
999e884886fSBarry Smith    Collective on Mat
1000e884886fSBarry Smith 
1001e884886fSBarry Smith    Input Parameters:
1002e884886fSBarry Smith .  J - the matrix-free matrix context
1003e884886fSBarry Smith 
1004e884886fSBarry Smith    Level: advanced
1005e884886fSBarry Smith 
1006e884886fSBarry Smith    Notes:
1007e884886fSBarry Smith    Use MatMFFDSetHHistory() to create the original history counter.
1008e884886fSBarry Smith 
1009e884886fSBarry Smith .keywords: SNES, matrix-free, h history, differencing history
1010e884886fSBarry Smith 
1011e884886fSBarry Smith .seealso: MatMFFDGetH(), MatCreateSNESMF(),
10121d0fab5eSBarry Smith           MatMFFDSetHHistory(), MatMFFDSetFunctionError()
1013e884886fSBarry Smith 
1014e884886fSBarry Smith @*/
1015e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDResetHHistory(Mat J)
1016e884886fSBarry Smith {
1017e884886fSBarry Smith   MatMFFD ctx = (MatMFFD)J->data;
1018e884886fSBarry Smith 
1019e884886fSBarry Smith   PetscFunctionBegin;
1020e884886fSBarry Smith   ctx->ncurrenth    = 0;
1021e884886fSBarry Smith   PetscFunctionReturn(0);
1022e884886fSBarry Smith }
1023e884886fSBarry Smith 
1024e884886fSBarry Smith 
1025e884886fSBarry Smith #undef __FUNCT__
1026e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetBase"
1027e884886fSBarry Smith /*@
1028e884886fSBarry Smith     MatMFFDSetBase - Sets the vector U at which matrix vector products of the
1029e884886fSBarry Smith         Jacobian are computed
1030e884886fSBarry Smith 
1031e884886fSBarry Smith     Collective on Mat
1032e884886fSBarry Smith 
1033e884886fSBarry Smith     Input Parameters:
1034e884886fSBarry Smith +   J - the MatMFFD matrix
10353ec795f1SBarry Smith .   U - the vector
1036bcddec3dSBarry Smith -   F - (optional) vector that contains F(u) if it has been already computed
1037e884886fSBarry Smith 
1038e884886fSBarry Smith     Notes: This is rarely used directly
1039e884886fSBarry Smith 
10408af5ae88SBarry Smith     If F is provided then it is not recomputed. Otherwise the function is evaluated at the base
10418af5ae88SBarry Smith     point during the first MatMult() after each call to MatMFFDSetBase().
1042dff2f722SBarry Smith 
1043e884886fSBarry Smith     Level: advanced
1044e884886fSBarry Smith 
1045e884886fSBarry Smith @*/
10463ec795f1SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetBase(Mat J,Vec U,Vec F)
1047e884886fSBarry Smith {
10483ec795f1SBarry Smith   PetscErrorCode ierr,(*f)(Mat,Vec,Vec);
1049e884886fSBarry Smith 
1050e884886fSBarry Smith   PetscFunctionBegin;
10510700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
10520700a824SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
10530700a824SBarry Smith   if (F) PetscValidHeaderSpecific(F,VEC_CLASSID,3);
1054e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetBase_C",(void (**)(void))&f);CHKERRQ(ierr);
1055e884886fSBarry Smith   if (f) {
10563ec795f1SBarry Smith     ierr = (*f)(J,U,F);CHKERRQ(ierr);
1057e884886fSBarry Smith   }
1058e884886fSBarry Smith   PetscFunctionReturn(0);
1059e884886fSBarry Smith }
1060e884886fSBarry Smith 
1061e884886fSBarry Smith #undef __FUNCT__
1062e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckh"
1063e884886fSBarry Smith /*@C
1064e884886fSBarry Smith     MatMFFDSetCheckh - Sets a function that checks the computed h and adjusts
1065e884886fSBarry Smith         it to satisfy some criteria
1066e884886fSBarry Smith 
1067e884886fSBarry Smith     Collective on Mat
1068e884886fSBarry Smith 
1069e884886fSBarry Smith     Input Parameters:
1070e884886fSBarry Smith +   J - the MatMFFD matrix
1071e884886fSBarry Smith .   fun - the function that checks h
1072e884886fSBarry Smith -   ctx - any context needed by the function
1073e884886fSBarry Smith 
1074e884886fSBarry Smith     Options Database Keys:
1075e884886fSBarry Smith .   -mat_mffd_check_positivity
1076e884886fSBarry Smith 
1077e884886fSBarry Smith     Level: advanced
1078e884886fSBarry Smith 
1079e884886fSBarry Smith     Notes: For example, MatMFFDSetCheckPositivity() insures that all entries
1080e884886fSBarry Smith        of U + h*a are non-negative
1081e884886fSBarry Smith 
1082e884886fSBarry Smith .seealso:  MatMFFDSetCheckPositivity()
1083e884886fSBarry Smith @*/
1084e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDSetCheckh(Mat J,PetscErrorCode (*fun)(void*,Vec,Vec,PetscScalar*),void* ctx)
1085e884886fSBarry Smith {
1086e884886fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscErrorCode (*)(void*,Vec,Vec,PetscScalar*),void*);
1087e884886fSBarry Smith 
1088e884886fSBarry Smith   PetscFunctionBegin;
10890700a824SBarry Smith   PetscValidHeaderSpecific(J,MAT_CLASSID,1);
1090e884886fSBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)J,"MatMFFDSetCheckh_C",(void (**)(void))&f);CHKERRQ(ierr);
1091e884886fSBarry Smith   if (f) {
1092e884886fSBarry Smith     ierr = (*f)(J,fun,ctx);CHKERRQ(ierr);
1093e884886fSBarry Smith   }
1094e884886fSBarry Smith   PetscFunctionReturn(0);
1095e884886fSBarry Smith }
1096e884886fSBarry Smith 
1097e884886fSBarry Smith #undef __FUNCT__
1098e884886fSBarry Smith #define __FUNCT__ "MatMFFDSetCheckPositivity"
1099e884886fSBarry Smith /*@
1100e884886fSBarry Smith     MatMFFDCheckPositivity - Checks that all entries in U + h*a are positive or
1101e884886fSBarry Smith         zero, decreases h until this is satisfied.
1102e884886fSBarry Smith 
1103e884886fSBarry Smith     Collective on Vec
1104e884886fSBarry Smith 
1105e884886fSBarry Smith     Input Parameters:
1106e884886fSBarry Smith +   U - base vector that is added to
1107e884886fSBarry Smith .   a - vector that is added
1108e884886fSBarry Smith .   h - scaling factor on a
1109e884886fSBarry Smith -   dummy - context variable (unused)
1110e884886fSBarry Smith 
1111e884886fSBarry Smith     Options Database Keys:
1112e884886fSBarry Smith .   -mat_mffd_check_positivity
1113e884886fSBarry Smith 
1114e884886fSBarry Smith     Level: advanced
1115e884886fSBarry Smith 
1116e884886fSBarry Smith     Notes: This is rarely used directly, rather it is passed as an argument to
1117e884886fSBarry Smith            MatMFFDSetCheckh()
1118e884886fSBarry Smith 
1119e884886fSBarry Smith .seealso:  MatMFFDSetCheckh()
1120e884886fSBarry Smith @*/
1121e884886fSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatMFFDCheckPositivity(void* dummy,Vec U,Vec a,PetscScalar *h)
1122e884886fSBarry Smith {
1123e884886fSBarry Smith   PetscReal      val, minval;
1124e884886fSBarry Smith   PetscScalar    *u_vec, *a_vec;
1125e884886fSBarry Smith   PetscErrorCode ierr;
1126e884886fSBarry Smith   PetscInt       i,n;
1127e884886fSBarry Smith   MPI_Comm       comm;
1128e884886fSBarry Smith 
1129e884886fSBarry Smith   PetscFunctionBegin;
1130e884886fSBarry Smith   ierr = PetscObjectGetComm((PetscObject)U,&comm);CHKERRQ(ierr);
1131e884886fSBarry Smith   ierr = VecGetArray(U,&u_vec);CHKERRQ(ierr);
1132e884886fSBarry Smith   ierr = VecGetArray(a,&a_vec);CHKERRQ(ierr);
1133e884886fSBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
1134e884886fSBarry Smith   minval = PetscAbsScalar(*h*1.01);
1135e884886fSBarry Smith   for(i=0;i<n;i++) {
1136e884886fSBarry Smith     if (PetscRealPart(u_vec[i] + *h*a_vec[i]) <= 0.0) {
1137e884886fSBarry Smith       val = PetscAbsScalar(u_vec[i]/a_vec[i]);
1138e884886fSBarry Smith       if (val < minval) minval = val;
1139e884886fSBarry Smith     }
1140e884886fSBarry Smith   }
1141e884886fSBarry Smith   ierr = VecRestoreArray(U,&u_vec);CHKERRQ(ierr);
1142e884886fSBarry Smith   ierr = VecRestoreArray(a,&a_vec);CHKERRQ(ierr);
1143e884886fSBarry Smith   ierr = PetscGlobalMin(&minval,&val,comm);CHKERRQ(ierr);
1144e884886fSBarry Smith   if (val <= PetscAbsScalar(*h)) {
1145e884886fSBarry Smith     ierr = PetscInfo2(U,"Scaling back h from %G to %G\n",PetscRealPart(*h),.99*val);CHKERRQ(ierr);
1146e884886fSBarry Smith     if (PetscRealPart(*h) > 0.0) *h =  0.99*val;
1147e884886fSBarry Smith     else                         *h = -0.99*val;
1148e884886fSBarry Smith   }
1149e884886fSBarry Smith   PetscFunctionReturn(0);
1150e884886fSBarry Smith }
1151e884886fSBarry Smith 
1152e884886fSBarry Smith 
1153e884886fSBarry Smith 
1154e884886fSBarry Smith 
1155e884886fSBarry Smith 
1156e884886fSBarry Smith 
1157e884886fSBarry Smith 
1158