Actual source code: dmlocalsnes.c
1: #include <petsc/private/dmimpl.h>
2: #include <petsc/private/snesimpl.h>
4: typedef struct {
5: PetscErrorCode (*objectivelocal)(DM, Vec, PetscReal *, PetscCtx);
6: PetscErrorCode (*residuallocal)(DM, Vec, Vec, PetscCtx);
7: PetscErrorCode (*jacobianlocal)(DM, Vec, Mat, Mat, PetscCtx);
8: PetscErrorCode (*boundarylocal)(DM, Vec, PetscCtx);
9: PetscCtx objectivelocalctx;
10: PetscCtx residuallocalctx;
11: PetscCtx jacobianlocalctx;
12: PetscCtx boundarylocalctx;
13: } DMSNES_Local;
15: static PetscErrorCode DMSNESDestroy_DMLocal(DMSNES sdm)
16: {
17: PetscFunctionBegin;
18: PetscCall(PetscFree(sdm->data));
19: PetscFunctionReturn(PETSC_SUCCESS);
20: }
22: static PetscErrorCode DMSNESDuplicate_DMLocal(DMSNES oldsdm, DMSNES sdm)
23: {
24: PetscFunctionBegin;
25: if (sdm->data != oldsdm->data) {
26: PetscCall(PetscFree(sdm->data));
27: PetscCall(PetscNew((DMSNES_Local **)&sdm->data));
28: if (oldsdm->data) PetscCall(PetscMemcpy(sdm->data, oldsdm->data, sizeof(DMSNES_Local)));
29: }
30: PetscFunctionReturn(PETSC_SUCCESS);
31: }
33: static PetscErrorCode DMLocalSNESGetContext(DM dm, DMSNES sdm, DMSNES_Local **dmlocalsnes)
34: {
35: PetscFunctionBegin;
36: *dmlocalsnes = NULL;
37: if (!sdm->data) {
38: PetscCall(PetscNew((DMSNES_Local **)&sdm->data));
40: sdm->ops->destroy = DMSNESDestroy_DMLocal;
41: sdm->ops->duplicate = DMSNESDuplicate_DMLocal;
42: }
43: *dmlocalsnes = (DMSNES_Local *)sdm->data;
44: PetscFunctionReturn(PETSC_SUCCESS);
45: }
47: static PetscErrorCode SNESComputeObjective_DMLocal(SNES snes, Vec X, PetscReal *obj, PetscCtx ctx)
48: {
49: DMSNES_Local *dmlocalsnes = (DMSNES_Local *)ctx;
50: DM dm;
51: Vec Xloc;
52: PetscBool transform;
54: PetscFunctionBegin;
57: PetscCall(SNESGetDM(snes, &dm));
58: PetscCall(DMGetLocalVector(dm, &Xloc));
59: PetscCall(VecZeroEntries(Xloc));
60: /* Non-conforming routines needs boundary values before G2L */
61: if (dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
62: PetscCall(DMGlobalToLocalBegin(dm, X, INSERT_VALUES, Xloc));
63: PetscCall(DMGlobalToLocalEnd(dm, X, INSERT_VALUES, Xloc));
64: /* Need to reset boundary values if we transformed */
65: PetscCall(DMHasBasisTransform(dm, &transform));
66: if (transform && dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
67: CHKMEMQ;
68: PetscCall((*dmlocalsnes->objectivelocal)(dm, Xloc, obj, dmlocalsnes->objectivelocalctx));
69: CHKMEMQ;
70: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, obj, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)snes)));
71: PetscCall(DMRestoreLocalVector(dm, &Xloc));
72: PetscFunctionReturn(PETSC_SUCCESS);
73: }
75: static PetscErrorCode SNESComputeFunction_DMLocal(SNES snes, Vec X, Vec F, PetscCtx ctx)
76: {
77: DMSNES_Local *dmlocalsnes = (DMSNES_Local *)ctx;
78: DM dm;
79: Vec Xloc, Floc;
80: PetscBool transform;
82: PetscFunctionBegin;
86: PetscCall(SNESGetDM(snes, &dm));
87: PetscCall(DMGetLocalVector(dm, &Xloc));
88: PetscCall(DMGetLocalVector(dm, &Floc));
89: PetscCall(VecZeroEntries(Xloc));
90: PetscCall(VecZeroEntries(Floc));
91: /* Non-conforming routines needs boundary values before G2L */
92: if (dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
93: PetscCall(DMGlobalToLocalBegin(dm, X, INSERT_VALUES, Xloc));
94: PetscCall(DMGlobalToLocalEnd(dm, X, INSERT_VALUES, Xloc));
95: /* Need to reset boundary values if we transformed */
96: PetscCall(DMHasBasisTransform(dm, &transform));
97: if (transform && dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
98: CHKMEMQ;
99: PetscCall((*dmlocalsnes->residuallocal)(dm, Xloc, Floc, dmlocalsnes->residuallocalctx));
100: CHKMEMQ;
101: PetscCall(VecZeroEntries(F));
102: PetscCall(DMLocalToGlobalBegin(dm, Floc, ADD_VALUES, F));
103: PetscCall(DMLocalToGlobalEnd(dm, Floc, ADD_VALUES, F));
104: PetscCall(DMRestoreLocalVector(dm, &Floc));
105: PetscCall(DMRestoreLocalVector(dm, &Xloc));
106: {
107: char name[PETSC_MAX_PATH_LEN];
108: char oldname[PETSC_MAX_PATH_LEN];
109: const char *tmp;
110: PetscInt it;
112: PetscCall(SNESGetIterationNumber(snes, &it));
113: PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "Solution, Iterate %" PetscInt_FMT, it));
114: PetscCall(PetscObjectGetName((PetscObject)X, &tmp));
115: PetscCall(PetscStrncpy(oldname, tmp, PETSC_MAX_PATH_LEN - 1));
116: PetscCall(PetscObjectSetName((PetscObject)X, name));
117: PetscCall(VecViewFromOptions(X, (PetscObject)snes, "-dmsnes_solution_vec_view"));
118: PetscCall(PetscObjectSetName((PetscObject)X, oldname));
119: PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "Residual, Iterate %" PetscInt_FMT, it));
120: PetscCall(PetscObjectSetName((PetscObject)F, name));
121: PetscCall(VecViewFromOptions(F, (PetscObject)snes, "-dmsnes_residual_vec_view"));
122: }
123: PetscFunctionReturn(PETSC_SUCCESS);
124: }
126: static PetscErrorCode SNESComputeJacobian_DMLocal(SNES snes, Vec X, Mat A, Mat B, PetscCtx ctx)
127: {
128: DMSNES_Local *dmlocalsnes = (DMSNES_Local *)ctx;
129: DM dm;
130: Vec Xloc;
131: PetscBool transform;
133: PetscFunctionBegin;
134: PetscCall(SNESGetDM(snes, &dm));
135: if (dmlocalsnes->jacobianlocal) {
136: PetscCall(DMGetLocalVector(dm, &Xloc));
137: PetscCall(VecZeroEntries(Xloc));
138: /* Non-conforming routines needs boundary values before G2L */
139: if (dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
140: PetscCall(DMGlobalToLocalBegin(dm, X, INSERT_VALUES, Xloc));
141: PetscCall(DMGlobalToLocalEnd(dm, X, INSERT_VALUES, Xloc));
142: /* Need to reset boundary values if we transformed */
143: PetscCall(DMHasBasisTransform(dm, &transform));
144: if (transform && dmlocalsnes->boundarylocal) PetscCall((*dmlocalsnes->boundarylocal)(dm, Xloc, dmlocalsnes->boundarylocalctx));
145: CHKMEMQ;
146: PetscCall((*dmlocalsnes->jacobianlocal)(dm, Xloc, A, B, dmlocalsnes->jacobianlocalctx));
147: CHKMEMQ;
148: PetscCall(DMRestoreLocalVector(dm, &Xloc));
149: } else {
150: MatFDColoring fdcoloring;
151: PetscCall(PetscObjectQuery((PetscObject)dm, "DMDASNES_FDCOLORING", (PetscObject *)&fdcoloring));
152: if (!fdcoloring) {
153: ISColoring coloring;
155: PetscCall(DMCreateColoring(dm, dm->coloringtype, &coloring));
156: PetscCall(MatFDColoringCreate(B, coloring, &fdcoloring));
157: PetscCall(ISColoringDestroy(&coloring));
158: switch (dm->coloringtype) {
159: case IS_COLORING_GLOBAL:
160: PetscCall(MatFDColoringSetFunction(fdcoloring, (MatFDColoringFn *)SNESComputeFunction_DMLocal, dmlocalsnes));
161: break;
162: default:
163: SETERRQ(PetscObjectComm((PetscObject)snes), PETSC_ERR_SUP, "No support for coloring type '%s'", ISColoringTypes[dm->coloringtype]);
164: }
165: PetscCall(PetscObjectSetOptionsPrefix((PetscObject)fdcoloring, ((PetscObject)dm)->prefix));
166: PetscCall(MatFDColoringSetFromOptions(fdcoloring));
167: PetscCall(MatFDColoringSetUp(B, coloring, fdcoloring));
168: PetscCall(PetscObjectCompose((PetscObject)dm, "DMDASNES_FDCOLORING", (PetscObject)fdcoloring));
169: PetscCall(PetscObjectDereference((PetscObject)fdcoloring));
171: /* The following breaks an ugly reference counting loop that deserves a paragraph. MatFDColoringApply() will call
172: * VecDuplicate() with the state Vec and store inside the MatFDColoring. This Vec will duplicate the Vec, but the
173: * MatFDColoring is composed with the DM. We dereference the DM here so that the reference count will eventually
174: * drop to 0. Note the code in DMDestroy() that exits early for a negative reference count. That code path will be
175: * taken when the PetscObjectList for the Vec inside MatFDColoring is destroyed.
176: */
177: PetscCall(PetscObjectDereference((PetscObject)dm));
178: }
179: PetscCall(MatFDColoringApply(B, fdcoloring, X, snes));
180: }
181: /* This will be redundant if the user called both, but it's too common to forget. */
182: if (A != B) {
183: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
184: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
185: }
186: PetscFunctionReturn(PETSC_SUCCESS);
187: }
189: /*@
190: DMSNESSetObjectiveLocal - set a local objective evaluation function. This function is called with local vector
191: containing the local vector information PLUS ghost point information. It should compute a result for all local
192: elements and `DMSNES` will automatically accumulate the overlapping values.
194: Logically Collective
196: Input Parameters:
197: + dm - `DM` to associate callback with
198: . func - local objective evaluation
199: - ctx - optional context for local objective function evaluation
201: Calling sequence of func:
202: + dm - the `DM`
203: . x - the location where the objective is to be evaluated
204: . obj - the value of the objective function
205: - ctx - optional context for the local objective function evaluation
207: Level: advanced
209: .seealso: `DMSNESSetFunctionLocal()`, `DMSNESSetJacobianLocal()`
210: @*/
211: PetscErrorCode DMSNESSetObjectiveLocal(DM dm, PetscErrorCode (*func)(DM dm, Vec x, PetscReal *obj, PetscCtx ctx), PetscCtx ctx)
212: {
213: DMSNES sdm;
214: DMSNES_Local *dmlocalsnes;
216: PetscFunctionBegin;
218: PetscCall(DMGetDMSNESWrite(dm, &sdm));
219: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
221: dmlocalsnes->objectivelocal = func;
222: dmlocalsnes->objectivelocalctx = ctx;
224: PetscCall(DMSNESSetObjective(dm, SNESComputeObjective_DMLocal, dmlocalsnes));
225: PetscFunctionReturn(PETSC_SUCCESS);
226: }
228: /*@
229: DMSNESSetFunctionLocal - set a local residual evaluation function. This function is called with local vector
230: containing the local vector information PLUS ghost point information. It should compute a result for all local
231: elements and `DMSNES` will automatically accumulate the overlapping values.
233: Logically Collective
235: Input Parameters:
236: + dm - `DM` to associate callback with
237: . func - local residual evaluation
238: - ctx - optional context for local residual evaluation
240: Calling sequence of `func`:
241: + dm - `DM` for the function
242: . x - vector to state at which to evaluate residual
243: . f - vector to hold the function evaluation
244: - ctx - optional context passed above
246: Level: advanced
248: .seealso: [](ch_snes), `DMSNESSetFunction()`, `DMSNESSetJacobianLocal()`
249: @*/
250: PetscErrorCode DMSNESSetFunctionLocal(DM dm, PetscErrorCode (*func)(DM dm, Vec x, Vec f, PetscCtx ctx), PetscCtx ctx) PeNSS
251: {
252: DMSNES sdm;
253: DMSNES_Local *dmlocalsnes;
255: PetscFunctionBegin;
257: PetscCall(DMGetDMSNESWrite(dm, &sdm));
258: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
260: dmlocalsnes->residuallocal = func;
261: dmlocalsnes->residuallocalctx = ctx;
263: PetscCall(DMSNESSetFunction(dm, SNESComputeFunction_DMLocal, dmlocalsnes));
264: if (!sdm->ops->computejacobian) { /* Call us for the Jacobian too, can be overridden by the user. */
265: PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobian_DMLocal, dmlocalsnes));
266: }
267: PetscFunctionReturn(PETSC_SUCCESS);
268: }
270: /*@
271: DMSNESSetBoundaryLocal - set a function to insert, for example, essential boundary conditions into a ghosted solution vector
273: Logically Collective
275: Input Parameters:
276: + dm - `DM` to associate callback with
277: . func - local boundary value evaluation
278: - ctx - optional context for local boundary value evaluation
280: Calling sequence of `func`:
281: + dm - the `DM` context
282: . X - ghosted solution vector, appropriate locations (such as essential boundary condition nodes) should be filled
283: - ctx - option context passed in `DMSNESSetBoundaryLocal()`
285: Level: advanced
287: .seealso: [](ch_snes), `DMSNESSetObjectiveLocal()`, `DMSNESSetFunctionLocal()`, `DMSNESSetJacobianLocal()`
288: @*/
289: PetscErrorCode DMSNESSetBoundaryLocal(DM dm, PetscErrorCode (*func)(DM dm, Vec X, PetscCtx ctx), PetscCtx ctx)
290: {
291: DMSNES sdm;
292: DMSNES_Local *dmlocalsnes;
294: PetscFunctionBegin;
296: PetscCall(DMGetDMSNESWrite(dm, &sdm));
297: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
299: dmlocalsnes->boundarylocal = func;
300: dmlocalsnes->boundarylocalctx = ctx;
301: PetscFunctionReturn(PETSC_SUCCESS);
302: }
304: /*@
305: DMSNESSetJacobianLocal - set a local Jacobian evaluation function
307: Logically Collective
309: Input Parameters:
310: + dm - `DM` to associate callback with
311: . func - local Jacobian evaluation
312: - ctx - optional context for local Jacobian evaluation
314: Calling sequence of `func`:
315: + dm - the `DM` context
316: . X - current solution vector (ghosted or not?)
317: . J - the Jacobian
318: . Jp - approximate Jacobian used to compute the preconditioner, often `J`
319: - ctx - a user provided context
321: Level: advanced
323: .seealso: [](ch_snes), `DMSNESSetObjectiveLocal()`, `DMSNESSetFunctionLocal()`, `DMSNESSetBoundaryLocal()`
324: @*/
325: PetscErrorCode DMSNESSetJacobianLocal(DM dm, PetscErrorCode (*func)(DM dm, Vec X, Mat J, Mat Jp, PetscCtx ctx), PetscCtx ctx) PeNSS
326: {
327: DMSNES sdm;
328: DMSNES_Local *dmlocalsnes;
330: PetscFunctionBegin;
332: PetscCall(DMGetDMSNESWrite(dm, &sdm));
333: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
335: dmlocalsnes->jacobianlocal = func;
336: dmlocalsnes->jacobianlocalctx = ctx;
338: PetscCall(DMSNESSetJacobian(dm, SNESComputeJacobian_DMLocal, dmlocalsnes));
339: PetscFunctionReturn(PETSC_SUCCESS);
340: }
342: /*@
343: DMSNESGetObjectiveLocal - get the local objective evaluation function information set with `DMSNESSetObjectiveLocal()`.
345: Not Collective
347: Input Parameter:
348: . dm - `DM` with the associated callback
350: Output Parameters:
351: + func - local objective evaluation
352: - ctx - context for local residual evaluation
354: Calling sequence of func:
355: + dm - the `DM`
356: . x - the location where the objective function is to be evaluated
357: . obj - the value of the objective function
358: - ctx - optional context for the local objective function evaluation
360: Level: beginner
362: .seealso: `DMSNESSetObjective()`, `DMSNESSetObjectiveLocal()`, `DMSNESSetFunctionLocal()`
363: @*/
364: PetscErrorCode DMSNESGetObjectiveLocal(DM dm, PetscErrorCode (**func)(DM dm, Vec x, PetscReal *obj, PetscCtx ctx), PetscCtxRt ctx) PeNSS
365: {
366: DMSNES sdm;
367: DMSNES_Local *dmlocalsnes;
369: PetscFunctionBegin;
371: PetscCall(DMGetDMSNES(dm, &sdm));
372: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
373: if (func) *func = dmlocalsnes->objectivelocal;
374: if (ctx) *(void **)ctx = dmlocalsnes->objectivelocalctx;
375: PetscFunctionReturn(PETSC_SUCCESS);
376: }
378: /*@
379: DMSNESGetFunctionLocal - get the local residual evaluation function information set with `DMSNESSetFunctionLocal()`.
381: Not Collective
383: Input Parameter:
384: . dm - `DM` with the associated callback
386: Output Parameters:
387: + func - local residual evaluation
388: - ctx - context for local residual evaluation
390: Calling sequence of `func`:
391: + dm - `DM` for the function
392: . x - vector to state at which to evaluate residual
393: . f - vector to hold the function evaluation
394: - ctx - optional context passed above
396: Level: beginner
398: .seealso: [](ch_snes), `DMSNESSetFunction()`, `DMSNESSetFunctionLocal()`, `DMSNESSetJacobianLocal()`
399: @*/
400: PetscErrorCode DMSNESGetFunctionLocal(DM dm, PetscErrorCode (**func)(DM dm, Vec x, Vec f, PetscCtx ctx), PetscCtxRt ctx)
401: {
402: DMSNES sdm;
403: DMSNES_Local *dmlocalsnes;
405: PetscFunctionBegin;
407: PetscCall(DMGetDMSNES(dm, &sdm));
408: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
409: if (func) *func = dmlocalsnes->residuallocal;
410: if (ctx) *(void **)ctx = dmlocalsnes->residuallocalctx;
411: PetscFunctionReturn(PETSC_SUCCESS);
412: }
414: /*@
415: DMSNESGetBoundaryLocal - get the local boundary value function set with `DMSNESSetBoundaryLocal()`.
417: Not Collective
419: Input Parameter:
420: . dm - `DM` with the associated callback
422: Output Parameters:
423: + func - local boundary value evaluation
424: - ctx - context for local boundary value evaluation
426: Calling sequence of `func`:
427: + dm - the `DM` context
428: . X - ghosted solution vector, appropriate locations (such as essential boundary condition nodes) should be filled
429: - ctx - option context passed in `DMSNESSetBoundaryLocal()`
431: Level: intermediate
433: .seealso: [](ch_snes), `DMSNESSetFunctionLocal()`, `DMSNESSetBoundaryLocal()`, `DMSNESSetJacobianLocal()`
434: @*/
435: PetscErrorCode DMSNESGetBoundaryLocal(DM dm, PetscErrorCode (**func)(DM dm, Vec X, PetscCtx ctx), PetscCtxRt ctx)
436: {
437: DMSNES sdm;
438: DMSNES_Local *dmlocalsnes;
440: PetscFunctionBegin;
442: PetscCall(DMGetDMSNES(dm, &sdm));
443: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
444: if (func) *func = dmlocalsnes->boundarylocal;
445: if (ctx) *(void **)ctx = dmlocalsnes->boundarylocalctx;
446: PetscFunctionReturn(PETSC_SUCCESS);
447: }
449: /*@
450: DMSNESGetJacobianLocal - the local Jacobian evaluation function set with `DMSNESSetJacobianLocal()`.
452: Logically Collective
454: Input Parameter:
455: . dm - `DM` with the associated callback
457: Output Parameters:
458: + func - local Jacobian evaluation
459: - ctx - context for local Jacobian evaluation
461: Calling sequence of `func`:
462: + dm - the `DM` context
463: . X - current solution vector (ghosted or not?)
464: . J - the Jacobian
465: . Jp - approximate Jacobian used to compute the preconditioner, often `J`
466: - ctx - a user provided context
468: Level: beginner
470: .seealso: [](ch_snes), `DMSNESSetJacobianLocal()`, `DMSNESSetJacobian()`
471: @*/
472: PetscErrorCode DMSNESGetJacobianLocal(DM dm, PetscErrorCode (**func)(DM dm, Vec X, Mat J, Mat Jp, PetscCtx ctx), PetscCtxRt ctx)
473: {
474: DMSNES sdm;
475: DMSNES_Local *dmlocalsnes;
477: PetscFunctionBegin;
479: PetscCall(DMGetDMSNES(dm, &sdm));
480: PetscCall(DMLocalSNESGetContext(dm, sdm, &dmlocalsnes));
481: if (func) *func = dmlocalsnes->jacobianlocal;
482: if (ctx) *(void **)ctx = dmlocalsnes->jacobianlocalctx;
483: PetscFunctionReturn(PETSC_SUCCESS);
484: }