Actual source code: dmts.c

  1: #include <petsc/private/tsimpl.h>
  2: #include <petsc/private/dmimpl.h>

  4: static PetscErrorCode DMTSUnsetRHSFunctionContext_DMTS(DMTS tsdm)
  5: {
  6:   PetscFunctionBegin;
  7:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "rhs function ctx", NULL));
  8:   tsdm->rhsfunctionctxcontainer = NULL;
  9:   PetscFunctionReturn(PETSC_SUCCESS);
 10: }

 12: static PetscErrorCode DMTSUnsetRHSJacobianContext_DMTS(DMTS tsdm)
 13: {
 14:   PetscFunctionBegin;
 15:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "rhs jacobian ctx", NULL));
 16:   tsdm->rhsjacobianctxcontainer = NULL;
 17:   PetscFunctionReturn(PETSC_SUCCESS);
 18: }

 20: static PetscErrorCode DMTSUnsetIFunctionContext_DMTS(DMTS tsdm)
 21: {
 22:   PetscFunctionBegin;
 23:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "ifunction ctx", NULL));
 24:   tsdm->ifunctionctxcontainer = NULL;
 25:   PetscFunctionReturn(PETSC_SUCCESS);
 26: }

 28: static PetscErrorCode DMTSUnsetIJacobianContext_DMTS(DMTS tsdm)
 29: {
 30:   PetscFunctionBegin;
 31:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "ijacobian ctx", NULL));
 32:   tsdm->ijacobianctxcontainer = NULL;
 33:   PetscFunctionReturn(PETSC_SUCCESS);
 34: }

 36: static PetscErrorCode DMTSUnsetMassMatrixContext_DMTS(DMTS tsdm)
 37: {
 38:   PetscFunctionBegin;
 39:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "massmatrix ctx", NULL));
 40:   tsdm->massmatrixctxcontainer = NULL;
 41:   PetscFunctionReturn(PETSC_SUCCESS);
 42: }

 44: static PetscErrorCode DMTSUnsetI2FunctionContext_DMTS(DMTS tsdm)
 45: {
 46:   PetscFunctionBegin;
 47:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "i2function ctx", NULL));
 48:   tsdm->i2functionctxcontainer = NULL;
 49:   PetscFunctionReturn(PETSC_SUCCESS);
 50: }

 52: static PetscErrorCode DMTSUnsetI2JacobianContext_DMTS(DMTS tsdm)
 53: {
 54:   PetscFunctionBegin;
 55:   PetscCall(PetscObjectCompose((PetscObject)tsdm, "i2jacobian ctx", NULL));
 56:   tsdm->i2jacobianctxcontainer = NULL;
 57:   PetscFunctionReturn(PETSC_SUCCESS);
 58: }

 60: static PetscErrorCode DMTSDestroy(DMTS *kdm)
 61: {
 62:   PetscFunctionBegin;
 63:   if (!*kdm) PetscFunctionReturn(PETSC_SUCCESS);
 65:   if (--((PetscObject)*kdm)->refct > 0) {
 66:     *kdm = NULL;
 67:     PetscFunctionReturn(PETSC_SUCCESS);
 68:   }
 69:   PetscCall(DMTSUnsetRHSFunctionContext_DMTS(*kdm));
 70:   PetscCall(DMTSUnsetRHSJacobianContext_DMTS(*kdm));
 71:   PetscCall(DMTSUnsetIFunctionContext_DMTS(*kdm));
 72:   PetscCall(DMTSUnsetIJacobianContext_DMTS(*kdm));
 73:   PetscCall(DMTSUnsetMassMatrixContext_DMTS(*kdm));
 74:   PetscCall(DMTSUnsetI2FunctionContext_DMTS(*kdm));
 75:   PetscCall(DMTSUnsetI2JacobianContext_DMTS(*kdm));
 76:   PetscTryTypeMethod(*kdm, destroy);
 77:   PetscCall(PetscHeaderDestroy(kdm));
 78:   PetscFunctionReturn(PETSC_SUCCESS);
 79: }

 81: PetscErrorCode DMTSLoad(DMTS kdm, PetscViewer viewer)
 82: {
 83:   PetscFunctionBegin;
 84:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ifunction, 1, NULL, PETSC_FUNCTION));
 85:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ifunctionview, 1, NULL, PETSC_FUNCTION));
 86:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ifunctionload, 1, NULL, PETSC_FUNCTION));
 87:   if (kdm->ops->ifunctionload) {
 88:     PetscCtx ctx;

 90:     PetscCall(PetscContainerGetPointer(kdm->ifunctionctxcontainer, &ctx));
 91:     PetscCall((*kdm->ops->ifunctionload)(&ctx, viewer));
 92:   }
 93:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ijacobian, 1, NULL, PETSC_FUNCTION));
 94:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ijacobianview, 1, NULL, PETSC_FUNCTION));
 95:   PetscCall(PetscViewerBinaryRead(viewer, &kdm->ops->ijacobianload, 1, NULL, PETSC_FUNCTION));
 96:   if (kdm->ops->ijacobianload) {
 97:     PetscCtx ctx;

 99:     PetscCall(PetscContainerGetPointer(kdm->ijacobianctxcontainer, &ctx));
100:     PetscCall((*kdm->ops->ijacobianload)(&ctx, viewer));
101:   }
102:   PetscFunctionReturn(PETSC_SUCCESS);
103: }

