xref: /petsc/src/snes/utils/dmplexsnes.c (revision 2eefff9c57a3c32dff51ff993ae69249be05fcf3)
124cdb843SMatthew G. Knepley #include <petsc-private/dmpleximpl.h>   /*I "petscdmplex.h" I*/
27d4028c8SMatthew G. Knepley #include <petsc-private/snesimpl.h>     /*I "petscsnes.h"   I*/
324cdb843SMatthew G. Knepley #include <petscds.h>
4afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>
5552f7358SJed Brown 
624cdb843SMatthew G. Knepley /************************** Interpolation *******************************/
724cdb843SMatthew G. Knepley 
8552f7358SJed Brown #undef __FUNCT__
9552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate"
100adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
110adebc6cSBarry Smith {
12552f7358SJed Brown   PetscErrorCode ierr;
13552f7358SJed Brown 
14552f7358SJed Brown   PetscFunctionBegin;
15552f7358SJed Brown   PetscValidPointer(ctx, 2);
16552f7358SJed Brown   ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr);
171aa26658SKarl Rupp 
18552f7358SJed Brown   (*ctx)->comm   = comm;
19552f7358SJed Brown   (*ctx)->dim    = -1;
20552f7358SJed Brown   (*ctx)->nInput = 0;
210298fd71SBarry Smith   (*ctx)->points = NULL;
220298fd71SBarry Smith   (*ctx)->cells  = NULL;
23552f7358SJed Brown   (*ctx)->n      = -1;
240298fd71SBarry Smith   (*ctx)->coords = NULL;
25552f7358SJed Brown   PetscFunctionReturn(0);
26552f7358SJed Brown }
27552f7358SJed Brown 
28552f7358SJed Brown #undef __FUNCT__
29552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim"
300adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
310adebc6cSBarry Smith {
32552f7358SJed Brown   PetscFunctionBegin;
330adebc6cSBarry Smith   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
34552f7358SJed Brown   ctx->dim = dim;
35552f7358SJed Brown   PetscFunctionReturn(0);
36552f7358SJed Brown }
37552f7358SJed Brown 
38552f7358SJed Brown #undef __FUNCT__
39552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim"
400adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
410adebc6cSBarry Smith {
42552f7358SJed Brown   PetscFunctionBegin;
43552f7358SJed Brown   PetscValidIntPointer(dim, 2);
44552f7358SJed Brown   *dim = ctx->dim;
45552f7358SJed Brown   PetscFunctionReturn(0);
46552f7358SJed Brown }
47552f7358SJed Brown 
48552f7358SJed Brown #undef __FUNCT__
49552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof"
500adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
510adebc6cSBarry Smith {
52552f7358SJed Brown   PetscFunctionBegin;
530adebc6cSBarry Smith   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
54552f7358SJed Brown   ctx->dof = dof;
55552f7358SJed Brown   PetscFunctionReturn(0);
56552f7358SJed Brown }
57552f7358SJed Brown 
58552f7358SJed Brown #undef __FUNCT__
59552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof"
600adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
610adebc6cSBarry Smith {
62552f7358SJed Brown   PetscFunctionBegin;
63552f7358SJed Brown   PetscValidIntPointer(dof, 2);
64552f7358SJed Brown   *dof = ctx->dof;
65552f7358SJed Brown   PetscFunctionReturn(0);
66552f7358SJed Brown }
67552f7358SJed Brown 
68552f7358SJed Brown #undef __FUNCT__
69552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints"
700adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
710adebc6cSBarry Smith {
72552f7358SJed Brown   PetscErrorCode ierr;
73552f7358SJed Brown 
74552f7358SJed Brown   PetscFunctionBegin;
750adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
760adebc6cSBarry Smith   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
77552f7358SJed Brown   ctx->nInput = n;
781aa26658SKarl Rupp 
79785e854fSJed Brown   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
80552f7358SJed Brown   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
81552f7358SJed Brown   PetscFunctionReturn(0);
82552f7358SJed Brown }
83552f7358SJed Brown 
84552f7358SJed Brown #undef __FUNCT__
85552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp"
860adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
870adebc6cSBarry Smith {
88552f7358SJed Brown   MPI_Comm       comm = ctx->comm;
89552f7358SJed Brown   PetscScalar    *a;
90552f7358SJed Brown   PetscInt       p, q, i;
91552f7358SJed Brown   PetscMPIInt    rank, size;
92552f7358SJed Brown   PetscErrorCode ierr;
93552f7358SJed Brown   Vec            pointVec;
94552f7358SJed Brown   IS             cellIS;
95552f7358SJed Brown   PetscLayout    layout;
96552f7358SJed Brown   PetscReal      *globalPoints;
97cb313848SJed Brown   PetscScalar    *globalPointsScalar;
98552f7358SJed Brown   const PetscInt *ranges;
99552f7358SJed Brown   PetscMPIInt    *counts, *displs;
100552f7358SJed Brown   const PetscInt *foundCells;
101552f7358SJed Brown   PetscMPIInt    *foundProcs, *globalProcs;
10219436ca2SJed Brown   PetscInt       n, N;
103552f7358SJed Brown 
10419436ca2SJed Brown   PetscFunctionBegin;
10519436ca2SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10619436ca2SJed Brown   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
10719436ca2SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1080adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
10919436ca2SJed Brown   /* Locate points */
11019436ca2SJed Brown   n = ctx->nInput;
111552f7358SJed Brown   if (!redundantPoints) {
112552f7358SJed Brown     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
113552f7358SJed Brown     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
114552f7358SJed Brown     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
115552f7358SJed Brown     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
116552f7358SJed Brown     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
117552f7358SJed Brown     /* Communicate all points to all processes */
118dcca6d9dSJed Brown     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
119552f7358SJed Brown     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
120552f7358SJed Brown     for (p = 0; p < size; ++p) {
121552f7358SJed Brown       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
122552f7358SJed Brown       displs[p] = ranges[p]*ctx->dim;
123552f7358SJed Brown     }
124552f7358SJed Brown     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
125552f7358SJed Brown   } else {
126552f7358SJed Brown     N = n;
127552f7358SJed Brown     globalPoints = ctx->points;
12838ea73c8SJed Brown     counts = displs = NULL;
12938ea73c8SJed Brown     layout = NULL;
130552f7358SJed Brown   }
131552f7358SJed Brown #if 0
132dcca6d9dSJed Brown   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
13319436ca2SJed Brown   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
134552f7358SJed Brown #else
135cb313848SJed Brown #if defined(PETSC_USE_COMPLEX)
136785e854fSJed Brown   ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr);
137cb313848SJed Brown   for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i];
138cb313848SJed Brown #else
139cb313848SJed Brown   globalPointsScalar = globalPoints;
140cb313848SJed Brown #endif
14104706141SMatthew G Knepley   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
142dcca6d9dSJed Brown   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
143552f7358SJed Brown   ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr);
144552f7358SJed Brown   ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr);
145552f7358SJed Brown #endif
146552f7358SJed Brown   for (p = 0; p < N; ++p) {
1471aa26658SKarl Rupp     if (foundCells[p] >= 0) foundProcs[p] = rank;
1481aa26658SKarl Rupp     else foundProcs[p] = size;
149552f7358SJed Brown   }
150552f7358SJed Brown   /* Let the lowest rank process own each point */
151efab3cc2SJed Brown   ierr   = MPI_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
152552f7358SJed Brown   ctx->n = 0;
153552f7358SJed Brown   for (p = 0; p < N; ++p) {
1540adebc6cSBarry 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);
1551aa26658SKarl Rupp     else if (globalProcs[p] == rank) ctx->n++;
156552f7358SJed Brown   }
157552f7358SJed Brown   /* Create coordinates vector and array of owned cells */
158785e854fSJed Brown   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
159552f7358SJed Brown   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
160552f7358SJed Brown   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
161552f7358SJed Brown   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
162c0dedaeaSBarry Smith   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
163552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
164552f7358SJed Brown   for (p = 0, q = 0, i = 0; p < N; ++p) {
165552f7358SJed Brown     if (globalProcs[p] == rank) {
166552f7358SJed Brown       PetscInt d;
167552f7358SJed Brown 
1681aa26658SKarl Rupp       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
169552f7358SJed Brown       ctx->cells[q++] = foundCells[p];
170552f7358SJed Brown     }
171552f7358SJed Brown   }
172552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
173552f7358SJed Brown #if 0
174552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
175552f7358SJed Brown #else
176552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
177552f7358SJed Brown   ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr);
178552f7358SJed Brown   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
179552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
180552f7358SJed Brown #endif
181cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
182d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
183552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
184552f7358SJed Brown   PetscFunctionReturn(0);
185552f7358SJed Brown }
186552f7358SJed Brown 
187552f7358SJed Brown #undef __FUNCT__
188552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates"
1890adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
1900adebc6cSBarry Smith {
191552f7358SJed Brown   PetscFunctionBegin;
192552f7358SJed Brown   PetscValidPointer(coordinates, 2);
1930adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
194552f7358SJed Brown   *coordinates = ctx->coords;
195552f7358SJed Brown   PetscFunctionReturn(0);
196552f7358SJed Brown }
197552f7358SJed Brown 
198552f7358SJed Brown #undef __FUNCT__
199552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector"
2000adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2010adebc6cSBarry Smith {
202552f7358SJed Brown   PetscErrorCode ierr;
203552f7358SJed Brown 
204552f7358SJed Brown   PetscFunctionBegin;
205552f7358SJed Brown   PetscValidPointer(v, 2);
2060adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
207552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
208552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
209552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
210c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
211552f7358SJed Brown   PetscFunctionReturn(0);
212552f7358SJed Brown }
213552f7358SJed Brown 
214552f7358SJed Brown #undef __FUNCT__
215552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector"
2160adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2170adebc6cSBarry Smith {
218552f7358SJed Brown   PetscErrorCode ierr;
219552f7358SJed Brown 
220552f7358SJed Brown   PetscFunctionBegin;
221552f7358SJed Brown   PetscValidPointer(v, 2);
2220adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
223552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
224552f7358SJed Brown   PetscFunctionReturn(0);
225552f7358SJed Brown }
226552f7358SJed Brown 
227552f7358SJed Brown #undef __FUNCT__
2287a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private"
2297a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
230a6dfd86eSKarl Rupp {
231552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
232552f7358SJed Brown   PetscScalar    *a, *coords;
233552f7358SJed Brown   PetscInt       p;
234552f7358SJed Brown   PetscErrorCode ierr;
235552f7358SJed Brown 
236552f7358SJed Brown   PetscFunctionBegin;
237dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
238552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
239552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
240552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
241552f7358SJed Brown     PetscInt     c = ctx->cells[p];
242a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
243552f7358SJed Brown     PetscReal    xi[4];
244552f7358SJed Brown     PetscInt     d, f, comp;
245552f7358SJed Brown 
2468e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
247552f7358SJed Brown     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2480298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2491aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2501aa26658SKarl Rupp 
251552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
252552f7358SJed Brown       xi[d] = 0.0;
2531aa26658SKarl 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]);
2541aa26658SKarl 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];
255552f7358SJed Brown     }
2560298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
257552f7358SJed Brown   }
258552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
259552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
260552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
261552f7358SJed Brown   PetscFunctionReturn(0);
262552f7358SJed Brown }
263552f7358SJed Brown 
264552f7358SJed Brown #undef __FUNCT__
2657a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private"
2667a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
2677a1931ceSMatthew G. Knepley {
2687a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
2697a1931ceSMatthew G. Knepley   PetscScalar    *a, *coords;
2707a1931ceSMatthew G. Knepley   PetscInt       p;
2717a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
2727a1931ceSMatthew G. Knepley 
2737a1931ceSMatthew G. Knepley   PetscFunctionBegin;
274dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
2757a1931ceSMatthew G. Knepley   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
2767a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
2777a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
2787a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
2797a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
2802584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
2817a1931ceSMatthew G. Knepley     PetscReal      xi[4];
2827a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
2837a1931ceSMatthew G. Knepley 
2848e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2857a1931ceSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2867a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2877a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2887a1931ceSMatthew G. Knepley 
2897a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
2907a1931ceSMatthew G. Knepley       xi[d] = 0.0;
2917a1931ceSMatthew 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]);
2927a1931ceSMatthew 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];
2937a1931ceSMatthew G. Knepley     }
2947a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2957a1931ceSMatthew G. Knepley   }
2967a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
2977a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
2987a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
2997a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3007a1931ceSMatthew G. Knepley }
3017a1931ceSMatthew G. Knepley 
3027a1931ceSMatthew G. Knepley #undef __FUNCT__
303552f7358SJed Brown #define __FUNCT__ "QuadMap_Private"
3045820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
305552f7358SJed Brown {
306552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
307552f7358SJed Brown   const PetscScalar x0        = vertices[0];
308552f7358SJed Brown   const PetscScalar y0        = vertices[1];
309552f7358SJed Brown   const PetscScalar x1        = vertices[2];
310552f7358SJed Brown   const PetscScalar y1        = vertices[3];
311552f7358SJed Brown   const PetscScalar x2        = vertices[4];
312552f7358SJed Brown   const PetscScalar y2        = vertices[5];
313552f7358SJed Brown   const PetscScalar x3        = vertices[6];
314552f7358SJed Brown   const PetscScalar y3        = vertices[7];
315552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
316552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
317552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
318552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
319552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
320552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
321552f7358SJed Brown   PetscScalar       *ref, *real;
322552f7358SJed Brown   PetscErrorCode    ierr;
323552f7358SJed Brown 
324552f7358SJed Brown   PetscFunctionBegin;
325552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
326552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
327552f7358SJed Brown   {
328552f7358SJed Brown     const PetscScalar p0 = ref[0];
329552f7358SJed Brown     const PetscScalar p1 = ref[1];
330552f7358SJed Brown 
331552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
332552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
333552f7358SJed Brown   }
334552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
335552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
336552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
337552f7358SJed Brown   PetscFunctionReturn(0);
338552f7358SJed Brown }
339552f7358SJed Brown 
340c0dedaeaSBarry Smith #include <petsc-private/dmimpl.h>
341552f7358SJed Brown #undef __FUNCT__
342552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private"
343d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
344552f7358SJed Brown {
345552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
346552f7358SJed Brown   const PetscScalar x0        = vertices[0];
347552f7358SJed Brown   const PetscScalar y0        = vertices[1];
348552f7358SJed Brown   const PetscScalar x1        = vertices[2];
349552f7358SJed Brown   const PetscScalar y1        = vertices[3];
350552f7358SJed Brown   const PetscScalar x2        = vertices[4];
351552f7358SJed Brown   const PetscScalar y2        = vertices[5];
352552f7358SJed Brown   const PetscScalar x3        = vertices[6];
353552f7358SJed Brown   const PetscScalar y3        = vertices[7];
354552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
355552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
356552f7358SJed Brown   PetscScalar       *ref;
357552f7358SJed Brown   PetscErrorCode    ierr;
358552f7358SJed Brown 
359552f7358SJed Brown   PetscFunctionBegin;
360552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
361552f7358SJed Brown   {
362552f7358SJed Brown     const PetscScalar x       = ref[0];
363552f7358SJed Brown     const PetscScalar y       = ref[1];
364552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
365da80777bSKarl Rupp     PetscScalar       values[4];
366da80777bSKarl Rupp 
367da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
368da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
36994ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
370552f7358SJed Brown   }
371552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
372552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
37394ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
37494ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
375552f7358SJed Brown   PetscFunctionReturn(0);
376552f7358SJed Brown }
377552f7358SJed Brown 
378552f7358SJed Brown #undef __FUNCT__
379552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private"
380a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
381a6dfd86eSKarl Rupp {
382fafc0619SMatthew G Knepley   DM             dmCoord;
383552f7358SJed Brown   SNES           snes;
384552f7358SJed Brown   KSP            ksp;
385552f7358SJed Brown   PC             pc;
386552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
387552f7358SJed Brown   Mat            J;
388552f7358SJed Brown   PetscScalar    *a, *coords;
389552f7358SJed Brown   PetscInt       p;
390552f7358SJed Brown   PetscErrorCode ierr;
391552f7358SJed Brown 
392552f7358SJed Brown   PetscFunctionBegin;
393552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
394fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
395552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
396552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
397552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
398552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
399c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
400552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
401552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
402552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
403552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
404552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
405552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4060298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4070298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
408552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
409552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
410552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
411552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
412552f7358SJed Brown 
413552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
414552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
415552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
416a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
417552f7358SJed Brown     PetscScalar *xi;
418cb313848SJed Brown     PetscReal    xir[2];
419552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
420552f7358SJed Brown 
421552f7358SJed Brown     /* Can make this do all points at once */
4220298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4230adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4240298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4250adebc6cSBarry Smith     if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof);
4260298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4270298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
428552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
429552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
430552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
431552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
432552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
433552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
434cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
435cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4361aa26658SKarl 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];
4371aa26658SKarl Rupp 
438552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4390298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4400298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
441552f7358SJed Brown   }
442552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
443552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
444552f7358SJed Brown 
445552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
446552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
447552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
448552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
449552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
450552f7358SJed Brown   PetscFunctionReturn(0);
451552f7358SJed Brown }
452552f7358SJed Brown 
453552f7358SJed Brown #undef __FUNCT__
454552f7358SJed Brown #define __FUNCT__ "HexMap_Private"
4555820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
456552f7358SJed Brown {
457552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
458552f7358SJed Brown   const PetscScalar x0        = vertices[0];
459552f7358SJed Brown   const PetscScalar y0        = vertices[1];
460552f7358SJed Brown   const PetscScalar z0        = vertices[2];
4617a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
4627a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
4637a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
464552f7358SJed Brown   const PetscScalar x2        = vertices[6];
465552f7358SJed Brown   const PetscScalar y2        = vertices[7];
466552f7358SJed Brown   const PetscScalar z2        = vertices[8];
4677a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
4687a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
4697a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
470552f7358SJed Brown   const PetscScalar x4        = vertices[12];
471552f7358SJed Brown   const PetscScalar y4        = vertices[13];
472552f7358SJed Brown   const PetscScalar z4        = vertices[14];
473552f7358SJed Brown   const PetscScalar x5        = vertices[15];
474552f7358SJed Brown   const PetscScalar y5        = vertices[16];
475552f7358SJed Brown   const PetscScalar z5        = vertices[17];
476552f7358SJed Brown   const PetscScalar x6        = vertices[18];
477552f7358SJed Brown   const PetscScalar y6        = vertices[19];
478552f7358SJed Brown   const PetscScalar z6        = vertices[20];
479552f7358SJed Brown   const PetscScalar x7        = vertices[21];
480552f7358SJed Brown   const PetscScalar y7        = vertices[22];
481552f7358SJed Brown   const PetscScalar z7        = vertices[23];
482552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
483552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
484552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
485552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
486552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
487552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
488552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
489552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
490552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
491552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
492552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
493552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
494552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
495552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
496552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
497552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
498552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
499552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
500552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
501552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
502552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
503552f7358SJed Brown   PetscScalar       *ref, *real;
504552f7358SJed Brown   PetscErrorCode    ierr;
505552f7358SJed Brown 
506552f7358SJed Brown   PetscFunctionBegin;
507552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
508552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
509552f7358SJed Brown   {
510552f7358SJed Brown     const PetscScalar p0 = ref[0];
511552f7358SJed Brown     const PetscScalar p1 = ref[1];
512552f7358SJed Brown     const PetscScalar p2 = ref[2];
513552f7358SJed Brown 
514552f7358SJed 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;
515552f7358SJed 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;
516552f7358SJed 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;
517552f7358SJed Brown   }
518552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
519552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
520552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
521552f7358SJed Brown   PetscFunctionReturn(0);
522552f7358SJed Brown }
523552f7358SJed Brown 
524552f7358SJed Brown #undef __FUNCT__
525552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private"
526d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
527552f7358SJed Brown {
528552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
529552f7358SJed Brown   const PetscScalar x0        = vertices[0];
530552f7358SJed Brown   const PetscScalar y0        = vertices[1];
531552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5327a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5337a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5347a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
535552f7358SJed Brown   const PetscScalar x2        = vertices[6];
536552f7358SJed Brown   const PetscScalar y2        = vertices[7];
537552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5387a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5397a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5407a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
541552f7358SJed Brown   const PetscScalar x4        = vertices[12];
542552f7358SJed Brown   const PetscScalar y4        = vertices[13];
543552f7358SJed Brown   const PetscScalar z4        = vertices[14];
544552f7358SJed Brown   const PetscScalar x5        = vertices[15];
545552f7358SJed Brown   const PetscScalar y5        = vertices[16];
546552f7358SJed Brown   const PetscScalar z5        = vertices[17];
547552f7358SJed Brown   const PetscScalar x6        = vertices[18];
548552f7358SJed Brown   const PetscScalar y6        = vertices[19];
549552f7358SJed Brown   const PetscScalar z6        = vertices[20];
550552f7358SJed Brown   const PetscScalar x7        = vertices[21];
551552f7358SJed Brown   const PetscScalar y7        = vertices[22];
552552f7358SJed Brown   const PetscScalar z7        = vertices[23];
553552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
554552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
555552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
556552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
557552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
558552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
559552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
560552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
561552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
562552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
563552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
564552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
565552f7358SJed Brown   PetscScalar       *ref;
566552f7358SJed Brown   PetscErrorCode    ierr;
567552f7358SJed Brown 
568552f7358SJed Brown   PetscFunctionBegin;
569552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
570552f7358SJed Brown   {
571552f7358SJed Brown     const PetscScalar x       = ref[0];
572552f7358SJed Brown     const PetscScalar y       = ref[1];
573552f7358SJed Brown     const PetscScalar z       = ref[2];
574552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
575da80777bSKarl Rupp     PetscScalar       values[9];
576da80777bSKarl Rupp 
577da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
578da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
579da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
580da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
581da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
582da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
583da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
584da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
585da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
5861aa26658SKarl Rupp 
58794ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
588552f7358SJed Brown   }
589552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
590552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
59194ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
59294ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
593552f7358SJed Brown   PetscFunctionReturn(0);
594552f7358SJed Brown }
595552f7358SJed Brown 
596552f7358SJed Brown #undef __FUNCT__
597552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private"
598a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
599a6dfd86eSKarl Rupp {
600fafc0619SMatthew G Knepley   DM             dmCoord;
601552f7358SJed Brown   SNES           snes;
602552f7358SJed Brown   KSP            ksp;
603552f7358SJed Brown   PC             pc;
604552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
605552f7358SJed Brown   Mat            J;
606552f7358SJed Brown   PetscScalar    *a, *coords;
607552f7358SJed Brown   PetscInt       p;
608552f7358SJed Brown   PetscErrorCode ierr;
609552f7358SJed Brown 
610552f7358SJed Brown   PetscFunctionBegin;
611552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
612fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
613552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
614552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
615552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
616552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
617c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
618552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
619552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
620552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
621552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
622552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
623552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6240298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6250298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
626552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
627552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
628552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
629552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
630552f7358SJed Brown 
631552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
632552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
633552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
634a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
635552f7358SJed Brown     PetscScalar *xi;
636cb313848SJed Brown     PetscReal    xir[3];
637552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
638552f7358SJed Brown 
639552f7358SJed Brown     /* Can make this do all points at once */
6400298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6410adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6420298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6430adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6440298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6450298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
646552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
647552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
648552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
649552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
650552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
651552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
652552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
653cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
654cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
655cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
656552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
657552f7358SJed Brown       a[p*ctx->dof+comp] =
658cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
6597a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
660cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
6617a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
662cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
663cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
664cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
665cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
666552f7358SJed Brown     }
667552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
6680298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6690298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
670552f7358SJed Brown   }
671552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
672552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
673552f7358SJed Brown 
674552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
675552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
676552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
677552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
678552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
679552f7358SJed Brown   PetscFunctionReturn(0);
680552f7358SJed Brown }
681552f7358SJed Brown 
682552f7358SJed Brown #undef __FUNCT__
683552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate"
684552f7358SJed Brown /*
685552f7358SJed Brown   Input Parameters:
686552f7358SJed Brown + ctx - The DMInterpolationInfo context
687552f7358SJed Brown . dm  - The DM
688552f7358SJed Brown - x   - The local vector containing the field to be interpolated
689552f7358SJed Brown 
690552f7358SJed Brown   Output Parameters:
691552f7358SJed Brown . v   - The vector containing the interpolated values
692552f7358SJed Brown */
6930adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
6940adebc6cSBarry Smith {
695552f7358SJed Brown   PetscInt       dim, coneSize, n;
696552f7358SJed Brown   PetscErrorCode ierr;
697552f7358SJed Brown 
698552f7358SJed Brown   PetscFunctionBegin;
699552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
700552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
701552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
702552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7030adebc6cSBarry 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);
704552f7358SJed Brown   if (n) {
705c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
706552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
707552f7358SJed Brown     if (dim == 2) {
708552f7358SJed Brown       if (coneSize == 3) {
7097a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
710552f7358SJed Brown       } else if (coneSize == 4) {
711552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7120adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
713552f7358SJed Brown     } else if (dim == 3) {
714552f7358SJed Brown       if (coneSize == 4) {
7157a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
716552f7358SJed Brown       } else {
717552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
718552f7358SJed Brown       }
7190adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
720552f7358SJed Brown   }
721552f7358SJed Brown   PetscFunctionReturn(0);
722552f7358SJed Brown }
723552f7358SJed Brown 
724552f7358SJed Brown #undef __FUNCT__
725552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy"
7260adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7270adebc6cSBarry Smith {
728552f7358SJed Brown   PetscErrorCode ierr;
729552f7358SJed Brown 
730552f7358SJed Brown   PetscFunctionBegin;
731552f7358SJed Brown   PetscValidPointer(ctx, 2);
732552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
733552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
734552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
735552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7360298fd71SBarry Smith   *ctx = NULL;
737552f7358SJed Brown   PetscFunctionReturn(0);
738552f7358SJed Brown }
739cc0c4584SMatthew G. Knepley 
740cc0c4584SMatthew G. Knepley #undef __FUNCT__
741cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields"
742cc0c4584SMatthew G. Knepley /*@C
743cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
744cc0c4584SMatthew G. Knepley 
745cc0c4584SMatthew G. Knepley   Collective on SNES
746cc0c4584SMatthew G. Knepley 
747cc0c4584SMatthew G. Knepley   Input Parameters:
748cc0c4584SMatthew G. Knepley + snes   - the SNES context
749cc0c4584SMatthew G. Knepley . its    - iteration number
750cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
751cc0c4584SMatthew G. Knepley - dummy  - unused context
752cc0c4584SMatthew G. Knepley 
753cc0c4584SMatthew G. Knepley   Notes:
754cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
755cc0c4584SMatthew G. Knepley 
756cc0c4584SMatthew G. Knepley   Level: intermediate
757cc0c4584SMatthew G. Knepley 
758cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
759cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
760cc0c4584SMatthew G. Knepley @*/
761cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy)
762cc0c4584SMatthew G. Knepley {
763cc0c4584SMatthew G. Knepley   PetscViewer        viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) snes));
764cc0c4584SMatthew G. Knepley   Vec                res;
765cc0c4584SMatthew G. Knepley   DM                 dm;
766cc0c4584SMatthew G. Knepley   PetscSection       s;
767cc0c4584SMatthew G. Knepley   const PetscScalar *r;
768cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
769cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
770cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
771cc0c4584SMatthew G. Knepley 
772cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
773cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
774cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
775cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
776cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
777cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
778cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
779cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
780cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
781cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
782cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
783cc0c4584SMatthew G. Knepley 
784cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
785cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
786cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
787cc0c4584SMatthew G. Knepley     }
788cc0c4584SMatthew G. Knepley   }
789cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
790cc0c4584SMatthew G. Knepley   ierr = MPI_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
791cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
792cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
793cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
794cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
795cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
796cc0c4584SMatthew G. Knepley   }
797cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
798cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
799cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
800cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
801cc0c4584SMatthew G. Knepley }
80224cdb843SMatthew G. Knepley 
80324cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
80424cdb843SMatthew G. Knepley 
80524cdb843SMatthew G. Knepley #undef __FUNCT__
8067d4028c8SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFEM"
8077d4028c8SMatthew G. Knepley /*@
8087d4028c8SMatthew G. Knepley   DMPlexSNESGetGeometryFEM - Return precomputed geometric data
8097d4028c8SMatthew G. Knepley 
8107d4028c8SMatthew G. Knepley   Input Parameter:
8117d4028c8SMatthew G. Knepley . dm - The DM
8127d4028c8SMatthew G. Knepley 
8137d4028c8SMatthew G. Knepley   Output Parameters:
8147d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
8157d4028c8SMatthew G. Knepley 
8167d4028c8SMatthew G. Knepley   Level: developer
8177d4028c8SMatthew G. Knepley 
8187d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal()
8197d4028c8SMatthew G. Knepley @*/
8207d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom)
8217d4028c8SMatthew G. Knepley {
8227d4028c8SMatthew G. Knepley   DMSNES         dmsnes;
8237d4028c8SMatthew G. Knepley   PetscObject    obj;
8247d4028c8SMatthew G. Knepley   PetscErrorCode ierr;
8257d4028c8SMatthew G. Knepley 
8267d4028c8SMatthew G. Knepley   PetscFunctionBegin;
8277d4028c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8287d4028c8SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
8297d4028c8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr);
8307d4028c8SMatthew G. Knepley   if (!obj) {
8317d4028c8SMatthew G. Knepley     Vec cellgeom;
8327d4028c8SMatthew G. Knepley 
8337d4028c8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
8347d4028c8SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr);
8357d4028c8SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
8367d4028c8SMatthew G. Knepley   }
8377d4028c8SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);}
8387d4028c8SMatthew G. Knepley   PetscFunctionReturn(0);
8397d4028c8SMatthew G. Knepley }
8407d4028c8SMatthew G. Knepley 
8417d4028c8SMatthew G. Knepley #undef __FUNCT__
84208449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM"
84308449791SMatthew G. Knepley /*@
84408449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
84508449791SMatthew G. Knepley 
84608449791SMatthew G. Knepley   Input Parameter:
84708449791SMatthew G. Knepley . dm - The DM
84808449791SMatthew G. Knepley 
84908449791SMatthew G. Knepley   Output Parameters:
85008449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
85108449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
85208449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
85308449791SMatthew G. Knepley 
85408449791SMatthew G. Knepley   Level: developer
85508449791SMatthew G. Knepley 
85608449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
85708449791SMatthew G. Knepley @*/
85808449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
85924cdb843SMatthew G. Knepley {
86008449791SMatthew G. Knepley   DMSNES         dmsnes;
86108449791SMatthew G. Knepley   PetscObject    obj;
86224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
86324cdb843SMatthew G. Knepley 
86424cdb843SMatthew G. Knepley   PetscFunctionBegin;
86508449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86608449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
86708449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr);
86808449791SMatthew G. Knepley   if (!obj) {
86908449791SMatthew G. Knepley     Vec cellgeom, facegeom;
87024cdb843SMatthew G. Knepley 
87108449791SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr);
87208449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr);
87308449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr);
87408449791SMatthew G. Knepley     ierr = VecDestroy(&facegeom);CHKERRQ(ierr);
87508449791SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
87608449791SMatthew G. Knepley   }
87708449791SMatthew G. Knepley   if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);}
87808449791SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);}
87908449791SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(dm, minRadius);CHKERRQ(ierr);}
88024cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
88124cdb843SMatthew G. Knepley }
88224cdb843SMatthew G. Knepley 
88324cdb843SMatthew G. Knepley #undef __FUNCT__
88408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM"
88508449791SMatthew G. Knepley /*@C
88608449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
88708449791SMatthew G. Knepley 
88808449791SMatthew G. Knepley   Input Parameters:
88908449791SMatthew G. Knepley + dm - The DM
89008449791SMatthew G. Knepley - fv - The PetscFV
89108449791SMatthew G. Knepley 
89208449791SMatthew G. Knepley   Output Parameter:
89308449791SMatthew G. Knepley . dmGrad - The layout for gradient values
89408449791SMatthew G. Knepley 
89508449791SMatthew G. Knepley   Level: developer
89608449791SMatthew G. Knepley 
89708449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
89808449791SMatthew G. Knepley @*/
89908449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
90024cdb843SMatthew G. Knepley {
90108449791SMatthew G. Knepley   DMSNES         dmsnes;
90208449791SMatthew G. Knepley   PetscObject    obj;
90308449791SMatthew G. Knepley   PetscBool      computeGradients;
90424cdb843SMatthew G. Knepley   PetscErrorCode ierr;
90524cdb843SMatthew G. Knepley 
90624cdb843SMatthew G. Knepley   PetscFunctionBegin;
90708449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90808449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
90908449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
91008449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
91108449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
91208449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
91308449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr);
91408449791SMatthew G. Knepley   if (!obj) {
91508449791SMatthew G. Knepley     DM  dmGrad;
91608449791SMatthew G. Knepley     Vec faceGeometry, cellGeometry;
91708449791SMatthew G. Knepley 
91808449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr);
91908449791SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr);
92008449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr);
92108449791SMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
92208449791SMatthew G. Knepley   }
92308449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr);
92408449791SMatthew G. Knepley   PetscFunctionReturn(0);
92508449791SMatthew G. Knepley }
92608449791SMatthew G. Knepley 
92708449791SMatthew G. Knepley #undef __FUNCT__
92808449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields"
92908449791SMatthew G. Knepley /*@C
93008449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
93108449791SMatthew G. Knepley 
93208449791SMatthew G. Knepley   Input Parameters:
93308449791SMatthew G. Knepley + dm     - The DM
93408449791SMatthew G. Knepley . cStart - The first cell to include
93508449791SMatthew G. Knepley . cEnd   - The first cell to exclude
93608449791SMatthew G. Knepley . locX   - A local vector with the solution fields
93708449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
93808449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
93908449791SMatthew G. Knepley 
94008449791SMatthew G. Knepley   Output Parameters:
94108449791SMatthew G. Knepley + u   - The field coefficients
94208449791SMatthew G. Knepley . u_t - The fields derivative coefficients
94308449791SMatthew G. Knepley - a   - The auxiliary field coefficients
94408449791SMatthew G. Knepley 
94508449791SMatthew G. Knepley   Level: developer
94608449791SMatthew G. Knepley 
94708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
94808449791SMatthew G. Knepley @*/
94908449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
95008449791SMatthew G. Knepley {
95108449791SMatthew G. Knepley   DM             dmAux;
95208449791SMatthew G. Knepley   PetscSection   section, sectionAux;
95308449791SMatthew G. Knepley   PetscDS        prob;
95408449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
95508449791SMatthew G. Knepley   PetscErrorCode ierr;
95608449791SMatthew G. Knepley 
95708449791SMatthew G. Knepley   PetscFunctionBegin;
95808449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95908449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
96008449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
96108449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
96208449791SMatthew G. Knepley   PetscValidPointer(u, 7);
96308449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
96408449791SMatthew G. Knepley   PetscValidPointer(a, 9);
96524cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
96624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
96724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
96808449791SMatthew G. Knepley   if (locA) {
96908449791SMatthew G. Knepley     PetscDS probAux;
97008449791SMatthew G. Knepley 
97108449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
97224cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
97324cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
97424cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
97524cdb843SMatthew G. Knepley   }
97608449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr);
97708449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, locX_t ? numCells*totDim : 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);
97808449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, locA ? numCells*totDimAux : 0, PETSC_SCALAR, a);CHKERRQ(ierr);
97924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
98008449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
98124cdb843SMatthew G. Knepley     PetscInt     i;
98224cdb843SMatthew G. Knepley 
98308449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
98408449791SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[c*totDim+i] = x[i];
98508449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
98608449791SMatthew G. Knepley     if (locX_t) {
98708449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
98808449791SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[c*totDim+i] = x_t[i];
98908449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
99024cdb843SMatthew G. Knepley     }
99108449791SMatthew G. Knepley     if (locA) {
99208449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
99308449791SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[c*totDimAux+i] = x[i];
99408449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
99524cdb843SMatthew G. Knepley     }
99624cdb843SMatthew G. Knepley   }
99708449791SMatthew G. Knepley   PetscFunctionReturn(0);
99808449791SMatthew G. Knepley }
99924cdb843SMatthew G. Knepley 
100008449791SMatthew G. Knepley #undef __FUNCT__
100108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields"
100208449791SMatthew G. Knepley /*@C
100308449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
100408449791SMatthew G. Knepley 
100508449791SMatthew G. Knepley   Input Parameters:
100608449791SMatthew G. Knepley + dm     - The DM
100708449791SMatthew G. Knepley . cStart - The first cell to include
100808449791SMatthew G. Knepley . cEnd   - The first cell to exclude
100908449791SMatthew G. Knepley . locX   - A local vector with the solution fields
101008449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
101108449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
101208449791SMatthew G. Knepley 
101308449791SMatthew G. Knepley   Output Parameters:
101408449791SMatthew G. Knepley + u   - The field coefficients
101508449791SMatthew G. Knepley . u_t - The fields derivative coefficients
101608449791SMatthew G. Knepley - a   - The auxiliary field coefficients
101708449791SMatthew G. Knepley 
101808449791SMatthew G. Knepley   Level: developer
101908449791SMatthew G. Knepley 
102008449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
102108449791SMatthew G. Knepley @*/
102208449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
102308449791SMatthew G. Knepley {
102408449791SMatthew G. Knepley   PetscErrorCode ierr;
102508449791SMatthew G. Knepley 
102608449791SMatthew G. Knepley   PetscFunctionBegin;
102708449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr);
102808449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);
102908449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);
103008449791SMatthew G. Knepley   PetscFunctionReturn(0);
103124cdb843SMatthew G. Knepley }
103208449791SMatthew G. Knepley 
103308449791SMatthew G. Knepley #undef __FUNCT__
103408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields"
103508449791SMatthew G. Knepley /*@C
103608449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
103708449791SMatthew G. Knepley 
103808449791SMatthew G. Knepley   Input Parameters:
103908449791SMatthew G. Knepley + dm     - The DM
104008449791SMatthew G. Knepley . fStart - The first face to include
104108449791SMatthew G. Knepley . fEnd   - The first face to exclude
104208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
104308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
104408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
104508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
104608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
104708449791SMatthew G. Knepley 
104808449791SMatthew G. Knepley   Output Parameters:
104908449791SMatthew G. Knepley + uL - The field coefficients at the left side of the face
105008449791SMatthew G. Knepley - uR - The field coefficients at the right side of the face
105108449791SMatthew G. Knepley 
105208449791SMatthew G. Knepley   Level: developer
105308449791SMatthew G. Knepley 
105408449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
105508449791SMatthew G. Knepley @*/
105608449791SMatthew 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)
105708449791SMatthew G. Knepley {
105808449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
105908449791SMatthew G. Knepley   PetscDS            prob;
106008449791SMatthew G. Knepley   DMLabel            ghostLabel;
106108449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
106208449791SMatthew G. Knepley   PetscInt           dim, totDim, numFaces = fEnd - fStart, iface, face;
106308449791SMatthew G. Knepley   PetscErrorCode     ierr;
106408449791SMatthew G. Knepley 
106508449791SMatthew G. Knepley   PetscFunctionBegin;
106608449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
106708449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
106808449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
106908449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
107008449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
107108449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
107208449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
107308449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
107408449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
107508449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
107608449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
107708449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
107808449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
107908449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
108008449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
108108449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
108208449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
108308449791SMatthew G. Knepley   if (locGrad) {
108408449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
108508449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
108624cdb843SMatthew G. Knepley   }
108708449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, uL);CHKERRQ(ierr);
108808449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, uR);CHKERRQ(ierr);
108908449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
109008449791SMatthew G. Knepley     const PetscInt        *cells;
109108449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
109208449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
109308449791SMatthew G. Knepley     const PetscScalar     *xL, *xR, *gL, *gR;
109408449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
109508449791SMatthew G. Knepley     PetscInt               ghost, d;
109608449791SMatthew G. Knepley 
109708449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
109808449791SMatthew G. Knepley     if (ghost >= 0) continue;
109908449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
110008449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
110108449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
110208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
110308449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cells[0], x, &xL);CHKERRQ(ierr);
110408449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cells[1], x, &xR);CHKERRQ(ierr);
110508449791SMatthew G. Knepley     if (dmGrad) {
110608449791SMatthew G. Knepley       PetscReal dxL[3], dxR[3];
110708449791SMatthew G. Knepley 
110808449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
110908449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
111008449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
111108449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
111208449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
111308449791SMatthew G. Knepley         uLl[iface*totDim+d] = xL[d] + DMPlex_DotD_Internal(dim, &gL[d*dim], dxL);
111408449791SMatthew G. Knepley         uRl[iface*totDim+d] = xR[d] + DMPlex_DotD_Internal(dim, &gR[d*dim], dxR);
111508449791SMatthew G. Knepley       }
111608449791SMatthew G. Knepley     } else {
111708449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
111808449791SMatthew G. Knepley         uLl[iface*totDim+d] = xL[d];
111908449791SMatthew G. Knepley         uRl[iface*totDim+d] = xR[d];
112008449791SMatthew G. Knepley       }
112108449791SMatthew G. Knepley     }
112208449791SMatthew G. Knepley     ++iface;
112308449791SMatthew G. Knepley   }
112408449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
112508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
112608449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
112708449791SMatthew G. Knepley   if (locGrad) {
112808449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
112908449791SMatthew G. Knepley   }
113008449791SMatthew G. Knepley   PetscFunctionReturn(0);
113108449791SMatthew G. Knepley }
113208449791SMatthew G. Knepley 
113308449791SMatthew G. Knepley #undef __FUNCT__
113408449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields"
113508449791SMatthew G. Knepley /*@C
113608449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
113708449791SMatthew G. Knepley 
113808449791SMatthew G. Knepley   Input Parameters:
113908449791SMatthew G. Knepley + dm     - The DM
114008449791SMatthew G. Knepley . fStart - The first face to include
114108449791SMatthew G. Knepley . fEnd   - The first face to exclude
114208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
114308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
114408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
114508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
114608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
114708449791SMatthew G. Knepley 
114808449791SMatthew G. Knepley   Output Parameters:
114908449791SMatthew G. Knepley + uL - The field coefficients at the left side of the face
115008449791SMatthew G. Knepley - uR - The field coefficients at the right side of the face
115108449791SMatthew G. Knepley 
115208449791SMatthew G. Knepley   Level: developer
115308449791SMatthew G. Knepley 
115408449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
115508449791SMatthew G. Knepley @*/
115608449791SMatthew 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)
115708449791SMatthew G. Knepley {
115808449791SMatthew G. Knepley   PetscErrorCode ierr;
115908449791SMatthew G. Knepley 
116008449791SMatthew G. Knepley   PetscFunctionBegin;
116108449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr);
116208449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr);
116308449791SMatthew G. Knepley   PetscFunctionReturn(0);
116408449791SMatthew G. Knepley }
116508449791SMatthew G. Knepley 
116608449791SMatthew G. Knepley #undef __FUNCT__
116708449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry"
116808449791SMatthew G. Knepley /*@C
116908449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
117008449791SMatthew G. Knepley 
117108449791SMatthew G. Knepley   Input Parameters:
117208449791SMatthew G. Knepley + dm     - The DM
117308449791SMatthew G. Knepley . fStart - The first face to include
117408449791SMatthew G. Knepley . fEnd   - The first face to exclude
117508449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
117608449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
117708449791SMatthew G. Knepley 
117808449791SMatthew G. Knepley   Output Parameters:
117908449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
118008449791SMatthew G. Knepley - vol   - The cell volume
118108449791SMatthew G. Knepley 
118208449791SMatthew G. Knepley   Level: developer
118308449791SMatthew G. Knepley 
118408449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
118508449791SMatthew G. Knepley @*/
118608449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
118708449791SMatthew G. Knepley {
118808449791SMatthew G. Knepley   DM                 dmFace, dmCell;
118908449791SMatthew G. Knepley   DMLabel            ghostLabel;
119008449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
119108449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
119208449791SMatthew G. Knepley   PetscErrorCode     ierr;
119308449791SMatthew G. Knepley 
119408449791SMatthew G. Knepley   PetscFunctionBegin;
119508449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
119608449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
119708449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
119808449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
119908449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
120008449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
120108449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
120208449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
120308449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
120408449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
120508449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
120608449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
120708449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr);
120808449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
120908449791SMatthew G. Knepley     const PetscInt        *cells;
121008449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
121108449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
121208449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
1213*2eefff9cSMatthew G. Knepley     PetscReal             *voll   = *vol;
121408449791SMatthew G. Knepley     PetscInt               ghost, d;
121508449791SMatthew G. Knepley 
121608449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
121708449791SMatthew G. Knepley     if (ghost >= 0) continue;
121808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
121908449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
122008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
122108449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
122208449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
122308449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
122408449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
122508449791SMatthew G. Knepley     }
122608449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
122708449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
122808449791SMatthew G. Knepley     ++iface;
122908449791SMatthew G. Knepley   }
123008449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
123108449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
123208449791SMatthew G. Knepley   PetscFunctionReturn(0);
123308449791SMatthew G. Knepley }
123408449791SMatthew G. Knepley 
123508449791SMatthew G. Knepley #undef __FUNCT__
123608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry"
123708449791SMatthew G. Knepley /*@C
123808449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
123908449791SMatthew G. Knepley 
124008449791SMatthew G. Knepley   Input Parameters:
124108449791SMatthew G. Knepley + dm     - The DM
124208449791SMatthew G. Knepley . fStart - The first face to include
124308449791SMatthew G. Knepley . fEnd   - The first face to exclude
124408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
124508449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
124608449791SMatthew G. Knepley 
124708449791SMatthew G. Knepley   Output Parameters:
124808449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
124908449791SMatthew G. Knepley - vol   - The cell volume
125008449791SMatthew G. Knepley 
125108449791SMatthew G. Knepley   Level: developer
125208449791SMatthew G. Knepley 
125308449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
125408449791SMatthew G. Knepley @*/
125508449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
125608449791SMatthew G. Knepley {
125708449791SMatthew G. Knepley   PetscErrorCode ierr;
125808449791SMatthew G. Knepley 
125908449791SMatthew G. Knepley   PetscFunctionBegin;
126008449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
126108449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr);
126208449791SMatthew G. Knepley   PetscFunctionReturn(0);
126308449791SMatthew G. Knepley }
126408449791SMatthew G. Knepley 
126508449791SMatthew G. Knepley #undef __FUNCT__
126608449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal"
126708449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad)
126808449791SMatthew G. Knepley {
126908449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
127008449791SMatthew G. Knepley   DMLabel            ghostLabel;
127108449791SMatthew G. Knepley   PetscDS            prob;
127208449791SMatthew G. Knepley   PetscFV            fvm;
127308449791SMatthew G. Knepley   PetscLimiter       lim;
127408449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x;
127508449791SMatthew G. Knepley   PetscScalar       *gr;
127608449791SMatthew G. Knepley   PetscReal         *cellPhi;
127708449791SMatthew G. Knepley   PetscInt           dim, face, cell, totDim, cStart, cEnd, cEndInterior;
127808449791SMatthew G. Knepley   PetscErrorCode     ierr;
127908449791SMatthew G. Knepley 
128008449791SMatthew G. Knepley   PetscFunctionBegin;
128108449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
128208449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
128308449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
128408449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
128508449791SMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr);
128608449791SMatthew G. Knepley   ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr);
128708449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
128808449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
128908449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
129008449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
129108449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
129208449791SMatthew G. Knepley   ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr);
129308449791SMatthew G. Knepley   ierr = VecZeroEntries(grad);CHKERRQ(ierr);
129408449791SMatthew G. Knepley   ierr = VecGetArray(grad, &gr);CHKERRQ(ierr);
129508449791SMatthew G. Knepley   /* Reconstruct gradients */
129608449791SMatthew G. Knepley   for (face = fStart; face < fEnd; ++face) {
129708449791SMatthew G. Knepley     const PetscInt        *cells;
129808449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
129908449791SMatthew G. Knepley     const PetscScalar     *cx[2];
130008449791SMatthew G. Knepley     PetscScalar           *cgrad[2];
130108449791SMatthew G. Knepley     PetscBool              boundary;
130208449791SMatthew G. Knepley     PetscInt               ghost, c, pd, d;
130308449791SMatthew G. Knepley 
130408449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
130508449791SMatthew G. Knepley     if (ghost >= 0) continue;
130608449791SMatthew G. Knepley     ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
130708449791SMatthew G. Knepley     if (boundary) continue;
130808449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
130908449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
131008449791SMatthew G. Knepley     for (c = 0; c < 2; ++c) {
131108449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr);
131208449791SMatthew G. Knepley       ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr);
131308449791SMatthew G. Knepley     }
131408449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd) {
131508449791SMatthew G. Knepley       PetscScalar delta = cx[1][pd] - cx[0][pd];
131608449791SMatthew G. Knepley 
131708449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
131808449791SMatthew G. Knepley         if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta;
131908449791SMatthew G. Knepley         if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta;
132008449791SMatthew G. Knepley       }
132108449791SMatthew G. Knepley     }
132208449791SMatthew G. Knepley   }
132308449791SMatthew G. Knepley   /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */
132408449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
132508449791SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
132608449791SMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
132708449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
132808449791SMatthew G. Knepley   for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) {
132908449791SMatthew G. Knepley     const PetscInt        *faces;
133008449791SMatthew G. Knepley     const PetscScalar     *cx;
133108449791SMatthew G. Knepley     const PetscFVCellGeom *cg;
133208449791SMatthew G. Knepley     PetscScalar           *cgrad;
133308449791SMatthew G. Knepley     PetscInt               coneSize, f, pd, d;
133408449791SMatthew G. Knepley 
133508449791SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
133608449791SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
133708449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr);
133808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr);
133908449791SMatthew G. Knepley     ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr);
134008449791SMatthew G. Knepley     if (!cgrad) continue; /* Unowned overlap cell, we do not compute */
134108449791SMatthew G. Knepley     /* Limiter will be minimum value over all neighbors */
134208449791SMatthew G. Knepley     for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL;
134308449791SMatthew G. Knepley     for (f = 0; f < coneSize; ++f) {
134408449791SMatthew G. Knepley       const PetscScalar     *ncx;
134508449791SMatthew G. Knepley       const PetscFVCellGeom *ncg;
134608449791SMatthew G. Knepley       const PetscInt        *fcells;
134708449791SMatthew G. Knepley       PetscInt               face = faces[f], ncell, ghost;
134808449791SMatthew G. Knepley       PetscReal              v[3];
134908449791SMatthew G. Knepley       PetscBool              boundary;
135008449791SMatthew G. Knepley 
135108449791SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
135208449791SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
135308449791SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
135408449791SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr);
135508449791SMatthew G. Knepley       ncell = cell == fcells[0] ? fcells[1] : fcells[0];
135608449791SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr);
135708449791SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr);
135808449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v);
135908449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
136008449791SMatthew G. Knepley         /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */
136108449791SMatthew G. Knepley         PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v);
136208449791SMatthew G. Knepley 
136308449791SMatthew G. Knepley         ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr);
136408449791SMatthew G. Knepley         cellPhi[d] = PetscMin(cellPhi[d], phi);
136508449791SMatthew G. Knepley       }
136608449791SMatthew G. Knepley     }
136708449791SMatthew G. Knepley     /* Apply limiter to gradient */
136808449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd)
136908449791SMatthew G. Knepley       /* Scalar limiter applied to each component separately */
137008449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd];
137108449791SMatthew G. Knepley   }
137208449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
137308449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
137408449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
137508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
137608449791SMatthew G. Knepley   ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr);
137708449791SMatthew G. Knepley   PetscFunctionReturn(0);
137808449791SMatthew G. Knepley }
137908449791SMatthew G. Knepley 
138008449791SMatthew G. Knepley #undef __FUNCT__
138108449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal"
138208449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user)
138308449791SMatthew G. Knepley {
138408449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
138508449791SMatthew G. Knepley   PetscSection     section;
138608449791SMatthew G. Knepley   PetscDS          prob;
138708449791SMatthew G. Knepley   DMLabel          depth;
138808449791SMatthew G. Knepley   PetscFECellGeom *cgeom;
138908449791SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *elemVec = NULL;
139008449791SMatthew G. Knepley   PetscInt         dim, Nf, f, totDimBd, numBd, bd;
139108449791SMatthew G. Knepley   PetscErrorCode   ierr;
139208449791SMatthew G. Knepley 
139308449791SMatthew G. Knepley   PetscFunctionBegin;
139408449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
139508449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
139608449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
139708449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
139808449791SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
139924cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
140024cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
140124cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
140224cdb843SMatthew G. Knepley     const char     *bdLabel;
140324cdb843SMatthew G. Knepley     DMLabel         label;
140424cdb843SMatthew G. Knepley     IS              pointIS;
140524cdb843SMatthew G. Knepley     const PetscInt *points;
140624cdb843SMatthew G. Knepley     const PetscInt *values;
140724cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
140824cdb843SMatthew G. Knepley     PetscBool       isEssential;
140924cdb843SMatthew G. Knepley 
141024cdb843SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
141124cdb843SMatthew G. Knepley     if (isEssential) continue;
141224cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
141324cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
141424cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
141524cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
141624cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
141724cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
141824cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
141924cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
142024cdb843SMatthew G. Knepley     }
1421bbce034cSMatthew G. Knepley     ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
142208449791SMatthew G. Knepley     if (locX_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
142324cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
142424cdb843SMatthew G. Knepley       const PetscInt point = points[p];
142524cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
142624cdb843SMatthew G. Knepley       PetscInt       i;
142724cdb843SMatthew G. Knepley 
142824cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
142924cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1430bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
1431bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);
1432bbce034cSMatthew 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);
143308449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
143424cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
143508449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
143608449791SMatthew G. Knepley       if (locX_t) {
143708449791SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
143824cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
143908449791SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX_t, point, NULL, &x);CHKERRQ(ierr);
144024cdb843SMatthew G. Knepley       }
144124cdb843SMatthew G. Knepley       ++f;
144224cdb843SMatthew G. Knepley     }
144324cdb843SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
144424cdb843SMatthew G. Knepley       PetscFE         fe;
144508449791SMatthew G. Knepley       PetscQuadrature q;
144624cdb843SMatthew G. Knepley       PetscInt        numQuadPoints, Nb;
144724cdb843SMatthew G. Knepley       /* Conforming batches */
144824cdb843SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
144924cdb843SMatthew G. Knepley       /* Remainder */
145024cdb843SMatthew G. Knepley       PetscInt        Nr, offset;
145124cdb843SMatthew G. Knepley 
145224cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
145324cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
145424cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
145524cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
145624cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
145724cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
145824cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
145924cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
146024cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
146124cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
146224cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
146324cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
1464bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, cgeom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
1465bbce034cSMatthew 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);
146624cdb843SMatthew G. Knepley     }
146724cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
146824cdb843SMatthew G. Knepley       const PetscInt point = points[p];
146924cdb843SMatthew G. Knepley 
147024cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
147124cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
147224cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
147308449791SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, locF, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
147424cdb843SMatthew G. Knepley       ++f;
147524cdb843SMatthew G. Knepley     }
147624cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
147724cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1478bbce034cSMatthew G. Knepley     ierr = PetscFree3(u,cgeom,elemVec);CHKERRQ(ierr);
147908449791SMatthew G. Knepley     if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
148024cdb843SMatthew G. Knepley   }
148108449791SMatthew G. Knepley   PetscFunctionReturn(0);
148208449791SMatthew G. Knepley }
148308449791SMatthew G. Knepley 
148408449791SMatthew G. Knepley #undef __FUNCT__
148508449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal"
148608449791SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
148708449791SMatthew G. Knepley {
148808449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
148908449791SMatthew G. Knepley   const char       *name       = "Residual";
149008449791SMatthew G. Knepley   DM                dmAux      = NULL;
149108449791SMatthew G. Knepley   DM                dmGrad     = NULL;
149208449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
149308449791SMatthew G. Knepley   PetscDS           prob       = NULL;
149408449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
149508449791SMatthew G. Knepley   PetscSection      section    = NULL;
149608449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
149708449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
149808449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
149908449791SMatthew G. Knepley   PetscFECellGeom  *cgeomFEM   = NULL;
150008449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
150108449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
150208449791SMatthew G. Knepley   Vec               locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
150308449791SMatthew G. Knepley   PetscScalar      *u, *u_t, *a, *uL, *uR;
150408449791SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, cStart, cEnd, fStart, fEnd;
150508449791SMatthew G. Knepley   PetscErrorCode    ierr;
150608449791SMatthew G. Knepley 
150708449791SMatthew G. Knepley   PetscFunctionBegin;
150808449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
150908449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
151008449791SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the preclac functions return exactly what we need */
151108449791SMatthew G. Knepley   /* FEM+FVM */
151208449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
151308449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
151408449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
151508449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
151608449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
151708449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
151808449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
151908449791SMatthew G. Knepley   if (locA) {
152008449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
152108449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
152208449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
152308449791SMatthew G. Knepley   }
152408449791SMatthew G. Knepley   /* 2: Get geometric data */
152508449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
152608449791SMatthew G. Knepley     PetscObject  obj;
152708449791SMatthew G. Knepley     PetscClassId id;
152808449791SMatthew G. Knepley 
152908449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
153008449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
153108449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
153208449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
153308449791SMatthew G. Knepley   }
153408449791SMatthew G. Knepley   if (useFEM) {
153508449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr);
153608449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFEM, (const PetscScalar **) &cgeomFEM);CHKERRQ(ierr);
153708449791SMatthew G. Knepley   }
153808449791SMatthew G. Knepley   if (useFVM) {
153908449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
154008449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
154108449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
154208449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
154308449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
154408449791SMatthew G. Knepley     if (dmGrad) {
154508449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
154608449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
154708449791SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
154808449791SMatthew G. Knepley       /* Communicate gradient values */
154908449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
155008449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
155108449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
155208449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
155308449791SMatthew G. Knepley     }
155408449791SMatthew G. Knepley   }
155508449791SMatthew G. Knepley   /* Handle boundary values */
155608449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
155708449791SMatthew G. Knepley   /* Loop over chunks */
155808449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
155908449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
156008449791SMatthew G. Knepley   numChunks     = 1;
156108449791SMatthew G. Knepley   cellChunkSize = (cEnd - cStart)/numChunks;
156208449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
156308449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
1564*2eefff9cSMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
1565*2eefff9cSMatthew G. Knepley     PetscReal       *vol;
156608449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
156708449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
156808449791SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face;
156908449791SMatthew G. Knepley 
157008449791SMatthew G. Knepley     /* Extract field coefficients */
157108449791SMatthew G. Knepley     if (useFEM) {
157208449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
157308449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
157408449791SMatthew G. Knepley     }
157508449791SMatthew G. Knepley     if (useFVM) {
157608449791SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
157708449791SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
157808449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
157908449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
158008449791SMatthew G. Knepley     }
158108449791SMatthew 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 */
158208449791SMatthew G. Knepley     /* Loop over fields */
158308449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
158408449791SMatthew G. Knepley       PetscObject  obj;
158508449791SMatthew G. Knepley       PetscClassId id;
158608449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
158708449791SMatthew G. Knepley 
158808449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
158908449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
159008449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
159108449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
159208449791SMatthew G. Knepley         PetscQuadrature q;
159308449791SMatthew G. Knepley         PetscInt        Nq, Nb;
159408449791SMatthew G. Knepley 
159508449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
159608449791SMatthew G. Knepley 
159708449791SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
159808449791SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
159908449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
160008449791SMatthew G. Knepley         blockSize = Nb*Nq;
160108449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
160208449791SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
160308449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
160408449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
160508449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
160608449791SMatthew G. Knepley         offset    = numCells - Nr;
160708449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
160808449791SMatthew 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) */
160908449791SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
161008449791SMatthew 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);
161108449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
161208449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
161308449791SMatthew G. Knepley 
161408449791SMatthew G. Knepley         Ne = numFaces;
161508449791SMatthew G. Knepley         Nr = 0;
161608449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
161708449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
161808449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
161908449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
162008449791SMatthew G. Knepley     }
162108449791SMatthew G. Knepley     /* Loop over domain */
162208449791SMatthew G. Knepley     if (useFEM) {
162308449791SMatthew G. Knepley       /* Add elemVec to locX */
162408449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
162508449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
162608449791SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_VALUES);CHKERRQ(ierr);
162708449791SMatthew G. Knepley       }
162808449791SMatthew G. Knepley     }
162908449791SMatthew G. Knepley     if (useFVM) {
163008449791SMatthew G. Knepley       PetscScalar *fa;
163108449791SMatthew G. Knepley       PetscInt     iface;
163208449791SMatthew G. Knepley 
163308449791SMatthew G. Knepley       /* Accumulate fluxes to cells */
163408449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
163508449791SMatthew G. Knepley       for (face = fS, iface = 0; face < fE; ++face) {
163608449791SMatthew G. Knepley         const PetscInt *cells;
163708449791SMatthew G. Knepley         PetscScalar    *fL, *fR;
163808449791SMatthew G. Knepley         PetscInt        ghost, d;
163908449791SMatthew G. Knepley 
164008449791SMatthew G. Knepley         ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
164108449791SMatthew G. Knepley         if (ghost >= 0) continue;
164208449791SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
164308449791SMatthew G. Knepley         ierr = DMPlexPointGlobalRef(dm, cells[0], fa, &fL);CHKERRQ(ierr);
164408449791SMatthew G. Knepley         ierr = DMPlexPointGlobalRef(dm, cells[1], fa, &fR);CHKERRQ(ierr);
164508449791SMatthew G. Knepley         for (d = 0; d < totDim; ++d) {
164608449791SMatthew G. Knepley           if (fL) fL[d] -= fluxL[iface*totDim+d];
164708449791SMatthew G. Knepley           if (fR) fR[d] += fluxR[iface*totDim+d];
164808449791SMatthew G. Knepley         }
164908449791SMatthew G. Knepley         ++iface;
165008449791SMatthew G. Knepley       }
165108449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
165208449791SMatthew G. Knepley     }
165308449791SMatthew G. Knepley     if (useFEM) {
165408449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
165508449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
165608449791SMatthew G. Knepley     }
165708449791SMatthew G. Knepley     if (useFVM) {
165808449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
165908449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
166008449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
166108449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
166208449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
166308449791SMatthew G. Knepley     }
166408449791SMatthew G. Knepley   }
166508449791SMatthew G. Knepley 
166608449791SMatthew G. Knepley   if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);}
166708449791SMatthew G. Knepley 
166808449791SMatthew G. Knepley   /* FEM */
166908449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
167008449791SMatthew G. Knepley   /* 2: Get geometric data */
167108449791SMatthew G. Knepley   /* 3: Handle boundary values */
167208449791SMatthew G. Knepley   /* 4: Loop over domain */
167308449791SMatthew G. Knepley   /*   Extract coefficients */
167408449791SMatthew G. Knepley   /* Loop over fields */
167508449791SMatthew G. Knepley   /*   Set tiling for FE*/
167608449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
167708449791SMatthew G. Knepley   /*     Loop over subdomain */
167808449791SMatthew G. Knepley   /*       Loop over quad points */
167908449791SMatthew G. Knepley   /*         Transform coords to real space */
168008449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
168108449791SMatthew G. Knepley   /*         Evaluate residual at point */
168208449791SMatthew G. Knepley   /*         Transform residual to real space */
168308449791SMatthew G. Knepley   /*       Add residual to elemVec */
168408449791SMatthew G. Knepley   /* Loop over domain */
168508449791SMatthew G. Knepley   /*   Add elemVec to locX */
168608449791SMatthew G. Knepley 
168708449791SMatthew G. Knepley   /* FVM */
168808449791SMatthew G. Knepley   /* Get geometric data */
168908449791SMatthew G. Knepley   /* If using gradients */
169008449791SMatthew G. Knepley   /*   Compute gradient data */
169108449791SMatthew G. Knepley   /*   Loop over domain faces */
169208449791SMatthew G. Knepley   /*     Count computational faces */
169308449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
169408449791SMatthew G. Knepley   /*   Loop over domain cells */
169508449791SMatthew G. Knepley   /*     Limit cell gradients */
169608449791SMatthew G. Knepley   /* Handle boundary values */
169708449791SMatthew G. Knepley   /* Loop over domain faces */
169808449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
169908449791SMatthew G. Knepley   /* Riemann solve over faces */
170008449791SMatthew G. Knepley   /* Loop over domain faces */
170108449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
170208449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
170308449791SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, locF);CHKERRQ(ierr);}
170424cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
170524cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
170624cdb843SMatthew G. Knepley }
170724cdb843SMatthew G. Knepley 
170824cdb843SMatthew G. Knepley #undef __FUNCT__
170924cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal"
171024cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
171124cdb843SMatthew G. Knepley {
171224cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1713bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
171424cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
171524cdb843SMatthew G. Knepley   PetscQuadrature   q;
171624cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
1717bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
171824cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
171924cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
172024cdb843SMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
172124cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
172224cdb843SMatthew G. Knepley 
172324cdb843SMatthew G. Knepley   PetscFunctionBegin;
172424cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
172524cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
172624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
172724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
172824cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
172924cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
173024cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
173124cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
173224cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
173324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
173424cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
173524cdb843SMatthew G. Knepley   if (dmAux) {
173624cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
173724cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
173824cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
173924cdb843SMatthew G. Knepley   }
174008449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
174124cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1742bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
174324cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
174424cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1745bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
1746bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
174724cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
174824cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
174924cdb843SMatthew G. Knepley     PetscInt     i;
175024cdb843SMatthew G. Knepley 
175124cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
175224cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
175324cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
175424cdb843SMatthew G. Knepley     if (X_t) {
175524cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
175624cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
175724cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
175824cdb843SMatthew G. Knepley     }
175924cdb843SMatthew G. Knepley     if (dmAux) {
176024cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
176124cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
176224cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
176324cdb843SMatthew G. Knepley     }
176424cdb843SMatthew G. Knepley   }
176524cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
176624cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
176724cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
176824cdb843SMatthew G. Knepley     /* Conforming batches */
176924cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
177024cdb843SMatthew G. Knepley     /* Remainder */
177124cdb843SMatthew G. Knepley     PetscInt Nr, offset;
177224cdb843SMatthew G. Knepley 
177324cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
177424cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
177524cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
177624cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
177724cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
177824cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
177924cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
178024cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
178124cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
178224cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
178324cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
178424cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
178524cdb843SMatthew G. Knepley     offset    = numCells - Nr;
1786bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
1787bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
1788bbce034cSMatthew 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);
1789bbce034cSMatthew 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);
179024cdb843SMatthew G. Knepley   }
179124cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
179224cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
179324cdb843SMatthew G. Knepley     PetscInt  d;
179424cdb843SMatthew G. Knepley 
179524cdb843SMatthew 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;}
179624cdb843SMatthew G. Knepley     if (diff) {
179724cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
179824cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
179924cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
180024cdb843SMatthew G. Knepley       ++diffCell;
180124cdb843SMatthew G. Knepley     }
180224cdb843SMatthew G. Knepley     if (diffCell > 9) break;
180324cdb843SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
180424cdb843SMatthew G. Knepley   }
1805bbce034cSMatthew G. Knepley   ierr = VecRestoreArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
1806bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
180724cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
180824cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
180924cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
181024cdb843SMatthew G. Knepley }
181124cdb843SMatthew G. Knepley 
181224cdb843SMatthew G. Knepley #undef __FUNCT__
181324cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
181424cdb843SMatthew G. Knepley /*@
181524cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
181624cdb843SMatthew G. Knepley 
181724cdb843SMatthew G. Knepley   Input Parameters:
181824cdb843SMatthew G. Knepley + dm - The mesh
181924cdb843SMatthew G. Knepley . X  - Local solution
182024cdb843SMatthew G. Knepley - user - The user context
182124cdb843SMatthew G. Knepley 
182224cdb843SMatthew G. Knepley   Output Parameter:
182324cdb843SMatthew G. Knepley . F  - Local output vector
182424cdb843SMatthew G. Knepley 
182524cdb843SMatthew G. Knepley   Level: developer
182624cdb843SMatthew G. Knepley 
182724cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
182824cdb843SMatthew G. Knepley @*/
182924cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
183024cdb843SMatthew G. Knepley {
183124cdb843SMatthew G. Knepley   PetscObject    check;
183224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
183324cdb843SMatthew G. Knepley 
183424cdb843SMatthew G. Knepley   PetscFunctionBegin;
183524cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
183624cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
183724cdb843SMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
183808449791SMatthew G. Knepley   else       {ierr = DMPlexComputeResidual_Internal(dm, 0.0, X, NULL, F, user);CHKERRQ(ierr);}
183924cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
184024cdb843SMatthew G. Knepley }
184124cdb843SMatthew G. Knepley 
184224cdb843SMatthew G. Knepley #undef __FUNCT__
184324cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
184424cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
184524cdb843SMatthew G. Knepley {
184624cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
184724cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
184824cdb843SMatthew G. Knepley   DM                dmAux;
184924cdb843SMatthew G. Knepley   DMLabel           depth;
1850bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
185124cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
185224cdb843SMatthew G. Knepley   PetscQuadrature   quad;
185324cdb843SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
1854bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
185524cdb843SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
185624cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
185724cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
185824cdb843SMatthew G. Knepley   PetscBool         isShell;
185924cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
186024cdb843SMatthew G. Knepley 
186124cdb843SMatthew G. Knepley   PetscFunctionBegin;
186224cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
186324cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
186424cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
186524cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
186624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
186724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
186824cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
186924cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
187024cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
187124cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
187224cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
187324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
187424cdb843SMatthew G. Knepley   if (dmAux) {
187524cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
187624cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
187724cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
187824cdb843SMatthew G. Knepley   }
187908449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
188024cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
1881bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
188224cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1883bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
1884bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
188524cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
188624cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
188724cdb843SMatthew G. Knepley     PetscInt     i;
188824cdb843SMatthew G. Knepley 
188924cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
189024cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
189124cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
189224cdb843SMatthew G. Knepley     if (X_t) {
189324cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
189424cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
189524cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
189624cdb843SMatthew G. Knepley     }
189724cdb843SMatthew G. Knepley     if (dmAux) {
189824cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
189924cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
190024cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
190124cdb843SMatthew G. Knepley     }
190224cdb843SMatthew G. Knepley   }
190324cdb843SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
190424cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
190524cdb843SMatthew G. Knepley     PetscFE  fe;
190624cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
190724cdb843SMatthew G. Knepley     /* Conforming batches */
190824cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
190924cdb843SMatthew G. Knepley     /* Remainder */
191024cdb843SMatthew G. Knepley     PetscInt Nr, offset;
191124cdb843SMatthew G. Knepley 
191224cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
191324cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
191424cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
191524cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
191624cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
191724cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
191824cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
191924cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
192024cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
192124cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
192224cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
192324cdb843SMatthew G. Knepley     offset    = numCells - Nr;
192424cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1925bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1926bbce034cSMatthew 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);
192724cdb843SMatthew G. Knepley     }
192824cdb843SMatthew G. Knepley   }
192924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
193024cdb843SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
193124cdb843SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
193224cdb843SMatthew G. Knepley   }
1933bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
1934bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr);
193524cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
193624cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
193724cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
193824cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
193924cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
194024cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
194124cdb843SMatthew G. Knepley     const char     *bdLabel;
194224cdb843SMatthew G. Knepley     DMLabel         label;
194324cdb843SMatthew G. Knepley     IS              pointIS;
194424cdb843SMatthew G. Knepley     const PetscInt *points;
194524cdb843SMatthew G. Knepley     const PetscInt *values;
194624cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
194724cdb843SMatthew G. Knepley     PetscBool       isEssential;
194824cdb843SMatthew G. Knepley 
194924cdb843SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
195024cdb843SMatthew G. Knepley     if (isEssential) continue;
195124cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
195224cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
195324cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
195424cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
195524cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
195624cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
195724cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
195824cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
195924cdb843SMatthew G. Knepley     }
1960bbce034cSMatthew G. Knepley     ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
196124cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
196224cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
196324cdb843SMatthew G. Knepley       const PetscInt point = points[p];
196424cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
196524cdb843SMatthew G. Knepley       PetscInt       i;
196624cdb843SMatthew G. Knepley 
196724cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
196824cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1969bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
1970bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);
1971bbce034cSMatthew 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);
197224cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
197324cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
197424cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
197524cdb843SMatthew G. Knepley       if (X_t) {
197624cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
197724cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
197824cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
197924cdb843SMatthew G. Knepley       }
198024cdb843SMatthew G. Knepley       ++f;
198124cdb843SMatthew G. Knepley     }
198224cdb843SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
198324cdb843SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
198424cdb843SMatthew G. Knepley       PetscFE  fe;
198524cdb843SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
198624cdb843SMatthew G. Knepley       /* Conforming batches */
198724cdb843SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
198824cdb843SMatthew G. Knepley       /* Remainder */
198924cdb843SMatthew G. Knepley       PetscInt Nr, offset;
199024cdb843SMatthew G. Knepley 
199124cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
199224cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
199324cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
199424cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
199524cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
199624cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
199724cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
199824cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
199924cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
200024cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
200124cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
200224cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
200324cdb843SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2004bbce034cSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
2005bbce034cSMatthew 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);
200624cdb843SMatthew G. Knepley       }
200724cdb843SMatthew G. Knepley     }
200824cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
200924cdb843SMatthew G. Knepley       const PetscInt point = points[p];
201024cdb843SMatthew G. Knepley 
201124cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
201224cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
201324cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
201424cdb843SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
201524cdb843SMatthew G. Knepley       ++f;
201624cdb843SMatthew G. Knepley     }
201724cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
201824cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2019bbce034cSMatthew G. Knepley     ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr);
202024cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
202124cdb843SMatthew G. Knepley   }
202224cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
202324cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
202424cdb843SMatthew G. Knepley   if (mesh->printFEM) {
202524cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
202624cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
202724cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
202824cdb843SMatthew G. Knepley   }
202924cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
203024cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
203124cdb843SMatthew G. Knepley   if (isShell) {
203224cdb843SMatthew G. Knepley     JacActionCtx *jctx;
203324cdb843SMatthew G. Knepley 
203424cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
203524cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
203624cdb843SMatthew G. Knepley   }
203724cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
203824cdb843SMatthew G. Knepley }
203924cdb843SMatthew G. Knepley 
204024cdb843SMatthew G. Knepley #undef __FUNCT__
204124cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
204224cdb843SMatthew G. Knepley /*@
204324cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
204424cdb843SMatthew G. Knepley 
204524cdb843SMatthew G. Knepley   Input Parameters:
204624cdb843SMatthew G. Knepley + dm - The mesh
204724cdb843SMatthew G. Knepley . X  - Local input vector
204824cdb843SMatthew G. Knepley - user - The user context
204924cdb843SMatthew G. Knepley 
205024cdb843SMatthew G. Knepley   Output Parameter:
205124cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
205224cdb843SMatthew G. Knepley 
205324cdb843SMatthew G. Knepley   Note:
205424cdb843SMatthew G. Knepley   The first member of the user context must be an FEMContext.
205524cdb843SMatthew G. Knepley 
205624cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
205724cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
205824cdb843SMatthew G. Knepley 
205924cdb843SMatthew G. Knepley   Level: developer
206024cdb843SMatthew G. Knepley 
206124cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
206224cdb843SMatthew G. Knepley @*/
206324cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
206424cdb843SMatthew G. Knepley {
206524cdb843SMatthew G. Knepley   PetscErrorCode ierr;
206624cdb843SMatthew G. Knepley 
206724cdb843SMatthew G. Knepley   PetscFunctionBegin;
206824cdb843SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
206924cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
207024cdb843SMatthew G. Knepley }
2071