xref: /petsc/src/snes/utils/dmplexsnes.c (revision b2566f29c2b6470df769aa9f7deb9e2726b0959e)
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__
10552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate"
110adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
120adebc6cSBarry Smith {
13552f7358SJed Brown   PetscErrorCode ierr;
14552f7358SJed Brown 
15552f7358SJed Brown   PetscFunctionBegin;
16552f7358SJed Brown   PetscValidPointer(ctx, 2);
17552f7358SJed Brown   ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr);
181aa26658SKarl Rupp 
19552f7358SJed Brown   (*ctx)->comm   = comm;
20552f7358SJed Brown   (*ctx)->dim    = -1;
21552f7358SJed Brown   (*ctx)->nInput = 0;
220298fd71SBarry Smith   (*ctx)->points = NULL;
230298fd71SBarry Smith   (*ctx)->cells  = NULL;
24552f7358SJed Brown   (*ctx)->n      = -1;
250298fd71SBarry Smith   (*ctx)->coords = NULL;
26552f7358SJed Brown   PetscFunctionReturn(0);
27552f7358SJed Brown }
28552f7358SJed Brown 
29552f7358SJed Brown #undef __FUNCT__
30552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim"
310adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
320adebc6cSBarry Smith {
33552f7358SJed Brown   PetscFunctionBegin;
340adebc6cSBarry Smith   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
35552f7358SJed Brown   ctx->dim = dim;
36552f7358SJed Brown   PetscFunctionReturn(0);
37552f7358SJed Brown }
38552f7358SJed Brown 
39552f7358SJed Brown #undef __FUNCT__
40552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim"
410adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
420adebc6cSBarry Smith {
43552f7358SJed Brown   PetscFunctionBegin;
44552f7358SJed Brown   PetscValidIntPointer(dim, 2);
45552f7358SJed Brown   *dim = ctx->dim;
46552f7358SJed Brown   PetscFunctionReturn(0);
47552f7358SJed Brown }
48552f7358SJed Brown 
49552f7358SJed Brown #undef __FUNCT__
50552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof"
510adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
520adebc6cSBarry Smith {
53552f7358SJed Brown   PetscFunctionBegin;
540adebc6cSBarry Smith   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
55552f7358SJed Brown   ctx->dof = dof;
56552f7358SJed Brown   PetscFunctionReturn(0);
57552f7358SJed Brown }
58552f7358SJed Brown 
59552f7358SJed Brown #undef __FUNCT__
60552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof"
610adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
620adebc6cSBarry Smith {
63552f7358SJed Brown   PetscFunctionBegin;
64552f7358SJed Brown   PetscValidIntPointer(dof, 2);
65552f7358SJed Brown   *dof = ctx->dof;
66552f7358SJed Brown   PetscFunctionReturn(0);
67552f7358SJed Brown }
68552f7358SJed Brown 
69552f7358SJed Brown #undef __FUNCT__
70552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints"
710adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
720adebc6cSBarry Smith {
73552f7358SJed Brown   PetscErrorCode ierr;
74552f7358SJed Brown 
75552f7358SJed Brown   PetscFunctionBegin;
760adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
770adebc6cSBarry Smith   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
78552f7358SJed Brown   ctx->nInput = n;
791aa26658SKarl Rupp 
80785e854fSJed Brown   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
81552f7358SJed Brown   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
82552f7358SJed Brown   PetscFunctionReturn(0);
83552f7358SJed Brown }
84552f7358SJed Brown 
85552f7358SJed Brown #undef __FUNCT__
86552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp"
870adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
880adebc6cSBarry Smith {
89552f7358SJed Brown   MPI_Comm       comm = ctx->comm;
90552f7358SJed Brown   PetscScalar   *a;
91552f7358SJed Brown   PetscInt       p, q, i;
92552f7358SJed Brown   PetscMPIInt    rank, size;
93552f7358SJed Brown   PetscErrorCode ierr;
94552f7358SJed Brown   Vec            pointVec;
95552f7358SJed Brown   IS             cellIS;
96552f7358SJed Brown   PetscLayout    layout;
97552f7358SJed Brown   PetscReal      *globalPoints;
98cb313848SJed Brown   PetscScalar    *globalPointsScalar;
99552f7358SJed Brown   const PetscInt *ranges;
100552f7358SJed Brown   PetscMPIInt    *counts, *displs;
101552f7358SJed Brown   const PetscInt *foundCells;
102552f7358SJed Brown   PetscMPIInt    *foundProcs, *globalProcs;
10319436ca2SJed Brown   PetscInt       n, N;
104552f7358SJed Brown 
10519436ca2SJed Brown   PetscFunctionBegin;
10619436ca2SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10719436ca2SJed Brown   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
10819436ca2SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1090adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
11019436ca2SJed Brown   /* Locate points */
11119436ca2SJed Brown   n = ctx->nInput;
112552f7358SJed Brown   if (!redundantPoints) {
113552f7358SJed Brown     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
114552f7358SJed Brown     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
115552f7358SJed Brown     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
116552f7358SJed Brown     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
117552f7358SJed Brown     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
118552f7358SJed Brown     /* Communicate all points to all processes */
119dcca6d9dSJed Brown     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
120552f7358SJed Brown     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
121552f7358SJed Brown     for (p = 0; p < size; ++p) {
122552f7358SJed Brown       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
123552f7358SJed Brown       displs[p] = ranges[p]*ctx->dim;
124552f7358SJed Brown     }
125552f7358SJed Brown     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
126552f7358SJed Brown   } else {
127552f7358SJed Brown     N = n;
128552f7358SJed Brown     globalPoints = ctx->points;
12938ea73c8SJed Brown     counts = displs = NULL;
13038ea73c8SJed Brown     layout = NULL;
131552f7358SJed Brown   }
132552f7358SJed Brown #if 0
133dcca6d9dSJed Brown   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
13419436ca2SJed Brown   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
135552f7358SJed Brown #else
136cb313848SJed Brown #if defined(PETSC_USE_COMPLEX)
137785e854fSJed Brown   ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr);
138cb313848SJed Brown   for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i];
139cb313848SJed Brown #else
140cb313848SJed Brown   globalPointsScalar = globalPoints;
141cb313848SJed Brown #endif
14204706141SMatthew G Knepley   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
143dcca6d9dSJed Brown   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
144552f7358SJed Brown   ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr);
145552f7358SJed Brown   ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr);
146552f7358SJed Brown #endif
147552f7358SJed Brown   for (p = 0; p < N; ++p) {
1481aa26658SKarl Rupp     if (foundCells[p] >= 0) foundProcs[p] = rank;
1491aa26658SKarl Rupp     else foundProcs[p] = size;
150552f7358SJed Brown   }
151552f7358SJed Brown   /* Let the lowest rank process own each point */
152*b2566f29SBarry Smith   ierr   = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
153552f7358SJed Brown   ctx->n = 0;
154552f7358SJed Brown   for (p = 0; p < N; ++p) {
1550adebc6cSBarry 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);
1561aa26658SKarl Rupp     else if (globalProcs[p] == rank) ctx->n++;
157552f7358SJed Brown   }
158552f7358SJed Brown   /* Create coordinates vector and array of owned cells */
159785e854fSJed Brown   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
160552f7358SJed Brown   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
161552f7358SJed Brown   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
162552f7358SJed Brown   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
163c0dedaeaSBarry Smith   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
164552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
165552f7358SJed Brown   for (p = 0, q = 0, i = 0; p < N; ++p) {
166552f7358SJed Brown     if (globalProcs[p] == rank) {
167552f7358SJed Brown       PetscInt d;
168552f7358SJed Brown 
1691aa26658SKarl Rupp       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
170552f7358SJed Brown       ctx->cells[q++] = foundCells[p];
171552f7358SJed Brown     }
172552f7358SJed Brown   }
173552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
174552f7358SJed Brown #if 0
175552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
176552f7358SJed Brown #else
177552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
178552f7358SJed Brown   ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr);
179552f7358SJed Brown   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
180552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
181552f7358SJed Brown #endif
182cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
183d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
184552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
185552f7358SJed Brown   PetscFunctionReturn(0);
186552f7358SJed Brown }
187552f7358SJed Brown 
188552f7358SJed Brown #undef __FUNCT__
189552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates"
1900adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
1910adebc6cSBarry Smith {
192552f7358SJed Brown   PetscFunctionBegin;
193552f7358SJed Brown   PetscValidPointer(coordinates, 2);
1940adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
195552f7358SJed Brown   *coordinates = ctx->coords;
196552f7358SJed Brown   PetscFunctionReturn(0);
197552f7358SJed Brown }
198552f7358SJed Brown 
199552f7358SJed Brown #undef __FUNCT__
200552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector"
2010adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2020adebc6cSBarry Smith {
203552f7358SJed Brown   PetscErrorCode ierr;
204552f7358SJed Brown 
205552f7358SJed Brown   PetscFunctionBegin;
206552f7358SJed Brown   PetscValidPointer(v, 2);
2070adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
208552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
209552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
210552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
211c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
212552f7358SJed Brown   PetscFunctionReturn(0);
213552f7358SJed Brown }
214552f7358SJed Brown 
215552f7358SJed Brown #undef __FUNCT__
216552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector"
2170adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2180adebc6cSBarry Smith {
219552f7358SJed Brown   PetscErrorCode ierr;
220552f7358SJed Brown 
221552f7358SJed Brown   PetscFunctionBegin;
222552f7358SJed Brown   PetscValidPointer(v, 2);
2230adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
224552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
225552f7358SJed Brown   PetscFunctionReturn(0);
226552f7358SJed Brown }
227552f7358SJed Brown 
228552f7358SJed Brown #undef __FUNCT__
2297a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private"
2307a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
231a6dfd86eSKarl Rupp {
232552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
23356044e6dSMatthew G. Knepley   const PetscScalar *coords;
23456044e6dSMatthew G. Knepley   PetscScalar    *a;
235552f7358SJed Brown   PetscInt       p;
236552f7358SJed Brown   PetscErrorCode ierr;
237552f7358SJed Brown 
238552f7358SJed Brown   PetscFunctionBegin;
239dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
24056044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
241552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
242552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
243552f7358SJed Brown     PetscInt     c = ctx->cells[p];
244a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
245552f7358SJed Brown     PetscReal    xi[4];
246552f7358SJed Brown     PetscInt     d, f, comp;
247552f7358SJed Brown 
2488e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
249552f7358SJed Brown     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2500298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2511aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2521aa26658SKarl Rupp 
253552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
254552f7358SJed Brown       xi[d] = 0.0;
2551aa26658SKarl 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]);
2561aa26658SKarl 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];
257552f7358SJed Brown     }
2580298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
259552f7358SJed Brown   }
260552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
26156044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
262552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
263552f7358SJed Brown   PetscFunctionReturn(0);
264552f7358SJed Brown }
265552f7358SJed Brown 
266552f7358SJed Brown #undef __FUNCT__
2677a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private"
2687a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
2697a1931ceSMatthew G. Knepley {
2707a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
27156044e6dSMatthew G. Knepley   const PetscScalar *coords;
27256044e6dSMatthew G. Knepley   PetscScalar    *a;
2737a1931ceSMatthew G. Knepley   PetscInt       p;
2747a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
2757a1931ceSMatthew G. Knepley 
2767a1931ceSMatthew G. Knepley   PetscFunctionBegin;
277dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
27856044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
2797a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
2807a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
2817a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
2827a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
2832584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
2847a1931ceSMatthew G. Knepley     PetscReal      xi[4];
2857a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
2867a1931ceSMatthew G. Knepley 
2878e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2887a1931ceSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2897a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2907a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2917a1931ceSMatthew G. Knepley 
2927a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
2937a1931ceSMatthew G. Knepley       xi[d] = 0.0;
2947a1931ceSMatthew 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]);
2957a1931ceSMatthew 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];
2967a1931ceSMatthew G. Knepley     }
2977a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2987a1931ceSMatthew G. Knepley   }
2997a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
30056044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
3017a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
3027a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3037a1931ceSMatthew G. Knepley }
3047a1931ceSMatthew G. Knepley 
3057a1931ceSMatthew G. Knepley #undef __FUNCT__
306552f7358SJed Brown #define __FUNCT__ "QuadMap_Private"
3075820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
308552f7358SJed Brown {
309552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
310552f7358SJed Brown   const PetscScalar x0        = vertices[0];
311552f7358SJed Brown   const PetscScalar y0        = vertices[1];
312552f7358SJed Brown   const PetscScalar x1        = vertices[2];
313552f7358SJed Brown   const PetscScalar y1        = vertices[3];
314552f7358SJed Brown   const PetscScalar x2        = vertices[4];
315552f7358SJed Brown   const PetscScalar y2        = vertices[5];
316552f7358SJed Brown   const PetscScalar x3        = vertices[6];
317552f7358SJed Brown   const PetscScalar y3        = vertices[7];
318552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
319552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
320552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
321552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
322552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
323552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
32456044e6dSMatthew G. Knepley   const PetscScalar *ref;
32556044e6dSMatthew G. Knepley   PetscScalar       *real;
326552f7358SJed Brown   PetscErrorCode    ierr;
327552f7358SJed Brown 
328552f7358SJed Brown   PetscFunctionBegin;
32956044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
330552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
331552f7358SJed Brown   {
332552f7358SJed Brown     const PetscScalar p0 = ref[0];
333552f7358SJed Brown     const PetscScalar p1 = ref[1];
334552f7358SJed Brown 
335552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
336552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
337552f7358SJed Brown   }
338552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
33956044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
340552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
341552f7358SJed Brown   PetscFunctionReturn(0);
342552f7358SJed Brown }
343552f7358SJed Brown 
344af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
345552f7358SJed Brown #undef __FUNCT__
346552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private"
347d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
348552f7358SJed Brown {
349552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
350552f7358SJed Brown   const PetscScalar x0        = vertices[0];
351552f7358SJed Brown   const PetscScalar y0        = vertices[1];
352552f7358SJed Brown   const PetscScalar x1        = vertices[2];
353552f7358SJed Brown   const PetscScalar y1        = vertices[3];
354552f7358SJed Brown   const PetscScalar x2        = vertices[4];
355552f7358SJed Brown   const PetscScalar y2        = vertices[5];
356552f7358SJed Brown   const PetscScalar x3        = vertices[6];
357552f7358SJed Brown   const PetscScalar y3        = vertices[7];
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;
361552f7358SJed Brown   PetscErrorCode    ierr;
362552f7358SJed Brown 
363552f7358SJed Brown   PetscFunctionBegin;
36456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
365552f7358SJed Brown   {
366552f7358SJed Brown     const PetscScalar x       = ref[0];
367552f7358SJed Brown     const PetscScalar y       = ref[1];
368552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
369da80777bSKarl Rupp     PetscScalar       values[4];
370da80777bSKarl Rupp 
371da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
372da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
37394ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
374552f7358SJed Brown   }
375552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
37656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
37794ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
37894ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
379552f7358SJed Brown   PetscFunctionReturn(0);
380552f7358SJed Brown }
381552f7358SJed Brown 
382552f7358SJed Brown #undef __FUNCT__
383552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private"
384a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
385a6dfd86eSKarl Rupp {
386fafc0619SMatthew G Knepley   DM             dmCoord;
387552f7358SJed Brown   SNES           snes;
388552f7358SJed Brown   KSP            ksp;
389552f7358SJed Brown   PC             pc;
390552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
391552f7358SJed Brown   Mat            J;
39256044e6dSMatthew G. Knepley   const PetscScalar *coords;
39356044e6dSMatthew G. Knepley   PetscScalar    *a;
394552f7358SJed Brown   PetscInt       p;
395552f7358SJed Brown   PetscErrorCode ierr;
396552f7358SJed Brown 
397552f7358SJed Brown   PetscFunctionBegin;
398552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
399fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
400552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
401552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
402552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
403552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
404c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
405552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
406552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
407552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
408552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
409552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
410552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4110298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4120298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
413552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
414552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
415552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
416552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
417552f7358SJed Brown 
41856044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
419552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
420552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
421a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
422552f7358SJed Brown     PetscScalar *xi;
423cb313848SJed Brown     PetscReal    xir[2];
424552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
425552f7358SJed Brown 
426552f7358SJed Brown     /* Can make this do all points at once */
4270298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4280adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4290298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4300adebc6cSBarry Smith     if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof);
4310298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4320298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
433552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
434552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
435552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
436552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
437552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
438552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
439cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
440cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4411aa26658SKarl 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];
4421aa26658SKarl Rupp 
443552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4440298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4450298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
446552f7358SJed Brown   }
447552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
44856044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
449552f7358SJed Brown 
450552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
451552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
452552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
453552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
454552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
455552f7358SJed Brown   PetscFunctionReturn(0);
456552f7358SJed Brown }
457552f7358SJed Brown 
458552f7358SJed Brown #undef __FUNCT__
459552f7358SJed Brown #define __FUNCT__ "HexMap_Private"
4605820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
461552f7358SJed Brown {
462552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
463552f7358SJed Brown   const PetscScalar x0        = vertices[0];
464552f7358SJed Brown   const PetscScalar y0        = vertices[1];
465552f7358SJed Brown   const PetscScalar z0        = vertices[2];
4667a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
4677a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
4687a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
469552f7358SJed Brown   const PetscScalar x2        = vertices[6];
470552f7358SJed Brown   const PetscScalar y2        = vertices[7];
471552f7358SJed Brown   const PetscScalar z2        = vertices[8];
4727a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
4737a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
4747a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
475552f7358SJed Brown   const PetscScalar x4        = vertices[12];
476552f7358SJed Brown   const PetscScalar y4        = vertices[13];
477552f7358SJed Brown   const PetscScalar z4        = vertices[14];
478552f7358SJed Brown   const PetscScalar x5        = vertices[15];
479552f7358SJed Brown   const PetscScalar y5        = vertices[16];
480552f7358SJed Brown   const PetscScalar z5        = vertices[17];
481552f7358SJed Brown   const PetscScalar x6        = vertices[18];
482552f7358SJed Brown   const PetscScalar y6        = vertices[19];
483552f7358SJed Brown   const PetscScalar z6        = vertices[20];
484552f7358SJed Brown   const PetscScalar x7        = vertices[21];
485552f7358SJed Brown   const PetscScalar y7        = vertices[22];
486552f7358SJed Brown   const PetscScalar z7        = vertices[23];
487552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
488552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
489552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
490552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
491552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
492552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
493552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
494552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
495552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
496552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
497552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
498552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
499552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
500552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
501552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
502552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
503552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
504552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
505552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
506552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
507552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
50856044e6dSMatthew G. Knepley   const PetscScalar *ref;
50956044e6dSMatthew G. Knepley   PetscScalar       *real;
510552f7358SJed Brown   PetscErrorCode    ierr;
511552f7358SJed Brown 
512552f7358SJed Brown   PetscFunctionBegin;
51356044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
514552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
515552f7358SJed Brown   {
516552f7358SJed Brown     const PetscScalar p0 = ref[0];
517552f7358SJed Brown     const PetscScalar p1 = ref[1];
518552f7358SJed Brown     const PetscScalar p2 = ref[2];
519552f7358SJed Brown 
520552f7358SJed 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;
521552f7358SJed 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;
522552f7358SJed 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;
523552f7358SJed Brown   }
524552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
52556044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
526552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
527552f7358SJed Brown   PetscFunctionReturn(0);
528552f7358SJed Brown }
529552f7358SJed Brown 
530552f7358SJed Brown #undef __FUNCT__
531552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private"
532d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
533552f7358SJed Brown {
534552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
535552f7358SJed Brown   const PetscScalar x0        = vertices[0];
536552f7358SJed Brown   const PetscScalar y0        = vertices[1];
537552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5387a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5397a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5407a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
541552f7358SJed Brown   const PetscScalar x2        = vertices[6];
542552f7358SJed Brown   const PetscScalar y2        = vertices[7];
543552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5447a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5457a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5467a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
547552f7358SJed Brown   const PetscScalar x4        = vertices[12];
548552f7358SJed Brown   const PetscScalar y4        = vertices[13];
549552f7358SJed Brown   const PetscScalar z4        = vertices[14];
550552f7358SJed Brown   const PetscScalar x5        = vertices[15];
551552f7358SJed Brown   const PetscScalar y5        = vertices[16];
552552f7358SJed Brown   const PetscScalar z5        = vertices[17];
553552f7358SJed Brown   const PetscScalar x6        = vertices[18];
554552f7358SJed Brown   const PetscScalar y6        = vertices[19];
555552f7358SJed Brown   const PetscScalar z6        = vertices[20];
556552f7358SJed Brown   const PetscScalar x7        = vertices[21];
557552f7358SJed Brown   const PetscScalar y7        = vertices[22];
558552f7358SJed Brown   const PetscScalar z7        = vertices[23];
559552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
560552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
561552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
562552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
563552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
564552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
565552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
566552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
567552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
568552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
569552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
570552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
57156044e6dSMatthew G. Knepley   const PetscScalar *ref;
572552f7358SJed Brown   PetscErrorCode    ierr;
573552f7358SJed Brown 
574552f7358SJed Brown   PetscFunctionBegin;
57556044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
576552f7358SJed Brown   {
577552f7358SJed Brown     const PetscScalar x       = ref[0];
578552f7358SJed Brown     const PetscScalar y       = ref[1];
579552f7358SJed Brown     const PetscScalar z       = ref[2];
580552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
581da80777bSKarl Rupp     PetscScalar       values[9];
582da80777bSKarl Rupp 
583da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
584da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
585da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
586da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
587da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
588da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
589da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
590da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
591da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
5921aa26658SKarl Rupp 
59394ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
594552f7358SJed Brown   }
595552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
59656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
59794ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
59894ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
599552f7358SJed Brown   PetscFunctionReturn(0);
600552f7358SJed Brown }
601552f7358SJed Brown 
602552f7358SJed Brown #undef __FUNCT__
603552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private"
604a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
605a6dfd86eSKarl Rupp {
606fafc0619SMatthew G Knepley   DM             dmCoord;
607552f7358SJed Brown   SNES           snes;
608552f7358SJed Brown   KSP            ksp;
609552f7358SJed Brown   PC             pc;
610552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
611552f7358SJed Brown   Mat            J;
61256044e6dSMatthew G. Knepley   const PetscScalar *coords;
61356044e6dSMatthew G. Knepley   PetscScalar    *a;
614552f7358SJed Brown   PetscInt       p;
615552f7358SJed Brown   PetscErrorCode ierr;
616552f7358SJed Brown 
617552f7358SJed Brown   PetscFunctionBegin;
618552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
619fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
620552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
621552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
622552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
623552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
624c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
625552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
626552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
627552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
628552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
629552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
630552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6310298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6320298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
633552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
634552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
635552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
636552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
637552f7358SJed Brown 
63856044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
639552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
640552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
641a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
642552f7358SJed Brown     PetscScalar *xi;
643cb313848SJed Brown     PetscReal    xir[3];
644552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
645552f7358SJed Brown 
646552f7358SJed Brown     /* Can make this do all points at once */
6470298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6480adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6490298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6500adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6510298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6520298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
653552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
654552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
655552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
656552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
657552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
658552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
659552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
660cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
661cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
662cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
663552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
664552f7358SJed Brown       a[p*ctx->dof+comp] =
665cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
6667a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
667cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
6687a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
669cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
670cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
671cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
672cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
673552f7358SJed Brown     }
674552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
6750298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6760298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
677552f7358SJed Brown   }
678552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
67956044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
680552f7358SJed Brown 
681552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
682552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
683552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
684552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
685552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
686552f7358SJed Brown   PetscFunctionReturn(0);
687552f7358SJed Brown }
688552f7358SJed Brown 
689552f7358SJed Brown #undef __FUNCT__
690552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate"
691552f7358SJed Brown /*
692552f7358SJed Brown   Input Parameters:
693552f7358SJed Brown + ctx - The DMInterpolationInfo context
694552f7358SJed Brown . dm  - The DM
695552f7358SJed Brown - x   - The local vector containing the field to be interpolated
696552f7358SJed Brown 
697552f7358SJed Brown   Output Parameters:
698552f7358SJed Brown . v   - The vector containing the interpolated values
699552f7358SJed Brown */
7000adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
7010adebc6cSBarry Smith {
702552f7358SJed Brown   PetscInt       dim, coneSize, n;
703552f7358SJed Brown   PetscErrorCode ierr;
704552f7358SJed Brown 
705552f7358SJed Brown   PetscFunctionBegin;
706552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
707552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
708552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
709552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7100adebc6cSBarry 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);
711552f7358SJed Brown   if (n) {
712c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
713552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
714552f7358SJed Brown     if (dim == 2) {
715552f7358SJed Brown       if (coneSize == 3) {
7167a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
717552f7358SJed Brown       } else if (coneSize == 4) {
718552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7190adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
720552f7358SJed Brown     } else if (dim == 3) {
721552f7358SJed Brown       if (coneSize == 4) {
7227a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
723552f7358SJed Brown       } else {
724552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
725552f7358SJed Brown       }
7260adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
727552f7358SJed Brown   }
728552f7358SJed Brown   PetscFunctionReturn(0);
729552f7358SJed Brown }
730552f7358SJed Brown 
731552f7358SJed Brown #undef __FUNCT__
732552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy"
7330adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7340adebc6cSBarry Smith {
735552f7358SJed Brown   PetscErrorCode ierr;
736552f7358SJed Brown 
737552f7358SJed Brown   PetscFunctionBegin;
738552f7358SJed Brown   PetscValidPointer(ctx, 2);
739552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
740552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
741552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
742552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7430298fd71SBarry Smith   *ctx = NULL;
744552f7358SJed Brown   PetscFunctionReturn(0);
745552f7358SJed Brown }
746cc0c4584SMatthew G. Knepley 
747cc0c4584SMatthew G. Knepley #undef __FUNCT__
748cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields"
749cc0c4584SMatthew G. Knepley /*@C
750cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
751cc0c4584SMatthew G. Knepley 
752cc0c4584SMatthew G. Knepley   Collective on SNES
753cc0c4584SMatthew G. Knepley 
754cc0c4584SMatthew G. Knepley   Input Parameters:
755cc0c4584SMatthew G. Knepley + snes   - the SNES context
756cc0c4584SMatthew G. Knepley . its    - iteration number
757cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
7584d4332d5SBarry Smith - dummy  - PetscViewer of type ASCII
759cc0c4584SMatthew G. Knepley 
760cc0c4584SMatthew G. Knepley   Notes:
761cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
762cc0c4584SMatthew G. Knepley 
763cc0c4584SMatthew G. Knepley   Level: intermediate
764cc0c4584SMatthew G. Knepley 
765cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
766cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
767cc0c4584SMatthew G. Knepley @*/
768cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy)
769cc0c4584SMatthew G. Knepley {
7704d4332d5SBarry Smith   PetscViewer        viewer = (PetscViewer) dummy;
771cc0c4584SMatthew G. Knepley   Vec                res;
772cc0c4584SMatthew G. Knepley   DM                 dm;
773cc0c4584SMatthew G. Knepley   PetscSection       s;
774cc0c4584SMatthew G. Knepley   const PetscScalar *r;
775cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
776cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
777cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
778cc0c4584SMatthew G. Knepley 
779cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
7804d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
781cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
782cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
783cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
784cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
785cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
786cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
787cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
788cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
789cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
790cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
791cc0c4584SMatthew G. Knepley 
792cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
793cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
794cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
795cc0c4584SMatthew G. Knepley     }
796cc0c4584SMatthew G. Knepley   }
797cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
798*b2566f29SBarry Smith   ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
799cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
800cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
801cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
802cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
803cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
804cc0c4584SMatthew G. Knepley   }
805cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
806cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
807cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
808cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
809cc0c4584SMatthew G. Knepley }
81024cdb843SMatthew G. Knepley 
81124cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
81224cdb843SMatthew G. Knepley 
81324cdb843SMatthew G. Knepley #undef __FUNCT__
8147d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM"
8157d4028c8SMatthew G. Knepley /*@
8167d4028c8SMatthew G. Knepley   DMPlexSNESGetGeometryFEM - Return precomputed geometric data
8177d4028c8SMatthew G. Knepley 
8187d4028c8SMatthew G. Knepley   Input Parameter:
8197d4028c8SMatthew G. Knepley . dm - The DM
8207d4028c8SMatthew G. Knepley 
8217d4028c8SMatthew G. Knepley   Output Parameters:
8227d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
8237d4028c8SMatthew G. Knepley 
8247d4028c8SMatthew G. Knepley   Level: developer
8257d4028c8SMatthew G. Knepley 
8267d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal()
8277d4028c8SMatthew G. Knepley @*/
8287d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom)
8297d4028c8SMatthew G. Knepley {
8307d4028c8SMatthew G. Knepley   DMSNES         dmsnes;
8317d4028c8SMatthew G. Knepley   PetscObject    obj;
8327d4028c8SMatthew G. Knepley   PetscErrorCode ierr;
8337d4028c8SMatthew G. Knepley 
8347d4028c8SMatthew G. Knepley   PetscFunctionBegin;
8357d4028c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8367d4028c8SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
8377d4028c8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr);
8387d4028c8SMatthew G. Knepley   if (!obj) {
8397d4028c8SMatthew G. Knepley     Vec cellgeom;
8407d4028c8SMatthew G. Knepley 
8417d4028c8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
8427d4028c8SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr);
8437d4028c8SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
8447d4028c8SMatthew G. Knepley   }
8457d4028c8SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);}
8467d4028c8SMatthew G. Knepley   PetscFunctionReturn(0);
8477d4028c8SMatthew G. Knepley }
8487d4028c8SMatthew G. Knepley 
8497d4028c8SMatthew G. Knepley #undef __FUNCT__
85008449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM"
85108449791SMatthew G. Knepley /*@
85208449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
85308449791SMatthew G. Knepley 
85408449791SMatthew G. Knepley   Input Parameter:
85508449791SMatthew G. Knepley . dm - The DM
85608449791SMatthew G. Knepley 
85708449791SMatthew G. Knepley   Output Parameters:
85808449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
85908449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
86008449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
86108449791SMatthew G. Knepley 
86208449791SMatthew G. Knepley   Level: developer
86308449791SMatthew G. Knepley 
86408449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
86508449791SMatthew G. Knepley @*/
86608449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
86724cdb843SMatthew G. Knepley {
86808449791SMatthew G. Knepley   DMSNES         dmsnes;
86908449791SMatthew G. Knepley   PetscObject    obj;
87024cdb843SMatthew G. Knepley   PetscErrorCode ierr;
87124cdb843SMatthew G. Knepley 
87224cdb843SMatthew G. Knepley   PetscFunctionBegin;
87308449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87408449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
87508449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr);
87608449791SMatthew G. Knepley   if (!obj) {
87708449791SMatthew G. Knepley     Vec cellgeom, facegeom;
87824cdb843SMatthew G. Knepley 
87908449791SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr);
88008449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr);
88108449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr);
88208449791SMatthew G. Knepley     ierr = VecDestroy(&facegeom);CHKERRQ(ierr);
88308449791SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
88408449791SMatthew G. Knepley   }
88508449791SMatthew G. Knepley   if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);}
88608449791SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);}
88708449791SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);}
88824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
88924cdb843SMatthew G. Knepley }
89024cdb843SMatthew G. Knepley 
89124cdb843SMatthew G. Knepley #undef __FUNCT__
89208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM"
893dbd489d2SMatthew G. Knepley /*@
89408449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
89508449791SMatthew G. Knepley 
89608449791SMatthew G. Knepley   Input Parameters:
89708449791SMatthew G. Knepley + dm - The DM
89808449791SMatthew G. Knepley - fv - The PetscFV
89908449791SMatthew G. Knepley 
90008449791SMatthew G. Knepley   Output Parameter:
90108449791SMatthew G. Knepley . dmGrad - The layout for gradient values
90208449791SMatthew G. Knepley 
90308449791SMatthew G. Knepley   Level: developer
90408449791SMatthew G. Knepley 
90508449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
90608449791SMatthew G. Knepley @*/
90708449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
90824cdb843SMatthew G. Knepley {
90908449791SMatthew G. Knepley   DMSNES         dmsnes;
91008449791SMatthew G. Knepley   PetscObject    obj;
91108449791SMatthew G. Knepley   PetscBool      computeGradients;
91224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
91324cdb843SMatthew G. Knepley 
91424cdb843SMatthew G. Knepley   PetscFunctionBegin;
91508449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91608449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
91708449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
91808449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
91908449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
92008449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
92108449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr);
92208449791SMatthew G. Knepley   if (!obj) {
92308449791SMatthew G. Knepley     DM  dmGrad;
92408449791SMatthew G. Knepley     Vec faceGeometry, cellGeometry;
92508449791SMatthew G. Knepley 
92608449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr);
92708449791SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr);
92808449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr);
92908449791SMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
93008449791SMatthew G. Knepley   }
93108449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr);
93208449791SMatthew G. Knepley   PetscFunctionReturn(0);
93308449791SMatthew G. Knepley }
93408449791SMatthew G. Knepley 
93508449791SMatthew G. Knepley #undef __FUNCT__
93608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields"
93708449791SMatthew G. Knepley /*@C
93808449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
93908449791SMatthew G. Knepley 
94008449791SMatthew G. Knepley   Input Parameters:
94108449791SMatthew G. Knepley + dm     - The DM
94208449791SMatthew G. Knepley . cStart - The first cell to include
94308449791SMatthew G. Knepley . cEnd   - The first cell to exclude
94408449791SMatthew G. Knepley . locX   - A local vector with the solution fields
94508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
94608449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
94708449791SMatthew G. Knepley 
94808449791SMatthew G. Knepley   Output Parameters:
94908449791SMatthew G. Knepley + u   - The field coefficients
95008449791SMatthew G. Knepley . u_t - The fields derivative coefficients
95108449791SMatthew G. Knepley - a   - The auxiliary field coefficients
95208449791SMatthew G. Knepley 
95308449791SMatthew G. Knepley   Level: developer
95408449791SMatthew G. Knepley 
95508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
95608449791SMatthew G. Knepley @*/
95708449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
95808449791SMatthew G. Knepley {
95908449791SMatthew G. Knepley   DM             dmAux;
96008449791SMatthew G. Knepley   PetscSection   section, sectionAux;
96108449791SMatthew G. Knepley   PetscDS        prob;
96208449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
96308449791SMatthew G. Knepley   PetscErrorCode ierr;
96408449791SMatthew G. Knepley 
96508449791SMatthew G. Knepley   PetscFunctionBegin;
96608449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96708449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
96808449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
96908449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
97008449791SMatthew G. Knepley   PetscValidPointer(u, 7);
97108449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
97208449791SMatthew G. Knepley   PetscValidPointer(a, 9);
97324cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
97424cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
97524cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
97608449791SMatthew G. Knepley   if (locA) {
97708449791SMatthew G. Knepley     PetscDS probAux;
97808449791SMatthew G. Knepley 
97908449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
98024cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
98124cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
98224cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
98324cdb843SMatthew G. Knepley   }
98408449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr);
98549073227SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
98649073227SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
98724cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
98808449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
98924cdb843SMatthew G. Knepley     PetscInt     i;
99024cdb843SMatthew G. Knepley 
99108449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
9929f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i];
99308449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
99408449791SMatthew G. Knepley     if (locX_t) {
99508449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
9969f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i];
99708449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
99824cdb843SMatthew G. Knepley     }
99908449791SMatthew G. Knepley     if (locA) {
100008449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
10019f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i];
100208449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
100324cdb843SMatthew G. Knepley     }
100424cdb843SMatthew G. Knepley   }
100508449791SMatthew G. Knepley   PetscFunctionReturn(0);
100608449791SMatthew G. Knepley }
100724cdb843SMatthew G. Knepley 
100808449791SMatthew G. Knepley #undef __FUNCT__
100908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields"
101008449791SMatthew G. Knepley /*@C
101108449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
101208449791SMatthew G. Knepley 
101308449791SMatthew G. Knepley   Input Parameters:
101408449791SMatthew G. Knepley + dm     - The DM
101508449791SMatthew G. Knepley . cStart - The first cell to include
101608449791SMatthew G. Knepley . cEnd   - The first cell to exclude
101708449791SMatthew G. Knepley . locX   - A local vector with the solution fields
101808449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
101908449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
102008449791SMatthew G. Knepley 
102108449791SMatthew G. Knepley   Output Parameters:
102208449791SMatthew G. Knepley + u   - The field coefficients
102308449791SMatthew G. Knepley . u_t - The fields derivative coefficients
102408449791SMatthew G. Knepley - a   - The auxiliary field coefficients
102508449791SMatthew G. Knepley 
102608449791SMatthew G. Knepley   Level: developer
102708449791SMatthew G. Knepley 
102808449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
102908449791SMatthew G. Knepley @*/
103008449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
103108449791SMatthew G. Knepley {
103208449791SMatthew G. Knepley   PetscErrorCode ierr;
103308449791SMatthew G. Knepley 
103408449791SMatthew G. Knepley   PetscFunctionBegin;
103508449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr);
103649073227SMatthew G. Knepley   if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);}
103749073227SMatthew G. Knepley   if (*a)   {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);}
103808449791SMatthew G. Knepley   PetscFunctionReturn(0);
103924cdb843SMatthew G. Knepley }
104008449791SMatthew G. Knepley 
104108449791SMatthew G. Knepley #undef __FUNCT__
104208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields"
104308449791SMatthew G. Knepley /*@C
104408449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
104508449791SMatthew G. Knepley 
104608449791SMatthew G. Knepley   Input Parameters:
104708449791SMatthew G. Knepley + dm     - The DM
104808449791SMatthew G. Knepley . fStart - The first face to include
104908449791SMatthew G. Knepley . fEnd   - The first face to exclude
105008449791SMatthew G. Knepley . locX   - A local vector with the solution fields
105108449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
105208449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
105308449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
105408449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
105508449791SMatthew G. Knepley 
105608449791SMatthew G. Knepley   Output Parameters:
1057477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face
1058477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
105908449791SMatthew G. Knepley 
106008449791SMatthew G. Knepley   Level: developer
106108449791SMatthew G. Knepley 
106208449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
106308449791SMatthew G. Knepley @*/
106408449791SMatthew 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)
106508449791SMatthew G. Knepley {
106608449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
1067195142f5SMatthew G. Knepley   PetscSection       section;
106808449791SMatthew G. Knepley   PetscDS            prob;
106908449791SMatthew G. Knepley   DMLabel            ghostLabel;
107008449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1071477f7dfdSMatthew G. Knepley   PetscBool         *isFE;
1072477f7dfdSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
107308449791SMatthew G. Knepley   PetscErrorCode     ierr;
107408449791SMatthew G. Knepley 
107508449791SMatthew G. Knepley   PetscFunctionBegin;
107608449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107708449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
107808449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
107908449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
108008449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
108108449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
108208449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
108308449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
108408449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
108508449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1086195142f5SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1087477f7dfdSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1088477f7dfdSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
1089477f7dfdSMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
1090477f7dfdSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1091477f7dfdSMatthew G. Knepley     PetscObject  obj;
1092477f7dfdSMatthew G. Knepley     PetscClassId id;
1093477f7dfdSMatthew G. Knepley 
1094477f7dfdSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
1095477f7dfdSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1096477f7dfdSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
1097477f7dfdSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
1098477f7dfdSMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
1099477f7dfdSMatthew G. Knepley   }
110008449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
110108449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
110208449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
110308449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
110408449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
110508449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
110608449791SMatthew G. Knepley   if (locGrad) {
110708449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
110808449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
110924cdb843SMatthew G. Knepley   }
1110477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr);
1111477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr);
1112477f7dfdSMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
111308449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
111408449791SMatthew G. Knepley     const PetscInt        *cells;
111508449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
111608449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
111708449791SMatthew G. Knepley     const PetscScalar     *xL, *xR, *gL, *gR;
111808449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
1119e697831aSToby Isaac     PetscInt               ghost, nsupp;
112008449791SMatthew G. Knepley 
112108449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1122e697831aSToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
112308449791SMatthew G. Knepley     if (ghost >= 0) continue;
112408449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
112508449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
112608449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
112708449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1128477f7dfdSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1129477f7dfdSMatthew G. Knepley       PetscInt off;
1130477f7dfdSMatthew G. Knepley 
113137a43ebbSMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1132477f7dfdSMatthew G. Knepley       if (isFE[f]) {
1133477f7dfdSMatthew G. Knepley         const PetscInt *cone;
1134cca9989dSMatthew G. Knepley         PetscInt        comp, coneSize, faceLocL, faceLocR, ldof, rdof, d;
1135477f7dfdSMatthew G. Knepley 
1136cca9989dSMatthew G. Knepley         xL = xR = NULL;
1137cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1138cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1139477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
1140477f7dfdSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSize);CHKERRQ(ierr);
1141477f7dfdSMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSize; ++faceLocL) if (cone[faceLocL] == face) break;
1142477f7dfdSMatthew 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]);
1143477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
1144477f7dfdSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSize);CHKERRQ(ierr);
1145477f7dfdSMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSize; ++faceLocR) if (cone[faceLocR] == face) break;
1146477f7dfdSMatthew 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]);
1147195142f5SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
1148477f7dfdSMatthew G. Knepley         ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
1149cca9989dSMatthew G. Knepley         if (rdof == ldof) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
1150cca9989dSMatthew 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];}
1151cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1152cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1153477f7dfdSMatthew G. Knepley       } else {
1154477f7dfdSMatthew G. Knepley         PetscFV  fv;
1155477f7dfdSMatthew G. Knepley         PetscInt numComp, c;
1156477f7dfdSMatthew G. Knepley 
1157477f7dfdSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
1158477f7dfdSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1159e697831aSToby Isaac         if (nsupp > 2) {
1160e697831aSToby Isaac           for (f = 0; f < Nf; ++f) {
1161e697831aSToby Isaac             PetscInt off;
1162e697831aSToby Isaac 
1163e697831aSToby Isaac             ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1164e697831aSToby Isaac             ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1165e697831aSToby Isaac             for (c = 0; c < numComp; ++c) {
1166e697831aSToby Isaac               uLl[iface*Nc+off+c] = 0.;
1167e697831aSToby Isaac               uRl[iface*Nc+off+c] = 0.;
1168e697831aSToby Isaac             }
1169e697831aSToby Isaac           }
1170e697831aSToby Isaac           continue;
1171e697831aSToby Isaac         }
1172cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
1173cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
117408449791SMatthew G. Knepley         if (dmGrad) {
117508449791SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
117608449791SMatthew G. Knepley 
117708449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
117808449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
117908449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
118008449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1181477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1182477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
1183477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
118408449791SMatthew G. Knepley           }
118508449791SMatthew G. Knepley         } else {
1186477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1187477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
1188477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
1189477f7dfdSMatthew G. Knepley           }
1190477f7dfdSMatthew G. Knepley         }
119108449791SMatthew G. Knepley       }
119208449791SMatthew G. Knepley     }
119308449791SMatthew G. Knepley     ++iface;
119408449791SMatthew G. Knepley   }
119508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
119608449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
119708449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
119808449791SMatthew G. Knepley   if (locGrad) {
119908449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
120008449791SMatthew G. Knepley   }
1201477f7dfdSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
120208449791SMatthew G. Knepley   PetscFunctionReturn(0);
120308449791SMatthew G. Knepley }
120408449791SMatthew G. Knepley 
120508449791SMatthew G. Knepley #undef __FUNCT__
120608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields"
120708449791SMatthew G. Knepley /*@C
120808449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
120908449791SMatthew G. Knepley 
121008449791SMatthew G. Knepley   Input Parameters:
121108449791SMatthew G. Knepley + dm     - The DM
121208449791SMatthew G. Knepley . fStart - The first face to include
121308449791SMatthew G. Knepley . fEnd   - The first face to exclude
121408449791SMatthew G. Knepley . locX   - A local vector with the solution fields
121508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
121608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
121708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
121808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
121908449791SMatthew G. Knepley 
122008449791SMatthew G. Knepley   Output Parameters:
1221477f7dfdSMatthew G. Knepley + uL - The field values at the left side of the face
1222477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
122308449791SMatthew G. Knepley 
122408449791SMatthew G. Knepley   Level: developer
122508449791SMatthew G. Knepley 
122608449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
122708449791SMatthew G. Knepley @*/
122808449791SMatthew 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)
122908449791SMatthew G. Knepley {
123008449791SMatthew G. Knepley   PetscErrorCode ierr;
123108449791SMatthew G. Knepley 
123208449791SMatthew G. Knepley   PetscFunctionBegin;
123308449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr);
123408449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr);
123508449791SMatthew G. Knepley   PetscFunctionReturn(0);
123608449791SMatthew G. Knepley }
123708449791SMatthew G. Knepley 
123808449791SMatthew G. Knepley #undef __FUNCT__
123908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry"
124008449791SMatthew G. Knepley /*@C
124108449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
124208449791SMatthew G. Knepley 
124308449791SMatthew G. Knepley   Input Parameters:
124408449791SMatthew G. Knepley + dm     - The DM
124508449791SMatthew G. Knepley . fStart - The first face to include
124608449791SMatthew G. Knepley . fEnd   - The first face to exclude
124708449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
124808449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
124908449791SMatthew G. Knepley 
125008449791SMatthew G. Knepley   Output Parameters:
125108449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
125208449791SMatthew G. Knepley - vol   - The cell volume
125308449791SMatthew G. Knepley 
125408449791SMatthew G. Knepley   Level: developer
125508449791SMatthew G. Knepley 
125608449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
125708449791SMatthew G. Knepley @*/
125808449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
125908449791SMatthew G. Knepley {
126008449791SMatthew G. Knepley   DM                 dmFace, dmCell;
126108449791SMatthew G. Knepley   DMLabel            ghostLabel;
126208449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
126308449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
126408449791SMatthew G. Knepley   PetscErrorCode     ierr;
126508449791SMatthew G. Knepley 
126608449791SMatthew G. Knepley   PetscFunctionBegin;
126708449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
126808449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
126908449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
127008449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
127108449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
127208449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
127308449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
127408449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
127508449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
127608449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
127708449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
127808449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
127908449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr);
128008449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
128108449791SMatthew G. Knepley     const PetscInt        *cells;
128208449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
128308449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
128408449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
12852eefff9cSMatthew G. Knepley     PetscReal             *voll   = *vol;
128608449791SMatthew G. Knepley     PetscInt               ghost, d;
128708449791SMatthew G. Knepley 
128808449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
128908449791SMatthew G. Knepley     if (ghost >= 0) continue;
129008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
129108449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
129208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
129308449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
129408449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
129508449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
129608449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
129708449791SMatthew G. Knepley     }
129808449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
129908449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
130008449791SMatthew G. Knepley     ++iface;
130108449791SMatthew G. Knepley   }
130208449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
130308449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
130408449791SMatthew G. Knepley   PetscFunctionReturn(0);
130508449791SMatthew G. Knepley }
130608449791SMatthew G. Knepley 
130708449791SMatthew G. Knepley #undef __FUNCT__
130808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry"
130908449791SMatthew G. Knepley /*@C
131008449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
131108449791SMatthew G. Knepley 
131208449791SMatthew G. Knepley   Input Parameters:
131308449791SMatthew G. Knepley + dm     - The DM
131408449791SMatthew G. Knepley . fStart - The first face to include
131508449791SMatthew G. Knepley . fEnd   - The first face to exclude
131608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
131708449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
131808449791SMatthew G. Knepley 
131908449791SMatthew G. Knepley   Output Parameters:
132008449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
132108449791SMatthew G. Knepley - vol   - The cell volume
132208449791SMatthew G. Knepley 
132308449791SMatthew G. Knepley   Level: developer
132408449791SMatthew G. Knepley 
132508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
132608449791SMatthew G. Knepley @*/
132708449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
132808449791SMatthew G. Knepley {
132908449791SMatthew G. Knepley   PetscErrorCode ierr;
133008449791SMatthew G. Knepley 
133108449791SMatthew G. Knepley   PetscFunctionBegin;
133208449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
133308449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr);
133408449791SMatthew G. Knepley   PetscFunctionReturn(0);
133508449791SMatthew G. Knepley }
133608449791SMatthew G. Knepley 
133708449791SMatthew G. Knepley #undef __FUNCT__
1338cd0a786dSToby Isaac #define __FUNCT__ "DMPlexApplyLimiter_Internal"
1339655c4201SToby 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,
1340cd0a786dSToby Isaac                                                    const PetscScalar *cellgeom, const PetscFVCellGeom *cg, const PetscScalar *cx, const PetscScalar *cgrad)
1341cd0a786dSToby Isaac {
1342cd0a786dSToby Isaac   const PetscInt        *children;
1343cd0a786dSToby Isaac   PetscInt               numChildren;
1344cd0a786dSToby Isaac   PetscErrorCode         ierr;
1345cd0a786dSToby Isaac 
1346cd0a786dSToby Isaac   PetscFunctionBegin;
1347cd0a786dSToby Isaac   ierr = DMPlexGetTreeChildren(dm,face,&numChildren,&children);CHKERRQ(ierr);
1348cd0a786dSToby Isaac   if (numChildren) {
1349cd0a786dSToby Isaac     PetscInt c;
1350cd0a786dSToby Isaac 
1351cd0a786dSToby Isaac     for (c = 0; c < numChildren; c++) {
1352cd0a786dSToby Isaac       PetscInt childFace = children[c];
1353655c4201SToby Isaac 
1354655c4201SToby Isaac       if (childFace >= fStart && childFace < fEnd) {
1355655c4201SToby Isaac         ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,childFace,fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr);
1356655c4201SToby Isaac       }
1357cd0a786dSToby Isaac     }
1358cd0a786dSToby Isaac   }
1359cd0a786dSToby Isaac   else {
1360cd0a786dSToby Isaac     const PetscScalar     *ncx;
1361cd0a786dSToby Isaac     const PetscFVCellGeom *ncg;
1362cd0a786dSToby Isaac     const PetscInt        *fcells;
1363cd0a786dSToby Isaac     PetscInt               ncell, d;
1364cd0a786dSToby Isaac     PetscReal              v[3];
1365cd0a786dSToby Isaac 
1366cd0a786dSToby Isaac     ierr  = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr);
1367cd0a786dSToby Isaac     ncell = cell == fcells[0] ? fcells[1] : fcells[0];
1368cd0a786dSToby Isaac     ierr  = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr);
1369cd0a786dSToby Isaac     ierr  = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr);
1370cd0a786dSToby Isaac     DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v);
1371cd0a786dSToby Isaac     for (d = 0; d < totDim; ++d) {
1372cd0a786dSToby Isaac       /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */
1373cd0a786dSToby Isaac       PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v);
1374cd0a786dSToby Isaac 
1375cd0a786dSToby Isaac       ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr);
1376cd0a786dSToby Isaac       cellPhi[d] = PetscMin(cellPhi[d], phi);
1377cd0a786dSToby Isaac     }
1378cd0a786dSToby Isaac   }
1379cd0a786dSToby Isaac   PetscFunctionReturn(0);
1380cd0a786dSToby Isaac }
1381cd0a786dSToby Isaac 
1382cd0a786dSToby Isaac #undef __FUNCT__
138308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal"
138408449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad)
138508449791SMatthew G. Knepley {
138608449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
138708449791SMatthew G. Knepley   DMLabel            ghostLabel;
138808449791SMatthew G. Knepley   PetscDS            prob;
138908449791SMatthew G. Knepley   PetscFV            fvm;
139008449791SMatthew G. Knepley   PetscLimiter       lim;
139108449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x;
139208449791SMatthew G. Knepley   PetscScalar       *gr;
139308449791SMatthew G. Knepley   PetscReal         *cellPhi;
139408449791SMatthew G. Knepley   PetscInt           dim, face, cell, totDim, cStart, cEnd, cEndInterior;
139508449791SMatthew G. Knepley   PetscErrorCode     ierr;
139608449791SMatthew G. Knepley 
139708449791SMatthew G. Knepley   PetscFunctionBegin;
139808449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
139908449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
140008449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
140108449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
140208449791SMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr);
140308449791SMatthew G. Knepley   ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr);
140408449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
140508449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
140608449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
140708449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
140808449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
140908449791SMatthew G. Knepley   ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr);
141008449791SMatthew G. Knepley   ierr = VecZeroEntries(grad);CHKERRQ(ierr);
141108449791SMatthew G. Knepley   ierr = VecGetArray(grad, &gr);CHKERRQ(ierr);
141208449791SMatthew G. Knepley   /* Reconstruct gradients */
141308449791SMatthew G. Knepley   for (face = fStart; face < fEnd; ++face) {
141408449791SMatthew G. Knepley     const PetscInt        *cells;
141508449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
141608449791SMatthew G. Knepley     const PetscScalar     *cx[2];
141708449791SMatthew G. Knepley     PetscScalar           *cgrad[2];
141808449791SMatthew G. Knepley     PetscBool              boundary;
141950d63984SToby Isaac     PetscInt               ghost, c, pd, d, numChildren, numCells;
142008449791SMatthew G. Knepley 
142108449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
142208449791SMatthew G. Knepley     ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
142350d63984SToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &numChildren, NULL);CHKERRQ(ierr);
142450d63984SToby Isaac     if (ghost >= 0 || boundary || numChildren) continue;
142550d63984SToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &numCells);CHKERRQ(ierr);
142650d63984SToby Isaac     if (numCells != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "facet %d has %d support points: expected 2",face,numCells);
142708449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
142808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
142908449791SMatthew G. Knepley     for (c = 0; c < 2; ++c) {
143008449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr);
143108449791SMatthew G. Knepley       ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr);
143208449791SMatthew G. Knepley     }
143308449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd) {
143408449791SMatthew G. Knepley       PetscScalar delta = cx[1][pd] - cx[0][pd];
143508449791SMatthew G. Knepley 
143608449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
143708449791SMatthew G. Knepley         if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta;
143808449791SMatthew G. Knepley         if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta;
143908449791SMatthew G. Knepley       }
144008449791SMatthew G. Knepley     }
144108449791SMatthew G. Knepley   }
144208449791SMatthew G. Knepley   /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */
144308449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
144408449791SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
144508449791SMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
144608449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
144708449791SMatthew G. Knepley   for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) {
144808449791SMatthew G. Knepley     const PetscInt        *faces;
144908449791SMatthew G. Knepley     const PetscScalar     *cx;
145008449791SMatthew G. Knepley     const PetscFVCellGeom *cg;
145108449791SMatthew G. Knepley     PetscScalar           *cgrad;
145208449791SMatthew G. Knepley     PetscInt               coneSize, f, pd, d;
145308449791SMatthew G. Knepley 
145408449791SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
145508449791SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
145608449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr);
145708449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr);
145808449791SMatthew G. Knepley     ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr);
145908449791SMatthew G. Knepley     if (!cgrad) continue; /* Unowned overlap cell, we do not compute */
146008449791SMatthew G. Knepley     /* Limiter will be minimum value over all neighbors */
146108449791SMatthew G. Knepley     for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL;
146208449791SMatthew G. Knepley     for (f = 0; f < coneSize; ++f) {
1463655c4201SToby Isaac       ierr = DMPlexApplyLimiter_Internal(dm,dmCell,lim,dim,totDim,cell,faces[f],fStart,fEnd,cellPhi,x,cellgeom,cg,cx,cgrad);CHKERRQ(ierr);
146408449791SMatthew G. Knepley     }
146508449791SMatthew G. Knepley     /* Apply limiter to gradient */
146608449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd)
146708449791SMatthew G. Knepley       /* Scalar limiter applied to each component separately */
146808449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd];
146908449791SMatthew G. Knepley   }
147008449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
147108449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
147208449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
147308449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
147408449791SMatthew G. Knepley   ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr);
147508449791SMatthew G. Knepley   PetscFunctionReturn(0);
147608449791SMatthew G. Knepley }
147708449791SMatthew G. Knepley 
147808449791SMatthew G. Knepley #undef __FUNCT__
147908449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal"
148008449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user)
148108449791SMatthew G. Knepley {
148208449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
148308449791SMatthew G. Knepley   PetscSection     section;
148408449791SMatthew G. Knepley   PetscDS          prob;
148508449791SMatthew G. Knepley   DMLabel          depth;
148608449791SMatthew G. Knepley   PetscFECellGeom *cgeom;
148708449791SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *elemVec = NULL;
148808449791SMatthew G. Knepley   PetscInt         dim, Nf, f, totDimBd, numBd, bd;
148908449791SMatthew G. Knepley   PetscErrorCode   ierr;
149008449791SMatthew G. Knepley 
149108449791SMatthew G. Knepley   PetscFunctionBegin;
149208449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
149308449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
149408449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
149508449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
149608449791SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
149724cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
149824cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
149924cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
150024cdb843SMatthew G. Knepley     const char     *bdLabel;
150124cdb843SMatthew G. Knepley     DMLabel         label;
150224cdb843SMatthew G. Knepley     IS              pointIS;
150324cdb843SMatthew G. Knepley     const PetscInt *points;
150424cdb843SMatthew G. Knepley     const PetscInt *values;
150524cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
150624cdb843SMatthew G. Knepley     PetscBool       isEssential;
150724cdb843SMatthew G. Knepley 
15081878804aSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
150924cdb843SMatthew G. Knepley     if (isEssential) continue;
151024cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
151124cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
151224cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
151324cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
151424cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
151524cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
151624cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
151724cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
151824cdb843SMatthew G. Knepley     }
1519bbce034cSMatthew G. Knepley     ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
152008449791SMatthew G. Knepley     if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
152124cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
152224cdb843SMatthew G. Knepley       const PetscInt point = points[p];
152324cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
152424cdb843SMatthew G. Knepley       PetscInt       i;
152524cdb843SMatthew G. Knepley 
152624cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
152724cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1528bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
1529302440fdSBarry Smith       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr);
1530bbce034cSMatthew 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);
153108449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
153224cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
153308449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
153408449791SMatthew G. Knepley       if (locX_t) {
153508449791SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
153624cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
153708449791SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
153824cdb843SMatthew G. Knepley       }
153924cdb843SMatthew G. Knepley       ++f;
154024cdb843SMatthew G. Knepley     }
154124cdb843SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
154224cdb843SMatthew G. Knepley       PetscFE         fe;
154308449791SMatthew G. Knepley       PetscQuadrature q;
154424cdb843SMatthew G. Knepley       PetscInt        numQuadPoints, Nb;
154524cdb843SMatthew G. Knepley       /* Conforming batches */
154624cdb843SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
154724cdb843SMatthew G. Knepley       /* Remainder */
154824cdb843SMatthew G. Knepley       PetscInt        Nr, offset;
154924cdb843SMatthew G. Knepley 
155024cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
155124cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
155224cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
155324cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
155424cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
155524cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
155624cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
155724cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
155824cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
155924cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
156024cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
156124cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
1562bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
1563bbce034cSMatthew 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);
156424cdb843SMatthew G. Knepley     }
156524cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
156624cdb843SMatthew G. Knepley       const PetscInt point = points[p];
156724cdb843SMatthew G. Knepley 
156824cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
156924cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
157024cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
157108449791SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
157224cdb843SMatthew G. Knepley       ++f;
157324cdb843SMatthew G. Knepley     }
157424cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
157524cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1576bbce034cSMatthew G. Knepley     ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr);
157708449791SMatthew G. Knepley     if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
157824cdb843SMatthew G. Knepley   }
157908449791SMatthew G. Knepley   PetscFunctionReturn(0);
158008449791SMatthew G. Knepley }
158108449791SMatthew G. Knepley 
158208449791SMatthew G. Knepley #undef __FUNCT__
1583f2c5ccc7SToby Isaac #define __FUNCT__ "DMPlexReconstructGradientsFVM"
1584f2c5ccc7SToby Isaac /*@
1585f2c5ccc7SToby Isaac   DMPlexReconstructGradientsFVM - reconstruct the gradient of a vector using a finite volume method.
1586f2c5ccc7SToby Isaac 
1587f2c5ccc7SToby Isaac   Input Parameters:
1588f2c5ccc7SToby Isaac + dm - the mesh
1589f2c5ccc7SToby Isaac - locX - the local representation of the vector
1590f2c5ccc7SToby Isaac 
1591f2c5ccc7SToby Isaac   Output Parameter:
1592f2c5ccc7SToby Isaac . grad - the global representation of the gradient
1593f2c5ccc7SToby Isaac 
1594f2c5ccc7SToby Isaac   Level: developer
1595f2c5ccc7SToby Isaac 
1596f2c5ccc7SToby Isaac .seealso: DMPlexSNESGetGradientDM()
1597f2c5ccc7SToby Isaac @*/
1598f2c5ccc7SToby Isaac PetscErrorCode DMPlexReconstructGradientsFVM(DM dm, Vec locX, Vec grad)
1599f2c5ccc7SToby Isaac {
1600f2c5ccc7SToby Isaac   PetscDS          prob;
1601f2c5ccc7SToby Isaac   PetscInt         Nf, f, fStart, fEnd;
1602f2c5ccc7SToby Isaac   PetscBool        useFVM;
1603f2c5ccc7SToby Isaac   PetscFV          fvm = NULL;
1604f2c5ccc7SToby Isaac   Vec              faceGeometryFVM, cellGeometryFVM;
1605f2c5ccc7SToby Isaac   PetscFVCellGeom  *cgeomFVM   = NULL;
1606f2c5ccc7SToby Isaac   PetscFVFaceGeom  *fgeomFVM   = NULL;
1607f2c5ccc7SToby Isaac   DM               dmGrad = NULL;
1608f2c5ccc7SToby Isaac   PetscErrorCode   ierr;
1609f2c5ccc7SToby Isaac 
1610f2c5ccc7SToby Isaac   PetscFunctionBegin;
1611f2c5ccc7SToby Isaac   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1612f2c5ccc7SToby Isaac   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1613f2c5ccc7SToby Isaac   for (f = 0; f < Nf; ++f) {
1614f2c5ccc7SToby Isaac     PetscObject  obj;
1615f2c5ccc7SToby Isaac     PetscClassId id;
1616f2c5ccc7SToby Isaac 
1617f2c5ccc7SToby Isaac     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1618f2c5ccc7SToby Isaac     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1619f2c5ccc7SToby Isaac     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
1620f2c5ccc7SToby Isaac   }
1621f2c5ccc7SToby Isaac   if (!useFVM) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm does not have a finite volume discretization");
1622f2c5ccc7SToby Isaac   ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
1623f2c5ccc7SToby Isaac   if (!dmGrad) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This dm's finite volume discretization does not reconstruct gradients");
1624f2c5ccc7SToby Isaac   ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
1625f2c5ccc7SToby Isaac   ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
1626f2c5ccc7SToby Isaac   ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1627f2c5ccc7SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1628f2c5ccc7SToby Isaac   ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1629f2c5ccc7SToby Isaac   PetscFunctionReturn(0);
1630f2c5ccc7SToby Isaac }
1631f2c5ccc7SToby Isaac 
1632f2c5ccc7SToby Isaac #undef __FUNCT__
163308449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal"
1634c4d4a4f8SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
163508449791SMatthew G. Knepley {
163608449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
163708449791SMatthew G. Knepley   const char       *name       = "Residual";
163808449791SMatthew G. Knepley   DM                dmAux      = NULL;
163908449791SMatthew G. Knepley   DM                dmGrad     = NULL;
164008449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
164108449791SMatthew G. Knepley   PetscDS           prob       = NULL;
164208449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
164308449791SMatthew G. Knepley   PetscSection      section    = NULL;
164408449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
164508449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
1646b2666ceaSMatthew G. Knepley   PetscBool         isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
164708449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
164808449791SMatthew G. Knepley   PetscFECellGeom  *cgeomFEM   = NULL;
16492f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
165008449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
165108449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
165208449791SMatthew G. Knepley   Vec               locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
165308449791SMatthew G. Knepley   PetscScalar      *u, *u_t, *a, *uL, *uR;
1654c4d4a4f8SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
165508449791SMatthew G. Knepley   PetscErrorCode    ierr;
165608449791SMatthew G. Knepley 
165708449791SMatthew G. Knepley   PetscFunctionBegin;
165808449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
165908449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1660195142f5SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
166108449791SMatthew G. Knepley   /* FEM+FVM */
166208449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
166308449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
166408449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
166508449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
166608449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
166708449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
166808449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
166908449791SMatthew G. Knepley   if (locA) {
167008449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
167108449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
167208449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
167308449791SMatthew G. Knepley   }
167408449791SMatthew G. Knepley   /* 2: Get geometric data */
167508449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
167608449791SMatthew G. Knepley     PetscObject  obj;
167708449791SMatthew G. Knepley     PetscClassId id;
16787173168dSMatthew G. Knepley     PetscBool    fimp;
167908449791SMatthew G. Knepley 
16807173168dSMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
16817173168dSMatthew G. Knepley     if (isImplicit != fimp) continue;
168208449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
168308449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
168408449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
168508449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
168608449791SMatthew G. Knepley   }
168708449791SMatthew G. Knepley   if (useFEM) {
168808449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr);
16892f84e9bcSToby Isaac     ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);
16902f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
16912f84e9bcSToby Isaac       DM dmCell;
16922f84e9bcSToby Isaac       PetscInt c;
16932f84e9bcSToby Isaac 
16942f84e9bcSToby Isaac       ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr);
16952f84e9bcSToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr);
16962f84e9bcSToby Isaac       for (c = 0; c < cEnd - cStart; c++) {
16972f84e9bcSToby Isaac         PetscScalar *thisgeom;
16982f84e9bcSToby Isaac 
16992f84e9bcSToby Isaac         ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
17002f84e9bcSToby Isaac         cgeomFEM[c] = *((PetscFECellGeom *) thisgeom);
17012f84e9bcSToby Isaac       }
17022f84e9bcSToby Isaac     }
17032f84e9bcSToby Isaac     else {
17042f84e9bcSToby Isaac       cgeomFEM = (PetscFECellGeom *) cgeomScal;
17052f84e9bcSToby Isaac     }
170608449791SMatthew G. Knepley   }
170708449791SMatthew G. Knepley   if (useFVM) {
170808449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
170908449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
171008449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
171108449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
171208449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
171308449791SMatthew G. Knepley     if (dmGrad) {
171408449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
171508449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
171608449791SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
171708449791SMatthew G. Knepley       /* Communicate gradient values */
171808449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
171908449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
172008449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
172108449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
172208449791SMatthew G. Knepley     }
172308449791SMatthew G. Knepley   }
172408449791SMatthew G. Knepley   /* Handle boundary values */
172508449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
172608449791SMatthew G. Knepley   /* Loop over chunks */
172708449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
172808449791SMatthew G. Knepley   numChunks     = 1;
172908449791SMatthew G. Knepley   cellChunkSize = (cEnd - cStart)/numChunks;
173008449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
173108449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
17322eefff9cSMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
17332eefff9cSMatthew G. Knepley     PetscReal       *vol;
173408449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
173508449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
173608449791SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face;
173708449791SMatthew G. Knepley 
173808449791SMatthew G. Knepley     /* Extract field coefficients */
173908449791SMatthew G. Knepley     if (useFEM) {
174008449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
174108449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
1742215c4595SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
174308449791SMatthew G. Knepley     }
174408449791SMatthew G. Knepley     if (useFVM) {
174508449791SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
174608449791SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
174708449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
174808449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
1749215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1750215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
175108449791SMatthew G. Knepley     }
175208449791SMatthew 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 */
175308449791SMatthew G. Knepley     /* Loop over fields */
175408449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
175508449791SMatthew G. Knepley       PetscObject  obj;
175608449791SMatthew G. Knepley       PetscClassId id;
17577173168dSMatthew G. Knepley       PetscBool    fimp;
175808449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
175908449791SMatthew G. Knepley 
17607173168dSMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
17617173168dSMatthew G. Knepley       if (isImplicit != fimp) continue;
176208449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
176308449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
176408449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
176508449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
176608449791SMatthew G. Knepley         PetscQuadrature q;
176708449791SMatthew G. Knepley         PetscInt        Nq, Nb;
176808449791SMatthew G. Knepley 
176908449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
177008449791SMatthew G. Knepley 
177108449791SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
177208449791SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
177308449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
177408449791SMatthew G. Knepley         blockSize = Nb*Nq;
177508449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
177608449791SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
177708449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
177808449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
177908449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
178008449791SMatthew G. Knepley         offset    = numCells - Nr;
178108449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
178208449791SMatthew 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) */
178308449791SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
178408449791SMatthew 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);
178508449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
178608449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
178708449791SMatthew G. Knepley 
178808449791SMatthew G. Knepley         Ne = numFaces;
178908449791SMatthew G. Knepley         Nr = 0;
179008449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
179108449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
179208449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
179308449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
179408449791SMatthew G. Knepley     }
17952f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
17962f84e9bcSToby Isaac       ierr = PetscFree(cgeomFEM);CHKERRQ(ierr);
17972f84e9bcSToby Isaac     }
17982f84e9bcSToby Isaac     else {
17992f84e9bcSToby Isaac       cgeomFEM = NULL;
18002f84e9bcSToby Isaac     }
18012f84e9bcSToby Isaac     ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);
180208449791SMatthew G. Knepley     /* Loop over domain */
180308449791SMatthew G. Knepley     if (useFEM) {
180408449791SMatthew G. Knepley       /* Add elemVec to locX */
180508449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
180608449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
180708449791SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_VALUES);CHKERRQ(ierr);
180808449791SMatthew G. Knepley       }
180908449791SMatthew G. Knepley     }
181008449791SMatthew G. Knepley     if (useFVM) {
18114a394323SMatthew G. Knepley       PetscScalar *fa;
181208449791SMatthew G. Knepley       PetscInt     iface;
181308449791SMatthew G. Knepley 
181408449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1815c10b5f1bSMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1816c10b5f1bSMatthew G. Knepley         PetscFV      fv;
1817c10b5f1bSMatthew G. Knepley         PetscObject  obj;
1818c10b5f1bSMatthew G. Knepley         PetscClassId id;
18194a394323SMatthew G. Knepley         PetscInt     foff, pdim;
1820c10b5f1bSMatthew G. Knepley 
1821c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1822c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1823c10b5f1bSMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1824c10b5f1bSMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1825c10b5f1bSMatthew G. Knepley         fv   = (PetscFV) obj;
1826c10b5f1bSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1827c10b5f1bSMatthew G. Knepley         /* Accumulate fluxes to cells */
182808449791SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
182908449791SMatthew G. Knepley           const PetscInt *cells;
183008449791SMatthew G. Knepley           PetscScalar    *fL, *fR;
1831ffe9ad51SToby Isaac           PetscInt        ghost, d, nsupp;
183208449791SMatthew G. Knepley 
183308449791SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1834ffe9ad51SToby Isaac           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
1835ffe9ad51SToby Isaac           if (ghost >= 0 || nsupp > 2) continue;
183608449791SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1837c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointGlobalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr);
1838c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointGlobalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr);
1839c10b5f1bSMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
1840c10b5f1bSMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1841c10b5f1bSMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
184208449791SMatthew G. Knepley           }
184308449791SMatthew G. Knepley           ++iface;
184408449791SMatthew G. Knepley         }
1845dab51205SMatthew G. Knepley       }
1846dab51205SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1847dab51205SMatthew G. Knepley     }
1848c10b5f1bSMatthew G. Knepley     /* Handle time derivative */
1849c10b5f1bSMatthew G. Knepley     if (locX_t) {
1850dab51205SMatthew G. Knepley       PetscScalar *x_t, *fa;
1851dab51205SMatthew G. Knepley 
1852dab51205SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1853c10b5f1bSMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
1854dab51205SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1855dab51205SMatthew G. Knepley         PetscFV      fv;
1856dab51205SMatthew G. Knepley         PetscObject  obj;
1857dab51205SMatthew G. Knepley         PetscClassId id;
1858dab51205SMatthew G. Knepley         PetscInt     pdim, d;
1859dab51205SMatthew G. Knepley 
1860dab51205SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1861dab51205SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1862dab51205SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1863dab51205SMatthew G. Knepley         fv   = (PetscFV) obj;
1864dab51205SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1865c10b5f1bSMatthew G. Knepley         for (cell = cS; cell < cE; ++cell) {
1866c10b5f1bSMatthew G. Knepley           PetscScalar *u_t, *r;
1867c10b5f1bSMatthew G. Knepley 
1868c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
1869c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
1870d63b37e5SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1871c10b5f1bSMatthew G. Knepley         }
1872dab51205SMatthew G. Knepley       }
1873c10b5f1bSMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
187408449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
187508449791SMatthew G. Knepley     }
187608449791SMatthew G. Knepley     if (useFEM) {
187708449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
187808449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
187908449791SMatthew G. Knepley     }
188008449791SMatthew G. Knepley     if (useFVM) {
188108449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
188208449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
188308449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
188408449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
188508449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
188608449791SMatthew G. Knepley     }
188708449791SMatthew G. Knepley   }
188808449791SMatthew G. Knepley 
188908449791SMatthew G. Knepley   if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);}
189008449791SMatthew G. Knepley 
189108449791SMatthew G. Knepley   /* FEM */
189208449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
189308449791SMatthew G. Knepley   /* 2: Get geometric data */
189408449791SMatthew G. Knepley   /* 3: Handle boundary values */
189508449791SMatthew G. Knepley   /* 4: Loop over domain */
189608449791SMatthew G. Knepley   /*   Extract coefficients */
189708449791SMatthew G. Knepley   /* Loop over fields */
189808449791SMatthew G. Knepley   /*   Set tiling for FE*/
189908449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
190008449791SMatthew G. Knepley   /*     Loop over subdomain */
190108449791SMatthew G. Knepley   /*       Loop over quad points */
190208449791SMatthew G. Knepley   /*         Transform coords to real space */
190308449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
190408449791SMatthew G. Knepley   /*         Evaluate residual at point */
190508449791SMatthew G. Knepley   /*         Transform residual to real space */
190608449791SMatthew G. Knepley   /*       Add residual to elemVec */
190708449791SMatthew G. Knepley   /* Loop over domain */
190808449791SMatthew G. Knepley   /*   Add elemVec to locX */
190908449791SMatthew G. Knepley 
191008449791SMatthew G. Knepley   /* FVM */
191108449791SMatthew G. Knepley   /* Get geometric data */
191208449791SMatthew G. Knepley   /* If using gradients */
191308449791SMatthew G. Knepley   /*   Compute gradient data */
191408449791SMatthew G. Knepley   /*   Loop over domain faces */
191508449791SMatthew G. Knepley   /*     Count computational faces */
191608449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
191708449791SMatthew G. Knepley   /*   Loop over domain cells */
191808449791SMatthew G. Knepley   /*     Limit cell gradients */
191908449791SMatthew G. Knepley   /* Handle boundary values */
192008449791SMatthew G. Knepley   /* Loop over domain faces */
192108449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
192208449791SMatthew G. Knepley   /* Riemann solve over faces */
192308449791SMatthew G. Knepley   /* Loop over domain faces */
192408449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
192508449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
192608449791SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, locF);CHKERRQ(ierr);}
192724cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
192824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
192924cdb843SMatthew G. Knepley }
193024cdb843SMatthew G. Knepley 
193124cdb843SMatthew G. Knepley #undef __FUNCT__
193224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal"
193324cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
193424cdb843SMatthew G. Knepley {
193524cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1936bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
193724cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
193824cdb843SMatthew G. Knepley   PetscQuadrature   q;
193924cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
19402f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
19412f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
194224cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
194324cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
194424cdb843SMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
194524cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
194624cdb843SMatthew G. Knepley 
194724cdb843SMatthew G. Knepley   PetscFunctionBegin;
194824cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
194924cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
195024cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
195124cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
195224cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
195324cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
195424cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
195524cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
195624cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
195724cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
195824cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
195924cdb843SMatthew G. Knepley   if (dmAux) {
196024cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
196124cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
196224cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
196324cdb843SMatthew G. Knepley   }
196408449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
196524cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1966bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
196724cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
196824cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1969bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
19702f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
19712f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
19722f84e9bcSToby Isaac     DM dmCell;
19732f84e9bcSToby Isaac 
19742f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
19752f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
19762f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
19777726bd6bSToby Isaac       PetscScalar *thisgeom;
19782f84e9bcSToby Isaac 
19792f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
19802f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
19812f84e9bcSToby Isaac     }
19822f84e9bcSToby Isaac   }
19832f84e9bcSToby Isaac   else {
19842f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
19852f84e9bcSToby Isaac   }
198624cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
198724cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
198824cdb843SMatthew G. Knepley     PetscInt     i;
198924cdb843SMatthew G. Knepley 
199024cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
199124cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
199224cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
199324cdb843SMatthew G. Knepley     if (X_t) {
199424cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
199524cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
199624cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
199724cdb843SMatthew G. Knepley     }
199824cdb843SMatthew G. Knepley     if (dmAux) {
199924cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
200024cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
200124cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
200224cdb843SMatthew G. Knepley     }
200324cdb843SMatthew G. Knepley   }
200424cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
200524cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
200624cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
200724cdb843SMatthew G. Knepley     /* Conforming batches */
200824cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
200924cdb843SMatthew G. Knepley     /* Remainder */
201024cdb843SMatthew G. Knepley     PetscInt Nr, offset;
201124cdb843SMatthew G. Knepley 
201224cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
201324cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
201424cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
201524cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
201624cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
201724cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
201824cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
201924cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
202024cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
202124cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
202224cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
202324cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
202424cdb843SMatthew G. Knepley     offset    = numCells - Nr;
2025bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
2026bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
2027bbce034cSMatthew 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);
2028bbce034cSMatthew 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);
202924cdb843SMatthew G. Knepley   }
203024cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
203124cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
203224cdb843SMatthew G. Knepley     PetscInt  d;
203324cdb843SMatthew G. Knepley 
203424cdb843SMatthew 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;}
203524cdb843SMatthew G. Knepley     if (diff) {
203624cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
203724cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
203824cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
203924cdb843SMatthew G. Knepley       ++diffCell;
204024cdb843SMatthew G. Knepley     }
204124cdb843SMatthew G. Knepley     if (diffCell > 9) break;
204224cdb843SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
204324cdb843SMatthew G. Knepley   }
20442f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
20452f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
20462f84e9bcSToby Isaac   }
20472f84e9bcSToby Isaac   else {
20482f84e9bcSToby Isaac     cgeom = NULL;
20492f84e9bcSToby Isaac   }
20502f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2051bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
205224cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
205324cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
205424cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
205524cdb843SMatthew G. Knepley }
205624cdb843SMatthew G. Knepley 
205724cdb843SMatthew G. Knepley #undef __FUNCT__
205824cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
205924cdb843SMatthew G. Knepley /*@
206024cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
206124cdb843SMatthew G. Knepley 
206224cdb843SMatthew G. Knepley   Input Parameters:
206324cdb843SMatthew G. Knepley + dm - The mesh
206424cdb843SMatthew G. Knepley . X  - Local solution
206524cdb843SMatthew G. Knepley - user - The user context
206624cdb843SMatthew G. Knepley 
206724cdb843SMatthew G. Knepley   Output Parameter:
206824cdb843SMatthew G. Knepley . F  - Local output vector
206924cdb843SMatthew G. Knepley 
207024cdb843SMatthew G. Knepley   Level: developer
207124cdb843SMatthew G. Knepley 
207224cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
207324cdb843SMatthew G. Knepley @*/
207424cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
207524cdb843SMatthew G. Knepley {
207624cdb843SMatthew G. Knepley   PetscObject    check;
2077c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
207824cdb843SMatthew G. Knepley   PetscErrorCode ierr;
207924cdb843SMatthew G. Knepley 
208024cdb843SMatthew G. Knepley   PetscFunctionBegin;
2081c4d4a4f8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2082c4d4a4f8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2083c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
208424cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
208524cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
208624cdb843SMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
2087c4d4a4f8SMatthew G. Knepley   else       {ierr = DMPlexComputeResidual_Internal(dm, cStart, cEnd, PETSC_MIN_REAL, X, NULL, F, user);CHKERRQ(ierr);}
208824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
208924cdb843SMatthew G. Knepley }
209024cdb843SMatthew G. Knepley 
209124cdb843SMatthew G. Knepley #undef __FUNCT__
2092b953af5fSMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobian_Internal"
2093b953af5fSMatthew 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)
209424cdb843SMatthew G. Knepley {
209524cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
209624cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
209724cdb843SMatthew G. Knepley   DM                dmAux;
209824cdb843SMatthew G. Knepley   DMLabel           depth;
2099bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
210024cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
210124cdb843SMatthew G. Knepley   PetscQuadrature   quad;
210224cdb843SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
21032f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
21042f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
210524cdb843SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
2106b953af5fSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, c;
210724cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
210824cdb843SMatthew G. Knepley   PetscBool         isShell;
210924cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
211024cdb843SMatthew G. Knepley 
211124cdb843SMatthew G. Knepley   PetscFunctionBegin;
211224cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
211324cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
211424cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
211524cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
211624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
211724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
211824cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
211924cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
212024cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
212124cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
212224cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
212324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
212424cdb843SMatthew G. Knepley   if (dmAux) {
212524cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
212624cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
212724cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
212824cdb843SMatthew G. Knepley   }
212908449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
213024cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
2131bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
213224cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2133bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
21342f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
21352f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
21362f84e9bcSToby Isaac     DM dmCell;
21372f84e9bcSToby Isaac 
21382f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
21392f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
21402f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
21417726bd6bSToby Isaac       PetscScalar *thisgeom;
21422f84e9bcSToby Isaac 
21432f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
21442f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
21452f84e9bcSToby Isaac     }
21462f84e9bcSToby Isaac   }
21472f84e9bcSToby Isaac   else {
21482f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
21492f84e9bcSToby Isaac   }
215024cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
215124cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
215224cdb843SMatthew G. Knepley     PetscInt     i;
215324cdb843SMatthew G. Knepley 
215424cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
21559f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
215624cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
215724cdb843SMatthew G. Knepley     if (X_t) {
215824cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
21599f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
216024cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
216124cdb843SMatthew G. Knepley     }
216224cdb843SMatthew G. Knepley     if (dmAux) {
216324cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
21649f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
216524cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
216624cdb843SMatthew G. Knepley     }
216724cdb843SMatthew G. Knepley   }
216824cdb843SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
216924cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
217024cdb843SMatthew G. Knepley     PetscFE  fe;
217124cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
217224cdb843SMatthew G. Knepley     /* Conforming batches */
217324cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
217424cdb843SMatthew G. Knepley     /* Remainder */
217524cdb843SMatthew G. Knepley     PetscInt Nr, offset;
217624cdb843SMatthew G. Knepley 
217724cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
217824cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
217924cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
218024cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
218124cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
218224cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
218324cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
218424cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
218524cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
218624cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
218724cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
218824cdb843SMatthew G. Knepley     offset    = numCells - Nr;
218924cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2190bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
2191bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, 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);
219224cdb843SMatthew G. Knepley     }
219324cdb843SMatthew G. Knepley   }
219424cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
21959f11d433SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
21969f11d433SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
219724cdb843SMatthew G. Knepley   }
21982f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
21992f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
22002f84e9bcSToby Isaac   }
22012f84e9bcSToby Isaac   else {
22022f84e9bcSToby Isaac     cgeom = NULL;
22032f84e9bcSToby Isaac   }
22042f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2205bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr);
220624cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
220724cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
220824cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
220924cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
221024cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
221124cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
221224cdb843SMatthew G. Knepley     const char     *bdLabel;
221324cdb843SMatthew G. Knepley     DMLabel         label;
221424cdb843SMatthew G. Knepley     IS              pointIS;
221524cdb843SMatthew G. Knepley     const PetscInt *points;
221624cdb843SMatthew G. Knepley     const PetscInt *values;
221724cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
221824cdb843SMatthew G. Knepley     PetscBool       isEssential;
221924cdb843SMatthew G. Knepley 
22201878804aSMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
222124cdb843SMatthew G. Knepley     if (isEssential) continue;
222224cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
222324cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
222424cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
222524cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
222624cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
222724cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
222824cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
222924cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
223024cdb843SMatthew G. Knepley     }
2231bbce034cSMatthew G. Knepley     ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
223224cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
223324cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
223424cdb843SMatthew G. Knepley       const PetscInt point = points[p];
223524cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
223624cdb843SMatthew G. Knepley       PetscInt       i;
223724cdb843SMatthew G. Knepley 
223824cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
223924cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
2240bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
2241302440fdSBarry Smith       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);CHKERRQ(ierr);
2242bbce034cSMatthew 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);
224324cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
224424cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
224524cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
224624cdb843SMatthew G. Knepley       if (X_t) {
224724cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
224824cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
224924cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
225024cdb843SMatthew G. Knepley       }
225124cdb843SMatthew G. Knepley       ++f;
225224cdb843SMatthew G. Knepley     }
225324cdb843SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
225424cdb843SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
225524cdb843SMatthew G. Knepley       PetscFE  fe;
225624cdb843SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
225724cdb843SMatthew G. Knepley       /* Conforming batches */
225824cdb843SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
225924cdb843SMatthew G. Knepley       /* Remainder */
226024cdb843SMatthew G. Knepley       PetscInt Nr, offset;
226124cdb843SMatthew G. Knepley 
226224cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
226324cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
226424cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
226524cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
226624cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
226724cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
226824cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
226924cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
227024cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
227124cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
227224cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
227324cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
227424cdb843SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2275bbce034cSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
2276bbce034cSMatthew 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);
227724cdb843SMatthew G. Knepley       }
227824cdb843SMatthew G. Knepley     }
227924cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
228024cdb843SMatthew G. Knepley       const PetscInt point = points[p];
228124cdb843SMatthew G. Knepley 
228224cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
228324cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
228424cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
228524cdb843SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
228624cdb843SMatthew G. Knepley       ++f;
228724cdb843SMatthew G. Knepley     }
228824cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
228924cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2290bbce034cSMatthew G. Knepley     ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr);
229124cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
229224cdb843SMatthew G. Knepley   }
229324cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
229424cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
229524cdb843SMatthew G. Knepley   if (mesh->printFEM) {
229624cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
229724cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
229824cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
229924cdb843SMatthew G. Knepley   }
230024cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
230124cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
230224cdb843SMatthew G. Knepley   if (isShell) {
230324cdb843SMatthew G. Knepley     JacActionCtx *jctx;
230424cdb843SMatthew G. Knepley 
230524cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
230624cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
230724cdb843SMatthew G. Knepley   }
230824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
230924cdb843SMatthew G. Knepley }
231024cdb843SMatthew G. Knepley 
231124cdb843SMatthew G. Knepley #undef __FUNCT__
231224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
231324cdb843SMatthew G. Knepley /*@
231424cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
231524cdb843SMatthew G. Knepley 
231624cdb843SMatthew G. Knepley   Input Parameters:
231724cdb843SMatthew G. Knepley + dm - The mesh
231824cdb843SMatthew G. Knepley . X  - Local input vector
231924cdb843SMatthew G. Knepley - user - The user context
232024cdb843SMatthew G. Knepley 
232124cdb843SMatthew G. Knepley   Output Parameter:
232224cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
232324cdb843SMatthew G. Knepley 
232424cdb843SMatthew G. Knepley   Note:
232524cdb843SMatthew G. Knepley   The first member of the user context must be an FEMContext.
232624cdb843SMatthew G. Knepley 
232724cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
232824cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
232924cdb843SMatthew G. Knepley 
233024cdb843SMatthew G. Knepley   Level: developer
233124cdb843SMatthew G. Knepley 
233224cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
233324cdb843SMatthew G. Knepley @*/
233424cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
233524cdb843SMatthew G. Knepley {
2336b953af5fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
233724cdb843SMatthew G. Knepley   PetscErrorCode ierr;
233824cdb843SMatthew G. Knepley 
233924cdb843SMatthew G. Knepley   PetscFunctionBegin;
2340b953af5fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2341b953af5fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2342b953af5fSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2343b953af5fSMatthew G. Knepley   ierr = DMPlexComputeJacobian_Internal(dm, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
234424cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
234524cdb843SMatthew G. Knepley }
23461878804aSMatthew G. Knepley 
23471878804aSMatthew G. Knepley #undef __FUNCT__
23481878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions_Internal"
2349ad917190SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, Vec sol, PetscErrorCode (**exactFuncs)(PetscInt, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs)
23501878804aSMatthew G. Knepley {
23511878804aSMatthew G. Knepley   Mat            J, M;
23521878804aSMatthew G. Knepley   Vec            r, b;
23531878804aSMatthew G. Knepley   MatNullSpace   nullSpace;
23541878804aSMatthew G. Knepley   PetscReal     *error, res = 0.0;
23551878804aSMatthew G. Knepley   PetscInt       numFields;
23561878804aSMatthew G. Knepley   PetscErrorCode ierr;
23571878804aSMatthew G. Knepley 
23581878804aSMatthew G. Knepley   PetscFunctionBegin;
23591878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &r);CHKERRQ(ierr);
23601878804aSMatthew G. Knepley   ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr);
23611878804aSMatthew G. Knepley   M    = J;
23621878804aSMatthew G. Knepley   /* TODO Null space for J */
23631878804aSMatthew G. Knepley   /* Check discretization error */
23641878804aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
23651878804aSMatthew G. Knepley   ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr);
23661878804aSMatthew G. Knepley   if (numFields > 1) {
23671878804aSMatthew G. Knepley     PetscInt f;
23681878804aSMatthew G. Knepley 
23691878804aSMatthew G. Knepley     ierr = DMPlexComputeL2FieldDiff(dm, exactFuncs, ctxs, u, error);CHKERRQ(ierr);
23701878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr);
23711878804aSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
23721878804aSMatthew G. Knepley       if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);}
23731878804aSMatthew G. Knepley       if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);}
23741878804aSMatthew G. Knepley       else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);}
23751878804aSMatthew G. Knepley     }
23761878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr);
23771878804aSMatthew G. Knepley   } else {
23781878804aSMatthew G. Knepley     ierr = DMPlexComputeL2Diff(dm, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr);
23791878804aSMatthew G. Knepley     if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);}
23801878804aSMatthew G. Knepley     else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);}
23811878804aSMatthew G. Knepley   }
23821878804aSMatthew G. Knepley   ierr = PetscFree(error);CHKERRQ(ierr);
23831878804aSMatthew G. Knepley   /* Check residual */
23841878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr);
23851878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
23861878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr);
23871878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
23881878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr);
2389685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr);
2390685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
23911878804aSMatthew G. Knepley   /* Check Jacobian */
23921878804aSMatthew G. Knepley   ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr);
23931878804aSMatthew G. Knepley   ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr);
23941878804aSMatthew G. Knepley   if (nullSpace) {
23951878804aSMatthew G. Knepley     PetscBool isNull;
23961878804aSMatthew G. Knepley     ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr);
23971878804aSMatthew G. Knepley     if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
23981878804aSMatthew G. Knepley   }
23991878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &b);CHKERRQ(ierr);
24001878804aSMatthew G. Knepley   ierr = VecSet(r, 0.0);CHKERRQ(ierr);
24011878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr);
24021878804aSMatthew G. Knepley   ierr = MatMult(M, u, r);CHKERRQ(ierr);
24031878804aSMatthew G. Knepley   ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr);
24041878804aSMatthew G. Knepley   ierr = VecDestroy(&b);CHKERRQ(ierr);
24051878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
24061878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr);
24071878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
24081878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr);
2409685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr);
2410685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
24111878804aSMatthew G. Knepley   ierr = VecDestroy(&r);CHKERRQ(ierr);
24121878804aSMatthew G. Knepley   ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
24131878804aSMatthew G. Knepley   ierr = MatDestroy(&J);CHKERRQ(ierr);
24141878804aSMatthew G. Knepley   PetscFunctionReturn(0);
24151878804aSMatthew G. Knepley }
24161878804aSMatthew G. Knepley 
24171878804aSMatthew G. Knepley #undef __FUNCT__
24181878804aSMatthew G. Knepley #define __FUNCT__ "DMSNESCheckFromOptions"
2419ad917190SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
24201878804aSMatthew G. Knepley {
24211878804aSMatthew G. Knepley   DM             dm;
24221878804aSMatthew G. Knepley   Vec            sol;
24231878804aSMatthew G. Knepley   PetscBool      check;
24241878804aSMatthew G. Knepley   PetscErrorCode ierr;
24251878804aSMatthew G. Knepley 
24261878804aSMatthew G. Knepley   PetscFunctionBegin;
24271878804aSMatthew G. Knepley   ierr = PetscOptionsHasName(snes->hdr.prefix, "-dmsnes_check", &check);CHKERRQ(ierr);
24281878804aSMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
24291878804aSMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
24301878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
24311878804aSMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
24321878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
24331878804aSMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2434552f7358SJed Brown   PetscFunctionReturn(0);
2435552f7358SJed Brown }
2436