xref: /petsc/src/ts/utils/dmts.c (revision 87497f523770ea28bdc907071c6e8146b51bca00)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h>     /*I "petscts.h" I*/
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
324989b8cSPeter Brune 
4800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetRHSFunctionContext_DMTS(DMTS tsdm)
5800f99ffSJeremy L Thompson {
6800f99ffSJeremy L Thompson   PetscFunctionBegin;
7800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"rhs function ctx",NULL));
8800f99ffSJeremy L Thompson   tsdm->rhsfunctionctxcontainer = NULL;
9800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
10800f99ffSJeremy L Thompson }
11800f99ffSJeremy L Thompson 
12800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetRHSJacobianContext_DMTS(DMTS tsdm)
13800f99ffSJeremy L Thompson {
14800f99ffSJeremy L Thompson   PetscFunctionBegin;
15800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"rhs jacobian ctx",NULL));
16800f99ffSJeremy L Thompson   tsdm->rhsjacobianctxcontainer = NULL;
17800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
18800f99ffSJeremy L Thompson }
19800f99ffSJeremy L Thompson 
20800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetIFunctionContext_DMTS(DMTS tsdm)
21800f99ffSJeremy L Thompson {
22800f99ffSJeremy L Thompson   PetscFunctionBegin;
23800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"ifunction ctx",NULL));
24800f99ffSJeremy L Thompson   tsdm->ifunctionctxcontainer = NULL;
25800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
26800f99ffSJeremy L Thompson }
27800f99ffSJeremy L Thompson 
28800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetIJacobianContext_DMTS(DMTS tsdm)
29800f99ffSJeremy L Thompson {
30800f99ffSJeremy L Thompson   PetscFunctionBegin;
31800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"ijacobian ctx",NULL));
32800f99ffSJeremy L Thompson   tsdm->ijacobianctxcontainer = NULL;
33800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
34800f99ffSJeremy L Thompson }
35800f99ffSJeremy L Thompson 
36800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetI2FunctionContext_DMTS(DMTS tsdm)
37800f99ffSJeremy L Thompson {
38800f99ffSJeremy L Thompson   PetscFunctionBegin;
39800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"i2function ctx",NULL));
40800f99ffSJeremy L Thompson   tsdm->i2functionctxcontainer = NULL;
41800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
42800f99ffSJeremy L Thompson }
43800f99ffSJeremy L Thompson 
44800f99ffSJeremy L Thompson static PetscErrorCode DMTSUnsetI2JacobianContext_DMTS(DMTS tsdm)
45800f99ffSJeremy L Thompson {
46800f99ffSJeremy L Thompson   PetscFunctionBegin;
47800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)tsdm,"i2jacobian ctx",NULL));
48800f99ffSJeremy L Thompson   tsdm->i2jacobianctxcontainer = NULL;
49800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
50800f99ffSJeremy L Thompson }
51800f99ffSJeremy L Thompson 
52d74926cbSBarry Smith static PetscErrorCode DMTSDestroy(DMTS *kdm)
53d74926cbSBarry Smith {
54d74926cbSBarry Smith   PetscFunctionBegin;
55d74926cbSBarry Smith   if (!*kdm) PetscFunctionReturn(0);
56d74926cbSBarry Smith   PetscValidHeaderSpecific((*kdm),DMTS_CLASSID,1);
57c793f718SLisandro Dalcin   if (--((PetscObject)(*kdm))->refct > 0) {*kdm = NULL; PetscFunctionReturn(0);}
58800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetRHSFunctionContext_DMTS(*kdm));
59800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetRHSJacobianContext_DMTS(*kdm));
60800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetIFunctionContext_DMTS(*kdm));
61800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetIJacobianContext_DMTS(*kdm));
62800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetI2FunctionContext_DMTS(*kdm));
63800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetI2JacobianContext_DMTS(*kdm));
649566063dSJacob Faibussowitsch   if ((*kdm)->ops->destroy) PetscCall(((*kdm)->ops->destroy)(*kdm));
659566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(kdm));
66d74926cbSBarry Smith   PetscFunctionReturn(0);
67d74926cbSBarry Smith }
68d74926cbSBarry Smith 
692d53ad75SBarry Smith PetscErrorCode DMTSLoad(DMTS kdm,PetscViewer viewer)
702d53ad75SBarry Smith {
712d53ad75SBarry Smith   PetscFunctionBegin;
729566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ifunction,1,NULL,PETSC_FUNCTION));
739566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ifunctionview,1,NULL,PETSC_FUNCTION));
749566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ifunctionload,1,NULL,PETSC_FUNCTION));
75ad6bc421SBarry Smith   if (kdm->ops->ifunctionload) {
76800f99ffSJeremy L Thompson     void *ctx;
77800f99ffSJeremy L Thompson 
78800f99ffSJeremy L Thompson     PetscCall(PetscContainerGetPointer(kdm->ifunctionctxcontainer,&ctx));
79800f99ffSJeremy L Thompson     PetscCall((*kdm->ops->ifunctionload)(&ctx,viewer));
80ad6bc421SBarry Smith   }
819566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ijacobian,1,NULL,PETSC_FUNCTION));
829566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ijacobianview,1,NULL,PETSC_FUNCTION));
839566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->ijacobianload,1,NULL,PETSC_FUNCTION));
84ad6bc421SBarry Smith   if (kdm->ops->ijacobianload) {
85800f99ffSJeremy L Thompson     void *ctx;
86800f99ffSJeremy L Thompson 
87800f99ffSJeremy L Thompson     PetscCall(PetscContainerGetPointer(kdm->ijacobianctxcontainer,&ctx));
88800f99ffSJeremy L Thompson     PetscCall((*kdm->ops->ijacobianload)(&ctx,viewer));
89ad6bc421SBarry Smith   }
902d53ad75SBarry Smith   PetscFunctionReturn(0);
912d53ad75SBarry Smith }
922d53ad75SBarry Smith 
932d53ad75SBarry Smith PetscErrorCode DMTSView(DMTS kdm,PetscViewer viewer)
942d53ad75SBarry Smith {
952d53ad75SBarry Smith   PetscBool      isascii,isbinary;
962d53ad75SBarry Smith 
972d53ad75SBarry Smith   PetscFunctionBegin;
989566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
999566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
1002d53ad75SBarry Smith   if (isascii) {
101c7a10e08SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
1022d53ad75SBarry Smith     const char *fname;
1032d53ad75SBarry Smith 
1049566063dSJacob Faibussowitsch     PetscCall(PetscFPTFind(kdm->ops->ifunction,&fname));
1052d53ad75SBarry Smith     if (fname) {
1069566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  IFunction used by TS: %s\n",fname));
1072d53ad75SBarry Smith     }
1089566063dSJacob Faibussowitsch     PetscCall(PetscFPTFind(kdm->ops->ijacobian,&fname));
1092d53ad75SBarry Smith     if (fname) {
1109566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"  IJacobian function used by TS: %s\n",fname));
1112d53ad75SBarry Smith     }
112c7a10e08SBarry Smith #endif
1132d53ad75SBarry Smith   } else if (isbinary) {
1143964eb88SJed Brown     struct {
1153964eb88SJed Brown       TSIFunction ifunction;
1169200755eSBarry Smith     } funcstruct;
1179200755eSBarry Smith     struct {
1183964eb88SJed Brown       PetscErrorCode (*ifunctionview)(void*,PetscViewer);
1199200755eSBarry Smith     } funcviewstruct;
1209200755eSBarry Smith     struct {
1213964eb88SJed Brown       PetscErrorCode (*ifunctionload)(void**,PetscViewer);
1229200755eSBarry Smith     } funcloadstruct;
1233964eb88SJed Brown     struct {
1243964eb88SJed Brown       TSIJacobian ijacobian;
1259200755eSBarry Smith     } jacstruct;
1269200755eSBarry Smith     struct {
1273964eb88SJed Brown       PetscErrorCode (*ijacobianview)(void*,PetscViewer);
1289200755eSBarry Smith     } jacviewstruct;
1299200755eSBarry Smith     struct {
1303964eb88SJed Brown       PetscErrorCode (*ijacobianload)(void**,PetscViewer);
1319200755eSBarry Smith     } jacloadstruct;
1323964eb88SJed Brown 
1339200755eSBarry Smith     funcstruct.ifunction         = kdm->ops->ifunction;
1349200755eSBarry Smith     funcviewstruct.ifunctionview = kdm->ops->ifunctionview;
1359200755eSBarry Smith     funcloadstruct.ifunctionload = kdm->ops->ifunctionload;
1369566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&funcstruct,1,PETSC_FUNCTION));
1379566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&funcviewstruct,1,PETSC_FUNCTION));
1389566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&funcloadstruct,1,PETSC_FUNCTION));
139800f99ffSJeremy L Thompson     if (kdm->ops->ifunctionview) {
140800f99ffSJeremy L Thompson       void *ctx;
141800f99ffSJeremy L Thompson 
142800f99ffSJeremy L Thompson       PetscCall(PetscContainerGetPointer(kdm->ifunctionctxcontainer,&ctx));
143800f99ffSJeremy L Thompson       PetscCall((*kdm->ops->ifunctionview)(ctx,viewer));
144800f99ffSJeremy L Thompson     }
1459200755eSBarry Smith     jacstruct.ijacobian = kdm->ops->ijacobian;
1469200755eSBarry Smith     jacviewstruct.ijacobianview = kdm->ops->ijacobianview;
1479200755eSBarry Smith     jacloadstruct.ijacobianload = kdm->ops->ijacobianload;
1489566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&jacstruct,1,PETSC_FUNCTION));
1499566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&jacviewstruct,1,PETSC_FUNCTION));
1509566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&jacloadstruct,1,PETSC_FUNCTION));
151800f99ffSJeremy L Thompson     if (kdm->ops->ijacobianview) {
152800f99ffSJeremy L Thompson       void *ctx;
153800f99ffSJeremy L Thompson 
154800f99ffSJeremy L Thompson       PetscCall(PetscContainerGetPointer(kdm->ijacobianctxcontainer,&ctx));
155800f99ffSJeremy L Thompson       PetscCall((*kdm->ops->ijacobianview)(ctx,viewer));
156800f99ffSJeremy L Thompson     }
1572d53ad75SBarry Smith   }
1582d53ad75SBarry Smith   PetscFunctionReturn(0);
1592d53ad75SBarry Smith }
1602d53ad75SBarry Smith 
161d74926cbSBarry Smith static PetscErrorCode DMTSCreate(MPI_Comm comm,DMTS *kdm)
162d74926cbSBarry Smith {
163d74926cbSBarry Smith   PetscFunctionBegin;
1649566063dSJacob Faibussowitsch   PetscCall(TSInitializePackage());
1659566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*kdm, DMTS_CLASSID, "DMTS", "DMTS", "DMTS", comm, DMTSDestroy, DMTSView));
166d74926cbSBarry Smith   PetscFunctionReturn(0);
167d74926cbSBarry Smith }
16824989b8cSPeter Brune 
1692a34c10cSBarry Smith /* Attaches the DMTS to the coarse level.
17024989b8cSPeter Brune  * Under what conditions should we copy versus duplicate?
17124989b8cSPeter Brune  */
172942e3340SBarry Smith static PetscErrorCode DMCoarsenHook_DMTS(DM dm,DM dmc,void *ctx)
17324989b8cSPeter Brune {
17424989b8cSPeter Brune   PetscFunctionBegin;
1759566063dSJacob Faibussowitsch   PetscCall(DMCopyDMTS(dm,dmc));
17624989b8cSPeter Brune   PetscFunctionReturn(0);
17724989b8cSPeter Brune }
17824989b8cSPeter Brune 
17924989b8cSPeter Brune /* This could restrict auxiliary information to the coarse level.
18024989b8cSPeter Brune  */
181942e3340SBarry Smith static PetscErrorCode DMRestrictHook_DMTS(DM dm,Mat Restrict,Vec rscale,Mat Inject,DM dmc,void *ctx)
18224989b8cSPeter Brune {
18324989b8cSPeter Brune   PetscFunctionBegin;
18424989b8cSPeter Brune   PetscFunctionReturn(0);
18524989b8cSPeter Brune }
18624989b8cSPeter Brune 
187258e1594SPeter Brune static PetscErrorCode DMSubDomainHook_DMTS(DM dm,DM subdm,void *ctx)
188258e1594SPeter Brune {
189258e1594SPeter Brune   PetscFunctionBegin;
1909566063dSJacob Faibussowitsch   PetscCall(DMCopyDMTS(dm,subdm));
191258e1594SPeter Brune   PetscFunctionReturn(0);
192258e1594SPeter Brune }
193258e1594SPeter Brune 
194258e1594SPeter Brune /* This could restrict auxiliary information to the coarse level.
195258e1594SPeter Brune  */
196258e1594SPeter Brune static PetscErrorCode DMSubDomainRestrictHook_DMTS(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
197258e1594SPeter Brune {
198258e1594SPeter Brune   PetscFunctionBegin;
199258e1594SPeter Brune   PetscFunctionReturn(0);
200258e1594SPeter Brune }
201258e1594SPeter Brune 
202d74926cbSBarry Smith /*@C
203d74926cbSBarry Smith    DMTSCopy - copies the information in a DMTS to another DMTS
204d74926cbSBarry Smith 
205d74926cbSBarry Smith    Not Collective
206d74926cbSBarry Smith 
2074165533cSJose E. Roman    Input Parameters:
208d74926cbSBarry Smith +  kdm - Original DMTS
209d74926cbSBarry Smith -  nkdm - DMTS to receive the data, should have been created with DMTSCreate()
210d74926cbSBarry Smith 
211d74926cbSBarry Smith    Level: developer
212d74926cbSBarry Smith 
213db781477SPatrick Sanan .seealso: `DMTSCreate()`, `DMTSDestroy()`
214d74926cbSBarry Smith @*/
215d74926cbSBarry Smith PetscErrorCode DMTSCopy(DMTS kdm,DMTS nkdm)
21624989b8cSPeter Brune {
21724989b8cSPeter Brune   PetscFunctionBegin;
218d74926cbSBarry Smith   PetscValidHeaderSpecific(kdm,DMTS_CLASSID,1);
219d74926cbSBarry Smith   PetscValidHeaderSpecific(nkdm,DMTS_CLASSID,2);
220d74926cbSBarry Smith   nkdm->ops->rhsfunction        = kdm->ops->rhsfunction;
221d74926cbSBarry Smith   nkdm->ops->rhsjacobian        = kdm->ops->rhsjacobian;
222d74926cbSBarry Smith   nkdm->ops->ifunction          = kdm->ops->ifunction;
223d74926cbSBarry Smith   nkdm->ops->ijacobian          = kdm->ops->ijacobian;
224efe9872eSLisandro Dalcin   nkdm->ops->i2function         = kdm->ops->i2function;
225efe9872eSLisandro Dalcin   nkdm->ops->i2jacobian         = kdm->ops->i2jacobian;
226d74926cbSBarry Smith   nkdm->ops->solution           = kdm->ops->solution;
227d74926cbSBarry Smith   nkdm->ops->destroy            = kdm->ops->destroy;
228d74926cbSBarry Smith   nkdm->ops->duplicate          = kdm->ops->duplicate;
229d74926cbSBarry Smith 
230d74926cbSBarry Smith   nkdm->solutionctx             = kdm->solutionctx;
231800f99ffSJeremy L Thompson   nkdm->rhsfunctionctxcontainer = kdm->rhsfunctionctxcontainer;
232800f99ffSJeremy L Thompson   nkdm->rhsjacobianctxcontainer = kdm->rhsjacobianctxcontainer;
233800f99ffSJeremy L Thompson   nkdm->ifunctionctxcontainer   = kdm->ifunctionctxcontainer;
234800f99ffSJeremy L Thompson   nkdm->ijacobianctxcontainer   = kdm->ijacobianctxcontainer;
235800f99ffSJeremy L Thompson   nkdm->i2functionctxcontainer  = kdm->i2functionctxcontainer;
236800f99ffSJeremy L Thompson   nkdm->i2jacobianctxcontainer  = kdm->i2jacobianctxcontainer;
237800f99ffSJeremy L Thompson   if (nkdm->rhsfunctionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"rhs function ctx",(PetscObject)nkdm->rhsfunctionctxcontainer));
238800f99ffSJeremy L Thompson   if (nkdm->rhsjacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"rhs jacobian ctx",(PetscObject)nkdm->rhsjacobianctxcontainer));
239800f99ffSJeremy L Thompson   if (nkdm->ifunctionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"ifunction ctx",(PetscObject)nkdm->ifunctionctxcontainer));
240800f99ffSJeremy L Thompson   if (nkdm->ijacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"ijacobian ctx",(PetscObject)nkdm->ijacobianctxcontainer));
241800f99ffSJeremy L Thompson   if (nkdm->i2functionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"i2function ctx",(PetscObject)nkdm->i2functionctxcontainer));
242800f99ffSJeremy L Thompson   if (nkdm->i2jacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"i2jacobian ctx",(PetscObject)nkdm->i2jacobianctxcontainer));
243d74926cbSBarry Smith 
244d74926cbSBarry Smith   nkdm->data = kdm->data;
245d74926cbSBarry Smith 
246d74926cbSBarry Smith   /*
247d74926cbSBarry Smith   nkdm->fortran_func_pointers[0] = kdm->fortran_func_pointers[0];
248d74926cbSBarry Smith   nkdm->fortran_func_pointers[1] = kdm->fortran_func_pointers[1];
249d74926cbSBarry Smith   nkdm->fortran_func_pointers[2] = kdm->fortran_func_pointers[2];
250d74926cbSBarry Smith   */
251d74926cbSBarry Smith 
252d74926cbSBarry Smith   /* implementation specific copy hooks */
2539566063dSJacob Faibussowitsch   if (kdm->ops->duplicate) PetscCall((*kdm->ops->duplicate)(kdm,nkdm));
25424989b8cSPeter Brune   PetscFunctionReturn(0);
25524989b8cSPeter Brune }
25624989b8cSPeter Brune 
25724989b8cSPeter Brune /*@C
258942e3340SBarry Smith    DMGetDMTS - get read-only private DMTS context from a DM
25924989b8cSPeter Brune 
26024989b8cSPeter Brune    Not Collective
26124989b8cSPeter Brune 
2624165533cSJose E. Roman    Input Parameter:
26324989b8cSPeter Brune .  dm - DM to be used with TS
26424989b8cSPeter Brune 
2654165533cSJose E. Roman    Output Parameter:
266942e3340SBarry Smith .  tsdm - private DMTS context
26724989b8cSPeter Brune 
26824989b8cSPeter Brune    Level: developer
26924989b8cSPeter Brune 
27024989b8cSPeter Brune    Notes:
271942e3340SBarry Smith    Use DMGetDMTSWrite() if write access is needed. The DMTSSetXXX API should be used wherever possible.
27224989b8cSPeter Brune 
273db781477SPatrick Sanan .seealso: `DMGetDMTSWrite()`
27424989b8cSPeter Brune @*/
275942e3340SBarry Smith PetscErrorCode DMGetDMTS(DM dm,DMTS *tsdm)
27624989b8cSPeter Brune {
27724989b8cSPeter Brune   PetscFunctionBegin;
27824989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2792a34c10cSBarry Smith   *tsdm = (DMTS) dm->dmts;
280d74926cbSBarry Smith   if (!*tsdm) {
2819566063dSJacob Faibussowitsch     PetscCall(PetscInfo(dm,"Creating new DMTS\n"));
2829566063dSJacob Faibussowitsch     PetscCall(DMTSCreate(PetscObjectComm((PetscObject)dm),tsdm));
2832a34c10cSBarry Smith     dm->dmts = (PetscObject) *tsdm;
2845c87d4f4SJunchao Zhang     (*tsdm)->originaldm = dm;
2859566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHookAdd(dm,DMCoarsenHook_DMTS,DMRestrictHook_DMTS,NULL));
2869566063dSJacob Faibussowitsch     PetscCall(DMSubDomainHookAdd(dm,DMSubDomainHook_DMTS,DMSubDomainRestrictHook_DMTS,NULL));
28724989b8cSPeter Brune   }
28824989b8cSPeter Brune   PetscFunctionReturn(0);
28924989b8cSPeter Brune }
29024989b8cSPeter Brune 
29124989b8cSPeter Brune /*@C
292942e3340SBarry Smith    DMGetDMTSWrite - get write access to private DMTS context from a DM
29324989b8cSPeter Brune 
29424989b8cSPeter Brune    Not Collective
29524989b8cSPeter Brune 
2964165533cSJose E. Roman    Input Parameter:
29724989b8cSPeter Brune .  dm - DM to be used with TS
29824989b8cSPeter Brune 
2994165533cSJose E. Roman    Output Parameter:
300942e3340SBarry Smith .  tsdm - private DMTS context
30124989b8cSPeter Brune 
30224989b8cSPeter Brune    Level: developer
30324989b8cSPeter Brune 
304db781477SPatrick Sanan .seealso: `DMGetDMTS()`
30524989b8cSPeter Brune @*/
306942e3340SBarry Smith PetscErrorCode DMGetDMTSWrite(DM dm,DMTS *tsdm)
30724989b8cSPeter Brune {
308942e3340SBarry Smith   DMTS           sdm;
30924989b8cSPeter Brune 
31024989b8cSPeter Brune   PetscFunctionBegin;
31124989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3129566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&sdm));
3133c633725SBarry Smith   PetscCheck(sdm->originaldm,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DMTS has a NULL originaldm");
31424989b8cSPeter Brune   if (sdm->originaldm != dm) {  /* Copy on write */
3152a34c10cSBarry Smith     DMTS oldsdm = sdm;
3169566063dSJacob Faibussowitsch     PetscCall(PetscInfo(dm,"Copying DMTS due to write\n"));
3179566063dSJacob Faibussowitsch     PetscCall(DMTSCreate(PetscObjectComm((PetscObject)dm),&sdm));
3189566063dSJacob Faibussowitsch     PetscCall(DMTSCopy(oldsdm,sdm));
3199566063dSJacob Faibussowitsch     PetscCall(DMTSDestroy((DMTS*)&dm->dmts));
3202a34c10cSBarry Smith     dm->dmts = (PetscObject) sdm;
3215c87d4f4SJunchao Zhang     sdm->originaldm = dm;
32224989b8cSPeter Brune   }
32324989b8cSPeter Brune   *tsdm = sdm;
32424989b8cSPeter Brune   PetscFunctionReturn(0);
32524989b8cSPeter Brune }
32624989b8cSPeter Brune 
32724989b8cSPeter Brune /*@C
328942e3340SBarry Smith    DMCopyDMTS - copies a DM context to a new DM
32924989b8cSPeter Brune 
33024989b8cSPeter Brune    Logically Collective
33124989b8cSPeter Brune 
3324165533cSJose E. Roman    Input Parameters:
33324989b8cSPeter Brune +  dmsrc - DM to obtain context from
33424989b8cSPeter Brune -  dmdest - DM to add context to
33524989b8cSPeter Brune 
33624989b8cSPeter Brune    Level: developer
33724989b8cSPeter Brune 
33824989b8cSPeter Brune    Note:
33924989b8cSPeter Brune    The context is copied by reference. This function does not ensure that a context exists.
34024989b8cSPeter Brune 
341db781477SPatrick Sanan .seealso: `DMGetDMTS()`, `TSSetDM()`
34224989b8cSPeter Brune @*/
343942e3340SBarry Smith PetscErrorCode DMCopyDMTS(DM dmsrc,DM dmdest)
34424989b8cSPeter Brune {
34524989b8cSPeter Brune   PetscFunctionBegin;
34624989b8cSPeter Brune   PetscValidHeaderSpecific(dmsrc,DM_CLASSID,1);
34724989b8cSPeter Brune   PetscValidHeaderSpecific(dmdest,DM_CLASSID,2);
3489566063dSJacob Faibussowitsch   PetscCall(DMTSDestroy((DMTS*)&dmdest->dmts));
3492a34c10cSBarry Smith   dmdest->dmts = dmsrc->dmts;
3509566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference(dmdest->dmts));
3519566063dSJacob Faibussowitsch   PetscCall(DMCoarsenHookAdd(dmdest,DMCoarsenHook_DMTS,DMRestrictHook_DMTS,NULL));
3529566063dSJacob Faibussowitsch   PetscCall(DMSubDomainHookAdd(dmdest,DMSubDomainHook_DMTS,DMSubDomainRestrictHook_DMTS,NULL));
35324989b8cSPeter Brune   PetscFunctionReturn(0);
35424989b8cSPeter Brune }
35524989b8cSPeter Brune 
35624989b8cSPeter Brune /*@C
35724989b8cSPeter Brune    DMTSSetIFunction - set TS implicit function evaluation function
35824989b8cSPeter Brune 
35924989b8cSPeter Brune    Not Collective
36024989b8cSPeter Brune 
3614165533cSJose E. Roman    Input Parameters:
36224989b8cSPeter Brune +  dm - DM to be used with TS
363a96d6ef6SBarry Smith .  func - function evaluating f(t,u,u_t)
36424989b8cSPeter Brune -  ctx - context for residual evaluation
36524989b8cSPeter Brune 
366a96d6ef6SBarry Smith    Calling sequence of func:
367a96d6ef6SBarry Smith $     PetscErrorCode func(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
368a96d6ef6SBarry Smith 
369a96d6ef6SBarry Smith +  t   - time at step/stage being solved
370a96d6ef6SBarry Smith .  u   - state vector
371a96d6ef6SBarry Smith .  u_t - time derivative of state vector
372a96d6ef6SBarry Smith .  F   - function vector
373a96d6ef6SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
374a96d6ef6SBarry Smith 
37524989b8cSPeter Brune    Level: advanced
37624989b8cSPeter Brune 
37724989b8cSPeter Brune    Note:
37824989b8cSPeter Brune    TSSetFunction() is normally used, but it calls this function internally because the user context is actually
37924989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
38024989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
38124989b8cSPeter Brune 
382db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetIFunction()`, `DMTSSetJacobian()`
38324989b8cSPeter Brune @*/
38424989b8cSPeter Brune PetscErrorCode DMTSSetIFunction(DM dm,TSIFunction func,void *ctx)
38524989b8cSPeter Brune {
386942e3340SBarry Smith   DMTS           tsdm;
38724989b8cSPeter Brune 
38824989b8cSPeter Brune   PetscFunctionBegin;
38924989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3909566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
391d74926cbSBarry Smith   if (func) tsdm->ops->ifunction = func;
392800f99ffSJeremy L Thompson   if (ctx) {
393800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
394800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
395800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
396800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"ifunction ctx",(PetscObject)ctxcontainer));
397800f99ffSJeremy L Thompson     tsdm->ifunctionctxcontainer = ctxcontainer;
398800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
399800f99ffSJeremy L Thompson   }
400800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
401800f99ffSJeremy L Thompson }
402800f99ffSJeremy L Thompson 
403800f99ffSJeremy L Thompson /*@C
4045cb80ecdSBarry Smith    DMTSSetIFunctionContextDestroy - set `TS` implicit evaluation context destroy function
405800f99ffSJeremy L Thompson 
406800f99ffSJeremy L Thompson    Not Collective
407800f99ffSJeremy L Thompson 
408800f99ffSJeremy L Thompson    Input Parameters:
4095cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
4105cb80ecdSBarry Smith -  f - implicit evaluation context destroy function
411800f99ffSJeremy L Thompson 
412800f99ffSJeremy L Thompson    Level: advanced
413800f99ffSJeremy L Thompson 
41490f54644SBarry Smith .seealso: `DMTSSetIFunction()`, `TSSetIFunction()`
415800f99ffSJeremy L Thompson @*/
416800f99ffSJeremy L Thompson PetscErrorCode DMTSSetIFunctionContextDestroy(DM dm,PetscErrorCode (*f)(void*))
417800f99ffSJeremy L Thompson {
418800f99ffSJeremy L Thompson   DMTS           tsdm;
419800f99ffSJeremy L Thompson 
420800f99ffSJeremy L Thompson   PetscFunctionBegin;
421800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
422800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
423800f99ffSJeremy L Thompson   if (tsdm->ifunctionctxcontainer)PetscCall(PetscContainerSetUserDestroy(tsdm->ifunctionctxcontainer,f));
424800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
425800f99ffSJeremy L Thompson }
426800f99ffSJeremy L Thompson 
427800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetIFunctionContext_Internal(DM dm)
428800f99ffSJeremy L Thompson {
429800f99ffSJeremy L Thompson   DMTS           tsdm;
430800f99ffSJeremy L Thompson 
431800f99ffSJeremy L Thompson   PetscFunctionBegin;
432800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
433800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
434800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetIFunctionContext_DMTS(tsdm));
43524989b8cSPeter Brune   PetscFunctionReturn(0);
43624989b8cSPeter Brune }
43724989b8cSPeter Brune 
43824989b8cSPeter Brune /*@C
43924989b8cSPeter Brune    DMTSGetIFunction - get TS implicit residual evaluation function
44024989b8cSPeter Brune 
44124989b8cSPeter Brune    Not Collective
44224989b8cSPeter Brune 
4434165533cSJose E. Roman    Input Parameter:
44424989b8cSPeter Brune .  dm - DM to be used with TS
44524989b8cSPeter Brune 
4464165533cSJose E. Roman    Output Parameters:
44724989b8cSPeter Brune +  func - function evaluation function, see TSSetIFunction() for calling sequence
44824989b8cSPeter Brune -  ctx - context for residual evaluation
44924989b8cSPeter Brune 
45024989b8cSPeter Brune    Level: advanced
45124989b8cSPeter Brune 
45224989b8cSPeter Brune    Note:
45324989b8cSPeter Brune    TSGetFunction() is normally used, but it calls this function internally because the user context is actually
45424989b8cSPeter Brune    associated with the DM.
45524989b8cSPeter Brune 
456db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `DMTSSetFunction()`, `TSSetFunction()`
45724989b8cSPeter Brune @*/
45824989b8cSPeter Brune PetscErrorCode DMTSGetIFunction(DM dm,TSIFunction *func,void **ctx)
45924989b8cSPeter Brune {
460942e3340SBarry Smith   DMTS           tsdm;
46124989b8cSPeter Brune 
46224989b8cSPeter Brune   PetscFunctionBegin;
46324989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4649566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
465d74926cbSBarry Smith   if (func) *func = tsdm->ops->ifunction;
466800f99ffSJeremy L Thompson   if (ctx) {
467800f99ffSJeremy L Thompson     if (tsdm->ifunctionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->ifunctionctxcontainer,ctx));
468800f99ffSJeremy L Thompson     else *ctx = NULL;
469800f99ffSJeremy L Thompson   }
47024989b8cSPeter Brune   PetscFunctionReturn(0);
47124989b8cSPeter Brune }
47224989b8cSPeter Brune 
473efe9872eSLisandro Dalcin /*@C
474efe9872eSLisandro Dalcin    DMTSSetI2Function - set TS implicit function evaluation function for 2nd order systems
475efe9872eSLisandro Dalcin 
476efe9872eSLisandro Dalcin    Not Collective
477efe9872eSLisandro Dalcin 
4784165533cSJose E. Roman    Input Parameters:
479efe9872eSLisandro Dalcin +  dm - DM to be used with TS
480a96d6ef6SBarry Smith .  fun - function evaluation routine
481efe9872eSLisandro Dalcin -  ctx - context for residual evaluation
482efe9872eSLisandro Dalcin 
483a96d6ef6SBarry Smith    Calling sequence of fun:
484a96d6ef6SBarry Smith $     PetscErrorCode fun(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,Vec F,ctx);
485a96d6ef6SBarry Smith 
486a96d6ef6SBarry Smith +  t    - time at step/stage being solved
487a96d6ef6SBarry Smith .  U    - state vector
488a96d6ef6SBarry Smith .  U_t  - time derivative of state vector
489a96d6ef6SBarry Smith .  U_tt - second time derivative of state vector
490a96d6ef6SBarry Smith .  F    - function vector
491a96d6ef6SBarry Smith -  ctx  - [optional] user-defined context for matrix evaluation routine (may be NULL)
492a96d6ef6SBarry Smith 
493efe9872eSLisandro Dalcin    Level: advanced
494efe9872eSLisandro Dalcin 
495efe9872eSLisandro Dalcin    Note:
496efe9872eSLisandro Dalcin    TSSetI2Function() is normally used, but it calls this function internally because the user context is actually
497efe9872eSLisandro Dalcin    associated with the DM.
498efe9872eSLisandro Dalcin 
499db781477SPatrick Sanan .seealso: `TSSetI2Function()`
500efe9872eSLisandro Dalcin @*/
501efe9872eSLisandro Dalcin PetscErrorCode DMTSSetI2Function(DM dm,TSI2Function fun,void *ctx)
502efe9872eSLisandro Dalcin {
503efe9872eSLisandro Dalcin   DMTS           tsdm;
504efe9872eSLisandro Dalcin 
505efe9872eSLisandro Dalcin   PetscFunctionBegin;
506efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5079566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
508efe9872eSLisandro Dalcin   if (fun) tsdm->ops->i2function = fun;
509800f99ffSJeremy L Thompson   if (ctx) {
510800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
511800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
512800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
513800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"i2function ctx",(PetscObject)ctxcontainer));
514800f99ffSJeremy L Thompson     tsdm->i2functionctxcontainer = ctxcontainer;
515800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
516800f99ffSJeremy L Thompson   }
517800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
518800f99ffSJeremy L Thompson }
519800f99ffSJeremy L Thompson 
520800f99ffSJeremy L Thompson /*@C
5215cb80ecdSBarry Smith    DMTSSetI2FunctionContextDestroy - set `TS` implicit evaluation for 2nd order systems context destroy
522800f99ffSJeremy L Thompson 
523800f99ffSJeremy L Thompson    Not Collective
524800f99ffSJeremy L Thompson 
525800f99ffSJeremy L Thompson    Input Parameters:
5265cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
5275cb80ecdSBarry Smith -  f - implicit evaluation context destroy function
528800f99ffSJeremy L Thompson 
529800f99ffSJeremy L Thompson    Level: advanced
530800f99ffSJeremy L Thompson 
531800f99ffSJeremy L Thompson    Note:
5325cb80ecdSBarry Smith    `TSSetI2FunctionContextDestroy()` is normally used, but it calls this function internally because the user context is actually
5335cb80ecdSBarry Smith    associated with the `DM`.
534800f99ffSJeremy L Thompson 
535800f99ffSJeremy L Thompson .seealso: `TSSetI2FunctionContextDestroy()`, `DMTSSetI2Function()`, `TSSetI2Function()`
536800f99ffSJeremy L Thompson @*/
537800f99ffSJeremy L Thompson PetscErrorCode DMTSSetI2FunctionContextDestroy(DM dm,PetscErrorCode (*f)(void*))
538800f99ffSJeremy L Thompson {
539800f99ffSJeremy L Thompson   DMTS           tsdm;
540800f99ffSJeremy L Thompson 
541800f99ffSJeremy L Thompson   PetscFunctionBegin;
542800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
543800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
544800f99ffSJeremy L Thompson   if (tsdm->i2functionctxcontainer) PetscCall(PetscContainerSetUserDestroy(tsdm->i2functionctxcontainer,f));
545800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
546800f99ffSJeremy L Thompson }
547800f99ffSJeremy L Thompson 
548800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetI2FunctionContext_Internal(DM dm)
549800f99ffSJeremy L Thompson {
550800f99ffSJeremy L Thompson   DMTS           tsdm;
551800f99ffSJeremy L Thompson 
552800f99ffSJeremy L Thompson   PetscFunctionBegin;
553800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
554800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
555800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetI2FunctionContext_DMTS(tsdm));
556efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
557efe9872eSLisandro Dalcin }
558efe9872eSLisandro Dalcin 
559efe9872eSLisandro Dalcin /*@C
560efe9872eSLisandro Dalcin    DMTSGetI2Function - get TS implicit residual evaluation function for 2nd order systems
561efe9872eSLisandro Dalcin 
562efe9872eSLisandro Dalcin    Not Collective
563efe9872eSLisandro Dalcin 
5644165533cSJose E. Roman    Input Parameter:
565efe9872eSLisandro Dalcin .  dm - DM to be used with TS
566efe9872eSLisandro Dalcin 
5674165533cSJose E. Roman    Output Parameters:
568efe9872eSLisandro Dalcin +  fun - function evaluation function, see TSSetI2Function() for calling sequence
569efe9872eSLisandro Dalcin -  ctx - context for residual evaluation
570efe9872eSLisandro Dalcin 
571efe9872eSLisandro Dalcin    Level: advanced
572efe9872eSLisandro Dalcin 
573efe9872eSLisandro Dalcin    Note:
574efe9872eSLisandro Dalcin    TSGetI2Function() is normally used, but it calls this function internally because the user context is actually
575efe9872eSLisandro Dalcin    associated with the DM.
576efe9872eSLisandro Dalcin 
577c2e3fba1SPatrick Sanan .seealso: `DMTSSetI2Function()`, `TSGetI2Function()`
578efe9872eSLisandro Dalcin @*/
579efe9872eSLisandro Dalcin PetscErrorCode DMTSGetI2Function(DM dm,TSI2Function *fun,void **ctx)
580efe9872eSLisandro Dalcin {
581efe9872eSLisandro Dalcin   DMTS           tsdm;
582efe9872eSLisandro Dalcin 
583efe9872eSLisandro Dalcin   PetscFunctionBegin;
584efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5859566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
586efe9872eSLisandro Dalcin   if (fun) *fun = tsdm->ops->i2function;
587800f99ffSJeremy L Thompson   if (ctx) {
588800f99ffSJeremy L Thompson     if (tsdm->i2functionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->i2functionctxcontainer,ctx));
589800f99ffSJeremy L Thompson     else *ctx = NULL;
590800f99ffSJeremy L Thompson   }
591efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
592efe9872eSLisandro Dalcin }
593efe9872eSLisandro Dalcin 
594efe9872eSLisandro Dalcin /*@C
595efe9872eSLisandro Dalcin    DMTSSetI2Jacobian - set TS implicit Jacobian evaluation function for 2nd order systems
596efe9872eSLisandro Dalcin 
597efe9872eSLisandro Dalcin    Not Collective
598efe9872eSLisandro Dalcin 
5994165533cSJose E. Roman    Input Parameters:
600efe9872eSLisandro Dalcin +  dm - DM to be used with TS
601a96d6ef6SBarry Smith .  fun - Jacobian evaluation routine
602efe9872eSLisandro Dalcin -  ctx - context for Jacobian evaluation
603efe9872eSLisandro Dalcin 
604a96d6ef6SBarry Smith    Calling sequence of jac:
605a96d6ef6SBarry Smith $    PetscErrorCode jac(TS ts,PetscReal t,Vec U,Vec U_t,Vec U_tt,PetscReal v,PetscReal a,Mat J,Mat P,void *ctx);
606a96d6ef6SBarry Smith 
607a96d6ef6SBarry Smith +  t    - time at step/stage being solved
608a96d6ef6SBarry Smith .  U    - state vector
609a96d6ef6SBarry Smith .  U_t  - time derivative of state vector
610a96d6ef6SBarry Smith .  U_tt - second time derivative of state vector
611a96d6ef6SBarry Smith .  v    - shift for U_t
612a96d6ef6SBarry Smith .  a    - shift for U_tt
613a96d6ef6SBarry Smith .  J    - Jacobian of G(U) = F(t,U,W+v*U,W'+a*U), equivalent to dF/dU + v*dF/dU_t  + a*dF/dU_tt
614a96d6ef6SBarry Smith .  P    - preconditioning matrix for J, may be same as J
615a96d6ef6SBarry Smith -  ctx  - [optional] user-defined context for matrix evaluation routine
616a96d6ef6SBarry Smith 
617efe9872eSLisandro Dalcin    Level: advanced
618efe9872eSLisandro Dalcin 
619efe9872eSLisandro Dalcin    Note:
620efe9872eSLisandro Dalcin    TSSetI2Jacobian() is normally used, but it calls this function internally because the user context is actually
621efe9872eSLisandro Dalcin    associated with the DM.
622efe9872eSLisandro Dalcin 
623db781477SPatrick Sanan .seealso: `TSSetI2Jacobian()`
624efe9872eSLisandro Dalcin @*/
625efe9872eSLisandro Dalcin PetscErrorCode DMTSSetI2Jacobian(DM dm,TSI2Jacobian jac,void *ctx)
626efe9872eSLisandro Dalcin {
627efe9872eSLisandro Dalcin   DMTS           tsdm;
628efe9872eSLisandro Dalcin 
629efe9872eSLisandro Dalcin   PetscFunctionBegin;
630efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6319566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
632efe9872eSLisandro Dalcin   if (jac) tsdm->ops->i2jacobian = jac;
633800f99ffSJeremy L Thompson   if (ctx) {
634800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
635800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
636800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
637800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"i2jacobian ctx",(PetscObject)ctxcontainer));
638800f99ffSJeremy L Thompson     tsdm->i2jacobianctxcontainer = ctxcontainer;
639800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
640800f99ffSJeremy L Thompson   }
641800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
642800f99ffSJeremy L Thompson }
643800f99ffSJeremy L Thompson 
644800f99ffSJeremy L Thompson /*@C
6455cb80ecdSBarry Smith    DMTSSetI2JacobianContextDestroy - set `TS` implicit Jacobian evaluation for 2nd order systems context destroy function
646800f99ffSJeremy L Thompson 
647800f99ffSJeremy L Thompson    Not Collective
648800f99ffSJeremy L Thompson 
649800f99ffSJeremy L Thompson    Input Parameters:
6505cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
6515cb80ecdSBarry Smith -  f - implicit Jacobian evaluation context destroy function
652800f99ffSJeremy L Thompson 
653*87497f52SBarry Smith    Level: advanced
654*87497f52SBarry Smith 
655800f99ffSJeremy L Thompson .seealso: `TSSetI2JacobianContextDestroy()`, `DMTSSetI2Jacobian()`, `TSSetI2Jacobian()`
656800f99ffSJeremy L Thompson @*/
657800f99ffSJeremy L Thompson PetscErrorCode DMTSSetI2JacobianContextDestroy(DM dm,PetscErrorCode (*f)(void*))
658800f99ffSJeremy L Thompson {
659800f99ffSJeremy L Thompson   DMTS           tsdm;
660800f99ffSJeremy L Thompson 
661800f99ffSJeremy L Thompson   PetscFunctionBegin;
662800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
663800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
664800f99ffSJeremy L Thompson   if (tsdm->i2jacobianctxcontainer) PetscCall(PetscContainerSetUserDestroy(tsdm->i2jacobianctxcontainer,f));
665800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
666800f99ffSJeremy L Thompson }
667800f99ffSJeremy L Thompson 
668800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetI2JacobianContext_Internal(DM dm)
669800f99ffSJeremy L Thompson {
670800f99ffSJeremy L Thompson   DMTS           tsdm;
671800f99ffSJeremy L Thompson 
672800f99ffSJeremy L Thompson   PetscFunctionBegin;
673800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
674800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
675800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetI2JacobianContext_DMTS(tsdm));
676efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
677efe9872eSLisandro Dalcin }
678efe9872eSLisandro Dalcin 
679efe9872eSLisandro Dalcin /*@C
680efe9872eSLisandro Dalcin    DMTSGetI2Jacobian - get TS implicit Jacobian evaluation function for 2nd order systems
681efe9872eSLisandro Dalcin 
682efe9872eSLisandro Dalcin    Not Collective
683efe9872eSLisandro Dalcin 
6844165533cSJose E. Roman    Input Parameter:
685efe9872eSLisandro Dalcin .  dm - DM to be used with TS
686efe9872eSLisandro Dalcin 
6874165533cSJose E. Roman    Output Parameters:
688efe9872eSLisandro Dalcin +  jac - Jacobian evaluation function, see TSSetI2Jacobian() for calling sequence
689efe9872eSLisandro Dalcin -  ctx - context for Jacobian evaluation
690efe9872eSLisandro Dalcin 
691efe9872eSLisandro Dalcin    Level: advanced
692efe9872eSLisandro Dalcin 
693efe9872eSLisandro Dalcin    Note:
694efe9872eSLisandro Dalcin    TSGetI2Jacobian() is normally used, but it calls this function internally because the user context is actually
695efe9872eSLisandro Dalcin    associated with the DM.
696efe9872eSLisandro Dalcin 
697c2e3fba1SPatrick Sanan .seealso: `DMTSSetI2Jacobian()`, `TSGetI2Jacobian()`
698efe9872eSLisandro Dalcin @*/
699efe9872eSLisandro Dalcin PetscErrorCode DMTSGetI2Jacobian(DM dm,TSI2Jacobian *jac,void **ctx)
700efe9872eSLisandro Dalcin {
701efe9872eSLisandro Dalcin   DMTS           tsdm;
702efe9872eSLisandro Dalcin 
703efe9872eSLisandro Dalcin   PetscFunctionBegin;
704efe9872eSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7059566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
706efe9872eSLisandro Dalcin   if (jac) *jac = tsdm->ops->i2jacobian;
707800f99ffSJeremy L Thompson   if (ctx) {
708800f99ffSJeremy L Thompson     if (tsdm->i2jacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->i2jacobianctxcontainer,ctx));
709800f99ffSJeremy L Thompson     else *ctx = NULL;
710800f99ffSJeremy L Thompson   }
711efe9872eSLisandro Dalcin   PetscFunctionReturn(0);
712efe9872eSLisandro Dalcin }
71324989b8cSPeter Brune 
71424989b8cSPeter Brune /*@C
71524989b8cSPeter Brune    DMTSSetRHSFunction - set TS explicit residual evaluation function
71624989b8cSPeter Brune 
71724989b8cSPeter Brune    Not Collective
71824989b8cSPeter Brune 
7194165533cSJose E. Roman    Input Parameters:
72024989b8cSPeter Brune +  dm - DM to be used with TS
721a96d6ef6SBarry Smith .  func - RHS function evaluation routine
72224989b8cSPeter Brune -  ctx - context for residual evaluation
72324989b8cSPeter Brune 
724a96d6ef6SBarry Smith     Calling sequence of func:
725a96d6ef6SBarry Smith $     PetscErrorCode func(TS ts,PetscReal t,Vec u,Vec F,void *ctx);
726a96d6ef6SBarry Smith 
727a96d6ef6SBarry Smith +   ts - timestep context
728a96d6ef6SBarry Smith .   t - current timestep
729a96d6ef6SBarry Smith .   u - input vector
730a96d6ef6SBarry Smith .   F - function vector
731a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
732a96d6ef6SBarry Smith 
73324989b8cSPeter Brune    Level: advanced
73424989b8cSPeter Brune 
73524989b8cSPeter Brune    Note:
736800f99ffSJeremy L Thompson    TSSetRHSFunction() is normally used, but it calls this function internally because the user context is actually
73724989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
73824989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
73924989b8cSPeter Brune 
740db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetRHSFunction()`, `DMTSSetJacobian()`
74124989b8cSPeter Brune @*/
74224989b8cSPeter Brune PetscErrorCode DMTSSetRHSFunction(DM dm,TSRHSFunction func,void *ctx)
74324989b8cSPeter Brune {
744942e3340SBarry Smith   DMTS           tsdm;
74524989b8cSPeter Brune 
74624989b8cSPeter Brune   PetscFunctionBegin;
74724989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7489566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
749d74926cbSBarry Smith   if (func) tsdm->ops->rhsfunction = func;
750800f99ffSJeremy L Thompson   if (ctx) {
751800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
752800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
753800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
754800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"rhs function ctx",(PetscObject)ctxcontainer));
755800f99ffSJeremy L Thompson     tsdm->rhsfunctionctxcontainer = ctxcontainer;
756800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
757800f99ffSJeremy L Thompson   }
758800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
759800f99ffSJeremy L Thompson }
760800f99ffSJeremy L Thompson 
761800f99ffSJeremy L Thompson /*@C
7625cb80ecdSBarry Smith    DMTSSetRHSFunctionContextDestroy - set `TS` explicit residual evaluation context destroy function
763800f99ffSJeremy L Thompson 
764800f99ffSJeremy L Thompson    Not Collective
765800f99ffSJeremy L Thompson 
766800f99ffSJeremy L Thompson    Input Parameters:
7675cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
7685cb80ecdSBarry Smith -  f - explicit evaluation context destroy function
769800f99ffSJeremy L Thompson 
770800f99ffSJeremy L Thompson    Level: advanced
771800f99ffSJeremy L Thompson 
772800f99ffSJeremy L Thompson    Note:
7735cb80ecdSBarry Smith    `TSSetRHSFunctionContextDestroy()` is normally used, but it calls this function internally because the user context is actually
7745cb80ecdSBarry Smith    associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
7755cb80ecdSBarry Smith    not.
7765cb80ecdSBarry Smith 
7775cb80ecdSBarry Smith    Developer Note:
7785cb80ecdSBarry Smith    If `DM` took a more central role at some later date, this could become the primary method of setting the residual.
779800f99ffSJeremy L Thompson 
780800f99ffSJeremy L Thompson .seealso: `TSSetRHSFunctionContextDestroy()`, `DMTSSetRHSFunction()`, `TSSetRHSFunction()`
781800f99ffSJeremy L Thompson @*/
782800f99ffSJeremy L Thompson PetscErrorCode DMTSSetRHSFunctionContextDestroy(DM dm,PetscErrorCode (*f)(void*))
783800f99ffSJeremy L Thompson {
784800f99ffSJeremy L Thompson   DMTS           tsdm;
785800f99ffSJeremy L Thompson 
786800f99ffSJeremy L Thompson   PetscFunctionBegin;
787800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
788800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
789800f99ffSJeremy L Thompson   if (tsdm->rhsfunctionctxcontainer) PetscCall(PetscContainerSetUserDestroy(tsdm->rhsfunctionctxcontainer,f));
790800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
791800f99ffSJeremy L Thompson }
792800f99ffSJeremy L Thompson 
793800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetRHSFunctionContext_Internal(DM dm)
794800f99ffSJeremy L Thompson {
795800f99ffSJeremy L Thompson   DMTS           tsdm;
796800f99ffSJeremy L Thompson 
797800f99ffSJeremy L Thompson   PetscFunctionBegin;
798800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
799800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
800800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetRHSFunctionContext_DMTS(tsdm));
801800f99ffSJeremy L Thompson   tsdm->rhsfunctionctxcontainer = NULL;
80224989b8cSPeter Brune   PetscFunctionReturn(0);
80324989b8cSPeter Brune }
80424989b8cSPeter Brune 
805ef20d060SBarry Smith /*@C
806e3c11fc1SJed Brown    DMTSSetTransientVariable - sets function to transform from state to transient variables
807e3c11fc1SJed Brown 
808e3c11fc1SJed Brown    Logically Collective
809e3c11fc1SJed Brown 
8104165533cSJose E. Roman    Input Parameters:
811e3c11fc1SJed Brown +  dm - DM to be used with TS
812a96d6ef6SBarry Smith .  tvar - a function that transforms to transient variables
813e3c11fc1SJed Brown -  ctx - a context for tvar
814e3c11fc1SJed Brown 
815a96d6ef6SBarry Smith     Calling sequence of tvar:
816a96d6ef6SBarry Smith $     PetscErrorCode tvar(TS ts,Vec p,Vec c,void *ctx);
817a96d6ef6SBarry Smith 
818a96d6ef6SBarry Smith +   ts - timestep context
8196aad120cSJose E. Roman .   p - input vector (primitive form)
820a96d6ef6SBarry Smith .   c - output vector, transient variables (conservative form)
821a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
822a96d6ef6SBarry Smith 
823e3c11fc1SJed Brown    Level: advanced
824e3c11fc1SJed Brown 
825e3c11fc1SJed Brown    Notes:
826e3c11fc1SJed Brown    This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., TSBDF)
827e3c11fc1SJed Brown    can be conservative.  In this context, primitive variables P are used to model the state (e.g., because they lead to
828e3c11fc1SJed Brown    well-conditioned formulations even in limiting cases such as low-Mach or zero porosity).  The transient variable is
829e3c11fc1SJed Brown    C(P), specified by calling this function.  An IFunction thus receives arguments (P, Cdot) and the IJacobian must be
830e3c11fc1SJed Brown    evaluated via the chain rule, as in
831e3c11fc1SJed Brown 
832e3c11fc1SJed Brown      dF/dP + shift * dF/dCdot dC/dP.
833e3c11fc1SJed Brown 
834db781477SPatrick Sanan .seealso: `TSSetTransientVariable()`, `DMTSGetTransientVariable()`, `DMTSSetIFunction()`, `DMTSSetIJacobian()`
835e3c11fc1SJed Brown @*/
836e3c11fc1SJed Brown PetscErrorCode DMTSSetTransientVariable(DM dm,TSTransientVariable tvar,void *ctx)
837e3c11fc1SJed Brown {
838e3c11fc1SJed Brown   DMTS           dmts;
839e3c11fc1SJed Brown 
840e3c11fc1SJed Brown   PetscFunctionBegin;
841e3c11fc1SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8429566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&dmts));
843e3c11fc1SJed Brown   dmts->ops->transientvar = tvar;
844e3c11fc1SJed Brown   dmts->transientvarctx = ctx;
845e3c11fc1SJed Brown   PetscFunctionReturn(0);
846e3c11fc1SJed Brown }
847e3c11fc1SJed Brown 
848e3c11fc1SJed Brown /*@C
849e3c11fc1SJed Brown    DMTSGetTransientVariable - gets function to transform from state to transient variables
850e3c11fc1SJed Brown 
851e3c11fc1SJed Brown    Logically Collective
852e3c11fc1SJed Brown 
8534165533cSJose E. Roman    Input Parameter:
854e3c11fc1SJed Brown .  dm - DM to be used with TS
855e3c11fc1SJed Brown 
8564165533cSJose E. Roman    Output Parameters:
857a96d6ef6SBarry Smith +  tvar - a function that transforms to transient variables
858e3c11fc1SJed Brown -  ctx - a context for tvar
859e3c11fc1SJed Brown 
860e3c11fc1SJed Brown    Level: advanced
861e3c11fc1SJed Brown 
862db781477SPatrick Sanan .seealso: `DMTSSetTransientVariable()`, `DMTSGetIFunction()`, `DMTSGetIJacobian()`
863e3c11fc1SJed Brown @*/
864e3c11fc1SJed Brown PetscErrorCode DMTSGetTransientVariable(DM dm,TSTransientVariable *tvar,void *ctx)
865e3c11fc1SJed Brown {
866e3c11fc1SJed Brown   DMTS           dmts;
867e3c11fc1SJed Brown 
868e3c11fc1SJed Brown   PetscFunctionBegin;
869e3c11fc1SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8709566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&dmts));
871e3c11fc1SJed Brown   if (tvar) *tvar = dmts->ops->transientvar;
872e3c11fc1SJed Brown   if (ctx)  *(void**)ctx = dmts->transientvarctx;
873e3c11fc1SJed Brown   PetscFunctionReturn(0);
874e3c11fc1SJed Brown }
875e3c11fc1SJed Brown 
876e3c11fc1SJed Brown /*@C
877ef20d060SBarry Smith    DMTSGetSolutionFunction - gets the TS solution evaluation function
878ef20d060SBarry Smith 
879ef20d060SBarry Smith    Not Collective
880ef20d060SBarry Smith 
8814165533cSJose E. Roman    Input Parameter:
882ef20d060SBarry Smith .  dm - DM to be used with TS
883ef20d060SBarry Smith 
884ef20d060SBarry Smith    Output Parameters:
885ef20d060SBarry Smith +  func - solution function evaluation function, see TSSetSolution() for calling sequence
886ef20d060SBarry Smith -  ctx - context for solution evaluation
887ef20d060SBarry Smith 
888ef20d060SBarry Smith    Level: advanced
889ef20d060SBarry Smith 
890db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`, `DMTSSetSolutionFunction()`
891ef20d060SBarry Smith @*/
892ef20d060SBarry Smith PetscErrorCode DMTSGetSolutionFunction(DM dm,TSSolutionFunction *func,void **ctx)
893ef20d060SBarry Smith {
894942e3340SBarry Smith   DMTS           tsdm;
895ef20d060SBarry Smith 
896ef20d060SBarry Smith   PetscFunctionBegin;
897ef20d060SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8989566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
899d74926cbSBarry Smith   if (func) *func = tsdm->ops->solution;
900ef20d060SBarry Smith   if (ctx)  *ctx  = tsdm->solutionctx;
901ef20d060SBarry Smith   PetscFunctionReturn(0);
902ef20d060SBarry Smith }
903ef20d060SBarry Smith 
904ef20d060SBarry Smith /*@C
905ef20d060SBarry Smith    DMTSSetSolutionFunction - set TS solution evaluation function
906ef20d060SBarry Smith 
907ef20d060SBarry Smith    Not Collective
908ef20d060SBarry Smith 
9094165533cSJose E. Roman    Input Parameters:
910ef20d060SBarry Smith +  dm - DM to be used with TS
911a96d6ef6SBarry Smith .  func - solution function evaluation routine
912ef20d060SBarry Smith -  ctx - context for solution evaluation
913ef20d060SBarry Smith 
914a96d6ef6SBarry Smith     Calling sequence of f:
915a96d6ef6SBarry Smith $     PetscErrorCode f(TS ts,PetscReal t,Vec u,void *ctx);
916a96d6ef6SBarry Smith 
917a96d6ef6SBarry Smith +   ts - timestep context
918a96d6ef6SBarry Smith .   t - current timestep
919a96d6ef6SBarry Smith .   u - output vector
920a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
921a96d6ef6SBarry Smith 
922ef20d060SBarry Smith    Level: advanced
923ef20d060SBarry Smith 
924ef20d060SBarry Smith    Note:
925ef20d060SBarry Smith    TSSetSolutionFunction() is normally used, but it calls this function internally because the user context is actually
926ef20d060SBarry Smith    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
927ef20d060SBarry Smith    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
928ef20d060SBarry Smith 
929db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`, `DMTSGetSolutionFunction()`
930ef20d060SBarry Smith @*/
931ef20d060SBarry Smith PetscErrorCode DMTSSetSolutionFunction(DM dm,TSSolutionFunction func,void *ctx)
932ef20d060SBarry Smith {
933942e3340SBarry Smith   DMTS           tsdm;
934ef20d060SBarry Smith 
935ef20d060SBarry Smith   PetscFunctionBegin;
936ef20d060SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9379566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
938d74926cbSBarry Smith   if (func) tsdm->ops->solution = func;
939ef20d060SBarry Smith   if (ctx)  tsdm->solutionctx   = ctx;
940ef20d060SBarry Smith   PetscFunctionReturn(0);
941ef20d060SBarry Smith }
942ef20d060SBarry Smith 
9439b7cd975SBarry Smith /*@C
9449b7cd975SBarry Smith    DMTSSetForcingFunction - set TS forcing function evaluation function
9459b7cd975SBarry Smith 
9469b7cd975SBarry Smith    Not Collective
9479b7cd975SBarry Smith 
9484165533cSJose E. Roman    Input Parameters:
9499b7cd975SBarry Smith +  dm - DM to be used with TS
950a96d6ef6SBarry Smith .  f - forcing function evaluation routine
9519b7cd975SBarry Smith -  ctx - context for solution evaluation
9529b7cd975SBarry Smith 
953a96d6ef6SBarry Smith     Calling sequence of func:
954a96d6ef6SBarry Smith $     PetscErrorCode func (TS ts,PetscReal t,Vec f,void *ctx);
955a96d6ef6SBarry Smith 
956a96d6ef6SBarry Smith +   ts - timestep context
957a96d6ef6SBarry Smith .   t - current timestep
958a96d6ef6SBarry Smith .   f - output vector
959a96d6ef6SBarry Smith -   ctx - [optional] user-defined function context
960a96d6ef6SBarry Smith 
9619b7cd975SBarry Smith    Level: advanced
9629b7cd975SBarry Smith 
9639b7cd975SBarry Smith    Note:
9649b7cd975SBarry Smith    TSSetForcingFunction() is normally used, but it calls this function internally because the user context is actually
9659b7cd975SBarry Smith    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
9669b7cd975SBarry Smith    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
9679b7cd975SBarry Smith 
968db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`, `TSSetForcingFunction()`, `DMTSGetForcingFunction()`
9699b7cd975SBarry Smith @*/
970d56366bfSLisandro Dalcin PetscErrorCode DMTSSetForcingFunction(DM dm,TSForcingFunction f,void *ctx)
9719b7cd975SBarry Smith {
9729b7cd975SBarry Smith   DMTS           tsdm;
9739b7cd975SBarry Smith 
9749b7cd975SBarry Smith   PetscFunctionBegin;
9759b7cd975SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9769566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
977f8b49ee9SBarry Smith   if (f)    tsdm->ops->forcing = f;
9789b7cd975SBarry Smith   if (ctx)  tsdm->forcingctx   = ctx;
9799b7cd975SBarry Smith   PetscFunctionReturn(0);
9809b7cd975SBarry Smith }
9819b7cd975SBarry Smith 
9829b7cd975SBarry Smith /*@C
9839b7cd975SBarry Smith    DMTSGetForcingFunction - get TS forcing function evaluation function
9849b7cd975SBarry Smith 
9859b7cd975SBarry Smith    Not Collective
9869b7cd975SBarry Smith 
9874165533cSJose E. Roman    Input Parameter:
9889b7cd975SBarry Smith .   dm - DM to be used with TS
9899b7cd975SBarry Smith 
9904165533cSJose E. Roman    Output Parameters:
991f8b49ee9SBarry Smith +  f - forcing function evaluation function; see TSForcingFunction for details
9929b7cd975SBarry Smith -  ctx - context for solution evaluation
9939b7cd975SBarry Smith 
9949b7cd975SBarry Smith    Level: advanced
9959b7cd975SBarry Smith 
9969b7cd975SBarry Smith    Note:
9979b7cd975SBarry Smith    TSSetForcingFunction() is normally used, but it calls this function internally because the user context is actually
9989b7cd975SBarry Smith    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
9999b7cd975SBarry Smith    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
10009b7cd975SBarry Smith 
1001db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`, `TSSetForcingFunction()`, `DMTSGetForcingFunction()`
10029b7cd975SBarry Smith @*/
1003d56366bfSLisandro Dalcin PetscErrorCode DMTSGetForcingFunction(DM dm,TSForcingFunction *f,void **ctx)
10049b7cd975SBarry Smith {
10059b7cd975SBarry Smith   DMTS           tsdm;
10069b7cd975SBarry Smith 
10079b7cd975SBarry Smith   PetscFunctionBegin;
10089b7cd975SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10099566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1010f8b49ee9SBarry Smith   if (f)   *f   = tsdm->ops->forcing;
10119b7cd975SBarry Smith   if (ctx) *ctx = tsdm->forcingctx;
10129b7cd975SBarry Smith   PetscFunctionReturn(0);
10139b7cd975SBarry Smith }
10149b7cd975SBarry Smith 
101524989b8cSPeter Brune /*@C
101624989b8cSPeter Brune    DMTSGetRHSFunction - get TS explicit residual evaluation function
101724989b8cSPeter Brune 
101824989b8cSPeter Brune    Not Collective
101924989b8cSPeter Brune 
10204165533cSJose E. Roman    Input Parameter:
102124989b8cSPeter Brune .  dm - DM to be used with TS
102224989b8cSPeter Brune 
10234165533cSJose E. Roman    Output Parameters:
102424989b8cSPeter Brune +  func - residual evaluation function, see TSSetRHSFunction() for calling sequence
102524989b8cSPeter Brune -  ctx - context for residual evaluation
102624989b8cSPeter Brune 
102724989b8cSPeter Brune    Level: advanced
102824989b8cSPeter Brune 
102924989b8cSPeter Brune    Note:
103024989b8cSPeter Brune    TSGetFunction() is normally used, but it calls this function internally because the user context is actually
103124989b8cSPeter Brune    associated with the DM.
103224989b8cSPeter Brune 
1033db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `DMTSSetRHSFunction()`, `TSSetRHSFunction()`
103424989b8cSPeter Brune @*/
103524989b8cSPeter Brune PetscErrorCode DMTSGetRHSFunction(DM dm,TSRHSFunction *func,void **ctx)
103624989b8cSPeter Brune {
1037942e3340SBarry Smith   DMTS           tsdm;
103824989b8cSPeter Brune 
103924989b8cSPeter Brune   PetscFunctionBegin;
104024989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10419566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
1042d74926cbSBarry Smith   if (func) *func = tsdm->ops->rhsfunction;
1043800f99ffSJeremy L Thompson   if (ctx) {
1044800f99ffSJeremy L Thompson     if (tsdm->rhsfunctionctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->rhsfunctionctxcontainer,ctx));
1045800f99ffSJeremy L Thompson     else *ctx = NULL;
1046800f99ffSJeremy L Thompson   }
104724989b8cSPeter Brune   PetscFunctionReturn(0);
104824989b8cSPeter Brune }
104924989b8cSPeter Brune 
105024989b8cSPeter Brune /*@C
105124989b8cSPeter Brune    DMTSSetIJacobian - set TS Jacobian evaluation function
105224989b8cSPeter Brune 
105324989b8cSPeter Brune    Not Collective
105424989b8cSPeter Brune 
10554165533cSJose E. Roman    Input Parameters:
105624989b8cSPeter Brune +  dm - DM to be used with TS
1057a96d6ef6SBarry Smith .  func - Jacobian evaluation routine
105824989b8cSPeter Brune -  ctx - context for residual evaluation
105924989b8cSPeter Brune 
1060a96d6ef6SBarry Smith    Calling sequence of f:
1061a96d6ef6SBarry Smith $    PetscErrorCode f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1062a96d6ef6SBarry Smith 
1063a96d6ef6SBarry Smith +  t    - time at step/stage being solved
1064a96d6ef6SBarry Smith .  U    - state vector
1065a96d6ef6SBarry Smith .  U_t  - time derivative of state vector
1066a96d6ef6SBarry Smith .  a    - shift
1067a96d6ef6SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1068a96d6ef6SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1069a96d6ef6SBarry Smith -  ctx  - [optional] user-defined context for matrix evaluation routine
1070a96d6ef6SBarry Smith 
107124989b8cSPeter Brune    Level: advanced
107224989b8cSPeter Brune 
107324989b8cSPeter Brune    Note:
107424989b8cSPeter Brune    TSSetJacobian() is normally used, but it calls this function internally because the user context is actually
107524989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
107624989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
107724989b8cSPeter Brune 
1078db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetRHSFunction()`, `DMTSGetJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()`
107924989b8cSPeter Brune @*/
108024989b8cSPeter Brune PetscErrorCode DMTSSetIJacobian(DM dm,TSIJacobian func,void *ctx)
108124989b8cSPeter Brune {
1082800f99ffSJeremy L Thompson   DMTS           tsdm;
108324989b8cSPeter Brune 
108424989b8cSPeter Brune   PetscFunctionBegin;
108524989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1086800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1087800f99ffSJeremy L Thompson   if (func) tsdm->ops->ijacobian = func;
1088800f99ffSJeremy L Thompson   if (ctx) {
1089800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
1090800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
1091800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
1092800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"ijacobian ctx",(PetscObject)ctxcontainer));
1093800f99ffSJeremy L Thompson     tsdm->ijacobianctxcontainer = ctxcontainer;
1094800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
1095800f99ffSJeremy L Thompson   }
1096800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
1097800f99ffSJeremy L Thompson }
1098800f99ffSJeremy L Thompson 
1099800f99ffSJeremy L Thompson /*@C
11005cb80ecdSBarry Smith    DMTSSetIJacobianContextDestroy - set `TS` Jacobian evaluation context destroy function
1101800f99ffSJeremy L Thompson 
1102800f99ffSJeremy L Thompson    Not Collective
1103800f99ffSJeremy L Thompson 
1104800f99ffSJeremy L Thompson    Input Parameters:
11055cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
11065cb80ecdSBarry Smith -  f - Jacobian evaluation context destroy function
1107800f99ffSJeremy L Thompson 
1108800f99ffSJeremy L Thompson    Level: advanced
1109800f99ffSJeremy L Thompson 
1110800f99ffSJeremy L Thompson    Note:
11115cb80ecdSBarry Smith    `TSSetIJacobianContextDestroy()` is normally used, but it calls this function internally because the user context is actually
11125cb80ecdSBarry Smith    associated with the `DM`.  This makes the interface consistent regardless of whether the user interacts with a `DM` or
11135cb80ecdSBarry Smith    not.
1114800f99ffSJeremy L Thompson 
11155cb80ecdSBarry Smith    Developer Note:
11165cb80ecdSBarry Smith    If `DM` took a more central role at some later date, this could become the primary method of setting the Jacobian.
11175cb80ecdSBarry Smith 
11185cb80ecdSBarry Smith .seealso: `TSSetIJacobianContextDestroy()`, `TSSetI2JacobianContextDestroy()`, `DMTSSetIJacobian()`, `TSSetIJacobian()`
1119800f99ffSJeremy L Thompson @*/
1120800f99ffSJeremy L Thompson PetscErrorCode DMTSSetIJacobianContextDestroy(DM dm,PetscErrorCode (*f)(void*))
1121800f99ffSJeremy L Thompson {
1122800f99ffSJeremy L Thompson   DMTS           tsdm;
1123800f99ffSJeremy L Thompson 
1124800f99ffSJeremy L Thompson   PetscFunctionBegin;
1125800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1126800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1127800f99ffSJeremy L Thompson   if (tsdm->ijacobianctxcontainer) PetscCall(PetscContainerSetUserDestroy(tsdm->ijacobianctxcontainer,f));
1128800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
1129800f99ffSJeremy L Thompson }
1130800f99ffSJeremy L Thompson 
1131800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetIJacobianContext_Internal(DM dm)
1132800f99ffSJeremy L Thompson {
1133800f99ffSJeremy L Thompson   DMTS           tsdm;
1134800f99ffSJeremy L Thompson 
1135800f99ffSJeremy L Thompson   PetscFunctionBegin;
1136800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1137800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1138800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetIJacobianContext_DMTS(tsdm));
113924989b8cSPeter Brune   PetscFunctionReturn(0);
114024989b8cSPeter Brune }
114124989b8cSPeter Brune 
114224989b8cSPeter Brune /*@C
114324989b8cSPeter Brune    DMTSGetIJacobian - get TS Jacobian evaluation function
114424989b8cSPeter Brune 
114524989b8cSPeter Brune    Not Collective
114624989b8cSPeter Brune 
11474165533cSJose E. Roman    Input Parameter:
114824989b8cSPeter Brune .  dm - DM to be used with TS
114924989b8cSPeter Brune 
11504165533cSJose E. Roman    Output Parameters:
115124989b8cSPeter Brune +  func - Jacobian evaluation function, see TSSetIJacobian() for calling sequence
115224989b8cSPeter Brune -  ctx - context for residual evaluation
115324989b8cSPeter Brune 
115424989b8cSPeter Brune    Level: advanced
115524989b8cSPeter Brune 
115624989b8cSPeter Brune    Note:
115724989b8cSPeter Brune    TSGetJacobian() is normally used, but it calls this function internally because the user context is actually
115824989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
115924989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
116024989b8cSPeter Brune 
1161db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`
116224989b8cSPeter Brune @*/
116324989b8cSPeter Brune PetscErrorCode DMTSGetIJacobian(DM dm,TSIJacobian *func,void **ctx)
116424989b8cSPeter Brune {
1165942e3340SBarry Smith   DMTS           tsdm;
116624989b8cSPeter Brune 
116724989b8cSPeter Brune   PetscFunctionBegin;
116824989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
11699566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
1170d74926cbSBarry Smith   if (func) *func = tsdm->ops->ijacobian;
1171800f99ffSJeremy L Thompson   if (ctx) {
1172800f99ffSJeremy L Thompson     if (tsdm->ijacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->ijacobianctxcontainer,ctx));
1173800f99ffSJeremy L Thompson     else *ctx = NULL;
1174800f99ffSJeremy L Thompson   }
117524989b8cSPeter Brune   PetscFunctionReturn(0);
117624989b8cSPeter Brune }
117724989b8cSPeter Brune 
117824989b8cSPeter Brune /*@C
117924989b8cSPeter Brune    DMTSSetRHSJacobian - set TS Jacobian evaluation function
118024989b8cSPeter Brune 
118124989b8cSPeter Brune    Not Collective
118224989b8cSPeter Brune 
11834165533cSJose E. Roman    Input Parameters:
118424989b8cSPeter Brune +  dm - DM to be used with TS
1185a96d6ef6SBarry Smith .  func - Jacobian evaluation routine
118624989b8cSPeter Brune -  ctx - context for residual evaluation
118724989b8cSPeter Brune 
1188a96d6ef6SBarry Smith    Calling sequence of func:
1189a96d6ef6SBarry Smith $     PetscErrorCode func(TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1190a96d6ef6SBarry Smith 
1191a96d6ef6SBarry Smith +  t - current timestep
1192a96d6ef6SBarry Smith .  u - input vector
1193a96d6ef6SBarry Smith .  Amat - (approximate) Jacobian matrix
1194a96d6ef6SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1195a96d6ef6SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1196a96d6ef6SBarry Smith 
119724989b8cSPeter Brune    Level: advanced
119824989b8cSPeter Brune 
119924989b8cSPeter Brune    Note:
120024989b8cSPeter Brune    TSSetJacobian() is normally used, but it calls this function internally because the user context is actually
120124989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
120224989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
120324989b8cSPeter Brune 
1204db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSGetJacobian()`, `TSSetRHSJacobian()`
120524989b8cSPeter Brune @*/
120624989b8cSPeter Brune PetscErrorCode DMTSSetRHSJacobian(DM dm,TSRHSJacobian func,void *ctx)
120724989b8cSPeter Brune {
1208942e3340SBarry Smith   DMTS           tsdm;
120924989b8cSPeter Brune 
121024989b8cSPeter Brune   PetscFunctionBegin;
121124989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12129566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1213d74926cbSBarry Smith   if (func) tsdm->ops->rhsjacobian = func;
1214800f99ffSJeremy L Thompson   if (ctx) {
1215800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
1216800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)tsdm),&ctxcontainer));
1217800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
1218800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)tsdm,"rhs jacobian ctx",(PetscObject)ctxcontainer));
1219800f99ffSJeremy L Thompson     tsdm->rhsjacobianctxcontainer = ctxcontainer;
1220800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
1221800f99ffSJeremy L Thompson   }
1222800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
1223800f99ffSJeremy L Thompson }
1224800f99ffSJeremy L Thompson 
1225800f99ffSJeremy L Thompson /*@C
12265cb80ecdSBarry Smith    DMTSSetRHSJacobianContextDestroy - set `TS` Jacobian evaluation context destroy function
1227800f99ffSJeremy L Thompson 
1228800f99ffSJeremy L Thompson    Not Collective
1229800f99ffSJeremy L Thompson 
1230800f99ffSJeremy L Thompson    Input Parameters:
12315cb80ecdSBarry Smith +  dm - `DM` to be used with `TS`
12325cb80ecdSBarry Smith -  f - Jacobian evaluation context destroy function
12335cb80ecdSBarry Smith 
12345cb80ecdSBarry Smith    Level: advanced
12355cb80ecdSBarry Smith 
12365cb80ecdSBarry Smith    Note:
12375cb80ecdSBarry Smith    The user usually calls `TSSetRHSJacobianContextDestroy()` which calls this routine
1238800f99ffSJeremy L Thompson 
1239*87497f52SBarry Smith    Level: advanced
1240*87497f52SBarry Smith 
1241800f99ffSJeremy L Thompson .seealso: `TSSetRHSJacobianContextDestroy()`, `DMTSSetRHSJacobian()`, `TSSetRHSJacobian()`
1242800f99ffSJeremy L Thompson @*/
1243800f99ffSJeremy L Thompson PetscErrorCode DMTSSetRHSJacobianContextDestroy(DM dm,PetscErrorCode (*f)(void*))
1244800f99ffSJeremy L Thompson {
1245800f99ffSJeremy L Thompson   DMTS           tsdm;
1246800f99ffSJeremy L Thompson 
1247800f99ffSJeremy L Thompson   PetscFunctionBegin;
1248800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1249800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1250800f99ffSJeremy L Thompson   if (tsdm->rhsjacobianctxcontainer) PetscCall(PetscContainerSetUserDestroy(tsdm->rhsjacobianctxcontainer,f));
1251800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
1252800f99ffSJeremy L Thompson }
1253800f99ffSJeremy L Thompson 
1254800f99ffSJeremy L Thompson PetscErrorCode DMTSUnsetRHSJacobianContext_Internal(DM dm)
1255800f99ffSJeremy L Thompson {
1256800f99ffSJeremy L Thompson   DMTS           tsdm;
1257800f99ffSJeremy L Thompson 
1258800f99ffSJeremy L Thompson   PetscFunctionBegin;
1259800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1260800f99ffSJeremy L Thompson   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1261800f99ffSJeremy L Thompson   PetscCall(DMTSUnsetRHSJacobianContext_DMTS(tsdm));
126224989b8cSPeter Brune   PetscFunctionReturn(0);
126324989b8cSPeter Brune }
126424989b8cSPeter Brune 
126524989b8cSPeter Brune /*@C
126624989b8cSPeter Brune    DMTSGetRHSJacobian - get TS Jacobian evaluation function
126724989b8cSPeter Brune 
126824989b8cSPeter Brune    Not Collective
126924989b8cSPeter Brune 
12704165533cSJose E. Roman    Input Parameter:
127124989b8cSPeter Brune .  dm - DM to be used with TS
127224989b8cSPeter Brune 
12734165533cSJose E. Roman    Output Parameters:
127424989b8cSPeter Brune +  func - Jacobian evaluation function, see TSSetRHSJacobian() for calling sequence
127524989b8cSPeter Brune -  ctx - context for residual evaluation
127624989b8cSPeter Brune 
127724989b8cSPeter Brune    Level: advanced
127824989b8cSPeter Brune 
127924989b8cSPeter Brune    Note:
128024989b8cSPeter Brune    TSGetJacobian() is normally used, but it calls this function internally because the user context is actually
128124989b8cSPeter Brune    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
128224989b8cSPeter Brune    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
128324989b8cSPeter Brune 
1284db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetRHSFunction()`, `DMTSSetRHSJacobian()`, `TSSetRHSJacobian()`
128524989b8cSPeter Brune @*/
128624989b8cSPeter Brune PetscErrorCode DMTSGetRHSJacobian(DM dm,TSRHSJacobian *func,void **ctx)
128724989b8cSPeter Brune {
1288942e3340SBarry Smith   DMTS           tsdm;
128924989b8cSPeter Brune 
129024989b8cSPeter Brune   PetscFunctionBegin;
129124989b8cSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12929566063dSJacob Faibussowitsch   PetscCall(DMGetDMTS(dm,&tsdm));
1293d74926cbSBarry Smith   if (func) *func = tsdm->ops->rhsjacobian;
1294800f99ffSJeremy L Thompson   if (ctx) {
1295800f99ffSJeremy L Thompson     if (tsdm->rhsjacobianctxcontainer) PetscCall(PetscContainerGetPointer(tsdm->rhsjacobianctxcontainer,ctx));
1296800f99ffSJeremy L Thompson     else *ctx = NULL;
1297800f99ffSJeremy L Thompson   }
129824989b8cSPeter Brune   PetscFunctionReturn(0);
129924989b8cSPeter Brune }
1300ad6bc421SBarry Smith 
1301ad6bc421SBarry Smith /*@C
1302ad6bc421SBarry Smith    DMTSSetIFunctionSerialize - sets functions used to view and load a IFunction context
1303ad6bc421SBarry Smith 
1304ad6bc421SBarry Smith    Not Collective
1305ad6bc421SBarry Smith 
13064165533cSJose E. Roman    Input Parameters:
1307ad6bc421SBarry Smith +  dm - DM to be used with TS
1308ad6bc421SBarry Smith .  view - viewer function
1309ad6bc421SBarry Smith -  load - loading function
1310ad6bc421SBarry Smith 
1311ad6bc421SBarry Smith    Level: advanced
1312ad6bc421SBarry Smith 
1313db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`
1314ad6bc421SBarry Smith @*/
1315ad6bc421SBarry Smith PetscErrorCode DMTSSetIFunctionSerialize(DM dm,PetscErrorCode (*view)(void*,PetscViewer),PetscErrorCode (*load)(void**,PetscViewer))
1316ad6bc421SBarry Smith {
1317ad6bc421SBarry Smith   DMTS           tsdm;
1318ad6bc421SBarry Smith 
1319ad6bc421SBarry Smith   PetscFunctionBegin;
1320ad6bc421SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13219566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1322ad6bc421SBarry Smith   tsdm->ops->ifunctionview = view;
1323ad6bc421SBarry Smith   tsdm->ops->ifunctionload = load;
1324ad6bc421SBarry Smith   PetscFunctionReturn(0);
1325ad6bc421SBarry Smith }
1326ad6bc421SBarry Smith 
1327ad6bc421SBarry Smith /*@C
1328ad6bc421SBarry Smith    DMTSSetIJacobianSerialize - sets functions used to view and load a IJacobian context
1329ad6bc421SBarry Smith 
1330ad6bc421SBarry Smith    Not Collective
1331ad6bc421SBarry Smith 
13324165533cSJose E. Roman    Input Parameters:
1333ad6bc421SBarry Smith +  dm - DM to be used with TS
1334ad6bc421SBarry Smith .  view - viewer function
1335ad6bc421SBarry Smith -  load - loading function
1336ad6bc421SBarry Smith 
1337ad6bc421SBarry Smith    Level: advanced
1338ad6bc421SBarry Smith 
1339db781477SPatrick Sanan .seealso: `DMTSSetContext()`, `TSSetFunction()`, `DMTSSetJacobian()`
1340ad6bc421SBarry Smith @*/
1341ad6bc421SBarry Smith PetscErrorCode DMTSSetIJacobianSerialize(DM dm,PetscErrorCode (*view)(void*,PetscViewer),PetscErrorCode (*load)(void**,PetscViewer))
1342ad6bc421SBarry Smith {
1343ad6bc421SBarry Smith   DMTS           tsdm;
1344ad6bc421SBarry Smith 
1345ad6bc421SBarry Smith   PetscFunctionBegin;
1346ad6bc421SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13479566063dSJacob Faibussowitsch   PetscCall(DMGetDMTSWrite(dm,&tsdm));
1348ad6bc421SBarry Smith   tsdm->ops->ijacobianview = view;
1349ad6bc421SBarry Smith   tsdm->ops->ijacobianload = load;
1350ad6bc421SBarry Smith   PetscFunctionReturn(0);
1351ad6bc421SBarry Smith }
1352