xref: /petsc/src/ts/utils/dmplexts.c (revision 9bda831a735708f7f9d6ec428c3119ee8bf69d2f)
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 
7254c1ad2SMatthew G. Knepley #undef __FUNCT__
86da023fcSToby Isaac #define __FUNCT__ "DMTSConvertPlex"
96da023fcSToby Isaac static PetscErrorCode DMTSConvertPlex(DM dm, DM *plex, PetscBool copy)
106da023fcSToby Isaac {
116da023fcSToby Isaac   PetscBool      isPlex;
126da023fcSToby Isaac   PetscErrorCode ierr;
136da023fcSToby Isaac 
146da023fcSToby Isaac   PetscFunctionBegin;
156da023fcSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
166da023fcSToby Isaac   if (isPlex) {
176da023fcSToby Isaac     *plex = dm;
186da023fcSToby Isaac     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
19f7148743SMatthew G. Knepley   } else {
20f7148743SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
21f7148743SMatthew G. Knepley     if (!*plex) {
226da023fcSToby Isaac       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
23f7148743SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
246da023fcSToby Isaac       if (copy) {
256da023fcSToby Isaac         PetscInt    i;
266da023fcSToby Isaac         PetscObject obj;
276da023fcSToby Isaac         const char *comps[3] = {"A","dmAux","dmCh"};
286da023fcSToby Isaac 
296da023fcSToby Isaac         ierr = DMCopyDMTS(dm, *plex);CHKERRQ(ierr);
306da023fcSToby Isaac         ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr);
316da023fcSToby Isaac         for (i = 0; i < 3; i++) {
326da023fcSToby Isaac           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
336da023fcSToby Isaac           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
346da023fcSToby Isaac         }
356da023fcSToby Isaac       }
36f7148743SMatthew G. Knepley     } else {
37f7148743SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
38f7148743SMatthew G. Knepley     }
396da023fcSToby Isaac   }
406da023fcSToby Isaac   PetscFunctionReturn(0);
416da023fcSToby Isaac }
426da023fcSToby Isaac 
436da023fcSToby Isaac 
446da023fcSToby Isaac #undef __FUNCT__
45b6aca0f9SMatthew G. Knepley #define __FUNCT__ "DMPlexTSGetGeometryFVM"
46c510411aSMatthew G. Knepley /*@
47b6aca0f9SMatthew G. Knepley   DMPlexTSGetGeometryFVM - Return precomputed geometric data
48c510411aSMatthew G. Knepley 
49c510411aSMatthew G. Knepley   Input Parameter:
50c510411aSMatthew G. Knepley . dm - The DM
51c510411aSMatthew G. Knepley 
52c510411aSMatthew G. Knepley   Output Parameters:
53c510411aSMatthew G. Knepley + facegeom - The values precomputed from face geometry
54c510411aSMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
55c510411aSMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
56c510411aSMatthew G. Knepley 
57c510411aSMatthew G. Knepley   Level: developer
58c510411aSMatthew G. Knepley 
59c510411aSMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
60c510411aSMatthew G. Knepley @*/
61b6aca0f9SMatthew G. Knepley PetscErrorCode DMPlexTSGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
62a0ac79e7SMatthew G. Knepley {
63a0ac79e7SMatthew G. Knepley   PetscErrorCode ierr;
64a0ac79e7SMatthew G. Knepley 
65a0ac79e7SMatthew G. Knepley   PetscFunctionBegin;
664b32e5bbSToby Isaac   ierr = DMPlexSNESGetGeometryFVM(dm,facegeom,cellgeom,minRadius);CHKERRQ(ierr);
67924a1b8fSMatthew G. Knepley   PetscFunctionReturn(0);
68924a1b8fSMatthew G. Knepley }
69924a1b8fSMatthew G. Knepley 
70924a1b8fSMatthew G. Knepley #undef __FUNCT__
71c49ccbb3SMatthew G. Knepley #define __FUNCT__ "DMPlexTSGetGradientDM"
72c49ccbb3SMatthew G. Knepley /*@C
73c49ccbb3SMatthew G. Knepley   DMPlexTSGetGradientDM - Return gradient data layout
74c49ccbb3SMatthew G. Knepley 
75c49ccbb3SMatthew G. Knepley   Input Parameters:
76c49ccbb3SMatthew G. Knepley + dm - The DM
77c49ccbb3SMatthew G. Knepley - fv - The PetscFV
78c49ccbb3SMatthew G. Knepley 
79c49ccbb3SMatthew G. Knepley   Output Parameter:
80c49ccbb3SMatthew G. Knepley . dmGrad - The layout for gradient values
81c49ccbb3SMatthew G. Knepley 
82c49ccbb3SMatthew G. Knepley   Level: developer
83c49ccbb3SMatthew G. Knepley 
84c49ccbb3SMatthew G. Knepley .seealso: DMPlexTSGetGeometryFVM(), DMPlexTSSetRHSFunctionLocal()
85c49ccbb3SMatthew G. Knepley @*/
86c49ccbb3SMatthew G. Knepley PetscErrorCode DMPlexTSGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
87c49ccbb3SMatthew G. Knepley {
88c49ccbb3SMatthew G. Knepley   PetscErrorCode ierr;
89c49ccbb3SMatthew G. Knepley 
90c49ccbb3SMatthew G. Knepley   PetscFunctionBegin;
914b32e5bbSToby Isaac   ierr = DMPlexSNESGetGradientDM(dm,fv,dmGrad);CHKERRQ(ierr);
92254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
93254c1ad2SMatthew G. Knepley }
94254c1ad2SMatthew G. Knepley 
95254c1ad2SMatthew G. Knepley #undef __FUNCT__
96924a1b8fSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeRHSFunctionFVM"
9708449791SMatthew G. Knepley /*@
9808449791SMatthew G. Knepley   DMPlexTSComputeRHSFunctionFVM - Form the local forcing F from the local input X using pointwise functions specified by the user
9908449791SMatthew G. Knepley 
10008449791SMatthew G. Knepley   Input Parameters:
10108449791SMatthew G. Knepley + dm - The mesh
10208449791SMatthew G. Knepley . t - The time
10308449791SMatthew G. Knepley . locX  - Local solution
10408449791SMatthew G. Knepley - user - The user context
10508449791SMatthew G. Knepley 
10608449791SMatthew G. Knepley   Output Parameter:
1073b16df42SMatthew G. Knepley . F  - Global output vector
10808449791SMatthew G. Knepley 
10908449791SMatthew G. Knepley   Level: developer
11008449791SMatthew G. Knepley 
11108449791SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
11208449791SMatthew G. Knepley @*/
1133b16df42SMatthew G. Knepley PetscErrorCode DMPlexTSComputeRHSFunctionFVM(DM dm, PetscReal time, Vec locX, Vec F, void *user)
114254c1ad2SMatthew G. Knepley {
1153b16df42SMatthew G. Knepley   Vec            locF;
116c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
1176da023fcSToby Isaac   DM             plex;
118254c1ad2SMatthew G. Knepley   PetscErrorCode ierr;
119254c1ad2SMatthew G. Knepley 
120254c1ad2SMatthew G. Knepley   PetscFunctionBegin;
1216da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1226da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
1236da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
124c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1256da023fcSToby Isaac   ierr = DMGetLocalVector(plex, &locF);CHKERRQ(ierr);
1263b16df42SMatthew G. Knepley   ierr = VecZeroEntries(locF);CHKERRQ(ierr);
12711dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, NULL, time, locF, user);CHKERRQ(ierr);
128*9bda831aSToby Isaac   ierr = DMLocalToGlobalBegin(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
129*9bda831aSToby Isaac   ierr = DMLocalToGlobalEnd(plex, locF, ADD_VALUES, F);CHKERRQ(ierr);
1306da023fcSToby Isaac   ierr = DMRestoreLocalVector(plex, &locF);CHKERRQ(ierr);
131b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
132254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
133254c1ad2SMatthew G. Knepley }
134254c1ad2SMatthew G. Knepley 
135254c1ad2SMatthew G. Knepley #undef __FUNCT__
136ef68eab9SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeBoundary"
137c5d70e09SMatthew G. Knepley /*@
138ef68eab9SMatthew 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
139c5d70e09SMatthew G. Knepley 
140c5d70e09SMatthew G. Knepley   Input Parameters:
141c5d70e09SMatthew G. Knepley + dm - The mesh
142a40652d4SToby Isaac . t - The time
143a40652d4SToby Isaac . locX  - Local solution
144a40652d4SToby Isaac . locX_t - Local solution time derivative, or NULL
145c5d70e09SMatthew G. Knepley - user - The user context
146c5d70e09SMatthew G. Knepley 
147c5d70e09SMatthew G. Knepley   Level: developer
148c5d70e09SMatthew G. Knepley 
149a40652d4SToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
150c5d70e09SMatthew G. Knepley @*/
151ef68eab9SMatthew G. Knepley PetscErrorCode DMPlexTSComputeBoundary(DM dm, PetscReal time, Vec locX, Vec locX_t, void *user)
152c5d70e09SMatthew G. Knepley {
153c5d70e09SMatthew G. Knepley   DM             plex;
154ef68eab9SMatthew G. Knepley   Vec            faceGeometryFVM = NULL;
155ef68eab9SMatthew G. Knepley   PetscInt       Nf, f;
156c5d70e09SMatthew G. Knepley   PetscErrorCode ierr;
157c5d70e09SMatthew G. Knepley 
158c5d70e09SMatthew G. Knepley   PetscFunctionBegin;
159c5d70e09SMatthew G. Knepley   ierr = DMTSConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr);
160ef68eab9SMatthew G. Knepley   ierr = DMGetNumFields(plex, &Nf);CHKERRQ(ierr);
1619586001cSMatthew G. Knepley   if (!locX_t) {
1629586001cSMatthew G. Knepley     /* This is the RHS part */
163ef68eab9SMatthew G. Knepley     for (f = 0; f < Nf; f++) {
164ef68eab9SMatthew G. Knepley       PetscObject  obj;
165ef68eab9SMatthew G. Knepley       PetscClassId id;
166ef68eab9SMatthew G. Knepley 
167ef68eab9SMatthew G. Knepley       ierr = DMGetField(plex, f, &obj);CHKERRQ(ierr);
168ef68eab9SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
169ef68eab9SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {
170ef68eab9SMatthew G. Knepley         ierr = DMPlexSNESGetGeometryFVM(plex, &faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
171ef68eab9SMatthew G. Knepley         break;
172ef68eab9SMatthew G. Knepley       }
173ef68eab9SMatthew G. Knepley     }
1749586001cSMatthew G. Knepley   }
1759586001cSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, time, faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
176a40652d4SToby Isaac   /* TODO: locX_t */
177c5d70e09SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
178c5d70e09SMatthew G. Knepley   PetscFunctionReturn(0);
179c5d70e09SMatthew G. Knepley }
180c5d70e09SMatthew G. Knepley 
181c5d70e09SMatthew G. Knepley #undef __FUNCT__
18224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
18324cdb843SMatthew G. Knepley /*@
18424cdb843SMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
18524cdb843SMatthew G. Knepley 
18624cdb843SMatthew G. Knepley   Input Parameters:
18724cdb843SMatthew G. Knepley + dm - The mesh
18824cdb843SMatthew G. Knepley . t - The time
18908449791SMatthew G. Knepley . locX  - Local solution
19008449791SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
19124cdb843SMatthew G. Knepley - user - The user context
19224cdb843SMatthew G. Knepley 
19324cdb843SMatthew G. Knepley   Output Parameter:
19408449791SMatthew G. Knepley . locF  - Local output vector
19524cdb843SMatthew G. Knepley 
19624cdb843SMatthew G. Knepley   Level: developer
19724cdb843SMatthew G. Knepley 
19824cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
19924cdb843SMatthew G. Knepley @*/
20008449791SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
20124cdb843SMatthew G. Knepley {
202c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
2036da023fcSToby Isaac   DM             plex;
20424cdb843SMatthew G. Knepley   PetscErrorCode ierr;
20524cdb843SMatthew G. Knepley 
20624cdb843SMatthew G. Knepley   PetscFunctionBegin;
2076da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2086da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
2096da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
210c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
21111dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, locX_t, time, locF, user);CHKERRQ(ierr);
212b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
21324cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
21424cdb843SMatthew G. Knepley }
2157cdb2a12SMatthew G. Knepley 
2167cdb2a12SMatthew G. Knepley #undef __FUNCT__
217756a1f44SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIJacobianFEM"
218756a1f44SMatthew G. Knepley /*@
219756a1f44SMatthew G. Knepley   DMPlexTSComputeIJacobianFEM - Form the local Jacobian J from the local input X using pointwise functions specified by the user
220756a1f44SMatthew G. Knepley 
221756a1f44SMatthew G. Knepley   Input Parameters:
222756a1f44SMatthew G. Knepley + dm - The mesh
223756a1f44SMatthew G. Knepley . t - The time
224756a1f44SMatthew G. Knepley . locX  - Local solution
225756a1f44SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
226756a1f44SMatthew G. Knepley . X_tshift - The multiplicative parameter for dF/du_t
227756a1f44SMatthew G. Knepley - user - The user context
228756a1f44SMatthew G. Knepley 
229756a1f44SMatthew G. Knepley   Output Parameter:
230756a1f44SMatthew G. Knepley . locF  - Local output vector
231756a1f44SMatthew G. Knepley 
232756a1f44SMatthew G. Knepley   Level: developer
233756a1f44SMatthew G. Knepley 
234756a1f44SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
235756a1f44SMatthew G. Knepley @*/
236756a1f44SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIJacobianFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
237756a1f44SMatthew G. Knepley {
238756a1f44SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
239756a1f44SMatthew G. Knepley   DM             plex;
240756a1f44SMatthew G. Knepley   PetscErrorCode ierr;
241756a1f44SMatthew G. Knepley 
242756a1f44SMatthew G. Knepley   PetscFunctionBegin;
243756a1f44SMatthew G. Knepley   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
244756a1f44SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
245756a1f44SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
246756a1f44SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
247756a1f44SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, time, X_tShift, locX, locX_t, Jac, JacP, user);CHKERRQ(ierr);
248756a1f44SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
249756a1f44SMatthew G. Knepley   PetscFunctionReturn(0);
250756a1f44SMatthew G. Knepley }
251756a1f44SMatthew G. Knepley 
252756a1f44SMatthew G. Knepley #undef __FUNCT__
2537cdb2a12SMatthew G. Knepley #define __FUNCT__ "DMTSCheckFromOptions"
2540163fd50SMatthew 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)
2557cdb2a12SMatthew G. Knepley {
2567cdb2a12SMatthew G. Knepley   DM             dm;
2577cdb2a12SMatthew G. Knepley   SNES           snes;
2581878804aSMatthew G. Knepley   Vec            sol;
2597cdb2a12SMatthew G. Knepley   PetscBool      check;
2607cdb2a12SMatthew G. Knepley   PetscErrorCode ierr;
2617cdb2a12SMatthew G. Knepley 
2627cdb2a12SMatthew G. Knepley   PetscFunctionBegin;
263c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix, "-dmts_check", &check);CHKERRQ(ierr);
2647cdb2a12SMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
26572fd7fc8SMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
26672fd7fc8SMatthew G. Knepley   ierr = TSSetSolution(ts, sol);CHKERRQ(ierr);
2677cdb2a12SMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
2687cdb2a12SMatthew G. Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2697cdb2a12SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
2707cdb2a12SMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
2711878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
27272fd7fc8SMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2737cdb2a12SMatthew G. Knepley   PetscFunctionReturn(0);
2747cdb2a12SMatthew G. Knepley }
275