Actual source code: mffd.c
1: #include <../src/mat/impls/shell/shell.h>
2: #include <../src/mat/impls/mffd/mffdimpl.h>
4: PetscFunctionList MatMFFDList = NULL;
5: PetscBool MatMFFDRegisterAllCalled = PETSC_FALSE;
7: PetscClassId MATMFFD_CLASSID;
8: PetscLogEvent MATMFFD_Mult;
10: static PetscBool MatMFFDPackageInitialized = PETSC_FALSE;
12: /*@
13: MatMFFDFinalizePackage - This function destroys everything in the MATMFFD` package. It is
14: called from `PetscFinalize()`.
16: Level: developer
18: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `PetscFinalize()`, `MatCreateMFFD()`, `MatCreateSNESMF()`
19: @*/
20: PetscErrorCode MatMFFDFinalizePackage(void)
21: {
22: PetscFunctionBegin;
23: PetscCall(PetscFunctionListDestroy(&MatMFFDList));
24: MatMFFDPackageInitialized = PETSC_FALSE;
25: MatMFFDRegisterAllCalled = PETSC_FALSE;
26: PetscFunctionReturn(PETSC_SUCCESS);
27: }
29: /*@
30: MatMFFDInitializePackage - This function initializes everything in the MATMFFD` package. It is called
31: from `MatInitializePackage()`.
33: Level: developer
35: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `PetscInitialize()`
36: @*/
37: PetscErrorCode MatMFFDInitializePackage(void)
38: {
39: char logList[256];
40: PetscBool opt, pkg;
42: PetscFunctionBegin;
43: if (MatMFFDPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
44: MatMFFDPackageInitialized = PETSC_TRUE;
45: /* Register Classes */
46: PetscCall(PetscClassIdRegister("MatMFFD", &MATMFFD_CLASSID));
47: /* Register Constructors */
48: PetscCall(MatMFFDRegisterAll());
49: /* Register Events */
50: PetscCall(PetscLogEventRegister("MatMult MF", MATMFFD_CLASSID, &MATMFFD_Mult));
51: /* Process Info */
52: {
53: PetscClassId classids[1];
55: classids[0] = MATMFFD_CLASSID;
56: PetscCall(PetscInfoProcessClass("matmffd", 1, classids));
57: }
58: /* Process summary exclusions */
59: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_exclude", logList, sizeof(logList), &opt));
60: if (opt) {
61: PetscCall(PetscStrInList("matmffd", logList, ',', &pkg));
62: if (pkg) PetscCall(PetscLogEventExcludeClass(MATMFFD_CLASSID));
63: }
64: /* Register package finalizer */
65: PetscCall(PetscRegisterFinalize(MatMFFDFinalizePackage));
66: PetscFunctionReturn(PETSC_SUCCESS);
67: }
69: static PetscErrorCode MatMFFDSetType_MFFD(Mat mat, MatMFFDType ftype)
70: {
71: MatMFFD ctx;
72: PetscBool match;
73: PetscErrorCode (*r)(MatMFFD);
75: PetscFunctionBegin;
77: PetscAssertPointer(ftype, 2);
78: PetscCall(MatShellGetContext(mat, &ctx));
80: /* already set, so just return */
81: PetscCall(PetscObjectTypeCompare((PetscObject)ctx, ftype, &match));
82: if (match) PetscFunctionReturn(PETSC_SUCCESS);
84: /* destroy the old one if it exists */
85: PetscTryTypeMethod(ctx, destroy);
87: PetscCall(PetscFunctionListFind(MatMFFDList, ftype, &r));
88: PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown MatMFFD type %s given", ftype);
89: PetscCall((*r)(ctx));
90: PetscCall(PetscObjectChangeTypeName((PetscObject)ctx, ftype));
91: PetscFunctionReturn(PETSC_SUCCESS);
92: }
94: /*@
95: MatMFFDSetType - Sets the method that is used to compute the
96: differencing parameter for finite difference matrix-free formulations.
98: Input Parameters:
99: + mat - the "matrix-free" matrix created via `MatCreateSNESMF()`, or `MatCreateMFFD()`
100: or `MatSetType`(mat,`MATMFFD`);
101: - ftype - the type requested, either `MATMFFD_WP` or `MATMFFD_DS`
103: Level: advanced
105: Note:
106: For example, such routines can compute `h` for use in
107: Jacobian-vector products of the form
108: .vb
110: F(x+ha) - F(x)
111: F'(u)a ~= ----------------
112: h
113: .ve
115: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MATMFFD_WP`, `MATMFFD_DS`, `MatCreateSNESMF()`, `MatMFFDRegister()`, `MatMFFDSetFunction()`, `MatCreateMFFD()`
116: @*/
117: PetscErrorCode MatMFFDSetType(Mat mat, MatMFFDType ftype)
118: {
119: PetscFunctionBegin;
121: PetscAssertPointer(ftype, 2);
122: PetscTryMethod(mat, "MatMFFDSetType_C", (Mat, MatMFFDType), (mat, ftype));
123: PetscFunctionReturn(PETSC_SUCCESS);
124: }
126: static PetscErrorCode MatGetDiagonal_MFFD(Mat, Vec);
128: typedef PetscErrorCode (*FCN1)(void *, Vec); /* force argument to next function to not be extern C*/
129: static PetscErrorCode MatMFFDSetFunctioniBase_MFFD(Mat mat, FCN1 func)
130: {
131: MatMFFD ctx;
133: PetscFunctionBegin;
134: PetscCall(MatShellGetContext(mat, &ctx));
135: ctx->funcisetbase = func;
136: PetscFunctionReturn(PETSC_SUCCESS);
137: }
139: typedef PetscErrorCode (*FCN2)(void *, PetscInt, Vec, PetscScalar *); /* force argument to next function to not be extern C*/
140: static PetscErrorCode MatMFFDSetFunctioni_MFFD(Mat mat, FCN2 funci)
141: {
142: MatMFFD ctx;
144: PetscFunctionBegin;
145: PetscCall(MatShellGetContext(mat, &ctx));
146: ctx->funci = funci;
147: PetscCall(MatShellSetOperation(mat, MATOP_GET_DIAGONAL, (PetscErrorCodeFn *)MatGetDiagonal_MFFD));
148: PetscFunctionReturn(PETSC_SUCCESS);
149: }
151: static PetscErrorCode MatMFFDGetH_MFFD(Mat mat, PetscScalar *h)
152: {
153: MatMFFD ctx;
155: PetscFunctionBegin;
156: PetscCall(MatShellGetContext(mat, &ctx));
157: *h = ctx->currenth;
158: PetscFunctionReturn(PETSC_SUCCESS);
159: }
161: static PetscErrorCode MatMFFDResetHHistory_MFFD(Mat J)
162: {
163: MatMFFD ctx;
165: PetscFunctionBegin;
166: PetscCall(MatShellGetContext(J, &ctx));
167: ctx->ncurrenth = 0;
168: PetscFunctionReturn(PETSC_SUCCESS);
169: }
171: /*@
172: MatMFFDRegister - Adds a method to the `MATMFFD` registry.
174: Not Collective, No Fortran Support
176: Input Parameters:
177: + sname - name of a new user-defined compute-h module
178: - function - routine to create method context
180: Level: developer
182: Note:
183: `MatMFFDRegister()` may be called multiple times to add several user-defined solvers.
185: Example Usage:
186: .vb
187: MatMFFDRegister("my_h", MyHCreate);
188: .ve
190: Then, your solver can be chosen with the procedural interface via `MatMFFDSetType`(mfctx, "my_h")` or at runtime via the option
191: `-mat_mffd_type my_h`
193: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDRegisterAll()`, `MatMFFDRegisterDestroy()`
194: @*/
195: PetscErrorCode MatMFFDRegister(const char sname[], PetscErrorCode (*function)(MatMFFD))
196: {
197: PetscFunctionBegin;
198: PetscCall(MatInitializePackage());
199: PetscCall(PetscFunctionListAdd(&MatMFFDList, sname, function));
200: PetscFunctionReturn(PETSC_SUCCESS);
201: }
203: static PetscErrorCode MatDestroy_MFFD(Mat mat)
204: {
205: MatMFFD ctx;
207: PetscFunctionBegin;
208: PetscCall(MatShellGetContext(mat, &ctx));
209: PetscCall(VecDestroy(&ctx->w));
210: PetscCall(VecDestroy(&ctx->current_u));
211: if (ctx->current_f_allocated) PetscCall(VecDestroy(&ctx->current_f));
212: PetscTryTypeMethod(ctx, destroy);
213: PetscCall(PetscHeaderDestroy(&ctx));
215: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetBase_C", NULL));
216: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetFunctioniBase_C", NULL));
217: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetFunctioni_C", NULL));
218: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetFunction_C", NULL));
219: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetFunctionError_C", NULL));
220: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetCheckh_C", NULL));
221: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetPeriod_C", NULL));
222: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDResetHHistory_C", NULL));
223: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetHHistory_C", NULL));
224: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDSetType_C", NULL));
225: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMFFDGetH_C", NULL));
226: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatSNESMFSetReuseBase_C", NULL));
227: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatSNESMFGetReuseBase_C", NULL));
228: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatShellSetContext_C", NULL));
229: PetscFunctionReturn(PETSC_SUCCESS);
230: }
232: /*
233: MatMFFDView_MFFD - Views matrix-free parameters.
235: */
236: static PetscErrorCode MatView_MFFD(Mat J, PetscViewer viewer)
237: {
238: MatMFFD ctx;
239: PetscBool isascii, viewbase, viewfunction;
240: const char *prefix;
242: PetscFunctionBegin;
243: PetscCall(MatShellGetContext(J, &ctx));
244: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
245: if (isascii) {
246: PetscCall(PetscViewerASCIIPrintf(viewer, "Matrix-free approximation:\n"));
247: PetscCall(PetscViewerASCIIPushTab(viewer));
248: PetscCall(PetscViewerASCIIPrintf(viewer, "err=%g (relative error in function evaluation)\n", (double)ctx->error_rel));
249: if (!((PetscObject)ctx)->type_name) {
250: PetscCall(PetscViewerASCIIPrintf(viewer, "The compute h routine has not yet been set\n"));
251: } else {
252: PetscCall(PetscViewerASCIIPrintf(viewer, "Using %s compute h routine\n", ((PetscObject)ctx)->type_name));
253: }
254: #if PetscDefined(USE_COMPLEX)
255: if (ctx->usecomplex) PetscCall(PetscViewerASCIIPrintf(viewer, "Using Lyness complex number trick to compute the matrix-vector product\n"));
256: #endif
257: PetscTryTypeMethod(ctx, view, viewer);
258: PetscCall(PetscObjectGetOptionsPrefix((PetscObject)J, &prefix));
260: PetscCall(PetscOptionsHasName(((PetscObject)J)->options, prefix, "-mat_mffd_view_base", &viewbase));
261: if (viewbase) {
262: PetscCall(PetscViewerASCIIPrintf(viewer, "Base:\n"));
263: PetscCall(VecView(ctx->current_u, viewer));
264: }
265: PetscCall(PetscOptionsHasName(((PetscObject)J)->options, prefix, "-mat_mffd_view_function", &viewfunction));
266: if (viewfunction) {
267: PetscCall(PetscViewerASCIIPrintf(viewer, "Function:\n"));
268: PetscCall(VecView(ctx->current_f, viewer));
269: }
270: PetscCall(PetscViewerASCIIPopTab(viewer));
271: }
272: PetscFunctionReturn(PETSC_SUCCESS);
273: }
275: /*
276: MatAssemblyEnd_MFFD - Resets the ctx->ncurrenth to zero. This
277: allows the user to indicate the beginning of a new linear solve by calling
278: MatAssemblyXXX() on the matrix-free matrix. This then allows the
279: MatCreateMFFD_WP() to properly compute ||U|| only the first time
280: in the linear solver rather than every time.
282: This function is referenced directly from MatAssemblyEnd_SNESMF(), which may be in a different shared library hence
283: it must be labeled as PETSC_EXTERN
284: */
285: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode MatAssemblyEnd_MFFD(Mat J, MatAssemblyType mt)
286: {
287: MatMFFD j;
289: PetscFunctionBegin;
290: PetscCall(MatShellGetContext(J, &j));
291: PetscCall(MatMFFDResetHHistory(J));
292: PetscFunctionReturn(PETSC_SUCCESS);
293: }
295: /*
296: MatMult_MFFD - Default matrix-free form for Jacobian-vector product, y = F'(u)*a:
298: y ~= (F(u + ha) - F(u))/h,
299: where F = nonlinear function, as set by SNESSetFunction()
300: u = current iterate
301: h = difference interval
302: */
303: static PetscErrorCode MatMult_MFFD(Mat mat, Vec a, Vec y)
304: {
305: MatMFFD ctx;
306: PetscScalar h;
307: Vec w, U, F;
308: PetscBool zeroa;
310: PetscFunctionBegin;
311: PetscCall(MatShellGetContext(mat, &ctx));
312: PetscCheck(ctx->current_u, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_WRONGSTATE, "MatMFFDSetBase() has not been called, this is often caused by forgetting to call MatAssemblyBegin/End on the first Mat in the SNES compute function");
313: /* We log matrix-free matrix-vector products separately, so that we can
314: separate the performance monitoring from the cases that use conventional
315: storage. We may eventually modify event logging to associate events
316: with particular objects, hence alleviating the more general problem. */
317: PetscCall(PetscLogEventBegin(MATMFFD_Mult, a, y, 0, 0));
319: w = ctx->w;
320: U = ctx->current_u;
321: F = ctx->current_f;
322: /*
323: Compute differencing parameter
324: */
325: if (!((PetscObject)ctx)->type_name) {
326: PetscCall(MatMFFDSetType(mat, MATMFFD_WP));
327: PetscCall(MatSetFromOptions(mat));
328: }
329: PetscUseTypeMethod(ctx, compute, U, a, &h, &zeroa);
330: if (zeroa) {
331: PetscCall(VecSet(y, 0.0));
332: PetscCall(PetscLogEventEnd(MATMFFD_Mult, a, y, 0, 0));
333: PetscFunctionReturn(PETSC_SUCCESS);
334: }
336: PetscCheck(!mat->erroriffailure || !PetscIsInfOrNanScalar(h), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Computed NaN differencing parameter h");
337: if (ctx->checkh) PetscCall((*ctx->checkh)(ctx->checkhctx, U, a, &h));
339: /* keep a record of the current differencing parameter h */
340: ctx->currenth = h;
341: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscInfo(mat, "Current differencing parameter: %g + %g i\n", (double)PetscRealPart(h), (double)PetscImaginaryPart(h)));
342: else PetscCall(PetscInfo(mat, "Current differencing parameter: %15.12e\n", (double)PetscRealPart(h)));
343: if (ctx->historyh && ctx->ncurrenth < ctx->maxcurrenth) ctx->historyh[ctx->ncurrenth] = h;
344: ctx->ncurrenth++;
346: #if PetscDefined(USE_COMPLEX)
347: if (ctx->usecomplex) h = PETSC_i * h;
348: #endif
350: /* w = u + ha */
351: PetscCall(VecWAXPY(w, h, a, U));
353: /* compute func(U) as base for differencing; only needed first time in and not when provided by user */
354: if (ctx->ncurrenth == 1 && ctx->current_f_allocated) PetscCall((*ctx->func)(ctx->funcctx, U, F));
355: PetscCall((*ctx->func)(ctx->funcctx, w, y));
357: #if PetscDefined(USE_COMPLEX)
358: if (ctx->usecomplex) {
359: PetscCall(VecImaginaryPart(y));
360: h = PetscImaginaryPart(h);
361: } else {
362: PetscCall(VecAXPY(y, -1.0, F));
363: }
364: #else
365: PetscCall(VecAXPY(y, -1.0, F));
366: #endif
367: PetscCall(VecScale(y, 1.0 / h));
368: if (mat->nullsp) PetscCall(MatNullSpaceRemove(mat->nullsp, y));
370: PetscCall(PetscLogEventEnd(MATMFFD_Mult, a, y, 0, 0));
371: PetscFunctionReturn(PETSC_SUCCESS);
372: }
374: /*
375: MatGetDiagonal_MFFD - Gets the diagonal for a matrix-free matrix
377: y ~= (F(u + ha) - F(u))/h,
378: where F = nonlinear function, as set by SNESSetFunction()
379: u = current iterate
380: h = difference interval
381: */
382: static PetscErrorCode MatGetDiagonal_MFFD(Mat mat, Vec a)
383: {
384: MatMFFD ctx;
385: PetscScalar h, *aa, *ww, v;
386: PetscReal epsilon = PETSC_SQRT_MACHINE_EPSILON, umin = 100.0 * PETSC_SQRT_MACHINE_EPSILON;
387: Vec w, U;
388: PetscInt i, rstart, rend;
390: PetscFunctionBegin;
391: PetscCall(MatShellGetContext(mat, &ctx));
392: PetscCheck(ctx->func, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Requires calling MatMFFDSetFunction() first");
393: PetscCheck(ctx->funci, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Requires calling MatMFFDSetFunctioni() first");
394: w = ctx->w;
395: U = ctx->current_u;
396: PetscCall((*ctx->func)(ctx->funcctx, U, a));
397: if (ctx->funcisetbase) PetscCall((*ctx->funcisetbase)(ctx->funcctx, U));
398: PetscCall(VecCopy(U, w));
400: PetscCall(VecGetOwnershipRange(a, &rstart, &rend));
401: PetscCall(VecGetArray(a, &aa));
402: for (i = rstart; i < rend; i++) {
403: PetscCall(VecGetArray(w, &ww));
404: h = ww[i - rstart];
405: if (h == 0.0) h = 1.0;
406: if (PetscAbsScalar(h) < umin && PetscRealPart(h) >= 0.0) h = umin;
407: else if (PetscRealPart(h) < 0.0 && PetscAbsScalar(h) < umin) h = -umin;
408: h *= epsilon;
410: ww[i - rstart] += h;
411: PetscCall(VecRestoreArray(w, &ww));
412: PetscCall((*ctx->funci)(ctx->funcctx, i, w, &v));
413: aa[i - rstart] = (v - aa[i - rstart]) / h;
415: PetscCall(VecGetArray(w, &ww));
416: ww[i - rstart] -= h;
417: PetscCall(VecRestoreArray(w, &ww));
418: }
419: PetscCall(VecRestoreArray(a, &aa));
420: PetscFunctionReturn(PETSC_SUCCESS);
421: }
423: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode MatMFFDSetBase_MFFD(Mat J, Vec U, Vec F)
424: {
425: MatMFFD ctx;
427: PetscFunctionBegin;
428: PetscCall(MatShellGetContext(J, &ctx));
429: PetscCall(MatMFFDResetHHistory(J));
430: if (!ctx->current_u) {
431: PetscCall(VecDuplicate(U, &ctx->current_u));
432: PetscCall(VecLockReadPush(ctx->current_u));
433: }
434: PetscCall(VecLockReadPop(ctx->current_u));
435: PetscCall(VecCopy(U, ctx->current_u));
436: PetscCall(VecLockReadPush(ctx->current_u));
437: if (F) {
438: if (ctx->current_f_allocated) PetscCall(VecDestroy(&ctx->current_f));
439: ctx->current_f = F;
440: ctx->current_f_allocated = PETSC_FALSE;
441: } else if (!ctx->current_f_allocated) {
442: PetscCall(MatCreateVecs(J, NULL, &ctx->current_f));
443: ctx->current_f_allocated = PETSC_TRUE;
444: }
445: if (!ctx->w) PetscCall(VecDuplicate(ctx->current_u, &ctx->w));
446: J->assembled = PETSC_TRUE;
447: PetscFunctionReturn(PETSC_SUCCESS);
448: }
450: typedef PetscErrorCode (*FCN3)(void *, Vec, Vec, PetscScalar *); /* force argument to next function to not be extern C*/
451: static PetscErrorCode MatMFFDSetCheckh_MFFD(Mat J, FCN3 fun, void *ectx)
452: {
453: MatMFFD ctx;
455: PetscFunctionBegin;
456: PetscCall(MatShellGetContext(J, &ctx));
457: ctx->checkh = fun;
458: ctx->checkhctx = ectx;
459: PetscFunctionReturn(PETSC_SUCCESS);
460: }
462: /*@
463: MatMFFDSetOptionsPrefix - Sets the prefix used for searching for all
464: MATMFFD` options in the database.
466: Collective
468: Input Parameters:
469: + mat - the `MATMFFD` context
470: - prefix - the prefix to prepend to all option names
472: Note:
473: A hyphen (-) must NOT be given at the beginning of the prefix name.
474: The first character of all runtime options is AUTOMATICALLY the hyphen.
476: Level: advanced
478: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatSetFromOptions()`, `MatCreateSNESMF()`, `MatCreateMFFD()`
479: @*/
480: PetscErrorCode MatMFFDSetOptionsPrefix(Mat mat, const char prefix[])
481: {
482: MatMFFD mfctx;
484: PetscFunctionBegin;
486: PetscCall(MatShellGetContext(mat, &mfctx));
488: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mfctx, prefix));
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: static PetscErrorCode MatSetFromOptions_MFFD(Mat mat, PetscOptionItems PetscOptionsObject)
493: {
494: MatMFFD mfctx;
495: PetscBool flg;
496: char ftype[256];
498: PetscFunctionBegin;
499: PetscCall(MatShellGetContext(mat, &mfctx));
501: PetscObjectOptionsBegin((PetscObject)mfctx);
502: PetscCall(PetscOptionsFList("-mat_mffd_type", "Matrix free type", "MatMFFDSetType", MatMFFDList, ((PetscObject)mfctx)->type_name, ftype, sizeof(ftype), &flg));
503: if (flg) PetscCall(MatMFFDSetType(mat, ftype));
505: PetscCall(PetscOptionsReal("-mat_mffd_err", "set sqrt relative error in function", "MatMFFDSetFunctionError", mfctx->error_rel, &mfctx->error_rel, NULL));
506: PetscCall(PetscOptionsInt("-mat_mffd_period", "how often h is recomputed", "MatMFFDSetPeriod", mfctx->recomputeperiod, &mfctx->recomputeperiod, NULL));
508: flg = PETSC_FALSE;
509: PetscCall(PetscOptionsBool("-mat_mffd_check_positivity", "Insure that U + h*a is nonnegative", "MatMFFDSetCheckh", flg, &flg, NULL));
510: if (flg) PetscCall(MatMFFDSetCheckh(mat, MatMFFDCheckPositivity, NULL));
511: #if PetscDefined(USE_COMPLEX)
512: PetscCall(PetscOptionsBool("-mat_mffd_complex", "Use Lyness complex number trick to compute the matrix-vector product", "None", mfctx->usecomplex, &mfctx->usecomplex, NULL));
513: #endif
514: PetscTryTypeMethod(mfctx, setfromoptions, PetscOptionsObject);
515: PetscOptionsEnd();
516: PetscFunctionReturn(PETSC_SUCCESS);
517: }
519: static PetscErrorCode MatMFFDSetPeriod_MFFD(Mat mat, PetscInt period)
520: {
521: MatMFFD ctx;
523: PetscFunctionBegin;
524: PetscCall(MatShellGetContext(mat, &ctx));
525: ctx->recomputeperiod = period;
526: PetscFunctionReturn(PETSC_SUCCESS);
527: }
529: static PetscErrorCode MatMFFDSetFunction_MFFD(Mat mat, MatMFFDFn *func, void *funcctx)
530: {
531: MatMFFD ctx;
533: PetscFunctionBegin;
534: PetscCall(MatShellGetContext(mat, &ctx));
535: ctx->func = func;
536: ctx->funcctx = funcctx;
537: PetscFunctionReturn(PETSC_SUCCESS);
538: }
540: static PetscErrorCode MatMFFDSetFunctionError_MFFD(Mat mat, PetscReal error)
541: {
542: PetscFunctionBegin;
543: if (error != (PetscReal)PETSC_DEFAULT) {
544: MatMFFD ctx;
546: PetscCall(MatShellGetContext(mat, &ctx));
547: ctx->error_rel = error;
548: }
549: PetscFunctionReturn(PETSC_SUCCESS);
550: }
552: static PetscErrorCode MatMFFDSetHHistory_MFFD(Mat J, PetscScalar history[], PetscInt nhistory)
553: {
554: MatMFFD ctx;
556: PetscFunctionBegin;
557: PetscCall(MatShellGetContext(J, &ctx));
558: ctx->historyh = history;
559: ctx->maxcurrenth = nhistory;
560: ctx->currenth = 0.;
561: PetscFunctionReturn(PETSC_SUCCESS);
562: }
564: /*MC
565: MATMFFD - "mffd" - A matrix-free matrix type.
567: Level: advanced
569: Developer Notes:
570: This is implemented on top of `MATSHELL` to get support for scaling and shifting without requiring duplicate code
572: Users should not MatShell... operations on this class, there is some error checking for that incorrect usage
574: .seealso: [](ch_matrices), `Mat`, `MatCreateMFFD()`, `MatCreateSNESMF()`, `MatMFFDSetFunction()`, `MatMFFDSetType()`,
575: `MatMFFDSetFunctionError()`, `MatMFFDDSSetUmin()`,
576: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`,
577: `MatMFFDGetH()`
578: M*/
579: PETSC_EXTERN PetscErrorCode MatCreate_MFFD(Mat A)
580: {
581: MatMFFD mfctx;
583: PetscFunctionBegin;
584: PetscCall(MatMFFDInitializePackage());
586: PetscCall(PetscHeaderCreate(mfctx, MATMFFD_CLASSID, "MatMFFD", "Matrix-free Finite Differencing", "Mat", PetscObjectComm((PetscObject)A), NULL, NULL));
588: mfctx->error_rel = PETSC_SQRT_MACHINE_EPSILON;
589: mfctx->recomputeperiod = 1;
590: mfctx->count = 0;
591: mfctx->currenth = 0.0;
592: mfctx->historyh = NULL;
593: mfctx->ncurrenth = 0;
594: mfctx->maxcurrenth = 0;
595: ((PetscObject)mfctx)->type_name = NULL;
597: /*
598: Create the empty data structure to contain compute-h routines.
599: These will be filled in below from the command line options or
600: a later call with MatMFFDSetType() or if that is not called
601: then it will default in the first use of MatMult_MFFD()
602: */
603: mfctx->hctx = NULL;
605: mfctx->func = NULL;
606: mfctx->funcctx = NULL;
607: mfctx->w = NULL;
608: mfctx->mat = A;
610: PetscCall(MatSetType(A, MATSHELL));
611: PetscCall(MatShellSetContext(A, mfctx));
612: PetscCall(MatShellSetOperation(A, MATOP_MULT, (PetscErrorCodeFn *)MatMult_MFFD));
613: PetscCall(MatShellSetOperation(A, MATOP_DESTROY, (PetscErrorCodeFn *)MatDestroy_MFFD));
614: PetscCall(MatShellSetOperation(A, MATOP_VIEW, (PetscErrorCodeFn *)MatView_MFFD));
615: PetscCall(MatShellSetOperation(A, MATOP_ASSEMBLY_END, (PetscErrorCodeFn *)MatAssemblyEnd_MFFD));
616: PetscCall(MatShellSetOperation(A, MATOP_SET_FROM_OPTIONS, (PetscErrorCodeFn *)MatSetFromOptions_MFFD));
617: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatShellSetContext_C", MatShellSetContext_Immutable));
618: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatShellSetContextDestroy_C", MatShellSetContextDestroy_Immutable));
619: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatShellSetManageScalingShifts_C", MatShellSetManageScalingShifts_Immutable));
621: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetBase_C", MatMFFDSetBase_MFFD));
622: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetFunctioniBase_C", MatMFFDSetFunctioniBase_MFFD));
623: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetFunctioni_C", MatMFFDSetFunctioni_MFFD));
624: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetFunction_C", MatMFFDSetFunction_MFFD));
625: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetCheckh_C", MatMFFDSetCheckh_MFFD));
626: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetPeriod_C", MatMFFDSetPeriod_MFFD));
627: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetFunctionError_C", MatMFFDSetFunctionError_MFFD));
628: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDResetHHistory_C", MatMFFDResetHHistory_MFFD));
629: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetHHistory_C", MatMFFDSetHHistory_MFFD));
630: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDSetType_C", MatMFFDSetType_MFFD));
631: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatMFFDGetH_C", MatMFFDGetH_MFFD));
632: PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATMFFD));
633: PetscFunctionReturn(PETSC_SUCCESS);
634: }
636: /*@
637: MatCreateMFFD - Creates a matrix-free matrix of type `MATMFFD` that uses finite differences on a provided function to
638: approximately multiply a vector by the matrix (Jacobian) . See also `MatCreateSNESMF()`
640: Collective
642: Input Parameters:
643: + comm - MPI communicator
644: . m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
645: This value should be the same as the local size used in creating the
646: y vector for the matrix-vector product y = Ax.
647: . n - This value should be the same as the local size used in creating the
648: x vector for the matrix-vector product y = Ax. (or `PETSC_DECIDE` to have
649: calculated if `N` is given) For square matrices `n` is almost always `m`.
650: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
651: - N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
653: Output Parameter:
654: . J - the matrix-free matrix
656: Options Database Keys:
657: + -mat_mffd_type (wp|ds) - see `MATMFFD_WP` or `MATMFFD_DS`
658: . -mat_mffd_err error_rel - square root of estimated relative error in function evaluation
659: . -mat_mffd_period period - how often h is recomputed, defaults to 1, every time
660: . -mat_mffd_check_positivity (true|false) - possibly decrease `h` until U + h*a has only positive values
661: . -mat_mffd_umin umin - Sets umin (for default PETSc routine that computes h only)
662: . -mat_mffd_complex (true|false) - use the Lyness trick with complex numbers to compute the matrix-vector product instead of differencing
663: (requires real valued functions but that PETSc be configured for complex numbers)
664: . -snes_mf (true|false) - use the finite difference based matrix-free matrix with `SNESSolve()` with no preconditioner by default
665: - -snes_mf_operator (true|false) - use the finite difference based matrix-free matrix with `SNESSolve()` but construct a preconditioner
666: using the matrix passed as `pmat` to `SNESSetJacobian()`.
668: Level: advanced
670: Notes:
671: Use `MatMFFDSetFunction()` to provide the function that will be differenced to compute the matrix-vector product.
673: The matrix-free matrix context contains the function pointers
674: and work space for performing finite difference approximations of
675: Jacobian-vector products, F'(u)*a,
677: The default code uses the following approach to compute h
679: .vb
680: F'(u)*a = [F(u+h*a) - F(u)]/h where
681: h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1}
682: = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 otherwise
683: where
684: error_rel = square root of relative error in function evaluation
685: umin = minimum iterate parameter
686: .ve
688: To have `SNES` use the matrix-free finite difference matrix-vector product and not provide a separate matrix
689: from which to compute the preconditioner (the `pmat` argument `SNESSetJacobian()`), then simply call `SNESSetJacobian()`
690: with `NULL` for the matrices and `MatMFFDComputeJacobian()`. Or use the options database option `-snes_mf`
692: The user can set `error_rel` via `MatMFFDSetFunctionError()` and `umin` via `MatMFFDDSSetUmin()`.
694: Use `MATSHELL` or `MatCreateShell()` to provide your own custom matrix-vector operation.
696: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatDestroy()`, `MatMFFDSetFunctionError()`, `MatMFFDDSSetUmin()`, `MatMFFDSetFunction()`,
697: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`, `MatCreateSNESMF()`, `MatCreateShell()`, `MATSHELL`,
698: `MatMFFDGetH()`, `MatMFFDRegister()`, `MatMFFDComputeJacobian()`
699: @*/
700: PetscErrorCode MatCreateMFFD(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, Mat *J)
701: {
702: PetscFunctionBegin;
703: PetscCall(MatCreate(comm, J));
704: PetscCall(MatSetSizes(*J, m, n, M, N));
705: PetscCall(MatSetType(*J, MATMFFD));
706: PetscCall(MatSetUp(*J));
707: PetscFunctionReturn(PETSC_SUCCESS);
708: }
710: /*@
711: MatMFFDGetH - Gets the last value that was used as the differencing for a `MATMFFD` matrix
712: parameter.
714: Not Collective
716: Input Parameters:
717: . mat - the `MATMFFD` matrix
719: Output Parameter:
720: . h - the differencing step size
722: Level: advanced
724: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatCreateSNESMF()`, `MatMFFDSetHHistory()`, `MatCreateMFFD()`, `MatMFFDResetHHistory()`
725: @*/
726: PetscErrorCode MatMFFDGetH(Mat mat, PetscScalar *h)
727: {
728: PetscFunctionBegin;
730: PetscAssertPointer(h, 2);
731: PetscUseMethod(mat, "MatMFFDGetH_C", (Mat, PetscScalar *), (mat, h));
732: PetscFunctionReturn(PETSC_SUCCESS);
733: }
735: /*@
736: MatMFFDSetFunction - Sets the function used in applying the matrix-free `MATMFFD` matrix.
738: Logically Collective
740: Input Parameters:
741: + mat - the matrix-free matrix `MATMFFD` created via `MatCreateSNESMF()` or `MatCreateMFFD()`
742: . func - the function to use
743: - funcctx - optional function context passed to function
745: Level: advanced
747: Notes:
748: If you use this you MUST call `MatAssemblyBegin()` and `MatAssemblyEnd()` on the matrix-free
749: matrix inside your compute Jacobian routine
751: If this is not set then it will use the function set with `SNESSetFunction()` if `MatCreateSNESMF()` was used.
753: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDFn`, `MatCreateSNESMF()`, `MatMFFDGetH()`, `MatCreateMFFD()`,
754: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`, `SNESSetFunction()`
755: @*/
756: PetscErrorCode MatMFFDSetFunction(Mat mat, MatMFFDFn *func, void *funcctx)
757: {
758: PetscFunctionBegin;
760: PetscTryMethod(mat, "MatMFFDSetFunction_C", (Mat, MatMFFDFn *, void *), (mat, func, funcctx));
761: PetscFunctionReturn(PETSC_SUCCESS);
762: }
764: /*@
765: MatMFFDSetFunctioni - Sets the function for computing a single component for a `MATMFFD` matrix
767: Logically Collective
769: Input Parameters:
770: + mat - the matrix-free matrix `MATMFFD`
771: - funci - the function to use
773: Level: advanced
775: Notes:
776: If you use this you MUST call `MatAssemblyBegin()` and `MatAssemblyEnd()` on the matrix-free
777: matrix inside your compute Jacobian routine.
779: This function is necessary to compute the diagonal of the matrix.
780: `funci` must not contain any MPI call as it is called inside a loop on the local portion of the vector.
782: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDiFn`, `MatCreateSNESMF()`, `MatMFFDGetH()`, `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`,
783: `SNESSetFunction()`, `MatGetDiagonal()`
784: @*/
785: PetscErrorCode MatMFFDSetFunctioni(Mat mat, MatMFFDiFn *funci)
786: {
787: PetscFunctionBegin;
789: PetscTryMethod(mat, "MatMFFDSetFunctioni_C", (Mat, MatMFFDiFn *), (mat, funci));
790: PetscFunctionReturn(PETSC_SUCCESS);
791: }
793: /*@
794: MatMFFDSetFunctioniBase - Sets the function to compute the base vector for a single component function evaluation for a `MATMFFD` matrix
796: Logically Collective
798: Input Parameters:
799: + mat - the `MATMFFD` matrix-free matrix
800: - func - the function to use
802: Level: advanced
804: Notes:
805: If you use this you MUST call `MatAssemblyBegin()` and `MatAssemblyEnd()` on the matrix-free
806: matrix inside your compute Jacobian routine.
808: This function is necessary to compute the diagonal of the matrix, used for example with `PCJACOBI`
810: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatCreateSNESMF()`, `MatMFFDGetH()`, `MatCreateMFFD()`,
811: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`, `SNESSetFunction()`, `MatGetDiagonal()`
812: @*/
813: PetscErrorCode MatMFFDSetFunctioniBase(Mat mat, MatMFFDiBaseFn *func)
814: {
815: PetscFunctionBegin;
817: PetscTryMethod(mat, "MatMFFDSetFunctioniBase_C", (Mat, MatMFFDiBaseFn *), (mat, func));
818: PetscFunctionReturn(PETSC_SUCCESS);
819: }
821: /*@
822: MatMFFDSetPeriod - Sets how often the step-size `h` is recomputed for a `MATMFFD` matrix, by default it is every time
824: Logically Collective
826: Input Parameters:
827: + mat - the `MATMFFD` matrix-free matrix
828: - period - 1 for every time, 2 for every second etc
830: Options Database Key:
831: . -mat_mffd_period period - Sets how often `h` is recomputed
833: Level: advanced
835: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatCreateSNESMF()`, `MatMFFDGetH()`,
836: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`
837: @*/
838: PetscErrorCode MatMFFDSetPeriod(Mat mat, PetscInt period)
839: {
840: PetscFunctionBegin;
843: PetscTryMethod(mat, "MatMFFDSetPeriod_C", (Mat, PetscInt), (mat, period));
844: PetscFunctionReturn(PETSC_SUCCESS);
845: }
847: /*@
848: MatMFFDSetFunctionError - Sets the error_rel for the approximation of matrix-vector products using finite differences with the `MATMFFD` matrix
850: Logically Collective
852: Input Parameters:
853: + mat - the `MATMFFD` matrix-free matrix
854: - error - relative error (should be set to the square root of the relative error in the function evaluations)
856: Options Database Key:
857: . -mat_mffd_err error_rel - Sets error_rel
859: Level: advanced
861: Note:
862: The default matrix-free matrix-vector product routine computes
863: .vb
864: F'(u)*a = [F(u+h*a) - F(u)]/h where
865: h = error_rel*u'a/||a||^2 if |u'a| > umin*||a||_{1}
866: = error_rel*umin*sign(u'a)*||a||_{1}/||a||^2 else
867: .ve
869: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatCreateSNESMF()`, `MatMFFDGetH()`, `MatCreateMFFD()`,
870: `MatMFFDSetHHistory()`, `MatMFFDResetHHistory()`
871: @*/
872: PetscErrorCode MatMFFDSetFunctionError(Mat mat, PetscReal error)
873: {
874: PetscFunctionBegin;
877: PetscTryMethod(mat, "MatMFFDSetFunctionError_C", (Mat, PetscReal), (mat, error));
878: PetscFunctionReturn(PETSC_SUCCESS);
879: }
881: /*@
882: MatMFFDSetHHistory - Sets an array to collect a history of the
883: differencing values (h) computed for the matrix-free product `MATMFFD` matrix
885: Logically Collective
887: Input Parameters:
888: + J - the `MATMFFD` matrix-free matrix
889: . history - space to hold the history
890: - nhistory - number of entries in history, if more entries are generated than
891: nhistory, then the later ones are discarded
893: Level: advanced
895: Note:
896: Use `MatMFFDResetHHistory()` to reset the history counter and collect
897: a new batch of differencing parameters, h.
899: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDGetH()`, `MatCreateSNESMF()`,
900: `MatMFFDResetHHistory()`, `MatMFFDSetFunctionError()`
901: @*/
902: PetscErrorCode MatMFFDSetHHistory(Mat J, PetscScalar history[], PetscInt nhistory)
903: {
904: PetscFunctionBegin;
906: if (history) PetscAssertPointer(history, 2);
908: PetscUseMethod(J, "MatMFFDSetHHistory_C", (Mat, PetscScalar[], PetscInt), (J, history, nhistory));
909: PetscFunctionReturn(PETSC_SUCCESS);
910: }
912: /*@
913: MatMFFDResetHHistory - Resets the counter to zero to begin
914: collecting a new set of differencing histories for the `MATMFFD` matrix
916: Logically Collective
918: Input Parameter:
919: . J - the matrix-free matrix context
921: Level: advanced
923: Note:
924: Use `MatMFFDSetHHistory()` to create the original history counter.
926: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDGetH()`, `MatCreateSNESMF()`,
927: `MatMFFDSetHHistory()`, `MatMFFDSetFunctionError()`
928: @*/
929: PetscErrorCode MatMFFDResetHHistory(Mat J)
930: {
931: PetscFunctionBegin;
933: PetscTryMethod(J, "MatMFFDResetHHistory_C", (Mat), (J));
934: PetscFunctionReturn(PETSC_SUCCESS);
935: }
937: /*@
938: MatMFFDSetBase - Sets the vector `U` at which matrix vector products of the
939: Jacobian are computed for the `MATMFFD` matrix
941: Logically Collective
943: Input Parameters:
944: + J - the `MATMFFD` matrix
945: . U - the vector
946: - F - (optional) vector that contains F(u) if it has been already computed
948: Level: advanced
950: Notes:
951: This is rarely used directly
953: If `F` is provided then it is not recomputed. Otherwise the function is evaluated at the base
954: point during the first `MatMult()` after each call to `MatMFFDSetBase()`.
956: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMult()`
957: @*/
958: PetscErrorCode MatMFFDSetBase(Mat J, Vec U, Vec F)
959: {
960: PetscFunctionBegin;
964: PetscTryMethod(J, "MatMFFDSetBase_C", (Mat, Vec, Vec), (J, U, F));
965: PetscFunctionReturn(PETSC_SUCCESS);
966: }
968: /*@
969: MatMFFDSetCheckh - Sets a function that checks the computed `h` and adjusts
970: it to satisfy some criteria for the `MATMFFD` matrix
972: Logically Collective
974: Input Parameters:
975: + J - the `MATMFFD` matrix
976: . fun - the function that checks `h`, see `MatMFFDCheckhFn`
977: - ctx - any context needed by the function
979: Options Database Keys:
980: . -mat_mffd_check_positivity (true|false) - Ensure that $U + h \, a$ is non-negative
982: Level: advanced
984: Notes:
985: For example, `MatMFFDCheckPositivity()` insures that all entries of $U + h \, a$ are non-negative
987: The function you provide is called after the default `h` has been computed and allows you to
988: modify it.
990: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDCheckhFn`, `MatMFFDCheckPositivity()`
991: @*/
992: PetscErrorCode MatMFFDSetCheckh(Mat J, MatMFFDCheckhFn *fun, PetscCtx ctx)
993: {
994: PetscFunctionBegin;
996: PetscTryMethod(J, "MatMFFDSetCheckh_C", (Mat, MatMFFDCheckhFn *, void *), (J, fun, ctx));
997: PetscFunctionReturn(PETSC_SUCCESS);
998: }
1000: /*@
1001: MatMFFDCheckPositivity - Checks that all entries in $U + h \, a$ are positive or
1002: zero, decreases `h` until this is satisfied for a `MATMFFD` matrix
1004: Logically Collective
1006: Input Parameters:
1007: + dummy - context variable (unused)
1008: . U - base vector that is added to
1009: . a - vector that is added
1010: - h - scaling factor on `a`, may be changed on output
1012: Options Database Keys:
1013: . -mat_mffd_check_positivity (true|false) - Ensure that $U + h \, a$ is nonnegative
1015: Level: advanced
1017: Note:
1018: This is rarely used directly, rather it is passed as an argument to `MatMFFDSetCheckh()`
1020: .seealso: [](ch_matrices), `Mat`, `MATMFFD`, `MatMFFDSetCheckh()`
1021: @*/
1022: PetscErrorCode MatMFFDCheckPositivity(void *dummy, Vec U, Vec a, PetscScalar *h)
1023: {
1024: PetscReal val, minval;
1025: PetscScalar *u_vec, *a_vec;
1026: PetscInt n;
1027: MPI_Comm comm;
1029: PetscFunctionBegin;
1032: PetscAssertPointer(h, 4);
1033: PetscCall(PetscObjectGetComm((PetscObject)U, &comm));
1034: PetscCall(VecGetArray(U, &u_vec));
1035: PetscCall(VecGetArray(a, &a_vec));
1036: PetscCall(VecGetLocalSize(U, &n));
1037: minval = PetscAbsScalar(*h) * PetscRealConstant(1.01);
1038: for (PetscInt i = 0; i < n; i++) {
1039: if (PetscRealPart(u_vec[i] + *h * a_vec[i]) <= 0.0) {
1040: val = PetscAbsScalar(u_vec[i] / a_vec[i]);
1041: if (val < minval) minval = val;
1042: }
1043: }
1044: PetscCall(VecRestoreArray(U, &u_vec));
1045: PetscCall(VecRestoreArray(a, &a_vec));
1046: PetscCallMPI(MPIU_Allreduce(&minval, &val, 1, MPIU_REAL, MPIU_MIN, comm));
1047: if (val <= PetscAbsScalar(*h)) {
1048: val = 0.99 * val;
1049: PetscCall(PetscInfo(U, "Scaling back h from %g to %g\n", (double)PetscRealPart(*h), (double)val));
1050: if (PetscRealPart(*h) > 0.0) *h = val;
1051: else *h = -val;
1052: }
1053: PetscFunctionReturn(PETSC_SUCCESS);
1054: }