xref: /petsc/src/snes/utils/dmplexsnes.c (revision 1a2443445f10a6cdb92f6af2ca31cdcaa5f5151b)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I "petscdmplex.h" I*/
2af0996ceSBarry Smith #include <petsc/private/snesimpl.h>     /*I "petscsnes.h"   I*/
324cdb843SMatthew G. Knepley #include <petscds.h>
4af0996ceSBarry Smith #include <petsc/private/petscimpl.h>
5af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
6552f7358SJed Brown 
724cdb843SMatthew G. Knepley /************************** Interpolation *******************************/
824cdb843SMatthew G. Knepley 
9552f7358SJed Brown #undef __FUNCT__
106da023fcSToby Isaac #define __FUNCT__ "DMSNESConvertPlex"
116da023fcSToby Isaac static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy)
126da023fcSToby Isaac {
136da023fcSToby Isaac   PetscBool      isPlex;
146da023fcSToby Isaac   PetscErrorCode ierr;
156da023fcSToby Isaac 
166da023fcSToby Isaac   PetscFunctionBegin;
176da023fcSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
186da023fcSToby Isaac   if (isPlex) {
196da023fcSToby Isaac     *plex = dm;
206da023fcSToby Isaac     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
21f7148743SMatthew G. Knepley   } else {
22f7148743SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
23f7148743SMatthew G. Knepley     if (!*plex) {
246da023fcSToby Isaac       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
25f7148743SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
266da023fcSToby Isaac       if (copy) {
276da023fcSToby Isaac         PetscInt    i;
286da023fcSToby Isaac         PetscObject obj;
296da023fcSToby Isaac         const char *comps[3] = {"A","dmAux","dmCh"};
306da023fcSToby Isaac 
316da023fcSToby Isaac         ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr);
326da023fcSToby Isaac         for (i = 0; i < 3; i++) {
336da023fcSToby Isaac           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
346da023fcSToby Isaac           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
356da023fcSToby Isaac         }
366da023fcSToby Isaac       }
37f7148743SMatthew G. Knepley     } else {
38f7148743SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
39f7148743SMatthew G. Knepley     }
406da023fcSToby Isaac   }
416da023fcSToby Isaac   PetscFunctionReturn(0);
426da023fcSToby Isaac }
436da023fcSToby Isaac 
446da023fcSToby Isaac #undef __FUNCT__
45552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate"
460adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
470adebc6cSBarry Smith {
48552f7358SJed Brown   PetscErrorCode ierr;
49552f7358SJed Brown 
50552f7358SJed Brown   PetscFunctionBegin;
51552f7358SJed Brown   PetscValidPointer(ctx, 2);
52552f7358SJed Brown   ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr);
531aa26658SKarl Rupp 
54552f7358SJed Brown   (*ctx)->comm   = comm;
55552f7358SJed Brown   (*ctx)->dim    = -1;
56552f7358SJed Brown   (*ctx)->nInput = 0;
570298fd71SBarry Smith   (*ctx)->points = NULL;
580298fd71SBarry Smith   (*ctx)->cells  = NULL;
59552f7358SJed Brown   (*ctx)->n      = -1;
600298fd71SBarry Smith   (*ctx)->coords = NULL;
61552f7358SJed Brown   PetscFunctionReturn(0);
62552f7358SJed Brown }
63552f7358SJed Brown 
64552f7358SJed Brown #undef __FUNCT__
65552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim"
660adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
670adebc6cSBarry Smith {
68552f7358SJed Brown   PetscFunctionBegin;
690adebc6cSBarry Smith   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
70552f7358SJed Brown   ctx->dim = dim;
71552f7358SJed Brown   PetscFunctionReturn(0);
72552f7358SJed Brown }
73552f7358SJed Brown 
74552f7358SJed Brown #undef __FUNCT__
75552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim"
760adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
770adebc6cSBarry Smith {
78552f7358SJed Brown   PetscFunctionBegin;
79552f7358SJed Brown   PetscValidIntPointer(dim, 2);
80552f7358SJed Brown   *dim = ctx->dim;
81552f7358SJed Brown   PetscFunctionReturn(0);
82552f7358SJed Brown }
83552f7358SJed Brown 
84552f7358SJed Brown #undef __FUNCT__
85552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof"
860adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
870adebc6cSBarry Smith {
88552f7358SJed Brown   PetscFunctionBegin;
890adebc6cSBarry Smith   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
90552f7358SJed Brown   ctx->dof = dof;
91552f7358SJed Brown   PetscFunctionReturn(0);
92552f7358SJed Brown }
93552f7358SJed Brown 
94552f7358SJed Brown #undef __FUNCT__
95552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof"
960adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
970adebc6cSBarry Smith {
98552f7358SJed Brown   PetscFunctionBegin;
99552f7358SJed Brown   PetscValidIntPointer(dof, 2);
100552f7358SJed Brown   *dof = ctx->dof;
101552f7358SJed Brown   PetscFunctionReturn(0);
102552f7358SJed Brown }
103552f7358SJed Brown 
104552f7358SJed Brown #undef __FUNCT__
105552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints"
1060adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
1070adebc6cSBarry Smith {
108552f7358SJed Brown   PetscErrorCode ierr;
109552f7358SJed Brown 
110552f7358SJed Brown   PetscFunctionBegin;
1110adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
1120adebc6cSBarry Smith   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
113552f7358SJed Brown   ctx->nInput = n;
1141aa26658SKarl Rupp 
115785e854fSJed Brown   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
116552f7358SJed Brown   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
117552f7358SJed Brown   PetscFunctionReturn(0);
118552f7358SJed Brown }
119552f7358SJed Brown 
120552f7358SJed Brown #undef __FUNCT__
121552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp"
1220adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
1230adebc6cSBarry Smith {
124552f7358SJed Brown   MPI_Comm          comm = ctx->comm;
125552f7358SJed Brown   PetscScalar       *a;
126552f7358SJed Brown   PetscInt          p, q, i;
127552f7358SJed Brown   PetscMPIInt       rank, size;
128552f7358SJed Brown   PetscErrorCode    ierr;
129552f7358SJed Brown   Vec               pointVec;
1303a93e3b7SToby Isaac   PetscSF           cellSF;
131552f7358SJed Brown   PetscLayout       layout;
132552f7358SJed Brown   PetscReal         *globalPoints;
133cb313848SJed Brown   PetscScalar       *globalPointsScalar;
134552f7358SJed Brown   const PetscInt    *ranges;
135552f7358SJed Brown   PetscMPIInt       *counts, *displs;
1363a93e3b7SToby Isaac   const PetscSFNode *foundCells;
1373a93e3b7SToby Isaac   const PetscInt    *foundPoints;
138552f7358SJed Brown   PetscMPIInt       *foundProcs, *globalProcs;
1393a93e3b7SToby Isaac   PetscInt          n, N, numFound;
140552f7358SJed Brown 
14119436ca2SJed Brown   PetscFunctionBegin;
14219436ca2SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14319436ca2SJed Brown   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
14419436ca2SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1450adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
14619436ca2SJed Brown   /* Locate points */
14719436ca2SJed Brown   n = ctx->nInput;
148552f7358SJed Brown   if (!redundantPoints) {
149552f7358SJed Brown     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
150552f7358SJed Brown     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
151552f7358SJed Brown     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
152552f7358SJed Brown     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
153552f7358SJed Brown     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
154552f7358SJed Brown     /* Communicate all points to all processes */
155dcca6d9dSJed Brown     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
156552f7358SJed Brown     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
157552f7358SJed Brown     for (p = 0; p < size; ++p) {
158552f7358SJed Brown       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
159552f7358SJed Brown       displs[p] = ranges[p]*ctx->dim;
160552f7358SJed Brown     }
161552f7358SJed Brown     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
162552f7358SJed Brown   } else {
163552f7358SJed Brown     N = n;
164552f7358SJed Brown     globalPoints = ctx->points;
16538ea73c8SJed Brown     counts = displs = NULL;
16638ea73c8SJed Brown     layout = NULL;
167552f7358SJed Brown   }
168552f7358SJed Brown #if 0
169dcca6d9dSJed Brown   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
17019436ca2SJed Brown   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
171552f7358SJed Brown #else
172cb313848SJed Brown #if defined(PETSC_USE_COMPLEX)
173785e854fSJed Brown   ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr);
174cb313848SJed Brown   for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i];
175cb313848SJed Brown #else
176cb313848SJed Brown   globalPointsScalar = globalPoints;
177cb313848SJed Brown #endif
17804706141SMatthew G Knepley   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
179dcca6d9dSJed Brown   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
1803a93e3b7SToby Isaac   cellSF = NULL;
1813a93e3b7SToby Isaac   ierr = DMLocatePoints(dm, pointVec, &cellSF);CHKERRQ(ierr);
1823a93e3b7SToby Isaac   ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr);
183552f7358SJed Brown #endif
1843a93e3b7SToby Isaac   for (p = 0; p < numFound; ++p) {
1853a93e3b7SToby Isaac     if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank;
1863a93e3b7SToby Isaac     else foundProcs[foundPoints ? foundPoints[p] : p] = size;
187552f7358SJed Brown   }
188552f7358SJed Brown   /* Let the lowest rank process own each point */
189b2566f29SBarry Smith   ierr   = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
190552f7358SJed Brown   ctx->n = 0;
191552f7358SJed Brown   for (p = 0; p < N; ++p) {
1920adebc6cSBarry Smith     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0);
1931aa26658SKarl Rupp     else if (globalProcs[p] == rank) ctx->n++;
194552f7358SJed Brown   }
195552f7358SJed Brown   /* Create coordinates vector and array of owned cells */
196785e854fSJed Brown   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
197552f7358SJed Brown   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
198552f7358SJed Brown   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
199552f7358SJed Brown   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
200c0dedaeaSBarry Smith   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
201552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
202552f7358SJed Brown   for (p = 0, q = 0, i = 0; p < N; ++p) {
203552f7358SJed Brown     if (globalProcs[p] == rank) {
204552f7358SJed Brown       PetscInt d;
205552f7358SJed Brown 
2061aa26658SKarl Rupp       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
2073a93e3b7SToby Isaac       ctx->cells[q++] = foundCells[p].index;
208552f7358SJed Brown     }
209552f7358SJed Brown   }
210552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
211552f7358SJed Brown #if 0
212552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
213552f7358SJed Brown #else
214552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
2153a93e3b7SToby Isaac   ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr);
216552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
217552f7358SJed Brown #endif
218cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
219d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
220552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
221552f7358SJed Brown   PetscFunctionReturn(0);
222552f7358SJed Brown }
223552f7358SJed Brown 
224552f7358SJed Brown #undef __FUNCT__
225552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates"
2260adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
2270adebc6cSBarry Smith {
228552f7358SJed Brown   PetscFunctionBegin;
229552f7358SJed Brown   PetscValidPointer(coordinates, 2);
2300adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
231552f7358SJed Brown   *coordinates = ctx->coords;
232552f7358SJed Brown   PetscFunctionReturn(0);
233552f7358SJed Brown }
234552f7358SJed Brown 
235552f7358SJed Brown #undef __FUNCT__
236552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector"
2370adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2380adebc6cSBarry Smith {
239552f7358SJed Brown   PetscErrorCode ierr;
240552f7358SJed Brown 
241552f7358SJed Brown   PetscFunctionBegin;
242552f7358SJed Brown   PetscValidPointer(v, 2);
2430adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
244552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
245552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
246552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
247c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
248552f7358SJed Brown   PetscFunctionReturn(0);
249552f7358SJed Brown }
250552f7358SJed Brown 
251552f7358SJed Brown #undef __FUNCT__
252552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector"
2530adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2540adebc6cSBarry Smith {
255552f7358SJed Brown   PetscErrorCode ierr;
256552f7358SJed Brown 
257552f7358SJed Brown   PetscFunctionBegin;
258552f7358SJed Brown   PetscValidPointer(v, 2);
2590adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
260552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
261552f7358SJed Brown   PetscFunctionReturn(0);
262552f7358SJed Brown }
263552f7358SJed Brown 
264552f7358SJed Brown #undef __FUNCT__
2657a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private"
2667a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
267a6dfd86eSKarl Rupp {
268552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
26956044e6dSMatthew G. Knepley   const PetscScalar *coords;
27056044e6dSMatthew G. Knepley   PetscScalar    *a;
271552f7358SJed Brown   PetscInt       p;
272552f7358SJed Brown   PetscErrorCode ierr;
273552f7358SJed Brown 
274552f7358SJed Brown   PetscFunctionBegin;
275dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
27656044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
277552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
278552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
279552f7358SJed Brown     PetscInt     c = ctx->cells[p];
280a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
281552f7358SJed Brown     PetscReal    xi[4];
282552f7358SJed Brown     PetscInt     d, f, comp;
283552f7358SJed Brown 
2848e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
285552f7358SJed Brown     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2860298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2871aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2881aa26658SKarl Rupp 
289552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
290552f7358SJed Brown       xi[d] = 0.0;
2911aa26658SKarl Rupp       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
2921aa26658SKarl Rupp       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
293552f7358SJed Brown     }
2940298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
295552f7358SJed Brown   }
296552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
29756044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
298552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
299552f7358SJed Brown   PetscFunctionReturn(0);
300552f7358SJed Brown }
301552f7358SJed Brown 
302552f7358SJed Brown #undef __FUNCT__
3037a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private"
3047a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
3057a1931ceSMatthew G. Knepley {
3067a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
30756044e6dSMatthew G. Knepley   const PetscScalar *coords;
30856044e6dSMatthew G. Knepley   PetscScalar    *a;
3097a1931ceSMatthew G. Knepley   PetscInt       p;
3107a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
3117a1931ceSMatthew G. Knepley 
3127a1931ceSMatthew G. Knepley   PetscFunctionBegin;
313dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
31456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
3157a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
3167a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
3177a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
3187a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
3192584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
3207a1931ceSMatthew G. Knepley     PetscReal      xi[4];
3217a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
3227a1931ceSMatthew G. Knepley 
3238e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
3247a1931ceSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
3257a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3267a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
3277a1931ceSMatthew G. Knepley 
3287a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
3297a1931ceSMatthew G. Knepley       xi[d] = 0.0;
3307a1931ceSMatthew G. Knepley       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
3317a1931ceSMatthew G. Knepley       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
3327a1931ceSMatthew G. Knepley     }
3337a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3347a1931ceSMatthew G. Knepley   }
3357a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
33656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
3377a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
3387a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3397a1931ceSMatthew G. Knepley }
3407a1931ceSMatthew G. Knepley 
3417a1931ceSMatthew G. Knepley #undef __FUNCT__
342552f7358SJed Brown #define __FUNCT__ "QuadMap_Private"
3435820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
344552f7358SJed Brown {
345552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
346552f7358SJed Brown   const PetscScalar x0        = vertices[0];
347552f7358SJed Brown   const PetscScalar y0        = vertices[1];
348552f7358SJed Brown   const PetscScalar x1        = vertices[2];
349552f7358SJed Brown   const PetscScalar y1        = vertices[3];
350552f7358SJed Brown   const PetscScalar x2        = vertices[4];
351552f7358SJed Brown   const PetscScalar y2        = vertices[5];
352552f7358SJed Brown   const PetscScalar x3        = vertices[6];
353552f7358SJed Brown   const PetscScalar y3        = vertices[7];
354552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
355552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
356552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
357552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
358552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
359552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
36056044e6dSMatthew G. Knepley   const PetscScalar *ref;
36156044e6dSMatthew G. Knepley   PetscScalar       *real;
362552f7358SJed Brown   PetscErrorCode    ierr;
363552f7358SJed Brown 
364552f7358SJed Brown   PetscFunctionBegin;
36556044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
366552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
367552f7358SJed Brown   {
368552f7358SJed Brown     const PetscScalar p0 = ref[0];
369552f7358SJed Brown     const PetscScalar p1 = ref[1];
370552f7358SJed Brown 
371552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
372552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
373552f7358SJed Brown   }
374552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
37556044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
376552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
377552f7358SJed Brown   PetscFunctionReturn(0);
378552f7358SJed Brown }
379552f7358SJed Brown 
380af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
381552f7358SJed Brown #undef __FUNCT__
382552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private"
383d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
384552f7358SJed Brown {
385552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
386552f7358SJed Brown   const PetscScalar x0        = vertices[0];
387552f7358SJed Brown   const PetscScalar y0        = vertices[1];
388552f7358SJed Brown   const PetscScalar x1        = vertices[2];
389552f7358SJed Brown   const PetscScalar y1        = vertices[3];
390552f7358SJed Brown   const PetscScalar x2        = vertices[4];
391552f7358SJed Brown   const PetscScalar y2        = vertices[5];
392552f7358SJed Brown   const PetscScalar x3        = vertices[6];
393552f7358SJed Brown   const PetscScalar y3        = vertices[7];
394552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
395552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
39656044e6dSMatthew G. Knepley   const PetscScalar *ref;
397552f7358SJed Brown   PetscErrorCode    ierr;
398552f7358SJed Brown 
399552f7358SJed Brown   PetscFunctionBegin;
40056044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
401552f7358SJed Brown   {
402552f7358SJed Brown     const PetscScalar x       = ref[0];
403552f7358SJed Brown     const PetscScalar y       = ref[1];
404552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
405da80777bSKarl Rupp     PetscScalar       values[4];
406da80777bSKarl Rupp 
407da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
408da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
40994ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
410552f7358SJed Brown   }
411552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
41256044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
41394ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
41494ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
415552f7358SJed Brown   PetscFunctionReturn(0);
416552f7358SJed Brown }
417552f7358SJed Brown 
418552f7358SJed Brown #undef __FUNCT__
419552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private"
420a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
421a6dfd86eSKarl Rupp {
422fafc0619SMatthew G Knepley   DM             dmCoord;
423552f7358SJed Brown   SNES           snes;
424552f7358SJed Brown   KSP            ksp;
425552f7358SJed Brown   PC             pc;
426552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
427552f7358SJed Brown   Mat            J;
42856044e6dSMatthew G. Knepley   const PetscScalar *coords;
42956044e6dSMatthew G. Knepley   PetscScalar    *a;
430552f7358SJed Brown   PetscInt       p;
431552f7358SJed Brown   PetscErrorCode ierr;
432552f7358SJed Brown 
433552f7358SJed Brown   PetscFunctionBegin;
434552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
435fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
436552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
437552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
438552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
439552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
440c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
441552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
442552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
443552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
444552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
445552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
446552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4470298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4480298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
449552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
450552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
451552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
452552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
453552f7358SJed Brown 
45456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
455552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
456552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
457a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
458552f7358SJed Brown     PetscScalar *xi;
459cb313848SJed Brown     PetscReal    xir[2];
460552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
461552f7358SJed Brown 
462552f7358SJed Brown     /* Can make this do all points at once */
4630298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4640adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4650298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4660adebc6cSBarry Smith     if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof);
4670298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4680298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
469552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
470552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
471552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
472552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
473552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
474552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
475cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
476cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4771aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1];
4781aa26658SKarl Rupp 
479552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4800298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4810298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
482552f7358SJed Brown   }
483552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
48456044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
485552f7358SJed Brown 
486552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
487552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
488552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
489552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
490552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
491552f7358SJed Brown   PetscFunctionReturn(0);
492552f7358SJed Brown }
493552f7358SJed Brown 
494552f7358SJed Brown #undef __FUNCT__
495552f7358SJed Brown #define __FUNCT__ "HexMap_Private"
4965820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
497552f7358SJed Brown {
498552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
499552f7358SJed Brown   const PetscScalar x0        = vertices[0];
500552f7358SJed Brown   const PetscScalar y0        = vertices[1];
501552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5027a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5037a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5047a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
505552f7358SJed Brown   const PetscScalar x2        = vertices[6];
506552f7358SJed Brown   const PetscScalar y2        = vertices[7];
507552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5087a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5097a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5107a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
511552f7358SJed Brown   const PetscScalar x4        = vertices[12];
512552f7358SJed Brown   const PetscScalar y4        = vertices[13];
513552f7358SJed Brown   const PetscScalar z4        = vertices[14];
514552f7358SJed Brown   const PetscScalar x5        = vertices[15];
515552f7358SJed Brown   const PetscScalar y5        = vertices[16];
516552f7358SJed Brown   const PetscScalar z5        = vertices[17];
517552f7358SJed Brown   const PetscScalar x6        = vertices[18];
518552f7358SJed Brown   const PetscScalar y6        = vertices[19];
519552f7358SJed Brown   const PetscScalar z6        = vertices[20];
520552f7358SJed Brown   const PetscScalar x7        = vertices[21];
521552f7358SJed Brown   const PetscScalar y7        = vertices[22];
522552f7358SJed Brown   const PetscScalar z7        = vertices[23];
523552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
524552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
525552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
526552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
527552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
528552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
529552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
530552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
531552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
532552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
533552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
534552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
535552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
536552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
537552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
538552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
539552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
540552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
541552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
542552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
543552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
54456044e6dSMatthew G. Knepley   const PetscScalar *ref;
54556044e6dSMatthew G. Knepley   PetscScalar       *real;
546552f7358SJed Brown   PetscErrorCode    ierr;
547552f7358SJed Brown 
548552f7358SJed Brown   PetscFunctionBegin;
54956044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
550552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
551552f7358SJed Brown   {
552552f7358SJed Brown     const PetscScalar p0 = ref[0];
553552f7358SJed Brown     const PetscScalar p1 = ref[1];
554552f7358SJed Brown     const PetscScalar p2 = ref[2];
555552f7358SJed Brown 
556552f7358SJed Brown     real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2;
557552f7358SJed Brown     real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2;
558552f7358SJed Brown     real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2;
559552f7358SJed Brown   }
560552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
56156044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
562552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
563552f7358SJed Brown   PetscFunctionReturn(0);
564552f7358SJed Brown }
565552f7358SJed Brown 
566552f7358SJed Brown #undef __FUNCT__
567552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private"
568d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
569552f7358SJed Brown {
570552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
571552f7358SJed Brown   const PetscScalar x0        = vertices[0];
572552f7358SJed Brown   const PetscScalar y0        = vertices[1];
573552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5747a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5757a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5767a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
577552f7358SJed Brown   const PetscScalar x2        = vertices[6];
578552f7358SJed Brown   const PetscScalar y2        = vertices[7];
579552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5807a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5817a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5827a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
583552f7358SJed Brown   const PetscScalar x4        = vertices[12];
584552f7358SJed Brown   const PetscScalar y4        = vertices[13];
585552f7358SJed Brown   const PetscScalar z4        = vertices[14];
586552f7358SJed Brown   const PetscScalar x5        = vertices[15];
587552f7358SJed Brown   const PetscScalar y5        = vertices[16];
588552f7358SJed Brown   const PetscScalar z5        = vertices[17];
589552f7358SJed Brown   const PetscScalar x6        = vertices[18];
590552f7358SJed Brown   const PetscScalar y6        = vertices[19];
591552f7358SJed Brown   const PetscScalar z6        = vertices[20];
592552f7358SJed Brown   const PetscScalar x7        = vertices[21];
593552f7358SJed Brown   const PetscScalar y7        = vertices[22];
594552f7358SJed Brown   const PetscScalar z7        = vertices[23];
595552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
596552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
597552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
598552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
599552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
600552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
601552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
602552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
603552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
604552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
605552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
606552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
60756044e6dSMatthew G. Knepley   const PetscScalar *ref;
608552f7358SJed Brown   PetscErrorCode    ierr;
609552f7358SJed Brown 
610552f7358SJed Brown   PetscFunctionBegin;
61156044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
612552f7358SJed Brown   {
613552f7358SJed Brown     const PetscScalar x       = ref[0];
614552f7358SJed Brown     const PetscScalar y       = ref[1];
615552f7358SJed Brown     const PetscScalar z       = ref[2];
616552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
617da80777bSKarl Rupp     PetscScalar       values[9];
618da80777bSKarl Rupp 
619da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
620da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
621da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
622da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
623da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
624da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
625da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
626da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
627da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
6281aa26658SKarl Rupp 
62994ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
630552f7358SJed Brown   }
631552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
63256044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
63394ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
63494ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
635552f7358SJed Brown   PetscFunctionReturn(0);
636552f7358SJed Brown }
637552f7358SJed Brown 
638552f7358SJed Brown #undef __FUNCT__
639552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private"
640a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
641a6dfd86eSKarl Rupp {
642fafc0619SMatthew G Knepley   DM             dmCoord;
643552f7358SJed Brown   SNES           snes;
644552f7358SJed Brown   KSP            ksp;
645552f7358SJed Brown   PC             pc;
646552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
647552f7358SJed Brown   Mat            J;
64856044e6dSMatthew G. Knepley   const PetscScalar *coords;
64956044e6dSMatthew G. Knepley   PetscScalar    *a;
650552f7358SJed Brown   PetscInt       p;
651552f7358SJed Brown   PetscErrorCode ierr;
652552f7358SJed Brown 
653552f7358SJed Brown   PetscFunctionBegin;
654552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
655fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
656552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
657552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
658552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
659552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
660c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
661552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
662552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
663552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
664552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
665552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
666552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6670298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6680298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
669552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
670552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
671552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
672552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
673552f7358SJed Brown 
67456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
675552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
676552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
677a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
678552f7358SJed Brown     PetscScalar *xi;
679cb313848SJed Brown     PetscReal    xir[3];
680552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
681552f7358SJed Brown 
682552f7358SJed Brown     /* Can make this do all points at once */
6830298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6840adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6850298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6860adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6870298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6880298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
689552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
690552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
691552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
692552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
693552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
694552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
695552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
696cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
697cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
698cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
699552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
700552f7358SJed Brown       a[p*ctx->dof+comp] =
701cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
7027a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
703cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
7047a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
705cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
706cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
707cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
708cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
709552f7358SJed Brown     }
710552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
7110298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
7120298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
713552f7358SJed Brown   }
714552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
71556044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
716552f7358SJed Brown 
717552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
718552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
719552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
720552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
721552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
722552f7358SJed Brown   PetscFunctionReturn(0);
723552f7358SJed Brown }
724552f7358SJed Brown 
725552f7358SJed Brown #undef __FUNCT__
726552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate"
727552f7358SJed Brown /*
728552f7358SJed Brown   Input Parameters:
729552f7358SJed Brown + ctx - The DMInterpolationInfo context
730552f7358SJed Brown . dm  - The DM
731552f7358SJed Brown - x   - The local vector containing the field to be interpolated
732552f7358SJed Brown 
733552f7358SJed Brown   Output Parameters:
734552f7358SJed Brown . v   - The vector containing the interpolated values
735552f7358SJed Brown */
7360adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
7370adebc6cSBarry Smith {
738552f7358SJed Brown   PetscInt       dim, coneSize, n;
739552f7358SJed Brown   PetscErrorCode ierr;
740552f7358SJed Brown 
741552f7358SJed Brown   PetscFunctionBegin;
742552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
743552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
744552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
745552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7460adebc6cSBarry Smith   if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %d should be %d", n, ctx->n*ctx->dof);
747552f7358SJed Brown   if (n) {
748c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
749552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
750552f7358SJed Brown     if (dim == 2) {
751552f7358SJed Brown       if (coneSize == 3) {
7527a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
753552f7358SJed Brown       } else if (coneSize == 4) {
754552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7550adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
756552f7358SJed Brown     } else if (dim == 3) {
757552f7358SJed Brown       if (coneSize == 4) {
7587a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
759552f7358SJed Brown       } else {
760552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
761552f7358SJed Brown       }
7620adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
763552f7358SJed Brown   }
764552f7358SJed Brown   PetscFunctionReturn(0);
765552f7358SJed Brown }
766552f7358SJed Brown 
767552f7358SJed Brown #undef __FUNCT__
768552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy"
7690adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7700adebc6cSBarry Smith {
771552f7358SJed Brown   PetscErrorCode ierr;
772552f7358SJed Brown 
773552f7358SJed Brown   PetscFunctionBegin;
774552f7358SJed Brown   PetscValidPointer(ctx, 2);
775552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
776552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
777552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
778552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7790298fd71SBarry Smith   *ctx = NULL;
780552f7358SJed Brown   PetscFunctionReturn(0);
781552f7358SJed Brown }
782cc0c4584SMatthew G. Knepley 
783cc0c4584SMatthew G. Knepley #undef __FUNCT__
784cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields"
785cc0c4584SMatthew G. Knepley /*@C
786cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
787cc0c4584SMatthew G. Knepley 
788cc0c4584SMatthew G. Knepley   Collective on SNES
789cc0c4584SMatthew G. Knepley 
790cc0c4584SMatthew G. Knepley   Input Parameters:
791cc0c4584SMatthew G. Knepley + snes   - the SNES context
792cc0c4584SMatthew G. Knepley . its    - iteration number
793cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
794d43b4f6eSBarry Smith - vf  - PetscViewerAndFormat of type ASCII
795cc0c4584SMatthew G. Knepley 
796cc0c4584SMatthew G. Knepley   Notes:
797cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
798cc0c4584SMatthew G. Knepley 
799cc0c4584SMatthew G. Knepley   Level: intermediate
800cc0c4584SMatthew G. Knepley 
801cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
802cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
803cc0c4584SMatthew G. Knepley @*/
804d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
805cc0c4584SMatthew G. Knepley {
806d43b4f6eSBarry Smith   PetscViewer        viewer = vf->viewer;
807cc0c4584SMatthew G. Knepley   Vec                res;
808cc0c4584SMatthew G. Knepley   DM                 dm;
809cc0c4584SMatthew G. Knepley   PetscSection       s;
810cc0c4584SMatthew G. Knepley   const PetscScalar *r;
811cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
812cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
813cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
814cc0c4584SMatthew G. Knepley 
815cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
8164d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
817cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
818cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
819cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
820cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
821cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
822cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
823cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
824cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
825cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
826cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
827cc0c4584SMatthew G. Knepley 
828cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
829cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
830cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
831cc0c4584SMatthew G. Knepley     }
832cc0c4584SMatthew G. Knepley   }
833cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
834b2566f29SBarry Smith   ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
835d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
836cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
837cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
838cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
839cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
840cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
841cc0c4584SMatthew G. Knepley   }
842cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
843cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
844d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
845cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
846cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
847cc0c4584SMatthew G. Knepley }
84824cdb843SMatthew G. Knepley 
84924cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
85024cdb843SMatthew G. Knepley 
85124cdb843SMatthew G. Knepley #undef __FUNCT__
8527d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM"
8537d4028c8SMatthew G. Knepley /*@
8547d4028c8SMatthew G. Knepley   DMPlexSNESGetGeometryFEM - Return precomputed geometric data
8557d4028c8SMatthew G. Knepley 
8567d4028c8SMatthew G. Knepley   Input Parameter:
8577d4028c8SMatthew G. Knepley . dm - The DM
8587d4028c8SMatthew G. Knepley 
8597d4028c8SMatthew G. Knepley   Output Parameters:
8607d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
8617d4028c8SMatthew G. Knepley 
8627d4028c8SMatthew G. Knepley   Level: developer
8637d4028c8SMatthew G. Knepley 
8647d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal()
8657d4028c8SMatthew G. Knepley @*/
8667d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom)
8677d4028c8SMatthew G. Knepley {
8687d4028c8SMatthew G. Knepley   DMSNES         dmsnes;
8697d4028c8SMatthew G. Knepley   PetscObject    obj;
8707d4028c8SMatthew G. Knepley   PetscErrorCode ierr;
8717d4028c8SMatthew G. Knepley 
8727d4028c8SMatthew G. Knepley   PetscFunctionBegin;
8737d4028c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8747d4028c8SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
8757d4028c8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr);
8767d4028c8SMatthew G. Knepley   if (!obj) {
8777d4028c8SMatthew G. Knepley     Vec cellgeom;
8787d4028c8SMatthew G. Knepley 
8797d4028c8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
8807d4028c8SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr);
8817d4028c8SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
8827d4028c8SMatthew G. Knepley   }
8837d4028c8SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);}
8847d4028c8SMatthew G. Knepley   PetscFunctionReturn(0);
8857d4028c8SMatthew G. Knepley }
8867d4028c8SMatthew G. Knepley 
8877d4028c8SMatthew G. Knepley #undef __FUNCT__
88808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM"
88908449791SMatthew G. Knepley /*@
89008449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
89108449791SMatthew G. Knepley 
89208449791SMatthew G. Knepley   Input Parameter:
89308449791SMatthew G. Knepley . dm - The DM
89408449791SMatthew G. Knepley 
89508449791SMatthew G. Knepley   Output Parameters:
89608449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
89708449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
89808449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
89908449791SMatthew G. Knepley 
90008449791SMatthew G. Knepley   Level: developer
90108449791SMatthew G. Knepley 
90208449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
90308449791SMatthew G. Knepley @*/
90408449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
90524cdb843SMatthew G. Knepley {
90608449791SMatthew G. Knepley   DMSNES         dmsnes;
90708449791SMatthew G. Knepley   PetscObject    obj;
90824cdb843SMatthew G. Knepley   PetscErrorCode ierr;
90924cdb843SMatthew G. Knepley 
91024cdb843SMatthew G. Knepley   PetscFunctionBegin;
91108449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91208449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
91308449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr);
91408449791SMatthew G. Knepley   if (!obj) {
91508449791SMatthew G. Knepley     Vec cellgeom, facegeom;
91624cdb843SMatthew G. Knepley 
91708449791SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr);
91808449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr);
91908449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr);
92008449791SMatthew G. Knepley     ierr = VecDestroy(&facegeom);CHKERRQ(ierr);
92108449791SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
92208449791SMatthew G. Knepley   }
92308449791SMatthew G. Knepley   if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);}
92408449791SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);}
92508449791SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);}
92624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
92724cdb843SMatthew G. Knepley }
92824cdb843SMatthew G. Knepley 
92924cdb843SMatthew G. Knepley #undef __FUNCT__
93008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM"
931dbd489d2SMatthew G. Knepley /*@
93208449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
93308449791SMatthew G. Knepley 
93408449791SMatthew G. Knepley   Input Parameters:
93508449791SMatthew G. Knepley + dm - The DM
93608449791SMatthew G. Knepley - fv - The PetscFV
93708449791SMatthew G. Knepley 
93808449791SMatthew G. Knepley   Output Parameter:
93908449791SMatthew G. Knepley . dmGrad - The layout for gradient values
94008449791SMatthew G. Knepley 
94108449791SMatthew G. Knepley   Level: developer
94208449791SMatthew G. Knepley 
94308449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
94408449791SMatthew G. Knepley @*/
94508449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
94624cdb843SMatthew G. Knepley {
94708449791SMatthew G. Knepley   DMSNES         dmsnes;
94808449791SMatthew G. Knepley   PetscObject    obj;
94908449791SMatthew G. Knepley   PetscBool      computeGradients;
95024cdb843SMatthew G. Knepley   PetscErrorCode ierr;
95124cdb843SMatthew G. Knepley 
95224cdb843SMatthew G. Knepley   PetscFunctionBegin;
95308449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95408449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
95508449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
95608449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
95708449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
95808449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
95908449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr);
96008449791SMatthew G. Knepley   if (!obj) {
96108449791SMatthew G. Knepley     DM  dmGrad;
96208449791SMatthew G. Knepley     Vec faceGeometry, cellGeometry;
96308449791SMatthew G. Knepley 
96408449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr);
96508449791SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr);
96608449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr);
96708449791SMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
96808449791SMatthew G. Knepley   }
96908449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr);
97008449791SMatthew G. Knepley   PetscFunctionReturn(0);
97108449791SMatthew G. Knepley }
97208449791SMatthew G. Knepley 
97308449791SMatthew G. Knepley #undef __FUNCT__
97408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields"
97508449791SMatthew G. Knepley /*@C
97608449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
97708449791SMatthew G. Knepley 
97808449791SMatthew G. Knepley   Input Parameters:
97908449791SMatthew G. Knepley + dm     - The DM
98008449791SMatthew G. Knepley . cStart - The first cell to include
98108449791SMatthew G. Knepley . cEnd   - The first cell to exclude
98208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
98308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
98408449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
98508449791SMatthew G. Knepley 
98608449791SMatthew G. Knepley   Output Parameters:
98708449791SMatthew G. Knepley + u   - The field coefficients
98808449791SMatthew G. Knepley . u_t - The fields derivative coefficients
98908449791SMatthew G. Knepley - a   - The auxiliary field coefficients
99008449791SMatthew G. Knepley 
99108449791SMatthew G. Knepley   Level: developer
99208449791SMatthew G. Knepley 
99308449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
99408449791SMatthew G. Knepley @*/
99508449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
99608449791SMatthew G. Knepley {
99708449791SMatthew G. Knepley   DM             dmAux;
99808449791SMatthew G. Knepley   PetscSection   section, sectionAux;
99908449791SMatthew G. Knepley   PetscDS        prob;
100008449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
100108449791SMatthew G. Knepley   PetscErrorCode ierr;
100208449791SMatthew G. Knepley 
100308449791SMatthew G. Knepley   PetscFunctionBegin;
100408449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
100508449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
100608449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
100708449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
100808449791SMatthew G. Knepley   PetscValidPointer(u, 7);
100908449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
101008449791SMatthew G. Knepley   PetscValidPointer(a, 9);
101124cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
101224cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
101324cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
101408449791SMatthew G. Knepley   if (locA) {
101508449791SMatthew G. Knepley     PetscDS probAux;
101608449791SMatthew G. Knepley 
101708449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
101824cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
101924cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
102024cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
102124cdb843SMatthew G. Knepley   }
102208449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr);
102349073227SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
102449073227SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
102524cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
102608449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
102724cdb843SMatthew G. Knepley     PetscInt     i;
102824cdb843SMatthew G. Knepley 
102908449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
10309f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i];
103108449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
103208449791SMatthew G. Knepley     if (locX_t) {
103308449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
10349f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i];
103508449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
103624cdb843SMatthew G. Knepley     }
103708449791SMatthew G. Knepley     if (locA) {
10386da023fcSToby Isaac       DM dmAuxPlex;
10396da023fcSToby Isaac 
10406da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
10416da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
10429f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i];
10436da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
10446da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
104524cdb843SMatthew G. Knepley     }
104624cdb843SMatthew G. Knepley   }
104708449791SMatthew G. Knepley   PetscFunctionReturn(0);
104808449791SMatthew G. Knepley }
104924cdb843SMatthew G. Knepley 
105008449791SMatthew G. Knepley #undef __FUNCT__
105108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields"
105208449791SMatthew G. Knepley /*@C
105308449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
105408449791SMatthew G. Knepley 
105508449791SMatthew G. Knepley   Input Parameters:
105608449791SMatthew G. Knepley + dm     - The DM
105708449791SMatthew G. Knepley . cStart - The first cell to include
105808449791SMatthew G. Knepley . cEnd   - The first cell to exclude
105908449791SMatthew G. Knepley . locX   - A local vector with the solution fields
106008449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
106108449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
106208449791SMatthew G. Knepley 
106308449791SMatthew G. Knepley   Output Parameters:
106408449791SMatthew G. Knepley + u   - The field coefficients
106508449791SMatthew G. Knepley . u_t - The fields derivative coefficients
106608449791SMatthew G. Knepley - a   - The auxiliary field coefficients
106708449791SMatthew G. Knepley 
106808449791SMatthew G. Knepley   Level: developer
106908449791SMatthew G. Knepley 
107008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
107108449791SMatthew G. Knepley @*/
107208449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
107308449791SMatthew G. Knepley {
107408449791SMatthew G. Knepley   PetscErrorCode ierr;
107508449791SMatthew G. Knepley 
107608449791SMatthew G. Knepley   PetscFunctionBegin;
107708449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr);
107849073227SMatthew G. Knepley   if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);}
107949073227SMatthew G. Knepley   if (*a)   {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);}
108008449791SMatthew G. Knepley   PetscFunctionReturn(0);
108124cdb843SMatthew G. Knepley }
108208449791SMatthew G. Knepley 
108308449791SMatthew G. Knepley #undef __FUNCT__
108408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields"
108508449791SMatthew G. Knepley /*@C
108608449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
108708449791SMatthew G. Knepley 
108808449791SMatthew G. Knepley   Input Parameters:
108908449791SMatthew G. Knepley + dm     - The DM
109008449791SMatthew G. Knepley . fStart - The first face to include
109108449791SMatthew G. Knepley . fEnd   - The first face to exclude
109208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
109308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
109408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
109508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
109608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
109708449791SMatthew G. Knepley 
109808449791SMatthew G. Knepley   Output Parameters:
1099477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face
1100477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
110108449791SMatthew G. Knepley 
110208449791SMatthew G. Knepley   Level: developer
110308449791SMatthew G. Knepley 
110408449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
110508449791SMatthew G. Knepley @*/
110608449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR)
110708449791SMatthew G. Knepley {
110808449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
1109195142f5SMatthew G. Knepley   PetscSection       section;
111008449791SMatthew G. Knepley   PetscDS            prob;
111108449791SMatthew G. Knepley   DMLabel            ghostLabel;
111208449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1113477f7dfdSMatthew G. Knepley   PetscBool         *isFE;
1114477f7dfdSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
111508449791SMatthew G. Knepley   PetscErrorCode     ierr;
111608449791SMatthew G. Knepley 
111708449791SMatthew G. Knepley   PetscFunctionBegin;
111808449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
111908449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
112008449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
112108449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
112208449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
112308449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
112408449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
112508449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
112608449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
112708449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1128195142f5SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1129477f7dfdSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1130477f7dfdSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
1131477f7dfdSMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
1132477f7dfdSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1133477f7dfdSMatthew G. Knepley     PetscObject  obj;
1134477f7dfdSMatthew G. Knepley     PetscClassId id;
1135477f7dfdSMatthew G. Knepley 
1136477f7dfdSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
1137477f7dfdSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1138477f7dfdSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
1139477f7dfdSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
1140477f7dfdSMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
1141477f7dfdSMatthew G. Knepley   }
1142c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
114308449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
114408449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
114508449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
114608449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
114708449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
114808449791SMatthew G. Knepley   if (locGrad) {
114908449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
115008449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
115124cdb843SMatthew G. Knepley   }
1152477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr);
1153477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr);
1154477f7dfdSMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
115508449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
115608449791SMatthew G. Knepley     const PetscInt        *cells;
1157640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1158640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
1159640bce14SSatish Balay     PetscScalar           *xL, *xR, *gL, *gR;
116008449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
1161e697831aSToby Isaac     PetscInt               ghost, nsupp;
116208449791SMatthew G. Knepley 
116308449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1164e697831aSToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
116508449791SMatthew G. Knepley     if (ghost >= 0) continue;
116608449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
116708449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
116808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
116908449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1170477f7dfdSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1171477f7dfdSMatthew G. Knepley       PetscInt off;
1172477f7dfdSMatthew G. Knepley 
117337a43ebbSMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1174477f7dfdSMatthew G. Knepley       if (isFE[f]) {
1175477f7dfdSMatthew G. Knepley         const PetscInt *cone;
1176cca9989dSMatthew G. Knepley         PetscInt        comp, coneSize, faceLocL, faceLocR, ldof, rdof, d;
1177477f7dfdSMatthew G. Knepley 
1178cca9989dSMatthew G. Knepley         xL = xR = NULL;
1179cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1180cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1181477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
1182477f7dfdSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr);
1183477f7dfdSMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break;
1184477f7dfdSMatthew G. Knepley         if (faceLocL == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[0]);
1185477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
1186477f7dfdSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr);
1187477f7dfdSMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break;
1188477f7dfdSMatthew G. Knepley         if (faceLocR == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d", face, cells[1]);
1189195142f5SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
1190477f7dfdSMatthew G. Knepley         ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
1191cca9989dSMatthew G. Knepley         if (rdof == ldof) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
1192cca9989dSMatthew G. Knepley         else              {ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr); for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
1193cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1194cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1195477f7dfdSMatthew G. Knepley       } else {
1196477f7dfdSMatthew G. Knepley         PetscFV  fv;
1197477f7dfdSMatthew G. Knepley         PetscInt numComp, c;
1198477f7dfdSMatthew G. Knepley 
1199477f7dfdSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
1200477f7dfdSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1201e697831aSToby Isaac         if (nsupp > 2) {
1202e697831aSToby Isaac           for (f = 0; f < Nf; ++f) {
1203e697831aSToby Isaac             PetscInt off;
1204e697831aSToby Isaac 
1205e697831aSToby Isaac             ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1206e697831aSToby Isaac             ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1207e697831aSToby Isaac             for (c = 0; c < numComp; ++c) {
1208e697831aSToby Isaac               uLl[iface*Nc+off+c] = 0.;
1209e697831aSToby Isaac               uRl[iface*Nc+off+c] = 0.;
1210e697831aSToby Isaac             }
1211e697831aSToby Isaac           }
1212e697831aSToby Isaac           continue;
1213e697831aSToby Isaac         }
1214cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
1215cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
121608449791SMatthew G. Knepley         if (dmGrad) {
121708449791SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
121808449791SMatthew G. Knepley 
121908449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
122008449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
122108449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
122208449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1223477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1224477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
1225477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
122608449791SMatthew G. Knepley           }
122708449791SMatthew G. Knepley         } else {
1228477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1229477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
1230477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
1231477f7dfdSMatthew G. Knepley           }
1232477f7dfdSMatthew G. Knepley         }
123308449791SMatthew G. Knepley       }
123408449791SMatthew G. Knepley     }
123508449791SMatthew G. Knepley     ++iface;
123608449791SMatthew G. Knepley   }
123708449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
123808449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
123908449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
124008449791SMatthew G. Knepley   if (locGrad) {
124108449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
124208449791SMatthew G. Knepley   }
1243477f7dfdSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
124408449791SMatthew G. Knepley   PetscFunctionReturn(0);
124508449791SMatthew G. Knepley }
124608449791SMatthew G. Knepley 
124708449791SMatthew G. Knepley #undef __FUNCT__
124808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields"
124908449791SMatthew G. Knepley /*@C
125008449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
125108449791SMatthew G. Knepley 
125208449791SMatthew G. Knepley   Input Parameters:
125308449791SMatthew G. Knepley + dm     - The DM
125408449791SMatthew G. Knepley . fStart - The first face to include
125508449791SMatthew G. Knepley . fEnd   - The first face to exclude
125608449791SMatthew G. Knepley . locX   - A local vector with the solution fields
125708449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
125808449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
125908449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
126008449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
126108449791SMatthew G. Knepley 
126208449791SMatthew G. Knepley   Output Parameters:
1263477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face
1264477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
126508449791SMatthew G. Knepley 
126608449791SMatthew G. Knepley   Level: developer
126708449791SMatthew G. Knepley 
126808449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
126908449791SMatthew G. Knepley @*/
127008449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscScalar **uL, PetscScalar **uR)
127108449791SMatthew G. Knepley {
127208449791SMatthew G. Knepley   PetscErrorCode ierr;
127308449791SMatthew G. Knepley 
127408449791SMatthew G. Knepley   PetscFunctionBegin;
127508449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr);
127608449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr);
127708449791SMatthew G. Knepley   PetscFunctionReturn(0);
127808449791SMatthew G. Knepley }
127908449791SMatthew G. Knepley 
128008449791SMatthew G. Knepley #undef __FUNCT__
128108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry"
128208449791SMatthew G. Knepley /*@C
128308449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
128408449791SMatthew G. Knepley 
128508449791SMatthew G. Knepley   Input Parameters:
128608449791SMatthew G. Knepley + dm     - The DM
128708449791SMatthew G. Knepley . fStart - The first face to include
128808449791SMatthew G. Knepley . fEnd   - The first face to exclude
128908449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
129008449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
129108449791SMatthew G. Knepley 
129208449791SMatthew G. Knepley   Output Parameters:
129308449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
129408449791SMatthew G. Knepley - vol   - The cell volume
129508449791SMatthew G. Knepley 
129608449791SMatthew G. Knepley   Level: developer
129708449791SMatthew G. Knepley 
129808449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
129908449791SMatthew G. Knepley @*/
130008449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
130108449791SMatthew G. Knepley {
130208449791SMatthew G. Knepley   DM                 dmFace, dmCell;
130308449791SMatthew G. Knepley   DMLabel            ghostLabel;
130408449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
130508449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
130608449791SMatthew G. Knepley   PetscErrorCode     ierr;
130708449791SMatthew G. Knepley 
130808449791SMatthew G. Knepley   PetscFunctionBegin;
130908449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
131008449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
131108449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
131208449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
131308449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
131408449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1315c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
131608449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
131708449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
131808449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
131908449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
132008449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
132108449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr);
132208449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
132308449791SMatthew G. Knepley     const PetscInt        *cells;
1324640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1325640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
132608449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
13272eefff9cSMatthew G. Knepley     PetscReal             *voll   = *vol;
132808449791SMatthew G. Knepley     PetscInt               ghost, d;
132908449791SMatthew G. Knepley 
133008449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
133108449791SMatthew G. Knepley     if (ghost >= 0) continue;
133208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
133308449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
133408449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
133508449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
133608449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
133708449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
133808449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
133908449791SMatthew G. Knepley     }
134008449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
134108449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
134208449791SMatthew G. Knepley     ++iface;
134308449791SMatthew G. Knepley   }
134408449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
134508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
134608449791SMatthew G. Knepley   PetscFunctionReturn(0);
134708449791SMatthew G. Knepley }
134808449791SMatthew G. Knepley 
134908449791SMatthew G. Knepley #undef __FUNCT__
135008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry"
135108449791SMatthew G. Knepley /*@C
135208449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
135308449791SMatthew G. Knepley 
135408449791SMatthew G. Knepley   Input Parameters:
135508449791SMatthew G. Knepley + dm     - The DM
135608449791SMatthew G. Knepley . fStart - The first face to include
135708449791SMatthew G. Knepley . fEnd   - The first face to exclude
135808449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
135908449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
136008449791SMatthew G. Knepley 
136108449791SMatthew G. Knepley   Output Parameters:
136208449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
136308449791SMatthew G. Knepley - vol   - The cell volume
136408449791SMatthew G. Knepley 
136508449791SMatthew G. Knepley   Level: developer
136608449791SMatthew G. Knepley 
136708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
136808449791SMatthew G. Knepley @*/
136908449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
137008449791SMatthew G. Knepley {
137108449791SMatthew G. Knepley   PetscErrorCode ierr;
137208449791SMatthew G. Knepley 
137308449791SMatthew G. Knepley   PetscFunctionBegin;
137408449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
137508449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr);
137608449791SMatthew G. Knepley   PetscFunctionReturn(0);
137708449791SMatthew G. Knepley }
137808449791SMatthew G. Knepley 
137908449791SMatthew G. Knepley #undef __FUNCT__
1380cd0a786dSToby Isaac #define __FUNCT__ "DMPlexApplyLimiter_Internal"
1381655c4201SToby Isaac static PetscErrorCode DMPlexApplyLimiter_Internal (DM dm, DM dmCell, PetscLimiter lim, PetscInt dim, PetscInt totDim, PetscInt cell, PetscInt face, PetscInt fStart, PetscInt fEnd, PetscReal *cellPhi, const PetscScalar *x,
1382cd0a786dSToby Isaac                                                    const PetscScalar *cellgeom, const PetscFVCellGeom *cg, const PetscScalar *cx, const PetscScalar *cgrad)
1383cd0a786dSToby Isaac {
1384cd0a786dSToby Isaac   const PetscInt        *children;
1385cd0a786dSToby Isaac   PetscInt               numChildren;
1386cd0a786dSToby Isaac   PetscErrorCode         ierr;
1387cd0a786dSToby Isaac 
1388cd0a786dSToby Isaac   PetscFunctionBegin;
1389cd0a786dSToby Isaac   ierr = DMPlexGetTreeChildren(dm,face,&numChildren,&children);CHKERRQ(ierr);
1390cd0a786dSToby Isaac   if (numChildren) {
1391cd0a786dSToby Isaac     PetscInt c;
1392cd0a786dSToby Isaac 
1393cd0a786dSToby Isaac     for (c = 0; c < numChildren; c++) {
1394cd0a786dSToby Isaac       PetscInt childFace = children[c];
1395655c4201SToby Isaac 
1396655c4201SToby Isaac       if (childFace >= fStart && childFace < fEnd) {
1397655c4201SToby Isaac         ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,childFace,fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr);
1398655c4201SToby Isaac       }
1399cd0a786dSToby Isaac     }
1400cd0a786dSToby Isaac   }
1401cd0a786dSToby Isaac   else {
1402640bce14SSatish Balay     PetscScalar           *ncx;
1403640bce14SSatish Balay     PetscFVCellGeom       *ncg;
1404cd0a786dSToby Isaac     const PetscInt        *fcells;
1405cd0a786dSToby Isaac     PetscInt               ncell, d;
1406cd0a786dSToby Isaac     PetscReal              v[3];
1407cd0a786dSToby Isaac 
1408cd0a786dSToby Isaac     ierr  = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr);
1409cd0a786dSToby Isaac     ncell = cell == fcells[0] ? fcells[1] : fcells[0];
1410cd0a786dSToby Isaac     ierr  = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr);
1411cd0a786dSToby Isaac     ierr  = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr);
1412cd0a786dSToby Isaac     DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v);
1413cd0a786dSToby Isaac     for (d = 0; d < totDim; ++d) {
1414cd0a786dSToby Isaac       /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */
1415cd0a786dSToby Isaac       PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v);
1416cd0a786dSToby Isaac 
1417cd0a786dSToby Isaac       ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr);
1418cd0a786dSToby Isaac       cellPhi[d] = PetscMin(cellPhi[d], phi);
1419cd0a786dSToby Isaac     }
1420cd0a786dSToby Isaac   }
1421cd0a786dSToby Isaac   PetscFunctionReturn(0);
1422cd0a786dSToby Isaac }
1423cd0a786dSToby Isaac 
1424cd0a786dSToby Isaac #undef __FUNCT__
142508449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal"
142608449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad)
142708449791SMatthew G. Knepley {
142808449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
142908449791SMatthew G. Knepley   DMLabel            ghostLabel;
143008449791SMatthew G. Knepley   PetscDS            prob;
143108449791SMatthew G. Knepley   PetscFV            fvm;
143208449791SMatthew G. Knepley   PetscLimiter       lim;
143308449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x;
143408449791SMatthew G. Knepley   PetscScalar       *gr;
143508449791SMatthew G. Knepley   PetscReal         *cellPhi;
143608449791SMatthew G. Knepley   PetscInt           dim, face, cell, totDim, cStart, cEnd, cEndInterior;
143708449791SMatthew G. Knepley   PetscErrorCode     ierr;
143808449791SMatthew G. Knepley 
143908449791SMatthew G. Knepley   PetscFunctionBegin;
144008449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
144108449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
144208449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1443c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
144408449791SMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr);
144508449791SMatthew G. Knepley   ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr);
144608449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
144708449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
144808449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
144908449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
145008449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
145108449791SMatthew G. Knepley   ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr);
145208449791SMatthew G. Knepley   ierr = VecZeroEntries(grad);CHKERRQ(ierr);
145308449791SMatthew G. Knepley   ierr = VecGetArray(grad, &gr);CHKERRQ(ierr);
145408449791SMatthew G. Knepley   /* Reconstruct gradients */
145508449791SMatthew G. Knepley   for (face = fStart; face < fEnd; ++face) {
145608449791SMatthew G. Knepley     const PetscInt        *cells;
1457640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1458640bce14SSatish Balay     PetscScalar           *cx[2];
145908449791SMatthew G. Knepley     PetscScalar           *cgrad[2];
146008449791SMatthew G. Knepley     PetscBool              boundary;
146150d63984SToby Isaac     PetscInt               ghost, c, pd, d, numChildren, numCells;
146208449791SMatthew G. Knepley 
146308449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1464a6ba4734SToby Isaac     ierr = DMIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
146550d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &numChildren, NULL);CHKERRQ(ierr);
146650d63984SToby Isaac     if (ghost >= 0 || boundary || numChildren) continue;
146750d63984SToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &numCells);CHKERRQ(ierr);
146850d63984SToby Isaac     if (numCells != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "facet %d has %d support points: expected 2",face,numCells);
146908449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
147008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
147108449791SMatthew G. Knepley     for (c = 0; c < 2; ++c) {
147208449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr);
147308449791SMatthew G. Knepley       ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr);
147408449791SMatthew G. Knepley     }
147508449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd) {
147608449791SMatthew G. Knepley       PetscScalar delta = cx[1][pd] - cx[0][pd];
147708449791SMatthew G. Knepley 
147808449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
147908449791SMatthew G. Knepley         if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta;
148008449791SMatthew G. Knepley         if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta;
148108449791SMatthew G. Knepley       }
148208449791SMatthew G. Knepley     }
148308449791SMatthew G. Knepley   }
148408449791SMatthew G. Knepley   /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */
148508449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
148608449791SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
148708449791SMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
148808449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
148908449791SMatthew G. Knepley   for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) {
149008449791SMatthew G. Knepley     const PetscInt        *faces;
1491640bce14SSatish Balay     PetscScalar           *cx;
1492640bce14SSatish Balay     PetscFVCellGeom       *cg;
149308449791SMatthew G. Knepley     PetscScalar           *cgrad;
149408449791SMatthew G. Knepley     PetscInt               coneSize, f, pd, d;
149508449791SMatthew G. Knepley 
149608449791SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
149708449791SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
149808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr);
149908449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr);
150008449791SMatthew G. Knepley     ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr);
150108449791SMatthew G. Knepley     if (!cgrad) continue; /* Unowned overlap cell, we do not compute */
150208449791SMatthew G. Knepley     /* Limiter will be minimum value over all neighbors */
150308449791SMatthew G. Knepley     for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL;
150408449791SMatthew G. Knepley     for (f = 0; f < coneSize; ++f) {
1505655c4201SToby Isaac       ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,faces[f],fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr);
150608449791SMatthew G. Knepley     }
150708449791SMatthew G. Knepley     /* Apply limiter to gradient */
150808449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd)
150908449791SMatthew G. Knepley       /* Scalar limiter applied to each component separately */
151008449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd];
151108449791SMatthew G. Knepley   }
151208449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
151308449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
151408449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
151508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
151608449791SMatthew G. Knepley   ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr);
151708449791SMatthew G. Knepley   PetscFunctionReturn(0);
151808449791SMatthew G. Knepley }
151908449791SMatthew G. Knepley 
152008449791SMatthew G. Knepley #undef __FUNCT__
152108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal"
152208449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user)
152308449791SMatthew G. Knepley {
152408449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
152508449791SMatthew G. Knepley   PetscSection     section;
152608449791SMatthew G. Knepley   PetscDS          prob;
152708449791SMatthew G. Knepley   DMLabel          depth;
152808449791SMatthew G. Knepley   PetscFECellGeom *cgeom;
152908449791SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *elemVec = NULL;
153008449791SMatthew G. Knepley   PetscInt         dim, Nf, f, totDimBd, numBd, bd;
153108449791SMatthew G. Knepley   PetscErrorCode   ierr;
153208449791SMatthew G. Knepley 
153308449791SMatthew G. Knepley   PetscFunctionBegin;
153408449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
153508449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
153608449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
153708449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
153808449791SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
153924cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1540a6ba4734SToby Isaac   ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
154124cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
154224cdb843SMatthew G. Knepley     const char     *bdLabel;
154324cdb843SMatthew G. Knepley     DMLabel         label;
154424cdb843SMatthew G. Knepley     IS              pointIS;
154524cdb843SMatthew G. Knepley     const PetscInt *points;
154624cdb843SMatthew G. Knepley     const PetscInt *values;
1547a8e83e26SSanderA     PetscInt        field, numValues, v, numPoints, p, dep, numFaces;
154824cdb843SMatthew G. Knepley     PetscBool       isEssential;
154980bc4632SMatthew G. Knepley     PetscObject     obj;
155080bc4632SMatthew G. Knepley     PetscClassId    id;
155124cdb843SMatthew G. Knepley 
1552a6ba4734SToby Isaac     ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
155380bc4632SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
155480bc4632SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
155580bc4632SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || isEssential) continue;
1556c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1557a8e83e26SSanderA     for (v = 0; v < numValues; ++v) {
1558a8e83e26SSanderA       ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
1559a8e83e26SSanderA       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
156022734eb1Ssarens       if (!pointIS) continue; /* No points with that id on this process */
156124cdb843SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
156224cdb843SMatthew G. Knepley       for (p = 0, numFaces = 0; p < numPoints; ++p) {
156324cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
156424cdb843SMatthew G. Knepley         if (dep == dim-1) ++numFaces;
156524cdb843SMatthew G. Knepley       }
1566bbce034cSMatthew G. Knepley       ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
156708449791SMatthew G. Knepley       if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
156824cdb843SMatthew G. Knepley       for (p = 0, f = 0; p < numPoints; ++p) {
156924cdb843SMatthew G. Knepley         const PetscInt point = points[p];
157024cdb843SMatthew G. Knepley         PetscScalar   *x     = NULL;
157124cdb843SMatthew G. Knepley         PetscInt       i;
157224cdb843SMatthew G. Knepley 
157324cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
157424cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
1575bbce034cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
1576302440fdSBarry Smith         ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr);
1577bbce034cSMatthew G. Knepley         if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point);
1578929b1830SToby Isaac         /* TODO: Matt, this is wrong if feBd does not match fe: i.e., if the order differs. */
157908449791SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
158024cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
158108449791SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
158208449791SMatthew G. Knepley         if (locX_t) {
158308449791SMatthew G. Knepley           ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
158424cdb843SMatthew G. Knepley           for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
158508449791SMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
158624cdb843SMatthew G. Knepley         }
158724cdb843SMatthew G. Knepley         ++f;
158824cdb843SMatthew G. Knepley       }
158924cdb843SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
159024cdb843SMatthew G. Knepley         PetscFE         fe;
159108449791SMatthew G. Knepley         PetscQuadrature q;
159224cdb843SMatthew G. Knepley         PetscInt        numQuadPoints, Nb;
159324cdb843SMatthew G. Knepley         /* Conforming batches */
159424cdb843SMatthew G. Knepley         PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
159524cdb843SMatthew G. Knepley         /* Remainder */
159624cdb843SMatthew G. Knepley         PetscInt        Nr, offset;
159724cdb843SMatthew G. Knepley 
159824cdb843SMatthew G. Knepley         ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
159924cdb843SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
160024cdb843SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
160124cdb843SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
160224cdb843SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
160324cdb843SMatthew G. Knepley         blockSize = Nb*numQuadPoints;
160424cdb843SMatthew G. Knepley         batchSize = numBlocks * blockSize;
160524cdb843SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
160624cdb843SMatthew G. Knepley         numChunks = numFaces / (numBatches*batchSize);
160724cdb843SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
160824cdb843SMatthew G. Knepley         Nr        = numFaces % (numBatches*batchSize);
160924cdb843SMatthew G. Knepley         offset    = numFaces - Nr;
1610bbce034cSMatthew G. Knepley         ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
1611bbce034cSMatthew G. Knepley         ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr);
161224cdb843SMatthew G. Knepley       }
161324cdb843SMatthew G. Knepley       for (p = 0, f = 0; p < numPoints; ++p) {
161424cdb843SMatthew G. Knepley         const PetscInt point = points[p];
161524cdb843SMatthew G. Knepley 
161624cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
161724cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
161824cdb843SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
1619c14a31d2SToby Isaac         ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_ALL_VALUES);CHKERRQ(ierr);
162024cdb843SMatthew G. Knepley         ++f;
162124cdb843SMatthew G. Knepley       }
162224cdb843SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
162324cdb843SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1624bbce034cSMatthew G. Knepley       ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr);
162508449791SMatthew G. Knepley       if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
162624cdb843SMatthew G. Knepley     }
1627a8e83e26SSanderA   }
162808449791SMatthew G. Knepley   PetscFunctionReturn(0);
162908449791SMatthew G. Knepley }
163008449791SMatthew G. Knepley 
163108449791SMatthew G. Knepley #undef __FUNCT__
1632f2c5ccc7SToby Isaac #define __FUNCT__ "DMPlexReconstructGradientsFVM"
1633f2c5ccc7SToby Isaac /*@
1634f2c5ccc7SToby Isaac   DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method.
1635f2c5ccc7SToby Isaac 
1636f2c5ccc7SToby Isaac   Input Parameters:
1637f2c5ccc7SToby Isaac + dm - the mesh
1638f2c5ccc7SToby Isaac - locX - the local representation of the vector
1639f2c5ccc7SToby Isaac 
1640f2c5ccc7SToby Isaac   Output Parameter:
1641f2c5ccc7SToby Isaac . grad - the global representation of the gradient
1642f2c5ccc7SToby Isaac 
1643f2c5ccc7SToby Isaac   Level: developer
1644f2c5ccc7SToby Isaac 
1645f2c5ccc7SToby Isaac .seealso: DMPlexSNESGetGradientDM()
1646f2c5ccc7SToby Isaac @*/
1647f2c5ccc7SToby Isaac PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad)
1648f2c5ccc7SToby Isaac {
1649f2c5ccc7SToby Isaac   PetscDS          prob;
1650f2c5ccc7SToby Isaac   PetscInt         Nf, f, fStart, fEnd;
16519fc93327SToby Isaac   PetscBool        useFVM = PETSC_FALSE;
1652f2c5ccc7SToby Isaac   PetscFV          fvm = NULL;
1653f2c5ccc7SToby Isaac   Vec              faceGeometryFVM, cellGeometryFVM;
1654f2c5ccc7SToby Isaac   PetscFVCellGeom  *cgeomFVM   = NULL;
1655f2c5ccc7SToby Isaac   PetscFVFaceGeom  *fgeomFVM   = NULL;
1656f2c5ccc7SToby Isaac   DM               dmGrad = NULL;
1657f2c5ccc7SToby Isaac   PetscErrorCode   ierr;
1658f2c5ccc7SToby Isaac 
1659f2c5ccc7SToby Isaac   PetscFunctionBegin;
1660f2c5ccc7SToby Isaac   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1661f2c5ccc7SToby Isaac   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1662f2c5ccc7SToby Isaac   for (f = 0; f < Nf; ++f) {
1663f2c5ccc7SToby Isaac     PetscObject  obj;
1664f2c5ccc7SToby Isaac     PetscClassId id;
1665f2c5ccc7SToby Isaac 
1666f2c5ccc7SToby Isaac     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1667f2c5ccc7SToby Isaac     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1668f2c5ccc7SToby Isaac     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
1669f2c5ccc7SToby Isaac   }
1670f2c5ccc7SToby Isaac   if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization");
1671f2c5ccc7SToby Isaac   ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
1672f2c5ccc7SToby Isaac   if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients");
1673f2c5ccc7SToby Isaac   ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
1674f2c5ccc7SToby Isaac   ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
1675f2c5ccc7SToby Isaac   ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1676f2c5ccc7SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1677f2c5ccc7SToby Isaac   ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1678f2c5ccc7SToby Isaac   PetscFunctionReturn(0);
1679f2c5ccc7SToby Isaac }
1680f2c5ccc7SToby Isaac 
1681f2c5ccc7SToby Isaac #undef __FUNCT__
168208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal"
1683c4d4a4f8SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
168408449791SMatthew G. Knepley {
168508449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
168608449791SMatthew G. Knepley   const char       *name       = "Residual";
168708449791SMatthew G. Knepley   DM                dmAux      = NULL;
168808449791SMatthew G. Knepley   DM                dmGrad     = NULL;
168908449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
169008449791SMatthew G. Knepley   PetscDS           prob       = NULL;
169108449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
169208449791SMatthew G. Knepley   PetscSection      section    = NULL;
169308449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
169408449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
1695b2666ceaSMatthew G. Knepley   PetscBool         isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
169608449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
169708449791SMatthew G. Knepley   PetscFECellGeom  *cgeomFEM   = NULL;
16982f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
169908449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
170008449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
170108449791SMatthew G. Knepley   Vec               locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
170208449791SMatthew G. Knepley   PetscScalar      *u, *u_t, *a, *uL, *uR;
1703c4d4a4f8SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
170408449791SMatthew G. Knepley   PetscErrorCode    ierr;
170508449791SMatthew G. Knepley 
170608449791SMatthew G. Knepley   PetscFunctionBegin;
170708449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
170808449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1709195142f5SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
171008449791SMatthew G. Knepley   /* FEM+FVM */
171108449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
171208449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1713c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
171408449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
171508449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
171608449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
171708449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
171808449791SMatthew G. Knepley   if (locA) {
171908449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
172008449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
172108449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
172208449791SMatthew G. Knepley   }
172308449791SMatthew G. Knepley   /* 2: Get geometric data */
172408449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
172508449791SMatthew G. Knepley     PetscObject  obj;
172608449791SMatthew G. Knepley     PetscClassId id;
17277173168dSMatthew G. Knepley     PetscBool    fimp;
172808449791SMatthew G. Knepley 
17297173168dSMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
17307173168dSMatthew G. Knepley     if (isImplicit != fimp) continue;
173108449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
173208449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
173308449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
173408449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
173508449791SMatthew G. Knepley   }
173608449791SMatthew G. Knepley   if (useFEM) {
173708449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr);
17382f84e9bcSToby Isaac     ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);
17392f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
17402f84e9bcSToby Isaac       DM dmCell;
17412f84e9bcSToby Isaac       PetscInt c;
17422f84e9bcSToby Isaac 
17432f84e9bcSToby Isaac       ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr);
17442f84e9bcSToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr);
17452f84e9bcSToby Isaac       for (c = 0; c < cEnd - cStart; c++) {
17462f84e9bcSToby Isaac         PetscScalar *thisgeom;
17472f84e9bcSToby Isaac 
17482f84e9bcSToby Isaac         ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
17492f84e9bcSToby Isaac         cgeomFEM[c] = *((PetscFECellGeom *) thisgeom);
17502f84e9bcSToby Isaac       }
17512f84e9bcSToby Isaac     }
17522f84e9bcSToby Isaac     else {
17532f84e9bcSToby Isaac       cgeomFEM = (PetscFECellGeom *) cgeomScal;
17542f84e9bcSToby Isaac     }
175508449791SMatthew G. Knepley   }
175608449791SMatthew G. Knepley   if (useFVM) {
175708449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
175808449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
175908449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
176008449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
176108449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
176208449791SMatthew G. Knepley     if (dmGrad) {
176308449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
176408449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
176508449791SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
176608449791SMatthew G. Knepley       /* Communicate gradient values */
176708449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
176808449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
176908449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
177008449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
177108449791SMatthew G. Knepley     }
1772bdd6f66aSToby Isaac     /* Handle non-essential (e.g. outflow) boundary values */
1773bdd6f66aSToby Isaac     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
177408449791SMatthew G. Knepley   }
177508449791SMatthew G. Knepley   /* Loop over chunks */
177608449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
177708449791SMatthew G. Knepley   numChunks     = 1;
177808449791SMatthew G. Knepley   cellChunkSize = (cEnd - cStart)/numChunks;
177908449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
178008449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
17812eefff9cSMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
17822eefff9cSMatthew G. Knepley     PetscReal       *vol;
178308449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
178408449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
178508449791SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face;
178608449791SMatthew G. Knepley 
178708449791SMatthew G. Knepley     /* Extract field coefficients */
178808449791SMatthew G. Knepley     if (useFEM) {
178908449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
179008449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
1791215c4595SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
179208449791SMatthew G. Knepley     }
179308449791SMatthew G. Knepley     if (useFVM) {
179408449791SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
179508449791SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
179608449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
179708449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
1798215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1799215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
180008449791SMatthew G. Knepley     }
180108449791SMatthew G. Knepley     /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */
180208449791SMatthew G. Knepley     /* Loop over fields */
180308449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
180408449791SMatthew G. Knepley       PetscObject  obj;
180508449791SMatthew G. Knepley       PetscClassId id;
18067173168dSMatthew G. Knepley       PetscBool    fimp;
180708449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
180808449791SMatthew G. Knepley 
18097173168dSMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
18107173168dSMatthew G. Knepley       if (isImplicit != fimp) continue;
181108449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
181208449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
181308449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
181408449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
181508449791SMatthew G. Knepley         PetscQuadrature q;
181608449791SMatthew G. Knepley         PetscInt        Nq, Nb;
181708449791SMatthew G. Knepley 
181808449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
181908449791SMatthew G. Knepley 
182008449791SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
182108449791SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
182208449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
182308449791SMatthew G. Knepley         blockSize = Nb*Nq;
182408449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
182508449791SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
182608449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
182708449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
182808449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
182908449791SMatthew G. Knepley         offset    = numCells - Nr;
183008449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
183108449791SMatthew G. Knepley         /*   For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */
183208449791SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
183308449791SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
183408449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
183508449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
183608449791SMatthew G. Knepley 
183708449791SMatthew G. Knepley         Ne = numFaces;
183808449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
183908449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
184008449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
184108449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
184208449791SMatthew G. Knepley     }
18432f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
18442f84e9bcSToby Isaac       ierr = PetscFree(cgeomFEM);CHKERRQ(ierr);
18452f84e9bcSToby Isaac     }
18462f84e9bcSToby Isaac     else {
18472f84e9bcSToby Isaac       cgeomFEM = NULL;
18482f84e9bcSToby Isaac     }
18490163fd50SMatthew G. Knepley     if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);}
185008449791SMatthew G. Knepley     /* Loop over domain */
185108449791SMatthew G. Knepley     if (useFEM) {
185208449791SMatthew G. Knepley       /* Add elemVec to locX */
185308449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
185408449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
1855c14a31d2SToby Isaac         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
185608449791SMatthew G. Knepley       }
185708449791SMatthew G. Knepley     }
185808449791SMatthew G. Knepley     if (useFVM) {
18594a394323SMatthew G. Knepley       PetscScalar *fa;
186008449791SMatthew G. Knepley       PetscInt     iface;
186108449791SMatthew G. Knepley 
186208449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1863c10b5f1bSMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1864c10b5f1bSMatthew G. Knepley         PetscFV      fv;
1865c10b5f1bSMatthew G. Knepley         PetscObject  obj;
1866c10b5f1bSMatthew G. Knepley         PetscClassId id;
18674a394323SMatthew G. Knepley         PetscInt     foff, pdim;
1868c10b5f1bSMatthew G. Knepley 
1869c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1870c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1871c10b5f1bSMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1872c10b5f1bSMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1873c10b5f1bSMatthew G. Knepley         fv   = (PetscFV) obj;
1874c10b5f1bSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1875c10b5f1bSMatthew G. Knepley         /* Accumulate fluxes to cells */
187608449791SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
187708449791SMatthew G. Knepley           const PetscInt *cells;
187808449791SMatthew G. Knepley           PetscScalar    *fL, *fR;
1879ffe9ad51SToby Isaac           PetscInt        ghost, d, nsupp;
188008449791SMatthew G. Knepley 
188108449791SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1882ffe9ad51SToby Isaac           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
18831993971cSToby Isaac           if (ghost >= 0) continue;
18841993971cSToby Isaac           if (nsupp > 2) { /* noop */
18851993971cSToby Isaac             ++iface;
18861993971cSToby Isaac             continue;
18871993971cSToby Isaac           }
188808449791SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1889c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr);
1890c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr);
1891c10b5f1bSMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
1892c10b5f1bSMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1893c10b5f1bSMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
189408449791SMatthew G. Knepley           }
189508449791SMatthew G. Knepley           ++iface;
189608449791SMatthew G. Knepley         }
1897dab51205SMatthew G. Knepley       }
1898dab51205SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1899dab51205SMatthew G. Knepley     }
1900c10b5f1bSMatthew G. Knepley     /* Handle time derivative */
1901c10b5f1bSMatthew G. Knepley     if (locX_t) {
1902dab51205SMatthew G. Knepley       PetscScalar *x_t, *fa;
1903dab51205SMatthew G. Knepley 
1904dab51205SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1905c10b5f1bSMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
1906dab51205SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1907dab51205SMatthew G. Knepley         PetscFV      fv;
1908dab51205SMatthew G. Knepley         PetscObject  obj;
1909dab51205SMatthew G. Knepley         PetscClassId id;
1910dab51205SMatthew G. Knepley         PetscInt     pdim, d;
1911dab51205SMatthew G. Knepley 
1912dab51205SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1913dab51205SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1914dab51205SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1915dab51205SMatthew G. Knepley         fv   = (PetscFV) obj;
1916dab51205SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1917c10b5f1bSMatthew G. Knepley         for (cell = cS; cell < cE; ++cell) {
1918c10b5f1bSMatthew G. Knepley           PetscScalar *u_t, *r;
1919c10b5f1bSMatthew G. Knepley 
1920c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
1921c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
1922d63b37e5SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1923c10b5f1bSMatthew G. Knepley         }
1924dab51205SMatthew G. Knepley       }
1925c10b5f1bSMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
192608449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
192708449791SMatthew G. Knepley     }
192808449791SMatthew G. Knepley     if (useFEM) {
192908449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
193008449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
193108449791SMatthew G. Knepley     }
193208449791SMatthew G. Knepley     if (useFVM) {
193308449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
193408449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
193508449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
193608449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
193708449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
193808449791SMatthew G. Knepley     }
193908449791SMatthew G. Knepley   }
194008449791SMatthew G. Knepley 
194108449791SMatthew G. Knepley   if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);}
194208449791SMatthew G. Knepley 
194308449791SMatthew G. Knepley   /* FEM */
194408449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
194508449791SMatthew G. Knepley   /* 2: Get geometric data */
194608449791SMatthew G. Knepley   /* 3: Handle boundary values */
194708449791SMatthew G. Knepley   /* 4: Loop over domain */
194808449791SMatthew G. Knepley   /*   Extract coefficients */
194908449791SMatthew G. Knepley   /* Loop over fields */
195008449791SMatthew G. Knepley   /*   Set tiling for FE*/
195108449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
195208449791SMatthew G. Knepley   /*     Loop over subdomain */
195308449791SMatthew G. Knepley   /*       Loop over quad points */
195408449791SMatthew G. Knepley   /*         Transform coords to real space */
195508449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
195608449791SMatthew G. Knepley   /*         Evaluate residual at point */
195708449791SMatthew G. Knepley   /*         Transform residual to real space */
195808449791SMatthew G. Knepley   /*       Add residual to elemVec */
195908449791SMatthew G. Knepley   /* Loop over domain */
196008449791SMatthew G. Knepley   /*   Add elemVec to locX */
196108449791SMatthew G. Knepley 
196208449791SMatthew G. Knepley   /* FVM */
196308449791SMatthew G. Knepley   /* Get geometric data */
196408449791SMatthew G. Knepley   /* If using gradients */
196508449791SMatthew G. Knepley   /*   Compute gradient data */
196608449791SMatthew G. Knepley   /*   Loop over domain faces */
196708449791SMatthew G. Knepley   /*     Count computational faces */
196808449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
196908449791SMatthew G. Knepley   /*   Loop over domain cells */
197008449791SMatthew G. Knepley   /*     Limit cell gradients */
197108449791SMatthew G. Knepley   /* Handle boundary values */
197208449791SMatthew G. Knepley   /* Loop over domain faces */
197308449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
197408449791SMatthew G. Knepley   /* Riemann solve over faces */
197508449791SMatthew G. Knepley   /* Loop over domain faces */
197608449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
197708449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
1978247ba720SToby Isaac   if (mesh->printFEM) {
1979247ba720SToby Isaac     Vec         locFbc;
1980247ba720SToby Isaac     PetscInt    pStart, pEnd, p, maxDof;
1981247ba720SToby Isaac     PetscScalar *zeroes;
1982247ba720SToby Isaac 
1983247ba720SToby Isaac     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
1984247ba720SToby Isaac     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
1985247ba720SToby Isaac     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
1986247ba720SToby Isaac     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
1987247ba720SToby Isaac     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
1988247ba720SToby Isaac     for (p = pStart; p < pEnd; p++) {
1989247ba720SToby Isaac       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
1990247ba720SToby Isaac     }
1991247ba720SToby Isaac     ierr = PetscFree(zeroes);CHKERRQ(ierr);
1992247ba720SToby Isaac     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
1993247ba720SToby Isaac     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
1994247ba720SToby Isaac   }
199524cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
199624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
199724cdb843SMatthew G. Knepley }
199824cdb843SMatthew G. Knepley 
199924cdb843SMatthew G. Knepley #undef __FUNCT__
200024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal"
200124cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
200224cdb843SMatthew G. Knepley {
200324cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
2004bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
200524cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
200624cdb843SMatthew G. Knepley   PetscQuadrature   q;
200724cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
20082f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
20092f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
201024cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
201124cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
201224cdb843SMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
201324cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
201424cdb843SMatthew G. Knepley 
201524cdb843SMatthew G. Knepley   PetscFunctionBegin;
201624cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
201724cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
201824cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
201924cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
202024cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
202124cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
202224cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
202324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
202424cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
202524cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
202624cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
202724cdb843SMatthew G. Knepley   if (dmAux) {
202824cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
202924cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
203024cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
203124cdb843SMatthew G. Knepley   }
203224cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
2033bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
203424cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
203524cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2036bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
20372f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
20382f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
20392f84e9bcSToby Isaac     DM dmCell;
20402f84e9bcSToby Isaac 
20412f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
20422f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
20432f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
20447726bd6bSToby Isaac       PetscScalar *thisgeom;
20452f84e9bcSToby Isaac 
20462f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
20472f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
20482f84e9bcSToby Isaac     }
20492f84e9bcSToby Isaac   }
20502f84e9bcSToby Isaac   else {
20512f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
20522f84e9bcSToby Isaac   }
205324cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
205424cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
205524cdb843SMatthew G. Knepley     PetscInt     i;
205624cdb843SMatthew G. Knepley 
205724cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
205824cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
205924cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
206024cdb843SMatthew G. Knepley     if (X_t) {
206124cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
206224cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
206324cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
206424cdb843SMatthew G. Knepley     }
206524cdb843SMatthew G. Knepley     if (dmAux) {
20666da023fcSToby Isaac       DM dmAuxPlex;
20676da023fcSToby Isaac 
20686da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
20696da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
207024cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
20716da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
20726da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
207324cdb843SMatthew G. Knepley     }
207424cdb843SMatthew G. Knepley   }
207524cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
207624cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
207724cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
207824cdb843SMatthew G. Knepley     /* Conforming batches */
207924cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
208024cdb843SMatthew G. Knepley     /* Remainder */
208124cdb843SMatthew G. Knepley     PetscInt Nr, offset;
208224cdb843SMatthew G. Knepley 
208324cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
208424cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
208524cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
208624cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
208724cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
208824cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
208924cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
209024cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
209124cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
209224cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
209324cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
209424cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
209524cdb843SMatthew G. Knepley     offset    = numCells - Nr;
2096bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
2097bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
2098bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
2099bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr);
210024cdb843SMatthew G. Knepley   }
210124cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
210224cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
210324cdb843SMatthew G. Knepley     PetscInt  d;
210424cdb843SMatthew G. Knepley 
210524cdb843SMatthew G. Knepley     for (d = 0; d < totDim; ++d) if (PetscAbsScalar(elemVec[c*totDim+d] - elemVecCh[c*totDim+d]) > 1.0e-7) {diff = PETSC_TRUE;break;}
210624cdb843SMatthew G. Knepley     if (diff) {
210724cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
210824cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
210924cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
211024cdb843SMatthew G. Knepley       ++diffCell;
211124cdb843SMatthew G. Knepley     }
211224cdb843SMatthew G. Knepley     if (diffCell > 9) break;
2113c14a31d2SToby Isaac     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
211424cdb843SMatthew G. Knepley   }
21152f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
21162f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
21172f84e9bcSToby Isaac   }
21182f84e9bcSToby Isaac   else {
21192f84e9bcSToby Isaac     cgeom = NULL;
21202f84e9bcSToby Isaac   }
21212f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2122bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
212324cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
212424cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
212524cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
212624cdb843SMatthew G. Knepley }
212724cdb843SMatthew G. Knepley 
212824cdb843SMatthew G. Knepley #undef __FUNCT__
212924cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
213024cdb843SMatthew G. Knepley /*@
213124cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
213224cdb843SMatthew G. Knepley 
213324cdb843SMatthew G. Knepley   Input Parameters:
213424cdb843SMatthew G. Knepley + dm - The mesh
213524cdb843SMatthew G. Knepley . X  - Local solution
213624cdb843SMatthew G. Knepley - user - The user context
213724cdb843SMatthew G. Knepley 
213824cdb843SMatthew G. Knepley   Output Parameter:
213924cdb843SMatthew G. Knepley . F  - Local output vector
214024cdb843SMatthew G. Knepley 
214124cdb843SMatthew G. Knepley   Level: developer
214224cdb843SMatthew G. Knepley 
214324cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
214424cdb843SMatthew G. Knepley @*/
214524cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
214624cdb843SMatthew G. Knepley {
214724cdb843SMatthew G. Knepley   PetscObject    check;
2148c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
21496da023fcSToby Isaac   DM             plex;
215024cdb843SMatthew G. Knepley   PetscErrorCode ierr;
215124cdb843SMatthew G. Knepley 
215224cdb843SMatthew G. Knepley   PetscFunctionBegin;
21536da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
21546da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
21556da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2156c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
215724cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
21586da023fcSToby Isaac   ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr);
21596da023fcSToby Isaac   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, F, user);CHKERRQ(ierr);}
21606da023fcSToby Isaac   else       {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, F, user);CHKERRQ(ierr);}
21619a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
216224cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
216324cdb843SMatthew G. Knepley }
216424cdb843SMatthew G. Knepley 
216524cdb843SMatthew G. Knepley #undef __FUNCT__
2166bdd6f66aSToby Isaac #define __FUNCT__ "DMPlexSNESComputeBoundaryFEM"
2167bdd6f66aSToby Isaac /*@
2168bdd6f66aSToby Isaac   DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X
2169bdd6f66aSToby Isaac 
2170bdd6f66aSToby Isaac   Input Parameters:
2171bdd6f66aSToby Isaac + dm - The mesh
2172bdd6f66aSToby Isaac - user - The user context
2173bdd6f66aSToby Isaac 
2174bdd6f66aSToby Isaac   Output Parameter:
2175bdd6f66aSToby Isaac . X  - Local solution
2176bdd6f66aSToby Isaac 
2177bdd6f66aSToby Isaac   Level: developer
2178bdd6f66aSToby Isaac 
2179bdd6f66aSToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
2180bdd6f66aSToby Isaac @*/
2181bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user)
2182bdd6f66aSToby Isaac {
2183bdd6f66aSToby Isaac   DM             plex;
2184bdd6f66aSToby Isaac   PetscErrorCode ierr;
2185bdd6f66aSToby Isaac 
2186bdd6f66aSToby Isaac   PetscFunctionBegin;
2187bdd6f66aSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2188bdd6f66aSToby Isaac   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr);
2189bdd6f66aSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2190bdd6f66aSToby Isaac   PetscFunctionReturn(0);
2191bdd6f66aSToby Isaac }
2192bdd6f66aSToby Isaac 
2193bdd6f66aSToby Isaac #undef __FUNCT__
2194b953af5fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobian_Internal"
2195b953af5fSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
219624cdb843SMatthew G. Knepley {
219724cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
219824cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
2199f7ed7b21SMatthew G. Knepley   DM                dmAux, plex;
220024cdb843SMatthew G. Knepley   DMLabel           depth;
2201bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
220224cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
220324cdb843SMatthew G. Knepley   PetscQuadrature   quad;
220424cdb843SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
22052f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
22062f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
2207426ff135SMatthew G. Knepley   PetscScalar      *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
2208b953af5fSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, c;
220924cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
2210426ff135SMatthew G. Knepley   PetscBool         isShell, hasPrec, hasDyn;
221124cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
221224cdb843SMatthew G. Knepley 
221324cdb843SMatthew G. Knepley   PetscFunctionBegin;
221424cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
221524cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
221624cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
221724cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
221824cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
221924cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
222024cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
222155ad3c34SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2222426ff135SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2223426ff135SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
222424cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
222524cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
222624cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
222724cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
222824cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
222924cdb843SMatthew G. Knepley   if (dmAux) {
2230f7ed7b21SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2231f7ed7b21SMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
223224cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
223324cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
223424cdb843SMatthew G. Knepley   }
223524cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
2236426ff135SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr);
223724cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2238bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
22392f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
22402f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
22412f84e9bcSToby Isaac     DM dmCell;
22422f84e9bcSToby Isaac 
22432f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
22442f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
22452f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
22467726bd6bSToby Isaac       PetscScalar *thisgeom;
22472f84e9bcSToby Isaac 
22482f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
22492f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
22502f84e9bcSToby Isaac     }
22512f84e9bcSToby Isaac   }
22522f84e9bcSToby Isaac   else {
22532f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
22542f84e9bcSToby Isaac   }
225524cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
225624cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
225724cdb843SMatthew G. Knepley     PetscInt     i;
225824cdb843SMatthew G. Knepley 
225924cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
22609f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
226124cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
226224cdb843SMatthew G. Knepley     if (X_t) {
226324cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
22649f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
226524cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
226624cdb843SMatthew G. Knepley     }
226724cdb843SMatthew G. Knepley     if (dmAux) {
2268f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
22699f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
2270f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
227124cdb843SMatthew G. Knepley     }
227224cdb843SMatthew G. Knepley   }
227324cdb843SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
227455ad3c34SMatthew G. Knepley   if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2275426ff135SMatthew G. Knepley   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
227624cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
227724cdb843SMatthew G. Knepley     PetscFE  fe;
227824cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
227924cdb843SMatthew G. Knepley     /* Conforming batches */
228024cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
228124cdb843SMatthew G. Knepley     /* Remainder */
228224cdb843SMatthew G. Knepley     PetscInt Nr, offset;
228324cdb843SMatthew G. Knepley 
228424cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
228524cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
228624cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
228724cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
228824cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
228924cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
229024cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
229124cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
229224cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
229324cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
229424cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
229524cdb843SMatthew G. Knepley     offset    = numCells - Nr;
229624cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2297426ff135SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
2298426ff135SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
229955ad3c34SMatthew G. Knepley       if (hasPrec) {
2300426ff135SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMatP);CHKERRQ(ierr);
2301426ff135SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
2302426ff135SMatthew G. Knepley       }
2303426ff135SMatthew G. Knepley       if (hasDyn) {
2304426ff135SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMatD);CHKERRQ(ierr);
2305426ff135SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
230655ad3c34SMatthew G. Knepley       }
230724cdb843SMatthew G. Knepley     }
230824cdb843SMatthew G. Knepley   }
2309426ff135SMatthew G. Knepley   if (hasDyn) {
2310426ff135SMatthew G. Knepley     for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2311426ff135SMatthew G. Knepley   }
231224cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
231355ad3c34SMatthew G. Knepley     if (hasPrec) {
231455ad3c34SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
231555ad3c34SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
231655ad3c34SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
231755ad3c34SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
231855ad3c34SMatthew G. Knepley     } else {
23199f11d433SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
23209f11d433SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
232124cdb843SMatthew G. Knepley     }
232255ad3c34SMatthew G. Knepley   }
23232f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
23242f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
23252f84e9bcSToby Isaac   }
23262f84e9bcSToby Isaac   else {
23272f84e9bcSToby Isaac     cgeom = NULL;
23282f84e9bcSToby Isaac   }
23292f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2330426ff135SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
2331f7ed7b21SMatthew G. Knepley   if (dmAux) {
2332f7ed7b21SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
2333f7ed7b21SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2334f7ed7b21SMatthew G. Knepley   }
233524cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2336a6ba4734SToby Isaac   ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
233724cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2338a6ba4734SToby Isaac   ierr = DMGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
233924cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
234024cdb843SMatthew G. Knepley     const char     *bdLabel;
234124cdb843SMatthew G. Knepley     DMLabel         label;
234224cdb843SMatthew G. Knepley     IS              pointIS;
234324cdb843SMatthew G. Knepley     const PetscInt *points;
234424cdb843SMatthew G. Knepley     const PetscInt *values;
2345a8e83e26SSanderA     PetscInt        field, numValues, v, numPoints, p, dep, numFaces;
234624cdb843SMatthew G. Knepley     PetscBool       isEssential;
234780bc4632SMatthew G. Knepley     PetscObject     obj;
234880bc4632SMatthew G. Knepley     PetscClassId    id;
234924cdb843SMatthew G. Knepley 
2350a6ba4734SToby Isaac     ierr = DMGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
235180bc4632SMatthew G. Knepley     ierr = DMGetField(dm, field, &obj);CHKERRQ(ierr);
235280bc4632SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
235380bc4632SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || isEssential) continue;
2354c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
2355a8e83e26SSanderA     for (v = 0; v < numValues; ++v) {
2356a8e83e26SSanderA       ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2357a8e83e26SSanderA       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
235822734eb1Ssarens       if (!pointIS) continue; /* No points with that id on this process */
235924cdb843SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
236024cdb843SMatthew G. Knepley       for (p = 0, numFaces = 0; p < numPoints; ++p) {
236124cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
236224cdb843SMatthew G. Knepley         if (dep == dim-1) ++numFaces;
236324cdb843SMatthew G. Knepley       }
2364bbce034cSMatthew G. Knepley       ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
236524cdb843SMatthew G. Knepley       if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
236624cdb843SMatthew G. Knepley       for (p = 0, f = 0; p < numPoints; ++p) {
236724cdb843SMatthew G. Knepley         const PetscInt point = points[p];
236824cdb843SMatthew G. Knepley         PetscScalar   *x     = NULL;
236924cdb843SMatthew G. Knepley         PetscInt       i;
237024cdb843SMatthew G. Knepley 
237124cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
237224cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
2373bbce034cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
2374302440fdSBarry Smith         ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr);
2375bbce034cSMatthew G. Knepley         if (cgeom[f].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", cgeom[f].detJ, point);
237624cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
237724cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
237824cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
237924cdb843SMatthew G. Knepley         if (X_t) {
238024cdb843SMatthew G. Knepley           ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
238124cdb843SMatthew G. Knepley           for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
238224cdb843SMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
238324cdb843SMatthew G. Knepley         }
238424cdb843SMatthew G. Knepley         ++f;
238524cdb843SMatthew G. Knepley       }
238624cdb843SMatthew G. Knepley       ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
238724cdb843SMatthew G. Knepley       for (fieldI = 0; fieldI < Nf; ++fieldI) {
238824cdb843SMatthew G. Knepley         PetscFE  fe;
238924cdb843SMatthew G. Knepley         PetscInt numQuadPoints, Nb;
239024cdb843SMatthew G. Knepley         /* Conforming batches */
239124cdb843SMatthew G. Knepley         PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
239224cdb843SMatthew G. Knepley         /* Remainder */
239324cdb843SMatthew G. Knepley         PetscInt Nr, offset;
239424cdb843SMatthew G. Knepley 
239524cdb843SMatthew G. Knepley         ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
239624cdb843SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
239724cdb843SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
239824cdb843SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
239924cdb843SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
240024cdb843SMatthew G. Knepley         blockSize = Nb*numQuadPoints;
240124cdb843SMatthew G. Knepley         batchSize = numBlocks * blockSize;
240224cdb843SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
240324cdb843SMatthew G. Knepley         numChunks = numFaces / (numBatches*batchSize);
240424cdb843SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
240524cdb843SMatthew G. Knepley         Nr        = numFaces % (numBatches*batchSize);
240624cdb843SMatthew G. Knepley         offset    = numFaces - Nr;
240724cdb843SMatthew G. Knepley         for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2408bbce034cSMatthew G. Knepley           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
2409bbce034cSMatthew G. Knepley           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr);
241024cdb843SMatthew G. Knepley         }
241124cdb843SMatthew G. Knepley       }
241224cdb843SMatthew G. Knepley       for (p = 0, f = 0; p < numPoints; ++p) {
241324cdb843SMatthew G. Knepley         const PetscInt point = points[p];
241424cdb843SMatthew G. Knepley 
241524cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
241624cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
241724cdb843SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
241824cdb843SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
241924cdb843SMatthew G. Knepley         ++f;
242024cdb843SMatthew G. Knepley       }
242124cdb843SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
242224cdb843SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2423bbce034cSMatthew G. Knepley       ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr);
242424cdb843SMatthew G. Knepley       if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
242524cdb843SMatthew G. Knepley     }
2426a8e83e26SSanderA   }
242782ef7567SMatthew G. Knepley   if (hasPrec) {
242882ef7567SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
242982ef7567SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
243082ef7567SMatthew G. Knepley   }
243124cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
243224cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
243324cdb843SMatthew G. Knepley   if (mesh->printFEM) {
243424cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
243524cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
243624cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
243724cdb843SMatthew G. Knepley   }
243824cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
243924cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
244024cdb843SMatthew G. Knepley   if (isShell) {
244124cdb843SMatthew G. Knepley     JacActionCtx *jctx;
244224cdb843SMatthew G. Knepley 
244324cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
244424cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
244524cdb843SMatthew G. Knepley   }
244624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
244724cdb843SMatthew G. Knepley }
244824cdb843SMatthew G. Knepley 
244924cdb843SMatthew G. Knepley #undef __FUNCT__
245024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
245124cdb843SMatthew G. Knepley /*@
245224cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
245324cdb843SMatthew G. Knepley 
245424cdb843SMatthew G. Knepley   Input Parameters:
245524cdb843SMatthew G. Knepley + dm - The mesh
245624cdb843SMatthew G. Knepley . X  - Local input vector
245724cdb843SMatthew G. Knepley - user - The user context
245824cdb843SMatthew G. Knepley 
245924cdb843SMatthew G. Knepley   Output Parameter:
246024cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
246124cdb843SMatthew G. Knepley 
246224cdb843SMatthew G. Knepley   Note:
246324cdb843SMatthew G. Knepley   The first member of the user context must be an FEMContext.
246424cdb843SMatthew G. Knepley 
246524cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
246624cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
246724cdb843SMatthew G. Knepley 
246824cdb843SMatthew G. Knepley   Level: developer
246924cdb843SMatthew G. Knepley 
247024cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
247124cdb843SMatthew G. Knepley @*/
247224cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
247324cdb843SMatthew G. Knepley {
2474b953af5fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
24756da023fcSToby Isaac   DM             plex;
247624cdb843SMatthew G. Knepley   PetscErrorCode ierr;
247724cdb843SMatthew G. Knepley 
247824cdb843SMatthew G. Knepley   PetscFunctionBegin;
24796da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
24806da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
24816da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2482b953af5fSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
24836da023fcSToby Isaac   ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
24849a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
248524cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
248624cdb843SMatthew G. Knepley }
24871878804aSMatthew G. Knepley 
24881878804aSMatthew G. Knepley #undef __FUNCT__
24899f520fc2SToby Isaac #define __FUNCT__ "DMPlexSetSNESLocalFEM"
24909f520fc2SToby Isaac /*@
24919f520fc2SToby Isaac   DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian.
24929f520fc2SToby Isaac 
24939f520fc2SToby Isaac   Input Parameters:
24949f520fc2SToby Isaac + dm - The DM object
24959f520fc2SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see DMAddBoundary())
24969f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual())
24979f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian())
2498*1a244344SSatish Balay 
2499*1a244344SSatish Balay   Level: developer
25009f520fc2SToby Isaac @*/
25019f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx)
25029f520fc2SToby Isaac {
25039f520fc2SToby Isaac   PetscErrorCode ierr;
25049f520fc2SToby Isaac 
25059f520fc2SToby Isaac   PetscFunctionBegin;
25069f520fc2SToby Isaac   ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr);
25079f520fc2SToby Isaac   ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr);
25089f520fc2SToby Isaac   ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr);
25099f520fc2SToby Isaac   PetscFunctionReturn(0);
25109f520fc2SToby Isaac }
25119f520fc2SToby Isaac 
25129f520fc2SToby Isaac #undef __FUNCT__
25131878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions_Internal"
25140163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, Vec sol, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs)
25151878804aSMatthew G. Knepley {
25161878804aSMatthew G. Knepley   Mat            J, M;
25171878804aSMatthew G. Knepley   Vec            r, b;
25181878804aSMatthew G. Knepley   MatNullSpace   nullSpace;
25191878804aSMatthew G. Knepley   PetscReal     *error, res = 0.0;
25201878804aSMatthew G. Knepley   PetscInt       numFields;
25211878804aSMatthew G. Knepley   PetscErrorCode ierr;
25221878804aSMatthew G. Knepley 
25231878804aSMatthew G. Knepley   PetscFunctionBegin;
25241878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &r);CHKERRQ(ierr);
25251878804aSMatthew G. Knepley   ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr);
25261878804aSMatthew G. Knepley   M    = J;
25271878804aSMatthew G. Knepley   /* TODO Null space for J */
25281878804aSMatthew G. Knepley   /* Check discretization error */
25291878804aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
25301878804aSMatthew G. Knepley   ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr);
25311878804aSMatthew G. Knepley   if (numFields > 1) {
25321878804aSMatthew G. Knepley     PetscInt f;
25331878804aSMatthew G. Knepley 
25341189c1efSToby Isaac     ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr);
25351878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr);
25361878804aSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
25371878804aSMatthew G. Knepley       if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);}
25381878804aSMatthew G. Knepley       if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);}
25391878804aSMatthew G. Knepley       else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);}
25401878804aSMatthew G. Knepley     }
25411878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr);
25421878804aSMatthew G. Knepley   } else {
25430709b2feSToby Isaac     ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr);
25441878804aSMatthew G. Knepley     if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);}
25451878804aSMatthew G. Knepley     else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);}
25461878804aSMatthew G. Knepley   }
25471878804aSMatthew G. Knepley   ierr = PetscFree(error);CHKERRQ(ierr);
25481878804aSMatthew G. Knepley   /* Check residual */
25491878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr);
25501878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
25511878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr);
25521878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
25531878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr);
2554685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr);
2555685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
25561878804aSMatthew G. Knepley   /* Check Jacobian */
25571878804aSMatthew G. Knepley   ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr);
25581878804aSMatthew G. Knepley   ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr);
25591878804aSMatthew G. Knepley   if (nullSpace) {
25601878804aSMatthew G. Knepley     PetscBool isNull;
25611878804aSMatthew G. Knepley     ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr);
25621878804aSMatthew G. Knepley     if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
25631878804aSMatthew G. Knepley   }
25641878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &b);CHKERRQ(ierr);
25651878804aSMatthew G. Knepley   ierr = VecSet(r, 0.0);CHKERRQ(ierr);
25661878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr);
25671878804aSMatthew G. Knepley   ierr = MatMult(M, u, r);CHKERRQ(ierr);
25681878804aSMatthew G. Knepley   ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr);
25691878804aSMatthew G. Knepley   ierr = VecDestroy(&b);CHKERRQ(ierr);
25701878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
25711878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr);
25721878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
25731878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr);
2574685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr);
2575685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
25761878804aSMatthew G. Knepley   ierr = VecDestroy(&r);CHKERRQ(ierr);
25771878804aSMatthew G. Knepley   ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
25781878804aSMatthew G. Knepley   ierr = MatDestroy(&J);CHKERRQ(ierr);
25791878804aSMatthew G. Knepley   PetscFunctionReturn(0);
25801878804aSMatthew G. Knepley }
25811878804aSMatthew G. Knepley 
25821878804aSMatthew G. Knepley #undef __FUNCT__
25831878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions"
25840163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
25851878804aSMatthew G. Knepley {
25861878804aSMatthew G. Knepley   DM             dm;
25871878804aSMatthew G. Knepley   Vec            sol;
25881878804aSMatthew G. Knepley   PetscBool      check;
25891878804aSMatthew G. Knepley   PetscErrorCode ierr;
25901878804aSMatthew G. Knepley 
25911878804aSMatthew G. Knepley   PetscFunctionBegin;
2592c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr);
25931878804aSMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
25941878804aSMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
25951878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
25961878804aSMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
25971878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
25981878804aSMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2599552f7358SJed Brown   PetscFunctionReturn(0);
2600552f7358SJed Brown }
2601