xref: /petsc/src/ts/utils/dmplexts.c (revision f04eb4ed2b5986080c6ede07d79a31d99dfbe477)
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;
108c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
1096da023fcSToby Isaac   DM             plex;
110254c1ad2SMatthew G. Knepley   PetscErrorCode ierr;
111254c1ad2SMatthew G. Knepley 
112254c1ad2SMatthew G. Knepley   PetscFunctionBegin;
1136da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1146da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
1156da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
116c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1176da023fcSToby Isaac   ierr = DMGetLocalVector(plex, &locF);CHKERRQ(ierr);
1183b16df42SMatthew G. Knepley   ierr = VecZeroEntries(locF);CHKERRQ(ierr);
11911dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, NULL, time, locF, user);CHKERRQ(ierr);
1209bda831aSToby Isaac   ierr = DMLocalToGlobalBegin(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
1219bda831aSToby Isaac   ierr = DMLocalToGlobalEnd(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
1226da023fcSToby Isaac   ierr = DMRestoreLocalVector(plex, &locF);CHKERRQ(ierr);
123b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
124254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
125254c1ad2SMatthew G. Knepley }
126254c1ad2SMatthew G. Knepley 
127c5d70e09SMatthew G. Knepley /*@
128ef68eab9SMatthew 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
129c5d70e09SMatthew G. Knepley 
130c5d70e09SMatthew G. Knepley   Input Parameters:
131c5d70e09SMatthew G. Knepley + dm - The mesh
132a40652d4SToby Isaac . t - The time
133a40652d4SToby Isaac . locX  - Local solution
134a40652d4SToby Isaac . locX_t - Local solution time derivative, or NULL
135c5d70e09SMatthew G. Knepley - user - The user context
136c5d70e09SMatthew G. Knepley 
137c5d70e09SMatthew G. Knepley   Level: developer
138c5d70e09SMatthew G. Knepley 
139a40652d4SToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
140c5d70e09SMatthew G. Knepley @*/
141ef68eab9SMatthew G. Knepley PetscErrorCode DMPlexTSComputeBoundary(DM dm, PetscReal time, Vec locX, Vec locX_t, void *user)
142c5d70e09SMatthew G. Knepley {
143c5d70e09SMatthew G. Knepley   DM             plex;
144ef68eab9SMatthew G. Knepley   Vec            faceGeometryFVM = NULL;
145ef68eab9SMatthew G. Knepley   PetscInt       Nf, f;
146c5d70e09SMatthew G. Knepley   PetscErrorCode ierr;
147c5d70e09SMatthew G. Knepley 
148c5d70e09SMatthew G. Knepley   PetscFunctionBegin;
149c5d70e09SMatthew G. Knepley   ierr = DMTSConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr);
150ef68eab9SMatthew G. Knepley   ierr = DMGetNumFields(plex, &Nf);CHKERRQ(ierr);
1519586001cSMatthew G. Knepley   if (!locX_t) {
1529586001cSMatthew G. Knepley     /* This is the RHS part */
153ef68eab9SMatthew G. Knepley     for (f = 0; f < Nf; f++) {
154ef68eab9SMatthew G. Knepley       PetscObject  obj;
155ef68eab9SMatthew G. Knepley       PetscClassId id;
156ef68eab9SMatthew G. Knepley 
157ef68eab9SMatthew G. Knepley       ierr = DMGetField(plex, f, &obj);CHKERRQ(ierr);
158ef68eab9SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
159ef68eab9SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {
160ef68eab9SMatthew G. Knepley         ierr = DMPlexSNESGetGeometryFVM(plex, &faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
161ef68eab9SMatthew G. Knepley         break;
162ef68eab9SMatthew G. Knepley       }
163ef68eab9SMatthew G. Knepley     }
1649586001cSMatthew G. Knepley   }
1659586001cSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, time, faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
166a40652d4SToby Isaac   /* TODO: locX_t */
167c5d70e09SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
168c5d70e09SMatthew G. Knepley   PetscFunctionReturn(0);
169c5d70e09SMatthew G. Knepley }
170c5d70e09SMatthew G. Knepley 
17124cdb843SMatthew G. Knepley /*@
17224cdb843SMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
17324cdb843SMatthew G. Knepley 
17424cdb843SMatthew G. Knepley   Input Parameters:
17524cdb843SMatthew G. Knepley + dm - The mesh
17624cdb843SMatthew G. Knepley . t - The time
17708449791SMatthew G. Knepley . locX  - Local solution
17808449791SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
17924cdb843SMatthew G. Knepley - user - The user context
18024cdb843SMatthew G. Knepley 
18124cdb843SMatthew G. Knepley   Output Parameter:
18208449791SMatthew G. Knepley . locF  - Local output vector
18324cdb843SMatthew G. Knepley 
18424cdb843SMatthew G. Knepley   Level: developer
18524cdb843SMatthew G. Knepley 
18624cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
18724cdb843SMatthew G. Knepley @*/
18808449791SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
18924cdb843SMatthew G. Knepley {
190c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
1916da023fcSToby Isaac   DM             plex;
19224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
19324cdb843SMatthew G. Knepley 
19424cdb843SMatthew G. Knepley   PetscFunctionBegin;
1956da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1966da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
1976da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
198c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
19911dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, locX_t, time, locF, user);CHKERRQ(ierr);
200b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
20124cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
20224cdb843SMatthew G. Knepley }
2037cdb2a12SMatthew G. Knepley 
204756a1f44SMatthew G. Knepley /*@
205756a1f44SMatthew G. Knepley   DMPlexTSComputeIJacobianFEM - Form the local Jacobian J from the local input X using pointwise functions specified by the user
206756a1f44SMatthew G. Knepley 
207756a1f44SMatthew G. Knepley   Input Parameters:
208756a1f44SMatthew G. Knepley + dm - The mesh
209756a1f44SMatthew G. Knepley . t - The time
210756a1f44SMatthew G. Knepley . locX  - Local solution
211756a1f44SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
212756a1f44SMatthew G. Knepley . X_tshift - The multiplicative parameter for dF/du_t
213756a1f44SMatthew G. Knepley - user - The user context
214756a1f44SMatthew G. Knepley 
215756a1f44SMatthew G. Knepley   Output Parameter:
216756a1f44SMatthew G. Knepley . locF  - Local output vector
217756a1f44SMatthew G. Knepley 
218756a1f44SMatthew G. Knepley   Level: developer
219756a1f44SMatthew G. Knepley 
220756a1f44SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
221756a1f44SMatthew G. Knepley @*/
222756a1f44SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIJacobianFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
223756a1f44SMatthew G. Knepley {
224756a1f44SMatthew G. Knepley   DM             plex;
225*f04eb4edSMatthew G. Knepley   PetscDS        prob;
226*f04eb4edSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
227*f04eb4edSMatthew G. Knepley   PetscBool      hasJac, hasPrec;
228756a1f44SMatthew G. Knepley   PetscErrorCode ierr;
229756a1f44SMatthew G. Knepley 
230756a1f44SMatthew G. Knepley   PetscFunctionBegin;
231756a1f44SMatthew G. Knepley   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
232756a1f44SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
233756a1f44SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
234756a1f44SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
235*f04eb4edSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
236*f04eb4edSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
237*f04eb4edSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
238*f04eb4edSMatthew G. Knepley   if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);}
239*f04eb4edSMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
240756a1f44SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, time, X_tShift, locX, locX_t, Jac, JacP, user);CHKERRQ(ierr);
241756a1f44SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
242756a1f44SMatthew G. Knepley   PetscFunctionReturn(0);
243756a1f44SMatthew G. Knepley }
244756a1f44SMatthew G. Knepley 
2450163fd50SMatthew 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)
2467cdb2a12SMatthew G. Knepley {
2477cdb2a12SMatthew G. Knepley   DM             dm;
2487cdb2a12SMatthew G. Knepley   SNES           snes;
2491878804aSMatthew G. Knepley   Vec            sol;
2507cdb2a12SMatthew G. Knepley   PetscBool      check;
2517cdb2a12SMatthew G. Knepley   PetscErrorCode ierr;
2527cdb2a12SMatthew G. Knepley 
2537cdb2a12SMatthew G. Knepley   PetscFunctionBegin;
254c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix, "-dmts_check", &check);CHKERRQ(ierr);
2557cdb2a12SMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
25672fd7fc8SMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
25772fd7fc8SMatthew G. Knepley   ierr = TSSetSolution(ts, sol);CHKERRQ(ierr);
2587cdb2a12SMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
2597cdb2a12SMatthew G. Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2607cdb2a12SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
2617cdb2a12SMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
2621878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
26372fd7fc8SMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2647cdb2a12SMatthew G. Knepley   PetscFunctionReturn(0);
2657cdb2a12SMatthew G. Knepley }
266