xref: /petsc/src/ts/utils/dmplexts.c (revision aeadca180150cc093a06dc6dc3afaf734334ac32)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/
2af0996ceSBarry Smith #include <petsc/private/tsimpl.h>     /*I "petscts.h" I*/
337c2070cSMatthew G. Knepley #include <petsc/private/snesimpl.h>
4924a1b8fSMatthew G. Knepley #include <petscds.h>
56dbbd306SMatthew G. Knepley #include <petscfv.h>
66dbbd306SMatthew G. Knepley 
76da023fcSToby Isaac static PetscErrorCode DMTSConvertPlex(DM dm, DM *plex, PetscBool copy)
86da023fcSToby Isaac {
96da023fcSToby Isaac   PetscBool      isPlex;
106da023fcSToby Isaac   PetscErrorCode ierr;
116da023fcSToby Isaac 
126da023fcSToby Isaac   PetscFunctionBegin;
136da023fcSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
146da023fcSToby Isaac   if (isPlex) {
156da023fcSToby Isaac     *plex = dm;
166da023fcSToby Isaac     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
17f7148743SMatthew G. Knepley   } else {
18f7148743SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
19f7148743SMatthew G. Knepley     if (!*plex) {
206da023fcSToby Isaac       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
21f7148743SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
226da023fcSToby Isaac       if (copy) {
236da023fcSToby Isaac         PetscInt    i;
246da023fcSToby Isaac         PetscObject obj;
256da023fcSToby Isaac         const char *comps[3] = {"A","dmAux","dmCh"};
266da023fcSToby Isaac 
276da023fcSToby Isaac         ierr = DMCopyDMTS(dm, *plex);CHKERRQ(ierr);
286da023fcSToby Isaac         ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr);
296da023fcSToby Isaac         for (i = 0; i < 3; i++) {
306da023fcSToby Isaac           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
316da023fcSToby Isaac           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
326da023fcSToby Isaac         }
336da023fcSToby Isaac       }
34f7148743SMatthew G. Knepley     } else {
35f7148743SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
36f7148743SMatthew G. Knepley     }
376da023fcSToby Isaac   }
386da023fcSToby Isaac   PetscFunctionReturn(0);
396da023fcSToby Isaac }
406da023fcSToby Isaac 
416da023fcSToby Isaac 
42c510411aSMatthew G. Knepley /*@
43b6aca0f9SMatthew G. Knepley   DMPlexTSGetGeometryFVM - Return precomputed geometric data
44c510411aSMatthew G. Knepley 
45c510411aSMatthew G. Knepley   Input Parameter:
46c510411aSMatthew G. Knepley . dm - The DM
47c510411aSMatthew G. Knepley 
48c510411aSMatthew G. Knepley   Output Parameters:
49c510411aSMatthew G. Knepley + facegeom - The values precomputed from face geometry
50c510411aSMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
51c510411aSMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
52c510411aSMatthew G. Knepley 
53c510411aSMatthew G. Knepley   Level: developer
54c510411aSMatthew G. Knepley 
55c510411aSMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
56c510411aSMatthew G. Knepley @*/
57b6aca0f9SMatthew G. Knepley PetscErrorCode DMPlexTSGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
58a0ac79e7SMatthew G. Knepley {
59a0ac79e7SMatthew G. Knepley   PetscErrorCode ierr;
60a0ac79e7SMatthew G. Knepley 
61a0ac79e7SMatthew G. Knepley   PetscFunctionBegin;
624b32e5bbSToby Isaac   ierr = DMPlexSNESGetGeometryFVM(dm,facegeom,cellgeom,minRadius);CHKERRQ(ierr);
63924a1b8fSMatthew G. Knepley   PetscFunctionReturn(0);
64924a1b8fSMatthew G. Knepley }
65924a1b8fSMatthew G. Knepley 
66c49ccbb3SMatthew G. Knepley /*@C
67c49ccbb3SMatthew G. Knepley   DMPlexTSGetGradientDM - Return gradient data layout
68c49ccbb3SMatthew G. Knepley 
69c49ccbb3SMatthew G. Knepley   Input Parameters:
70c49ccbb3SMatthew G. Knepley + dm - The DM
71c49ccbb3SMatthew G. Knepley - fv - The PetscFV
72c49ccbb3SMatthew G. Knepley 
73c49ccbb3SMatthew G. Knepley   Output Parameter:
74c49ccbb3SMatthew G. Knepley . dmGrad - The layout for gradient values
75c49ccbb3SMatthew G. Knepley 
76c49ccbb3SMatthew G. Knepley   Level: developer
77c49ccbb3SMatthew G. Knepley 
78c49ccbb3SMatthew G. Knepley .seealso: DMPlexTSGetGeometryFVM(), DMPlexTSSetRHSFunctionLocal()
79c49ccbb3SMatthew G. Knepley @*/
80c49ccbb3SMatthew G. Knepley PetscErrorCode DMPlexTSGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
81c49ccbb3SMatthew G. Knepley {
82c49ccbb3SMatthew G. Knepley   PetscErrorCode ierr;
83c49ccbb3SMatthew G. Knepley 
84c49ccbb3SMatthew G. Knepley   PetscFunctionBegin;
854b32e5bbSToby Isaac   ierr = DMPlexSNESGetGradientDM(dm,fv,dmGrad);CHKERRQ(ierr);
86254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
87254c1ad2SMatthew G. Knepley }
88254c1ad2SMatthew G. Knepley 
8908449791SMatthew G. Knepley /*@
9008449791SMatthew G. Knepley   DMPlexTSComputeRHSFunctionFVM - Form the local forcing F from the local input X using pointwise functions specified by the user
9108449791SMatthew G. Knepley 
9208449791SMatthew G. Knepley   Input Parameters:
9308449791SMatthew G. Knepley + dm - The mesh
9408449791SMatthew G. Knepley . t - The time
9508449791SMatthew G. Knepley . locX  - Local solution
9608449791SMatthew G. Knepley - user - The user context
9708449791SMatthew G. Knepley 
9808449791SMatthew G. Knepley   Output Parameter:
993b16df42SMatthew G. Knepley . F  - Global output vector
10008449791SMatthew G. Knepley 
10108449791SMatthew G. Knepley   Level: developer
10208449791SMatthew G. Knepley 
10308449791SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
10408449791SMatthew G. Knepley @*/
1053b16df42SMatthew G. Knepley PetscErrorCode DMPlexTSComputeRHSFunctionFVM(DM dm, PetscReal time, Vec locX, Vec F, void *user)
106254c1ad2SMatthew G. Knepley {
1073b16df42SMatthew G. Knepley   Vec            locF;
1084a3e9fdbSToby Isaac   IS             cellIS;
1096da023fcSToby Isaac   DM             plex;
1104a3e9fdbSToby Isaac   PetscInt       depth;
111254c1ad2SMatthew G. Knepley   PetscErrorCode ierr;
112254c1ad2SMatthew G. Knepley 
113254c1ad2SMatthew G. Knepley   PetscFunctionBegin;
1146da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1154a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
116*aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
1174a3e9fdbSToby Isaac   if (!cellIS) {
1184a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
1194a3e9fdbSToby Isaac   }
1206da023fcSToby Isaac   ierr = DMGetLocalVector(plex, &locF);CHKERRQ(ierr);
1213b16df42SMatthew G. Knepley   ierr = VecZeroEntries(locF);CHKERRQ(ierr);
1224a3e9fdbSToby Isaac   ierr = DMPlexComputeResidual_Internal(plex, cellIS, time, locX, NULL, time, locF, user);CHKERRQ(ierr);
1239bda831aSToby Isaac   ierr = DMLocalToGlobalBegin(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
1249bda831aSToby Isaac   ierr = DMLocalToGlobalEnd(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
1256da023fcSToby Isaac   ierr = DMRestoreLocalVector(plex, &locF);CHKERRQ(ierr);
1264a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
127b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
128254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
129254c1ad2SMatthew G. Knepley }
130254c1ad2SMatthew G. Knepley 
131c5d70e09SMatthew G. Knepley /*@
132ef68eab9SMatthew G. Knepley   DMPlexTSComputeBoundary - Insert the essential boundary values for the local input X and/or its time derivative X_t using pointwise functions specified by the user
133c5d70e09SMatthew G. Knepley 
134c5d70e09SMatthew G. Knepley   Input Parameters:
135c5d70e09SMatthew G. Knepley + dm - The mesh
136a40652d4SToby Isaac . t - The time
137a40652d4SToby Isaac . locX  - Local solution
138a40652d4SToby Isaac . locX_t - Local solution time derivative, or NULL
139c5d70e09SMatthew G. Knepley - user - The user context
140c5d70e09SMatthew G. Knepley 
141c5d70e09SMatthew G. Knepley   Level: developer
142c5d70e09SMatthew G. Knepley 
143a40652d4SToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
144c5d70e09SMatthew G. Knepley @*/
145ef68eab9SMatthew G. Knepley PetscErrorCode DMPlexTSComputeBoundary(DM dm, PetscReal time, Vec locX, Vec locX_t, void *user)
146c5d70e09SMatthew G. Knepley {
147c5d70e09SMatthew G. Knepley   DM             plex;
148ef68eab9SMatthew G. Knepley   Vec            faceGeometryFVM = NULL;
149ef68eab9SMatthew G. Knepley   PetscInt       Nf, f;
150c5d70e09SMatthew G. Knepley   PetscErrorCode ierr;
151c5d70e09SMatthew G. Knepley 
152c5d70e09SMatthew G. Knepley   PetscFunctionBegin;
153c5d70e09SMatthew G. Knepley   ierr = DMTSConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr);
154ef68eab9SMatthew G. Knepley   ierr = DMGetNumFields(plex, &Nf);CHKERRQ(ierr);
1559586001cSMatthew G. Knepley   if (!locX_t) {
1569586001cSMatthew G. Knepley     /* This is the RHS part */
157ef68eab9SMatthew G. Knepley     for (f = 0; f < Nf; f++) {
158ef68eab9SMatthew G. Knepley       PetscObject  obj;
159ef68eab9SMatthew G. Knepley       PetscClassId id;
160ef68eab9SMatthew G. Knepley 
161ef68eab9SMatthew G. Knepley       ierr = DMGetField(plex, f, &obj);CHKERRQ(ierr);
162ef68eab9SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
163ef68eab9SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {
164ef68eab9SMatthew G. Knepley         ierr = DMPlexSNESGetGeometryFVM(plex, &faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
165ef68eab9SMatthew G. Knepley         break;
166ef68eab9SMatthew G. Knepley       }
167ef68eab9SMatthew G. Knepley     }
1689586001cSMatthew G. Knepley   }
1699586001cSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, time, faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
170a40652d4SToby Isaac   /* TODO: locX_t */
171c5d70e09SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
172c5d70e09SMatthew G. Knepley   PetscFunctionReturn(0);
173c5d70e09SMatthew G. Knepley }
174c5d70e09SMatthew G. Knepley 
17524cdb843SMatthew G. Knepley /*@
17624cdb843SMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
17724cdb843SMatthew G. Knepley 
17824cdb843SMatthew G. Knepley   Input Parameters:
17924cdb843SMatthew G. Knepley + dm - The mesh
18024cdb843SMatthew G. Knepley . t - The time
18108449791SMatthew G. Knepley . locX  - Local solution
18208449791SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
18324cdb843SMatthew G. Knepley - user - The user context
18424cdb843SMatthew G. Knepley 
18524cdb843SMatthew G. Knepley   Output Parameter:
18608449791SMatthew G. Knepley . locF  - Local output vector
18724cdb843SMatthew G. Knepley 
18824cdb843SMatthew G. Knepley   Level: developer
18924cdb843SMatthew G. Knepley 
19024cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
19124cdb843SMatthew G. Knepley @*/
19208449791SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
19324cdb843SMatthew G. Knepley {
1946da023fcSToby Isaac   DM             plex;
1954a3e9fdbSToby Isaac   IS             cellIS;
1964a3e9fdbSToby Isaac   PetscInt       depth;
19724cdb843SMatthew G. Knepley   PetscErrorCode ierr;
19824cdb843SMatthew G. Knepley 
19924cdb843SMatthew G. Knepley   PetscFunctionBegin;
2006da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2014a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
202*aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
2034a3e9fdbSToby Isaac   if (!cellIS) {
2044a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
2054a3e9fdbSToby Isaac   }
2064a3e9fdbSToby Isaac   ierr = DMPlexComputeResidual_Internal(plex, cellIS, time, locX, locX_t, time, locF, user);CHKERRQ(ierr);
2074a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
208b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
20924cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
21024cdb843SMatthew G. Knepley }
2117cdb2a12SMatthew G. Knepley 
212756a1f44SMatthew G. Knepley /*@
213756a1f44SMatthew G. Knepley   DMPlexTSComputeIJacobianFEM - Form the local Jacobian J from the local input X using pointwise functions specified by the user
214756a1f44SMatthew G. Knepley 
215756a1f44SMatthew G. Knepley   Input Parameters:
216756a1f44SMatthew G. Knepley + dm - The mesh
217756a1f44SMatthew G. Knepley . t - The time
218756a1f44SMatthew G. Knepley . locX  - Local solution
219756a1f44SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
220756a1f44SMatthew G. Knepley . X_tshift - The multiplicative parameter for dF/du_t
221756a1f44SMatthew G. Knepley - user - The user context
222756a1f44SMatthew G. Knepley 
223756a1f44SMatthew G. Knepley   Output Parameter:
224756a1f44SMatthew G. Knepley . locF  - Local output vector
225756a1f44SMatthew G. Knepley 
226756a1f44SMatthew G. Knepley   Level: developer
227756a1f44SMatthew G. Knepley 
228756a1f44SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
229756a1f44SMatthew G. Knepley @*/
230756a1f44SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIJacobianFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
231756a1f44SMatthew G. Knepley {
2324a3e9fdbSToby Isaac   IS             cellIS;
233756a1f44SMatthew G. Knepley   DM             plex;
2344a3e9fdbSToby Isaac   PetscInt       depth;
235756a1f44SMatthew G. Knepley   PetscErrorCode ierr;
236756a1f44SMatthew G. Knepley 
237756a1f44SMatthew G. Knepley   PetscFunctionBegin;
238756a1f44SMatthew G. Knepley   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2394a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex,&depth);CHKERRQ(ierr);
240*aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
2414a3e9fdbSToby Isaac   if (!cellIS) {
2424a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
2434a3e9fdbSToby Isaac   }
2444a3e9fdbSToby Isaac   ierr = DMPlexComputeJacobian_Internal(plex, cellIS, time, X_tShift, locX, locX_t, Jac, JacP, user);CHKERRQ(ierr);
2454a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
246756a1f44SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
247756a1f44SMatthew G. Knepley   PetscFunctionReturn(0);
248756a1f44SMatthew G. Knepley }
249756a1f44SMatthew G. Knepley 
2500163fd50SMatthew G. Knepley PetscErrorCode DMTSCheckFromOptions(TS ts, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
2517cdb2a12SMatthew G. Knepley {
2527cdb2a12SMatthew G. Knepley   DM             dm;
2537cdb2a12SMatthew G. Knepley   SNES           snes;
2541878804aSMatthew G. Knepley   Vec            sol;
2557cdb2a12SMatthew G. Knepley   PetscBool      check;
2567cdb2a12SMatthew G. Knepley   PetscErrorCode ierr;
2577cdb2a12SMatthew G. Knepley 
2587cdb2a12SMatthew G. Knepley   PetscFunctionBegin;
259c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix, "-dmts_check", &check);CHKERRQ(ierr);
2607cdb2a12SMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
26172fd7fc8SMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
26272fd7fc8SMatthew G. Knepley   ierr = TSSetSolution(ts, sol);CHKERRQ(ierr);
2637cdb2a12SMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
2647cdb2a12SMatthew G. Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2657cdb2a12SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
2667cdb2a12SMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
2671878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
26872fd7fc8SMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2697cdb2a12SMatthew G. Knepley   PetscFunctionReturn(0);
2707cdb2a12SMatthew G. Knepley }
271