105: PetscErrorCode DMTSView(DMTS kdm, PetscViewer viewer)
106: {
107:   PetscBool isbinary;

109:   PetscFunctionBegin;
110:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
111:   if (isbinary) {
112:     struct {
113:       TSIFunctionFn *ifunction;
114:     } funcstruct;
115:     struct {
116:       PetscErrorCode (*ifunctionview)(void *, PetscViewer);
117:     } funcviewstruct;
118:     struct {
119:       PetscErrorCode (*ifunctionload)(void **, PetscViewer);
120:     } funcloadstruct;
121:     struct {
122:       TSIJacobianFn *ijacobian;
123:     } jacstruct;
124:     struct {
125:       PetscErrorCode (*ijacobianview)(void *, PetscViewer);
126:     } jacviewstruct;
127:     struct {
128:       PetscErrorCode (*ijacobianload)(void **, PetscViewer);
129:     } jacloadstruct;

131:     funcstruct.ifunction         = kdm->ops->ifunction;
132:     funcviewstruct.ifunctionview = kdm->ops->ifunctionview;
133:     funcloadstruct.ifunctionload = kdm->ops->ifunctionload;
134:     PetscCall(PetscViewerBinaryWrite(viewer, &funcstruct, 1, PETSC_FUNCTION));
135:     PetscCall(PetscViewerBinaryWrite(viewer, &funcviewstruct, 1, PETSC_FUNCTION));
136:     PetscCall(PetscViewerBinaryWrite(viewer, &funcloadstruct, 1, PETSC_FUNCTION));
137:     if (kdm->ops->ifunctionview) {
138:       PetscCtx ctx;

140:       PetscCall(PetscContainerGetPointer(kdm->ifunctionctxcontainer, &ctx));
141:       PetscCall((*kdm->ops->ifunctionview)(ctx, viewer));
142:     }
143:     jacstruct.ijacobian         = kdm->ops->ijacobian;
144:     jacviewstruct.ijacobianview = kdm->ops->ijacobianview;
145:     jacloadstruct.ijacobianload = kdm->ops->ijacobianload;
146:     PetscCall(PetscViewerBinaryWrite(viewer, &jacstruct, 1, PETSC_FUNCTION));
147:     PetscCall(PetscViewerBinaryWrite(viewer, &jacviewstruct, 1, PETSC_FUNCTION));
148:     PetscCall(PetscViewerBinaryWrite(viewer, &jacloadstruct, 1, PETSC_FUNCTION));
149:     if (kdm->ops->ijacobianview) {
150:       PetscCtx ctx;

152:       PetscCall(PetscContainerGetPointer(kdm->ijacobianctxcontainer, &ctx));
153:       PetscCall((*kdm->ops->ijacobianview)(ctx, viewer));
154:     }
155:   }
156:   PetscFunctionReturn(PETSC_SUCCESS);
157: }

159: static PetscErrorCode DMTSCreate(MPI_Comm comm, DMTS *kdm)
160: {
161:   PetscFunctionBegin;
162:   PetscCall(TSInitializePackage());
163:   PetscCall(PetscHeaderCreate(*kdm, DMTS_CLASSID, "DMTS", "DMTS", "DMTS", comm, DMTSDestroy, DMTSView));
164:   PetscFunctionReturn(PETSC_SUCCESS);
165: }

167: /* Attaches the DMTS to the coarse level.
168:  * Under what conditions should we copy versus duplicate?
169:  */
170: static PetscErrorCode DMCoarsenHook_DMTS(DM dm, DM dmc, PetscCtx ctx)
171: {
172:   PetscFunctionBegin;
173:   PetscCall(DMCopyDMTS(dm, dmc));
174:   PetscFunctionReturn(PETSC_SUCCESS);
175: }

177: /* This could restrict auxiliary information to the coarse level.
178:  */
179: static PetscErrorCode DMRestrictHook_DMTS(DM dm, Mat Restrict, Vec rscale, Mat Inject, DM dmc, PetscCtx ctx)
180: {
181:   PetscFunctionBegin;
182:   PetscFunctionReturn(PETSC_SUCCESS);
183: }

185: static PetscErrorCode DMSubDomainHook_DMTS(DM dm, DM subdm, PetscCtx ctx)
186: {
187:   PetscFunctionBegin;
188:   PetscCall(DMCopyDMTS(dm, subdm));
189:   PetscFunctionReturn(PETSC_SUCCESS);
190: }

192: /* This could restrict auxiliary information to the coarse level.
193:  */
194: static PetscErrorCode DMSubDomainRestrictHook_DMTS(DM dm, VecScatter gscat, VecScatter lscat, DM subdm, PetscCtx ctx)
195: {
196:   PetscFunctionBegin;
197:   PetscFunctionReturn(PETSC_SUCCESS);
198: }

200: /*@
201:   DMTSCopy - copies the information in a `DMTS` to another `DMTS`

203:   Not Collective

205:   Input Parameters:
206: + kdm  - Original `DMTS`
207: - nkdm - `DMTS` to receive the data, should have been created with `DMTSCreate()`

209:   Level: developer

211: .seealso: [](ch_ts), `DMTSCreate()`, `DMTSDestroy()`
212: @*/
213: PetscErrorCode DMTSCopy(DMTS kdm, DMTS nkdm)
214: {
215:   PetscFunctionBegin;
218:   nkdm->ops->rhsfunction = kdm->ops->rhsfunction;
219:   nkdm->ops->rhsjacobian = kdm->ops->rhsjacobian;
220:   nkdm->ops->ifunction   = kdm->ops->ifunction;
221:   nkdm->ops->ijacobian   = kdm->ops->ijacobian;
222:   nkdm->ops->massmatrix  = kdm->ops->massmatrix;
223:   nkdm->ops->i2function  = kdm->ops->i2function;
224:   nkdm->ops->i2jacobian  = kdm->ops->i2jacobian;
225:   nkdm->ops->solution    = kdm->ops->solution;
226:   nkdm->ops->destroy     = kdm->ops->destroy;
227:   nkdm->ops->duplicate   = kdm->ops->duplicate;

229:   nkdm->solutionctx             = kdm->solutionctx;
230:   nkdm->rhsfunctionctxcontainer = kdm->rhsfunctionctxcontainer;
231:   nkdm->rhsjacobianctxcontainer = kdm->rhsjacobianctxcontainer;
232:   nkdm->ifunctionctxcontainer   = kdm->ifunctionctxcontainer;
233:   nkdm->ijacobianctxcontainer   = kdm->ijacobianctxcontainer;
234:   nkdm->massmatrixctxcontainer  = kdm->massmatrixctxcontainer;
235:   nkdm->i2functionctxcontainer  = kdm->i2functionctxcontainer;
236:   nkdm->i2jacobianctxcontainer  = kdm->i2jacobianctxcontainer;
237:   if (nkdm->rhsfunctionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "rhs function ctx", (PetscObject)nkdm->rhsfunctionctxcontainer));
238:   if (nkdm->rhsjacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "rhs jacobian ctx", (PetscObject)nkdm->rhsjacobianctxcontainer));
239:   if (nkdm->ifunctionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "ifunction ctx", (PetscObject)nkdm->ifunctionctxcontainer));
240:   if (nkdm->ijacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "ijacobian ctx", (PetscObject)nkdm->ijacobianctxcontainer));
241:   if (nkdm->massmatrixctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "massmatrix ctx", (PetscObject)nkdm->massmatrixctxcontainer));
242:   if (nkdm->i2functionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "i2function ctx", (PetscObject)nkdm->i2functionctxcontainer));
243:   if (nkdm->i2jacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm, "i2jacobian ctx", (PetscObject)nkdm->i2jacobianctxcontainer));

245:   nkdm->data = kdm->data;

247:   /*
248:   nkdm->fortran_func_pointers[0] = kdm->fortran_func_pointers[0];
249:   nkdm->fortran_func_pointers[1] = kdm->fortran_func_pointers[1];
250:   nkdm->fortran_func_pointers[2] = kdm->fortran_func_pointers[2];
251:   */

