xref: /petsc/src/ts/utils/dmplexts.c (revision bebe2cf65d55febe21a5af8db2bd2e168caaa2e7)
1 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/
2 #include <petsc/private/tsimpl.h>     /*I "petscts.h" I*/
3 #include <petsc/private/snesimpl.h>
4 #include <petscds.h>
5 #include <petscfv.h>
6 
7 #undef __FUNCT__
8 #define __FUNCT__ "DMPlexTSGetGeometryFVM"
9 /*@
10   DMPlexTSGetGeometryFVM - Return precomputed geometric data
11 
12   Input Parameter:
13 . dm - The DM
14 
15   Output Parameters:
16 + facegeom - The values precomputed from face geometry
17 . cellgeom - The values precomputed from cell geometry
18 - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
19 
20   Level: developer
21 
22 .seealso: DMPlexTSSetRHSFunctionLocal()
23 @*/
24 PetscErrorCode DMPlexTSGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
25 {
26   DMTS           dmts;
27   PetscObject    obj;
28   PetscErrorCode ierr;
29 
30   PetscFunctionBegin;
31   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32   ierr = DMGetDMTS(dm, &dmts);CHKERRQ(ierr);
33   ierr = PetscObjectQuery((PetscObject) dmts, "DMPlexTS_facegeom_fvm", &obj);CHKERRQ(ierr);
34   if (!obj) {
35     Vec cellgeom, facegeom;
36 
37     ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr);
38     ierr = PetscObjectCompose((PetscObject) dmts, "DMPlexTS_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr);
39     ierr = PetscObjectCompose((PetscObject) dmts, "DMPlexTS_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr);
40     ierr = VecDestroy(&facegeom);CHKERRQ(ierr);
41     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
42   }
43   if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmts, "DMPlexTS_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);}
44   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmts, "DMPlexTS_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);}
45   if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);}
46   PetscFunctionReturn(0);
47 }
48 
49 #undef __FUNCT__
50 #define __FUNCT__ "DMPlexTSGetGradientDM"
51 /*@C
52   DMPlexTSGetGradientDM - Return gradient data layout
53 
54   Input Parameters:
55 + dm - The DM
56 - fv - The PetscFV
57 
58   Output Parameter:
59 . dmGrad - The layout for gradient values
60 
61   Level: developer
62 
63 .seealso: DMPlexTSGetGeometryFVM(), DMPlexTSSetRHSFunctionLocal()
64 @*/
65 PetscErrorCode DMPlexTSGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
66 {
67   DMTS           dmts;
68   PetscObject    obj;
69   PetscBool      computeGradients;
70   PetscErrorCode ierr;
71 
72   PetscFunctionBegin;
73   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
74   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
75   PetscValidPointer(dmGrad,3);
76   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
77   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
78   ierr = DMGetDMTS(dm, &dmts);CHKERRQ(ierr);
79   ierr = PetscObjectQuery((PetscObject) dmts, "DMPlexTS_dmgrad_fvm", &obj);CHKERRQ(ierr);
80   if (!obj) {
81     DM  dmGrad;
82     Vec faceGeometry, cellGeometry;
83 
84     ierr = DMPlexTSGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr);
85     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr);
86     ierr = PetscObjectCompose((PetscObject) dmts, "DMPlexTS_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr);
87     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
88   }
89   ierr = PetscObjectQuery((PetscObject) dmts, "DMPlexTS_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr);
90   PetscFunctionReturn(0);
91 }
92 
93 #undef __FUNCT__
94 #define __FUNCT__ "DMPlexTSComputeRHSFunctionFVM"
95 /*@
96   DMPlexTSComputeRHSFunctionFVM - Form the local forcing F from the local input X using pointwise functions specified by the user
97 
98   Input Parameters:
99 + dm - The mesh
100 . t - The time
101 . locX  - Local solution
102 - user - The user context
103 
104   Output Parameter:
105 . F  - Global output vector
106 
107   Level: developer
108 
109 .seealso: DMPlexComputeJacobianActionFEM()
110 @*/
111 PetscErrorCode DMPlexTSComputeRHSFunctionFVM(DM dm, PetscReal time, Vec locX, Vec F, void *user)
112 {
113   Vec            locF;
114   PetscErrorCode ierr;
115 
116   PetscFunctionBegin;
117   ierr = DMGetLocalVector(dm, &locF);CHKERRQ(ierr);
118   ierr = VecZeroEntries(locF);CHKERRQ(ierr);
119   ierr = DMPlexComputeResidual_Internal(dm, time, locX, NULL, locF, user);CHKERRQ(ierr);
120   ierr = DMLocalToGlobalBegin(dm, locF, INSERT_VALUES, F);CHKERRQ(ierr);
121   ierr = DMLocalToGlobalEnd(dm, locF, INSERT_VALUES, F);CHKERRQ(ierr);
122   ierr = DMRestoreLocalVector(dm, &locF);CHKERRQ(ierr);
123   PetscFunctionReturn(0);
124 }
125 
126 #undef __FUNCT__
127 #define __FUNCT__ "DMPlexTSComputeIFunctionFEM"
128 /*@
129   DMPlexTSComputeIFunctionFEM - Form the local residual F from the local input X using pointwise functions specified by the user
130 
131   Input Parameters:
132 + dm - The mesh
133 . t - The time
134 . locX  - Local solution
135 . locX_t - Local solution time derivative, or NULL
136 - user - The user context
137 
138   Output Parameter:
139 . locF  - Local output vector
140 
141   Level: developer
142 
143 .seealso: DMPlexComputeJacobianActionFEM()
144 @*/
145 PetscErrorCode DMPlexTSComputeIFunctionFEM(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
146 {
147   PetscErrorCode ierr;
148 
149   PetscFunctionBegin;
150   ierr = DMPlexComputeResidual_Internal(dm, time, locX, locX_t, locF, user);CHKERRQ(ierr);
151   PetscFunctionReturn(0);
152 }
153 
154 #undef __FUNCT__
155 #define __FUNCT__ "DMTSCheckFromOptions"
156 PetscErrorCode DMTSCheckFromOptions(TS ts, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
157 {
158   DM             dm;
159   SNES           snes;
160   Vec            sol;
161   PetscBool      check;
162   PetscErrorCode ierr;
163 
164   PetscFunctionBegin;
165   ierr = PetscOptionsHasName(ts->hdr.prefix, "-dmts_check", &check);CHKERRQ(ierr);
166   if (!check) PetscFunctionReturn(0);
167   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
168   ierr = TSSetSolution(ts, sol);CHKERRQ(ierr);
169   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
170   ierr = TSSetUp(ts);CHKERRQ(ierr);
171   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
172   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
173   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
174   ierr = VecDestroy(&sol);CHKERRQ(ierr);
175   PetscFunctionReturn(0);
176 }
177