xref: /petsc/src/snes/utils/dmplexsnes.c (revision 24cdb843c62e1437a3052e6f6c6af2ebc2f9cd7c)
1*24cdb843SMatthew G. Knepley #include <petsc-private/dmpleximpl.h>   /*I "petscdmplex.h" I*/
2552f7358SJed Brown #include <petscsnes.h>                  /*I "petscsnes.h"   I*/
3*24cdb843SMatthew G. Knepley #include <petscds.h>
4afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>
5552f7358SJed Brown 
6*24cdb843SMatthew G. Knepley /************************** Interpolation *******************************/
7*24cdb843SMatthew G. Knepley 
8552f7358SJed Brown #undef __FUNCT__
9552f7358SJed Brown #define __FUNCT__ "DMInterpolationCreate"
100adebc6cSBarry Smith PetscErrorCode DMInterpolationCreate(MPI_Comm comm, DMInterpolationInfo *ctx)
110adebc6cSBarry Smith {
12552f7358SJed Brown   PetscErrorCode ierr;
13552f7358SJed Brown 
14552f7358SJed Brown   PetscFunctionBegin;
15552f7358SJed Brown   PetscValidPointer(ctx, 2);
16552f7358SJed Brown   ierr = PetscMalloc(sizeof(struct _DMInterpolationInfo), ctx);CHKERRQ(ierr);
171aa26658SKarl Rupp 
18552f7358SJed Brown   (*ctx)->comm   = comm;
19552f7358SJed Brown   (*ctx)->dim    = -1;
20552f7358SJed Brown   (*ctx)->nInput = 0;
210298fd71SBarry Smith   (*ctx)->points = NULL;
220298fd71SBarry Smith   (*ctx)->cells  = NULL;
23552f7358SJed Brown   (*ctx)->n      = -1;
240298fd71SBarry Smith   (*ctx)->coords = NULL;
25552f7358SJed Brown   PetscFunctionReturn(0);
26552f7358SJed Brown }
27552f7358SJed Brown 
28552f7358SJed Brown #undef __FUNCT__
29552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDim"
300adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDim(DMInterpolationInfo ctx, PetscInt dim)
310adebc6cSBarry Smith {
32552f7358SJed Brown   PetscFunctionBegin;
330adebc6cSBarry Smith   if ((dim < 1) || (dim > 3)) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension for points: %d", dim);
34552f7358SJed Brown   ctx->dim = dim;
35552f7358SJed Brown   PetscFunctionReturn(0);
36552f7358SJed Brown }
37552f7358SJed Brown 
38552f7358SJed Brown #undef __FUNCT__
39552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDim"
400adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDim(DMInterpolationInfo ctx, PetscInt *dim)
410adebc6cSBarry Smith {
42552f7358SJed Brown   PetscFunctionBegin;
43552f7358SJed Brown   PetscValidIntPointer(dim, 2);
44552f7358SJed Brown   *dim = ctx->dim;
45552f7358SJed Brown   PetscFunctionReturn(0);
46552f7358SJed Brown }
47552f7358SJed Brown 
48552f7358SJed Brown #undef __FUNCT__
49552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetDof"
500adebc6cSBarry Smith PetscErrorCode DMInterpolationSetDof(DMInterpolationInfo ctx, PetscInt dof)
510adebc6cSBarry Smith {
52552f7358SJed Brown   PetscFunctionBegin;
530adebc6cSBarry Smith   if (dof < 1) SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of components: %d", dof);
54552f7358SJed Brown   ctx->dof = dof;
55552f7358SJed Brown   PetscFunctionReturn(0);
56552f7358SJed Brown }
57552f7358SJed Brown 
58552f7358SJed Brown #undef __FUNCT__
59552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetDof"
600adebc6cSBarry Smith PetscErrorCode DMInterpolationGetDof(DMInterpolationInfo ctx, PetscInt *dof)
610adebc6cSBarry Smith {
62552f7358SJed Brown   PetscFunctionBegin;
63552f7358SJed Brown   PetscValidIntPointer(dof, 2);
64552f7358SJed Brown   *dof = ctx->dof;
65552f7358SJed Brown   PetscFunctionReturn(0);
66552f7358SJed Brown }
67552f7358SJed Brown 
68552f7358SJed Brown #undef __FUNCT__
69552f7358SJed Brown #define __FUNCT__ "DMInterpolationAddPoints"
700adebc6cSBarry Smith PetscErrorCode DMInterpolationAddPoints(DMInterpolationInfo ctx, PetscInt n, PetscReal points[])
710adebc6cSBarry Smith {
72552f7358SJed Brown   PetscErrorCode ierr;
73552f7358SJed Brown 
74552f7358SJed Brown   PetscFunctionBegin;
750adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
760adebc6cSBarry Smith   if (ctx->points)  SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "Cannot add points multiple times yet");
77552f7358SJed Brown   ctx->nInput = n;
781aa26658SKarl Rupp 
79785e854fSJed Brown   ierr = PetscMalloc1(n*ctx->dim, &ctx->points);CHKERRQ(ierr);
80552f7358SJed Brown   ierr = PetscMemcpy(ctx->points, points, n*ctx->dim * sizeof(PetscReal));CHKERRQ(ierr);
81552f7358SJed Brown   PetscFunctionReturn(0);
82552f7358SJed Brown }
83552f7358SJed Brown 
84552f7358SJed Brown #undef __FUNCT__
85552f7358SJed Brown #define __FUNCT__ "DMInterpolationSetUp"
860adebc6cSBarry Smith PetscErrorCode DMInterpolationSetUp(DMInterpolationInfo ctx, DM dm, PetscBool redundantPoints)
870adebc6cSBarry Smith {
88552f7358SJed Brown   MPI_Comm       comm = ctx->comm;
89552f7358SJed Brown   PetscScalar    *a;
90552f7358SJed Brown   PetscInt       p, q, i;
91552f7358SJed Brown   PetscMPIInt    rank, size;
92552f7358SJed Brown   PetscErrorCode ierr;
93552f7358SJed Brown   Vec            pointVec;
94552f7358SJed Brown   IS             cellIS;
95552f7358SJed Brown   PetscLayout    layout;
96552f7358SJed Brown   PetscReal      *globalPoints;
97cb313848SJed Brown   PetscScalar    *globalPointsScalar;
98552f7358SJed Brown   const PetscInt *ranges;
99552f7358SJed Brown   PetscMPIInt    *counts, *displs;
100552f7358SJed Brown   const PetscInt *foundCells;
101552f7358SJed Brown   PetscMPIInt    *foundProcs, *globalProcs;
10219436ca2SJed Brown   PetscInt       n, N;
103552f7358SJed Brown 
10419436ca2SJed Brown   PetscFunctionBegin;
10519436ca2SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10619436ca2SJed Brown   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
10719436ca2SJed Brown   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1080adebc6cSBarry Smith   if (ctx->dim < 0) SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "The spatial dimension has not been set");
10919436ca2SJed Brown   /* Locate points */
11019436ca2SJed Brown   n = ctx->nInput;
111552f7358SJed Brown   if (!redundantPoints) {
112552f7358SJed Brown     ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
113552f7358SJed Brown     ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
114552f7358SJed Brown     ierr = PetscLayoutSetLocalSize(layout, n);CHKERRQ(ierr);
115552f7358SJed Brown     ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
116552f7358SJed Brown     ierr = PetscLayoutGetSize(layout, &N);CHKERRQ(ierr);
117552f7358SJed Brown     /* Communicate all points to all processes */
118dcca6d9dSJed Brown     ierr = PetscMalloc3(N*ctx->dim,&globalPoints,size,&counts,size,&displs);CHKERRQ(ierr);
119552f7358SJed Brown     ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
120552f7358SJed Brown     for (p = 0; p < size; ++p) {
121552f7358SJed Brown       counts[p] = (ranges[p+1] - ranges[p])*ctx->dim;
122552f7358SJed Brown       displs[p] = ranges[p]*ctx->dim;
123552f7358SJed Brown     }
124552f7358SJed Brown     ierr = MPI_Allgatherv(ctx->points, n*ctx->dim, MPIU_REAL, globalPoints, counts, displs, MPIU_REAL, comm);CHKERRQ(ierr);
125552f7358SJed Brown   } else {
126552f7358SJed Brown     N = n;
127552f7358SJed Brown     globalPoints = ctx->points;
12838ea73c8SJed Brown     counts = displs = NULL;
12938ea73c8SJed Brown     layout = NULL;
130552f7358SJed Brown   }
131552f7358SJed Brown #if 0
132dcca6d9dSJed Brown   ierr = PetscMalloc3(N,&foundCells,N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
13319436ca2SJed Brown   /* foundCells[p] = m->locatePoint(&globalPoints[p*ctx->dim]); */
134552f7358SJed Brown #else
135cb313848SJed Brown #if defined(PETSC_USE_COMPLEX)
136785e854fSJed Brown   ierr = PetscMalloc1(N,&globalPointsScalar);CHKERRQ(ierr);
137cb313848SJed Brown   for (i=0; i<N; i++) globalPointsScalar[i] = globalPoints[i];
138cb313848SJed Brown #else
139cb313848SJed Brown   globalPointsScalar = globalPoints;
140cb313848SJed Brown #endif
14104706141SMatthew G Knepley   ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, ctx->dim, N*ctx->dim, globalPointsScalar, &pointVec);CHKERRQ(ierr);
142dcca6d9dSJed Brown   ierr = PetscMalloc2(N,&foundProcs,N,&globalProcs);CHKERRQ(ierr);
143552f7358SJed Brown   ierr = DMLocatePoints(dm, pointVec, &cellIS);CHKERRQ(ierr);
144552f7358SJed Brown   ierr = ISGetIndices(cellIS, &foundCells);CHKERRQ(ierr);
145552f7358SJed Brown #endif
146552f7358SJed Brown   for (p = 0; p < N; ++p) {
1471aa26658SKarl Rupp     if (foundCells[p] >= 0) foundProcs[p] = rank;
1481aa26658SKarl Rupp     else foundProcs[p] = size;
149552f7358SJed Brown   }
150552f7358SJed Brown   /* Let the lowest rank process own each point */
151efab3cc2SJed Brown   ierr   = MPI_Allreduce(foundProcs, globalProcs, N, MPI_INT, MPI_MIN, comm);CHKERRQ(ierr);
152552f7358SJed Brown   ctx->n = 0;
153552f7358SJed Brown   for (p = 0; p < N; ++p) {
1540adebc6cSBarry Smith     if (globalProcs[p] == size) SETERRQ4(comm, PETSC_ERR_PLIB, "Point %d: %g %g %g not located in mesh", p, globalPoints[p*ctx->dim+0], ctx->dim > 1 ? globalPoints[p*ctx->dim+1] : 0.0, ctx->dim > 2 ? globalPoints[p*ctx->dim+2] : 0.0);
1551aa26658SKarl Rupp     else if (globalProcs[p] == rank) ctx->n++;
156552f7358SJed Brown   }
157552f7358SJed Brown   /* Create coordinates vector and array of owned cells */
158785e854fSJed Brown   ierr = PetscMalloc1(ctx->n, &ctx->cells);CHKERRQ(ierr);
159552f7358SJed Brown   ierr = VecCreate(comm, &ctx->coords);CHKERRQ(ierr);
160552f7358SJed Brown   ierr = VecSetSizes(ctx->coords, ctx->n*ctx->dim, PETSC_DECIDE);CHKERRQ(ierr);
161552f7358SJed Brown   ierr = VecSetBlockSize(ctx->coords, ctx->dim);CHKERRQ(ierr);
162c0dedaeaSBarry Smith   ierr = VecSetType(ctx->coords,VECSTANDARD);CHKERRQ(ierr);
163552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &a);CHKERRQ(ierr);
164552f7358SJed Brown   for (p = 0, q = 0, i = 0; p < N; ++p) {
165552f7358SJed Brown     if (globalProcs[p] == rank) {
166552f7358SJed Brown       PetscInt d;
167552f7358SJed Brown 
1681aa26658SKarl Rupp       for (d = 0; d < ctx->dim; ++d, ++i) a[i] = globalPoints[p*ctx->dim+d];
169552f7358SJed Brown       ctx->cells[q++] = foundCells[p];
170552f7358SJed Brown     }
171552f7358SJed Brown   }
172552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &a);CHKERRQ(ierr);
173552f7358SJed Brown #if 0
174552f7358SJed Brown   ierr = PetscFree3(foundCells,foundProcs,globalProcs);CHKERRQ(ierr);
175552f7358SJed Brown #else
176552f7358SJed Brown   ierr = PetscFree2(foundProcs,globalProcs);CHKERRQ(ierr);
177552f7358SJed Brown   ierr = ISRestoreIndices(cellIS, &foundCells);CHKERRQ(ierr);
178552f7358SJed Brown   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
179552f7358SJed Brown   ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
180552f7358SJed Brown #endif
181cb313848SJed Brown   if ((void*)globalPointsScalar != (void*)globalPoints) {ierr = PetscFree(globalPointsScalar);CHKERRQ(ierr);}
182d343d804SMatthew G. Knepley   if (!redundantPoints) {ierr = PetscFree3(globalPoints,counts,displs);CHKERRQ(ierr);}
183552f7358SJed Brown   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
184552f7358SJed Brown   PetscFunctionReturn(0);
185552f7358SJed Brown }
186552f7358SJed Brown 
187552f7358SJed Brown #undef __FUNCT__
188552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetCoordinates"
1890adebc6cSBarry Smith PetscErrorCode DMInterpolationGetCoordinates(DMInterpolationInfo ctx, Vec *coordinates)
1900adebc6cSBarry Smith {
191552f7358SJed Brown   PetscFunctionBegin;
192552f7358SJed Brown   PetscValidPointer(coordinates, 2);
1930adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
194552f7358SJed Brown   *coordinates = ctx->coords;
195552f7358SJed Brown   PetscFunctionReturn(0);
196552f7358SJed Brown }
197552f7358SJed Brown 
198552f7358SJed Brown #undef __FUNCT__
199552f7358SJed Brown #define __FUNCT__ "DMInterpolationGetVector"
2000adebc6cSBarry Smith PetscErrorCode DMInterpolationGetVector(DMInterpolationInfo ctx, Vec *v)
2010adebc6cSBarry Smith {
202552f7358SJed Brown   PetscErrorCode ierr;
203552f7358SJed Brown 
204552f7358SJed Brown   PetscFunctionBegin;
205552f7358SJed Brown   PetscValidPointer(v, 2);
2060adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
207552f7358SJed Brown   ierr = VecCreate(ctx->comm, v);CHKERRQ(ierr);
208552f7358SJed Brown   ierr = VecSetSizes(*v, ctx->n*ctx->dof, PETSC_DECIDE);CHKERRQ(ierr);
209552f7358SJed Brown   ierr = VecSetBlockSize(*v, ctx->dof);CHKERRQ(ierr);
210c0dedaeaSBarry Smith   ierr = VecSetType(*v,VECSTANDARD);CHKERRQ(ierr);
211552f7358SJed Brown   PetscFunctionReturn(0);
212552f7358SJed Brown }
213552f7358SJed Brown 
214552f7358SJed Brown #undef __FUNCT__
215552f7358SJed Brown #define __FUNCT__ "DMInterpolationRestoreVector"
2160adebc6cSBarry Smith PetscErrorCode DMInterpolationRestoreVector(DMInterpolationInfo ctx, Vec *v)
2170adebc6cSBarry Smith {
218552f7358SJed Brown   PetscErrorCode ierr;
219552f7358SJed Brown 
220552f7358SJed Brown   PetscFunctionBegin;
221552f7358SJed Brown   PetscValidPointer(v, 2);
2220adebc6cSBarry Smith   if (!ctx->coords) SETERRQ(ctx->comm, PETSC_ERR_ARG_WRONGSTATE, "The interpolation context has not been setup.");
223552f7358SJed Brown   ierr = VecDestroy(v);CHKERRQ(ierr);
224552f7358SJed Brown   PetscFunctionReturn(0);
225552f7358SJed Brown }
226552f7358SJed Brown 
227552f7358SJed Brown #undef __FUNCT__
2287a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Triangle_Private"
2297a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Triangle_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
230a6dfd86eSKarl Rupp {
231552f7358SJed Brown   PetscReal      *v0, *J, *invJ, detJ;
232552f7358SJed Brown   PetscScalar    *a, *coords;
233552f7358SJed Brown   PetscInt       p;
234552f7358SJed Brown   PetscErrorCode ierr;
235552f7358SJed Brown 
236552f7358SJed Brown   PetscFunctionBegin;
237dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
238552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
239552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
240552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
241552f7358SJed Brown     PetscInt     c = ctx->cells[p];
242a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
243552f7358SJed Brown     PetscReal    xi[4];
244552f7358SJed Brown     PetscInt     d, f, comp;
245552f7358SJed Brown 
2468e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
247552f7358SJed Brown     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2480298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2491aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2501aa26658SKarl Rupp 
251552f7358SJed Brown     for (d = 0; d < ctx->dim; ++d) {
252552f7358SJed Brown       xi[d] = 0.0;
2531aa26658SKarl Rupp       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
2541aa26658SKarl Rupp       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[(d+1)*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
255552f7358SJed Brown     }
2560298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
257552f7358SJed Brown   }
258552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
259552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
260552f7358SJed Brown   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
261552f7358SJed Brown   PetscFunctionReturn(0);
262552f7358SJed Brown }
263552f7358SJed Brown 
264552f7358SJed Brown #undef __FUNCT__
2657a1931ceSMatthew G. Knepley #define __FUNCT__ "DMInterpolate_Tetrahedron_Private"
2667a1931ceSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Tetrahedron_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
2677a1931ceSMatthew G. Knepley {
2687a1931ceSMatthew G. Knepley   PetscReal      *v0, *J, *invJ, detJ;
2697a1931ceSMatthew G. Knepley   PetscScalar    *a, *coords;
2707a1931ceSMatthew G. Knepley   PetscInt       p;
2717a1931ceSMatthew G. Knepley   PetscErrorCode ierr;
2727a1931ceSMatthew G. Knepley 
2737a1931ceSMatthew G. Knepley   PetscFunctionBegin;
274dcca6d9dSJed Brown   ierr = PetscMalloc3(ctx->dim,&v0,ctx->dim*ctx->dim,&J,ctx->dim*ctx->dim,&invJ);CHKERRQ(ierr);
2757a1931ceSMatthew G. Knepley   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
2767a1931ceSMatthew G. Knepley   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
2777a1931ceSMatthew G. Knepley   for (p = 0; p < ctx->n; ++p) {
2787a1931ceSMatthew G. Knepley     PetscInt       c = ctx->cells[p];
2797a1931ceSMatthew G. Knepley     const PetscInt order[3] = {2, 1, 3};
2802584bbe8SMatthew G. Knepley     PetscScalar   *x = NULL;
2817a1931ceSMatthew G. Knepley     PetscReal      xi[4];
2827a1931ceSMatthew G. Knepley     PetscInt       d, f, comp;
2837a1931ceSMatthew G. Knepley 
2848e0841e0SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2857a1931ceSMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ, c);
2867a1931ceSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2877a1931ceSMatthew G. Knepley     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp];
2887a1931ceSMatthew G. Knepley 
2897a1931ceSMatthew G. Knepley     for (d = 0; d < ctx->dim; ++d) {
2907a1931ceSMatthew G. Knepley       xi[d] = 0.0;
2917a1931ceSMatthew G. Knepley       for (f = 0; f < ctx->dim; ++f) xi[d] += invJ[d*ctx->dim+f]*0.5*PetscRealPart(coords[p*ctx->dim+f] - v0[f]);
2927a1931ceSMatthew G. Knepley       for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] += PetscRealPart(x[order[d]*ctx->dof+comp] - x[0*ctx->dof+comp])*xi[d];
2937a1931ceSMatthew G. Knepley     }
2947a1931ceSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, NULL, &x);CHKERRQ(ierr);
2957a1931ceSMatthew G. Knepley   }
2967a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
2977a1931ceSMatthew G. Knepley   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
2987a1931ceSMatthew G. Knepley   ierr = PetscFree3(v0, J, invJ);CHKERRQ(ierr);
2997a1931ceSMatthew G. Knepley   PetscFunctionReturn(0);
3007a1931ceSMatthew G. Knepley }
3017a1931ceSMatthew G. Knepley 
3027a1931ceSMatthew G. Knepley #undef __FUNCT__
303552f7358SJed Brown #define __FUNCT__ "QuadMap_Private"
3045820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode QuadMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
305552f7358SJed Brown {
306552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
307552f7358SJed Brown   const PetscScalar x0        = vertices[0];
308552f7358SJed Brown   const PetscScalar y0        = vertices[1];
309552f7358SJed Brown   const PetscScalar x1        = vertices[2];
310552f7358SJed Brown   const PetscScalar y1        = vertices[3];
311552f7358SJed Brown   const PetscScalar x2        = vertices[4];
312552f7358SJed Brown   const PetscScalar y2        = vertices[5];
313552f7358SJed Brown   const PetscScalar x3        = vertices[6];
314552f7358SJed Brown   const PetscScalar y3        = vertices[7];
315552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
316552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
317552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
318552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
319552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
320552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
321552f7358SJed Brown   PetscScalar       *ref, *real;
322552f7358SJed Brown   PetscErrorCode    ierr;
323552f7358SJed Brown 
324552f7358SJed Brown   PetscFunctionBegin;
325552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
326552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
327552f7358SJed Brown   {
328552f7358SJed Brown     const PetscScalar p0 = ref[0];
329552f7358SJed Brown     const PetscScalar p1 = ref[1];
330552f7358SJed Brown 
331552f7358SJed Brown     real[0] = x0 + f_1 * p0 + f_3 * p1 + f_01 * p0 * p1;
332552f7358SJed Brown     real[1] = y0 + g_1 * p0 + g_3 * p1 + g_01 * p0 * p1;
333552f7358SJed Brown   }
334552f7358SJed Brown   ierr = PetscLogFlops(28);CHKERRQ(ierr);
335552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
336552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
337552f7358SJed Brown   PetscFunctionReturn(0);
338552f7358SJed Brown }
339552f7358SJed Brown 
340c0dedaeaSBarry Smith #include <petsc-private/dmimpl.h>
341552f7358SJed Brown #undef __FUNCT__
342552f7358SJed Brown #define __FUNCT__ "QuadJacobian_Private"
343d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode QuadJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
344552f7358SJed Brown {
345552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
346552f7358SJed Brown   const PetscScalar x0        = vertices[0];
347552f7358SJed Brown   const PetscScalar y0        = vertices[1];
348552f7358SJed Brown   const PetscScalar x1        = vertices[2];
349552f7358SJed Brown   const PetscScalar y1        = vertices[3];
350552f7358SJed Brown   const PetscScalar x2        = vertices[4];
351552f7358SJed Brown   const PetscScalar y2        = vertices[5];
352552f7358SJed Brown   const PetscScalar x3        = vertices[6];
353552f7358SJed Brown   const PetscScalar y3        = vertices[7];
354552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
355552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
356552f7358SJed Brown   PetscScalar       *ref;
357552f7358SJed Brown   PetscErrorCode    ierr;
358552f7358SJed Brown 
359552f7358SJed Brown   PetscFunctionBegin;
360552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
361552f7358SJed Brown   {
362552f7358SJed Brown     const PetscScalar x       = ref[0];
363552f7358SJed Brown     const PetscScalar y       = ref[1];
364552f7358SJed Brown     const PetscInt    rows[2] = {0, 1};
365da80777bSKarl Rupp     PetscScalar       values[4];
366da80777bSKarl Rupp 
367da80777bSKarl Rupp     values[0] = (x1 - x0 + f_01*y) * 0.5; values[1] = (x3 - x0 + f_01*x) * 0.5;
368da80777bSKarl Rupp     values[2] = (y1 - y0 + g_01*y) * 0.5; values[3] = (y3 - y0 + g_01*x) * 0.5;
36994ab13aaSBarry Smith     ierr      = MatSetValues(J, 2, rows, 2, rows, values, INSERT_VALUES);CHKERRQ(ierr);
370552f7358SJed Brown   }
371552f7358SJed Brown   ierr = PetscLogFlops(30);CHKERRQ(ierr);
372552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
37394ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
37494ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
375552f7358SJed Brown   PetscFunctionReturn(0);
376552f7358SJed Brown }
377552f7358SJed Brown 
378552f7358SJed Brown #undef __FUNCT__
379552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Quad_Private"
380a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Quad_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
381a6dfd86eSKarl Rupp {
382fafc0619SMatthew G Knepley   DM             dmCoord;
383552f7358SJed Brown   SNES           snes;
384552f7358SJed Brown   KSP            ksp;
385552f7358SJed Brown   PC             pc;
386552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
387552f7358SJed Brown   Mat            J;
388552f7358SJed Brown   PetscScalar    *a, *coords;
389552f7358SJed Brown   PetscInt       p;
390552f7358SJed Brown   PetscErrorCode ierr;
391552f7358SJed Brown 
392552f7358SJed Brown   PetscFunctionBegin;
393552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
394fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
395552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
396552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "quad_interp_");CHKERRQ(ierr);
397552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
398552f7358SJed Brown   ierr = VecSetSizes(r, 2, 2);CHKERRQ(ierr);
399c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
400552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
401552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
402552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
403552f7358SJed Brown   ierr = MatSetSizes(J, 2, 2, 2, 2);CHKERRQ(ierr);
404552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
405552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
4060298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, QuadMap_Private, NULL);CHKERRQ(ierr);
4070298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, QuadJacobian_Private, NULL);CHKERRQ(ierr);
408552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
409552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
410552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
411552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
412552f7358SJed Brown 
413552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
414552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
415552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
416a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
417552f7358SJed Brown     PetscScalar *xi;
418cb313848SJed Brown     PetscReal    xir[2];
419552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
420552f7358SJed Brown 
421552f7358SJed Brown     /* Can make this do all points at once */
4220298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4230adebc6cSBarry Smith     if (4*2 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 4*2);
4240298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
4250adebc6cSBarry Smith     if (4*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 4*ctx->dof);
4260298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
4270298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
428552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
429552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
430552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
431552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
432552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
433552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
434cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
435cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
4361aa26658SKarl Rupp     for (comp = 0; comp < ctx->dof; ++comp) a[p*ctx->dof+comp] = x[0*ctx->dof+comp]*(1 - xir[0])*(1 - xir[1]) + x[1*ctx->dof+comp]*xir[0]*(1 - xir[1]) + x[2*ctx->dof+comp]*xir[0]*xir[1] + x[3*ctx->dof+comp]*(1 - xir[0])*xir[1];
4371aa26658SKarl Rupp 
438552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
4390298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
4400298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
441552f7358SJed Brown   }
442552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
443552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
444552f7358SJed Brown 
445552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
446552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
447552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
448552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
449552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
450552f7358SJed Brown   PetscFunctionReturn(0);
451552f7358SJed Brown }
452552f7358SJed Brown 
453552f7358SJed Brown #undef __FUNCT__
454552f7358SJed Brown #define __FUNCT__ "HexMap_Private"
4555820edbdSMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode HexMap_Private(SNES snes, Vec Xref, Vec Xreal, void *ctx)
456552f7358SJed Brown {
457552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
458552f7358SJed Brown   const PetscScalar x0        = vertices[0];
459552f7358SJed Brown   const PetscScalar y0        = vertices[1];
460552f7358SJed Brown   const PetscScalar z0        = vertices[2];
4617a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
4627a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
4637a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
464552f7358SJed Brown   const PetscScalar x2        = vertices[6];
465552f7358SJed Brown   const PetscScalar y2        = vertices[7];
466552f7358SJed Brown   const PetscScalar z2        = vertices[8];
4677a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
4687a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
4697a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
470552f7358SJed Brown   const PetscScalar x4        = vertices[12];
471552f7358SJed Brown   const PetscScalar y4        = vertices[13];
472552f7358SJed Brown   const PetscScalar z4        = vertices[14];
473552f7358SJed Brown   const PetscScalar x5        = vertices[15];
474552f7358SJed Brown   const PetscScalar y5        = vertices[16];
475552f7358SJed Brown   const PetscScalar z5        = vertices[17];
476552f7358SJed Brown   const PetscScalar x6        = vertices[18];
477552f7358SJed Brown   const PetscScalar y6        = vertices[19];
478552f7358SJed Brown   const PetscScalar z6        = vertices[20];
479552f7358SJed Brown   const PetscScalar x7        = vertices[21];
480552f7358SJed Brown   const PetscScalar y7        = vertices[22];
481552f7358SJed Brown   const PetscScalar z7        = vertices[23];
482552f7358SJed Brown   const PetscScalar f_1       = x1 - x0;
483552f7358SJed Brown   const PetscScalar g_1       = y1 - y0;
484552f7358SJed Brown   const PetscScalar h_1       = z1 - z0;
485552f7358SJed Brown   const PetscScalar f_3       = x3 - x0;
486552f7358SJed Brown   const PetscScalar g_3       = y3 - y0;
487552f7358SJed Brown   const PetscScalar h_3       = z3 - z0;
488552f7358SJed Brown   const PetscScalar f_4       = x4 - x0;
489552f7358SJed Brown   const PetscScalar g_4       = y4 - y0;
490552f7358SJed Brown   const PetscScalar h_4       = z4 - z0;
491552f7358SJed Brown   const PetscScalar f_01      = x2 - x1 - x3 + x0;
492552f7358SJed Brown   const PetscScalar g_01      = y2 - y1 - y3 + y0;
493552f7358SJed Brown   const PetscScalar h_01      = z2 - z1 - z3 + z0;
494552f7358SJed Brown   const PetscScalar f_12      = x7 - x3 - x4 + x0;
495552f7358SJed Brown   const PetscScalar g_12      = y7 - y3 - y4 + y0;
496552f7358SJed Brown   const PetscScalar h_12      = z7 - z3 - z4 + z0;
497552f7358SJed Brown   const PetscScalar f_02      = x5 - x1 - x4 + x0;
498552f7358SJed Brown   const PetscScalar g_02      = y5 - y1 - y4 + y0;
499552f7358SJed Brown   const PetscScalar h_02      = z5 - z1 - z4 + z0;
500552f7358SJed Brown   const PetscScalar f_012     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
501552f7358SJed Brown   const PetscScalar g_012     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
502552f7358SJed Brown   const PetscScalar h_012     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
503552f7358SJed Brown   PetscScalar       *ref, *real;
504552f7358SJed Brown   PetscErrorCode    ierr;
505552f7358SJed Brown 
506552f7358SJed Brown   PetscFunctionBegin;
507552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
508552f7358SJed Brown   ierr = VecGetArray(Xreal, &real);CHKERRQ(ierr);
509552f7358SJed Brown   {
510552f7358SJed Brown     const PetscScalar p0 = ref[0];
511552f7358SJed Brown     const PetscScalar p1 = ref[1];
512552f7358SJed Brown     const PetscScalar p2 = ref[2];
513552f7358SJed Brown 
514552f7358SJed Brown     real[0] = x0 + f_1*p0 + f_3*p1 + f_4*p2 + f_01*p0*p1 + f_12*p1*p2 + f_02*p0*p2 + f_012*p0*p1*p2;
515552f7358SJed Brown     real[1] = y0 + g_1*p0 + g_3*p1 + g_4*p2 + g_01*p0*p1 + g_01*p0*p1 + g_12*p1*p2 + g_02*p0*p2 + g_012*p0*p1*p2;
516552f7358SJed Brown     real[2] = z0 + h_1*p0 + h_3*p1 + h_4*p2 + h_01*p0*p1 + h_01*p0*p1 + h_12*p1*p2 + h_02*p0*p2 + h_012*p0*p1*p2;
517552f7358SJed Brown   }
518552f7358SJed Brown   ierr = PetscLogFlops(114);CHKERRQ(ierr);
519552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
520552f7358SJed Brown   ierr = VecRestoreArray(Xreal, &real);CHKERRQ(ierr);
521552f7358SJed Brown   PetscFunctionReturn(0);
522552f7358SJed Brown }
523552f7358SJed Brown 
524552f7358SJed Brown #undef __FUNCT__
525552f7358SJed Brown #define __FUNCT__ "HexJacobian_Private"
526d1e9a80fSBarry Smith PETSC_STATIC_INLINE PetscErrorCode HexJacobian_Private(SNES snes, Vec Xref, Mat J, Mat M, void *ctx)
527552f7358SJed Brown {
528552f7358SJed Brown   const PetscScalar *vertices = (const PetscScalar*) ctx;
529552f7358SJed Brown   const PetscScalar x0        = vertices[0];
530552f7358SJed Brown   const PetscScalar y0        = vertices[1];
531552f7358SJed Brown   const PetscScalar z0        = vertices[2];
5327a1931ceSMatthew G. Knepley   const PetscScalar x1        = vertices[9];
5337a1931ceSMatthew G. Knepley   const PetscScalar y1        = vertices[10];
5347a1931ceSMatthew G. Knepley   const PetscScalar z1        = vertices[11];
535552f7358SJed Brown   const PetscScalar x2        = vertices[6];
536552f7358SJed Brown   const PetscScalar y2        = vertices[7];
537552f7358SJed Brown   const PetscScalar z2        = vertices[8];
5387a1931ceSMatthew G. Knepley   const PetscScalar x3        = vertices[3];
5397a1931ceSMatthew G. Knepley   const PetscScalar y3        = vertices[4];
5407a1931ceSMatthew G. Knepley   const PetscScalar z3        = vertices[5];
541552f7358SJed Brown   const PetscScalar x4        = vertices[12];
542552f7358SJed Brown   const PetscScalar y4        = vertices[13];
543552f7358SJed Brown   const PetscScalar z4        = vertices[14];
544552f7358SJed Brown   const PetscScalar x5        = vertices[15];
545552f7358SJed Brown   const PetscScalar y5        = vertices[16];
546552f7358SJed Brown   const PetscScalar z5        = vertices[17];
547552f7358SJed Brown   const PetscScalar x6        = vertices[18];
548552f7358SJed Brown   const PetscScalar y6        = vertices[19];
549552f7358SJed Brown   const PetscScalar z6        = vertices[20];
550552f7358SJed Brown   const PetscScalar x7        = vertices[21];
551552f7358SJed Brown   const PetscScalar y7        = vertices[22];
552552f7358SJed Brown   const PetscScalar z7        = vertices[23];
553552f7358SJed Brown   const PetscScalar f_xy      = x2 - x1 - x3 + x0;
554552f7358SJed Brown   const PetscScalar g_xy      = y2 - y1 - y3 + y0;
555552f7358SJed Brown   const PetscScalar h_xy      = z2 - z1 - z3 + z0;
556552f7358SJed Brown   const PetscScalar f_yz      = x7 - x3 - x4 + x0;
557552f7358SJed Brown   const PetscScalar g_yz      = y7 - y3 - y4 + y0;
558552f7358SJed Brown   const PetscScalar h_yz      = z7 - z3 - z4 + z0;
559552f7358SJed Brown   const PetscScalar f_xz      = x5 - x1 - x4 + x0;
560552f7358SJed Brown   const PetscScalar g_xz      = y5 - y1 - y4 + y0;
561552f7358SJed Brown   const PetscScalar h_xz      = z5 - z1 - z4 + z0;
562552f7358SJed Brown   const PetscScalar f_xyz     = x6 - x0 + x1 - x2 + x3 + x4 - x5 - x7;
563552f7358SJed Brown   const PetscScalar g_xyz     = y6 - y0 + y1 - y2 + y3 + y4 - y5 - y7;
564552f7358SJed Brown   const PetscScalar h_xyz     = z6 - z0 + z1 - z2 + z3 + z4 - z5 - z7;
565552f7358SJed Brown   PetscScalar       *ref;
566552f7358SJed Brown   PetscErrorCode    ierr;
567552f7358SJed Brown 
568552f7358SJed Brown   PetscFunctionBegin;
569552f7358SJed Brown   ierr = VecGetArray(Xref,  &ref);CHKERRQ(ierr);
570552f7358SJed Brown   {
571552f7358SJed Brown     const PetscScalar x       = ref[0];
572552f7358SJed Brown     const PetscScalar y       = ref[1];
573552f7358SJed Brown     const PetscScalar z       = ref[2];
574552f7358SJed Brown     const PetscInt    rows[3] = {0, 1, 2};
575da80777bSKarl Rupp     PetscScalar       values[9];
576da80777bSKarl Rupp 
577da80777bSKarl Rupp     values[0] = (x1 - x0 + f_xy*y + f_xz*z + f_xyz*y*z) / 2.0;
578da80777bSKarl Rupp     values[1] = (x3 - x0 + f_xy*x + f_yz*z + f_xyz*x*z) / 2.0;
579da80777bSKarl Rupp     values[2] = (x4 - x0 + f_yz*y + f_xz*x + f_xyz*x*y) / 2.0;
580da80777bSKarl Rupp     values[3] = (y1 - y0 + g_xy*y + g_xz*z + g_xyz*y*z) / 2.0;
581da80777bSKarl Rupp     values[4] = (y3 - y0 + g_xy*x + g_yz*z + g_xyz*x*z) / 2.0;
582da80777bSKarl Rupp     values[5] = (y4 - y0 + g_yz*y + g_xz*x + g_xyz*x*y) / 2.0;
583da80777bSKarl Rupp     values[6] = (z1 - z0 + h_xy*y + h_xz*z + h_xyz*y*z) / 2.0;
584da80777bSKarl Rupp     values[7] = (z3 - z0 + h_xy*x + h_yz*z + h_xyz*x*z) / 2.0;
585da80777bSKarl Rupp     values[8] = (z4 - z0 + h_yz*y + h_xz*x + h_xyz*x*y) / 2.0;
5861aa26658SKarl Rupp 
58794ab13aaSBarry Smith     ierr = MatSetValues(J, 3, rows, 3, rows, values, INSERT_VALUES);CHKERRQ(ierr);
588552f7358SJed Brown   }
589552f7358SJed Brown   ierr = PetscLogFlops(152);CHKERRQ(ierr);
590552f7358SJed Brown   ierr = VecRestoreArray(Xref,  &ref);CHKERRQ(ierr);
59194ab13aaSBarry Smith   ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
59294ab13aaSBarry Smith   ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
593552f7358SJed Brown   PetscFunctionReturn(0);
594552f7358SJed Brown }
595552f7358SJed Brown 
596552f7358SJed Brown #undef __FUNCT__
597552f7358SJed Brown #define __FUNCT__ "DMInterpolate_Hex_Private"
598a6dfd86eSKarl Rupp PETSC_STATIC_INLINE PetscErrorCode DMInterpolate_Hex_Private(DMInterpolationInfo ctx, DM dm, Vec xLocal, Vec v)
599a6dfd86eSKarl Rupp {
600fafc0619SMatthew G Knepley   DM             dmCoord;
601552f7358SJed Brown   SNES           snes;
602552f7358SJed Brown   KSP            ksp;
603552f7358SJed Brown   PC             pc;
604552f7358SJed Brown   Vec            coordsLocal, r, ref, real;
605552f7358SJed Brown   Mat            J;
606552f7358SJed Brown   PetscScalar    *a, *coords;
607552f7358SJed Brown   PetscInt       p;
608552f7358SJed Brown   PetscErrorCode ierr;
609552f7358SJed Brown 
610552f7358SJed Brown   PetscFunctionBegin;
611552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
612fafc0619SMatthew G Knepley   ierr = DMGetCoordinateDM(dm, &dmCoord);CHKERRQ(ierr);
613552f7358SJed Brown   ierr = SNESCreate(PETSC_COMM_SELF, &snes);CHKERRQ(ierr);
614552f7358SJed Brown   ierr = SNESSetOptionsPrefix(snes, "hex_interp_");CHKERRQ(ierr);
615552f7358SJed Brown   ierr = VecCreate(PETSC_COMM_SELF, &r);CHKERRQ(ierr);
616552f7358SJed Brown   ierr = VecSetSizes(r, 3, 3);CHKERRQ(ierr);
617c0dedaeaSBarry Smith   ierr = VecSetType(r,dm->vectype);CHKERRQ(ierr);
618552f7358SJed Brown   ierr = VecDuplicate(r, &ref);CHKERRQ(ierr);
619552f7358SJed Brown   ierr = VecDuplicate(r, &real);CHKERRQ(ierr);
620552f7358SJed Brown   ierr = MatCreate(PETSC_COMM_SELF, &J);CHKERRQ(ierr);
621552f7358SJed Brown   ierr = MatSetSizes(J, 3, 3, 3, 3);CHKERRQ(ierr);
622552f7358SJed Brown   ierr = MatSetType(J, MATSEQDENSE);CHKERRQ(ierr);
623552f7358SJed Brown   ierr = MatSetUp(J);CHKERRQ(ierr);
6240298fd71SBarry Smith   ierr = SNESSetFunction(snes, r, HexMap_Private, NULL);CHKERRQ(ierr);
6250298fd71SBarry Smith   ierr = SNESSetJacobian(snes, J, J, HexJacobian_Private, NULL);CHKERRQ(ierr);
626552f7358SJed Brown   ierr = SNESGetKSP(snes, &ksp);CHKERRQ(ierr);
627552f7358SJed Brown   ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr);
628552f7358SJed Brown   ierr = PCSetType(pc, PCLU);CHKERRQ(ierr);
629552f7358SJed Brown   ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
630552f7358SJed Brown 
631552f7358SJed Brown   ierr = VecGetArray(ctx->coords, &coords);CHKERRQ(ierr);
632552f7358SJed Brown   ierr = VecGetArray(v, &a);CHKERRQ(ierr);
633552f7358SJed Brown   for (p = 0; p < ctx->n; ++p) {
634a1e44745SMatthew G. Knepley     PetscScalar *x = NULL, *vertices = NULL;
635552f7358SJed Brown     PetscScalar *xi;
636cb313848SJed Brown     PetscReal    xir[3];
637552f7358SJed Brown     PetscInt     c = ctx->cells[p], comp, coordSize, xSize;
638552f7358SJed Brown 
639552f7358SJed Brown     /* Can make this do all points at once */
6400298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6410adebc6cSBarry Smith     if (8*3 != coordSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", coordSize, 8*3);
6420298fd71SBarry Smith     ierr = DMPlexVecGetClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
6430adebc6cSBarry Smith     if (8*ctx->dof != xSize) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid closure size %d should be %d", xSize, 8*ctx->dof);
6440298fd71SBarry Smith     ierr   = SNESSetFunction(snes, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
6450298fd71SBarry Smith     ierr   = SNESSetJacobian(snes, NULL, NULL, NULL, (void*) vertices);CHKERRQ(ierr);
646552f7358SJed Brown     ierr   = VecGetArray(real, &xi);CHKERRQ(ierr);
647552f7358SJed Brown     xi[0]  = coords[p*ctx->dim+0];
648552f7358SJed Brown     xi[1]  = coords[p*ctx->dim+1];
649552f7358SJed Brown     xi[2]  = coords[p*ctx->dim+2];
650552f7358SJed Brown     ierr   = VecRestoreArray(real, &xi);CHKERRQ(ierr);
651552f7358SJed Brown     ierr   = SNESSolve(snes, real, ref);CHKERRQ(ierr);
652552f7358SJed Brown     ierr   = VecGetArray(ref, &xi);CHKERRQ(ierr);
653cb313848SJed Brown     xir[0] = PetscRealPart(xi[0]);
654cb313848SJed Brown     xir[1] = PetscRealPart(xi[1]);
655cb313848SJed Brown     xir[2] = PetscRealPart(xi[2]);
656552f7358SJed Brown     for (comp = 0; comp < ctx->dof; ++comp) {
657552f7358SJed Brown       a[p*ctx->dof+comp] =
658cb313848SJed Brown         x[0*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*(1-xir[2]) +
6597a1931ceSMatthew G. Knepley         x[3*ctx->dof+comp]*    xir[0]*(1-xir[1])*(1-xir[2]) +
660cb313848SJed Brown         x[2*ctx->dof+comp]*    xir[0]*    xir[1]*(1-xir[2]) +
6617a1931ceSMatthew G. Knepley         x[1*ctx->dof+comp]*(1-xir[0])*    xir[1]*(1-xir[2]) +
662cb313848SJed Brown         x[4*ctx->dof+comp]*(1-xir[0])*(1-xir[1])*   xir[2] +
663cb313848SJed Brown         x[5*ctx->dof+comp]*    xir[0]*(1-xir[1])*   xir[2] +
664cb313848SJed Brown         x[6*ctx->dof+comp]*    xir[0]*    xir[1]*   xir[2] +
665cb313848SJed Brown         x[7*ctx->dof+comp]*(1-xir[0])*    xir[1]*   xir[2];
666552f7358SJed Brown     }
667552f7358SJed Brown     ierr = VecRestoreArray(ref, &xi);CHKERRQ(ierr);
6680298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dmCoord, NULL, coordsLocal, c, &coordSize, &vertices);CHKERRQ(ierr);
6690298fd71SBarry Smith     ierr = DMPlexVecRestoreClosure(dm, NULL, xLocal, c, &xSize, &x);CHKERRQ(ierr);
670552f7358SJed Brown   }
671552f7358SJed Brown   ierr = VecRestoreArray(v, &a);CHKERRQ(ierr);
672552f7358SJed Brown   ierr = VecRestoreArray(ctx->coords, &coords);CHKERRQ(ierr);
673552f7358SJed Brown 
674552f7358SJed Brown   ierr = SNESDestroy(&snes);CHKERRQ(ierr);
675552f7358SJed Brown   ierr = VecDestroy(&r);CHKERRQ(ierr);
676552f7358SJed Brown   ierr = VecDestroy(&ref);CHKERRQ(ierr);
677552f7358SJed Brown   ierr = VecDestroy(&real);CHKERRQ(ierr);
678552f7358SJed Brown   ierr = MatDestroy(&J);CHKERRQ(ierr);
679552f7358SJed Brown   PetscFunctionReturn(0);
680552f7358SJed Brown }
681552f7358SJed Brown 
682552f7358SJed Brown #undef __FUNCT__
683552f7358SJed Brown #define __FUNCT__ "DMInterpolationEvaluate"
684552f7358SJed Brown /*
685552f7358SJed Brown   Input Parameters:
686552f7358SJed Brown + ctx - The DMInterpolationInfo context
687552f7358SJed Brown . dm  - The DM
688552f7358SJed Brown - x   - The local vector containing the field to be interpolated
689552f7358SJed Brown 
690552f7358SJed Brown   Output Parameters:
691552f7358SJed Brown . v   - The vector containing the interpolated values
692552f7358SJed Brown */
6930adebc6cSBarry Smith PetscErrorCode DMInterpolationEvaluate(DMInterpolationInfo ctx, DM dm, Vec x, Vec v)
6940adebc6cSBarry Smith {
695552f7358SJed Brown   PetscInt       dim, coneSize, n;
696552f7358SJed Brown   PetscErrorCode ierr;
697552f7358SJed Brown 
698552f7358SJed Brown   PetscFunctionBegin;
699552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
700552f7358SJed Brown   PetscValidHeaderSpecific(x, VEC_CLASSID, 3);
701552f7358SJed Brown   PetscValidHeaderSpecific(v, VEC_CLASSID, 4);
702552f7358SJed Brown   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
7030adebc6cSBarry Smith   if (n != ctx->n*ctx->dof) SETERRQ2(ctx->comm, PETSC_ERR_ARG_SIZ, "Invalid input vector size %d should be %d", n, ctx->n*ctx->dof);
704552f7358SJed Brown   if (n) {
705c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
706552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, ctx->cells[0], &coneSize);CHKERRQ(ierr);
707552f7358SJed Brown     if (dim == 2) {
708552f7358SJed Brown       if (coneSize == 3) {
7097a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Triangle_Private(ctx, dm, x, v);CHKERRQ(ierr);
710552f7358SJed Brown       } else if (coneSize == 4) {
711552f7358SJed Brown         ierr = DMInterpolate_Quad_Private(ctx, dm, x, v);CHKERRQ(ierr);
7120adebc6cSBarry Smith       } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
713552f7358SJed Brown     } else if (dim == 3) {
714552f7358SJed Brown       if (coneSize == 4) {
7157a1931ceSMatthew G. Knepley         ierr = DMInterpolate_Tetrahedron_Private(ctx, dm, x, v);CHKERRQ(ierr);
716552f7358SJed Brown       } else {
717552f7358SJed Brown         ierr = DMInterpolate_Hex_Private(ctx, dm, x, v);CHKERRQ(ierr);
718552f7358SJed Brown       }
7190adebc6cSBarry Smith     } else SETERRQ1(ctx->comm, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported dimension %d for point interpolation", dim);
720552f7358SJed Brown   }
721552f7358SJed Brown   PetscFunctionReturn(0);
722552f7358SJed Brown }
723552f7358SJed Brown 
724552f7358SJed Brown #undef __FUNCT__
725552f7358SJed Brown #define __FUNCT__ "DMInterpolationDestroy"
7260adebc6cSBarry Smith PetscErrorCode DMInterpolationDestroy(DMInterpolationInfo *ctx)
7270adebc6cSBarry Smith {
728552f7358SJed Brown   PetscErrorCode ierr;
729552f7358SJed Brown 
730552f7358SJed Brown   PetscFunctionBegin;
731552f7358SJed Brown   PetscValidPointer(ctx, 2);
732552f7358SJed Brown   ierr = VecDestroy(&(*ctx)->coords);CHKERRQ(ierr);
733552f7358SJed Brown   ierr = PetscFree((*ctx)->points);CHKERRQ(ierr);
734552f7358SJed Brown   ierr = PetscFree((*ctx)->cells);CHKERRQ(ierr);
735552f7358SJed Brown   ierr = PetscFree(*ctx);CHKERRQ(ierr);
7360298fd71SBarry Smith   *ctx = NULL;
737552f7358SJed Brown   PetscFunctionReturn(0);
738552f7358SJed Brown }
739cc0c4584SMatthew G. Knepley 
740cc0c4584SMatthew G. Knepley #undef __FUNCT__
741cc0c4584SMatthew G. Knepley #define __FUNCT__ "SNESMonitorFields"
742cc0c4584SMatthew G. Knepley /*@C
743cc0c4584SMatthew G. Knepley   SNESMonitorFields - Monitors the residual for each field separately
744cc0c4584SMatthew G. Knepley 
745cc0c4584SMatthew G. Knepley   Collective on SNES
746cc0c4584SMatthew G. Knepley 
747cc0c4584SMatthew G. Knepley   Input Parameters:
748cc0c4584SMatthew G. Knepley + snes   - the SNES context
749cc0c4584SMatthew G. Knepley . its    - iteration number
750cc0c4584SMatthew G. Knepley . fgnorm - 2-norm of residual
751cc0c4584SMatthew G. Knepley - dummy  - unused context
752cc0c4584SMatthew G. Knepley 
753cc0c4584SMatthew G. Knepley   Notes:
754cc0c4584SMatthew G. Knepley   This routine prints the residual norm at each iteration.
755cc0c4584SMatthew G. Knepley 
756cc0c4584SMatthew G. Knepley   Level: intermediate
757cc0c4584SMatthew G. Knepley 
758cc0c4584SMatthew G. Knepley .keywords: SNES, nonlinear, default, monitor, norm
759cc0c4584SMatthew G. Knepley .seealso: SNESMonitorSet(), SNESMonitorDefault()
760cc0c4584SMatthew G. Knepley @*/
761cc0c4584SMatthew G. Knepley PetscErrorCode SNESMonitorFields(SNES snes, PetscInt its, PetscReal fgnorm, void *dummy)
762cc0c4584SMatthew G. Knepley {
763cc0c4584SMatthew G. Knepley   PetscViewer        viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) snes));
764cc0c4584SMatthew G. Knepley   Vec                res;
765cc0c4584SMatthew G. Knepley   DM                 dm;
766cc0c4584SMatthew G. Knepley   PetscSection       s;
767cc0c4584SMatthew G. Knepley   const PetscScalar *r;
768cc0c4584SMatthew G. Knepley   PetscReal         *lnorms, *norms;
769cc0c4584SMatthew G. Knepley   PetscInt           numFields, f, pStart, pEnd, p;
770cc0c4584SMatthew G. Knepley   PetscErrorCode     ierr;
771cc0c4584SMatthew G. Knepley 
772cc0c4584SMatthew G. Knepley   PetscFunctionBegin;
773cc0c4584SMatthew G. Knepley   ierr = SNESGetFunction(snes, &res, 0, 0);CHKERRQ(ierr);
774cc0c4584SMatthew G. Knepley   ierr = SNESGetDM(snes, &dm);CHKERRQ(ierr);
775cc0c4584SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
776cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &numFields);CHKERRQ(ierr);
777cc0c4584SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
778cc0c4584SMatthew G. Knepley   ierr = PetscCalloc2(numFields, &lnorms, numFields, &norms);CHKERRQ(ierr);
779cc0c4584SMatthew G. Knepley   ierr = VecGetArrayRead(res, &r);CHKERRQ(ierr);
780cc0c4584SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
781cc0c4584SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
782cc0c4584SMatthew G. Knepley       PetscInt fdof, foff, d;
783cc0c4584SMatthew G. Knepley 
784cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &fdof);CHKERRQ(ierr);
785cc0c4584SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(s, p, f, &foff);CHKERRQ(ierr);
786cc0c4584SMatthew G. Knepley       for (d = 0; d < fdof; ++d) lnorms[f] += PetscRealPart(PetscSqr(r[foff+d]));
787cc0c4584SMatthew G. Knepley     }
788cc0c4584SMatthew G. Knepley   }
789cc0c4584SMatthew G. Knepley   ierr = VecRestoreArrayRead(res, &r);CHKERRQ(ierr);
790cc0c4584SMatthew G. Knepley   ierr = MPI_Allreduce(lnorms, norms, numFields, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
791cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIAddTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
792cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "%3D SNES Function norm %14.12e [", its, (double) fgnorm);CHKERRQ(ierr);
793cc0c4584SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
794cc0c4584SMatthew G. Knepley     if (f > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
795cc0c4584SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "%14.12e", (double) PetscSqrtReal(norms[f]));CHKERRQ(ierr);
796cc0c4584SMatthew G. Knepley   }
797cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIIPrintf(viewer, "]\n");CHKERRQ(ierr);
798cc0c4584SMatthew G. Knepley   ierr = PetscViewerASCIISubtractTab(viewer, ((PetscObject) snes)->tablevel);CHKERRQ(ierr);
799cc0c4584SMatthew G. Knepley   ierr = PetscFree2(lnorms, norms);CHKERRQ(ierr);
800cc0c4584SMatthew G. Knepley   PetscFunctionReturn(0);
801cc0c4584SMatthew G. Knepley }
802*24cdb843SMatthew G. Knepley 
803*24cdb843SMatthew G. Knepley /********************* Residual Computation **************************/
804*24cdb843SMatthew G. Knepley 
805*24cdb843SMatthew G. Knepley #undef __FUNCT__
806*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidual_Internal"
807*24cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
808*24cdb843SMatthew G. Knepley {
809*24cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
810*24cdb843SMatthew G. Knepley 
811*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
812*24cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
813*24cdb843SMatthew G. Knepley   /* FEM+FVM */
814*24cdb843SMatthew G. Knepley   /* Get sizes from dm and dmAux */
815*24cdb843SMatthew G. Knepley   /* ONCE: Compute geometric data (includes gradient), stash in mesh */
816*24cdb843SMatthew G. Knepley   /*   Use named vectors */
817*24cdb843SMatthew G. Knepley   /* Limit cell gradients */
818*24cdb843SMatthew G. Knepley   /* Handle boundary values */
819*24cdb843SMatthew G. Knepley   /* Loop over domain */
820*24cdb843SMatthew G. Knepley   /*   Extract geometry and coefficients */
821*24cdb843SMatthew G. Knepley   /* Loop over fields */
822*24cdb843SMatthew G. Knepley   /*   Riemann solve over faces             (need fields at face centroids) */
823*24cdb843SMatthew G. Knepley   /*   Integrate FE residual to get elemVec (need fields at quadrature points) */
824*24cdb843SMatthew G. Knepley   /* Loop over domain */
825*24cdb843SMatthew G. Knepley   /*   Add elemVec to locX */
826*24cdb843SMatthew G. Knepley   /*   Accumulate fluxes to cells */
827*24cdb843SMatthew G. Knepley 
828*24cdb843SMatthew G. Knepley   /* FEM */
829*24cdb843SMatthew G. Knepley   /* Get sizes from dm and dmAux */
830*24cdb843SMatthew G. Knepley   /* Handle boundary values */
831*24cdb843SMatthew G. Knepley   /* Loop over domain */
832*24cdb843SMatthew G. Knepley   /*   Calculate geometry */
833*24cdb843SMatthew G. Knepley   /*   Extract coefficients */
834*24cdb843SMatthew G. Knepley   /* Loop over fields */
835*24cdb843SMatthew G. Knepley   /*   Set tiling for FE*/
836*24cdb843SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
837*24cdb843SMatthew G. Knepley   /*     Loop over subdomain */
838*24cdb843SMatthew G. Knepley   /*       Loop over quad points */
839*24cdb843SMatthew G. Knepley   /*         Transform coords to real space */
840*24cdb843SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
841*24cdb843SMatthew G. Knepley   /*         Evaluate residual at point */
842*24cdb843SMatthew G. Knepley   /*         Transform residual to real space */
843*24cdb843SMatthew G. Knepley   /*       Add residual to elemVec */
844*24cdb843SMatthew G. Knepley   /* Loop over domain */
845*24cdb843SMatthew G. Knepley   /*   Add elemVec to locX */
846*24cdb843SMatthew G. Knepley 
847*24cdb843SMatthew G. Knepley   /* FVM */
848*24cdb843SMatthew G. Knepley   /* Compute geometric data */
849*24cdb843SMatthew G. Knepley   /* If using gradients */
850*24cdb843SMatthew G. Knepley   /*   Compute gradient data */
851*24cdb843SMatthew G. Knepley   /*   Loop over domain faces */
852*24cdb843SMatthew G. Knepley   /*     Count computational faces */
853*24cdb843SMatthew G. Knepley   /*     Reconstruct cell gradient */
854*24cdb843SMatthew G. Knepley   /*   Loop over domain cells */
855*24cdb843SMatthew G. Knepley   /*     Limit cell gradients */
856*24cdb843SMatthew G. Knepley   /* Handle boundary values */
857*24cdb843SMatthew G. Knepley   /* Loop over domain faces */
858*24cdb843SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
859*24cdb843SMatthew G. Knepley   /* Riemann solve over faces */
860*24cdb843SMatthew G. Knepley   /* Loop over domain faces */
861*24cdb843SMatthew G. Knepley   /*   Accumulate fluxes to cells */
862*24cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
863*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
864*24cdb843SMatthew G. Knepley }
865*24cdb843SMatthew G. Knepley 
866*24cdb843SMatthew G. Knepley #undef __FUNCT__
867*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Internal"
868*24cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeResidualFEM_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
869*24cdb843SMatthew G. Knepley {
870*24cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
871*24cdb843SMatthew G. Knepley   const char       *name  = "Residual";
872*24cdb843SMatthew G. Knepley   DM                dmAux;
873*24cdb843SMatthew G. Knepley   DMLabel           depth;
874*24cdb843SMatthew G. Knepley   Vec               A;
875*24cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
876*24cdb843SMatthew G. Knepley   PetscQuadrature   q;
877*24cdb843SMatthew G. Knepley   PetscCellGeometry geom;
878*24cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
879*24cdb843SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
880*24cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *u, *u_t, *a = NULL;
881*24cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c, numBd, bd;
882*24cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux;
883*24cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
884*24cdb843SMatthew G. Knepley 
885*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
886*24cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
887*24cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
888*24cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
889*24cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
890*24cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
891*24cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
892*24cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
893*24cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
894*24cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
895*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
896*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
897*24cdb843SMatthew G. Knepley   if (dmAux) {
898*24cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
899*24cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
900*24cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
901*24cdb843SMatthew G. Knepley   }
902*24cdb843SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
903*24cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
904*24cdb843SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
905*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
906*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
907*24cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
908*24cdb843SMatthew G. Knepley     PetscInt     i;
909*24cdb843SMatthew G. Knepley 
910*24cdb843SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
911*24cdb843SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
912*24cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
913*24cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
914*24cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
915*24cdb843SMatthew G. Knepley     if (X_t) {
916*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
917*24cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
918*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
919*24cdb843SMatthew G. Knepley     }
920*24cdb843SMatthew G. Knepley     if (dmAux) {
921*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
922*24cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
923*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
924*24cdb843SMatthew G. Knepley     }
925*24cdb843SMatthew G. Knepley   }
926*24cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
927*24cdb843SMatthew G. Knepley     PetscFE  fe;
928*24cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
929*24cdb843SMatthew G. Knepley     /* Conforming batches */
930*24cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
931*24cdb843SMatthew G. Knepley     /* Remainder */
932*24cdb843SMatthew G. Knepley     PetscInt Nr, offset;
933*24cdb843SMatthew G. Knepley 
934*24cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
935*24cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
936*24cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
937*24cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
938*24cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
939*24cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
940*24cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
941*24cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
942*24cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
943*24cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
944*24cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
945*24cdb843SMatthew G. Knepley     offset    = numCells - Nr;
946*24cdb843SMatthew G. Knepley     geom.v0   = v0;
947*24cdb843SMatthew G. Knepley     geom.J    = J;
948*24cdb843SMatthew G. Knepley     geom.invJ = invJ;
949*24cdb843SMatthew G. Knepley     geom.detJ = detJ;
950*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
951*24cdb843SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
952*24cdb843SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
953*24cdb843SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
954*24cdb843SMatthew G. Knepley     geom.detJ = &detJ[offset];
955*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
956*24cdb843SMatthew G. Knepley   }
957*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
958*24cdb843SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(c, name, totDim, &elemVec[c*totDim]);CHKERRQ(ierr);}
959*24cdb843SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
960*24cdb843SMatthew G. Knepley   }
961*24cdb843SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
962*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
963*24cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
964*24cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
965*24cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
966*24cdb843SMatthew G. Knepley     const char     *bdLabel;
967*24cdb843SMatthew G. Knepley     DMLabel         label;
968*24cdb843SMatthew G. Knepley     IS              pointIS;
969*24cdb843SMatthew G. Knepley     const PetscInt *points;
970*24cdb843SMatthew G. Knepley     const PetscInt *values;
971*24cdb843SMatthew G. Knepley     PetscReal      *n;
972*24cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
973*24cdb843SMatthew G. Knepley     PetscBool       isEssential;
974*24cdb843SMatthew G. Knepley 
975*24cdb843SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
976*24cdb843SMatthew G. Knepley     if (isEssential) continue;
977*24cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
978*24cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
979*24cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
980*24cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
981*24cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
982*24cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
983*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
984*24cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
985*24cdb843SMatthew G. Knepley     }
986*24cdb843SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd,&elemVec);CHKERRQ(ierr);
987*24cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
988*24cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
989*24cdb843SMatthew G. Knepley       const PetscInt point = points[p];
990*24cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
991*24cdb843SMatthew G. Knepley       PetscInt       i;
992*24cdb843SMatthew G. Knepley 
993*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
994*24cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
995*24cdb843SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
996*24cdb843SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
997*24cdb843SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
998*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
999*24cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1000*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1001*24cdb843SMatthew G. Knepley       if (X_t) {
1002*24cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1003*24cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1004*24cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1005*24cdb843SMatthew G. Knepley       }
1006*24cdb843SMatthew G. Knepley       ++f;
1007*24cdb843SMatthew G. Knepley     }
1008*24cdb843SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1009*24cdb843SMatthew G. Knepley       PetscFE  fe;
1010*24cdb843SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1011*24cdb843SMatthew G. Knepley       /* Conforming batches */
1012*24cdb843SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1013*24cdb843SMatthew G. Knepley       /* Remainder */
1014*24cdb843SMatthew G. Knepley       PetscInt Nr, offset;
1015*24cdb843SMatthew G. Knepley 
1016*24cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1017*24cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1018*24cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1019*24cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1020*24cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1021*24cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
1022*24cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
1023*24cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1024*24cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
1025*24cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
1026*24cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
1027*24cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
1028*24cdb843SMatthew G. Knepley       geom.v0   = v0;
1029*24cdb843SMatthew G. Knepley       geom.n    = n;
1030*24cdb843SMatthew G. Knepley       geom.J    = J;
1031*24cdb843SMatthew G. Knepley       geom.invJ = invJ;
1032*24cdb843SMatthew G. Knepley       geom.detJ = detJ;
1033*24cdb843SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Ne, geom, u, u_t, NULL, NULL, elemVec);CHKERRQ(ierr);
1034*24cdb843SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1035*24cdb843SMatthew G. Knepley       geom.n    = &n[offset*dim];
1036*24cdb843SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1037*24cdb843SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1038*24cdb843SMatthew G. Knepley       geom.detJ = &detJ[offset];
1039*24cdb843SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(fe, prob, f, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemVec[offset*totDimBd]);CHKERRQ(ierr);
1040*24cdb843SMatthew G. Knepley     }
1041*24cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1042*24cdb843SMatthew G. Knepley       const PetscInt point = points[p];
1043*24cdb843SMatthew G. Knepley 
1044*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
1045*24cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1046*24cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDimBd, &elemVec[f*totDimBd]);CHKERRQ(ierr);}
1047*24cdb843SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, NULL, F, point, &elemVec[f*totDimBd], ADD_VALUES);CHKERRQ(ierr);
1048*24cdb843SMatthew G. Knepley       ++f;
1049*24cdb843SMatthew G. Knepley     }
1050*24cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1051*24cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1052*24cdb843SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemVec);CHKERRQ(ierr);
1053*24cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1054*24cdb843SMatthew G. Knepley   }
1055*24cdb843SMatthew G. Knepley   if (mesh->printFEM) {ierr = DMPrintLocalVec(dm, name, mesh->printTol, F);CHKERRQ(ierr);}
1056*24cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
1057*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
1058*24cdb843SMatthew G. Knepley }
1059*24cdb843SMatthew G. Knepley 
1060*24cdb843SMatthew G. Knepley #undef __FUNCT__
1061*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeResidualFEM_Check_Internal"
1062*24cdb843SMatthew G. Knepley static PetscErrorCode DMPlexComputeResidualFEM_Check_Internal(DM dm, Vec X, Vec X_t, Vec F, void *user)
1063*24cdb843SMatthew G. Knepley {
1064*24cdb843SMatthew G. Knepley   DM                dmCh, dmAux;
1065*24cdb843SMatthew G. Knepley   Vec               A;
1066*24cdb843SMatthew G. Knepley   PetscDS           prob, probCh, probAux = NULL;
1067*24cdb843SMatthew G. Knepley   PetscQuadrature   q;
1068*24cdb843SMatthew G. Knepley   PetscCellGeometry geom;
1069*24cdb843SMatthew G. Knepley   PetscSection      section, sectionAux;
1070*24cdb843SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
1071*24cdb843SMatthew G. Knepley   PetscScalar      *elemVec, *elemVecCh, *u, *u_t, *a = NULL;
1072*24cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, numCells, cStart, cEnd, c;
1073*24cdb843SMatthew G. Knepley   PetscInt          totDim, totDimAux, diffCell = 0;
1074*24cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
1075*24cdb843SMatthew G. Knepley 
1076*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
1077*24cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1078*24cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1079*24cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1080*24cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1081*24cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1082*24cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1083*24cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
1084*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", (PetscObject *) &dmCh);CHKERRQ(ierr);
1085*24cdb843SMatthew G. Knepley   ierr = DMGetDS(dmCh, &probCh);CHKERRQ(ierr);
1086*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1087*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
1088*24cdb843SMatthew G. Knepley   if (dmAux) {
1089*24cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
1090*24cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1091*24cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1092*24cdb843SMatthew G. Knepley   }
1093*24cdb843SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
1094*24cdb843SMatthew G. Knepley   ierr = VecSet(F, 0.0);CHKERRQ(ierr);
1095*24cdb843SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim,&elemVec);CHKERRQ(ierr);
1096*24cdb843SMatthew G. Knepley   ierr = PetscMalloc1(numCells*totDim,&elemVecCh);CHKERRQ(ierr);
1097*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1098*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1099*24cdb843SMatthew G. Knepley     PetscScalar *x = NULL, *x_t = NULL;
1100*24cdb843SMatthew G. Knepley     PetscInt     i;
1101*24cdb843SMatthew G. Knepley 
1102*24cdb843SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1103*24cdb843SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1104*24cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1105*24cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1106*24cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1107*24cdb843SMatthew G. Knepley     if (X_t) {
1108*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1109*24cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
1110*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1111*24cdb843SMatthew G. Knepley     }
1112*24cdb843SMatthew G. Knepley     if (dmAux) {
1113*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1114*24cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1115*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1116*24cdb843SMatthew G. Knepley     }
1117*24cdb843SMatthew G. Knepley   }
1118*24cdb843SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1119*24cdb843SMatthew G. Knepley     PetscFE  fe, feCh;
1120*24cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1121*24cdb843SMatthew G. Knepley     /* Conforming batches */
1122*24cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1123*24cdb843SMatthew G. Knepley     /* Remainder */
1124*24cdb843SMatthew G. Knepley     PetscInt Nr, offset;
1125*24cdb843SMatthew G. Knepley 
1126*24cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
1127*24cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(probCh, f, (PetscObject *) &feCh);CHKERRQ(ierr);
1128*24cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
1129*24cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1130*24cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1131*24cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1132*24cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1133*24cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
1134*24cdb843SMatthew G. Knepley     ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1135*24cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1136*24cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1137*24cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1138*24cdb843SMatthew G. Knepley     offset    = numCells - Nr;
1139*24cdb843SMatthew G. Knepley     geom.v0   = v0;
1140*24cdb843SMatthew G. Knepley     geom.J    = J;
1141*24cdb843SMatthew G. Knepley     geom.invJ = invJ;
1142*24cdb843SMatthew G. Knepley     geom.detJ = detJ;
1143*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, geom, u, u_t, probAux, a, elemVec);CHKERRQ(ierr);
1144*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Ne, geom, u, u_t, probAux, a, elemVecCh);CHKERRQ(ierr);
1145*24cdb843SMatthew G. Knepley     geom.v0   = &v0[offset*dim];
1146*24cdb843SMatthew G. Knepley     geom.J    = &J[offset*dim*dim];
1147*24cdb843SMatthew G. Knepley     geom.invJ = &invJ[offset*dim*dim];
1148*24cdb843SMatthew G. Knepley     geom.detJ = &detJ[offset];
1149*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVec[offset*totDim]);CHKERRQ(ierr);
1150*24cdb843SMatthew G. Knepley     ierr = PetscFEIntegrateResidual(feCh, prob, f, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemVecCh[offset*totDim]);CHKERRQ(ierr);
1151*24cdb843SMatthew G. Knepley   }
1152*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1153*24cdb843SMatthew G. Knepley     PetscBool diff = PETSC_FALSE;
1154*24cdb843SMatthew G. Knepley     PetscInt  d;
1155*24cdb843SMatthew G. Knepley 
1156*24cdb843SMatthew 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;}
1157*24cdb843SMatthew G. Knepley     if (diff) {
1158*24cdb843SMatthew G. Knepley       ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Different cell %d\n", c);CHKERRQ(ierr);
1159*24cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Residual", totDim, &elemVec[c*totDim]);CHKERRQ(ierr);
1160*24cdb843SMatthew G. Knepley       ierr = DMPrintCellVector(c, "Check Residual", totDim, &elemVecCh[c*totDim]);CHKERRQ(ierr);
1161*24cdb843SMatthew G. Knepley       ++diffCell;
1162*24cdb843SMatthew G. Knepley     }
1163*24cdb843SMatthew G. Knepley     if (diffCell > 9) break;
1164*24cdb843SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, F, c, &elemVec[c*totDim], ADD_VALUES);CHKERRQ(ierr);
1165*24cdb843SMatthew G. Knepley   }
1166*24cdb843SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemVec);CHKERRQ(ierr);
1167*24cdb843SMatthew G. Knepley   ierr = PetscFree(elemVecCh);CHKERRQ(ierr);
1168*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1169*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
1170*24cdb843SMatthew G. Knepley }
1171*24cdb843SMatthew G. Knepley 
1172*24cdb843SMatthew G. Knepley #undef __FUNCT__
1173*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeResidualFEM"
1174*24cdb843SMatthew G. Knepley /*@
1175*24cdb843SMatthew G. Knepley   DMPlexSNESComputeResidualFEM - Form the local residual F from the local input X using pointwise functions specified by the user
1176*24cdb843SMatthew G. Knepley 
1177*24cdb843SMatthew G. Knepley   Input Parameters:
1178*24cdb843SMatthew G. Knepley + dm - The mesh
1179*24cdb843SMatthew G. Knepley . X  - Local solution
1180*24cdb843SMatthew G. Knepley - user - The user context
1181*24cdb843SMatthew G. Knepley 
1182*24cdb843SMatthew G. Knepley   Output Parameter:
1183*24cdb843SMatthew G. Knepley . F  - Local output vector
1184*24cdb843SMatthew G. Knepley 
1185*24cdb843SMatthew G. Knepley   Level: developer
1186*24cdb843SMatthew G. Knepley 
1187*24cdb843SMatthew G. Knepley .seealso: DMPlexComputeJacobianActionFEM()
1188*24cdb843SMatthew G. Knepley @*/
1189*24cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeResidualFEM(DM dm, Vec X, Vec F, void *user)
1190*24cdb843SMatthew G. Knepley {
1191*24cdb843SMatthew G. Knepley   PetscObject    check;
1192*24cdb843SMatthew G. Knepley   PetscErrorCode ierr;
1193*24cdb843SMatthew G. Knepley 
1194*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
1195*24cdb843SMatthew G. Knepley   /* The dmCh is used to check two mathematically equivalent discretizations for computational equivalence */
1196*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmCh", &check);CHKERRQ(ierr);
1197*24cdb843SMatthew G. Knepley   if (check) {ierr = DMPlexComputeResidualFEM_Check_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
1198*24cdb843SMatthew G. Knepley   else       {ierr = DMPlexComputeResidualFEM_Internal(dm, X, NULL, F, user);CHKERRQ(ierr);}
1199*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
1200*24cdb843SMatthew G. Knepley }
1201*24cdb843SMatthew G. Knepley 
1202*24cdb843SMatthew G. Knepley #undef __FUNCT__
1203*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexComputeJacobianFEM_Internal"
1204*24cdb843SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobianFEM_Internal(DM dm, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
1205*24cdb843SMatthew G. Knepley {
1206*24cdb843SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dm->data;
1207*24cdb843SMatthew G. Knepley   const char       *name  = "Jacobian";
1208*24cdb843SMatthew G. Knepley   DM                dmAux;
1209*24cdb843SMatthew G. Knepley   DMLabel           depth;
1210*24cdb843SMatthew G. Knepley   Vec               A;
1211*24cdb843SMatthew G. Knepley   PetscDS           prob, probAux = NULL;
1212*24cdb843SMatthew G. Knepley   PetscQuadrature   quad;
1213*24cdb843SMatthew G. Knepley   PetscCellGeometry geom;
1214*24cdb843SMatthew G. Knepley   PetscSection      section, globalSection, sectionAux;
1215*24cdb843SMatthew G. Knepley   PetscReal        *v0, *J, *invJ, *detJ;
1216*24cdb843SMatthew G. Knepley   PetscScalar      *elemMat, *u, *u_t, *a = NULL;
1217*24cdb843SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, numCells, cStart, cEnd, c;
1218*24cdb843SMatthew G. Knepley   PetscInt          totDim, totDimBd, totDimAux, numBd, bd;
1219*24cdb843SMatthew G. Knepley   PetscBool         isShell;
1220*24cdb843SMatthew G. Knepley   PetscErrorCode    ierr;
1221*24cdb843SMatthew G. Knepley 
1222*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
1223*24cdb843SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1224*24cdb843SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1225*24cdb843SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1226*24cdb843SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);
1227*24cdb843SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1228*24cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1229*24cdb843SMatthew G. Knepley   ierr = PetscDSGetTotalBdDimension(prob, &totDimBd);CHKERRQ(ierr);
1230*24cdb843SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1231*24cdb843SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1232*24cdb843SMatthew G. Knepley   numCells = cEnd - cStart;
1233*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1234*24cdb843SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
1235*24cdb843SMatthew G. Knepley   if (dmAux) {
1236*24cdb843SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
1237*24cdb843SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1238*24cdb843SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1239*24cdb843SMatthew G. Knepley   }
1240*24cdb843SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValuesFEM(dm, X);CHKERRQ(ierr);
1241*24cdb843SMatthew G. Knepley   ierr = MatZeroEntries(JacP);CHKERRQ(ierr);
1242*24cdb843SMatthew G. Knepley   ierr = PetscMalloc7(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*dim,&v0,numCells*dim*dim,&J,numCells*dim*dim,&invJ,numCells,&detJ,numCells*totDim*totDim,&elemMat);CHKERRQ(ierr);
1243*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1244*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1245*24cdb843SMatthew G. Knepley     PetscScalar *x = NULL,  *x_t = NULL;
1246*24cdb843SMatthew G. Knepley     PetscInt     i;
1247*24cdb843SMatthew G. Knepley 
1248*24cdb843SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, &v0[c*dim], &J[c*dim*dim], &invJ[c*dim*dim], &detJ[c]);CHKERRQ(ierr);
1249*24cdb843SMatthew G. Knepley     if (detJ[c] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %d", detJ[c], c);
1250*24cdb843SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1251*24cdb843SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1252*24cdb843SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, c, NULL, &x);CHKERRQ(ierr);
1253*24cdb843SMatthew G. Knepley     if (X_t) {
1254*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1255*24cdb843SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
1256*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, c, NULL, &x_t);CHKERRQ(ierr);
1257*24cdb843SMatthew G. Knepley     }
1258*24cdb843SMatthew G. Knepley     if (dmAux) {
1259*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1260*24cdb843SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1261*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &x);CHKERRQ(ierr);
1262*24cdb843SMatthew G. Knepley     }
1263*24cdb843SMatthew G. Knepley   }
1264*24cdb843SMatthew G. Knepley   ierr = PetscMemzero(elemMat, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
1265*24cdb843SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
1266*24cdb843SMatthew G. Knepley     PetscFE  fe;
1267*24cdb843SMatthew G. Knepley     PetscInt numQuadPoints, Nb;
1268*24cdb843SMatthew G. Knepley     /* Conforming batches */
1269*24cdb843SMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1270*24cdb843SMatthew G. Knepley     /* Remainder */
1271*24cdb843SMatthew G. Knepley     PetscInt Nr, offset;
1272*24cdb843SMatthew G. Knepley 
1273*24cdb843SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
1274*24cdb843SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1275*24cdb843SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1276*24cdb843SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1277*24cdb843SMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1278*24cdb843SMatthew G. Knepley     blockSize = Nb*numQuadPoints;
1279*24cdb843SMatthew G. Knepley     batchSize = numBlocks * blockSize;
1280*24cdb843SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1281*24cdb843SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
1282*24cdb843SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
1283*24cdb843SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
1284*24cdb843SMatthew G. Knepley     offset    = numCells - Nr;
1285*24cdb843SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1286*24cdb843SMatthew G. Knepley       geom.v0   = v0;
1287*24cdb843SMatthew G. Knepley       geom.J    = J;
1288*24cdb843SMatthew G. Knepley       geom.invJ = invJ;
1289*24cdb843SMatthew G. Knepley       geom.detJ = detJ;
1290*24cdb843SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, probAux, a, elemMat);CHKERRQ(ierr);
1291*24cdb843SMatthew G. Knepley       geom.v0   = &v0[offset*dim];
1292*24cdb843SMatthew G. Knepley       geom.J    = &J[offset*dim*dim];
1293*24cdb843SMatthew G. Knepley       geom.invJ = &invJ[offset*dim*dim];
1294*24cdb843SMatthew G. Knepley       geom.detJ = &detJ[offset];
1295*24cdb843SMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
1296*24cdb843SMatthew G. Knepley     }
1297*24cdb843SMatthew G. Knepley   }
1298*24cdb843SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1299*24cdb843SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[c*totDim*totDim]);CHKERRQ(ierr);}
1300*24cdb843SMatthew G. Knepley     ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, c, &elemMat[c*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
1301*24cdb843SMatthew G. Knepley   }
1302*24cdb843SMatthew G. Knepley   ierr = PetscFree7(u,u_t,v0,J,invJ,detJ,elemMat);CHKERRQ(ierr);
1303*24cdb843SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1304*24cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1305*24cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
1306*24cdb843SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
1307*24cdb843SMatthew G. Knepley   ierr = DMPlexGetNumBoundary(dm, &numBd);CHKERRQ(ierr);
1308*24cdb843SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
1309*24cdb843SMatthew G. Knepley     const char     *bdLabel;
1310*24cdb843SMatthew G. Knepley     DMLabel         label;
1311*24cdb843SMatthew G. Knepley     IS              pointIS;
1312*24cdb843SMatthew G. Knepley     const PetscInt *points;
1313*24cdb843SMatthew G. Knepley     const PetscInt *values;
1314*24cdb843SMatthew G. Knepley     PetscReal      *n;
1315*24cdb843SMatthew G. Knepley     PetscInt        field, numValues, numPoints, p, dep, numFaces;
1316*24cdb843SMatthew G. Knepley     PetscBool       isEssential;
1317*24cdb843SMatthew G. Knepley 
1318*24cdb843SMatthew G. Knepley     ierr = DMPlexGetBoundary(dm, bd, &isEssential, NULL, &bdLabel, &field, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
1319*24cdb843SMatthew G. Knepley     if (isEssential) continue;
1320*24cdb843SMatthew G. Knepley     if (numValues != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Bug me and I will fix this");
1321*24cdb843SMatthew G. Knepley     ierr = DMPlexGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
1322*24cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, 1, &numPoints);CHKERRQ(ierr);
1323*24cdb843SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
1324*24cdb843SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1325*24cdb843SMatthew G. Knepley     for (p = 0, numFaces = 0; p < numPoints; ++p) {
1326*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1327*24cdb843SMatthew G. Knepley       if (dep == dim-1) ++numFaces;
1328*24cdb843SMatthew G. Knepley     }
1329*24cdb843SMatthew G. Knepley     ierr = PetscMalloc7(numFaces*totDimBd,&u,numFaces*dim,&v0,numFaces*dim,&n,numFaces*dim*dim,&J,numFaces*dim*dim,&invJ,numFaces,&detJ,numFaces*totDimBd*totDimBd,&elemMat);CHKERRQ(ierr);
1330*24cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscMalloc1(numFaces*totDimBd,&u_t);CHKERRQ(ierr);}
1331*24cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1332*24cdb843SMatthew G. Knepley       const PetscInt point = points[p];
1333*24cdb843SMatthew G. Knepley       PetscScalar   *x     = NULL;
1334*24cdb843SMatthew G. Knepley       PetscInt       i;
1335*24cdb843SMatthew G. Knepley 
1336*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, points[p], &dep);CHKERRQ(ierr);
1337*24cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1338*24cdb843SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, point, NULL, &v0[f*dim], &J[f*dim*dim], &invJ[f*dim*dim], &detJ[f]);CHKERRQ(ierr);
1339*24cdb843SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, point, NULL, NULL, &n[f*dim]);
1340*24cdb843SMatthew G. Knepley       if (detJ[f] <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for face %d", detJ[f], point);
1341*24cdb843SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1342*24cdb843SMatthew G. Knepley       for (i = 0; i < totDimBd; ++i) u[f*totDimBd+i] = x[i];
1343*24cdb843SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X, point, NULL, &x);CHKERRQ(ierr);
1344*24cdb843SMatthew G. Knepley       if (X_t) {
1345*24cdb843SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1346*24cdb843SMatthew G. Knepley         for (i = 0; i < totDimBd; ++i) u_t[f*totDimBd+i] = x[i];
1347*24cdb843SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, point, NULL, &x);CHKERRQ(ierr);
1348*24cdb843SMatthew G. Knepley       }
1349*24cdb843SMatthew G. Knepley       ++f;
1350*24cdb843SMatthew G. Knepley     }
1351*24cdb843SMatthew G. Knepley     ierr = PetscMemzero(elemMat, numFaces*totDimBd*totDimBd * sizeof(PetscScalar));CHKERRQ(ierr);
1352*24cdb843SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
1353*24cdb843SMatthew G. Knepley       PetscFE  fe;
1354*24cdb843SMatthew G. Knepley       PetscInt numQuadPoints, Nb;
1355*24cdb843SMatthew G. Knepley       /* Conforming batches */
1356*24cdb843SMatthew G. Knepley       PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
1357*24cdb843SMatthew G. Knepley       /* Remainder */
1358*24cdb843SMatthew G. Knepley       PetscInt Nr, offset;
1359*24cdb843SMatthew G. Knepley 
1360*24cdb843SMatthew G. Knepley       ierr = PetscDSGetBdDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
1361*24cdb843SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1362*24cdb843SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1363*24cdb843SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1364*24cdb843SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
1365*24cdb843SMatthew G. Knepley       blockSize = Nb*numQuadPoints;
1366*24cdb843SMatthew G. Knepley       batchSize = numBlocks * blockSize;
1367*24cdb843SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
1368*24cdb843SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
1369*24cdb843SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
1370*24cdb843SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
1371*24cdb843SMatthew G. Knepley       offset    = numFaces - Nr;
1372*24cdb843SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
1373*24cdb843SMatthew G. Knepley         geom.v0   = v0;
1374*24cdb843SMatthew G. Knepley         geom.n    = n;
1375*24cdb843SMatthew G. Knepley         geom.J    = J;
1376*24cdb843SMatthew G. Knepley         geom.invJ = invJ;
1377*24cdb843SMatthew G. Knepley         geom.detJ = detJ;
1378*24cdb843SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Ne, geom, u, u_t, NULL, NULL, elemMat);CHKERRQ(ierr);
1379*24cdb843SMatthew G. Knepley         geom.v0   = &v0[offset*dim];
1380*24cdb843SMatthew G. Knepley         geom.n    = &n[offset*dim];
1381*24cdb843SMatthew G. Knepley         geom.J    = &J[offset*dim*dim];
1382*24cdb843SMatthew G. Knepley         geom.invJ = &invJ[offset*dim*dim];
1383*24cdb843SMatthew G. Knepley         geom.detJ = &detJ[offset];
1384*24cdb843SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(fe, prob, fieldI, fieldJ, Nr, geom, &u[offset*totDimBd], u_t ? &u_t[offset*totDimBd] : NULL, NULL, NULL, &elemMat[offset*totDimBd*totDimBd]);CHKERRQ(ierr);
1385*24cdb843SMatthew G. Knepley       }
1386*24cdb843SMatthew G. Knepley     }
1387*24cdb843SMatthew G. Knepley     for (p = 0, f = 0; p < numPoints; ++p) {
1388*24cdb843SMatthew G. Knepley       const PetscInt point = points[p];
1389*24cdb843SMatthew G. Knepley 
1390*24cdb843SMatthew G. Knepley       ierr = DMLabelGetValue(depth, point, &dep);CHKERRQ(ierr);
1391*24cdb843SMatthew G. Knepley       if (dep != dim-1) continue;
1392*24cdb843SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDimBd, totDimBd, &elemMat[f*totDimBd*totDimBd]);CHKERRQ(ierr);}
1393*24cdb843SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, point, &elemMat[f*totDimBd*totDimBd], ADD_VALUES);CHKERRQ(ierr);
1394*24cdb843SMatthew G. Knepley       ++f;
1395*24cdb843SMatthew G. Knepley     }
1396*24cdb843SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1397*24cdb843SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1398*24cdb843SMatthew G. Knepley     ierr = PetscFree7(u,v0,n,J,invJ,detJ,elemMat);CHKERRQ(ierr);
1399*24cdb843SMatthew G. Knepley     if (X_t) {ierr = PetscFree(u_t);CHKERRQ(ierr);}
1400*24cdb843SMatthew G. Knepley   }
1401*24cdb843SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1402*24cdb843SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1403*24cdb843SMatthew G. Knepley   if (mesh->printFEM) {
1404*24cdb843SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1405*24cdb843SMatthew G. Knepley     ierr = MatChop(JacP, 1.0e-10);CHKERRQ(ierr);
1406*24cdb843SMatthew G. Knepley     ierr = MatView(JacP, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1407*24cdb843SMatthew G. Knepley   }
1408*24cdb843SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
1409*24cdb843SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac, MATSHELL, &isShell);CHKERRQ(ierr);
1410*24cdb843SMatthew G. Knepley   if (isShell) {
1411*24cdb843SMatthew G. Knepley     JacActionCtx *jctx;
1412*24cdb843SMatthew G. Knepley 
1413*24cdb843SMatthew G. Knepley     ierr = MatShellGetContext(Jac, &jctx);CHKERRQ(ierr);
1414*24cdb843SMatthew G. Knepley     ierr = VecCopy(X, jctx->u);CHKERRQ(ierr);
1415*24cdb843SMatthew G. Knepley   }
1416*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
1417*24cdb843SMatthew G. Knepley }
1418*24cdb843SMatthew G. Knepley 
1419*24cdb843SMatthew G. Knepley #undef __FUNCT__
1420*24cdb843SMatthew G. Knepley #define __FUNCT__ "DMPlexSNESComputeJacobianFEM"
1421*24cdb843SMatthew G. Knepley /*@
1422*24cdb843SMatthew G. Knepley   DMPlexSNESComputeJacobianFEM - Form the local portion of the Jacobian matrix J at the local solution X using pointwise functions specified by the user.
1423*24cdb843SMatthew G. Knepley 
1424*24cdb843SMatthew G. Knepley   Input Parameters:
1425*24cdb843SMatthew G. Knepley + dm - The mesh
1426*24cdb843SMatthew G. Knepley . X  - Local input vector
1427*24cdb843SMatthew G. Knepley - user - The user context
1428*24cdb843SMatthew G. Knepley 
1429*24cdb843SMatthew G. Knepley   Output Parameter:
1430*24cdb843SMatthew G. Knepley . Jac  - Jacobian matrix
1431*24cdb843SMatthew G. Knepley 
1432*24cdb843SMatthew G. Knepley   Note:
1433*24cdb843SMatthew G. Knepley   The first member of the user context must be an FEMContext.
1434*24cdb843SMatthew G. Knepley 
1435*24cdb843SMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
1436*24cdb843SMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
1437*24cdb843SMatthew G. Knepley 
1438*24cdb843SMatthew G. Knepley   Level: developer
1439*24cdb843SMatthew G. Knepley 
1440*24cdb843SMatthew G. Knepley .seealso: FormFunctionLocal()
1441*24cdb843SMatthew G. Knepley @*/
1442*24cdb843SMatthew G. Knepley PetscErrorCode DMPlexSNESComputeJacobianFEM(DM dm, Vec X, Mat Jac, Mat JacP,void *user)
1443*24cdb843SMatthew G. Knepley {
1444*24cdb843SMatthew G. Knepley   PetscErrorCode ierr;
1445*24cdb843SMatthew G. Knepley 
1446*24cdb843SMatthew G. Knepley   PetscFunctionBegin;
1447*24cdb843SMatthew G. Knepley   ierr = DMPlexComputeJacobianFEM_Internal(dm, X, NULL, Jac, JacP, user);CHKERRQ(ierr);
1448*24cdb843SMatthew G. Knepley   PetscFunctionReturn(0);
1449*24cdb843SMatthew G. Knepley }
1450