253:   /* implementation specific copy hooks */
254:   PetscTryTypeMethod(kdm, duplicate, nkdm);
255:   PetscFunctionReturn(PETSC_SUCCESS);
256: }

258: /*@
259:   DMGetDMTS - get read-only private `DMTS` context from a `DM`

261:   Not Collective

263:   Input Parameter:
264: . dm - `DM` to be used with `TS`

266:   Output Parameter:
267: . tsdm - private `DMTS` context

269:   Level: developer

271:   Notes:
272:   Use `DMGetDMTSWrite()` if write access is needed. The `DMTSSetXXX()` API should be used wherever possible.

274: .seealso: [](ch_ts), `DMTS`, `DMGetDMTSWrite()`
275: @*/
276: PetscErrorCode DMGetDMTS(DM dm, DMTS *tsdm)
277: {
278:   PetscFunctionBegin;
280:   *tsdm = (DMTS)dm->dmts;
281:   if (!*tsdm) {
282:     PetscCall(PetscInfo(dm, "Creating new DMTS\n"));
283:     PetscCall(DMTSCreate(PetscObjectComm((PetscObject)dm), tsdm));
284:     dm->dmts            = (PetscObject)*tsdm;
285:     (*tsdm)->originaldm = dm;
286:     PetscCall(DMCoarsenHookAdd(dm, DMCoarsenHook_DMTS, DMRestrictHook_DMTS, NULL));
287:     PetscCall(DMSubDomainHookAdd(dm, DMSubDomainHook_DMTS, DMSubDomainRestrictHook_DMTS, NULL));
288:   }
289:   PetscFunctionReturn(PETSC_SUCCESS);
290: }

292: /*@
293:   DMGetDMTSWrite - get write access to private `DMTS` context from a `DM`

295:   Not Collective

297:   Input Parameter:
298: . dm - `DM` to be used with `TS`

300:   Output Parameter:
301: . tsdm - private `DMTS` context

303:   Level: developer

305: .seealso: [](ch_ts), `DMTS`, `DMGetDMTS()`
306: @*/
307: PetscErrorCode DMGetDMTSWrite(DM dm, DMTS *tsdm)
308: {
309:   DMTS sdm;

311:   PetscFunctionBegin;
313:   PetscCall(DMGetDMTS(dm, &sdm));
314:   PetscCheck(sdm->originaldm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DMTS has a NULL originaldm");
315:   if (sdm->originaldm != dm) { /* Copy on write */
316:     DMTS oldsdm = sdm;
317:     PetscCall(PetscInfo(dm, "Copying DMTS due to write\n"));
318:     PetscCall(DMTSCreate(PetscObjectComm((PetscObject)dm), &sdm));
319:     PetscCall(DMTSCopy(oldsdm, sdm));
320:     PetscCall(DMTSDestroy((DMTS *)&dm->dmts));
321:     dm->dmts        = (PetscObject)sdm;
322:     sdm->originaldm = dm;
323:   }
324:   *tsdm = sdm;
325:   PetscFunctionReturn(PETSC_SUCCESS);
326: }

328: /*@
329:   DMCopyDMTS - copies a `DMTS` context to a new `DM`

331:   Logically Collective

333:   Input Parameters:
334: + dmsrc  - `DM` to obtain context from
335: - dmdest - `DM` to add context to

337:   Level: developer

339:   Note:
340:   The context is copied by reference. This function does not ensure that a context exists.

342: .seealso: [](ch_ts), `DMTS`, `DMGetDMTS()`, `TSSetDM()`
343: @*/
344: PetscErrorCode DMCopyDMTS(DM dmsrc, DM dmdest)
345: {
346:   PetscFunctionBegin;
349:   PetscCall(DMTSDestroy((DMTS *)&dmdest->dmts));
350:   dmdest->dmts = dmsrc->dmts;
351:   PetscCall(PetscObjectReference(dmdest->dmts));
352:   PetscCall(DMCoarsenHookAdd(dmdest, DMCoarsenHook_DMTS, DMRestrictHook_DMTS, NULL));
353:   PetscCall(DMSubDomainHookAdd(dmdest, DMSubDomainHook_DMTS, DMSubDomainRestrictHook_DMTS, NULL));
354:   PetscFunctionReturn(PETSC_SUCCESS);
355: }

357: /*@
358:   DMTSSetIFunction - set `TS` implicit function evaluation function into a `DMTS`

360:   Not Collective

362:   Input Parameters:
363: + dm   - `DM` to be used with `TS`
364: . func - function evaluating f(t,u,u_t)
365: - ctx  - context for residual evaluation

367:   Level: developer

369:   Note:
370:   `TSSetIFunction()` is normally used, but it calls this function internally because the application context is actually
371:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
372:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

374: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `TSIFunctionFn`
375: @*/
376: PetscErrorCode DMTSSetIFunction(DM dm, TSIFunctionFn *func, PetscCtx ctx)
377: {
378:   DMTS tsdm;

380:   PetscFunctionBegin;
382:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
383:   if (func) tsdm->ops->ifunction = func;
384:   if (ctx) {
385:     PetscContainer ctxcontainer;
386:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
387:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
388:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "ifunction ctx", (PetscObject)ctxcontainer));
389:     tsdm->ifunctionctxcontainer = ctxcontainer;
390:     PetscCall(PetscContainerDestroy(&ctxcontainer));
391:   }
392:   PetscFunctionReturn(PETSC_SUCCESS);
393: }

395: /*@
396:   DMTSSetIFunctionContextDestroy - set `TS` implicit evaluation context destroy function into a `DMTS`

398:   Not Collective

400:   Input Parameters:
401: + dm - `DM` to be used with `TS`
402: - f  - implicit evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

404:   Level: developer

406: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetIFunction()`, `TSSetIFunction()`, `PetscCtxDestroyFn`
407: @*/
408: PetscErrorCode DMTSSetIFunctionContextDestroy(DM dm, PetscCtxDestroyFn *f)
409: {
410:   DMTS tsdm;

412:   PetscFunctionBegin;
414:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
415:   if (tsdm->ifunctionctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->ifunctionctxcontainer, f));
416:   PetscFunctionReturn(PETSC_SUCCESS);
417: }

419: PetscErrorCode DMTSUnsetIFunctionContext_Internal(DM dm)
420: {
421:   DMTS tsdm;

423:   PetscFunctionBegin;
425:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
426:   PetscCall(DMTSUnsetIFunctionContext_DMTS(tsdm));
427:   PetscFunctionReturn(PETSC_SUCCESS);
428: }

