xref: /petsc/src/dm/impls/plex/plexproject.c (revision 47923291d84d281e537c9eb35b619f1dfa674018)
1*47923291SMatthew G. Knepley #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2*47923291SMatthew G. Knepley 
3*47923291SMatthew G. Knepley #undef __FUNCT__
4*47923291SMatthew G. Knepley #define __FUNCT__ "DMProjectFunctionLocal_Plex"
5*47923291SMatthew G. Knepley PetscErrorCode DMProjectFunctionLocal_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
6*47923291SMatthew G. Knepley {
7*47923291SMatthew G. Knepley   PetscDualSpace *sp, *cellsp;
8*47923291SMatthew G. Knepley   PetscInt       *numComp;
9*47923291SMatthew G. Knepley   PetscSection    section;
10*47923291SMatthew G. Knepley   PetscScalar    *values;
11*47923291SMatthew G. Knepley   PetscInt        Nf, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, p, cStart, cEnd, cEndInterior, f, d, v, comp, h, maxHeight;
12*47923291SMatthew G. Knepley   PetscBool      *isFE, hasFE = PETSC_FALSE, hasFV = PETSC_FALSE;
13*47923291SMatthew G. Knepley   PetscErrorCode  ierr;
14*47923291SMatthew G. Knepley 
15*47923291SMatthew G. Knepley   PetscFunctionBegin;
16*47923291SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
17*47923291SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
18*47923291SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
19*47923291SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
20*47923291SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
21*47923291SMatthew G. Knepley   ierr = PetscMalloc3(Nf, &isFE, Nf, &sp, Nf, &numComp);CHKERRQ(ierr);
22*47923291SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
23*47923291SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
24*47923291SMatthew G. Knepley   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
25*47923291SMatthew G. Knepley   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_USER, "maximum projection height %d not in [0, %d)\n", maxHeight, dim);}
26*47923291SMatthew G. Knepley   if (maxHeight > 0) {
27*47923291SMatthew G. Knepley     ierr = PetscMalloc1(Nf, &cellsp);CHKERRQ(ierr);
28*47923291SMatthew G. Knepley   } else {
29*47923291SMatthew G. Knepley     cellsp = sp;
30*47923291SMatthew G. Knepley   }
31*47923291SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
32*47923291SMatthew G. Knepley     PetscObject  obj;
33*47923291SMatthew G. Knepley     PetscClassId id;
34*47923291SMatthew G. Knepley 
35*47923291SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
36*47923291SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
37*47923291SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
38*47923291SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
39*47923291SMatthew G. Knepley 
40*47923291SMatthew G. Knepley       hasFE   = PETSC_TRUE;
41*47923291SMatthew G. Knepley       isFE[f] = PETSC_TRUE;
42*47923291SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
43*47923291SMatthew G. Knepley       ierr  = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
44*47923291SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
45*47923291SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
46*47923291SMatthew G. Knepley 
47*47923291SMatthew G. Knepley       hasFV   = PETSC_TRUE;
48*47923291SMatthew G. Knepley       isFE[f] = PETSC_FALSE;
49*47923291SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
50*47923291SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &cellsp[f]);CHKERRQ(ierr);
51*47923291SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
52*47923291SMatthew G. Knepley   }
53*47923291SMatthew G. Knepley   /* Note: We make no attempt to optimize for height. Higher height things just overwrite the lower height results. */
54*47923291SMatthew G. Knepley   for (h = 0; h <= maxHeight; h++) {
55*47923291SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
56*47923291SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;} /* Respect hybrid bounds */
57*47923291SMatthew G. Knepley     if (pEnd <= pStart) continue;
58*47923291SMatthew G. Knepley     /* Compute totDim, the number of dofs in the closure of a point at this height */
59*47923291SMatthew G. Knepley     totDim = 0;
60*47923291SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
61*47923291SMatthew G. Knepley       if (!h) {
62*47923291SMatthew G. Knepley         sp[f] = cellsp[f];
63*47923291SMatthew G. Knepley       } else {
64*47923291SMatthew G. Knepley         ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
65*47923291SMatthew G. Knepley         if (!sp[f]) continue;
66*47923291SMatthew G. Knepley       }
67*47923291SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
68*47923291SMatthew G. Knepley       totDim += spDim*numComp[f];
69*47923291SMatthew G. Knepley     }
70*47923291SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
71*47923291SMatthew G. Knepley     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
72*47923291SMatthew G. Knepley     if (!totDim) continue;
73*47923291SMatthew G. Knepley     /* Loop over points at this height */
74*47923291SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
75*47923291SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
76*47923291SMatthew G. Knepley       PetscFECellGeom fegeom;
77*47923291SMatthew G. Knepley       PetscFVCellGeom fvgeom;
78*47923291SMatthew G. Knepley 
79*47923291SMatthew G. Knepley       if (hasFE) {
80*47923291SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, p, NULL, fegeom.v0, fegeom.J, NULL, &fegeom.detJ);CHKERRQ(ierr);
81*47923291SMatthew G. Knepley         fegeom.dim      = dim - h;
82*47923291SMatthew G. Knepley         fegeom.dimEmbed = dimEmbed;
83*47923291SMatthew G. Knepley       }
84*47923291SMatthew G. Knepley       if (hasFV) {ierr = DMPlexComputeCellGeometryFVM(dm, p, &fvgeom.volume, fvgeom.centroid, NULL);CHKERRQ(ierr);}
85*47923291SMatthew G. Knepley       /* Get values for closure */
86*47923291SMatthew G. Knepley       for (f = 0, v = 0; f < Nf; ++f) {
87*47923291SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[f] : NULL;
88*47923291SMatthew G. Knepley 
89*47923291SMatthew G. Knepley         if (!sp[f]) continue;o
90*47923291SMatthew G. Knepley         ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
91*47923291SMatthew G. Knepley         for (d = 0; d < spDim; ++d) {
92*47923291SMatthew G. Knepley           if (funcs[f]) {
93*47923291SMatthew G. Knepley             if (isFE[f]) {ierr = PetscDualSpaceApply(sp[f], d, time, &fegeom, numComp[f], funcs[f], ctx, &values[v]);}
94*47923291SMatthew G. Knepley             else         {ierr = PetscDualSpaceApplyFVM(sp[f], d, time, &fvgeom, numComp[f], funcs[f], ctx, &values[v]);}
95*47923291SMatthew G. Knepley             if (ierr) {
96*47923291SMatthew G. Knepley               PetscErrorCode ierr2;
97*47923291SMatthew G. Knepley               ierr2 = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr2);
98*47923291SMatthew G. Knepley               CHKERRQ(ierr);
99*47923291SMatthew G. Knepley             }
100*47923291SMatthew G. Knepley           } else {
101*47923291SMatthew G. Knepley             for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
102*47923291SMatthew G. Knepley           }
103*47923291SMatthew G. Knepley           v += numComp[f];
104*47923291SMatthew G. Knepley         }
105*47923291SMatthew G. Knepley       }
106*47923291SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, section, localX, p, values, mode);CHKERRQ(ierr);
107*47923291SMatthew G. Knepley     }
108*47923291SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
109*47923291SMatthew G. Knepley   }
110*47923291SMatthew G. Knepley   ierr = PetscFree3(isFE, sp, numComp);CHKERRQ(ierr);
111*47923291SMatthew G. Knepley   if (maxHeight > 0) {ierr = PetscFree(cellsp);CHKERRQ(ierr);}
112*47923291SMatthew G. Knepley   PetscFunctionReturn(0);
113*47923291SMatthew G. Knepley }
114*47923291SMatthew G. Knepley 
115*47923291SMatthew G. Knepley #undef __FUNCT__
116*47923291SMatthew G. Knepley #define __FUNCT__ "DMProjectFunctionLabelLocal_Plex"
117*47923291SMatthew G. Knepley PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
118*47923291SMatthew G. Knepley {
119*47923291SMatthew G. Knepley   PetscDualSpace *sp, *cellsp;
120*47923291SMatthew G. Knepley   PetscInt       *numComp;
121*47923291SMatthew G. Knepley   PetscSection    section;
122*47923291SMatthew G. Knepley   PetscScalar    *values;
123*47923291SMatthew G. Knepley   PetscBool      *fieldActive;
124*47923291SMatthew G. Knepley   PetscInt        numFields, dim, dimEmbed, spDim, totDim = 0, numValues, pStart, pEnd, cStart, cEnd, cEndInterior, f, d, v, i, comp, maxHeight, h;
125*47923291SMatthew G. Knepley   PetscErrorCode  ierr;
126*47923291SMatthew G. Knepley 
127*47923291SMatthew G. Knepley   PetscFunctionBegin;
128*47923291SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
129*47923291SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
130*47923291SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
131*47923291SMatthew G. Knepley   if (cEnd <= cStart) PetscFunctionReturn(0);
132*47923291SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
133*47923291SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
134*47923291SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
135*47923291SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
136*47923291SMatthew G. Knepley   ierr = PetscMalloc2(numFields,&sp,numFields,&numComp);CHKERRQ(ierr);
137*47923291SMatthew G. Knepley   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
138*47923291SMatthew G. Knepley   if (maxHeight < 0 || maxHeight > dim) {SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"maximum projection height %d not in [0, %d)\n", maxHeight,dim);}
139*47923291SMatthew G. Knepley   if (maxHeight > 0) {ierr = PetscMalloc1(numFields,&cellsp);CHKERRQ(ierr);}
140*47923291SMatthew G. Knepley   else               {cellsp = sp;}
141*47923291SMatthew G. Knepley   for (h = 0; h <= maxHeight; h++) {
142*47923291SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
143*47923291SMatthew G. Knepley     if (!h) {pStart = cStart; pEnd = cEnd;}
144*47923291SMatthew G. Knepley     if (pEnd <= pStart) continue;
145*47923291SMatthew G. Knepley     totDim = 0;
146*47923291SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
147*47923291SMatthew G. Knepley       PetscObject  obj;
148*47923291SMatthew G. Knepley       PetscClassId id;
149*47923291SMatthew G. Knepley 
150*47923291SMatthew G. Knepley       ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
151*47923291SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
152*47923291SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
153*47923291SMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
154*47923291SMatthew G. Knepley 
155*47923291SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
156*47923291SMatthew G. Knepley         if (!h) {
157*47923291SMatthew G. Knepley           ierr = PetscFEGetDualSpace(fe, &cellsp[f]);CHKERRQ(ierr);
158*47923291SMatthew G. Knepley           sp[f] = cellsp[f];
159*47923291SMatthew G. Knepley         } else {
160*47923291SMatthew G. Knepley           ierr = PetscDualSpaceGetHeightSubspace(cellsp[f], h, &sp[f]);CHKERRQ(ierr);
161*47923291SMatthew G. Knepley           if (!sp[f]) continue;
162*47923291SMatthew G. Knepley         }
163*47923291SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
164*47923291SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
165*47923291SMatthew G. Knepley 
166*47923291SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
167*47923291SMatthew G. Knepley         ierr = PetscFVGetDualSpace(fv, &cellsp[f]);CHKERRQ(ierr);
168*47923291SMatthew G. Knepley         sp[f] = cellsp[f];
169*47923291SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
170*47923291SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
171*47923291SMatthew G. Knepley       totDim += spDim*numComp[f];
172*47923291SMatthew G. Knepley     }
173*47923291SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localX, pStart, &numValues, NULL);CHKERRQ(ierr);
174*47923291SMatthew G. Knepley     if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section point closure size %d != dual space dimension %d", numValues, totDim);
175*47923291SMatthew G. Knepley     if (!totDim) continue;
176*47923291SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
177*47923291SMatthew G. Knepley     ierr = DMGetWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
178*47923291SMatthew G. Knepley     for (f = 0; f < numFields; ++f) fieldActive[f] = (funcs[f] && sp[f]) ? PETSC_TRUE : PETSC_FALSE;
179*47923291SMatthew G. Knepley     for (i = 0; i < numIds; ++i) {
180*47923291SMatthew G. Knepley       IS              pointIS;
181*47923291SMatthew G. Knepley       const PetscInt *points;
182*47923291SMatthew G. Knepley       PetscInt        n, p;
183*47923291SMatthew G. Knepley 
184*47923291SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, ids[i], &pointIS);CHKERRQ(ierr);
185*47923291SMatthew G. Knepley       if (!pointIS) continue; /* No points with that id on this process */
186*47923291SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &n);CHKERRQ(ierr);
187*47923291SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
188*47923291SMatthew G. Knepley       for (p = 0; p < n; ++p) {
189*47923291SMatthew G. Knepley         const PetscInt    point = points[p];
190*47923291SMatthew G. Knepley         PetscFECellGeom   geom;
191*47923291SMatthew G. Knepley 
192*47923291SMatthew G. Knepley         if ((point < pStart) || (point >= pEnd)) continue;
193*47923291SMatthew G. Knepley         ierr          = DMPlexComputeCellGeometryFEM(dm, point, NULL, geom.v0, geom.J, NULL, &geom.detJ);CHKERRQ(ierr);
194*47923291SMatthew G. Knepley         geom.dim      = dim - h;
195*47923291SMatthew G. Knepley         geom.dimEmbed = dimEmbed;
196*47923291SMatthew G. Knepley         for (f = 0, v = 0; f < numFields; ++f) {
197*47923291SMatthew G. Knepley           void * const ctx = ctxs ? ctxs[f] : NULL;
198*47923291SMatthew G. Knepley 
199*47923291SMatthew G. Knepley           if (!sp[f]) continue;
200*47923291SMatthew G. Knepley           ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
201*47923291SMatthew G. Knepley           for (d = 0; d < spDim; ++d) {
202*47923291SMatthew G. Knepley             if (funcs[f]) {
203*47923291SMatthew G. Knepley               ierr = PetscDualSpaceApply(sp[f], d, time, &geom, numComp[f], funcs[f], ctx, &values[v]);
204*47923291SMatthew G. Knepley               if (ierr) {
205*47923291SMatthew G. Knepley                 PetscErrorCode ierr2;
206*47923291SMatthew G. Knepley                 ierr2 = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr2);
207*47923291SMatthew G. Knepley                 ierr2 = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr2);
208*47923291SMatthew G. Knepley                 CHKERRQ(ierr);
209*47923291SMatthew G. Knepley               }
210*47923291SMatthew G. Knepley             } else {
211*47923291SMatthew G. Knepley               for (comp = 0; comp < numComp[f]; ++comp) values[v+comp] = 0.0;
212*47923291SMatthew G. Knepley             }
213*47923291SMatthew G. Knepley             v += numComp[f];
214*47923291SMatthew G. Knepley           }
215*47923291SMatthew G. Knepley         }
216*47923291SMatthew G. Knepley         ierr = DMPlexVecSetFieldClosure_Internal(dm, section, localX, fieldActive, point, values, mode);CHKERRQ(ierr);
217*47923291SMatthew G. Knepley       }
218*47923291SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
219*47923291SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
220*47923291SMatthew G. Knepley     }
221*47923291SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
222*47923291SMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, numFields, PETSC_BOOL, &fieldActive);CHKERRQ(ierr);
223*47923291SMatthew G. Knepley   }
224*47923291SMatthew G. Knepley   ierr = PetscFree2(sp, numComp);CHKERRQ(ierr);
225*47923291SMatthew G. Knepley   if (maxHeight > 0) {
226*47923291SMatthew G. Knepley     ierr = PetscFree(cellsp);CHKERRQ(ierr);
227*47923291SMatthew G. Knepley   }
228*47923291SMatthew G. Knepley   PetscFunctionReturn(0);
229*47923291SMatthew G. Knepley }
230*47923291SMatthew G. Knepley 
231*47923291SMatthew G. Knepley #undef __FUNCT__
232*47923291SMatthew G. Knepley #define __FUNCT__ "DMProjectFieldLocal_Plex"
233*47923291SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal_Plex(DM dm, Vec localU,
234*47923291SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
235*47923291SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
236*47923291SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
237*47923291SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscScalar[]),
238*47923291SMatthew G. Knepley                                         InsertMode mode, Vec localX)
239*47923291SMatthew G. Knepley {
240*47923291SMatthew G. Knepley   DM              dmAux;
241*47923291SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
242*47923291SMatthew G. Knepley   Vec             A;
243*47923291SMatthew G. Knepley   PetscSection    section, sectionAux = NULL;
244*47923291SMatthew G. Knepley   PetscDualSpace *sp;
245*47923291SMatthew G. Knepley   PetscInt       *Ncf;
246*47923291SMatthew G. Knepley   PetscScalar    *values, *u, *u_x, *a, *a_x;
247*47923291SMatthew G. Knepley   PetscReal      *x, *v0, *J, *invJ, detJ;
248*47923291SMatthew G. Knepley   PetscInt       *uOff, *uOff_x, *aOff = NULL, *aOff_x = NULL;
249*47923291SMatthew G. Knepley   PetscInt        Nf, NfAux = 0, dim, spDim, totDim, numValues, cStart, cEnd, cEndInterior, c, f, d, v, comp, maxHeight;
250*47923291SMatthew G. Knepley   PetscErrorCode  ierr;
251*47923291SMatthew G. Knepley 
252*47923291SMatthew G. Knepley   PetscFunctionBegin;
253*47923291SMatthew G. Knepley   ierr = DMPlexGetMaxProjectionHeight(dm,&maxHeight);CHKERRQ(ierr);
254*47923291SMatthew G. Knepley   if (maxHeight > 0) {SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Field projection for height > 0 not supported yet");}
255*47923291SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
256*47923291SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
257*47923291SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
258*47923291SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
259*47923291SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &sp, Nf, &Ncf);CHKERRQ(ierr);
260*47923291SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
261*47923291SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
262*47923291SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
263*47923291SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
264*47923291SMatthew G. Knepley   ierr = PetscDSGetEvaluationArrays(prob, &u, NULL, &u_x);CHKERRQ(ierr);
265*47923291SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
266*47923291SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
267*47923291SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
268*47923291SMatthew G. Knepley   if (dmAux) {
269*47923291SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
270*47923291SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
271*47923291SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
272*47923291SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
273*47923291SMatthew G. Knepley     ierr = PetscDSGetComponentDerivativeOffsets(probAux, &aOff_x);CHKERRQ(ierr);
274*47923291SMatthew G. Knepley     ierr = PetscDSGetEvaluationArrays(probAux, &a, NULL, &a_x);CHKERRQ(ierr);
275*47923291SMatthew G. Knepley   }
276*47923291SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localU, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
277*47923291SMatthew G. Knepley   ierr = DMPlexVecGetClosure(dm, section, localX, cStart, &numValues, NULL);CHKERRQ(ierr);
278*47923291SMatthew G. Knepley   if (numValues != totDim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The section cell closure size %d != dual space dimension %d", numValues, totDim);
279*47923291SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
280*47923291SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
281*47923291SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
282*47923291SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
283*47923291SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
284*47923291SMatthew G. Knepley     PetscScalar *coefficients = NULL, *coefficientsAux = NULL;
285*47923291SMatthew G. Knepley 
286*47923291SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
287*47923291SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
288*47923291SMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
289*47923291SMatthew G. Knepley     for (f = 0, v = 0; f < Nf; ++f) {
290*47923291SMatthew G. Knepley       PetscObject  obj;
291*47923291SMatthew G. Knepley       PetscClassId id;
292*47923291SMatthew G. Knepley 
293*47923291SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
294*47923291SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
295*47923291SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
296*47923291SMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
297*47923291SMatthew G. Knepley 
298*47923291SMatthew G. Knepley         ierr = PetscFEGetDualSpace(fe, &sp[f]);CHKERRQ(ierr);
299*47923291SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &Ncf[f]);CHKERRQ(ierr);
300*47923291SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
301*47923291SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
302*47923291SMatthew G. Knepley 
303*47923291SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Ncf[f]);CHKERRQ(ierr);
304*47923291SMatthew G. Knepley         ierr = PetscFVGetDualSpace(fv, &sp[f]);CHKERRQ(ierr);
305*47923291SMatthew G. Knepley       }
306*47923291SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(sp[f], &spDim);CHKERRQ(ierr);
307*47923291SMatthew G. Knepley       for (d = 0; d < spDim; ++d) {
308*47923291SMatthew G. Knepley         PetscQuadrature  quad;
309*47923291SMatthew G. Knepley         const PetscReal *points, *weights;
310*47923291SMatthew G. Knepley         PetscInt         numPoints, q;
311*47923291SMatthew G. Knepley 
312*47923291SMatthew G. Knepley         if (funcs[f]) {
313*47923291SMatthew G. Knepley           ierr = PetscDualSpaceGetFunctional(sp[f], d, &quad);CHKERRQ(ierr);
314*47923291SMatthew G. Knepley           ierr = PetscQuadratureGetData(quad, NULL, &numPoints, &points, &weights);CHKERRQ(ierr);
315*47923291SMatthew G. Knepley           for (q = 0; q < numPoints; ++q) {
316*47923291SMatthew G. Knepley             CoordinatesRefToReal(dim, dim, v0, J, &points[q*dim], x);
317*47923291SMatthew G. Knepley             ierr = EvaluateFieldJets(prob,    PETSC_FALSE, q, invJ, coefficients,    NULL, u, u_x, NULL);CHKERRQ(ierr);
318*47923291SMatthew G. Knepley             ierr = EvaluateFieldJets(probAux, PETSC_FALSE, q, invJ, coefficientsAux, NULL, a, a_x, NULL);CHKERRQ(ierr);
319*47923291SMatthew G. Knepley             (*funcs[f])(dim, Nf, NfAux, uOff, uOff_x, u, NULL, u_x, aOff, aOff_x, a, NULL, a_x, 0.0, x, &values[v]);
320*47923291SMatthew G. Knepley           }
321*47923291SMatthew G. Knepley         } else {
322*47923291SMatthew G. Knepley           for (comp = 0; comp < Ncf[f]; ++comp) values[v+comp] = 0.0;
323*47923291SMatthew G. Knepley         }
324*47923291SMatthew G. Knepley         v += Ncf[f];
325*47923291SMatthew G. Knepley       }
326*47923291SMatthew G. Knepley     }
327*47923291SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, localU, c, NULL, &coefficients);CHKERRQ(ierr);
328*47923291SMatthew G. Knepley     if (dmAux) {ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, c, NULL, &coefficientsAux);CHKERRQ(ierr);}
329*47923291SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, localX, c, values, mode);CHKERRQ(ierr);
330*47923291SMatthew G. Knepley   }
331*47923291SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
332*47923291SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numValues, PETSC_SCALAR, &values);CHKERRQ(ierr);
333*47923291SMatthew G. Knepley   ierr = PetscFree2(sp, Ncf);CHKERRQ(ierr);
334*47923291SMatthew G. Knepley   PetscFunctionReturn(0);
335*47923291SMatthew G. Knepley }
336