xref: /petsc/src/snes/utils/dmplexsnes.c (revision 741db25d7eadfc751402c7231710fb0c4faa9e27)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I "petscdmplex.h" I*/
2af0996ceSBarry Smith #include <petsc/private/snesimpl.h>     /*I "petscsnes.h"   I*/
324cdb843SMatthew G. Knepley #include <petscds.h>
4a925c78cSMatthew G. Knepley #include <petscblaslapack.h>
5af0996ceSBarry Smith #include <petsc/private/petscimpl.h>
6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
7552f7358SJed Brown 
824cdb843SMatthew G. Knepley /************************** Interpolation *******************************/
924cdb843SMatthew G. Knepley 
106da023fcSToby Isaac static PetscErrorCode DMSNESConvertPlex(DM dm, DM *plex, PetscBool copy)
116da023fcSToby Isaac {
126da023fcSToby Isaac   PetscBool      isPlex;
136da023fcSToby Isaac   PetscErrorCode ierr;
146da023fcSToby Isaac 
156da023fcSToby Isaac   PetscFunctionBegin;
166da023fcSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
176da023fcSToby Isaac   if (isPlex) {
186da023fcSToby Isaac     *plex = dm;
196da023fcSToby Isaac     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
20f7148743SMatthew G. Knepley   } else {
21f7148743SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
22f7148743SMatthew G. Knepley     if (!*plex) {
236da023fcSToby Isaac       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
24f7148743SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
256da023fcSToby Isaac       if (copy) {
266da023fcSToby Isaac         PetscInt    i;
276da023fcSToby Isaac         PetscObject obj;
286da023fcSToby Isaac         const char *comps[3] = {"A","dmAux","dmCh"};
296da023fcSToby Isaac 
306da023fcSToby Isaac         ierr = DMCopyDMSNES(dm, *plex);CHKERRQ(ierr);
316da023fcSToby Isaac         for (i = 0; i < 3; i++) {
326da023fcSToby Isaac           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
336da023fcSToby Isaac           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
346da023fcSToby Isaac         }
356da023fcSToby Isaac       }
36f7148743SMatthew G. Knepley     } else {
37f7148743SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
38f7148743SMatthew G. Knepley     }
396da023fcSToby Isaac   }
406da023fcSToby Isaac   PetscFunctionReturn(0);
416da023fcSToby Isaac }
426da023fcSToby Isaac 
430adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
440adebc6cSBarry Smith {
45552f7358SJed Brown   PetscErrorCode ierr;
46552f7358SJed Brown 
47552f7358SJed Brown   PetscFunctionBegin;
48552f7358SJed Brown   PetscValidPointer(ctx, 2);
4995dccacaSBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
501aa26658SKarl Rupp 
51552f7358SJed Brown   (*ctx)->comm   = comm;
52552f7358SJed Brown   (*ctx)->dim    = -1;
53552f7358SJed Brown   (*ctx)->nInput = 0;
540298fd71SBarry Smith   (*ctx)->points = NULL;
550298fd71SBarry Smith   (*ctx)->cells  = NULL;
56552f7358SJed Brown   (*ctx)->n      = -1;
570298fd71SBarry Smith   (*ctx)->coords = NULL;
58552f7358SJed Brown   PetscFunctionReturn(0);
59552f7358SJed Brown }
60552f7358SJed Brown 
610adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
620adebc6cSBarry Smith {
63552f7358SJed Brown   PetscFunctionBegin;
640adebc6cSBarry Smith   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
65552f7358SJed Brown   ctx->dim = dim;
66552f7358SJed Brown   PetscFunctionReturn(0);
67552f7358SJed Brown }
68552f7358SJed Brown 
690adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
700adebc6cSBarry Smith {
71552f7358SJed Brown   PetscFunctionBegin;
72552f7358SJed Brown   PetscValidIntPointer(dim, 2);
73552f7358SJed Brown   *dim = ctx->dim;
74552f7358SJed Brown   PetscFunctionReturn(0);
75552f7358SJed Brown }
76552f7358SJed Brown 
770adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
780adebc6cSBarry Smith {
79552f7358SJed Brown   PetscFunctionBegin;
800adebc6cSBarry Smith   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
81552f7358SJed Brown   ctx->dof = dof;
82552f7358SJed Brown   PetscFunctionReturn(0);
83552f7358SJed Brown }
84552f7358SJed Brown 
850adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
860adebc6cSBarry Smith {
87552f7358SJed Brown   PetscFunctionBegin;
88552f7358SJed Brown   PetscValidIntPointer(dof, 2);
89552f7358SJed Brown   *dof = ctx->dof;
90552f7358SJed Brown   PetscFunctionReturn(0);
91552f7358SJed Brown }
92552f7358SJed Brown 
930adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
940adebc6cSBarry Smith {
95552f7358SJed Brown   PetscErrorCode ierr;
96552f7358SJed Brown 
97552f7358SJed Brown   PetscFunctionBegin;
980adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
990adebc6cSBarry Smith   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
100552f7358SJed Brown   ctx->nInput = n;
1011aa26658SKarl Rupp 
102785e854fSJed Brown   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
103552f7358SJed Brown   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
104552f7358SJed Brown   PetscFunctionReturn(0);
105552f7358SJed Brown }
106552f7358SJed Brown 
1070adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
1080adebc6cSBarry Smith {
109552f7358SJed Brown   MPI_Comm          comm = ctx->comm;
110552f7358SJed Brown   PetscScalar       *a;
111552f7358SJed Brown   PetscInt          p, q, i;
112552f7358SJed Brown   PetscMPIInt       rank, size;
113552f7358SJed Brown   PetscErrorCode    ierr;
114552f7358SJed Brown   Vec               pointVec;
1153a93e3b7SToby Isaac   PetscSF           cellSF;
116552f7358SJed Brown   PetscLayout       layout;
117552f7358SJed Brown   PetscReal         *globalPoints;
118cb313848SJed Brown   PetscScalar       *globalPointsScalar;
119552f7358SJed Brown   const PetscInt    *ranges;
120552f7358SJed Brown   PetscMPIInt       *counts, *displs;
1213a93e3b7SToby Isaac   const PetscSFNode *foundCells;
1223a93e3b7SToby Isaac   const PetscInt    *foundPoints;
123552f7358SJed Brown   PetscMPIInt       *foundProcs, *globalProcs;
1243a93e3b7SToby Isaac   PetscInt          n, N, numFound;
125552f7358SJed Brown 
12619436ca2SJed Brown   PetscFunctionBegin;
12719436ca2SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12819436ca2SJed Brown   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
12919436ca2SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1300adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
13119436ca2SJed Brown   /* Locate points */
13219436ca2SJed Brown   n = ctx->nInput;
133552f7358SJed Brown   if (!redundantPoints) {
134552f7358SJed Brown     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
135552f7358SJed Brown     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
136552f7358SJed Brown     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
137552f7358SJed Brown     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
138552f7358SJed Brown     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
139552f7358SJed Brown     /* Communicate all points to all processes */
140dcca6d9dSJed Brown     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
141552f7358SJed Brown     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
142552f7358SJed Brown     for (p = 0; p < size; ++p) {
143552f7358SJed Brown       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
144552f7358SJed Brown       displs[p] = ranges[p]*ctx->dim;
145552f7358SJed Brown     }
146552f7358SJed Brown     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
147552f7358SJed Brown   } else {
148552f7358SJed Brown     N = n;
149552f7358SJed Brown     globalPoints = ctx->points;
15038ea73c8SJed Brown     counts = displs = NULL;
15138ea73c8SJed Brown     layout = NULL;
152552f7358SJed Brown   }
153552f7358SJed Brown #if 0
154dcca6d9dSJed Brown   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
15519436ca2SJed Brown   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
156552f7358SJed Brown #else
157cb313848SJed Brown #if defined(PETSC_USE_COMPLEX)
15846b3086cSToby Isaac   ierr = PetscMalloc1(N*ctx->dim,&globalPointsScalar);CHKERRQ(ierr);
15946b3086cSToby Isaac   for (i=0; i<N*ctx->dim; i++) globalPointsScalar[i] = globalPoints[i];
160cb313848SJed Brown #else
161cb313848SJed Brown   globalPointsScalar = globalPoints;
162cb313848SJed Brown #endif
16304706141SMatthew G Knepley   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
164dcca6d9dSJed Brown   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
1657b5f2079SMatthew G. Knepley   for (p = 0; p < N; ++p) {foundProcs[p] = size;}
1663a93e3b7SToby Isaac   cellSF = NULL;
1672d1fa6caSMatthew G. Knepley   ierr = DMLocatePoints(dm, pointVec, DM_POINTLOCATION_REMOVE, &cellSF);CHKERRQ(ierr);
1683a93e3b7SToby Isaac   ierr = PetscSFGetGraph(cellSF,NULL,&numFound,&foundPoints,&foundCells);CHKERRQ(ierr);
169552f7358SJed Brown #endif
1703a93e3b7SToby Isaac   for (p = 0; p < numFound; ++p) {
1713a93e3b7SToby Isaac     if (foundCells[p].index >= 0) foundProcs[foundPoints ? foundPoints[p] : p] = rank;
172552f7358SJed Brown   }
173552f7358SJed Brown   /* Let the lowest rank process own each point */
174b2566f29SBarry Smith   ierr   = MPIU_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
175552f7358SJed Brown   ctx->n = 0;
176552f7358SJed Brown   for (p = 0; p < N; ++p) {
177e5b268a4SToby Isaac     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, (double)globalPoints[p*ctx->dim+0], (double)(ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0), (double)(ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0));
1781aa26658SKarl Rupp     else if (globalProcs[p] == rank) ctx->n++;
179552f7358SJed Brown   }
180552f7358SJed Brown   /* Create coordinates vector and array of owned cells */
181785e854fSJed Brown   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
182552f7358SJed Brown   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
183552f7358SJed Brown   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
184552f7358SJed Brown   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
185c0dedaeaSBarry Smith   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
186552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
187552f7358SJed Brown   for (p = 0, q = 0, i = 0; p < N; ++p) {
188552f7358SJed Brown     if (globalProcs[p] == rank) {
189552f7358SJed Brown       PetscInt d;
190552f7358SJed Brown 
1911aa26658SKarl Rupp       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
192f317b747SMatthew G. Knepley       ctx->cells[q] = foundCells[q].index;
193f317b747SMatthew G. Knepley       ++q;
194552f7358SJed Brown     }
195552f7358SJed Brown   }
196552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
197552f7358SJed Brown #if 0
198552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
199552f7358SJed Brown #else
200552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
2013a93e3b7SToby Isaac   ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr);
202552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
203552f7358SJed Brown #endif
204cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
205d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
206552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
207552f7358SJed Brown   PetscFunctionReturn(0);
208552f7358SJed Brown }
209552f7358SJed Brown 
2100adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
2110adebc6cSBarry Smith {
212552f7358SJed Brown   PetscFunctionBegin;
213552f7358SJed Brown   PetscValidPointer(coordinates, 2);
2140adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
215552f7358SJed Brown   *coordinates = ctx->coords;
216552f7358SJed Brown   PetscFunctionReturn(0);
217552f7358SJed Brown }
218552f7358SJed Brown 
2190adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2200adebc6cSBarry Smith {
221552f7358SJed Brown   PetscErrorCode ierr;
222552f7358SJed Brown 
223552f7358SJed Brown   PetscFunctionBegin;
224552f7358SJed Brown   PetscValidPointer(v, 2);
2250adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
226552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
227552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
228552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
229c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
230552f7358SJed Brown   PetscFunctionReturn(0);
231552f7358SJed Brown }
232552f7358SJed Brown 
2330adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2340adebc6cSBarry Smith {
235552f7358SJed Brown   PetscErrorCode ierr;
236552f7358SJed Brown 
237552f7358SJed Brown   PetscFunctionBegin;
238552f7358SJed Brown   PetscValidPointer(v, 2);
2390adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
240552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
241552f7358SJed Brown   PetscFunctionReturn(0);
242552f7358SJed Brown }
243552f7358SJed Brown 
2447a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
245a6dfd86eSKarl Rupp {
246552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
24756044e6dSMatthew G. Knepley   const PetscScalar *coords;
24856044e6dSMatthew G. Knepley   PetscScalar    *a;
249552f7358SJed Brown   PetscInt       p;
250552f7358SJed Brown   PetscErrorCode ierr;
251552f7358SJed Brown 
252552f7358SJed Brown   PetscFunctionBegin;
253dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
25456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
255552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
256552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
257552f7358SJed Brown     PetscInt     c = ctx->cells[p];
258a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
259552f7358SJed Brown     PetscReal    xi[4];
260552f7358SJed Brown     PetscInt     d, f, comp;
261552f7358SJed Brown 
2628e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
263e5b268a4SToby Isaac     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c);
2640298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2651aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2661aa26658SKarl Rupp 
267552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
268552f7358SJed Brown       xi[d] = 0.0;
2691aa26658SKarl 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]);
2701aa26658SKarl 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];
271552f7358SJed Brown     }
2720298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
273552f7358SJed Brown   }
274552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
27556044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
276552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
277552f7358SJed Brown   PetscFunctionReturn(0);
278552f7358SJed Brown }
279552f7358SJed Brown 
2807a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
2817a1931ceSMatthew G. Knepley {
2827a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
28356044e6dSMatthew G. Knepley   const PetscScalar *coords;
28456044e6dSMatthew G. Knepley   PetscScalar    *a;
2857a1931ceSMatthew G. Knepley   PetscInt       p;
2867a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
2877a1931ceSMatthew G. Knepley 
2887a1931ceSMatthew G. Knepley   PetscFunctionBegin;
289dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
29056044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
2917a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
2927a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
2937a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
2947a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
2952584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
2967a1931ceSMatthew G. Knepley     PetscReal      xi[4];
2977a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
2987a1931ceSMatthew G. Knepley 
2998e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
300e5b268a4SToby Isaac     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", (double)detJ, c);
3017a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3027a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
3037a1931ceSMatthew G. Knepley 
3047a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
3057a1931ceSMatthew G. Knepley       xi[d] = 0.0;
3067a1931ceSMatthew 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]);
3077a1931ceSMatthew 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];
3087a1931ceSMatthew G. Knepley     }
3097a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3107a1931ceSMatthew G. Knepley   }
3117a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
31256044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
3137a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
3147a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3157a1931ceSMatthew G. Knepley }
3167a1931ceSMatthew G. Knepley 
3175820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
318552f7358SJed Brown {
319552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
320552f7358SJed Brown   const PetscScalar x0        = vertices[0];
321552f7358SJed Brown   const PetscScalar y0        = vertices[1];
322552f7358SJed Brown   const PetscScalar x1        = vertices[2];
323552f7358SJed Brown   const PetscScalar y1        = vertices[3];
324552f7358SJed Brown   const PetscScalar x2        = vertices[4];
325552f7358SJed Brown   const PetscScalar y2        = vertices[5];
326552f7358SJed Brown   const PetscScalar x3        = vertices[6];
327552f7358SJed Brown   const PetscScalar y3        = vertices[7];
328552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
329552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
330552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
331552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
332552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
333552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
33456044e6dSMatthew G. Knepley   const PetscScalar *ref;
33556044e6dSMatthew G. Knepley   PetscScalar       *real;
336552f7358SJed Brown   PetscErrorCode    ierr;
337552f7358SJed Brown 
338552f7358SJed Brown   PetscFunctionBegin;
33956044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
340552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
341552f7358SJed Brown   {
342552f7358SJed Brown     const PetscScalar p0 = ref[0];
343552f7358SJed Brown     const PetscScalar p1 = ref[1];
344552f7358SJed Brown 
345552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
346552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
347552f7358SJed Brown   }
348552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
34956044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
350552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
351552f7358SJed Brown   PetscFunctionReturn(0);
352552f7358SJed Brown }
353552f7358SJed Brown 
354af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
355d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
356552f7358SJed Brown {
357552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
358552f7358SJed Brown   const PetscScalar x0        = vertices[0];
359552f7358SJed Brown   const PetscScalar y0        = vertices[1];
360552f7358SJed Brown   const PetscScalar x1        = vertices[2];
361552f7358SJed Brown   const PetscScalar y1        = vertices[3];
362552f7358SJed Brown   const PetscScalar x2        = vertices[4];
363552f7358SJed Brown   const PetscScalar y2        = vertices[5];
364552f7358SJed Brown   const PetscScalar x3        = vertices[6];
365552f7358SJed Brown   const PetscScalar y3        = vertices[7];
366552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
367552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
36856044e6dSMatthew G. Knepley   const PetscScalar *ref;
369552f7358SJed Brown   PetscErrorCode    ierr;
370552f7358SJed Brown 
371552f7358SJed Brown   PetscFunctionBegin;
37256044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
373552f7358SJed Brown   {
374552f7358SJed Brown     const PetscScalar x       = ref[0];
375552f7358SJed Brown     const PetscScalar y       = ref[1];
376552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
377da80777bSKarl Rupp     PetscScalar       values[4];
378da80777bSKarl Rupp 
379da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
380da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
38194ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
382552f7358SJed Brown   }
383552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
38456044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
38594ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38694ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
387552f7358SJed Brown   PetscFunctionReturn(0);
388552f7358SJed Brown }
389552f7358SJed Brown 
390a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
391a6dfd86eSKarl Rupp {
392fafc0619SMatthew G Knepley   DM             dmCoord;
393de73a395SMatthew G. Knepley   PetscFE        fem = NULL;
394552f7358SJed Brown   SNES           snes;
395552f7358SJed Brown   KSP            ksp;
396552f7358SJed Brown   PC             pc;
397552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
398552f7358SJed Brown   Mat            J;
39956044e6dSMatthew G. Knepley   const PetscScalar *coords;
40056044e6dSMatthew G. Knepley   PetscScalar    *a;
401de73a395SMatthew G. Knepley   PetscInt       Nf, p;
4025509d985SMatthew G. Knepley   const PetscInt dof = ctx->dof;
403552f7358SJed Brown   PetscErrorCode ierr;
404552f7358SJed Brown 
405552f7358SJed Brown   PetscFunctionBegin;
406de73a395SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
407de73a395SMatthew G. Knepley   if (Nf) {ierr = DMGetField(dm, 0, (PetscObject *) &fem);CHKERRQ(ierr);}
408552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
409fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
410552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
411552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
412552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
413552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
414c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
415552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
416552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
417552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
418552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
419552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
420552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4210298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4220298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
423552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
424552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
425552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
426552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
427552f7358SJed Brown 
42856044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
429552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
430552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
431a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
432552f7358SJed Brown     PetscScalar *xi;
433cb313848SJed Brown     PetscReal    xir[2];
434552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
435552f7358SJed Brown 
436552f7358SJed Brown     /* Can make this do all points at once */
4370298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4380adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4390298fd71SBarry Smith     ierr   = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4400298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4410298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
442552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
443552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
444552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
445552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
446552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
447552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
448cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
449cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4505509d985SMatthew G. Knepley     if (4*dof != xSize) {
4515509d985SMatthew G. Knepley       PetscReal *B;
4525509d985SMatthew G. Knepley       PetscInt   d;
4531aa26658SKarl Rupp 
4545509d985SMatthew G. Knepley       xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0;
4555509d985SMatthew G. Knepley       ierr = PetscFEGetTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
4565509d985SMatthew G. Knepley       for (comp = 0; comp < dof; ++comp) {
4575509d985SMatthew G. Knepley         a[p*dof+comp] = 0.0;
4585509d985SMatthew G. Knepley         for (d = 0; d < xSize/dof; ++d) {
4595509d985SMatthew G. Knepley           a[p*dof+comp] += x[d*dof+comp]*B[d*dof+comp];
4605509d985SMatthew G. Knepley         }
4615509d985SMatthew G. Knepley       }
4625509d985SMatthew G. Knepley       ierr = PetscFERestoreTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
4635509d985SMatthew G. Knepley     } else {
4645509d985SMatthew G. Knepley       for (comp = 0; comp < dof; ++comp)
4655509d985SMatthew G. Knepley         a[p*dof+comp] = x[0*dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*dof+comp]*xir[0]*(1 - xir[1]) + x[2*dof+comp]*xir[0]*xir[1] + x[3*dof+comp]*(1 - xir[0])*xir[1];
4665509d985SMatthew G. Knepley     }
467552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4680298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4690298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
470552f7358SJed Brown   }
471552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
47256044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
473552f7358SJed Brown 
474552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
475552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
476552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
477552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
478552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
479552f7358SJed Brown   PetscFunctionReturn(0);
480552f7358SJed Brown }
481552f7358SJed Brown 
4825820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
483552f7358SJed Brown {
484552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
485552f7358SJed Brown   const PetscScalar x0        = vertices[0];
486552f7358SJed Brown   const PetscScalar y0        = vertices[1];
487552f7358SJed Brown   const PetscScalar z0        = vertices[2];
4887a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
4897a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
4907a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
491552f7358SJed Brown   const PetscScalar x2        = vertices[6];
492552f7358SJed Brown   const PetscScalar y2        = vertices[7];
493552f7358SJed Brown   const PetscScalar z2        = vertices[8];
4947a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
4957a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
4967a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
497552f7358SJed Brown   const PetscScalar x4        = vertices[12];
498552f7358SJed Brown   const PetscScalar y4        = vertices[13];
499552f7358SJed Brown   const PetscScalar z4        = vertices[14];
500552f7358SJed Brown   const PetscScalar x5        = vertices[15];
501552f7358SJed Brown   const PetscScalar y5        = vertices[16];
502552f7358SJed Brown   const PetscScalar z5        = vertices[17];
503552f7358SJed Brown   const PetscScalar x6        = vertices[18];
504552f7358SJed Brown   const PetscScalar y6        = vertices[19];
505552f7358SJed Brown   const PetscScalar z6        = vertices[20];
506552f7358SJed Brown   const PetscScalar x7        = vertices[21];
507552f7358SJed Brown   const PetscScalar y7        = vertices[22];
508552f7358SJed Brown   const PetscScalar z7        = vertices[23];
509552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
510552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
511552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
512552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
513552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
514552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
515552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
516552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
517552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
518552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
519552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
520552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
521552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
522552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
523552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
524552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
525552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
526552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
527552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
528552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
529552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
53056044e6dSMatthew G. Knepley   const PetscScalar *ref;
53156044e6dSMatthew G. Knepley   PetscScalar       *real;
532552f7358SJed Brown   PetscErrorCode    ierr;
533552f7358SJed Brown 
534552f7358SJed Brown   PetscFunctionBegin;
53556044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
536552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
537552f7358SJed Brown   {
538552f7358SJed Brown     const PetscScalar p0 = ref[0];
539552f7358SJed Brown     const PetscScalar p1 = ref[1];
540552f7358SJed Brown     const PetscScalar p2 = ref[2];
541552f7358SJed Brown 
542552f7358SJed 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;
543552f7358SJed 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;
544552f7358SJed 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;
545552f7358SJed Brown   }
546552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
54756044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
548552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
549552f7358SJed Brown   PetscFunctionReturn(0);
550552f7358SJed Brown }
551552f7358SJed Brown 
552d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
553552f7358SJed Brown {
554552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
555552f7358SJed Brown   const PetscScalar x0        = vertices[0];
556552f7358SJed Brown   const PetscScalar y0        = vertices[1];
557552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5587a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5597a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5607a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
561552f7358SJed Brown   const PetscScalar x2        = vertices[6];
562552f7358SJed Brown   const PetscScalar y2        = vertices[7];
563552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5647a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5657a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5667a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
567552f7358SJed Brown   const PetscScalar x4        = vertices[12];
568552f7358SJed Brown   const PetscScalar y4        = vertices[13];
569552f7358SJed Brown   const PetscScalar z4        = vertices[14];
570552f7358SJed Brown   const PetscScalar x5        = vertices[15];
571552f7358SJed Brown   const PetscScalar y5        = vertices[16];
572552f7358SJed Brown   const PetscScalar z5        = vertices[17];
573552f7358SJed Brown   const PetscScalar x6        = vertices[18];
574552f7358SJed Brown   const PetscScalar y6        = vertices[19];
575552f7358SJed Brown   const PetscScalar z6        = vertices[20];
576552f7358SJed Brown   const PetscScalar x7        = vertices[21];
577552f7358SJed Brown   const PetscScalar y7        = vertices[22];
578552f7358SJed Brown   const PetscScalar z7        = vertices[23];
579552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
580552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
581552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
582552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
583552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
584552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
585552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
586552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
587552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
588552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
589552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
590552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
59156044e6dSMatthew G. Knepley   const PetscScalar *ref;
592552f7358SJed Brown   PetscErrorCode    ierr;
593552f7358SJed Brown 
594552f7358SJed Brown   PetscFunctionBegin;
59556044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
596552f7358SJed Brown   {
597552f7358SJed Brown     const PetscScalar x       = ref[0];
598552f7358SJed Brown     const PetscScalar y       = ref[1];
599552f7358SJed Brown     const PetscScalar z       = ref[2];
600552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
601da80777bSKarl Rupp     PetscScalar       values[9];
602da80777bSKarl Rupp 
603da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
604da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
605da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
606da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
607da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
608da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
609da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
610da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
611da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
6121aa26658SKarl Rupp 
61394ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
614552f7358SJed Brown   }
615552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
61656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
61794ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
61894ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
619552f7358SJed Brown   PetscFunctionReturn(0);
620552f7358SJed Brown }
621552f7358SJed Brown 
622a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
623a6dfd86eSKarl Rupp {
624fafc0619SMatthew G Knepley   DM             dmCoord;
625552f7358SJed Brown   SNES           snes;
626552f7358SJed Brown   KSP            ksp;
627552f7358SJed Brown   PC             pc;
628552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
629552f7358SJed Brown   Mat            J;
63056044e6dSMatthew G. Knepley   const PetscScalar *coords;
63156044e6dSMatthew G. Knepley   PetscScalar    *a;
632552f7358SJed Brown   PetscInt       p;
633552f7358SJed Brown   PetscErrorCode ierr;
634552f7358SJed Brown 
635552f7358SJed Brown   PetscFunctionBegin;
636552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
637fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
638552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
639552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
640552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
641552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
642c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
643552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
644552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
645552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
646552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
647552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
648552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6490298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6500298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
651552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
652552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
653552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
654552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
655552f7358SJed Brown 
65656044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
657552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
658552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
659a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
660552f7358SJed Brown     PetscScalar *xi;
661cb313848SJed Brown     PetscReal    xir[3];
662552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
663552f7358SJed Brown 
664552f7358SJed Brown     /* Can make this do all points at once */
6650298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6660adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6670298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6680adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6690298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6700298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
671552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
672552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
673552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
674552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
675552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
676552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
677552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
678cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
679cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
680cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
681552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
682552f7358SJed Brown       a[p*ctx->dof+comp] =
683cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
6847a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
685cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
6867a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
687cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
688cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
689cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
690cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
691552f7358SJed Brown     }
692552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
6930298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6940298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
695552f7358SJed Brown   }
696552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
69756044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
698552f7358SJed Brown 
699552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
700552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
701552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
702552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
703552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
704552f7358SJed Brown   PetscFunctionReturn(0);
705552f7358SJed Brown }
706552f7358SJed Brown 
707552f7358SJed Brown /*
708552f7358SJed Brown   Input Parameters:
709552f7358SJed Brown + ctx - The DMInterpolationInfo context
710552f7358SJed Brown . dm  - The DM
711552f7358SJed Brown - x   - The local vector containing the field to be interpolated
712552f7358SJed Brown 
713552f7358SJed Brown   Output Parameters:
714552f7358SJed Brown . v   - The vector containing the interpolated values
715552f7358SJed Brown */
7160adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
7170adebc6cSBarry Smith {
718552f7358SJed Brown   PetscInt       dim, coneSize, n;
719552f7358SJed Brown   PetscErrorCode ierr;
720552f7358SJed Brown 
721552f7358SJed Brown   PetscFunctionBegin;
722552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
723552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
724552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
725552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7260adebc6cSBarry 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);
727552f7358SJed Brown   if (n) {
728c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
729552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
730552f7358SJed Brown     if (dim == 2) {
731552f7358SJed Brown       if (coneSize == 3) {
7327a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
733552f7358SJed Brown       } else if (coneSize == 4) {
734552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7350adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
736552f7358SJed Brown     } else if (dim == 3) {
737552f7358SJed Brown       if (coneSize == 4) {
7387a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
739552f7358SJed Brown       } else {
740552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
741552f7358SJed Brown       }
7420adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
743552f7358SJed Brown   }
744552f7358SJed Brown   PetscFunctionReturn(0);
745552f7358SJed Brown }
746552f7358SJed Brown 
7470adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7480adebc6cSBarry Smith {
749552f7358SJed Brown   PetscErrorCode ierr;
750552f7358SJed Brown 
751552f7358SJed Brown   PetscFunctionBegin;
752552f7358SJed Brown   PetscValidPointer(ctx, 2);
753552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
754552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
755552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
756552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7570298fd71SBarry Smith   *ctx = NULL;
758552f7358SJed Brown   PetscFunctionReturn(0);
759552f7358SJed Brown }
760cc0c4584SMatthew G. Knepley 
761cc0c4584SMatthew G. Knepley /*@C
762cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
763cc0c4584SMatthew G. Knepley 
764cc0c4584SMatthew G. Knepley   Collective on SNES
765cc0c4584SMatthew G. Knepley 
766cc0c4584SMatthew G. Knepley   Input Parameters:
767cc0c4584SMatthew G. Knepley + snes   - the SNES context
768cc0c4584SMatthew G. Knepley . its    - iteration number
769cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
770d43b4f6eSBarry Smith - vf  - PetscViewerAndFormat of type ASCII
771cc0c4584SMatthew G. Knepley 
772cc0c4584SMatthew G. Knepley   Notes:
773cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
774cc0c4584SMatthew G. Knepley 
775cc0c4584SMatthew G. Knepley   Level: intermediate
776cc0c4584SMatthew G. Knepley 
777cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
778cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
779cc0c4584SMatthew G. Knepley @*/
780d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
781cc0c4584SMatthew G. Knepley {
782d43b4f6eSBarry Smith   PetscViewer        viewer = vf->viewer;
783cc0c4584SMatthew G. Knepley   Vec                res;
784cc0c4584SMatthew G. Knepley   DM                 dm;
785cc0c4584SMatthew G. Knepley   PetscSection       s;
786cc0c4584SMatthew G. Knepley   const PetscScalar *r;
787cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
788cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
789cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
790cc0c4584SMatthew G. Knepley 
791cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
7924d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
793cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
794cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
795cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
796cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
797cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
798cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
799cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
800cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
801cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
802cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
803cc0c4584SMatthew G. Knepley 
804cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
805cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
806cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
807cc0c4584SMatthew G. Knepley     }
808cc0c4584SMatthew G. Knepley   }
809cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
810b2566f29SBarry Smith   ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
811d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
812cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
813cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
814cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
815cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
816cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
817cc0c4584SMatthew G. Knepley   }
818cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
819cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
820d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
821cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
822cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
823cc0c4584SMatthew G. Knepley }
82424cdb843SMatthew G. Knepley 
82524cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
82624cdb843SMatthew G. Knepley 
8274a3e9fdbSToby Isaac static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
8287d4028c8SMatthew G. Knepley {
8294a3e9fdbSToby Isaac   PetscFEGeom *geom = (PetscFEGeom *) ctx;
8307d4028c8SMatthew G. Knepley   PetscErrorCode ierr;
8317d4028c8SMatthew G. Knepley 
8327d4028c8SMatthew G. Knepley   PetscFunctionBegin;
8334a3e9fdbSToby Isaac   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
8344a3e9fdbSToby Isaac   PetscFunctionReturn(0);
8357d4028c8SMatthew G. Knepley }
8364a3e9fdbSToby Isaac 
8374a3e9fdbSToby Isaac static PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
8384a3e9fdbSToby Isaac {
8394a3e9fdbSToby Isaac   char            composeStr[33] = {0};
8404a3e9fdbSToby Isaac   PetscObjectId   id;
8414a3e9fdbSToby Isaac   PetscContainer  container;
8424a3e9fdbSToby Isaac   PetscErrorCode  ierr;
8434a3e9fdbSToby Isaac 
8444a3e9fdbSToby Isaac   PetscFunctionBegin;
8454a3e9fdbSToby Isaac   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
8464a3e9fdbSToby Isaac   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
8474a3e9fdbSToby Isaac   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
8484a3e9fdbSToby Isaac   if (container) {
8494a3e9fdbSToby Isaac     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
8504a3e9fdbSToby Isaac   } else {
8514a3e9fdbSToby Isaac     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
8524a3e9fdbSToby Isaac     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
8534a3e9fdbSToby Isaac     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
8544a3e9fdbSToby Isaac     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
8554a3e9fdbSToby Isaac     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
8564a3e9fdbSToby Isaac     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
8574a3e9fdbSToby Isaac   }
8584a3e9fdbSToby Isaac   PetscFunctionReturn(0);
8594a3e9fdbSToby Isaac }
8604a3e9fdbSToby Isaac 
8614a3e9fdbSToby Isaac static PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
8624a3e9fdbSToby Isaac {
8634a3e9fdbSToby Isaac   PetscFunctionBegin;
8644a3e9fdbSToby Isaac   *geom = NULL;
8657d4028c8SMatthew G. Knepley   PetscFunctionReturn(0);
8667d4028c8SMatthew G. Knepley }
8677d4028c8SMatthew G. Knepley 
86808449791SMatthew G. Knepley /*@
86908449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
87008449791SMatthew G. Knepley 
87108449791SMatthew G. Knepley   Input Parameter:
87208449791SMatthew G. Knepley . dm - The DM
87308449791SMatthew G. Knepley 
87408449791SMatthew G. Knepley   Output Parameters:
87508449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
87608449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
87708449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
87808449791SMatthew G. Knepley 
87908449791SMatthew G. Knepley   Level: developer
88008449791SMatthew G. Knepley 
88108449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
88208449791SMatthew G. Knepley @*/
88308449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
88424cdb843SMatthew G. Knepley {
8854b32e5bbSToby Isaac   DM             plex;
88624cdb843SMatthew G. Knepley   PetscErrorCode ierr;
88724cdb843SMatthew G. Knepley 
88824cdb843SMatthew G. Knepley   PetscFunctionBegin;
88908449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8904b32e5bbSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
8914b32e5bbSToby Isaac   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
8924b32e5bbSToby Isaac   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
8934b32e5bbSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
89424cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
89524cdb843SMatthew G. Knepley }
89624cdb843SMatthew G. Knepley 
897dbd489d2SMatthew G. Knepley /*@
89808449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
89908449791SMatthew G. Knepley 
90008449791SMatthew G. Knepley   Input Parameters:
90108449791SMatthew G. Knepley + dm - The DM
90208449791SMatthew G. Knepley - fv - The PetscFV
90308449791SMatthew G. Knepley 
90408449791SMatthew G. Knepley   Output Parameter:
90508449791SMatthew G. Knepley . dmGrad - The layout for gradient values
90608449791SMatthew G. Knepley 
90708449791SMatthew G. Knepley   Level: developer
90808449791SMatthew G. Knepley 
90908449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
91008449791SMatthew G. Knepley @*/
91108449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
91224cdb843SMatthew G. Knepley {
9134b32e5bbSToby Isaac   DM             plex;
91408449791SMatthew G. Knepley   PetscBool      computeGradients;
91524cdb843SMatthew G. Knepley   PetscErrorCode ierr;
91624cdb843SMatthew G. Knepley 
91724cdb843SMatthew G. Knepley   PetscFunctionBegin;
91808449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91908449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
92008449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
92108449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
92208449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
9234b32e5bbSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
9244b32e5bbSToby Isaac   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
9254b32e5bbSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
92608449791SMatthew G. Knepley   PetscFunctionReturn(0);
92708449791SMatthew G. Knepley }
92808449791SMatthew G. Knepley 
92908449791SMatthew G. Knepley /*@C
93008449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
93108449791SMatthew G. Knepley 
93208449791SMatthew G. Knepley   Input Parameters:
93308449791SMatthew G. Knepley + dm     - The DM
93408449791SMatthew G. Knepley . cStart - The first cell to include
93508449791SMatthew G. Knepley . cEnd   - The first cell to exclude
93608449791SMatthew G. Knepley . locX   - A local vector with the solution fields
93708449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
93808449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
93908449791SMatthew G. Knepley 
94008449791SMatthew G. Knepley   Output Parameters:
94108449791SMatthew G. Knepley + u   - The field coefficients
94208449791SMatthew G. Knepley . u_t - The fields derivative coefficients
94308449791SMatthew G. Knepley - a   - The auxiliary field coefficients
94408449791SMatthew G. Knepley 
94508449791SMatthew G. Knepley   Level: developer
94608449791SMatthew G. Knepley 
94708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
94808449791SMatthew G. Knepley @*/
94908449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
95008449791SMatthew G. Knepley {
95108449791SMatthew G. Knepley   DM             dmAux;
95208449791SMatthew G. Knepley   PetscSection   section, sectionAux;
95308449791SMatthew G. Knepley   PetscDS        prob;
95408449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
95508449791SMatthew G. Knepley   PetscErrorCode ierr;
95608449791SMatthew G. Knepley 
95708449791SMatthew G. Knepley   PetscFunctionBegin;
95808449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95908449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
96008449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
96108449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
96208449791SMatthew G. Knepley   PetscValidPointer(u, 7);
96308449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
96408449791SMatthew G. Knepley   PetscValidPointer(a, 9);
96524cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
96624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
96724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
96808449791SMatthew G. Knepley   if (locA) {
96908449791SMatthew G. Knepley     PetscDS probAux;
97008449791SMatthew G. Knepley 
97108449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
97224cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
97324cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
97424cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
97524cdb843SMatthew G. Knepley   }
97669291d52SBarry Smith   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
97769291d52SBarry Smith   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
97869291d52SBarry Smith   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
97924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
98008449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
98124cdb843SMatthew G. Knepley     PetscInt     i;
98224cdb843SMatthew G. Knepley 
98308449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
9849f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i];
98508449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
98608449791SMatthew G. Knepley     if (locX_t) {
98708449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
9889f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i];
98908449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
99024cdb843SMatthew G. Knepley     }
99108449791SMatthew G. Knepley     if (locA) {
9926da023fcSToby Isaac       DM dmAuxPlex;
9936da023fcSToby Isaac 
9946da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
9956da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
9969f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i];
9976da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
9986da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
99924cdb843SMatthew G. Knepley     }
100024cdb843SMatthew G. Knepley   }
100108449791SMatthew G. Knepley   PetscFunctionReturn(0);
100208449791SMatthew G. Knepley }
100324cdb843SMatthew G. Knepley 
100408449791SMatthew G. Knepley /*@C
100508449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
100608449791SMatthew G. Knepley 
100708449791SMatthew G. Knepley   Input Parameters:
100808449791SMatthew G. Knepley + dm     - The DM
100908449791SMatthew G. Knepley . cStart - The first cell to include
101008449791SMatthew G. Knepley . cEnd   - The first cell to exclude
101108449791SMatthew G. Knepley . locX   - A local vector with the solution fields
101208449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
101308449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
101408449791SMatthew G. Knepley 
101508449791SMatthew G. Knepley   Output Parameters:
101608449791SMatthew G. Knepley + u   - The field coefficients
101708449791SMatthew G. Knepley . u_t - The fields derivative coefficients
101808449791SMatthew G. Knepley - a   - The auxiliary field coefficients
101908449791SMatthew G. Knepley 
102008449791SMatthew G. Knepley   Level: developer
102108449791SMatthew G. Knepley 
102208449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
102308449791SMatthew G. Knepley @*/
102408449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
102508449791SMatthew G. Knepley {
102608449791SMatthew G. Knepley   PetscErrorCode ierr;
102708449791SMatthew G. Knepley 
102808449791SMatthew G. Knepley   PetscFunctionBegin;
102969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
1030040f60c8SVaclav Hapla   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
1031040f60c8SVaclav Hapla   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
103208449791SMatthew G. Knepley   PetscFunctionReturn(0);
103324cdb843SMatthew G. Knepley }
103408449791SMatthew G. Knepley 
103508449791SMatthew G. Knepley /*@C
103608449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
103708449791SMatthew G. Knepley 
103808449791SMatthew G. Knepley   Input Parameters:
103908449791SMatthew G. Knepley + dm     - The DM
104008449791SMatthew G. Knepley . fStart - The first face to include
104108449791SMatthew G. Knepley . fEnd   - The first face to exclude
104208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
104308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
104408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
104508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
104608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
104708449791SMatthew G. Knepley 
104808449791SMatthew G. Knepley   Output Parameters:
10495f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
10505f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face
1051477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
105208449791SMatthew G. Knepley 
105308449791SMatthew G. Knepley   Level: developer
105408449791SMatthew G. Knepley 
105508449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
105608449791SMatthew G. Knepley @*/
10575f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
105808449791SMatthew G. Knepley {
105908449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
1060195142f5SMatthew G. Knepley   PetscSection       section;
106108449791SMatthew G. Knepley   PetscDS            prob;
106208449791SMatthew G. Knepley   DMLabel            ghostLabel;
106308449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1064477f7dfdSMatthew G. Knepley   PetscBool         *isFE;
1065477f7dfdSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
106608449791SMatthew G. Knepley   PetscErrorCode     ierr;
106708449791SMatthew G. Knepley 
106808449791SMatthew G. Knepley   PetscFunctionBegin;
106908449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107008449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
107108449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
107208449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
107308449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
107408449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
107508449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
107608449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
107708449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
107808449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1079195142f5SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1080477f7dfdSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1081477f7dfdSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
1082477f7dfdSMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
1083477f7dfdSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1084477f7dfdSMatthew G. Knepley     PetscObject  obj;
1085477f7dfdSMatthew G. Knepley     PetscClassId id;
1086477f7dfdSMatthew G. Knepley 
10874a270516SSander Arens     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1088477f7dfdSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1089477f7dfdSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
1090477f7dfdSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
1091477f7dfdSMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
1092477f7dfdSMatthew G. Knepley   }
1093c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
109408449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
109508449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
109608449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
109708449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
109808449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
109908449791SMatthew G. Knepley   if (locGrad) {
110008449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
110108449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
110224cdb843SMatthew G. Knepley   }
110369291d52SBarry Smith   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
110469291d52SBarry Smith   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
1105477f7dfdSMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
110608449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
110708449791SMatthew G. Knepley     const PetscInt        *cells;
1108640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1109640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
1110640bce14SSatish Balay     PetscScalar           *xL, *xR, *gL, *gR;
111108449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
11123e64cd2fSToby Isaac     PetscInt               ghost, nsupp, nchild;
111308449791SMatthew G. Knepley 
111408449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1115e697831aSToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
11163e64cd2fSToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
11177c45b140SToby Isaac     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
111808449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
111908449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
112008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
112108449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1122477f7dfdSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1123477f7dfdSMatthew G. Knepley       PetscInt off;
1124477f7dfdSMatthew G. Knepley 
112537a43ebbSMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1126477f7dfdSMatthew G. Knepley       if (isFE[f]) {
1127477f7dfdSMatthew G. Knepley         const PetscInt *cone;
11283e64cd2fSToby Isaac         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
1129477f7dfdSMatthew G. Knepley 
1130cca9989dSMatthew G. Knepley         xL = xR = NULL;
11317c45b140SToby Isaac         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
1132cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1133cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1134477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
11353e64cd2fSToby Isaac         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
11363e64cd2fSToby Isaac         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
1137477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
11383e64cd2fSToby Isaac         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
11393e64cd2fSToby Isaac         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
11403e64cd2fSToby Isaac         if (faceLocL == coneSizeL && faceLocR == coneSizeR) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of cell %d or cell %d", face, cells[0], cells[1]);
1141195142f5SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
11423e64cd2fSToby Isaac         /* TODO: this is a hack that might not be right for nonconforming */
11433e64cd2fSToby Isaac         if (faceLocL < coneSizeL) {
1144477f7dfdSMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
11453e64cd2fSToby Isaac           if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
11467c45b140SToby Isaac           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
11473e64cd2fSToby Isaac         }
11483e64cd2fSToby Isaac         else {
11493e64cd2fSToby Isaac           ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
11503e64cd2fSToby Isaac           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
11513e64cd2fSToby Isaac           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
11523e64cd2fSToby Isaac         }
1153cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1154cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1155477f7dfdSMatthew G. Knepley       } else {
1156477f7dfdSMatthew G. Knepley         PetscFV  fv;
1157477f7dfdSMatthew G. Knepley         PetscInt numComp, c;
1158477f7dfdSMatthew G. Knepley 
1159477f7dfdSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
1160477f7dfdSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1161cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
1162cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
116308449791SMatthew G. Knepley         if (dmGrad) {
116408449791SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
116508449791SMatthew G. Knepley 
116608449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
116708449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
116808449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
116908449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1170477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1171477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
1172477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
117308449791SMatthew G. Knepley           }
117408449791SMatthew G. Knepley         } else {
1175477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1176477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
1177477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
1178477f7dfdSMatthew G. Knepley           }
1179477f7dfdSMatthew G. Knepley         }
118008449791SMatthew G. Knepley       }
118108449791SMatthew G. Knepley     }
118208449791SMatthew G. Knepley     ++iface;
118308449791SMatthew G. Knepley   }
11845f942ad5SMatthew G. Knepley   *Nface = iface;
118508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
118608449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
118708449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
118808449791SMatthew G. Knepley   if (locGrad) {
118908449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
119008449791SMatthew G. Knepley   }
1191477f7dfdSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
119208449791SMatthew G. Knepley   PetscFunctionReturn(0);
119308449791SMatthew G. Knepley }
119408449791SMatthew G. Knepley 
119508449791SMatthew G. Knepley /*@C
119608449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
119708449791SMatthew G. Knepley 
119808449791SMatthew G. Knepley   Input Parameters:
119908449791SMatthew G. Knepley + dm     - The DM
120008449791SMatthew G. Knepley . fStart - The first face to include
120108449791SMatthew G. Knepley . fEnd   - The first face to exclude
120208449791SMatthew G. Knepley . locX   - A local vector with the solution fields
120308449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
120408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
120508449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
120608449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
120708449791SMatthew G. Knepley 
120808449791SMatthew G. Knepley   Output Parameters:
12095f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
12105f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face
1211477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
121208449791SMatthew G. Knepley 
121308449791SMatthew G. Knepley   Level: developer
121408449791SMatthew G. Knepley 
121508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
121608449791SMatthew G. Knepley @*/
12175f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
121808449791SMatthew G. Knepley {
121908449791SMatthew G. Knepley   PetscErrorCode ierr;
122008449791SMatthew G. Knepley 
122108449791SMatthew G. Knepley   PetscFunctionBegin;
122269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
122369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
122408449791SMatthew G. Knepley   PetscFunctionReturn(0);
122508449791SMatthew G. Knepley }
122608449791SMatthew G. Knepley 
122708449791SMatthew G. Knepley /*@C
122808449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
122908449791SMatthew G. Knepley 
123008449791SMatthew G. Knepley   Input Parameters:
123108449791SMatthew G. Knepley + dm     - The DM
123208449791SMatthew G. Knepley . fStart - The first face to include
123308449791SMatthew G. Knepley . fEnd   - The first face to exclude
123408449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
123508449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
123608449791SMatthew G. Knepley 
123708449791SMatthew G. Knepley   Output Parameters:
12385f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
12395f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal
124008449791SMatthew G. Knepley - vol   - The cell volume
124108449791SMatthew G. Knepley 
124208449791SMatthew G. Knepley   Level: developer
124308449791SMatthew G. Knepley 
124408449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
124508449791SMatthew G. Knepley @*/
12465f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
124708449791SMatthew G. Knepley {
124808449791SMatthew G. Knepley   DM                 dmFace, dmCell;
124908449791SMatthew G. Knepley   DMLabel            ghostLabel;
125008449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
125108449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
125208449791SMatthew G. Knepley   PetscErrorCode     ierr;
125308449791SMatthew G. Knepley 
125408449791SMatthew G. Knepley   PetscFunctionBegin;
125508449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
125608449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
125708449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
125808449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
125908449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
126008449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1261c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
126208449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
126308449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
126408449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
126508449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
126608449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
126769291d52SBarry Smith   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
126808449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
126908449791SMatthew G. Knepley     const PetscInt        *cells;
1270640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1271640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
127208449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
12732eefff9cSMatthew G. Knepley     PetscReal             *voll   = *vol;
12747c45b140SToby Isaac     PetscInt               ghost, d, nchild, nsupp;
127508449791SMatthew G. Knepley 
127608449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
12777c45b140SToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
12787c45b140SToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
12797c45b140SToby Isaac     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
128008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
128108449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
128208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
128308449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
128408449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
128508449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
128608449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
128708449791SMatthew G. Knepley     }
128808449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
128908449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
129008449791SMatthew G. Knepley     ++iface;
129108449791SMatthew G. Knepley   }
12925f942ad5SMatthew G. Knepley   *Nface = iface;
129308449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
129408449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
129508449791SMatthew G. Knepley   PetscFunctionReturn(0);
129608449791SMatthew G. Knepley }
129708449791SMatthew G. Knepley 
129808449791SMatthew G. Knepley /*@C
129908449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
130008449791SMatthew G. Knepley 
130108449791SMatthew G. Knepley   Input Parameters:
130208449791SMatthew G. Knepley + dm     - The DM
130308449791SMatthew G. Knepley . fStart - The first face to include
130408449791SMatthew G. Knepley . fEnd   - The first face to exclude
130508449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
130608449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
130708449791SMatthew G. Knepley 
130808449791SMatthew G. Knepley   Output Parameters:
13095f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
13105f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal
131108449791SMatthew G. Knepley - vol   - The cell volume
131208449791SMatthew G. Knepley 
131308449791SMatthew G. Knepley   Level: developer
131408449791SMatthew G. Knepley 
131508449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
131608449791SMatthew G. Knepley @*/
13175f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
131808449791SMatthew G. Knepley {
131908449791SMatthew G. Knepley   PetscErrorCode ierr;
132008449791SMatthew G. Knepley 
132108449791SMatthew G. Knepley   PetscFunctionBegin;
132208449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
132369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
132408449791SMatthew G. Knepley   PetscFunctionReturn(0);
132508449791SMatthew G. Knepley }
132608449791SMatthew G. Knepley 
13274a3e9fdbSToby Isaac static PetscErrorCode ISIntersect_Caching(IS is1, IS is2, IS *isect)
13284a3e9fdbSToby Isaac {
13294a3e9fdbSToby Isaac   PetscErrorCode ierr;
13304a3e9fdbSToby Isaac 
13314a3e9fdbSToby Isaac   PetscFunctionBegin;
13324a3e9fdbSToby Isaac   *isect = NULL;
13334a3e9fdbSToby Isaac   if (is2 && is1) {
13344a3e9fdbSToby Isaac     char           composeStr[33] = {0};
13354a3e9fdbSToby Isaac     PetscObjectId  is2id;
13364a3e9fdbSToby Isaac 
13374a3e9fdbSToby Isaac     ierr = PetscObjectGetId((PetscObject)is2,&is2id);CHKERRQ(ierr);
13384a3e9fdbSToby Isaac     ierr = PetscSNPrintf(composeStr,32,"ISIntersect_Caching_%x",is2id);CHKERRQ(ierr);
13394a3e9fdbSToby Isaac     ierr = PetscObjectQuery((PetscObject) is1, composeStr, (PetscObject *) isect);CHKERRQ(ierr);
13404a3e9fdbSToby Isaac     if (*isect == NULL) {
13414a3e9fdbSToby Isaac       ierr = ISIntersect(is1, is2, isect);CHKERRQ(ierr);
13424a3e9fdbSToby Isaac       ierr = PetscObjectCompose((PetscObject) is1, composeStr, (PetscObject) *isect);CHKERRQ(ierr);
13434a3e9fdbSToby Isaac     } else {
13444a3e9fdbSToby Isaac       ierr = PetscObjectReference((PetscObject) *isect);CHKERRQ(ierr);
13454a3e9fdbSToby Isaac     }
13464a3e9fdbSToby Isaac   }
13474a3e9fdbSToby Isaac   PetscFunctionReturn(0);
13484a3e9fdbSToby Isaac }
13494a3e9fdbSToby Isaac 
1350c330f8ffSToby Isaac static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
135108449791SMatthew G. Knepley {
135208449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
13534236e4b7SMatthew G. Knepley   DM               plex = NULL;
13544236e4b7SMatthew G. Knepley   PetscDS          prob, probAux = NULL;
13554236e4b7SMatthew G. Knepley   PetscSection     section, sectionAux = NULL;
13562d91c981SSander Arens   Vec              locA = NULL;
1357c330f8ffSToby Isaac   PetscFEGeom     *fgeom;
13582d91c981SSander Arens   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
13594236e4b7SMatthew G. Knepley   PetscInt         v;
1360c330f8ffSToby Isaac   PetscInt         totDim, totDimAux = 0;
136108449791SMatthew G. Knepley   PetscErrorCode   ierr;
136208449791SMatthew G. Knepley 
136308449791SMatthew G. Knepley   PetscFunctionBegin;
13644236e4b7SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
136508449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
13664d0b9603SSander Arens   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
13672d91c981SSander Arens   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
13682d91c981SSander Arens   if (locA) {
13694236e4b7SMatthew G. Knepley     DM dmAux;
13704236e4b7SMatthew G. Knepley 
13712d91c981SSander Arens     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
13722d91c981SSander Arens     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
13734d0b9603SSander Arens     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
13744236e4b7SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
13754236e4b7SMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
13762d91c981SSander Arens   }
13774236e4b7SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
1378c330f8ffSToby Isaac     PetscBool        isAffine;
1379c330f8ffSToby Isaac     PetscQuadrature  qGeom = NULL;
138024cdb843SMatthew G. Knepley     IS               pointIS;
138124cdb843SMatthew G. Knepley     const PetscInt  *points;
1382f74ed4f0SToby Isaac     PetscInt         numFaces, face, Nq;
138324cdb843SMatthew G. Knepley 
1384a8e83e26SSanderA     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
138522734eb1Ssarens     if (!pointIS) continue; /* No points with that id on this process */
1386c330f8ffSToby Isaac     {
1387c330f8ffSToby Isaac       IS isectIS;
1388c330f8ffSToby Isaac 
13894a3e9fdbSToby Isaac       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
13904a3e9fdbSToby Isaac       ierr = ISIntersect_Caching(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
1391c330f8ffSToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1392c330f8ffSToby Isaac       pointIS = isectIS;
139324cdb843SMatthew G. Knepley     }
1394c330f8ffSToby Isaac     ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr);
1395c330f8ffSToby Isaac     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
1396c330f8ffSToby Isaac     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
1397c330f8ffSToby Isaac     ierr = DMFieldGetFEInvariance(coordField,pointIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
1398c330f8ffSToby Isaac     if (isAffine) {
1399c330f8ffSToby Isaac       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
1400c330f8ffSToby Isaac     }
1401c330f8ffSToby Isaac     if (!qGeom) {
1402c330f8ffSToby Isaac       PetscFE fe;
1403c330f8ffSToby Isaac 
1404c330f8ffSToby Isaac       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1405c330f8ffSToby Isaac       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
1406c330f8ffSToby Isaac       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
1407c330f8ffSToby Isaac     }
1408c330f8ffSToby Isaac     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
14094a3e9fdbSToby Isaac     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
1410c330f8ffSToby Isaac     for (face = 0; face < numFaces; ++face) {
1411c330f8ffSToby Isaac       const PetscInt point = points[face], *support, *cone;
141224cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
14139bfb2fe4SJed Brown       PetscInt       i, coneSize, faceLoc;
141424cdb843SMatthew G. Knepley 
14154d0b9603SSander Arens       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
14164d0b9603SSander Arens       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
14174d0b9603SSander Arens       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
14184d0b9603SSander Arens       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
14194236e4b7SMatthew G. Knepley       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
1420c330f8ffSToby Isaac       fgeom->face[face][0] = faceLoc;
14214d0b9603SSander Arens       ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
14224d0b9603SSander Arens       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
14234d0b9603SSander Arens       ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
142408449791SMatthew G. Knepley       if (locX_t) {
14254d0b9603SSander Arens         ierr = DMPlexVecGetClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
14264d0b9603SSander Arens         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
14274d0b9603SSander Arens         ierr = DMPlexVecRestoreClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
142824cdb843SMatthew G. Knepley       }
14292d91c981SSander Arens       if (locA) {
14304d0b9603SSander Arens         ierr = DMPlexVecGetClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
14314d0b9603SSander Arens         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
14324d0b9603SSander Arens         ierr = DMPlexVecRestoreClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
143324cdb843SMatthew G. Knepley       }
143424cdb843SMatthew G. Knepley     }
14354d0b9603SSander Arens     ierr = PetscMemzero(elemVec, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1436cbf52bb1SSander Arens     {
143724cdb843SMatthew G. Knepley       PetscFE         fe;
1438f74ed4f0SToby Isaac       PetscInt        Nb;
1439c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
144024cdb843SMatthew G. Knepley       /* Conforming batches */
144124cdb843SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
144224cdb843SMatthew G. Knepley       /* Remainder */
144324cdb843SMatthew G. Knepley       PetscInt        Nr, offset;
144424cdb843SMatthew G. Knepley 
14454d0b9603SSander Arens       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
144624cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
144724cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1448c330f8ffSToby Isaac       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
1449c330f8ffSToby Isaac       blockSize = Nb;
145024cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
145124cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
145224cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
145324cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
145424cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
145524cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
1456c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
1457c330f8ffSToby Isaac       ierr = PetscFEIntegrateBdResidual(fe, prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
1458c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
1459c330f8ffSToby Isaac       ierr = PetscFEIntegrateBdResidual(fe, prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
1460c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
146124cdb843SMatthew G. Knepley     }
1462c330f8ffSToby Isaac     for (face = 0; face < numFaces; ++face) {
1463c330f8ffSToby Isaac       const PetscInt point = points[face], *support;
146424cdb843SMatthew G. Knepley 
14654d0b9603SSander Arens       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
14664d0b9603SSander Arens       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
14674d0b9603SSander Arens       ierr = DMPlexVecSetClosure(dm, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
146824cdb843SMatthew G. Knepley     }
14694a3e9fdbSToby Isaac     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
1470c330f8ffSToby Isaac     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
147124cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
147224cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1473c330f8ffSToby Isaac     ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr);
147424cdb843SMatthew G. Knepley   }
14754a3e9fdbSToby Isaac   if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
14764236e4b7SMatthew G. Knepley   PetscFunctionReturn(0);
1477a8e83e26SSanderA }
14784236e4b7SMatthew G. Knepley 
14794236e4b7SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
14804236e4b7SMatthew G. Knepley {
14814236e4b7SMatthew G. Knepley   PetscDS        prob;
1482a179f353SToby Isaac   PetscInt       dim, numBd, bd;
1483a179f353SToby Isaac   DMLabel        depthLabel;
1484c330f8ffSToby Isaac   DMField        coordField = NULL;
1485c330f8ffSToby Isaac   IS             facetIS;
14864236e4b7SMatthew G. Knepley   PetscErrorCode ierr;
14874236e4b7SMatthew G. Knepley 
14884236e4b7SMatthew G. Knepley   PetscFunctionBegin;
14894236e4b7SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1490a179f353SToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1491c330f8ffSToby Isaac   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1492a179f353SToby Isaac   ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr);
14934236e4b7SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
14944a3e9fdbSToby Isaac   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
14954236e4b7SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
14964236e4b7SMatthew G. Knepley     DMBoundaryConditionType type;
14974236e4b7SMatthew G. Knepley     const char             *bdLabel;
14984236e4b7SMatthew G. Knepley     DMLabel                 label;
14994236e4b7SMatthew G. Knepley     const PetscInt         *values;
15004236e4b7SMatthew G. Knepley     PetscInt                field, numValues;
15014236e4b7SMatthew G. Knepley     PetscObject             obj;
15024236e4b7SMatthew G. Knepley     PetscClassId            id;
15034236e4b7SMatthew G. Knepley 
15044236e4b7SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
15054236e4b7SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
15064236e4b7SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15074236e4b7SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
15084236e4b7SMatthew G. Knepley     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1509c330f8ffSToby Isaac     ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
15104236e4b7SMatthew G. Knepley   }
1511c330f8ffSToby Isaac   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
151208449791SMatthew G. Knepley   PetscFunctionReturn(0);
151308449791SMatthew G. Knepley }
151408449791SMatthew G. Knepley 
15154a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
151608449791SMatthew G. Knepley {
151708449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
151808449791SMatthew G. Knepley   const char       *name       = "Residual";
151908449791SMatthew G. Knepley   DM                dmAux      = NULL;
152008449791SMatthew G. Knepley   DM                dmGrad     = NULL;
152108449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
152208449791SMatthew G. Knepley   PetscDS           prob       = NULL;
152308449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
152408449791SMatthew G. Knepley   PetscSection      section    = NULL;
152508449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
152608449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
1527b2666ceaSMatthew G. Knepley   PetscBool         isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
152808449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
152908449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
153008449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
1531c330f8ffSToby Isaac   DMField           coordField = NULL;
1532c330f8ffSToby Isaac   Vec               locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
15333755293bSMatthew G. Knepley   PetscScalar      *u = NULL, *u_t, *a, *uL, *uR;
1534c4d4a4f8SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
15354a3e9fdbSToby Isaac   PetscBool         isStride;
15364a3e9fdbSToby Isaac   PetscInt          cStart = -1, cEnd = -1, numCells;
15374a3e9fdbSToby Isaac   PetscBool         isAffine = PETSC_FALSE;
15384a3e9fdbSToby Isaac   PetscQuadrature   affineQuad = NULL, *quads = NULL;
15394a3e9fdbSToby Isaac   PetscFEGeom      *affineGeom = NULL, **geoms = NULL;
154008449791SMatthew G. Knepley   PetscErrorCode    ierr;
154108449791SMatthew G. Knepley 
154208449791SMatthew G. Knepley   PetscFunctionBegin;
154308449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
154408449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1545195142f5SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
154608449791SMatthew G. Knepley   /* FEM+FVM */
154708449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
154808449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1549c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
155008449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
155108449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
155208449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
155308449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
155408449791SMatthew G. Knepley   if (locA) {
155508449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
155608449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
155708449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
155808449791SMatthew G. Knepley   }
155908449791SMatthew G. Knepley   /* 2: Get geometric data */
156008449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
156108449791SMatthew G. Knepley     PetscObject  obj;
156208449791SMatthew G. Knepley     PetscClassId id;
15637173168dSMatthew G. Knepley     PetscBool    fimp;
156408449791SMatthew G. Knepley 
15657173168dSMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
15667173168dSMatthew G. Knepley     if (isImplicit != fimp) continue;
156708449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
156808449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
156908449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
157008449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
157108449791SMatthew G. Knepley   }
157208449791SMatthew G. Knepley   if (useFEM) {
15734a3e9fdbSToby Isaac     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
15744a3e9fdbSToby Isaac     ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
15754a3e9fdbSToby Isaac     if (isAffine) {
15764a3e9fdbSToby Isaac       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1577*741db25dSToby Isaac       if (affineQuad) {
15784a3e9fdbSToby Isaac         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
1579*741db25dSToby Isaac       }
15804a3e9fdbSToby Isaac     } else {
15814a3e9fdbSToby Isaac       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
15824a3e9fdbSToby Isaac       for (f = 0; f < Nf; ++f) {
15834a3e9fdbSToby Isaac         PetscObject  obj;
15844a3e9fdbSToby Isaac         PetscClassId id;
15854a3e9fdbSToby Isaac         PetscBool    fimp;
15864a3e9fdbSToby Isaac 
15874a3e9fdbSToby Isaac         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
15884a3e9fdbSToby Isaac         if (isImplicit != fimp) continue;
15894a3e9fdbSToby Isaac         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
15904a3e9fdbSToby Isaac         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15914a3e9fdbSToby Isaac         if (id == PETSCFE_CLASSID) {
15924a3e9fdbSToby Isaac           PetscFE fe = (PetscFE) obj;
15934a3e9fdbSToby Isaac 
15944a3e9fdbSToby Isaac           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
15954a3e9fdbSToby Isaac           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
15964a3e9fdbSToby Isaac           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
15974a3e9fdbSToby Isaac         }
15984a3e9fdbSToby Isaac       }
15994a3e9fdbSToby Isaac     }
160008449791SMatthew G. Knepley   }
160108449791SMatthew G. Knepley   if (useFVM) {
160208449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
160308449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
160408449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
160508449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
160608449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
160708449791SMatthew G. Knepley     if (dmGrad) {
160808449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
160908449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1610de555695SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
161108449791SMatthew G. Knepley       /* Communicate gradient values */
161208449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
161308449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
161408449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
161508449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
161608449791SMatthew G. Knepley     }
1617bdd6f66aSToby Isaac     /* Handle non-essential (e.g. outflow) boundary values */
1618bdd6f66aSToby Isaac     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
161908449791SMatthew G. Knepley   }
162008449791SMatthew G. Knepley   /* Loop over chunks */
16214a3e9fdbSToby Isaac   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
16224a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) cellIS, ISSTRIDE, &isStride);CHKERRQ(ierr);
16234a3e9fdbSToby Isaac   if (isStride) {
16244a3e9fdbSToby Isaac     PetscInt step;
16254a3e9fdbSToby Isaac 
16264a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(cellIS, &cStart, &step);CHKERRQ(ierr);
16274a3e9fdbSToby Isaac     cEnd = cStart + numCells;
16284a3e9fdbSToby Isaac     if (step != 1) {
16294a3e9fdbSToby Isaac       isStride = PETSC_FALSE;
16304a3e9fdbSToby Isaac     }
16314a3e9fdbSToby Isaac   }
16324a3e9fdbSToby Isaac   if (!isStride) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not implemented yet");
163308449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
163408449791SMatthew G. Knepley   numChunks     = 1;
16354a3e9fdbSToby Isaac   cellChunkSize = numCells/numChunks;
163608449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
1637*741db25dSToby Isaac   numChunks     = PetscMin(1,numCells);
163808449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
16392eefff9cSMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
16402eefff9cSMatthew G. Knepley     PetscReal       *vol;
164108449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
164208449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
16433755293bSMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
164408449791SMatthew G. Knepley 
164508449791SMatthew G. Knepley     /* Extract field coefficients */
164608449791SMatthew G. Knepley     if (useFEM) {
164708449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
164869291d52SBarry Smith       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
1649215c4595SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
165008449791SMatthew G. Knepley     }
165108449791SMatthew G. Knepley     if (useFVM) {
16525f942ad5SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
16535f942ad5SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
165469291d52SBarry Smith       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
165569291d52SBarry Smith       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
1656215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1657215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
165808449791SMatthew G. Knepley     }
165908449791SMatthew 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 */
166008449791SMatthew G. Knepley     /* Loop over fields */
166108449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
166208449791SMatthew G. Knepley       PetscObject  obj;
166308449791SMatthew G. Knepley       PetscClassId id;
16647173168dSMatthew G. Knepley       PetscBool    fimp;
166508449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
166608449791SMatthew G. Knepley 
16677173168dSMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
16687173168dSMatthew G. Knepley       if (isImplicit != fimp) continue;
166908449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
167008449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
167108449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
167208449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
16734a3e9fdbSToby Isaac         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
16744a3e9fdbSToby Isaac         PetscFEGeom    *chunkGeom = NULL;
16754a3e9fdbSToby Isaac         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
167608449791SMatthew G. Knepley         PetscInt        Nq, Nb;
167708449791SMatthew G. Knepley 
167808449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
16794a3e9fdbSToby Isaac         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
168008449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1681c330f8ffSToby Isaac         blockSize = Nb;
168208449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
168308449791SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
168408449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
168508449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
168608449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
168708449791SMatthew G. Knepley         offset    = numCells - Nr;
168808449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
168908449791SMatthew 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) */
16904a3e9fdbSToby Isaac         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
1691c330f8ffSToby Isaac         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
16924a3e9fdbSToby Isaac         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1693c330f8ffSToby Isaac         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
16944a3e9fdbSToby Isaac         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
169508449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
169608449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
169708449791SMatthew G. Knepley 
169808449791SMatthew G. Knepley         Ne = numFaces;
169908449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
170008449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
170108449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
170208449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
170308449791SMatthew G. Knepley     }
170408449791SMatthew G. Knepley     /* Loop over domain */
170508449791SMatthew G. Knepley     if (useFEM) {
170608449791SMatthew G. Knepley       /* Add elemVec to locX */
170708449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
170808449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
1709b4920ed3SToby Isaac         if (ghostLabel) {
1710b4920ed3SToby Isaac           PetscInt ghostVal;
1711b4920ed3SToby Isaac 
1712b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
1713b4920ed3SToby Isaac           if (ghostVal > 0) continue;
1714b4920ed3SToby Isaac         }
1715c14a31d2SToby Isaac         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
171608449791SMatthew G. Knepley       }
171708449791SMatthew G. Knepley     }
171808449791SMatthew G. Knepley     if (useFVM) {
17194a394323SMatthew G. Knepley       PetscScalar *fa;
172008449791SMatthew G. Knepley       PetscInt     iface;
172108449791SMatthew G. Knepley 
172208449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1723c10b5f1bSMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1724c10b5f1bSMatthew G. Knepley         PetscFV      fv;
1725c10b5f1bSMatthew G. Knepley         PetscObject  obj;
1726c10b5f1bSMatthew G. Knepley         PetscClassId id;
17274a394323SMatthew G. Knepley         PetscInt     foff, pdim;
1728c10b5f1bSMatthew G. Knepley 
1729c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1730c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1731c10b5f1bSMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1732c10b5f1bSMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1733c10b5f1bSMatthew G. Knepley         fv   = (PetscFV) obj;
1734c10b5f1bSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1735c10b5f1bSMatthew G. Knepley         /* Accumulate fluxes to cells */
173608449791SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
173708449791SMatthew G. Knepley           const PetscInt *cells;
1738b4920ed3SToby Isaac           PetscScalar    *fL = NULL, *fR = NULL;
17397c45b140SToby Isaac           PetscInt        ghost, d, nsupp, nchild;
174008449791SMatthew G. Knepley 
174108449791SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1742ffe9ad51SToby Isaac           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
17437c45b140SToby Isaac           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
17447c45b140SToby Isaac           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
174508449791SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1746b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cells[0],&ghost);CHKERRQ(ierr);
1747b6a34eadSToby Isaac           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr);}
1748b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cells[1],&ghost);CHKERRQ(ierr);
1749b6a34eadSToby Isaac           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr);}
1750c10b5f1bSMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
1751c10b5f1bSMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1752c10b5f1bSMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
175308449791SMatthew G. Knepley           }
175408449791SMatthew G. Knepley           ++iface;
175508449791SMatthew G. Knepley         }
1756dab51205SMatthew G. Knepley       }
1757dab51205SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1758dab51205SMatthew G. Knepley     }
1759c10b5f1bSMatthew G. Knepley     /* Handle time derivative */
1760c10b5f1bSMatthew G. Knepley     if (locX_t) {
1761dab51205SMatthew G. Knepley       PetscScalar *x_t, *fa;
1762dab51205SMatthew G. Knepley 
1763dab51205SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1764c10b5f1bSMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
1765dab51205SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1766dab51205SMatthew G. Knepley         PetscFV      fv;
1767dab51205SMatthew G. Knepley         PetscObject  obj;
1768dab51205SMatthew G. Knepley         PetscClassId id;
1769dab51205SMatthew G. Knepley         PetscInt     pdim, d;
1770dab51205SMatthew G. Knepley 
1771dab51205SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1772dab51205SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1773dab51205SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1774dab51205SMatthew G. Knepley         fv   = (PetscFV) obj;
1775dab51205SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1776c10b5f1bSMatthew G. Knepley         for (cell = cS; cell < cE; ++cell) {
1777c10b5f1bSMatthew G. Knepley           PetscScalar *u_t, *r;
1778c10b5f1bSMatthew G. Knepley 
1779b4920ed3SToby Isaac           if (ghostLabel) {
1780b4920ed3SToby Isaac             PetscInt ghostVal;
1781b4920ed3SToby Isaac 
1782b4920ed3SToby Isaac             ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
1783b4920ed3SToby Isaac             if (ghostVal > 0) continue;
1784b4920ed3SToby Isaac           }
1785c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
1786c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
1787d63b37e5SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1788c10b5f1bSMatthew G. Knepley         }
1789dab51205SMatthew G. Knepley       }
1790c10b5f1bSMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
179108449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
179208449791SMatthew G. Knepley     }
179308449791SMatthew G. Knepley     if (useFEM) {
179408449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
179569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
179608449791SMatthew G. Knepley     }
179708449791SMatthew G. Knepley     if (useFVM) {
17985f942ad5SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
17995f942ad5SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
180069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
180169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
180208449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
180308449791SMatthew G. Knepley     }
180408449791SMatthew G. Knepley   }
180508449791SMatthew G. Knepley 
18064a3e9fdbSToby Isaac   if (useFEM) {
18074a3e9fdbSToby Isaac     ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);
18084a3e9fdbSToby Isaac 
18094a3e9fdbSToby Isaac     if (isAffine) {
18104a3e9fdbSToby Isaac       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
18114a3e9fdbSToby Isaac       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
18124a3e9fdbSToby Isaac     } else {
18134a3e9fdbSToby Isaac       for (f = 0; f < Nf; ++f) {
18144a3e9fdbSToby Isaac         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
18154a3e9fdbSToby Isaac         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
18164a3e9fdbSToby Isaac       }
18174a3e9fdbSToby Isaac       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
18184a3e9fdbSToby Isaac     }
18194a3e9fdbSToby Isaac   }
182008449791SMatthew G. Knepley 
182108449791SMatthew G. Knepley   /* FEM */
182208449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
182308449791SMatthew G. Knepley   /* 2: Get geometric data */
182408449791SMatthew G. Knepley   /* 3: Handle boundary values */
182508449791SMatthew G. Knepley   /* 4: Loop over domain */
182608449791SMatthew G. Knepley   /*   Extract coefficients */
182708449791SMatthew G. Knepley   /* Loop over fields */
182808449791SMatthew G. Knepley   /*   Set tiling for FE*/
182908449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
183008449791SMatthew G. Knepley   /*     Loop over subdomain */
183108449791SMatthew G. Knepley   /*       Loop over quad points */
183208449791SMatthew G. Knepley   /*         Transform coords to real space */
183308449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
183408449791SMatthew G. Knepley   /*         Evaluate residual at point */
183508449791SMatthew G. Knepley   /*         Transform residual to real space */
183608449791SMatthew G. Knepley   /*       Add residual to elemVec */
183708449791SMatthew G. Knepley   /* Loop over domain */
183808449791SMatthew G. Knepley   /*   Add elemVec to locX */
183908449791SMatthew G. Knepley 
184008449791SMatthew G. Knepley   /* FVM */
184108449791SMatthew G. Knepley   /* Get geometric data */
184208449791SMatthew G. Knepley   /* If using gradients */
184308449791SMatthew G. Knepley   /*   Compute gradient data */
184408449791SMatthew G. Knepley   /*   Loop over domain faces */
184508449791SMatthew G. Knepley   /*     Count computational faces */
184608449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
184708449791SMatthew G. Knepley   /*   Loop over domain cells */
184808449791SMatthew G. Knepley   /*     Limit cell gradients */
184908449791SMatthew G. Knepley   /* Handle boundary values */
185008449791SMatthew G. Knepley   /* Loop over domain faces */
185108449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
185208449791SMatthew G. Knepley   /* Riemann solve over faces */
185308449791SMatthew G. Knepley   /* Loop over domain faces */
185408449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
185508449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
1856247ba720SToby Isaac   if (mesh->printFEM) {
1857247ba720SToby Isaac     Vec         locFbc;
1858247ba720SToby Isaac     PetscInt    pStart, pEnd, p, maxDof;
1859247ba720SToby Isaac     PetscScalar *zeroes;
1860247ba720SToby Isaac 
1861247ba720SToby Isaac     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
1862247ba720SToby Isaac     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
1863247ba720SToby Isaac     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
1864247ba720SToby Isaac     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
1865247ba720SToby Isaac     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
1866247ba720SToby Isaac     for (p = pStart; p < pEnd; p++) {
1867247ba720SToby Isaac       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
1868247ba720SToby Isaac     }
1869247ba720SToby Isaac     ierr = PetscFree(zeroes);CHKERRQ(ierr);
1870247ba720SToby Isaac     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
1871247ba720SToby Isaac     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
1872247ba720SToby Isaac   }
187324cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
187424cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
187524cdb843SMatthew G. Knepley }
187624cdb843SMatthew G. Knepley 
187711dd639bSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user)
187824cdb843SMatthew G. Knepley {
187924cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1880c330f8ffSToby Isaac   Vec               A;
1881c330f8ffSToby Isaac   DMField           coordField = NULL;
188224cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
188324cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
188424cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
1885c330f8ffSToby Isaac   PetscInt          Nf, f, numCells, cStart, cEnd, c;
18863755293bSMatthew G. Knepley   PetscInt          totDim, totDimAux = 0, diffCell = 0;
18874a3e9fdbSToby Isaac   PetscInt          depth;
1888c330f8ffSToby Isaac   PetscBool         isAffine;
1889c330f8ffSToby Isaac   IS                cellIS;
18904a3e9fdbSToby Isaac   DMLabel           depthLabel;
189124cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
189224cdb843SMatthew G. Knepley 
189324cdb843SMatthew G. Knepley   PetscFunctionBegin;
189424cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
189524cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
189624cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
189724cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
189824cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
189924cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
190024cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
190124cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
190224cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
190324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
190424cdb843SMatthew G. Knepley   if (dmAux) {
190524cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
190624cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
190724cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
190824cdb843SMatthew G. Knepley   }
190924cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1910bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
191124cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
191224cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
19134a3e9fdbSToby Isaac   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
19144a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
19154a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel,depth,&cellIS);CHKERRQ(ierr);
19164a3e9fdbSToby Isaac   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
191724cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
191824cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
191924cdb843SMatthew G. Knepley     PetscInt     i;
192024cdb843SMatthew G. Knepley 
192124cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
192224cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
192324cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
192424cdb843SMatthew G. Knepley     if (X_t) {
192524cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
192624cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
192724cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
192824cdb843SMatthew G. Knepley     }
192924cdb843SMatthew G. Knepley     if (dmAux) {
19306da023fcSToby Isaac       DM dmAuxPlex;
19316da023fcSToby Isaac 
19326da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
19336da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
193424cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
19356da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
19366da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
193724cdb843SMatthew G. Knepley     }
193824cdb843SMatthew G. Knepley   }
193924cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
194024cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
1941c330f8ffSToby Isaac     PetscInt Nq, Nb;
194224cdb843SMatthew G. Knepley     /* Conforming batches */
194324cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
194424cdb843SMatthew G. Knepley     /* Remainder */
194524cdb843SMatthew G. Knepley     PetscInt Nr, offset;
1946c330f8ffSToby Isaac     PetscQuadrature qGeom = NULL;
1947c330f8ffSToby Isaac     PetscFEGeom *cgeomFEM, *chunkGeom = NULL;
194824cdb843SMatthew G. Knepley 
194924cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
195024cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
195124cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
195224cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1953c330f8ffSToby Isaac     ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
1954c330f8ffSToby Isaac     if (isAffine) {
1955c330f8ffSToby Isaac       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
1956c330f8ffSToby Isaac     }
1957c330f8ffSToby Isaac     if (!qGeom) {
1958c330f8ffSToby Isaac       ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
1959c330f8ffSToby Isaac       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
1960c330f8ffSToby Isaac     }
1961c330f8ffSToby Isaac     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
19624a3e9fdbSToby Isaac     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1963c330f8ffSToby Isaac     blockSize = Nb;
196424cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
196524cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
196624cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
196724cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
196824cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
196924cdb843SMatthew G. Knepley     offset    = numCells - Nr;
1970c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
1971c330f8ffSToby Isaac     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
1972c330f8ffSToby Isaac     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr);
1973c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1974c330f8ffSToby Isaac     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
1975c330f8ffSToby Isaac     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVecCh[offset*totDim]);CHKERRQ(ierr);
1976c330f8ffSToby Isaac     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
19774a3e9fdbSToby Isaac     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1978c330f8ffSToby Isaac     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
197924cdb843SMatthew G. Knepley   }
1980c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
198124cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
198224cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
198324cdb843SMatthew G. Knepley     PetscInt  d;
198424cdb843SMatthew G. Knepley 
198524cdb843SMatthew 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;}
198624cdb843SMatthew G. Knepley     if (diff) {
198724cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
198824cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
198924cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
199024cdb843SMatthew G. Knepley       ++diffCell;
199124cdb843SMatthew G. Knepley     }
199224cdb843SMatthew G. Knepley     if (diffCell > 9) break;
1993c14a31d2SToby Isaac     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
199424cdb843SMatthew G. Knepley   }
1995bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
199624cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
199724cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
199824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
199924cdb843SMatthew G. Knepley }
200024cdb843SMatthew G. Knepley 
200124cdb843SMatthew G. Knepley /*@
200224cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
200324cdb843SMatthew G. Knepley 
200424cdb843SMatthew G. Knepley   Input Parameters:
200524cdb843SMatthew G. Knepley + dm - The mesh
200624cdb843SMatthew G. Knepley . X  - Local solution
200724cdb843SMatthew G. Knepley - user - The user context
200824cdb843SMatthew G. Knepley 
200924cdb843SMatthew G. Knepley   Output Parameter:
201024cdb843SMatthew G. Knepley . F  - Local output vector
201124cdb843SMatthew G. Knepley 
201224cdb843SMatthew G. Knepley   Level: developer
201324cdb843SMatthew G. Knepley 
201424cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
201524cdb843SMatthew G. Knepley @*/
201624cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
201724cdb843SMatthew G. Knepley {
201824cdb843SMatthew G. Knepley   PetscObject    check;
20196da023fcSToby Isaac   DM             plex;
20204a3e9fdbSToby Isaac   IS             cellIS;
20214a3e9fdbSToby Isaac   PetscInt       depth;
202224cdb843SMatthew G. Knepley   PetscErrorCode ierr;
202324cdb843SMatthew G. Knepley 
202424cdb843SMatthew G. Knepley   PetscFunctionBegin;
20256da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
20264a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2027aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
20284a3e9fdbSToby Isaac   if (!cellIS) {
20294a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
20304a3e9fdbSToby Isaac   }
203124cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
20326da023fcSToby Isaac   ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr);
203311dd639bSMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
20344a3e9fdbSToby Isaac   else       {ierr = DMPlexComputeResidual_Internal(plex, cellIS, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
20354a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
20369a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
203724cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
203824cdb843SMatthew G. Knepley }
203924cdb843SMatthew G. Knepley 
2040bdd6f66aSToby Isaac /*@
2041bdd6f66aSToby Isaac   DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X
2042bdd6f66aSToby Isaac 
2043bdd6f66aSToby Isaac   Input Parameters:
2044bdd6f66aSToby Isaac + dm - The mesh
2045bdd6f66aSToby Isaac - user - The user context
2046bdd6f66aSToby Isaac 
2047bdd6f66aSToby Isaac   Output Parameter:
2048bdd6f66aSToby Isaac . X  - Local solution
2049bdd6f66aSToby Isaac 
2050bdd6f66aSToby Isaac   Level: developer
2051bdd6f66aSToby Isaac 
2052bdd6f66aSToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
2053bdd6f66aSToby Isaac @*/
2054bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user)
2055bdd6f66aSToby Isaac {
2056bdd6f66aSToby Isaac   DM             plex;
2057bdd6f66aSToby Isaac   PetscErrorCode ierr;
2058bdd6f66aSToby Isaac 
2059bdd6f66aSToby Isaac   PetscFunctionBegin;
2060bdd6f66aSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2061bdd6f66aSToby Isaac   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr);
2062bdd6f66aSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2063bdd6f66aSToby Isaac   PetscFunctionReturn(0);
2064bdd6f66aSToby Isaac }
2065bdd6f66aSToby Isaac 
2066089bfe53SSander Arens PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
2067089bfe53SSander Arens {
2068089bfe53SSander Arens   DM_Plex         *mesh = (DM_Plex *) dm->data;
20692d91c981SSander Arens   DM               dmAux = NULL, plex = NULL;
2070c330f8ffSToby Isaac   DMField          coordField = NULL;
20712d91c981SSander Arens   PetscSection     section, globalSection, subSection, sectionAux = NULL;
20722d91c981SSander Arens   PetscDS          prob, probAux = NULL;
2073089bfe53SSander Arens   DMLabel          depth;
20742d91c981SSander Arens   Vec              locA = NULL;
20752d91c981SSander Arens   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
20764d0b9603SSander Arens   PetscInt         dim, totDim, totDimAux, numBd, bd, Nf;
2077089bfe53SSander Arens   PetscBool        isMatISP;
2078c330f8ffSToby Isaac   IS               facetIS;
2079089bfe53SSander Arens   PetscErrorCode   ierr;
2080089bfe53SSander Arens 
2081089bfe53SSander Arens   PetscFunctionBegin;
2082089bfe53SSander Arens   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2083089bfe53SSander Arens   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2084089bfe53SSander Arens   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
2085089bfe53SSander Arens   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2086089bfe53SSander Arens   if (isMatISP) {
2087089bfe53SSander Arens     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2088089bfe53SSander Arens   }
2089089bfe53SSander Arens   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2090c330f8ffSToby Isaac   ierr = DMLabelGetStratumIS(depth,dim-1,&facetIS);CHKERRQ(ierr);
2091089bfe53SSander Arens   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2092089bfe53SSander Arens   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
20934d0b9603SSander Arens   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2094089bfe53SSander Arens   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
20952d91c981SSander Arens   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
20962d91c981SSander Arens   if (locA) {
20972d91c981SSander Arens     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
20982d91c981SSander Arens     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
20992d91c981SSander Arens     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
21002d91c981SSander Arens     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
21014d0b9603SSander Arens     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
21022d91c981SSander Arens   }
21034a3e9fdbSToby Isaac   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
2104089bfe53SSander Arens   for (bd = 0; bd < numBd; ++bd) {
2105df1fddb2SMatthew G. Knepley     DMBoundaryConditionType type;
2106089bfe53SSander Arens     const char             *bdLabel;
2107089bfe53SSander Arens     DMLabel                 label;
2108089bfe53SSander Arens     IS                      pointIS;
2109089bfe53SSander Arens     const PetscInt         *points;
2110089bfe53SSander Arens     const PetscInt         *values;
2111c330f8ffSToby Isaac     PetscInt                fieldI, fieldJ, numValues, v, numFaces, face, Nq;
2112089bfe53SSander Arens     PetscObject             obj;
2113c330f8ffSToby Isaac     PetscBool               isAffine;
2114c330f8ffSToby Isaac     PetscFE                 fe;
2115089bfe53SSander Arens     PetscClassId            id;
2116089bfe53SSander Arens 
2117df1fddb2SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
21184d0b9603SSander Arens     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
2119089bfe53SSander Arens     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2120df1fddb2SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
2121c330f8ffSToby Isaac     fe   = (PetscFE) obj;
2122089bfe53SSander Arens     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
2123089bfe53SSander Arens     for (v = 0; v < numValues; ++v) {
2124c330f8ffSToby Isaac       PetscQuadrature qGeom = NULL;
2125c330f8ffSToby Isaac       PetscFEGeom     *fgeom;
2126c330f8ffSToby Isaac 
2127089bfe53SSander Arens       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2128089bfe53SSander Arens       if (!pointIS) continue; /* No points with that id on this process */
2129c330f8ffSToby Isaac       {
2130c330f8ffSToby Isaac         IS isectIS;
2131c330f8ffSToby Isaac 
2132c330f8ffSToby Isaac         /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
21334a3e9fdbSToby Isaac         ierr = ISIntersect_Caching(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
2134c330f8ffSToby Isaac         ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2135c330f8ffSToby Isaac         pointIS = isectIS;
2136089bfe53SSander Arens       }
2137c330f8ffSToby Isaac       ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
2138c330f8ffSToby Isaac       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2139f99c8401SToby Isaac       ierr = PetscMalloc3(numFaces*totDim,&u,locX_t ? numFaces*totDim : 0,&u_t,numFaces*totDim*totDim,&elemMat);CHKERRQ(ierr);
21404d0b9603SSander Arens       if (locA) {ierr = PetscMalloc1(numFaces*totDimAux,&a);CHKERRQ(ierr);}
2141c330f8ffSToby Isaac       ierr = DMFieldGetFEInvariance(coordField,pointIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
2142c330f8ffSToby Isaac       if (isAffine) {
2143c330f8ffSToby Isaac         ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
2144c330f8ffSToby Isaac       }
2145c330f8ffSToby Isaac       if (!qGeom) {
2146c330f8ffSToby Isaac         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
2147c330f8ffSToby Isaac         ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2148c330f8ffSToby Isaac       }
2149c330f8ffSToby Isaac       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
21504a3e9fdbSToby Isaac       ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
2151c330f8ffSToby Isaac       for (face = 0; face < numFaces; ++face) {
2152c330f8ffSToby Isaac         const PetscInt point = points[face], *support, *cone;
2153089bfe53SSander Arens         PetscScalar   *x     = NULL;
21549bfb2fe4SJed Brown         PetscInt       i, coneSize, faceLoc;
2155089bfe53SSander Arens 
21564d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
21574d0b9603SSander Arens         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
21584d0b9603SSander Arens         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
21594d0b9603SSander Arens         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
21604d0b9603SSander Arens         if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %d in cone of support[0] %d", point, support[0]);
2161f99c8401SToby Isaac         fgeom->face[face][0] = faceLoc;
21624d0b9603SSander Arens         ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
21634d0b9603SSander Arens         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
21644d0b9603SSander Arens         ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
2165089bfe53SSander Arens         if (locX_t) {
21664d0b9603SSander Arens           ierr = DMPlexVecGetClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
21674d0b9603SSander Arens           for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
21684d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
2169089bfe53SSander Arens         }
21702d91c981SSander Arens         if (locA) {
21714d0b9603SSander Arens           ierr = DMPlexVecGetClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
21724d0b9603SSander Arens           for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
21734d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
21742d91c981SSander Arens         }
2175089bfe53SSander Arens       }
21764d0b9603SSander Arens       ierr = PetscMemzero(elemMat, numFaces*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2177089bfe53SSander Arens       {
2178089bfe53SSander Arens         PetscFE         fe;
2179c330f8ffSToby Isaac         PetscInt        Nb;
2180089bfe53SSander Arens         /* Conforming batches */
2181089bfe53SSander Arens         PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2182089bfe53SSander Arens         /* Remainder */
2183c330f8ffSToby Isaac         PetscFEGeom    *chunkGeom = NULL;
2184089bfe53SSander Arens         PetscInt        Nr, offset;
2185089bfe53SSander Arens 
21864d0b9603SSander Arens         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2187089bfe53SSander Arens         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2188089bfe53SSander Arens         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2189c330f8ffSToby Isaac         blockSize = Nb;
2190089bfe53SSander Arens         batchSize = numBlocks * blockSize;
2191089bfe53SSander Arens         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2192089bfe53SSander Arens         numChunks = numFaces / (numBatches*batchSize);
2193089bfe53SSander Arens         Ne        = numChunks*numBatches*batchSize;
2194089bfe53SSander Arens         Nr        = numFaces % (numBatches*batchSize);
2195089bfe53SSander Arens         offset    = numFaces - Nr;
2196c330f8ffSToby Isaac         ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
2197089bfe53SSander Arens         for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2198c330f8ffSToby Isaac           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2199089bfe53SSander Arens         }
2200c330f8ffSToby Isaac         ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
2201c330f8ffSToby Isaac         for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2202c330f8ffSToby Isaac           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2203089bfe53SSander Arens         }
2204c330f8ffSToby Isaac         ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
2205c330f8ffSToby Isaac       }
2206c330f8ffSToby Isaac       for (face = 0; face < numFaces; ++face) {
2207c330f8ffSToby Isaac         const PetscInt point = points[face], *support;
2208089bfe53SSander Arens 
22094d0b9603SSander Arens         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
22104d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
2211089bfe53SSander Arens         if (!isMatISP) {
22124d0b9603SSander Arens           ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2213089bfe53SSander Arens         } else {
2214089bfe53SSander Arens           Mat lJ;
2215089bfe53SSander Arens 
2216089bfe53SSander Arens           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
22174d0b9603SSander Arens           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2218089bfe53SSander Arens         }
2219089bfe53SSander Arens       }
22204a3e9fdbSToby Isaac       ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
2221c330f8ffSToby Isaac       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2222f99c8401SToby Isaac       ierr = PetscFree3(u,u_t,elemMat);CHKERRQ(ierr);
2223089bfe53SSander Arens       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2224089bfe53SSander Arens       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22251c46aac1SSander Arens       if (locA) {ierr = PetscFree(a);CHKERRQ(ierr);}
22262d91c981SSander Arens     }
2227089bfe53SSander Arens   }
2228c330f8ffSToby Isaac   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
22291c46aac1SSander Arens   if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
2230089bfe53SSander Arens   PetscFunctionReturn(0);
2231089bfe53SSander Arens }
2232089bfe53SSander Arens 
22334a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
223424cdb843SMatthew G. Knepley {
223524cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
223624cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
2237f7ed7b21SMatthew G. Knepley   DM                dmAux, plex;
2238c330f8ffSToby Isaac   Vec               A;
2239c330f8ffSToby Isaac   DMField           coordField;
224024cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
2241be36d101SStefano Zampini   PetscSection      section, globalSection, subSection, sectionAux;
2242426ff135SMatthew G. Knepley   PetscScalar      *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
2243c330f8ffSToby Isaac   PetscInt          Nf, fieldI, fieldJ, numCells, c;
22444a3e9fdbSToby Isaac   PetscInt          totDim, totDimAux, cStart = -1, cEnd = -1;
2245be36d101SStefano Zampini   PetscBool         isMatIS, isMatISP, isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE;
22464a3e9fdbSToby Isaac   PetscBool         isStride;
224724cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
224824cdb843SMatthew G. Knepley 
224924cdb843SMatthew G. Knepley   PetscFunctionBegin;
225024cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
225124cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2252be36d101SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
225324cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2254be36d101SStefano Zampini   if (isMatISP) {
2255be36d101SStefano Zampini     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2256be36d101SStefano Zampini   }
225724cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
225824cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2259efc10488SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
226055ad3c34SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2261426ff135SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2262426ff135SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
226324cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
22644a3e9fdbSToby Isaac   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
226524cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
226624cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
226724cdb843SMatthew G. Knepley   if (dmAux) {
2268f7ed7b21SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2269f7ed7b21SMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
227024cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
227124cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
227224cdb843SMatthew G. Knepley   }
2273290cf56fSMatthew G. Knepley   if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);}
227424cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
2275efc10488SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr);
227624cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
22774a3e9fdbSToby Isaac   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
22784a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) cellIS, ISSTRIDE, &isStride);CHKERRQ(ierr);
22794a3e9fdbSToby Isaac   if (isStride) {
22804a3e9fdbSToby Isaac     PetscInt step;
22814a3e9fdbSToby Isaac 
22824a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(cellIS, &cStart, &step);CHKERRQ(ierr);
22834a3e9fdbSToby Isaac     cEnd = cStart + numCells;
22844a3e9fdbSToby Isaac     if (step != 1) {
22854a3e9fdbSToby Isaac       isStride = PETSC_FALSE;
22864a3e9fdbSToby Isaac     }
22874a3e9fdbSToby Isaac   }
22884a3e9fdbSToby Isaac   if (!isStride) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not implemented yet");
228924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
229024cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
229124cdb843SMatthew G. Knepley     PetscInt     i;
229224cdb843SMatthew G. Knepley 
229324cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
22949f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
229524cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
229624cdb843SMatthew G. Knepley     if (X_t) {
229724cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
22989f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
229924cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
230024cdb843SMatthew G. Knepley     }
230124cdb843SMatthew G. Knepley     if (dmAux) {
2302f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
23039f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
2304f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
230524cdb843SMatthew G. Knepley     }
230624cdb843SMatthew G. Knepley   }
2307efc10488SMatthew G. Knepley   if (hasJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
230855ad3c34SMatthew G. Knepley   if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2309426ff135SMatthew G. Knepley   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
231024cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
23115244db28SMatthew G. Knepley     PetscClassId    id;
231224cdb843SMatthew G. Knepley     PetscFE         fe;
2313c330f8ffSToby Isaac     PetscQuadrature qGeom = NULL;
2314c330f8ffSToby Isaac     PetscInt        Nb;
231524cdb843SMatthew G. Knepley     /* Conforming batches */
231624cdb843SMatthew G. Knepley     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
231724cdb843SMatthew G. Knepley     /* Remainder */
2318c330f8ffSToby Isaac     PetscInt        Nr, offset, Nq;
2319c330f8ffSToby Isaac     PetscBool       isAffine;
2320c330f8ffSToby Isaac     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
232124cdb843SMatthew G. Knepley 
232224cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
23235244db28SMatthew G. Knepley     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
23245244db28SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
232524cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
232624cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2327c330f8ffSToby Isaac     ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
2328c330f8ffSToby Isaac     if (isAffine) {
2329c330f8ffSToby Isaac       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
2330c330f8ffSToby Isaac     }
2331c330f8ffSToby Isaac     if (!qGeom) {
2332c330f8ffSToby Isaac       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
2333c330f8ffSToby Isaac       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2334c330f8ffSToby Isaac     }
2335c330f8ffSToby Isaac     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
23364a3e9fdbSToby Isaac     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2337c330f8ffSToby Isaac     blockSize = Nb;
233824cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
233924cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
234024cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
234124cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
234224cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
234324cdb843SMatthew G. Knepley     offset    = numCells - Nr;
2344c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2345c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
234624cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2347efc10488SMatthew G. Knepley       if (hasJac) {
2348c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2349c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2350efc10488SMatthew G. Knepley       }
235155ad3c34SMatthew G. Knepley       if (hasPrec) {
2352c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
2353c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
2354426ff135SMatthew G. Knepley       }
2355426ff135SMatthew G. Knepley       if (hasDyn) {
2356c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
2357c330f8ffSToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
235855ad3c34SMatthew G. Knepley       }
235924cdb843SMatthew G. Knepley     }
2360c330f8ffSToby Isaac     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2361c330f8ffSToby Isaac     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
23624a3e9fdbSToby Isaac     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2363c330f8ffSToby Isaac     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
236424cdb843SMatthew G. Knepley   }
2365426ff135SMatthew G. Knepley   if (hasDyn) {
2366426ff135SMatthew G. Knepley     for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2367426ff135SMatthew G. Knepley   }
23685244db28SMatthew G. Knepley   if (hasFV) {
23695244db28SMatthew G. Knepley     PetscClassId id;
23705244db28SMatthew G. Knepley     PetscFV      fv;
23715244db28SMatthew G. Knepley     PetscInt     offsetI, NcI, NbI = 1, fc, f;
23725244db28SMatthew G. Knepley 
23735244db28SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
23745244db28SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
23755244db28SMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
23765244db28SMatthew G. Knepley       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
23775244db28SMatthew G. Knepley       if (id != PETSCFV_CLASSID) continue;
23785244db28SMatthew G. Knepley       /* Put in the identity */
23795244db28SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
23805244db28SMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
23815244db28SMatthew G. Knepley         const PetscInt eOffset = (c-cStart)*totDim*totDim;
23825244db28SMatthew G. Knepley         for (fc = 0; fc < NcI; ++fc) {
23835244db28SMatthew G. Knepley           for (f = 0; f < NbI; ++f) {
23845244db28SMatthew G. Knepley             const PetscInt i = offsetI + f*NcI+fc;
23855244db28SMatthew G. Knepley             if (hasPrec) {
23865244db28SMatthew G. Knepley               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
23875244db28SMatthew G. Knepley               elemMatP[eOffset+i*totDim+i] = 1.0;
23885244db28SMatthew G. Knepley             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
23895244db28SMatthew G. Knepley           }
23905244db28SMatthew G. Knepley         }
23915244db28SMatthew G. Knepley       }
23925244db28SMatthew G. Knepley     }
23935244db28SMatthew G. Knepley     /* No allocated space for FV stuff, so ignore the zero entries */
23945244db28SMatthew G. Knepley     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
23955244db28SMatthew G. Knepley   }
2396be36d101SStefano Zampini   isMatIS = PETSC_FALSE;
2397be36d101SStefano Zampini   if (hasPrec && hasJac) {
2398be36d101SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);
2399be36d101SStefano Zampini   }
2400be36d101SStefano Zampini   if (isMatIS && !subSection) {
2401be36d101SStefano Zampini     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2402be36d101SStefano Zampini   }
240324cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
240455ad3c34SMatthew G. Knepley     if (hasPrec) {
2405efc10488SMatthew G. Knepley       if (hasJac) {
240655ad3c34SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2407be36d101SStefano Zampini         if (!isMatIS) {
240855ad3c34SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2409be36d101SStefano Zampini         } else {
2410be36d101SStefano Zampini           Mat lJ;
2411be36d101SStefano Zampini 
2412be36d101SStefano Zampini           ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
2413be36d101SStefano Zampini           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2414be36d101SStefano Zampini         }
2415efc10488SMatthew G. Knepley       }
241655ad3c34SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2417be36d101SStefano Zampini       if (!isMatISP) {
241855ad3c34SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
241955ad3c34SMatthew G. Knepley       } else {
2420be36d101SStefano Zampini         Mat lJ;
2421be36d101SStefano Zampini 
2422be36d101SStefano Zampini         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2423be36d101SStefano Zampini         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2424be36d101SStefano Zampini       }
2425be36d101SStefano Zampini     } else {
24269f11d433SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2427be36d101SStefano Zampini       if (!isMatISP) {
24289f11d433SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2429be36d101SStefano Zampini       } else {
2430be36d101SStefano Zampini         Mat lJ;
2431be36d101SStefano Zampini 
2432be36d101SStefano Zampini         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2433be36d101SStefano Zampini         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2434be36d101SStefano Zampini       }
243524cdb843SMatthew G. Knepley     }
243655ad3c34SMatthew G. Knepley   }
24375244db28SMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
2438426ff135SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
2439f7ed7b21SMatthew G. Knepley   if (dmAux) {
2440f7ed7b21SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
2441f7ed7b21SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2442f7ed7b21SMatthew G. Knepley   }
2443089bfe53SSander Arens   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
2444efc10488SMatthew G. Knepley   if (hasJac && hasPrec) {
244582ef7567SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
244682ef7567SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
244782ef7567SMatthew G. Knepley   }
244824cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
244924cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
245024cdb843SMatthew G. Knepley   if (mesh->printFEM) {
245124cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
245224cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
245324cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
245424cdb843SMatthew G. Knepley   }
245524cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
245624cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
245724cdb843SMatthew G. Knepley   if (isShell) {
245824cdb843SMatthew G. Knepley     JacActionCtx *jctx;
245924cdb843SMatthew G. Knepley 
246024cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
246124cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
246224cdb843SMatthew G. Knepley   }
246324cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
246424cdb843SMatthew G. Knepley }
246524cdb843SMatthew G. Knepley 
24664a3e9fdbSToby Isaac PetscErrorCode DMPlexComputeJacobianAction_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
2467a925c78cSMatthew G. Knepley {
2468a925c78cSMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
2469a925c78cSMatthew G. Knepley   const char       *name  = "Jacobian";
2470a925c78cSMatthew G. Knepley   DM                dmAux, plex;
2471c330f8ffSToby Isaac   Vec               A;
2472a925c78cSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
2473a925c78cSMatthew G. Knepley   PetscQuadrature   quad;
2474a925c78cSMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
2475a925c78cSMatthew G. Knepley   PetscScalar      *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
2476c330f8ffSToby Isaac   PetscInt          Nf, fieldI, fieldJ, numCells, c;
24774d0b9603SSander Arens   PetscInt          totDim, totDimAux = 0;
24784a3e9fdbSToby Isaac   PetscBool         isStride;
24794a3e9fdbSToby Isaac   PetscInt          cStart = -1, cEnd = -1;
248075b37a90SMatthew G. Knepley   PetscBool         hasDyn;
2481c330f8ffSToby Isaac   DMField           coordField;
2482a925c78cSMatthew G. Knepley   PetscErrorCode    ierr;
2483a925c78cSMatthew G. Knepley 
2484a925c78cSMatthew G. Knepley   PetscFunctionBegin;
2485a925c78cSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2486a925c78cSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2487a925c78cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2488a925c78cSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2489a925c78cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2490a925c78cSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2491a925c78cSMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2492a925c78cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
24934a3e9fdbSToby Isaac   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
2494a925c78cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2495a925c78cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2496a925c78cSMatthew G. Knepley   if (dmAux) {
2497a925c78cSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2498a925c78cSMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
2499a925c78cSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2500a925c78cSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2501a925c78cSMatthew G. Knepley   }
2502a925c78cSMatthew G. Knepley   ierr = VecSet(Z, 0.0);CHKERRQ(ierr);
2503a925c78cSMatthew G. Knepley   ierr = PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);CHKERRQ(ierr);
2504a925c78cSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
25054a3e9fdbSToby Isaac   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
25064a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) cellIS, ISSTRIDE, &isStride);CHKERRQ(ierr);
25074a3e9fdbSToby Isaac   if (isStride) {
25084a3e9fdbSToby Isaac     PetscInt step;
25094a3e9fdbSToby Isaac 
25104a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(cellIS, &cStart, &step);CHKERRQ(ierr);
25114a3e9fdbSToby Isaac     cEnd = cStart + numCells;
25124a3e9fdbSToby Isaac     if (step != 1) {
25134a3e9fdbSToby Isaac       isStride = PETSC_FALSE;
25144a3e9fdbSToby Isaac     }
25154a3e9fdbSToby Isaac   }
25164a3e9fdbSToby Isaac   if (!isStride) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not implemented yet");
2517a925c78cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2518a925c78cSMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
2519a925c78cSMatthew G. Knepley     PetscInt     i;
2520a925c78cSMatthew G. Knepley 
2521a925c78cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
2522a925c78cSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
2523a925c78cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
2524a925c78cSMatthew G. Knepley     if (X_t) {
2525a925c78cSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
2526a925c78cSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
2527a925c78cSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
2528a925c78cSMatthew G. Knepley     }
2529a925c78cSMatthew G. Knepley     if (dmAux) {
2530a925c78cSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
2531a925c78cSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
2532a925c78cSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
2533a925c78cSMatthew G. Knepley     }
2534a925c78cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr);
2535a925c78cSMatthew G. Knepley     for (i = 0; i < totDim; ++i) y[(c-cStart)*totDim+i] = x[i];
2536a925c78cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr);
2537a925c78cSMatthew G. Knepley   }
2538a925c78cSMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2539a925c78cSMatthew G. Knepley   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2540a925c78cSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2541a925c78cSMatthew G. Knepley     PetscFE  fe;
2542c330f8ffSToby Isaac     PetscInt Nb;
2543a925c78cSMatthew G. Knepley     /* Conforming batches */
2544a925c78cSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2545a925c78cSMatthew G. Knepley     /* Remainder */
2546c330f8ffSToby Isaac     PetscInt Nr, offset, Nq;
2547c330f8ffSToby Isaac     PetscQuadrature qGeom = NULL;
2548c330f8ffSToby Isaac     PetscBool   isAffine;
2549c330f8ffSToby Isaac     PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
2550a925c78cSMatthew G. Knepley 
2551a925c78cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2552a925c78cSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
2553a925c78cSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2554a925c78cSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2555c330f8ffSToby Isaac     ierr = DMFieldGetFEInvariance(coordField,cellIS,NULL,&isAffine,NULL);CHKERRQ(ierr);
2556c330f8ffSToby Isaac     if (isAffine) {
2557c330f8ffSToby Isaac       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
2558c330f8ffSToby Isaac     }
2559c330f8ffSToby Isaac     if (!qGeom) {
2560c330f8ffSToby Isaac       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
2561c330f8ffSToby Isaac       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
2562c330f8ffSToby Isaac     }
2563c330f8ffSToby Isaac     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
25644a3e9fdbSToby Isaac     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2565c330f8ffSToby Isaac     blockSize = Nb;
2566a925c78cSMatthew G. Knepley     batchSize = numBlocks * blockSize;
2567a925c78cSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2568a925c78cSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
2569a925c78cSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
2570a925c78cSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
2571a925c78cSMatthew G. Knepley     offset    = numCells - Nr;
2572c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
2573c330f8ffSToby Isaac     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2574a925c78cSMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2575f99c8401SToby Isaac       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
2576f99c8401SToby Isaac       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2577a925c78cSMatthew G. Knepley       if (hasDyn) {
2578f99c8401SToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
2579f99c8401SToby Isaac         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
2580a925c78cSMatthew G. Knepley       }
2581a925c78cSMatthew G. Knepley     }
2582c330f8ffSToby Isaac     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
2583c330f8ffSToby Isaac     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
25844a3e9fdbSToby Isaac     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2585c330f8ffSToby Isaac     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
2586a925c78cSMatthew G. Knepley   }
2587a925c78cSMatthew G. Knepley   if (hasDyn) {
2588a925c78cSMatthew G. Knepley     for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2589a925c78cSMatthew G. Knepley   }
2590a925c78cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2591a925c78cSMatthew G. Knepley     const PetscBLASInt M = totDim, one = 1;
2592a925c78cSMatthew G. Knepley     const PetscScalar  a = 1.0, b = 0.0;
2593a925c78cSMatthew G. Knepley 
2594a925c78cSMatthew G. Knepley     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[(c-cStart)*totDim*totDim], &M, &y[(c-cStart)*totDim], &one, &b, z, &one));
2595a925c78cSMatthew G. Knepley     if (mesh->printFEM > 1) {
2596a925c78cSMatthew G. Knepley       ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);
2597a925c78cSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Y",  totDim, &y[(c-cStart)*totDim]);CHKERRQ(ierr);
2598a925c78cSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Z",  totDim, z);CHKERRQ(ierr);
2599a925c78cSMatthew G. Knepley     }
2600a925c78cSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, Z, c, z, ADD_VALUES);CHKERRQ(ierr);
2601a925c78cSMatthew G. Knepley   }
2602a925c78cSMatthew G. Knepley   ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr);
2603a925c78cSMatthew G. Knepley   if (dmAux) {
2604a925c78cSMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
2605a925c78cSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2606a925c78cSMatthew G. Knepley   }
2607a925c78cSMatthew G. Knepley   if (mesh->printFEM) {
2608a925c78cSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr);
2609a925c78cSMatthew G. Knepley     ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
2610a925c78cSMatthew G. Knepley   }
2611a925c78cSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2612a925c78cSMatthew G. Knepley   PetscFunctionReturn(0);
2613a925c78cSMatthew G. Knepley }
2614a925c78cSMatthew G. Knepley 
261524cdb843SMatthew G. Knepley /*@
261624cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
261724cdb843SMatthew G. Knepley 
261824cdb843SMatthew G. Knepley   Input Parameters:
261924cdb843SMatthew G. Knepley + dm - The mesh
262024cdb843SMatthew G. Knepley . X  - Local input vector
262124cdb843SMatthew G. Knepley - user - The user context
262224cdb843SMatthew G. Knepley 
262324cdb843SMatthew G. Knepley   Output Parameter:
262424cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
262524cdb843SMatthew G. Knepley 
262624cdb843SMatthew G. Knepley   Note:
262724cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
262824cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
262924cdb843SMatthew G. Knepley 
263024cdb843SMatthew G. Knepley   Level: developer
263124cdb843SMatthew G. Knepley 
263224cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
263324cdb843SMatthew G. Knepley @*/
263424cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
263524cdb843SMatthew G. Knepley {
26366da023fcSToby Isaac   DM             plex;
26374a3e9fdbSToby Isaac   IS             cellIS;
26384a3e9fdbSToby Isaac   PetscInt       depth;
263924cdb843SMatthew G. Knepley   PetscErrorCode ierr;
264024cdb843SMatthew G. Knepley 
264124cdb843SMatthew G. Knepley   PetscFunctionBegin;
26426da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
26434a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2644aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
26454a3e9fdbSToby Isaac   if (!cellIS) {
26464a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
26474a3e9fdbSToby Isaac   }
26484a3e9fdbSToby Isaac   ierr = DMPlexComputeJacobian_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
26494a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
26509a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
265124cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
265224cdb843SMatthew G. Knepley }
26531878804aSMatthew G. Knepley 
2654a925c78cSMatthew G. Knepley /*@
2655a925c78cSMatthew G. Knepley   DMPlexSNESComputeJacobianActionFEM - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user.
2656a925c78cSMatthew G. Knepley 
2657a925c78cSMatthew G. Knepley   Input Parameters:
2658a925c78cSMatthew G. Knepley + dm - The mesh
2659a925c78cSMatthew G. Knepley . X  - Local solution vector
2660a925c78cSMatthew G. Knepley . Y  - Local input vector
2661a925c78cSMatthew G. Knepley - user - The user context
2662a925c78cSMatthew G. Knepley 
2663a925c78cSMatthew G. Knepley   Output Parameter:
2664a925c78cSMatthew G. Knepley . Z - Local output vector
2665a925c78cSMatthew G. Knepley 
2666a925c78cSMatthew G. Knepley   Note:
2667a925c78cSMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2668a925c78cSMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
2669a925c78cSMatthew G. Knepley 
2670a925c78cSMatthew G. Knepley   Level: developer
2671a925c78cSMatthew G. Knepley 
2672a925c78cSMatthew G. Knepley .seealso: FormFunctionLocal()
2673a925c78cSMatthew G. Knepley @*/
2674a925c78cSMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianActionFEM(DM dm, Vec X, Vec Y, Vec Z, void *user)
2675a925c78cSMatthew G. Knepley {
2676a925c78cSMatthew G. Knepley   DM             plex;
26774a3e9fdbSToby Isaac   IS             cellIS;
26784a3e9fdbSToby Isaac   PetscInt       depth;
2679a925c78cSMatthew G. Knepley   PetscErrorCode ierr;
2680a925c78cSMatthew G. Knepley 
2681a925c78cSMatthew G. Knepley   PetscFunctionBegin;
2682a925c78cSMatthew G. Knepley   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
26834a3e9fdbSToby Isaac   ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
2684aeadca18SToby Isaac   ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
26854a3e9fdbSToby Isaac   if (!cellIS) {
26864a3e9fdbSToby Isaac     ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);
26874a3e9fdbSToby Isaac   }
26884a3e9fdbSToby Isaac   ierr = DMPlexComputeJacobianAction_Internal(plex, cellIS, 0.0, 0.0, X, NULL, Y, Z, user);CHKERRQ(ierr);
26894a3e9fdbSToby Isaac   ierr = ISDestroy(&cellIS);
2690a925c78cSMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2691a925c78cSMatthew G. Knepley   PetscFunctionReturn(0);
2692a925c78cSMatthew G. Knepley }
2693a925c78cSMatthew G. Knepley 
26949f520fc2SToby Isaac /*@
26959f520fc2SToby Isaac   DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian.
26969f520fc2SToby Isaac 
26979f520fc2SToby Isaac   Input Parameters:
26989f520fc2SToby Isaac + dm - The DM object
2699dff059c6SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary())
27009f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual())
27019f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian())
27021a244344SSatish Balay 
27031a244344SSatish Balay   Level: developer
27049f520fc2SToby Isaac @*/
27059f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx)
27069f520fc2SToby Isaac {
27079f520fc2SToby Isaac   PetscErrorCode ierr;
27089f520fc2SToby Isaac 
27099f520fc2SToby Isaac   PetscFunctionBegin;
27109f520fc2SToby Isaac   ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr);
27119f520fc2SToby Isaac   ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr);
27129f520fc2SToby Isaac   ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr);
27139f520fc2SToby Isaac   PetscFunctionReturn(0);
27149f520fc2SToby Isaac }
27159f520fc2SToby Isaac 
27160163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions_Internal(SNES snes, DM dm, Vec u, Vec sol, PetscErrorCode (**exactFuncs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx), void **ctxs)
27171878804aSMatthew G. Knepley {
2718282e7bb4SMatthew G. Knepley   PetscDS        prob;
27191878804aSMatthew G. Knepley   Mat            J, M;
27201878804aSMatthew G. Knepley   Vec            r, b;
27211878804aSMatthew G. Knepley   MatNullSpace   nullSpace;
27221878804aSMatthew G. Knepley   PetscReal     *error, res = 0.0;
27231878804aSMatthew G. Knepley   PetscInt       numFields;
2724282e7bb4SMatthew G. Knepley   PetscBool      hasJac, hasPrec;
27251878804aSMatthew G. Knepley   PetscErrorCode ierr;
27261878804aSMatthew G. Knepley 
27271878804aSMatthew G. Knepley   PetscFunctionBegin;
27281878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &r);CHKERRQ(ierr);
27291878804aSMatthew G. Knepley   ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr);
27301878804aSMatthew G. Knepley   /* TODO Null space for J */
27311878804aSMatthew G. Knepley   /* Check discretization error */
27321878804aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
27331878804aSMatthew G. Knepley   ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr);
27341878804aSMatthew G. Knepley   if (numFields > 1) {
27351878804aSMatthew G. Knepley     PetscInt f;
27361878804aSMatthew G. Knepley 
27371189c1efSToby Isaac     ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr);
27381878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr);
27391878804aSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
27401878804aSMatthew G. Knepley       if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);}
2741e5b268a4SToby Isaac       if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", (double)error[f]);CHKERRQ(ierr);}
27421878804aSMatthew G. Knepley       else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);}
27431878804aSMatthew G. Knepley     }
27441878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr);
27451878804aSMatthew G. Knepley   } else {
27460709b2feSToby Isaac     ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr);
2747e5b268a4SToby Isaac     if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", (double)error[0]);CHKERRQ(ierr);}
27481878804aSMatthew G. Knepley     else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);}
27491878804aSMatthew G. Knepley   }
27501878804aSMatthew G. Knepley   ierr = PetscFree(error);CHKERRQ(ierr);
27511878804aSMatthew G. Knepley   /* Check residual */
27521878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr);
27531878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
2754e5b268a4SToby Isaac   ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", (double)res);CHKERRQ(ierr);
27551878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
27561878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr);
2757685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr);
2758685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
27591878804aSMatthew G. Knepley   /* Check Jacobian */
2760282e7bb4SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2761282e7bb4SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
2762282e7bb4SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2763282e7bb4SMatthew G. Knepley   if (hasJac && hasPrec) {
2764282e7bb4SMatthew G. Knepley     ierr = DMCreateMatrix(dm, &M);CHKERRQ(ierr);
2765282e7bb4SMatthew G. Knepley     ierr = SNESComputeJacobian(snes, u, J, M);CHKERRQ(ierr);
2766282e7bb4SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) M, "jacpre_");CHKERRQ(ierr);
2767282e7bb4SMatthew G. Knepley     ierr = MatViewFromOptions(M, NULL, "-mat_view");CHKERRQ(ierr);
2768282e7bb4SMatthew G. Knepley     ierr = MatDestroy(&M);CHKERRQ(ierr);
2769282e7bb4SMatthew G. Knepley   } else {
2770282e7bb4SMatthew G. Knepley     ierr = SNESComputeJacobian(snes, u, J, J);CHKERRQ(ierr);
2771282e7bb4SMatthew G. Knepley   }
2772282e7bb4SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) J, "jac_");CHKERRQ(ierr);
2773282e7bb4SMatthew G. Knepley   ierr = MatViewFromOptions(J, NULL, "-mat_view");CHKERRQ(ierr);
27741878804aSMatthew G. Knepley   ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr);
27751878804aSMatthew G. Knepley   if (nullSpace) {
27761878804aSMatthew G. Knepley     PetscBool isNull;
27771878804aSMatthew G. Knepley     ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr);
27781878804aSMatthew G. Knepley     if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
27791878804aSMatthew G. Knepley   }
27801878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &b);CHKERRQ(ierr);
27811878804aSMatthew G. Knepley   ierr = VecSet(r, 0.0);CHKERRQ(ierr);
27821878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr);
2783282e7bb4SMatthew G. Knepley   ierr = MatMult(J, u, r);CHKERRQ(ierr);
27841878804aSMatthew G. Knepley   ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr);
27851878804aSMatthew G. Knepley   ierr = VecDestroy(&b);CHKERRQ(ierr);
27861878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
2787e5b268a4SToby Isaac   ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", (double)res);CHKERRQ(ierr);
27881878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
27891878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr);
2790685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr);
2791685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
27921878804aSMatthew G. Knepley   ierr = VecDestroy(&r);CHKERRQ(ierr);
27931878804aSMatthew G. Knepley   ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
27941878804aSMatthew G. Knepley   ierr = MatDestroy(&J);CHKERRQ(ierr);
27951878804aSMatthew G. Knepley   PetscFunctionReturn(0);
27961878804aSMatthew G. Knepley }
27971878804aSMatthew G. Knepley 
27980163fd50SMatthew G. Knepley PetscErrorCode DMSNESCheckFromOptions(SNES snes, Vec u, PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx), void **ctxs)
27991878804aSMatthew G. Knepley {
28001878804aSMatthew G. Knepley   DM             dm;
28011878804aSMatthew G. Knepley   Vec            sol;
28021878804aSMatthew G. Knepley   PetscBool      check;
28031878804aSMatthew G. Knepley   PetscErrorCode ierr;
28041878804aSMatthew G. Knepley 
28051878804aSMatthew G. Knepley   PetscFunctionBegin;
2806c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr);
28071878804aSMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
28081878804aSMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
28091878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
28101878804aSMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
28111878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
28121878804aSMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2813552f7358SJed Brown   PetscFunctionReturn(0);
2814552f7358SJed Brown }
2815