430: /*@
431:   DMTSGetIFunction - get `TS` implicit residual evaluation function from a `DMTS`

433:   Not Collective

435:   Input Parameter:
436: . dm - `DM` to be used with `TS`

438:   Output Parameters:
439: + func - function evaluation function, for calling sequence see `TSIFunctionFn`
440: - ctx  - context for residual evaluation

442:   Level: developer

444:   Note:
445:   `TSGetIFunction()` is normally used, but it calls this function internally because the application context is actually
446:   associated with the `DM`.

448: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `DMTSSetIFunction()`, `TSIFunctionFn`
449: @*/
450: PetscErrorCode DMTSGetIFunction(DM dm, TSIFunctionFn **func, PetscCtxRt ctx)
451: {
452:   DMTS tsdm;

454:   PetscFunctionBegin;
456:   PetscCall(DMGetDMTS(dm, &tsdm));
457:   if (func) *func = tsdm->ops->ifunction;
458:   if (ctx) {
459:     if (tsdm->ifunctionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->ifunctionctxcontainer, ctx));
460:     else *(void **)ctx = NULL;
461:   }
462:   PetscFunctionReturn(PETSC_SUCCESS);
463: }

465: /*@
466:   DMTSSetI2Function - set `TS` implicit function evaluation function for 2nd order systems into a `TSDM`

468:   Not Collective

470:   Input Parameters:
471: + dm  - `DM` to be used with `TS`
472: . fun - function evaluation routine
473: - ctx - context for residual evaluation

475:   Level: developer

477:   Note:
478:   `TSSetI2Function()` is normally used, but it calls this function internally because the application context is actually
479:   associated with the `DM`.

481: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSSetI2Function()`
482: @*/
483: PetscErrorCode DMTSSetI2Function(DM dm, TSI2FunctionFn *fun, PetscCtx ctx)
484: {
485:   DMTS tsdm;

487:   PetscFunctionBegin;
489:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
490:   if (fun) tsdm->ops->i2function = fun;
491:   if (ctx) {
492:     PetscContainer ctxcontainer;
493:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
494:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
495:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "i2function ctx", (PetscObject)ctxcontainer));
496:     tsdm->i2functionctxcontainer = ctxcontainer;
497:     PetscCall(PetscContainerDestroy(&ctxcontainer));
498:   }
499:   PetscFunctionReturn(PETSC_SUCCESS);
500: }

502: /*@
503:   DMTSSetI2FunctionContextDestroy - set `TS` implicit evaluation for 2nd order systems context destroy into a `DMTS`

505:   Not Collective

507:   Input Parameters:
508: + dm - `DM` to be used with `TS`
509: - f  - implicit evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

511:   Level: developer

513:   Note:
514:   `TSSetI2FunctionContextDestroy()` is normally used, but it calls this function internally because the application context is actually
515:   associated with the `DM`.

517: .seealso: [](ch_ts), `DMTS`, `TSSetI2FunctionContextDestroy()`, `DMTSSetI2Function()`, `TSSetI2Function()`
518: @*/
519: PetscErrorCode DMTSSetI2FunctionContextDestroy(DM dm, PetscCtxDestroyFn *f)
520: {
521:   DMTS tsdm;

523:   PetscFunctionBegin;
525:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
526:   if (tsdm->i2functionctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->i2functionctxcontainer, f));
527:   PetscFunctionReturn(PETSC_SUCCESS);
528: }

530: PetscErrorCode DMTSUnsetI2FunctionContext_Internal(DM dm)
531: {
532:   DMTS tsdm;

534:   PetscFunctionBegin;
536:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
537:   PetscCall(DMTSUnsetI2FunctionContext_DMTS(tsdm));
538:   PetscFunctionReturn(PETSC_SUCCESS);
539: }

541: /*@
542:   DMTSGetI2Function - get `TS` implicit residual evaluation function for 2nd order systems from a `DMTS`

544:   Not Collective

546:   Input Parameter:
547: . dm - `DM` to be used with `TS`

549:   Output Parameters:
550: + fun - function evaluation function, for calling sequence see `TSSetI2Function()`
551: - ctx - context for residual evaluation

553:   Level: developer

555:   Note:
556:   `TSGetI2Function()` is normally used, but it calls this function internally because the application context is actually
557:   associated with the `DM`.

559: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetI2Function()`, `TSGetI2Function()`
560: @*/
561: PetscErrorCode DMTSGetI2Function(DM dm, TSI2FunctionFn **fun, PetscCtxRt ctx)
562: {
563:   DMTS tsdm;

565:   PetscFunctionBegin;
567:   PetscCall(DMGetDMTS(dm, &tsdm));
568:   if (fun) *fun = tsdm->ops->i2function;
569:   if (ctx) {
570:     if (tsdm->i2functionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->i2functionctxcontainer, ctx));
571:     else *(void **)ctx = NULL;
572:   }
573:   PetscFunctionReturn(PETSC_SUCCESS);
574: }

576: /*@
577:   DMTSSetI2Jacobian - set `TS` implicit Jacobian evaluation function for 2nd order systems from a `DMTS`

579:   Not Collective

581:   Input Parameters:
582: + dm  - `DM` to be used with `TS`
583: . jac - Jacobian evaluation routine
584: - ctx - context for Jacobian evaluation

586:   Level: developer

588:   Note:
589:   `TSSetI2Jacobian()` is normally used, but it calls this function internally because the application context is actually
590:   associated with the `DM`.

592: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSI2JacobianFn`, `TSSetI2Jacobian()`
593: @*/
594: PetscErrorCode DMTSSetI2Jacobian(DM dm, TSI2JacobianFn *jac, PetscCtx ctx)
595: {
596:   DMTS tsdm;

598:   PetscFunctionBegin;
600:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
601:   if (jac) tsdm->ops->i2jacobian = jac;
602:   if (ctx) {
603:     PetscContainer ctxcontainer;
604:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
605:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
606:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "i2jacobian ctx", (PetscObject)ctxcontainer));
607:     tsdm->i2jacobianctxcontainer = ctxcontainer;
608:     PetscCall(PetscContainerDestroy(&ctxcontainer));
609:   }
610:   PetscFunctionReturn(PETSC_SUCCESS);
611: }

613: /*@
614:   DMTSSetI2JacobianContextDestroy - set `TS` implicit Jacobian evaluation for 2nd order systems context destroy function into a `DMTS`

616:   Not Collective

618:   Input Parameters:
619: + dm - `DM` to be used with `TS`
620: - f  - implicit Jacobian evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

622:   Level: developer

624:   Note:
625:   Normally `TSSetI2JacobianContextDestroy()` is used

627: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSSetI2JacobianContextDestroy()`, `DMTSSetI2Jacobian()`, `TSSetI2Jacobian()`
628: @*/
629: PetscErrorCode DMTSSetI2JacobianContextDestroy(DM dm, PetscCtxDestroyFn *f)
630: {
631:   DMTS tsdm;

633:   PetscFunctionBegin;
635:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
636:   if (tsdm->i2jacobianctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->i2jacobianctxcontainer, f));
637:   PetscFunctionReturn(PETSC_SUCCESS);
638: }

