xref: /petsc/src/dm/field/impls/ds/dmfieldds.c (revision fa2f3a98544cffa8147e57031dd6780dd55215d2)
1 #include <petsc/private/dmfieldimpl.h> /*I "petscdmfield.h" I*/
2 #include <petsc/private/petscfeimpl.h> /*I "petscdmfield.h" I*/
3 #include <petscfe.h>
4 #include <petscdmplex.h>
5 #include <petscds.h>
6 
7 typedef struct _n_DMField_DS
8 {
9   PetscInt    fieldNum;
10   Vec         vec;
11   PetscInt    height;
12   PetscObject *disc;
13   PetscBool   multifieldVec;
14 }
15 DMField_DS;
16 
17 static PetscErrorCode DMFieldDestroy_DS(DMField field)
18 {
19   DMField_DS     *dsfield;
20   PetscInt       i;
21   PetscErrorCode ierr;
22 
23   PetscFunctionBegin;
24   dsfield = (DMField_DS *) field->data;
25   ierr = VecDestroy(&dsfield->vec);CHKERRQ(ierr);
26   for (i = 0; i < dsfield->height; i++) {
27     ierr = PetscObjectDereference(dsfield->disc[i]);CHKERRQ(ierr);
28   }
29   ierr = PetscFree(dsfield->disc);CHKERRQ(ierr);
30   ierr = PetscFree(dsfield);CHKERRQ(ierr);
31   PetscFunctionReturn(0);
32 }
33 
34 static PetscErrorCode DMFieldView_DS(DMField field,PetscViewer viewer)
35 {
36   DMField_DS     *dsfield = (DMField_DS *) field->data;
37   PetscBool      iascii;
38   PetscObject    disc;
39   PetscErrorCode ierr;
40 
41   PetscFunctionBegin;
42   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
43   disc = dsfield->disc[0];
44   if (iascii) {
45     PetscViewerASCIIPrintf(viewer, "PetscDS field %D\n",dsfield->fieldNum);CHKERRQ(ierr);
46     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
47     ierr = PetscObjectView(disc,viewer);CHKERRQ(ierr);
48     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
49   }
50   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
51   if (dsfield->multifieldVec) {
52     SETERRQ(PetscObjectComm((PetscObject)field),PETSC_ERR_SUP,"View of subfield not implemented yet");
53   } else {
54     ierr = VecView(dsfield->vec,viewer);CHKERRQ(ierr);
55   }
56   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
57   PetscFunctionReturn(0);
58 }
59 
60 static PetscErrorCode DMFieldDSGetHeightDisc(DMField field, PetscInt height, PetscObject *disc)
61 {
62   DMField_DS     *dsfield = (DMField_DS *) field->data;
63   PetscErrorCode ierr;
64 
65   PetscFunctionBegin;
66   if (!dsfield->disc[height]) {
67     PetscClassId   id;
68 
69     ierr = PetscObjectGetClassId(dsfield->disc[0],&id);CHKERRQ(ierr);
70     if (id == PETSCFE_CLASSID) {
71       PetscFE fe = (PetscFE) dsfield->disc[0];
72 
73       ierr = PetscFECreateHeightTrace(fe,height,(PetscFE *)&dsfield->disc[height]);CHKERRQ(ierr);
74     }
75   }
76   *disc = dsfield->disc[height];
77   PetscFunctionReturn(0);
78 }
79 
80 #define DMFieldDSdot(y,A,b,m,n,c,cast)                                           \
81   do {                                                                           \
82     PetscInt _i, _j, _k;                                                         \
83     for (_i = 0; _i < (m); _i++) {                                               \
84       for (_k = 0; _k < (c); _k++) {                                             \
85         (y)[_i * (c) + _k] = 0.;                                                 \
86       }                                                                          \
87       for (_j = 0; _j < (n); _j++) {                                             \
88         for (_k = 0; _k < (c); _k++) {                                           \
89           (y)[_i * (c) + _k] += (A)[(_i * (n) + _j) * (c) + _k] * cast((b)[_j]); \
90         }                                                                        \
91       }                                                                          \
92     }                                                                            \
93   } while (0)
94 
95 static PetscErrorCode DMFieldEvaluateFE_DS(DMField field, IS pointIS, PetscQuadrature quad, PetscDataType type, void *B, void *D, void *H)
96 {
97   DMField_DS      *dsfield = (DMField_DS *) field->data;
98   DM              dm;
99   PetscObject     disc;
100   PetscClassId    classid;
101   PetscInt        nq, nc, dim, meshDim, numCells;
102   PetscSection    section;
103   const PetscReal *qpoints;
104   PetscBool       isStride;
105   const PetscInt  *points = NULL;
106   PetscInt        sfirst = -1, stride = -1;
107   PetscErrorCode  ierr;
108 
109   PetscFunctionBeginHot;
110   dm   = field->dm;
111   nc   = field->numComponents;
112   ierr = PetscQuadratureGetData(quad,&dim,NULL,&nq,&qpoints,NULL);CHKERRQ(ierr);
113   ierr = DMFieldDSGetHeightDisc(field,dsfield->height - 1 - dim,&disc);CHKERRQ(ierr);
114   ierr = DMGetDimension(dm,&meshDim);CHKERRQ(ierr);
115   ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
116   ierr = PetscSectionGetField(section,dsfield->fieldNum,&section);CHKERRQ(ierr);
117   ierr = PetscObjectGetClassId(disc,&classid);CHKERRQ(ierr);
118   /* TODO: batch */
119   ierr = PetscObjectTypeCompare((PetscObject)pointIS,ISSTRIDE,&isStride);CHKERRQ(ierr);
120   ierr = ISGetLocalSize(pointIS,&numCells);CHKERRQ(ierr);
121   if (isStride) {
122     ierr = ISStrideGetInfo(pointIS,&sfirst,&stride);CHKERRQ(ierr);
123   } else {
124     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
125   }
126   if (classid == PETSCFE_CLASSID) {
127     PetscFE      fe = (PetscFE) disc;
128     PetscInt     feDim, i;
129     PetscReal    *fB = NULL, *fD = NULL, *fH = NULL;
130 
131     ierr = PetscFEGetDimension(fe,&feDim);CHKERRQ(ierr);
132     ierr = PetscFEGetTabulation(fe,nq,qpoints,B ? &fB : NULL,D ? &fD : NULL,H ? &fH : NULL);CHKERRQ(ierr);
133     for (i = 0; i < numCells; i++) {
134       PetscInt     c = isStride ? (sfirst + i * stride) : points[i];
135       PetscInt     closureSize;
136       PetscScalar *elem = NULL;
137 
138       ierr = DMPlexVecGetClosure(dm,section,dsfield->vec,c,&closureSize,&elem);CHKERRQ(ierr);
139       if (B) {
140         if (type == PETSC_SCALAR) {
141           PetscScalar *cB = &((PetscScalar *) B)[nc * nq * i];
142 
143           DMFieldDSdot(cB,fB,elem,nq,feDim,nc,(PetscScalar));
144         } else {
145           PetscReal *cB = &((PetscReal *) B)[nc * nq * i];
146 
147           DMFieldDSdot(cB,fB,elem,nq,feDim,nc,PetscRealPart);
148         }
149       }
150       if (D) {
151         if (type == PETSC_SCALAR) {
152           PetscScalar *cD = &((PetscScalar *) D)[nc * nq * dim * i];
153 
154           DMFieldDSdot(cD,fD,elem,nq,feDim,(nc * dim),(PetscScalar));
155         } else {
156           PetscReal *cD = &((PetscReal *) D)[nc * nq * dim * i];
157 
158           DMFieldDSdot(cD,fD,elem,nq,feDim,(nc * dim),PetscRealPart);
159         }
160       }
161       if (H) {
162         if (type == PETSC_SCALAR) {
163           PetscScalar *cH = &((PetscScalar *) H)[nc * nq * dim * dim * i];
164 
165           DMFieldDSdot(cH,fH,elem,nq,feDim,(nc * dim * dim),(PetscScalar));
166         } else {
167           PetscReal *cH = &((PetscReal *) H)[nc * nq * dim * dim * i];
168 
169           DMFieldDSdot(cH,fH,elem,nq,feDim,(nc * dim * dim),PetscRealPart);
170         }
171       }
172       ierr = DMPlexVecRestoreClosure(dm,section,dsfield->vec,c,&closureSize,&elem);CHKERRQ(ierr);
173     }
174     ierr = PetscFERestoreTabulation(fe,nq,qpoints,B ? &fB : NULL,D ? &fD : NULL,H ? &fH : NULL);CHKERRQ(ierr);
175   } else {SETERRQ(PetscObjectComm((PetscObject)field),PETSC_ERR_SUP,"Not implemented");}
176   if (!isStride) {
177     ierr = ISRestoreIndices(pointIS,&points);CHKERRQ(ierr);
178   }
179   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
180   PetscFunctionReturn(0);
181 }
182 
183 static PetscErrorCode DMFieldEvaluate_DS(DMField field, Vec points, PetscDataType datatype, void *B, void *D, void *H)
184 {
185   DMField_DS        *dsfield = (DMField_DS *) field->data;
186   PetscSF            cellSF = NULL;
187   const PetscSFNode *cells;
188   PetscInt           c, nFound, numCells, feDim, nc;
189   const PetscInt    *cellDegrees;
190   const PetscScalar *pointsArray;
191   PetscScalar       *cellPoints;
192   PetscInt           gatherSize, gatherMax;
193   PetscInt           dim, dimR, offset;
194   MPI_Datatype       pointType;
195   PetscObject        cellDisc;
196   PetscFE            cellFE;
197   PetscClassId       discID;
198   PetscReal         *coordsReal, *coordsRef;
199   PetscSection       section;
200   PetscScalar       *cellBs = NULL, *cellDs = NULL, *cellHs = NULL;
201   PetscReal         *cellBr = NULL, *cellDr = NULL, *cellHr = NULL;
202   PetscReal         *v, *J, *invJ, *detJ;
203   PetscErrorCode     ierr;
204 
205   PetscFunctionBegin;
206   nc   = field->numComponents;
207   ierr = DMGetDefaultSection(field->dm,&section);CHKERRQ(ierr);
208   ierr = DMFieldDSGetHeightDisc(field,0,&cellDisc);CHKERRQ(ierr);
209   ierr = PetscObjectGetClassId(cellDisc, &discID);CHKERRQ(ierr);
210   if (discID != PETSCFE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Discretization type not supported\n");
211   cellFE = (PetscFE) cellDisc;
212   ierr = PetscFEGetDimension(cellFE,&feDim);CHKERRQ(ierr);
213   ierr = DMGetCoordinateDim(field->dm, &dim);CHKERRQ(ierr);
214   ierr = DMGetDimension(field->dm, &dimR);CHKERRQ(ierr);
215   ierr = DMLocatePoints(field->dm, points, DM_POINTLOCATION_NONE, &cellSF);CHKERRQ(ierr);
216   ierr = PetscSFGetGraph(cellSF, &numCells, &nFound, NULL, &cells);CHKERRQ(ierr);
217   for (c = 0; c < nFound; c++) {
218     if (cells[c].index < 0) SETERRQ1(PetscObjectComm((PetscObject)points),PETSC_ERR_ARG_WRONG, "Point %D could not be located\n", c);
219   }
220   ierr = PetscSFComputeDegreeBegin(cellSF,&cellDegrees);CHKERRQ(ierr);
221   ierr = PetscSFComputeDegreeEnd(cellSF,&cellDegrees);CHKERRQ(ierr);
222   for (c = 0, gatherSize = 0, gatherMax = 0; c < numCells; c++) {
223     gatherMax = PetscMax(gatherMax,cellDegrees[c]);
224     gatherSize += cellDegrees[c];
225   }
226   ierr = PetscMalloc3(gatherSize*dim,&cellPoints,gatherMax*dim,&coordsReal,gatherMax*dimR,&coordsRef);CHKERRQ(ierr);
227   ierr = PetscMalloc4(gatherMax*dimR,&v,gatherMax*dimR*dimR,&J,gatherMax*dimR*dimR,&invJ,gatherMax,&detJ);CHKERRQ(ierr);
228   if (datatype == PETSC_SCALAR) {
229     ierr = PetscMalloc3(B ? nc * gatherSize : 0, &cellBs, D ? nc * dim * gatherSize : 0, &cellDs, H ? nc * dim * dim * gatherSize : 0, &cellHs);CHKERRQ(ierr);
230   } else {
231     ierr = PetscMalloc3(B ? nc * gatherSize : 0, &cellBr, D ? nc * dim * gatherSize : 0, &cellDr, H ? nc * dim * dim * gatherSize : 0, &cellHr);CHKERRQ(ierr);
232   }
233 
234   ierr = MPI_Type_contiguous(dim,MPIU_SCALAR,&pointType);CHKERRQ(ierr);
235   ierr = MPI_Type_commit(&pointType);CHKERRQ(ierr);
236   ierr = VecGetArrayRead(points,&pointsArray);CHKERRQ(ierr);
237   ierr = PetscSFGatherBegin(cellSF, pointType, pointsArray, cellPoints);CHKERRQ(ierr);
238   ierr = PetscSFGatherEnd(cellSF, pointType, pointsArray, cellPoints);CHKERRQ(ierr);
239   ierr = VecRestoreArrayRead(points,&pointsArray);CHKERRQ(ierr);
240   for (c = 0, offset = 0; c < numCells; c++) {
241     PetscInt nq = cellDegrees[c], p;
242 
243     if (nq) {
244       PetscReal *fB, *fD, *fH;
245       PetscInt     closureSize;
246       PetscScalar *elem = NULL;
247       PetscReal   *quadPoints;
248       PetscQuadrature quad;
249       PetscInt d, e, f, g;
250 
251       for (p = 0; p < dim * nq; p++) coordsReal[p] = PetscRealPart(cellPoints[dim * offset + p]);
252       ierr = DMPlexCoordinatesToReference(field->dm, c, nq, coordsReal, coordsRef);CHKERRQ(ierr);
253       ierr = PetscFEGetTabulation(cellFE,nq,coordsRef,B ? &fB : NULL,D ? &fD : NULL,H ? &fH : NULL);CHKERRQ(ierr);
254       ierr = PetscQuadratureCreate(PETSC_COMM_SELF, &quad);CHKERRQ(ierr);
255       ierr = PetscMalloc1(dimR * nq, &quadPoints);CHKERRQ(ierr);
256       for (p = 0; p < dimR * nq; p++) quadPoints[p] = coordsRef[p];
257       ierr = PetscQuadratureSetData(quad, dimR, 0, nq, quadPoints, NULL);CHKERRQ(ierr);
258       ierr = DMPlexComputeCellGeometryFEM(field->dm, c, quad, v, J, invJ, detJ);CHKERRQ(ierr);
259       ierr = PetscQuadratureDestroy(&quad);CHKERRQ(ierr);
260       ierr = DMPlexVecGetClosure(field->dm,section,dsfield->vec,c,&closureSize,&elem);CHKERRQ(ierr);
261       if (B) {
262         if (datatype == PETSC_SCALAR) {
263           PetscScalar *cB = &cellBs[nc * offset];
264 
265           DMFieldDSdot(cB,fB,elem,nq,feDim,nc,(PetscScalar));
266         } else {
267           PetscReal *cB = &cellBr[nc * offset];
268 
269           DMFieldDSdot(cB,fB,elem,nq,feDim,nc,PetscRealPart);
270         }
271       }
272       if (D) {
273         if (datatype == PETSC_SCALAR) {
274           PetscScalar *cD = &cellDs[nc * dim * offset];
275 
276           DMFieldDSdot(cD,fD,elem,nq,feDim,(nc * dim),(PetscScalar));
277           for (p = 0; p < nq; p++) {
278             for (g = 0; g < nc; g++) {
279               PetscScalar vs[3];
280 
281               for (d = 0; d < dimR; d++) {
282                 vs[d] = 0.;
283                 for (e = 0; e < dimR; e++) {
284                   vs[d] += invJ[dimR * dimR * p + e * dimR + d] * cD[(nc * p + g) * dimR + e];
285                 }
286               }
287               for (d = 0; d < dimR; d++) {
288                 cD[(nc * p + g) * dimR + d] = vs[d];
289               }
290             }
291           }
292         } else {
293           PetscReal *cD = &cellDr[nc * dim * offset];
294 
295           DMFieldDSdot(cD,fD,elem,nq,feDim,(nc * dim),PetscRealPart);
296           for (p = 0; p < nq; p++) {
297             for (g = 0; g < nc; g++) {
298               for (d = 0; d < dimR; d++) {
299                 v[d] = 0.;
300                 for (e = 0; e < dimR; e++) {
301                   v[d] += invJ[dimR * dimR * p + e * dimR + d] * cD[(nc * p + g) * dimR + e];
302                 }
303               }
304               for (d = 0; d < dimR; d++) {
305                 cD[(nc * p + g) * dimR + d] = v[d];
306               }
307             }
308           }
309         }
310       }
311       if (H) {
312         if (datatype == PETSC_SCALAR) {
313           PetscScalar *cH = &cellHs[nc * dim * dim * offset];
314 
315           DMFieldDSdot(cH,fH,elem,nq,feDim,(nc * dim * dim),(PetscScalar));
316           for (p = 0; p < nq; p++) {
317             for (g = 0; g < nc * dimR; g++) {
318               PetscScalar vs[3];
319 
320               for (d = 0; d < dimR; d++) {
321                 vs[d] = 0.;
322                 for (e = 0; e < dimR; e++) {
323                   vs[d] += invJ[dimR * dimR * p + e * dimR + d] * cH[(nc * dimR * p + g) * dimR + e];
324                 }
325               }
326               for (d = 0; d < dimR; d++) {
327                 cH[(nc * dimR * p + g) * dimR + d] = vs[d];
328               }
329             }
330             for (g = 0; g < nc; g++) {
331               for (f = 0; f < dimR; f++) {
332                 PetscScalar vs[3];
333 
334                 for (d = 0; d < dimR; d++) {
335                   vs[d] = 0.;
336                   for (e = 0; e < dimR; e++) {
337                     vs[d] += invJ[dimR * dimR * p + e * dimR + d] * cH[((nc * p + g) * dimR + e) * dimR + f];
338                   }
339                 }
340                 for (d = 0; d < dimR; d++) {
341                   cH[((nc * p + g) * dimR + d) * dimR + f] = vs[d];
342                 }
343               }
344             }
345           }
346         } else {
347           PetscReal *cH = &cellHr[nc * dim * dim * offset];
348 
349           DMFieldDSdot(cH,fH,elem,nq,feDim,(nc * dim * dim),PetscRealPart);
350           for (p = 0; p < nq; p++) {
351             for (g = 0; g < nc * dimR; g++) {
352               for (d = 0; d < dimR; d++) {
353                 v[d] = 0.;
354                 for (e = 0; e < dimR; e++) {
355                   v[d] += invJ[dimR * dimR * p + e * dimR + d] * cH[(nc * dimR * p + g) * dimR + e];
356                 }
357               }
358               for (d = 0; d < dimR; d++) {
359                 cH[(nc * dimR * p + g) * dimR + d] = v[d];
360               }
361             }
362             for (g = 0; g < nc; g++) {
363               for (f = 0; f < dimR; f++) {
364                 for (d = 0; d < dimR; d++) {
365                   v[d] = 0.;
366                   for (e = 0; e < dimR; e++) {
367                     v[d] += invJ[dimR * dimR * p + e * dimR + d] * cH[((nc * p + g) * dimR + e) * dimR + f];
368                   }
369                 }
370                 for (d = 0; d < dimR; d++) {
371                   cH[((nc * p + g) * dimR + d) * dimR + f] = v[d];
372                 }
373               }
374             }
375           }
376         }
377       }
378       ierr = DMPlexVecRestoreClosure(field->dm,section,dsfield->vec,c,&closureSize,&elem);CHKERRQ(ierr);
379       ierr = PetscFERestoreTabulation(cellFE,nq,coordsRef,B ? &fB : NULL,D ? &fD : NULL,H ? &fH : NULL);CHKERRQ(ierr);
380     }
381     offset += nq;
382   }
383   {
384     MPI_Datatype origtype;
385 
386     if (datatype == PETSC_SCALAR) {
387       origtype = MPIU_SCALAR;
388     } else {
389       origtype = MPIU_REAL;
390     }
391     if (B) {
392       MPI_Datatype Btype;
393 
394       ierr = MPI_Type_contiguous(nc, origtype, &Btype);CHKERRQ(ierr);
395       ierr = MPI_Type_commit(&Btype);CHKERRQ(ierr);
396       ierr = PetscSFScatterBegin(cellSF,Btype,(datatype == PETSC_SCALAR) ? (void *) cellBs : (void *) cellBr, B);CHKERRQ(ierr);
397       ierr = PetscSFScatterEnd(cellSF,Btype,(datatype == PETSC_SCALAR) ? (void *) cellBs : (void *) cellBr, B);CHKERRQ(ierr);
398       ierr = MPI_Type_free(&Btype);CHKERRQ(ierr);
399     }
400     if (D) {
401       MPI_Datatype Dtype;
402 
403       ierr = MPI_Type_contiguous(nc * dim, origtype, &Dtype);CHKERRQ(ierr);
404       ierr = MPI_Type_commit(&Dtype);CHKERRQ(ierr);
405       ierr = PetscSFScatterBegin(cellSF,Dtype,(datatype == PETSC_SCALAR) ? (void *) cellDs : (void *) cellDr, D);CHKERRQ(ierr);
406       ierr = PetscSFScatterEnd(cellSF,Dtype,(datatype == PETSC_SCALAR) ? (void *) cellDs : (void *) cellDr, D);CHKERRQ(ierr);
407       ierr = MPI_Type_free(&Dtype);CHKERRQ(ierr);
408     }
409     if (H) {
410       MPI_Datatype Htype;
411 
412       ierr = MPI_Type_contiguous(nc * dim * dim, origtype, &Htype);CHKERRQ(ierr);
413       ierr = MPI_Type_commit(&Htype);CHKERRQ(ierr);
414       ierr = PetscSFScatterBegin(cellSF,Htype,(datatype == PETSC_SCALAR) ? (void *) cellHs : (void *) cellHr, H);CHKERRQ(ierr);
415       ierr = PetscSFScatterEnd(cellSF,Htype,(datatype == PETSC_SCALAR) ? (void *) cellHs : (void *) cellHr, H);CHKERRQ(ierr);
416       ierr = MPI_Type_free(&Htype);CHKERRQ(ierr);
417     }
418   }
419   ierr = PetscFree4(v,J,invJ,detJ);CHKERRQ(ierr);
420   ierr = PetscFree3(cellBr, cellDr, cellHr);CHKERRQ(ierr);
421   ierr = PetscFree3(cellBs, cellDs, cellHs);CHKERRQ(ierr);
422   ierr = PetscFree3(cellPoints,coordsReal,coordsRef);CHKERRQ(ierr);
423   ierr = MPI_Type_free(&pointType);CHKERRQ(ierr);
424   ierr = PetscSFDestroy(&cellSF);CHKERRQ(ierr);
425   PetscFunctionReturn(0);
426 }
427 
428 static PetscErrorCode DMFieldEvaluateFV_DS(DMField field, IS pointIS, PetscDataType type, void *B, void *D, void *H)
429 {
430   DMField_DS      *dsfield = (DMField_DS *) field->data;
431   PetscInt         h, imin;
432   PetscInt         dim;
433   PetscClassId     id;
434   PetscQuadrature  quad = NULL;
435   PetscBool        isAffine;
436   PetscFEGeom      *geom;
437   PetscInt         Nq, Nc, dimC, qNc, N;
438   PetscInt         numPoints;
439   void            *qB = NULL, *qD = NULL, *qH = NULL;
440   const PetscReal *weights;
441   MPI_Datatype     mpitype = type == PETSC_SCALAR ? MPIU_SCALAR : MPIU_REAL;
442   PetscObject      disc;
443   DMField          coordField;
444   PetscErrorCode   ierr;
445 
446   PetscFunctionBegin;
447   Nc = field->numComponents;
448   dsfield = (DMField_DS *) field->data;
449   ierr = DMGetCoordinateDim(field->dm, &dimC);CHKERRQ(ierr);
450   ierr = DMGetDimension(field->dm, &dim);CHKERRQ(ierr);
451   ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
452   ierr = ISGetMinMax(pointIS,&imin,NULL);CHKERRQ(ierr);
453   for (h = 0; h < dsfield->height; h++) {
454     PetscInt hEnd;
455 
456     ierr = DMPlexGetHeightStratum(field->dm,h,NULL,&hEnd);CHKERRQ(ierr);
457     if (imin < hEnd) break;
458   }
459   dim -= h;
460   ierr = DMFieldDSGetHeightDisc(field,h,&disc);CHKERRQ(ierr);
461   ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
462   if (id != PETSCFE_CLASSID) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Discretization not supported\n");
463   ierr = DMGetCoordinateField(field->dm, &coordField);CHKERRQ(ierr);
464   ierr = DMFieldGetFEInvariance(coordField, pointIS, NULL, &isAffine, NULL);CHKERRQ(ierr);
465   if (isAffine) {
466     ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &quad);CHKERRQ(ierr);
467   }
468   if (!quad) {ierr = DMFieldCreateDefaultQuadrature(field, pointIS, &quad);CHKERRQ(ierr);}
469   if (!quad) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not determine quadrature for cell averages\n");
470   ierr = DMFieldCreateFEGeom(coordField,pointIS,quad,PETSC_FALSE,&geom);CHKERRQ(ierr);
471   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &weights);CHKERRQ(ierr);
472   if (qNc != 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected scalar quadrature components\n");
473   N = numPoints * Nq * Nc;
474   if (B) ierr = DMGetWorkArray(field->dm, N, mpitype, &qB);CHKERRQ(ierr);
475   if (D) ierr = DMGetWorkArray(field->dm, N * dimC, mpitype, &qD);CHKERRQ(ierr);
476   if (H) ierr = DMGetWorkArray(field->dm, N * dimC * dimC, mpitype, &qH);CHKERRQ(ierr);
477   ierr = DMFieldEvaluateFE(field,pointIS,quad,type,qB,qD,qH);CHKERRQ(ierr);
478   if (B) {
479     PetscInt i, j, k;
480 
481     if (type == PETSC_SCALAR) {
482       PetscScalar * sB  = (PetscScalar *) B;
483       PetscScalar * sqB = (PetscScalar *) qB;
484 
485       for (i = 0; i < numPoints; i++) {
486         PetscReal vol = 0.;
487 
488         for (j = 0; j < Nc; j++) {sB[i * Nc + j] = 0.;}
489         for (k = 0; k < Nq; k++) {
490           vol += geom->detJ[i * Nq + k] * weights[k];
491           for (j = 0; j < Nc; j++) {
492             sB[i * Nc + j] += geom->detJ[i * Nq + k] * weights[k] * sqB[ (i * Nq + k) * Nc + j];
493           }
494         }
495         for (k = 0; k < Nq * Nc; k++) sB[i * Nq * Nc + k] /= vol;
496       }
497     } else {
498       PetscReal * rB  = (PetscReal *) B;
499       PetscReal * rqB = (PetscReal *) qB;
500 
501       for (i = 0; i < numPoints; i++) {
502         PetscReal vol = 0.;
503 
504         for (j = 0; j < Nc; j++) {rB[i * Nc + j] = 0.;}
505         for (k = 0; k < Nq; k++) {
506           vol += geom->detJ[i * Nq + k] * weights[k];
507           for (j = 0; j < Nc; j++) {
508             rB[i * Nc + j] += weights[k] * rqB[ (i * Nq + k) * Nc + j];
509           }
510         }
511         for (k = 0; k < Nc; k++) rB[i * Nc + k] /= vol;
512       }
513     }
514   }
515   if (D) {
516     PetscInt i, j, k, l, m;
517 
518     if (type == PETSC_SCALAR) {
519       PetscScalar * sD  = (PetscScalar *) D;
520       PetscScalar * sqD = (PetscScalar *) qD;
521 
522       for (i = 0; i < numPoints; i++) {
523         PetscReal vol = 0.;
524 
525         for (j = 0; j < Nc * dimC; j++) {sD[i * Nc * dimC + j] = 0.;}
526         for (k = 0; k < Nq; k++) {
527           vol += geom->detJ[i * Nq + k] * weights[k];
528           for (j = 0; j < Nc; j++) {
529             PetscScalar pD[3] = {0.,0.,0.};
530 
531             for (l = 0; l < dimC; l++) {
532               for (m = 0; m < dim; m++) {
533                 pD[l] += geom->invJ[((i * Nq + k) * dimC + m) * dimC + l] * sqD[((i * Nq + k) * Nc + j) * dim + m];
534               }
535             }
536             for (l = 0; l < dimC; l++) {
537               sD[(i * Nc + j) * dimC + l] += geom->detJ[i * Nq + k] * weights[k] * pD[l];
538             }
539           }
540         }
541         for (k = 0; k < Nc * dimC; k++) sD[i * Nc * dimC + k] /= vol;
542       }
543     } else {
544       PetscReal * rD  = (PetscReal *) D;
545       PetscReal * rqD = (PetscReal *) qD;
546 
547       for (i = 0; i < numPoints; i++) {
548         PetscReal vol = 0.;
549 
550         for (j = 0; j < Nc * dimC; j++) {rD[i * Nc * dimC + j] = 0.;}
551         for (k = 0; k < Nq; k++) {
552           vol += geom->detJ[i * Nq + k] * weights[k];
553           for (j = 0; j < Nc; j++) {
554             PetscReal pD[3] = {0.,0.,0.};
555 
556             for (l = 0; l < dimC; l++) {
557               for (m = 0; m < dim; m++) {
558                 pD[l] += geom->invJ[((i * Nq + k) * dimC + m) * dimC + l] * rqD[((i * Nq + k) * Nc + j) * dim + m];
559               }
560             }
561             for (l = 0; l < dimC; l++) {
562               rD[(i * Nc + j) * dimC + l] += geom->detJ[i * Nq + k] * weights[k] * pD[l];
563             }
564           }
565         }
566         for (k = 0; k < Nc * dimC; k++) rD[i * Nc * dimC + k] /= vol;
567       }
568     }
569   }
570   if (H) {
571     PetscInt i, j, k, l, m, q, r;
572 
573     if (type == PETSC_SCALAR) {
574       PetscScalar * sH  = (PetscScalar *) H;
575       PetscScalar * sqH = (PetscScalar *) qH;
576 
577       for (i = 0; i < numPoints; i++) {
578         PetscReal vol = 0.;
579 
580         for (j = 0; j < Nc * dimC * dimC; j++) {sH[i * Nc * dimC * dimC + j] = 0.;}
581         for (k = 0; k < Nq; k++) {
582           const PetscReal *invJ = &geom->invJ[(i * Nq + k) * dimC * dimC];
583 
584           vol += geom->detJ[i * Nq + k] * weights[k];
585           for (j = 0; j < Nc; j++) {
586             PetscScalar pH[3][3] = {{0.,0.,0.},{0.,0.,0.},{0.,0.,0.}};
587             const PetscScalar *spH = &sqH[((i * Nq + k) * Nc + j) * dimC * dimC];
588 
589             for (l = 0; l < dimC; l++) {
590               for (m = 0; m < dimC; m++) {
591                 for (q = 0; q < dim; q++) {
592                   for (r = 0; r < dim; r++) {
593                     pH[l][m] += invJ[q * dimC + l] * invJ[r * dimC + m] * spH[q * dim + r];
594                   }
595                 }
596               }
597             }
598             for (l = 0; l < dimC; l++) {
599               for (m = 0; m < dimC; m++) {
600                 sH[(i * Nc + j) * dimC * dimC + l * dimC + m] += geom->detJ[i * Nq + k] * weights[k] * pH[l][m];
601               }
602             }
603           }
604         }
605         for (k = 0; k < Nc * dimC * dimC; k++) sH[i * Nc * dimC * dimC + k] /= vol;
606       }
607     } else {
608       PetscReal * rH  = (PetscReal *) H;
609       PetscReal * rqH = (PetscReal *) qH;
610 
611       for (i = 0; i < numPoints; i++) {
612         PetscReal vol = 0.;
613 
614         for (j = 0; j < Nc * dimC * dimC; j++) {rH[i * Nc * dimC * dimC + j] = 0.;}
615         for (k = 0; k < Nq; k++) {
616           const PetscReal *invJ = &geom->invJ[(i * Nq + k) * dimC * dimC];
617 
618           vol += geom->detJ[i * Nq + k] * weights[k];
619           for (j = 0; j < Nc; j++) {
620             PetscReal pH[3][3] = {{0.,0.,0.},{0.,0.,0.},{0.,0.,0.}};
621             const PetscReal *rpH = &rqH[((i * Nq + k) * Nc + j) * dimC * dimC];
622 
623             for (l = 0; l < dimC; l++) {
624               for (m = 0; m < dimC; m++) {
625                 for (q = 0; q < dim; q++) {
626                   for (r = 0; r < dim; r++) {
627                     pH[l][m] += invJ[q * dimC + l] * invJ[r * dimC + m] * rpH[q * dim + r];
628                   }
629                 }
630               }
631             }
632             for (l = 0; l < dimC; l++) {
633               for (m = 0; m < dimC; m++) {
634                 rH[(i * Nc + j) * dimC * dimC + l * dimC + m] += geom->detJ[i * Nq + k] * weights[k] * pH[l][m];
635               }
636             }
637           }
638         }
639         for (k = 0; k < Nc * dimC * dimC; k++) rH[i * Nc * dimC * dimC + k] /= vol;
640       }
641     }
642   }
643   if (B) ierr = DMRestoreWorkArray(field->dm, N, mpitype, &qB);CHKERRQ(ierr);
644   if (D) ierr = DMRestoreWorkArray(field->dm, N * dimC, mpitype, &qD);CHKERRQ(ierr);
645   if (H) ierr = DMRestoreWorkArray(field->dm, N * dimC * dimC, mpitype, &qH);CHKERRQ(ierr);
646   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
647   ierr = PetscQuadratureDestroy(&quad);CHKERRQ(ierr);
648   PetscFunctionReturn(0);
649 }
650 
651 static PetscErrorCode DMFieldGetFEInvariance_DS(DMField field, IS pointIS, PetscBool *isConstant, PetscBool *isAffine, PetscBool *isQuadratic)
652 {
653   DMField_DS     *dsfield;
654   PetscObject    disc;
655   PetscInt       h, imin, imax;
656   PetscClassId   id;
657   PetscErrorCode ierr;
658 
659   PetscFunctionBegin;
660   dsfield = (DMField_DS *) field->data;
661   ierr = ISGetMinMax(pointIS,&imin,&imax);CHKERRQ(ierr);
662   if (imin >= imax) {
663     if (isConstant) {*isConstant = PETSC_TRUE;}
664     if (isAffine) {*isAffine = PETSC_TRUE;}
665     if (isQuadratic) {*isQuadratic = PETSC_TRUE;}
666     PetscFunctionReturn(0);
667   }
668   for (h = 0; h < dsfield->height; h++) {
669     PetscInt hEnd;
670 
671     ierr = DMPlexGetHeightStratum(field->dm,h,NULL,&hEnd);CHKERRQ(ierr);
672     if (imin < hEnd) break;
673   }
674   ierr = DMFieldDSGetHeightDisc(field,h,&disc);CHKERRQ(ierr);
675   ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
676   if (id == PETSCFE_CLASSID) {
677     PetscFE    fe = (PetscFE) disc;
678     PetscInt   order, maxOrder;
679     PetscBool  tensor = PETSC_FALSE;
680     PetscSpace sp;
681 
682     ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
683     ierr = PetscSpaceGetOrder(sp,&order);CHKERRQ(ierr);
684     ierr = PetscSpacePolynomialGetTensor(sp,&tensor);CHKERRQ(ierr);
685     if (tensor) {
686       PetscInt dim;
687 
688       ierr = DMGetDimension(field->dm,&dim);CHKERRQ(ierr);
689       maxOrder = order * dim;
690     } else {
691       maxOrder = order;
692     }
693     if (isConstant)  *isConstant  = (maxOrder < 1) ? PETSC_TRUE : PETSC_FALSE;
694     if (isAffine)    *isAffine    = (maxOrder < 2) ? PETSC_TRUE : PETSC_FALSE;
695     if (isQuadratic) *isQuadratic = (maxOrder < 3) ? PETSC_TRUE : PETSC_FALSE;
696   }
697   PetscFunctionReturn(0);
698 }
699 
700 static PetscErrorCode DMFieldCreateDefaultQuadrature_DS(DMField field, IS pointIS, PetscQuadrature *quad)
701 {
702   PetscInt       h, dim, imax, imin;
703   DM             dm;
704   DMField_DS     *dsfield;
705   PetscObject    disc;
706   PetscFE        fe;
707   PetscClassId   id;
708   PetscErrorCode ierr;
709 
710 
711   PetscFunctionBegin;
712   dm = field->dm;
713   dsfield = (DMField_DS *) field->data;
714   ierr = ISGetMinMax(pointIS,&imax,&imin);CHKERRQ(ierr);
715   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
716   for (h = 0; h <= dim; h++) {
717     PetscInt hStart, hEnd;
718 
719     ierr = DMPlexGetHeightStratum(dm,h,&hStart,&hEnd);CHKERRQ(ierr);
720     if (imin >= hStart && imax < hEnd) break;
721   }
722   *quad = NULL;
723   if (h < dsfield->height) {
724     ierr = DMFieldDSGetHeightDisc(field,h,&disc);CHKERRQ(ierr);
725     ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
726     if (id != PETSCFE_CLASSID) PetscFunctionReturn(0);
727     fe = (PetscFE) disc;
728     ierr = PetscFEGetQuadrature(fe,quad);CHKERRQ(ierr);
729     ierr = PetscObjectReference((PetscObject)*quad);CHKERRQ(ierr);
730   }
731   PetscFunctionReturn(0);
732 }
733 
734 static PetscErrorCode DMFieldComputeFaceData_DS(DMField field, IS pointIS, PetscQuadrature quad, PetscFEGeom *geom)
735 {
736   const PetscInt *points;
737   PetscInt        p, dim, dE, numFaces, Nq;
738   PetscBool       affineCells;
739   DMLabel         depthLabel;
740   IS              cellIS;
741   DM              dm = field->dm;
742   PetscErrorCode  ierr;
743 
744   PetscFunctionBegin;
745   dim = geom->dim;
746   dE  = geom->dimEmbed;
747   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
748   ierr = DMLabelGetStratumIS(depthLabel, dim + 1, &cellIS);CHKERRQ(ierr);
749   ierr = DMFieldGetFEInvariance(field,cellIS,NULL,&affineCells,NULL);CHKERRQ(ierr);
750   ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
751   numFaces = geom->numCells;
752   Nq = geom->numPoints;
753   if (affineCells) {
754     PetscInt        numCells, offset, *cells;
755     PetscFEGeom     *cellGeom;
756     IS              suppIS;
757     PetscQuadrature cellQuad = NULL;
758 
759     ierr = DMFieldCreateDefaultQuadrature(field,cellIS,&cellQuad);CHKERRQ(ierr);
760     for (p = 0, numCells = 0; p < numFaces; p++) {
761       PetscInt        point = points[p];
762       PetscInt        numSupp, numChildren;
763 
764       ierr = DMPlexGetTreeChildren(dm, point, &numChildren, NULL); CHKERRQ(ierr);
765       if (numChildren) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face data not valid for facets with children");
766       ierr = DMPlexGetSupportSize(dm, point,&numSupp);CHKERRQ(ierr);
767       if (numSupp > 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D has %D support, expected at most 2\n", point, numSupp);
768       numCells += numSupp;
769     }
770     ierr = PetscMalloc1(numCells, &cells);CHKERRQ(ierr);
771     for (p = 0, offset = 0; p < numFaces; p++) {
772       PetscInt        point = points[p];
773       PetscInt        numSupp, s;
774       const PetscInt *supp;
775 
776       ierr = DMPlexGetSupportSize(dm, point,&numSupp);CHKERRQ(ierr);
777       ierr = DMPlexGetSupport(dm, point, &supp);CHKERRQ(ierr);
778       for (s = 0; s < numSupp; s++, offset++) {
779         cells[offset] = supp[s];
780       }
781     }
782     ierr = ISCreateGeneral(PETSC_COMM_SELF,numCells,cells,PETSC_USE_POINTER, &suppIS);CHKERRQ(ierr);
783     ierr = DMFieldCreateFEGeom(field,suppIS,cellQuad,PETSC_FALSE,&cellGeom);CHKERRQ(ierr);
784     for (p = 0, offset = 0; p < numFaces; p++) {
785       PetscInt        point = points[p];
786       PetscInt        numSupp, s, q;
787       const PetscInt *supp;
788 
789       ierr = DMPlexGetSupportSize(dm, point,&numSupp);CHKERRQ(ierr);
790       ierr = DMPlexGetSupport(dm, point, &supp);CHKERRQ(ierr);
791       for (s = 0; s < numSupp; s++, offset++) {
792         for (q = 0; q < Nq * dE * dE; q++) {
793           geom->suppInvJ[s][p * Nq * dE * dE + q] = cellGeom->invJ[offset * Nq * dE * dE + q];
794         }
795       }
796     }
797     ierr = PetscFEGeomDestroy(&cellGeom);CHKERRQ(ierr);
798     ierr = ISDestroy(&suppIS);CHKERRQ(ierr);
799     ierr = PetscFree(cells);CHKERRQ(ierr);
800     ierr = PetscQuadratureDestroy(&cellQuad);CHKERRQ(ierr);
801   } else {
802     PetscObject          faceDisc, cellDisc;
803     PetscClassId         faceId, cellId;
804     PetscDualSpace       dsp;
805     DM                   K;
806     PetscInt           (*co)[2][3];
807     PetscInt             coneSize;
808     PetscInt           **counts;
809     PetscInt             f, i, o, q, s;
810     const PetscInt      *coneK;
811     PetscInt             minOrient, maxOrient, numOrient;
812     PetscInt            *orients;
813     PetscReal          **orientPoints;
814     PetscReal           *cellPoints;
815     PetscReal           *dummyWeights;
816     PetscQuadrature      cellQuad = NULL;
817 
818     ierr = DMFieldDSGetHeightDisc(field, 1, &faceDisc);CHKERRQ(ierr);
819     ierr = DMFieldDSGetHeightDisc(field, 0, &cellDisc);CHKERRQ(ierr);
820     ierr = PetscObjectGetClassId(faceDisc,&faceId);CHKERRQ(ierr);
821     ierr = PetscObjectGetClassId(cellDisc,&cellId);CHKERRQ(ierr);
822     if (faceId != PETSCFE_CLASSID || cellId != PETSCFE_CLASSID) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not supported\n");
823     ierr = PetscFEGetDualSpace((PetscFE)cellDisc, &dsp);CHKERRQ(ierr);
824     ierr = PetscDualSpaceGetDM(dsp, &K); CHKERRQ(ierr);
825     ierr = DMPlexGetConeSize(K,0,&coneSize);CHKERRQ(ierr);
826     ierr = DMPlexGetCone(K,0,&coneK);CHKERRQ(ierr);
827     ierr = PetscMalloc4(numFaces,&co,dE*Nq,&cellPoints,coneSize,&counts,Nq,&dummyWeights);CHKERRQ(ierr);
828     ierr = PetscQuadratureCreate(PetscObjectComm((PetscObject)field), &cellQuad);CHKERRQ(ierr);
829     ierr = PetscQuadratureSetData(cellQuad, dE, 1, Nq, cellPoints, dummyWeights);CHKERRQ(ierr);
830     minOrient = PETSC_MAX_INT;
831     maxOrient = PETSC_MIN_INT;
832     for (p = 0; p < numFaces; p++) { /* record the orientation of the facet wrt the support cells */
833       PetscInt        point = points[p];
834       PetscInt        numSupp, numChildren;
835       const PetscInt *supp;
836 
837       ierr = DMPlexGetTreeChildren(dm, point, &numChildren, NULL); CHKERRQ(ierr);
838       if (numChildren) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Face data not valid for facets with children");
839       ierr = DMPlexGetSupportSize(dm, point,&numSupp);CHKERRQ(ierr);
840       if (numSupp > 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D has %D support, expected at most 2\n", point, numSupp);
841       ierr = DMPlexGetSupport(dm, point, &supp);CHKERRQ(ierr);
842       for (s = 0; s < numSupp; s++) {
843         PetscInt        cell = supp[s];
844         PetscInt        numCone;
845         const PetscInt *cone, *orient;
846 
847         ierr = DMPlexGetConeSize(dm, cell, &numCone);CHKERRQ(ierr);
848         if (numCone != coneSize) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support point does not match reference element");
849         ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
850         ierr = DMPlexGetConeOrientation(dm, cell, &orient);CHKERRQ(ierr);
851         for (f = 0; f < coneSize; f++) {
852           if (cone[f] == point) break;
853         }
854         co[p][s][0] = f;
855         co[p][s][1] = orient[f];
856         co[p][s][2] = cell;
857         minOrient = PetscMin(minOrient, orient[f]);
858         maxOrient = PetscMin(maxOrient, orient[f]);
859       }
860       for (; s < 2; s++) {
861         co[p][s][0] = -1;
862         co[p][s][1] = -1;
863         co[p][s][2] = -1;
864       }
865     }
866     numOrient = maxOrient + 1 - minOrient;
867     ierr = DMPlexGetCone(K,0,&coneK);CHKERRQ(ierr);
868     /* count all (face,orientation) doubles that appear */
869     ierr = PetscCalloc2(numOrient,&orients,numOrient,&orientPoints);CHKERRQ(ierr);
870     for (f = 0; f < coneSize; f++) {ierr = PetscCalloc1(numOrient, &counts[f]);CHKERRQ(ierr);}
871     for (p = 0; p < numFaces; p++) {
872       for (s = 0; s < 2; s++) {
873         if (co[p][s][0] >= 0) {
874           counts[co[p][s][0]][co[p][s][1] - minOrient]++;
875           orients[co[p][s][1] - minOrient]++;
876         }
877       }
878     }
879     for (o = 0; o < numOrient; o++) {
880       if (orients[o]) {
881         PetscInt orient = o + minOrient;
882         PetscInt q;
883 
884         ierr = PetscMalloc1(Nq * dim, &orientPoints[o]);CHKERRQ(ierr);
885         /* rotate the quadrature points appropriately */
886         switch (dim) {
887         case 0:
888           break;
889         case 1:
890           if (orient == -2 || orient == 1) {
891             for (q = 0; q < Nq; q++) {
892               orientPoints[o][q] = -geom->xi[q];
893             }
894           } else {
895             for (q = 0; q < Nq; q++) {
896               orientPoints[o][q] = geom->xi[q];
897             }
898           }
899           break;
900         case 2:
901           switch (coneSize) {
902           case 3:
903             for (q = 0; q < Nq; q++) {
904               PetscReal lambda[3];
905               PetscReal lambdao[3];
906 
907               /* convert to barycentric */
908               lambda[0] = - (geom->xi[2 * q] + geom->xi[2 * q + 1]) / 2.;
909               lambda[1] = (geom->xi[2 * q] + 1.) / 2.;
910               lambda[2] = (geom->xi[2 * q + 1] + 1.) / 2.;
911               if (orient >= 0) {
912                 for (i = 0; i < 3; i++) {
913                   lambdao[i] = lambda[(orient + i) % 3];
914                 }
915               } else {
916                 for (i = 0; i < 3; i++) {
917                   lambdao[i] = lambda[(-(orient + i) + 3) % 3];
918                 }
919               }
920               /* convert to coordinates */
921               orientPoints[o][2 * q + 0] = -(lambdao[0] + lambdao[2]) + lambdao[1];
922               orientPoints[o][2 * q + 1] = -(lambdao[0] + lambdao[1]) + lambdao[2];
923             }
924             break;
925           case 4:
926             for (q = 0; q < Nq; q++) {
927               PetscReal xi[2], xio[2];
928               PetscInt oabs = (orient >= 0) ? orient : -(orient + 1);
929 
930               xi[0] = geom->xi[2 * q];
931               xi[1] = geom->xi[2 * q + 1];
932               switch (oabs) {
933               case 1:
934                 xio[0] = xi[1];
935                 xio[1] = -xi[0];
936                 break;
937               case 2:
938                 xio[0] = -xi[0];
939                 xio[1] = -xi[1];
940               case 3:
941                 xio[0] = -xi[1];
942                 xio[1] = xi[0];
943               case 0:
944               default:
945                 xio[0] = xi[0];
946                 xio[1] = xi[1];
947                 break;
948               }
949               if (orient < 0) {
950                 xio[0] = -xio[0];
951               }
952               orientPoints[o][2 * q + 0] = xio[0];
953               orientPoints[o][2 * q + 1] = xio[1];
954             }
955             break;
956           default:
957             SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not implemented yet\n");
958           }
959         default:
960           SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not implemented yet\n");
961         }
962       }
963     }
964     for (f = 0; f < coneSize; f++) {
965       PetscInt face = coneK[f];
966       PetscReal v0[3];
967       PetscReal J[9];
968       PetscInt numCells, offset;
969       PetscInt *cells;
970       IS suppIS;
971 
972       ierr = DMPlexComputeCellGeometryFEM(K, face, NULL, v0, J, NULL, NULL);CHKERRQ(ierr);
973       for (o = 0; o <= numOrient; o++) {
974         PetscFEGeom *cellGeom;
975 
976         if (!counts[f][o]) continue;
977         /* If this (face,orientation) double appears,
978          * convert the face quadrature points into volume quadrature points */
979         for (q = 0; q < Nq; q++) {
980           PetscReal xi0[3] = {-1., -1., -1.};
981 
982           CoordinatesRefToReal(dE, dim, xi0, v0, J, &orientPoints[o][dim * q + 0], &cellPoints[dE * q + 0]);
983         }
984         for (p = 0, numCells = 0; p < numFaces; p++) {
985           for (s = 0; s < 2; s++) {
986             if (co[p][s][0] == f && co[p][s][1] == o + minOrient) numCells++;
987           }
988         }
989         ierr = PetscMalloc1(numCells, &cells);CHKERRQ(ierr);
990         for (p = 0, offset = 0; p < numFaces; p++) {
991           for (s = 0; s < 2; s++) {
992             if (co[p][s][0] == f && co[p][s][1] == o + minOrient) {
993               cells[offset++] = co[p][s][2];
994             }
995           }
996         }
997         ierr = ISCreateGeneral(PETSC_COMM_SELF,numCells,cells,PETSC_USE_POINTER, &suppIS);CHKERRQ(ierr);
998         ierr = DMFieldCreateFEGeom(field,suppIS,cellQuad,PETSC_FALSE,&cellGeom);CHKERRQ(ierr);
999         for (p = 0, offset = 0; p < numFaces; p++) {
1000           for (s = 0; s < 2; s++) {
1001             if (co[p][s][0] == f && co[p][s][1] == o + minOrient) {
1002               for (q = 0; q < Nq * dE * dE; q++) {
1003                 geom->suppInvJ[s][p * Nq * dE * dE + q] = cellGeom->invJ[offset * Nq * dE * dE + q];
1004               }
1005               offset++;
1006             }
1007           }
1008         }
1009         ierr = PetscFEGeomDestroy(&cellGeom);CHKERRQ(ierr);
1010         ierr = ISDestroy(&suppIS);CHKERRQ(ierr);
1011         ierr = PetscFree(cells);CHKERRQ(ierr);
1012       }
1013     }
1014     for (o = 0; o < numOrient; o++) {
1015       if (orients[o]) {
1016         ierr = PetscFree(orientPoints[o]);CHKERRQ(ierr);
1017       }
1018     }
1019     ierr = PetscFree2(orients,orientPoints);CHKERRQ(ierr);
1020     ierr = PetscQuadratureDestroy(&cellQuad);CHKERRQ(ierr);
1021     ierr = PetscFree4(co,cellPoints,counts,dummyWeights);CHKERRQ(ierr);
1022   }
1023   ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1024   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1025   PetscFunctionReturn(0);
1026 }
1027 
1028 static PetscErrorCode DMFieldInitialize_DS(DMField field)
1029 {
1030   PetscFunctionBegin;
1031   field->ops->destroy                 = DMFieldDestroy_DS;
1032   field->ops->evaluate                = DMFieldEvaluate_DS;
1033   field->ops->evaluateFE              = DMFieldEvaluateFE_DS;
1034   field->ops->evaluateFV              = DMFieldEvaluateFV_DS;
1035   field->ops->getFEInvariance         = DMFieldGetFEInvariance_DS;
1036   field->ops->createDefaultQuadrature = DMFieldCreateDefaultQuadrature_DS;
1037   field->ops->view                    = DMFieldView_DS;
1038   field->ops->computeFaceData         = DMFieldComputeFaceData_DS;
1039   PetscFunctionReturn(0);
1040 }
1041 
1042 PETSC_INTERN PetscErrorCode DMFieldCreate_DS(DMField field)
1043 {
1044   DMField_DS     *dsfield;
1045   PetscErrorCode ierr;
1046 
1047   PetscFunctionBegin;
1048   ierr = PetscNewLog(field,&dsfield);CHKERRQ(ierr);
1049   field->data = dsfield;
1050   ierr = DMFieldInitialize_DS(field);CHKERRQ(ierr);
1051   PetscFunctionReturn(0);
1052 }
1053 
1054 PetscErrorCode DMFieldCreateDS(DM dm, PetscInt fieldNum, Vec vec,DMField *field)
1055 {
1056   DMField        b;
1057   DMField_DS     *dsfield;
1058   PetscObject    disc = NULL;
1059   PetscBool      isContainer = PETSC_FALSE;
1060   PetscClassId   id = -1;
1061   PetscInt       numComponents = -1, dsNumFields;
1062   PetscSection   section;
1063   PetscErrorCode ierr;
1064 
1065   PetscFunctionBegin;
1066   ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1067   ierr = PetscSectionGetFieldComponents(section,fieldNum,&numComponents);CHKERRQ(ierr);
1068   ierr = DMGetNumFields(dm,&dsNumFields);CHKERRQ(ierr);
1069   if (dsNumFields) {ierr = DMGetField(dm,fieldNum,&disc);CHKERRQ(ierr);}
1070   if (disc) {
1071     ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1072     isContainer = (id == PETSC_CONTAINER_CLASSID) ? PETSC_TRUE : PETSC_FALSE;
1073   }
1074   if (!disc || isContainer) {
1075     MPI_Comm        comm = PetscObjectComm((PetscObject) dm);
1076     PetscInt        cStart, cEnd, dim;
1077     PetscInt        localConeSize = 0, coneSize;
1078     PetscFE         fe;
1079     PetscDualSpace  Q;
1080     PetscSpace      P;
1081     DM              K;
1082     PetscQuadrature quad, fquad;
1083     PetscBool       isSimplex;
1084 
1085     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1086     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1087     if (cEnd > cStart) {
1088       ierr = DMPlexGetConeSize(dm, cStart, &localConeSize);CHKERRQ(ierr);
1089     }
1090     ierr = MPI_Allreduce(&localConeSize,&coneSize,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
1091     isSimplex = (coneSize == (dim + 1)) ? PETSC_TRUE : PETSC_FALSE;
1092     ierr = PetscSpaceCreate(comm, &P);CHKERRQ(ierr);
1093     ierr = PetscSpaceSetOrder(P, 1);CHKERRQ(ierr);
1094     ierr = PetscSpaceSetNumComponents(P, numComponents);CHKERRQ(ierr);
1095     ierr = PetscSpaceSetType(P,PETSCSPACEPOLYNOMIAL);CHKERRQ(ierr);
1096     ierr = PetscSpaceSetNumVariables(P, dim);CHKERRQ(ierr);
1097     ierr = PetscSpacePolynomialSetTensor(P, isSimplex ? PETSC_FALSE : PETSC_TRUE);CHKERRQ(ierr);
1098     ierr = PetscSpaceSetUp(P);CHKERRQ(ierr);
1099     ierr = PetscDualSpaceCreate(comm, &Q);CHKERRQ(ierr);
1100     ierr = PetscDualSpaceSetType(Q,PETSCDUALSPACELAGRANGE);CHKERRQ(ierr);
1101     ierr = PetscDualSpaceCreateReferenceCell(Q, dim, isSimplex, &K);CHKERRQ(ierr);
1102     ierr = PetscDualSpaceSetDM(Q, K);CHKERRQ(ierr);
1103     ierr = DMDestroy(&K);CHKERRQ(ierr);
1104     ierr = PetscDualSpaceSetNumComponents(Q, numComponents);CHKERRQ(ierr);
1105     ierr = PetscDualSpaceSetOrder(Q, 1);CHKERRQ(ierr);
1106     ierr = PetscDualSpaceLagrangeSetTensor(Q, isSimplex ? PETSC_FALSE : PETSC_TRUE);CHKERRQ(ierr);
1107     ierr = PetscDualSpaceSetUp(Q);CHKERRQ(ierr);
1108     ierr = PetscFECreate(comm, &fe);CHKERRQ(ierr);
1109     ierr = PetscFESetType(fe,PETSCFEBASIC);CHKERRQ(ierr);
1110     ierr = PetscFESetBasisSpace(fe, P);CHKERRQ(ierr);
1111     ierr = PetscFESetDualSpace(fe, Q);CHKERRQ(ierr);
1112     ierr = PetscFESetNumComponents(fe, numComponents);CHKERRQ(ierr);
1113     ierr = PetscFESetUp(fe);CHKERRQ(ierr);
1114     ierr = PetscSpaceDestroy(&P);CHKERRQ(ierr);
1115     ierr = PetscDualSpaceDestroy(&Q);CHKERRQ(ierr);
1116     if (isSimplex) {
1117       ierr = PetscDTGaussJacobiQuadrature(dim,   1, 1, -1.0, 1.0, &quad);CHKERRQ(ierr);
1118       ierr = PetscDTGaussJacobiQuadrature(dim-1, 1, 1, -1.0, 1.0, &fquad);CHKERRQ(ierr);
1119     }
1120     else {
1121       ierr = PetscDTGaussTensorQuadrature(dim,   1, 1, -1.0, 1.0, &quad);CHKERRQ(ierr);
1122       ierr = PetscDTGaussTensorQuadrature(dim-1, 1, 1, -1.0, 1.0, &fquad);CHKERRQ(ierr);
1123     }
1124     ierr = PetscFESetQuadrature(fe, quad);CHKERRQ(ierr);
1125     ierr = PetscFESetFaceQuadrature(fe, fquad);CHKERRQ(ierr);
1126     ierr = PetscQuadratureDestroy(&quad);CHKERRQ(ierr);
1127     ierr = PetscQuadratureDestroy(&fquad);CHKERRQ(ierr);
1128     disc = (PetscObject) fe;
1129   } else {
1130     ierr = PetscObjectReference(disc);CHKERRQ(ierr);
1131   }
1132   ierr = PetscObjectGetClassId(disc,&id);CHKERRQ(ierr);
1133   if (id == PETSCFE_CLASSID) {
1134     PetscFE fe = (PetscFE) disc;
1135 
1136     ierr = PetscFEGetNumComponents(fe,&numComponents);CHKERRQ(ierr);
1137   } else {SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Not implemented");}
1138   ierr = DMFieldCreate(dm,numComponents,DMFIELD_VERTEX,&b);CHKERRQ(ierr);
1139   ierr = DMFieldSetType(b,DMFIELDDS);CHKERRQ(ierr);
1140   dsfield = (DMField_DS *) b->data;
1141   dsfield->fieldNum = fieldNum;
1142   ierr = DMGetDimension(dm,&dsfield->height);CHKERRQ(ierr);
1143   dsfield->height++;
1144   ierr = PetscCalloc1(dsfield->height,&dsfield->disc);CHKERRQ(ierr);
1145   dsfield->disc[0] = disc;
1146   ierr = PetscObjectReference((PetscObject)vec);CHKERRQ(ierr);
1147   dsfield->vec = vec;
1148   *field = b;
1149 
1150   PetscFunctionReturn(0);
1151 }
1152