xref: /petsc/src/dm/impls/plex/plexfem.c (revision 648eda8ca8a072c40921b11a3edfa81e59ac4e57) !
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3bfabdd78SPatrick Farrell #include <petscsnes.h>
4cb1e1211SMatthew G Knepley 
5e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h>
6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
7af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
8a0845e3aSMatthew G. Knepley 
99b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
109b6f715bSMatthew G. Knepley {
119b6f715bSMatthew G. Knepley   PetscFEGeom *geom = (PetscFEGeom *) ctx;
129b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
139b6f715bSMatthew G. Knepley 
149b6f715bSMatthew G. Knepley   PetscFunctionBegin;
159b6f715bSMatthew G. Knepley   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
169b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
179b6f715bSMatthew G. Knepley }
189b6f715bSMatthew G. Knepley 
199b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
209b6f715bSMatthew G. Knepley {
219b6f715bSMatthew G. Knepley   char            composeStr[33] = {0};
229b6f715bSMatthew G. Knepley   PetscObjectId   id;
239b6f715bSMatthew G. Knepley   PetscContainer  container;
249b6f715bSMatthew G. Knepley   PetscErrorCode  ierr;
259b6f715bSMatthew G. Knepley 
269b6f715bSMatthew G. Knepley   PetscFunctionBegin;
279b6f715bSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
289b6f715bSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr);
299b6f715bSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
309b6f715bSMatthew G. Knepley   if (container) {
319b6f715bSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
329b6f715bSMatthew G. Knepley   } else {
339b6f715bSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
349b6f715bSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
359b6f715bSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
369b6f715bSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
379b6f715bSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
389b6f715bSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
399b6f715bSMatthew G. Knepley   }
409b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
419b6f715bSMatthew G. Knepley }
429b6f715bSMatthew G. Knepley 
439b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
449b6f715bSMatthew G. Knepley {
459b6f715bSMatthew G. Knepley   PetscFunctionBegin;
469b6f715bSMatthew G. Knepley   *geom = NULL;
479b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
489b6f715bSMatthew G. Knepley }
499b6f715bSMatthew G. Knepley 
5046fa42a0SMatthew G. Knepley /*@
5146fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
5246fa42a0SMatthew G. Knepley 
5346fa42a0SMatthew G. Knepley   Not collective
5446fa42a0SMatthew G. Knepley 
5546fa42a0SMatthew G. Knepley   Input Arguments:
5646fa42a0SMatthew G. Knepley + dm   - the DM
5746fa42a0SMatthew G. Knepley - unit - The SI unit
5846fa42a0SMatthew G. Knepley 
5946fa42a0SMatthew G. Knepley   Output Argument:
6046fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
6146fa42a0SMatthew G. Knepley 
6246fa42a0SMatthew G. Knepley   Level: advanced
6346fa42a0SMatthew G. Knepley 
6446fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
6546fa42a0SMatthew G. Knepley @*/
66cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
67cb1e1211SMatthew G Knepley {
68cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
69cb1e1211SMatthew G Knepley 
70cb1e1211SMatthew G Knepley   PetscFunctionBegin;
71cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
72cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
73cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
74cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
75cb1e1211SMatthew G Knepley }
76cb1e1211SMatthew G Knepley 
7746fa42a0SMatthew G. Knepley /*@
7846fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
7946fa42a0SMatthew G. Knepley 
8046fa42a0SMatthew G. Knepley   Not collective
8146fa42a0SMatthew G. Knepley 
8246fa42a0SMatthew G. Knepley   Input Arguments:
8346fa42a0SMatthew G. Knepley + dm   - the DM
8446fa42a0SMatthew G. Knepley . unit - The SI unit
8546fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
8646fa42a0SMatthew G. Knepley 
8746fa42a0SMatthew G. Knepley   Level: advanced
8846fa42a0SMatthew G. Knepley 
8946fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
9046fa42a0SMatthew G. Knepley @*/
91cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
92cb1e1211SMatthew G Knepley {
93cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
94cb1e1211SMatthew G Knepley 
95cb1e1211SMatthew G Knepley   PetscFunctionBegin;
96cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
97cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
98cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
99cb1e1211SMatthew G Knepley }
100cb1e1211SMatthew G Knepley 
101d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx)
102026175e5SToby Isaac {
10312adca46SMatthew G. Knepley   const PetscInt eps[3][3][3] = {{{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{0, 0, -1}, {0, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0}}};
104026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
105ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
106026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
107026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
108026175e5SToby Isaac 
109ad917190SMatthew G. Knepley   PetscFunctionBegin;
110ad917190SMatthew G. Knepley   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2);
111026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
112026175e5SToby Isaac   if (d < dim) {
11312adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
114ad917190SMatthew G. Knepley   } else {
115026175e5SToby Isaac     for (i = 0; i < dim; i++) {
116026175e5SToby Isaac       for (j = 0; j < dim; j++) {
11712adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
118026175e5SToby Isaac       }
119026175e5SToby Isaac     }
120026175e5SToby Isaac   }
121ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
122026175e5SToby Isaac }
123026175e5SToby Isaac 
124cc4e42d9SMartin Diehl /*@
12512adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
126cb1e1211SMatthew G Knepley 
127cb1e1211SMatthew G Knepley   Collective on DM
128cb1e1211SMatthew G Knepley 
129cb1e1211SMatthew G Knepley   Input Arguments:
130026175e5SToby Isaac . dm - the DM
131cb1e1211SMatthew G Knepley 
132cb1e1211SMatthew G Knepley   Output Argument:
133cb1e1211SMatthew G Knepley . sp - the null space
134cb1e1211SMatthew G Knepley 
13512adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
136cb1e1211SMatthew G Knepley 
137cb1e1211SMatthew G Knepley   Level: advanced
138cb1e1211SMatthew G Knepley 
13912adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
140cb1e1211SMatthew G Knepley @*/
141026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp)
142cb1e1211SMatthew G Knepley {
143cb1e1211SMatthew G Knepley   MPI_Comm       comm;
144026175e5SToby Isaac   Vec            mode[6];
145026175e5SToby Isaac   PetscSection   section, globalSection;
146b247467aSMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, mmin, d, i, j;
147cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
148cb1e1211SMatthew G Knepley 
149cb1e1211SMatthew G Knepley   PetscFunctionBegin;
150cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
151c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1529d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
153cb1e1211SMatthew G Knepley   if (dim == 1) {
154cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
155cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
156cb1e1211SMatthew G Knepley   }
157e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
158e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
159cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
160b247467aSMatthew G. Knepley   m    = (dim*(dim+1))/2;
161cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
162cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
163cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
164b247467aSMatthew G. Knepley   ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr);
165b247467aSMatthew G. Knepley   mmin = PetscMin(m, n);
166cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
167026175e5SToby Isaac   for (d = 0; d < m; d++) {
168330485fdSToby Isaac     PetscInt         ctx[2];
169d1828a1cSMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
170026175e5SToby Isaac     void            *voidctx = (void *) (&ctx[0]);
171cb1e1211SMatthew G Knepley 
1729d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
173330485fdSToby Isaac     ctx[1] = d;
1740709b2feSToby Isaac     ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
175cb1e1211SMatthew G Knepley   }
176b247467aSMatthew G. Knepley   for (i = 0; i < PetscMin(dim, mmin); ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
177cb1e1211SMatthew G Knepley   /* Orthonormalize system */
178b247467aSMatthew G. Knepley   for (i = dim; i < mmin; ++i) {
179cb1e1211SMatthew G Knepley     PetscScalar dots[6];
180cb1e1211SMatthew G Knepley 
181cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
182cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
183cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
184cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
185cb1e1211SMatthew G Knepley   }
186b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
187b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
188cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
189cb1e1211SMatthew G Knepley }
190cb1e1211SMatthew G Knepley 
191cc4e42d9SMartin Diehl /*@
19212adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
19312adca46SMatthew G. Knepley 
19412adca46SMatthew G. Knepley   Collective on DM
19512adca46SMatthew G. Knepley 
19612adca46SMatthew G. Knepley   Input Arguments:
19712adca46SMatthew G. Knepley + dm    - the DM
19812adca46SMatthew G. Knepley . nb    - The number of bodies
19912adca46SMatthew G. Knepley . label - The DMLabel marking each domain
20012adca46SMatthew G. Knepley . nids  - The number of ids per body
20112adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
20212adca46SMatthew G. Knepley 
20312adca46SMatthew G. Knepley   Output Argument:
20412adca46SMatthew G. Knepley . sp - the null space
20512adca46SMatthew G. Knepley 
20612adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
20712adca46SMatthew G. Knepley 
20812adca46SMatthew G. Knepley   Level: advanced
20912adca46SMatthew G. Knepley 
21012adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
21112adca46SMatthew G. Knepley @*/
21212adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
21312adca46SMatthew G. Knepley {
21412adca46SMatthew G. Knepley   MPI_Comm       comm;
21512adca46SMatthew G. Knepley   PetscSection   section, globalSection;
21612adca46SMatthew G. Knepley   Vec           *mode;
21712adca46SMatthew G. Knepley   PetscScalar   *dots;
21812adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
21912adca46SMatthew G. Knepley   PetscErrorCode ierr;
22012adca46SMatthew G. Knepley 
22112adca46SMatthew G. Knepley   PetscFunctionBegin;
22212adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
22312adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
22412adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
225e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
226e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
22712adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
22812adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
22912adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
23012adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
23112adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
23212adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
23312adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
23412adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
23512adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
23612adca46SMatthew G. Knepley       PetscInt         ctx[2];
23712adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
23812adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
23912adca46SMatthew G. Knepley 
24012adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
24112adca46SMatthew G. Knepley       ctx[1] = d;
24212adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
24312adca46SMatthew G. Knepley       off   += nids[b];
24412adca46SMatthew G. Knepley     }
24512adca46SMatthew G. Knepley   }
24612adca46SMatthew G. Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
24712adca46SMatthew G. Knepley   /* Orthonormalize system */
24812adca46SMatthew G. Knepley   for (i = 0; i < m; ++i) {
24912adca46SMatthew G. Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
25012adca46SMatthew G. Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
25112adca46SMatthew G. Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
25212adca46SMatthew G. Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
25312adca46SMatthew G. Knepley   }
25412adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
25512adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
25612adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
25712adca46SMatthew G. Knepley   PetscFunctionReturn(0);
25812adca46SMatthew G. Knepley }
25912adca46SMatthew G. Knepley 
260b29cfa1cSToby Isaac /*@
261b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
262b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
263b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
264b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
265b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
266b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
267b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
268b29cfa1cSToby Isaac 
269b29cfa1cSToby Isaac   Input Parameters:
270b29cfa1cSToby Isaac + dm - the DMPlex object
271b29cfa1cSToby Isaac - height - the maximum projection height >= 0
272b29cfa1cSToby Isaac 
273b29cfa1cSToby Isaac   Level: advanced
274b29cfa1cSToby Isaac 
2754d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
276b29cfa1cSToby Isaac @*/
277b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
278b29cfa1cSToby Isaac {
279b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
280b29cfa1cSToby Isaac 
281b29cfa1cSToby Isaac   PetscFunctionBegin;
282b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
283b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
284b29cfa1cSToby Isaac   PetscFunctionReturn(0);
285b29cfa1cSToby Isaac }
286b29cfa1cSToby Isaac 
287b29cfa1cSToby Isaac /*@
288b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
289b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
290b29cfa1cSToby Isaac 
291b29cfa1cSToby Isaac   Input Parameters:
292b29cfa1cSToby Isaac . dm - the DMPlex object
293b29cfa1cSToby Isaac 
294b29cfa1cSToby Isaac   Output Parameters:
295b29cfa1cSToby Isaac . height - the maximum projection height
296b29cfa1cSToby Isaac 
297b29cfa1cSToby Isaac   Level: intermediate
298b29cfa1cSToby Isaac 
2994d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
300b29cfa1cSToby Isaac @*/
301b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
302b29cfa1cSToby Isaac {
303b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
304b29cfa1cSToby Isaac 
305b29cfa1cSToby Isaac   PetscFunctionBegin;
306b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
307b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
308b29cfa1cSToby Isaac   PetscFunctionReturn(0);
309b29cfa1cSToby Isaac }
310b29cfa1cSToby Isaac 
311b278463cSMatthew G. Knepley /*@C
312b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector
313b278463cSMatthew G. Knepley 
314b278463cSMatthew G. Knepley   Input Parameters:
315b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
316b278463cSMatthew G. Knepley . time   - The time
317b278463cSMatthew G. Knepley . field  - The field to constrain
3181c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3191c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
320b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
321b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
322b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
323b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
324b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
325b278463cSMatthew G. Knepley 
326b278463cSMatthew G. Knepley   Output Parameter:
327b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
328b278463cSMatthew G. Knepley 
329b278463cSMatthew G. Knepley   Level: developer
330b278463cSMatthew G. Knepley 
331b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
332b278463cSMatthew G. Knepley @*/
3331c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssential(DM dm, PetscReal time, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[], PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void *ctx, Vec locX)
33455f2e967SMatthew G. Knepley {
3350163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
33655f2e967SMatthew G. Knepley   void            **ctxs;
337d7ddef95SMatthew G. Knepley   PetscInt          numFields;
338d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
339d7ddef95SMatthew G. Knepley 
340d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
341d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
342d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
343d7ddef95SMatthew G. Knepley   funcs[field] = func;
344d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
3451c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
346d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
347d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
348d7ddef95SMatthew G. Knepley }
349d7ddef95SMatthew G. Knepley 
350b278463cSMatthew G. Knepley /*@C
351b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector
352b278463cSMatthew G. Knepley 
353b278463cSMatthew G. Knepley   Input Parameters:
354b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
355b278463cSMatthew G. Knepley . time   - The time
356b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
357b278463cSMatthew G. Knepley . field  - The field to constrain
3581c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3591c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
360b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
361b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
362b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
363b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
364b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
365b278463cSMatthew G. Knepley 
366b278463cSMatthew G. Knepley   Output Parameter:
367b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
368b278463cSMatthew G. Knepley 
369b278463cSMatthew G. Knepley   Level: developer
370b278463cSMatthew G. Knepley 
371b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary()
372b278463cSMatthew G. Knepley @*/
3731c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
374c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
375c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
376c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
37797b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
378b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
379b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
380c60e475cSMatthew G. Knepley {
381c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
382c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
383c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
38497b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
385c60e475cSMatthew G. Knepley   void            **ctxs;
386c60e475cSMatthew G. Knepley   PetscInt          numFields;
387c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
388c60e475cSMatthew G. Knepley 
389c60e475cSMatthew G. Knepley   PetscFunctionBegin;
390c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
391c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
392c60e475cSMatthew G. Knepley   funcs[field] = func;
393c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
3941c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
395c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
396c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
397c60e475cSMatthew G. Knepley }
398c60e475cSMatthew G. Knepley 
399b278463cSMatthew G. Knepley /*@C
400b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
401b278463cSMatthew G. Knepley 
402b278463cSMatthew G. Knepley   Input Parameters:
403b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
404b278463cSMatthew G. Knepley . time   - The time
405b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
406b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
407b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
408b278463cSMatthew G. Knepley . field  - The field to constrain
4091c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
4101c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
411b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
412b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
413b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
414b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
415b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
416b278463cSMatthew G. Knepley 
417b278463cSMatthew G. Knepley   Output Parameter:
418b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
419b278463cSMatthew G. Knepley 
420b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
421b278463cSMatthew G. Knepley 
422b278463cSMatthew G. Knepley   Level: developer
423b278463cSMatthew G. Knepley 
424b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
425b278463cSMatthew G. Knepley @*/
4261c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesRiemann(DM dm, PetscReal time, Vec faceGeometry, Vec cellGeometry, Vec Grad, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
427b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
428d7ddef95SMatthew G. Knepley {
42961f58d28SMatthew G. Knepley   PetscDS            prob;
430da97024aSMatthew G. Knepley   PetscSF            sf;
431d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
43220369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
433da97024aSMatthew G. Knepley   const PetscInt    *leaves;
434d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
435520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
436e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
437d7ddef95SMatthew G. Knepley 
438d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
439da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
440da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
441da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
442d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
443d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
44461f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
445d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
446d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
44720369375SToby Isaac   if (cellGeometry) {
448d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
449d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
45020369375SToby Isaac   }
451d7ddef95SMatthew G. Knepley   if (Grad) {
452c0a6632aSMatthew G. Knepley     PetscFV fv;
453c0a6632aSMatthew G. Knepley 
454c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
455d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
456d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
457d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
45869291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
459d7ddef95SMatthew G. Knepley   }
460d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
461d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
462d7ddef95SMatthew G. Knepley     IS              faceIS;
463d7ddef95SMatthew G. Knepley     const PetscInt *faces;
464d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
465d7ddef95SMatthew G. Knepley 
466d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
467d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
468d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
469d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
470d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
471d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
472640bce14SSatish Balay       PetscFVFaceGeom        *fg;
473d7ddef95SMatthew G. Knepley 
474d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
475da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
476da97024aSMatthew G. Knepley       if (loc >= 0) continue;
477d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
478d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
479d7ddef95SMatthew G. Knepley       if (Grad) {
480640bce14SSatish Balay         PetscFVCellGeom       *cg;
481640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
482d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
483d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
484d7ddef95SMatthew G. Knepley         PetscInt               d;
485d7ddef95SMatthew G. Knepley 
486d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
487d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
488d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
489520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
490d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
491d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
492e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
493e735a8a9SMatthew G. Knepley         if (ierru) {
494e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
495e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
496e735a8a9SMatthew G. Knepley           goto cleanup;
497e735a8a9SMatthew G. Knepley         }
498d7ddef95SMatthew G. Knepley       } else {
499640bce14SSatish Balay         PetscScalar       *xI;
500d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
501d7ddef95SMatthew G. Knepley 
502d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
503520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
504e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
505e735a8a9SMatthew G. Knepley         if (ierru) {
506e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
507e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
508e735a8a9SMatthew G. Knepley           goto cleanup;
509e735a8a9SMatthew G. Knepley         }
510d7ddef95SMatthew G. Knepley       }
511d7ddef95SMatthew G. Knepley     }
512d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
513d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
514d7ddef95SMatthew G. Knepley   }
515e735a8a9SMatthew G. Knepley   cleanup:
516d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
517d7ddef95SMatthew G. Knepley   if (Grad) {
51869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
519d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
520d7ddef95SMatthew G. Knepley   }
521e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
522d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
523e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
524d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
525d7ddef95SMatthew G. Knepley }
526d7ddef95SMatthew G. Knepley 
527f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
528d7ddef95SMatthew G. Knepley {
529e5e52638SMatthew G. Knepley   PetscDS        prob;
530d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
53155f2e967SMatthew G. Knepley   PetscErrorCode ierr;
53255f2e967SMatthew G. Knepley 
53355f2e967SMatthew G. Knepley   PetscFunctionBegin;
534e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
535e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
53655f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
537f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
538*648eda8cSMatthew G. Knepley     const char             *name, *labelname;
539d7ddef95SMatthew G. Knepley     DMLabel                 label;
5401c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
5411c531cf8SMatthew G. Knepley     const PetscInt         *comps;
542d7ddef95SMatthew G. Knepley     PetscObject             obj;
543d7ddef95SMatthew G. Knepley     PetscClassId            id;
544a30ec4eaSSatish Balay     void                    (*func)(void);
545d7ddef95SMatthew G. Knepley     PetscInt                numids;
546d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
54755f2e967SMatthew G. Knepley     void                   *ctx;
54855f2e967SMatthew G. Knepley 
549*648eda8cSMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
550f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
551c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
552*648eda8cSMatthew G. Knepley     if (!label) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "Label %s for boundary condition %s does not exist in the DM", labelname, name);
55344a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
554d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
555d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
556c60e475cSMatthew G. Knepley       switch (type) {
557c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
558c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
559092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
5601c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
561092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
562c60e475cSMatthew G. Knepley         break;
563c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
564c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
5651c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
566b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
567c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
56897b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
569c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
570c60e475cSMatthew G. Knepley         break;
571c60e475cSMatthew G. Knepley       default: break;
572c60e475cSMatthew G. Knepley       }
573d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
57443ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
5751c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
576b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
577d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
57855f2e967SMatthew G. Knepley   }
57955f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
58055f2e967SMatthew G. Knepley }
58155f2e967SMatthew G. Knepley 
582f1d73a7aSMatthew G. Knepley /*@
583f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
584f1d73a7aSMatthew G. Knepley 
585f1d73a7aSMatthew G. Knepley   Input Parameters:
586f1d73a7aSMatthew G. Knepley + dm - The DM
587f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
588f1d73a7aSMatthew G. Knepley . time - The time
589f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
590f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
591f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
592f1d73a7aSMatthew G. Knepley 
593f1d73a7aSMatthew G. Knepley   Output Parameters:
594f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
595f1d73a7aSMatthew G. Knepley 
596f1d73a7aSMatthew G. Knepley   Level: developer
597f1d73a7aSMatthew G. Knepley 
598f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
599f1d73a7aSMatthew G. Knepley @*/
600f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
601f1d73a7aSMatthew G. Knepley {
602f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
603f1d73a7aSMatthew G. Knepley 
604f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
605f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
606f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
607f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
608f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
609f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
610f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
611f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
612f1d73a7aSMatthew G. Knepley }
613f1d73a7aSMatthew G. Knepley 
6140709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
615cb1e1211SMatthew G Knepley {
616574a98acSMatthew G. Knepley   Vec              localX;
617574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
618574a98acSMatthew G. Knepley 
619574a98acSMatthew G. Knepley   PetscFunctionBegin;
620574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
6215d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
622574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
623574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
624574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
625574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
626574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
627574a98acSMatthew G. Knepley }
628574a98acSMatthew G. Knepley 
629574a98acSMatthew G. Knepley /*@C
630574a98acSMatthew G. Knepley   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
631574a98acSMatthew G. Knepley 
632574a98acSMatthew G. Knepley   Input Parameters:
633574a98acSMatthew G. Knepley + dm     - The DM
634574a98acSMatthew G. Knepley . time   - The time
635574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
636574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
637574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
638574a98acSMatthew G. Knepley 
639574a98acSMatthew G. Knepley   Output Parameter:
640574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
641574a98acSMatthew G. Knepley 
642574a98acSMatthew G. Knepley   Level: developer
643574a98acSMatthew G. Knepley 
644574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
645574a98acSMatthew G. Knepley @*/
646574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
647574a98acSMatthew G. Knepley {
6480f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
649cb1e1211SMatthew G Knepley   PetscSection     section;
650c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
65115496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
6527318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
653cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
6547318780aSToby Isaac   const PetscReal *quadWeights;
655aed3cbd0SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset;
656cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
657cb1e1211SMatthew G Knepley 
658cb1e1211SMatthew G Knepley   PetscFunctionBegin;
659c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6607318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
661e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
662cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
663cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
66415496722SMatthew G. Knepley     PetscObject  obj;
66515496722SMatthew G. Knepley     PetscClassId id;
666c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
667c5bbbd5bSMatthew G. Knepley 
66844a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
66915496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
67015496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
67115496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
67215496722SMatthew G. Knepley 
6730f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
6740f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
67515496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
67615496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
67715496722SMatthew G. Knepley 
67815496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
67915496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
68015496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
681c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
682cb1e1211SMatthew G Knepley   }
6839c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
684beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
6857318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
686aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
687aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6889ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6899ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
690cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
691a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
692cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
6939c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
694cb1e1211SMatthew G Knepley 
6957318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
696cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
697cb1e1211SMatthew G Knepley 
69815496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
69915496722SMatthew G. Knepley       PetscObject  obj;
70015496722SMatthew G. Knepley       PetscClassId id;
701c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
70215496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
703cb1e1211SMatthew G Knepley 
70444a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
70515496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
70615496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
70715496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
70815496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
709cb1e1211SMatthew G Knepley       if (debug) {
710cb1e1211SMatthew G Knepley         char title[1024];
711cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
7124c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
713cb1e1211SMatthew G Knepley       }
7147318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
7157318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", detJ[q], c, q);
7167318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim * q], Nc, funcVal, ctx);
717e735a8a9SMatthew G. Knepley         if (ierr) {
718e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
719e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
720e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
7217318780aSToby Isaac           ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
722e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
723e735a8a9SMatthew G. Knepley         }
72415496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
72515496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
72615496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
72715496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
728beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
729beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
730beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
731cb1e1211SMatthew G Knepley         }
732cb1e1211SMatthew G Knepley       }
7339c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
734beaa55a6SMatthew G. Knepley       qc += Nc;
735cb1e1211SMatthew G Knepley     }
736cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
737cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
738cb1e1211SMatthew G Knepley     localDiff += elemDiff;
739cb1e1211SMatthew G Knepley   }
7407318780aSToby Isaac   ierr  = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
741b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
742cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
743cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
744cb1e1211SMatthew G Knepley }
745cb1e1211SMatthew G Knepley 
746b698f381SToby Isaac PetscErrorCode DMComputeL2GradientDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
747cb1e1211SMatthew G Knepley {
7480f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
749cb1e1211SMatthew G Knepley   PetscSection     section;
75040e14135SMatthew G. Knepley   PetscQuadrature  quad;
75140e14135SMatthew G. Knepley   Vec              localX;
7529c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
7539c3cf19fSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7547318780aSToby Isaac   PetscReal       *coords, *realSpaceDer, *J, *invJ, *detJ;
75540e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
7569c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset;
757cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
758cb1e1211SMatthew G Knepley 
759cb1e1211SMatthew G Knepley   PetscFunctionBegin;
760c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7617318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
762e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
76340e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
76440e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
76540e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
76640e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
767652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
7680f2d7e86SMatthew G. Knepley     PetscFE  fe;
76940e14135SMatthew G. Knepley     PetscInt Nc;
770652b88e8SMatthew G. Knepley 
77144a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
7720f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7730f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
77440e14135SMatthew G. Knepley     numComponents += Nc;
775652b88e8SMatthew G. Knepley   }
7769c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
777beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
7784d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
7799c3cf19fSMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*Nq,&realSpaceDer,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ,numComponents,&interpolant,Nq,&detJ);CHKERRQ(ierr);
78040e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
7819ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
7829ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
78340e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
78440e14135SMatthew G. Knepley     PetscScalar *x = NULL;
78540e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
7869c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
787652b88e8SMatthew G. Knepley 
7887318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
78940e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
79040e14135SMatthew G. Knepley 
7919c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
7920f2d7e86SMatthew G. Knepley       PetscFE          fe;
79351259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
79440e14135SMatthew G. Knepley       PetscReal       *basisDer;
7959c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
79640e14135SMatthew G. Knepley 
79744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
7980f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
7999c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
8000f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
80140e14135SMatthew G. Knepley       if (debug) {
80240e14135SMatthew G. Knepley         char title[1024];
80340e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
8049c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
805652b88e8SMatthew G. Knepley       }
8069c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
8077318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", detJ[q], c, q);
8087318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx);
809e735a8a9SMatthew G. Knepley         if (ierr) {
810e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
811e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
812e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
8139c3cf19fSMatthew G. Knepley           ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2);
814e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
815e735a8a9SMatthew G. Knepley         }
8169c3cf19fSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr);
8179c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
818beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
819beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
820beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
82140e14135SMatthew G. Knepley         }
82240e14135SMatthew G. Knepley       }
8239c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
8249c3cf19fSMatthew G. Knepley       qc          += Nc;
82540e14135SMatthew G. Knepley     }
82640e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
82740e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
82840e14135SMatthew G. Knepley     localDiff += elemDiff;
82940e14135SMatthew G. Knepley   }
8309c3cf19fSMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr);
83140e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
832b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
83340e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
834cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
835cb1e1211SMatthew G Knepley }
836cb1e1211SMatthew G Knepley 
837c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
83873d901b8SMatthew G. Knepley {
8390f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
84073d901b8SMatthew G. Knepley   PetscSection     section;
84173d901b8SMatthew G. Knepley   PetscQuadrature  quad;
84273d901b8SMatthew G. Knepley   Vec              localX;
84315496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
8447318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
84573d901b8SMatthew G. Knepley   PetscReal       *localDiff;
84615496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
8479c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
84873d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
84973d901b8SMatthew G. Knepley 
85073d901b8SMatthew G. Knepley   PetscFunctionBegin;
851c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8527318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
853e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
85473d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
85573d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
856bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
85773d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
85873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
85973d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
86015496722SMatthew G. Knepley     PetscObject  obj;
86115496722SMatthew G. Knepley     PetscClassId id;
86273d901b8SMatthew G. Knepley     PetscInt     Nc;
86373d901b8SMatthew G. Knepley 
86444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
86515496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
86615496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
86715496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
86815496722SMatthew G. Knepley 
8690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8700f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
87115496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
87215496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
87315496722SMatthew G. Knepley 
87415496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
87515496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
87615496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
87773d901b8SMatthew G. Knepley     numComponents += Nc;
87873d901b8SMatthew G. Knepley   }
8799c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
880beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
8817318780aSToby Isaac   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
88273d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8839ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8849ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
88573d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
88673d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
8879c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
88873d901b8SMatthew G. Knepley 
8897318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
89073d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
89173d901b8SMatthew G. Knepley 
89215496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
89315496722SMatthew G. Knepley       PetscObject  obj;
89415496722SMatthew G. Knepley       PetscClassId id;
89573d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
89615496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
89773d901b8SMatthew G. Knepley 
89815496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
89915496722SMatthew G. Knepley 
90044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
90115496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
90215496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
90315496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
90415496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
90573d901b8SMatthew G. Knepley       if (debug) {
90673d901b8SMatthew G. Knepley         char title[1024];
90773d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
90815496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
90973d901b8SMatthew G. Knepley       }
9107318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
9117318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature point %D", detJ, c, q);
9127318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim*q], numFields, funcVal, ctx);
913e735a8a9SMatthew G. Knepley         if (ierr) {
914e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
915e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
916e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
9177318780aSToby Isaac           ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
918e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
919e735a8a9SMatthew G. Knepley         }
92015496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
92115496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
92215496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
92315496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
924beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
925beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d point %g %g %g diff %g\n", c, field, coordDim > 0 ? coords[0] : 0., coordDim > 1 ? coords[1] : 0., coordDim > 2 ? coords[2] : 0., PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
926beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
92773d901b8SMatthew G. Knepley         }
92873d901b8SMatthew G. Knepley       }
929beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
9309c3cf19fSMatthew G. Knepley       qc          += Nc;
93173d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
93273d901b8SMatthew G. Knepley     }
93373d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
93473d901b8SMatthew G. Knepley   }
93573d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
936b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
93773d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
9387318780aSToby Isaac   ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
93973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
94073d901b8SMatthew G. Knepley }
94173d901b8SMatthew G. Knepley 
942e729f68cSMatthew G. Knepley /*@C
943e729f68cSMatthew G. Knepley   DMPlexComputeL2DiffVec - This function computes the cellwise L_2 difference between a function u and an FEM interpolant solution u_h, and stores it in a Vec.
944e729f68cSMatthew G. Knepley 
945e729f68cSMatthew G. Knepley   Input Parameters:
946e729f68cSMatthew G. Knepley + dm    - The DM
9470163fd50SMatthew G. Knepley . time  - The time
948ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
949e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
950e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
951e729f68cSMatthew G. Knepley 
952e729f68cSMatthew G. Knepley   Output Parameter:
953e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
954e729f68cSMatthew G. Knepley 
955e729f68cSMatthew G. Knepley   Level: developer
956e729f68cSMatthew G. Knepley 
957b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
958e729f68cSMatthew G. Knepley @*/
9590163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
960e729f68cSMatthew G. Knepley {
961e729f68cSMatthew G. Knepley   PetscSection     section;
962e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
963e729f68cSMatthew G. Knepley   Vec              localX;
964e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
9657318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
966e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
9679c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
968e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
969e729f68cSMatthew G. Knepley 
970e729f68cSMatthew G. Knepley   PetscFunctionBegin;
971e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
972e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
9737318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
974e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
975e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
976e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
977bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
978e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
979e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
980e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
981e729f68cSMatthew G. Knepley     PetscObject  obj;
982e729f68cSMatthew G. Knepley     PetscClassId id;
983e729f68cSMatthew G. Knepley     PetscInt     Nc;
984e729f68cSMatthew G. Knepley 
98544a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
986e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
987e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
988e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
989e729f68cSMatthew G. Knepley 
990e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
991e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
992e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
993e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
994e729f68cSMatthew G. Knepley 
995e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
996e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
997e729f68cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
998e729f68cSMatthew G. Knepley     numComponents += Nc;
999e729f68cSMatthew G. Knepley   }
10009c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1001beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
10027318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
1003e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1004e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1005e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1006e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1007e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
10086f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
10099c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1010e729f68cSMatthew G. Knepley 
10117318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
1012e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1013e729f68cSMatthew G. Knepley 
1014e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1015e729f68cSMatthew G. Knepley       PetscObject  obj;
1016e729f68cSMatthew G. Knepley       PetscClassId id;
1017e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1018e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1019e729f68cSMatthew G. Knepley 
102044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1021e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1022e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1023e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1024e729f68cSMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
102523f34ed2SToby Isaac       if (funcs[field]) {
10267318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
10277f79407eSBarry Smith           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], c, q);
1028274e8aaeSMatthew G. Knepley           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);
1029e735a8a9SMatthew G. Knepley           if (ierr) {
1030e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1031e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
10327f79407eSBarry Smith             ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
1033e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1034e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1035e735a8a9SMatthew G. Knepley           }
1036e729f68cSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1037e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1038e729f68cSMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1039e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1040beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1041beaa55a6SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
1042e729f68cSMatthew G. Knepley           }
1043e729f68cSMatthew G. Knepley         }
104423f34ed2SToby Isaac       }
1045beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
10469c3cf19fSMatthew G. Knepley       qc          += Nc;
1047e729f68cSMatthew G. Knepley     }
1048e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
104923f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1050e729f68cSMatthew G. Knepley   }
10517318780aSToby Isaac   ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
1052e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1053e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1054e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1055e729f68cSMatthew G. Knepley }
1056e729f68cSMatthew G. Knepley 
10571555c271SMatthew G. Knepley /*@C
10581555c271SMatthew G. Knepley   DMPlexComputeGradientClementInterpolant - This function computes the L2 projection of the cellwise gradient of a function u onto P1, and stores it in a Vec.
10591555c271SMatthew G. Knepley 
10601555c271SMatthew G. Knepley   Input Parameters:
10611555c271SMatthew G. Knepley + dm - The DM
10621555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
10631555c271SMatthew G. Knepley 
10641555c271SMatthew G. Knepley   Output Parameter:
10651555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
10661555c271SMatthew G. Knepley 
106795452b02SPatrick Sanan   Notes:
106895452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
10691555c271SMatthew G. Knepley   \nabla u_h(v_i) = \sum_{T_i \in support(v_i)} |T_i| \nabla u_h(T_i) / \sum_{T_i \in support(v_i)} |T_i| where |T_i| is the cell volume
10701555c271SMatthew G. Knepley 
10711555c271SMatthew G. Knepley   Level: developer
10721555c271SMatthew G. Knepley 
10731555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
10741555c271SMatthew G. Knepley @*/
10751555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
10761555c271SMatthew G. Knepley {
1077db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1078db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
10791555c271SMatthew G. Knepley   DM               dmC;
10801555c271SMatthew G. Knepley   PetscSection     section;
10811555c271SMatthew G. Knepley   PetscQuadrature  quad;
10821555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
10831555c271SMatthew G. Knepley   PetscReal       *coords, *detJ, *J, *invJ;
10841555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
10851555c271SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset;
10861555c271SMatthew G. Knepley   PetscErrorCode   ierr;
10871555c271SMatthew G. Knepley 
10881555c271SMatthew G. Knepley   PetscFunctionBegin;
10891555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
10901555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
10911555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10921555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1093e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
10941555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
10951555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
10961555c271SMatthew G. Knepley     PetscObject  obj;
10971555c271SMatthew G. Knepley     PetscClassId id;
10981555c271SMatthew G. Knepley     PetscInt     Nc;
10991555c271SMatthew G. Knepley 
110044a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
11011555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
11021555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
11031555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
11041555c271SMatthew G. Knepley 
11051555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
11061555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
11071555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
11081555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
11091555c271SMatthew G. Knepley 
11101555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
11111555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
11121555c271SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11131555c271SMatthew G. Knepley     numComponents += Nc;
11141555c271SMatthew G. Knepley   }
11151555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
11161555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
11171555c271SMatthew G. Knepley   ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ);CHKERRQ(ierr);
11181555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
11191555c271SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11201555c271SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11211555c271SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
11221555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
11231555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
11241555c271SMatthew G. Knepley     PetscInt   *star = NULL;
11251555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
11261555c271SMatthew G. Knepley 
11271555c271SMatthew G. Knepley     ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
11281555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
11291555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
11301555c271SMatthew G. Knepley       const PetscInt cell = star[st];
11311555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
11321555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
11331555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
11341555c271SMatthew G. Knepley 
11351555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
11361555c271SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
11371555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
11381555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
11391555c271SMatthew G. Knepley         PetscObject  obj;
11401555c271SMatthew G. Knepley         PetscClassId id;
11411555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
11421555c271SMatthew G. Knepley 
11431555c271SMatthew G. Knepley         ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
114444a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
11451555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
11461555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
11471555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
11481555c271SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11491555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
11501555c271SMatthew G. Knepley           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], cell, q);
11511555c271SMatthew G. Knepley           if (ierr) {
11521555c271SMatthew G. Knepley             PetscErrorCode ierr2;
11531555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
11541555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
11551555c271SMatthew G. Knepley             ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2);
11561555c271SMatthew G. Knepley             CHKERRQ(ierr);
11571555c271SMatthew G. Knepley           }
11581555c271SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);}
11591555c271SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11601555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
11611555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
11621555c271SMatthew G. Knepley 
11631555c271SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q];
11641555c271SMatthew G. Knepley           }
11651555c271SMatthew G. Knepley           vol += quadWeights[q*qNc]*detJ[q];
11661555c271SMatthew G. Knepley         }
11671555c271SMatthew G. Knepley         fieldOffset += Nb;
11681555c271SMatthew G. Knepley         qc          += Nc;
11691555c271SMatthew G. Knepley       }
11701555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1171f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1172f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1173f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1174f8527842SMatthew G. Knepley         }
1175f8527842SMatthew G. Knepley       }
1176f8527842SMatthew G. Knepley       volsum += vol;
1177db1066baSMatthew G. Knepley       if (debug) {
11786d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
11791555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
11801555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
11811555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
11826d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
11831555c271SMatthew G. Knepley           }
11841555c271SMatthew G. Knepley         }
11851555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1186db1066baSMatthew G. Knepley       }
11871555c271SMatthew G. Knepley     }
11881555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
11891555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
11901555c271SMatthew G. Knepley     }
11911555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
11921555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
11931555c271SMatthew G. Knepley   }
11941555c271SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr);
11951555c271SMatthew G. Knepley   PetscFunctionReturn(0);
11961555c271SMatthew G. Knepley }
11971555c271SMatthew G. Knepley 
1198338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
119973d901b8SMatthew G. Knepley {
1200338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
120161aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
120273d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1203338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1204c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1205c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1206338f77d5SMatthew G. Knepley   /* DS */
1207338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1208338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1209338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1210338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1211338f77d5SMatthew G. Knepley   /* Geometry */
1212c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1213338f77d5SMatthew G. Knepley   DM                 dmGrad;
1214c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1215338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1216b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1217338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1218b7260050SToby Isaac   PetscInt           maxDegree;
1219c330f8ffSToby Isaac   DMField            coordField;
1220c330f8ffSToby Isaac   IS                 cellIS;
122173d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
122273d901b8SMatthew G. Knepley 
122373d901b8SMatthew G. Knepley   PetscFunctionBegin;
1224338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1225c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1226e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
122773d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1228338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1229b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1230b5a3613cSMatthew G. Knepley     PetscObject  obj;
1231b5a3613cSMatthew G. Knepley     PetscClassId id;
1232b5a3613cSMatthew G. Knepley 
1233b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1234b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1235338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1236338f77d5SMatthew G. Knepley   }
1237338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1238338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1239338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1240338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1241338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1242338f77d5SMatthew G. Knepley   /* Read DS information */
1243338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1244338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1245338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1246c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1247338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1248338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1249338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1250338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1251338f77d5SMatthew G. Knepley   if (dmAux) {
1252338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1253338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
1254e87a4003SBarry Smith     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
1255338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1256338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1257338f77d5SMatthew G. Knepley   }
1258338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1259338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1260338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1261338f77d5SMatthew G. Knepley   /* Read out geometry */
1262c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1263b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1264b7260050SToby Isaac   if (maxDegree <= 1) {
1265c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1266c330f8ffSToby Isaac     if (affineQuad) {
1267c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1268338f77d5SMatthew G. Knepley     }
1269b5a3613cSMatthew G. Knepley   }
1270b5a3613cSMatthew G. Knepley   if (useFVM) {
1271338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1272b5a3613cSMatthew G. Knepley     Vec       grad;
1273b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1274b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1275b5a3613cSMatthew G. Knepley 
1276338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1277338f77d5SMatthew G. Knepley       PetscObject  obj;
1278338f77d5SMatthew G. Knepley       PetscClassId id;
1279338f77d5SMatthew G. Knepley 
1280338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1281338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1282338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1283338f77d5SMatthew G. Knepley     }
1284338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1285338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1286b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1287338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1288338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1289b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1290b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1291b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1292b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1293338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1294b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1295b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1296b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1297b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1298b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1299b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1300338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1301b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1302b5a3613cSMatthew G. Knepley   }
1303338f77d5SMatthew G. Knepley   /* Read out data from inputs */
130473d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
130573d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
130673d901b8SMatthew G. Knepley     PetscInt     i;
130773d901b8SMatthew G. Knepley 
1308338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
13090f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1310338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
131173d901b8SMatthew G. Knepley     if (dmAux) {
1312338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
13130f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1314338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
131573d901b8SMatthew G. Knepley     }
131673d901b8SMatthew G. Knepley   }
1317338f77d5SMatthew G. Knepley   /* Do integration for each field */
131873d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1319c1f031eeSMatthew G. Knepley     PetscObject  obj;
1320c1f031eeSMatthew G. Knepley     PetscClassId id;
1321c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
132273d901b8SMatthew G. Knepley 
1323c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1324c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1325c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1326c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1327c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1328c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1329c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1330c1f031eeSMatthew G. Knepley 
13310f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1332c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
13339c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1334c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1335c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
133673d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
13370f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
133873d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
133973d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
134073d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
134173d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1342c330f8ffSToby Isaac       if (!affineQuad) {
1343c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1344c330f8ffSToby Isaac       }
1345c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
1346c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
1347c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1348c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1349c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1350c330f8ffSToby Isaac       if (!affineQuad) {
1351c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1352c330f8ffSToby Isaac       }
1353c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1354c1f031eeSMatthew G. Knepley       PetscInt       foff;
1355420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1356b69edc29SMatthew G. Knepley       PetscScalar    lint;
1357c1f031eeSMatthew G. Knepley 
1358c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1359c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1360c1f031eeSMatthew G. Knepley       if (obj_func) {
1361c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1362b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1363b5a3613cSMatthew G. Knepley 
1364b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1365338f77d5SMatthew G. Knepley           obj_func(dim, Nf, NfAux, uOff, uOff_x, &u[totDim*c+foff], NULL, u_x, aOff, NULL, &a[totDimAux*c], NULL, NULL, 0.0, cgeomFVM[c].centroid, numConstants, constants, &lint);
1366338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
136773d901b8SMatthew G. Knepley         }
1368c1f031eeSMatthew G. Knepley       }
1369c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1370c1f031eeSMatthew G. Knepley   }
1371338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1372b5a3613cSMatthew G. Knepley   if (useFVM) {
1373b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1374b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1375b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1376b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1377b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1378b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1379b5a3613cSMatthew G. Knepley   }
138073d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1381338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1382338f77d5SMatthew G. Knepley   /* Cleanup */
1383f99c8401SToby Isaac   if (affineQuad) {
1384f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1385f99c8401SToby Isaac   }
1386f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1387c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1388338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1389338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1390338f77d5SMatthew G. Knepley }
1391338f77d5SMatthew G. Knepley 
1392338f77d5SMatthew G. Knepley /*@
1393338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1394338f77d5SMatthew G. Knepley 
1395338f77d5SMatthew G. Knepley   Input Parameters:
1396338f77d5SMatthew G. Knepley + dm - The mesh
1397338f77d5SMatthew G. Knepley . X  - Global input vector
1398338f77d5SMatthew G. Knepley - user - The user context
1399338f77d5SMatthew G. Knepley 
1400338f77d5SMatthew G. Knepley   Output Parameter:
1401338f77d5SMatthew G. Knepley . integral - Integral for each field
1402338f77d5SMatthew G. Knepley 
1403338f77d5SMatthew G. Knepley   Level: developer
1404338f77d5SMatthew G. Knepley 
1405338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1406338f77d5SMatthew G. Knepley @*/
1407b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1408338f77d5SMatthew G. Knepley {
1409338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1410b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1411338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1412338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1413338f77d5SMatthew G. Knepley 
1414338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1415338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1416338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1417338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1418338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1419338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1420338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1421338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1422338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1423338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1424338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1425338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1426338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1427338f77d5SMatthew G. Knepley   /* Sum up values */
1428338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1429338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1430338f77d5SMatthew G. Knepley 
1431338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1432b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
1433338f77d5SMatthew G. Knepley   }
1434b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
143573d901b8SMatthew G. Knepley   if (mesh->printFEM) {
1436338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
1437b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
143873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
143973d901b8SMatthew G. Knepley   }
1440338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
1441338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1442338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1443338f77d5SMatthew G. Knepley }
1444338f77d5SMatthew G. Knepley 
1445338f77d5SMatthew G. Knepley /*@
1446338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
1447338f77d5SMatthew G. Knepley 
1448338f77d5SMatthew G. Knepley   Input Parameters:
1449338f77d5SMatthew G. Knepley + dm - The mesh
1450338f77d5SMatthew G. Knepley . X  - Global input vector
1451338f77d5SMatthew G. Knepley - user - The user context
1452338f77d5SMatthew G. Knepley 
1453338f77d5SMatthew G. Knepley   Output Parameter:
1454338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
1455338f77d5SMatthew G. Knepley 
1456338f77d5SMatthew G. Knepley   Level: developer
1457338f77d5SMatthew G. Knepley 
1458338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1459338f77d5SMatthew G. Knepley @*/
1460338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
1461338f77d5SMatthew G. Knepley {
1462338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1463338f77d5SMatthew G. Knepley   DM             dmF;
1464338f77d5SMatthew G. Knepley   PetscSection   sectionF;
1465338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
1466338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1467338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1468338f77d5SMatthew G. Knepley 
1469338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1470338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1471338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1472338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
1473338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1474338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1475338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1476338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1477338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1478338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1479338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1480338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1481338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1482338f77d5SMatthew G. Knepley   /* Put values in F*/
1483338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
1484e87a4003SBarry Smith   ierr = DMGetSection(dmF, &sectionF);CHKERRQ(ierr);
1485338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
1486338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1487338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1488338f77d5SMatthew G. Knepley     PetscInt       dof, off;
1489338f77d5SMatthew G. Knepley 
1490338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1491338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
1492338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
1493338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
1494338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
1495338f77d5SMatthew G. Knepley   }
1496338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
1497338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
1498c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
149973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
150073d901b8SMatthew G. Knepley }
150173d901b8SMatthew G. Knepley 
15029b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
15039b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
150464c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
150564c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
150664c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
150764c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
150864c72086SMatthew G. Knepley {
15099b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
15109b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
15119b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
15129b6f715bSMatthew G. Knepley   Vec                locA = NULL;
15139b6f715bSMatthew G. Knepley   DMField            coordField;
15149b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
15159b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
15169b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
151764c72086SMatthew G. Knepley   const PetscScalar *constants;
15189b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
151964c72086SMatthew G. Knepley   PetscErrorCode     ierr;
152064c72086SMatthew G. Knepley 
152164c72086SMatthew G. Knepley   PetscFunctionBegin;
15229b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
15239b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
152464c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
152564c72086SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
152664c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
152764c72086SMatthew G. Knepley   /* Determine which discretizations we have */
15289b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
152964c72086SMatthew G. Knepley     PetscObject  obj;
153064c72086SMatthew G. Knepley     PetscClassId id;
153164c72086SMatthew G. Knepley 
15329b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
153364c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15349b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
153564c72086SMatthew G. Knepley   }
153664c72086SMatthew G. Knepley   /* Read DS information */
153764c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
153864c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
153964c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
154064c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
154164c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
154264c72086SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
15439b6f715bSMatthew G. Knepley   if (locA) {
15449b6f715bSMatthew G. Knepley     DM dmAux;
15459b6f715bSMatthew G. Knepley 
15469b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
15479b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
154864c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
154964c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
155064c72086SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
155164c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
155264c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
155364c72086SMatthew G. Knepley   }
15549b6f715bSMatthew G. Knepley   /* Integrate over points */
15559b6f715bSMatthew G. Knepley   {
15569b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
1557b7260050SToby Isaac     PetscInt        maxDegree;
15589b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
15599b6f715bSMatthew G. Knepley     const PetscInt *points;
15609b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
15619b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
156264c72086SMatthew G. Knepley 
15639b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
15649b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
15659b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
1566b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
156764c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
156864c72086SMatthew G. Knepley       PetscFE fe;
156964c72086SMatthew G. Knepley 
157064c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1571b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
15729b6f715bSMatthew G. Knepley       if (!qGeom) {
15739b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
15749b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
15759b6f715bSMatthew G. Knepley       }
15769b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
15779b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
15789b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
15799b6f715bSMatthew G. Knepley         const PetscInt point = points[face], *support, *cone;
15809b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
15819b6f715bSMatthew G. Knepley         PetscInt       i, coneSize, faceLoc;
15829b6f715bSMatthew G. Knepley 
15839b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
15849b6f715bSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
15859b6f715bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
15869b6f715bSMatthew G. Knepley         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
15879b6f715bSMatthew G. Knepley         if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", face, support[0]);
15889b6f715bSMatthew G. Knepley         fgeom->face[face][0] = faceLoc;
15899b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
15909b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
15919b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
15929b6f715bSMatthew G. Knepley         if (locA) {
15939b6f715bSMatthew G. Knepley           PetscInt subp;
15949b6f715bSMatthew G. Knepley           ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);
15959b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
15969b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
15979b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
15989b6f715bSMatthew G. Knepley         }
15999b6f715bSMatthew G. Knepley       }
16009b6f715bSMatthew G. Knepley       /* Get blocking */
16019b6f715bSMatthew G. Knepley       {
16029b6f715bSMatthew G. Knepley         PetscQuadrature q;
16039b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
16049b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
16059b6f715bSMatthew G. Knepley 
160664c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
160764c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
160864c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
160964c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
161064c72086SMatthew G. Knepley         blockSize = Nb*Nq;
161164c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
16129b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
161364c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
16149b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
16159b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
161664c72086SMatthew G. Knepley         offset    = numFaces - Nr;
161764c72086SMatthew G. Knepley       }
16189b6f715bSMatthew G. Knepley       /* Do integration for each field */
16199b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
16209b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
16219b6f715bSMatthew G. Knepley         ierr = PetscFEIntegrateBd(fe, prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
16229b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
162364c72086SMatthew G. Knepley       }
16249b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
16259b6f715bSMatthew G. Knepley       ierr = PetscFEIntegrateBd(fe, prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
16269b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
162764c72086SMatthew G. Knepley       /* Cleanup data arrays */
16289b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
16299b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
16309b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
16319b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
163264c72086SMatthew G. Knepley     }
163364c72086SMatthew G. Knepley   }
16349b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
16359b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
16369b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
16379b6f715bSMatthew G. Knepley }
16389b6f715bSMatthew G. Knepley 
16399b6f715bSMatthew G. Knepley /*@
16409b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
16419b6f715bSMatthew G. Knepley 
16429b6f715bSMatthew G. Knepley   Input Parameters:
16439b6f715bSMatthew G. Knepley + dm      - The mesh
16449b6f715bSMatthew G. Knepley . X       - Global input vector
16459b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
16469b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
16479b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
16489b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
16499b6f715bSMatthew G. Knepley - user    - The user context
16509b6f715bSMatthew G. Knepley 
16519b6f715bSMatthew G. Knepley   Output Parameter:
16529b6f715bSMatthew G. Knepley . integral - Integral for each field
16539b6f715bSMatthew G. Knepley 
16549b6f715bSMatthew G. Knepley   Level: developer
16559b6f715bSMatthew G. Knepley 
16569b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
16579b6f715bSMatthew G. Knepley @*/
16589b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
16599b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
16609b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
16619b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
16629b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
16639b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
16649b6f715bSMatthew G. Knepley {
16659b6f715bSMatthew G. Knepley   Vec            locX;
16669b6f715bSMatthew G. Knepley   PetscSection   section;
16679b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
16689b6f715bSMatthew G. Knepley   IS             facetIS;
16699b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
16709b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
16719b6f715bSMatthew G. Knepley 
16729b6f715bSMatthew G. Knepley   PetscFunctionBegin;
16739b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16749b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
16759b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
16769b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
16779b6f715bSMatthew G. Knepley   PetscValidPointer(integral, 6);
16789b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
16799b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
16809b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16819b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
16829b6f715bSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
16839b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
16849b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
16859b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
16869b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
16879b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
16889b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
16899b6f715bSMatthew G. Knepley   /* Loop over label values */
16909b6f715bSMatthew G. Knepley   ierr = PetscMemzero(integral, Nf * sizeof(PetscScalar));CHKERRQ(ierr);
16919b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
16929b6f715bSMatthew G. Knepley     IS           pointIS;
16939b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
16949b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
16959b6f715bSMatthew G. Knepley 
16969b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
16979b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
16989b6f715bSMatthew G. Knepley     {
16999b6f715bSMatthew G. Knepley       IS isectIS;
17009b6f715bSMatthew G. Knepley 
17019b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
17029b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
17039b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
17049b6f715bSMatthew G. Knepley       pointIS = isectIS;
17059b6f715bSMatthew G. Knepley     }
17069b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
17079b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
17089b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
17099b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
17109b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
17119b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
17129b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
17139b6f715bSMatthew G. Knepley   }
171464c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
17159b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
17169b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
171764c72086SMatthew G. Knepley   PetscFunctionReturn(0);
171864c72086SMatthew G. Knepley }
171964c72086SMatthew G. Knepley 
1720d69c5d34SMatthew G. Knepley /*@
172168132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1722d69c5d34SMatthew G. Knepley 
1723d69c5d34SMatthew G. Knepley   Input Parameters:
1724d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1725d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1726d69c5d34SMatthew G. Knepley - user - The user context
1727d69c5d34SMatthew G. Knepley 
1728d69c5d34SMatthew G. Knepley   Output Parameter:
1729934789fcSMatthew G. Knepley . In  - The interpolation matrix
1730d69c5d34SMatthew G. Knepley 
1731d69c5d34SMatthew G. Knepley   Level: developer
1732d69c5d34SMatthew G. Knepley 
173368132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1734d69c5d34SMatthew G. Knepley @*/
173568132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
1736d69c5d34SMatthew G. Knepley {
1737d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1738d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
17392764a2aaSMatthew G. Knepley   PetscDS           prob;
1740d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
174197c42addSMatthew G. Knepley   PetscFV          *fvRef;
1742d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1743d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1744d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
17459ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
17460f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1747d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1748d69c5d34SMatthew G. Knepley 
1749d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1750d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1751c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1752e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
1753e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1754e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
1755e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1756d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1757d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
17589ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
17599ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
17602764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
176197c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1762d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
176397c42addSMatthew G. Knepley     PetscObject  obj;
176497c42addSMatthew G. Knepley     PetscClassId id;
1765aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1766d69c5d34SMatthew G. Knepley 
176797c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
176897c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
176997c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
177097c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
177197c42addSMatthew G. Knepley 
17720f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1773d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
17740f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
177597c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
177697c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
177797c42addSMatthew G. Knepley       PetscDualSpace Q;
177897c42addSMatthew G. Knepley 
177997c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
178097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
178197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
178297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
178397c42addSMatthew G. Knepley     }
17849c3cf19fSMatthew G. Knepley     rTotDim += rNb;
1785d69c5d34SMatthew G. Knepley   }
17862764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
17870f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
17880f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1789d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1790d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1791d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1792d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1793d69c5d34SMatthew G. Knepley     PetscReal       *points;
1794d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1795d69c5d34SMatthew G. Knepley 
1796d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
179797c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1798d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
17990f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
180097c42addSMatthew G. Knepley     } else {
180197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
180297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
180397c42addSMatthew G. Knepley     }
1804d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1805d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1806d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18079c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1808d69c5d34SMatthew G. Knepley       npoints += Np;
1809d69c5d34SMatthew G. Knepley     }
1810d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1811d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1812d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18139c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1814d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1815d69c5d34SMatthew G. Knepley     }
1816d69c5d34SMatthew G. Knepley 
1817d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
181897c42addSMatthew G. Knepley       PetscObject  obj;
181997c42addSMatthew G. Knepley       PetscClassId id;
1820d69c5d34SMatthew G. Knepley       PetscReal   *B;
18219c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
1822d69c5d34SMatthew G. Knepley 
182397c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
182497c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
182597c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
182697c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1827d69c5d34SMatthew G. Knepley 
1828d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
18290f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
18300f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1831ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1832ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
18339c3cf19fSMatthew G. Knepley           if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", Nc, NcJ);
18340f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1835d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1836d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18379c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
18389c3cf19fSMatthew G. Knepley             if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
1839d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
184036a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
1841d172c84bSMatthew G. Knepley                 /*
1842d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
1843d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
1844d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
1845d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
1846d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
1847d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
1848d172c84bSMatthew G. Knepley                 */
18499c3cf19fSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c];
185036a6d9c0SMatthew G. Knepley               }
1851d69c5d34SMatthew G. Knepley             }
1852d69c5d34SMatthew G. Knepley           }
18530f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1854ffe73a53SMatthew G. Knepley         }
185597c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
185697c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
185797c42addSMatthew G. Knepley 
185897c42addSMatthew G. Knepley         /* Evaluate constant function at points */
185997c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
186097c42addSMatthew G. Knepley         cpdim = 1;
186197c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
186297c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
186397c42addSMatthew G. Knepley           if (Nc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", Nc, NcJ);
186497c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
186597c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18669c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
18679c3cf19fSMatthew G. Knepley             if (qNc != NcJ) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
186897c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
186997c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
1870458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
187197c42addSMatthew G. Knepley               }
187297c42addSMatthew G. Knepley             }
187397c42addSMatthew G. Knepley           }
187497c42addSMatthew G. Knepley         }
187597c42addSMatthew G. Knepley       }
1876d172c84bSMatthew G. Knepley       offsetJ += cpdim;
1877d69c5d34SMatthew G. Knepley     }
1878d172c84bSMatthew G. Knepley     offsetI += fpdim;
1879549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1880d69c5d34SMatthew G. Knepley   }
18810f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
18827f5b169aSMatthew G. Knepley   /* Preallocate matrix */
18837f5b169aSMatthew G. Knepley   {
1884c094ef40SMatthew G. Knepley     Mat          preallocator;
1885c094ef40SMatthew G. Knepley     PetscScalar *vals;
1886c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
1887c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
18887f5b169aSMatthew G. Knepley 
1889c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
1890c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
1891c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1892c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1893c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1894c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
18957f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
18967f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
1897c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
18987f5b169aSMatthew G. Knepley     }
1899c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
1900c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1901c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1902c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
1903c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
19047f5b169aSMatthew G. Knepley   }
19057f5b169aSMatthew G. Knepley   /* Fill matrix */
19067f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1907d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1908934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1909d69c5d34SMatthew G. Knepley   }
1910549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
191197c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1912549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1913934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1914934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1915d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1916d69c5d34SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_WORLD, "%s:\n", name);CHKERRQ(ierr);
1917934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1918934789fcSMatthew G. Knepley     ierr = MatView(In, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1919d69c5d34SMatthew G. Knepley   }
1920d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1921d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1922d69c5d34SMatthew G. Knepley }
19236c73c22cSMatthew G. Knepley 
1924bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
1925bd041c0cSMatthew G. Knepley {
1926bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
1927bd041c0cSMatthew G. Knepley }
1928bd041c0cSMatthew G. Knepley 
192968132eb9SMatthew G. Knepley /*@
193068132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
193168132eb9SMatthew G. Knepley 
193268132eb9SMatthew G. Knepley   Input Parameters:
193368132eb9SMatthew G. Knepley + dmf  - The fine mesh
193468132eb9SMatthew G. Knepley . dmc  - The coarse mesh
193568132eb9SMatthew G. Knepley - user - The user context
193668132eb9SMatthew G. Knepley 
193768132eb9SMatthew G. Knepley   Output Parameter:
193868132eb9SMatthew G. Knepley . In  - The interpolation matrix
193968132eb9SMatthew G. Knepley 
194068132eb9SMatthew G. Knepley   Level: developer
194168132eb9SMatthew G. Knepley 
194268132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
194368132eb9SMatthew G. Knepley @*/
194468132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
19454ef9d792SMatthew G. Knepley {
194664e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
194764e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
19484ef9d792SMatthew G. Knepley   PetscDS        prob;
19494ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
1950e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
19514ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
19524ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
19534ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
19544ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
19554ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
19564ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
19574ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
19584ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
19594ef9d792SMatthew G. Knepley 
19604ef9d792SMatthew G. Knepley   PetscFunctionBegin;
196177711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
19624ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
19634ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
19644ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
19654ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
19664ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
19674ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
1968e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
1969e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
1970e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
1971e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
19724ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
19734ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
19749c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
19754ef9d792SMatthew G. Knepley 
19764ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
19774ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
19784ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
19794ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
19804ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
19814ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
19824ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
19834ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
1984e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
19854ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
19864ef9d792SMatthew G. Knepley     PetscObject      obj;
19874ef9d792SMatthew G. Knepley     PetscClassId     id;
1988c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
19894ef9d792SMatthew G. Knepley     PetscQuadrature  f;
199017f047d8SMatthew G. Knepley     const PetscReal *qpoints;
199117f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
19924ef9d792SMatthew G. Knepley 
19934ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
19944ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
19954ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
19964ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
19974ef9d792SMatthew G. Knepley 
19984ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
19994ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
20004ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
20014ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
20024ef9d792SMatthew G. Knepley 
20034ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
20044ef9d792SMatthew G. Knepley       Nc   = 1;
20054ef9d792SMatthew G. Knepley     }
20064ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
20074ef9d792SMatthew G. Knepley     /* For each fine grid cell */
20084ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
20094ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
20104ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
20114ef9d792SMatthew G. Knepley 
20126ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
20134ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
20149c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
20154ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
20164ef9d792SMatthew G. Knepley         Vec             pointVec;
20174ef9d792SMatthew G. Knepley         PetscScalar    *pV;
20183a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
20193a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
20209c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
20214ef9d792SMatthew G. Knepley 
20224ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
20234ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
20249c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
20254ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
20264ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
20274ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
20284ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2029c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2030c330f8ffSToby Isaac 
20314ef9d792SMatthew G. Knepley           /* Transform point to real space */
2032c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
20334ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
20344ef9d792SMatthew G. Knepley         }
20354ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
20364ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
20371555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
203862a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
20393a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
20404ef9d792SMatthew G. Knepley         /* Update preallocation info */
20413a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
20423a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
20439c3cf19fSMatthew G. Knepley         {
2044e8f14785SLisandro Dalcin           PetscHashIJKey key;
2045e8f14785SLisandro Dalcin           PetscBool      missing;
20464ef9d792SMatthew G. Knepley 
2047e8f14785SLisandro Dalcin           key.i = findices[i];
2048e8f14785SLisandro Dalcin           if (key.i >= 0) {
20494ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
20504ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
20513a93e3b7SToby Isaac               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
20524ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2053e8f14785SLisandro Dalcin                 key.j = cindices[c];
2054e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2055e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
20564ef9d792SMatthew G. Knepley                 if (missing) {
2057e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2058e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
20594ef9d792SMatthew G. Knepley                 }
20604ef9d792SMatthew G. Knepley               }
20613a93e3b7SToby Isaac               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
20624ef9d792SMatthew G. Knepley             }
20634ef9d792SMatthew G. Knepley           }
20648c543595SMatthew G. Knepley         }
20653a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
20664ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
20674ef9d792SMatthew G. Knepley       }
206846bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
20694ef9d792SMatthew G. Knepley     }
20704ef9d792SMatthew G. Knepley   }
2071e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
20724ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
20734ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
20744ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
20754ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
20764ef9d792SMatthew G. Knepley     PetscObject      obj;
20774ef9d792SMatthew G. Knepley     PetscClassId     id;
2078c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
20794ef9d792SMatthew G. Knepley     PetscQuadrature  f;
20804ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
20819c3cf19fSMatthew G. Knepley     PetscInt         Nc, qNc, Np, fpdim, i, d;
20824ef9d792SMatthew G. Knepley 
20834ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
20844ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
20854ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
20864ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
20874ef9d792SMatthew G. Knepley 
20884ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
20894ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
20904ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
20914ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
20924ef9d792SMatthew G. Knepley 
20934ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
20944ef9d792SMatthew G. Knepley       Nc   = 1;
209525ce1634SJed Brown     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %d",field);
20964ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
20974ef9d792SMatthew G. Knepley     /* For each fine grid cell */
20984ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
20994ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
21004ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
21014ef9d792SMatthew G. Knepley 
21026ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
21034ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
21049c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
21054ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
21064ef9d792SMatthew G. Knepley         Vec             pointVec;
21074ef9d792SMatthew G. Knepley         PetscScalar    *pV;
210812111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
21093a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
211017f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
21114ef9d792SMatthew G. Knepley 
21124ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
21134ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
21149c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
21159c3cf19fSMatthew G. Knepley         if (qNc != Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, Nc);
21164ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
21174ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
21184ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
21194ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2120c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2121c330f8ffSToby Isaac 
21224ef9d792SMatthew G. Knepley           /* Transform point to real space */
2123c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
21244ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
21254ef9d792SMatthew G. Knepley         }
21264ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
21274ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
21281555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
212962a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
21304ef9d792SMatthew G. Knepley         /* Update preallocation info */
21313a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
21323a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
21334ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
21344ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2135826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2136c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2137826eb36dSMatthew G. Knepley 
21383a93e3b7SToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
21394ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
21403a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2141e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2142c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
21434ef9d792SMatthew G. Knepley 
21444ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
21454ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
21464ef9d792SMatthew G. Knepley             PetscReal *B;
21474ef9d792SMatthew G. Knepley 
21484ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
21494ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
21504ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
21519c3cf19fSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
21524ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
21534ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
21549c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c];
21554ef9d792SMatthew G. Knepley             }
21564ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
21574ef9d792SMatthew G. Knepley           } else {
21584ef9d792SMatthew G. Knepley             cpdim = 1;
21594ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
21609c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
21614ef9d792SMatthew G. Knepley             }
21624ef9d792SMatthew G. Knepley           }
21634ef9d792SMatthew G. Knepley           /* Update interpolator */
21649c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
21659c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
21669c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
21673a93e3b7SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
21684ef9d792SMatthew G. Knepley         }
21694ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
21703a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
21714ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
21724ef9d792SMatthew G. Knepley       }
217346bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
21744ef9d792SMatthew G. Knepley     }
21754ef9d792SMatthew G. Knepley   }
21764ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
21774ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
21784ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
21794ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
21804ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
218177711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
21824ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
21834ef9d792SMatthew G. Knepley }
21844ef9d792SMatthew G. Knepley 
218546fa42a0SMatthew G. Knepley /*@
2186bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2187bd041c0cSMatthew G. Knepley 
2188bd041c0cSMatthew G. Knepley   Input Parameters:
2189bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2190bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2191bd041c0cSMatthew G. Knepley - user - The user context
2192bd041c0cSMatthew G. Knepley 
2193bd041c0cSMatthew G. Knepley   Output Parameter:
2194bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2195bd041c0cSMatthew G. Knepley 
2196bd041c0cSMatthew G. Knepley   Level: developer
2197bd041c0cSMatthew G. Knepley 
2198bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2199bd041c0cSMatthew G. Knepley @*/
2200bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2201bd041c0cSMatthew G. Knepley {
2202bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2203bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2204bd041c0cSMatthew G. Knepley   PetscDS        prob;
2205bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2206e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2207bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2208bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2209bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2210bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2211bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2212bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2213bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2214bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2215bd041c0cSMatthew G. Knepley 
2216bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2217bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2218bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
2219bd041c0cSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
2220bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2221bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2222bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
2223e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2224e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
2225e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2226e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2227bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2228bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2229bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2230bd041c0cSMatthew G. Knepley 
2231bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2232bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2233bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2234bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2235bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2236bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2237bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2238bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2239e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2240bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2241bd041c0cSMatthew G. Knepley     PetscObject      obj;
2242bd041c0cSMatthew G. Knepley     PetscClassId     id;
2243bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2244bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2245bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2246bd041c0cSMatthew G. Knepley 
2247bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2248bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2249bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2250bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2251bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2252bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2253bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2254bd041c0cSMatthew G. Knepley       Vec                pointVec;
2255bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2256bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2257bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2258bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2259bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2260bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2261bd041c0cSMatthew G. Knepley 
2262bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2263bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2264bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2265bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2266bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2267bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2268bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2269c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2270c330f8ffSToby Isaac 
2271bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2272c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2273bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2274bd041c0cSMatthew G. Knepley       }
2275bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2276bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2277bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2278bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2279bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2280bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2281bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2282bd041c0cSMatthew G. Knepley       {
2283e8f14785SLisandro Dalcin         PetscHashIJKey key;
2284e8f14785SLisandro Dalcin         PetscBool      missing;
2285bd041c0cSMatthew G. Knepley 
2286bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2287e8f14785SLisandro Dalcin           key.i = findices[i];
2288e8f14785SLisandro Dalcin           if (key.i >= 0) {
2289bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2290bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2291bd041c0cSMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2292bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2293e8f14785SLisandro Dalcin                 key.j = cindices[c];
2294e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2295e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2296bd041c0cSMatthew G. Knepley                 if (missing) {
2297e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2298e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2299bd041c0cSMatthew G. Knepley                 }
2300bd041c0cSMatthew G. Knepley               }
2301bd041c0cSMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2302bd041c0cSMatthew G. Knepley             }
2303bd041c0cSMatthew G. Knepley           }
2304bd041c0cSMatthew G. Knepley         }
2305bd041c0cSMatthew G. Knepley       }
2306bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2307bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2308bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2309bd041c0cSMatthew G. Knepley     }
2310bd041c0cSMatthew G. Knepley   }
2311e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
2312bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2313bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2314bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2315bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2316bd041c0cSMatthew G. Knepley     PetscObject      obj;
2317bd041c0cSMatthew G. Knepley     PetscClassId     id;
2318bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2319bd041c0cSMatthew G. Knepley     PetscReal       *Bfine;
2320bd041c0cSMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2321bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2322bd041c0cSMatthew G. Knepley 
2323bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2324bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2325bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);}
2326bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2327bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2328bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2329bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2330bd041c0cSMatthew G. Knepley       Vec                pointVec;
2331bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2332bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2333bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2334bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2335bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2336bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2337bd041c0cSMatthew G. Knepley 
2338bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2339bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2340bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2341bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2342bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2343bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2344bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2345c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2346c330f8ffSToby Isaac 
2347bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2348c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2349bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2350bd041c0cSMatthew G. Knepley       }
2351bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2352bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2353bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2354bd041c0cSMatthew G. Knepley       /* Update matrix */
2355bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2356bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2357bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2358bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2359bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2360c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2361c330f8ffSToby Isaac 
2362bd041c0cSMatthew G. Knepley 
2363bd041c0cSMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2364bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2365bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2366bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2367c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
2368bd041c0cSMatthew G. Knepley 
2369bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2370bd041c0cSMatthew G. Knepley           PetscFE    fe = (PetscFE) obj;
2371bd041c0cSMatthew G. Knepley           PetscReal *B;
2372bd041c0cSMatthew G. Knepley 
2373bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2374bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2375bd041c0cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2376bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2377bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2378bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2379bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2380bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*Bfine[(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ;
2381bd041c0cSMatthew G. Knepley             }
2382bd041c0cSMatthew G. Knepley             /* Update interpolator */
2383bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2384bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2385bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2386bd041c0cSMatthew G. Knepley           }
2387bd041c0cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2388bd041c0cSMatthew G. Knepley         } else {
2389bd041c0cSMatthew G. Knepley           cpdim = 1;
2390bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2391bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2392bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2393bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2394bd041c0cSMatthew G. Knepley             }
2395bd041c0cSMatthew G. Knepley             /* Update interpolator */
2396bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2397bd041c0cSMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %d %d Nf: %d %d Nc: %d %d\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
2398bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2399bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2400bd041c0cSMatthew G. Knepley           }
2401bd041c0cSMatthew G. Knepley         }
2402bd041c0cSMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2403bd041c0cSMatthew G. Knepley       }
2404bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2405bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2406bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2407bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2408bd041c0cSMatthew G. Knepley     }
2409bd041c0cSMatthew G. Knepley   }
2410bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
2411bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
2412bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2413bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2414bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2415bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
2416bd041c0cSMatthew G. Knepley }
2417bd041c0cSMatthew G. Knepley 
2418bd041c0cSMatthew G. Knepley /*@
241946fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
242046fa42a0SMatthew G. Knepley 
242146fa42a0SMatthew G. Knepley   Input Parameters:
242246fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
242346fa42a0SMatthew G. Knepley - dmf  - The fine mesh
242446fa42a0SMatthew G. Knepley - user - The user context
242546fa42a0SMatthew G. Knepley 
242646fa42a0SMatthew G. Knepley   Output Parameter:
242746fa42a0SMatthew G. Knepley . sc   - The mapping
242846fa42a0SMatthew G. Knepley 
242946fa42a0SMatthew G. Knepley   Level: developer
243046fa42a0SMatthew G. Knepley 
243146fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
243246fa42a0SMatthew G. Knepley @*/
24337c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
24347c927364SMatthew G. Knepley {
2435e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
24367c927364SMatthew G. Knepley   PetscFE       *feRef;
243797c42addSMatthew G. Knepley   PetscFV       *fvRef;
24387c927364SMatthew G. Knepley   Vec            fv, cv;
24397c927364SMatthew G. Knepley   IS             fis, cis;
24407c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
24417c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
24420bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
24436f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
24447c927364SMatthew G. Knepley   PetscErrorCode ierr;
24457c927364SMatthew G. Knepley 
24467c927364SMatthew G. Knepley   PetscFunctionBegin;
244775a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2448c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
2449e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2450e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
2451e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2452e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
24537c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
24547c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
24559ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
24569ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2457e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
24586f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
24597c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
246097c42addSMatthew G. Knepley     PetscObject  obj;
246197c42addSMatthew G. Knepley     PetscClassId id;
2462aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
24637c927364SMatthew G. Knepley 
246497c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
246597c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
246697c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
246797c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
24686f3d3cbcSMatthew G. Knepley       PetscSpace sp;
24699b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
247097c42addSMatthew G. Knepley 
24717c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
24727c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
24737c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
24746f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
24759b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
24769b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
247797c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
247897c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
247997c42addSMatthew G. Knepley       PetscDualSpace Q;
248097c42addSMatthew G. Knepley 
248197c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
248297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
248397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
248497c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
24856f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
248697c42addSMatthew G. Knepley     }
2487d172c84bSMatthew G. Knepley     fTotDim += fNb;
24887c927364SMatthew G. Knepley   }
2489e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
24907c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
24917c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
24927c927364SMatthew G. Knepley     PetscFE        feC;
249397c42addSMatthew G. Knepley     PetscFV        fvC;
24947c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
2495d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
24967c927364SMatthew G. Knepley 
249797c42addSMatthew G. Knepley     if (feRef[field]) {
2498e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
24997c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
25007c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
25017c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
2502d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
25037c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
25047c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
25057c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
250697c42addSMatthew G. Knepley     } else {
250797c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
250897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
250997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
251097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
251197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
251297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
251397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
251497c42addSMatthew G. Knepley     }
251597c42addSMatthew G. Knepley     if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC);
25167c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
25177c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
2518d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
2519d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
252097c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
25217c927364SMatthew G. Knepley 
25227c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
2523d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
2524d172c84bSMatthew G. Knepley       if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcC, NcC);
252597c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
25267c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
25277c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
2528d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
25297c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
2530d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
25317c927364SMatthew G. Knepley 
25327c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
2533d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
2534d172c84bSMatthew G. Knepley         if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcF, NcF);
25357c927364SMatthew G. Knepley         if (NpC != NpF) continue;
25367c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
25377c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
2538d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
2539d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
2540d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
254197c42addSMatthew G. Knepley         found = PETSC_TRUE;
25427c927364SMatthew G. Knepley         break;
25437c927364SMatthew G. Knepley       }
254497c42addSMatthew G. Knepley       if (!found) {
254597c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
2546d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
2547d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
254897c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
254997c42addSMatthew G. Knepley       }
25507c927364SMatthew G. Knepley     }
2551d172c84bSMatthew G. Knepley     offsetC += cpdim;
2552d172c84bSMatthew G. Knepley     offsetF += fpdim;
25537c927364SMatthew G. Knepley   }
255497c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
25556f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
25567c927364SMatthew G. Knepley 
25577c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
25587c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
25590bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
25607c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
25617c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
2562aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
2563aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
25647c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
25657c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
25667c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
25677c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
25680bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
25697c927364SMatthew G. Knepley       if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]);
25707c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
25717c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
25727c927364SMatthew G. Knepley     }
25737c927364SMatthew G. Knepley   }
25747c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
25757c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
25767c927364SMatthew G. Knepley 
25777c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
25787c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
257935928de7SBarry Smith   ierr = VecScatterCreateWithData(cv, cis, fv, fis, sc);CHKERRQ(ierr);
25807c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
25817c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
25827c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
25837c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
258475a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2585cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2586cb1e1211SMatthew G Knepley }
2587a1cf66bbSMatthew G. Knepley 
2588a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
2589a1cf66bbSMatthew G. Knepley {
2590a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
2591a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
2592a1cf66bbSMatthew G. Knepley   PetscContainer  container;
2593a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
2594a1cf66bbSMatthew G. Knepley 
2595a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
2596a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
2597a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
2598a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
2599a1cf66bbSMatthew G. Knepley   if (container) {
2600a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
2601a1cf66bbSMatthew G. Knepley   } else {
2602a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
2603a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
2604a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
2605a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
2606a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
2607a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
2608a1cf66bbSMatthew G. Knepley   }
2609a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
2610a1cf66bbSMatthew G. Knepley }
2611a1cf66bbSMatthew G. Knepley 
2612a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
2613a1cf66bbSMatthew G. Knepley {
2614a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
2615a1cf66bbSMatthew G. Knepley   *geom = NULL;
2616a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
2617a1cf66bbSMatthew G. Knepley }
2618a1cf66bbSMatthew G. Knepley 
261992d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
262092d50984SMatthew G. Knepley {
262192d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
262292d50984SMatthew G. Knepley   const char      *name       = "Residual";
262392d50984SMatthew G. Knepley   DM               dmAux      = NULL;
262492d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
262592d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
262692d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
262792d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
262892d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
262992d50984SMatthew G. Knepley   DMField          coordField = NULL;
2630c0006e53SPatrick Farrell   Vec              locA;
2631c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
263292d50984SMatthew G. Knepley   IS               chunkIS;
263392d50984SMatthew G. Knepley   const PetscInt  *cells;
263492d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
2635364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
263692d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
263792d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
263892d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
263992d50984SMatthew G. Knepley   PetscErrorCode   ierr;
264092d50984SMatthew G. Knepley 
264192d50984SMatthew G. Knepley   PetscFunctionBegin;
264292d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
264392d50984SMatthew G. Knepley   /* FEM+FVM */
264492d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
264592d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
264692d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
264792d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
264892d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
264992d50984SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
265092d50984SMatthew G. Knepley   if (locA) {
265192d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
265292d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
265392d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
265492d50984SMatthew G. Knepley   }
265592d50984SMatthew G. Knepley   /* 2: Get geometric data */
265692d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
265792d50984SMatthew G. Knepley     PetscObject  obj;
265892d50984SMatthew G. Knepley     PetscClassId id;
265992d50984SMatthew G. Knepley     PetscBool    fimp;
266092d50984SMatthew G. Knepley 
266192d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
266292d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
266392d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
266492d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
266592d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
2666364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
266792d50984SMatthew G. Knepley   }
266892d50984SMatthew G. Knepley   if (useFEM) {
266992d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
267092d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
267192d50984SMatthew G. Knepley     if (maxDegree <= 1) {
267292d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
267392d50984SMatthew G. Knepley       if (affineQuad) {
267492d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
267592d50984SMatthew G. Knepley       }
267692d50984SMatthew G. Knepley     } else {
267792d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
267892d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
267992d50984SMatthew G. Knepley         PetscObject  obj;
268092d50984SMatthew G. Knepley         PetscClassId id;
268192d50984SMatthew G. Knepley         PetscBool    fimp;
268292d50984SMatthew G. Knepley 
268392d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
268492d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
268592d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
268692d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
268792d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
268892d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
268992d50984SMatthew G. Knepley 
269092d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
269192d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
269292d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
269392d50984SMatthew G. Knepley         }
269492d50984SMatthew G. Knepley       }
269592d50984SMatthew G. Knepley     }
269692d50984SMatthew G. Knepley   }
269792d50984SMatthew G. Knepley   /* Loop over chunks */
269892d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
269992d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
270092d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
270192d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
270292d50984SMatthew G. Knepley   numChunks     = 1;
270392d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
270492d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
270592d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
2706c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
2707c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
2708c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
270992d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
2710c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
271192d50984SMatthew G. Knepley 
271292d50984SMatthew G. Knepley     /* Extract field coefficients */
271392d50984SMatthew G. Knepley     if (useFEM) {
271492d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
271592d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
271692d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
271792d50984SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
271892d50984SMatthew G. Knepley     }
271992d50984SMatthew G. Knepley     /* TODO We will interlace both our field coefficients (u, u_t, uL, uR, etc.) and our output (elemVec, fL, fR). I think this works */
272092d50984SMatthew G. Knepley     /* Loop over fields */
272192d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
272292d50984SMatthew G. Knepley       PetscObject  obj;
272392d50984SMatthew G. Knepley       PetscClassId id;
272492d50984SMatthew G. Knepley       PetscBool    fimp;
272592d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
272692d50984SMatthew G. Knepley 
272792d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
272892d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
272992d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
273092d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
273192d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
273292d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
273392d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
273492d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
273592d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
273692d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
273792d50984SMatthew G. Knepley 
273892d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
273992d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
274092d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
274192d50984SMatthew G. Knepley         blockSize = Nb;
274292d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
274392d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
274492d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
274592d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
274692d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
274792d50984SMatthew G. Knepley         offset    = numCells - Nr;
274892d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
274992d50984SMatthew G. Knepley         /*   For FV, I think we use a P0 basis and the cell coefficients (for subdivided cells, we can tweak the basis tabulation to be the indicator function) */
275092d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
275192d50984SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
275292d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
275392d50984SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
275492d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
275592d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
275692d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
275792d50984SMatthew G. Knepley 
275892d50984SMatthew G. Knepley         Ne = numFaces;
275992d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
276092d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
276192d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
276292d50984SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
276392d50984SMatthew G. Knepley     }
276492d50984SMatthew G. Knepley     /* Loop over domain */
276592d50984SMatthew G. Knepley     if (useFEM) {
276692d50984SMatthew G. Knepley       /* Add elemVec to locX */
276792d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
276892d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
276992d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
277092d50984SMatthew G. Knepley 
277192d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
277292d50984SMatthew G. Knepley         if (ghostLabel) {
277392d50984SMatthew G. Knepley           PetscInt ghostVal;
277492d50984SMatthew G. Knepley 
277592d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
277692d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
277792d50984SMatthew G. Knepley         }
277892d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
277992d50984SMatthew G. Knepley       }
278092d50984SMatthew G. Knepley     }
278192d50984SMatthew G. Knepley     /* Handle time derivative */
278292d50984SMatthew G. Knepley     if (locX_t) {
278392d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
278492d50984SMatthew G. Knepley 
278592d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
278692d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
278792d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
278892d50984SMatthew G. Knepley         PetscFV      fv;
278992d50984SMatthew G. Knepley         PetscObject  obj;
279092d50984SMatthew G. Knepley         PetscClassId id;
279192d50984SMatthew G. Knepley         PetscInt     pdim, d;
279292d50984SMatthew G. Knepley 
279392d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
279492d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
279592d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
279692d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
279792d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
279892d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
279992d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
280092d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
280192d50984SMatthew G. Knepley 
280292d50984SMatthew G. Knepley           if (ghostLabel) {
280392d50984SMatthew G. Knepley             PetscInt ghostVal;
280492d50984SMatthew G. Knepley 
280592d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
280692d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
280792d50984SMatthew G. Knepley           }
280892d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
280992d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
281092d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
281192d50984SMatthew G. Knepley         }
281292d50984SMatthew G. Knepley       }
281392d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
281492d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
281592d50984SMatthew G. Knepley     }
281692d50984SMatthew G. Knepley     if (useFEM) {
281792d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
281892d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
281992d50984SMatthew G. Knepley     }
282092d50984SMatthew G. Knepley   }
282192d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
282292d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
282392d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
282492d50984SMatthew G. Knepley   if (useFEM) {
282592d50984SMatthew G. Knepley     if (maxDegree <= 1) {
282692d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
282792d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
282892d50984SMatthew G. Knepley     } else {
282992d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
283092d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
283192d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
283292d50984SMatthew G. Knepley       }
283392d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
283492d50984SMatthew G. Knepley     }
283592d50984SMatthew G. Knepley   }
283692d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
283792d50984SMatthew G. Knepley   PetscFunctionReturn(0);
283892d50984SMatthew G. Knepley }
283992d50984SMatthew G. Knepley 
2840a1cf66bbSMatthew G. Knepley /*
2841a1cf66bbSMatthew G. Knepley   We always assemble JacP, and if the matrix is different from Jac and two different sets of point functions are provided, we also assemble Jac
2842a1cf66bbSMatthew G. Knepley 
2843a1cf66bbSMatthew G. Knepley   X   - The local solution vector
2844a1cf66bbSMatthew G. Knepley   X_t - The local solution time derviative vector, or NULL
2845a1cf66bbSMatthew G. Knepley */
2846a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
2847a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
2848a1cf66bbSMatthew G. Knepley {
2849a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
2850a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
2851a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
2852a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
2853a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
2854a1cf66bbSMatthew G. Knepley   Vec              A;
2855a1cf66bbSMatthew G. Knepley   DMField          coordField;
2856a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
2857a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
2858a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
2859a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
28609b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
2861a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
28629b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
2863a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
2864a1cf66bbSMatthew G. Knepley 
2865a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
2866a1cf66bbSMatthew G. Knepley   CHKMEMQ;
2867a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
2868a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2869a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
2870a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2871a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2872a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2873a1cf66bbSMatthew G. Knepley   if (dmAux) {
2874a1cf66bbSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
2875a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2876a1cf66bbSMatthew G. Knepley   }
2877a1cf66bbSMatthew G. Knepley   /* Get flags */
2878a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2879a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
2880a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
2881a1cf66bbSMatthew G. Knepley     PetscObject  disc;
2882a1cf66bbSMatthew G. Knepley     PetscClassId id;
2883a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
2884a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
2885a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
2886a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
2887a1cf66bbSMatthew G. Knepley   }
2888a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
2889a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
2890a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
2891a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
2892a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
2893a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
2894a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
2895a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
2896a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
2897a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
2898a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
2899a1cf66bbSMatthew G. Knepley   if (hasFV)    {ierr = MatSetOption(JP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);} /* No allocated space for FV stuff, so ignore the zero entries */
2900a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
2901a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
2902a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2903a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
2904a1cf66bbSMatthew G. Knepley   CHKMEMQ;
2905a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
2906a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
2907a1cf66bbSMatthew G. Knepley     PetscFE         fe;
2908a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
2909a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
2910a1cf66bbSMatthew G. Knepley 
2911a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
2912a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
2913a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
2914a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2915a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2916a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
2917a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
2918a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
2919a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
2920a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
2921a1cf66bbSMatthew G. Knepley   } else {
2922a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
2923a1cf66bbSMatthew G. Knepley     numChunks = 1;
2924a1cf66bbSMatthew G. Knepley   }
2925a1cf66bbSMatthew G. Knepley   /* Get work space */
2926a1cf66bbSMatthew G. Knepley   wsz  = (((X?1:0) + (X_t?1:0) + (dmAux?1:0))*totDim + ((hasJac?1:0) + (hasPrec?1:0) + (hasDyn?1:0))*totDim*totDim)*chunkSize;
2927a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
2928a1cf66bbSMatthew G. Knepley   ierr = PetscMemzero(work, wsz * sizeof(PetscScalar));CHKERRQ(ierr);
2929a1cf66bbSMatthew G. Knepley   off      = 0;
2930a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
2931a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
2932a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
2933a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
2934a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
2935a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
2936a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
2937a1cf66bbSMatthew G. Knepley   /* Setup geometry */
2938a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
29399b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
29409b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
2941a1cf66bbSMatthew G. Knepley   if (!qGeom) {
2942a1cf66bbSMatthew G. Knepley     PetscFE fe;
2943a1cf66bbSMatthew G. Knepley 
2944a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
2945a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
2946a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
2947a1cf66bbSMatthew G. Knepley   }
2948a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
2949a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
2950a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
2951a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
2952a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
2953a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
2954a1cf66bbSMatthew G. Knepley     PetscInt         c;
2955a1cf66bbSMatthew G. Knepley 
2956a1cf66bbSMatthew G. Knepley     /* Extract values */
2957a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
2958a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
2959a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
2960a1cf66bbSMatthew G. Knepley       PetscInt       i;
2961a1cf66bbSMatthew G. Knepley 
2962a1cf66bbSMatthew G. Knepley       if (X) {
2963a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2964a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
2965a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
2966a1cf66bbSMatthew G. Knepley       }
2967a1cf66bbSMatthew G. Knepley       if (X_t) {
2968a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2969a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
2970a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
2971a1cf66bbSMatthew G. Knepley       }
2972a1cf66bbSMatthew G. Knepley       if (dmAux) {
2973a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2974a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
2975a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
2976a1cf66bbSMatthew G. Knepley       }
2977a1cf66bbSMatthew G. Knepley     }
2978a1cf66bbSMatthew G. Knepley     CHKMEMQ;
2979a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
2980a1cf66bbSMatthew G. Knepley       PetscFE fe;
2981a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
2982a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
2983a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN,     fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
2984a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);}
2985a1cf66bbSMatthew G. Knepley         if (hasDyn)  {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);}
2986a1cf66bbSMatthew G. Knepley       }
2987a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
2988a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
2989a1cf66bbSMatthew G. Knepley         PetscFV  fv;
2990a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
2991a1cf66bbSMatthew G. Knepley 
2992a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
2993a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
2994a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
2995a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
2996a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
2997a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
2998a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
2999a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
3000a1cf66bbSMatthew G. Knepley           }
3001a1cf66bbSMatthew G. Knepley         }
3002a1cf66bbSMatthew G. Knepley       }
3003a1cf66bbSMatthew G. Knepley     }
3004a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3005a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
3006a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
3007a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
3008a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3009a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3010a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
3011a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3012a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3013a1cf66bbSMatthew G. Knepley       }
3014a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
3015a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
3016a1cf66bbSMatthew G. Knepley     }
3017a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3018a1cf66bbSMatthew G. Knepley   }
3019a1cf66bbSMatthew G. Knepley   /* Cleanup */
3020a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3021a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
3022a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
3023a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3024a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, ((1 + (X_t?1:0) + (dmAux?1:0))*totDim + ((hasJac?1:0) + (hasPrec?1:0) + (hasDyn?1:0))*totDim*totDim)*chunkSize, MPIU_SCALAR, &work);CHKERRQ(ierr);
3025a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
3026a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
3027a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
3028a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
3029a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3030a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3031a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3032a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3033a1cf66bbSMatthew G. Knepley }
3034