640: PetscErrorCode DMTSUnsetI2JacobianContext_Internal(DM dm)
641: {
642:   DMTS tsdm;

644:   PetscFunctionBegin;
646:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
647:   PetscCall(DMTSUnsetI2JacobianContext_DMTS(tsdm));
648:   PetscFunctionReturn(PETSC_SUCCESS);
649: }

651: /*@
652:   DMTSGetI2Jacobian - get `TS` implicit Jacobian evaluation function for 2nd order systems from a `DMTS`

654:   Not Collective

656:   Input Parameter:
657: . dm - `DM` to be used with `TS`

659:   Output Parameters:
660: + jac - Jacobian evaluation function,  for calling sequence see `TSI2JacobianFn`
661: - ctx - context for Jacobian evaluation

663:   Level: developer

665:   Note:
666:   `TSGetI2Jacobian()` is normally used, but it calls this function internally because the application context is actually
667:   associated with the `DM`.

669: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetI2Jacobian()`, `TSGetI2Jacobian()`, `TSI2JacobianFn`
670: @*/
671: PetscErrorCode DMTSGetI2Jacobian(DM dm, TSI2JacobianFn **jac, PetscCtxRt ctx)
672: {
673:   DMTS tsdm;

675:   PetscFunctionBegin;
677:   PetscCall(DMGetDMTS(dm, &tsdm));
678:   if (jac) *jac = tsdm->ops->i2jacobian;
679:   if (ctx) {
680:     if (tsdm->i2jacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->i2jacobianctxcontainer, ctx));
681:     else *(void **)ctx = NULL;
682:   }
683:   PetscFunctionReturn(PETSC_SUCCESS);
684: }

686: /*@
687:   DMTSSetRHSFunction - set `TS` explicit residual evaluation function into a `DMTS`

689:   Not Collective

691:   Input Parameters:
692: + dm   - `DM` to be used with `TS`
693: . func - RHS function evaluation routine, see `TSRHSFunctionFn` for the calling sequence
694: - ctx  - context for residual evaluation

696:   Level: developer

698:   Note:
699:   `TSSetRHSFunction()` is normally used, but it calls this function internally because the application context is actually
700:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
701:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

703: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSRHSFunctionFn`
704: @*/
705: PetscErrorCode DMTSSetRHSFunction(DM dm, TSRHSFunctionFn *func, PetscCtx ctx)
706: {
707:   DMTS tsdm;

709:   PetscFunctionBegin;
711:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
712:   if (func) tsdm->ops->rhsfunction = func;
713:   if (ctx) {
714:     PetscContainer ctxcontainer;
715:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
716:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
717:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "rhs function ctx", (PetscObject)ctxcontainer));
718:     tsdm->rhsfunctionctxcontainer = ctxcontainer;
719:     PetscCall(PetscContainerDestroy(&ctxcontainer));
720:   }
721:   PetscFunctionReturn(PETSC_SUCCESS);
722: }

724: /*@
725:   DMTSSetRHSFunctionContextDestroy - set `TS` explicit residual evaluation context destroy function into a `DMTS`

727:   Not Collective

729:   Input Parameters:
730: + dm - `DM` to be used with `TS`
731: - f  - explicit evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

733:   Level: developer

735:   Note:
736:   `TSSetRHSFunctionContextDestroy()` is normally used, but it calls this function internally because the application context is actually
737:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
738:   not.

740:   Developer Notes:
741:   If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

743: .seealso: [](ch_ts), `DMTS`, `TSSetRHSFunctionContextDestroy()`, `DMTSSetRHSFunction()`, `TSSetRHSFunction()`
744: @*/
745: PetscErrorCode DMTSSetRHSFunctionContextDestroy(DM dm, PetscCtxDestroyFn *f)
746: {
747:   DMTS tsdm;

749:   PetscFunctionBegin;
751:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
752:   if (tsdm->rhsfunctionctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->rhsfunctionctxcontainer, f));
753:   PetscFunctionReturn(PETSC_SUCCESS);
754: }

756: PetscErrorCode DMTSUnsetRHSFunctionContext_Internal(DM dm)
757: {
758:   DMTS tsdm;

760:   PetscFunctionBegin;
762:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
763:   PetscCall(DMTSUnsetRHSFunctionContext_DMTS(tsdm));
764:   tsdm->rhsfunctionctxcontainer = NULL;
765:   PetscFunctionReturn(PETSC_SUCCESS);
766: }

768: /*@
769:   DMTSSetTransientVariable - sets function to transform from state to transient variables into a `DMTS`

771:   Logically Collective

773:   Input Parameters:
774: + dm   - `DM` to be used with `TS`
775: . tvar - a function that transforms to transient variables, see `TSTransientVariableFn` for the calling sequence
776: - ctx  - a context for tvar

778:   Level: developer

780:   Notes:
781:   Normally `TSSetTransientVariable()` is used

783:   This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`)
784:   can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
785:   well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
786:   C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
787:   evaluated via the chain rule, as in

789:   $$
790:   dF/dP + shift dF/dCdot dC/dP.
791:   $$

793: .seealso: [](ch_ts), `DMTS`, `TS`, `TSBDF`, `TSSetTransientVariable()`, `DMTSGetTransientVariable()`, `DMTSSetIFunction()`, `DMTSSetIJacobian()`, `TSTransientVariableFn`
794: @*/
795: PetscErrorCode DMTSSetTransientVariable(DM dm, TSTransientVariableFn *tvar, PetscCtx ctx)
796: {
797:   DMTS dmts;

799:   PetscFunctionBegin;
801:   PetscCall(DMGetDMTSWrite(dm, &dmts));
802:   dmts->ops->transientvar = tvar;
803:   dmts->transientvarctx   = ctx;
804:   PetscFunctionReturn(PETSC_SUCCESS);
805: }

