xref: /petsc/src/dm/impls/plex/plexfem.c (revision 5fedec97950c19de564efaecd0f125b1a6cb2b20)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3cb1e1211SMatthew G Knepley 
48e3a2eefSMatthew G. Knepley #include <petscblaslapack.h>
5e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h>
6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
7af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
8a0845e3aSMatthew G. Knepley 
92f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy)
102f856554SMatthew G. Knepley {
112f856554SMatthew G. Knepley   PetscBool      isPlex;
122f856554SMatthew G. Knepley   PetscErrorCode ierr;
132f856554SMatthew G. Knepley 
142f856554SMatthew G. Knepley   PetscFunctionBegin;
152f856554SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
162f856554SMatthew G. Knepley   if (isPlex) {
172f856554SMatthew G. Knepley     *plex = dm;
182f856554SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
192f856554SMatthew G. Knepley   } else {
202f856554SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
212f856554SMatthew G. Knepley     if (!*plex) {
222f856554SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, plex);CHKERRQ(ierr);
232f856554SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
242f856554SMatthew G. Knepley       if (copy) {
252f856554SMatthew G. Knepley         DMSubDomainHookLink link;
269a2a23afSMatthew G. Knepley 
279a2a23afSMatthew G. Knepley         ierr = DMCopyAuxiliaryVec(dm, *plex);CHKERRQ(ierr);
289a2a23afSMatthew G. Knepley         /* Run the subdomain hook (this will copy the DMSNES/DMTS) */
292f856554SMatthew G. Knepley         for (link = dm->subdomainhook; link; link = link->next) {
302f856554SMatthew G. Knepley           if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);}
312f856554SMatthew G. Knepley         }
322f856554SMatthew G. Knepley       }
332f856554SMatthew G. Knepley     } else {
342f856554SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
352f856554SMatthew G. Knepley     }
362f856554SMatthew G. Knepley   }
372f856554SMatthew G. Knepley   PetscFunctionReturn(0);
382f856554SMatthew G. Knepley }
392f856554SMatthew G. Knepley 
409b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
419b6f715bSMatthew G. Knepley {
429b6f715bSMatthew G. Knepley   PetscFEGeom *geom = (PetscFEGeom *) ctx;
439b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
449b6f715bSMatthew G. Knepley 
459b6f715bSMatthew G. Knepley   PetscFunctionBegin;
469b6f715bSMatthew G. Knepley   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
479b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
489b6f715bSMatthew G. Knepley }
499b6f715bSMatthew G. Knepley 
509b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
519b6f715bSMatthew G. Knepley {
529b6f715bSMatthew G. Knepley   char            composeStr[33] = {0};
539b6f715bSMatthew G. Knepley   PetscObjectId   id;
549b6f715bSMatthew G. Knepley   PetscContainer  container;
559b6f715bSMatthew G. Knepley   PetscErrorCode  ierr;
569b6f715bSMatthew G. Knepley 
579b6f715bSMatthew G. Knepley   PetscFunctionBegin;
589b6f715bSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
599b6f715bSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr);
609b6f715bSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
619b6f715bSMatthew G. Knepley   if (container) {
629b6f715bSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
639b6f715bSMatthew G. Knepley   } else {
649b6f715bSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
659b6f715bSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
669b6f715bSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
679b6f715bSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
689b6f715bSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
699b6f715bSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
709b6f715bSMatthew G. Knepley   }
719b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
729b6f715bSMatthew G. Knepley }
739b6f715bSMatthew G. Knepley 
749b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
759b6f715bSMatthew G. Knepley {
769b6f715bSMatthew G. Knepley   PetscFunctionBegin;
779b6f715bSMatthew G. Knepley   *geom = NULL;
789b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
799b6f715bSMatthew G. Knepley }
809b6f715bSMatthew G. Knepley 
8146fa42a0SMatthew G. Knepley /*@
8246fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
8346fa42a0SMatthew G. Knepley 
8446fa42a0SMatthew G. Knepley   Not collective
8546fa42a0SMatthew G. Knepley 
864165533cSJose E. Roman   Input Parameters:
8746fa42a0SMatthew G. Knepley + dm   - the DM
8846fa42a0SMatthew G. Knepley - unit - The SI unit
8946fa42a0SMatthew G. Knepley 
904165533cSJose E. Roman   Output Parameter:
9146fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
9246fa42a0SMatthew G. Knepley 
9346fa42a0SMatthew G. Knepley   Level: advanced
9446fa42a0SMatthew G. Knepley 
9546fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
9646fa42a0SMatthew G. Knepley @*/
97cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
98cb1e1211SMatthew G Knepley {
99cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
100cb1e1211SMatthew G Knepley 
101cb1e1211SMatthew G Knepley   PetscFunctionBegin;
102cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
103cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
104cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
105cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
106cb1e1211SMatthew G Knepley }
107cb1e1211SMatthew G Knepley 
10846fa42a0SMatthew G. Knepley /*@
10946fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
11046fa42a0SMatthew G. Knepley 
11146fa42a0SMatthew G. Knepley   Not collective
11246fa42a0SMatthew G. Knepley 
1134165533cSJose E. Roman   Input Parameters:
11446fa42a0SMatthew G. Knepley + dm   - the DM
11546fa42a0SMatthew G. Knepley . unit - The SI unit
11646fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
11746fa42a0SMatthew G. Knepley 
11846fa42a0SMatthew G. Knepley   Level: advanced
11946fa42a0SMatthew G. Knepley 
12046fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
12146fa42a0SMatthew G. Knepley @*/
122cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
123cb1e1211SMatthew G Knepley {
124cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
125cb1e1211SMatthew G Knepley 
126cb1e1211SMatthew G Knepley   PetscFunctionBegin;
127cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
129cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
130cb1e1211SMatthew G Knepley }
131cb1e1211SMatthew G Knepley 
132327779f2SMatthew Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nc, PetscScalar *mode, void *ctx)
133026175e5SToby Isaac {
13412adca46SMatthew 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}}};
135026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
136ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
137026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
138026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
139026175e5SToby Isaac 
140ad917190SMatthew G. Knepley   PetscFunctionBegin;
141ff1e0c32SBarry Smith   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %D does not match context dimension %D", dim, dim2);
142026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
143026175e5SToby Isaac   if (d < dim) {
14412adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
145ad917190SMatthew G. Knepley   } else {
146026175e5SToby Isaac     for (i = 0; i < dim; i++) {
147026175e5SToby Isaac       for (j = 0; j < dim; j++) {
14812adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
149026175e5SToby Isaac       }
150026175e5SToby Isaac     }
151026175e5SToby Isaac   }
152ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
153026175e5SToby Isaac }
154026175e5SToby Isaac 
155cc4e42d9SMartin Diehl /*@
15612adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
157cb1e1211SMatthew G Knepley 
158d083f849SBarry Smith   Collective on dm
159cb1e1211SMatthew G Knepley 
1604165533cSJose E. Roman   Input Parameters:
16156cf3b9cSMatthew G. Knepley + dm - the DM
16256cf3b9cSMatthew G. Knepley - field - The field number for the rigid body space, or 0 for the default
163cb1e1211SMatthew G Knepley 
1644165533cSJose E. Roman   Output Parameter:
165cb1e1211SMatthew G Knepley . sp - the null space
166cb1e1211SMatthew G Knepley 
16712adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
168cb1e1211SMatthew G Knepley 
169cb1e1211SMatthew G Knepley   Level: advanced
170cb1e1211SMatthew G Knepley 
17112adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
172cb1e1211SMatthew G Knepley @*/
17356cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscInt field, MatNullSpace *sp)
174cb1e1211SMatthew G Knepley {
17556cf3b9cSMatthew G. Knepley   PetscErrorCode (**func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *);
176cb1e1211SMatthew G Knepley   MPI_Comm          comm;
177026175e5SToby Isaac   Vec               mode[6];
178026175e5SToby Isaac   PetscSection      section, globalSection;
17956cf3b9cSMatthew G. Knepley   PetscInt          dim, dimEmbed, Nf, n, m, mmin, d, i, j;
180cb1e1211SMatthew G Knepley   PetscErrorCode    ierr;
181cb1e1211SMatthew G Knepley 
182cb1e1211SMatthew G Knepley   PetscFunctionBegin;
183cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
184c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1859d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
18656cf3b9cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
18756cf3b9cSMatthew G. Knepley   if (Nf && (field < 0 || field >= Nf)) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, Nf)", field, Nf);
18856cf3b9cSMatthew G. Knepley   if (dim == 1 && Nf < 2) {
189cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
190cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
191cb1e1211SMatthew G Knepley   }
19292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
193e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
194cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
19556cf3b9cSMatthew G. Knepley   ierr = PetscCalloc1(Nf, &func);CHKERRQ(ierr);
196b247467aSMatthew G. Knepley   m    = (dim*(dim+1))/2;
197cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
198cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
199cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
200b247467aSMatthew G. Knepley   ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr);
201b247467aSMatthew G. Knepley   mmin = PetscMin(m, n);
20256cf3b9cSMatthew G. Knepley   func[field] = DMPlexProjectRigidBody_Private;
203cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
204026175e5SToby Isaac   for (d = 0; d < m; d++) {
205330485fdSToby Isaac     PetscInt ctx[2];
206026175e5SToby Isaac     void    *voidctx = (void *) (&ctx[0]);
207cb1e1211SMatthew G Knepley 
2089d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
209330485fdSToby Isaac     ctx[1] = d;
21056cf3b9cSMatthew G. Knepley     ierr = DMProjectFunction(dm, 0.0, func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
211cb1e1211SMatthew G Knepley   }
2123b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
213b50a2c0aSJacob Faibussowitsch   for (i = 0; i < mmin; ++i) {
214b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
215b50a2c0aSJacob Faibussowitsch 
2169019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
217b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], mmin-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
218b50a2c0aSJacob Faibussowitsch     for (j = i+1; j < mmin; ++j) {
219b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
220b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
221b50a2c0aSJacob Faibussowitsch     }
222cb1e1211SMatthew G Knepley   }
223b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
224b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
22556cf3b9cSMatthew G. Knepley   ierr = PetscFree(func);CHKERRQ(ierr);
226cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
227cb1e1211SMatthew G Knepley }
228cb1e1211SMatthew G Knepley 
229cc4e42d9SMartin Diehl /*@
23012adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
23112adca46SMatthew G. Knepley 
232d083f849SBarry Smith   Collective on dm
23312adca46SMatthew G. Knepley 
2344165533cSJose E. Roman   Input Parameters:
23512adca46SMatthew G. Knepley + dm    - the DM
23612adca46SMatthew G. Knepley . nb    - The number of bodies
23712adca46SMatthew G. Knepley . label - The DMLabel marking each domain
23812adca46SMatthew G. Knepley . nids  - The number of ids per body
23912adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
24012adca46SMatthew G. Knepley 
2414165533cSJose E. Roman   Output Parameter:
24212adca46SMatthew G. Knepley . sp - the null space
24312adca46SMatthew G. Knepley 
24412adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
24512adca46SMatthew G. Knepley 
24612adca46SMatthew G. Knepley   Level: advanced
24712adca46SMatthew G. Knepley 
24812adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
24912adca46SMatthew G. Knepley @*/
25012adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
25112adca46SMatthew G. Knepley {
25212adca46SMatthew G. Knepley   MPI_Comm       comm;
25312adca46SMatthew G. Knepley   PetscSection   section, globalSection;
25412adca46SMatthew G. Knepley   Vec           *mode;
25512adca46SMatthew G. Knepley   PetscScalar   *dots;
25612adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
25712adca46SMatthew G. Knepley   PetscErrorCode ierr;
25812adca46SMatthew G. Knepley 
25912adca46SMatthew G. Knepley   PetscFunctionBegin;
26012adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
26112adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26212adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
26392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
264e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
26512adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
26612adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
26712adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
26812adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
26912adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
27012adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
27112adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
27212adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
27312adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
27412adca46SMatthew G. Knepley       PetscInt         ctx[2];
27512adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
27612adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
27712adca46SMatthew G. Knepley 
27812adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
27912adca46SMatthew G. Knepley       ctx[1] = d;
28012adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
28112adca46SMatthew G. Knepley       off   += nids[b];
28212adca46SMatthew G. Knepley     }
28312adca46SMatthew G. Knepley   }
2843b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
285606c1a1cSJacob Faibussowitsch   for (i = 0; i < m; ++i) {
286b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
2875a0e29b9SJacob Faibussowitsch 
2889019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
289b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], m-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
2905a0e29b9SJacob Faibussowitsch     for (j = i+1; j < m; ++j) {
291b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
292b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
2935a0e29b9SJacob Faibussowitsch     }
29412adca46SMatthew G. Knepley   }
29512adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
29612adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
29712adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
29812adca46SMatthew G. Knepley   PetscFunctionReturn(0);
29912adca46SMatthew G. Knepley }
30012adca46SMatthew G. Knepley 
301b29cfa1cSToby Isaac /*@
302b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
303b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
304b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
305b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
306b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
307b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
308b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
309b29cfa1cSToby Isaac 
310b29cfa1cSToby Isaac   Input Parameters:
311b29cfa1cSToby Isaac + dm - the DMPlex object
312b29cfa1cSToby Isaac - height - the maximum projection height >= 0
313b29cfa1cSToby Isaac 
314b29cfa1cSToby Isaac   Level: advanced
315b29cfa1cSToby Isaac 
3164d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
317b29cfa1cSToby Isaac @*/
318b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
319b29cfa1cSToby Isaac {
320b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
321b29cfa1cSToby Isaac 
322b29cfa1cSToby Isaac   PetscFunctionBegin;
323b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
324b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
325b29cfa1cSToby Isaac   PetscFunctionReturn(0);
326b29cfa1cSToby Isaac }
327b29cfa1cSToby Isaac 
328b29cfa1cSToby Isaac /*@
329b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
330b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
331b29cfa1cSToby Isaac 
332b29cfa1cSToby Isaac   Input Parameters:
333b29cfa1cSToby Isaac . dm - the DMPlex object
334b29cfa1cSToby Isaac 
335b29cfa1cSToby Isaac   Output Parameters:
336b29cfa1cSToby Isaac . height - the maximum projection height
337b29cfa1cSToby Isaac 
338b29cfa1cSToby Isaac   Level: intermediate
339b29cfa1cSToby Isaac 
3404d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
341b29cfa1cSToby Isaac @*/
342b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
343b29cfa1cSToby Isaac {
344b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
345b29cfa1cSToby Isaac 
346b29cfa1cSToby Isaac   PetscFunctionBegin;
347b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
348b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
349b29cfa1cSToby Isaac   PetscFunctionReturn(0);
350b29cfa1cSToby Isaac }
351b29cfa1cSToby Isaac 
352ca3d3a14SMatthew G. Knepley typedef struct {
353ca3d3a14SMatthew G. Knepley   PetscReal    alpha; /* The first Euler angle, and in 2D the only one */
354ca3d3a14SMatthew G. Knepley   PetscReal    beta;  /* The second Euler angle */
355ca3d3a14SMatthew G. Knepley   PetscReal    gamma; /* The third Euler angle */
356ca3d3a14SMatthew G. Knepley   PetscInt     dim;   /* The dimension of R */
357ca3d3a14SMatthew G. Knepley   PetscScalar *R;     /* The rotation matrix, transforming a vector in the local basis to the global basis */
358ca3d3a14SMatthew G. Knepley   PetscScalar *RT;    /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */
359ca3d3a14SMatthew G. Knepley } RotCtx;
360ca3d3a14SMatthew G. Knepley 
361ca3d3a14SMatthew G. Knepley /*
362ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
363ca3d3a14SMatthew G. Knepley   we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows:
364ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis.
365ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis.
366ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
367ca3d3a14SMatthew G. Knepley */
368ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx)
369ca3d3a14SMatthew G. Knepley {
370ca3d3a14SMatthew G. Knepley   RotCtx        *rc  = (RotCtx *) ctx;
371ca3d3a14SMatthew G. Knepley   PetscInt       dim = rc->dim;
372ca3d3a14SMatthew G. Knepley   PetscReal      c1, s1, c2, s2, c3, s3;
373ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
374ca3d3a14SMatthew G. Knepley 
375ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
376ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr);
377ca3d3a14SMatthew G. Knepley   switch (dim) {
378ca3d3a14SMatthew G. Knepley   case 2:
379ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
380ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1;rc->R[1] = s1;
381ca3d3a14SMatthew G. Knepley     rc->R[2] = -s1;rc->R[3] = c1;
382580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
383b458e8f1SJose E. Roman     DMPlex_Transpose2D_Internal(rc->RT);
384ca3d3a14SMatthew G. Knepley     break;
385ca3d3a14SMatthew G. Knepley   case 3:
386ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
387ca3d3a14SMatthew G. Knepley     c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta);
388ca3d3a14SMatthew G. Knepley     c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma);
389ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1*c3 - c2*s1*s3;rc->R[1] =  c3*s1    + c1*c2*s3;rc->R[2] = s2*s3;
390ca3d3a14SMatthew G. Knepley     rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] =  c1*c2*c3 - s1*s3;   rc->R[5] = c3*s2;
391ca3d3a14SMatthew G. Knepley     rc->R[6] =  s1*s2;           rc->R[7] = -c1*s2;              rc->R[8] = c2;
392580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
393b458e8f1SJose E. Roman     DMPlex_Transpose3D_Internal(rc->RT);
394ca3d3a14SMatthew G. Knepley     break;
395ca3d3a14SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
396ca3d3a14SMatthew G. Knepley   }
397ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
398ca3d3a14SMatthew G. Knepley }
399ca3d3a14SMatthew G. Knepley 
400ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx)
401ca3d3a14SMatthew G. Knepley {
402ca3d3a14SMatthew G. Knepley   RotCtx        *rc = (RotCtx *) ctx;
403ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
404ca3d3a14SMatthew G. Knepley 
405ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
406ca3d3a14SMatthew G. Knepley   ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr);
407ca3d3a14SMatthew G. Knepley   ierr = PetscFree(rc);CHKERRQ(ierr);
408ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
409ca3d3a14SMatthew G. Knepley }
410ca3d3a14SMatthew G. Knepley 
411ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx)
412ca3d3a14SMatthew G. Knepley {
413ca3d3a14SMatthew G. Knepley   RotCtx *rc = (RotCtx *) ctx;
414ca3d3a14SMatthew G. Knepley 
415ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
416ca3d3a14SMatthew G. Knepley   PetscValidPointer(ctx, 5);
417ca3d3a14SMatthew G. Knepley   if (l2g) {*A = rc->R;}
418ca3d3a14SMatthew G. Knepley   else     {*A = rc->RT;}
419ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
420ca3d3a14SMatthew G. Knepley }
421ca3d3a14SMatthew G. Knepley 
422ab6a9622SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApplyReal_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscReal *y, PetscReal *z, void *ctx)
423ab6a9622SMatthew G. Knepley {
424ec277c0fSMatthew G. Knepley   PetscErrorCode ierr;
425ec277c0fSMatthew G. Knepley 
426ec277c0fSMatthew G. Knepley   PetscFunctionBegin;
427ab6a9622SMatthew G. Knepley   #if defined(PETSC_USE_COMPLEX)
428ab6a9622SMatthew G. Knepley   switch (dim) {
429ab6a9622SMatthew G. Knepley     case 2:
430ab6a9622SMatthew G. Knepley     {
43127104ee2SJacob Faibussowitsch       PetscScalar yt[2] = {y[0], y[1]}, zt[2] = {0.0,0.0};
432ab6a9622SMatthew G. Knepley 
433ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
434ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]);
435ab6a9622SMatthew G. Knepley     }
4369b4815dcSMatthew G. Knepley     break;
437ab6a9622SMatthew G. Knepley     case 3:
438ab6a9622SMatthew G. Knepley     {
43927104ee2SJacob Faibussowitsch       PetscScalar yt[3] = {y[0], y[1], y[2]}, zt[3] = {0.0,0.0,0.0};
440ab6a9622SMatthew G. Knepley 
441ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
442ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); z[2] = PetscRealPart(zt[2]);
443ab6a9622SMatthew G. Knepley     }
4449b4815dcSMatthew G. Knepley     break;
445ab6a9622SMatthew G. Knepley   }
446ab6a9622SMatthew G. Knepley   #else
447ab6a9622SMatthew G. Knepley   ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, y, z, ctx);CHKERRQ(ierr);
448ab6a9622SMatthew G. Knepley   #endif
449ec277c0fSMatthew G. Knepley   PetscFunctionReturn(0);
450ab6a9622SMatthew G. Knepley }
451ab6a9622SMatthew G. Knepley 
452ec277c0fSMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx)
453ca3d3a14SMatthew G. Knepley {
454ca3d3a14SMatthew G. Knepley   const PetscScalar *A;
455ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
456ca3d3a14SMatthew G. Knepley 
457ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
458ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr);
459ca3d3a14SMatthew G. Knepley   switch (dim) {
4601fba962aSMatthew G. Knepley   case 2: DMPlex_Mult2D_Internal(A, 1, y, z);break;
4611fba962aSMatthew G. Knepley   case 3: DMPlex_Mult3D_Internal(A, 1, y, z);break;
462ca3d3a14SMatthew G. Knepley   }
463ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
464ca3d3a14SMatthew G. Knepley }
465ca3d3a14SMatthew G. Knepley 
466ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a)
467ca3d3a14SMatthew G. Knepley {
468ca3d3a14SMatthew G. Knepley   PetscSection       ts;
469ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tva;
470ca3d3a14SMatthew G. Knepley   PetscInt           dof;
471ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
472ca3d3a14SMatthew G. Knepley 
473ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
47492fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
475ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
476ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
47727104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, &tva);CHKERRQ(ierr);
478ca3d3a14SMatthew G. Knepley   if (l2g) {
479ca3d3a14SMatthew G. Knepley     switch (dof) {
4801fba962aSMatthew G. Knepley     case 4: DMPlex_Mult2D_Internal(tva, 1, a, a);break;
4811fba962aSMatthew G. Knepley     case 9: DMPlex_Mult3D_Internal(tva, 1, a, a);break;
482ca3d3a14SMatthew G. Knepley     }
483ca3d3a14SMatthew G. Knepley   } else {
484ca3d3a14SMatthew G. Knepley     switch (dof) {
4851fba962aSMatthew G. Knepley     case 4: DMPlex_MultTranspose2D_Internal(tva, 1, a, a);break;
4861fba962aSMatthew G. Knepley     case 9: DMPlex_MultTranspose3D_Internal(tva, 1, a, a);break;
487ca3d3a14SMatthew G. Knepley     }
488ca3d3a14SMatthew G. Knepley   }
489ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
490ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
491ca3d3a14SMatthew G. Knepley }
492ca3d3a14SMatthew G. Knepley 
493ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformFieldTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt pf, PetscInt f, PetscInt pg, PetscInt g, PetscBool l2g, PetscInt lda, PetscScalar *a)
494ca3d3a14SMatthew G. Knepley {
495ca3d3a14SMatthew G. Knepley   PetscSection       s, ts;
496ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tvaf, *tvag;
497ca3d3a14SMatthew G. Knepley   PetscInt           fdof, gdof, fpdof, gpdof;
498ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
499ca3d3a14SMatthew G. Knepley 
500ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
50192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
50292fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
503ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr);
504ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr);
505ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr);
506ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr);
507ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
50827104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, &tvaf);CHKERRQ(ierr);
50927104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, &tvag);CHKERRQ(ierr);
510ca3d3a14SMatthew G. Knepley   if (l2g) {
511ca3d3a14SMatthew G. Knepley     switch (fdof) {
512ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break;
513ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break;
514ca3d3a14SMatthew G. Knepley     }
515ca3d3a14SMatthew G. Knepley     switch (gdof) {
516ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break;
517ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break;
518ca3d3a14SMatthew G. Knepley     }
519ca3d3a14SMatthew G. Knepley   } else {
520ca3d3a14SMatthew G. Knepley     switch (fdof) {
521ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break;
522ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break;
523ca3d3a14SMatthew G. Knepley     }
524ca3d3a14SMatthew G. Knepley     switch (gdof) {
525ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break;
526ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break;
527ca3d3a14SMatthew G. Knepley     }
528ca3d3a14SMatthew G. Knepley   }
529ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
530ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
531ca3d3a14SMatthew G. Knepley }
532ca3d3a14SMatthew G. Knepley 
533ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a)
534ca3d3a14SMatthew G. Knepley {
535ca3d3a14SMatthew G. Knepley   PetscSection    s;
536ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
537ca3d3a14SMatthew G. Knepley   IS              clPoints;
538ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
539ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
540ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, Np, cp, dof, d = 0;
541ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
542ca3d3a14SMatthew G. Knepley 
543ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
54492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
545ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
546ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
547ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
548ca3d3a14SMatthew G. Knepley     for (cp = 0; cp < Np*2; cp += 2) {
549ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr);
550ca3d3a14SMatthew G. Knepley       if (!dof) continue;
551ca3d3a14SMatthew G. Knepley       if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);}
552ca3d3a14SMatthew G. Knepley       d += dof;
553ca3d3a14SMatthew G. Knepley     }
554ca3d3a14SMatthew G. Knepley   }
555ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
556ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
557ca3d3a14SMatthew G. Knepley }
558ca3d3a14SMatthew G. Knepley 
559ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a)
560ca3d3a14SMatthew G. Knepley {
561ca3d3a14SMatthew G. Knepley   PetscSection    s;
562ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
563ca3d3a14SMatthew G. Knepley   IS              clPoints;
564ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
565ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
5668bdb3c71SMatthew G. Knepley   PetscInt        Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c = 0;
567ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
568ca3d3a14SMatthew G. Knepley 
569ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
57092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
571ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
572ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
573ca3d3a14SMatthew G. Knepley   for (f = 0, r = 0; f < Nf; ++f) {
574ca3d3a14SMatthew G. Knepley     for (cpf = 0; cpf < Np*2; cpf += 2) {
575ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr);
576ca3d3a14SMatthew G. Knepley       for (g = 0, c = 0; g < Nf; ++g) {
577ca3d3a14SMatthew G. Knepley         for (cpg = 0; cpg < Np*2; cpg += 2) {
578ca3d3a14SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr);
579ca3d3a14SMatthew G. Knepley           ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr);
580ca3d3a14SMatthew G. Knepley           c += gdof;
581ca3d3a14SMatthew G. Knepley         }
582ca3d3a14SMatthew G. Knepley       }
583ca3d3a14SMatthew G. Knepley       if (c != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda);
584ca3d3a14SMatthew G. Knepley       r += fdof;
585ca3d3a14SMatthew G. Knepley     }
586ca3d3a14SMatthew G. Knepley   }
587ca3d3a14SMatthew G. Knepley   if (r != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda);
588ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
589ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
590ca3d3a14SMatthew G. Knepley }
591ca3d3a14SMatthew G. Knepley 
592ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g)
593ca3d3a14SMatthew G. Knepley {
594ca3d3a14SMatthew G. Knepley   DM                 tdm;
595ca3d3a14SMatthew G. Knepley   Vec                tv;
596ca3d3a14SMatthew G. Knepley   PetscSection       ts, s;
597ca3d3a14SMatthew G. Knepley   const PetscScalar *ta;
598ca3d3a14SMatthew G. Knepley   PetscScalar       *a, *va;
599ca3d3a14SMatthew G. Knepley   PetscInt           pStart, pEnd, p, Nf, f;
600ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
601ca3d3a14SMatthew G. Knepley 
602ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
603ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
604ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
60592fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
60692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
607ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
608ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
609ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(lv, &a);CHKERRQ(ierr);
610ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
611ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
612ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
61327104ee2SJacob Faibussowitsch       ierr = DMPlexPointLocalFieldRef(dm, p, f, a, &va);CHKERRQ(ierr);
614ca3d3a14SMatthew G. Knepley       ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr);
615ca3d3a14SMatthew G. Knepley     }
616ca3d3a14SMatthew G. Knepley   }
617ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr);
618ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
619ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
620ca3d3a14SMatthew G. Knepley }
621ca3d3a14SMatthew G. Knepley 
622ca3d3a14SMatthew G. Knepley /*@
623ca3d3a14SMatthew G. Knepley   DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis
624ca3d3a14SMatthew G. Knepley 
625ca3d3a14SMatthew G. Knepley   Input Parameters:
626ca3d3a14SMatthew G. Knepley + dm - The DM
627ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis
628ca3d3a14SMatthew G. Knepley 
629ca3d3a14SMatthew G. Knepley   Output Parameters:
630ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis
631ca3d3a14SMatthew G. Knepley 
632c0f8e1fdSMatthew G. Knepley   Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user will have a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal.
633c0f8e1fdSMatthew G. Knepley 
634ca3d3a14SMatthew G. Knepley   Level: developer
635ca3d3a14SMatthew G. Knepley 
636436bc73aSJed Brown .seealso: DMPlexLocalToGlobalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
637ca3d3a14SMatthew G. Knepley @*/
638ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv)
639ca3d3a14SMatthew G. Knepley {
640ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
641ca3d3a14SMatthew G. Knepley 
642ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
643ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
644ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
645ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr);
646ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
647ca3d3a14SMatthew G. Knepley }
648ca3d3a14SMatthew G. Knepley 
649ca3d3a14SMatthew G. Knepley /*@
650ca3d3a14SMatthew G. Knepley   DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis
651ca3d3a14SMatthew G. Knepley 
652ca3d3a14SMatthew G. Knepley   Input Parameters:
653ca3d3a14SMatthew G. Knepley + dm - The DM
654ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis
655ca3d3a14SMatthew G. Knepley 
656ca3d3a14SMatthew G. Knepley   Output Parameters:
657ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis
658ca3d3a14SMatthew G. Knepley 
659c0f8e1fdSMatthew G. Knepley   Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user would want a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal.
660c0f8e1fdSMatthew G. Knepley 
661ca3d3a14SMatthew G. Knepley   Level: developer
662ca3d3a14SMatthew G. Knepley 
663436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
664ca3d3a14SMatthew G. Knepley @*/
665ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv)
666ca3d3a14SMatthew G. Knepley {
667ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
668ca3d3a14SMatthew G. Knepley 
669ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
670ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
671ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
672ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr);
673ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
674ca3d3a14SMatthew G. Knepley }
675ca3d3a14SMatthew G. Knepley 
676ca3d3a14SMatthew G. Knepley /*@
677ca3d3a14SMatthew G. Knepley   DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions
678ca3d3a14SMatthew G. Knepley     and global solutions, to a local basis, appropriate for discretization integrals and assembly.
679ca3d3a14SMatthew G. Knepley 
680ca3d3a14SMatthew G. Knepley   Input Parameters:
681ca3d3a14SMatthew G. Knepley + dm    - The DM
682ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one
683ca3d3a14SMatthew G. Knepley . beta  - The second Euler angle
684f0fc11ceSJed Brown - gamma - The third Euler angle
685ca3d3a14SMatthew G. Knepley 
686ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
687ca3d3a14SMatthew G. Knepley   we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows:
688ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis.
689ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis.
690ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
691ca3d3a14SMatthew G. Knepley 
692ca3d3a14SMatthew G. Knepley   Level: developer
693ca3d3a14SMatthew G. Knepley 
694ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()
695ca3d3a14SMatthew G. Knepley @*/
696ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma)
697ca3d3a14SMatthew G. Knepley {
698ca3d3a14SMatthew G. Knepley   RotCtx        *rc;
699ca3d3a14SMatthew G. Knepley   PetscInt       cdim;
700ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
701ca3d3a14SMatthew G. Knepley 
702362febeeSStefano Zampini   PetscFunctionBegin;
703ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
704ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr);
705ca3d3a14SMatthew G. Knepley   dm->transformCtx       = rc;
706ca3d3a14SMatthew G. Knepley   dm->transformSetUp     = DMPlexBasisTransformSetUp_Rotation_Internal;
707ca3d3a14SMatthew G. Knepley   dm->transformDestroy   = DMPlexBasisTransformDestroy_Rotation_Internal;
708ca3d3a14SMatthew G. Knepley   dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal;
709ca3d3a14SMatthew G. Knepley   rc->dim   = cdim;
710ca3d3a14SMatthew G. Knepley   rc->alpha = alpha;
711ca3d3a14SMatthew G. Knepley   rc->beta  = beta;
712ca3d3a14SMatthew G. Knepley   rc->gamma = gamma;
713ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr);
714ca3d3a14SMatthew G. Knepley   ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr);
715ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
716ca3d3a14SMatthew G. Knepley }
717ca3d3a14SMatthew G. Knepley 
718b278463cSMatthew G. Knepley /*@C
719ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector using a function of the coordinates
720b278463cSMatthew G. Knepley 
721b278463cSMatthew G. Knepley   Input Parameters:
722b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
723b278463cSMatthew G. Knepley . time   - The time
724b278463cSMatthew G. Knepley . field  - The field to constrain
7251c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7261c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
727b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
728b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
729b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
730b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
731b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
732b278463cSMatthew G. Knepley 
733b278463cSMatthew G. Knepley   Output Parameter:
734b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
735b278463cSMatthew G. Knepley 
736b278463cSMatthew G. Knepley   Level: developer
737b278463cSMatthew G. Knepley 
738ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
739b278463cSMatthew G. Knepley @*/
7401c531cf8SMatthew 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)
74155f2e967SMatthew G. Knepley {
7420163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
74355f2e967SMatthew G. Knepley   void            **ctxs;
744d7ddef95SMatthew G. Knepley   PetscInt          numFields;
745d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
746d7ddef95SMatthew G. Knepley 
747d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
748d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
749d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
750d7ddef95SMatthew G. Knepley   funcs[field] = func;
751d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
7521c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
753d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
754d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
755d7ddef95SMatthew G. Knepley }
756d7ddef95SMatthew G. Knepley 
757b278463cSMatthew G. Knepley /*@C
758ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector using a function of the coordinates and field data
759b278463cSMatthew G. Knepley 
760b278463cSMatthew G. Knepley   Input Parameters:
761b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
762b278463cSMatthew G. Knepley . time   - The time
763b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
764b278463cSMatthew G. Knepley . field  - The field to constrain
7651c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7661c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
767b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
768b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
769b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
770b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
771b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
772b278463cSMatthew G. Knepley 
773b278463cSMatthew G. Knepley   Output Parameter:
774b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
775b278463cSMatthew G. Knepley 
776b278463cSMatthew G. Knepley   Level: developer
777b278463cSMatthew G. Knepley 
778ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
779b278463cSMatthew G. Knepley @*/
7801c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
781c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
782c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
783c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78497b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
785b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
786b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
787c60e475cSMatthew G. Knepley {
788c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
789c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
790c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79197b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
792c60e475cSMatthew G. Knepley   void            **ctxs;
793c60e475cSMatthew G. Knepley   PetscInt          numFields;
794c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
795c60e475cSMatthew G. Knepley 
796c60e475cSMatthew G. Knepley   PetscFunctionBegin;
797c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
798c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
799c60e475cSMatthew G. Knepley   funcs[field] = func;
800c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
8011c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
802c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
803c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
804c60e475cSMatthew G. Knepley }
805c60e475cSMatthew G. Knepley 
806b278463cSMatthew G. Knepley /*@C
807ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialBdField - Insert boundary values into a local vector using a function of the coodinates and boundary field data
808ece3a9fcSMatthew G. Knepley 
809ece3a9fcSMatthew G. Knepley   Collective on dm
810ece3a9fcSMatthew G. Knepley 
811ece3a9fcSMatthew G. Knepley   Input Parameters:
812ece3a9fcSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
813ece3a9fcSMatthew G. Knepley . time   - The time
814ece3a9fcSMatthew G. Knepley . locU   - A local vector with the input solution values
815ece3a9fcSMatthew G. Knepley . field  - The field to constrain
816ece3a9fcSMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
817ece3a9fcSMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
818ece3a9fcSMatthew G. Knepley . label  - The DMLabel defining constrained points
819ece3a9fcSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
820ece3a9fcSMatthew G. Knepley . ids    - An array of ids for constrained points
821ece3a9fcSMatthew G. Knepley . func   - A pointwise function giving boundary values, the calling sequence is given in DMProjectBdFieldLabelLocal()
822ece3a9fcSMatthew G. Knepley - ctx    - An optional user context for bcFunc
823ece3a9fcSMatthew G. Knepley 
824ece3a9fcSMatthew G. Knepley   Output Parameter:
825ece3a9fcSMatthew G. Knepley . locX   - A local vector to receive the boundary values
826ece3a9fcSMatthew G. Knepley 
827ece3a9fcSMatthew G. Knepley   Level: developer
828ece3a9fcSMatthew G. Knepley 
829ece3a9fcSMatthew G. Knepley .seealso: DMProjectBdFieldLabelLocal(), DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
830ece3a9fcSMatthew G. Knepley @*/
831ece3a9fcSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialBdField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
832ece3a9fcSMatthew G. Knepley                                                           void (*func)(PetscInt, PetscInt, PetscInt,
833ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
834ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
835ece3a9fcSMatthew G. Knepley                                                                        PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[],
836ece3a9fcSMatthew G. Knepley                                                                        PetscScalar[]),
837ece3a9fcSMatthew G. Knepley                                                           void *ctx, Vec locX)
838ece3a9fcSMatthew G. Knepley {
839ece3a9fcSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
840ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
841ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
842ece3a9fcSMatthew G. Knepley                  PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
843ece3a9fcSMatthew G. Knepley   void            **ctxs;
844ece3a9fcSMatthew G. Knepley   PetscInt          numFields;
845ece3a9fcSMatthew G. Knepley   PetscErrorCode    ierr;
846ece3a9fcSMatthew G. Knepley 
847ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
848ece3a9fcSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
849ece3a9fcSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
850ece3a9fcSMatthew G. Knepley   funcs[field] = func;
851ece3a9fcSMatthew G. Knepley   ctxs[field]  = ctx;
852ece3a9fcSMatthew G. Knepley   ierr = DMProjectBdFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
853ece3a9fcSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
854ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
855ece3a9fcSMatthew G. Knepley }
856ece3a9fcSMatthew G. Knepley 
857ece3a9fcSMatthew G. Knepley /*@C
858b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
859b278463cSMatthew G. Knepley 
860b278463cSMatthew G. Knepley   Input Parameters:
861b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
862b278463cSMatthew G. Knepley . time   - The time
863b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
864b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
865b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
866b278463cSMatthew G. Knepley . field  - The field to constrain
8671c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
8681c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
869b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
870b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
871b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
872b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
873b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
874b278463cSMatthew G. Knepley 
875b278463cSMatthew G. Knepley   Output Parameter:
876b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
877b278463cSMatthew G. Knepley 
878b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
879b278463cSMatthew G. Knepley 
880b278463cSMatthew G. Knepley   Level: developer
881b278463cSMatthew G. Knepley 
882b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
883b278463cSMatthew G. Knepley @*/
8841c531cf8SMatthew 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[],
885b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
886d7ddef95SMatthew G. Knepley {
88761f58d28SMatthew G. Knepley   PetscDS            prob;
888da97024aSMatthew G. Knepley   PetscSF            sf;
889d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
89020369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
891da97024aSMatthew G. Knepley   const PetscInt    *leaves;
892d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
893520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
894e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr,ierru = 0;
895d7ddef95SMatthew G. Knepley 
896d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
897da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
898da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
899da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
900d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
901d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
90261f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
903d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
904d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
90520369375SToby Isaac   if (cellGeometry) {
906d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
907d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
90820369375SToby Isaac   }
909d7ddef95SMatthew G. Knepley   if (Grad) {
910c0a6632aSMatthew G. Knepley     PetscFV fv;
911c0a6632aSMatthew G. Knepley 
912c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
913d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
914d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
915d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
91669291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
917d7ddef95SMatthew G. Knepley   }
918d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
919d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
920d7ddef95SMatthew G. Knepley     IS              faceIS;
921d7ddef95SMatthew G. Knepley     const PetscInt *faces;
922d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
923d7ddef95SMatthew G. Knepley 
924d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
925d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
926d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
927d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
928d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
929d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
930640bce14SSatish Balay       PetscFVFaceGeom        *fg;
931d7ddef95SMatthew G. Knepley 
932d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
933da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
934da97024aSMatthew G. Knepley       if (loc >= 0) continue;
935d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
936d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
937d7ddef95SMatthew G. Knepley       if (Grad) {
938640bce14SSatish Balay         PetscFVCellGeom       *cg;
939640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
940d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
941d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
942d7ddef95SMatthew G. Knepley         PetscInt               d;
943d7ddef95SMatthew G. Knepley 
944d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
945d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
946d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
947520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
948d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
949d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
950c3e24edfSBarry Smith         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
951d7ddef95SMatthew G. Knepley       } else {
952640bce14SSatish Balay         PetscScalar       *xI;
953d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
954d7ddef95SMatthew G. Knepley 
955d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
956520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
957e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
958e735a8a9SMatthew G. Knepley         if (ierru) {
959e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
960e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
961e735a8a9SMatthew G. Knepley           goto cleanup;
962e735a8a9SMatthew G. Knepley         }
963d7ddef95SMatthew G. Knepley       }
964d7ddef95SMatthew G. Knepley     }
965d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
966d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
967d7ddef95SMatthew G. Knepley   }
968e735a8a9SMatthew G. Knepley   cleanup:
969d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
970d7ddef95SMatthew G. Knepley   if (Grad) {
97169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
972d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
973d7ddef95SMatthew G. Knepley   }
974e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
975d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
976e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
977d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
978d7ddef95SMatthew G. Knepley }
979d7ddef95SMatthew G. Knepley 
9800c364540SMatthew G. Knepley static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
9810c364540SMatthew G. Knepley {
9820c364540SMatthew G. Knepley   PetscInt c;
9830c364540SMatthew G. Knepley   for (c = 0; c < Nc; ++c) u[c] = 0.0;
9840c364540SMatthew G. Knepley   return 0;
9850c364540SMatthew G. Knepley }
9860c364540SMatthew G. Knepley 
987f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
988d7ddef95SMatthew G. Knepley {
9890c364540SMatthew G. Knepley   PetscObject    isZero;
990e5e52638SMatthew G. Knepley   PetscDS        prob;
991d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
99255f2e967SMatthew G. Knepley   PetscErrorCode ierr;
99355f2e967SMatthew G. Knepley 
99455f2e967SMatthew G. Knepley   PetscFunctionBegin;
995e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
996e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
9970c364540SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
99855f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
99945480ffeSMatthew G. Knepley     PetscWeakForm           wf;
1000f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
100145480ffeSMatthew G. Knepley     const char             *name;
1002d7ddef95SMatthew G. Knepley     DMLabel                 label;
10031c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
10041c531cf8SMatthew G. Knepley     const PetscInt         *comps;
1005d7ddef95SMatthew G. Knepley     PetscObject             obj;
1006d7ddef95SMatthew G. Knepley     PetscClassId            id;
100745480ffeSMatthew G. Knepley     void                  (*bvfunc)(void);
1008d7ddef95SMatthew G. Knepley     PetscInt                numids;
1009d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
101055f2e967SMatthew G. Knepley     void                   *ctx;
101155f2e967SMatthew G. Knepley 
101245480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, &bvfunc, NULL, &ctx);CHKERRQ(ierr);
1013f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
101444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1015d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1016d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1017c60e475cSMatthew G. Knepley       switch (type) {
1018c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
1019c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
102045480ffeSMatthew G. Knepley         {
102145480ffeSMatthew G. Knepley           PetscSimplePointFunc func = (PetscSimplePointFunc) bvfunc;
102245480ffeSMatthew G. Knepley 
102345480ffeSMatthew G. Knepley           if (isZero) func = zero;
1024092e5057SToby Isaac           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
102545480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
1026092e5057SToby Isaac           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
102745480ffeSMatthew G. Knepley         }
1028c60e475cSMatthew G. Knepley         break;
1029c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
103045480ffeSMatthew G. Knepley         {
103145480ffeSMatthew G. Knepley           PetscPointFunc func = (PetscPointFunc) bvfunc;
103245480ffeSMatthew G. Knepley 
1033c60e475cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
103445480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
1035c60e475cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
103645480ffeSMatthew G. Knepley         }
1037c60e475cSMatthew G. Knepley         break;
1038c60e475cSMatthew G. Knepley       default: break;
1039c60e475cSMatthew G. Knepley       }
1040d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
104145480ffeSMatthew G. Knepley       {
104245480ffeSMatthew G. Knepley         PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*) = (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) bvfunc;
104345480ffeSMatthew G. Knepley 
104443ea7facSMatthew G. Knepley         if (!faceGeomFVM) continue;
104545480ffeSMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
104645480ffeSMatthew G. Knepley       }
1047ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
104855f2e967SMatthew G. Knepley   }
104955f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
105055f2e967SMatthew G. Knepley }
105155f2e967SMatthew G. Knepley 
105256cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
105356cf3b9cSMatthew G. Knepley {
105456cf3b9cSMatthew G. Knepley   PetscObject    isZero;
105556cf3b9cSMatthew G. Knepley   PetscDS        prob;
105656cf3b9cSMatthew G. Knepley   PetscInt       numBd, b;
105756cf3b9cSMatthew G. Knepley   PetscErrorCode ierr;
105856cf3b9cSMatthew G. Knepley 
105956cf3b9cSMatthew G. Knepley   PetscFunctionBegin;
106056cf3b9cSMatthew G. Knepley   if (!locX) PetscFunctionReturn(0);
106156cf3b9cSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
106256cf3b9cSMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
106356cf3b9cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
106456cf3b9cSMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
106545480ffeSMatthew G. Knepley     PetscWeakForm           wf;
106656cf3b9cSMatthew G. Knepley     DMBoundaryConditionType type;
106745480ffeSMatthew G. Knepley     const char             *name;
106856cf3b9cSMatthew G. Knepley     DMLabel                 label;
106956cf3b9cSMatthew G. Knepley     PetscInt                field, Nc;
107056cf3b9cSMatthew G. Knepley     const PetscInt         *comps;
107156cf3b9cSMatthew G. Knepley     PetscObject             obj;
107256cf3b9cSMatthew G. Knepley     PetscClassId            id;
107356cf3b9cSMatthew G. Knepley     PetscInt                numids;
107456cf3b9cSMatthew G. Knepley     const PetscInt         *ids;
107545480ffeSMatthew G. Knepley     void                  (*bvfunc)(void);
107656cf3b9cSMatthew G. Knepley     void                   *ctx;
107756cf3b9cSMatthew G. Knepley 
107845480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, NULL, &bvfunc, &ctx);CHKERRQ(ierr);
107956cf3b9cSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
108056cf3b9cSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
108156cf3b9cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
108256cf3b9cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
108356cf3b9cSMatthew G. Knepley       switch (type) {
108456cf3b9cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
108556cf3b9cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
108645480ffeSMatthew G. Knepley         {
108745480ffeSMatthew G. Knepley           PetscSimplePointFunc func_t = (PetscSimplePointFunc) bvfunc;
108845480ffeSMatthew G. Knepley 
108945480ffeSMatthew G. Knepley           if (isZero) func_t = zero;
109056cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
109145480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, func_t, ctx, locX);CHKERRQ(ierr);
109256cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
109345480ffeSMatthew G. Knepley         }
109456cf3b9cSMatthew G. Knepley         break;
109556cf3b9cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
109645480ffeSMatthew G. Knepley         {
109745480ffeSMatthew G. Knepley           PetscPointFunc func_t = (PetscPointFunc) bvfunc;
109845480ffeSMatthew G. Knepley 
109956cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
110045480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, func_t, ctx, locX);CHKERRQ(ierr);
110156cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
110245480ffeSMatthew G. Knepley         }
110356cf3b9cSMatthew G. Knepley         break;
110456cf3b9cSMatthew G. Knepley       default: break;
110556cf3b9cSMatthew G. Knepley       }
110656cf3b9cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
110756cf3b9cSMatthew G. Knepley   }
110856cf3b9cSMatthew G. Knepley   PetscFunctionReturn(0);
110956cf3b9cSMatthew G. Knepley }
111056cf3b9cSMatthew G. Knepley 
1111f1d73a7aSMatthew G. Knepley /*@
1112f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
1113f1d73a7aSMatthew G. Knepley 
1114f1d73a7aSMatthew G. Knepley   Input Parameters:
1115f1d73a7aSMatthew G. Knepley + dm - The DM
1116f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
1117f1d73a7aSMatthew G. Knepley . time - The time
1118f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
1119f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
1120f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
1121f1d73a7aSMatthew G. Knepley 
1122f1d73a7aSMatthew G. Knepley   Output Parameters:
1123f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
1124f1d73a7aSMatthew G. Knepley 
1125f1d73a7aSMatthew G. Knepley   Level: developer
1126f1d73a7aSMatthew G. Knepley 
1127f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
1128f1d73a7aSMatthew G. Knepley @*/
1129f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
1130f1d73a7aSMatthew G. Knepley {
1131f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
1132f1d73a7aSMatthew G. Knepley 
1133f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
1134f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1135064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(locX, VEC_CLASSID, 3);
1136064a246eSJacob Faibussowitsch   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 5);}
1137064a246eSJacob Faibussowitsch   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 6);}
1138064a246eSJacob Faibussowitsch   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 7);}
1139f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
1140f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
1141f1d73a7aSMatthew G. Knepley }
1142f1d73a7aSMatthew G. Knepley 
114356cf3b9cSMatthew G. Knepley /*@
1144a5b23f4aSJose E. Roman   DMPlexInsertTimeDerivativeBoundaryValues - Puts coefficients which represent boundary values of the time derivative into the local solution vector
114556cf3b9cSMatthew G. Knepley 
114656cf3b9cSMatthew G. Knepley   Input Parameters:
114756cf3b9cSMatthew G. Knepley + dm - The DM
114856cf3b9cSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
114956cf3b9cSMatthew G. Knepley . time - The time
115056cf3b9cSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
115156cf3b9cSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
115256cf3b9cSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
115356cf3b9cSMatthew G. Knepley 
115456cf3b9cSMatthew G. Knepley   Output Parameters:
115556cf3b9cSMatthew G. Knepley . locX_t - Solution updated with boundary values
115656cf3b9cSMatthew G. Knepley 
115756cf3b9cSMatthew G. Knepley   Level: developer
115856cf3b9cSMatthew G. Knepley 
115956cf3b9cSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
116056cf3b9cSMatthew G. Knepley @*/
116156cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues(DM dm, PetscBool insertEssential, Vec locX_t, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
116256cf3b9cSMatthew G. Knepley {
116356cf3b9cSMatthew G. Knepley   PetscErrorCode ierr;
116456cf3b9cSMatthew G. Knepley 
116556cf3b9cSMatthew G. Knepley   PetscFunctionBegin;
116656cf3b9cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1167064a246eSJacob Faibussowitsch   if (locX_t)      {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 3);}
1168064a246eSJacob Faibussowitsch   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 5);}
1169064a246eSJacob Faibussowitsch   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 6);}
1170064a246eSJacob Faibussowitsch   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 7);}
117156cf3b9cSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX_t,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
117256cf3b9cSMatthew G. Knepley   PetscFunctionReturn(0);
117356cf3b9cSMatthew G. Knepley }
117456cf3b9cSMatthew G. Knepley 
11750709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
1176cb1e1211SMatthew G Knepley {
1177574a98acSMatthew G. Knepley   Vec              localX;
1178574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
1179574a98acSMatthew G. Knepley 
1180574a98acSMatthew G. Knepley   PetscFunctionBegin;
1181574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
11825d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
1183574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1184574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1185574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
1186574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1187574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
1188574a98acSMatthew G. Knepley }
1189574a98acSMatthew G. Knepley 
1190574a98acSMatthew G. Knepley /*@C
1191ca3d3a14SMatthew G. Knepley   DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
1192574a98acSMatthew G. Knepley 
1193d083f849SBarry Smith   Collective on dm
1194c0f8e1fdSMatthew G. Knepley 
1195574a98acSMatthew G. Knepley   Input Parameters:
1196574a98acSMatthew G. Knepley + dm     - The DM
1197574a98acSMatthew G. Knepley . time   - The time
1198574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
1199574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
1200574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
1201574a98acSMatthew G. Knepley 
1202574a98acSMatthew G. Knepley   Output Parameter:
1203574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
1204574a98acSMatthew G. Knepley 
1205574a98acSMatthew G. Knepley   Level: developer
1206574a98acSMatthew G. Knepley 
1207574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
1208574a98acSMatthew G. Knepley @*/
1209574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
1210574a98acSMatthew G. Knepley {
12110f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1212ca3d3a14SMatthew G. Knepley   DM               tdm;
1213ca3d3a14SMatthew G. Knepley   Vec              tv;
1214cb1e1211SMatthew G Knepley   PetscSection     section;
1215c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
12164bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
121715496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
12184bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
1219cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
12207318780aSToby Isaac   const PetscReal *quadWeights;
1221412e9a14SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, c, field, fieldOffset;
1222ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1223cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1224cb1e1211SMatthew G Knepley 
1225cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1226c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12277318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
12282a4e142eSMatthew G. Knepley   fegeom.dimEmbed = coordDim;
122992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1230cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1231ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1232ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1233ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1234cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
123515496722SMatthew G. Knepley     PetscObject  obj;
123615496722SMatthew G. Knepley     PetscClassId id;
1237c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
1238c5bbbd5bSMatthew G. Knepley 
123944a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
124015496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
124115496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
124215496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
124315496722SMatthew G. Knepley 
12440f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12450f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
124615496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
124715496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
124815496722SMatthew G. Knepley 
124915496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
125015496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1251ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1252c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
1253cb1e1211SMatthew G Knepley   }
12549c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1255beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
12562a4e142eSMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
1257aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1258412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1259cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
1260a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
1261cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
12629c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1263cb1e1211SMatthew G Knepley 
12642a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1265cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1266cb1e1211SMatthew G Knepley 
126715496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
126815496722SMatthew G. Knepley       PetscObject  obj;
126915496722SMatthew G. Knepley       PetscClassId id;
1270c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
127115496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1272cb1e1211SMatthew G Knepley 
127344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
127415496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
127515496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
127615496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1277ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1278cb1e1211SMatthew G Knepley       if (debug) {
1279cb1e1211SMatthew G Knepley         char title[1024];
1280ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
12814c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1282cb1e1211SMatthew G Knepley       }
12837318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
12842a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
12852a4e142eSMatthew G. Knepley 
12862a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
12872a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
12882a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
12892a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1290ff1e0c32SBarry Smith         if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", (double)fegeom.detJ[q], c, q);
1291d3a7d86cSMatthew G. Knepley         if (transform) {
1292d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1293d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1294d3a7d86cSMatthew G. Knepley         } else {
1295d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1296d3a7d86cSMatthew G. Knepley         }
1297ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1298e735a8a9SMatthew G. Knepley         if (ierr) {
1299e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1300e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1301e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
13022a4e142eSMatthew G. Knepley           ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1303e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1304e735a8a9SMatthew G. Knepley         }
1305ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
13062a4e142eSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
130715496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1308ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
130915496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1310beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
131180dbbc5dSMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %D field %D,%D point %g %g %g diff %g\n", c, field, fc, (double)(coordDim > 0 ? coords[coordDim*q] : 0.), (double)(coordDim > 1 ? coords[coordDim*q+1] : 0.),(double)(coordDim > 2 ? coords[coordDim*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
13124bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1313cb1e1211SMatthew G Knepley         }
1314cb1e1211SMatthew G Knepley       }
13159c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
1316beaa55a6SMatthew G. Knepley       qc += Nc;
1317cb1e1211SMatthew G Knepley     }
1318cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1319ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
1320cb1e1211SMatthew G Knepley     localDiff += elemDiff;
1321cb1e1211SMatthew G Knepley   }
13222a4e142eSMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1323820f2d46SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1324cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
1325cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1326cb1e1211SMatthew G Knepley }
1327cb1e1211SMatthew G Knepley 
1328b698f381SToby 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)
1329cb1e1211SMatthew G Knepley {
13300f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1331ca3d3a14SMatthew G. Knepley   DM               tdm;
1332cb1e1211SMatthew G Knepley   PetscSection     section;
133340e14135SMatthew G. Knepley   PetscQuadrature  quad;
1334ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
13359c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
13362a4e142eSMatthew G. Knepley   const PetscReal *quadWeights;
13374bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
13384bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
133940e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
1340485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset;
1341ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1342cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1343cb1e1211SMatthew G Knepley 
1344cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1345c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13467318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
13474bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
134892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
134940e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
135040e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
135140e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
135240e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1353ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1354ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1355ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1356652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
13570f2d7e86SMatthew G. Knepley     PetscFE  fe;
135840e14135SMatthew G. Knepley     PetscInt Nc;
1359652b88e8SMatthew G. Knepley 
136044a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
13610f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
13620f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
136340e14135SMatthew G. Knepley     numComponents += Nc;
1364652b88e8SMatthew G. Knepley   }
13652a4e142eSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1366beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
13674d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
13684bee2e38SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ,numComponents*coordDim,&interpolant,Nq,&fegeom.detJ);CHKERRQ(ierr);
1369412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
137040e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
137140e14135SMatthew G. Knepley     PetscScalar *x = NULL;
137240e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
13739c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1374652b88e8SMatthew G. Knepley 
13754bee2e38SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
137640e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
137740e14135SMatthew G. Knepley 
13789c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
13790f2d7e86SMatthew G. Knepley       PetscFE          fe;
138051259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
13819c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
138240e14135SMatthew G. Knepley 
138344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
13840f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
13859c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
138640e14135SMatthew G. Knepley       if (debug) {
138740e14135SMatthew G. Knepley         char title[1024];
1388ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
13899c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1390652b88e8SMatthew G. Knepley       }
13919c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
13922a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
13932a4e142eSMatthew G. Knepley 
13942a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
13952a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
13962a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
13972a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1398ff1e0c32SBarry Smith         if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], c, q);
1399d3a7d86cSMatthew G. Knepley         if (transform) {
1400d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1401d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1402d3a7d86cSMatthew G. Knepley         } else {
1403d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1404d3a7d86cSMatthew G. Knepley         }
14054bee2e38SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, n, Nc, funcVal, ctx);
1406e735a8a9SMatthew G. Knepley         if (ierr) {
1407e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1408e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1409e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
14104bee2e38SMatthew G. Knepley           ierr2 = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr2);
1411e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1412e735a8a9SMatthew G. Knepley         }
1413ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
1414f9244615SMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, 1, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);
14154bee2e38SMatthew G. Knepley         /* Overwrite with the dot product if the normal is given */
14164bee2e38SMatthew G. Knepley         if (n) {
14174bee2e38SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
14184bee2e38SMatthew G. Knepley             PetscScalar sum = 0.0;
14194bee2e38SMatthew G. Knepley             PetscInt    d;
14204bee2e38SMatthew G. Knepley             for (d = 0; d < dim; ++d) sum += interpolant[fc*dim+d]*n[d];
14214bee2e38SMatthew G. Knepley             interpolant[fc] = sum;
14224bee2e38SMatthew G. Knepley           }
14234bee2e38SMatthew G. Knepley         }
14249c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1425beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1426ff1e0c32SBarry Smith           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %D fieldDer %D,%D diff %g\n", c, field, fc, (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
14274bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
142840e14135SMatthew G. Knepley         }
142940e14135SMatthew G. Knepley       }
14309c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
14319c3cf19fSMatthew G. Knepley       qc          += Nc;
143240e14135SMatthew G. Knepley     }
143340e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1434ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
143540e14135SMatthew G. Knepley     localDiff += elemDiff;
143640e14135SMatthew G. Knepley   }
14374bee2e38SMatthew G. Knepley   ierr  = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr);
143840e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1439820f2d46SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
144040e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1441cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1442cb1e1211SMatthew G Knepley }
1443cb1e1211SMatthew G Knepley 
1444c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
144573d901b8SMatthew G. Knepley {
14460f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1447ca3d3a14SMatthew G. Knepley   DM               tdm;
1448083401c6SMatthew G. Knepley   DMLabel          depthLabel;
144973d901b8SMatthew G. Knepley   PetscSection     section;
1450ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
145173d901b8SMatthew G. Knepley   PetscReal       *localDiff;
1452083401c6SMatthew G. Knepley   PetscInt         dim, depth, dE, Nf, f, Nds, s;
1453ca3d3a14SMatthew G. Knepley   PetscBool        transform;
145473d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
145573d901b8SMatthew G. Knepley 
145673d901b8SMatthew G. Knepley   PetscFunctionBegin;
1457c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1458083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
145992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
146073d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1461083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1462083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1463083401c6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1464083401c6SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1465083401c6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1466083401c6SMatthew G. Knepley   ierr = DMLabelGetNumValues(depthLabel, &depth);CHKERRQ(ierr);
1467083401c6SMatthew G. Knepley 
1468ca3d3a14SMatthew G. Knepley   ierr = VecSet(localX, 0.0);CHKERRQ(ierr);
146973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
147073d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1471ca3d3a14SMatthew G. Knepley   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1472083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
1473083401c6SMatthew G. Knepley   ierr = PetscCalloc1(Nf, &localDiff);CHKERRQ(ierr);
1474083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
1475083401c6SMatthew G. Knepley     PetscDS          ds;
1476083401c6SMatthew G. Knepley     DMLabel          label;
1477083401c6SMatthew G. Knepley     IS               fieldIS, pointIS;
1478083401c6SMatthew G. Knepley     const PetscInt  *fields, *points = NULL;
1479083401c6SMatthew G. Knepley     PetscQuadrature  quad;
1480083401c6SMatthew G. Knepley     const PetscReal *quadPoints, *quadWeights;
1481083401c6SMatthew G. Knepley     PetscFEGeom      fegeom;
1482083401c6SMatthew G. Knepley     PetscReal       *coords, *gcoords;
1483083401c6SMatthew G. Knepley     PetscScalar     *funcVal, *interpolant;
1484*5fedec97SMatthew G. Knepley     PetscBool        isCohesive;
1485083401c6SMatthew G. Knepley     PetscInt         qNc, Nq, totNc, cStart = 0, cEnd, c, dsNf;
148673d901b8SMatthew G. Knepley 
1487083401c6SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
1488083401c6SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
1489*5fedec97SMatthew G. Knepley     ierr = PetscDSIsCohesive(ds, &isCohesive);CHKERRQ(ierr);
1490083401c6SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
1491083401c6SMatthew G. Knepley     ierr = PetscDSGetTotalComponents(ds, &totNc);CHKERRQ(ierr);
1492083401c6SMatthew G. Knepley     ierr = PetscDSGetQuadrature(ds, &quad);CHKERRQ(ierr);
14939c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1494083401c6SMatthew G. Knepley     if ((qNc != 1) && (qNc != totNc)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, totNc);
1495083401c6SMatthew G. Knepley     ierr = PetscCalloc6(totNc, &funcVal, totNc, &interpolant, dE*(Nq+1), &coords,Nq, &fegeom.detJ, dE*dE*Nq, &fegeom.J, dE*dE*Nq, &fegeom.invJ);CHKERRQ(ierr);
1496083401c6SMatthew G. Knepley     if (!label) {
1497412e9a14SMatthew G. Knepley       ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1498083401c6SMatthew G. Knepley     } else {
1499083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
1500083401c6SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &cEnd);CHKERRQ(ierr);
1501083401c6SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1502083401c6SMatthew G. Knepley     }
150373d901b8SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
1504083401c6SMatthew G. Knepley       const PetscInt cell  = points ? points[c] : c;
150573d901b8SMatthew G. Knepley       PetscScalar    *x    = NULL;
1506*5fedec97SMatthew G. Knepley       const PetscInt *cone;
1507*5fedec97SMatthew G. Knepley       PetscInt        qc   = 0, fOff = 0, dep;
150873d901b8SMatthew G. Knepley 
1509083401c6SMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, cell, &dep);CHKERRQ(ierr);
1510083401c6SMatthew G. Knepley       if (dep != depth-1) continue;
1511*5fedec97SMatthew G. Knepley       if (isCohesive) {
151296959cd1SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
151396959cd1SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cone[0], quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
151496959cd1SMatthew G. Knepley       } else {
1515083401c6SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
151696959cd1SMatthew G. Knepley       }
1517083401c6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
1518*5fedec97SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
151915496722SMatthew G. Knepley         PetscObject  obj;
152015496722SMatthew G. Knepley         PetscClassId id;
1521083401c6SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[fields[f]] : NULL;
152215496722SMatthew G. Knepley         PetscInt     Nb, Nc, q, fc;
152315496722SMatthew G. Knepley         PetscReal    elemDiff = 0.0;
1524*5fedec97SMatthew G. Knepley         PetscBool    cohesive;
152515496722SMatthew G. Knepley 
1526*5fedec97SMatthew G. Knepley         ierr = PetscDSGetCohesive(ds, f, &cohesive);CHKERRQ(ierr);
1527*5fedec97SMatthew G. Knepley         if (isCohesive && !cohesive) continue;
1528083401c6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
152915496722SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
153015496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
153115496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1532083401c6SMatthew G. Knepley         else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
153373d901b8SMatthew G. Knepley         if (debug) {
153473d901b8SMatthew G. Knepley           char title[1024];
1535083401c6SMatthew G. Knepley           ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", fields[f]);CHKERRQ(ierr);
1536083401c6SMatthew G. Knepley           ierr = DMPrintCellVector(cell, title, Nb, &x[fOff]);CHKERRQ(ierr);
153773d901b8SMatthew G. Knepley         }
15387318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
15392a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
15402a4e142eSMatthew G. Knepley 
15412a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
1542083401c6SMatthew G. Knepley           qgeom.J        = &fegeom.J[q*dE*dE];
1543083401c6SMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*dE*dE];
15442a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
1545083401c6SMatthew G. Knepley           if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for cell %D, quadrature point %D", (double)fegeom.detJ[q], cell, q);
1546d3a7d86cSMatthew G. Knepley           if (transform) {
1547083401c6SMatthew G. Knepley             gcoords = &coords[dE*Nq];
1548083401c6SMatthew G. Knepley             ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[dE*q], PETSC_TRUE, dE, &coords[dE*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1549d3a7d86cSMatthew G. Knepley           } else {
1550083401c6SMatthew G. Knepley             gcoords = &coords[dE*q];
1551d3a7d86cSMatthew G. Knepley           }
1552083401c6SMatthew G. Knepley           ierr = (*funcs[fields[f]])(dE, time, gcoords, Nc, funcVal, ctx);
1553e735a8a9SMatthew G. Knepley           if (ierr) {
1554e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1555083401c6SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr2);
1556e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1557083401c6SMatthew G. Knepley             ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1558e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1559e735a8a9SMatthew G. Knepley           }
1560083401c6SMatthew G. Knepley           if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[dE*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
156196959cd1SMatthew G. Knepley           /* Call once for each face, except for lagrange field */
1562083401c6SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fOff], &qgeom, q, interpolant);CHKERRQ(ierr);}
1563083401c6SMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fOff], q, interpolant);CHKERRQ(ierr);}
1564083401c6SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
156515496722SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1566beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1567083401c6SMatthew G. Knepley             if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    cell %D field %D,%D point %g %g %g diff %g\n", cell, fields[f], fc, (double)(dE > 0 ? coords[dE*q] : 0.), (double)(dE > 1 ? coords[dE*q+1] : 0.),(double)(dE > 2 ? coords[dE*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
15684bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
156973d901b8SMatthew G. Knepley           }
157073d901b8SMatthew G. Knepley         }
1571083401c6SMatthew G. Knepley         fOff += Nb;
15729c3cf19fSMatthew G. Knepley         qc   += Nc;
1573083401c6SMatthew G. Knepley         localDiff[fields[f]] += elemDiff;
1574083401c6SMatthew G. Knepley         if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  cell %D field %D cum diff %g\n", cell, fields[f], (double)localDiff[fields[f]]);CHKERRQ(ierr);}
157573d901b8SMatthew G. Knepley       }
1576083401c6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
1577083401c6SMatthew G. Knepley     }
1578083401c6SMatthew G. Knepley     if (label) {
1579083401c6SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1580083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1581083401c6SMatthew G. Knepley     }
1582083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
1583083401c6SMatthew G. Knepley     ierr = PetscFree6(funcVal, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ);CHKERRQ(ierr);
158473d901b8SMatthew G. Knepley   }
158573d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1586820f2d46SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1587083401c6SMatthew G. Knepley   ierr = PetscFree(localDiff);CHKERRQ(ierr);
1588083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) diff[f] = PetscSqrtReal(diff[f]);
158973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
159073d901b8SMatthew G. Knepley }
159173d901b8SMatthew G. Knepley 
1592e729f68cSMatthew G. Knepley /*@C
1593e729f68cSMatthew 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.
1594e729f68cSMatthew G. Knepley 
1595d083f849SBarry Smith   Collective on dm
1596c0f8e1fdSMatthew G. Knepley 
1597e729f68cSMatthew G. Knepley   Input Parameters:
1598e729f68cSMatthew G. Knepley + dm    - The DM
15990163fd50SMatthew G. Knepley . time  - The time
1600ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1601e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1602e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1603e729f68cSMatthew G. Knepley 
1604e729f68cSMatthew G. Knepley   Output Parameter:
1605e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1606e729f68cSMatthew G. Knepley 
1607e729f68cSMatthew G. Knepley   Level: developer
1608e729f68cSMatthew G. Knepley 
1609b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1610e729f68cSMatthew G. Knepley @*/
16110163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1612e729f68cSMatthew G. Knepley {
1613e729f68cSMatthew G. Knepley   PetscSection     section;
1614e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1615e729f68cSMatthew G. Knepley   Vec              localX;
16164bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
1617e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
16184bee2e38SMatthew G. Knepley   PetscReal       *coords;
1619e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1620485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, c, field, fieldOffset;
1621e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1622e729f68cSMatthew G. Knepley 
1623e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1624e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1625e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16267318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
162792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1628e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1629e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1630bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1631e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1632e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1633e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1634e729f68cSMatthew G. Knepley     PetscObject  obj;
1635e729f68cSMatthew G. Knepley     PetscClassId id;
1636e729f68cSMatthew G. Knepley     PetscInt     Nc;
1637e729f68cSMatthew G. Knepley 
163844a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1639e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1640e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1641e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1642e729f68cSMatthew G. Knepley 
1643e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1644e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1645e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1646e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1647e729f68cSMatthew G. Knepley 
1648e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1649e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1650ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1651e729f68cSMatthew G. Knepley     numComponents += Nc;
1652e729f68cSMatthew G. Knepley   }
16539c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1654beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
16552a4e142eSMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
1656412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1657e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1658e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
16596f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
16609c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1661e729f68cSMatthew G. Knepley 
16622a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1663e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1664e729f68cSMatthew G. Knepley 
1665e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1666e729f68cSMatthew G. Knepley       PetscObject  obj;
1667e729f68cSMatthew G. Knepley       PetscClassId id;
1668e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1669e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1670e729f68cSMatthew G. Knepley 
167144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1672e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1673e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1674e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1675ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
167623f34ed2SToby Isaac       if (funcs[field]) {
16777318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
16782a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
16792a4e142eSMatthew G. Knepley 
16802a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
16812a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
16822a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
16832a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
16844bee2e38SMatthew G. Knepley           if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], c, q);
16852f613bf5SBarry Smith           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);CHKERRQ(ierr);
1686c3e24edfSBarry Smith #if defined(needs_fix_with_return_code_argument)
1687e735a8a9SMatthew G. Knepley           if (ierr) {
1688c3e24edfSBarry Smith             PetscErrorCode ierr;
1689c3e24edfSBarry Smith             ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1690c3e24edfSBarry Smith             ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1691c3e24edfSBarry Smith             ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1692e735a8a9SMatthew G. Knepley           }
1693c3e24edfSBarry Smith #endif
16942a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1695e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1696ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1697e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1698beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
16994bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1700e729f68cSMatthew G. Knepley           }
1701e729f68cSMatthew G. Knepley         }
170223f34ed2SToby Isaac       }
1703beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
17049c3cf19fSMatthew G. Knepley       qc          += Nc;
1705e729f68cSMatthew G. Knepley     }
1706e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
170723f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1708e729f68cSMatthew G. Knepley   }
17092a4e142eSMatthew G. Knepley   ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1710e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1711e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1712e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1713e729f68cSMatthew G. Knepley }
1714e729f68cSMatthew G. Knepley 
17151555c271SMatthew G. Knepley /*@C
17161555c271SMatthew 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.
17171555c271SMatthew G. Knepley 
1718d083f849SBarry Smith   Collective on dm
1719c0f8e1fdSMatthew G. Knepley 
17201555c271SMatthew G. Knepley   Input Parameters:
17211555c271SMatthew G. Knepley + dm - The DM
17221555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
17231555c271SMatthew G. Knepley 
17241555c271SMatthew G. Knepley   Output Parameter:
17251555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
17261555c271SMatthew G. Knepley 
172795452b02SPatrick Sanan   Notes:
172895452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
17291555c271SMatthew 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
17301555c271SMatthew G. Knepley 
17311555c271SMatthew G. Knepley   Level: developer
17321555c271SMatthew G. Knepley 
17331555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
17341555c271SMatthew G. Knepley @*/
17351555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
17361555c271SMatthew G. Knepley {
1737db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1738db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
17391555c271SMatthew G. Knepley   DM               dmC;
17401555c271SMatthew G. Knepley   PetscQuadrature  quad;
17411555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
17424bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
17434bee2e38SMatthew G. Knepley   PetscReal       *coords;
17441555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1745485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, vStart, vEnd, v, field, fieldOffset;
17461555c271SMatthew G. Knepley   PetscErrorCode   ierr;
17471555c271SMatthew G. Knepley 
17481555c271SMatthew G. Knepley   PetscFunctionBegin;
17491555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
17501555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
17511555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
17521555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
17534bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
17547a8154cbSJoe Wallwork   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
17557a8154cbSJoe Wallwork   if (numFields == 0) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fields is zero!");
17561555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
17571555c271SMatthew G. Knepley     PetscObject  obj;
17581555c271SMatthew G. Knepley     PetscClassId id;
17591555c271SMatthew G. Knepley     PetscInt     Nc;
17601555c271SMatthew G. Knepley 
176144a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
17621555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
17631555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
17641555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
17651555c271SMatthew G. Knepley 
17661555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
17671555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
17681555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
17691555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
17701555c271SMatthew G. Knepley 
17711555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
17721555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1773ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
17741555c271SMatthew G. Knepley     numComponents += Nc;
17751555c271SMatthew G. Knepley   }
17761555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
17771555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
17784bee2e38SMatthew G. Knepley   ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
17791555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1780412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
17811555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
17821555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
17831555c271SMatthew G. Knepley     PetscInt   *star   = NULL;
17841555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
17851555c271SMatthew G. Knepley 
1786580bdb30SBarry Smith     ierr = PetscArrayzero(gradsum, coordDim*numComponents);CHKERRQ(ierr);
17871555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
17881555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
17891555c271SMatthew G. Knepley       const PetscInt cell = star[st];
17901555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
17911555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
17921555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
17931555c271SMatthew G. Knepley 
17941555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
17954bee2e38SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
17961555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
17971555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
17981555c271SMatthew G. Knepley         PetscObject  obj;
17991555c271SMatthew G. Knepley         PetscClassId id;
18001555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
18011555c271SMatthew G. Knepley 
1802580bdb30SBarry Smith         ierr = PetscArrayzero(grad, coordDim*numComponents);CHKERRQ(ierr);
180344a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
18041555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
18051555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
18061555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1807ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
18081555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
18092a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
18102a4e142eSMatthew G. Knepley 
18112a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
18122a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
18132a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
18142a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
18154bee2e38SMatthew G. Knepley           if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], cell, q);
18161555c271SMatthew G. Knepley           if (ierr) {
18171555c271SMatthew G. Knepley             PetscErrorCode ierr2;
18181555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
18191555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
18204bee2e38SMatthew G. Knepley             ierr2 = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
18211555c271SMatthew G. Knepley             CHKERRQ(ierr);
18221555c271SMatthew G. Knepley           }
1823f9244615SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, 1, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1824ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
18251555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
182653d2db2dSJoe Wallwork             const PetscReal wt = quadWeights[q*qNc+qc];
18271555c271SMatthew G. Knepley 
18284bee2e38SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*fegeom.detJ[q];
18291555c271SMatthew G. Knepley           }
18304bee2e38SMatthew G. Knepley           vol += quadWeights[q*qNc]*fegeom.detJ[q];
18311555c271SMatthew G. Knepley         }
18321555c271SMatthew G. Knepley         fieldOffset += Nb;
18331555c271SMatthew G. Knepley         qc          += Nc;
18341555c271SMatthew G. Knepley       }
18351555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1836f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1837f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1838f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1839f8527842SMatthew G. Knepley         }
1840f8527842SMatthew G. Knepley       }
1841f8527842SMatthew G. Knepley       volsum += vol;
1842db1066baSMatthew G. Knepley       if (debug) {
1843bb48be70SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT " Cell %" PetscInt_FMT " gradient: [", v, cell);CHKERRQ(ierr);
18441555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
18451555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
18461555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
18476d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
18481555c271SMatthew G. Knepley           }
18491555c271SMatthew G. Knepley         }
18501555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1851db1066baSMatthew G. Knepley       }
18521555c271SMatthew G. Knepley     }
18531555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
18541555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
18551555c271SMatthew G. Knepley     }
18561555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
18571555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
18581555c271SMatthew G. Knepley   }
18594bee2e38SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
18601555c271SMatthew G. Knepley   PetscFunctionReturn(0);
18611555c271SMatthew G. Knepley }
18621555c271SMatthew G. Knepley 
1863338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
186473d901b8SMatthew G. Knepley {
1865338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
186661aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
186773d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1868338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1869c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1870c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1871338f77d5SMatthew G. Knepley   /* DS */
1872338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1873338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1874338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1875338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1876338f77d5SMatthew G. Knepley   /* Geometry */
1877c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1878338f77d5SMatthew G. Knepley   DM                 dmGrad;
1879c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1880338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1881b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1882338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1883b7260050SToby Isaac   PetscInt           maxDegree;
1884c330f8ffSToby Isaac   DMField            coordField;
1885c330f8ffSToby Isaac   IS                 cellIS;
188673d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
188773d901b8SMatthew G. Knepley 
188873d901b8SMatthew G. Knepley   PetscFunctionBegin;
1889338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1890c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
189192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
18927a8154cbSJoe Wallwork   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1893338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1894b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1895b5a3613cSMatthew G. Knepley     PetscObject  obj;
1896b5a3613cSMatthew G. Knepley     PetscClassId id;
1897b5a3613cSMatthew G. Knepley 
1898b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1899b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1900338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1901338f77d5SMatthew G. Knepley   }
1902338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1903338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1904338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1905338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1906338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1907338f77d5SMatthew G. Knepley   /* Read DS information */
1908338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1909338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1910338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1911c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1912338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1913338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
19149a2a23afSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, &locA);CHKERRQ(ierr);
19159a2a23afSMatthew G. Knepley   if (locA) {
19169a2a23afSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
1917338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1918338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
191992fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
1920338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1921338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1922338f77d5SMatthew G. Knepley   }
1923338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1924338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1925338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1926338f77d5SMatthew G. Knepley   /* Read out geometry */
1927c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1928b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1929b7260050SToby Isaac   if (maxDegree <= 1) {
1930c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1931c330f8ffSToby Isaac     if (affineQuad) {
1932c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1933338f77d5SMatthew G. Knepley     }
1934b5a3613cSMatthew G. Knepley   }
1935b5a3613cSMatthew G. Knepley   if (useFVM) {
1936338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1937b5a3613cSMatthew G. Knepley     Vec       grad;
1938b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1939b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1940b5a3613cSMatthew G. Knepley 
1941338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1942338f77d5SMatthew G. Knepley       PetscObject  obj;
1943338f77d5SMatthew G. Knepley       PetscClassId id;
1944338f77d5SMatthew G. Knepley 
1945338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1946338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1947338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1948338f77d5SMatthew G. Knepley     }
1949338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1950338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1951b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1952338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1953338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1954b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1955b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1956b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1957b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1958338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1959b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1960b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1961b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1962b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1963b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1964b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1965338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1966b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1967b5a3613cSMatthew G. Knepley   }
1968338f77d5SMatthew G. Knepley   /* Read out data from inputs */
196973d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
197073d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
197173d901b8SMatthew G. Knepley     PetscInt     i;
197273d901b8SMatthew G. Knepley 
1973338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
19740f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1975338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
197673d901b8SMatthew G. Knepley     if (dmAux) {
1977338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
19780f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1979338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
198073d901b8SMatthew G. Knepley     }
198173d901b8SMatthew G. Knepley   }
1982338f77d5SMatthew G. Knepley   /* Do integration for each field */
198373d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1984c1f031eeSMatthew G. Knepley     PetscObject  obj;
1985c1f031eeSMatthew G. Knepley     PetscClassId id;
1986c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
198773d901b8SMatthew G. Knepley 
1988c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1989c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1990c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1991c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1992c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1993c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1994c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1995c1f031eeSMatthew G. Knepley 
19960f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1997c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
19989c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1999c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2000c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
200173d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
20020f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
200373d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
200473d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
200573d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
200673d901b8SMatthew G. Knepley       offset    = numCells - Nr;
2007c330f8ffSToby Isaac       if (!affineQuad) {
2008c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2009c330f8ffSToby Isaac       }
2010c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
20114bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
2012c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
20134bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
2014c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
2015c330f8ffSToby Isaac       if (!affineQuad) {
2016c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
2017c330f8ffSToby Isaac       }
2018c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
2019c1f031eeSMatthew G. Knepley       PetscInt       foff;
2020420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
2021b69edc29SMatthew G. Knepley       PetscScalar    lint;
2022c1f031eeSMatthew G. Knepley 
2023c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
2024c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
2025c1f031eeSMatthew G. Knepley       if (obj_func) {
2026c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
2027b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
2028b5a3613cSMatthew G. Knepley 
2029b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
2030338f77d5SMatthew 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);
2031338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
203273d901b8SMatthew G. Knepley         }
2033c1f031eeSMatthew G. Knepley       }
2034ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
2035c1f031eeSMatthew G. Knepley   }
2036338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
2037b5a3613cSMatthew G. Knepley   if (useFVM) {
2038b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
2039b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
2040b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
2041b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
2042b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
2043b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
2044b5a3613cSMatthew G. Knepley   }
204573d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
2046338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
2047338f77d5SMatthew G. Knepley   /* Cleanup */
2048f99c8401SToby Isaac   if (affineQuad) {
2049f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
2050f99c8401SToby Isaac   }
2051f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
2052c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2053338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
2054338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
2055338f77d5SMatthew G. Knepley }
2056338f77d5SMatthew G. Knepley 
2057338f77d5SMatthew G. Knepley /*@
2058338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
2059338f77d5SMatthew G. Knepley 
2060338f77d5SMatthew G. Knepley   Input Parameters:
2061338f77d5SMatthew G. Knepley + dm - The mesh
2062338f77d5SMatthew G. Knepley . X  - Global input vector
2063338f77d5SMatthew G. Knepley - user - The user context
2064338f77d5SMatthew G. Knepley 
2065338f77d5SMatthew G. Knepley   Output Parameter:
2066338f77d5SMatthew G. Knepley . integral - Integral for each field
2067338f77d5SMatthew G. Knepley 
2068338f77d5SMatthew G. Knepley   Level: developer
2069338f77d5SMatthew G. Knepley 
2070446c23c1SPierre Jolivet .seealso: DMPlexSNESComputeResidualFEM()
2071338f77d5SMatthew G. Knepley @*/
2072b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
2073338f77d5SMatthew G. Knepley {
2074338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
2075b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
2076412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
2077338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
2078338f77d5SMatthew G. Knepley 
2079338f77d5SMatthew G. Knepley   PetscFunctionBegin;
2080338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2081338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
2082338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
2083338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2084338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2085338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
2086412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2087338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
2088338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
2089338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
2090338f77d5SMatthew G. Knepley   /* Sum up values */
2091338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
2092338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2093338f77d5SMatthew G. Knepley 
2094338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2095b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
2096338f77d5SMatthew G. Knepley   }
2097820f2d46SBarry Smith   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
209873d901b8SMatthew G. Knepley   if (mesh->printFEM) {
2099338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
2100b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
210173d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
210273d901b8SMatthew G. Knepley   }
2103338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
2104338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2105338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
2106338f77d5SMatthew G. Knepley }
2107338f77d5SMatthew G. Knepley 
2108338f77d5SMatthew G. Knepley /*@
2109338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
2110338f77d5SMatthew G. Knepley 
2111338f77d5SMatthew G. Knepley   Input Parameters:
2112338f77d5SMatthew G. Knepley + dm - The mesh
2113338f77d5SMatthew G. Knepley . X  - Global input vector
2114338f77d5SMatthew G. Knepley - user - The user context
2115338f77d5SMatthew G. Knepley 
2116338f77d5SMatthew G. Knepley   Output Parameter:
2117338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
2118338f77d5SMatthew G. Knepley 
2119338f77d5SMatthew G. Knepley   Level: developer
2120338f77d5SMatthew G. Knepley 
2121446c23c1SPierre Jolivet .seealso: DMPlexSNESComputeResidualFEM()
2122338f77d5SMatthew G. Knepley @*/
2123338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
2124338f77d5SMatthew G. Knepley {
2125338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
2126338f77d5SMatthew G. Knepley   DM             dmF;
2127338f77d5SMatthew G. Knepley   PetscSection   sectionF;
2128338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
2129412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
2130338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
2131338f77d5SMatthew G. Knepley 
2132338f77d5SMatthew G. Knepley   PetscFunctionBegin;
2133338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2134338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
2135338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
2136338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2137338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2138338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
2139412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2140338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
2141338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
2142338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
2143338f77d5SMatthew G. Knepley   /* Put values in F*/
2144338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
214592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmF, &sectionF);CHKERRQ(ierr);
2146338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
2147338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
2148338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2149338f77d5SMatthew G. Knepley     PetscInt       dof, off;
2150338f77d5SMatthew G. Knepley 
2151338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2152338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
2153338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
2154338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
2155338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
2156338f77d5SMatthew G. Knepley   }
2157338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
2158338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
2159c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
216073d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
216173d901b8SMatthew G. Knepley }
216273d901b8SMatthew G. Knepley 
21639b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
21649b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
216564c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
216664c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
216764c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
216864c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
216964c72086SMatthew G. Knepley {
21709b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
2171a6e0b375SMatthew G. Knepley   DMEnclosureType    encAux;
21729b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
21739b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
21749b6f715bSMatthew G. Knepley   Vec                locA = NULL;
21759b6f715bSMatthew G. Knepley   DMField            coordField;
21769b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
21779b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
21789b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
217964c72086SMatthew G. Knepley   const PetscScalar *constants;
21809b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
218164c72086SMatthew G. Knepley   PetscErrorCode     ierr;
218264c72086SMatthew G. Knepley 
218364c72086SMatthew G. Knepley   PetscFunctionBegin;
21849b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
21859b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
218664c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
218792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
218864c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
218964c72086SMatthew G. Knepley   /* Determine which discretizations we have */
21909b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
219164c72086SMatthew G. Knepley     PetscObject  obj;
219264c72086SMatthew G. Knepley     PetscClassId id;
219364c72086SMatthew G. Knepley 
21949b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
219564c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
21969b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
219764c72086SMatthew G. Knepley   }
219864c72086SMatthew G. Knepley   /* Read DS information */
219964c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
220064c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
220164c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
220264c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
220364c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
22049a2a23afSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, &locA);CHKERRQ(ierr);
22059b6f715bSMatthew G. Knepley   if (locA) {
22069b6f715bSMatthew G. Knepley     DM dmAux;
22079b6f715bSMatthew G. Knepley 
22089b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2209a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
22109b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
221164c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
221264c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
221392fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
221464c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
221564c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
221664c72086SMatthew G. Knepley   }
22179b6f715bSMatthew G. Knepley   /* Integrate over points */
22189b6f715bSMatthew G. Knepley   {
22199b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
2220b7260050SToby Isaac     PetscInt        maxDegree;
22219b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
22229b6f715bSMatthew G. Knepley     const PetscInt *points;
22239b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
22249b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
222564c72086SMatthew G. Knepley 
22269b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
22279b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
22289b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
2229b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
223064c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
223164c72086SMatthew G. Knepley       PetscFE fe;
223264c72086SMatthew G. Knepley 
223364c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
2234b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
22359b6f715bSMatthew G. Knepley       if (!qGeom) {
22369b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
22379b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
22389b6f715bSMatthew G. Knepley       }
22399b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
22409b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
22419b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
2242f15274beSMatthew Knepley         const PetscInt point = points[face], *support;
22439b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
2244f15274beSMatthew Knepley         PetscInt       i;
22459b6f715bSMatthew G. Knepley 
22469b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
22479b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
22489b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
22499b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
22509b6f715bSMatthew G. Knepley         if (locA) {
22519b6f715bSMatthew G. Knepley           PetscInt subp;
2252a6e0b375SMatthew G. Knepley           ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
22539b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
22549b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
22559b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
22569b6f715bSMatthew G. Knepley         }
22579b6f715bSMatthew G. Knepley       }
22589b6f715bSMatthew G. Knepley       /* Get blocking */
22599b6f715bSMatthew G. Knepley       {
22609b6f715bSMatthew G. Knepley         PetscQuadrature q;
22619b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
22629b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
22639b6f715bSMatthew G. Knepley 
226464c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
226564c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
226664c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
226764c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
226864c72086SMatthew G. Knepley         blockSize = Nb*Nq;
226964c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
22709b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
227164c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
22729b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
22739b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
227464c72086SMatthew G. Knepley         offset    = numFaces - Nr;
227564c72086SMatthew G. Knepley       }
22769b6f715bSMatthew G. Knepley       /* Do integration for each field */
22779b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
22789b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
22794bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateBd(prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
22809b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
228164c72086SMatthew G. Knepley       }
22829b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
22834bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrateBd(prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
22849b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
228564c72086SMatthew G. Knepley       /* Cleanup data arrays */
22869b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
22879b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
22889b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
22899b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
229064c72086SMatthew G. Knepley     }
229164c72086SMatthew G. Knepley   }
22929b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
22939b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
22949b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
22959b6f715bSMatthew G. Knepley }
22969b6f715bSMatthew G. Knepley 
22979b6f715bSMatthew G. Knepley /*@
22989b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
22999b6f715bSMatthew G. Knepley 
23009b6f715bSMatthew G. Knepley   Input Parameters:
23019b6f715bSMatthew G. Knepley + dm      - The mesh
23029b6f715bSMatthew G. Knepley . X       - Global input vector
23039b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
23049b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
23059b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
23069b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
23079b6f715bSMatthew G. Knepley - user    - The user context
23089b6f715bSMatthew G. Knepley 
23099b6f715bSMatthew G. Knepley   Output Parameter:
23109b6f715bSMatthew G. Knepley . integral - Integral for each field
23119b6f715bSMatthew G. Knepley 
23129b6f715bSMatthew G. Knepley   Level: developer
23139b6f715bSMatthew G. Knepley 
23149b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
23159b6f715bSMatthew G. Knepley @*/
23169b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
23179b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
23189b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
23199b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
23209b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
23219b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
23229b6f715bSMatthew G. Knepley {
23239b6f715bSMatthew G. Knepley   Vec            locX;
23249b6f715bSMatthew G. Knepley   PetscSection   section;
23259b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
23269b6f715bSMatthew G. Knepley   IS             facetIS;
23279b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
23289b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
23299b6f715bSMatthew G. Knepley 
23309b6f715bSMatthew G. Knepley   PetscFunctionBegin;
23319b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23329b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
23339b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
23349b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
2335064a246eSJacob Faibussowitsch   PetscValidPointer(integral, 7);
23369b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
23379b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
23389b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
23399b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
234092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
23419b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
23429b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
23439b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
23449b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
23459b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
23469b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
23479b6f715bSMatthew G. Knepley   /* Loop over label values */
2348580bdb30SBarry Smith   ierr = PetscArrayzero(integral, Nf);CHKERRQ(ierr);
23499b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
23509b6f715bSMatthew G. Knepley     IS           pointIS;
23519b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
23529b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
23539b6f715bSMatthew G. Knepley 
23549b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
23559b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
23569b6f715bSMatthew G. Knepley     {
23579b6f715bSMatthew G. Knepley       IS isectIS;
23589b6f715bSMatthew G. Knepley 
23599b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
23609b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
23619b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23629b6f715bSMatthew G. Knepley       pointIS = isectIS;
23639b6f715bSMatthew G. Knepley     }
23649b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
23659b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
23669b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
23679b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
23689b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
23699b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
23709b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23719b6f715bSMatthew G. Knepley   }
237264c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
23739b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
23749b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
237564c72086SMatthew G. Knepley   PetscFunctionReturn(0);
237664c72086SMatthew G. Knepley }
237764c72086SMatthew G. Knepley 
2378d69c5d34SMatthew G. Knepley /*@
2379cf51de39SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to a uniformly refined DM.
2380d69c5d34SMatthew G. Knepley 
2381d69c5d34SMatthew G. Knepley   Input Parameters:
2382cf51de39SMatthew G. Knepley + dmc  - The coarse mesh
2383cf51de39SMatthew G. Knepley . dmf  - The fine mesh
2384cf51de39SMatthew G. Knepley . isRefined - Flag indicating regular refinement, rather than the same topology
2385d69c5d34SMatthew G. Knepley - user - The user context
2386d69c5d34SMatthew G. Knepley 
2387d69c5d34SMatthew G. Knepley   Output Parameter:
2388934789fcSMatthew G. Knepley . In  - The interpolation matrix
2389d69c5d34SMatthew G. Knepley 
2390d69c5d34SMatthew G. Knepley   Level: developer
2391d69c5d34SMatthew G. Knepley 
239268132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2393d69c5d34SMatthew G. Knepley @*/
2394cf51de39SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, PetscBool isRefined, Mat In, void *user)
2395d69c5d34SMatthew G. Knepley {
2396d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
2397d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
2398d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
239997c42addSMatthew G. Knepley   PetscFV          *fvRef;
2400d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
2401d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
2402d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
2403485ad865SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
24042ea9c922SToby Isaac   PetscInt          cTotDim=0, rTotDim = 0;
2405d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
2406d69c5d34SMatthew G. Knepley 
2407d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
2408d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2409c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
241092fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2411e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
241292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2413e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
2414d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
2415412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
241697c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &feRef, Nf, &fvRef);CHKERRQ(ierr);
2417d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
24182ea9c922SToby Isaac     PetscObject  obj, objc;
24192ea9c922SToby Isaac     PetscClassId id, idc;
24202ea9c922SToby Isaac     PetscInt     rNb = 0, Nc = 0, cNb = 0;
2421d69c5d34SMatthew G. Knepley 
24222ea9c922SToby Isaac     ierr = DMGetField(dmf, f, NULL, &obj);CHKERRQ(ierr);
242397c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
242497c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
242597c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
242697c42addSMatthew G. Knepley 
2427cf51de39SMatthew G. Knepley       if (isRefined) {
24280f2d7e86SMatthew G. Knepley         ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
2429cf51de39SMatthew G. Knepley       } else {
2430cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fe);CHKERRQ(ierr);
2431cf51de39SMatthew G. Knepley         feRef[f] = fe;
2432cf51de39SMatthew G. Knepley       }
2433d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
24340f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
243597c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
243697c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
243797c42addSMatthew G. Knepley       PetscDualSpace Q;
243897c42addSMatthew G. Knepley 
2439cf51de39SMatthew G. Knepley       if (isRefined) {
244097c42addSMatthew G. Knepley         ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
2441cf51de39SMatthew G. Knepley       } else {
2442cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fv);CHKERRQ(ierr);
2443cf51de39SMatthew G. Knepley         fvRef[f] = fv;
2444cf51de39SMatthew G. Knepley       }
244597c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
244697c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
24472ea9c922SToby Isaac       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
244897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
244997c42addSMatthew G. Knepley     }
24502ea9c922SToby Isaac     ierr = DMGetField(dmc, f, NULL, &objc);CHKERRQ(ierr);
24512ea9c922SToby Isaac     ierr = PetscObjectGetClassId(objc, &idc);CHKERRQ(ierr);
24522ea9c922SToby Isaac     if (idc == PETSCFE_CLASSID) {
24532ea9c922SToby Isaac       PetscFE fe = (PetscFE) objc;
24542ea9c922SToby Isaac 
24552ea9c922SToby Isaac       ierr = PetscFEGetDimension(fe, &cNb);CHKERRQ(ierr);
24562ea9c922SToby Isaac     } else if (id == PETSCFV_CLASSID) {
24572ea9c922SToby Isaac       PetscFV        fv = (PetscFV) obj;
24582ea9c922SToby Isaac       PetscDualSpace Q;
24592ea9c922SToby Isaac 
24602ea9c922SToby Isaac       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
24612ea9c922SToby Isaac       ierr = PetscDualSpaceGetDimension(Q, &cNb);CHKERRQ(ierr);
2462d69c5d34SMatthew G. Knepley     }
24632ea9c922SToby Isaac     rTotDim += rNb;
24642ea9c922SToby Isaac     cTotDim += cNb;
24652ea9c922SToby Isaac   }
24660f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
2467580bdb30SBarry Smith   ierr = PetscArrayzero(elemMat, rTotDim*cTotDim);CHKERRQ(ierr);
2468d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
2469d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
2470d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
2471d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2472d69c5d34SMatthew G. Knepley     PetscReal       *points;
2473d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
2474d69c5d34SMatthew G. Knepley 
2475d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
247697c42addSMatthew G. Knepley     if (feRef[fieldI]) {
2477d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
24780f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
247997c42addSMatthew G. Knepley     } else {
248097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
248197c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
248297c42addSMatthew G. Knepley     }
2483d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
2484d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
2485d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
24869c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
2487d69c5d34SMatthew G. Knepley       npoints += Np;
2488d69c5d34SMatthew G. Knepley     }
2489d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
2490d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
2491d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
24929c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
2493d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
2494d69c5d34SMatthew G. Knepley     }
2495d69c5d34SMatthew G. Knepley 
2496d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
249797c42addSMatthew G. Knepley       PetscObject  obj;
249897c42addSMatthew G. Knepley       PetscClassId id;
24999c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
2500d69c5d34SMatthew G. Knepley 
25012ea9c922SToby Isaac       ierr = DMGetField(dmc, fieldJ, NULL, &obj);CHKERRQ(ierr);
250297c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
250397c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
250497c42addSMatthew G. Knepley         PetscFE           fe = (PetscFE) obj;
2505ef0bb6c7SMatthew G. Knepley         PetscTabulation T  = NULL;
2506d69c5d34SMatthew G. Knepley 
2507d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
25080f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
25090f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2510ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
2511ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
25129c3cf19fSMatthew 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);
2513ef0bb6c7SMatthew G. Knepley           ierr = PetscFECreateTabulation(fe, 1, npoints, points, 0, &T);CHKERRQ(ierr);
2514d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
2515d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
25169c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
25179c3cf19fSMatthew 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);
2518d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
251936a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2520d172c84bSMatthew G. Knepley                 /*
2521d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
2522d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
2523d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
2524d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
2525d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
2526d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
2527d172c84bSMatthew G. Knepley                 */
2528ef0bb6c7SMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += T->T[0][k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c];
252936a6d9c0SMatthew G. Knepley               }
2530d69c5d34SMatthew G. Knepley             }
2531d69c5d34SMatthew G. Knepley           }
2532ef0bb6c7SMatthew G. Knepley           ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);CHKERRQ(ierr);
2533ffe73a53SMatthew G. Knepley         }
253497c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
253597c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
253697c42addSMatthew G. Knepley 
253797c42addSMatthew G. Knepley         /* Evaluate constant function at points */
253897c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
253997c42addSMatthew G. Knepley         cpdim = 1;
254097c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
254197c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
2542ff1e0c32SBarry Smith           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);
254397c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
254497c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
25459c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
25469c3cf19fSMatthew 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);
254797c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
254897c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2549458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
255097c42addSMatthew G. Knepley               }
255197c42addSMatthew G. Knepley             }
255297c42addSMatthew G. Knepley           }
255397c42addSMatthew G. Knepley         }
255497c42addSMatthew G. Knepley       }
2555d172c84bSMatthew G. Knepley       offsetJ += cpdim;
2556d69c5d34SMatthew G. Knepley     }
2557d172c84bSMatthew G. Knepley     offsetI += fpdim;
2558549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
2559d69c5d34SMatthew G. Knepley   }
25600f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
25617f5b169aSMatthew G. Knepley   /* Preallocate matrix */
25627f5b169aSMatthew G. Knepley   {
2563c094ef40SMatthew G. Knepley     Mat          preallocator;
2564c094ef40SMatthew G. Knepley     PetscScalar *vals;
2565c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
2566c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
25677f5b169aSMatthew G. Knepley 
2568c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
2569c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
2570c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
2571c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2572c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
2573c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
25747f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2575cf51de39SMatthew G. Knepley       if (isRefined) {
25767f5b169aSMatthew G. Knepley         ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
2577c094ef40SMatthew G. Knepley         ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
2578cf51de39SMatthew G. Knepley       } else {
2579cf51de39SMatthew G. Knepley         ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, preallocator, cell, vals, INSERT_VALUES);CHKERRQ(ierr);
2580cf51de39SMatthew G. Knepley       }
25817f5b169aSMatthew G. Knepley     }
2582c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
2583c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2584c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2585c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
2586c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
25877f5b169aSMatthew G. Knepley   }
25887f5b169aSMatthew G. Knepley   /* Fill matrix */
25897f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
2590d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2591cf51de39SMatthew G. Knepley     if (isRefined) {
2592934789fcSMatthew G. Knepley       ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2593cf51de39SMatthew G. Knepley     } else {
2594cf51de39SMatthew G. Knepley       ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2595cf51de39SMatthew G. Knepley     }
2596d69c5d34SMatthew G. Knepley   }
2597549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
259897c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
2599549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2600934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2601934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26029318fe57SMatthew G. Knepley   if (mesh->printFEM > 1) {
2603825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
2604934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
2605825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
2606d69c5d34SMatthew G. Knepley   }
2607d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2608d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
2609d69c5d34SMatthew G. Knepley }
26106c73c22cSMatthew G. Knepley 
2611bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
2612bd041c0cSMatthew G. Knepley {
2613bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
2614bd041c0cSMatthew G. Knepley }
2615bd041c0cSMatthew G. Knepley 
261668132eb9SMatthew G. Knepley /*@
261768132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
261868132eb9SMatthew G. Knepley 
261968132eb9SMatthew G. Knepley   Input Parameters:
262068132eb9SMatthew G. Knepley + dmf  - The fine mesh
262168132eb9SMatthew G. Knepley . dmc  - The coarse mesh
262268132eb9SMatthew G. Knepley - user - The user context
262368132eb9SMatthew G. Knepley 
262468132eb9SMatthew G. Knepley   Output Parameter:
262568132eb9SMatthew G. Knepley . In  - The interpolation matrix
262668132eb9SMatthew G. Knepley 
262768132eb9SMatthew G. Knepley   Level: developer
262868132eb9SMatthew G. Knepley 
262968132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
263068132eb9SMatthew G. Knepley @*/
263168132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
26324ef9d792SMatthew G. Knepley {
263364e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
263464e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
26354ef9d792SMatthew G. Knepley   PetscDS        prob;
26364ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2637e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
26384ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
26394ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
26404ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
26414ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
26424ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
26434ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
26444ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
26454ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
26464ef9d792SMatthew G. Knepley 
26474ef9d792SMatthew G. Knepley   PetscFunctionBegin;
264877711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
26494ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
26504ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
26514bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
26524ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
26534ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
26544ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
265592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2656e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
265792fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2658e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
26594ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
26604ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
26619c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
26624ef9d792SMatthew G. Knepley 
26634ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
26644ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
26654ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
26664ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
26674ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
26684ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
26694ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
26704ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2671e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
26724ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
26734ef9d792SMatthew G. Knepley     PetscObject      obj;
26744ef9d792SMatthew G. Knepley     PetscClassId     id;
2675c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
26764ef9d792SMatthew G. Knepley     PetscQuadrature  f;
267717f047d8SMatthew G. Knepley     const PetscReal *qpoints;
267817f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
26794ef9d792SMatthew G. Knepley 
26804ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
26814ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
26824ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
26834ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
26844ef9d792SMatthew G. Knepley 
26854ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
26864ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
26874ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
26884ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
26894ef9d792SMatthew G. Knepley 
26904ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
26914ef9d792SMatthew G. Knepley       Nc   = 1;
26924ef9d792SMatthew G. Knepley     }
26934ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
26944ef9d792SMatthew G. Knepley     /* For each fine grid cell */
26954ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
26964ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
26974ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
26984ef9d792SMatthew G. Knepley 
269971f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
27004ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2701ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
27024ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
27034ef9d792SMatthew G. Knepley         Vec             pointVec;
27044ef9d792SMatthew G. Knepley         PetscScalar    *pV;
27053a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
27063a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
27079c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
27084ef9d792SMatthew G. Knepley 
27094ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
27104ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
27119c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
27124ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
27134ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
27144ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
27154ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2716c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2717c330f8ffSToby Isaac 
27184ef9d792SMatthew G. Knepley           /* Transform point to real space */
2719c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
27204ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
27214ef9d792SMatthew G. Knepley         }
27224ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
27234ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
27241555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
272562a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
27263a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
27274ef9d792SMatthew G. Knepley         /* Update preallocation info */
27283a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
27293a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
27309c3cf19fSMatthew G. Knepley         {
2731e8f14785SLisandro Dalcin           PetscHashIJKey key;
2732e8f14785SLisandro Dalcin           PetscBool      missing;
27334ef9d792SMatthew G. Knepley 
2734e8f14785SLisandro Dalcin           key.i = findices[i];
2735e8f14785SLisandro Dalcin           if (key.i >= 0) {
27364ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
27374ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
273871f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
27394ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2740e8f14785SLisandro Dalcin                 key.j = cindices[c];
2741e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2742e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
27434ef9d792SMatthew G. Knepley                 if (missing) {
2744e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2745e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
27464ef9d792SMatthew G. Knepley                 }
27474ef9d792SMatthew G. Knepley               }
274871f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
27494ef9d792SMatthew G. Knepley             }
27504ef9d792SMatthew G. Knepley           }
27518c543595SMatthew G. Knepley         }
27523a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
27534ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
27544ef9d792SMatthew G. Knepley       }
275571f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
27564ef9d792SMatthew G. Knepley     }
27574ef9d792SMatthew G. Knepley   }
2758e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
27594ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
27604ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
27614ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
27624ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
27634ef9d792SMatthew G. Knepley     PetscObject       obj;
27644ef9d792SMatthew G. Knepley     PetscClassId      id;
2765c0d7054bSMatthew G. Knepley     PetscDualSpace    Q = NULL;
2766ef0bb6c7SMatthew G. Knepley     PetscTabulation T = NULL;
27674ef9d792SMatthew G. Knepley     PetscQuadrature   f;
27684ef9d792SMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
27699c3cf19fSMatthew G. Knepley     PetscInt          Nc, qNc, Np, fpdim, i, d;
27704ef9d792SMatthew G. Knepley 
27714ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
27724ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
27734ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
27744ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
27754ef9d792SMatthew G. Knepley 
27764ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
27774ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
2778ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation(fe, 1, 1, x, 0, &T);CHKERRQ(ierr);
27794ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
27804ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
27814ef9d792SMatthew G. Knepley 
27824ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
27834ef9d792SMatthew G. Knepley       Nc   = 1;
2784ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",field);
27854ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
27864ef9d792SMatthew G. Knepley     /* For each fine grid cell */
27874ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
27884ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
27894ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
27904ef9d792SMatthew G. Knepley 
279171f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
27924ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2793ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
27944ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
27954ef9d792SMatthew G. Knepley         Vec             pointVec;
27964ef9d792SMatthew G. Knepley         PetscScalar    *pV;
279712111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
27983a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
279917f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
28004ef9d792SMatthew G. Knepley 
28014ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
28024ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
28039c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
28049c3cf19fSMatthew 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);
28054ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
28064ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
28074ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
28084ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2809c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2810c330f8ffSToby Isaac 
28114ef9d792SMatthew G. Knepley           /* Transform point to real space */
2812c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
28134ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
28144ef9d792SMatthew G. Knepley         }
28154ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
28164ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
28171555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
281862a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
28194ef9d792SMatthew G. Knepley         /* Update preallocation info */
28203a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
28213a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
28224ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
28234ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2824826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2825c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2826826eb36dSMatthew G. Knepley 
282771f0bbf9SMatthew G. Knepley           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
28284ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
28293a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2830e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2831c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
28324ef9d792SMatthew G. Knepley 
28334ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
28344ef9d792SMatthew G. Knepley             PetscFE fe = (PetscFE) obj;
28354ef9d792SMatthew G. Knepley 
28364ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
28374ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2838ef0bb6c7SMatthew G. Knepley             ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
2839580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
28404ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
28414ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2842ef0bb6c7SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*qweights[ccell*qNc + c];
28434ef9d792SMatthew G. Knepley             }
28444ef9d792SMatthew G. Knepley           } else {
28454ef9d792SMatthew G. Knepley             cpdim = 1;
28464ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
28479c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
28484ef9d792SMatthew G. Knepley             }
28494ef9d792SMatthew G. Knepley           }
28504ef9d792SMatthew G. Knepley           /* Update interpolator */
28519c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
28529c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
28539c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
285471f0bbf9SMatthew G. Knepley           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
28554ef9d792SMatthew G. Knepley         }
28564ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
28573a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
28584ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
28594ef9d792SMatthew G. Knepley       }
286071f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
28614ef9d792SMatthew G. Knepley     }
2862ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
28634ef9d792SMatthew G. Knepley   }
28644ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
28654ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
28664ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
28674ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
28684ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
286977711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
28704ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
28714ef9d792SMatthew G. Knepley }
28724ef9d792SMatthew G. Knepley 
287346fa42a0SMatthew G. Knepley /*@
2874bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2875bd041c0cSMatthew G. Knepley 
2876bd041c0cSMatthew G. Knepley   Input Parameters:
2877bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2878bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2879bd041c0cSMatthew G. Knepley - user - The user context
2880bd041c0cSMatthew G. Knepley 
2881bd041c0cSMatthew G. Knepley   Output Parameter:
2882bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2883bd041c0cSMatthew G. Knepley 
2884bd041c0cSMatthew G. Knepley   Level: developer
2885bd041c0cSMatthew G. Knepley 
2886bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2887bd041c0cSMatthew G. Knepley @*/
2888bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2889bd041c0cSMatthew G. Knepley {
2890bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2891bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2892bd041c0cSMatthew G. Knepley   PetscDS        prob;
2893bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2894e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2895bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2896bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2897bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2898bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2899bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2900bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2901bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2902bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2903bd041c0cSMatthew G. Knepley 
2904bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2905bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2906bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
29074bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2908bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2909bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2910bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
291192fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2912e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
291392fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2914e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2915bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2916bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2917bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2918bd041c0cSMatthew G. Knepley 
2919bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2920bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2921bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2922bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2923bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2924bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2925bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2926bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2927e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2928bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2929bd041c0cSMatthew G. Knepley     PetscObject      obj;
2930bd041c0cSMatthew G. Knepley     PetscClassId     id;
2931bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2932bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2933bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2934bd041c0cSMatthew G. Knepley 
2935bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2936bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2937bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2938bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2939bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2940bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2941bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2942bd041c0cSMatthew G. Knepley       Vec                pointVec;
2943bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2944bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2945bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2946bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2947bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2948bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2949bd041c0cSMatthew G. Knepley 
295071f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
2951bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2952bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2953bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2954bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2955bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2956bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2957c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2958c330f8ffSToby Isaac 
2959bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2960c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2961bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2962bd041c0cSMatthew G. Knepley       }
2963bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2964bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2965bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2966bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2967bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2968bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2969bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2970bd041c0cSMatthew G. Knepley       {
2971e8f14785SLisandro Dalcin         PetscHashIJKey key;
2972e8f14785SLisandro Dalcin         PetscBool      missing;
2973bd041c0cSMatthew G. Knepley 
2974bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2975e8f14785SLisandro Dalcin           key.i = findices[i];
2976e8f14785SLisandro Dalcin           if (key.i >= 0) {
2977bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2978bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
297971f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2980bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2981e8f14785SLisandro Dalcin                 key.j = cindices[c];
2982e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2983e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2984bd041c0cSMatthew G. Knepley                 if (missing) {
2985e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2986e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2987bd041c0cSMatthew G. Knepley                 }
2988bd041c0cSMatthew G. Knepley               }
298971f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2990bd041c0cSMatthew G. Knepley             }
2991bd041c0cSMatthew G. Knepley           }
2992bd041c0cSMatthew G. Knepley         }
2993bd041c0cSMatthew G. Knepley       }
2994bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2995bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
299671f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
2997bd041c0cSMatthew G. Knepley     }
2998bd041c0cSMatthew G. Knepley   }
2999e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
3000bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
3001bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3002bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
3003bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
3004bd041c0cSMatthew G. Knepley     PetscObject       obj;
3005bd041c0cSMatthew G. Knepley     PetscClassId      id;
3006ef0bb6c7SMatthew G. Knepley     PetscTabulation T, Tfine;
3007bd041c0cSMatthew G. Knepley     PetscQuadrature   quad;
3008bd041c0cSMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
3009bd041c0cSMatthew G. Knepley     PetscInt          Nq, Nc, i, d;
3010bd041c0cSMatthew G. Knepley 
3011bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
3012bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3013ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
3014ef0bb6c7SMatthew G. Knepley       ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);
3015f9244615SMatthew G. Knepley       ierr = PetscFEGetCellTabulation((PetscFE) obj, 1, &Tfine);CHKERRQ(ierr);
3016ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation((PetscFE) obj, 1, 1, x, 0, &T);CHKERRQ(ierr);
3017ef0bb6c7SMatthew G. Knepley     } else {
3018ef0bb6c7SMatthew G. Knepley       ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);
3019ef0bb6c7SMatthew G. Knepley     }
3020bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
3021bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
3022bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
3023bd041c0cSMatthew G. Knepley       Vec                pointVec;
3024bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
3025bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
3026bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
3027bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
3028bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
3029bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
3030bd041c0cSMatthew G. Knepley 
303171f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3032bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
3033bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
3034bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
3035bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
3036bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
3037bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
3038c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3039c330f8ffSToby Isaac 
3040bd041c0cSMatthew G. Knepley         /* Transform point to real space */
3041c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
3042bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
3043bd041c0cSMatthew G. Knepley       }
3044bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
3045bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
3046bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
3047bd041c0cSMatthew G. Knepley       /* Update matrix */
3048bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
3049bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
3050bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
3051bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
3052bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
3053c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3054c330f8ffSToby Isaac 
305571f0bbf9SMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3056bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
3057bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
3058bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
3059c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
3060bd041c0cSMatthew G. Knepley 
3061bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
3062bd041c0cSMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
3063bd041c0cSMatthew G. Knepley 
3064bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
3065bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
3066ef0bb6c7SMatthew G. Knepley           ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
3067bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
3068bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
3069580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
3070bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
3071ef0bb6c7SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*Tfine->T[0][(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ;
3072bd041c0cSMatthew G. Knepley             }
3073bd041c0cSMatthew G. Knepley             /* Update interpolator */
3074bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
3075bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
3076bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
3077bd041c0cSMatthew G. Knepley           }
3078bd041c0cSMatthew G. Knepley         } else {
3079bd041c0cSMatthew G. Knepley           cpdim = 1;
3080bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
3081580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
3082bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
3083bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
3084bd041c0cSMatthew G. Knepley             }
3085bd041c0cSMatthew G. Knepley             /* Update interpolator */
3086bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
3087ff1e0c32SBarry Smith             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %D %D Nf: %D %D Nc: %D %D\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
3088bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
3089bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
3090bd041c0cSMatthew G. Knepley           }
3091bd041c0cSMatthew G. Knepley         }
309271f0bbf9SMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3093bd041c0cSMatthew G. Knepley       }
3094bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
3095bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
3096bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
309771f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3098bd041c0cSMatthew G. Knepley     }
3099ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
3100bd041c0cSMatthew G. Knepley   }
3101bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
3102bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
3103bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
3104bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3105bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3106bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
3107bd041c0cSMatthew G. Knepley }
3108bd041c0cSMatthew G. Knepley 
3109bd041c0cSMatthew G. Knepley /*@
311046fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
311146fa42a0SMatthew G. Knepley 
311246fa42a0SMatthew G. Knepley   Input Parameters:
311346fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
311446fa42a0SMatthew G. Knepley - dmf  - The fine mesh
311546fa42a0SMatthew G. Knepley - user - The user context
311646fa42a0SMatthew G. Knepley 
311746fa42a0SMatthew G. Knepley   Output Parameter:
311846fa42a0SMatthew G. Knepley . sc   - The mapping
311946fa42a0SMatthew G. Knepley 
312046fa42a0SMatthew G. Knepley   Level: developer
312146fa42a0SMatthew G. Knepley 
312246fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
312346fa42a0SMatthew G. Knepley @*/
31247c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
31257c927364SMatthew G. Knepley {
3126e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
31277c927364SMatthew G. Knepley   PetscFE       *feRef;
312897c42addSMatthew G. Knepley   PetscFV       *fvRef;
31297c927364SMatthew G. Knepley   Vec            fv, cv;
31307c927364SMatthew G. Knepley   IS             fis, cis;
31317c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
31327c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
3133485ad865SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, endC, offsetC, offsetF, m;
31346f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
31357c927364SMatthew G. Knepley   PetscErrorCode ierr;
31367c927364SMatthew G. Knepley 
31377c927364SMatthew G. Knepley   PetscFunctionBegin;
313875a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3139c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
314092fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
3141e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
314292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
3143e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
31447c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
3145412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
3146e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
31476f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
31487c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
314997c42addSMatthew G. Knepley     PetscObject  obj;
315097c42addSMatthew G. Knepley     PetscClassId id;
3151aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
31527c927364SMatthew G. Knepley 
315397c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
315497c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
315597c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
315697c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
31576f3d3cbcSMatthew G. Knepley       PetscSpace sp;
31589b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
315997c42addSMatthew G. Knepley 
31607c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
31617c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
31627c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
31636f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
31649b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
31659b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
316697c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
316797c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
316897c42addSMatthew G. Knepley       PetscDualSpace Q;
316997c42addSMatthew G. Knepley 
317097c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
317197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
317297c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
317397c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
31746f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
317597c42addSMatthew G. Knepley     }
3176d172c84bSMatthew G. Knepley     fTotDim += fNb;
31777c927364SMatthew G. Knepley   }
3178e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
31797c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
31807c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
31817c927364SMatthew G. Knepley     PetscFE        feC;
318297c42addSMatthew G. Knepley     PetscFV        fvC;
31837c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
3184d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
31857c927364SMatthew G. Knepley 
318697c42addSMatthew G. Knepley     if (feRef[field]) {
3187e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
31887c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
31897c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
31907c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
3191d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
31927c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
31937c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
31947c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
319597c42addSMatthew G. Knepley     } else {
319697c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
319797c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
319897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
319997c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
320097c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
320197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
320297c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
320397c42addSMatthew G. Knepley     }
3204ff1e0c32SBarry Smith     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);
32057c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
32067c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
3207d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
3208d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
320997c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
32107c927364SMatthew G. Knepley 
32117c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
3212d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
3213ff1e0c32SBarry Smith       if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcC, NcC);
321497c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
32157c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
32167c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
3217d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
32187c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
3219d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
32207c927364SMatthew G. Knepley 
32217c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
3222d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
3223ff1e0c32SBarry Smith         if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcF, NcF);
32247c927364SMatthew G. Knepley         if (NpC != NpF) continue;
32257c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
32267c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
3227d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
3228d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
3229d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
323097c42addSMatthew G. Knepley         found = PETSC_TRUE;
32317c927364SMatthew G. Knepley         break;
32327c927364SMatthew G. Knepley       }
323397c42addSMatthew G. Knepley       if (!found) {
323497c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
3235d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
3236d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
323797c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
323897c42addSMatthew G. Knepley       }
32397c927364SMatthew G. Knepley     }
3240d172c84bSMatthew G. Knepley     offsetC += cpdim;
3241d172c84bSMatthew G. Knepley     offsetF += fpdim;
32427c927364SMatthew G. Knepley   }
324397c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
32446f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
32457c927364SMatthew G. Knepley 
32467c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
32477c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
32480bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
32497c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
32507c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
3251aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
3252aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
32537c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
32547c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
32557c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
32567c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
32570bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
3258ff1e0c32SBarry Smith       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]]);
32597c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
32607c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
32617c927364SMatthew G. Knepley     }
32627c927364SMatthew G. Knepley   }
32637c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
32647c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
32657c927364SMatthew G. Knepley 
32667c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
32677c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
32689448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
32697c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
32707c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
32717c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
32727c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
327375a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3274cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
3275cb1e1211SMatthew G Knepley }
3276a1cf66bbSMatthew G. Knepley 
32772f856554SMatthew G. Knepley /*@C
32782f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
32792f856554SMatthew G. Knepley 
32802f856554SMatthew G. Knepley   Input Parameters:
32812f856554SMatthew G. Knepley + dm     - The DM
32822f856554SMatthew G. Knepley . cellIS - The cells to include
32832f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
32842f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
32852f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
32862f856554SMatthew G. Knepley 
32872f856554SMatthew G. Knepley   Output Parameters:
32882f856554SMatthew G. Knepley + u   - The field coefficients
32892f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
32902f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
32912f856554SMatthew G. Knepley 
32922f856554SMatthew G. Knepley   Level: developer
32932f856554SMatthew G. Knepley 
32942f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
32952f856554SMatthew G. Knepley @*/
32962f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
32972f856554SMatthew G. Knepley {
32982f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
3299a6e0b375SMatthew G. Knepley   DMEnclosureType encAux;
33002f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
33012f856554SMatthew G. Knepley   PetscDS         prob;
33022f856554SMatthew G. Knepley   const PetscInt *cells;
33032f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
33042f856554SMatthew G. Knepley   PetscErrorCode  ierr;
33052f856554SMatthew G. Knepley 
33062f856554SMatthew G. Knepley   PetscFunctionBegin;
33072f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3308064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(locX, VEC_CLASSID, 3);
3309064a246eSJacob Faibussowitsch   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 4);}
3310064a246eSJacob Faibussowitsch   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 5);}
3311064a246eSJacob Faibussowitsch   PetscValidPointer(u, 6);
3312064a246eSJacob Faibussowitsch   PetscValidPointer(u_t, 7);
3313064a246eSJacob Faibussowitsch   PetscValidPointer(a, 8);
33142f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
33152f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
331692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
3317083401c6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
33182f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
33192f856554SMatthew G. Knepley   if (locA) {
33202f856554SMatthew G. Knepley     DM      dmAux;
33212f856554SMatthew G. Knepley     PetscDS probAux;
33222f856554SMatthew G. Knepley 
33232f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
3324a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
33252f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
332692fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
33272f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
33282f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
33292f856554SMatthew G. Knepley   }
33302f856554SMatthew G. Knepley   numCells = cEnd - cStart;
33312f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
33322f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
33332f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
33342f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
33352f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
33362f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
33372f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
33382f856554SMatthew G. Knepley     PetscInt       i;
33392f856554SMatthew G. Knepley 
33402f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
33412f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
33422f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
33432f856554SMatthew G. Knepley     if (locX_t) {
33442f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
33452f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
33462f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
33472f856554SMatthew G. Knepley     }
33482f856554SMatthew G. Knepley     if (locA) {
33492f856554SMatthew G. Knepley       PetscInt subcell;
3350a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(plexA, dm, encAux, cell, &subcell);CHKERRQ(ierr);
33512f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
33522f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
33532f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
33542f856554SMatthew G. Knepley     }
33552f856554SMatthew G. Knepley   }
33562f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
33572f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
33582f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
33592f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33602f856554SMatthew G. Knepley }
33612f856554SMatthew G. Knepley 
33622f856554SMatthew G. Knepley /*@C
33632f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
33642f856554SMatthew G. Knepley 
33652f856554SMatthew G. Knepley   Input Parameters:
33662f856554SMatthew G. Knepley + dm     - The DM
33672f856554SMatthew G. Knepley . cellIS - The cells to include
33682f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
33692f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
33702f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
33712f856554SMatthew G. Knepley 
33722f856554SMatthew G. Knepley   Output Parameters:
33732f856554SMatthew G. Knepley + u   - The field coefficients
33742f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
33752f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
33762f856554SMatthew G. Knepley 
33772f856554SMatthew G. Knepley   Level: developer
33782f856554SMatthew G. Knepley 
33792f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
33802f856554SMatthew G. Knepley @*/
33812f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
33822f856554SMatthew G. Knepley {
33832f856554SMatthew G. Knepley   PetscErrorCode ierr;
33842f856554SMatthew G. Knepley 
33852f856554SMatthew G. Knepley   PetscFunctionBegin;
33862f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
33872f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
33882f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
33892f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33902f856554SMatthew G. Knepley }
33912f856554SMatthew G. Knepley 
3392faff4f7fSMatthew G. Knepley /*
3393faff4f7fSMatthew G. Knepley   Get the auxiliary field vectors for the negative side (s = 0) and positive side (s = 1) of the interfaace
3394faff4f7fSMatthew G. Knepley */
3395148442b3SMatthew G. Knepley static PetscErrorCode DMPlexGetHybridAuxFields(DM dm, DM dmAux[], PetscDS dsAux[], IS cellIS, Vec locA[], PetscScalar *a[])
33966528b96dSMatthew G. Knepley {
339704c51a94SMatthew G. Knepley   DM              plexA[2];
3398148442b3SMatthew G. Knepley   DMEnclosureType encAux[2];
339904c51a94SMatthew G. Knepley   PetscSection    sectionAux[2];
34006528b96dSMatthew G. Knepley   const PetscInt *cells;
3401148442b3SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, c, s, totDimAux[2];
34026528b96dSMatthew G. Knepley   PetscErrorCode  ierr;
34036528b96dSMatthew G. Knepley 
34046528b96dSMatthew G. Knepley   PetscFunctionBegin;
3405148442b3SMatthew G. Knepley   PetscValidPointer(locA, 5);
340604c51a94SMatthew G. Knepley   if (!locA[0] || !locA[1]) PetscFunctionReturn(0);
3407148442b3SMatthew G. Knepley   PetscValidPointer(dmAux, 2);
3408148442b3SMatthew G. Knepley   PetscValidPointer(dsAux, 3);
3409148442b3SMatthew G. Knepley   PetscValidPointer(a, 6);
34106528b96dSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
34116528b96dSMatthew G. Knepley   numCells = cEnd - cStart;
3412148442b3SMatthew G. Knepley   for (s = 0; s < 2; ++s) {
3413148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(dmAux[s], DM_CLASSID, 2);
3414148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(dsAux[s], PETSCDS_CLASSID, 3);
3415148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(locA[s], VEC_CLASSID, 5);
3416148442b3SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux[s], &plexA[s], PETSC_FALSE);CHKERRQ(ierr);
3417148442b3SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux[s], dm, &encAux[s]);CHKERRQ(ierr);
3418148442b3SMatthew G. Knepley     ierr = DMGetLocalSection(dmAux[s], &sectionAux[s]);CHKERRQ(ierr);
3419148442b3SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[s], &totDimAux[s]);CHKERRQ(ierr);
3420148442b3SMatthew G. Knepley     ierr = DMGetWorkArray(dmAux[s], numCells*totDimAux[s], MPIU_SCALAR, &a[s]);CHKERRQ(ierr);
342104c51a94SMatthew G. Knepley   }
3422148442b3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
34236528b96dSMatthew G. Knepley     const PetscInt  cell = cells ? cells[c] : c;
34246528b96dSMatthew G. Knepley     const PetscInt  cind = c - cStart;
34256528b96dSMatthew G. Knepley     const PetscInt *cone, *ornt;
34266528b96dSMatthew G. Knepley 
3427148442b3SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
3428148442b3SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
3429148442b3SMatthew G. Knepley     if (ornt[0]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_SUP, "Face %D in hybrid cell %D has orientation %D != 0", cone[0], cell, ornt[0]);
3430148442b3SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
3431*5fedec97SMatthew G. Knepley       const PetscInt *support;
3432148442b3SMatthew G. Knepley       PetscScalar    *x = NULL, *al = a[s];
3433148442b3SMatthew G. Knepley       const PetscInt  tdA = totDimAux[s];
3434*5fedec97SMatthew G. Knepley       PetscInt        ssize, scell;
3435148442b3SMatthew G. Knepley       PetscInt        subface, Na, i;
34366528b96dSMatthew G. Knepley 
3437*5fedec97SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[s], &support);CHKERRQ(ierr);
3438*5fedec97SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[s], &ssize);CHKERRQ(ierr);
3439*5fedec97SMatthew G. Knepley       if (ssize != 2) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[s], cell, ssize);
3440*5fedec97SMatthew G. Knepley       if      (support[0] == cell) scell = support[1];
3441*5fedec97SMatthew G. Knepley       else if (support[1] == cell) scell = support[0];
3442*5fedec97SMatthew G. Knepley       else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[s], cell);
3443*5fedec97SMatthew G. Knepley 
3444*5fedec97SMatthew G. Knepley       ierr = DMGetEnclosurePoint(plexA[s], dm, encAux[s], scell, &subface);CHKERRQ(ierr);
3445148442b3SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA[s], sectionAux[s], locA[s], subface, &Na, &x);CHKERRQ(ierr);
34466528b96dSMatthew G. Knepley       for (i = 0; i < Na; ++i) al[cind*tdA+i] = x[i];
3447148442b3SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA[s], sectionAux[s], locA[s], subface, &Na, &x);CHKERRQ(ierr);
34486528b96dSMatthew G. Knepley     }
34496528b96dSMatthew G. Knepley   }
3450148442b3SMatthew G. Knepley   for (s = 0; s < 2; ++s) {ierr = DMDestroy(&plexA[s]);CHKERRQ(ierr);}
34516528b96dSMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
34526528b96dSMatthew G. Knepley   PetscFunctionReturn(0);
34536528b96dSMatthew G. Knepley }
34546528b96dSMatthew G. Knepley 
345504c51a94SMatthew G. Knepley static PetscErrorCode DMPlexRestoreHybridAuxFields(DM dmAux[], PetscDS dsAux[], IS cellIS, Vec locA[], PetscScalar *a[])
34566528b96dSMatthew G. Knepley {
34576528b96dSMatthew G. Knepley   PetscErrorCode ierr;
34586528b96dSMatthew G. Knepley 
34596528b96dSMatthew G. Knepley   PetscFunctionBegin;
346004c51a94SMatthew G. Knepley   if (!locA[0] || !locA[1]) PetscFunctionReturn(0);
346104c51a94SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmAux[0], 0, MPIU_SCALAR, &a[0]);CHKERRQ(ierr);
346204c51a94SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmAux[1], 0, MPIU_SCALAR, &a[1]);CHKERRQ(ierr);
34636528b96dSMatthew G. Knepley   PetscFunctionReturn(0);
34646528b96dSMatthew G. Knepley }
34656528b96dSMatthew G. Knepley 
34662f856554SMatthew G. Knepley /*@C
34672f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
34682f856554SMatthew G. Knepley 
34692f856554SMatthew G. Knepley   Input Parameters:
34702f856554SMatthew G. Knepley + dm     - The DM
34712f856554SMatthew G. Knepley . fStart - The first face to include
34722f856554SMatthew G. Knepley . fEnd   - The first face to exclude
34732f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
34742f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
34752f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
34762f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
34772f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
34782f856554SMatthew G. Knepley 
34792f856554SMatthew G. Knepley   Output Parameters:
34802f856554SMatthew G. Knepley + Nface - The number of faces with field values
34812f856554SMatthew G. Knepley . uL - The field values at the left side of the face
34822f856554SMatthew G. Knepley - uR - The field values at the right side of the face
34832f856554SMatthew G. Knepley 
34842f856554SMatthew G. Knepley   Level: developer
34852f856554SMatthew G. Knepley 
34862f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
34872f856554SMatthew G. Knepley @*/
34882f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
34892f856554SMatthew G. Knepley {
34902f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
34912f856554SMatthew G. Knepley   PetscSection       section;
34922f856554SMatthew G. Knepley   PetscDS            prob;
34932f856554SMatthew G. Knepley   DMLabel            ghostLabel;
34942f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
34952f856554SMatthew G. Knepley   PetscBool         *isFE;
34962f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
34972f856554SMatthew G. Knepley   PetscErrorCode     ierr;
34982f856554SMatthew G. Knepley 
34992f856554SMatthew G. Knepley   PetscFunctionBegin;
35002f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35012f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
35022f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
35032f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
35042f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
35052f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
3506064a246eSJacob Faibussowitsch   PetscValidPointer(uL, 10);
3507064a246eSJacob Faibussowitsch   PetscValidPointer(uR, 11);
35082f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35092f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
351092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
35112f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
35122f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
35132f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
35142f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35152f856554SMatthew G. Knepley     PetscObject  obj;
35162f856554SMatthew G. Knepley     PetscClassId id;
35172f856554SMatthew G. Knepley 
35182f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
35192f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
35202f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
35212f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
35222f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
35232f856554SMatthew G. Knepley   }
35242f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
35252f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
35262f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
35272f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
35282f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
35292f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
35302f856554SMatthew G. Knepley   if (locGrad) {
35312f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
35322f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
35332f856554SMatthew G. Knepley   }
35342f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
35352f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
35362f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
35372f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
35382f856554SMatthew G. Knepley     const PetscInt        *cells;
35392f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
35402f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
35412f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
35422f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
35432f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
35442f856554SMatthew G. Knepley 
35452f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
35462f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
35472f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
35482f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
35492f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
35502f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
35512f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
35522f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
35532f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
35542f856554SMatthew G. Knepley       PetscInt off;
35552f856554SMatthew G. Knepley 
35562f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
35572f856554SMatthew G. Knepley       if (isFE[f]) {
35582f856554SMatthew G. Knepley         const PetscInt *cone;
35592f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
35602f856554SMatthew G. Knepley 
35612f856554SMatthew G. Knepley         xL = xR = NULL;
35622f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
35632f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
35642f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
35652f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
35662f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
35672f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
35682f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
35692f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
35702f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
3571ff1e0c32SBarry Smith         if (faceLocL == coneSizeL && faceLocR == coneSizeR) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of cell %D or cell %D", face, cells[0], cells[1]);
35722f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
35732f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
35742f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
3575a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
3576a8f1f9e5SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
35772f856554SMatthew G. Knepley           else              {for (d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
35782f856554SMatthew G. Knepley         }
35792f856554SMatthew G. Knepley         else {
3580a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
35812f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
35822f856554SMatthew G. Knepley           for (d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
35832f856554SMatthew G. Knepley         }
35842f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
35852f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
35862f856554SMatthew G. Knepley       } else {
35872f856554SMatthew G. Knepley         PetscFV  fv;
35882f856554SMatthew G. Knepley         PetscInt numComp, c;
35892f856554SMatthew G. Knepley 
35902f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
35912f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
35922f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
35932f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
35942f856554SMatthew G. Knepley         if (dmGrad) {
35952f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
35962f856554SMatthew G. Knepley 
35972f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
35982f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
35992f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
36002f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
36012f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
36022f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
36032f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
36042f856554SMatthew G. Knepley           }
36052f856554SMatthew G. Knepley         } else {
36062f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
36072f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
36082f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
36092f856554SMatthew G. Knepley           }
36102f856554SMatthew G. Knepley         }
36112f856554SMatthew G. Knepley       }
36122f856554SMatthew G. Knepley     }
36132f856554SMatthew G. Knepley     ++iface;
36142f856554SMatthew G. Knepley   }
36152f856554SMatthew G. Knepley   *Nface = iface;
36162f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
36172f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
36182f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
36192f856554SMatthew G. Knepley   if (locGrad) {
36202f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
36212f856554SMatthew G. Knepley   }
36222f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
36232f856554SMatthew G. Knepley   PetscFunctionReturn(0);
36242f856554SMatthew G. Knepley }
36252f856554SMatthew G. Knepley 
36262f856554SMatthew G. Knepley /*@C
36272f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
36282f856554SMatthew G. Knepley 
36292f856554SMatthew G. Knepley   Input Parameters:
36302f856554SMatthew G. Knepley + dm     - The DM
36312f856554SMatthew G. Knepley . fStart - The first face to include
36322f856554SMatthew G. Knepley . fEnd   - The first face to exclude
36332f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
36342f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
36352f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
36362f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
36372f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
36382f856554SMatthew G. Knepley 
36392f856554SMatthew G. Knepley   Output Parameters:
36402f856554SMatthew G. Knepley + Nface - The number of faces with field values
36412f856554SMatthew G. Knepley . uL - The field values at the left side of the face
36422f856554SMatthew G. Knepley - uR - The field values at the right side of the face
36432f856554SMatthew G. Knepley 
36442f856554SMatthew G. Knepley   Level: developer
36452f856554SMatthew G. Knepley 
36462f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
36472f856554SMatthew G. Knepley @*/
36482f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceFields(DM dm, PetscInt fStart, PetscInt fEnd, Vec locX, Vec locX_t, Vec faceGeometry, Vec cellGeometry, Vec locGrad, PetscInt *Nface, PetscScalar **uL, PetscScalar **uR)
36492f856554SMatthew G. Knepley {
36502f856554SMatthew G. Knepley   PetscErrorCode ierr;
36512f856554SMatthew G. Knepley 
36522f856554SMatthew G. Knepley   PetscFunctionBegin;
36532f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
36542f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
36552f856554SMatthew G. Knepley   PetscFunctionReturn(0);
36562f856554SMatthew G. Knepley }
36572f856554SMatthew G. Knepley 
36582f856554SMatthew G. Knepley /*@C
36592f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
36602f856554SMatthew G. Knepley 
36612f856554SMatthew G. Knepley   Input Parameters:
36622f856554SMatthew G. Knepley + dm     - The DM
36632f856554SMatthew G. Knepley . fStart - The first face to include
36642f856554SMatthew G. Knepley . fEnd   - The first face to exclude
36652f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
36662f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
36672f856554SMatthew G. Knepley 
36682f856554SMatthew G. Knepley   Output Parameters:
36692f856554SMatthew G. Knepley + Nface - The number of faces with field values
36702f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
36712f856554SMatthew G. Knepley - vol   - The cell volume
36722f856554SMatthew G. Knepley 
36732f856554SMatthew G. Knepley   Level: developer
36742f856554SMatthew G. Knepley 
36752f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
36762f856554SMatthew G. Knepley @*/
36772f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
36782f856554SMatthew G. Knepley {
36792f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
36802f856554SMatthew G. Knepley   DMLabel            ghostLabel;
36812f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
36822f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
36832f856554SMatthew G. Knepley   PetscErrorCode     ierr;
36842f856554SMatthew G. Knepley 
36852f856554SMatthew G. Knepley   PetscFunctionBegin;
36862f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36872f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
36882f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
3689064a246eSJacob Faibussowitsch   PetscValidPointer(fgeom, 7);
3690064a246eSJacob Faibussowitsch   PetscValidPointer(vol, 8);
36912f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
36922f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
36932f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
36942f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
36952f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
36962f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
36972f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
36982f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
36992f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
37002f856554SMatthew G. Knepley     const PetscInt        *cells;
37012f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
37022f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
37032f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
37042f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
37052f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
37062f856554SMatthew G. Knepley 
37072f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
37082f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
37092f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
37102f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
37112f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
37122f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
37132f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
37142f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
37152f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
37162f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
37172f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
37182f856554SMatthew G. Knepley     }
37192f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
37202f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
37212f856554SMatthew G. Knepley     ++iface;
37222f856554SMatthew G. Knepley   }
37232f856554SMatthew G. Knepley   *Nface = iface;
37242f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
37252f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
37262f856554SMatthew G. Knepley   PetscFunctionReturn(0);
37272f856554SMatthew G. Knepley }
37282f856554SMatthew G. Knepley 
37292f856554SMatthew G. Knepley /*@C
37302f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
37312f856554SMatthew G. Knepley 
37322f856554SMatthew G. Knepley   Input Parameters:
37332f856554SMatthew G. Knepley + dm     - The DM
37342f856554SMatthew G. Knepley . fStart - The first face to include
37352f856554SMatthew G. Knepley . fEnd   - The first face to exclude
37362f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
37372f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
37382f856554SMatthew G. Knepley 
37392f856554SMatthew G. Knepley   Output Parameters:
37402f856554SMatthew G. Knepley + Nface - The number of faces with field values
37412f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
37422f856554SMatthew G. Knepley - vol   - The cell volume
37432f856554SMatthew G. Knepley 
37442f856554SMatthew G. Knepley   Level: developer
37452f856554SMatthew G. Knepley 
37462f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
37472f856554SMatthew G. Knepley @*/
37482f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
37492f856554SMatthew G. Knepley {
37502f856554SMatthew G. Knepley   PetscErrorCode ierr;
37512f856554SMatthew G. Knepley 
37522f856554SMatthew G. Knepley   PetscFunctionBegin;
37532f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
37542f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
37552f856554SMatthew G. Knepley   PetscFunctionReturn(0);
37562f856554SMatthew G. Knepley }
37572f856554SMatthew G. Knepley 
3758a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3759a1cf66bbSMatthew G. Knepley {
3760a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3761a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3762a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3763a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3764a1cf66bbSMatthew G. Knepley 
3765a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3766a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3767a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3768a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3769a1cf66bbSMatthew G. Knepley   if (container) {
3770a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3771a1cf66bbSMatthew G. Knepley   } else {
3772a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3773a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3774a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3775a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3776a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3777a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3778a1cf66bbSMatthew G. Knepley   }
3779a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3780a1cf66bbSMatthew G. Knepley }
3781a1cf66bbSMatthew G. Knepley 
3782a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3783a1cf66bbSMatthew G. Knepley {
3784a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3785a1cf66bbSMatthew G. Knepley   *geom = NULL;
3786a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3787a1cf66bbSMatthew G. Knepley }
3788a1cf66bbSMatthew G. Knepley 
378992d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
379092d50984SMatthew G. Knepley {
379192d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
379292d50984SMatthew G. Knepley   const char      *name       = "Residual";
379392d50984SMatthew G. Knepley   DM               dmAux      = NULL;
379492d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
379592d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
379692d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
379792d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
379892d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
379992d50984SMatthew G. Knepley   DMField          coordField = NULL;
3800c0006e53SPatrick Farrell   Vec              locA;
3801c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
380292d50984SMatthew G. Knepley   IS               chunkIS;
380392d50984SMatthew G. Knepley   const PetscInt  *cells;
380492d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3805364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
380692d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
380706ad1575SMatthew G. Knepley   PetscFormKey key;
380892d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
380992d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
381092d50984SMatthew G. Knepley   PetscErrorCode   ierr;
381192d50984SMatthew G. Knepley 
381292d50984SMatthew G. Knepley   PetscFunctionBegin;
381392d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
381492d50984SMatthew G. Knepley   /* FEM+FVM */
381592d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
381692d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
381792d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
381892d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
381992d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
38209a2a23afSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, &locA);CHKERRQ(ierr);
382192d50984SMatthew G. Knepley   if (locA) {
382292d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
382392d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
382492d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
382592d50984SMatthew G. Knepley   }
382692d50984SMatthew G. Knepley   /* 2: Get geometric data */
382792d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
382892d50984SMatthew G. Knepley     PetscObject  obj;
382992d50984SMatthew G. Knepley     PetscClassId id;
383092d50984SMatthew G. Knepley     PetscBool    fimp;
383192d50984SMatthew G. Knepley 
383292d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
383392d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
383492d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
383592d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
383692d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
3837364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
383892d50984SMatthew G. Knepley   }
383992d50984SMatthew G. Knepley   if (useFEM) {
384092d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
384192d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
384292d50984SMatthew G. Knepley     if (maxDegree <= 1) {
384392d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
384492d50984SMatthew G. Knepley       if (affineQuad) {
384592d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
384692d50984SMatthew G. Knepley       }
384792d50984SMatthew G. Knepley     } else {
384892d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
384992d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
385092d50984SMatthew G. Knepley         PetscObject  obj;
385192d50984SMatthew G. Knepley         PetscClassId id;
385292d50984SMatthew G. Knepley         PetscBool    fimp;
385392d50984SMatthew G. Knepley 
385492d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
385592d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
385692d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
385792d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
385892d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
385992d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
386092d50984SMatthew G. Knepley 
386192d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
386292d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
386392d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
386492d50984SMatthew G. Knepley         }
386592d50984SMatthew G. Knepley       }
386692d50984SMatthew G. Knepley     }
386792d50984SMatthew G. Knepley   }
386892d50984SMatthew G. Knepley   /* Loop over chunks */
386992d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
387092d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
387192d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
387292d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
387392d50984SMatthew G. Knepley   numChunks     = 1;
387492d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
387592d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
38766528b96dSMatthew G. Knepley   key.label     = NULL;
38776528b96dSMatthew G. Knepley   key.value     = 0;
387806ad1575SMatthew G. Knepley   key.part      = 0;
387992d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
3880c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
3881c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
3882c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
388392d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
3884c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
388592d50984SMatthew G. Knepley 
388692d50984SMatthew G. Knepley     /* Extract field coefficients */
388792d50984SMatthew G. Knepley     if (useFEM) {
388892d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
388992d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
389092d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
3891580bdb30SBarry Smith       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
389292d50984SMatthew G. Knepley     }
389392d50984SMatthew 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 */
389492d50984SMatthew G. Knepley     /* Loop over fields */
389592d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
389692d50984SMatthew G. Knepley       PetscObject  obj;
389792d50984SMatthew G. Knepley       PetscClassId id;
389892d50984SMatthew G. Knepley       PetscBool    fimp;
389992d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
390092d50984SMatthew G. Knepley 
39016528b96dSMatthew G. Knepley       key.field = f;
390292d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
390392d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
390492d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
390592d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
390692d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
390792d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
390892d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
390992d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
391092d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
391192d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
391292d50984SMatthew G. Knepley 
391392d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
391492d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
391592d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
391692d50984SMatthew G. Knepley         blockSize = Nb;
391792d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
391892d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
391992d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
392092d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
392192d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
392292d50984SMatthew G. Knepley         offset    = numCells - Nr;
392392d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
392492d50984SMatthew 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) */
392592d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
39266528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, key, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
392792d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
39286528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
392992d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
393092d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
393192d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
393292d50984SMatthew G. Knepley 
393392d50984SMatthew G. Knepley         Ne = numFaces;
393492d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
393592d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
393692d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
3937ff1e0c32SBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
393892d50984SMatthew G. Knepley     }
393992d50984SMatthew G. Knepley     /* Loop over domain */
394092d50984SMatthew G. Knepley     if (useFEM) {
394192d50984SMatthew G. Knepley       /* Add elemVec to locX */
394292d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
394392d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
394492d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
394592d50984SMatthew G. Knepley 
394692d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
394792d50984SMatthew G. Knepley         if (ghostLabel) {
394892d50984SMatthew G. Knepley           PetscInt ghostVal;
394992d50984SMatthew G. Knepley 
395092d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
395192d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
395292d50984SMatthew G. Knepley         }
395392d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
395492d50984SMatthew G. Knepley       }
395592d50984SMatthew G. Knepley     }
395692d50984SMatthew G. Knepley     /* Handle time derivative */
395792d50984SMatthew G. Knepley     if (locX_t) {
395892d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
395992d50984SMatthew G. Knepley 
396092d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
396192d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
396292d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
396392d50984SMatthew G. Knepley         PetscFV      fv;
396492d50984SMatthew G. Knepley         PetscObject  obj;
396592d50984SMatthew G. Knepley         PetscClassId id;
396692d50984SMatthew G. Knepley         PetscInt     pdim, d;
396792d50984SMatthew G. Knepley 
396892d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
396992d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
397092d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
397192d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
397292d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
397392d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
397492d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
397592d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
397692d50984SMatthew G. Knepley 
397792d50984SMatthew G. Knepley           if (ghostLabel) {
397892d50984SMatthew G. Knepley             PetscInt ghostVal;
397992d50984SMatthew G. Knepley 
398092d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
398192d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
398292d50984SMatthew G. Knepley           }
398392d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
398492d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
398592d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
398692d50984SMatthew G. Knepley         }
398792d50984SMatthew G. Knepley       }
398892d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
398992d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
399092d50984SMatthew G. Knepley     }
399192d50984SMatthew G. Knepley     if (useFEM) {
399292d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
399392d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
399492d50984SMatthew G. Knepley     }
399592d50984SMatthew G. Knepley   }
399692d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
399792d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
399892d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
399992d50984SMatthew G. Knepley   if (useFEM) {
400092d50984SMatthew G. Knepley     if (maxDegree <= 1) {
400192d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
400292d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
400392d50984SMatthew G. Knepley     } else {
400492d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
400592d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
400692d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
400792d50984SMatthew G. Knepley       }
400892d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
400992d50984SMatthew G. Knepley     }
401092d50984SMatthew G. Knepley   }
401192d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
401292d50984SMatthew G. Knepley   PetscFunctionReturn(0);
401392d50984SMatthew G. Knepley }
401492d50984SMatthew G. Knepley 
4015a1cf66bbSMatthew G. Knepley /*
4016a1cf66bbSMatthew 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
4017a1cf66bbSMatthew G. Knepley 
4018a1cf66bbSMatthew G. Knepley   X   - The local solution vector
4019a5b23f4aSJose E. Roman   X_t - The local solution time derivative vector, or NULL
4020a1cf66bbSMatthew G. Knepley */
4021a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
4022a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
4023a1cf66bbSMatthew G. Knepley {
4024a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
4025a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
4026a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
4027a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
4028a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
4029a1cf66bbSMatthew G. Knepley   Vec              A;
4030a1cf66bbSMatthew G. Knepley   DMField          coordField;
4031a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
4032a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
4033a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
4034a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
40359b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
4036a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
403706ad1575SMatthew G. Knepley   PetscFormKey key;
40389b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
4039a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
4040a1cf66bbSMatthew G. Knepley 
4041a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
4042a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
4043a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
4044a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
4045a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
40469a2a23afSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, &A);CHKERRQ(ierr);
40479a2a23afSMatthew G. Knepley   if (A) {
40489a2a23afSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
404992fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
4050a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
4051a1cf66bbSMatthew G. Knepley   }
4052a1cf66bbSMatthew G. Knepley   /* Get flags */
4053a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
4054a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
4055a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
4056a1cf66bbSMatthew G. Knepley     PetscObject  disc;
4057a1cf66bbSMatthew G. Knepley     PetscClassId id;
4058a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
4059a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
4060a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
4061a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
4062a1cf66bbSMatthew G. Knepley   }
4063a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
4064a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
4065a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
4066a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
4067a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
4068a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
4069a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
4070a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
4071a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
4072a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
4073a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
4074a1cf66bbSMatthew 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 */
4075a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
4076a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
4077a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
4078a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
4079a1cf66bbSMatthew G. Knepley     PetscFE         fe;
4080a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
4081a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
4082a1cf66bbSMatthew G. Knepley 
4083a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
4084a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
4085a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
4086a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
4087a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
4088a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
4089a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
4090a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
4091a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
4092a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
4093a1cf66bbSMatthew G. Knepley   } else {
4094a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
4095a1cf66bbSMatthew G. Knepley     numChunks = 1;
4096a1cf66bbSMatthew G. Knepley   }
4097a1cf66bbSMatthew G. Knepley   /* Get work space */
4098a1cf66bbSMatthew 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;
4099a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
4100580bdb30SBarry Smith   ierr = PetscArrayzero(work, wsz);CHKERRQ(ierr);
4101a1cf66bbSMatthew G. Knepley   off      = 0;
4102a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
4103a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
4104a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
4105a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
4106a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
4107a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
4108a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
4109a1cf66bbSMatthew G. Knepley   /* Setup geometry */
4110a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
41119b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
41129b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
4113a1cf66bbSMatthew G. Knepley   if (!qGeom) {
4114a1cf66bbSMatthew G. Knepley     PetscFE fe;
4115a1cf66bbSMatthew G. Knepley 
4116a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
4117a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
4118a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
4119a1cf66bbSMatthew G. Knepley   }
4120a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
4121a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
4122a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
4123a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
41246528b96dSMatthew G. Knepley   key.label = NULL;
41256528b96dSMatthew G. Knepley   key.value = 0;
412606ad1575SMatthew G. Knepley   key.part  = 0;
4127a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
4128a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
4129a1cf66bbSMatthew G. Knepley     PetscInt         c;
4130a1cf66bbSMatthew G. Knepley 
4131a1cf66bbSMatthew G. Knepley     /* Extract values */
4132a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
4133a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
4134a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
4135a1cf66bbSMatthew G. Knepley       PetscInt       i;
4136a1cf66bbSMatthew G. Knepley 
4137a1cf66bbSMatthew G. Knepley       if (X) {
4138a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
4139a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
4140a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
4141a1cf66bbSMatthew G. Knepley       }
4142a1cf66bbSMatthew G. Knepley       if (X_t) {
4143a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
4144a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
4145a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
4146a1cf66bbSMatthew G. Knepley       }
4147a1cf66bbSMatthew G. Knepley       if (dmAux) {
4148a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
4149a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
4150a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
4151a1cf66bbSMatthew G. Knepley       }
4152a1cf66bbSMatthew G. Knepley     }
4153a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
4154a1cf66bbSMatthew G. Knepley       PetscFE fe;
4155a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
4156a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
41576528b96dSMatthew G. Knepley         key.field = fieldI*Nf + fieldJ;
41586528b96dSMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN,     key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
41596528b96dSMatthew G. Knepley         if (hasPrec) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);}
41606528b96dSMatthew G. Knepley         if (hasDyn)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);}
4161a1cf66bbSMatthew G. Knepley       }
4162a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
4163a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
4164a1cf66bbSMatthew G. Knepley         PetscFV  fv;
4165a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
4166a1cf66bbSMatthew G. Knepley 
4167a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
4168a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
4169a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
4170a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
4171a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
4172a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
4173a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
4174a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
4175a1cf66bbSMatthew G. Knepley           }
4176a1cf66bbSMatthew G. Knepley         }
4177a1cf66bbSMatthew G. Knepley       }
4178a1cf66bbSMatthew G. Knepley     }
4179a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
4180a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
4181a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
4182a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
4183a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
4184a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
4185a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4186a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4187a1cf66bbSMatthew G. Knepley       }
4188a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
4189a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
4190a1cf66bbSMatthew G. Knepley     }
4191a1cf66bbSMatthew G. Knepley   }
4192a1cf66bbSMatthew G. Knepley   /* Cleanup */
4193a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
4194a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
4195a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
4196a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
4197a1cf66bbSMatthew 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);
4198a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
4199a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
4200a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
4201a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
4202a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4203a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
4204a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
4205a1cf66bbSMatthew G. Knepley }
42063e9753d6SMatthew G. Knepley 
42073e9753d6SMatthew G. Knepley /******** FEM Assembly Function ********/
42083e9753d6SMatthew G. Knepley 
42093e9753d6SMatthew G. Knepley static PetscErrorCode DMConvertPlex_Internal(DM dm, DM *plex, PetscBool copy)
42103e9753d6SMatthew G. Knepley {
42113e9753d6SMatthew G. Knepley   PetscBool      isPlex;
42123e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
42133e9753d6SMatthew G. Knepley 
42143e9753d6SMatthew G. Knepley   PetscFunctionBegin;
42153e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
42163e9753d6SMatthew G. Knepley   if (isPlex) {
42173e9753d6SMatthew G. Knepley     *plex = dm;
42183e9753d6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
42193e9753d6SMatthew G. Knepley   } else {
42203e9753d6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
42213e9753d6SMatthew G. Knepley     if (!*plex) {
42223e9753d6SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
42233e9753d6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
42243e9753d6SMatthew G. Knepley       if (copy) {
42259a2a23afSMatthew G. Knepley         ierr = DMCopyAuxiliaryVec(dm, *plex);CHKERRQ(ierr);
42263e9753d6SMatthew G. Knepley       }
42273e9753d6SMatthew G. Knepley     } else {
42283e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
42293e9753d6SMatthew G. Knepley     }
42303e9753d6SMatthew G. Knepley   }
42313e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
42323e9753d6SMatthew G. Knepley }
42333e9753d6SMatthew G. Knepley 
42343e9753d6SMatthew G. Knepley /*@
42353e9753d6SMatthew G. Knepley   DMPlexGetGeometryFVM - Return precomputed geometric data
42363e9753d6SMatthew G. Knepley 
42373e9753d6SMatthew G. Knepley   Collective on DM
42383e9753d6SMatthew G. Knepley 
42393e9753d6SMatthew G. Knepley   Input Parameter:
42403e9753d6SMatthew G. Knepley . dm - The DM
42413e9753d6SMatthew G. Knepley 
42423e9753d6SMatthew G. Knepley   Output Parameters:
42433e9753d6SMatthew G. Knepley + facegeom - The values precomputed from face geometry
42443e9753d6SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
42453e9753d6SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
42463e9753d6SMatthew G. Knepley 
42473e9753d6SMatthew G. Knepley   Level: developer
42483e9753d6SMatthew G. Knepley 
4249446c23c1SPierre Jolivet .seealso: DMTSSetRHSFunctionLocal()
42503e9753d6SMatthew G. Knepley @*/
42513e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
42523e9753d6SMatthew G. Knepley {
42533e9753d6SMatthew G. Knepley   DM             plex;
42543e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
42553e9753d6SMatthew G. Knepley 
42563e9753d6SMatthew G. Knepley   PetscFunctionBegin;
42573e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42583e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
42593e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
42603e9753d6SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
42613e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
42623e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
42633e9753d6SMatthew G. Knepley }
42643e9753d6SMatthew G. Knepley 
42653e9753d6SMatthew G. Knepley /*@
42663e9753d6SMatthew G. Knepley   DMPlexGetGradientDM - Return gradient data layout
42673e9753d6SMatthew G. Knepley 
42683e9753d6SMatthew G. Knepley   Collective on DM
42693e9753d6SMatthew G. Knepley 
42703e9753d6SMatthew G. Knepley   Input Parameters:
42713e9753d6SMatthew G. Knepley + dm - The DM
42723e9753d6SMatthew G. Knepley - fv - The PetscFV
42733e9753d6SMatthew G. Knepley 
42743e9753d6SMatthew G. Knepley   Output Parameter:
42753e9753d6SMatthew G. Knepley . dmGrad - The layout for gradient values
42763e9753d6SMatthew G. Knepley 
42773e9753d6SMatthew G. Knepley   Level: developer
42783e9753d6SMatthew G. Knepley 
4279446c23c1SPierre Jolivet .seealso: DMPlexGetGeometryFVM()
42803e9753d6SMatthew G. Knepley @*/
42813e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
42823e9753d6SMatthew G. Knepley {
42833e9753d6SMatthew G. Knepley   DM             plex;
42843e9753d6SMatthew G. Knepley   PetscBool      computeGradients;
42853e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
42863e9753d6SMatthew G. Knepley 
42873e9753d6SMatthew G. Knepley   PetscFunctionBegin;
42883e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42893e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
42903e9753d6SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
42913e9753d6SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
42923e9753d6SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
42933e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
42943e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
42953e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
42963e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
42973e9753d6SMatthew G. Knepley }
42983e9753d6SMatthew G. Knepley 
42990c290148SMatthew G. Knepley static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, PetscWeakForm wf, PetscFormKey key, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
43003e9753d6SMatthew G. Knepley {
43013e9753d6SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
43023e9753d6SMatthew G. Knepley   DM               plex = NULL, plexA = NULL;
43033e9753d6SMatthew G. Knepley   DMEnclosureType  encAux;
43043e9753d6SMatthew G. Knepley   PetscDS          prob, probAux = NULL;
43053e9753d6SMatthew G. Knepley   PetscSection     section, sectionAux = NULL;
43063e9753d6SMatthew G. Knepley   Vec              locA = NULL;
43073e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
43083e9753d6SMatthew G. Knepley   PetscInt         totDim, totDimAux = 0;
43093e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
43103e9753d6SMatthew G. Knepley 
43113e9753d6SMatthew G. Knepley   PetscFunctionBegin;
43123e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
43133e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
43143e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
43153e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
43160c290148SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, &locA);CHKERRQ(ierr);
43173e9753d6SMatthew G. Knepley   if (locA) {
43183e9753d6SMatthew G. Knepley     DM dmAux;
43193e9753d6SMatthew G. Knepley 
43203e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
43213e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
43223e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
43233e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
43243e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
43253e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
43263e9753d6SMatthew G. Knepley   }
43270c290148SMatthew G. Knepley   {
43283e9753d6SMatthew G. Knepley     PetscFEGeom     *fgeom;
43293e9753d6SMatthew G. Knepley     PetscInt         maxDegree;
43303e9753d6SMatthew G. Knepley     PetscQuadrature  qGeom = NULL;
43313e9753d6SMatthew G. Knepley     IS               pointIS;
43323e9753d6SMatthew G. Knepley     const PetscInt  *points;
43333e9753d6SMatthew G. Knepley     PetscInt         numFaces, face, Nq;
43343e9753d6SMatthew G. Knepley 
43350c290148SMatthew G. Knepley     ierr = DMLabelGetStratumIS(key.label, key.value, &pointIS);CHKERRQ(ierr);
43360c290148SMatthew G. Knepley     if (!pointIS) goto end; /* No points with that id on this process */
43373e9753d6SMatthew G. Knepley     {
43383e9753d6SMatthew G. Knepley       IS isectIS;
43393e9753d6SMatthew G. Knepley 
43403e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
43413e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
43423e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
43433e9753d6SMatthew G. Knepley       pointIS = isectIS;
43443e9753d6SMatthew G. Knepley     }
43453e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr);
43463e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
43473e9753d6SMatthew G. Knepley     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
43483e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
43493e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
43503e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
43513e9753d6SMatthew G. Knepley     }
43523e9753d6SMatthew G. Knepley     if (!qGeom) {
43533e9753d6SMatthew G. Knepley       PetscFE fe;
43543e9753d6SMatthew G. Knepley 
43550c290148SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, key.field, (PetscObject *) &fe);CHKERRQ(ierr);
43563e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
43573e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
43583e9753d6SMatthew G. Knepley     }
43593e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
43603e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
43613e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
4362f15274beSMatthew Knepley       const PetscInt point = points[face], *support;
43633e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
4364f15274beSMatthew Knepley       PetscInt       i;
43653e9753d6SMatthew G. Knepley 
43663e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
43673e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
43683e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
43693e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
43703e9753d6SMatthew G. Knepley       if (locX_t) {
43713e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
43723e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
43733e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
43743e9753d6SMatthew G. Knepley       }
43753e9753d6SMatthew G. Knepley       if (locA) {
43763e9753d6SMatthew G. Knepley         PetscInt subp;
43773e9753d6SMatthew G. Knepley 
43783e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
43793e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
43803e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
43813e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
43823e9753d6SMatthew G. Knepley       }
43833e9753d6SMatthew G. Knepley     }
43843e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemVec, numFaces*totDim);CHKERRQ(ierr);
43853e9753d6SMatthew G. Knepley     {
43863e9753d6SMatthew G. Knepley       PetscFE         fe;
43873e9753d6SMatthew G. Knepley       PetscInt        Nb;
43883e9753d6SMatthew G. Knepley       PetscFEGeom     *chunkGeom = NULL;
43893e9753d6SMatthew G. Knepley       /* Conforming batches */
43903e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
43913e9753d6SMatthew G. Knepley       /* Remainder */
43923e9753d6SMatthew G. Knepley       PetscInt        Nr, offset;
43933e9753d6SMatthew G. Knepley 
43940c290148SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, key.field, (PetscObject *) &fe);CHKERRQ(ierr);
43953e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
43963e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
43973e9753d6SMatthew G. Knepley       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
43983e9753d6SMatthew G. Knepley       blockSize = Nb;
43993e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
44003e9753d6SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
44013e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
44023e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
44033e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
44043e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
44053e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
440645480ffeSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, wf, key, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
44073e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
44083e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
440945480ffeSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, wf, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
44103e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
44113e9753d6SMatthew G. Knepley     }
44123e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
44133e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
44143e9753d6SMatthew G. Knepley 
44153e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
44163e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
44173e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
44183e9753d6SMatthew G. Knepley     }
44193e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
44203e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
44213e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
44223e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
44233e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr);
44243e9753d6SMatthew G. Knepley   }
44250c290148SMatthew G. Knepley   end:
44263e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
44273e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
44283e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
44293e9753d6SMatthew G. Knepley }
44303e9753d6SMatthew G. Knepley 
44310c290148SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, PetscWeakForm wf, PetscFormKey key, Vec locX, Vec locX_t, Vec locF)
44323e9753d6SMatthew G. Knepley {
44333e9753d6SMatthew G. Knepley   DMField        coordField;
44343e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
44353e9753d6SMatthew G. Knepley   IS             facetIS;
44363e9753d6SMatthew G. Knepley   PetscInt       dim;
44373e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
44383e9753d6SMatthew G. Knepley 
44393e9753d6SMatthew G. Knepley   PetscFunctionBegin;
44403e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44413e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
44423e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
44433e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
44440c290148SMatthew G. Knepley   ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, wf, key, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
44453e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
44463e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
44473e9753d6SMatthew G. Knepley }
44483e9753d6SMatthew G. Knepley 
44493e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
44503e9753d6SMatthew G. Knepley {
44513e9753d6SMatthew G. Knepley   PetscDS        prob;
44523e9753d6SMatthew G. Knepley   PetscInt       numBd, bd;
44533e9753d6SMatthew G. Knepley   DMField        coordField = NULL;
44543e9753d6SMatthew G. Knepley   IS             facetIS    = NULL;
44553e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
44563e9753d6SMatthew G. Knepley   PetscInt       dim;
44573e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
44583e9753d6SMatthew G. Knepley 
44593e9753d6SMatthew G. Knepley   PetscFunctionBegin;
44603e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
44613e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
44623e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44633e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr);
44643e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
44653e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
446645480ffeSMatthew G. Knepley     PetscWeakForm           wf;
44673e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
44683e9753d6SMatthew G. Knepley     DMLabel                 label;
44693e9753d6SMatthew G. Knepley     const PetscInt         *values;
44700c290148SMatthew G. Knepley     PetscInt                field, numValues, v;
44713e9753d6SMatthew G. Knepley     PetscObject             obj;
44723e9753d6SMatthew G. Knepley     PetscClassId            id;
44730c290148SMatthew G. Knepley     PetscFormKey            key;
44743e9753d6SMatthew G. Knepley 
447545480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &wf, &type, NULL, &label, &numValues, &values, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
44763e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
44773e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
44783e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
44793e9753d6SMatthew G. Knepley     if (!facetIS) {
44803e9753d6SMatthew G. Knepley       DMLabel  depthLabel;
44813e9753d6SMatthew G. Knepley       PetscInt dim;
44823e9753d6SMatthew G. Knepley 
44833e9753d6SMatthew G. Knepley       ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
44843e9753d6SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44853e9753d6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr);
44863e9753d6SMatthew G. Knepley     }
44873e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
44880c290148SMatthew G. Knepley     for (v = 0; v < numValues; ++v) {
44890c290148SMatthew G. Knepley       key.label = label;
44900c290148SMatthew G. Knepley       key.value = values[v];
44910c290148SMatthew G. Knepley       key.field = field;
44920c290148SMatthew G. Knepley       key.part  = 0;
44930c290148SMatthew G. Knepley       ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, wf, key, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
44940c290148SMatthew G. Knepley     }
44953e9753d6SMatthew G. Knepley   }
44963e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
44973e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
44983e9753d6SMatthew G. Knepley }
44993e9753d6SMatthew G. Knepley 
450006ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
45013e9753d6SMatthew G. Knepley {
45023e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
45033e9753d6SMatthew G. Knepley   const char      *name       = "Residual";
45043e9753d6SMatthew G. Knepley   DM               dmAux      = NULL;
45053e9753d6SMatthew G. Knepley   DM               dmGrad     = NULL;
45063e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
45076528b96dSMatthew G. Knepley   PetscDS          ds         = NULL;
45086528b96dSMatthew G. Knepley   PetscDS          dsAux      = NULL;
45093e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
45103e9753d6SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
45113e9753d6SMatthew G. Knepley   PetscBool        useFVM     = PETSC_FALSE;
45123e9753d6SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
45133e9753d6SMatthew G. Knepley   PetscFV          fvm        = NULL;
45143e9753d6SMatthew G. Knepley   PetscFVCellGeom *cgeomFVM   = NULL;
45153e9753d6SMatthew G. Knepley   PetscFVFaceGeom *fgeomFVM   = NULL;
45163e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
45173e9753d6SMatthew G. Knepley   Vec              locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
45183e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a, *uL, *uR;
45193e9753d6SMatthew G. Knepley   IS               chunkIS;
45203e9753d6SMatthew G. Knepley   const PetscInt  *cells;
45213e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
45223e9753d6SMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
45233e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
45243e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
45253e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
45263e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
45273e9753d6SMatthew G. Knepley 
45283e9753d6SMatthew G. Knepley   PetscFunctionBegin;
45293e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
45303e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
45313e9753d6SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
45323e9753d6SMatthew G. Knepley   /* FEM+FVM */
45333e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
45343e9753d6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
45353e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
45363e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
45373e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
45386528b96dSMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &ds);CHKERRQ(ierr);
45396528b96dSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
45406528b96dSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
454104c51a94SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, &locA);CHKERRQ(ierr);
45423e9753d6SMatthew G. Knepley   if (locA) {
45433e9753d6SMatthew G. Knepley     PetscInt subcell;
45443e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
45453e9753d6SMatthew G. Knepley     ierr = DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);CHKERRQ(ierr);
45466528b96dSMatthew G. Knepley     ierr = DMGetCellDS(dmAux, subcell, &dsAux);CHKERRQ(ierr);
45476528b96dSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux, &totDimAux);CHKERRQ(ierr);
45483e9753d6SMatthew G. Knepley   }
45493e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
45503e9753d6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
45513e9753d6SMatthew G. Knepley     PetscObject  obj;
45523e9753d6SMatthew G. Knepley     PetscClassId id;
45533e9753d6SMatthew G. Knepley     PetscBool    fimp;
45543e9753d6SMatthew G. Knepley 
45556528b96dSMatthew G. Knepley     ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
45563e9753d6SMatthew G. Knepley     if (isImplicit != fimp) continue;
45576528b96dSMatthew G. Knepley     ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
45583e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
45593e9753d6SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
45603e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
45613e9753d6SMatthew G. Knepley   }
45623e9753d6SMatthew G. Knepley   if (useFEM) {
45633e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
45643e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
45653e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
45663e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
45673e9753d6SMatthew G. Knepley       if (affineQuad) {
45683e9753d6SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
45693e9753d6SMatthew G. Knepley       }
45703e9753d6SMatthew G. Knepley     } else {
45713e9753d6SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
45723e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
45733e9753d6SMatthew G. Knepley         PetscObject  obj;
45743e9753d6SMatthew G. Knepley         PetscClassId id;
45753e9753d6SMatthew G. Knepley         PetscBool    fimp;
45763e9753d6SMatthew G. Knepley 
45776528b96dSMatthew G. Knepley         ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
45783e9753d6SMatthew G. Knepley         if (isImplicit != fimp) continue;
45796528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
45803e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
45813e9753d6SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
45823e9753d6SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
45833e9753d6SMatthew G. Knepley 
45843e9753d6SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
45853e9753d6SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
45863e9753d6SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
45873e9753d6SMatthew G. Knepley         }
45883e9753d6SMatthew G. Knepley       }
45893e9753d6SMatthew G. Knepley     }
45903e9753d6SMatthew G. Knepley   }
45913e9753d6SMatthew G. Knepley   if (useFVM) {
45923e9753d6SMatthew G. Knepley     ierr = DMPlexGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
45933e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
45943e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
45953e9753d6SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
45963e9753d6SMatthew G. Knepley     ierr = DMPlexGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
45973e9753d6SMatthew G. Knepley     if (dmGrad) {
45983e9753d6SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
45993e9753d6SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
46003e9753d6SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
46013e9753d6SMatthew G. Knepley       /* Communicate gradient values */
46023e9753d6SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
46033e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
46043e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
46053e9753d6SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
46063e9753d6SMatthew G. Knepley     }
46073e9753d6SMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
46083e9753d6SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
46093e9753d6SMatthew G. Knepley   }
46103e9753d6SMatthew G. Knepley   /* Loop over chunks */
46113e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
46123e9753d6SMatthew G. Knepley   numCells      = cEnd - cStart;
46133e9753d6SMatthew G. Knepley   numChunks     = 1;
46143e9753d6SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
46153e9753d6SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
46163e9753d6SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
46173e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
46183e9753d6SMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
46193e9753d6SMatthew G. Knepley     PetscReal       *vol;
46203e9753d6SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
46213e9753d6SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
46223e9753d6SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
46233e9753d6SMatthew G. Knepley 
46243e9753d6SMatthew G. Knepley     /* Extract field coefficients */
46253e9753d6SMatthew G. Knepley     if (useFEM) {
46263e9753d6SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
46273e9753d6SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
46283e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
46293e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
46303e9753d6SMatthew G. Knepley     }
46313e9753d6SMatthew G. Knepley     if (useFVM) {
46323e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
46333e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
46343e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
46353e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
46363e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxL, numFaces*totDim);CHKERRQ(ierr);
46373e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxR, numFaces*totDim);CHKERRQ(ierr);
46383e9753d6SMatthew G. Knepley     }
46393e9753d6SMatthew 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 */
46403e9753d6SMatthew G. Knepley     /* Loop over fields */
46413e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
46423e9753d6SMatthew G. Knepley       PetscObject  obj;
46433e9753d6SMatthew G. Knepley       PetscClassId id;
46443e9753d6SMatthew G. Knepley       PetscBool    fimp;
46453e9753d6SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
46463e9753d6SMatthew G. Knepley 
46476528b96dSMatthew G. Knepley       key.field = f;
46486528b96dSMatthew G. Knepley       ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
46493e9753d6SMatthew G. Knepley       if (isImplicit != fimp) continue;
46506528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
46513e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
46523e9753d6SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
46533e9753d6SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
46543e9753d6SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
46553e9753d6SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
46563e9753d6SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
46573e9753d6SMatthew G. Knepley         PetscInt        Nq, Nb;
46583e9753d6SMatthew G. Knepley 
46593e9753d6SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
46603e9753d6SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
46613e9753d6SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
46623e9753d6SMatthew G. Knepley         blockSize = Nb;
46633e9753d6SMatthew G. Knepley         batchSize = numBlocks * blockSize;
46643e9753d6SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
46653e9753d6SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
46663e9753d6SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
46673e9753d6SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
46683e9753d6SMatthew G. Knepley         offset    = numCells - Nr;
46693e9753d6SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
46703e9753d6SMatthew 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) */
46713e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
46726528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(ds, key, Ne, chunkGeom, u, u_t, dsAux, a, t, elemVec);CHKERRQ(ierr);
46733e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
46746528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(ds, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
46753e9753d6SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
46763e9753d6SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
46773e9753d6SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
46783e9753d6SMatthew G. Knepley 
46793e9753d6SMatthew G. Knepley         Ne = numFaces;
46803e9753d6SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
46813e9753d6SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
46826528b96dSMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, ds, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
46833e9753d6SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
46843e9753d6SMatthew G. Knepley     }
46853e9753d6SMatthew G. Knepley     /* Loop over domain */
46863e9753d6SMatthew G. Knepley     if (useFEM) {
46873e9753d6SMatthew G. Knepley       /* Add elemVec to locX */
46883e9753d6SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
46893e9753d6SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
46903e9753d6SMatthew G. Knepley         const PetscInt cind = c - cStart;
46913e9753d6SMatthew G. Knepley 
46923e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
46933e9753d6SMatthew G. Knepley         if (ghostLabel) {
46943e9753d6SMatthew G. Knepley           PetscInt ghostVal;
46953e9753d6SMatthew G. Knepley 
46963e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
46973e9753d6SMatthew G. Knepley           if (ghostVal > 0) continue;
46983e9753d6SMatthew G. Knepley         }
46993e9753d6SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
47003e9753d6SMatthew G. Knepley       }
47013e9753d6SMatthew G. Knepley     }
47023e9753d6SMatthew G. Knepley     if (useFVM) {
47033e9753d6SMatthew G. Knepley       PetscScalar *fa;
47043e9753d6SMatthew G. Knepley       PetscInt     iface;
47053e9753d6SMatthew G. Knepley 
47063e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
47073e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
47083e9753d6SMatthew G. Knepley         PetscFV      fv;
47093e9753d6SMatthew G. Knepley         PetscObject  obj;
47103e9753d6SMatthew G. Knepley         PetscClassId id;
47113e9753d6SMatthew G. Knepley         PetscInt     foff, pdim;
47123e9753d6SMatthew G. Knepley 
47136528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
47146528b96dSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(ds, f, &foff);CHKERRQ(ierr);
47153e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
47163e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
47173e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
47183e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
47193e9753d6SMatthew G. Knepley         /* Accumulate fluxes to cells */
47203e9753d6SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
47213e9753d6SMatthew G. Knepley           const PetscInt *scells;
47223e9753d6SMatthew G. Knepley           PetscScalar    *fL = NULL, *fR = NULL;
47233e9753d6SMatthew G. Knepley           PetscInt        ghost, d, nsupp, nchild;
47243e9753d6SMatthew G. Knepley 
47253e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
47263e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
47273e9753d6SMatthew G. Knepley           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
47283e9753d6SMatthew G. Knepley           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
47293e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr);
47303e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr);
47313e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);}
47323e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr);
47333e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);}
47343e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
47353e9753d6SMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
47363e9753d6SMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
47373e9753d6SMatthew G. Knepley           }
47383e9753d6SMatthew G. Knepley           ++iface;
47393e9753d6SMatthew G. Knepley         }
47403e9753d6SMatthew G. Knepley       }
47413e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
47423e9753d6SMatthew G. Knepley     }
47433e9753d6SMatthew G. Knepley     /* Handle time derivative */
47443e9753d6SMatthew G. Knepley     if (locX_t) {
47453e9753d6SMatthew G. Knepley       PetscScalar *x_t, *fa;
47463e9753d6SMatthew G. Knepley 
47473e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
47483e9753d6SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
47493e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
47503e9753d6SMatthew G. Knepley         PetscFV      fv;
47513e9753d6SMatthew G. Knepley         PetscObject  obj;
47523e9753d6SMatthew G. Knepley         PetscClassId id;
47533e9753d6SMatthew G. Knepley         PetscInt     pdim, d;
47543e9753d6SMatthew G. Knepley 
47556528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
47563e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
47573e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
47583e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
47593e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
47603e9753d6SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
47613e9753d6SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
47623e9753d6SMatthew G. Knepley           PetscScalar   *u_t, *r;
47633e9753d6SMatthew G. Knepley 
47643e9753d6SMatthew G. Knepley           if (ghostLabel) {
47653e9753d6SMatthew G. Knepley             PetscInt ghostVal;
47663e9753d6SMatthew G. Knepley 
47673e9753d6SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
47683e9753d6SMatthew G. Knepley             if (ghostVal > 0) continue;
47693e9753d6SMatthew G. Knepley           }
47703e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
47713e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
47723e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
47733e9753d6SMatthew G. Knepley         }
47743e9753d6SMatthew G. Knepley       }
47753e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
47763e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
47773e9753d6SMatthew G. Knepley     }
47783e9753d6SMatthew G. Knepley     if (useFEM) {
47793e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
47803e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
47813e9753d6SMatthew G. Knepley     }
47823e9753d6SMatthew G. Knepley     if (useFVM) {
47833e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
47843e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
47853e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
47863e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
47873e9753d6SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
47883e9753d6SMatthew G. Knepley     }
47893e9753d6SMatthew G. Knepley   }
47903e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
47913e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
47923e9753d6SMatthew G. Knepley 
47933e9753d6SMatthew G. Knepley   if (useFEM) {
47943e9753d6SMatthew G. Knepley     ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);
47953e9753d6SMatthew G. Knepley 
47963e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
47973e9753d6SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
47983e9753d6SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
47993e9753d6SMatthew G. Knepley     } else {
48003e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
48013e9753d6SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
48023e9753d6SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
48033e9753d6SMatthew G. Knepley       }
48043e9753d6SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
48053e9753d6SMatthew G. Knepley     }
48063e9753d6SMatthew G. Knepley   }
48073e9753d6SMatthew G. Knepley 
48083e9753d6SMatthew G. Knepley   /* FEM */
48093e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
48103e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
48113e9753d6SMatthew G. Knepley   /* 3: Handle boundary values */
48123e9753d6SMatthew G. Knepley   /* 4: Loop over domain */
48133e9753d6SMatthew G. Knepley   /*   Extract coefficients */
48143e9753d6SMatthew G. Knepley   /* Loop over fields */
48153e9753d6SMatthew G. Knepley   /*   Set tiling for FE*/
48163e9753d6SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
48173e9753d6SMatthew G. Knepley   /*     Loop over subdomain */
48183e9753d6SMatthew G. Knepley   /*       Loop over quad points */
48193e9753d6SMatthew G. Knepley   /*         Transform coords to real space */
48203e9753d6SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
48213e9753d6SMatthew G. Knepley   /*         Evaluate residual at point */
48223e9753d6SMatthew G. Knepley   /*         Transform residual to real space */
48233e9753d6SMatthew G. Knepley   /*       Add residual to elemVec */
48243e9753d6SMatthew G. Knepley   /* Loop over domain */
48253e9753d6SMatthew G. Knepley   /*   Add elemVec to locX */
48263e9753d6SMatthew G. Knepley 
48273e9753d6SMatthew G. Knepley   /* FVM */
48283e9753d6SMatthew G. Knepley   /* Get geometric data */
48293e9753d6SMatthew G. Knepley   /* If using gradients */
48303e9753d6SMatthew G. Knepley   /*   Compute gradient data */
48313e9753d6SMatthew G. Knepley   /*   Loop over domain faces */
48323e9753d6SMatthew G. Knepley   /*     Count computational faces */
48333e9753d6SMatthew G. Knepley   /*     Reconstruct cell gradient */
48343e9753d6SMatthew G. Knepley   /*   Loop over domain cells */
48353e9753d6SMatthew G. Knepley   /*     Limit cell gradients */
48363e9753d6SMatthew G. Knepley   /* Handle boundary values */
48373e9753d6SMatthew G. Knepley   /* Loop over domain faces */
48383e9753d6SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
48393e9753d6SMatthew G. Knepley   /* Riemann solve over faces */
48403e9753d6SMatthew G. Knepley   /* Loop over domain faces */
48413e9753d6SMatthew G. Knepley   /*   Accumulate fluxes to cells */
48423e9753d6SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
48433e9753d6SMatthew G. Knepley   if (mesh->printFEM) {
48443e9753d6SMatthew G. Knepley     Vec         locFbc;
48453e9753d6SMatthew G. Knepley     PetscInt    pStart, pEnd, p, maxDof;
48463e9753d6SMatthew G. Knepley     PetscScalar *zeroes;
48473e9753d6SMatthew G. Knepley 
48483e9753d6SMatthew G. Knepley     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
48493e9753d6SMatthew G. Knepley     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
48503e9753d6SMatthew G. Knepley     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
48513e9753d6SMatthew G. Knepley     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
48523e9753d6SMatthew G. Knepley     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
48533e9753d6SMatthew G. Knepley     for (p = pStart; p < pEnd; p++) {
48543e9753d6SMatthew G. Knepley       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
48553e9753d6SMatthew G. Knepley     }
48563e9753d6SMatthew G. Knepley     ierr = PetscFree(zeroes);CHKERRQ(ierr);
48573e9753d6SMatthew G. Knepley     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
48583e9753d6SMatthew G. Knepley     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
48593e9753d6SMatthew G. Knepley   }
48603e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
48613e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
48623e9753d6SMatthew G. Knepley }
48633e9753d6SMatthew G. Knepley 
48646528b96dSMatthew G. Knepley /*
48656528b96dSMatthew G. Knepley   1) Allow multiple kernels for BdResidual for hybrid DS
48666528b96dSMatthew G. Knepley 
48676528b96dSMatthew G. Knepley   DONE 2) Get out dsAux for either side at the same time as cohesive cell dsAux
48686528b96dSMatthew G. Knepley 
48696528b96dSMatthew G. Knepley   DONE 3) Change DMGetCellFields() to get different aux data a[] for each side
48706528b96dSMatthew G. Knepley      - I think I just need to replace a[] with the closure from each face
48716528b96dSMatthew G. Knepley 
48726528b96dSMatthew G. Knepley   4) Run both kernels for each non-hybrid field with correct dsAux, and then hybrid field as before
48736528b96dSMatthew G. Knepley */
487406ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, PetscFormKey key[], IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
48753e9753d6SMatthew G. Knepley {
48763e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
48773e9753d6SMatthew G. Knepley   const char      *name       = "Hybrid Residual";
487804c51a94SMatthew G. Knepley   DM               dmAux[3]   = {NULL, NULL, NULL};
48793e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
48806528b96dSMatthew G. Knepley   PetscDS          ds         = NULL;
48816528b96dSMatthew G. Knepley   PetscDS          dsAux[3]   = {NULL, NULL, NULL};
488204c51a94SMatthew G. Knepley   Vec              locA[3]    = {NULL, NULL, NULL};
48833e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
48843e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
48856528b96dSMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a[3];
48863e9753d6SMatthew G. Knepley   PetscScalar     *elemVec;
48873e9753d6SMatthew G. Knepley   IS               chunkIS;
48883e9753d6SMatthew G. Knepley   const PetscInt  *cells;
48893e9753d6SMatthew G. Knepley   PetscInt        *faces;
48903e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
48916528b96dSMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux[3], numChunks, cellChunkSize, chunk;
48923e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
48933e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
48943e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
48953e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
48963e9753d6SMatthew G. Knepley 
48973e9753d6SMatthew G. Knepley   PetscFunctionBegin;
4898*5fedec97SMatthew G. Knepley   if ((key[0].label == key[1].label) && (key[0].value == key[1].value) && (key[0].part == key[1].part)) {
4899*5fedec97SMatthew G. Knepley     const char *name;
4900*5fedec97SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) key[0].label, &name);CHKERRQ(ierr);
4901*5fedec97SMatthew G. Knepley     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Form keys for each side of a cohesive surface must be different (%s, %D, %D)", name, key[0].value, key[0].part);
4902*5fedec97SMatthew G. Knepley   }
49033e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
49043e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
49053e9753d6SMatthew G. Knepley   /* FEM */
4906148442b3SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
49073e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
49083e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
49093e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
49103e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
49116528b96dSMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &ds);CHKERRQ(ierr);
49126528b96dSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
49136528b96dSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
491404c51a94SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key[2].label, key[2].value, &locA[2]);CHKERRQ(ierr);
491504c51a94SMatthew G. Knepley   if (locA[2]) {
491604c51a94SMatthew G. Knepley     ierr = VecGetDM(locA[2], &dmAux[2]);CHKERRQ(ierr);
491704c51a94SMatthew G. Knepley     ierr = DMGetCellDS(dmAux[2], cStart, &dsAux[2]);CHKERRQ(ierr);
49186528b96dSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[2], &totDimAux[2]);CHKERRQ(ierr);
49196528b96dSMatthew G. Knepley     {
49206528b96dSMatthew G. Knepley       const PetscInt *cone;
49216528b96dSMatthew G. Knepley       PetscInt        c;
49226528b96dSMatthew G. Knepley 
49236528b96dSMatthew G. Knepley       ierr = DMPlexGetCone(dm, cStart, &cone);CHKERRQ(ierr);
49246528b96dSMatthew G. Knepley       for (c = 0; c < 2; ++c) {
49256528b96dSMatthew G. Knepley         const PetscInt *support;
49266528b96dSMatthew G. Knepley         PetscInt ssize, s;
49276528b96dSMatthew G. Knepley 
49286528b96dSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
49296528b96dSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, cone[c], &ssize);CHKERRQ(ierr);
49306528b96dSMatthew G. Knepley         if (ssize != 2) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[c], cStart, ssize);
49316528b96dSMatthew G. Knepley         if      (support[0] == cStart) s = 1;
49326528b96dSMatthew G. Knepley         else if (support[1] == cStart) s = 0;
49336528b96dSMatthew G. Knepley         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[c], cStart);
493404c51a94SMatthew G. Knepley         ierr = DMGetAuxiliaryVec(dm, key[c].label, key[c].value, &locA[c]);CHKERRQ(ierr);
493504c51a94SMatthew G. Knepley         if (locA[c]) {ierr = VecGetDM(locA[c], &dmAux[c]);CHKERRQ(ierr);}
493604c51a94SMatthew G. Knepley         else         {dmAux[c] = dmAux[2];}
493704c51a94SMatthew G. Knepley         ierr = DMGetCellDS(dmAux[c], support[s], &dsAux[c]);CHKERRQ(ierr);
49386528b96dSMatthew G. Knepley         ierr = PetscDSGetTotalDimension(dsAux[c], &totDimAux[c]);CHKERRQ(ierr);
49396528b96dSMatthew G. Knepley       }
49406528b96dSMatthew G. Knepley     }
49413e9753d6SMatthew G. Knepley   }
49423e9753d6SMatthew G. Knepley   /* 2: Setup geometric data */
49433e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
49443e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
49453e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
49463e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf, &quads, Nf, &geoms);CHKERRQ(ierr);
49473e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
49483e9753d6SMatthew G. Knepley       PetscFE fe;
49493e9753d6SMatthew G. Knepley 
49506528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
49513e9753d6SMatthew G. Knepley       if (fe) {
49523e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
49533e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
49543e9753d6SMatthew G. Knepley       }
49553e9753d6SMatthew G. Knepley     }
49563e9753d6SMatthew G. Knepley   }
49573e9753d6SMatthew G. Knepley   /* Loop over chunks */
49583e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
49593e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
49603e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
49613e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
49623e9753d6SMatthew G. Knepley   /* Extract field coefficients */
49633e9753d6SMatthew G. Knepley   /* NOTE This needs the end cap faces to have identical orientations */
496404c51a94SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
4965148442b3SMatthew G. Knepley   ierr = DMPlexGetHybridAuxFields(dm, dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
49663e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
49673e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
49683e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
49693e9753d6SMatthew G. Knepley 
49703e9753d6SMatthew G. Knepley     ierr = PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
49713e9753d6SMatthew G. Knepley     /* Get faces */
49723e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
49733e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
49743e9753d6SMatthew G. Knepley       const PetscInt *cone;
49753e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
49763e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
49773e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
49783e9753d6SMatthew G. Knepley     }
49793e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
49803e9753d6SMatthew G. Knepley     /* Get geometric data */
49813e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
49823e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
49833e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
49843e9753d6SMatthew G. Knepley     } else {
49853e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
49863e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
49873e9753d6SMatthew G. Knepley       }
49883e9753d6SMatthew G. Knepley     }
49893e9753d6SMatthew G. Knepley     /* Loop over fields */
49903e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
49913e9753d6SMatthew G. Knepley       PetscFE         fe;
49923e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
4993148442b3SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
49943e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
49953e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
4996*5fedec97SMatthew G. Knepley       PetscBool       isCohesiveField;
49973e9753d6SMatthew G. Knepley 
49986528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
49993e9753d6SMatthew G. Knepley       if (!fe) continue;
50003e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
50013e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
50023e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
50033e9753d6SMatthew G. Knepley       blockSize = Nb;
50043e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
50053e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
50063e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
50073e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
50083e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
50093e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
5010148442b3SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
5011148442b3SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
5012*5fedec97SMatthew G. Knepley       ierr = PetscDSGetCohesive(ds, f, &isCohesiveField);CHKERRQ(ierr);
5013*5fedec97SMatthew G. Knepley       chunkGeom->isCohesive = remGeom->isCohesive = PETSC_TRUE;
50146528b96dSMatthew G. Knepley       key[0].field = f;
50156528b96dSMatthew G. Knepley       key[1].field = f;
5016*5fedec97SMatthew G. Knepley       key[2].field = f;
5017c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, elemVec);CHKERRQ(ierr);
5018c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[0], 0, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
5019*5fedec97SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, elemVec);CHKERRQ(ierr);
5020c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[1], 1, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
5021*5fedec97SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[2], 0, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, elemVec);CHKERRQ(ierr);
5022*5fedec97SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[2], 0, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
5023148442b3SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
5024148442b3SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
50253e9753d6SMatthew G. Knepley     }
50263e9753d6SMatthew G. Knepley     /* Add elemVec to locX */
50273e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
50283e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
50293e9753d6SMatthew G. Knepley       const PetscInt cind = c - cStart;
50303e9753d6SMatthew G. Knepley 
50313e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
50323e9753d6SMatthew G. Knepley       if (ghostLabel) {
50333e9753d6SMatthew G. Knepley         PetscInt ghostVal;
50343e9753d6SMatthew G. Knepley 
50353e9753d6SMatthew G. Knepley         ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
50363e9753d6SMatthew G. Knepley         if (ghostVal > 0) continue;
50373e9753d6SMatthew G. Knepley       }
50383e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
50393e9753d6SMatthew G. Knepley     }
50403e9753d6SMatthew G. Knepley   }
504104c51a94SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
504204c51a94SMatthew G. Knepley   ierr = DMPlexRestoreHybridAuxFields(dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
50433e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
50443e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
50453e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
50463e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
50473e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
50483e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
50493e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
50503e9753d6SMatthew G. Knepley   } else {
50513e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
50523e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);}
50533e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
50543e9753d6SMatthew G. Knepley     }
50553e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
50563e9753d6SMatthew G. Knepley   }
50573e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
50583e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
50593e9753d6SMatthew G. Knepley }
50603e9753d6SMatthew G. Knepley 
506145480ffeSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, PetscWeakForm wf, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS)
50623e9753d6SMatthew G. Knepley {
50633e9753d6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
50643e9753d6SMatthew G. Knepley   DM              plex = NULL, plexA = NULL, tdm;
50653e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
50663e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
50673e9753d6SMatthew G. Knepley   PetscSection    section, sectionAux = NULL;
50683e9753d6SMatthew G. Knepley   PetscSection    globalSection, subSection = NULL;
50693e9753d6SMatthew G. Knepley   Vec             locA = NULL, tv;
50703e9753d6SMatthew G. Knepley   PetscScalar    *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
50713e9753d6SMatthew G. Knepley   PetscInt        v;
50723e9753d6SMatthew G. Knepley   PetscInt        Nf, totDim, totDimAux = 0;
50733e9753d6SMatthew G. Knepley   PetscBool       isMatISP, transform;
50743e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
50753e9753d6SMatthew G. Knepley 
50763e9753d6SMatthew G. Knepley   PetscFunctionBegin;
50773e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
50783e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
50793e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
50803e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
50813e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
50823e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
50833e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
50843e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
508504c51a94SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, label, values[0], &locA);CHKERRQ(ierr);
50863e9753d6SMatthew G. Knepley   if (locA) {
50873e9753d6SMatthew G. Knepley     DM dmAux;
50883e9753d6SMatthew G. Knepley 
50893e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
50903e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
50913e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
50923e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
50933e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
50943e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
50953e9753d6SMatthew G. Knepley   }
50963e9753d6SMatthew G. Knepley 
50973e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
50983e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
50993e9753d6SMatthew G. Knepley   if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);}
51003e9753d6SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
51013e9753d6SMatthew G. Knepley     PetscFEGeom     *fgeom;
51023e9753d6SMatthew G. Knepley     PetscInt         maxDegree;
51033e9753d6SMatthew G. Knepley     PetscQuadrature  qGeom = NULL;
51043e9753d6SMatthew G. Knepley     IS               pointIS;
51053e9753d6SMatthew G. Knepley     const PetscInt  *points;
510606ad1575SMatthew G. Knepley     PetscFormKey key;
51073e9753d6SMatthew G. Knepley     PetscInt         numFaces, face, Nq;
51083e9753d6SMatthew G. Knepley 
510945480ffeSMatthew G. Knepley     key.label = label;
511045480ffeSMatthew G. Knepley     key.value = values[v];
511106ad1575SMatthew G. Knepley     key.part  = 0;
51123e9753d6SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
51133e9753d6SMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
51143e9753d6SMatthew G. Knepley     {
51153e9753d6SMatthew G. Knepley       IS isectIS;
51163e9753d6SMatthew G. Knepley 
51173e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
51183e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
51193e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
51203e9753d6SMatthew G. Knepley       pointIS = isectIS;
51213e9753d6SMatthew G. Knepley     }
51223e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
51233e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
51243e9753d6SMatthew G. Knepley     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
51253e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
51263e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
51273e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
51283e9753d6SMatthew G. Knepley     }
51293e9753d6SMatthew G. Knepley     if (!qGeom) {
51303e9753d6SMatthew G. Knepley       PetscFE fe;
51313e9753d6SMatthew G. Knepley 
51323e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
51333e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
51343e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
51353e9753d6SMatthew G. Knepley     }
51363e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
51373e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
51383e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
5139f15274beSMatthew Knepley       const PetscInt point = points[face], *support;
51403e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
5141f15274beSMatthew Knepley       PetscInt       i;
51423e9753d6SMatthew G. Knepley 
51433e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
51443e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
51453e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
51463e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
51473e9753d6SMatthew G. Knepley       if (locX_t) {
51483e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
51493e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
51503e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
51513e9753d6SMatthew G. Knepley       }
51523e9753d6SMatthew G. Knepley       if (locA) {
51533e9753d6SMatthew G. Knepley         PetscInt subp;
51543e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
51553e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
51563e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
51573e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
51583e9753d6SMatthew G. Knepley       }
51593e9753d6SMatthew G. Knepley     }
51603e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemMat, numFaces*totDim*totDim);CHKERRQ(ierr);
51613e9753d6SMatthew G. Knepley     {
51623e9753d6SMatthew G. Knepley       PetscFE         fe;
51633e9753d6SMatthew G. Knepley       PetscInt        Nb;
51643e9753d6SMatthew G. Knepley       /* Conforming batches */
51653e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
51663e9753d6SMatthew G. Knepley       /* Remainder */
51673e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL;
51683e9753d6SMatthew G. Knepley       PetscInt        fieldJ, Nr, offset;
51693e9753d6SMatthew G. Knepley 
51703e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
51713e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
51723e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
51733e9753d6SMatthew G. Knepley       blockSize = Nb;
51743e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
51753e9753d6SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
51763e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
51773e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
51783e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
51793e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
51803e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
51813e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
518245480ffeSMatthew G. Knepley         key.field = fieldI*Nf+fieldJ;
518345480ffeSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, wf, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
51843e9753d6SMatthew G. Knepley       }
51853e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
51863e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
518745480ffeSMatthew G. Knepley         key.field = fieldI*Nf+fieldJ;
518845480ffeSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, wf, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
51893e9753d6SMatthew G. Knepley       }
51903e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
51913e9753d6SMatthew G. Knepley     }
51923e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
51933e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
51943e9753d6SMatthew G. Knepley 
51953e9753d6SMatthew G. Knepley       /* Transform to global basis before insertion in Jacobian */
51963e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
51973e9753d6SMatthew G. Knepley       if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
51983e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
51993e9753d6SMatthew G. Knepley       if (!isMatISP) {
52003e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52013e9753d6SMatthew G. Knepley       } else {
52023e9753d6SMatthew G. Knepley         Mat lJ;
52033e9753d6SMatthew G. Knepley 
52043e9753d6SMatthew G. Knepley         ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr);
52053e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52063e9753d6SMatthew G. Knepley       }
52073e9753d6SMatthew G. Knepley     }
52083e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
52093e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
52103e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
52113e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
52123e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr);
52133e9753d6SMatthew G. Knepley   }
52143e9753d6SMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
52153e9753d6SMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
52163e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
52173e9753d6SMatthew G. Knepley }
52183e9753d6SMatthew G. Knepley 
521945480ffeSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, PetscWeakForm wf, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP)
52203e9753d6SMatthew G. Knepley {
52213e9753d6SMatthew G. Knepley   DMField        coordField;
52223e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
52233e9753d6SMatthew G. Knepley   IS             facetIS;
52243e9753d6SMatthew G. Knepley   PetscInt       dim;
52253e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
52263e9753d6SMatthew G. Knepley 
52273e9753d6SMatthew G. Knepley   PetscFunctionBegin;
52283e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
52293e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
52303e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
52313e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
523245480ffeSMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, wf, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
52333e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
52343e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
52353e9753d6SMatthew G. Knepley }
52363e9753d6SMatthew G. Knepley 
52373e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
52383e9753d6SMatthew G. Knepley {
52393e9753d6SMatthew G. Knepley   PetscDS          prob;
52403e9753d6SMatthew G. Knepley   PetscInt         dim, numBd, bd;
52413e9753d6SMatthew G. Knepley   DMLabel          depthLabel;
52423e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
52433e9753d6SMatthew G. Knepley   IS               facetIS;
52443e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
52453e9753d6SMatthew G. Knepley 
52463e9753d6SMatthew G. Knepley   PetscFunctionBegin;
52473e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
52483e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
52493e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
52503e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
52513e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
52523e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
52533e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
525445480ffeSMatthew G. Knepley     PetscWeakForm           wf;
52553e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
52563e9753d6SMatthew G. Knepley     DMLabel                 label;
52573e9753d6SMatthew G. Knepley     const PetscInt         *values;
52583e9753d6SMatthew G. Knepley     PetscInt                fieldI, numValues;
52593e9753d6SMatthew G. Knepley     PetscObject             obj;
52603e9753d6SMatthew G. Knepley     PetscClassId            id;
52613e9753d6SMatthew G. Knepley 
526245480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &wf, &type, NULL, &label, &numValues, &values, &fieldI, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
52633e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
52643e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
52653e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
526645480ffeSMatthew G. Knepley     ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, wf, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
52673e9753d6SMatthew G. Knepley   }
52683e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
52693e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
52703e9753d6SMatthew G. Knepley }
52713e9753d6SMatthew G. Knepley 
527206ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
52733e9753d6SMatthew G. Knepley {
52743e9753d6SMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *) dm->data;
52753e9753d6SMatthew G. Knepley   const char     *name  = "Jacobian";
52769a2a23afSMatthew G. Knepley   DM              dmAux = NULL, plex, tdm;
52773e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
52783e9753d6SMatthew G. Knepley   Vec             A, tv;
52793e9753d6SMatthew G. Knepley   DMField         coordField;
52803e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
52813e9753d6SMatthew G. Knepley   PetscSection    section, globalSection, subSection, sectionAux;
52823e9753d6SMatthew G. Knepley   PetscScalar    *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
52833e9753d6SMatthew G. Knepley   const PetscInt *cells;
52843e9753d6SMatthew G. Knepley   PetscInt        Nf, fieldI, fieldJ;
52853e9753d6SMatthew G. Knepley   PetscInt        totDim, totDimAux, cStart, cEnd, numCells, c;
52863e9753d6SMatthew G. Knepley   PetscBool       isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform;
52873e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
52883e9753d6SMatthew G. Knepley 
52893e9753d6SMatthew G. Knepley   PetscFunctionBegin;
52903e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
52913e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
52923e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
52933e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
52943e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
52953e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
52963e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
52973e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
52983e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
52993e9753d6SMatthew G. Knepley   if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);}
53003e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
53013e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
53023e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
53033e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
53043e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
53053e9753d6SMatthew G. Knepley   /* user passed in the same matrix, avoid double contributions and
53063e9753d6SMatthew G. Knepley      only assemble the Jacobian */
53073e9753d6SMatthew G. Knepley   if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE;
53083e9753d6SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
53093e9753d6SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
531004c51a94SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, &A);CHKERRQ(ierr);
53119a2a23afSMatthew G. Knepley   if (A) {
53129a2a23afSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
53133e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
53143e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
53153e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plex, &sectionAux);CHKERRQ(ierr);
53163e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
53173e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
53183e9753d6SMatthew G. Knepley   }
53193e9753d6SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr);
53203e9753d6SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
53213e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
53223e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
53233e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
53243e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
53253e9753d6SMatthew G. Knepley     PetscScalar   *x = NULL,  *x_t = NULL;
53263e9753d6SMatthew G. Knepley     PetscInt       i;
53273e9753d6SMatthew G. Knepley 
53283e9753d6SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
53293e9753d6SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
53303e9753d6SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
53313e9753d6SMatthew G. Knepley     if (X_t) {
53323e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
53333e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
53343e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
53353e9753d6SMatthew G. Knepley     }
53363e9753d6SMatthew G. Knepley     if (dmAux) {
53373e9753d6SMatthew G. Knepley       PetscInt subcell;
53383e9753d6SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr);
53393e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
53403e9753d6SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
53413e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
53423e9753d6SMatthew G. Knepley     }
53433e9753d6SMatthew G. Knepley   }
53443e9753d6SMatthew G. Knepley   if (hasJac)  {ierr = PetscArrayzero(elemMat,  numCells*totDim*totDim);CHKERRQ(ierr);}
53453e9753d6SMatthew G. Knepley   if (hasPrec) {ierr = PetscArrayzero(elemMatP, numCells*totDim*totDim);CHKERRQ(ierr);}
53463e9753d6SMatthew G. Knepley   if (hasDyn)  {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);}
53473e9753d6SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
53483e9753d6SMatthew G. Knepley     PetscClassId    id;
53493e9753d6SMatthew G. Knepley     PetscFE         fe;
53503e9753d6SMatthew G. Knepley     PetscQuadrature qGeom = NULL;
53513e9753d6SMatthew G. Knepley     PetscInt        Nb;
53523e9753d6SMatthew G. Knepley     /* Conforming batches */
53533e9753d6SMatthew G. Knepley     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
53543e9753d6SMatthew G. Knepley     /* Remainder */
53553e9753d6SMatthew G. Knepley     PetscInt        Nr, offset, Nq;
53563e9753d6SMatthew G. Knepley     PetscInt        maxDegree;
53573e9753d6SMatthew G. Knepley     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
53583e9753d6SMatthew G. Knepley 
53593e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
53603e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
53613e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
53623e9753d6SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
53633e9753d6SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
53643e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
53653e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
53663e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
53673e9753d6SMatthew G. Knepley     }
53683e9753d6SMatthew G. Knepley     if (!qGeom) {
53693e9753d6SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
53703e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
53713e9753d6SMatthew G. Knepley     }
53723e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
53733e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
53743e9753d6SMatthew G. Knepley     blockSize = Nb;
53753e9753d6SMatthew G. Knepley     batchSize = numBlocks * blockSize;
53763e9753d6SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
53773e9753d6SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
53783e9753d6SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
53793e9753d6SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
53803e9753d6SMatthew G. Knepley     offset    = numCells - Nr;
53813e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
53823e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
53833e9753d6SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
53846528b96dSMatthew G. Knepley       key.field = fieldI*Nf+fieldJ;
53853e9753d6SMatthew G. Knepley       if (hasJac) {
53866528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
53876528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
53883e9753d6SMatthew G. Knepley       }
53893e9753d6SMatthew G. Knepley       if (hasPrec) {
53906528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
53916528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
53923e9753d6SMatthew G. Knepley       }
53933e9753d6SMatthew G. Knepley       if (hasDyn) {
53946528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
53956528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
53963e9753d6SMatthew G. Knepley       }
53973e9753d6SMatthew G. Knepley     }
53983e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
53993e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
54003e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
54013e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
54023e9753d6SMatthew G. Knepley   }
54033e9753d6SMatthew G. Knepley   /*   Add contribution from X_t */
54043e9753d6SMatthew G. Knepley   if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
54053e9753d6SMatthew G. Knepley   if (hasFV) {
54063e9753d6SMatthew G. Knepley     PetscClassId id;
54073e9753d6SMatthew G. Knepley     PetscFV      fv;
54083e9753d6SMatthew G. Knepley     PetscInt     offsetI, NcI, NbI = 1, fc, f;
54093e9753d6SMatthew G. Knepley 
54103e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
54113e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
54123e9753d6SMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
54133e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
54143e9753d6SMatthew G. Knepley       if (id != PETSCFV_CLASSID) continue;
54153e9753d6SMatthew G. Knepley       /* Put in the identity */
54163e9753d6SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
54173e9753d6SMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
54183e9753d6SMatthew G. Knepley         const PetscInt cind    = c - cStart;
54193e9753d6SMatthew G. Knepley         const PetscInt eOffset = cind*totDim*totDim;
54203e9753d6SMatthew G. Knepley         for (fc = 0; fc < NcI; ++fc) {
54213e9753d6SMatthew G. Knepley           for (f = 0; f < NbI; ++f) {
54223e9753d6SMatthew G. Knepley             const PetscInt i = offsetI + f*NcI+fc;
54233e9753d6SMatthew G. Knepley             if (hasPrec) {
54243e9753d6SMatthew G. Knepley               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
54253e9753d6SMatthew G. Knepley               elemMatP[eOffset+i*totDim+i] = 1.0;
54263e9753d6SMatthew G. Knepley             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
54273e9753d6SMatthew G. Knepley           }
54283e9753d6SMatthew G. Knepley         }
54293e9753d6SMatthew G. Knepley       }
54303e9753d6SMatthew G. Knepley     }
54313e9753d6SMatthew G. Knepley     /* No allocated space for FV stuff, so ignore the zero entries */
54323e9753d6SMatthew G. Knepley     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
54333e9753d6SMatthew G. Knepley   }
54343e9753d6SMatthew G. Knepley   /* Insert values into matrix */
54353e9753d6SMatthew G. Knepley   isMatIS = PETSC_FALSE;
54363e9753d6SMatthew G. Knepley   if (hasPrec && hasJac) {
54373e9753d6SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);
54383e9753d6SMatthew G. Knepley   }
54393e9753d6SMatthew G. Knepley   if (isMatIS && !subSection) {
54403e9753d6SMatthew G. Knepley     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
54413e9753d6SMatthew G. Knepley   }
54423e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
54433e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
54443e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
54453e9753d6SMatthew G. Knepley 
54463e9753d6SMatthew G. Knepley     /* Transform to global basis before insertion in Jacobian */
54473e9753d6SMatthew G. Knepley     if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
54483e9753d6SMatthew G. Knepley     if (hasPrec) {
54493e9753d6SMatthew G. Knepley       if (hasJac) {
54503e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
54513e9753d6SMatthew G. Knepley         if (!isMatIS) {
54523e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54533e9753d6SMatthew G. Knepley         } else {
54543e9753d6SMatthew G. Knepley           Mat lJ;
54553e9753d6SMatthew G. Knepley 
54563e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
54573e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54583e9753d6SMatthew G. Knepley         }
54593e9753d6SMatthew G. Knepley       }
54603e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
54613e9753d6SMatthew G. Knepley       if (!isMatISP) {
54623e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54633e9753d6SMatthew G. Knepley       } else {
54643e9753d6SMatthew G. Knepley         Mat lJ;
54653e9753d6SMatthew G. Knepley 
54663e9753d6SMatthew G. Knepley         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
54673e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54683e9753d6SMatthew G. Knepley       }
54693e9753d6SMatthew G. Knepley     } else {
54703e9753d6SMatthew G. Knepley       if (hasJac) {
54713e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
54723e9753d6SMatthew G. Knepley         if (!isMatISP) {
54733e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54743e9753d6SMatthew G. Knepley         } else {
54753e9753d6SMatthew G. Knepley           Mat lJ;
54763e9753d6SMatthew G. Knepley 
54773e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
54783e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54793e9753d6SMatthew G. Knepley         }
54803e9753d6SMatthew G. Knepley       }
54813e9753d6SMatthew G. Knepley     }
54823e9753d6SMatthew G. Knepley   }
54833e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
54843e9753d6SMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
54853e9753d6SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
54863e9753d6SMatthew G. Knepley   if (dmAux) {
54873e9753d6SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
54883e9753d6SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
54893e9753d6SMatthew G. Knepley   }
54903e9753d6SMatthew G. Knepley   /* Compute boundary integrals */
54913e9753d6SMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
54923e9753d6SMatthew G. Knepley   /* Assemble matrix */
54933e9753d6SMatthew G. Knepley   if (hasJac && hasPrec) {
54943e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54953e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54963e9753d6SMatthew G. Knepley   }
54973e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54983e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54993e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
55003e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
55013e9753d6SMatthew G. Knepley }
55023e9753d6SMatthew G. Knepley 
550306ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, PetscFormKey key[], IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user)
55043e9753d6SMatthew G. Knepley {
55053e9753d6SMatthew G. Knepley   DM_Plex         *mesh          = (DM_Plex *) dm->data;
55063e9753d6SMatthew G. Knepley   const char      *name          = "Hybrid Jacobian";
5507148442b3SMatthew G. Knepley   DM               dmAux[3]      = {NULL, NULL, NULL};
5508148442b3SMatthew G. Knepley   DMLabel          ghostLabel    = NULL;
55093e9753d6SMatthew G. Knepley   DM               plex          = NULL;
55103e9753d6SMatthew G. Knepley   DM               plexA         = NULL;
5511148442b3SMatthew G. Knepley   PetscDS          ds            = NULL;
5512148442b3SMatthew G. Knepley   PetscDS          dsAux[3]      = {NULL, NULL, NULL};
5513148442b3SMatthew G. Knepley   Vec              locA[3]       = {NULL, NULL, NULL};
55143e9753d6SMatthew G. Knepley   PetscSection     section       = NULL;
5515148442b3SMatthew G. Knepley   PetscSection     sectionAux[3] = {NULL, NULL, NULL};
55163e9753d6SMatthew G. Knepley   DMField          coordField    = NULL;
5517148442b3SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a[3];
55183e9753d6SMatthew G. Knepley   PetscScalar     *elemMat, *elemMatP;
5519148442b3SMatthew G. Knepley   PetscSection     globalSection, subSection;
55203e9753d6SMatthew G. Knepley   IS               chunkIS;
55213e9753d6SMatthew G. Knepley   const PetscInt  *cells;
55223e9753d6SMatthew G. Knepley   PetscInt        *faces;
55233e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
5524148442b3SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, totDim, totDimAux[3], numChunks, cellChunkSize, chunk;
55253e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
55263e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
55273e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
5528*5fedec97SMatthew G. Knepley   PetscBool        isMatIS = PETSC_FALSE, isMatISP = PETSC_FALSE, hasBdJac, hasBdPrec;
55293e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
55303e9753d6SMatthew G. Knepley 
55313e9753d6SMatthew G. Knepley   PetscFunctionBegin;
5532*5fedec97SMatthew G. Knepley   if ((key[0].label == key[1].label) && (key[0].value == key[1].value) && (key[0].part == key[1].part)) {
5533*5fedec97SMatthew G. Knepley     const char *name;
5534*5fedec97SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) key[0].label, &name);CHKERRQ(ierr);
5535*5fedec97SMatthew G. Knepley     SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Form keys for each side of a cohesive surface must be different (%s, %D, %D)", name, key[0].value, key[0].part);
5536*5fedec97SMatthew G. Knepley   }
55373e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
55383e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
55393e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
55403e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
55413e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
55423e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
55433e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
5544148442b3SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &ds);CHKERRQ(ierr);
5545148442b3SMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
5546148442b3SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
5547148442b3SMatthew G. Knepley   ierr = PetscDSHasBdJacobian(ds, &hasBdJac);CHKERRQ(ierr);
5548148442b3SMatthew G. Knepley   ierr = PetscDSHasBdJacobianPreconditioner(ds, &hasBdPrec);CHKERRQ(ierr);
55493e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
55503e9753d6SMatthew G. Knepley   if (isMatISP)               {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);}
55513e9753d6SMatthew G. Knepley   if (hasBdPrec && hasBdJac)  {ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);}
55523e9753d6SMatthew G. Knepley   if (isMatIS && !subSection) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);}
5553148442b3SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key[2].label, key[2].value, &locA[2]);CHKERRQ(ierr);
5554148442b3SMatthew G. Knepley   if (locA[2]) {
5555148442b3SMatthew G. Knepley     ierr = VecGetDM(locA[2], &dmAux[2]);CHKERRQ(ierr);
5556148442b3SMatthew G. Knepley     ierr = DMConvert(dmAux[2], DMPLEX, &plexA);CHKERRQ(ierr);
5557148442b3SMatthew G. Knepley     ierr = DMGetSection(dmAux[2], &sectionAux[2]);CHKERRQ(ierr);
5558148442b3SMatthew G. Knepley     ierr = DMGetCellDS(dmAux[2], cStart, &dsAux[2]);CHKERRQ(ierr);
5559148442b3SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[2], &totDimAux[2]);CHKERRQ(ierr);
5560148442b3SMatthew G. Knepley     {
5561148442b3SMatthew G. Knepley       const PetscInt *cone;
5562148442b3SMatthew G. Knepley       PetscInt        c;
5563148442b3SMatthew G. Knepley 
5564148442b3SMatthew G. Knepley       ierr = DMPlexGetCone(dm, cStart, &cone);CHKERRQ(ierr);
5565148442b3SMatthew G. Knepley       for (c = 0; c < 2; ++c) {
5566148442b3SMatthew G. Knepley         const PetscInt *support;
5567148442b3SMatthew G. Knepley         PetscInt ssize, s;
5568148442b3SMatthew G. Knepley 
5569148442b3SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5570148442b3SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, cone[c], &ssize);CHKERRQ(ierr);
5571148442b3SMatthew G. Knepley         if (ssize != 2) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[c], cStart, ssize);
5572148442b3SMatthew G. Knepley         if      (support[0] == cStart) s = 1;
5573148442b3SMatthew G. Knepley         else if (support[1] == cStart) s = 0;
5574148442b3SMatthew G. Knepley         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[c], cStart);
5575148442b3SMatthew G. Knepley         ierr = DMGetAuxiliaryVec(dm, key[c].label, key[c].value, &locA[c]);CHKERRQ(ierr);
5576148442b3SMatthew G. Knepley         if (locA[c]) {ierr = VecGetDM(locA[c], &dmAux[c]);CHKERRQ(ierr);}
5577148442b3SMatthew G. Knepley         else         {dmAux[c] = dmAux[2];}
5578148442b3SMatthew G. Knepley         ierr = DMGetCellDS(dmAux[c], support[s], &dsAux[c]);CHKERRQ(ierr);
5579148442b3SMatthew G. Knepley         ierr = PetscDSGetTotalDimension(dsAux[c], &totDimAux[c]);CHKERRQ(ierr);
5580148442b3SMatthew G. Knepley       }
5581148442b3SMatthew G. Knepley     }
55823e9753d6SMatthew G. Knepley   }
55833e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
55843e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
55853e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
55863e9753d6SMatthew G. Knepley     PetscInt f;
55873e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf, &quads, Nf, &geoms);CHKERRQ(ierr);
55883e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
55893e9753d6SMatthew G. Knepley       PetscFE fe;
55903e9753d6SMatthew G. Knepley 
5591148442b3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
55923e9753d6SMatthew G. Knepley       if (fe) {
55933e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
55943e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
55953e9753d6SMatthew G. Knepley       }
55963e9753d6SMatthew G. Knepley     }
55973e9753d6SMatthew G. Knepley   }
55983e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
55993e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
56003e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
56013e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
5602148442b3SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
5603148442b3SMatthew G. Knepley   ierr = DMPlexGetHybridAuxFields(dm, dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
56043e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
56053e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
56063e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
56073e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
56083e9753d6SMatthew G. Knepley 
56093e9753d6SMatthew G. Knepley     if (hasBdJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
56103e9753d6SMatthew G. Knepley     if (hasBdPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
56113e9753d6SMatthew G. Knepley     /* Get faces */
56123e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
56133e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
56143e9753d6SMatthew G. Knepley       const PetscInt *cone;
56153e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(plex, cell, &cone);CHKERRQ(ierr);
56163e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
56173e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
56183e9753d6SMatthew G. Knepley     }
56193e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
56203e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
56213e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
56223e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
56233e9753d6SMatthew G. Knepley     } else {
56243e9753d6SMatthew G. Knepley       PetscInt f;
56253e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
56263e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
56273e9753d6SMatthew G. Knepley       }
56283e9753d6SMatthew G. Knepley     }
56293e9753d6SMatthew G. Knepley 
56303e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
56313e9753d6SMatthew G. Knepley       PetscFE         feI;
56323e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[fieldI];
56333e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
56343e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI];
56353e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
5636*5fedec97SMatthew G. Knepley       PetscBool       isCohesiveField;
56373e9753d6SMatthew G. Knepley 
5638148442b3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, fieldI, (PetscObject *) &feI);CHKERRQ(ierr);
56393e9753d6SMatthew G. Knepley       if (!feI) continue;
56403e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(feI, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
56413e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
56423e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(feI, &Nb);CHKERRQ(ierr);
56433e9753d6SMatthew G. Knepley       blockSize = Nb;
56443e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
56453e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(feI, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
56463e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
56473e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
56483e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
56493e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
56503e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
56513e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
5652*5fedec97SMatthew G. Knepley       ierr = PetscDSGetCohesive(ds, fieldI, &isCohesiveField);CHKERRQ(ierr);
56533e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
56543e9753d6SMatthew G. Knepley         PetscFE feJ;
56553e9753d6SMatthew G. Knepley 
5656148442b3SMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, fieldJ, (PetscObject *) &feJ);CHKERRQ(ierr);
56573e9753d6SMatthew G. Knepley         if (!feJ) continue;
5658148442b3SMatthew G. Knepley         key[0].field = fieldI*Nf+fieldJ;
5659148442b3SMatthew G. Knepley         key[1].field = fieldI*Nf+fieldJ;
5660*5fedec97SMatthew G. Knepley         key[2].field = fieldI*Nf+fieldJ;
5661148442b3SMatthew G. Knepley         if (hasBdJac) {
5662*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, X_tShift, elemMat);CHKERRQ(ierr);
5663*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[0], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
5664*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, X_tShift, elemMat);CHKERRQ(ierr);
5665*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[1], 1, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
5666148442b3SMatthew G. Knepley         }
5667148442b3SMatthew G. Knepley         if (hasBdPrec) {
5668*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, X_tShift, elemMatP);CHKERRQ(ierr);
5669*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[0], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
5670*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, X_tShift, elemMatP);CHKERRQ(ierr);
5671*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[1], 1, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
5672148442b3SMatthew G. Knepley         }
5673*5fedec97SMatthew G. Knepley         if (hasBdJac) {
5674*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[2], 0, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, X_tShift, elemMat);CHKERRQ(ierr);
5675*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[2], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
5676148442b3SMatthew G. Knepley         }
5677*5fedec97SMatthew G. Knepley         if (hasBdPrec) {
5678*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[2], 0, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, X_tShift, elemMatP);CHKERRQ(ierr);
5679*5fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[2], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
56803e9753d6SMatthew G. Knepley         }
56813e9753d6SMatthew G. Knepley       }
56823e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
56833e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
56843e9753d6SMatthew G. Knepley     }
56853e9753d6SMatthew G. Knepley     /* Insert values into matrix */
56863e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
56873e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
56883e9753d6SMatthew G. Knepley       const PetscInt cind = c - cS;
56893e9753d6SMatthew G. Knepley 
56903e9753d6SMatthew G. Knepley       if (hasBdPrec) {
56913e9753d6SMatthew G. Knepley         if (hasBdJac) {
56923e9753d6SMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
56933e9753d6SMatthew G. Knepley           if (!isMatIS) {
56943e9753d6SMatthew G. Knepley             ierr = DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
56953e9753d6SMatthew G. Knepley           } else {
56963e9753d6SMatthew G. Knepley             Mat lJ;
56973e9753d6SMatthew G. Knepley 
56983e9753d6SMatthew G. Knepley             ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
56993e9753d6SMatthew G. Knepley             ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57003e9753d6SMatthew G. Knepley           }
57013e9753d6SMatthew G. Knepley         }
57023e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
57033e9753d6SMatthew G. Knepley         if (!isMatISP) {
57043e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57053e9753d6SMatthew G. Knepley         } else {
57063e9753d6SMatthew G. Knepley           Mat lJ;
57073e9753d6SMatthew G. Knepley 
57083e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
57093e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57103e9753d6SMatthew G. Knepley         }
57113e9753d6SMatthew G. Knepley       } else if (hasBdJac) {
57123e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
57133e9753d6SMatthew G. Knepley         if (!isMatISP) {
57143e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57153e9753d6SMatthew G. Knepley         } else {
57163e9753d6SMatthew G. Knepley           Mat lJ;
57173e9753d6SMatthew G. Knepley 
57183e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
57193e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57203e9753d6SMatthew G. Knepley         }
57213e9753d6SMatthew G. Knepley       }
57223e9753d6SMatthew G. Knepley     }
57233e9753d6SMatthew G. Knepley   }
5724148442b3SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
5725148442b3SMatthew G. Knepley   ierr = DMPlexRestoreHybridAuxFields(dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
57263e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
57273e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
57283e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
57293e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
57303e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
57313e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
57323e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
57333e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
57343e9753d6SMatthew G. Knepley   } else {
57353e9753d6SMatthew G. Knepley     PetscInt f;
57363e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
57373e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);CHKERRQ(ierr);}
57383e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
57393e9753d6SMatthew G. Knepley     }
57403e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
57413e9753d6SMatthew G. Knepley   }
5742148442b3SMatthew G. Knepley   if (dmAux[2]) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
57433e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
57443e9753d6SMatthew G. Knepley   /* Assemble matrix */
57453e9753d6SMatthew G. Knepley   if (hasBdJac && hasBdPrec) {
57463e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57473e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57483e9753d6SMatthew G. Knepley   }
57493e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57503e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
57513e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
57523e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
57533e9753d6SMatthew G. Knepley }
57548e3a2eefSMatthew G. Knepley 
57558e3a2eefSMatthew G. Knepley /*
57568e3a2eefSMatthew G. Knepley   DMPlexComputeJacobian_Action_Internal - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user.
57578e3a2eefSMatthew G. Knepley 
57588e3a2eefSMatthew G. Knepley   Input Parameters:
57598e3a2eefSMatthew G. Knepley + dm     - The mesh
57608e3a2eefSMatthew G. Knepley . key    - The PetscWeakFormKey indcating where integration should happen
576106ad1575SMatthew G. Knepley . cellIS - The cells to integrate over
57628e3a2eefSMatthew G. Knepley . t      - The time
57638e3a2eefSMatthew G. Knepley . X_tShift - The multiplier for the Jacobian with repsect to X_t
57648e3a2eefSMatthew G. Knepley . X      - Local solution vector
57658e3a2eefSMatthew G. Knepley . X_t    - Time-derivative of the local solution vector
57668e3a2eefSMatthew G. Knepley . Y      - Local input vector
576706ad1575SMatthew G. Knepley - user   - the user context
57688e3a2eefSMatthew G. Knepley 
57698e3a2eefSMatthew G. Knepley   Output Parameter:
57708e3a2eefSMatthew G. Knepley . Z - Local output vector
57718e3a2eefSMatthew G. Knepley 
57728e3a2eefSMatthew G. Knepley   Note:
57738e3a2eefSMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
57748e3a2eefSMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
57758e3a2eefSMatthew G. Knepley */
577606ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Action_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
57778e3a2eefSMatthew G. Knepley {
57788e3a2eefSMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *) dm->data;
57798e3a2eefSMatthew G. Knepley   const char     *name  = "Jacobian";
57808e3a2eefSMatthew G. Knepley   DM              dmAux = NULL, plex, plexAux = NULL;
57818e3a2eefSMatthew G. Knepley   DMEnclosureType encAux;
57828e3a2eefSMatthew G. Knepley   Vec             A;
57838e3a2eefSMatthew G. Knepley   DMField         coordField;
57848e3a2eefSMatthew G. Knepley   PetscDS         prob, probAux = NULL;
57858e3a2eefSMatthew G. Knepley   PetscQuadrature quad;
57868e3a2eefSMatthew G. Knepley   PetscSection    section, globalSection, sectionAux;
57878e3a2eefSMatthew G. Knepley   PetscScalar    *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
57888e3a2eefSMatthew G. Knepley   const PetscInt *cells;
57898e3a2eefSMatthew G. Knepley   PetscInt        Nf, fieldI, fieldJ;
57908e3a2eefSMatthew G. Knepley   PetscInt        totDim, totDimAux = 0, cStart, cEnd, numCells, c;
57918e3a2eefSMatthew G. Knepley   PetscBool       hasDyn;
57928e3a2eefSMatthew G. Knepley   PetscErrorCode  ierr;
57938e3a2eefSMatthew G. Knepley 
57948e3a2eefSMatthew G. Knepley   PetscFunctionBegin;
57958e3a2eefSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
57968e3a2eefSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
57978e3a2eefSMatthew G. Knepley   if (!cellIS) {
57988e3a2eefSMatthew G. Knepley     PetscInt depth;
57998e3a2eefSMatthew G. Knepley 
58008e3a2eefSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
58018e3a2eefSMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
58028e3a2eefSMatthew G. Knepley     if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);}
58038e3a2eefSMatthew G. Knepley   } else {
58048e3a2eefSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) cellIS);CHKERRQ(ierr);
58058e3a2eefSMatthew G. Knepley   }
58068e3a2eefSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
58078e3a2eefSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
58088e3a2eefSMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
58098e3a2eefSMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
58108e3a2eefSMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
58118e3a2eefSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
58128e3a2eefSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
58138e3a2eefSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
58148e3a2eefSMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
5815e7225ec2SMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, &A);CHKERRQ(ierr);
58168e3a2eefSMatthew G. Knepley   if (A) {
58178e3a2eefSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
58188e3a2eefSMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
58198e3a2eefSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexAux);CHKERRQ(ierr);
58208e3a2eefSMatthew G. Knepley     ierr = DMGetLocalSection(plexAux, &sectionAux);CHKERRQ(ierr);
58218e3a2eefSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
58228e3a2eefSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
58238e3a2eefSMatthew G. Knepley   }
58248e3a2eefSMatthew G. Knepley   ierr = VecSet(Z, 0.0);CHKERRQ(ierr);
58258e3a2eefSMatthew G. Knepley   ierr = PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);CHKERRQ(ierr);
58268e3a2eefSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
58278e3a2eefSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
58288e3a2eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
58298e3a2eefSMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
58308e3a2eefSMatthew G. Knepley     const PetscInt cind = c - cStart;
58318e3a2eefSMatthew G. Knepley     PetscScalar   *x = NULL,  *x_t = NULL;
58328e3a2eefSMatthew G. Knepley     PetscInt       i;
58338e3a2eefSMatthew G. Knepley 
58348e3a2eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, X, cell, NULL, &x);CHKERRQ(ierr);
58358e3a2eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
58368e3a2eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, X, cell, NULL, &x);CHKERRQ(ierr);
58378e3a2eefSMatthew G. Knepley     if (X_t) {
58388e3a2eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
58398e3a2eefSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
58408e3a2eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
58418e3a2eefSMatthew G. Knepley     }
58428e3a2eefSMatthew G. Knepley     if (dmAux) {
58438e3a2eefSMatthew G. Knepley       PetscInt subcell;
58448e3a2eefSMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr);
58458e3a2eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
58468e3a2eefSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
58478e3a2eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
58488e3a2eefSMatthew G. Knepley     }
58498e3a2eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, Y, cell, NULL, &x);CHKERRQ(ierr);
58508e3a2eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i];
58518e3a2eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, Y, cell, NULL, &x);CHKERRQ(ierr);
58528e3a2eefSMatthew G. Knepley   }
58538e3a2eefSMatthew G. Knepley   ierr = PetscArrayzero(elemMat, numCells*totDim*totDim);CHKERRQ(ierr);
58548e3a2eefSMatthew G. Knepley   if (hasDyn)  {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);}
58558e3a2eefSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
58568e3a2eefSMatthew G. Knepley     PetscFE  fe;
58578e3a2eefSMatthew G. Knepley     PetscInt Nb;
58588e3a2eefSMatthew G. Knepley     /* Conforming batches */
58598e3a2eefSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
58608e3a2eefSMatthew G. Knepley     /* Remainder */
58618e3a2eefSMatthew G. Knepley     PetscInt Nr, offset, Nq;
58628e3a2eefSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
58638e3a2eefSMatthew G. Knepley     PetscInt    maxDegree;
58648e3a2eefSMatthew G. Knepley     PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
58658e3a2eefSMatthew G. Knepley 
58668e3a2eefSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
58678e3a2eefSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
58688e3a2eefSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
58698e3a2eefSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
58708e3a2eefSMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
58718e3a2eefSMatthew G. Knepley     if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);}
58728e3a2eefSMatthew G. Knepley     if (!qGeom) {
58738e3a2eefSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
58748e3a2eefSMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
58758e3a2eefSMatthew G. Knepley     }
58768e3a2eefSMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
58778e3a2eefSMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
58788e3a2eefSMatthew G. Knepley     blockSize = Nb;
58798e3a2eefSMatthew G. Knepley     batchSize = numBlocks * blockSize;
58808e3a2eefSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
58818e3a2eefSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
58828e3a2eefSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
58838e3a2eefSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
58848e3a2eefSMatthew G. Knepley     offset    = numCells - Nr;
58858e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
58868e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
58878e3a2eefSMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
58888e3a2eefSMatthew G. Knepley       key.field = fieldI*Nf + fieldJ;
58898e3a2eefSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
58908e3a2eefSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
58918e3a2eefSMatthew G. Knepley       if (hasDyn) {
58928e3a2eefSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
58938e3a2eefSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
58948e3a2eefSMatthew G. Knepley       }
58958e3a2eefSMatthew G. Knepley     }
58968e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
58978e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
58988e3a2eefSMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
58998e3a2eefSMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
59008e3a2eefSMatthew G. Knepley   }
59018e3a2eefSMatthew G. Knepley   if (hasDyn) {
59028e3a2eefSMatthew G. Knepley     for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
59038e3a2eefSMatthew G. Knepley   }
59048e3a2eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
59058e3a2eefSMatthew G. Knepley     const PetscInt     cell = cells ? cells[c] : c;
59068e3a2eefSMatthew G. Knepley     const PetscInt     cind = c - cStart;
59078e3a2eefSMatthew G. Knepley     const PetscBLASInt M = totDim, one = 1;
59088e3a2eefSMatthew G. Knepley     const PetscScalar  a = 1.0, b = 0.0;
59098e3a2eefSMatthew G. Knepley 
59108e3a2eefSMatthew G. Knepley     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one));
59118e3a2eefSMatthew G. Knepley     if (mesh->printFEM > 1) {
59128e3a2eefSMatthew G. Knepley       ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);
59138e3a2eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Y",  totDim, &y[cind*totDim]);CHKERRQ(ierr);
59148e3a2eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Z",  totDim, z);CHKERRQ(ierr);
59158e3a2eefSMatthew G. Knepley     }
59168e3a2eefSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr);
59178e3a2eefSMatthew G. Knepley   }
59188e3a2eefSMatthew G. Knepley   ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr);
59198e3a2eefSMatthew G. Knepley   if (mesh->printFEM) {
59208e3a2eefSMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject)Z), "Z:\n");CHKERRQ(ierr);
59218e3a2eefSMatthew G. Knepley     ierr = VecView(Z, NULL);CHKERRQ(ierr);
59228e3a2eefSMatthew G. Knepley   }
59238e3a2eefSMatthew G. Knepley   ierr = PetscFree(a);CHKERRQ(ierr);
59248e3a2eefSMatthew G. Knepley   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
59258e3a2eefSMatthew G. Knepley   ierr = DMDestroy(&plexAux);CHKERRQ(ierr);
59268e3a2eefSMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
59278e3a2eefSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
59288e3a2eefSMatthew G. Knepley   PetscFunctionReturn(0);
59298e3a2eefSMatthew G. Knepley }
5930