xref: /petsc/src/ts/utils/dmplexts.c (revision 4b32e5bb03f0d1940c4dd014b909fec0242d5c28)
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   DMTS           dmts;
641faf85eaSMatthew G. Knepley   PetscObject    obj;
656da023fcSToby Isaac   DM             plex;
66a0ac79e7SMatthew G. Knepley   PetscErrorCode ierr;
67a0ac79e7SMatthew G. Knepley 
68a0ac79e7SMatthew G. Knepley   PetscFunctionBegin;
69*4b32e5bbSToby Isaac   ierr = DMPlexSNESGetGeometryFVM(dm,facegeom,cellgeom,minRadius);CHKERRQ(ierr);
70924a1b8fSMatthew G. Knepley   PetscFunctionReturn(0);
71924a1b8fSMatthew G. Knepley }
72924a1b8fSMatthew G. Knepley 
73924a1b8fSMatthew G. Knepley #undef __FUNCT__
74c49ccbb3SMatthew G. Knepley #define __FUNCT__ "DMPlexTSGetGradientDM"
75c49ccbb3SMatthew G. Knepley /*@C
76c49ccbb3SMatthew G. Knepley   DMPlexTSGetGradientDM - Return gradient data layout
77c49ccbb3SMatthew G. Knepley 
78c49ccbb3SMatthew G. Knepley   Input Parameters:
79c49ccbb3SMatthew G. Knepley + dm - The DM
80c49ccbb3SMatthew G. Knepley - fv - The PetscFV
81c49ccbb3SMatthew G. Knepley 
82c49ccbb3SMatthew G. Knepley   Output Parameter:
83c49ccbb3SMatthew G. Knepley . dmGrad - The layout for gradient values
84c49ccbb3SMatthew G. Knepley 
85c49ccbb3SMatthew G. Knepley   Level: developer
86c49ccbb3SMatthew G. Knepley 
87c49ccbb3SMatthew G. Knepley .seealso: DMPlexTSGetGeometryFVM(), DMPlexTSSetRHSFunctionLocal()
88c49ccbb3SMatthew G. Knepley @*/
89c49ccbb3SMatthew G. Knepley PetscErrorCode DMPlexTSGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
90c49ccbb3SMatthew G. Knepley {
91c49ccbb3SMatthew G. Knepley   DMTS           dmts;
92c49ccbb3SMatthew G. Knepley   PetscObject    obj;
93c49ccbb3SMatthew G. Knepley   PetscBool      computeGradients;
946da023fcSToby Isaac   DM             plex;
95c49ccbb3SMatthew G. Knepley   PetscErrorCode ierr;
96c49ccbb3SMatthew G. Knepley 
97c49ccbb3SMatthew G. Knepley   PetscFunctionBegin;
98*4b32e5bbSToby Isaac   ierr = DMPlexSNESGetGradientDM(dm,fv,dmGrad);CHKERRQ(ierr);
99254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
100254c1ad2SMatthew G. Knepley }
101254c1ad2SMatthew G. Knepley 
102254c1ad2SMatthew G. Knepley #undef __FUNCT__
103924a1b8fSMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeRHSFunctionFVM"
10408449791SMatthew G. Knepley /*@
10508449791SMatthew G. Knepley   DMPlexTSComputeRHSFunctionFVM - Form the local forcing F from the local input X using pointwise functions specified by the user
10608449791SMatthew G. Knepley 
10708449791SMatthew G. Knepley   Input Parameters:
10808449791SMatthew G. Knepley + dm - The mesh
10908449791SMatthew G. Knepley . t - The time
11008449791SMatthew G. Knepley . locX  - Local solution
11108449791SMatthew G. Knepley - user - The user context
11208449791SMatthew G. Knepley 
11308449791SMatthew G. Knepley   Output Parameter:
1143b16df42SMatthew G. Knepley . F  - Global output vector
11508449791SMatthew G. Knepley 
11608449791SMatthew G. Knepley   Level: developer
11708449791SMatthew G. Knepley 
11808449791SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
11908449791SMatthew G. Knepley @*/
1203b16df42SMatthew G. Knepley PetscErrorCode DMPlexTSComputeRHSFunctionFVM(DM dm, PetscReal time, Vec locX, Vec F, void *user)
121254c1ad2SMatthew G. Knepley {
1223b16df42SMatthew G. Knepley   Vec            locF;
123c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
1246da023fcSToby Isaac   DM             plex;
125254c1ad2SMatthew G. Knepley   PetscErrorCode ierr;
126254c1ad2SMatthew G. Knepley 
127254c1ad2SMatthew G. Knepley   PetscFunctionBegin;
1286da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1296da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
1306da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
131c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1326da023fcSToby Isaac   ierr = DMGetLocalVector(plex, &locF);CHKERRQ(ierr);
1333b16df42SMatthew G. Knepley   ierr = VecZeroEntries(locF);CHKERRQ(ierr);
13411dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, NULL, time, locF, user);CHKERRQ(ierr);
1356da023fcSToby Isaac   ierr = DMLocalToGlobalBegin(plex, locF, INSERT_VALUES, F);CHKERRQ(ierr);
1366da023fcSToby Isaac   ierr = DMLocalToGlobalEnd(plex, locF, INSERT_VALUES, F);CHKERRQ(ierr);
1376da023fcSToby Isaac   ierr = DMRestoreLocalVector(plex, &locF);CHKERRQ(ierr);
138b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
139254c1ad2SMatthew G. Knepley   PetscFunctionReturn(0);
140254c1ad2SMatthew G. Knepley }
141254c1ad2SMatthew G. Knepley 
142254c1ad2SMatthew G. Knepley #undef __FUNCT__
143ef68eab9SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeBoundary"
144c5d70e09SMatthew G. Knepley /*@
145ef68eab9SMatthew 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
146c5d70e09SMatthew G. Knepley 
147c5d70e09SMatthew G. Knepley   Input Parameters:
148c5d70e09SMatthew G. Knepley + dm - The mesh
149a40652d4SToby Isaac . t - The time
150a40652d4SToby Isaac . locX  - Local solution
151a40652d4SToby Isaac . locX_t - Local solution time derivative, or NULL
152c5d70e09SMatthew G. Knepley - user - The user context
153c5d70e09SMatthew G. Knepley 
154c5d70e09SMatthew G. Knepley   Level: developer
155c5d70e09SMatthew G. Knepley 
156a40652d4SToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
157c5d70e09SMatthew G. Knepley @*/
158ef68eab9SMatthew G. Knepley PetscErrorCode DMPlexTSComputeBoundary(DM dm, PetscReal time, Vec locX, Vec locX_t, void *user)
159c5d70e09SMatthew G. Knepley {
160c5d70e09SMatthew G. Knepley   DM             plex;
161ef68eab9SMatthew G. Knepley   Vec            faceGeometryFVM = NULL;
162ef68eab9SMatthew G. Knepley   PetscInt       Nf, f;
163c5d70e09SMatthew G. Knepley   PetscErrorCode ierr;
164c5d70e09SMatthew G. Knepley 
165c5d70e09SMatthew G. Knepley   PetscFunctionBegin;
166c5d70e09SMatthew G. Knepley   ierr = DMTSConvertPlex(dm, &plex, PETSC_TRUE);CHKERRQ(ierr);
167ef68eab9SMatthew G. Knepley   ierr = DMGetNumFields(plex, &Nf);CHKERRQ(ierr);
1689586001cSMatthew G. Knepley   if (!locX_t) {
1699586001cSMatthew G. Knepley     /* This is the RHS part */
170ef68eab9SMatthew G. Knepley     for (f = 0; f < Nf; f++) {
171ef68eab9SMatthew G. Knepley       PetscObject  obj;
172ef68eab9SMatthew G. Knepley       PetscClassId id;
173ef68eab9SMatthew G. Knepley 
174ef68eab9SMatthew G. Knepley       ierr = DMGetField(plex, f, &obj);CHKERRQ(ierr);
175ef68eab9SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
176ef68eab9SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {
177ef68eab9SMatthew G. Knepley         ierr = DMPlexSNESGetGeometryFVM(plex, &faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
178ef68eab9SMatthew G. Knepley         break;
179ef68eab9SMatthew G. Knepley       }
180ef68eab9SMatthew G. Knepley     }
1819586001cSMatthew G. Knepley   }
1829586001cSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, time, faceGeometryFVM, NULL, NULL);CHKERRQ(ierr);
183a40652d4SToby Isaac   /* TODO: locX_t */
184c5d70e09SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
185c5d70e09SMatthew G. Knepley   PetscFunctionReturn(0);
186c5d70e09SMatthew G. Knepley }
187c5d70e09SMatthew G. Knepley 
188c5d70e09SMatthew G. Knepley #undef __FUNCT__
18924cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
19024cdb843SMatthew G. Knepley /*@
19124cdb843SMatthew G. Knepley   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
19224cdb843SMatthew G. Knepley 
19324cdb843SMatthew G. Knepley   Input Parameters:
19424cdb843SMatthew G. Knepley + dm - The mesh
19524cdb843SMatthew G. Knepley . t - The time
19608449791SMatthew G. Knepley . locX  - Local solution
19708449791SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
19824cdb843SMatthew G. Knepley - user - The user context
19924cdb843SMatthew G. Knepley 
20024cdb843SMatthew G. Knepley   Output Parameter:
20108449791SMatthew G. Knepley . locF  - Local output vector
20224cdb843SMatthew G. Knepley 
20324cdb843SMatthew G. Knepley   Level: developer
20424cdb843SMatthew G. Knepley 
20524cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
20624cdb843SMatthew G. Knepley @*/
20708449791SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
20824cdb843SMatthew G. Knepley {
209c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
2106da023fcSToby Isaac   DM             plex;
21124cdb843SMatthew G. Knepley   PetscErrorCode ierr;
21224cdb843SMatthew G. Knepley 
21324cdb843SMatthew G. Knepley   PetscFunctionBegin;
2146da023fcSToby Isaac   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2156da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
2166da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
217c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
21811dd639bSMatthew G. Knepley   ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, time, locX, locX_t, time, locF, user);CHKERRQ(ierr);
219b2338dbcSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
22024cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
22124cdb843SMatthew G. Knepley }
2227cdb2a12SMatthew G. Knepley 
2237cdb2a12SMatthew G. Knepley #undef __FUNCT__
224756a1f44SMatthew G. Knepley #define __FUNCT__ "DMPlexTSComputeIJacobianFEM"
225756a1f44SMatthew G. Knepley /*@
226756a1f44SMatthew G. Knepley   DMPlexTSComputeIJacobianFEM - Form the local Jacobian J from the local input X using pointwise functions specified by the user
227756a1f44SMatthew G. Knepley 
228756a1f44SMatthew G. Knepley   Input Parameters:
229756a1f44SMatthew G. Knepley + dm - The mesh
230756a1f44SMatthew G. Knepley . t - The time
231756a1f44SMatthew G. Knepley . locX  - Local solution
232756a1f44SMatthew G. Knepley . locX_t - Local solution time derivative, or NULL
233756a1f44SMatthew G. Knepley . X_tshift - The multiplicative parameter for dF/du_t
234756a1f44SMatthew G. Knepley - user - The user context
235756a1f44SMatthew G. Knepley 
236756a1f44SMatthew G. Knepley   Output Parameter:
237756a1f44SMatthew G. Knepley . locF  - Local output vector
238756a1f44SMatthew G. Knepley 
239756a1f44SMatthew G. Knepley   Level: developer
240756a1f44SMatthew G. Knepley 
241756a1f44SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
242756a1f44SMatthew G. Knepley @*/
243756a1f44SMatthew G. Knepley PetscErrorCode DMPlexTSComputeIJacobianFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
244756a1f44SMatthew G. Knepley {
245756a1f44SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
246756a1f44SMatthew G. Knepley   DM             plex;
247756a1f44SMatthew G. Knepley   PetscErrorCode ierr;
248756a1f44SMatthew G. Knepley 
249756a1f44SMatthew G. Knepley   PetscFunctionBegin;
250756a1f44SMatthew G. Knepley   ierr = DMTSConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
251756a1f44SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
252756a1f44SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
253756a1f44SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
254756a1f44SMatthew G. Knepley   ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, time, X_tShift, locX, locX_t, Jac, JacP, user);CHKERRQ(ierr);
255756a1f44SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
256756a1f44SMatthew G. Knepley   PetscFunctionReturn(0);
257756a1f44SMatthew G. Knepley }
258756a1f44SMatthew G. Knepley 
259756a1f44SMatthew G. Knepley #undef __FUNCT__
2607cdb2a12SMatthew G. Knepley #define __FUNCT__ "DMTSCheckFromOptions"
2610163fd50SMatthew 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)
2627cdb2a12SMatthew G. Knepley {
2637cdb2a12SMatthew G. Knepley   DM             dm;
2647cdb2a12SMatthew G. Knepley   SNES           snes;
2651878804aSMatthew G. Knepley   Vec            sol;
2667cdb2a12SMatthew G. Knepley   PetscBool      check;
2677cdb2a12SMatthew G. Knepley   PetscErrorCode ierr;
2687cdb2a12SMatthew G. Knepley 
2697cdb2a12SMatthew G. Knepley   PetscFunctionBegin;
270c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)ts)->options,((PetscObject)ts)->prefix, "-dmts_check", &check);CHKERRQ(ierr);
2717cdb2a12SMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
27272fd7fc8SMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
27372fd7fc8SMatthew G. Knepley   ierr = TSSetSolution(ts, sol);CHKERRQ(ierr);
2747cdb2a12SMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
2757cdb2a12SMatthew G. Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
2767cdb2a12SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
2777cdb2a12SMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
2781878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
27972fd7fc8SMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2807cdb2a12SMatthew G. Knepley   PetscFunctionReturn(0);
2817cdb2a12SMatthew G. Knepley }
282