807: /*@
808:   DMTSGetTransientVariable - gets function to transform from state to transient variables set with `DMTSSetTransientVariable()` from a `TSDM`

810:   Logically Collective

812:   Input Parameter:
813: . dm - `DM` to be used with `TS`

815:   Output Parameters:
816: + tvar - a function that transforms to transient variables, see `TSTransientVariableFn` for the calling sequence
817: - ctx  - a context for tvar

819:   Level: developer

821:   Note:
822:   Normally `TSSetTransientVariable()` is used

824: .seealso: [](ch_ts), `DMTS`, `DM`, `DMTSSetTransientVariable()`, `DMTSGetIFunction()`, `DMTSGetIJacobian()`, `TSTransientVariableFn`
825: @*/
826: PetscErrorCode DMTSGetTransientVariable(DM dm, TSTransientVariableFn **tvar, PetscCtx ctx)
827: {
828:   DMTS dmts;

830:   PetscFunctionBegin;
832:   PetscCall(DMGetDMTS(dm, &dmts));
833:   if (tvar) *tvar = dmts->ops->transientvar;
834:   if (ctx) *(void **)ctx = dmts->transientvarctx;
835:   PetscFunctionReturn(PETSC_SUCCESS);
836: }

838: /*@
839:   DMTSGetSolutionFunction - gets the `TS` solution evaluation function from a `DMTS`

841:   Not Collective

843:   Input Parameter:
844: . dm - `DM` to be used with `TS`

846:   Output Parameters:
847: + func - solution function evaluation function, for calling sequence see `TSSolutionFn`
848: - ctx  - context for solution evaluation

850:   Level: developer

852: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `DMTSSetSolutionFunction()`, `TSSolutionFn`
853: @*/
854: PetscErrorCode DMTSGetSolutionFunction(DM dm, TSSolutionFn **func, PetscCtxRt ctx)
855: {
856:   DMTS tsdm;

858:   PetscFunctionBegin;
860:   PetscCall(DMGetDMTS(dm, &tsdm));
861:   if (func) *func = tsdm->ops->solution;
862:   if (ctx) *(void **)ctx = tsdm->solutionctx;
863:   PetscFunctionReturn(PETSC_SUCCESS);
864: }

866: /*@
867:   DMTSSetSolutionFunction - set `TS` solution evaluation function into a `DMTS`

869:   Not Collective

871:   Input Parameters:
872: + dm   - `DM` to be used with `TS`
873: . func - solution function evaluation routine, for calling sequence see `TSSolutionFn`
874: - ctx  - context for solution evaluation

876:   Level: developer

878:   Note:
879:   `TSSetSolutionFunction()` is normally used, but it calls this function internally because the application context is actually
880:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
881:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

883: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSGetSolutionFunction()`, `TSSolutionFn`
884: @*/
885: PetscErrorCode DMTSSetSolutionFunction(DM dm, TSSolutionFn *func, PetscCtx ctx)
886: {
887:   DMTS tsdm;

889:   PetscFunctionBegin;
891:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
892:   if (func) tsdm->ops->solution = func;
893:   if (ctx) tsdm->solutionctx = ctx;
894:   PetscFunctionReturn(PETSC_SUCCESS);
895: }

897: /*@
898:   DMTSSetForcingFunction - set `TS` forcing function evaluation function into a `DMTS`

900:   Not Collective

902:   Input Parameters:
903: + dm   - `DM` to be used with `TS`
904: . func - forcing function evaluation routine, for calling sequence see `TSForcingFn`
905: - ctx  - context for solution evaluation

907:   Level: developer

909:   Note:
910:   `TSSetForcingFunction()` is normally used, but it calls this function internally because the application context is actually
911:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
912:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

914: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSForcingFn`, `TSSetForcingFunction()`, `DMTSGetForcingFunction()`
915: @*/
916: PetscErrorCode DMTSSetForcingFunction(DM dm, TSForcingFn *func, PetscCtx ctx)
917: {
918:   DMTS tsdm;

920:   PetscFunctionBegin;
922:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
923:   if (func) tsdm->ops->forcing = func;
924:   if (ctx) tsdm->forcingctx = ctx;
925:   PetscFunctionReturn(PETSC_SUCCESS);
926: }

928: /*@
929:   DMTSGetForcingFunction - get `TS` forcing function evaluation function from a `DMTS`

931:   Not Collective

933:   Input Parameter:
934: . dm - `DM` to be used with `TS`

936:   Output Parameters:
937: + f   - forcing function evaluation function; see `TSForcingFn` for the calling sequence
938: - ctx - context for solution evaluation

940:   Level: developer

942:   Note:
943:   `TSSetForcingFunction()` is normally used, but it calls this function internally because the application context is actually
944:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
945:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the residual.

947: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `TSSetForcingFunction()`, `TSForcingFn`
948: @*/
949: PetscErrorCode DMTSGetForcingFunction(DM dm, TSForcingFn **f, PetscCtxRt ctx)
950: {
951:   DMTS tsdm;

953:   PetscFunctionBegin;
955:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
956:   if (f) *f = tsdm->ops->forcing;
957:   if (ctx) *(void **)ctx = tsdm->forcingctx;
958:   PetscFunctionReturn(PETSC_SUCCESS);
959: }

961: /*@
962:   DMTSGetRHSFunction - get `TS` explicit residual evaluation function from a `DMTS`

964:   Not Collective

966:   Input Parameter:
967: . dm - `DM` to be used with `TS`

969:   Output Parameters:
970: + func - residual evaluation function, for calling sequence see `TSRHSFunctionFn`
971: - ctx  - context for residual evaluation

973:   Level: developer

975:   Note:
976:   `TSGetRHSFunction()` is normally used, but it calls this function internally because the application context is actually
977:   associated with the DM.

979: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `TSRHSFunctionFn`, `TSGetRHSFunction()`
980: @*/
981: PetscErrorCode DMTSGetRHSFunction(DM dm, TSRHSFunctionFn **func, PetscCtxRt ctx)
982: {
983:   DMTS tsdm;

985:   PetscFunctionBegin;
987:   PetscCall(DMGetDMTS(dm, &tsdm));
988:   if (func) *func = tsdm->ops->rhsfunction;
989:   if (ctx) {
990:     if (tsdm->rhsfunctionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->rhsfunctionctxcontainer, ctx));
991:     else *(void **)ctx = NULL;
992:   }
993:   PetscFunctionReturn(PETSC_SUCCESS);
994: }

996: /*@
997:   DMTSSetIJacobian - set `TS` Jacobian evaluation function into a `DMTS`

999:   Not Collective

1001:   Input Parameters:
1002: + dm   - `DM` to be used with `TS`
1003: . func - Jacobian evaluation routine, see `TSIJacobianFn` for the calling sequence
1004: - ctx  - context for residual evaluation

1006:   Level: developer

1008:   Note:
1009:   `TSSetIJacobian()` is normally used, but it calls this function internally because the application context is actually
1010:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
1011:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.

1013: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `TSIJacobianFn`, `DMTSGetIJacobian()`, `TSSetIJacobian()`
1014: @*/
1015: PetscErrorCode DMTSSetIJacobian(DM dm, TSIJacobianFn *func, PetscCtx ctx)
1016: {
1017:   DMTS tsdm;

1019:   PetscFunctionBegin;
1021:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1022:   if (func) tsdm->ops->ijacobian = func;
1023:   if (ctx) {
1024:     PetscContainer ctxcontainer;
1025:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
1026:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
1027:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "ijacobian ctx", (PetscObject)ctxcontainer));
1028:     tsdm->ijacobianctxcontainer = ctxcontainer;
1029:     PetscCall(PetscContainerDestroy(&ctxcontainer));
1030:   }
1031:   PetscFunctionReturn(PETSC_SUCCESS);
1032: }

