xref: /petsc/src/snes/utils/dmplexsnes.c (revision 290cf56f1633a67d73a9e544e72b9d71921afdc5)
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)
158785e854fSJed Brown   ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr);
159cb313848SJed Brown   for (i=0; i<N; 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) {
1770adebc6cSBarry Smith     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0);
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];
1923a93e3b7SToby Isaac       ctx->cells[q++] = foundCells[p].index;
193552f7358SJed Brown     }
194552f7358SJed Brown   }
195552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
196552f7358SJed Brown #if 0
197552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
198552f7358SJed Brown #else
199552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
2003a93e3b7SToby Isaac   ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr);
201552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
202552f7358SJed Brown #endif
203cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
204d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
205552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
206552f7358SJed Brown   PetscFunctionReturn(0);
207552f7358SJed Brown }
208552f7358SJed Brown 
2090adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
2100adebc6cSBarry Smith {
211552f7358SJed Brown   PetscFunctionBegin;
212552f7358SJed Brown   PetscValidPointer(coordinates, 2);
2130adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
214552f7358SJed Brown   *coordinates = ctx->coords;
215552f7358SJed Brown   PetscFunctionReturn(0);
216552f7358SJed Brown }
217552f7358SJed Brown 
2180adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2190adebc6cSBarry Smith {
220552f7358SJed Brown   PetscErrorCode ierr;
221552f7358SJed Brown 
222552f7358SJed Brown   PetscFunctionBegin;
223552f7358SJed Brown   PetscValidPointer(v, 2);
2240adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
225552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
226552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
227552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
228c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
229552f7358SJed Brown   PetscFunctionReturn(0);
230552f7358SJed Brown }
231552f7358SJed Brown 
2320adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2330adebc6cSBarry Smith {
234552f7358SJed Brown   PetscErrorCode ierr;
235552f7358SJed Brown 
236552f7358SJed Brown   PetscFunctionBegin;
237552f7358SJed Brown   PetscValidPointer(v, 2);
2380adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
239552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
240552f7358SJed Brown   PetscFunctionReturn(0);
241552f7358SJed Brown }
242552f7358SJed Brown 
2437a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
244a6dfd86eSKarl Rupp {
245552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
24656044e6dSMatthew G. Knepley   const PetscScalar *coords;
24756044e6dSMatthew G. Knepley   PetscScalar    *a;
248552f7358SJed Brown   PetscInt       p;
249552f7358SJed Brown   PetscErrorCode ierr;
250552f7358SJed Brown 
251552f7358SJed Brown   PetscFunctionBegin;
252dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
25356044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
254552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
255552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
256552f7358SJed Brown     PetscInt     c = ctx->cells[p];
257a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
258552f7358SJed Brown     PetscReal    xi[4];
259552f7358SJed Brown     PetscInt     d, f, comp;
260552f7358SJed Brown 
2618e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
262552f7358SJed Brown     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2630298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2641aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2651aa26658SKarl Rupp 
266552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
267552f7358SJed Brown       xi[d] = 0.0;
2681aa26658SKarl 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]);
2691aa26658SKarl 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];
270552f7358SJed Brown     }
2710298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
272552f7358SJed Brown   }
273552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
27456044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
275552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
276552f7358SJed Brown   PetscFunctionReturn(0);
277552f7358SJed Brown }
278552f7358SJed Brown 
2797a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
2807a1931ceSMatthew G. Knepley {
2817a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
28256044e6dSMatthew G. Knepley   const PetscScalar *coords;
28356044e6dSMatthew G. Knepley   PetscScalar    *a;
2847a1931ceSMatthew G. Knepley   PetscInt       p;
2857a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
2867a1931ceSMatthew G. Knepley 
2877a1931ceSMatthew G. Knepley   PetscFunctionBegin;
288dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
28956044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
2907a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
2917a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
2927a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
2937a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
2942584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
2957a1931ceSMatthew G. Knepley     PetscReal      xi[4];
2967a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
2977a1931ceSMatthew G. Knepley 
2988e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2997a1931ceSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
3007a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3017a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
3027a1931ceSMatthew G. Knepley 
3037a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
3047a1931ceSMatthew G. Knepley       xi[d] = 0.0;
3057a1931ceSMatthew 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]);
3067a1931ceSMatthew 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];
3077a1931ceSMatthew G. Knepley     }
3087a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
3097a1931ceSMatthew G. Knepley   }
3107a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
31156044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
3127a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
3137a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3147a1931ceSMatthew G. Knepley }
3157a1931ceSMatthew G. Knepley 
3165820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
317552f7358SJed Brown {
318552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
319552f7358SJed Brown   const PetscScalar x0        = vertices[0];
320552f7358SJed Brown   const PetscScalar y0        = vertices[1];
321552f7358SJed Brown   const PetscScalar x1        = vertices[2];
322552f7358SJed Brown   const PetscScalar y1        = vertices[3];
323552f7358SJed Brown   const PetscScalar x2        = vertices[4];
324552f7358SJed Brown   const PetscScalar y2        = vertices[5];
325552f7358SJed Brown   const PetscScalar x3        = vertices[6];
326552f7358SJed Brown   const PetscScalar y3        = vertices[7];
327552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
328552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
329552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
330552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
331552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
332552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
33356044e6dSMatthew G. Knepley   const PetscScalar *ref;
33456044e6dSMatthew G. Knepley   PetscScalar       *real;
335552f7358SJed Brown   PetscErrorCode    ierr;
336552f7358SJed Brown 
337552f7358SJed Brown   PetscFunctionBegin;
33856044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
339552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
340552f7358SJed Brown   {
341552f7358SJed Brown     const PetscScalar p0 = ref[0];
342552f7358SJed Brown     const PetscScalar p1 = ref[1];
343552f7358SJed Brown 
344552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
345552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
346552f7358SJed Brown   }
347552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
34856044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
349552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
350552f7358SJed Brown   PetscFunctionReturn(0);
351552f7358SJed Brown }
352552f7358SJed Brown 
353af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
354d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
355552f7358SJed Brown {
356552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
357552f7358SJed Brown   const PetscScalar x0        = vertices[0];
358552f7358SJed Brown   const PetscScalar y0        = vertices[1];
359552f7358SJed Brown   const PetscScalar x1        = vertices[2];
360552f7358SJed Brown   const PetscScalar y1        = vertices[3];
361552f7358SJed Brown   const PetscScalar x2        = vertices[4];
362552f7358SJed Brown   const PetscScalar y2        = vertices[5];
363552f7358SJed Brown   const PetscScalar x3        = vertices[6];
364552f7358SJed Brown   const PetscScalar y3        = vertices[7];
365552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
366552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
36756044e6dSMatthew G. Knepley   const PetscScalar *ref;
368552f7358SJed Brown   PetscErrorCode    ierr;
369552f7358SJed Brown 
370552f7358SJed Brown   PetscFunctionBegin;
37156044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
372552f7358SJed Brown   {
373552f7358SJed Brown     const PetscScalar x       = ref[0];
374552f7358SJed Brown     const PetscScalar y       = ref[1];
375552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
376da80777bSKarl Rupp     PetscScalar       values[4];
377da80777bSKarl Rupp 
378da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
379da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
38094ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
381552f7358SJed Brown   }
382552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
38356044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
38494ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
38594ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
386552f7358SJed Brown   PetscFunctionReturn(0);
387552f7358SJed Brown }
388552f7358SJed Brown 
389a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
390a6dfd86eSKarl Rupp {
391fafc0619SMatthew G Knepley   DM             dmCoord;
392de73a395SMatthew G. Knepley   PetscFE        fem = NULL;
393552f7358SJed Brown   SNES           snes;
394552f7358SJed Brown   KSP            ksp;
395552f7358SJed Brown   PC             pc;
396552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
397552f7358SJed Brown   Mat            J;
39856044e6dSMatthew G. Knepley   const PetscScalar *coords;
39956044e6dSMatthew G. Knepley   PetscScalar    *a;
400de73a395SMatthew G. Knepley   PetscInt       Nf, p;
4015509d985SMatthew G. Knepley   const PetscInt dof = ctx->dof;
402552f7358SJed Brown   PetscErrorCode ierr;
403552f7358SJed Brown 
404552f7358SJed Brown   PetscFunctionBegin;
405de73a395SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
406de73a395SMatthew G. Knepley   if (Nf) {ierr = DMGetField(dm, 0, (PetscObject *) &fem);CHKERRQ(ierr);}
407552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
408fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
409552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
410552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
411552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
412552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
413c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
414552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
415552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
416552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
417552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
418552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
419552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4200298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4210298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
422552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
423552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
424552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
425552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
426552f7358SJed Brown 
42756044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
428552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
429552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
430a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
431552f7358SJed Brown     PetscScalar *xi;
432cb313848SJed Brown     PetscReal    xir[2];
433552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
434552f7358SJed Brown 
435552f7358SJed Brown     /* Can make this do all points at once */
4360298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4370adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4380298fd71SBarry Smith     ierr   = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4390298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4400298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
441552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
442552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
443552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
444552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
445552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
446552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
447cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
448cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4495509d985SMatthew G. Knepley     if (4*dof != xSize) {
4505509d985SMatthew G. Knepley       PetscReal *B;
4515509d985SMatthew G. Knepley       PetscInt   d;
4521aa26658SKarl Rupp 
4535509d985SMatthew G. Knepley       xir[0] = 2.0*xir[0] - 1.0; xir[1] = 2.0*xir[1] - 1.0;
4545509d985SMatthew G. Knepley       ierr = PetscFEGetTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
4555509d985SMatthew G. Knepley       for (comp = 0; comp < dof; ++comp) {
4565509d985SMatthew G. Knepley         a[p*dof+comp] = 0.0;
4575509d985SMatthew G. Knepley         for (d = 0; d < xSize/dof; ++d) {
4585509d985SMatthew G. Knepley           a[p*dof+comp] += x[d*dof+comp]*B[d*dof+comp];
4595509d985SMatthew G. Knepley         }
4605509d985SMatthew G. Knepley       }
4615509d985SMatthew G. Knepley       ierr = PetscFERestoreTabulation(fem, 1, xir, &B, NULL, NULL);CHKERRQ(ierr);
4625509d985SMatthew G. Knepley     } else {
4635509d985SMatthew G. Knepley       for (comp = 0; comp < dof; ++comp)
4645509d985SMatthew 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];
4655509d985SMatthew G. Knepley     }
466552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4670298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4680298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
469552f7358SJed Brown   }
470552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
47156044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
472552f7358SJed Brown 
473552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
474552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
475552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
476552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
477552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
478552f7358SJed Brown   PetscFunctionReturn(0);
479552f7358SJed Brown }
480552f7358SJed Brown 
4815820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
482552f7358SJed Brown {
483552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
484552f7358SJed Brown   const PetscScalar x0        = vertices[0];
485552f7358SJed Brown   const PetscScalar y0        = vertices[1];
486552f7358SJed Brown   const PetscScalar z0        = vertices[2];
4877a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
4887a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
4897a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
490552f7358SJed Brown   const PetscScalar x2        = vertices[6];
491552f7358SJed Brown   const PetscScalar y2        = vertices[7];
492552f7358SJed Brown   const PetscScalar z2        = vertices[8];
4937a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
4947a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
4957a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
496552f7358SJed Brown   const PetscScalar x4        = vertices[12];
497552f7358SJed Brown   const PetscScalar y4        = vertices[13];
498552f7358SJed Brown   const PetscScalar z4        = vertices[14];
499552f7358SJed Brown   const PetscScalar x5        = vertices[15];
500552f7358SJed Brown   const PetscScalar y5        = vertices[16];
501552f7358SJed Brown   const PetscScalar z5        = vertices[17];
502552f7358SJed Brown   const PetscScalar x6        = vertices[18];
503552f7358SJed Brown   const PetscScalar y6        = vertices[19];
504552f7358SJed Brown   const PetscScalar z6        = vertices[20];
505552f7358SJed Brown   const PetscScalar x7        = vertices[21];
506552f7358SJed Brown   const PetscScalar y7        = vertices[22];
507552f7358SJed Brown   const PetscScalar z7        = vertices[23];
508552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
509552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
510552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
511552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
512552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
513552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
514552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
515552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
516552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
517552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
518552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
519552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
520552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
521552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
522552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
523552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
524552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
525552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
526552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
527552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
528552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
52956044e6dSMatthew G. Knepley   const PetscScalar *ref;
53056044e6dSMatthew G. Knepley   PetscScalar       *real;
531552f7358SJed Brown   PetscErrorCode    ierr;
532552f7358SJed Brown 
533552f7358SJed Brown   PetscFunctionBegin;
53456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
535552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
536552f7358SJed Brown   {
537552f7358SJed Brown     const PetscScalar p0 = ref[0];
538552f7358SJed Brown     const PetscScalar p1 = ref[1];
539552f7358SJed Brown     const PetscScalar p2 = ref[2];
540552f7358SJed Brown 
541552f7358SJed 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;
542552f7358SJed 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;
543552f7358SJed 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;
544552f7358SJed Brown   }
545552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
54656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
547552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
548552f7358SJed Brown   PetscFunctionReturn(0);
549552f7358SJed Brown }
550552f7358SJed Brown 
551d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
552552f7358SJed Brown {
553552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
554552f7358SJed Brown   const PetscScalar x0        = vertices[0];
555552f7358SJed Brown   const PetscScalar y0        = vertices[1];
556552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5577a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5587a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5597a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
560552f7358SJed Brown   const PetscScalar x2        = vertices[6];
561552f7358SJed Brown   const PetscScalar y2        = vertices[7];
562552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5637a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5647a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5657a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
566552f7358SJed Brown   const PetscScalar x4        = vertices[12];
567552f7358SJed Brown   const PetscScalar y4        = vertices[13];
568552f7358SJed Brown   const PetscScalar z4        = vertices[14];
569552f7358SJed Brown   const PetscScalar x5        = vertices[15];
570552f7358SJed Brown   const PetscScalar y5        = vertices[16];
571552f7358SJed Brown   const PetscScalar z5        = vertices[17];
572552f7358SJed Brown   const PetscScalar x6        = vertices[18];
573552f7358SJed Brown   const PetscScalar y6        = vertices[19];
574552f7358SJed Brown   const PetscScalar z6        = vertices[20];
575552f7358SJed Brown   const PetscScalar x7        = vertices[21];
576552f7358SJed Brown   const PetscScalar y7        = vertices[22];
577552f7358SJed Brown   const PetscScalar z7        = vertices[23];
578552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
579552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
580552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
581552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
582552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
583552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
584552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
585552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
586552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
587552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
588552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
589552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
59056044e6dSMatthew G. Knepley   const PetscScalar *ref;
591552f7358SJed Brown   PetscErrorCode    ierr;
592552f7358SJed Brown 
593552f7358SJed Brown   PetscFunctionBegin;
59456044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(Xref,  &ref);CHKERRQ(ierr);
595552f7358SJed Brown   {
596552f7358SJed Brown     const PetscScalar x       = ref[0];
597552f7358SJed Brown     const PetscScalar y       = ref[1];
598552f7358SJed Brown     const PetscScalar z       = ref[2];
599552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
600da80777bSKarl Rupp     PetscScalar       values[9];
601da80777bSKarl Rupp 
602da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
603da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
604da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
605da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
606da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
607da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
608da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
609da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
610da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
6111aa26658SKarl Rupp 
61294ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
613552f7358SJed Brown   }
614552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
61556044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(Xref,  &ref);CHKERRQ(ierr);
61694ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
61794ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
618552f7358SJed Brown   PetscFunctionReturn(0);
619552f7358SJed Brown }
620552f7358SJed Brown 
621a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
622a6dfd86eSKarl Rupp {
623fafc0619SMatthew G Knepley   DM             dmCoord;
624552f7358SJed Brown   SNES           snes;
625552f7358SJed Brown   KSP            ksp;
626552f7358SJed Brown   PC             pc;
627552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
628552f7358SJed Brown   Mat            J;
62956044e6dSMatthew G. Knepley   const PetscScalar *coords;
63056044e6dSMatthew G. Knepley   PetscScalar    *a;
631552f7358SJed Brown   PetscInt       p;
632552f7358SJed Brown   PetscErrorCode ierr;
633552f7358SJed Brown 
634552f7358SJed Brown   PetscFunctionBegin;
635552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
636fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
637552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
638552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
639552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
640552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
641c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
642552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
643552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
644552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
645552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
646552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
647552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6480298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6490298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
650552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
651552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
652552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
653552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
654552f7358SJed Brown 
65556044e6dSMatthew G. Knepley   ierr = VecGetArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
656552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
657552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
658a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
659552f7358SJed Brown     PetscScalar *xi;
660cb313848SJed Brown     PetscReal    xir[3];
661552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
662552f7358SJed Brown 
663552f7358SJed Brown     /* Can make this do all points at once */
6640298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6650adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6660298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6670adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6680298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6690298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
670552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
671552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
672552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
673552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
674552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
675552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
676552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
677cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
678cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
679cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
680552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
681552f7358SJed Brown       a[p*ctx->dof+comp] =
682cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
6837a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
684cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
6857a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
686cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
687cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
688cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
689cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
690552f7358SJed Brown     }
691552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
6920298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6930298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
694552f7358SJed Brown   }
695552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
69656044e6dSMatthew G. Knepley   ierr = VecRestoreArrayRead(ctx->coords, &coords);CHKERRQ(ierr);
697552f7358SJed Brown 
698552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
699552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
700552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
701552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
702552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
703552f7358SJed Brown   PetscFunctionReturn(0);
704552f7358SJed Brown }
705552f7358SJed Brown 
706552f7358SJed Brown /*
707552f7358SJed Brown   Input Parameters:
708552f7358SJed Brown + ctx - The DMInterpolationInfo context
709552f7358SJed Brown . dm  - The DM
710552f7358SJed Brown - x   - The local vector containing the field to be interpolated
711552f7358SJed Brown 
712552f7358SJed Brown   Output Parameters:
713552f7358SJed Brown . v   - The vector containing the interpolated values
714552f7358SJed Brown */
7150adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
7160adebc6cSBarry Smith {
717552f7358SJed Brown   PetscInt       dim, coneSize, n;
718552f7358SJed Brown   PetscErrorCode ierr;
719552f7358SJed Brown 
720552f7358SJed Brown   PetscFunctionBegin;
721552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
722552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
723552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
724552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7250adebc6cSBarry 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);
726552f7358SJed Brown   if (n) {
727c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
728552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
729552f7358SJed Brown     if (dim == 2) {
730552f7358SJed Brown       if (coneSize == 3) {
7317a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
732552f7358SJed Brown       } else if (coneSize == 4) {
733552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7340adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
735552f7358SJed Brown     } else if (dim == 3) {
736552f7358SJed Brown       if (coneSize == 4) {
7377a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
738552f7358SJed Brown       } else {
739552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
740552f7358SJed Brown       }
7410adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
742552f7358SJed Brown   }
743552f7358SJed Brown   PetscFunctionReturn(0);
744552f7358SJed Brown }
745552f7358SJed Brown 
7460adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7470adebc6cSBarry Smith {
748552f7358SJed Brown   PetscErrorCode ierr;
749552f7358SJed Brown 
750552f7358SJed Brown   PetscFunctionBegin;
751552f7358SJed Brown   PetscValidPointer(ctx, 2);
752552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
753552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
754552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
755552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7560298fd71SBarry Smith   *ctx = NULL;
757552f7358SJed Brown   PetscFunctionReturn(0);
758552f7358SJed Brown }
759cc0c4584SMatthew G. Knepley 
760cc0c4584SMatthew G. Knepley /*@C
761cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
762cc0c4584SMatthew G. Knepley 
763cc0c4584SMatthew G. Knepley   Collective on SNES
764cc0c4584SMatthew G. Knepley 
765cc0c4584SMatthew G. Knepley   Input Parameters:
766cc0c4584SMatthew G. Knepley + snes   - the SNES context
767cc0c4584SMatthew G. Knepley . its    - iteration number
768cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
769d43b4f6eSBarry Smith - vf  - PetscViewerAndFormat of type ASCII
770cc0c4584SMatthew G. Knepley 
771cc0c4584SMatthew G. Knepley   Notes:
772cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
773cc0c4584SMatthew G. Knepley 
774cc0c4584SMatthew G. Knepley   Level: intermediate
775cc0c4584SMatthew G. Knepley 
776cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
777cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
778cc0c4584SMatthew G. Knepley @*/
779d43b4f6eSBarry Smith PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, PetscViewerAndFormat *vf)
780cc0c4584SMatthew G. Knepley {
781d43b4f6eSBarry Smith   PetscViewer        viewer = vf->viewer;
782cc0c4584SMatthew G. Knepley   Vec                res;
783cc0c4584SMatthew G. Knepley   DM                 dm;
784cc0c4584SMatthew G. Knepley   PetscSection       s;
785cc0c4584SMatthew G. Knepley   const PetscScalar *r;
786cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
787cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
788cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
789cc0c4584SMatthew G. Knepley 
790cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
7914d4332d5SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
792cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
793cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
794cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
795cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
796cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
797cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
798cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
799cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
800cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
801cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
802cc0c4584SMatthew G. Knepley 
803cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
804cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
805cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
806cc0c4584SMatthew G. Knepley     }
807cc0c4584SMatthew G. Knepley   }
808cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
809b2566f29SBarry Smith   ierr = MPIU_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
810d43b4f6eSBarry Smith   ierr = PetscViewerPushFormat(viewer,vf->format);CHKERRQ(ierr);
811cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
812cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
813cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
814cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
815cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
816cc0c4584SMatthew G. Knepley   }
817cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
818cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
819d43b4f6eSBarry Smith   ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
820cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
821cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
822cc0c4584SMatthew G. Knepley }
82324cdb843SMatthew G. Knepley 
82424cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
82524cdb843SMatthew G. Knepley 
8267d4028c8SMatthew G. Knepley /*@
8277d4028c8SMatthew G. Knepley   DMPlexSNESGetGeometryFEM - Return precomputed geometric data
8287d4028c8SMatthew G. Knepley 
8297d4028c8SMatthew G. Knepley   Input Parameter:
8307d4028c8SMatthew G. Knepley . dm - The DM
8317d4028c8SMatthew G. Knepley 
8327d4028c8SMatthew G. Knepley   Output Parameters:
8337d4028c8SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
8347d4028c8SMatthew G. Knepley 
8357d4028c8SMatthew G. Knepley   Level: developer
8367d4028c8SMatthew G. Knepley 
8377d4028c8SMatthew G. Knepley .seealso: DMPlexSNESSetFunctionLocal()
8387d4028c8SMatthew G. Knepley @*/
8397d4028c8SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFEM(DM dm, Vec *cellgeom)
8407d4028c8SMatthew G. Knepley {
8417d4028c8SMatthew G. Knepley   DMSNES         dmsnes;
8427d4028c8SMatthew G. Knepley   PetscObject    obj;
8437d4028c8SMatthew G. Knepley   PetscErrorCode ierr;
8447d4028c8SMatthew G. Knepley 
8457d4028c8SMatthew G. Knepley   PetscFunctionBegin;
8467d4028c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8477d4028c8SMatthew G. Knepley   ierr = DMGetDMSNES(dm, &dmsnes);CHKERRQ(ierr);
8487d4028c8SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", &obj);CHKERRQ(ierr);
8497d4028c8SMatthew G. Knepley   if (!obj) {
8507d4028c8SMatthew G. Knepley     Vec cellgeom;
8517d4028c8SMatthew G. Knepley 
8527d4028c8SMatthew G. Knepley     ierr = DMPlexComputeGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
8537d4028c8SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject) cellgeom);CHKERRQ(ierr);
8547d4028c8SMatthew G. Knepley     ierr = VecDestroy(&cellgeom);CHKERRQ(ierr);
8557d4028c8SMatthew G. Knepley   }
8567d4028c8SMatthew G. Knepley   if (cellgeom) {PetscValidPointer(cellgeom, 3); ierr = PetscObjectQuery((PetscObject) dmsnes, "DMPlexSNES_cellgeom_fem", (PetscObject *) cellgeom);CHKERRQ(ierr);}
8577d4028c8SMatthew G. Knepley   PetscFunctionReturn(0);
8587d4028c8SMatthew G. Knepley }
8597d4028c8SMatthew G. Knepley 
86008449791SMatthew G. Knepley /*@
86108449791SMatthew G. Knepley   DMPlexSNESGetGeometryFVM - Return precomputed geometric data
86208449791SMatthew G. Knepley 
86308449791SMatthew G. Knepley   Input Parameter:
86408449791SMatthew G. Knepley . dm - The DM
86508449791SMatthew G. Knepley 
86608449791SMatthew G. Knepley   Output Parameters:
86708449791SMatthew G. Knepley + facegeom - The values precomputed from face geometry
86808449791SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
86908449791SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
87008449791SMatthew G. Knepley 
87108449791SMatthew G. Knepley   Level: developer
87208449791SMatthew G. Knepley 
87308449791SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
87408449791SMatthew G. Knepley @*/
87508449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
87624cdb843SMatthew G. Knepley {
8774b32e5bbSToby Isaac   DM             plex;
87824cdb843SMatthew G. Knepley   PetscErrorCode ierr;
87924cdb843SMatthew G. Knepley 
88024cdb843SMatthew G. Knepley   PetscFunctionBegin;
88108449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8824b32e5bbSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
8834b32e5bbSToby Isaac   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
8844b32e5bbSToby Isaac   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
8854b32e5bbSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
88624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
88724cdb843SMatthew G. Knepley }
88824cdb843SMatthew G. Knepley 
889dbd489d2SMatthew G. Knepley /*@
89008449791SMatthew G. Knepley   DMPlexSNESGetGradientDM - Return gradient data layout
89108449791SMatthew G. Knepley 
89208449791SMatthew G. Knepley   Input Parameters:
89308449791SMatthew G. Knepley + dm - The DM
89408449791SMatthew G. Knepley - fv - The PetscFV
89508449791SMatthew G. Knepley 
89608449791SMatthew G. Knepley   Output Parameter:
89708449791SMatthew G. Knepley . dmGrad - The layout for gradient values
89808449791SMatthew G. Knepley 
89908449791SMatthew G. Knepley   Level: developer
90008449791SMatthew G. Knepley 
90108449791SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
90208449791SMatthew G. Knepley @*/
90308449791SMatthew G. Knepley PetscErrorCode DMPlexSNESGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
90424cdb843SMatthew G. Knepley {
9054b32e5bbSToby Isaac   DM             plex;
90608449791SMatthew G. Knepley   PetscBool      computeGradients;
90724cdb843SMatthew G. Knepley   PetscErrorCode ierr;
90824cdb843SMatthew G. Knepley 
90924cdb843SMatthew G. Knepley   PetscFunctionBegin;
91008449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
91108449791SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
91208449791SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
91308449791SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
91408449791SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
9154b32e5bbSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
9164b32e5bbSToby Isaac   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
9174b32e5bbSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
91808449791SMatthew G. Knepley   PetscFunctionReturn(0);
91908449791SMatthew G. Knepley }
92008449791SMatthew G. Knepley 
92108449791SMatthew G. Knepley /*@C
92208449791SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
92308449791SMatthew G. Knepley 
92408449791SMatthew G. Knepley   Input Parameters:
92508449791SMatthew G. Knepley + dm     - The DM
92608449791SMatthew G. Knepley . cStart - The first cell to include
92708449791SMatthew G. Knepley . cEnd   - The first cell to exclude
92808449791SMatthew G. Knepley . locX   - A local vector with the solution fields
92908449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
93008449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
93108449791SMatthew G. Knepley 
93208449791SMatthew G. Knepley   Output Parameters:
93308449791SMatthew G. Knepley + u   - The field coefficients
93408449791SMatthew G. Knepley . u_t - The fields derivative coefficients
93508449791SMatthew G. Knepley - a   - The auxiliary field coefficients
93608449791SMatthew G. Knepley 
93708449791SMatthew G. Knepley   Level: developer
93808449791SMatthew G. Knepley 
93908449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
94008449791SMatthew G. Knepley @*/
94108449791SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
94208449791SMatthew G. Knepley {
94308449791SMatthew G. Knepley   DM             dmAux;
94408449791SMatthew G. Knepley   PetscSection   section, sectionAux;
94508449791SMatthew G. Knepley   PetscDS        prob;
94608449791SMatthew G. Knepley   PetscInt       numCells = cEnd - cStart, totDim, totDimAux, c;
94708449791SMatthew G. Knepley   PetscErrorCode ierr;
94808449791SMatthew G. Knepley 
94908449791SMatthew G. Knepley   PetscFunctionBegin;
95008449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
95108449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
95208449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
95308449791SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
95408449791SMatthew G. Knepley   PetscValidPointer(u, 7);
95508449791SMatthew G. Knepley   PetscValidPointer(u_t, 8);
95608449791SMatthew G. Knepley   PetscValidPointer(a, 9);
95724cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
95824cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
95924cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
96008449791SMatthew G. Knepley   if (locA) {
96108449791SMatthew G. Knepley     PetscDS probAux;
96208449791SMatthew G. Knepley 
96308449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
96424cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
96524cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
96624cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
96724cdb843SMatthew G. Knepley   }
96808449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u);CHKERRQ(ierr);
96949073227SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
97049073227SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, PETSC_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
97124cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
97208449791SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
97324cdb843SMatthew G. Knepley     PetscInt     i;
97424cdb843SMatthew G. Knepley 
97508449791SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
9769f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[(c-cStart)*totDim+i] = x[i];
97708449791SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
97808449791SMatthew G. Knepley     if (locX_t) {
97908449791SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
9809f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[(c-cStart)*totDim+i] = x_t[i];
98108449791SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, locX_t, c, NULL, &x_t);CHKERRQ(ierr);
98224cdb843SMatthew G. Knepley     }
98308449791SMatthew G. Knepley     if (locA) {
9846da023fcSToby Isaac       DM dmAuxPlex;
9856da023fcSToby Isaac 
9866da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux, &dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
9876da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
9889f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[(c-cStart)*totDimAux+i] = x[i];
9896da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
9906da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
99124cdb843SMatthew G. Knepley     }
99224cdb843SMatthew G. Knepley   }
99308449791SMatthew G. Knepley   PetscFunctionReturn(0);
99408449791SMatthew G. Knepley }
99524cdb843SMatthew G. Knepley 
99608449791SMatthew G. Knepley /*@C
99708449791SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
99808449791SMatthew G. Knepley 
99908449791SMatthew G. Knepley   Input Parameters:
100008449791SMatthew G. Knepley + dm     - The DM
100108449791SMatthew G. Knepley . cStart - The first cell to include
100208449791SMatthew G. Knepley . cEnd   - The first cell to exclude
100308449791SMatthew G. Knepley . locX   - A local vector with the solution fields
100408449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
100508449791SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
100608449791SMatthew G. Knepley 
100708449791SMatthew G. Knepley   Output Parameters:
100808449791SMatthew G. Knepley + u   - The field coefficients
100908449791SMatthew G. Knepley . u_t - The fields derivative coefficients
101008449791SMatthew G. Knepley - a   - The auxiliary field coefficients
101108449791SMatthew G. Knepley 
101208449791SMatthew G. Knepley   Level: developer
101308449791SMatthew G. Knepley 
101408449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
101508449791SMatthew G. Knepley @*/
101608449791SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, PetscInt cStart, PetscInt cEnd, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
101708449791SMatthew G. Knepley {
101808449791SMatthew G. Knepley   PetscErrorCode ierr;
101908449791SMatthew G. Knepley 
102008449791SMatthew G. Knepley   PetscFunctionBegin;
102108449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u);CHKERRQ(ierr);
102249073227SMatthew G. Knepley   if (*u_t) {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, u_t);CHKERRQ(ierr);}
102349073227SMatthew G. Knepley   if (*a)   {ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, a);CHKERRQ(ierr);}
102408449791SMatthew G. Knepley   PetscFunctionReturn(0);
102524cdb843SMatthew G. Knepley }
102608449791SMatthew G. Knepley 
102708449791SMatthew G. Knepley /*@C
102808449791SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
102908449791SMatthew G. Knepley 
103008449791SMatthew G. Knepley   Input Parameters:
103108449791SMatthew G. Knepley + dm     - The DM
103208449791SMatthew G. Knepley . fStart - The first face to include
103308449791SMatthew G. Knepley . fEnd   - The first face to exclude
103408449791SMatthew G. Knepley . locX   - A local vector with the solution fields
103508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
103608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
103708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
103808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
103908449791SMatthew G. Knepley 
104008449791SMatthew G. Knepley   Output Parameters:
10415f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
10425f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face
1043477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
104408449791SMatthew G. Knepley 
104508449791SMatthew G. Knepley   Level: developer
104608449791SMatthew G. Knepley 
104708449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
104808449791SMatthew G. Knepley @*/
10495f942ad5SMatthew 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)
105008449791SMatthew G. Knepley {
105108449791SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
1052195142f5SMatthew G. Knepley   PetscSection       section;
105308449791SMatthew G. Knepley   PetscDS            prob;
105408449791SMatthew G. Knepley   DMLabel            ghostLabel;
105508449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
1056477f7dfdSMatthew G. Knepley   PetscBool         *isFE;
1057477f7dfdSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
105808449791SMatthew G. Knepley   PetscErrorCode     ierr;
105908449791SMatthew G. Knepley 
106008449791SMatthew G. Knepley   PetscFunctionBegin;
106108449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
106208449791SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
106308449791SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
106408449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
106508449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
106608449791SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
106708449791SMatthew G. Knepley   PetscValidPointer(uL, 9);
106808449791SMatthew G. Knepley   PetscValidPointer(uR, 10);
106908449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
107008449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1071195142f5SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1072477f7dfdSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
1073477f7dfdSMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
1074477f7dfdSMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
1075477f7dfdSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1076477f7dfdSMatthew G. Knepley     PetscObject  obj;
1077477f7dfdSMatthew G. Knepley     PetscClassId id;
1078477f7dfdSMatthew G. Knepley 
10794a270516SSander Arens     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1080477f7dfdSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1081477f7dfdSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
1082477f7dfdSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
1083477f7dfdSMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
1084477f7dfdSMatthew G. Knepley   }
1085c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
108608449791SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
108708449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
108808449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
108908449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
109008449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
109108449791SMatthew G. Knepley   if (locGrad) {
109208449791SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
109308449791SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
109424cdb843SMatthew G. Knepley   }
1095477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uL);CHKERRQ(ierr);
1096477f7dfdSMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, PETSC_SCALAR, uR);CHKERRQ(ierr);
1097477f7dfdSMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
109808449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
109908449791SMatthew G. Knepley     const PetscInt        *cells;
1100640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1101640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
1102640bce14SSatish Balay     PetscScalar           *xL, *xR, *gL, *gR;
110308449791SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
11043e64cd2fSToby Isaac     PetscInt               ghost, nsupp, nchild;
110508449791SMatthew G. Knepley 
110608449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1107e697831aSToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
11083e64cd2fSToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
11097c45b140SToby Isaac     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
111008449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
111108449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
111208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
111308449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
1114477f7dfdSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1115477f7dfdSMatthew G. Knepley       PetscInt off;
1116477f7dfdSMatthew G. Knepley 
111737a43ebbSMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
1118477f7dfdSMatthew G. Knepley       if (isFE[f]) {
1119477f7dfdSMatthew G. Knepley         const PetscInt *cone;
11203e64cd2fSToby Isaac         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
1121477f7dfdSMatthew G. Knepley 
1122cca9989dSMatthew G. Knepley         xL = xR = NULL;
11237c45b140SToby Isaac         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
1124cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1125cca9989dSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1126477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
11273e64cd2fSToby Isaac         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
11283e64cd2fSToby Isaac         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
1129477f7dfdSMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
11303e64cd2fSToby Isaac         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
11313e64cd2fSToby Isaac         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
11323e64cd2fSToby 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]);
1133195142f5SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
11343e64cd2fSToby Isaac         /* TODO: this is a hack that might not be right for nonconforming */
11353e64cd2fSToby Isaac         if (faceLocL < coneSizeL) {
1136477f7dfdSMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
11373e64cd2fSToby Isaac           if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
11387c45b140SToby Isaac           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
11393e64cd2fSToby Isaac         }
11403e64cd2fSToby Isaac         else {
11413e64cd2fSToby Isaac           ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
11423e64cd2fSToby Isaac           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
11433e64cd2fSToby Isaac           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
11443e64cd2fSToby Isaac         }
1145cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
1146cca9989dSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
1147477f7dfdSMatthew G. Knepley       } else {
1148477f7dfdSMatthew G. Knepley         PetscFV  fv;
1149477f7dfdSMatthew G. Knepley         PetscInt numComp, c;
1150477f7dfdSMatthew G. Knepley 
1151477f7dfdSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
1152477f7dfdSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
1153cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
1154cca9989dSMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
115508449791SMatthew G. Knepley         if (dmGrad) {
115608449791SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
115708449791SMatthew G. Knepley 
115808449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
115908449791SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
116008449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
116108449791SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
1162477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1163477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
1164477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
116508449791SMatthew G. Knepley           }
116608449791SMatthew G. Knepley         } else {
1167477f7dfdSMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
1168477f7dfdSMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
1169477f7dfdSMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
1170477f7dfdSMatthew G. Knepley           }
1171477f7dfdSMatthew G. Knepley         }
117208449791SMatthew G. Knepley       }
117308449791SMatthew G. Knepley     }
117408449791SMatthew G. Knepley     ++iface;
117508449791SMatthew G. Knepley   }
11765f942ad5SMatthew G. Knepley   *Nface = iface;
117708449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
117808449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
117908449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
118008449791SMatthew G. Knepley   if (locGrad) {
118108449791SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
118208449791SMatthew G. Knepley   }
1183477f7dfdSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
118408449791SMatthew G. Knepley   PetscFunctionReturn(0);
118508449791SMatthew G. Knepley }
118608449791SMatthew G. Knepley 
118708449791SMatthew G. Knepley /*@C
118808449791SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
118908449791SMatthew G. Knepley 
119008449791SMatthew G. Knepley   Input Parameters:
119108449791SMatthew G. Knepley + dm     - The DM
119208449791SMatthew G. Knepley . fStart - The first face to include
119308449791SMatthew G. Knepley . fEnd   - The first face to exclude
119408449791SMatthew G. Knepley . locX   - A local vector with the solution fields
119508449791SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
119608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
119708449791SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
119808449791SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
119908449791SMatthew G. Knepley 
120008449791SMatthew G. Knepley   Output Parameters:
12015f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
12025f942ad5SMatthew G. Knepley . uL - The field values at the left side of the face
1203477f7dfdSMatthew G. Knepley - uR - The field values at the right side of the face
120408449791SMatthew G. Knepley 
120508449791SMatthew G. Knepley   Level: developer
120608449791SMatthew G. Knepley 
120708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
120808449791SMatthew G. Knepley @*/
12095f942ad5SMatthew 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)
121008449791SMatthew G. Knepley {
121108449791SMatthew G. Knepley   PetscErrorCode ierr;
121208449791SMatthew G. Knepley 
121308449791SMatthew G. Knepley   PetscFunctionBegin;
121408449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uL);CHKERRQ(ierr);
121508449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_SCALAR, uR);CHKERRQ(ierr);
121608449791SMatthew G. Knepley   PetscFunctionReturn(0);
121708449791SMatthew G. Knepley }
121808449791SMatthew G. Knepley 
121908449791SMatthew G. Knepley /*@C
122008449791SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
122108449791SMatthew G. Knepley 
122208449791SMatthew G. Knepley   Input Parameters:
122308449791SMatthew G. Knepley + dm     - The DM
122408449791SMatthew G. Knepley . fStart - The first face to include
122508449791SMatthew G. Knepley . fEnd   - The first face to exclude
122608449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
122708449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
122808449791SMatthew G. Knepley 
122908449791SMatthew G. Knepley   Output Parameters:
12305f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
12315f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal
123208449791SMatthew G. Knepley - vol   - The cell volume
123308449791SMatthew G. Knepley 
123408449791SMatthew G. Knepley   Level: developer
123508449791SMatthew G. Knepley 
123608449791SMatthew G. Knepley .seealso: DMPlexGetCellFields()
123708449791SMatthew G. Knepley @*/
12385f942ad5SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
123908449791SMatthew G. Knepley {
124008449791SMatthew G. Knepley   DM                 dmFace, dmCell;
124108449791SMatthew G. Knepley   DMLabel            ghostLabel;
124208449791SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
124308449791SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
124408449791SMatthew G. Knepley   PetscErrorCode     ierr;
124508449791SMatthew G. Knepley 
124608449791SMatthew G. Knepley   PetscFunctionBegin;
124708449791SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
124808449791SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
124908449791SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
125008449791SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
125108449791SMatthew G. Knepley   PetscValidPointer(vol, 7);
125208449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1253c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
125408449791SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
125508449791SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
125608449791SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
125708449791SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
125808449791SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
125908449791SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, PETSC_SCALAR, vol);CHKERRQ(ierr);
126008449791SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
126108449791SMatthew G. Knepley     const PetscInt        *cells;
1262640bce14SSatish Balay     PetscFVFaceGeom       *fg;
1263640bce14SSatish Balay     PetscFVCellGeom       *cgL, *cgR;
126408449791SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
12652eefff9cSMatthew G. Knepley     PetscReal             *voll   = *vol;
12667c45b140SToby Isaac     PetscInt               ghost, d, nchild, nsupp;
126708449791SMatthew G. Knepley 
126808449791SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
12697c45b140SToby Isaac     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
12707c45b140SToby Isaac     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
12717c45b140SToby Isaac     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
127208449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
127308449791SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
127408449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
127508449791SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
127608449791SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
127708449791SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
127808449791SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
127908449791SMatthew G. Knepley     }
128008449791SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
128108449791SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
128208449791SMatthew G. Knepley     ++iface;
128308449791SMatthew G. Knepley   }
12845f942ad5SMatthew G. Knepley   *Nface = iface;
128508449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
128608449791SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
128708449791SMatthew G. Knepley   PetscFunctionReturn(0);
128808449791SMatthew G. Knepley }
128908449791SMatthew G. Knepley 
129008449791SMatthew G. Knepley /*@C
129108449791SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
129208449791SMatthew G. Knepley 
129308449791SMatthew G. Knepley   Input Parameters:
129408449791SMatthew G. Knepley + dm     - The DM
129508449791SMatthew G. Knepley . fStart - The first face to include
129608449791SMatthew G. Knepley . fEnd   - The first face to exclude
129708449791SMatthew G. Knepley . faceGeometry - A local vector with face geometry
129808449791SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
129908449791SMatthew G. Knepley 
130008449791SMatthew G. Knepley   Output Parameters:
13015f942ad5SMatthew G. Knepley + Nface - The number of faces with field values
13025f942ad5SMatthew G. Knepley . fgeom - The extract the face centroid and normal
130308449791SMatthew G. Knepley - vol   - The cell volume
130408449791SMatthew G. Knepley 
130508449791SMatthew G. Knepley   Level: developer
130608449791SMatthew G. Knepley 
130708449791SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
130808449791SMatthew G. Knepley @*/
13095f942ad5SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
131008449791SMatthew G. Knepley {
131108449791SMatthew G. Knepley   PetscErrorCode ierr;
131208449791SMatthew G. Knepley 
131308449791SMatthew G. Knepley   PetscFunctionBegin;
131408449791SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
131508449791SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_REAL, vol);CHKERRQ(ierr);
131608449791SMatthew G. Knepley   PetscFunctionReturn(0);
131708449791SMatthew G. Knepley }
131808449791SMatthew G. Knepley 
131911dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
132008449791SMatthew G. Knepley {
132108449791SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
13222d91c981SSander Arens   DM               dmAux = NULL, plex = NULL;
13232d91c981SSander Arens   PetscSection     section, sectionAux = NULL;
13242d91c981SSander Arens   PetscDS          prob, probAux = NULL;
132508449791SMatthew G. Knepley   DMLabel          depth;
13262d91c981SSander Arens   Vec              locA = NULL;
13274d0b9603SSander Arens   PetscFEFaceGeom *fgeom;
13282d91c981SSander Arens   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
13294d0b9603SSander Arens   PetscInt         dim, totDim, totDimAux, numBd, bd;
133008449791SMatthew G. Knepley   PetscErrorCode   ierr;
133108449791SMatthew G. Knepley 
133208449791SMatthew G. Knepley   PetscFunctionBegin;
133308449791SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
133408449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
133524cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
133624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
13374d0b9603SSander Arens   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1338fa04768aSSander Arens   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
13392d91c981SSander Arens   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
13402d91c981SSander Arens   if (locA) {
13412d91c981SSander Arens     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
13422d91c981SSander Arens     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
13432d91c981SSander Arens     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
13442d91c981SSander Arens     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
13454d0b9603SSander Arens     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
13462d91c981SSander Arens   }
134724cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
13484fd14557SMatthew G. Knepley     DMBoundaryConditionType type;
134924cdb843SMatthew G. Knepley     const char             *bdLabel;
135024cdb843SMatthew G. Knepley     DMLabel                 label;
135124cdb843SMatthew G. Knepley     IS                      pointIS;
135224cdb843SMatthew G. Knepley     const PetscInt         *points;
135324cdb843SMatthew G. Knepley     const PetscInt         *values;
13544d0b9603SSander Arens     PetscInt                field, numValues, v, numPoints, p, dep, numFaces, face;
135580bc4632SMatthew G. Knepley     PetscObject             obj;
135680bc4632SMatthew G. Knepley     PetscClassId            id;
135724cdb843SMatthew G. Knepley 
135844ac9936SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
13594d0b9603SSander Arens     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
136080bc4632SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
13614fd14557SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
1362c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1363a8e83e26SSanderA     for (v = 0; v < numValues; ++v) {
1364a8e83e26SSanderA       ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
1365a8e83e26SSanderA       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
136622734eb1Ssarens       if (!pointIS) continue; /* No points with that id on this process */
136724cdb843SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
136824cdb843SMatthew G. Knepley       for (p = 0, numFaces = 0; p < numPoints; ++p) {
136924cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
137024cdb843SMatthew G. Knepley         if (dep == dim-1) ++numFaces;
137124cdb843SMatthew G. Knepley       }
13724d0b9603SSander Arens       ierr = PetscMalloc4(numFaces*totDim,&u,locX_t ? numFaces*totDim : 0, &u_t, numFaces,&fgeom, numFaces*totDim,&elemVec);CHKERRQ(ierr);
13734d0b9603SSander Arens       if (locA) {ierr = PetscMalloc1(numFaces*totDimAux,&a);CHKERRQ(ierr);}
13744d0b9603SSander Arens       for (p = 0, face = 0; p < numPoints; ++p) {
13754d0b9603SSander Arens         const PetscInt point = points[p], *support, *cone;
137624cdb843SMatthew G. Knepley         PetscScalar   *x     = NULL;
13774d0b9603SSander Arens         PetscReal      dummyJ[9], dummyDetJ;
13789bfb2fe4SJed Brown         PetscInt       i, coneSize, faceLoc;
137924cdb843SMatthew G. Knepley 
138024cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
138124cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
13824d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
13834d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFEM(dm, support[0], NULL, NULL, dummyJ, fgeom[face].invJ[0], &dummyDetJ);CHKERRQ(ierr);
13844d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, fgeom[face].v0, fgeom[face].J, NULL, &fgeom[face].detJ);CHKERRQ(ierr);
13854d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, fgeom[face].n);CHKERRQ(ierr);
13864d0b9603SSander Arens         if (fgeom[face].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", fgeom[face].detJ, point);
13874d0b9603SSander Arens         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
13884d0b9603SSander Arens         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
13894d0b9603SSander Arens         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
13904d0b9603SSander 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]);
13914d0b9603SSander Arens         fgeom[face].face[0] = faceLoc;
13924d0b9603SSander Arens         ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
13934d0b9603SSander Arens         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
13944d0b9603SSander Arens         ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
139508449791SMatthew G. Knepley         if (locX_t) {
13964d0b9603SSander Arens           ierr = DMPlexVecGetClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
13974d0b9603SSander Arens           for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
13984d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
139924cdb843SMatthew G. Knepley         }
14002d91c981SSander Arens         if (locA) {
14014d0b9603SSander Arens           ierr = DMPlexVecGetClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
14024d0b9603SSander Arens           for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
14034d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
140424cdb843SMatthew G. Knepley         }
14054d0b9603SSander Arens         ++face;
140624cdb843SMatthew G. Knepley       }
14074d0b9603SSander Arens       ierr = PetscMemzero(elemVec, numFaces* totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1408cbf52bb1SSander Arens       {
140924cdb843SMatthew G. Knepley         PetscFE         fe;
141008449791SMatthew G. Knepley         PetscQuadrature q;
141124cdb843SMatthew G. Knepley         PetscInt        numQuadPoints, Nb;
141224cdb843SMatthew G. Knepley         /* Conforming batches */
141324cdb843SMatthew G. Knepley         PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
141424cdb843SMatthew G. Knepley         /* Remainder */
141524cdb843SMatthew G. Knepley         PetscInt        Nr, offset;
141624cdb843SMatthew G. Knepley 
14174d0b9603SSander Arens         ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
14184d0b9603SSander Arens         ierr = PetscFEGetFaceQuadrature(fe, &q);CHKERRQ(ierr);
141924cdb843SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
142024cdb843SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
142124cdb843SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
142224cdb843SMatthew G. Knepley         blockSize = Nb*numQuadPoints;
142324cdb843SMatthew G. Knepley         batchSize = numBlocks * blockSize;
142424cdb843SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
142524cdb843SMatthew G. Knepley         numChunks = numFaces / (numBatches*batchSize);
142624cdb843SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
142724cdb843SMatthew G. Knepley         Nr        = numFaces % (numBatches*batchSize);
142824cdb843SMatthew G. Knepley         offset    = numFaces - Nr;
14294d0b9603SSander Arens         ierr = PetscFEIntegrateBdResidual(fe, prob, field, Ne, fgeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
14304d0b9603SSander Arens         ierr = PetscFEIntegrateBdResidual(fe, prob, field, Nr, &fgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
143124cdb843SMatthew G. Knepley       }
14324d0b9603SSander Arens       for (p = 0, face = 0; p < numPoints; ++p) {
14334d0b9603SSander Arens         const PetscInt point = points[p], *support;
143424cdb843SMatthew G. Knepley 
143524cdb843SMatthew G. Knepley         ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
143624cdb843SMatthew G. Knepley         if (dep != dim-1) continue;
14374d0b9603SSander Arens         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
14384d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
14394d0b9603SSander Arens         ierr = DMPlexVecSetClosure(dm, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
14404d0b9603SSander Arens         ++face;
144124cdb843SMatthew G. Knepley       }
144224cdb843SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
144324cdb843SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
14444d0b9603SSander Arens       ierr = PetscFree4(u,u_t,fgeom,elemVec);CHKERRQ(ierr);
14451c46aac1SSander Arens       if (locA) {ierr = PetscFree(a);CHKERRQ(ierr);}
144624cdb843SMatthew G. Knepley     }
1447a8e83e26SSanderA   }
14481c46aac1SSander Arens   if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
144908449791SMatthew G. Knepley   PetscFunctionReturn(0);
145008449791SMatthew G. Knepley }
145108449791SMatthew G. Knepley 
145211dd639bSMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
145308449791SMatthew G. Knepley {
145408449791SMatthew G. Knepley   DM_Plex          *mesh       = (DM_Plex *) dm->data;
145508449791SMatthew G. Knepley   const char       *name       = "Residual";
145608449791SMatthew G. Knepley   DM                dmAux      = NULL;
145708449791SMatthew G. Knepley   DM                dmGrad     = NULL;
145808449791SMatthew G. Knepley   DMLabel           ghostLabel = NULL;
145908449791SMatthew G. Knepley   PetscDS           prob       = NULL;
146008449791SMatthew G. Knepley   PetscDS           probAux    = NULL;
146108449791SMatthew G. Knepley   PetscSection      section    = NULL;
146208449791SMatthew G. Knepley   PetscBool         useFEM     = PETSC_FALSE;
146308449791SMatthew G. Knepley   PetscBool         useFVM     = PETSC_FALSE;
1464b2666ceaSMatthew G. Knepley   PetscBool         isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
146508449791SMatthew G. Knepley   PetscFV           fvm        = NULL;
146608449791SMatthew G. Knepley   PetscFECellGeom  *cgeomFEM   = NULL;
14672f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
146808449791SMatthew G. Knepley   PetscFVCellGeom  *cgeomFVM   = NULL;
146908449791SMatthew G. Knepley   PetscFVFaceGeom  *fgeomFVM   = NULL;
147008449791SMatthew G. Knepley   Vec               locA, cellGeometryFEM = NULL, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
14713755293bSMatthew G. Knepley   PetscScalar      *u = NULL, *u_t, *a, *uL, *uR;
1472c4d4a4f8SMatthew G. Knepley   PetscInt          Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
147308449791SMatthew G. Knepley   PetscErrorCode    ierr;
147408449791SMatthew G. Knepley 
147508449791SMatthew G. Knepley   PetscFunctionBegin;
147608449791SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
147708449791SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
1478195142f5SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
147908449791SMatthew G. Knepley   /* FEM+FVM */
148008449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
148108449791SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1482c58f1c22SToby Isaac   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
148308449791SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
148408449791SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
148508449791SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
148608449791SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
148708449791SMatthew G. Knepley   if (locA) {
148808449791SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
148908449791SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
149008449791SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
149108449791SMatthew G. Knepley   }
149208449791SMatthew G. Knepley   /* 2: Get geometric data */
149308449791SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
149408449791SMatthew G. Knepley     PetscObject  obj;
149508449791SMatthew G. Knepley     PetscClassId id;
14967173168dSMatthew G. Knepley     PetscBool    fimp;
149708449791SMatthew G. Knepley 
14987173168dSMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
14997173168dSMatthew G. Knepley     if (isImplicit != fimp) continue;
150008449791SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
150108449791SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
150208449791SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
150308449791SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
150408449791SMatthew G. Knepley   }
150508449791SMatthew G. Knepley   if (useFEM) {
150608449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFEM(dm, &cellGeometryFEM);CHKERRQ(ierr);
15072f84e9bcSToby Isaac     ierr = VecGetArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);
15082f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
15092f84e9bcSToby Isaac       DM dmCell;
15102f84e9bcSToby Isaac       PetscInt c;
15112f84e9bcSToby Isaac 
15122f84e9bcSToby Isaac       ierr = VecGetDM(cellGeometryFEM,&dmCell);CHKERRQ(ierr);
15132f84e9bcSToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&cgeomFEM);CHKERRQ(ierr);
15142f84e9bcSToby Isaac       for (c = 0; c < cEnd - cStart; c++) {
15152f84e9bcSToby Isaac         PetscScalar *thisgeom;
15162f84e9bcSToby Isaac 
15172f84e9bcSToby Isaac         ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
15182f84e9bcSToby Isaac         cgeomFEM[c] = *((PetscFECellGeom *) thisgeom);
15192f84e9bcSToby Isaac       }
15202f84e9bcSToby Isaac     }
15212f84e9bcSToby Isaac     else {
15222f84e9bcSToby Isaac       cgeomFEM = (PetscFECellGeom *) cgeomScal;
15232f84e9bcSToby Isaac     }
152408449791SMatthew G. Knepley   }
152508449791SMatthew G. Knepley   if (useFVM) {
152608449791SMatthew G. Knepley     ierr = DMPlexSNESGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
152708449791SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
152808449791SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
152908449791SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
153008449791SMatthew G. Knepley     ierr = DMPlexSNESGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
153108449791SMatthew G. Knepley     if (dmGrad) {
153208449791SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
153308449791SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1534de555695SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
153508449791SMatthew G. Knepley       /* Communicate gradient values */
153608449791SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
153708449791SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
153808449791SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
153908449791SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
154008449791SMatthew G. Knepley     }
1541bdd6f66aSToby Isaac     /* Handle non-essential (e.g. outflow) boundary values */
1542bdd6f66aSToby Isaac     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
154308449791SMatthew G. Knepley   }
154408449791SMatthew G. Knepley   /* Loop over chunks */
154508449791SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
154608449791SMatthew G. Knepley   numChunks     = 1;
154708449791SMatthew G. Knepley   cellChunkSize = (cEnd - cStart)/numChunks;
154808449791SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
154908449791SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
15502eefff9cSMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
15512eefff9cSMatthew G. Knepley     PetscReal       *vol;
155208449791SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
155308449791SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, cell;
15543755293bSMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
155508449791SMatthew G. Knepley 
155608449791SMatthew G. Knepley     /* Extract field coefficients */
155708449791SMatthew G. Knepley     if (useFEM) {
155808449791SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
155908449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
1560215c4595SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
156108449791SMatthew G. Knepley     }
156208449791SMatthew G. Knepley     if (useFVM) {
15635f942ad5SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
15645f942ad5SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
156508449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
156608449791SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
1567215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxL, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1568215c4595SMatthew G. Knepley       ierr = PetscMemzero(fluxR, numFaces*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
156908449791SMatthew G. Knepley     }
157008449791SMatthew 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 */
157108449791SMatthew G. Knepley     /* Loop over fields */
157208449791SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
157308449791SMatthew G. Knepley       PetscObject  obj;
157408449791SMatthew G. Knepley       PetscClassId id;
15757173168dSMatthew G. Knepley       PetscBool    fimp;
157608449791SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
157708449791SMatthew G. Knepley 
15787173168dSMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
15797173168dSMatthew G. Knepley       if (isImplicit != fimp) continue;
158008449791SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
158108449791SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
158208449791SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
158308449791SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
158408449791SMatthew G. Knepley         PetscQuadrature q;
158508449791SMatthew G. Knepley         PetscInt        Nq, Nb;
158608449791SMatthew G. Knepley 
158708449791SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
158808449791SMatthew G. Knepley 
158908449791SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
159008449791SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
159108449791SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
159208449791SMatthew G. Knepley         blockSize = Nb*Nq;
159308449791SMatthew G. Knepley         batchSize = numBlocks * blockSize;
159408449791SMatthew G. Knepley         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
159508449791SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
159608449791SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
159708449791SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
159808449791SMatthew G. Knepley         offset    = numCells - Nr;
159908449791SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
160008449791SMatthew 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) */
160111dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeomFEM, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
160211dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeomFEM[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
160308449791SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
160408449791SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
160508449791SMatthew G. Knepley 
160608449791SMatthew G. Knepley         Ne = numFaces;
160708449791SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
160808449791SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
160908449791SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
161008449791SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
161108449791SMatthew G. Knepley     }
16122f84e9bcSToby Isaac     if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
16132f84e9bcSToby Isaac       ierr = PetscFree(cgeomFEM);CHKERRQ(ierr);
16142f84e9bcSToby Isaac     }
16152f84e9bcSToby Isaac     else {
16162f84e9bcSToby Isaac       cgeomFEM = NULL;
16172f84e9bcSToby Isaac     }
16180163fd50SMatthew G. Knepley     if (cellGeometryFEM) {ierr = VecRestoreArray(cellGeometryFEM, &cgeomScal);CHKERRQ(ierr);}
161908449791SMatthew G. Knepley     /* Loop over domain */
162008449791SMatthew G. Knepley     if (useFEM) {
162108449791SMatthew G. Knepley       /* Add elemVec to locX */
162208449791SMatthew G. Knepley       for (cell = cS; cell < cE; ++cell) {
162308449791SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cell*totDim]);CHKERRQ(ierr);}
1624b4920ed3SToby Isaac         if (ghostLabel) {
1625b4920ed3SToby Isaac           PetscInt ghostVal;
1626b4920ed3SToby Isaac 
1627b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
1628b4920ed3SToby Isaac           if (ghostVal > 0) continue;
1629b4920ed3SToby Isaac         }
1630c14a31d2SToby Isaac         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cell*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
163108449791SMatthew G. Knepley       }
163208449791SMatthew G. Knepley     }
163308449791SMatthew G. Knepley     if (useFVM) {
16344a394323SMatthew G. Knepley       PetscScalar *fa;
163508449791SMatthew G. Knepley       PetscInt     iface;
163608449791SMatthew G. Knepley 
163708449791SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1638c10b5f1bSMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1639c10b5f1bSMatthew G. Knepley         PetscFV      fv;
1640c10b5f1bSMatthew G. Knepley         PetscObject  obj;
1641c10b5f1bSMatthew G. Knepley         PetscClassId id;
16424a394323SMatthew G. Knepley         PetscInt     foff, pdim;
1643c10b5f1bSMatthew G. Knepley 
1644c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1645c10b5f1bSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1646c10b5f1bSMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1647c10b5f1bSMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1648c10b5f1bSMatthew G. Knepley         fv   = (PetscFV) obj;
1649c10b5f1bSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1650c10b5f1bSMatthew G. Knepley         /* Accumulate fluxes to cells */
165108449791SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
165208449791SMatthew G. Knepley           const PetscInt *cells;
1653b4920ed3SToby Isaac           PetscScalar    *fL = NULL, *fR = NULL;
16547c45b140SToby Isaac           PetscInt        ghost, d, nsupp, nchild;
165508449791SMatthew G. Knepley 
165608449791SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
1657ffe9ad51SToby Isaac           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
16587c45b140SToby Isaac           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
16597c45b140SToby Isaac           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
166008449791SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
1661b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cells[0],&ghost);CHKERRQ(ierr);
1662b6a34eadSToby Isaac           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, cells[0], f, fa, &fL);CHKERRQ(ierr);}
1663b4920ed3SToby Isaac           ierr = DMLabelGetValue(ghostLabel,cells[1],&ghost);CHKERRQ(ierr);
1664b6a34eadSToby Isaac           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, cells[1], f, fa, &fR);CHKERRQ(ierr);}
1665c10b5f1bSMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
1666c10b5f1bSMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
1667c10b5f1bSMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
166808449791SMatthew G. Knepley           }
166908449791SMatthew G. Knepley           ++iface;
167008449791SMatthew G. Knepley         }
1671dab51205SMatthew G. Knepley       }
1672dab51205SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
1673dab51205SMatthew G. Knepley     }
1674c10b5f1bSMatthew G. Knepley     /* Handle time derivative */
1675c10b5f1bSMatthew G. Knepley     if (locX_t) {
1676dab51205SMatthew G. Knepley       PetscScalar *x_t, *fa;
1677dab51205SMatthew G. Knepley 
1678dab51205SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
1679c10b5f1bSMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
1680dab51205SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1681dab51205SMatthew G. Knepley         PetscFV      fv;
1682dab51205SMatthew G. Knepley         PetscObject  obj;
1683dab51205SMatthew G. Knepley         PetscClassId id;
1684dab51205SMatthew G. Knepley         PetscInt     pdim, d;
1685dab51205SMatthew G. Knepley 
1686dab51205SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1687dab51205SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1688dab51205SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
1689dab51205SMatthew G. Knepley         fv   = (PetscFV) obj;
1690dab51205SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
1691c10b5f1bSMatthew G. Knepley         for (cell = cS; cell < cE; ++cell) {
1692c10b5f1bSMatthew G. Knepley           PetscScalar *u_t, *r;
1693c10b5f1bSMatthew G. Knepley 
1694b4920ed3SToby Isaac           if (ghostLabel) {
1695b4920ed3SToby Isaac             PetscInt ghostVal;
1696b4920ed3SToby Isaac 
1697b4920ed3SToby Isaac             ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
1698b4920ed3SToby Isaac             if (ghostVal > 0) continue;
1699b4920ed3SToby Isaac           }
1700c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
1701c10b5f1bSMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
1702d63b37e5SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
1703c10b5f1bSMatthew G. Knepley         }
1704dab51205SMatthew G. Knepley       }
1705c10b5f1bSMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
170608449791SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
170708449791SMatthew G. Knepley     }
170808449791SMatthew G. Knepley     if (useFEM) {
170908449791SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, cS, cE, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
171008449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, PETSC_SCALAR, &elemVec);CHKERRQ(ierr);
171108449791SMatthew G. Knepley     }
171208449791SMatthew G. Knepley     if (useFVM) {
17135f942ad5SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
17145f942ad5SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
171508449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxL);CHKERRQ(ierr);
171608449791SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, PETSC_SCALAR, &fluxR);CHKERRQ(ierr);
171708449791SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
171808449791SMatthew G. Knepley     }
171908449791SMatthew G. Knepley   }
172008449791SMatthew G. Knepley 
172111dd639bSMatthew G. Knepley   if (useFEM) {ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);}
172208449791SMatthew G. Knepley 
172308449791SMatthew G. Knepley   /* FEM */
172408449791SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
172508449791SMatthew G. Knepley   /* 2: Get geometric data */
172608449791SMatthew G. Knepley   /* 3: Handle boundary values */
172708449791SMatthew G. Knepley   /* 4: Loop over domain */
172808449791SMatthew G. Knepley   /*   Extract coefficients */
172908449791SMatthew G. Knepley   /* Loop over fields */
173008449791SMatthew G. Knepley   /*   Set tiling for FE*/
173108449791SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
173208449791SMatthew G. Knepley   /*     Loop over subdomain */
173308449791SMatthew G. Knepley   /*       Loop over quad points */
173408449791SMatthew G. Knepley   /*         Transform coords to real space */
173508449791SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
173608449791SMatthew G. Knepley   /*         Evaluate residual at point */
173708449791SMatthew G. Knepley   /*         Transform residual to real space */
173808449791SMatthew G. Knepley   /*       Add residual to elemVec */
173908449791SMatthew G. Knepley   /* Loop over domain */
174008449791SMatthew G. Knepley   /*   Add elemVec to locX */
174108449791SMatthew G. Knepley 
174208449791SMatthew G. Knepley   /* FVM */
174308449791SMatthew G. Knepley   /* Get geometric data */
174408449791SMatthew G. Knepley   /* If using gradients */
174508449791SMatthew G. Knepley   /*   Compute gradient data */
174608449791SMatthew G. Knepley   /*   Loop over domain faces */
174708449791SMatthew G. Knepley   /*     Count computational faces */
174808449791SMatthew G. Knepley   /*     Reconstruct cell gradient */
174908449791SMatthew G. Knepley   /*   Loop over domain cells */
175008449791SMatthew G. Knepley   /*     Limit cell gradients */
175108449791SMatthew G. Knepley   /* Handle boundary values */
175208449791SMatthew G. Knepley   /* Loop over domain faces */
175308449791SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
175408449791SMatthew G. Knepley   /* Riemann solve over faces */
175508449791SMatthew G. Knepley   /* Loop over domain faces */
175608449791SMatthew G. Knepley   /*   Accumulate fluxes to cells */
175708449791SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
1758247ba720SToby Isaac   if (mesh->printFEM) {
1759247ba720SToby Isaac     Vec         locFbc;
1760247ba720SToby Isaac     PetscInt    pStart, pEnd, p, maxDof;
1761247ba720SToby Isaac     PetscScalar *zeroes;
1762247ba720SToby Isaac 
1763247ba720SToby Isaac     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
1764247ba720SToby Isaac     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
1765247ba720SToby Isaac     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
1766247ba720SToby Isaac     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
1767247ba720SToby Isaac     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
1768247ba720SToby Isaac     for (p = pStart; p < pEnd; p++) {
1769247ba720SToby Isaac       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
1770247ba720SToby Isaac     }
1771247ba720SToby Isaac     ierr = PetscFree(zeroes);CHKERRQ(ierr);
1772247ba720SToby Isaac     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
1773247ba720SToby Isaac     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
1774247ba720SToby Isaac   }
177524cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
177624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
177724cdb843SMatthew G. Knepley }
177824cdb843SMatthew G. Knepley 
177911dd639bSMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, PetscReal t, Vec F, void *user)
178024cdb843SMatthew G. Knepley {
178124cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1782bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
178324cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
178424cdb843SMatthew G. Knepley   PetscQuadrature   q;
178524cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
17862f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
17872f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
178824cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
178924cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
17903755293bSMatthew G. Knepley   PetscInt          totDim, totDimAux = 0, diffCell = 0;
179124cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
179224cdb843SMatthew G. Knepley 
179324cdb843SMatthew G. Knepley   PetscFunctionBegin;
179424cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
179524cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
179624cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
179724cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
179824cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
179924cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
180024cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
180124cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
180224cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
180324cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
180424cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
180524cdb843SMatthew G. Knepley   if (dmAux) {
180624cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
180724cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
180824cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
180924cdb843SMatthew G. Knepley   }
181024cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1811bbce034cSMatthew G. Knepley   ierr = PetscMalloc3(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim,&elemVec);CHKERRQ(ierr);
181224cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
181324cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1814bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
18152f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
18162f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
18172f84e9bcSToby Isaac     DM dmCell;
18182f84e9bcSToby Isaac 
18192f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
18202f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
18212f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
18227726bd6bSToby Isaac       PetscScalar *thisgeom;
18232f84e9bcSToby Isaac 
18242f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
18252f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
18262f84e9bcSToby Isaac     }
18272f84e9bcSToby Isaac   }
18282f84e9bcSToby Isaac   else {
18292f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
18302f84e9bcSToby Isaac   }
183124cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
183224cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
183324cdb843SMatthew G. Knepley     PetscInt     i;
183424cdb843SMatthew G. Knepley 
183524cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
183624cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
183724cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
183824cdb843SMatthew G. Knepley     if (X_t) {
183924cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
184024cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
184124cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
184224cdb843SMatthew G. Knepley     }
184324cdb843SMatthew G. Knepley     if (dmAux) {
18446da023fcSToby Isaac       DM dmAuxPlex;
18456da023fcSToby Isaac 
18466da023fcSToby Isaac       ierr = DMSNESConvertPlex(dmAux,&dmAuxPlex, PETSC_FALSE);CHKERRQ(ierr);
18476da023fcSToby Isaac       ierr = DMPlexVecGetClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
184824cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
18496da023fcSToby Isaac       ierr = DMPlexVecRestoreClosure(dmAuxPlex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
18506da023fcSToby Isaac       ierr = DMDestroy(&dmAuxPlex);CHKERRQ(ierr);
185124cdb843SMatthew G. Knepley     }
185224cdb843SMatthew G. Knepley   }
185324cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
185424cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
185524cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
185624cdb843SMatthew G. Knepley     /* Conforming batches */
185724cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
185824cdb843SMatthew G. Knepley     /* Remainder */
185924cdb843SMatthew G. Knepley     PetscInt Nr, offset;
186024cdb843SMatthew G. Knepley 
186124cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
186224cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
186324cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
186424cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
186524cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
186624cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
186724cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
186824cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
186924cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
187024cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
187124cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
187224cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
187324cdb843SMatthew G. Knepley     offset    = numCells - Nr;
187411dd639bSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
187511dd639bSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, cgeom, u, u_t, probAux, a, t, elemVecCh);CHKERRQ(ierr);
187611dd639bSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
187711dd639bSMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVecCh[offset*totDim]);CHKERRQ(ierr);
187824cdb843SMatthew G. Knepley   }
187924cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
188024cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
188124cdb843SMatthew G. Knepley     PetscInt  d;
188224cdb843SMatthew G. Knepley 
188324cdb843SMatthew 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;}
188424cdb843SMatthew G. Knepley     if (diff) {
188524cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
188624cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
188724cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
188824cdb843SMatthew G. Knepley       ++diffCell;
188924cdb843SMatthew G. Knepley     }
189024cdb843SMatthew G. Knepley     if (diffCell > 9) break;
1891c14a31d2SToby Isaac     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
189224cdb843SMatthew G. Knepley   }
18932f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
18942f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
18952f84e9bcSToby Isaac   }
18962f84e9bcSToby Isaac   else {
18972f84e9bcSToby Isaac     cgeom = NULL;
18982f84e9bcSToby Isaac   }
18992f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
1900bbce034cSMatthew G. Knepley   ierr = PetscFree3(u,u_t,elemVec);CHKERRQ(ierr);
190124cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
190224cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
190324cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
190424cdb843SMatthew G. Knepley }
190524cdb843SMatthew G. Knepley 
190624cdb843SMatthew G. Knepley /*@
190724cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
190824cdb843SMatthew G. Knepley 
190924cdb843SMatthew G. Knepley   Input Parameters:
191024cdb843SMatthew G. Knepley + dm - The mesh
191124cdb843SMatthew G. Knepley . X  - Local solution
191224cdb843SMatthew G. Knepley - user - The user context
191324cdb843SMatthew G. Knepley 
191424cdb843SMatthew G. Knepley   Output Parameter:
191524cdb843SMatthew G. Knepley . F  - Local output vector
191624cdb843SMatthew G. Knepley 
191724cdb843SMatthew G. Knepley   Level: developer
191824cdb843SMatthew G. Knepley 
191924cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
192024cdb843SMatthew G. Knepley @*/
192124cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
192224cdb843SMatthew G. Knepley {
192324cdb843SMatthew G. Knepley   PetscObject    check;
1924c4d4a4f8SMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
19256da023fcSToby Isaac   DM             plex;
192624cdb843SMatthew G. Knepley   PetscErrorCode ierr;
192724cdb843SMatthew G. Knepley 
192824cdb843SMatthew G. Knepley   PetscFunctionBegin;
19296da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
19306da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
19316da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1932c4d4a4f8SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
193324cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
19346da023fcSToby Isaac   ierr = PetscObjectQuery((PetscObject) plex, "dmCh", &check);CHKERRQ(ierr);
193511dd639bSMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(plex, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
193611dd639bSMatthew G. Knepley   else       {ierr = DMPlexComputeResidual_Internal(plex, cStart, cEnd, PETSC_MIN_REAL, X, NULL, 0.0, F, user);CHKERRQ(ierr);}
19379a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
193824cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
193924cdb843SMatthew G. Knepley }
194024cdb843SMatthew G. Knepley 
1941bdd6f66aSToby Isaac /*@
1942bdd6f66aSToby Isaac   DMPlexSNESComputeBoundaryFEM - Form the boundary values for the local input X
1943bdd6f66aSToby Isaac 
1944bdd6f66aSToby Isaac   Input Parameters:
1945bdd6f66aSToby Isaac + dm - The mesh
1946bdd6f66aSToby Isaac - user - The user context
1947bdd6f66aSToby Isaac 
1948bdd6f66aSToby Isaac   Output Parameter:
1949bdd6f66aSToby Isaac . X  - Local solution
1950bdd6f66aSToby Isaac 
1951bdd6f66aSToby Isaac   Level: developer
1952bdd6f66aSToby Isaac 
1953bdd6f66aSToby Isaac .seealso: DMPlexComputeJacobianActionFEM()
1954bdd6f66aSToby Isaac @*/
1955bdd6f66aSToby Isaac PetscErrorCode DMPlexSNESComputeBoundaryFEM(DM dm, Vec X, void *user)
1956bdd6f66aSToby Isaac {
1957bdd6f66aSToby Isaac   DM             plex;
1958bdd6f66aSToby Isaac   PetscErrorCode ierr;
1959bdd6f66aSToby Isaac 
1960bdd6f66aSToby Isaac   PetscFunctionBegin;
1961bdd6f66aSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
1962bdd6f66aSToby Isaac   ierr = DMPlexInsertBoundaryValues(plex, PETSC_TRUE, X, PETSC_MIN_REAL, NULL, NULL, NULL);CHKERRQ(ierr);
1963bdd6f66aSToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
1964bdd6f66aSToby Isaac   PetscFunctionReturn(0);
1965bdd6f66aSToby Isaac }
1966bdd6f66aSToby Isaac 
1967089bfe53SSander Arens PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
1968089bfe53SSander Arens {
1969089bfe53SSander Arens   DM_Plex         *mesh = (DM_Plex *) dm->data;
19702d91c981SSander Arens   DM               dmAux = NULL, plex = NULL;
19712d91c981SSander Arens   PetscSection     section, globalSection, subSection, sectionAux = NULL;
19722d91c981SSander Arens   PetscDS          prob, probAux = NULL;
1973089bfe53SSander Arens   DMLabel          depth;
19742d91c981SSander Arens   Vec              locA = NULL;
19754d0b9603SSander Arens   PetscFEFaceGeom *fgeom;
19762d91c981SSander Arens   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
19774d0b9603SSander Arens   PetscInt         dim, totDim, totDimAux, numBd, bd, Nf;
1978089bfe53SSander Arens   PetscBool        isMatISP;
1979089bfe53SSander Arens   PetscErrorCode   ierr;
1980089bfe53SSander Arens 
1981089bfe53SSander Arens   PetscFunctionBegin;
1982089bfe53SSander Arens   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1983089bfe53SSander Arens   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1984089bfe53SSander Arens   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
1985089bfe53SSander Arens   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
1986089bfe53SSander Arens   if (isMatISP) {
1987089bfe53SSander Arens     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1988089bfe53SSander Arens   }
1989089bfe53SSander Arens   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1990089bfe53SSander Arens   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1991089bfe53SSander Arens   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
19924d0b9603SSander Arens   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1993089bfe53SSander Arens   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
19942d91c981SSander Arens   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
19952d91c981SSander Arens   if (locA) {
19962d91c981SSander Arens     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
19972d91c981SSander Arens     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
19982d91c981SSander Arens     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
19992d91c981SSander Arens     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
20004d0b9603SSander Arens     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
20012d91c981SSander Arens   }
2002089bfe53SSander Arens   for (bd = 0; bd < numBd; ++bd) {
2003df1fddb2SMatthew G. Knepley     DMBoundaryConditionType type;
2004089bfe53SSander Arens     const char             *bdLabel;
2005089bfe53SSander Arens     DMLabel                 label;
2006089bfe53SSander Arens     IS                      pointIS;
2007089bfe53SSander Arens     const PetscInt         *points;
2008089bfe53SSander Arens     const PetscInt         *values;
20094d0b9603SSander Arens     PetscInt                fieldI, fieldJ, numValues, v, numPoints, p, dep, numFaces, face;
2010089bfe53SSander Arens     PetscObject             obj;
2011089bfe53SSander Arens     PetscClassId            id;
2012089bfe53SSander Arens 
2013df1fddb2SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
20144d0b9603SSander Arens     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
2015089bfe53SSander Arens     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2016df1fddb2SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
2017089bfe53SSander Arens     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
2018089bfe53SSander Arens     for (v = 0; v < numValues; ++v) {
2019089bfe53SSander Arens       ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2020089bfe53SSander Arens       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2021089bfe53SSander Arens       if (!pointIS) continue; /* No points with that id on this process */
2022089bfe53SSander Arens       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2023089bfe53SSander Arens       for (p = 0, numFaces = 0; p < numPoints; ++p) {
2024089bfe53SSander Arens         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
2025089bfe53SSander Arens         if (dep == dim-1) ++numFaces;
2026089bfe53SSander Arens       }
20274d0b9603SSander Arens       ierr = PetscMalloc4(numFaces*totDim,&u,locX_t ? numFaces*totDim : 0,&u_t,numFaces,&fgeom,numFaces*totDim*totDim,&elemMat);CHKERRQ(ierr);
20284d0b9603SSander Arens       if (locA) {ierr = PetscMalloc1(numFaces*totDimAux,&a);CHKERRQ(ierr);}
20294d0b9603SSander Arens       for (p = 0, face = 0; p < numPoints; ++p) {
20304d0b9603SSander Arens         const PetscInt point = points[p], *support, *cone;
2031089bfe53SSander Arens         PetscScalar   *x     = NULL;
20324d0b9603SSander Arens         PetscReal      dummyJ[9], dummyDetJ;
20339bfb2fe4SJed Brown         PetscInt       i, coneSize, faceLoc;
2034089bfe53SSander Arens 
20354d0b9603SSander Arens         ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
2036089bfe53SSander Arens         if (dep != dim-1) continue;
20374d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
20384d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFEM(dm, support[0], NULL, NULL, dummyJ, fgeom[face].invJ[0], &dummyDetJ);CHKERRQ(ierr);
20394d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, fgeom[face].v0, fgeom[face].J, NULL, &fgeom[face].detJ);CHKERRQ(ierr);
20404d0b9603SSander Arens         ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, fgeom[face].n);CHKERRQ(ierr);
20414d0b9603SSander Arens         if (fgeom[face].detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", fgeom[face].detJ, point);
20424d0b9603SSander Arens         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
20434d0b9603SSander Arens         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
20444d0b9603SSander Arens         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
20454d0b9603SSander 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]);
20464d0b9603SSander Arens         fgeom[face].face[0] = faceLoc;
20474d0b9603SSander Arens         ierr = DMPlexVecGetClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
20484d0b9603SSander Arens         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
20494d0b9603SSander Arens         ierr = DMPlexVecRestoreClosure(dm, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
2050089bfe53SSander Arens         if (locX_t) {
20514d0b9603SSander Arens           ierr = DMPlexVecGetClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
20524d0b9603SSander Arens           for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
20534d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(dm, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
2054089bfe53SSander Arens         }
20552d91c981SSander Arens         if (locA) {
20564d0b9603SSander Arens           ierr = DMPlexVecGetClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
20574d0b9603SSander Arens           for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
20584d0b9603SSander Arens           ierr = DMPlexVecRestoreClosure(plex, sectionAux, locA, support[0], NULL, &x);CHKERRQ(ierr);
20592d91c981SSander Arens         }
20604d0b9603SSander Arens         ++face;
2061089bfe53SSander Arens       }
20624d0b9603SSander Arens       ierr = PetscMemzero(elemMat, numFaces*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2063089bfe53SSander Arens       {
2064089bfe53SSander Arens         PetscFE         fe;
2065089bfe53SSander Arens         PetscQuadrature q;
2066089bfe53SSander Arens         PetscInt        numQuadPoints, Nb;
2067089bfe53SSander Arens         /* Conforming batches */
2068089bfe53SSander Arens         PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2069089bfe53SSander Arens         /* Remainder */
2070089bfe53SSander Arens         PetscInt        Nr, offset;
2071089bfe53SSander Arens 
20724d0b9603SSander Arens         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
20734d0b9603SSander Arens         ierr = PetscFEGetFaceQuadrature(fe, &q);CHKERRQ(ierr);
2074089bfe53SSander Arens         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2075089bfe53SSander Arens         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2076089bfe53SSander Arens         ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
2077089bfe53SSander Arens         blockSize = Nb*numQuadPoints;
2078089bfe53SSander Arens         batchSize = numBlocks * blockSize;
2079089bfe53SSander Arens         ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2080089bfe53SSander Arens         numChunks = numFaces / (numBatches*batchSize);
2081089bfe53SSander Arens         Ne        = numChunks*numBatches*batchSize;
2082089bfe53SSander Arens         Nr        = numFaces % (numBatches*batchSize);
2083089bfe53SSander Arens         offset    = numFaces - Nr;
2084089bfe53SSander Arens         for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
20854d0b9603SSander Arens           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, fgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
20864d0b9603SSander Arens           ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, &fgeom[offset], &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);
2087089bfe53SSander Arens         }
2088089bfe53SSander Arens       }
20894d0b9603SSander Arens       for (p = 0, face = 0; p < numPoints; ++p) {
20904d0b9603SSander Arens         const PetscInt point = points[p], *support;
2091089bfe53SSander Arens 
2092089bfe53SSander Arens         ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
2093089bfe53SSander Arens         if (dep != dim-1) continue;
20944d0b9603SSander Arens         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
20954d0b9603SSander Arens         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
2096089bfe53SSander Arens         if (!isMatISP) {
20974d0b9603SSander Arens           ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2098089bfe53SSander Arens         } else {
2099089bfe53SSander Arens           Mat lJ;
2100089bfe53SSander Arens 
2101089bfe53SSander Arens           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
21024d0b9603SSander Arens           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2103089bfe53SSander Arens         }
21044d0b9603SSander Arens         ++face;
2105089bfe53SSander Arens       }
2106089bfe53SSander Arens       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2107089bfe53SSander Arens       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21084d0b9603SSander Arens       ierr = PetscFree4(u,u_t,fgeom,elemMat);CHKERRQ(ierr);
21091c46aac1SSander Arens       if (locA) {ierr = PetscFree(a);CHKERRQ(ierr);}
21102d91c981SSander Arens     }
2111089bfe53SSander Arens   }
21121c46aac1SSander Arens   if (plex) {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
2113089bfe53SSander Arens   PetscFunctionReturn(0);
2114089bfe53SSander Arens }
2115089bfe53SSander Arens 
2116b953af5fSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
211724cdb843SMatthew G. Knepley {
211824cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
211924cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
2120f7ed7b21SMatthew G. Knepley   DM                dmAux, plex;
2121bbce034cSMatthew G. Knepley   Vec               A, cellgeom;
212224cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
2123be36d101SStefano Zampini   PetscSection      section, globalSection, subSection, sectionAux;
21242f84e9bcSToby Isaac   PetscFECellGeom  *cgeom = NULL;
21252f84e9bcSToby Isaac   PetscScalar      *cgeomScal;
2126426ff135SMatthew G. Knepley   PetscScalar      *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
212784f7fa7aSMatthew G. Knepley   PetscInt          dim, Nf, fieldI, fieldJ, numCells, c;
2128089bfe53SSander Arens   PetscInt          totDim, totDimAux;
2129be36d101SStefano Zampini   PetscBool         isMatIS, isMatISP, isShell, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE;
213024cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
213124cdb843SMatthew G. Knepley 
213224cdb843SMatthew G. Knepley   PetscFunctionBegin;
213324cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
213424cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
213524cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2136be36d101SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
213724cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2138be36d101SStefano Zampini   if (isMatISP) {
2139be36d101SStefano Zampini     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2140be36d101SStefano Zampini   }
214124cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
214224cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2143efc10488SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
214455ad3c34SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2145426ff135SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2146426ff135SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
214724cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
214824cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
214924cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
215024cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
215124cdb843SMatthew G. Knepley   if (dmAux) {
2152f7ed7b21SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2153f7ed7b21SMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
215424cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
215524cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
215624cdb843SMatthew G. Knepley   }
2157*290cf56fSMatthew G. Knepley   if (hasJac && hasPrec) {ierr = MatZeroEntries(Jac);CHKERRQ(ierr);}
215824cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
2159efc10488SMatthew 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);
216024cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2161bbce034cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
21622f84e9bcSToby Isaac   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
21632f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
21642f84e9bcSToby Isaac     DM dmCell;
21652f84e9bcSToby Isaac 
21662f84e9bcSToby Isaac     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
21672f84e9bcSToby Isaac     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
21682f84e9bcSToby Isaac     for (c = 0; c < cEnd - cStart; c++) {
21697726bd6bSToby Isaac       PetscScalar *thisgeom;
21702f84e9bcSToby Isaac 
21712f84e9bcSToby Isaac       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
21722f84e9bcSToby Isaac       cgeom[c] = *((PetscFECellGeom *) thisgeom);
21732f84e9bcSToby Isaac     }
2174efc10488SMatthew G. Knepley   } else {
21752f84e9bcSToby Isaac     cgeom = (PetscFECellGeom *) cgeomScal;
21762f84e9bcSToby Isaac   }
217724cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
217824cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
217924cdb843SMatthew G. Knepley     PetscInt     i;
218024cdb843SMatthew G. Knepley 
218124cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
21829f11d433SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
218324cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
218424cdb843SMatthew G. Knepley     if (X_t) {
218524cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
21869f11d433SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
218724cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
218824cdb843SMatthew G. Knepley     }
218924cdb843SMatthew G. Knepley     if (dmAux) {
2190f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
21919f11d433SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
2192f7ed7b21SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
219324cdb843SMatthew G. Knepley     }
219424cdb843SMatthew G. Knepley   }
2195efc10488SMatthew G. Knepley   if (hasJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
219655ad3c34SMatthew G. Knepley   if (hasPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2197426ff135SMatthew G. Knepley   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
219824cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
21995244db28SMatthew G. Knepley     PetscClassId    id;
220024cdb843SMatthew G. Knepley     PetscFE         fe;
2201089bfe53SSander Arens     PetscQuadrature q;
220224cdb843SMatthew G. Knepley     PetscInt        numQuadPoints, Nb;
220324cdb843SMatthew G. Knepley     /* Conforming batches */
220424cdb843SMatthew G. Knepley     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
220524cdb843SMatthew G. Knepley     /* Remainder */
220624cdb843SMatthew G. Knepley     PetscInt        Nr, offset;
220724cdb843SMatthew G. Knepley 
220824cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
22095244db28SMatthew G. Knepley     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
22105244db28SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
2211089bfe53SSander Arens     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
221224cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
221324cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2214089bfe53SSander Arens     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
221524cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
221624cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
221724cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
221824cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
221924cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
222024cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
222124cdb843SMatthew G. Knepley     offset    = numCells - Nr;
222224cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2223efc10488SMatthew G. Knepley       if (hasJac) {
222411dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
222511dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2226efc10488SMatthew G. Knepley       }
222755ad3c34SMatthew G. Knepley       if (hasPrec) {
222811dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
222911dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
2230426ff135SMatthew G. Knepley       }
2231426ff135SMatthew G. Knepley       if (hasDyn) {
223211dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
223311dd639bSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
223455ad3c34SMatthew G. Knepley       }
223524cdb843SMatthew G. Knepley     }
223624cdb843SMatthew G. Knepley   }
2237426ff135SMatthew G. Knepley   if (hasDyn) {
2238426ff135SMatthew G. Knepley     for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2239426ff135SMatthew G. Knepley   }
22405244db28SMatthew G. Knepley   if (hasFV) {
22415244db28SMatthew G. Knepley     PetscClassId id;
22425244db28SMatthew G. Knepley     PetscFV      fv;
22435244db28SMatthew G. Knepley     PetscInt     offsetI, NcI, NbI = 1, fc, f;
22445244db28SMatthew G. Knepley 
22455244db28SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
22465244db28SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
22475244db28SMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
22485244db28SMatthew G. Knepley       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
22495244db28SMatthew G. Knepley       if (id != PETSCFV_CLASSID) continue;
22505244db28SMatthew G. Knepley       /* Put in the identity */
22515244db28SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
22525244db28SMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22535244db28SMatthew G. Knepley         const PetscInt eOffset = (c-cStart)*totDim*totDim;
22545244db28SMatthew G. Knepley         for (fc = 0; fc < NcI; ++fc) {
22555244db28SMatthew G. Knepley           for (f = 0; f < NbI; ++f) {
22565244db28SMatthew G. Knepley             const PetscInt i = offsetI + f*NcI+fc;
22575244db28SMatthew G. Knepley             if (hasPrec) {
22585244db28SMatthew G. Knepley               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
22595244db28SMatthew G. Knepley               elemMatP[eOffset+i*totDim+i] = 1.0;
22605244db28SMatthew G. Knepley             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
22615244db28SMatthew G. Knepley           }
22625244db28SMatthew G. Knepley         }
22635244db28SMatthew G. Knepley       }
22645244db28SMatthew G. Knepley     }
22655244db28SMatthew G. Knepley     /* No allocated space for FV stuff, so ignore the zero entries */
22665244db28SMatthew G. Knepley     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
22675244db28SMatthew G. Knepley   }
2268be36d101SStefano Zampini   isMatIS = PETSC_FALSE;
2269be36d101SStefano Zampini   if (hasPrec && hasJac) {
2270be36d101SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);
2271be36d101SStefano Zampini   }
2272be36d101SStefano Zampini   if (isMatIS && !subSection) {
2273be36d101SStefano Zampini     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
2274be36d101SStefano Zampini   }
227524cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
227655ad3c34SMatthew G. Knepley     if (hasPrec) {
2277efc10488SMatthew G. Knepley       if (hasJac) {
227855ad3c34SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2279be36d101SStefano Zampini         if (!isMatIS) {
228055ad3c34SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2281be36d101SStefano Zampini         } else {
2282be36d101SStefano Zampini           Mat lJ;
2283be36d101SStefano Zampini 
2284be36d101SStefano Zampini           ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
2285be36d101SStefano Zampini           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2286be36d101SStefano Zampini         }
2287efc10488SMatthew G. Knepley       }
228855ad3c34SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2289be36d101SStefano Zampini       if (!isMatISP) {
229055ad3c34SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
229155ad3c34SMatthew G. Knepley       } else {
2292be36d101SStefano Zampini         Mat lJ;
2293be36d101SStefano Zampini 
2294be36d101SStefano Zampini         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2295be36d101SStefano Zampini         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMatP[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2296be36d101SStefano Zampini       }
2297be36d101SStefano Zampini     } else {
22989f11d433SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
2299be36d101SStefano Zampini       if (!isMatISP) {
23009f11d433SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2301be36d101SStefano Zampini       } else {
2302be36d101SStefano Zampini         Mat lJ;
2303be36d101SStefano Zampini 
2304be36d101SStefano Zampini         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
2305be36d101SStefano Zampini         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, c, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
2306be36d101SStefano Zampini       }
230724cdb843SMatthew G. Knepley     }
230855ad3c34SMatthew G. Knepley   }
23095244db28SMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
23102f84e9bcSToby Isaac   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
23112f84e9bcSToby Isaac     ierr = PetscFree(cgeom);CHKERRQ(ierr);
23125244db28SMatthew G. Knepley   } else {
23132f84e9bcSToby Isaac     cgeom = NULL;
23142f84e9bcSToby Isaac   }
23152f84e9bcSToby Isaac   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2316426ff135SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
2317f7ed7b21SMatthew G. Knepley   if (dmAux) {
2318f7ed7b21SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
2319f7ed7b21SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2320f7ed7b21SMatthew G. Knepley   }
2321089bfe53SSander Arens   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
2322efc10488SMatthew G. Knepley   if (hasJac && hasPrec) {
232382ef7567SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232482ef7567SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232582ef7567SMatthew G. Knepley   }
232624cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232724cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
232824cdb843SMatthew G. Knepley   if (mesh->printFEM) {
232924cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
233024cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
233124cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
233224cdb843SMatthew G. Knepley   }
233324cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
233424cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
233524cdb843SMatthew G. Knepley   if (isShell) {
233624cdb843SMatthew G. Knepley     JacActionCtx *jctx;
233724cdb843SMatthew G. Knepley 
233824cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
233924cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
234024cdb843SMatthew G. Knepley   }
234124cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
234224cdb843SMatthew G. Knepley }
234324cdb843SMatthew G. Knepley 
2344a925c78cSMatthew G. Knepley 
2345a925c78cSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianAction_Internal(DM dm, PetscInt cStart, PetscInt cEnd, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
2346a925c78cSMatthew G. Knepley {
2347a925c78cSMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
2348a925c78cSMatthew G. Knepley   const char       *name  = "Jacobian";
2349a925c78cSMatthew G. Knepley   DM                dmAux, plex;
2350a925c78cSMatthew G. Knepley   Vec               A, cellgeom;
2351a925c78cSMatthew G. Knepley   PetscDS           prob, probAux = NULL;
2352a925c78cSMatthew G. Knepley   PetscQuadrature   quad;
2353a925c78cSMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
2354a925c78cSMatthew G. Knepley   PetscFECellGeom  *cgeom = NULL;
2355a925c78cSMatthew G. Knepley   PetscScalar      *cgeomScal;
2356a925c78cSMatthew G. Knepley   PetscScalar      *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
235775b37a90SMatthew G. Knepley   PetscInt          dim, Nf, fieldI, fieldJ, numCells, c;
23584d0b9603SSander Arens   PetscInt          totDim, totDimAux = 0;
235975b37a90SMatthew G. Knepley   PetscBool         hasDyn;
2360a925c78cSMatthew G. Knepley   PetscErrorCode    ierr;
2361a925c78cSMatthew G. Knepley 
2362a925c78cSMatthew G. Knepley   PetscFunctionBegin;
2363a925c78cSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2364a925c78cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2365a925c78cSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2366a925c78cSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
2367a925c78cSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2368a925c78cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2369a925c78cSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2370a925c78cSMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2371a925c78cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
2372a925c78cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2373a925c78cSMatthew G. Knepley   numCells = cEnd - cStart;
2374a925c78cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2375a925c78cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2376a925c78cSMatthew G. Knepley   if (dmAux) {
2377a925c78cSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
2378a925c78cSMatthew G. Knepley     ierr = DMGetDefaultSection(plex, &sectionAux);CHKERRQ(ierr);
2379a925c78cSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2380a925c78cSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2381a925c78cSMatthew G. Knepley   }
2382a925c78cSMatthew G. Knepley   ierr = VecSet(Z, 0.0);CHKERRQ(ierr);
2383a925c78cSMatthew 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);
2384a925c78cSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2385a925c78cSMatthew G. Knepley   ierr = DMPlexSNESGetGeometryFEM(dm, &cellgeom);CHKERRQ(ierr);
2386a925c78cSMatthew G. Knepley   ierr = VecGetArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2387a925c78cSMatthew G. Knepley   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {
2388a925c78cSMatthew G. Knepley     DM dmCell;
2389a925c78cSMatthew G. Knepley 
2390a925c78cSMatthew G. Knepley     ierr = VecGetDM(cellgeom,&dmCell);CHKERRQ(ierr);
2391a925c78cSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cStart,&cgeom);CHKERRQ(ierr);
2392a925c78cSMatthew G. Knepley     for (c = 0; c < cEnd - cStart; c++) {
2393a925c78cSMatthew G. Knepley       PetscScalar *thisgeom;
2394a925c78cSMatthew G. Knepley 
2395a925c78cSMatthew G. Knepley       ierr = DMPlexPointLocalRef(dmCell, c + cStart, cgeomScal, &thisgeom);CHKERRQ(ierr);
2396a925c78cSMatthew G. Knepley       cgeom[c] = *((PetscFECellGeom *) thisgeom);
2397a925c78cSMatthew G. Knepley     }
2398a925c78cSMatthew G. Knepley   } else {
2399a925c78cSMatthew G. Knepley     cgeom = (PetscFECellGeom *) cgeomScal;
2400a925c78cSMatthew G. Knepley   }
2401a925c78cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2402a925c78cSMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
2403a925c78cSMatthew G. Knepley     PetscInt     i;
2404a925c78cSMatthew G. Knepley 
2405a925c78cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
2406a925c78cSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[(c-cStart)*totDim+i] = x[i];
2407a925c78cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
2408a925c78cSMatthew G. Knepley     if (X_t) {
2409a925c78cSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
2410a925c78cSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[(c-cStart)*totDim+i] = x_t[i];
2411a925c78cSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
2412a925c78cSMatthew G. Knepley     }
2413a925c78cSMatthew G. Knepley     if (dmAux) {
2414a925c78cSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
2415a925c78cSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[(c-cStart)*totDimAux+i] = x[i];
2416a925c78cSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
2417a925c78cSMatthew G. Knepley     }
2418a925c78cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr);
2419a925c78cSMatthew G. Knepley     for (i = 0; i < totDim; ++i) y[(c-cStart)*totDim+i] = x[i];
2420a925c78cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, Y, c, NULL, &x);CHKERRQ(ierr);
2421a925c78cSMatthew G. Knepley   }
2422a925c78cSMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
2423a925c78cSMatthew G. Knepley   if (hasDyn)  {ierr = PetscMemzero(elemMatD, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
2424a925c78cSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2425a925c78cSMatthew G. Knepley     PetscFE  fe;
2426a925c78cSMatthew G. Knepley     PetscInt numQuadPoints, Nb;
2427a925c78cSMatthew G. Knepley     /* Conforming batches */
2428a925c78cSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
2429a925c78cSMatthew G. Knepley     /* Remainder */
2430a925c78cSMatthew G. Knepley     PetscInt Nr, offset;
2431a925c78cSMatthew G. Knepley 
2432a925c78cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2433a925c78cSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
2434a925c78cSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2435a925c78cSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2436a925c78cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
2437a925c78cSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
2438a925c78cSMatthew G. Knepley     batchSize = numBlocks * blockSize;
2439a925c78cSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2440a925c78cSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
2441a925c78cSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
2442a925c78cSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
2443a925c78cSMatthew G. Knepley     offset    = numCells - Nr;
2444a925c78cSMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
24451bf9e2faSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
24461bf9e2faSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
2447a925c78cSMatthew G. Knepley       if (hasDyn) {
24481bf9e2faSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, cgeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
24491bf9e2faSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Nr, &cgeom[offset], &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
2450a925c78cSMatthew G. Knepley       }
2451a925c78cSMatthew G. Knepley     }
2452a925c78cSMatthew G. Knepley   }
2453a925c78cSMatthew G. Knepley   if (hasDyn) {
2454a925c78cSMatthew G. Knepley     for (c = 0; c < (cEnd - cStart)*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
2455a925c78cSMatthew G. Knepley   }
2456a925c78cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2457a925c78cSMatthew G. Knepley     const PetscBLASInt M = totDim, one = 1;
2458a925c78cSMatthew G. Knepley     const PetscScalar  a = 1.0, b = 0.0;
2459a925c78cSMatthew G. Knepley 
2460a925c78cSMatthew G. Knepley     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[(c-cStart)*totDim*totDim], &M, &y[(c-cStart)*totDim], &one, &b, z, &one));
2461a925c78cSMatthew G. Knepley     if (mesh->printFEM > 1) {
2462a925c78cSMatthew G. Knepley       ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);
2463a925c78cSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Y",  totDim, &y[(c-cStart)*totDim]);CHKERRQ(ierr);
2464a925c78cSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Z",  totDim, z);CHKERRQ(ierr);
2465a925c78cSMatthew G. Knepley     }
2466a925c78cSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, Z, c, z, ADD_VALUES);CHKERRQ(ierr);
2467a925c78cSMatthew G. Knepley   }
2468a925c78cSMatthew G. Knepley   if (sizeof(PetscFECellGeom) % sizeof(PetscScalar)) {ierr = PetscFree(cgeom);CHKERRQ(ierr);}
2469a925c78cSMatthew G. Knepley   else                                               {cgeom = NULL;}
2470a925c78cSMatthew G. Knepley   ierr = VecRestoreArray(cellgeom, &cgeomScal);CHKERRQ(ierr);
2471a925c78cSMatthew G. Knepley   ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr);
2472a925c78cSMatthew G. Knepley   if (dmAux) {
2473a925c78cSMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
2474a925c78cSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
2475a925c78cSMatthew G. Knepley   }
2476a925c78cSMatthew G. Knepley   if (mesh->printFEM) {
2477a925c78cSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "Z:\n");CHKERRQ(ierr);
2478a925c78cSMatthew G. Knepley     ierr = VecView(Z, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
2479a925c78cSMatthew G. Knepley   }
2480a925c78cSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2481a925c78cSMatthew G. Knepley   PetscFunctionReturn(0);
2482a925c78cSMatthew G. Knepley }
2483a925c78cSMatthew G. Knepley 
248424cdb843SMatthew G. Knepley /*@
248524cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
248624cdb843SMatthew G. Knepley 
248724cdb843SMatthew G. Knepley   Input Parameters:
248824cdb843SMatthew G. Knepley + dm - The mesh
248924cdb843SMatthew G. Knepley . X  - Local input vector
249024cdb843SMatthew G. Knepley - user - The user context
249124cdb843SMatthew G. Knepley 
249224cdb843SMatthew G. Knepley   Output Parameter:
249324cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
249424cdb843SMatthew G. Knepley 
249524cdb843SMatthew G. Knepley   Note:
249624cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
249724cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
249824cdb843SMatthew G. Knepley 
249924cdb843SMatthew G. Knepley   Level: developer
250024cdb843SMatthew G. Knepley 
250124cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
250224cdb843SMatthew G. Knepley @*/
250324cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
250424cdb843SMatthew G. Knepley {
2505b953af5fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
25066da023fcSToby Isaac   DM             plex;
250724cdb843SMatthew G. Knepley   PetscErrorCode ierr;
250824cdb843SMatthew G. Knepley 
250924cdb843SMatthew G. Knepley   PetscFunctionBegin;
25106da023fcSToby Isaac   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
25116da023fcSToby Isaac   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
25126da023fcSToby Isaac   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2513b953af5fSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
25146da023fcSToby Isaac   ierr = DMPlexComputeJacobian_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
25159a81d013SToby Isaac   ierr = DMDestroy(&plex);CHKERRQ(ierr);
251624cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
251724cdb843SMatthew G. Knepley }
25181878804aSMatthew G. Knepley 
2519a925c78cSMatthew G. Knepley /*@
2520a925c78cSMatthew 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.
2521a925c78cSMatthew G. Knepley 
2522a925c78cSMatthew G. Knepley   Input Parameters:
2523a925c78cSMatthew G. Knepley + dm - The mesh
2524a925c78cSMatthew G. Knepley . X  - Local solution vector
2525a925c78cSMatthew G. Knepley . Y  - Local input vector
2526a925c78cSMatthew G. Knepley - user - The user context
2527a925c78cSMatthew G. Knepley 
2528a925c78cSMatthew G. Knepley   Output Parameter:
2529a925c78cSMatthew G. Knepley . Z - Local output vector
2530a925c78cSMatthew G. Knepley 
2531a925c78cSMatthew G. Knepley   Note:
2532a925c78cSMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
2533a925c78cSMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
2534a925c78cSMatthew G. Knepley 
2535a925c78cSMatthew G. Knepley   Level: developer
2536a925c78cSMatthew G. Knepley 
2537a925c78cSMatthew G. Knepley .seealso: FormFunctionLocal()
2538a925c78cSMatthew G. Knepley @*/
2539a925c78cSMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianActionFEM(DM dm, Vec X, Vec Y, Vec Z, void *user)
2540a925c78cSMatthew G. Knepley {
2541a925c78cSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
2542a925c78cSMatthew G. Knepley   DM             plex;
2543a925c78cSMatthew G. Knepley   PetscErrorCode ierr;
2544a925c78cSMatthew G. Knepley 
2545a925c78cSMatthew G. Knepley   PetscFunctionBegin;
2546a925c78cSMatthew G. Knepley   ierr = DMSNESConvertPlex(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
2547a925c78cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(plex, 0, &cStart, &cEnd);CHKERRQ(ierr);
2548a925c78cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(plex, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2549a925c78cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2550a925c78cSMatthew G. Knepley   ierr = DMPlexComputeJacobianAction_Internal(plex, cStart, cEnd, 0.0, 0.0, X, NULL, Y, Z, user);CHKERRQ(ierr);
2551a925c78cSMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2552a925c78cSMatthew G. Knepley   PetscFunctionReturn(0);
2553a925c78cSMatthew G. Knepley }
2554a925c78cSMatthew G. Knepley 
25559f520fc2SToby Isaac /*@
25569f520fc2SToby Isaac   DMPlexSetSNESLocalFEM - Use DMPlex's internal FEM routines to compute SNES boundary values, residual, and Jacobian.
25579f520fc2SToby Isaac 
25589f520fc2SToby Isaac   Input Parameters:
25599f520fc2SToby Isaac + dm - The DM object
2560dff059c6SToby Isaac . boundaryctx - the user context that will be passed to pointwise evaluation of boundary values (see PetscDSAddBoundary())
25619f520fc2SToby Isaac . residualctx - the user context that will be passed to pointwise evaluation of finite element residual computations (see PetscDSSetResidual())
25629f520fc2SToby Isaac - jacobianctx - the user context that will be passed to pointwise evaluation of finite element Jacobian construction (see PetscDSSetJacobian())
25631a244344SSatish Balay 
25641a244344SSatish Balay   Level: developer
25659f520fc2SToby Isaac @*/
25669f520fc2SToby Isaac PetscErrorCode DMPlexSetSNESLocalFEM(DM dm, void *boundaryctx, void *residualctx, void *jacobianctx)
25679f520fc2SToby Isaac {
25689f520fc2SToby Isaac   PetscErrorCode ierr;
25699f520fc2SToby Isaac 
25709f520fc2SToby Isaac   PetscFunctionBegin;
25719f520fc2SToby Isaac   ierr = DMSNESSetBoundaryLocal(dm,DMPlexSNESComputeBoundaryFEM,boundaryctx);CHKERRQ(ierr);
25729f520fc2SToby Isaac   ierr = DMSNESSetFunctionLocal(dm,DMPlexSNESComputeResidualFEM,residualctx);CHKERRQ(ierr);
25739f520fc2SToby Isaac   ierr = DMSNESSetJacobianLocal(dm,DMPlexSNESComputeJacobianFEM,jacobianctx);CHKERRQ(ierr);
25749f520fc2SToby Isaac   PetscFunctionReturn(0);
25759f520fc2SToby Isaac }
25769f520fc2SToby Isaac 
25770163fd50SMatthew 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)
25781878804aSMatthew G. Knepley {
25791878804aSMatthew G. Knepley   Mat            J, M;
25801878804aSMatthew G. Knepley   Vec            r, b;
25811878804aSMatthew G. Knepley   MatNullSpace   nullSpace;
25821878804aSMatthew G. Knepley   PetscReal     *error, res = 0.0;
25831878804aSMatthew G. Knepley   PetscInt       numFields;
25841878804aSMatthew G. Knepley   PetscErrorCode ierr;
25851878804aSMatthew G. Knepley 
25861878804aSMatthew G. Knepley   PetscFunctionBegin;
25871878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &r);CHKERRQ(ierr);
25881878804aSMatthew G. Knepley   ierr = DMCreateMatrix(dm, &J);CHKERRQ(ierr);
25891878804aSMatthew G. Knepley   M    = J;
25901878804aSMatthew G. Knepley   /* TODO Null space for J */
25911878804aSMatthew G. Knepley   /* Check discretization error */
25921878804aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
25931878804aSMatthew G. Knepley   ierr = PetscMalloc1(PetscMax(1, numFields), &error);CHKERRQ(ierr);
25941878804aSMatthew G. Knepley   if (numFields > 1) {
25951878804aSMatthew G. Knepley     PetscInt f;
25961878804aSMatthew G. Knepley 
25971189c1efSToby Isaac     ierr = DMComputeL2FieldDiff(dm, 0.0, exactFuncs, ctxs, u, error);CHKERRQ(ierr);
25981878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: [");CHKERRQ(ierr);
25991878804aSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
26001878804aSMatthew G. Knepley       if (f) {ierr = PetscPrintf(PETSC_COMM_WORLD, ", ");CHKERRQ(ierr);}
26011878804aSMatthew G. Knepley       if (error[f] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "%g", error[f]);CHKERRQ(ierr);}
26021878804aSMatthew G. Knepley       else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "< 1.0e-11");CHKERRQ(ierr);}
26031878804aSMatthew G. Knepley     }
26041878804aSMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "]\n");CHKERRQ(ierr);
26051878804aSMatthew G. Knepley   } else {
26060709b2feSToby Isaac     ierr = DMComputeL2Diff(dm, 0.0, exactFuncs, ctxs, u, &error[0]);CHKERRQ(ierr);
26071878804aSMatthew G. Knepley     if (error[0] >= 1.0e-11) {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: %g\n", error[0]);CHKERRQ(ierr);}
26081878804aSMatthew G. Knepley     else                     {ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Error: < 1.0e-11\n");CHKERRQ(ierr);}
26091878804aSMatthew G. Knepley   }
26101878804aSMatthew G. Knepley   ierr = PetscFree(error);CHKERRQ(ierr);
26111878804aSMatthew G. Knepley   /* Check residual */
26121878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, u, r);CHKERRQ(ierr);
26131878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
26141878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "L_2 Residual: %g\n", res);CHKERRQ(ierr);
26151878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
26161878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Initial Residual");CHKERRQ(ierr);
2617685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"res_");CHKERRQ(ierr);
2618685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
26191878804aSMatthew G. Knepley   /* Check Jacobian */
26201878804aSMatthew G. Knepley   ierr = SNESComputeJacobian(snes, u, M, M);CHKERRQ(ierr);
26211878804aSMatthew G. Knepley   ierr = MatGetNullSpace(J, &nullSpace);CHKERRQ(ierr);
26221878804aSMatthew G. Knepley   if (nullSpace) {
26231878804aSMatthew G. Knepley     PetscBool isNull;
26241878804aSMatthew G. Knepley     ierr = MatNullSpaceTest(nullSpace, J, &isNull);CHKERRQ(ierr);
26251878804aSMatthew G. Knepley     if (!isNull) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "The null space calculated for the system operator is invalid.");
26261878804aSMatthew G. Knepley   }
26271878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &b);CHKERRQ(ierr);
26281878804aSMatthew G. Knepley   ierr = VecSet(r, 0.0);CHKERRQ(ierr);
26291878804aSMatthew G. Knepley   ierr = SNESComputeFunction(snes, r, b);CHKERRQ(ierr);
26301878804aSMatthew G. Knepley   ierr = MatMult(M, u, r);CHKERRQ(ierr);
26311878804aSMatthew G. Knepley   ierr = VecAXPY(r, 1.0, b);CHKERRQ(ierr);
26321878804aSMatthew G. Knepley   ierr = VecDestroy(&b);CHKERRQ(ierr);
26331878804aSMatthew G. Knepley   ierr = VecNorm(r, NORM_2, &res);CHKERRQ(ierr);
26341878804aSMatthew G. Knepley   ierr = PetscPrintf(PETSC_COMM_WORLD, "Linear L_2 Residual: %g\n", res);CHKERRQ(ierr);
26351878804aSMatthew G. Knepley   ierr = VecChop(r, 1.0e-10);CHKERRQ(ierr);
26361878804aSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) r, "Au - b = Au + F(0)");CHKERRQ(ierr);
2637685405a1SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)r,"linear_res_");CHKERRQ(ierr);
2638685405a1SBarry Smith   ierr = VecViewFromOptions(r, NULL, "-vec_view");CHKERRQ(ierr);
26391878804aSMatthew G. Knepley   ierr = VecDestroy(&r);CHKERRQ(ierr);
26401878804aSMatthew G. Knepley   ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
26411878804aSMatthew G. Knepley   ierr = MatDestroy(&J);CHKERRQ(ierr);
26421878804aSMatthew G. Knepley   PetscFunctionReturn(0);
26431878804aSMatthew G. Knepley }
26441878804aSMatthew G. Knepley 
26450163fd50SMatthew 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)
26461878804aSMatthew G. Knepley {
26471878804aSMatthew G. Knepley   DM             dm;
26481878804aSMatthew G. Knepley   Vec            sol;
26491878804aSMatthew G. Knepley   PetscBool      check;
26501878804aSMatthew G. Knepley   PetscErrorCode ierr;
26511878804aSMatthew G. Knepley 
26521878804aSMatthew G. Knepley   PetscFunctionBegin;
2653c5929fdfSBarry Smith   ierr = PetscOptionsHasName(((PetscObject)snes)->options,((PetscObject)snes)->prefix, "-dmsnes_check", &check);CHKERRQ(ierr);
26541878804aSMatthew G. Knepley   if (!check) PetscFunctionReturn(0);
26551878804aSMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
26561878804aSMatthew G. Knepley   ierr = VecDuplicate(u, &sol);CHKERRQ(ierr);
26571878804aSMatthew G. Knepley   ierr = SNESSetSolution(snes, sol);CHKERRQ(ierr);
26581878804aSMatthew G. Knepley   ierr = DMSNESCheckFromOptions_Internal(snes, dm, u, sol, exactFuncs, ctxs);CHKERRQ(ierr);
26591878804aSMatthew G. Knepley   ierr = VecDestroy(&sol);CHKERRQ(ierr);
2660552f7358SJed Brown   PetscFunctionReturn(0);
2661552f7358SJed Brown }
2662