xref: /petsc/src/snes/utils/dmsnes.c (revision dbbe0bcd3f3a8fbab5a45420dc06f8387e5764c6)
1af0996ceSBarry Smith #include <petsc/private/snesimpl.h>   /*I "petscsnes.h" I*/
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>     /*I "petscdm.h" I*/
36cab3a1bSJed Brown 
4800f99ffSJeremy L Thompson static PetscErrorCode DMSNESUnsetFunctionContext_DMSNES(DMSNES sdm)
5800f99ffSJeremy L Thompson {
6800f99ffSJeremy L Thompson   PetscFunctionBegin;
7800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)sdm,"function ctx",NULL));
8800f99ffSJeremy L Thompson   sdm->functionctxcontainer = NULL;
9800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
10800f99ffSJeremy L Thompson }
11800f99ffSJeremy L Thompson 
12800f99ffSJeremy L Thompson static PetscErrorCode DMSNESUnsetJacobianContext_DMSNES(DMSNES sdm)
13800f99ffSJeremy L Thompson {
14800f99ffSJeremy L Thompson   PetscFunctionBegin;
15800f99ffSJeremy L Thompson   PetscCall(PetscObjectCompose((PetscObject)sdm,"jacobian ctx",NULL));
16800f99ffSJeremy L Thompson   sdm->jacobianctxcontainer = NULL;
17800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
18800f99ffSJeremy L Thompson }
19800f99ffSJeremy L Thompson 
2022c6f798SBarry Smith static PetscErrorCode DMSNESDestroy(DMSNES *kdm)
216cab3a1bSJed Brown {
226cab3a1bSJed Brown   PetscFunctionBegin;
2322c6f798SBarry Smith   if (!*kdm) PetscFunctionReturn(0);
2422c6f798SBarry Smith   PetscValidHeaderSpecific((*kdm),DMSNES_CLASSID,1);
259e5d0892SLisandro Dalcin   if (--((PetscObject)(*kdm))->refct > 0) {*kdm = NULL; PetscFunctionReturn(0);}
26800f99ffSJeremy L Thompson   PetscCall(DMSNESUnsetFunctionContext_DMSNES(*kdm));
27800f99ffSJeremy L Thompson   PetscCall(DMSNESUnsetJacobianContext_DMSNES(*kdm));
289566063dSJacob Faibussowitsch   if ((*kdm)->ops->destroy) PetscCall(((*kdm)->ops->destroy)(*kdm));
299566063dSJacob Faibussowitsch   PetscCall(PetscHeaderDestroy(kdm));
3022c6f798SBarry Smith   PetscFunctionReturn(0);
3122c6f798SBarry Smith }
3222c6f798SBarry Smith 
332d53ad75SBarry Smith PetscErrorCode DMSNESLoad(DMSNES kdm,PetscViewer viewer)
342d53ad75SBarry Smith {
352d53ad75SBarry Smith   PetscFunctionBegin;
369566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->computefunction,1,NULL,PETSC_FUNCTION));
379566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryRead(viewer,&kdm->ops->computejacobian,1,NULL,PETSC_FUNCTION));
382d53ad75SBarry Smith   PetscFunctionReturn(0);
392d53ad75SBarry Smith }
402d53ad75SBarry Smith 
412d53ad75SBarry Smith PetscErrorCode DMSNESView(DMSNES kdm,PetscViewer viewer)
422d53ad75SBarry Smith {
432d53ad75SBarry Smith   PetscBool      isascii,isbinary;
442d53ad75SBarry Smith 
452d53ad75SBarry Smith   PetscFunctionBegin;
469566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii));
479566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary));
482d53ad75SBarry Smith   if (isascii) {
494b5d3663SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS) && defined(PETSC_SERIALIZE_FUNCTIONS_VIEW)
502d53ad75SBarry Smith     const char *fname;
512d53ad75SBarry Smith 
529566063dSJacob Faibussowitsch     PetscCall(PetscFPTFind(kdm->ops->computefunction,&fname));
532d53ad75SBarry Smith     if (fname) {
549566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Function used by SNES: %s\n",fname));
552d53ad75SBarry Smith     }
569566063dSJacob Faibussowitsch     PetscCall(PetscFPTFind(kdm->ops->computejacobian,&fname));
572d53ad75SBarry Smith     if (fname) {
589566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIIPrintf(viewer,"Jacobian function used by SNES: %s\n",fname));
592d53ad75SBarry Smith     }
60c7a10e08SBarry Smith #endif
612d53ad75SBarry Smith   } else if (isbinary) {
623964eb88SJed Brown     struct {
633964eb88SJed Brown       PetscErrorCode (*func)(SNES,Vec,Vec,void*);
649200755eSBarry Smith     } funcstruct;
659200755eSBarry Smith     struct {
66d1e9a80fSBarry Smith       PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
679200755eSBarry Smith     } jacstruct;
689200755eSBarry Smith     funcstruct.func = kdm->ops->computefunction;
699200755eSBarry Smith     jacstruct.jac   = kdm->ops->computejacobian;
709566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&funcstruct,1,PETSC_FUNCTION));
719566063dSJacob Faibussowitsch     PetscCall(PetscViewerBinaryWrite(viewer,&jacstruct,1,PETSC_FUNCTION));
722d53ad75SBarry Smith   }
732d53ad75SBarry Smith   PetscFunctionReturn(0);
742d53ad75SBarry Smith }
752d53ad75SBarry Smith 
7622c6f798SBarry Smith static PetscErrorCode DMSNESCreate(MPI_Comm comm,DMSNES *kdm)
7722c6f798SBarry Smith {
7822c6f798SBarry Smith   PetscFunctionBegin;
799566063dSJacob Faibussowitsch   PetscCall(SNESInitializePackage());
809566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(*kdm, DMSNES_CLASSID,  "DMSNES", "DMSNES", "DMSNES", comm, DMSNESDestroy, DMSNESView));
816cab3a1bSJed Brown   PetscFunctionReturn(0);
826cab3a1bSJed Brown }
836cab3a1bSJed Brown 
84942e3340SBarry Smith /* Attaches the DMSNES to the coarse level.
856cab3a1bSJed Brown  * Under what conditions should we copy versus duplicate?
866cab3a1bSJed Brown  */
87942e3340SBarry Smith static PetscErrorCode DMCoarsenHook_DMSNES(DM dm,DM dmc,void *ctx)
886cab3a1bSJed Brown {
896cab3a1bSJed Brown   PetscFunctionBegin;
909566063dSJacob Faibussowitsch   PetscCall(DMCopyDMSNES(dm,dmc));
916cab3a1bSJed Brown   PetscFunctionReturn(0);
926cab3a1bSJed Brown }
936cab3a1bSJed Brown 
94dfe15315SJed Brown /* This could restrict auxiliary information to the coarse level.
95caa4e7f2SJed Brown  */
96942e3340SBarry Smith static PetscErrorCode DMRestrictHook_DMSNES(DM dm,Mat Restrict,Vec rscale,Mat Inject,DM dmc,void *ctx)
97caa4e7f2SJed Brown {
98caa4e7f2SJed Brown   PetscFunctionBegin;
99caa4e7f2SJed Brown   PetscFunctionReturn(0);
100caa4e7f2SJed Brown }
101caa4e7f2SJed Brown 
102be081cd6SPeter Brune /* Attaches the DMSNES to the subdomain. */
103be081cd6SPeter Brune static PetscErrorCode DMSubDomainHook_DMSNES(DM dm,DM subdm,void *ctx)
104be081cd6SPeter Brune {
105be081cd6SPeter Brune   PetscFunctionBegin;
1069566063dSJacob Faibussowitsch   PetscCall(DMCopyDMSNES(dm,subdm));
107be081cd6SPeter Brune   PetscFunctionReturn(0);
108be081cd6SPeter Brune }
109be081cd6SPeter Brune 
110be081cd6SPeter Brune /* This could restrict auxiliary information to the coarse level.
111be081cd6SPeter Brune  */
112be081cd6SPeter Brune static PetscErrorCode DMSubDomainRestrictHook_DMSNES(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
113be081cd6SPeter Brune {
114be081cd6SPeter Brune   PetscFunctionBegin;
115be081cd6SPeter Brune   PetscFunctionReturn(0);
116be081cd6SPeter Brune }
117be081cd6SPeter Brune 
118942e3340SBarry Smith static PetscErrorCode DMRefineHook_DMSNES(DM dm,DM dmf,void *ctx)
11903a0fabfSPeter Brune {
12003a0fabfSPeter Brune   PetscFunctionBegin;
1219566063dSJacob Faibussowitsch   PetscCall(DMCopyDMSNES(dm,dmf));
12203a0fabfSPeter Brune   PetscFunctionReturn(0);
12303a0fabfSPeter Brune }
12403a0fabfSPeter Brune 
12503a0fabfSPeter Brune /* This could restrict auxiliary information to the coarse level.
12603a0fabfSPeter Brune  */
127942e3340SBarry Smith static PetscErrorCode DMInterpolateHook_DMSNES(DM dm,Mat Interp,DM dmf,void *ctx)
12803a0fabfSPeter Brune {
12903a0fabfSPeter Brune   PetscFunctionBegin;
13003a0fabfSPeter Brune   PetscFunctionReturn(0);
13103a0fabfSPeter Brune }
13203a0fabfSPeter Brune 
13322c6f798SBarry Smith /*@C
13422c6f798SBarry Smith    DMSNESCopy - copies the information in a DMSNES to another DMSNES
13522c6f798SBarry Smith 
13622c6f798SBarry Smith    Not Collective
13722c6f798SBarry Smith 
1384165533cSJose E. Roman    Input Parameters:
13922c6f798SBarry Smith +  kdm - Original DMSNES
14022c6f798SBarry Smith -  nkdm - DMSNES to receive the data, should have been created with DMSNESCreate()
14122c6f798SBarry Smith 
14222c6f798SBarry Smith    Level: developer
14322c6f798SBarry Smith 
144db781477SPatrick Sanan .seealso: `DMSNESCreate()`, `DMSNESDestroy()`
14522c6f798SBarry Smith @*/
14622c6f798SBarry Smith PetscErrorCode DMSNESCopy(DMSNES kdm,DMSNES nkdm)
14722c6f798SBarry Smith {
14822c6f798SBarry Smith   PetscFunctionBegin;
14922c6f798SBarry Smith   PetscValidHeaderSpecific(kdm,DMSNES_CLASSID,1);
15022c6f798SBarry Smith   PetscValidHeaderSpecific(nkdm,DMSNES_CLASSID,2);
15122c6f798SBarry Smith   nkdm->ops->computefunction  = kdm->ops->computefunction;
1522bc4d0c4SPeter Brune   nkdm->ops->computejacobian  = kdm->ops->computejacobian;
15322c6f798SBarry Smith   nkdm->ops->computegs        = kdm->ops->computegs;
15422c6f798SBarry Smith   nkdm->ops->computeobjective = kdm->ops->computeobjective;
15522c6f798SBarry Smith   nkdm->ops->computepjacobian = kdm->ops->computepjacobian;
15622c6f798SBarry Smith   nkdm->ops->computepfunction = kdm->ops->computepfunction;
15722c6f798SBarry Smith   nkdm->ops->destroy          = kdm->ops->destroy;
15822c6f798SBarry Smith   nkdm->ops->duplicate        = kdm->ops->duplicate;
15922c6f798SBarry Smith 
16022c6f798SBarry Smith   nkdm->gsctx                 = kdm->gsctx;
16122c6f798SBarry Smith   nkdm->pctx                  = kdm->pctx;
16222c6f798SBarry Smith   nkdm->objectivectx          = kdm->objectivectx;
163af903c1dSJunchao Zhang   nkdm->originaldm            = kdm->originaldm;
164800f99ffSJeremy L Thompson   nkdm->functionctxcontainer  = kdm->functionctxcontainer;
165800f99ffSJeremy L Thompson   nkdm->jacobianctxcontainer  = kdm->jacobianctxcontainer;
166800f99ffSJeremy L Thompson   if (nkdm->functionctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"function ctx",(PetscObject)nkdm->functionctxcontainer));
167800f99ffSJeremy L Thompson   if (nkdm->jacobianctxcontainer) PetscCall(PetscObjectCompose((PetscObject)nkdm,"jacobian ctx",(PetscObject)nkdm->jacobianctxcontainer));
16822c6f798SBarry Smith 
16922c6f798SBarry Smith   /*
17022c6f798SBarry Smith   nkdm->fortran_func_pointers[0] = kdm->fortran_func_pointers[0];
17122c6f798SBarry Smith   nkdm->fortran_func_pointers[1] = kdm->fortran_func_pointers[1];
17222c6f798SBarry Smith   nkdm->fortran_func_pointers[2] = kdm->fortran_func_pointers[2];
17322c6f798SBarry Smith   */
17422c6f798SBarry Smith 
17522c6f798SBarry Smith   /* implementation specific copy hooks */
176*dbbe0bcdSBarry Smith   PetscTryTypeMethod(kdm,duplicate,nkdm);
17722c6f798SBarry Smith   PetscFunctionReturn(0);
17822c6f798SBarry Smith }
17922c6f798SBarry Smith 
1806cab3a1bSJed Brown /*@C
181942e3340SBarry Smith    DMGetDMSNES - get read-only private DMSNES context from a DM
1826cab3a1bSJed Brown 
1836cab3a1bSJed Brown    Not Collective
1846cab3a1bSJed Brown 
1854165533cSJose E. Roman    Input Parameter:
1866cab3a1bSJed Brown .  dm - DM to be used with SNES
1876cab3a1bSJed Brown 
1884165533cSJose E. Roman    Output Parameter:
189942e3340SBarry Smith .  snesdm - private DMSNES context
1906cab3a1bSJed Brown 
1916cab3a1bSJed Brown    Level: developer
1926cab3a1bSJed Brown 
1936cab3a1bSJed Brown    Notes:
194942e3340SBarry Smith    Use DMGetDMSNESWrite() if write access is needed. The DMSNESSetXXX API should be used wherever possible.
1956cab3a1bSJed Brown 
196db781477SPatrick Sanan .seealso: `DMGetDMSNESWrite()`
1976cab3a1bSJed Brown @*/
198942e3340SBarry Smith PetscErrorCode DMGetDMSNES(DM dm,DMSNES *snesdm)
1996cab3a1bSJed Brown {
2006cab3a1bSJed Brown   PetscFunctionBegin;
2016cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
202b4615a05SBarry Smith   *snesdm = (DMSNES) dm->dmsnes;
20322c6f798SBarry Smith   if (!*snesdm) {
2049566063dSJacob Faibussowitsch     PetscCall(PetscInfo(dm,"Creating new DMSNES\n"));
2059566063dSJacob Faibussowitsch     PetscCall(DMSNESCreate(PetscObjectComm((PetscObject)dm),snesdm));
2061aa26658SKarl Rupp 
207b4615a05SBarry Smith     dm->dmsnes            = (PetscObject) *snesdm;
208af903c1dSJunchao Zhang     (*snesdm)->originaldm = dm;
2099566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHookAdd(dm,DMCoarsenHook_DMSNES,DMRestrictHook_DMSNES,NULL));
2109566063dSJacob Faibussowitsch     PetscCall(DMRefineHookAdd(dm,DMRefineHook_DMSNES,DMInterpolateHook_DMSNES,NULL));
2119566063dSJacob Faibussowitsch     PetscCall(DMSubDomainHookAdd(dm,DMSubDomainHook_DMSNES,DMSubDomainRestrictHook_DMSNES,NULL));
2126cab3a1bSJed Brown   }
2136cab3a1bSJed Brown   PetscFunctionReturn(0);
2146cab3a1bSJed Brown }
2156cab3a1bSJed Brown 
2166cab3a1bSJed Brown /*@C
217942e3340SBarry Smith    DMGetDMSNESWrite - get write access to private DMSNES context from a DM
2186cab3a1bSJed Brown 
2196cab3a1bSJed Brown    Not Collective
2206cab3a1bSJed Brown 
2214165533cSJose E. Roman    Input Parameter:
2226cab3a1bSJed Brown .  dm - DM to be used with SNES
2236cab3a1bSJed Brown 
2244165533cSJose E. Roman    Output Parameter:
225942e3340SBarry Smith .  snesdm - private DMSNES context
2266cab3a1bSJed Brown 
2276cab3a1bSJed Brown    Level: developer
2286cab3a1bSJed Brown 
229db781477SPatrick Sanan .seealso: `DMGetDMSNES()`
2306cab3a1bSJed Brown @*/
231942e3340SBarry Smith PetscErrorCode DMGetDMSNESWrite(DM dm,DMSNES *snesdm)
2326cab3a1bSJed Brown {
233942e3340SBarry Smith   DMSNES         sdm;
2346cab3a1bSJed Brown 
2356cab3a1bSJed Brown   PetscFunctionBegin;
2366cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2379566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
23828b400f6SJacob Faibussowitsch   PetscCheck(sdm->originaldm,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DMSNES has a NULL originaldm");
2396cab3a1bSJed Brown   if (sdm->originaldm != dm) {  /* Copy on write */
240b4615a05SBarry Smith     DMSNES oldsdm = sdm;
2419566063dSJacob Faibussowitsch     PetscCall(PetscInfo(dm,"Copying DMSNES due to write\n"));
2429566063dSJacob Faibussowitsch     PetscCall(DMSNESCreate(PetscObjectComm((PetscObject)dm),&sdm));
2439566063dSJacob Faibussowitsch     PetscCall(DMSNESCopy(oldsdm,sdm));
2449566063dSJacob Faibussowitsch     PetscCall(DMSNESDestroy((DMSNES*)&dm->dmsnes));
245b4615a05SBarry Smith     dm->dmsnes = (PetscObject)sdm;
246af903c1dSJunchao Zhang     sdm->originaldm = dm;
2476cab3a1bSJed Brown   }
2486cab3a1bSJed Brown   *snesdm = sdm;
2496cab3a1bSJed Brown   PetscFunctionReturn(0);
2506cab3a1bSJed Brown }
2516cab3a1bSJed Brown 
2526cab3a1bSJed Brown /*@C
253942e3340SBarry Smith    DMCopyDMSNES - copies a DM context to a new DM
2546cab3a1bSJed Brown 
2556cab3a1bSJed Brown    Logically Collective
2566cab3a1bSJed Brown 
2574165533cSJose E. Roman    Input Parameters:
2586cab3a1bSJed Brown +  dmsrc - DM to obtain context from
2596cab3a1bSJed Brown -  dmdest - DM to add context to
2606cab3a1bSJed Brown 
2616cab3a1bSJed Brown    Level: developer
2626cab3a1bSJed Brown 
2636cab3a1bSJed Brown    Note:
2646cab3a1bSJed Brown    The context is copied by reference. This function does not ensure that a context exists.
2656cab3a1bSJed Brown 
266db781477SPatrick Sanan .seealso: `DMGetDMSNES()`, `SNESSetDM()`
2676cab3a1bSJed Brown @*/
268942e3340SBarry Smith PetscErrorCode DMCopyDMSNES(DM dmsrc,DM dmdest)
2696cab3a1bSJed Brown {
2706cab3a1bSJed Brown   PetscFunctionBegin;
2716cab3a1bSJed Brown   PetscValidHeaderSpecific(dmsrc,DM_CLASSID,1);
2726cab3a1bSJed Brown   PetscValidHeaderSpecific(dmdest,DM_CLASSID,2);
2739566063dSJacob Faibussowitsch   if (!dmdest->dmsnes) PetscCall(DMSNESCreate(PetscObjectComm((PetscObject) dmdest), (DMSNES *) &dmdest->dmsnes));
2749566063dSJacob Faibussowitsch   PetscCall(DMSNESCopy((DMSNES) dmsrc->dmsnes, (DMSNES) dmdest->dmsnes));
2759566063dSJacob Faibussowitsch   PetscCall(DMCoarsenHookAdd(dmdest,DMCoarsenHook_DMSNES,NULL,NULL));
2769566063dSJacob Faibussowitsch   PetscCall(DMRefineHookAdd(dmdest,DMRefineHook_DMSNES,NULL,NULL));
2779566063dSJacob Faibussowitsch   PetscCall(DMSubDomainHookAdd(dmdest,DMSubDomainHook_DMSNES,DMSubDomainRestrictHook_DMSNES,NULL));
2786cab3a1bSJed Brown   PetscFunctionReturn(0);
2796cab3a1bSJed Brown }
2806cab3a1bSJed Brown 
2816cab3a1bSJed Brown /*@C
2826cab3a1bSJed Brown    DMSNESSetFunction - set SNES residual evaluation function
2836cab3a1bSJed Brown 
2846cab3a1bSJed Brown    Not Collective
2856cab3a1bSJed Brown 
2864165533cSJose E. Roman    Input Parameters:
2876cab3a1bSJed Brown +  dm - DM to be used with SNES
288f8b49ee9SBarry Smith .  f - residual evaluation function; see SNESFunction for details
2896cab3a1bSJed Brown -  ctx - context for residual evaluation
2906cab3a1bSJed Brown 
2916cab3a1bSJed Brown    Level: advanced
2926cab3a1bSJed Brown 
2936cab3a1bSJed Brown    Note:
2946cab3a1bSJed Brown    SNESSetFunction() is normally used, but it calls this function internally because the user context is actually
2956cab3a1bSJed Brown    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
2966cab3a1bSJed Brown    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
2976cab3a1bSJed Brown 
298db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESSetJacobian()`, `SNESFunction`
2996cab3a1bSJed Brown @*/
300f8b49ee9SBarry Smith PetscErrorCode DMSNESSetFunction(DM dm,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx)
3016cab3a1bSJed Brown {
302942e3340SBarry Smith   DMSNES         sdm;
3036cab3a1bSJed Brown 
3046cab3a1bSJed Brown   PetscFunctionBegin;
3056cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3069566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNESWrite(dm,&sdm));
307f8b49ee9SBarry Smith   if (f) sdm->ops->computefunction = f;
308800f99ffSJeremy L Thompson   if (ctx) {
309800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
310800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)sdm),&ctxcontainer));
311800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
312800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)sdm,"function ctx",(PetscObject)ctxcontainer));
313800f99ffSJeremy L Thompson     sdm->functionctxcontainer = ctxcontainer;
314800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
315800f99ffSJeremy L Thompson   }
316800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
317800f99ffSJeremy L Thompson }
318800f99ffSJeremy L Thompson 
319800f99ffSJeremy L Thompson /*@C
3205cb80ecdSBarry Smith    DMSNESSetFunctionContextDestroy - set `SNES` residual evaluation context destroy function
321800f99ffSJeremy L Thompson 
322800f99ffSJeremy L Thompson    Not Collective
323800f99ffSJeremy L Thompson 
324800f99ffSJeremy L Thompson    Input Parameters:
3255cb80ecdSBarry Smith +  dm - `DM` to be used with `SNES`
3265cb80ecdSBarry Smith -  f - residual evaluation context destroy function
327800f99ffSJeremy L Thompson 
328800f99ffSJeremy L Thompson    Level: advanced
329800f99ffSJeremy L Thompson 
330800f99ffSJeremy L Thompson .seealso: `DMSNESSetFunction()`, `SNESSetFunction()`
331800f99ffSJeremy L Thompson @*/
332800f99ffSJeremy L Thompson PetscErrorCode DMSNESSetFunctionContextDestroy(DM dm,PetscErrorCode (*f)(void*))
333800f99ffSJeremy L Thompson {
334800f99ffSJeremy L Thompson   DMSNES         sdm;
335800f99ffSJeremy L Thompson 
336800f99ffSJeremy L Thompson   PetscFunctionBegin;
337800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
338800f99ffSJeremy L Thompson   PetscCall(DMGetDMSNESWrite(dm,&sdm));
339800f99ffSJeremy L Thompson   if (sdm->functionctxcontainer) PetscCall(PetscContainerSetUserDestroy(sdm->functionctxcontainer,f));
340800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
341800f99ffSJeremy L Thompson }
342800f99ffSJeremy L Thompson 
343800f99ffSJeremy L Thompson PetscErrorCode DMSNESUnsetFunctionContext_Internal(DM dm)
344800f99ffSJeremy L Thompson {
345800f99ffSJeremy L Thompson   DMSNES         sdm;
346800f99ffSJeremy L Thompson 
347800f99ffSJeremy L Thompson   PetscFunctionBegin;
348800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
349800f99ffSJeremy L Thompson   PetscCall(DMGetDMSNESWrite(dm,&sdm));
350800f99ffSJeremy L Thompson   PetscCall(DMSNESUnsetFunctionContext_DMSNES(sdm));
3516cab3a1bSJed Brown   PetscFunctionReturn(0);
3526cab3a1bSJed Brown }
3536cab3a1bSJed Brown 
3546cab3a1bSJed Brown /*@C
355bbc1464cSBarry Smith    DMSNESSetMFFunction - set SNES residual evaluation function used in applying the matrix-free Jacobian with -snes_mf_operator
356bbc1464cSBarry Smith 
357bbc1464cSBarry Smith    Logically Collective on dm
358bbc1464cSBarry Smith 
3594165533cSJose E. Roman    Input Parameters:
360bbc1464cSBarry Smith +  dm - DM to be used with SNES
361bbc1464cSBarry Smith -  f - residual evaluation function; see SNESFunction for details
362bbc1464cSBarry Smith 
363bbc1464cSBarry Smith    Level: advanced
364bbc1464cSBarry Smith 
365db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESSetJacobian()`, `SNESFunction`, `DMSNESSetFunction()`
366bbc1464cSBarry Smith @*/
367bbc1464cSBarry Smith PetscErrorCode DMSNESSetMFFunction(DM dm,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx)
368bbc1464cSBarry Smith {
369bbc1464cSBarry Smith   DMSNES         sdm;
370bbc1464cSBarry Smith 
371bbc1464cSBarry Smith   PetscFunctionBegin;
372bbc1464cSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373bbc1464cSBarry Smith   if (f || ctx) {
3749566063dSJacob Faibussowitsch     PetscCall(DMGetDMSNESWrite(dm,&sdm));
375bbc1464cSBarry Smith   }
376bbc1464cSBarry Smith   if (f) sdm->ops->computemffunction = f;
377bbc1464cSBarry Smith   if (ctx) sdm->mffunctionctx = ctx;
378bbc1464cSBarry Smith   PetscFunctionReturn(0);
379bbc1464cSBarry Smith }
380bbc1464cSBarry Smith 
381bbc1464cSBarry Smith /*@C
3826cab3a1bSJed Brown    DMSNESGetFunction - get SNES residual evaluation function
3836cab3a1bSJed Brown 
3846cab3a1bSJed Brown    Not Collective
3856cab3a1bSJed Brown 
3864165533cSJose E. Roman    Input Parameter:
3876cab3a1bSJed Brown .  dm - DM to be used with SNES
3886cab3a1bSJed Brown 
3894165533cSJose E. Roman    Output Parameters:
390f8b49ee9SBarry Smith +  f - residual evaluation function; see SNESFunction for details
3916cab3a1bSJed Brown -  ctx - context for residual evaluation
3926cab3a1bSJed Brown 
3936cab3a1bSJed Brown    Level: advanced
3946cab3a1bSJed Brown 
3956cab3a1bSJed Brown    Note:
3966cab3a1bSJed Brown    SNESGetFunction() is normally used, but it calls this function internally because the user context is actually
3976cab3a1bSJed Brown    associated with the DM.
3986cab3a1bSJed Brown 
399db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `DMSNESSetFunction()`, `SNESSetFunction()`, `SNESFunction`
4006cab3a1bSJed Brown @*/
401f8b49ee9SBarry Smith PetscErrorCode DMSNESGetFunction(DM dm,PetscErrorCode (**f)(SNES,Vec,Vec,void*),void **ctx)
4026cab3a1bSJed Brown {
403942e3340SBarry Smith   DMSNES         sdm;
4046cab3a1bSJed Brown 
4056cab3a1bSJed Brown   PetscFunctionBegin;
4066cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4079566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
408f8b49ee9SBarry Smith   if (f) *f = sdm->ops->computefunction;
409800f99ffSJeremy L Thompson   if (ctx) {
410800f99ffSJeremy L Thompson     if (sdm->functionctxcontainer) PetscCall(PetscContainerGetPointer(sdm->functionctxcontainer,ctx));
411800f99ffSJeremy L Thompson     else *ctx = NULL;
412800f99ffSJeremy L Thompson   }
4136cab3a1bSJed Brown   PetscFunctionReturn(0);
4146cab3a1bSJed Brown }
4156cab3a1bSJed Brown 
4162a4ee8f2SPeter Brune /*@C
417081a7dcdSPeter Brune    DMSNESSetObjective - set SNES objective evaluation function
4182a4ee8f2SPeter Brune 
4192a4ee8f2SPeter Brune    Not Collective
4202a4ee8f2SPeter Brune 
4214165533cSJose E. Roman    Input Parameters:
4222a4ee8f2SPeter Brune +  dm - DM to be used with SNES
423f8b49ee9SBarry Smith .  obj - objective evaluation function; see SNESObjectiveFunction for details
4242a4ee8f2SPeter Brune -  ctx - context for residual evaluation
4252a4ee8f2SPeter Brune 
4262a4ee8f2SPeter Brune    Level: advanced
4272a4ee8f2SPeter Brune 
428db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESGetObjective()`, `DMSNESSetFunction()`
4292a4ee8f2SPeter Brune @*/
430f8b49ee9SBarry Smith PetscErrorCode DMSNESSetObjective(DM dm,PetscErrorCode (*obj)(SNES,Vec,PetscReal*,void*),void *ctx)
4312a4ee8f2SPeter Brune {
432942e3340SBarry Smith   DMSNES         sdm;
4332a4ee8f2SPeter Brune 
4342a4ee8f2SPeter Brune   PetscFunctionBegin;
4352a4ee8f2SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
436f8b49ee9SBarry Smith   if (obj || ctx) {
4379566063dSJacob Faibussowitsch     PetscCall(DMGetDMSNESWrite(dm,&sdm));
438fdaff8d6SPeter Brune   }
439f8b49ee9SBarry Smith   if (obj) sdm->ops->computeobjective = obj;
4402a4ee8f2SPeter Brune   if (ctx) sdm->objectivectx = ctx;
4412a4ee8f2SPeter Brune   PetscFunctionReturn(0);
4422a4ee8f2SPeter Brune }
4432a4ee8f2SPeter Brune 
4442a4ee8f2SPeter Brune /*@C
4452a4ee8f2SPeter Brune    DMSNESGetObjective - get SNES objective evaluation function
4462a4ee8f2SPeter Brune 
4472a4ee8f2SPeter Brune    Not Collective
4482a4ee8f2SPeter Brune 
4494165533cSJose E. Roman    Input Parameter:
4502a4ee8f2SPeter Brune .  dm - DM to be used with SNES
4512a4ee8f2SPeter Brune 
4524165533cSJose E. Roman    Output Parameters:
453f8b49ee9SBarry Smith +  obj- residual evaluation function; see SNESObjectiveFunction for details
4542a4ee8f2SPeter Brune -  ctx - context for residual evaluation
4552a4ee8f2SPeter Brune 
4562a4ee8f2SPeter Brune    Level: advanced
4572a4ee8f2SPeter Brune 
4582a4ee8f2SPeter Brune    Note:
4592a4ee8f2SPeter Brune    SNESGetFunction() is normally used, but it calls this function internally because the user context is actually
4602a4ee8f2SPeter Brune    associated with the DM.
4612a4ee8f2SPeter Brune 
462db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `DMSNESSetObjective()`, `SNESSetFunction()`
4632a4ee8f2SPeter Brune @*/
464f8b49ee9SBarry Smith PetscErrorCode DMSNESGetObjective(DM dm,PetscErrorCode (**obj)(SNES,Vec,PetscReal*,void*),void **ctx)
4652a4ee8f2SPeter Brune {
466942e3340SBarry Smith   DMSNES         sdm;
4672a4ee8f2SPeter Brune 
4682a4ee8f2SPeter Brune   PetscFunctionBegin;
4692a4ee8f2SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4709566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
471f8b49ee9SBarry Smith   if (obj) *obj = sdm->ops->computeobjective;
4722a4ee8f2SPeter Brune   if (ctx) *ctx = sdm->objectivectx;
4732a4ee8f2SPeter Brune   PetscFunctionReturn(0);
4742a4ee8f2SPeter Brune }
4752a4ee8f2SPeter Brune 
4766cab3a1bSJed Brown /*@C
477be95d8f1SBarry Smith    DMSNESSetNGS - set SNES Gauss-Seidel relaxation function
4786cab3a1bSJed Brown 
4796cab3a1bSJed Brown    Not Collective
4806cab3a1bSJed Brown 
4814165533cSJose E. Roman    Input Parameters:
4826cab3a1bSJed Brown +  dm - DM to be used with SNES
483be95d8f1SBarry Smith .  f  - relaxation function, see SNESGSFunction
4846cab3a1bSJed Brown -  ctx - context for residual evaluation
4856cab3a1bSJed Brown 
4866cab3a1bSJed Brown    Level: advanced
4876cab3a1bSJed Brown 
4886cab3a1bSJed Brown    Note:
489be95d8f1SBarry Smith    SNESSetNGS() is normally used, but it calls this function internally because the user context is actually
4906cab3a1bSJed Brown    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
4916cab3a1bSJed Brown    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
4926cab3a1bSJed Brown 
493db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESSetJacobian()`, `DMSNESSetFunction()`, `SNESGSFunction`
4946cab3a1bSJed Brown @*/
495be95d8f1SBarry Smith PetscErrorCode DMSNESSetNGS(DM dm,PetscErrorCode (*f)(SNES,Vec,Vec,void*),void *ctx)
4966cab3a1bSJed Brown {
497942e3340SBarry Smith   DMSNES         sdm;
4986cab3a1bSJed Brown 
4996cab3a1bSJed Brown   PetscFunctionBegin;
5006cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
501be95d8f1SBarry Smith   if (f || ctx) {
5029566063dSJacob Faibussowitsch     PetscCall(DMGetDMSNESWrite(dm,&sdm));
503fdaff8d6SPeter Brune   }
504be95d8f1SBarry Smith   if (f) sdm->ops->computegs = f;
5056cab3a1bSJed Brown   if (ctx) sdm->gsctx = ctx;
5066cab3a1bSJed Brown   PetscFunctionReturn(0);
5076cab3a1bSJed Brown }
5086cab3a1bSJed Brown 
5096cab3a1bSJed Brown /*@C
510be95d8f1SBarry Smith    DMSNESGetNGS - get SNES Gauss-Seidel relaxation function
5116cab3a1bSJed Brown 
5126cab3a1bSJed Brown    Not Collective
5136cab3a1bSJed Brown 
5144165533cSJose E. Roman    Input Parameter:
5156cab3a1bSJed Brown .  dm - DM to be used with SNES
5166cab3a1bSJed Brown 
5174165533cSJose E. Roman    Output Parameters:
518be95d8f1SBarry Smith +  f - relaxation function which performs Gauss-Seidel sweeps, see SNESGSFunction
5196cab3a1bSJed Brown -  ctx - context for residual evaluation
5206cab3a1bSJed Brown 
5216cab3a1bSJed Brown    Level: advanced
5226cab3a1bSJed Brown 
5236cab3a1bSJed Brown    Note:
524be95d8f1SBarry Smith    SNESGetNGS() is normally used, but it calls this function internally because the user context is actually
5256cab3a1bSJed Brown    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
5266cab3a1bSJed Brown    not. If DM took a more central role at some later date, this could become the primary method of setting the residual.
5276cab3a1bSJed Brown 
528db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESGetNGS()`, `DMSNESGetJacobian()`, `DMSNESGetFunction()`, `SNESNGSFunction`
5296cab3a1bSJed Brown @*/
530be95d8f1SBarry Smith PetscErrorCode DMSNESGetNGS(DM dm,PetscErrorCode (**f)(SNES,Vec,Vec,void*),void **ctx)
5316cab3a1bSJed Brown {
532942e3340SBarry Smith   DMSNES         sdm;
5336cab3a1bSJed Brown 
5346cab3a1bSJed Brown   PetscFunctionBegin;
5356cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5369566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
537be95d8f1SBarry Smith   if (f) *f = sdm->ops->computegs;
5386cab3a1bSJed Brown   if (ctx) *ctx = sdm->gsctx;
5396cab3a1bSJed Brown   PetscFunctionReturn(0);
5406cab3a1bSJed Brown }
5416cab3a1bSJed Brown 
5426cab3a1bSJed Brown /*@C
543ecfdb398SPeter Brune    DMSNESSetJacobian - set SNES Jacobian evaluation function
5446cab3a1bSJed Brown 
5456cab3a1bSJed Brown    Not Collective
5466cab3a1bSJed Brown 
5474165533cSJose E. Roman    Input Parameters:
5486cab3a1bSJed Brown +  dm - DM to be used with SNES
549f8b49ee9SBarry Smith .  J - Jacobian evaluation function
5506cab3a1bSJed Brown -  ctx - context for residual evaluation
5516cab3a1bSJed Brown 
5526cab3a1bSJed Brown    Level: advanced
5536cab3a1bSJed Brown 
5546cab3a1bSJed Brown    Note:
5556cab3a1bSJed Brown    SNESSetJacobian() is normally used, but it calls this function internally because the user context is actually
5566cab3a1bSJed Brown    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
5576cab3a1bSJed Brown    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
5586cab3a1bSJed Brown 
559db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESGetJacobian()`, `SNESSetJacobian()`, `SNESJacobianFunction`
5606cab3a1bSJed Brown @*/
561d1e9a80fSBarry Smith PetscErrorCode DMSNESSetJacobian(DM dm,PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
5626cab3a1bSJed Brown {
563942e3340SBarry Smith   DMSNES         sdm;
5646cab3a1bSJed Brown 
5656cab3a1bSJed Brown   PetscFunctionBegin;
5666cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
567800f99ffSJeremy L Thompson   if (J || ctx) PetscCall(DMGetDMSNESWrite(dm,&sdm));
568f8b49ee9SBarry Smith   if (J) sdm->ops->computejacobian = J;
569800f99ffSJeremy L Thompson   if (ctx) {
570800f99ffSJeremy L Thompson     PetscContainer ctxcontainer;
571800f99ffSJeremy L Thompson     PetscCall(PetscContainerCreate(PetscObjectComm((PetscObject)sdm),&ctxcontainer));
572800f99ffSJeremy L Thompson     PetscCall(PetscContainerSetPointer(ctxcontainer,ctx));
573800f99ffSJeremy L Thompson     PetscCall(PetscObjectCompose((PetscObject)sdm,"jacobian ctx",(PetscObject)ctxcontainer));
574800f99ffSJeremy L Thompson     sdm->jacobianctxcontainer = ctxcontainer;
575800f99ffSJeremy L Thompson     PetscCall(PetscContainerDestroy(&ctxcontainer));
576800f99ffSJeremy L Thompson   }
577800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
578800f99ffSJeremy L Thompson }
579800f99ffSJeremy L Thompson 
580800f99ffSJeremy L Thompson /*@C
5815cb80ecdSBarry Smith    DMSNESSetJacobianContextDestroy - set `SNES` Jacobian evaluation context destroy function
582800f99ffSJeremy L Thompson 
583800f99ffSJeremy L Thompson    Not Collective
584800f99ffSJeremy L Thompson 
585800f99ffSJeremy L Thompson    Input Parameters:
5865cb80ecdSBarry Smith +  dm - `DM` to be used with `SNES`
5875cb80ecdSBarry Smith -  f - Jacobian evaluation contex destroy function
588800f99ffSJeremy L Thompson 
589800f99ffSJeremy L Thompson    Level: advanced
590800f99ffSJeremy L Thompson 
59190f54644SBarry Smith .seealso: `DMSNESSetJacobian()`
592800f99ffSJeremy L Thompson @*/
593800f99ffSJeremy L Thompson PetscErrorCode DMSNESSetJacobianContextDestroy(DM dm,PetscErrorCode (*f)(void*))
594800f99ffSJeremy L Thompson {
595800f99ffSJeremy L Thompson   DMSNES         sdm;
596800f99ffSJeremy L Thompson 
597800f99ffSJeremy L Thompson   PetscFunctionBegin;
598800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
599800f99ffSJeremy L Thompson   PetscCall(DMGetDMSNESWrite(dm,&sdm));
600800f99ffSJeremy L Thompson   if (sdm->jacobianctxcontainer) PetscCall(PetscContainerSetUserDestroy(sdm->jacobianctxcontainer,f));
601800f99ffSJeremy L Thompson   PetscFunctionReturn(0);
602800f99ffSJeremy L Thompson }
603800f99ffSJeremy L Thompson 
604800f99ffSJeremy L Thompson PetscErrorCode DMSNESUnsetJacobianContext_Internal(DM dm)
605800f99ffSJeremy L Thompson {
606800f99ffSJeremy L Thompson   DMSNES         sdm;
607800f99ffSJeremy L Thompson 
608800f99ffSJeremy L Thompson   PetscFunctionBegin;
609800f99ffSJeremy L Thompson   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
610800f99ffSJeremy L Thompson   PetscCall(DMGetDMSNESWrite(dm,&sdm));
611800f99ffSJeremy L Thompson   PetscCall(DMSNESUnsetJacobianContext_DMSNES(sdm));
6126cab3a1bSJed Brown   PetscFunctionReturn(0);
6136cab3a1bSJed Brown }
6146cab3a1bSJed Brown 
6156cab3a1bSJed Brown /*@C
616ecfdb398SPeter Brune    DMSNESGetJacobian - get SNES Jacobian evaluation function
6176cab3a1bSJed Brown 
6186cab3a1bSJed Brown    Not Collective
6196cab3a1bSJed Brown 
6204165533cSJose E. Roman    Input Parameter:
6216cab3a1bSJed Brown .  dm - DM to be used with SNES
6226cab3a1bSJed Brown 
6234165533cSJose E. Roman    Output Parameters:
624f8b49ee9SBarry Smith +  J - Jacobian evaluation function; see SNESJacobianFunction for all calling sequence
6256cab3a1bSJed Brown -  ctx - context for residual evaluation
6266cab3a1bSJed Brown 
6276cab3a1bSJed Brown    Level: advanced
6286cab3a1bSJed Brown 
6296cab3a1bSJed Brown    Note:
6306cab3a1bSJed Brown    SNESGetJacobian() is normally used, but it calls this function internally because the user context is actually
6316cab3a1bSJed Brown    associated with the DM.  This makes the interface consistent regardless of whether the user interacts with a DM or
6326cab3a1bSJed Brown    not. If DM took a more central role at some later date, this could become the primary method of setting the Jacobian.
6336cab3a1bSJed Brown 
634db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESSetJacobian()`, `SNESJacobianFunction`
6356cab3a1bSJed Brown @*/
636d1e9a80fSBarry Smith PetscErrorCode DMSNESGetJacobian(DM dm,PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx)
6376cab3a1bSJed Brown {
638942e3340SBarry Smith   DMSNES         sdm;
6396cab3a1bSJed Brown 
6406cab3a1bSJed Brown   PetscFunctionBegin;
6416cab3a1bSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6429566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
643f8b49ee9SBarry Smith   if (J) *J = sdm->ops->computejacobian;
644800f99ffSJeremy L Thompson   if (ctx) {
645800f99ffSJeremy L Thompson     if (sdm->jacobianctxcontainer) PetscCall(PetscContainerGetPointer(sdm->jacobianctxcontainer,ctx));
646800f99ffSJeremy L Thompson     else *ctx = NULL;
647800f99ffSJeremy L Thompson   }
6486cab3a1bSJed Brown   PetscFunctionReturn(0);
6496cab3a1bSJed Brown }
6506cab3a1bSJed Brown 
651e03ab78fSPeter Brune /*@C
652e03ab78fSPeter Brune    DMSNESSetPicard - set SNES Picard iteration matrix and RHS evaluation functions.
653e03ab78fSPeter Brune 
654e03ab78fSPeter Brune    Not Collective
655e03ab78fSPeter Brune 
6564165533cSJose E. Roman    Input Parameters:
657e03ab78fSPeter Brune +  dm - DM to be used with SNES
658f8b49ee9SBarry Smith .  b - RHS evaluation function
659f8b49ee9SBarry Smith .  J - Picard matrix evaluation function
660e03ab78fSPeter Brune -  ctx - context for residual evaluation
661e03ab78fSPeter Brune 
662e03ab78fSPeter Brune    Level: advanced
663e03ab78fSPeter Brune 
664db781477SPatrick Sanan .seealso: `SNESSetPicard()`, `DMSNESSetFunction()`, `DMSNESSetJacobian()`
665e03ab78fSPeter Brune @*/
666d1e9a80fSBarry Smith PetscErrorCode DMSNESSetPicard(DM dm,PetscErrorCode (*b)(SNES,Vec,Vec,void*),PetscErrorCode (*J)(SNES,Vec,Mat,Mat,void*),void *ctx)
667e03ab78fSPeter Brune {
668942e3340SBarry Smith   DMSNES         sdm;
669e03ab78fSPeter Brune 
670e03ab78fSPeter Brune   PetscFunctionBegin;
671e03ab78fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6729566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
673f8b49ee9SBarry Smith   if (b) sdm->ops->computepfunction = b;
674f8b49ee9SBarry Smith   if (J) sdm->ops->computepjacobian = J;
675e03ab78fSPeter Brune   if (ctx) sdm->pctx = ctx;
676e03ab78fSPeter Brune   PetscFunctionReturn(0);
677e03ab78fSPeter Brune }
678e03ab78fSPeter Brune 
6797971a8bfSPeter Brune /*@C
6807971a8bfSPeter Brune    DMSNESGetPicard - get SNES Picard iteration evaluation functions
6817971a8bfSPeter Brune 
6827971a8bfSPeter Brune    Not Collective
6837971a8bfSPeter Brune 
6844165533cSJose E. Roman    Input Parameter:
6857971a8bfSPeter Brune .  dm - DM to be used with SNES
6867971a8bfSPeter Brune 
6874165533cSJose E. Roman    Output Parameters:
688f8b49ee9SBarry Smith +  b - RHS evaluation function; see SNESFunction for details
689f8b49ee9SBarry Smith .  J  - RHS evaluation function; see SNESJacobianFunction for detailsa
6907971a8bfSPeter Brune -  ctx - context for residual evaluation
6917971a8bfSPeter Brune 
6927971a8bfSPeter Brune    Level: advanced
6937971a8bfSPeter Brune 
694db781477SPatrick Sanan .seealso: `DMSNESSetContext()`, `SNESSetFunction()`, `DMSNESSetJacobian()`
6957971a8bfSPeter Brune @*/
696d1e9a80fSBarry Smith PetscErrorCode DMSNESGetPicard(DM dm,PetscErrorCode (**b)(SNES,Vec,Vec,void*),PetscErrorCode (**J)(SNES,Vec,Mat,Mat,void*),void **ctx)
6977971a8bfSPeter Brune {
698942e3340SBarry Smith   DMSNES         sdm;
6997971a8bfSPeter Brune 
7007971a8bfSPeter Brune   PetscFunctionBegin;
7017971a8bfSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7029566063dSJacob Faibussowitsch   PetscCall(DMGetDMSNES(dm,&sdm));
703f8b49ee9SBarry Smith   if (b) *b = sdm->ops->computepfunction;
704f8b49ee9SBarry Smith   if (J) *J = sdm->ops->computepjacobian;
7057971a8bfSPeter Brune   if (ctx) *ctx = sdm->pctx;
7067971a8bfSPeter Brune   PetscFunctionReturn(0);
7077971a8bfSPeter Brune }
708