xref: /petsc/src/snes/utils/dmplexsnes.c (revision 0844979179b278507d32940e306fd9da3eae660b)
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__
842*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGeometryFVM"
843*08449791SMatthew G. Knepley /*@
844*08449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
845*08449791SMatthew G. Knepley 
846*08449791SMatthew G. Knepley   Input Parameter:
847*08449791SMatthew G. Knepley . dm - The DM
848*08449791SMatthew G. Knepley 
849*08449791SMatthew G. Knepley   Output Parameters:
850*08449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
851*08449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
852*08449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
853*08449791SMatthew G. Knepley 
854*08449791SMatthew G. Knepley   Level: developer
855*08449791SMatthew G. Knepley 
856*08449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
857*08449791SMatthew G. Knepley @*/
858*08449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
85924cdb843SMatthew G. Knepley {
860*08449791SMatthew G. Knepley   DMSNES         dmsnes;
861*08449791SMatthew G. Knepley   PetscObject    obj;
86224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
86324cdb843SMatthew G. Knepley 
86424cdb843SMatthew G. Knepley   PetscFunctionBegin;
865*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
866*08449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
867*08449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", &obj);CHKERRQ(ierr);
868*08449791SMatthew G. Knepley   if (!obj) {
869*08449791SMatthew G. Knepley     Vec cellgeom, facegeom;
87024cdb843SMatthew G. Knepley 
871*08449791SMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellgeom, &facegeom);CHKERRQ(ierr);
872*08449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject) facegeom);CHKERRQ(ierr);
873*08449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject) cellgeom);CHKERRQ(ierr);
874*08449791SMatthew G. Knepley     ierr = VecDestroy(&facegeom);CHKERRQ(ierr);
875*08449791SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
876*08449791SMatthew G. Knepley   }
877*08449791SMatthew G. Knepley   if (facegeom) {PetscValidPointer(facegeom, 2); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_facegeom_fvm", (PetscObject *) facegeom);CHKERRQ(ierr);}
878*08449791SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fvm", (PetscObject *) cellgeom);CHKERRQ(ierr);}
879*08449791SMatthew 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__
884*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESGetGradientDM"
885*08449791SMatthew G. Knepley /*@C
886*08449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
887*08449791SMatthew G. Knepley 
888*08449791SMatthew G. Knepley   Input Parameters:
889*08449791SMatthew G. Knepley + dm - The DM
890*08449791SMatthew G. Knepley - fv - The PetscFV
891*08449791SMatthew G. Knepley 
892*08449791SMatthew G. Knepley   Output Parameter:
893*08449791SMatthew G. Knepley . dmGrad - The layout for gradient values
894*08449791SMatthew G. Knepley 
895*08449791SMatthew G. Knepley   Level: developer
896*08449791SMatthew G. Knepley 
897*08449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
898*08449791SMatthew G. Knepley @*/
899*08449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
90024cdb843SMatthew G. Knepley {
901*08449791SMatthew G. Knepley   DMSNES         dmsnes;
902*08449791SMatthew G. Knepley   PetscObject    obj;
903*08449791SMatthew G. Knepley   PetscBool      computeGradients;
90424cdb843SMatthew G. Knepley   PetscErrorCode ierr;
90524cdb843SMatthew G. Knepley 
90624cdb843SMatthew G. Knepley   PetscFunctionBegin;
907*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
908*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
909*08449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
910*08449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
911*08449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
912*08449791SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
913*08449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", &obj);CHKERRQ(ierr);
914*08449791SMatthew G. Knepley   if (!obj) {
915*08449791SMatthew G. Knepley     DM  dmGrad;
916*08449791SMatthew G. Knepley     Vec faceGeometry, cellGeometry;
917*08449791SMatthew G. Knepley 
918*08449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometry, &cellGeometry, NULL);CHKERRQ(ierr);
919*08449791SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometry, cellGeometry, &dmGrad);CHKERRQ(ierr);
920*08449791SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject) dmGrad);CHKERRQ(ierr);
921*08449791SMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
922*08449791SMatthew G. Knepley   }
923*08449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_dmgrad_fvm", (PetscObject *) dmGrad);CHKERRQ(ierr);
924*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
925*08449791SMatthew G. Knepley }
926*08449791SMatthew G. Knepley 
927*08449791SMatthew G. Knepley #undef __FUNCT__
928*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellFields"
929*08449791SMatthew G. Knepley /*@C
930*08449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
931*08449791SMatthew G. Knepley 
932*08449791SMatthew G. Knepley   Input Parameters:
933*08449791SMatthew G. Knepley + dm     - The DM
934*08449791SMatthew G. Knepley . cStart - The first cell to include
935*08449791SMatthew G. Knepley . cEnd   - The first cell to exclude
936*08449791SMatthew G. Knepley . locX   - A local vector with the solution fields
937*08449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
938*08449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
939*08449791SMatthew G. Knepley 
940*08449791SMatthew G. Knepley   Output Parameters:
941*08449791SMatthew G. Knepley + u   - The field coefficients
942*08449791SMatthew G. Knepley . u_t - The fields derivative coefficients
943*08449791SMatthew G. Knepley - a   - The auxiliary field coefficients
944*08449791SMatthew G. Knepley 
945*08449791SMatthew G. Knepley   Level: developer
946*08449791SMatthew G. Knepley 
947*08449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
948*08449791SMatthew G. Knepley @*/
949*08449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
950*08449791SMatthew G. Knepley {
951*08449791SMatthew G. Knepley   DM             dmAux;
952*08449791SMatthew G. Knepley   PetscSection   section, sectionAux;
953*08449791SMatthew G. Knepley   PetscDS        prob;
954*08449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
955*08449791SMatthew G. Knepley   PetscErrorCode ierr;
956*08449791SMatthew G. Knepley 
957*08449791SMatthew G. Knepley   PetscFunctionBegin;
958*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
959*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
960*08449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
961*08449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
962*08449791SMatthew G. Knepley   PetscValidPointer(u, 7);
963*08449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
964*08449791SMatthew 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);
968*08449791SMatthew G. Knepley   if (locA) {
969*08449791SMatthew G. Knepley     PetscDS probAux;
970*08449791SMatthew G. Knepley 
971*08449791SMatthew 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   }
976*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr);
977*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, locX_t ? numCells*totDim : 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);
978*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, locA ? numCells*totDimAux : 0, PETSC_SCALAR, a);CHKERRQ(ierr);
97924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
980*08449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
98124cdb843SMatthew G. Knepley     PetscInt     i;
98224cdb843SMatthew G. Knepley 
983*08449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
984*08449791SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[c*totDim+i] = x[i];
985*08449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
986*08449791SMatthew G. Knepley     if (locX_t) {
987*08449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
988*08449791SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[c*totDim+i] = x_t[i];
989*08449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
99024cdb843SMatthew G. Knepley     }
991*08449791SMatthew G. Knepley     if (locA) {
992*08449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
993*08449791SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[c*totDimAux+i] = x[i];
994*08449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
99524cdb843SMatthew G. Knepley     }
99624cdb843SMatthew G. Knepley   }
997*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
998*08449791SMatthew G. Knepley }
99924cdb843SMatthew G. Knepley 
1000*08449791SMatthew G. Knepley #undef __FUNCT__
1001*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreCellFields"
1002*08449791SMatthew G. Knepley /*@C
1003*08449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
1004*08449791SMatthew G. Knepley 
1005*08449791SMatthew G. Knepley   Input Parameters:
1006*08449791SMatthew G. Knepley + dm     - The DM
1007*08449791SMatthew G. Knepley . cStart - The first cell to include
1008*08449791SMatthew G. Knepley . cEnd   - The first cell to exclude
1009*08449791SMatthew G. Knepley . locX   - A local vector with the solution fields
1010*08449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
1011*08449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
1012*08449791SMatthew G. Knepley 
1013*08449791SMatthew G. Knepley   Output Parameters:
1014*08449791SMatthew G. Knepley + u   - The field coefficients
1015*08449791SMatthew G. Knepley . u_t - The fields derivative coefficients
1016*08449791SMatthew G. Knepley - a   - The auxiliary field coefficients
1017*08449791SMatthew G. Knepley 
1018*08449791SMatthew G. Knepley   Level: developer
1019*08449791SMatthew G. Knepley 
1020*08449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
1021*08449791SMatthew G. Knepley @*/
1022*08449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
1023*08449791SMatthew G. Knepley {
1024*08449791SMatthew G. Knepley   PetscErrorCode ierr;
1025*08449791SMatthew G. Knepley 
1026*08449791SMatthew G. Knepley   PetscFunctionBegin;
1027*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr);
1028*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);
1029*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);
1030*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
103124cdb843SMatthew G. Knepley }
1032*08449791SMatthew G. Knepley 
1033*08449791SMatthew G. Knepley #undef __FUNCT__
1034*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceFields"
1035*08449791SMatthew G. Knepley /*@C
1036*08449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
1037*08449791SMatthew G. Knepley 
1038*08449791SMatthew G. Knepley   Input Parameters:
1039*08449791SMatthew G. Knepley + dm     - The DM
1040*08449791SMatthew G. Knepley . fStart - The first face to include
1041*08449791SMatthew G. Knepley . fEnd   - The first face to exclude
1042*08449791SMatthew G. Knepley . locX   - A local vector with the solution fields
1043*08449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
1044*08449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
1045*08449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
1046*08449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
1047*08449791SMatthew G. Knepley 
1048*08449791SMatthew G. Knepley   Output Parameters:
1049*08449791SMatthew G. Knepley + uL - The field coefficients at the left side of the face
1050*08449791SMatthew G. Knepley - uR - The field coefficients at the right side of the face
1051*08449791SMatthew G. Knepley 
1052*08449791SMatthew G. Knepley   Level: developer
1053*08449791SMatthew G. Knepley 
1054*08449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
1055*08449791SMatthew G. Knepley @*/
1056*08449791SMatthew 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)
1057*08449791SMatthew G. Knepley {
1058*08449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
1059*08449791SMatthew G. Knepley   PetscDS            prob;
1060*08449791SMatthew G. Knepley   DMLabel            ghostLabel;
1061*08449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1062*08449791SMatthew G. Knepley   PetscInt           dim, totDim, numFaces = fEnd - fStart, iface, face;
1063*08449791SMatthew G. Knepley   PetscErrorCode     ierr;
1064*08449791SMatthew G. Knepley 
1065*08449791SMatthew G. Knepley   PetscFunctionBegin;
1066*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1067*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
1068*08449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
1069*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
1070*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
1071*08449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
1072*08449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
1073*08449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
1074*08449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1075*08449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1076*08449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1077*08449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1078*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
1079*08449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1080*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1081*08449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1082*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1083*08449791SMatthew G. Knepley   if (locGrad) {
1084*08449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
1085*08449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
108624cdb843SMatthew G. Knepley   }
1087*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, uL);CHKERRQ(ierr);
1088*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, uR);CHKERRQ(ierr);
1089*08449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
1090*08449791SMatthew G. Knepley     const PetscInt        *cells;
1091*08449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
1092*08449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
1093*08449791SMatthew G. Knepley     const PetscScalar     *xL, *xR, *gL, *gR;
1094*08449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
1095*08449791SMatthew G. Knepley     PetscInt               ghost, d;
1096*08449791SMatthew G. Knepley 
1097*08449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1098*08449791SMatthew G. Knepley     if (ghost >= 0) continue;
1099*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
1100*08449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1101*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
1102*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1103*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cells[0], x, &xL);CHKERRQ(ierr);
1104*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cells[1], x, &xR);CHKERRQ(ierr);
1105*08449791SMatthew G. Knepley     if (dmGrad) {
1106*08449791SMatthew G. Knepley       PetscReal dxL[3], dxR[3];
1107*08449791SMatthew G. Knepley 
1108*08449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
1109*08449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
1110*08449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
1111*08449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1112*08449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
1113*08449791SMatthew G. Knepley         uLl[iface*totDim+d] = xL[d] + DMPlex_DotD_Internal(dim, &gL[d*dim], dxL);
1114*08449791SMatthew G. Knepley         uRl[iface*totDim+d] = xR[d] + DMPlex_DotD_Internal(dim, &gR[d*dim], dxR);
1115*08449791SMatthew G. Knepley       }
1116*08449791SMatthew G. Knepley     } else {
1117*08449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
1118*08449791SMatthew G. Knepley         uLl[iface*totDim+d] = xL[d];
1119*08449791SMatthew G. Knepley         uRl[iface*totDim+d] = xR[d];
1120*08449791SMatthew G. Knepley       }
1121*08449791SMatthew G. Knepley     }
1122*08449791SMatthew G. Knepley     ++iface;
1123*08449791SMatthew G. Knepley   }
1124*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
1125*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1126*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1127*08449791SMatthew G. Knepley   if (locGrad) {
1128*08449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1129*08449791SMatthew G. Knepley   }
1130*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1131*08449791SMatthew G. Knepley }
1132*08449791SMatthew G. Knepley 
1133*08449791SMatthew G. Knepley #undef __FUNCT__
1134*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceFields"
1135*08449791SMatthew G. Knepley /*@C
1136*08449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
1137*08449791SMatthew G. Knepley 
1138*08449791SMatthew G. Knepley   Input Parameters:
1139*08449791SMatthew G. Knepley + dm     - The DM
1140*08449791SMatthew G. Knepley . fStart - The first face to include
1141*08449791SMatthew G. Knepley . fEnd   - The first face to exclude
1142*08449791SMatthew G. Knepley . locX   - A local vector with the solution fields
1143*08449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
1144*08449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
1145*08449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
1146*08449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
1147*08449791SMatthew G. Knepley 
1148*08449791SMatthew G. Knepley   Output Parameters:
1149*08449791SMatthew G. Knepley + uL - The field coefficients at the left side of the face
1150*08449791SMatthew G. Knepley - uR - The field coefficients at the right side of the face
1151*08449791SMatthew G. Knepley 
1152*08449791SMatthew G. Knepley   Level: developer
1153*08449791SMatthew G. Knepley 
1154*08449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
1155*08449791SMatthew G. Knepley @*/
1156*08449791SMatthew 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)
1157*08449791SMatthew G. Knepley {
1158*08449791SMatthew G. Knepley   PetscErrorCode ierr;
1159*08449791SMatthew G. Knepley 
1160*08449791SMatthew G. Knepley   PetscFunctionBegin;
1161*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr);
1162*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr);
1163*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1164*08449791SMatthew G. Knepley }
1165*08449791SMatthew G. Knepley 
1166*08449791SMatthew G. Knepley #undef __FUNCT__
1167*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFaceGeometry"
1168*08449791SMatthew G. Knepley /*@C
1169*08449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
1170*08449791SMatthew G. Knepley 
1171*08449791SMatthew G. Knepley   Input Parameters:
1172*08449791SMatthew G. Knepley + dm     - The DM
1173*08449791SMatthew G. Knepley . fStart - The first face to include
1174*08449791SMatthew G. Knepley . fEnd   - The first face to exclude
1175*08449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
1176*08449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
1177*08449791SMatthew G. Knepley 
1178*08449791SMatthew G. Knepley   Output Parameters:
1179*08449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
1180*08449791SMatthew G. Knepley - vol   - The cell volume
1181*08449791SMatthew G. Knepley 
1182*08449791SMatthew G. Knepley   Level: developer
1183*08449791SMatthew G. Knepley 
1184*08449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
1185*08449791SMatthew G. Knepley @*/
1186*08449791SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
1187*08449791SMatthew G. Knepley {
1188*08449791SMatthew G. Knepley   DM                 dmFace, dmCell;
1189*08449791SMatthew G. Knepley   DMLabel            ghostLabel;
1190*08449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
1191*08449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
1192*08449791SMatthew G. Knepley   PetscErrorCode     ierr;
1193*08449791SMatthew G. Knepley 
1194*08449791SMatthew G. Knepley   PetscFunctionBegin;
1195*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1196*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
1197*08449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
1198*08449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
1199*08449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
1200*08449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1201*08449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1202*08449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1203*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1204*08449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1205*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1206*08449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
1207*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr);
1208*08449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
1209*08449791SMatthew G. Knepley     const PetscInt        *cells;
1210*08449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
1211*08449791SMatthew G. Knepley     const PetscFVCellGeom *cgL, *cgR;
1212*08449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
1213*08449791SMatthew G. Knepley     PetscScalar           *voll = *vol;
1214*08449791SMatthew G. Knepley     PetscInt               ghost, d;
1215*08449791SMatthew G. Knepley 
1216*08449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1217*08449791SMatthew G. Knepley     if (ghost >= 0) continue;
1218*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
1219*08449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1220*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
1221*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1222*08449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1223*08449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
1224*08449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
1225*08449791SMatthew G. Knepley     }
1226*08449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
1227*08449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
1228*08449791SMatthew G. Knepley     ++iface;
1229*08449791SMatthew G. Knepley   }
1230*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1231*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1232*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1233*08449791SMatthew G. Knepley }
1234*08449791SMatthew G. Knepley 
1235*08449791SMatthew G. Knepley #undef __FUNCT__
1236*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreFaceGeometry"
1237*08449791SMatthew G. Knepley /*@C
1238*08449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
1239*08449791SMatthew G. Knepley 
1240*08449791SMatthew G. Knepley   Input Parameters:
1241*08449791SMatthew G. Knepley + dm     - The DM
1242*08449791SMatthew G. Knepley . fStart - The first face to include
1243*08449791SMatthew G. Knepley . fEnd   - The first face to exclude
1244*08449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
1245*08449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
1246*08449791SMatthew G. Knepley 
1247*08449791SMatthew G. Knepley   Output Parameters:
1248*08449791SMatthew G. Knepley + fgeom - The extract the face centroid and normal
1249*08449791SMatthew G. Knepley - vol   - The cell volume
1250*08449791SMatthew G. Knepley 
1251*08449791SMatthew G. Knepley   Level: developer
1252*08449791SMatthew G. Knepley 
1253*08449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
1254*08449791SMatthew G. Knepley @*/
1255*08449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscFVFaceGeom **fgeom, PetscReal **vol)
1256*08449791SMatthew G. Knepley {
1257*08449791SMatthew G. Knepley   PetscErrorCode ierr;
1258*08449791SMatthew G. Knepley 
1259*08449791SMatthew G. Knepley   PetscFunctionBegin;
1260*08449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
1261*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr);
1262*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1263*08449791SMatthew G. Knepley }
1264*08449791SMatthew G. Knepley 
1265*08449791SMatthew G. Knepley #undef __FUNCT__
1266*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexReconstructGradients_Internal"
1267*08449791SMatthew G. Knepley PetscErrorCode DMPlexReconstructGradients_Internal(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, Vec locX, Vec grad)
1268*08449791SMatthew G. Knepley {
1269*08449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
1270*08449791SMatthew G. Knepley   DMLabel            ghostLabel;
1271*08449791SMatthew G. Knepley   PetscDS            prob;
1272*08449791SMatthew G. Knepley   PetscFV            fvm;
1273*08449791SMatthew G. Knepley   PetscLimiter       lim;
1274*08449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x;
1275*08449791SMatthew G. Knepley   PetscScalar       *gr;
1276*08449791SMatthew G. Knepley   PetscReal         *cellPhi;
1277*08449791SMatthew G. Knepley   PetscInt           dim, face, cell, totDim, cStart, cEnd, cEndInterior;
1278*08449791SMatthew G. Knepley   PetscErrorCode     ierr;
1279*08449791SMatthew G. Knepley 
1280*08449791SMatthew G. Knepley   PetscFunctionBegin;
1281*08449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1282*08449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1283*08449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1284*08449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1285*08449791SMatthew G. Knepley   ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fvm);CHKERRQ(ierr);
1286*08449791SMatthew G. Knepley   ierr = PetscFVGetLimiter(fvm, &lim);CHKERRQ(ierr);
1287*08449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
1288*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1289*08449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
1290*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1291*08449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
1292*08449791SMatthew G. Knepley   ierr = VecGetDM(grad, &dmGrad);CHKERRQ(ierr);
1293*08449791SMatthew G. Knepley   ierr = VecZeroEntries(grad);CHKERRQ(ierr);
1294*08449791SMatthew G. Knepley   ierr = VecGetArray(grad, &gr);CHKERRQ(ierr);
1295*08449791SMatthew G. Knepley   /* Reconstruct gradients */
1296*08449791SMatthew G. Knepley   for (face = fStart; face < fEnd; ++face) {
1297*08449791SMatthew G. Knepley     const PetscInt        *cells;
1298*08449791SMatthew G. Knepley     const PetscFVFaceGeom *fg;
1299*08449791SMatthew G. Knepley     const PetscScalar     *cx[2];
1300*08449791SMatthew G. Knepley     PetscScalar           *cgrad[2];
1301*08449791SMatthew G. Knepley     PetscBool              boundary;
1302*08449791SMatthew G. Knepley     PetscInt               ghost, c, pd, d;
1303*08449791SMatthew G. Knepley 
1304*08449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1305*08449791SMatthew G. Knepley     if (ghost >= 0) continue;
1306*08449791SMatthew G. Knepley     ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
1307*08449791SMatthew G. Knepley     if (boundary) continue;
1308*08449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1309*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
1310*08449791SMatthew G. Knepley     for (c = 0; c < 2; ++c) {
1311*08449791SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dm, cells[c], x, &cx[c]);CHKERRQ(ierr);
1312*08449791SMatthew G. Knepley       ierr = DMPlexPointGlobalRef(dmGrad, cells[c], gr, &cgrad[c]);CHKERRQ(ierr);
1313*08449791SMatthew G. Knepley     }
1314*08449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd) {
1315*08449791SMatthew G. Knepley       PetscScalar delta = cx[1][pd] - cx[0][pd];
1316*08449791SMatthew G. Knepley 
1317*08449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
1318*08449791SMatthew G. Knepley         if (cgrad[0]) cgrad[0][pd*dim+d] += fg->grad[0][d] * delta;
1319*08449791SMatthew G. Knepley         if (cgrad[1]) cgrad[1][pd*dim+d] -= fg->grad[1][d] * delta;
1320*08449791SMatthew G. Knepley       }
1321*08449791SMatthew G. Knepley     }
1322*08449791SMatthew G. Knepley   }
1323*08449791SMatthew G. Knepley   /* Limit interior gradients (using cell-based loop because it generalizes better to vector limiters) */
1324*08449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1325*08449791SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1326*08449791SMatthew G. Knepley   cEndInterior = cEndInterior < 0 ? cEnd : cEndInterior;
1327*08449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
1328*08449791SMatthew G. Knepley   for (cell = dmGrad && lim ? cStart : cEnd; cell < cEndInterior; ++cell) {
1329*08449791SMatthew G. Knepley     const PetscInt        *faces;
1330*08449791SMatthew G. Knepley     const PetscScalar     *cx;
1331*08449791SMatthew G. Knepley     const PetscFVCellGeom *cg;
1332*08449791SMatthew G. Knepley     PetscScalar           *cgrad;
1333*08449791SMatthew G. Knepley     PetscInt               coneSize, f, pd, d;
1334*08449791SMatthew G. Knepley 
1335*08449791SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1336*08449791SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &faces);CHKERRQ(ierr);
1337*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, cell, x, &cx);CHKERRQ(ierr);
1338*08449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cell, cellgeom, &cg);CHKERRQ(ierr);
1339*08449791SMatthew G. Knepley     ierr = DMPlexPointGlobalRef(dmGrad, cell, gr, &cgrad);CHKERRQ(ierr);
1340*08449791SMatthew G. Knepley     if (!cgrad) continue; /* Unowned overlap cell, we do not compute */
1341*08449791SMatthew G. Knepley     /* Limiter will be minimum value over all neighbors */
1342*08449791SMatthew G. Knepley     for (d = 0; d < totDim; ++d) cellPhi[d] = PETSC_MAX_REAL;
1343*08449791SMatthew G. Knepley     for (f = 0; f < coneSize; ++f) {
1344*08449791SMatthew G. Knepley       const PetscScalar     *ncx;
1345*08449791SMatthew G. Knepley       const PetscFVCellGeom *ncg;
1346*08449791SMatthew G. Knepley       const PetscInt        *fcells;
1347*08449791SMatthew G. Knepley       PetscInt               face = faces[f], ncell, ghost;
1348*08449791SMatthew G. Knepley       PetscReal              v[3];
1349*08449791SMatthew G. Knepley       PetscBool              boundary;
1350*08449791SMatthew G. Knepley 
1351*08449791SMatthew G. Knepley       ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1352*08449791SMatthew G. Knepley       ierr = DMPlexIsBoundaryPoint(dm, face, &boundary);CHKERRQ(ierr);
1353*08449791SMatthew G. Knepley       if ((ghost >= 0) || boundary) continue;
1354*08449791SMatthew G. Knepley       ierr  = DMPlexGetSupport(dm, face, &fcells);CHKERRQ(ierr);
1355*08449791SMatthew G. Knepley       ncell = cell == fcells[0] ? fcells[1] : fcells[0];
1356*08449791SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dm, ncell, x, &ncx);CHKERRQ(ierr);
1357*08449791SMatthew G. Knepley       ierr  = DMPlexPointLocalRead(dmCell, ncell, cellgeom, &ncg);CHKERRQ(ierr);
1358*08449791SMatthew G. Knepley       DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, ncg->centroid, v);
1359*08449791SMatthew G. Knepley       for (d = 0; d < totDim; ++d) {
1360*08449791SMatthew G. Knepley         /* We use the symmetric slope limited form of Berger, Aftosmis, and Murman 2005 */
1361*08449791SMatthew G. Knepley         PetscReal phi, flim = 0.5 * PetscRealPart(ncx[d] - cx[d]) / DMPlex_DotD_Internal(dim, &cgrad[d*dim], v);
1362*08449791SMatthew G. Knepley 
1363*08449791SMatthew G. Knepley         ierr = PetscLimiterLimit(lim, flim, &phi);CHKERRQ(ierr);
1364*08449791SMatthew G. Knepley         cellPhi[d] = PetscMin(cellPhi[d], phi);
1365*08449791SMatthew G. Knepley       }
1366*08449791SMatthew G. Knepley     }
1367*08449791SMatthew G. Knepley     /* Apply limiter to gradient */
1368*08449791SMatthew G. Knepley     for (pd = 0; pd < totDim; ++pd)
1369*08449791SMatthew G. Knepley       /* Scalar limiter applied to each component separately */
1370*08449791SMatthew G. Knepley       for (d = 0; d < dim; ++d) cgrad[pd*dim+d] *= cellPhi[pd];
1371*08449791SMatthew G. Knepley   }
1372*08449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, totDim, PETSC_REAL, &cellPhi);CHKERRQ(ierr);
1373*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
1374*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
1375*08449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
1376*08449791SMatthew G. Knepley   ierr = VecRestoreArray(grad, &gr);CHKERRQ(ierr);
1377*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1378*08449791SMatthew G. Knepley }
1379*08449791SMatthew G. Knepley 
1380*08449791SMatthew G. Knepley #undef __FUNCT__
1381*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeBdResidual_Internal"
1382*08449791SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, Vec locF, void *user)
1383*08449791SMatthew G. Knepley {
1384*08449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
1385*08449791SMatthew G. Knepley   PetscSection     section;
1386*08449791SMatthew G. Knepley   PetscDS          prob;
1387*08449791SMatthew G. Knepley   DMLabel          depth;
1388*08449791SMatthew G. Knepley   PetscFECellGeom *cgeom;
1389*08449791SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *elemVec = NULL;
1390*08449791SMatthew G. Knepley   PetscInt         dim, Nf, f, totDimBd, numBd, bd;
1391*08449791SMatthew G. Knepley   PetscErrorCode   ierr;
1392*08449791SMatthew G. Knepley 
1393*08449791SMatthew G. Knepley   PetscFunctionBegin;
1394*08449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1395*08449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1396*08449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1397*08449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1398*08449791SMatthew 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);
1422*08449791SMatthew 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);
1433*08449791SMatthew 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];
1435*08449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX, point, NULL, &x);CHKERRQ(ierr);
1436*08449791SMatthew G. Knepley       if (locX_t) {
1437*08449791SMatthew 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];
1439*08449791SMatthew 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;
1445*08449791SMatthew 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);}
1473*08449791SMatthew 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);
1479*08449791SMatthew G. Knepley     if (locX_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
148024cdb843SMatthew G. Knepley   }
1481*08449791SMatthew G. Knepley   PetscFunctionReturn(0);
1482*08449791SMatthew G. Knepley }
1483*08449791SMatthew G. Knepley 
1484*08449791SMatthew G. Knepley #undef __FUNCT__
1485*08449791SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal"
1486*08449791SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscReal time, Vec locX, Vec locX_t, Vec locF, void *user)
1487*08449791SMatthew G. Knepley {
1488*08449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
1489*08449791SMatthew G. Knepley   const char       *name       = "Residual";
1490*08449791SMatthew G. Knepley   DM                dmAux      = NULL;
1491*08449791SMatthew G. Knepley   DM                dmGrad     = NULL;
1492*08449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
1493*08449791SMatthew G. Knepley   PetscDS           prob       = NULL;
1494*08449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
1495*08449791SMatthew G. Knepley   PetscSection      section    = NULL;
1496*08449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
1497*08449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
1498*08449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
1499*08449791SMatthew G. Knepley   PetscFECellGeom  *cgeomFEM   = NULL;
1500*08449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
1501*08449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
1502*08449791SMatthew G. Knepley   Vec               locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
1503*08449791SMatthew G. Knepley   PetscScalar      *u, *u_t, *a, *uL, *uR;
1504*08449791SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, cStart, cEnd, fStart, fEnd;
1505*08449791SMatthew G. Knepley   PetscErrorCode    ierr;
1506*08449791SMatthew G. Knepley 
1507*08449791SMatthew G. Knepley   PetscFunctionBegin;
1508*08449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1509*08449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1510*08449791SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the preclac functions return exactly what we need */
1511*08449791SMatthew G. Knepley   /* FEM+FVM */
1512*08449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
1513*08449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1514*08449791SMatthew G. Knepley   ierr = DMPlexGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
1515*08449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1516*08449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1517*08449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1518*08449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1519*08449791SMatthew G. Knepley   if (locA) {
1520*08449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
1521*08449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1522*08449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1523*08449791SMatthew G. Knepley   }
1524*08449791SMatthew G. Knepley   /* 2: Get geometric data */
1525*08449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1526*08449791SMatthew G. Knepley     PetscObject  obj;
1527*08449791SMatthew G. Knepley     PetscClassId id;
1528*08449791SMatthew G. Knepley 
1529*08449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1530*08449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1531*08449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
1532*08449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
1533*08449791SMatthew G. Knepley   }
1534*08449791SMatthew G. Knepley   if (useFEM) {
1535*08449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr);
1536*08449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFEM, (const PetscScalar **) &cgeomFEM);CHKERRQ(ierr);
1537*08449791SMatthew G. Knepley   }
1538*08449791SMatthew G. Knepley   if (useFVM) {
1539*08449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
1540*08449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
1541*08449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1542*08449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1543*08449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
1544*08449791SMatthew G. Knepley     if (dmGrad) {
1545*08449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1546*08449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1547*08449791SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1548*08449791SMatthew G. Knepley       /* Communicate gradient values */
1549*08449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1550*08449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1551*08449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1552*08449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1553*08449791SMatthew G. Knepley     }
1554*08449791SMatthew G. Knepley   }
1555*08449791SMatthew G. Knepley   /* Handle boundary values */
1556*08449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1557*08449791SMatthew G. Knepley   /* Loop over chunks */
1558*08449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1559*08449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1560*08449791SMatthew G. Knepley   numChunks     = 1;
1561*08449791SMatthew G. Knepley   cellChunkSize = (cEnd - cStart)/numChunks;
1562*08449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
1563*08449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
1564*08449791SMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR, *vol;
1565*08449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
1566*08449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
1567*08449791SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = fE - fS, face;
1568*08449791SMatthew G. Knepley 
1569*08449791SMatthew G. Knepley     /* Extract field coefficients */
1570*08449791SMatthew G. Knepley     if (useFEM) {
1571*08449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
1572*08449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
1573*08449791SMatthew G. Knepley     }
1574*08449791SMatthew G. Knepley     if (useFVM) {
1575*08449791SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
1576*08449791SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
1577*08449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
1578*08449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
1579*08449791SMatthew G. Knepley     }
1580*08449791SMatthew 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 */
1581*08449791SMatthew G. Knepley     /* Loop over fields */
1582*08449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1583*08449791SMatthew G. Knepley       PetscObject  obj;
1584*08449791SMatthew G. Knepley       PetscClassId id;
1585*08449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
1586*08449791SMatthew G. Knepley 
1587*08449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1588*08449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1589*08449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
1590*08449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
1591*08449791SMatthew G. Knepley         PetscQuadrature q;
1592*08449791SMatthew G. Knepley         PetscInt        Nq, Nb;
1593*08449791SMatthew G. Knepley 
1594*08449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1595*08449791SMatthew G. Knepley 
1596*08449791SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1597*08449791SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1598*08449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1599*08449791SMatthew G. Knepley         blockSize = Nb*Nq;
1600*08449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
1601*08449791SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1602*08449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
1603*08449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
1604*08449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
1605*08449791SMatthew G. Knepley         offset    = numCells - Nr;
1606*08449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
1607*08449791SMatthew 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) */
1608*08449791SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
1609*08449791SMatthew 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);
1610*08449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
1611*08449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
1612*08449791SMatthew G. Knepley 
1613*08449791SMatthew G. Knepley         Ne = numFaces;
1614*08449791SMatthew G. Knepley         Nr = 0;
1615*08449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
1616*08449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
1617*08449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
1618*08449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1619*08449791SMatthew G. Knepley     }
1620*08449791SMatthew G. Knepley     /* Loop over domain */
1621*08449791SMatthew G. Knepley     if (useFEM) {
1622*08449791SMatthew G. Knepley       /* Add elemVec to locX */
1623*08449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
1624*08449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
1625*08449791SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_VALUES);CHKERRQ(ierr);
1626*08449791SMatthew G. Knepley       }
1627*08449791SMatthew G. Knepley     }
1628*08449791SMatthew G. Knepley     if (useFVM) {
1629*08449791SMatthew G. Knepley       PetscScalar *fa;
1630*08449791SMatthew G. Knepley       PetscInt     iface;
1631*08449791SMatthew G. Knepley 
1632*08449791SMatthew G. Knepley       /* Accumulate fluxes to cells */
1633*08449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1634*08449791SMatthew G. Knepley       for (face = fS, iface = 0; face < fE; ++face) {
1635*08449791SMatthew G. Knepley         const PetscInt *cells;
1636*08449791SMatthew G. Knepley         PetscScalar    *fL, *fR;
1637*08449791SMatthew G. Knepley         PetscInt        ghost, d;
1638*08449791SMatthew G. Knepley 
1639*08449791SMatthew G. Knepley         ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1640*08449791SMatthew G. Knepley         if (ghost >= 0) continue;
1641*08449791SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1642*08449791SMatthew G. Knepley         ierr = DMPlexPointGlobalRef(dm, cells[0], fa, &fL);CHKERRQ(ierr);
1643*08449791SMatthew G. Knepley         ierr = DMPlexPointGlobalRef(dm, cells[1], fa, &fR);CHKERRQ(ierr);
1644*08449791SMatthew G. Knepley         for (d = 0; d < totDim; ++d) {
1645*08449791SMatthew G. Knepley           if (fL) fL[d] -= fluxL[iface*totDim+d];
1646*08449791SMatthew G. Knepley           if (fR) fR[d] += fluxR[iface*totDim+d];
1647*08449791SMatthew G. Knepley         }
1648*08449791SMatthew G. Knepley         ++iface;
1649*08449791SMatthew G. Knepley       }
1650*08449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1651*08449791SMatthew G. Knepley     }
1652*08449791SMatthew G. Knepley     if (useFEM) {
1653*08449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
1654*08449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
1655*08449791SMatthew G. Knepley     }
1656*08449791SMatthew G. Knepley     if (useFVM) {
1657*08449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &uL, &uR);CHKERRQ(ierr);
1658*08449791SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &fgeom, &vol);CHKERRQ(ierr);
1659*08449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
1660*08449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
1661*08449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
1662*08449791SMatthew G. Knepley     }
1663*08449791SMatthew G. Knepley   }
1664*08449791SMatthew G. Knepley 
1665*08449791SMatthew G. Knepley   if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, locF, user);CHKERRQ(ierr);}
1666*08449791SMatthew G. Knepley 
1667*08449791SMatthew G. Knepley   /* FEM */
1668*08449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
1669*08449791SMatthew G. Knepley   /* 2: Get geometric data */
1670*08449791SMatthew G. Knepley   /* 3: Handle boundary values */
1671*08449791SMatthew G. Knepley   /* 4: Loop over domain */
1672*08449791SMatthew G. Knepley   /*   Extract coefficients */
1673*08449791SMatthew G. Knepley   /* Loop over fields */
1674*08449791SMatthew G. Knepley   /*   Set tiling for FE*/
1675*08449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
1676*08449791SMatthew G. Knepley   /*     Loop over subdomain */
1677*08449791SMatthew G. Knepley   /*       Loop over quad points */
1678*08449791SMatthew G. Knepley   /*         Transform coords to real space */
1679*08449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
1680*08449791SMatthew G. Knepley   /*         Evaluate residual at point */
1681*08449791SMatthew G. Knepley   /*         Transform residual to real space */
1682*08449791SMatthew G. Knepley   /*       Add residual to elemVec */
1683*08449791SMatthew G. Knepley   /* Loop over domain */
1684*08449791SMatthew G. Knepley   /*   Add elemVec to locX */
1685*08449791SMatthew G. Knepley 
1686*08449791SMatthew G. Knepley   /* FVM */
1687*08449791SMatthew G. Knepley   /* Get geometric data */
1688*08449791SMatthew G. Knepley   /* If using gradients */
1689*08449791SMatthew G. Knepley   /*   Compute gradient data */
1690*08449791SMatthew G. Knepley   /*   Loop over domain faces */
1691*08449791SMatthew G. Knepley   /*     Count computational faces */
1692*08449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
1693*08449791SMatthew G. Knepley   /*   Loop over domain cells */
1694*08449791SMatthew G. Knepley   /*     Limit cell gradients */
1695*08449791SMatthew G. Knepley   /* Handle boundary values */
1696*08449791SMatthew G. Knepley   /* Loop over domain faces */
1697*08449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
1698*08449791SMatthew G. Knepley   /* Riemann solve over faces */
1699*08449791SMatthew G. Knepley   /* Loop over domain faces */
1700*08449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
1701*08449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
1702*08449791SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, locF);CHKERRQ(ierr);}
170324cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
170424cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
170524cdb843SMatthew G. Knepley }
170624cdb843SMatthew G. Knepley 
170724cdb843SMatthew G. Knepley #undef __FUNCT__
170824cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal"
170924cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
171024cdb843SMatthew G. Knepley {
171124cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1712bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
171324cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
171424cdb843SMatthew G. Knepley   PetscQuadrature   q;
171524cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
1716bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
171724cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
171824cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
171924cdb843SMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
172024cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
172124cdb843SMatthew G. Knepley 
172224cdb843SMatthew G. Knepley   PetscFunctionBegin;
172324cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
172424cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
172524cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
172624cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
172724cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
172824cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
172924cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
173024cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
173124cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
173224cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
173324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
173424cdb843SMatthew G. Knepley   if (dmAux) {
173524cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
173624cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
173724cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
173824cdb843SMatthew G. Knepley   }
1739*08449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
174024cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1741bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
174224cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
174324cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1744bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
1745bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
174624cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
174724cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
174824cdb843SMatthew G. Knepley     PetscInt     i;
174924cdb843SMatthew G. Knepley 
175024cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
175124cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
175224cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
175324cdb843SMatthew G. Knepley     if (X_t) {
175424cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
175524cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
175624cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
175724cdb843SMatthew G. Knepley     }
175824cdb843SMatthew G. Knepley     if (dmAux) {
175924cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
176024cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
176124cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
176224cdb843SMatthew G. Knepley     }
176324cdb843SMatthew G. Knepley   }
176424cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
176524cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
176624cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
176724cdb843SMatthew G. Knepley     /* Conforming batches */
176824cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
176924cdb843SMatthew G. Knepley     /* Remainder */
177024cdb843SMatthew G. Knepley     PetscInt Nr, offset;
177124cdb843SMatthew G. Knepley 
177224cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
177324cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
177424cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
177524cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
177624cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
177724cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
177824cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
177924cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
178024cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
178124cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
178224cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
178324cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
178424cdb843SMatthew G. Knepley     offset    = numCells - Nr;
1785bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
1786bbce034cSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
1787bbce034cSMatthew 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);
1788bbce034cSMatthew 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);
178924cdb843SMatthew G. Knepley   }
179024cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
179124cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
179224cdb843SMatthew G. Knepley     PetscInt  d;
179324cdb843SMatthew G. Knepley 
179424cdb843SMatthew 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;}
179524cdb843SMatthew G. Knepley     if (diff) {
179624cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
179724cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
179824cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
179924cdb843SMatthew G. Knepley       ++diffCell;
180024cdb843SMatthew G. Knepley     }
180124cdb843SMatthew G. Knepley     if (diffCell > 9) break;
180224cdb843SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
180324cdb843SMatthew G. Knepley   }
1804bbce034cSMatthew G. Knepley   ierr = VecRestoreArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
1805bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
180624cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
180724cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
180824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
180924cdb843SMatthew G. Knepley }
181024cdb843SMatthew G. Knepley 
181124cdb843SMatthew G. Knepley #undef __FUNCT__
181224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
181324cdb843SMatthew G. Knepley /*@
181424cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
181524cdb843SMatthew G. Knepley 
181624cdb843SMatthew G. Knepley   Input Parameters:
181724cdb843SMatthew G. Knepley + dm - The mesh
181824cdb843SMatthew G. Knepley . X  - Local solution
181924cdb843SMatthew G. Knepley - user - The user context
182024cdb843SMatthew G. Knepley 
182124cdb843SMatthew G. Knepley   Output Parameter:
182224cdb843SMatthew G. Knepley . F  - Local output vector
182324cdb843SMatthew G. Knepley 
182424cdb843SMatthew G. Knepley   Level: developer
182524cdb843SMatthew G. Knepley 
182624cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
182724cdb843SMatthew G. Knepley @*/
182824cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
182924cdb843SMatthew G. Knepley {
183024cdb843SMatthew G. Knepley   PetscObject    check;
183124cdb843SMatthew G. Knepley   PetscErrorCode ierr;
183224cdb843SMatthew G. Knepley 
183324cdb843SMatthew G. Knepley   PetscFunctionBegin;
183424cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
183524cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
183624cdb843SMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
1837*08449791SMatthew G. Knepley   else       {ierr = DMPlexComputeResidual_Internal(dm, 0.0, X, NULL, F, user);CHKERRQ(ierr);}
183824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
183924cdb843SMatthew G. Knepley }
184024cdb843SMatthew G. Knepley 
184124cdb843SMatthew G. Knepley #undef __FUNCT__
184224cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
184324cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
184424cdb843SMatthew G. Knepley {
184524cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
184624cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
184724cdb843SMatthew G. Knepley   DM                dmAux;
184824cdb843SMatthew G. Knepley   DMLabel           depth;
1849bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
185024cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
185124cdb843SMatthew G. Knepley   PetscQuadrature   quad;
185224cdb843SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
1853bbce034cSMatthew G. Knepley   PetscFECellGeom  *cgeom;
185424cdb843SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
185524cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
185624cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
185724cdb843SMatthew G. Knepley   PetscBool         isShell;
185824cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
185924cdb843SMatthew G. Knepley 
186024cdb843SMatthew G. Knepley   PetscFunctionBegin;
186124cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
186224cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
186324cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
186424cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
186524cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
186624cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
186724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
186824cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
186924cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
187024cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
187124cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
187224cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
187324cdb843SMatthew G. Knepley   if (dmAux) {
187424cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
187524cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
187624cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
187724cdb843SMatthew G. Knepley   }
1878*08449791SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, X, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
187924cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
1880bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
188124cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1882bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
1883bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
188424cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
188524cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
188624cdb843SMatthew G. Knepley     PetscInt     i;
188724cdb843SMatthew G. Knepley 
188824cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
188924cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
189024cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
189124cdb843SMatthew G. Knepley     if (X_t) {
189224cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
189324cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
189424cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
189524cdb843SMatthew G. Knepley     }
189624cdb843SMatthew G. Knepley     if (dmAux) {
189724cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
189824cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
189924cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
190024cdb843SMatthew G. Knepley     }
190124cdb843SMatthew G. Knepley   }
190224cdb843SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
190324cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
190424cdb843SMatthew G. Knepley     PetscFE  fe;
190524cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
190624cdb843SMatthew G. Knepley     /* Conforming batches */
190724cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
190824cdb843SMatthew G. Knepley     /* Remainder */
190924cdb843SMatthew G. Knepley     PetscInt Nr, offset;
191024cdb843SMatthew G. Knepley 
191124cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
191224cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
191324cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
191424cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
191524cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
191624cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
191724cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
191824cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
191924cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
192024cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
192124cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
192224cdb843SMatthew G. Knepley     offset    = numCells - Nr;
192324cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1924bbce034cSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1925bbce034cSMatthew 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);
192624cdb843SMatthew G. Knepley     }
192724cdb843SMatthew G. Knepley   }
192824cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
192924cdb843SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
193024cdb843SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
193124cdb843SMatthew G. Knepley   }
1932bbce034cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, (PetscScalar **) &cgeom);CHKERRQ(ierr);
1933bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr);
193424cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
193524cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
193624cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
193724cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
193824cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
193924cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
194024cdb843SMatthew G. Knepley     const char     *bdLabel;
194124cdb843SMatthew G. Knepley     DMLabel         label;
194224cdb843SMatthew G. Knepley     IS              pointIS;
194324cdb843SMatthew G. Knepley     const PetscInt *points;
194424cdb843SMatthew G. Knepley     const PetscInt *values;
194524cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
194624cdb843SMatthew G. Knepley     PetscBool       isEssential;
194724cdb843SMatthew G. Knepley 
194824cdb843SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
194924cdb843SMatthew G. Knepley     if (isEssential) continue;
195024cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
195124cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
195224cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
195324cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
195424cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
195524cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
195624cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
195724cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
195824cdb843SMatthew G. Knepley     }
1959bbce034cSMatthew G. Knepley     ierr = PetscMalloc3(numFaces*totDimBd,&u,numFaces,&cgeom,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
196024cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
196124cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
196224cdb843SMatthew G. Knepley       const PetscInt point = points[p];
196324cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
196424cdb843SMatthew G. Knepley       PetscInt       i;
196524cdb843SMatthew G. Knepley 
196624cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
196724cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1968bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, cgeom[f].v0, cgeom[f].J, cgeom[f].invJ, &cgeom[f].detJ);CHKERRQ(ierr);
1969bbce034cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, cgeom[f].n);
1970bbce034cSMatthew 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);
197124cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
197224cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
197324cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
197424cdb843SMatthew G. Knepley       if (X_t) {
197524cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
197624cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
197724cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
197824cdb843SMatthew G. Knepley       }
197924cdb843SMatthew G. Knepley       ++f;
198024cdb843SMatthew G. Knepley     }
198124cdb843SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
198224cdb843SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
198324cdb843SMatthew G. Knepley       PetscFE  fe;
198424cdb843SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
198524cdb843SMatthew G. Knepley       /* Conforming batches */
198624cdb843SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
198724cdb843SMatthew G. Knepley       /* Remainder */
198824cdb843SMatthew G. Knepley       PetscInt Nr, offset;
198924cdb843SMatthew G. Knepley 
199024cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
199124cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
199224cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
199324cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
199424cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
199524cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
199624cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
199724cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
199824cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
199924cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
200024cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
200124cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
200224cdb843SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2003bbce034cSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, cgeom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
2004bbce034cSMatthew 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);
200524cdb843SMatthew G. Knepley       }
200624cdb843SMatthew G. Knepley     }
200724cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
200824cdb843SMatthew G. Knepley       const PetscInt point = points[p];
200924cdb843SMatthew G. Knepley 
201024cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
201124cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
201224cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
201324cdb843SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
201424cdb843SMatthew G. Knepley       ++f;
201524cdb843SMatthew G. Knepley     }
201624cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
201724cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2018bbce034cSMatthew G. Knepley     ierr = PetscFree3(u,cgeom,elemMat);CHKERRQ(ierr);
201924cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
202024cdb843SMatthew G. Knepley   }
202124cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
202224cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
202324cdb843SMatthew G. Knepley   if (mesh->printFEM) {
202424cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
202524cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
202624cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
202724cdb843SMatthew G. Knepley   }
202824cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
202924cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
203024cdb843SMatthew G. Knepley   if (isShell) {
203124cdb843SMatthew G. Knepley     JacActionCtx *jctx;
203224cdb843SMatthew G. Knepley 
203324cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
203424cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
203524cdb843SMatthew G. Knepley   }
203624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
203724cdb843SMatthew G. Knepley }
203824cdb843SMatthew G. Knepley 
203924cdb843SMatthew G. Knepley #undef __FUNCT__
204024cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
204124cdb843SMatthew G. Knepley /*@
204224cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
204324cdb843SMatthew G. Knepley 
204424cdb843SMatthew G. Knepley   Input Parameters:
204524cdb843SMatthew G. Knepley + dm - The mesh
204624cdb843SMatthew G. Knepley . X  - Local input vector
204724cdb843SMatthew G. Knepley - user - The user context
204824cdb843SMatthew G. Knepley 
204924cdb843SMatthew G. Knepley   Output Parameter:
205024cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
205124cdb843SMatthew G. Knepley 
205224cdb843SMatthew G. Knepley   Note:
205324cdb843SMatthew G. Knepley   The first member of the user context must be an FEMContext.
205424cdb843SMatthew G. Knepley 
205524cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
205624cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
205724cdb843SMatthew G. Knepley 
205824cdb843SMatthew G. Knepley   Level: developer
205924cdb843SMatthew G. Knepley 
206024cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
206124cdb843SMatthew G. Knepley @*/
206224cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
206324cdb843SMatthew G. Knepley {
206424cdb843SMatthew G. Knepley   PetscErrorCode ierr;
206524cdb843SMatthew G. Knepley 
206624cdb843SMatthew G. Knepley   PetscFunctionBegin;
206724cdb843SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
206824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
206924cdb843SMatthew G. Knepley }
2070