1034: /*@
1035:   DMTSSetIJacobianContextDestroy - set `TS` Jacobian evaluation context destroy function into a `DMTS`

1037:   Not Collective

1039:   Input Parameters:
1040: + dm - `DM` to be used with `TS`
1041: - f  - Jacobian evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

1043:   Level: developer

1045:   Note:
1046:   `TSSetIJacobianContextDestroy()` is normally used, but it calls this function internally because the application context is actually
1047:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
1048:   not.

1050:   Developer Notes:
1051:   If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.

1053: .seealso: [](ch_ts), `DMTS`, `TSSetIJacobianContextDestroy()`, `TSSetI2JacobianContextDestroy()`, `DMTSSetIJacobian()`, `TSSetIJacobian()`
1054: @*/
1055: PetscErrorCode DMTSSetIJacobianContextDestroy(DM dm, PetscCtxDestroyFn *f)
1056: {
1057:   DMTS tsdm;

1059:   PetscFunctionBegin;
1061:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1062:   if (tsdm->ijacobianctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->ijacobianctxcontainer, f));
1063:   PetscFunctionReturn(PETSC_SUCCESS);
1064: }

1066: PetscErrorCode DMTSUnsetIJacobianContext_Internal(DM dm)
1067: {
1068:   DMTS tsdm;

1070:   PetscFunctionBegin;
1072:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1073:   PetscCall(DMTSUnsetIJacobianContext_DMTS(tsdm));
1074:   PetscFunctionReturn(PETSC_SUCCESS);
1075: }

1077: /*@
1078:   DMTSGetIJacobian - get `TS` Jacobian evaluation function from a `DMTS`

1080:   Not Collective

1082:   Input Parameter:
1083: . dm - `DM` to be used with `TS`

1085:   Output Parameters:
1086: + func - Jacobian evaluation function, for calling sequence see `TSIJacobianFn`
1087: - ctx  - context for residual evaluation

1089:   Level: developer

1091:   Note:
1092:   `TSGetIJacobian()` is normally used, but it calls this function internally because the application context is actually
1093:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
1094:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.

1096: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetIJacobian()`, `TSIJacobianFn`
1097: @*/
1098: PetscErrorCode DMTSGetIJacobian(DM dm, TSIJacobianFn **func, PetscCtxRt ctx)
1099: {
1100:   DMTS tsdm;

1102:   PetscFunctionBegin;
1104:   PetscCall(DMGetDMTS(dm, &tsdm));
1105:   if (func) *func = tsdm->ops->ijacobian;
1106:   if (ctx) {
1107:     if (tsdm->ijacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->ijacobianctxcontainer, ctx));
1108:     else *(void **)ctx = NULL;
1109:   }
1110:   PetscFunctionReturn(PETSC_SUCCESS);
1111: }

1113: /*@
1114:   DMTSSetMassMatrix - set the `TS` mass matrix evaluation function into a `DMTS`

1116:   Not Collective

1118:   Input Parameters:
1119: + dm   - `DM` to be used with `TS`
1120: . func - mass matrix evaluation routine, see `TSMassMatrixFn` for the calling sequence
1121: - ctx  - application context for the mass matrix evaluation (may be `NULL`)

1123:   Level: developer

1125:   Note:
1126:   `TSSetMassMatrix()` is normally used, but it calls this function internally because the application context is actually
1127:   associated with the `DM`. Because the callback lives on the `DM`, it is copied to coarser `DM`s in a multigrid
1128:   hierarchy, so an integrator can assemble a level-appropriate mass matrix.

1130: .seealso: [](ch_ts), `DMTS`, `TS`, `DM`, `TSMassMatrixFn`, `DMTSGetMassMatrix()`, `TSSetMassMatrix()`
1131: @*/
1132: PetscErrorCode DMTSSetMassMatrix(DM dm, TSMassMatrixFn *func, PetscCtx ctx)
1133: {
1134:   DMTS tsdm;

1136:   PetscFunctionBegin;
1138:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1139:   if (func) tsdm->ops->massmatrix = func;
1140:   if (ctx) {
1141:     PetscContainer ctxcontainer;

1143:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
1144:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
1145:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "massmatrix ctx", (PetscObject)ctxcontainer));
1146:     tsdm->massmatrixctxcontainer = ctxcontainer;
1147:     PetscCall(PetscContainerDestroy(&ctxcontainer));
1148:   }
1149:   PetscFunctionReturn(PETSC_SUCCESS);
1150: }

1152: /*@
1153:   DMTSSetMassMatrixContextDestroy - set the `TS` mass matrix evaluation context destroy function into a `DMTS`

1155:   Not Collective

1157:   Input Parameters:
1158: + dm - `DM` to be used with `TS`
1159: - f  - mass matrix evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

1161:   Level: developer

1163: .seealso: [](ch_ts), `DMTS`, `TSSetMassMatrixContextDestroy()`, `DMTSSetMassMatrix()`, `TSSetMassMatrix()`
1164: @*/
1165: PetscErrorCode DMTSSetMassMatrixContextDestroy(DM dm, PetscCtxDestroyFn *f)
1166: {
1167:   DMTS tsdm;

1169:   PetscFunctionBegin;
1171:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1172:   if (tsdm->massmatrixctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->massmatrixctxcontainer, f));
1173:   PetscFunctionReturn(PETSC_SUCCESS);
1174: }

1176: /*@
1177:   DMTSGetMassMatrix - get the `TS` mass matrix evaluation function from a `DMTS`

1179:   Not Collective

1181:   Input Parameter:
1182: . dm - `DM` to be used with `TS`

1184:   Output Parameters:
1185: + func - mass matrix evaluation function, for calling sequence see `TSMassMatrixFn`
1186: - ctx  - application context for the mass matrix evaluation

1188:   Level: developer

1190: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetMassMatrix()`, `TSMassMatrixFn`
1191: @*/
1192: PetscErrorCode DMTSGetMassMatrix(DM dm, TSMassMatrixFn **func, PetscCtxRt ctx)
1193: {
1194:   DMTS tsdm;

1196:   PetscFunctionBegin;
1198:   PetscCall(DMGetDMTS(dm, &tsdm));
1199:   if (func) *func = tsdm->ops->massmatrix;
1200:   if (ctx) {
1201:     if (tsdm->massmatrixctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->massmatrixctxcontainer, ctx));
1202:     else *(void **)ctx = NULL;
1203:   }
1204:   PetscFunctionReturn(PETSC_SUCCESS);
1205: }

1207: /*@
1208:   DMTSSetRHSJacobian - set `TS` Jacobian evaluation function into a `DMTS`

1210:   Not Collective

1212:   Input Parameters:
1213: + dm   - `DM` to be used with `TS`
1214: . func - Jacobian evaluation routine, for calling sequence see `TSIJacobianFn`
1215: - ctx  - context for residual evaluation

1217:   Level: developer

1219:   Note:
1220:   `TSSetRHSJacobian()` is normally used, but it calls this function internally because the application context is actually
1221:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
1222:   not.

1224:   Developer Notes:
1225:   If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.

1227: .seealso: [](ch_ts), `DMTS`, `TSRHSJacobianFn`, `DMTSGetRHSJacobian()`, `TSSetRHSJacobian()`
1228: @*/
1229: PetscErrorCode DMTSSetRHSJacobian(DM dm, TSRHSJacobianFn *func, PetscCtx ctx)
1230: {
1231:   DMTS tsdm;

1233:   PetscFunctionBegin;
1235:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1236:   if (func) tsdm->ops->rhsjacobian = func;
1237:   if (ctx) {
1238:     PetscContainer ctxcontainer;
1239:     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm), &ctxcontainer));
1240:     PetscCall(PetscContainerSetPointer(ctxcontainer, ctx));
1241:     PetscCall(PetscObjectCompose((PetscObject)tsdm, "rhs jacobian ctx", (PetscObject)ctxcontainer));
1242:     tsdm->rhsjacobianctxcontainer = ctxcontainer;
1243:     PetscCall(PetscContainerDestroy(&ctxcontainer));
1244:   }
1245:   PetscFunctionReturn(PETSC_SUCCESS);
1246: }

1248: /*@
1249:   DMTSSetRHSJacobianContextDestroy - set `TS` Jacobian evaluation context destroy function from a `DMTS`

1251:   Not Collective

1253:   Input Parameters:
1254: + dm - `DM` to be used with `TS`
1255: - f  - Jacobian evaluation context destroy function, see `PetscCtxDestroyFn` for its calling sequence

1257:   Level: developer

1259:   Note:
1260:   The user usually calls `TSSetRHSJacobianContextDestroy()` which calls this routine

1262: .seealso: [](ch_ts), `DMTS`, `TS`, `TSSetRHSJacobianContextDestroy()`, `DMTSSetRHSJacobian()`, `TSSetRHSJacobian()`
1263: @*/
1264: PetscErrorCode DMTSSetRHSJacobianContextDestroy(DM dm, PetscCtxDestroyFn *f)
1265: {
1266:   DMTS tsdm;

1268:   PetscFunctionBegin;
1270:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1271:   if (tsdm->rhsjacobianctxcontainer) PetscCall(PetscContainerSetCtxDestroy(tsdm->rhsjacobianctxcontainer, f));
1272:   PetscFunctionReturn(PETSC_SUCCESS);
1273: }

1275: PetscErrorCode DMTSUnsetRHSJacobianContext_Internal(DM dm)
1276: {
1277:   DMTS tsdm;

1279:   PetscFunctionBegin;
1281:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1282:   PetscCall(DMTSUnsetRHSJacobianContext_DMTS(tsdm));
1283:   PetscFunctionReturn(PETSC_SUCCESS);
1284: }

1286: /*@
1287:   DMTSGetRHSJacobian - get `TS` Jacobian evaluation function from a `DMTS`

1289:   Not Collective

1291:   Input Parameter:
1292: . dm - `DM` to be used with `TS`

1294:   Output Parameters:
1295: + func - Jacobian evaluation function, for calling sequence see `TSRHSJacobianFn`
1296: - ctx  - context for residual evaluation

1298:   Level: developer

1300:   Note:
1301:   `TSGetRHSJacobian()` is normally used, but it calls this function internally because the application context is actually
1302:   associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
1303:   not. If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.

1305: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`, `DMTSSetRHSJacobian()`, `TSRHSJacobianFn`
1306: @*/
1307: PetscErrorCode DMTSGetRHSJacobian(DM dm, TSRHSJacobianFn **func, PetscCtxRt ctx)
1308: {
1309:   DMTS tsdm;

1311:   PetscFunctionBegin;
1313:   PetscCall(DMGetDMTS(dm, &tsdm));
1314:   if (func) *func = tsdm->ops->rhsjacobian;
1315:   if (ctx) {
1316:     if (tsdm->rhsjacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->rhsjacobianctxcontainer, ctx));
1317:     else *(void **)ctx = NULL;
1318:   }
1319:   PetscFunctionReturn(PETSC_SUCCESS);
1320: }

1322: /*@
1323:   DMTSSetIFunctionSerialize - sets functions used to view and load a `TSIFunctionFn` context

1325:   Not Collective

1327:   Input Parameters:
1328: + dm   - `DM` to be used with `TS`
1329: . view - viewer function
1330: - load - loading function

1332:   Level: developer

1334: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`
1335: @*/
1336: PetscErrorCode DMTSSetIFunctionSerialize(DM dm, PetscErrorCode (*view)(void *, PetscViewer), PetscErrorCode (*load)(void **, PetscViewer))
1337: {
1338:   DMTS tsdm;

1340:   PetscFunctionBegin;
1342:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1343:   tsdm->ops->ifunctionview = view;
1344:   tsdm->ops->ifunctionload = load;
1345:   PetscFunctionReturn(PETSC_SUCCESS);
1346: }

1348: /*@
1349:   DMTSSetIJacobianSerialize - sets functions used to view and load a `TSIJacobianFn` context

1351:   Not Collective

1353:   Input Parameters:
1354: + dm   - `DM` to be used with `TS`
1355: . view - viewer function
1356: - load - loading function

1358:   Level: developer

1360: .seealso: [](ch_ts), `DMTS`, `DM`, `TS`
1361: @*/
1362: PetscErrorCode DMTSSetIJacobianSerialize(DM dm, PetscErrorCode (*view)(void *, PetscViewer), PetscErrorCode (*load)(void **, PetscViewer))
1363: {
1364:   DMTS tsdm;

1366:   PetscFunctionBegin;
1368:   PetscCall(DMGetDMTSWrite(dm, &tsdm));
1369:   tsdm->ops->ijacobianview = view;
1370:   tsdm->ops->ijacobianload = load;
1371:   PetscFunctionReturn(PETSC_SUCCESS);
1372: }