xref: /petsc/src/dm/impls/plex/plexfem.c (revision 0c36454096925ed0dc33600f014db4c058649b2b)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3cb1e1211SMatthew G Knepley 
4e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h>
5af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
6af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
7a0845e3aSMatthew G. Knepley 
82f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy)
92f856554SMatthew G. Knepley {
102f856554SMatthew G. Knepley   PetscBool      isPlex;
112f856554SMatthew G. Knepley   PetscErrorCode ierr;
122f856554SMatthew G. Knepley 
132f856554SMatthew G. Knepley   PetscFunctionBegin;
142f856554SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
152f856554SMatthew G. Knepley   if (isPlex) {
162f856554SMatthew G. Knepley     *plex = dm;
172f856554SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
182f856554SMatthew G. Knepley   } else {
192f856554SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
202f856554SMatthew G. Knepley     if (!*plex) {
212f856554SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
222f856554SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
232f856554SMatthew G. Knepley       if (copy) {
242f856554SMatthew G. Knepley         const char *comps[3] = {"A", "dmAux"};
252f856554SMatthew G. Knepley         PetscObject obj;
262f856554SMatthew G. Knepley         PetscInt    i;
272f856554SMatthew G. Knepley 
282f856554SMatthew G. Knepley         {
292f856554SMatthew G. Knepley           /* Run the subdomain hook (this will copy the DMSNES/DMTS) */
302f856554SMatthew G. Knepley           DMSubDomainHookLink link;
312f856554SMatthew G. Knepley           for (link = dm->subdomainhook; link; link = link->next) {
322f856554SMatthew G. Knepley             if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);}
332f856554SMatthew G. Knepley           }
342f856554SMatthew G. Knepley         }
352f856554SMatthew G. Knepley         for (i = 0; i < 3; i++) {
362f856554SMatthew G. Knepley           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
372f856554SMatthew G. Knepley           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
382f856554SMatthew G. Knepley         }
392f856554SMatthew G. Knepley       }
402f856554SMatthew G. Knepley     } else {
412f856554SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
422f856554SMatthew G. Knepley     }
432f856554SMatthew G. Knepley   }
442f856554SMatthew G. Knepley   PetscFunctionReturn(0);
452f856554SMatthew G. Knepley }
462f856554SMatthew G. Knepley 
479b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
489b6f715bSMatthew G. Knepley {
499b6f715bSMatthew G. Knepley   PetscFEGeom *geom = (PetscFEGeom *) ctx;
509b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
519b6f715bSMatthew G. Knepley 
529b6f715bSMatthew G. Knepley   PetscFunctionBegin;
539b6f715bSMatthew G. Knepley   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
549b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
559b6f715bSMatthew G. Knepley }
569b6f715bSMatthew G. Knepley 
579b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
589b6f715bSMatthew G. Knepley {
599b6f715bSMatthew G. Knepley   char            composeStr[33] = {0};
609b6f715bSMatthew G. Knepley   PetscObjectId   id;
619b6f715bSMatthew G. Knepley   PetscContainer  container;
629b6f715bSMatthew G. Knepley   PetscErrorCode  ierr;
639b6f715bSMatthew G. Knepley 
649b6f715bSMatthew G. Knepley   PetscFunctionBegin;
659b6f715bSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
669b6f715bSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr);
679b6f715bSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
689b6f715bSMatthew G. Knepley   if (container) {
699b6f715bSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
709b6f715bSMatthew G. Knepley   } else {
719b6f715bSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
729b6f715bSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
739b6f715bSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
749b6f715bSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
759b6f715bSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
769b6f715bSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
779b6f715bSMatthew G. Knepley   }
789b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
799b6f715bSMatthew G. Knepley }
809b6f715bSMatthew G. Knepley 
819b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
829b6f715bSMatthew G. Knepley {
839b6f715bSMatthew G. Knepley   PetscFunctionBegin;
849b6f715bSMatthew G. Knepley   *geom = NULL;
859b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
869b6f715bSMatthew G. Knepley }
879b6f715bSMatthew G. Knepley 
8846fa42a0SMatthew G. Knepley /*@
8946fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
9046fa42a0SMatthew G. Knepley 
9146fa42a0SMatthew G. Knepley   Not collective
9246fa42a0SMatthew G. Knepley 
9346fa42a0SMatthew G. Knepley   Input Arguments:
9446fa42a0SMatthew G. Knepley + dm   - the DM
9546fa42a0SMatthew G. Knepley - unit - The SI unit
9646fa42a0SMatthew G. Knepley 
9746fa42a0SMatthew G. Knepley   Output Argument:
9846fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
9946fa42a0SMatthew G. Knepley 
10046fa42a0SMatthew G. Knepley   Level: advanced
10146fa42a0SMatthew G. Knepley 
10246fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
10346fa42a0SMatthew G. Knepley @*/
104cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
105cb1e1211SMatthew G Knepley {
106cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
107cb1e1211SMatthew G Knepley 
108cb1e1211SMatthew G Knepley   PetscFunctionBegin;
109cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
110cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
111cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
112cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
113cb1e1211SMatthew G Knepley }
114cb1e1211SMatthew G Knepley 
11546fa42a0SMatthew G. Knepley /*@
11646fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
11746fa42a0SMatthew G. Knepley 
11846fa42a0SMatthew G. Knepley   Not collective
11946fa42a0SMatthew G. Knepley 
12046fa42a0SMatthew G. Knepley   Input Arguments:
12146fa42a0SMatthew G. Knepley + dm   - the DM
12246fa42a0SMatthew G. Knepley . unit - The SI unit
12346fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
12446fa42a0SMatthew G. Knepley 
12546fa42a0SMatthew G. Knepley   Level: advanced
12646fa42a0SMatthew G. Knepley 
12746fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
12846fa42a0SMatthew G. Knepley @*/
129cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
130cb1e1211SMatthew G Knepley {
131cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
132cb1e1211SMatthew G Knepley 
133cb1e1211SMatthew G Knepley   PetscFunctionBegin;
134cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
135cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
136cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
137cb1e1211SMatthew G Knepley }
138cb1e1211SMatthew G Knepley 
139327779f2SMatthew Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nc, PetscScalar *mode, void *ctx)
140026175e5SToby Isaac {
14112adca46SMatthew 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}}};
142026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
143ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
144026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
145026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
146026175e5SToby Isaac 
147ad917190SMatthew G. Knepley   PetscFunctionBegin;
148ff1e0c32SBarry Smith   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %D does not match context dimension %D", dim, dim2);
149026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
150026175e5SToby Isaac   if (d < dim) {
15112adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
152ad917190SMatthew G. Knepley   } else {
153026175e5SToby Isaac     for (i = 0; i < dim; i++) {
154026175e5SToby Isaac       for (j = 0; j < dim; j++) {
15512adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
156026175e5SToby Isaac       }
157026175e5SToby Isaac     }
158026175e5SToby Isaac   }
159ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
160026175e5SToby Isaac }
161026175e5SToby Isaac 
162cc4e42d9SMartin Diehl /*@
16312adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
164cb1e1211SMatthew G Knepley 
165d083f849SBarry Smith   Collective on dm
166cb1e1211SMatthew G Knepley 
167cb1e1211SMatthew G Knepley   Input Arguments:
168026175e5SToby Isaac . dm - the DM
169cb1e1211SMatthew G Knepley 
170cb1e1211SMatthew G Knepley   Output Argument:
171cb1e1211SMatthew G Knepley . sp - the null space
172cb1e1211SMatthew G Knepley 
17312adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
174cb1e1211SMatthew G Knepley 
175cb1e1211SMatthew G Knepley   Level: advanced
176cb1e1211SMatthew G Knepley 
17712adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
178cb1e1211SMatthew G Knepley @*/
179026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp)
180cb1e1211SMatthew G Knepley {
181cb1e1211SMatthew G Knepley   MPI_Comm       comm;
182026175e5SToby Isaac   Vec            mode[6];
183026175e5SToby Isaac   PetscSection   section, globalSection;
184b247467aSMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, mmin, d, i, j;
185cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
186cb1e1211SMatthew G Knepley 
187cb1e1211SMatthew G Knepley   PetscFunctionBegin;
188cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
189c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1909d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
191cb1e1211SMatthew G Knepley   if (dim == 1) {
192cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
193cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
194cb1e1211SMatthew G Knepley   }
19592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
196e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
197cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
198b247467aSMatthew G. Knepley   m    = (dim*(dim+1))/2;
199cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
200cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
201cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
202b247467aSMatthew G. Knepley   ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr);
203b247467aSMatthew G. Knepley   mmin = PetscMin(m, n);
204cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
205026175e5SToby Isaac   for (d = 0; d < m; d++) {
206330485fdSToby Isaac     PetscInt         ctx[2];
207d1828a1cSMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
208026175e5SToby Isaac     void            *voidctx = (void *) (&ctx[0]);
209cb1e1211SMatthew G Knepley 
2109d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
211330485fdSToby Isaac     ctx[1] = d;
2120709b2feSToby Isaac     ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
213cb1e1211SMatthew G Knepley   }
2143b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
215b50a2c0aSJacob Faibussowitsch   for (i = 0; i < mmin; ++i) {
216b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
217b50a2c0aSJacob Faibussowitsch 
2189019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
219b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], mmin-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
220b50a2c0aSJacob Faibussowitsch     for (j = i+1; j < mmin; ++j) {
221b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
222b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
223b50a2c0aSJacob Faibussowitsch     }
224cb1e1211SMatthew G Knepley   }
225b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
226b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
227cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
228cb1e1211SMatthew G Knepley }
229cb1e1211SMatthew G Knepley 
230cc4e42d9SMartin Diehl /*@
23112adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
23212adca46SMatthew G. Knepley 
233d083f849SBarry Smith   Collective on dm
23412adca46SMatthew G. Knepley 
23512adca46SMatthew G. Knepley   Input Arguments:
23612adca46SMatthew G. Knepley + dm    - the DM
23712adca46SMatthew G. Knepley . nb    - The number of bodies
23812adca46SMatthew G. Knepley . label - The DMLabel marking each domain
23912adca46SMatthew G. Knepley . nids  - The number of ids per body
24012adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
24112adca46SMatthew G. Knepley 
24212adca46SMatthew G. Knepley   Output Argument:
24312adca46SMatthew G. Knepley . sp - the null space
24412adca46SMatthew G. Knepley 
24512adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
24612adca46SMatthew G. Knepley 
24712adca46SMatthew G. Knepley   Level: advanced
24812adca46SMatthew G. Knepley 
24912adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
25012adca46SMatthew G. Knepley @*/
25112adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
25212adca46SMatthew G. Knepley {
25312adca46SMatthew G. Knepley   MPI_Comm       comm;
25412adca46SMatthew G. Knepley   PetscSection   section, globalSection;
25512adca46SMatthew G. Knepley   Vec           *mode;
25612adca46SMatthew G. Knepley   PetscScalar   *dots;
25712adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
25812adca46SMatthew G. Knepley   PetscErrorCode ierr;
25912adca46SMatthew G. Knepley 
26012adca46SMatthew G. Knepley   PetscFunctionBegin;
26112adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
26212adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26312adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
26492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
265e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
26612adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
26712adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
26812adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
26912adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
27012adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
27112adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
27212adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
27312adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
27412adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
27512adca46SMatthew G. Knepley       PetscInt         ctx[2];
27612adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
27712adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
27812adca46SMatthew G. Knepley 
27912adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
28012adca46SMatthew G. Knepley       ctx[1] = d;
28112adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
28212adca46SMatthew G. Knepley       off   += nids[b];
28312adca46SMatthew G. Knepley     }
28412adca46SMatthew G. Knepley   }
2853b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
286606c1a1cSJacob Faibussowitsch   for (i = 0; i < m; ++i) {
287b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
2885a0e29b9SJacob Faibussowitsch 
2899019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
290b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], m-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
2915a0e29b9SJacob Faibussowitsch     for (j = i+1; j < m; ++j) {
292b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
293b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
2945a0e29b9SJacob Faibussowitsch     }
29512adca46SMatthew G. Knepley   }
29612adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
29712adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
29812adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
29912adca46SMatthew G. Knepley   PetscFunctionReturn(0);
30012adca46SMatthew G. Knepley }
30112adca46SMatthew G. Knepley 
302b29cfa1cSToby Isaac /*@
303b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
304b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
305b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
306b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
307b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
308b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
309b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
310b29cfa1cSToby Isaac 
311b29cfa1cSToby Isaac   Input Parameters:
312b29cfa1cSToby Isaac + dm - the DMPlex object
313b29cfa1cSToby Isaac - height - the maximum projection height >= 0
314b29cfa1cSToby Isaac 
315b29cfa1cSToby Isaac   Level: advanced
316b29cfa1cSToby Isaac 
3174d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
318b29cfa1cSToby Isaac @*/
319b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
320b29cfa1cSToby Isaac {
321b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
322b29cfa1cSToby Isaac 
323b29cfa1cSToby Isaac   PetscFunctionBegin;
324b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
325b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
326b29cfa1cSToby Isaac   PetscFunctionReturn(0);
327b29cfa1cSToby Isaac }
328b29cfa1cSToby Isaac 
329b29cfa1cSToby Isaac /*@
330b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
331b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
332b29cfa1cSToby Isaac 
333b29cfa1cSToby Isaac   Input Parameters:
334b29cfa1cSToby Isaac . dm - the DMPlex object
335b29cfa1cSToby Isaac 
336b29cfa1cSToby Isaac   Output Parameters:
337b29cfa1cSToby Isaac . height - the maximum projection height
338b29cfa1cSToby Isaac 
339b29cfa1cSToby Isaac   Level: intermediate
340b29cfa1cSToby Isaac 
3414d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
342b29cfa1cSToby Isaac @*/
343b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
344b29cfa1cSToby Isaac {
345b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
346b29cfa1cSToby Isaac 
347b29cfa1cSToby Isaac   PetscFunctionBegin;
348b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
349b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
350b29cfa1cSToby Isaac   PetscFunctionReturn(0);
351b29cfa1cSToby Isaac }
352b29cfa1cSToby Isaac 
353ca3d3a14SMatthew G. Knepley typedef struct {
354ca3d3a14SMatthew G. Knepley   PetscReal    alpha; /* The first Euler angle, and in 2D the only one */
355ca3d3a14SMatthew G. Knepley   PetscReal    beta;  /* The second Euler angle */
356ca3d3a14SMatthew G. Knepley   PetscReal    gamma; /* The third Euler angle */
357ca3d3a14SMatthew G. Knepley   PetscInt     dim;   /* The dimension of R */
358ca3d3a14SMatthew G. Knepley   PetscScalar *R;     /* The rotation matrix, transforming a vector in the local basis to the global basis */
359ca3d3a14SMatthew G. Knepley   PetscScalar *RT;    /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */
360ca3d3a14SMatthew G. Knepley } RotCtx;
361ca3d3a14SMatthew G. Knepley 
362ca3d3a14SMatthew G. Knepley /*
363ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
364ca3d3a14SMatthew 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:
365ca3d3a14SMatthew 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.
366ca3d3a14SMatthew 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.
367ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
368ca3d3a14SMatthew G. Knepley */
369ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx)
370ca3d3a14SMatthew G. Knepley {
371ca3d3a14SMatthew G. Knepley   RotCtx        *rc  = (RotCtx *) ctx;
372ca3d3a14SMatthew G. Knepley   PetscInt       dim = rc->dim;
373ca3d3a14SMatthew G. Knepley   PetscReal      c1, s1, c2, s2, c3, s3;
374ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
375ca3d3a14SMatthew G. Knepley 
376ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
377ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr);
378ca3d3a14SMatthew G. Knepley   switch (dim) {
379ca3d3a14SMatthew G. Knepley   case 2:
380ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
381ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1;rc->R[1] = s1;
382ca3d3a14SMatthew G. Knepley     rc->R[2] = -s1;rc->R[3] = c1;
383580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
384ca3d3a14SMatthew G. Knepley     DMPlex_Transpose2D_Internal(rc->RT);break;
385ca3d3a14SMatthew G. Knepley     break;
386ca3d3a14SMatthew G. Knepley   case 3:
387ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
388ca3d3a14SMatthew G. Knepley     c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta);
389ca3d3a14SMatthew G. Knepley     c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma);
390ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1*c3 - c2*s1*s3;rc->R[1] =  c3*s1    + c1*c2*s3;rc->R[2] = s2*s3;
391ca3d3a14SMatthew G. Knepley     rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] =  c1*c2*c3 - s1*s3;   rc->R[5] = c3*s2;
392ca3d3a14SMatthew G. Knepley     rc->R[6] =  s1*s2;           rc->R[7] = -c1*s2;              rc->R[8] = c2;
393580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
394ca3d3a14SMatthew G. Knepley     DMPlex_Transpose3D_Internal(rc->RT);break;
395ca3d3a14SMatthew G. Knepley     break;
396ca3d3a14SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
397ca3d3a14SMatthew G. Knepley   }
398ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
399ca3d3a14SMatthew G. Knepley }
400ca3d3a14SMatthew G. Knepley 
401ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx)
402ca3d3a14SMatthew G. Knepley {
403ca3d3a14SMatthew G. Knepley   RotCtx        *rc = (RotCtx *) ctx;
404ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
405ca3d3a14SMatthew G. Knepley 
406ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
407ca3d3a14SMatthew G. Knepley   ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr);
408ca3d3a14SMatthew G. Knepley   ierr = PetscFree(rc);CHKERRQ(ierr);
409ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
410ca3d3a14SMatthew G. Knepley }
411ca3d3a14SMatthew G. Knepley 
412ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx)
413ca3d3a14SMatthew G. Knepley {
414ca3d3a14SMatthew G. Knepley   RotCtx *rc = (RotCtx *) ctx;
415ca3d3a14SMatthew G. Knepley 
416ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
417ca3d3a14SMatthew G. Knepley   PetscValidPointer(ctx, 5);
418ca3d3a14SMatthew G. Knepley   if (l2g) {*A = rc->R;}
419ca3d3a14SMatthew G. Knepley   else     {*A = rc->RT;}
420ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
421ca3d3a14SMatthew G. Knepley }
422ca3d3a14SMatthew G. Knepley 
423ab6a9622SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApplyReal_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscReal *y, PetscReal *z, void *ctx)
424ab6a9622SMatthew G. Knepley {
425ec277c0fSMatthew G. Knepley   PetscErrorCode ierr;
426ec277c0fSMatthew G. Knepley 
427ec277c0fSMatthew G. Knepley   PetscFunctionBegin;
428ab6a9622SMatthew G. Knepley   #if defined(PETSC_USE_COMPLEX)
429ab6a9622SMatthew G. Knepley   switch (dim) {
430ab6a9622SMatthew G. Knepley     case 2:
431ab6a9622SMatthew G. Knepley     {
432ab6a9622SMatthew G. Knepley       PetscScalar yt[2], zt[2];
433ab6a9622SMatthew G. Knepley 
434ab6a9622SMatthew G. Knepley       yt[0] = y[0]; yt[1] = y[1];
435ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
436ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]);
437ab6a9622SMatthew G. Knepley     }
4389b4815dcSMatthew G. Knepley     break;
439ab6a9622SMatthew G. Knepley     case 3:
440ab6a9622SMatthew G. Knepley     {
441ab6a9622SMatthew G. Knepley       PetscScalar yt[3], zt[3];
442ab6a9622SMatthew G. Knepley 
443ab6a9622SMatthew G. Knepley       yt[0] = y[0]; yt[1] = y[1]; yt[2] = y[2];
444ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
445ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); z[2] = PetscRealPart(zt[2]);
446ab6a9622SMatthew G. Knepley     }
4479b4815dcSMatthew G. Knepley     break;
448ab6a9622SMatthew G. Knepley   }
449ab6a9622SMatthew G. Knepley   #else
450ab6a9622SMatthew G. Knepley   ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, y, z, ctx);CHKERRQ(ierr);
451ab6a9622SMatthew G. Knepley   #endif
452ec277c0fSMatthew G. Knepley   PetscFunctionReturn(0);
453ab6a9622SMatthew G. Knepley }
454ab6a9622SMatthew G. Knepley 
455ec277c0fSMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx)
456ca3d3a14SMatthew G. Knepley {
457ca3d3a14SMatthew G. Knepley   const PetscScalar *A;
458ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
459ca3d3a14SMatthew G. Knepley 
460ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
461ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr);
462ca3d3a14SMatthew G. Knepley   switch (dim) {
4631fba962aSMatthew G. Knepley   case 2: DMPlex_Mult2D_Internal(A, 1, y, z);break;
4641fba962aSMatthew G. Knepley   case 3: DMPlex_Mult3D_Internal(A, 1, y, z);break;
465ca3d3a14SMatthew G. Knepley   }
466ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
467ca3d3a14SMatthew G. Knepley }
468ca3d3a14SMatthew G. Knepley 
469ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a)
470ca3d3a14SMatthew G. Knepley {
471ca3d3a14SMatthew G. Knepley   PetscSection       ts;
472ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tva;
473ca3d3a14SMatthew G. Knepley   PetscInt           dof;
474ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
475ca3d3a14SMatthew G. Knepley 
476ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
47792fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
478ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
479ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
480ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, (void *) &tva);CHKERRQ(ierr);
481ca3d3a14SMatthew G. Knepley   if (l2g) {
482ca3d3a14SMatthew G. Knepley     switch (dof) {
4831fba962aSMatthew G. Knepley     case 4: DMPlex_Mult2D_Internal(tva, 1, a, a);break;
4841fba962aSMatthew G. Knepley     case 9: DMPlex_Mult3D_Internal(tva, 1, a, a);break;
485ca3d3a14SMatthew G. Knepley     }
486ca3d3a14SMatthew G. Knepley   } else {
487ca3d3a14SMatthew G. Knepley     switch (dof) {
4881fba962aSMatthew G. Knepley     case 4: DMPlex_MultTranspose2D_Internal(tva, 1, a, a);break;
4891fba962aSMatthew G. Knepley     case 9: DMPlex_MultTranspose3D_Internal(tva, 1, a, a);break;
490ca3d3a14SMatthew G. Knepley     }
491ca3d3a14SMatthew G. Knepley   }
492ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
493ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
494ca3d3a14SMatthew G. Knepley }
495ca3d3a14SMatthew G. Knepley 
496ca3d3a14SMatthew 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)
497ca3d3a14SMatthew G. Knepley {
498ca3d3a14SMatthew G. Knepley   PetscSection       s, ts;
499ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tvaf, *tvag;
500ca3d3a14SMatthew G. Knepley   PetscInt           fdof, gdof, fpdof, gpdof;
501ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
502ca3d3a14SMatthew G. Knepley 
503ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
50492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
50592fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
506ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr);
507ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr);
508ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr);
509ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr);
510ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
511ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, (void *) &tvaf);CHKERRQ(ierr);
512ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, (void *) &tvag);CHKERRQ(ierr);
513ca3d3a14SMatthew G. Knepley   if (l2g) {
514ca3d3a14SMatthew G. Knepley     switch (fdof) {
515ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break;
516ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break;
517ca3d3a14SMatthew G. Knepley     }
518ca3d3a14SMatthew G. Knepley     switch (gdof) {
519ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break;
520ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break;
521ca3d3a14SMatthew G. Knepley     }
522ca3d3a14SMatthew G. Knepley   } else {
523ca3d3a14SMatthew G. Knepley     switch (fdof) {
524ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break;
525ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break;
526ca3d3a14SMatthew G. Knepley     }
527ca3d3a14SMatthew G. Knepley     switch (gdof) {
528ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break;
529ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break;
530ca3d3a14SMatthew G. Knepley     }
531ca3d3a14SMatthew G. Knepley   }
532ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
533ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
534ca3d3a14SMatthew G. Knepley }
535ca3d3a14SMatthew G. Knepley 
536ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a)
537ca3d3a14SMatthew G. Knepley {
538ca3d3a14SMatthew G. Knepley   PetscSection    s;
539ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
540ca3d3a14SMatthew G. Knepley   IS              clPoints;
541ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
542ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
543ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, Np, cp, dof, d = 0;
544ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
545ca3d3a14SMatthew G. Knepley 
546ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
54792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
548ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
549ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
550ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
551ca3d3a14SMatthew G. Knepley     for (cp = 0; cp < Np*2; cp += 2) {
552ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr);
553ca3d3a14SMatthew G. Knepley       if (!dof) continue;
554ca3d3a14SMatthew G. Knepley       if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);}
555ca3d3a14SMatthew G. Knepley       d += dof;
556ca3d3a14SMatthew G. Knepley     }
557ca3d3a14SMatthew G. Knepley   }
558ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
559ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
560ca3d3a14SMatthew G. Knepley }
561ca3d3a14SMatthew G. Knepley 
562ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a)
563ca3d3a14SMatthew G. Knepley {
564ca3d3a14SMatthew G. Knepley   PetscSection    s;
565ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
566ca3d3a14SMatthew G. Knepley   IS              clPoints;
567ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
568ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
5698bdb3c71SMatthew G. Knepley   PetscInt        Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c = 0;
570ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
571ca3d3a14SMatthew G. Knepley 
572ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
57392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
574ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
575ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
576ca3d3a14SMatthew G. Knepley   for (f = 0, r = 0; f < Nf; ++f) {
577ca3d3a14SMatthew G. Knepley     for (cpf = 0; cpf < Np*2; cpf += 2) {
578ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr);
579ca3d3a14SMatthew G. Knepley       for (g = 0, c = 0; g < Nf; ++g) {
580ca3d3a14SMatthew G. Knepley         for (cpg = 0; cpg < Np*2; cpg += 2) {
581ca3d3a14SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr);
582ca3d3a14SMatthew G. Knepley           ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr);
583ca3d3a14SMatthew G. Knepley           c += gdof;
584ca3d3a14SMatthew G. Knepley         }
585ca3d3a14SMatthew G. Knepley       }
586ca3d3a14SMatthew G. Knepley       if (c != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda);
587ca3d3a14SMatthew G. Knepley       r += fdof;
588ca3d3a14SMatthew G. Knepley     }
589ca3d3a14SMatthew G. Knepley   }
590ca3d3a14SMatthew G. Knepley   if (r != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda);
591ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
592ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
593ca3d3a14SMatthew G. Knepley }
594ca3d3a14SMatthew G. Knepley 
595ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g)
596ca3d3a14SMatthew G. Knepley {
597ca3d3a14SMatthew G. Knepley   DM                 tdm;
598ca3d3a14SMatthew G. Knepley   Vec                tv;
599ca3d3a14SMatthew G. Knepley   PetscSection       ts, s;
600ca3d3a14SMatthew G. Knepley   const PetscScalar *ta;
601ca3d3a14SMatthew G. Knepley   PetscScalar       *a, *va;
602ca3d3a14SMatthew G. Knepley   PetscInt           pStart, pEnd, p, Nf, f;
603ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
604ca3d3a14SMatthew G. Knepley 
605ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
606ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
607ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
60892fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
60992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
610ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
611ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
612ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(lv, &a);CHKERRQ(ierr);
613ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
614ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
615ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
616ca3d3a14SMatthew G. Knepley       ierr = DMPlexPointLocalFieldRef(dm, p, f, a, (void *) &va);CHKERRQ(ierr);
617ca3d3a14SMatthew G. Knepley       ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr);
618ca3d3a14SMatthew G. Knepley     }
619ca3d3a14SMatthew G. Knepley   }
620ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr);
621ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
622ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
623ca3d3a14SMatthew G. Knepley }
624ca3d3a14SMatthew G. Knepley 
625ca3d3a14SMatthew G. Knepley /*@
626ca3d3a14SMatthew G. Knepley   DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis
627ca3d3a14SMatthew G. Knepley 
628ca3d3a14SMatthew G. Knepley   Input Parameters:
629ca3d3a14SMatthew G. Knepley + dm - The DM
630ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis
631ca3d3a14SMatthew G. Knepley 
632ca3d3a14SMatthew G. Knepley   Output Parameters:
633ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis
634ca3d3a14SMatthew G. Knepley 
635c0f8e1fdSMatthew 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.
636c0f8e1fdSMatthew G. Knepley 
637ca3d3a14SMatthew G. Knepley   Level: developer
638ca3d3a14SMatthew G. Knepley 
639436bc73aSJed Brown .seealso: DMPlexLocalToGlobalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
640ca3d3a14SMatthew G. Knepley @*/
641ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv)
642ca3d3a14SMatthew G. Knepley {
643ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
644ca3d3a14SMatthew G. Knepley 
645ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
646ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
647ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
648ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr);
649ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
650ca3d3a14SMatthew G. Knepley }
651ca3d3a14SMatthew G. Knepley 
652ca3d3a14SMatthew G. Knepley /*@
653ca3d3a14SMatthew G. Knepley   DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis
654ca3d3a14SMatthew G. Knepley 
655ca3d3a14SMatthew G. Knepley   Input Parameters:
656ca3d3a14SMatthew G. Knepley + dm - The DM
657ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis
658ca3d3a14SMatthew G. Knepley 
659ca3d3a14SMatthew G. Knepley   Output Parameters:
660ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis
661ca3d3a14SMatthew G. Knepley 
662c0f8e1fdSMatthew 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.
663c0f8e1fdSMatthew G. Knepley 
664ca3d3a14SMatthew G. Knepley   Level: developer
665ca3d3a14SMatthew G. Knepley 
666436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
667ca3d3a14SMatthew G. Knepley @*/
668ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv)
669ca3d3a14SMatthew G. Knepley {
670ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
671ca3d3a14SMatthew G. Knepley 
672ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
673ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
674ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
675ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr);
676ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
677ca3d3a14SMatthew G. Knepley }
678ca3d3a14SMatthew G. Knepley 
679ca3d3a14SMatthew G. Knepley /*@
680ca3d3a14SMatthew G. Knepley   DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions
681ca3d3a14SMatthew G. Knepley     and global solutions, to a local basis, appropriate for discretization integrals and assembly.
682ca3d3a14SMatthew G. Knepley 
683ca3d3a14SMatthew G. Knepley   Input Parameters:
684ca3d3a14SMatthew G. Knepley + dm    - The DM
685ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one
686ca3d3a14SMatthew G. Knepley . beta  - The second Euler angle
687f0fc11ceSJed Brown - gamma - The third Euler angle
688ca3d3a14SMatthew G. Knepley 
689ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
690ca3d3a14SMatthew 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:
691ca3d3a14SMatthew 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.
692ca3d3a14SMatthew 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.
693ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
694ca3d3a14SMatthew G. Knepley 
695ca3d3a14SMatthew G. Knepley   Level: developer
696ca3d3a14SMatthew G. Knepley 
697ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()
698ca3d3a14SMatthew G. Knepley @*/
699ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma)
700ca3d3a14SMatthew G. Knepley {
701ca3d3a14SMatthew G. Knepley   RotCtx        *rc;
702ca3d3a14SMatthew G. Knepley   PetscInt       cdim;
703ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
704ca3d3a14SMatthew G. Knepley 
705ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
706ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr);
707ca3d3a14SMatthew G. Knepley   dm->transformCtx       = rc;
708ca3d3a14SMatthew G. Knepley   dm->transformSetUp     = DMPlexBasisTransformSetUp_Rotation_Internal;
709ca3d3a14SMatthew G. Knepley   dm->transformDestroy   = DMPlexBasisTransformDestroy_Rotation_Internal;
710ca3d3a14SMatthew G. Knepley   dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal;
711ca3d3a14SMatthew G. Knepley   rc->dim   = cdim;
712ca3d3a14SMatthew G. Knepley   rc->alpha = alpha;
713ca3d3a14SMatthew G. Knepley   rc->beta  = beta;
714ca3d3a14SMatthew G. Knepley   rc->gamma = gamma;
715ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr);
716ca3d3a14SMatthew G. Knepley   ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr);
717ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
718ca3d3a14SMatthew G. Knepley }
719ca3d3a14SMatthew G. Knepley 
720b278463cSMatthew G. Knepley /*@C
721ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector using a function of the coordinates
722b278463cSMatthew G. Knepley 
723b278463cSMatthew G. Knepley   Input Parameters:
724b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
725b278463cSMatthew G. Knepley . time   - The time
726b278463cSMatthew G. Knepley . field  - The field to constrain
7271c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7281c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
729b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
730b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
731b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
732b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
733b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
734b278463cSMatthew G. Knepley 
735b278463cSMatthew G. Knepley   Output Parameter:
736b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
737b278463cSMatthew G. Knepley 
738b278463cSMatthew G. Knepley   Level: developer
739b278463cSMatthew G. Knepley 
740ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
741b278463cSMatthew G. Knepley @*/
7421c531cf8SMatthew 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)
74355f2e967SMatthew G. Knepley {
7440163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
74555f2e967SMatthew G. Knepley   void            **ctxs;
746d7ddef95SMatthew G. Knepley   PetscInt          numFields;
747d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
748d7ddef95SMatthew G. Knepley 
749d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
750d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
751d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
752d7ddef95SMatthew G. Knepley   funcs[field] = func;
753d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
7541c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
755d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
756d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
757d7ddef95SMatthew G. Knepley }
758d7ddef95SMatthew G. Knepley 
759b278463cSMatthew G. Knepley /*@C
760ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector using a function of the coordinates and field data
761b278463cSMatthew G. Knepley 
762b278463cSMatthew G. Knepley   Input Parameters:
763b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
764b278463cSMatthew G. Knepley . time   - The time
765b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
766b278463cSMatthew G. Knepley . field  - The field to constrain
7671c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7681c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
769b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
770b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
771b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
772b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
773b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
774b278463cSMatthew G. Knepley 
775b278463cSMatthew G. Knepley   Output Parameter:
776b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
777b278463cSMatthew G. Knepley 
778b278463cSMatthew G. Knepley   Level: developer
779b278463cSMatthew G. Knepley 
780ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
781b278463cSMatthew G. Knepley @*/
7821c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
783c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
784c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
785c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78697b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
787b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
788b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
789c60e475cSMatthew G. Knepley {
790c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
791c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
792c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79397b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
794c60e475cSMatthew G. Knepley   void            **ctxs;
795c60e475cSMatthew G. Knepley   PetscInt          numFields;
796c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
797c60e475cSMatthew G. Knepley 
798c60e475cSMatthew G. Knepley   PetscFunctionBegin;
799c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
800c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
801c60e475cSMatthew G. Knepley   funcs[field] = func;
802c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
8031c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
804c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
805c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
806c60e475cSMatthew G. Knepley }
807c60e475cSMatthew G. Knepley 
808b278463cSMatthew G. Knepley /*@C
809ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialBdField - Insert boundary values into a local vector using a function of the coodinates and boundary field data
810ece3a9fcSMatthew G. Knepley 
811ece3a9fcSMatthew G. Knepley   Collective on dm
812ece3a9fcSMatthew G. Knepley 
813ece3a9fcSMatthew G. Knepley   Input Parameters:
814ece3a9fcSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
815ece3a9fcSMatthew G. Knepley . time   - The time
816ece3a9fcSMatthew G. Knepley . locU   - A local vector with the input solution values
817ece3a9fcSMatthew G. Knepley . field  - The field to constrain
818ece3a9fcSMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
819ece3a9fcSMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
820ece3a9fcSMatthew G. Knepley . label  - The DMLabel defining constrained points
821ece3a9fcSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
822ece3a9fcSMatthew G. Knepley . ids    - An array of ids for constrained points
823ece3a9fcSMatthew G. Knepley . func   - A pointwise function giving boundary values, the calling sequence is given in DMProjectBdFieldLabelLocal()
824ece3a9fcSMatthew G. Knepley - ctx    - An optional user context for bcFunc
825ece3a9fcSMatthew G. Knepley 
826ece3a9fcSMatthew G. Knepley   Output Parameter:
827ece3a9fcSMatthew G. Knepley . locX   - A local vector to receive the boundary values
828ece3a9fcSMatthew G. Knepley 
829ece3a9fcSMatthew G. Knepley   Level: developer
830ece3a9fcSMatthew G. Knepley 
831ece3a9fcSMatthew G. Knepley .seealso: DMProjectBdFieldLabelLocal(), DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
832ece3a9fcSMatthew G. Knepley @*/
833ece3a9fcSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialBdField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
834ece3a9fcSMatthew G. Knepley                                                           void (*func)(PetscInt, PetscInt, PetscInt,
835ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
836ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
837ece3a9fcSMatthew G. Knepley                                                                        PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[],
838ece3a9fcSMatthew G. Knepley                                                                        PetscScalar[]),
839ece3a9fcSMatthew G. Knepley                                                           void *ctx, Vec locX)
840ece3a9fcSMatthew G. Knepley {
841ece3a9fcSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
842ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
843ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
844ece3a9fcSMatthew G. Knepley                  PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
845ece3a9fcSMatthew G. Knepley   void            **ctxs;
846ece3a9fcSMatthew G. Knepley   PetscInt          numFields;
847ece3a9fcSMatthew G. Knepley   PetscErrorCode    ierr;
848ece3a9fcSMatthew G. Knepley 
849ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
850ece3a9fcSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
851ece3a9fcSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
852ece3a9fcSMatthew G. Knepley   funcs[field] = func;
853ece3a9fcSMatthew G. Knepley   ctxs[field]  = ctx;
854ece3a9fcSMatthew G. Knepley   ierr = DMProjectBdFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
855ece3a9fcSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
856ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
857ece3a9fcSMatthew G. Knepley }
858ece3a9fcSMatthew G. Knepley 
859ece3a9fcSMatthew G. Knepley /*@C
860b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
861b278463cSMatthew G. Knepley 
862b278463cSMatthew G. Knepley   Input Parameters:
863b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
864b278463cSMatthew G. Knepley . time   - The time
865b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
866b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
867b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
868b278463cSMatthew G. Knepley . field  - The field to constrain
8691c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
8701c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
871b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
872b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
873b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
874b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
875b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
876b278463cSMatthew G. Knepley 
877b278463cSMatthew G. Knepley   Output Parameter:
878b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
879b278463cSMatthew G. Knepley 
880b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
881b278463cSMatthew G. Knepley 
882b278463cSMatthew G. Knepley   Level: developer
883b278463cSMatthew G. Knepley 
884b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
885b278463cSMatthew G. Knepley @*/
8861c531cf8SMatthew 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[],
887b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
888d7ddef95SMatthew G. Knepley {
88961f58d28SMatthew G. Knepley   PetscDS            prob;
890da97024aSMatthew G. Knepley   PetscSF            sf;
891d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
89220369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
893da97024aSMatthew G. Knepley   const PetscInt    *leaves;
894d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
895520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
896e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
897d7ddef95SMatthew G. Knepley 
898d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
899da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
900da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
901da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
902d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
903d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
90461f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
905d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
906d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
90720369375SToby Isaac   if (cellGeometry) {
908d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
909d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
91020369375SToby Isaac   }
911d7ddef95SMatthew G. Knepley   if (Grad) {
912c0a6632aSMatthew G. Knepley     PetscFV fv;
913c0a6632aSMatthew G. Knepley 
914c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
915d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
916d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
917d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
91869291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
919d7ddef95SMatthew G. Knepley   }
920d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
921d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
922d7ddef95SMatthew G. Knepley     IS              faceIS;
923d7ddef95SMatthew G. Knepley     const PetscInt *faces;
924d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
925d7ddef95SMatthew G. Knepley 
926d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
927d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
928d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
929d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
930d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
931d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
932640bce14SSatish Balay       PetscFVFaceGeom        *fg;
933d7ddef95SMatthew G. Knepley 
934d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
935da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
936da97024aSMatthew G. Knepley       if (loc >= 0) continue;
937d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
938d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
939d7ddef95SMatthew G. Knepley       if (Grad) {
940640bce14SSatish Balay         PetscFVCellGeom       *cg;
941640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
942d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
943d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
944d7ddef95SMatthew G. Knepley         PetscInt               d;
945d7ddef95SMatthew G. Knepley 
946d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
947d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
948d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
949520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
950d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
951d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
952e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
953e735a8a9SMatthew G. Knepley         if (ierru) {
954e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
955e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
956e735a8a9SMatthew G. Knepley           goto cleanup;
957e735a8a9SMatthew G. Knepley         }
958d7ddef95SMatthew G. Knepley       } else {
959640bce14SSatish Balay         PetscScalar       *xI;
960d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
961d7ddef95SMatthew G. Knepley 
962d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
963520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
964e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
965e735a8a9SMatthew G. Knepley         if (ierru) {
966e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
967e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
968e735a8a9SMatthew G. Knepley           goto cleanup;
969e735a8a9SMatthew G. Knepley         }
970d7ddef95SMatthew G. Knepley       }
971d7ddef95SMatthew G. Knepley     }
972d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
973d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
974d7ddef95SMatthew G. Knepley   }
975e735a8a9SMatthew G. Knepley   cleanup:
976d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
977d7ddef95SMatthew G. Knepley   if (Grad) {
97869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
979d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
980d7ddef95SMatthew G. Knepley   }
981e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
982d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
983e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
984d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
985d7ddef95SMatthew G. Knepley }
986d7ddef95SMatthew G. Knepley 
987*0c364540SMatthew G. Knepley static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
988*0c364540SMatthew G. Knepley {
989*0c364540SMatthew G. Knepley   PetscInt c;
990*0c364540SMatthew G. Knepley   for (c = 0; c < Nc; ++c) u[c] = 0.0;
991*0c364540SMatthew G. Knepley   return 0;
992*0c364540SMatthew G. Knepley }
993*0c364540SMatthew G. Knepley 
994f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
995d7ddef95SMatthew G. Knepley {
996*0c364540SMatthew G. Knepley   PetscObject    isZero;
997e5e52638SMatthew G. Knepley   PetscDS        prob;
998d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
99955f2e967SMatthew G. Knepley   PetscErrorCode ierr;
100055f2e967SMatthew G. Knepley 
100155f2e967SMatthew G. Knepley   PetscFunctionBegin;
1002e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1003e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
1004*0c364540SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
100555f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
1006f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
1007648eda8cSMatthew G. Knepley     const char             *name, *labelname;
1008d7ddef95SMatthew G. Knepley     DMLabel                 label;
10091c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
10101c531cf8SMatthew G. Knepley     const PetscInt         *comps;
1011d7ddef95SMatthew G. Knepley     PetscObject             obj;
1012d7ddef95SMatthew G. Knepley     PetscClassId            id;
1013a30ec4eaSSatish Balay     void                    (*func)(void);
1014d7ddef95SMatthew G. Knepley     PetscInt                numids;
1015d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
101655f2e967SMatthew G. Knepley     void                   *ctx;
101755f2e967SMatthew G. Knepley 
1018648eda8cSMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
1019f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
1020c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
1021648eda8cSMatthew G. Knepley     if (!label) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "Label %s for boundary condition %s does not exist in the DM", labelname, name);
102244a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1023d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1024d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1025c60e475cSMatthew G. Knepley       switch (type) {
1026c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
1027c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
1028*0c364540SMatthew G. Knepley         if (isZero) func = (void (*)(void)) zero;
1029092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
10301c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, (PetscErrorCode (*)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *)) func, ctx, locX);CHKERRQ(ierr);
1031092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
1032c60e475cSMatthew G. Knepley         break;
1033c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
1034c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
10351c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
1036b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
1037c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
103897b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
1039c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
1040c60e475cSMatthew G. Knepley         break;
1041c60e475cSMatthew G. Knepley       default: break;
1042c60e475cSMatthew G. Knepley       }
1043d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
104443ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
10451c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
1046b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
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 
1052f1d73a7aSMatthew G. Knepley /*@
1053f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
1054f1d73a7aSMatthew G. Knepley 
1055f1d73a7aSMatthew G. Knepley   Input Parameters:
1056f1d73a7aSMatthew G. Knepley + dm - The DM
1057f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
1058f1d73a7aSMatthew G. Knepley . time - The time
1059f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
1060f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
1061f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
1062f1d73a7aSMatthew G. Knepley 
1063f1d73a7aSMatthew G. Knepley   Output Parameters:
1064f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
1065f1d73a7aSMatthew G. Knepley 
1066f1d73a7aSMatthew G. Knepley   Level: developer
1067f1d73a7aSMatthew G. Knepley 
1068f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
1069f1d73a7aSMatthew G. Knepley @*/
1070f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
1071f1d73a7aSMatthew G. Knepley {
1072f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
1073f1d73a7aSMatthew G. Knepley 
1074f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
1075f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1076f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
1077f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
1078f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
1079f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
1080f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
1081f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
1082f1d73a7aSMatthew G. Knepley }
1083f1d73a7aSMatthew G. Knepley 
10840709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
1085cb1e1211SMatthew G Knepley {
1086574a98acSMatthew G. Knepley   Vec              localX;
1087574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
1088574a98acSMatthew G. Knepley 
1089574a98acSMatthew G. Knepley   PetscFunctionBegin;
1090574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
10915d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
1092574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1093574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1094574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
1095574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1096574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
1097574a98acSMatthew G. Knepley }
1098574a98acSMatthew G. Knepley 
1099574a98acSMatthew G. Knepley /*@C
1100ca3d3a14SMatthew G. Knepley   DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
1101574a98acSMatthew G. Knepley 
1102d083f849SBarry Smith   Collective on dm
1103c0f8e1fdSMatthew G. Knepley 
1104574a98acSMatthew G. Knepley   Input Parameters:
1105574a98acSMatthew G. Knepley + dm     - The DM
1106574a98acSMatthew G. Knepley . time   - The time
1107574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
1108574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
1109574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
1110574a98acSMatthew G. Knepley 
1111574a98acSMatthew G. Knepley   Output Parameter:
1112574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
1113574a98acSMatthew G. Knepley 
1114574a98acSMatthew G. Knepley   Level: developer
1115574a98acSMatthew G. Knepley 
1116574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
1117574a98acSMatthew G. Knepley @*/
1118574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
1119574a98acSMatthew G. Knepley {
11200f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1121ca3d3a14SMatthew G. Knepley   DM               tdm;
1122ca3d3a14SMatthew G. Knepley   Vec              tv;
1123cb1e1211SMatthew G Knepley   PetscSection     section;
1124c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
11254bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
112615496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
11274bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
1128cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
11297318780aSToby Isaac   const PetscReal *quadWeights;
1130412e9a14SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, c, field, fieldOffset;
1131ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1132cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1133cb1e1211SMatthew G Knepley 
1134cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1135c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11367318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
11372a4e142eSMatthew G. Knepley   fegeom.dimEmbed = coordDim;
113892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1139cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1140ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1141ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1142ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1143cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
114415496722SMatthew G. Knepley     PetscObject  obj;
114515496722SMatthew G. Knepley     PetscClassId id;
1146c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
1147c5bbbd5bSMatthew G. Knepley 
114844a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
114915496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
115015496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
115115496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
115215496722SMatthew G. Knepley 
11530f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
11540f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
115515496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
115615496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
115715496722SMatthew G. Knepley 
115815496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
115915496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1160ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1161c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
1162cb1e1211SMatthew G Knepley   }
11639c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1164beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
11652a4e142eSMatthew 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);
1166aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1167412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1168cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
1169a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
1170cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
11719c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1172cb1e1211SMatthew G Knepley 
11732a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1174cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1175cb1e1211SMatthew G Knepley 
117615496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
117715496722SMatthew G. Knepley       PetscObject  obj;
117815496722SMatthew G. Knepley       PetscClassId id;
1179c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
118015496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1181cb1e1211SMatthew G Knepley 
118244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
118315496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
118415496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
118515496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1186ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1187cb1e1211SMatthew G Knepley       if (debug) {
1188cb1e1211SMatthew G Knepley         char title[1024];
1189ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
11904c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1191cb1e1211SMatthew G Knepley       }
11927318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
11932a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
11942a4e142eSMatthew G. Knepley 
11952a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
11962a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
11972a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
11982a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1199ff1e0c32SBarry 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);
1200d3a7d86cSMatthew G. Knepley         if (transform) {
1201d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1202d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1203d3a7d86cSMatthew G. Knepley         } else {
1204d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1205d3a7d86cSMatthew G. Knepley         }
1206ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1207e735a8a9SMatthew G. Knepley         if (ierr) {
1208e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1209e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1210e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
12112a4e142eSMatthew G. Knepley           ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1212e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1213e735a8a9SMatthew G. Knepley         }
1214ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
12152a4e142eSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
121615496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1217ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
121815496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1219beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
122080dbbc5dSMatthew 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);}
12214bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1222cb1e1211SMatthew G Knepley         }
1223cb1e1211SMatthew G Knepley       }
12249c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
1225beaa55a6SMatthew G. Knepley       qc += Nc;
1226cb1e1211SMatthew G Knepley     }
1227cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1228ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
1229cb1e1211SMatthew G Knepley     localDiff += elemDiff;
1230cb1e1211SMatthew G Knepley   }
12312a4e142eSMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1232b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1233cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
1234cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1235cb1e1211SMatthew G Knepley }
1236cb1e1211SMatthew G Knepley 
1237b698f381SToby 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)
1238cb1e1211SMatthew G Knepley {
12390f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1240ca3d3a14SMatthew G. Knepley   DM               tdm;
1241cb1e1211SMatthew G Knepley   PetscSection     section;
124240e14135SMatthew G. Knepley   PetscQuadrature  quad;
1243ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
12449c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
12452a4e142eSMatthew G. Knepley   const PetscReal *quadWeights;
12464bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
12474bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
124840e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
1249485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset;
1250ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1251cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1252cb1e1211SMatthew G Knepley 
1253cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1254c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12557318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
12564bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
125792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
125840e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
125940e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
126040e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
126140e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1262ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1263ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1264ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1265652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
12660f2d7e86SMatthew G. Knepley     PetscFE  fe;
126740e14135SMatthew G. Knepley     PetscInt Nc;
1268652b88e8SMatthew G. Knepley 
126944a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
12700f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12710f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
127240e14135SMatthew G. Knepley     numComponents += Nc;
1273652b88e8SMatthew G. Knepley   }
12742a4e142eSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1275beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
12764d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
12774bee2e38SMatthew 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);
1278412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
127940e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
128040e14135SMatthew G. Knepley     PetscScalar *x = NULL;
128140e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
12829c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1283652b88e8SMatthew G. Knepley 
12844bee2e38SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
128540e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
128640e14135SMatthew G. Knepley 
12879c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
12880f2d7e86SMatthew G. Knepley       PetscFE          fe;
128951259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
12909c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
129140e14135SMatthew G. Knepley 
129244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
12930f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
12949c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
129540e14135SMatthew G. Knepley       if (debug) {
129640e14135SMatthew G. Knepley         char title[1024];
1297ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
12989c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1299652b88e8SMatthew G. Knepley       }
13009c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
13012a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
13022a4e142eSMatthew G. Knepley 
13032a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
13042a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
13052a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
13062a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1307ff1e0c32SBarry 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);
1308d3a7d86cSMatthew G. Knepley         if (transform) {
1309d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1310d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1311d3a7d86cSMatthew G. Knepley         } else {
1312d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1313d3a7d86cSMatthew G. Knepley         }
13144bee2e38SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, n, Nc, funcVal, ctx);
1315e735a8a9SMatthew G. Knepley         if (ierr) {
1316e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1317e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1318e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
13194bee2e38SMatthew G. Knepley           ierr2 = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr2);
1320e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1321e735a8a9SMatthew G. Knepley         }
1322ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
13232a4e142eSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);
13244bee2e38SMatthew G. Knepley         /* Overwrite with the dot product if the normal is given */
13254bee2e38SMatthew G. Knepley         if (n) {
13264bee2e38SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
13274bee2e38SMatthew G. Knepley             PetscScalar sum = 0.0;
13284bee2e38SMatthew G. Knepley             PetscInt    d;
13294bee2e38SMatthew G. Knepley             for (d = 0; d < dim; ++d) sum += interpolant[fc*dim+d]*n[d];
13304bee2e38SMatthew G. Knepley             interpolant[fc] = sum;
13314bee2e38SMatthew G. Knepley           }
13324bee2e38SMatthew G. Knepley         }
13339c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1334beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1335ff1e0c32SBarry 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);}
13364bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
133740e14135SMatthew G. Knepley         }
133840e14135SMatthew G. Knepley       }
13399c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
13409c3cf19fSMatthew G. Knepley       qc          += Nc;
134140e14135SMatthew G. Knepley     }
134240e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1343ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
134440e14135SMatthew G. Knepley     localDiff += elemDiff;
134540e14135SMatthew G. Knepley   }
13464bee2e38SMatthew G. Knepley   ierr  = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr);
134740e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1348b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
134940e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1350cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1351cb1e1211SMatthew G Knepley }
1352cb1e1211SMatthew G Knepley 
1353c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
135473d901b8SMatthew G. Knepley {
13550f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1356ca3d3a14SMatthew G. Knepley   DM               tdm;
1357083401c6SMatthew G. Knepley   DMLabel          depthLabel;
135873d901b8SMatthew G. Knepley   PetscSection     section;
1359ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
136073d901b8SMatthew G. Knepley   PetscReal       *localDiff;
1361083401c6SMatthew G. Knepley   PetscInt         dim, depth, dE, Nf, f, Nds, s;
1362ca3d3a14SMatthew G. Knepley   PetscBool        transform;
136373d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
136473d901b8SMatthew G. Knepley 
136573d901b8SMatthew G. Knepley   PetscFunctionBegin;
1366c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1367083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
136892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
136973d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1370083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1371083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1372083401c6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1373083401c6SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1374083401c6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1375083401c6SMatthew G. Knepley   ierr = DMLabelGetNumValues(depthLabel, &depth);CHKERRQ(ierr);
1376083401c6SMatthew G. Knepley 
1377ca3d3a14SMatthew G. Knepley   ierr = VecSet(localX, 0.0);CHKERRQ(ierr);
137873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
137973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1380ca3d3a14SMatthew G. Knepley   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1381083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
1382083401c6SMatthew G. Knepley   ierr = PetscCalloc1(Nf, &localDiff);CHKERRQ(ierr);
1383083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
1384083401c6SMatthew G. Knepley     PetscDS          ds;
1385083401c6SMatthew G. Knepley     DMLabel          label;
1386083401c6SMatthew G. Knepley     IS               fieldIS, pointIS;
1387083401c6SMatthew G. Knepley     const PetscInt  *fields, *points = NULL;
1388083401c6SMatthew G. Knepley     PetscQuadrature  quad;
1389083401c6SMatthew G. Knepley     const PetscReal *quadPoints, *quadWeights;
1390083401c6SMatthew G. Knepley     PetscFEGeom      fegeom;
1391083401c6SMatthew G. Knepley     PetscReal       *coords, *gcoords;
1392083401c6SMatthew G. Knepley     PetscScalar     *funcVal, *interpolant;
139396959cd1SMatthew G. Knepley     PetscBool        isHybrid;
1394083401c6SMatthew G. Knepley     PetscInt         qNc, Nq, totNc, cStart = 0, cEnd, c, dsNf;
139573d901b8SMatthew G. Knepley 
1396083401c6SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
1397083401c6SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
139896959cd1SMatthew G. Knepley     ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
1399083401c6SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
1400083401c6SMatthew G. Knepley     ierr = PetscDSGetTotalComponents(ds, &totNc);CHKERRQ(ierr);
1401083401c6SMatthew G. Knepley     ierr = PetscDSGetQuadrature(ds, &quad);CHKERRQ(ierr);
14029c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1403083401c6SMatthew G. Knepley     if ((qNc != 1) && (qNc != totNc)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, totNc);
1404083401c6SMatthew 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);
1405083401c6SMatthew G. Knepley     if (!label) {
1406412e9a14SMatthew G. Knepley       ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1407083401c6SMatthew G. Knepley     } else {
1408083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
1409083401c6SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &cEnd);CHKERRQ(ierr);
1410083401c6SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1411083401c6SMatthew G. Knepley     }
141273d901b8SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
1413083401c6SMatthew G. Knepley       const PetscInt cell = points ? points[c] : c;
141473d901b8SMatthew G. Knepley       PetscScalar   *x    = NULL;
141596959cd1SMatthew G. Knepley       PetscInt       qc   = 0, fOff = 0, dep, fStart = isHybrid ? dsNf-1 : 0;
141673d901b8SMatthew G. Knepley 
1417083401c6SMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, cell, &dep);CHKERRQ(ierr);
1418083401c6SMatthew G. Knepley       if (dep != depth-1) continue;
141996959cd1SMatthew G. Knepley       if (isHybrid) {
142096959cd1SMatthew G. Knepley         const PetscInt *cone;
142196959cd1SMatthew G. Knepley 
142296959cd1SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
142396959cd1SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cone[0], quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
142496959cd1SMatthew G. Knepley       } else {
1425083401c6SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
142696959cd1SMatthew G. Knepley       }
1427083401c6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
142896959cd1SMatthew G. Knepley       for (f = fStart; f < dsNf; ++f) {
142915496722SMatthew G. Knepley         PetscObject  obj;
143015496722SMatthew G. Knepley         PetscClassId id;
1431083401c6SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[fields[f]] : NULL;
143215496722SMatthew G. Knepley         PetscInt     Nb, Nc, q, fc;
143315496722SMatthew G. Knepley         PetscReal    elemDiff = 0.0;
143415496722SMatthew G. Knepley 
1435083401c6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
143615496722SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
143715496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
143815496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1439083401c6SMatthew G. Knepley         else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
144073d901b8SMatthew G. Knepley         if (debug) {
144173d901b8SMatthew G. Knepley           char title[1024];
1442083401c6SMatthew G. Knepley           ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", fields[f]);CHKERRQ(ierr);
1443083401c6SMatthew G. Knepley           ierr = DMPrintCellVector(cell, title, Nb, &x[fOff]);CHKERRQ(ierr);
144473d901b8SMatthew G. Knepley         }
14457318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
14462a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
14472a4e142eSMatthew G. Knepley 
14482a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
1449083401c6SMatthew G. Knepley           qgeom.J        = &fegeom.J[q*dE*dE];
1450083401c6SMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*dE*dE];
14512a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
1452083401c6SMatthew 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);
1453d3a7d86cSMatthew G. Knepley           if (transform) {
1454083401c6SMatthew G. Knepley             gcoords = &coords[dE*Nq];
1455083401c6SMatthew G. Knepley             ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[dE*q], PETSC_TRUE, dE, &coords[dE*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1456d3a7d86cSMatthew G. Knepley           } else {
1457083401c6SMatthew G. Knepley             gcoords = &coords[dE*q];
1458d3a7d86cSMatthew G. Knepley           }
1459083401c6SMatthew G. Knepley           ierr = (*funcs[fields[f]])(dE, time, gcoords, Nc, funcVal, ctx);
1460e735a8a9SMatthew G. Knepley           if (ierr) {
1461e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1462083401c6SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr2);
1463e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1464083401c6SMatthew G. Knepley             ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1465e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1466e735a8a9SMatthew G. Knepley           }
1467083401c6SMatthew G. Knepley           if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[dE*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
146896959cd1SMatthew G. Knepley           /* Call once for each face, except for lagrange field */
1469083401c6SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fOff], &qgeom, q, interpolant);CHKERRQ(ierr);}
1470083401c6SMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fOff], q, interpolant);CHKERRQ(ierr);}
1471083401c6SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
147215496722SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1473beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1474083401c6SMatthew 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);}
14754bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
147673d901b8SMatthew G. Knepley           }
147773d901b8SMatthew G. Knepley         }
1478083401c6SMatthew G. Knepley         fOff += Nb;
14799c3cf19fSMatthew G. Knepley         qc   += Nc;
1480083401c6SMatthew G. Knepley         localDiff[fields[f]] += elemDiff;
1481083401c6SMatthew 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);}
148273d901b8SMatthew G. Knepley       }
1483083401c6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
1484083401c6SMatthew G. Knepley     }
1485083401c6SMatthew G. Knepley     if (label) {
1486083401c6SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1487083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1488083401c6SMatthew G. Knepley     }
1489083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
1490083401c6SMatthew G. Knepley     ierr = PetscFree6(funcVal, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ);CHKERRQ(ierr);
149173d901b8SMatthew G. Knepley   }
149273d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1493083401c6SMatthew G. Knepley   ierr = MPIU_Allreduce(localDiff, diff, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1494083401c6SMatthew G. Knepley   ierr = PetscFree(localDiff);CHKERRQ(ierr);
1495083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) diff[f] = PetscSqrtReal(diff[f]);
149673d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
149773d901b8SMatthew G. Knepley }
149873d901b8SMatthew G. Knepley 
1499e729f68cSMatthew G. Knepley /*@C
1500e729f68cSMatthew 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.
1501e729f68cSMatthew G. Knepley 
1502d083f849SBarry Smith   Collective on dm
1503c0f8e1fdSMatthew G. Knepley 
1504e729f68cSMatthew G. Knepley   Input Parameters:
1505e729f68cSMatthew G. Knepley + dm    - The DM
15060163fd50SMatthew G. Knepley . time  - The time
1507ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1508e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1509e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1510e729f68cSMatthew G. Knepley 
1511e729f68cSMatthew G. Knepley   Output Parameter:
1512e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1513e729f68cSMatthew G. Knepley 
1514e729f68cSMatthew G. Knepley   Level: developer
1515e729f68cSMatthew G. Knepley 
1516b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1517e729f68cSMatthew G. Knepley @*/
15180163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1519e729f68cSMatthew G. Knepley {
1520e729f68cSMatthew G. Knepley   PetscSection     section;
1521e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1522e729f68cSMatthew G. Knepley   Vec              localX;
15234bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
1524e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
15254bee2e38SMatthew G. Knepley   PetscReal       *coords;
1526e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1527485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, c, field, fieldOffset;
1528e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1529e729f68cSMatthew G. Knepley 
1530e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1531e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1532e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15337318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
153492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1535e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1536e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1537bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1538e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1539e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1540e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1541e729f68cSMatthew G. Knepley     PetscObject  obj;
1542e729f68cSMatthew G. Knepley     PetscClassId id;
1543e729f68cSMatthew G. Knepley     PetscInt     Nc;
1544e729f68cSMatthew G. Knepley 
154544a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1546e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1547e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1548e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1549e729f68cSMatthew G. Knepley 
1550e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1551e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1552e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1553e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1554e729f68cSMatthew G. Knepley 
1555e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1556e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1557ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1558e729f68cSMatthew G. Knepley     numComponents += Nc;
1559e729f68cSMatthew G. Knepley   }
15609c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1561beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
15622a4e142eSMatthew 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);
1563412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1564e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1565e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
15666f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
15679c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1568e729f68cSMatthew G. Knepley 
15692a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1570e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1571e729f68cSMatthew G. Knepley 
1572e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1573e729f68cSMatthew G. Knepley       PetscObject  obj;
1574e729f68cSMatthew G. Knepley       PetscClassId id;
1575e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1576e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1577e729f68cSMatthew G. Knepley 
157844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1579e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1580e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1581e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1582ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
158323f34ed2SToby Isaac       if (funcs[field]) {
15847318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
15852a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
15862a4e142eSMatthew G. Knepley 
15872a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
15882a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
15892a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
15902a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
15914bee2e38SMatthew 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);
1592274e8aaeSMatthew G. Knepley           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);
1593e735a8a9SMatthew G. Knepley           if (ierr) {
1594e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1595e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
15962a4e142eSMatthew G. Knepley             ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1597e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1598e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1599e735a8a9SMatthew G. Knepley           }
16002a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1601e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1602ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1603e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1604beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
16054bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1606e729f68cSMatthew G. Knepley           }
1607e729f68cSMatthew G. Knepley         }
160823f34ed2SToby Isaac       }
1609beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
16109c3cf19fSMatthew G. Knepley       qc          += Nc;
1611e729f68cSMatthew G. Knepley     }
1612e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
161323f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1614e729f68cSMatthew G. Knepley   }
16152a4e142eSMatthew G. Knepley   ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1616e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1617e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1618e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1619e729f68cSMatthew G. Knepley }
1620e729f68cSMatthew G. Knepley 
16211555c271SMatthew G. Knepley /*@C
16221555c271SMatthew 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.
16231555c271SMatthew G. Knepley 
1624d083f849SBarry Smith   Collective on dm
1625c0f8e1fdSMatthew G. Knepley 
16261555c271SMatthew G. Knepley   Input Parameters:
16271555c271SMatthew G. Knepley + dm - The DM
16281555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
16291555c271SMatthew G. Knepley 
16301555c271SMatthew G. Knepley   Output Parameter:
16311555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
16321555c271SMatthew G. Knepley 
163395452b02SPatrick Sanan   Notes:
163495452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
16351555c271SMatthew 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
16361555c271SMatthew G. Knepley 
16371555c271SMatthew G. Knepley   Level: developer
16381555c271SMatthew G. Knepley 
16391555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
16401555c271SMatthew G. Knepley @*/
16411555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
16421555c271SMatthew G. Knepley {
1643db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1644db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
16451555c271SMatthew G. Knepley   DM               dmC;
16461555c271SMatthew G. Knepley   PetscSection     section;
16471555c271SMatthew G. Knepley   PetscQuadrature  quad;
16481555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
16494bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
16504bee2e38SMatthew G. Knepley   PetscReal       *coords;
16511555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1652485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, vStart, vEnd, v, field, fieldOffset;
16531555c271SMatthew G. Knepley   PetscErrorCode   ierr;
16541555c271SMatthew G. Knepley 
16551555c271SMatthew G. Knepley   PetscFunctionBegin;
16561555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
16571555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
16581555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16591555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
16604bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
166192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
16621555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
16631555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
16641555c271SMatthew G. Knepley     PetscObject  obj;
16651555c271SMatthew G. Knepley     PetscClassId id;
16661555c271SMatthew G. Knepley     PetscInt     Nc;
16671555c271SMatthew G. Knepley 
166844a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
16691555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
16701555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
16711555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
16721555c271SMatthew G. Knepley 
16731555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
16741555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
16751555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
16761555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
16771555c271SMatthew G. Knepley 
16781555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
16791555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1680ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
16811555c271SMatthew G. Knepley     numComponents += Nc;
16821555c271SMatthew G. Knepley   }
16831555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
16841555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
16854bee2e38SMatthew 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);
16861555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1687412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
16881555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
16891555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
16901555c271SMatthew G. Knepley     PetscInt   *star = NULL;
16911555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
16921555c271SMatthew G. Knepley 
1693580bdb30SBarry Smith     ierr = PetscArrayzero(gradsum, coordDim*numComponents);CHKERRQ(ierr);
16941555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
16951555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
16961555c271SMatthew G. Knepley       const PetscInt cell = star[st];
16971555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
16981555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
16991555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
17001555c271SMatthew G. Knepley 
17011555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
17024bee2e38SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
17031555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
17041555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
17051555c271SMatthew G. Knepley         PetscObject  obj;
17061555c271SMatthew G. Knepley         PetscClassId id;
17071555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
17081555c271SMatthew G. Knepley 
1709580bdb30SBarry Smith         ierr = PetscArrayzero(grad, coordDim*numComponents);CHKERRQ(ierr);
171044a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
17111555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
17121555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
17131555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1714ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
17151555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
17162a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
17172a4e142eSMatthew G. Knepley 
17182a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
17192a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
17202a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
17212a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
17224bee2e38SMatthew 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);
17231555c271SMatthew G. Knepley           if (ierr) {
17241555c271SMatthew G. Knepley             PetscErrorCode ierr2;
17251555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
17261555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
17274bee2e38SMatthew G. Knepley             ierr2 = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
17281555c271SMatthew G. Knepley             CHKERRQ(ierr);
17291555c271SMatthew G. Knepley           }
17302a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1731ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
17321555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
17331555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
17341555c271SMatthew G. Knepley 
17354bee2e38SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*fegeom.detJ[q];
17361555c271SMatthew G. Knepley           }
17374bee2e38SMatthew G. Knepley           vol += quadWeights[q*qNc]*fegeom.detJ[q];
17381555c271SMatthew G. Knepley         }
17391555c271SMatthew G. Knepley         fieldOffset += Nb;
17401555c271SMatthew G. Knepley         qc          += Nc;
17411555c271SMatthew G. Knepley       }
17421555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1743f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1744f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1745f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1746f8527842SMatthew G. Knepley         }
1747f8527842SMatthew G. Knepley       }
1748f8527842SMatthew G. Knepley       volsum += vol;
1749db1066baSMatthew G. Knepley       if (debug) {
17506d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
17511555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
17521555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
17531555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
17546d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
17551555c271SMatthew G. Knepley           }
17561555c271SMatthew G. Knepley         }
17571555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1758db1066baSMatthew G. Knepley       }
17591555c271SMatthew G. Knepley     }
17601555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
17611555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
17621555c271SMatthew G. Knepley     }
17631555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
17641555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
17651555c271SMatthew G. Knepley   }
17664bee2e38SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
17671555c271SMatthew G. Knepley   PetscFunctionReturn(0);
17681555c271SMatthew G. Knepley }
17691555c271SMatthew G. Knepley 
1770338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
177173d901b8SMatthew G. Knepley {
1772338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
177361aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
177473d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1775338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1776c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1777c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1778338f77d5SMatthew G. Knepley   /* DS */
1779338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1780338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1781338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1782338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1783338f77d5SMatthew G. Knepley   /* Geometry */
1784c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1785338f77d5SMatthew G. Knepley   DM                 dmGrad;
1786c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1787338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1788b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1789338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1790b7260050SToby Isaac   PetscInt           maxDegree;
1791c330f8ffSToby Isaac   DMField            coordField;
1792c330f8ffSToby Isaac   IS                 cellIS;
179373d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
179473d901b8SMatthew G. Knepley 
179573d901b8SMatthew G. Knepley   PetscFunctionBegin;
1796338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1797c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
179892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
179973d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1800338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1801b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1802b5a3613cSMatthew G. Knepley     PetscObject  obj;
1803b5a3613cSMatthew G. Knepley     PetscClassId id;
1804b5a3613cSMatthew G. Knepley 
1805b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1806b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1807338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1808338f77d5SMatthew G. Knepley   }
1809338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1810338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1811338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1812338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1813338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1814338f77d5SMatthew G. Knepley   /* Read DS information */
1815338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1816338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1817338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1818c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1819338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1820338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1821338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1822338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1823338f77d5SMatthew G. Knepley   if (dmAux) {
1824338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1825338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
182692fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
1827338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1828338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1829338f77d5SMatthew G. Knepley   }
1830338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1831338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1832338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1833338f77d5SMatthew G. Knepley   /* Read out geometry */
1834c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1835b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1836b7260050SToby Isaac   if (maxDegree <= 1) {
1837c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1838c330f8ffSToby Isaac     if (affineQuad) {
1839c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1840338f77d5SMatthew G. Knepley     }
1841b5a3613cSMatthew G. Knepley   }
1842b5a3613cSMatthew G. Knepley   if (useFVM) {
1843338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1844b5a3613cSMatthew G. Knepley     Vec       grad;
1845b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1846b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1847b5a3613cSMatthew G. Knepley 
1848338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1849338f77d5SMatthew G. Knepley       PetscObject  obj;
1850338f77d5SMatthew G. Knepley       PetscClassId id;
1851338f77d5SMatthew G. Knepley 
1852338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1853338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1854338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1855338f77d5SMatthew G. Knepley     }
1856338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1857338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1858b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1859338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1860338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1861b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1862b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1863b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1864b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1865338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1866b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1867b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1868b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1869b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1870b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1871b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1872338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1873b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1874b5a3613cSMatthew G. Knepley   }
1875338f77d5SMatthew G. Knepley   /* Read out data from inputs */
187673d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
187773d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
187873d901b8SMatthew G. Knepley     PetscInt     i;
187973d901b8SMatthew G. Knepley 
1880338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
18810f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1882338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
188373d901b8SMatthew G. Knepley     if (dmAux) {
1884338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
18850f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1886338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
188773d901b8SMatthew G. Knepley     }
188873d901b8SMatthew G. Knepley   }
1889338f77d5SMatthew G. Knepley   /* Do integration for each field */
189073d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1891c1f031eeSMatthew G. Knepley     PetscObject  obj;
1892c1f031eeSMatthew G. Knepley     PetscClassId id;
1893c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
189473d901b8SMatthew G. Knepley 
1895c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1896c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1897c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1898c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1899c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1900c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1901c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1902c1f031eeSMatthew G. Knepley 
19030f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1904c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
19059c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1906c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1907c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
190873d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
19090f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
191073d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
191173d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
191273d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
191373d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1914c330f8ffSToby Isaac       if (!affineQuad) {
1915c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1916c330f8ffSToby Isaac       }
1917c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
19184bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
1919c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
19204bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1921c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1922c330f8ffSToby Isaac       if (!affineQuad) {
1923c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1924c330f8ffSToby Isaac       }
1925c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1926c1f031eeSMatthew G. Knepley       PetscInt       foff;
1927420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1928b69edc29SMatthew G. Knepley       PetscScalar    lint;
1929c1f031eeSMatthew G. Knepley 
1930c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1931c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1932c1f031eeSMatthew G. Knepley       if (obj_func) {
1933c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1934b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1935b5a3613cSMatthew G. Knepley 
1936b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1937338f77d5SMatthew 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);
1938338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
193973d901b8SMatthew G. Knepley         }
1940c1f031eeSMatthew G. Knepley       }
1941ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1942c1f031eeSMatthew G. Knepley   }
1943338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1944b5a3613cSMatthew G. Knepley   if (useFVM) {
1945b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1946b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1947b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1948b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1949b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1950b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1951b5a3613cSMatthew G. Knepley   }
195273d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1953338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1954338f77d5SMatthew G. Knepley   /* Cleanup */
1955f99c8401SToby Isaac   if (affineQuad) {
1956f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1957f99c8401SToby Isaac   }
1958f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1959c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1960338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1961338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1962338f77d5SMatthew G. Knepley }
1963338f77d5SMatthew G. Knepley 
1964338f77d5SMatthew G. Knepley /*@
1965338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1966338f77d5SMatthew G. Knepley 
1967338f77d5SMatthew G. Knepley   Input Parameters:
1968338f77d5SMatthew G. Knepley + dm - The mesh
1969338f77d5SMatthew G. Knepley . X  - Global input vector
1970338f77d5SMatthew G. Knepley - user - The user context
1971338f77d5SMatthew G. Knepley 
1972338f77d5SMatthew G. Knepley   Output Parameter:
1973338f77d5SMatthew G. Knepley . integral - Integral for each field
1974338f77d5SMatthew G. Knepley 
1975338f77d5SMatthew G. Knepley   Level: developer
1976338f77d5SMatthew G. Knepley 
1977338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1978338f77d5SMatthew G. Knepley @*/
1979b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1980338f77d5SMatthew G. Knepley {
1981338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1982b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1983412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
1984338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1985338f77d5SMatthew G. Knepley 
1986338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1987338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1988338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1989338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1990338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1991338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1992338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1993412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1994338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1995338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1996338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1997338f77d5SMatthew G. Knepley   /* Sum up values */
1998338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1999338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2000338f77d5SMatthew G. Knepley 
2001338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2002b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
2003338f77d5SMatthew G. Knepley   }
2004b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
200573d901b8SMatthew G. Knepley   if (mesh->printFEM) {
2006338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
2007b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
200873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
200973d901b8SMatthew G. Knepley   }
2010338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
2011338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2012338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
2013338f77d5SMatthew G. Knepley }
2014338f77d5SMatthew G. Knepley 
2015338f77d5SMatthew G. Knepley /*@
2016338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
2017338f77d5SMatthew G. Knepley 
2018338f77d5SMatthew G. Knepley   Input Parameters:
2019338f77d5SMatthew G. Knepley + dm - The mesh
2020338f77d5SMatthew G. Knepley . X  - Global input vector
2021338f77d5SMatthew G. Knepley - user - The user context
2022338f77d5SMatthew G. Knepley 
2023338f77d5SMatthew G. Knepley   Output Parameter:
2024338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
2025338f77d5SMatthew G. Knepley 
2026338f77d5SMatthew G. Knepley   Level: developer
2027338f77d5SMatthew G. Knepley 
2028338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
2029338f77d5SMatthew G. Knepley @*/
2030338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
2031338f77d5SMatthew G. Knepley {
2032338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
2033338f77d5SMatthew G. Knepley   DM             dmF;
2034338f77d5SMatthew G. Knepley   PetscSection   sectionF;
2035338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
2036412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
2037338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
2038338f77d5SMatthew G. Knepley 
2039338f77d5SMatthew G. Knepley   PetscFunctionBegin;
2040338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2041338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
2042338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
2043338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2044338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2045338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
2046412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2047338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
2048338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
2049338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
2050338f77d5SMatthew G. Knepley   /* Put values in F*/
2051338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
205292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmF, &sectionF);CHKERRQ(ierr);
2053338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
2054338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
2055338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2056338f77d5SMatthew G. Knepley     PetscInt       dof, off;
2057338f77d5SMatthew G. Knepley 
2058338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2059338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
2060338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
2061338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
2062338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
2063338f77d5SMatthew G. Knepley   }
2064338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
2065338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
2066c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
206773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
206873d901b8SMatthew G. Knepley }
206973d901b8SMatthew G. Knepley 
20709b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
20719b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
207264c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
207364c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
207464c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
207564c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
207664c72086SMatthew G. Knepley {
20779b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
2078a6e0b375SMatthew G. Knepley   DMEnclosureType    encAux;
20799b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
20809b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
20819b6f715bSMatthew G. Knepley   Vec                locA = NULL;
20829b6f715bSMatthew G. Knepley   DMField            coordField;
20839b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
20849b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
20859b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
208664c72086SMatthew G. Knepley   const PetscScalar *constants;
20879b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
208864c72086SMatthew G. Knepley   PetscErrorCode     ierr;
208964c72086SMatthew G. Knepley 
209064c72086SMatthew G. Knepley   PetscFunctionBegin;
20919b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
20929b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
209364c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
209492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
209564c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
209664c72086SMatthew G. Knepley   /* Determine which discretizations we have */
20979b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
209864c72086SMatthew G. Knepley     PetscObject  obj;
209964c72086SMatthew G. Knepley     PetscClassId id;
210064c72086SMatthew G. Knepley 
21019b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
210264c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
21039b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
210464c72086SMatthew G. Knepley   }
210564c72086SMatthew G. Knepley   /* Read DS information */
210664c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
210764c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
210864c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
210964c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
211064c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
211164c72086SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
21129b6f715bSMatthew G. Knepley   if (locA) {
21139b6f715bSMatthew G. Knepley     DM dmAux;
21149b6f715bSMatthew G. Knepley 
21159b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2116a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
21179b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
211864c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
211964c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
212092fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
212164c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
212264c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
212364c72086SMatthew G. Knepley   }
21249b6f715bSMatthew G. Knepley   /* Integrate over points */
21259b6f715bSMatthew G. Knepley   {
21269b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
2127b7260050SToby Isaac     PetscInt        maxDegree;
21289b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
21299b6f715bSMatthew G. Knepley     const PetscInt *points;
21309b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
21319b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
213264c72086SMatthew G. Knepley 
21339b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
21349b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
21359b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
2136b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
213764c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
213864c72086SMatthew G. Knepley       PetscFE fe;
213964c72086SMatthew G. Knepley 
214064c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
2141b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
21429b6f715bSMatthew G. Knepley       if (!qGeom) {
21439b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
21449b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
21459b6f715bSMatthew G. Knepley       }
21469b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
21479b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
21489b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
21499b6f715bSMatthew G. Knepley         const PetscInt point = points[face], *support, *cone;
21509b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
21519b6f715bSMatthew G. Knepley         PetscInt       i, coneSize, faceLoc;
21529b6f715bSMatthew G. Knepley 
21539b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
21549b6f715bSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
21559b6f715bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
21569b6f715bSMatthew G. Knepley         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
21579b6f715bSMatthew G. Knepley         if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", face, support[0]);
21589b6f715bSMatthew G. Knepley         fgeom->face[face][0] = faceLoc;
21599b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
21609b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
21619b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
21629b6f715bSMatthew G. Knepley         if (locA) {
21639b6f715bSMatthew G. Knepley           PetscInt subp;
2164a6e0b375SMatthew G. Knepley           ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
21659b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
21669b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
21679b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
21689b6f715bSMatthew G. Knepley         }
21699b6f715bSMatthew G. Knepley       }
21709b6f715bSMatthew G. Knepley       /* Get blocking */
21719b6f715bSMatthew G. Knepley       {
21729b6f715bSMatthew G. Knepley         PetscQuadrature q;
21739b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
21749b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
21759b6f715bSMatthew G. Knepley 
217664c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
217764c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
217864c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
217964c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
218064c72086SMatthew G. Knepley         blockSize = Nb*Nq;
218164c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
21829b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
218364c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
21849b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
21859b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
218664c72086SMatthew G. Knepley         offset    = numFaces - Nr;
218764c72086SMatthew G. Knepley       }
21889b6f715bSMatthew G. Knepley       /* Do integration for each field */
21899b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
21909b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
21914bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateBd(prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
21929b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
219364c72086SMatthew G. Knepley       }
21949b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
21954bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrateBd(prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
21969b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
219764c72086SMatthew G. Knepley       /* Cleanup data arrays */
21989b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
21999b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
22009b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
22019b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
220264c72086SMatthew G. Knepley     }
220364c72086SMatthew G. Knepley   }
22049b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
22059b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
22069b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
22079b6f715bSMatthew G. Knepley }
22089b6f715bSMatthew G. Knepley 
22099b6f715bSMatthew G. Knepley /*@
22109b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
22119b6f715bSMatthew G. Knepley 
22129b6f715bSMatthew G. Knepley   Input Parameters:
22139b6f715bSMatthew G. Knepley + dm      - The mesh
22149b6f715bSMatthew G. Knepley . X       - Global input vector
22159b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
22169b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
22179b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
22189b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
22199b6f715bSMatthew G. Knepley - user    - The user context
22209b6f715bSMatthew G. Knepley 
22219b6f715bSMatthew G. Knepley   Output Parameter:
22229b6f715bSMatthew G. Knepley . integral - Integral for each field
22239b6f715bSMatthew G. Knepley 
22249b6f715bSMatthew G. Knepley   Level: developer
22259b6f715bSMatthew G. Knepley 
22269b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
22279b6f715bSMatthew G. Knepley @*/
22289b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
22299b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
22309b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
22319b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
22329b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
22339b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
22349b6f715bSMatthew G. Knepley {
22359b6f715bSMatthew G. Knepley   Vec            locX;
22369b6f715bSMatthew G. Knepley   PetscSection   section;
22379b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
22389b6f715bSMatthew G. Knepley   IS             facetIS;
22399b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
22409b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
22419b6f715bSMatthew G. Knepley 
22429b6f715bSMatthew G. Knepley   PetscFunctionBegin;
22439b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22449b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
22459b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
22469b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
22479b6f715bSMatthew G. Knepley   PetscValidPointer(integral, 6);
22489b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
22499b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
22509b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
22519b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
225292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
22539b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
22549b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
22559b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
22569b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
22579b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
22589b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
22599b6f715bSMatthew G. Knepley   /* Loop over label values */
2260580bdb30SBarry Smith   ierr = PetscArrayzero(integral, Nf);CHKERRQ(ierr);
22619b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
22629b6f715bSMatthew G. Knepley     IS           pointIS;
22639b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
22649b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
22659b6f715bSMatthew G. Knepley 
22669b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
22679b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
22689b6f715bSMatthew G. Knepley     {
22699b6f715bSMatthew G. Knepley       IS isectIS;
22709b6f715bSMatthew G. Knepley 
22719b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
22729b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
22739b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22749b6f715bSMatthew G. Knepley       pointIS = isectIS;
22759b6f715bSMatthew G. Knepley     }
22769b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
22779b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
22789b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
22799b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
22809b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
22819b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
22829b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22839b6f715bSMatthew G. Knepley   }
228464c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
22859b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
22869b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
228764c72086SMatthew G. Knepley   PetscFunctionReturn(0);
228864c72086SMatthew G. Knepley }
228964c72086SMatthew G. Knepley 
2290d69c5d34SMatthew G. Knepley /*@
2291cf51de39SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to a uniformly refined DM.
2292d69c5d34SMatthew G. Knepley 
2293d69c5d34SMatthew G. Knepley   Input Parameters:
2294cf51de39SMatthew G. Knepley + dmc  - The coarse mesh
2295cf51de39SMatthew G. Knepley . dmf  - The fine mesh
2296cf51de39SMatthew G. Knepley . isRefined - Flag indicating regular refinement, rather than the same topology
2297d69c5d34SMatthew G. Knepley - user - The user context
2298d69c5d34SMatthew G. Knepley 
2299d69c5d34SMatthew G. Knepley   Output Parameter:
2300934789fcSMatthew G. Knepley . In  - The interpolation matrix
2301d69c5d34SMatthew G. Knepley 
2302d69c5d34SMatthew G. Knepley   Level: developer
2303d69c5d34SMatthew G. Knepley 
230468132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2305d69c5d34SMatthew G. Knepley @*/
2306cf51de39SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, PetscBool isRefined, Mat In, void *user)
2307d69c5d34SMatthew G. Knepley {
2308d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
2309d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
23107c2b9c7aSMatthew G. Knepley   PetscDS           cds, rds;
2311d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
231297c42addSMatthew G. Knepley   PetscFV          *fvRef;
2313d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
2314d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
2315d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
2316485ad865SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
23170f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
2318d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
2319d69c5d34SMatthew G. Knepley 
2320d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
2321d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2322c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
232392fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2324e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
232592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2326e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
2327d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
2328412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
23297c2b9c7aSMatthew G. Knepley   ierr = DMGetDS(dmc, &cds);CHKERRQ(ierr);
23307c2b9c7aSMatthew G. Knepley   ierr = DMGetDS(dmf, &rds);CHKERRQ(ierr);
233197c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &feRef, Nf, &fvRef);CHKERRQ(ierr);
2332d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
233397c42addSMatthew G. Knepley     PetscObject  obj;
233497c42addSMatthew G. Knepley     PetscClassId id;
2335aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
2336d69c5d34SMatthew G. Knepley 
23377c2b9c7aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(rds, f, &obj);CHKERRQ(ierr);
233897c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
233997c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
234097c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
234197c42addSMatthew G. Knepley 
2342cf51de39SMatthew G. Knepley       if (isRefined) {
23430f2d7e86SMatthew G. Knepley         ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
2344cf51de39SMatthew G. Knepley       } else {
2345cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fe);CHKERRQ(ierr);
2346cf51de39SMatthew G. Knepley         feRef[f] = fe;
2347cf51de39SMatthew G. Knepley       }
2348d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
23490f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
235097c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
235197c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
235297c42addSMatthew G. Knepley       PetscDualSpace Q;
235397c42addSMatthew G. Knepley 
2354cf51de39SMatthew G. Knepley       if (isRefined) {
235597c42addSMatthew G. Knepley         ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
2356cf51de39SMatthew G. Knepley       } else {
2357cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fv);CHKERRQ(ierr);
2358cf51de39SMatthew G. Knepley         fvRef[f] = fv;
2359cf51de39SMatthew G. Knepley       }
236097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
236197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
236297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
236397c42addSMatthew G. Knepley     }
23649c3cf19fSMatthew G. Knepley     rTotDim += rNb;
2365d69c5d34SMatthew G. Knepley   }
23667c2b9c7aSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(cds, &cTotDim);CHKERRQ(ierr);
23670f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
2368580bdb30SBarry Smith   ierr = PetscArrayzero(elemMat, rTotDim*cTotDim);CHKERRQ(ierr);
2369d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
2370d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
2371d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
2372d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2373d69c5d34SMatthew G. Knepley     PetscReal       *points;
2374d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
2375d69c5d34SMatthew G. Knepley 
2376d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
237797c42addSMatthew G. Knepley     if (feRef[fieldI]) {
2378d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
23790f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
238097c42addSMatthew G. Knepley     } else {
238197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
238297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
238397c42addSMatthew G. Knepley     }
2384d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
2385d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
2386d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23879c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
2388d69c5d34SMatthew G. Knepley       npoints += Np;
2389d69c5d34SMatthew G. Knepley     }
2390d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
2391d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
2392d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23939c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
2394d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
2395d69c5d34SMatthew G. Knepley     }
2396d69c5d34SMatthew G. Knepley 
2397d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
239897c42addSMatthew G. Knepley       PetscObject  obj;
239997c42addSMatthew G. Knepley       PetscClassId id;
24009c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
2401d69c5d34SMatthew G. Knepley 
24027c2b9c7aSMatthew G. Knepley       ierr = PetscDSGetDiscretization(cds, fieldJ, &obj);CHKERRQ(ierr);
240397c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
240497c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
240597c42addSMatthew G. Knepley         PetscFE           fe = (PetscFE) obj;
2406ef0bb6c7SMatthew G. Knepley         PetscTabulation T  = NULL;
2407d69c5d34SMatthew G. Knepley 
2408d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
24090f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
24100f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2411ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
2412ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
24139c3cf19fSMatthew 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);
2414ef0bb6c7SMatthew G. Knepley           ierr = PetscFECreateTabulation(fe, 1, npoints, points, 0, &T);CHKERRQ(ierr);
2415d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
2416d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
24179c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
24189c3cf19fSMatthew 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);
2419d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
242036a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2421d172c84bSMatthew G. Knepley                 /*
2422d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
2423d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
2424d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
2425d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
2426d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
2427d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
2428d172c84bSMatthew G. Knepley                 */
2429ef0bb6c7SMatthew 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];
243036a6d9c0SMatthew G. Knepley               }
2431d69c5d34SMatthew G. Knepley             }
2432d69c5d34SMatthew G. Knepley           }
2433ef0bb6c7SMatthew G. Knepley           ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);CHKERRQ(ierr);
2434ffe73a53SMatthew G. Knepley         }
243597c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
243697c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
243797c42addSMatthew G. Knepley 
243897c42addSMatthew G. Knepley         /* Evaluate constant function at points */
243997c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
244097c42addSMatthew G. Knepley         cpdim = 1;
244197c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
244297c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
2443ff1e0c32SBarry 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);
244497c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
244597c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
24469c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
24479c3cf19fSMatthew 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);
244897c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
244997c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2450458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
245197c42addSMatthew G. Knepley               }
245297c42addSMatthew G. Knepley             }
245397c42addSMatthew G. Knepley           }
245497c42addSMatthew G. Knepley         }
245597c42addSMatthew G. Knepley       }
2456d172c84bSMatthew G. Knepley       offsetJ += cpdim;
2457d69c5d34SMatthew G. Knepley     }
2458d172c84bSMatthew G. Knepley     offsetI += fpdim;
2459549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
2460d69c5d34SMatthew G. Knepley   }
24610f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
24627f5b169aSMatthew G. Knepley   /* Preallocate matrix */
24637f5b169aSMatthew G. Knepley   {
2464c094ef40SMatthew G. Knepley     Mat          preallocator;
2465c094ef40SMatthew G. Knepley     PetscScalar *vals;
2466c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
2467c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
24687f5b169aSMatthew G. Knepley 
2469c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
2470c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
2471c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
2472c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2473c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
2474c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
24757f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2476cf51de39SMatthew G. Knepley       if (isRefined) {
24777f5b169aSMatthew G. Knepley         ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
2478c094ef40SMatthew G. Knepley         ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
2479cf51de39SMatthew G. Knepley       } else {
2480cf51de39SMatthew G. Knepley         ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, preallocator, cell, vals, INSERT_VALUES);CHKERRQ(ierr);
2481cf51de39SMatthew G. Knepley       }
24827f5b169aSMatthew G. Knepley     }
2483c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
2484c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2485c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2486c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
2487c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
24887f5b169aSMatthew G. Knepley   }
24897f5b169aSMatthew G. Knepley   /* Fill matrix */
24907f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
2491d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2492cf51de39SMatthew G. Knepley     if (isRefined) {
2493934789fcSMatthew G. Knepley       ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2494cf51de39SMatthew G. Knepley     } else {
2495cf51de39SMatthew G. Knepley       ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2496cf51de39SMatthew G. Knepley     }
2497d69c5d34SMatthew G. Knepley   }
2498549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
249997c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
2500549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2501934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2502934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2503d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
2504825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
2505934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
2506825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
2507d69c5d34SMatthew G. Knepley   }
2508d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2509d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
2510d69c5d34SMatthew G. Knepley }
25116c73c22cSMatthew G. Knepley 
2512bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
2513bd041c0cSMatthew G. Knepley {
2514bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
2515bd041c0cSMatthew G. Knepley }
2516bd041c0cSMatthew G. Knepley 
251768132eb9SMatthew G. Knepley /*@
251868132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
251968132eb9SMatthew G. Knepley 
252068132eb9SMatthew G. Knepley   Input Parameters:
252168132eb9SMatthew G. Knepley + dmf  - The fine mesh
252268132eb9SMatthew G. Knepley . dmc  - The coarse mesh
252368132eb9SMatthew G. Knepley - user - The user context
252468132eb9SMatthew G. Knepley 
252568132eb9SMatthew G. Knepley   Output Parameter:
252668132eb9SMatthew G. Knepley . In  - The interpolation matrix
252768132eb9SMatthew G. Knepley 
252868132eb9SMatthew G. Knepley   Level: developer
252968132eb9SMatthew G. Knepley 
253068132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
253168132eb9SMatthew G. Knepley @*/
253268132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
25334ef9d792SMatthew G. Knepley {
253464e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
253564e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
25364ef9d792SMatthew G. Knepley   PetscDS        prob;
25374ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2538e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
25394ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
25404ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
25414ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
25424ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
25434ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
25444ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
25454ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
25464ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
25474ef9d792SMatthew G. Knepley 
25484ef9d792SMatthew G. Knepley   PetscFunctionBegin;
254977711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
25504ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
25514ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
25524bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
25534ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
25544ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
25554ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
255692fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2557e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
255892fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2559e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
25604ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
25614ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
25629c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
25634ef9d792SMatthew G. Knepley 
25644ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
25654ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
25664ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
25674ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
25684ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
25694ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
25704ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
25714ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2572e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
25734ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
25744ef9d792SMatthew G. Knepley     PetscObject      obj;
25754ef9d792SMatthew G. Knepley     PetscClassId     id;
2576c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
25774ef9d792SMatthew G. Knepley     PetscQuadrature  f;
257817f047d8SMatthew G. Knepley     const PetscReal *qpoints;
257917f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
25804ef9d792SMatthew G. Knepley 
25814ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
25824ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
25834ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
25844ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
25854ef9d792SMatthew G. Knepley 
25864ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
25874ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
25884ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
25894ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
25904ef9d792SMatthew G. Knepley 
25914ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
25924ef9d792SMatthew G. Knepley       Nc   = 1;
25934ef9d792SMatthew G. Knepley     }
25944ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
25954ef9d792SMatthew G. Knepley     /* For each fine grid cell */
25964ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
25974ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
25984ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
25994ef9d792SMatthew G. Knepley 
260071f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
26014ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2602ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
26034ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
26044ef9d792SMatthew G. Knepley         Vec             pointVec;
26054ef9d792SMatthew G. Knepley         PetscScalar    *pV;
26063a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
26073a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
26089c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
26094ef9d792SMatthew G. Knepley 
26104ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
26114ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
26129c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
26134ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
26144ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
26154ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
26164ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2617c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2618c330f8ffSToby Isaac 
26194ef9d792SMatthew G. Knepley           /* Transform point to real space */
2620c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
26214ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
26224ef9d792SMatthew G. Knepley         }
26234ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
26244ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
26251555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
262662a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
26273a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
26284ef9d792SMatthew G. Knepley         /* Update preallocation info */
26293a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
26303a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
26319c3cf19fSMatthew G. Knepley         {
2632e8f14785SLisandro Dalcin           PetscHashIJKey key;
2633e8f14785SLisandro Dalcin           PetscBool      missing;
26344ef9d792SMatthew G. Knepley 
2635e8f14785SLisandro Dalcin           key.i = findices[i];
2636e8f14785SLisandro Dalcin           if (key.i >= 0) {
26374ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
26384ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
263971f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
26404ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2641e8f14785SLisandro Dalcin                 key.j = cindices[c];
2642e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2643e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
26444ef9d792SMatthew G. Knepley                 if (missing) {
2645e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2646e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
26474ef9d792SMatthew G. Knepley                 }
26484ef9d792SMatthew G. Knepley               }
264971f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
26504ef9d792SMatthew G. Knepley             }
26514ef9d792SMatthew G. Knepley           }
26528c543595SMatthew G. Knepley         }
26533a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
26544ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
26554ef9d792SMatthew G. Knepley       }
265671f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
26574ef9d792SMatthew G. Knepley     }
26584ef9d792SMatthew G. Knepley   }
2659e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
26604ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
26614ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
26624ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
26634ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
26644ef9d792SMatthew G. Knepley     PetscObject       obj;
26654ef9d792SMatthew G. Knepley     PetscClassId      id;
2666c0d7054bSMatthew G. Knepley     PetscDualSpace    Q = NULL;
2667ef0bb6c7SMatthew G. Knepley     PetscTabulation T = NULL;
26684ef9d792SMatthew G. Knepley     PetscQuadrature   f;
26694ef9d792SMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
26709c3cf19fSMatthew G. Knepley     PetscInt          Nc, qNc, Np, fpdim, i, d;
26714ef9d792SMatthew G. Knepley 
26724ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
26734ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
26744ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
26754ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
26764ef9d792SMatthew G. Knepley 
26774ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
26784ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
2679ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation(fe, 1, 1, x, 0, &T);CHKERRQ(ierr);
26804ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
26814ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
26824ef9d792SMatthew G. Knepley 
26834ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
26844ef9d792SMatthew G. Knepley       Nc   = 1;
2685ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",field);
26864ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
26874ef9d792SMatthew G. Knepley     /* For each fine grid cell */
26884ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
26894ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
26904ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
26914ef9d792SMatthew G. Knepley 
269271f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
26934ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2694ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
26954ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
26964ef9d792SMatthew G. Knepley         Vec             pointVec;
26974ef9d792SMatthew G. Knepley         PetscScalar    *pV;
269812111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
26993a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
270017f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
27014ef9d792SMatthew G. Knepley 
27024ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
27034ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
27049c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
27059c3cf19fSMatthew 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);
27064ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
27074ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
27084ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
27094ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2710c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2711c330f8ffSToby Isaac 
27124ef9d792SMatthew G. Knepley           /* Transform point to real space */
2713c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
27144ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
27154ef9d792SMatthew G. Knepley         }
27164ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
27174ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
27181555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
271962a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
27204ef9d792SMatthew G. Knepley         /* Update preallocation info */
27213a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
27223a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
27234ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
27244ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2725826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2726c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2727826eb36dSMatthew G. Knepley 
272871f0bbf9SMatthew G. Knepley           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
27294ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
27303a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2731e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2732c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
27334ef9d792SMatthew G. Knepley 
27344ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
27354ef9d792SMatthew G. Knepley             PetscFE fe = (PetscFE) obj;
27364ef9d792SMatthew G. Knepley 
27374ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
27384ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2739ef0bb6c7SMatthew G. Knepley             ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
2740580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
27414ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
27424ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2743ef0bb6c7SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*qweights[ccell*qNc + c];
27444ef9d792SMatthew G. Knepley             }
27454ef9d792SMatthew G. Knepley           } else {
27464ef9d792SMatthew G. Knepley             cpdim = 1;
27474ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
27489c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
27494ef9d792SMatthew G. Knepley             }
27504ef9d792SMatthew G. Knepley           }
27514ef9d792SMatthew G. Knepley           /* Update interpolator */
27529c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
27539c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
27549c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
275571f0bbf9SMatthew G. Knepley           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
27564ef9d792SMatthew G. Knepley         }
27574ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
27583a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
27594ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
27604ef9d792SMatthew G. Knepley       }
276171f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
27624ef9d792SMatthew G. Knepley     }
2763ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
27644ef9d792SMatthew G. Knepley   }
27654ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
27664ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
27674ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
27684ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27694ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
277077711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
27714ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
27724ef9d792SMatthew G. Knepley }
27734ef9d792SMatthew G. Knepley 
277446fa42a0SMatthew G. Knepley /*@
2775bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2776bd041c0cSMatthew G. Knepley 
2777bd041c0cSMatthew G. Knepley   Input Parameters:
2778bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2779bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2780bd041c0cSMatthew G. Knepley - user - The user context
2781bd041c0cSMatthew G. Knepley 
2782bd041c0cSMatthew G. Knepley   Output Parameter:
2783bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2784bd041c0cSMatthew G. Knepley 
2785bd041c0cSMatthew G. Knepley   Level: developer
2786bd041c0cSMatthew G. Knepley 
2787bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2788bd041c0cSMatthew G. Knepley @*/
2789bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2790bd041c0cSMatthew G. Knepley {
2791bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2792bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2793bd041c0cSMatthew G. Knepley   PetscDS        prob;
2794bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2795e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2796bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2797bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2798bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2799bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2800bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2801bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2802bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2803bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2804bd041c0cSMatthew G. Knepley 
2805bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2806bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2807bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
28084bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2809bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2810bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2811bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
281292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2813e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
281492fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2815e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2816bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2817bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2818bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2819bd041c0cSMatthew G. Knepley 
2820bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2821bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2822bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2823bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2824bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2825bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2826bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2827bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2828e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2829bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2830bd041c0cSMatthew G. Knepley     PetscObject      obj;
2831bd041c0cSMatthew G. Knepley     PetscClassId     id;
2832bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2833bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2834bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2835bd041c0cSMatthew G. Knepley 
2836bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2837bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2838bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2839bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2840bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2841bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2842bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2843bd041c0cSMatthew G. Knepley       Vec                pointVec;
2844bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2845bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2846bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2847bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2848bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2849bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2850bd041c0cSMatthew G. Knepley 
285171f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
2852bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2853bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2854bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2855bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2856bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2857bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2858c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2859c330f8ffSToby Isaac 
2860bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2861c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2862bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2863bd041c0cSMatthew G. Knepley       }
2864bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2865bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2866bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2867bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2868bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2869bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2870bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2871bd041c0cSMatthew G. Knepley       {
2872e8f14785SLisandro Dalcin         PetscHashIJKey key;
2873e8f14785SLisandro Dalcin         PetscBool      missing;
2874bd041c0cSMatthew G. Knepley 
2875bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2876e8f14785SLisandro Dalcin           key.i = findices[i];
2877e8f14785SLisandro Dalcin           if (key.i >= 0) {
2878bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2879bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
288071f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2881bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2882e8f14785SLisandro Dalcin                 key.j = cindices[c];
2883e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2884e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2885bd041c0cSMatthew G. Knepley                 if (missing) {
2886e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2887e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2888bd041c0cSMatthew G. Knepley                 }
2889bd041c0cSMatthew G. Knepley               }
289071f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2891bd041c0cSMatthew G. Knepley             }
2892bd041c0cSMatthew G. Knepley           }
2893bd041c0cSMatthew G. Knepley         }
2894bd041c0cSMatthew G. Knepley       }
2895bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2896bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
289771f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
2898bd041c0cSMatthew G. Knepley     }
2899bd041c0cSMatthew G. Knepley   }
2900e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
2901bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2902bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2903bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2904bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2905bd041c0cSMatthew G. Knepley     PetscObject       obj;
2906bd041c0cSMatthew G. Knepley     PetscClassId      id;
2907ef0bb6c7SMatthew G. Knepley     PetscTabulation T, Tfine;
2908bd041c0cSMatthew G. Knepley     PetscQuadrature   quad;
2909bd041c0cSMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
2910bd041c0cSMatthew G. Knepley     PetscInt          Nq, Nc, i, d;
2911bd041c0cSMatthew G. Knepley 
2912bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2913bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2914ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
2915ef0bb6c7SMatthew G. Knepley       ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);
2916ef0bb6c7SMatthew G. Knepley       ierr = PetscFEGetCellTabulation((PetscFE) obj, &Tfine);CHKERRQ(ierr);
2917ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation((PetscFE) obj, 1, 1, x, 0, &T);CHKERRQ(ierr);
2918ef0bb6c7SMatthew G. Knepley     } else {
2919ef0bb6c7SMatthew G. Knepley       ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);
2920ef0bb6c7SMatthew G. Knepley     }
2921bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2922bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2923bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2924bd041c0cSMatthew G. Knepley       Vec                pointVec;
2925bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2926bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2927bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2928bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2929bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2930bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2931bd041c0cSMatthew G. Knepley 
293271f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
2933bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2934bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2935bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2936bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2937bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2938bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2939c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2940c330f8ffSToby Isaac 
2941bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2942c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2943bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2944bd041c0cSMatthew G. Knepley       }
2945bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2946bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2947bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2948bd041c0cSMatthew G. Knepley       /* Update matrix */
2949bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2950bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2951bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2952bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2953bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2954c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2955c330f8ffSToby Isaac 
2956bd041c0cSMatthew G. Knepley 
295771f0bbf9SMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2958bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2959bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2960bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2961c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
2962bd041c0cSMatthew G. Knepley 
2963bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2964bd041c0cSMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
2965bd041c0cSMatthew G. Knepley 
2966bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2967bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2968ef0bb6c7SMatthew G. Knepley           ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
2969bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2970bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2971580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
2972bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2973ef0bb6c7SMatthew 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;
2974bd041c0cSMatthew G. Knepley             }
2975bd041c0cSMatthew G. Knepley             /* Update interpolator */
2976bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2977bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2978bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2979bd041c0cSMatthew G. Knepley           }
2980bd041c0cSMatthew G. Knepley         } else {
2981bd041c0cSMatthew G. Knepley           cpdim = 1;
2982bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2983580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
2984bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2985bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2986bd041c0cSMatthew G. Knepley             }
2987bd041c0cSMatthew G. Knepley             /* Update interpolator */
2988bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2989ff1e0c32SBarry Smith             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %D %D Nf: %D %D Nc: %D %D\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
2990bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2991bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2992bd041c0cSMatthew G. Knepley           }
2993bd041c0cSMatthew G. Knepley         }
299471f0bbf9SMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
2995bd041c0cSMatthew G. Knepley       }
2996bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2997bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2998bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
299971f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3000bd041c0cSMatthew G. Knepley     }
3001ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
3002bd041c0cSMatthew G. Knepley   }
3003bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
3004bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
3005bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
3006bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3007bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3008bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
3009bd041c0cSMatthew G. Knepley }
3010bd041c0cSMatthew G. Knepley 
3011bd041c0cSMatthew G. Knepley /*@
301246fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
301346fa42a0SMatthew G. Knepley 
301446fa42a0SMatthew G. Knepley   Input Parameters:
301546fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
301646fa42a0SMatthew G. Knepley - dmf  - The fine mesh
301746fa42a0SMatthew G. Knepley - user - The user context
301846fa42a0SMatthew G. Knepley 
301946fa42a0SMatthew G. Knepley   Output Parameter:
302046fa42a0SMatthew G. Knepley . sc   - The mapping
302146fa42a0SMatthew G. Knepley 
302246fa42a0SMatthew G. Knepley   Level: developer
302346fa42a0SMatthew G. Knepley 
302446fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
302546fa42a0SMatthew G. Knepley @*/
30267c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
30277c927364SMatthew G. Knepley {
3028e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
30297c927364SMatthew G. Knepley   PetscFE       *feRef;
303097c42addSMatthew G. Knepley   PetscFV       *fvRef;
30317c927364SMatthew G. Knepley   Vec            fv, cv;
30327c927364SMatthew G. Knepley   IS             fis, cis;
30337c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
30347c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
3035485ad865SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, endC, offsetC, offsetF, m;
30366f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
30377c927364SMatthew G. Knepley   PetscErrorCode ierr;
30387c927364SMatthew G. Knepley 
30397c927364SMatthew G. Knepley   PetscFunctionBegin;
304075a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3041c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
304292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
3043e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
304492fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
3045e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
30467c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
3047412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
3048e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
30496f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
30507c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
305197c42addSMatthew G. Knepley     PetscObject  obj;
305297c42addSMatthew G. Knepley     PetscClassId id;
3053aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
30547c927364SMatthew G. Knepley 
305597c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
305697c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
305797c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
305897c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
30596f3d3cbcSMatthew G. Knepley       PetscSpace sp;
30609b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
306197c42addSMatthew G. Knepley 
30627c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
30637c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
30647c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
30656f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
30669b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
30679b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
306897c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
306997c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
307097c42addSMatthew G. Knepley       PetscDualSpace Q;
307197c42addSMatthew G. Knepley 
307297c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
307397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
307497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
307597c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
30766f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
307797c42addSMatthew G. Knepley     }
3078d172c84bSMatthew G. Knepley     fTotDim += fNb;
30797c927364SMatthew G. Knepley   }
3080e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
30817c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
30827c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
30837c927364SMatthew G. Knepley     PetscFE        feC;
308497c42addSMatthew G. Knepley     PetscFV        fvC;
30857c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
3086d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
30877c927364SMatthew G. Knepley 
308897c42addSMatthew G. Knepley     if (feRef[field]) {
3089e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
30907c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
30917c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
30927c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
3093d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
30947c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
30957c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
30967c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
309797c42addSMatthew G. Knepley     } else {
309897c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
309997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
310097c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
310197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
310297c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
310397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
310497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
310597c42addSMatthew G. Knepley     }
3106ff1e0c32SBarry 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);
31077c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
31087c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
3109d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
3110d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
311197c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
31127c927364SMatthew G. Knepley 
31137c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
3114d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
3115ff1e0c32SBarry 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);
311697c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
31177c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
31187c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
3119d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
31207c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
3121d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
31227c927364SMatthew G. Knepley 
31237c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
3124d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
3125ff1e0c32SBarry 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);
31267c927364SMatthew G. Knepley         if (NpC != NpF) continue;
31277c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
31287c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
3129d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
3130d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
3131d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
313297c42addSMatthew G. Knepley         found = PETSC_TRUE;
31337c927364SMatthew G. Knepley         break;
31347c927364SMatthew G. Knepley       }
313597c42addSMatthew G. Knepley       if (!found) {
313697c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
3137d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
3138d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
313997c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
314097c42addSMatthew G. Knepley       }
31417c927364SMatthew G. Knepley     }
3142d172c84bSMatthew G. Knepley     offsetC += cpdim;
3143d172c84bSMatthew G. Knepley     offsetF += fpdim;
31447c927364SMatthew G. Knepley   }
314597c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
31466f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
31477c927364SMatthew G. Knepley 
31487c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
31497c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
31500bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
31517c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
31527c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
3153aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
3154aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
31557c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
31567c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
31577c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
31587c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
31590bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
3160ff1e0c32SBarry 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]]);
31617c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
31627c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
31637c927364SMatthew G. Knepley     }
31647c927364SMatthew G. Knepley   }
31657c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
31667c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
31677c927364SMatthew G. Knepley 
31687c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
31697c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
31709448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
31717c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
31727c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
31737c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
31747c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
317575a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3176cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
3177cb1e1211SMatthew G Knepley }
3178a1cf66bbSMatthew G. Knepley 
31792f856554SMatthew G. Knepley /*@C
31802f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
31812f856554SMatthew G. Knepley 
31822f856554SMatthew G. Knepley   Input Parameters:
31832f856554SMatthew G. Knepley + dm     - The DM
31842f856554SMatthew G. Knepley . cellIS - The cells to include
31852f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
31862f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
31872f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
31882f856554SMatthew G. Knepley 
31892f856554SMatthew G. Knepley   Output Parameters:
31902f856554SMatthew G. Knepley + u   - The field coefficients
31912f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
31922f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
31932f856554SMatthew G. Knepley 
31942f856554SMatthew G. Knepley   Level: developer
31952f856554SMatthew G. Knepley 
31962f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
31972f856554SMatthew G. Knepley @*/
31982f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
31992f856554SMatthew G. Knepley {
32002f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
3201a6e0b375SMatthew G. Knepley   DMEnclosureType encAux;
32022f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
32032f856554SMatthew G. Knepley   PetscDS         prob;
32042f856554SMatthew G. Knepley   const PetscInt *cells;
32052f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
32062f856554SMatthew G. Knepley   PetscErrorCode  ierr;
32072f856554SMatthew G. Knepley 
32082f856554SMatthew G. Knepley   PetscFunctionBegin;
32092f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32102f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
32112f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
32122f856554SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
32132f856554SMatthew G. Knepley   PetscValidPointer(u, 7);
32142f856554SMatthew G. Knepley   PetscValidPointer(u_t, 8);
32152f856554SMatthew G. Knepley   PetscValidPointer(a, 9);
32162f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
32172f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
321892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
3219083401c6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
32202f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
32212f856554SMatthew G. Knepley   if (locA) {
32222f856554SMatthew G. Knepley     DM      dmAux;
32232f856554SMatthew G. Knepley     PetscDS probAux;
32242f856554SMatthew G. Knepley 
32252f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
3226a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
32272f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
322892fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
32292f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
32302f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
32312f856554SMatthew G. Knepley   }
32322f856554SMatthew G. Knepley   numCells = cEnd - cStart;
32332f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
32342f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
32352f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
32362f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
32372f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
32382f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
32392f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
32402f856554SMatthew G. Knepley     PetscInt       i;
32412f856554SMatthew G. Knepley 
32422f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
32432f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
32442f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
32452f856554SMatthew G. Knepley     if (locX_t) {
32462f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
32472f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
32482f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
32492f856554SMatthew G. Knepley     }
32502f856554SMatthew G. Knepley     if (locA) {
32512f856554SMatthew G. Knepley       PetscInt subcell;
3252a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(plexA, dm, encAux, cell, &subcell);CHKERRQ(ierr);
32532f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
32542f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
32552f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
32562f856554SMatthew G. Knepley     }
32572f856554SMatthew G. Knepley   }
32582f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
32592f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
32602f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
32612f856554SMatthew G. Knepley   PetscFunctionReturn(0);
32622f856554SMatthew G. Knepley }
32632f856554SMatthew G. Knepley 
32642f856554SMatthew G. Knepley /*@C
32652f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
32662f856554SMatthew G. Knepley 
32672f856554SMatthew G. Knepley   Input Parameters:
32682f856554SMatthew G. Knepley + dm     - The DM
32692f856554SMatthew G. Knepley . cellIS - The cells to include
32702f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
32712f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
32722f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
32732f856554SMatthew G. Knepley 
32742f856554SMatthew G. Knepley   Output Parameters:
32752f856554SMatthew G. Knepley + u   - The field coefficients
32762f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
32772f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
32782f856554SMatthew G. Knepley 
32792f856554SMatthew G. Knepley   Level: developer
32802f856554SMatthew G. Knepley 
32812f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
32822f856554SMatthew G. Knepley @*/
32832f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
32842f856554SMatthew G. Knepley {
32852f856554SMatthew G. Knepley   PetscErrorCode ierr;
32862f856554SMatthew G. Knepley 
32872f856554SMatthew G. Knepley   PetscFunctionBegin;
32882f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
32892f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
32902f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
32912f856554SMatthew G. Knepley   PetscFunctionReturn(0);
32922f856554SMatthew G. Knepley }
32932f856554SMatthew G. Knepley 
32942f856554SMatthew G. Knepley /*@C
32952f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
32962f856554SMatthew G. Knepley 
32972f856554SMatthew G. Knepley   Input Parameters:
32982f856554SMatthew G. Knepley + dm     - The DM
32992f856554SMatthew G. Knepley . fStart - The first face to include
33002f856554SMatthew G. Knepley . fEnd   - The first face to exclude
33012f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
33022f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
33032f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
33042f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
33052f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
33062f856554SMatthew G. Knepley 
33072f856554SMatthew G. Knepley   Output Parameters:
33082f856554SMatthew G. Knepley + Nface - The number of faces with field values
33092f856554SMatthew G. Knepley . uL - The field values at the left side of the face
33102f856554SMatthew G. Knepley - uR - The field values at the right side of the face
33112f856554SMatthew G. Knepley 
33122f856554SMatthew G. Knepley   Level: developer
33132f856554SMatthew G. Knepley 
33142f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
33152f856554SMatthew G. Knepley @*/
33162f856554SMatthew 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)
33172f856554SMatthew G. Knepley {
33182f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
33192f856554SMatthew G. Knepley   PetscSection       section;
33202f856554SMatthew G. Knepley   PetscDS            prob;
33212f856554SMatthew G. Knepley   DMLabel            ghostLabel;
33222f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
33232f856554SMatthew G. Knepley   PetscBool         *isFE;
33242f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
33252f856554SMatthew G. Knepley   PetscErrorCode     ierr;
33262f856554SMatthew G. Knepley 
33272f856554SMatthew G. Knepley   PetscFunctionBegin;
33282f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33292f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
33302f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
33312f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
33322f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
33332f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
33342f856554SMatthew G. Knepley   PetscValidPointer(uL, 9);
33352f856554SMatthew G. Knepley   PetscValidPointer(uR, 10);
33362f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33372f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
333892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
33392f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
33402f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
33412f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
33422f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
33432f856554SMatthew G. Knepley     PetscObject  obj;
33442f856554SMatthew G. Knepley     PetscClassId id;
33452f856554SMatthew G. Knepley 
33462f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
33472f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
33482f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
33492f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
33502f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
33512f856554SMatthew G. Knepley   }
33522f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
33532f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
33542f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
33552f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
33562f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
33572f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
33582f856554SMatthew G. Knepley   if (locGrad) {
33592f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
33602f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
33612f856554SMatthew G. Knepley   }
33622f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
33632f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
33642f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
33652f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
33662f856554SMatthew G. Knepley     const PetscInt        *cells;
33672f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
33682f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
33692f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
33702f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
33712f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
33722f856554SMatthew G. Knepley 
33732f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
33742f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
33752f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
33762f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
33772f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
33782f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
33792f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
33802f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
33812f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
33822f856554SMatthew G. Knepley       PetscInt off;
33832f856554SMatthew G. Knepley 
33842f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
33852f856554SMatthew G. Knepley       if (isFE[f]) {
33862f856554SMatthew G. Knepley         const PetscInt *cone;
33872f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
33882f856554SMatthew G. Knepley 
33892f856554SMatthew G. Knepley         xL = xR = NULL;
33902f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
33912f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
33922f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
33932f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
33942f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
33952f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
33962f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
33972f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
33982f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
3399ff1e0c32SBarry 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]);
34002f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
34012f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
34022f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
3403a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
3404a8f1f9e5SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
34052f856554SMatthew G. Knepley           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
34062f856554SMatthew G. Knepley         }
34072f856554SMatthew G. Knepley         else {
3408a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
34092f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
34102f856554SMatthew G. Knepley           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
34112f856554SMatthew G. Knepley         }
34122f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
34132f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
34142f856554SMatthew G. Knepley       } else {
34152f856554SMatthew G. Knepley         PetscFV  fv;
34162f856554SMatthew G. Knepley         PetscInt numComp, c;
34172f856554SMatthew G. Knepley 
34182f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
34192f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
34202f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
34212f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
34222f856554SMatthew G. Knepley         if (dmGrad) {
34232f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
34242f856554SMatthew G. Knepley 
34252f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
34262f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
34272f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
34282f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
34292f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
34302f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
34312f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
34322f856554SMatthew G. Knepley           }
34332f856554SMatthew G. Knepley         } else {
34342f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
34352f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
34362f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
34372f856554SMatthew G. Knepley           }
34382f856554SMatthew G. Knepley         }
34392f856554SMatthew G. Knepley       }
34402f856554SMatthew G. Knepley     }
34412f856554SMatthew G. Knepley     ++iface;
34422f856554SMatthew G. Knepley   }
34432f856554SMatthew G. Knepley   *Nface = iface;
34442f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
34452f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
34462f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
34472f856554SMatthew G. Knepley   if (locGrad) {
34482f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
34492f856554SMatthew G. Knepley   }
34502f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
34512f856554SMatthew G. Knepley   PetscFunctionReturn(0);
34522f856554SMatthew G. Knepley }
34532f856554SMatthew G. Knepley 
34542f856554SMatthew G. Knepley /*@C
34552f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
34562f856554SMatthew G. Knepley 
34572f856554SMatthew G. Knepley   Input Parameters:
34582f856554SMatthew G. Knepley + dm     - The DM
34592f856554SMatthew G. Knepley . fStart - The first face to include
34602f856554SMatthew G. Knepley . fEnd   - The first face to exclude
34612f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
34622f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
34632f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
34642f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
34652f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
34662f856554SMatthew G. Knepley 
34672f856554SMatthew G. Knepley   Output Parameters:
34682f856554SMatthew G. Knepley + Nface - The number of faces with field values
34692f856554SMatthew G. Knepley . uL - The field values at the left side of the face
34702f856554SMatthew G. Knepley - uR - The field values at the right side of the face
34712f856554SMatthew G. Knepley 
34722f856554SMatthew G. Knepley   Level: developer
34732f856554SMatthew G. Knepley 
34742f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
34752f856554SMatthew G. Knepley @*/
34762f856554SMatthew 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)
34772f856554SMatthew G. Knepley {
34782f856554SMatthew G. Knepley   PetscErrorCode ierr;
34792f856554SMatthew G. Knepley 
34802f856554SMatthew G. Knepley   PetscFunctionBegin;
34812f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
34822f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
34832f856554SMatthew G. Knepley   PetscFunctionReturn(0);
34842f856554SMatthew G. Knepley }
34852f856554SMatthew G. Knepley 
34862f856554SMatthew G. Knepley /*@C
34872f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
34882f856554SMatthew G. Knepley 
34892f856554SMatthew G. Knepley   Input Parameters:
34902f856554SMatthew G. Knepley + dm     - The DM
34912f856554SMatthew G. Knepley . fStart - The first face to include
34922f856554SMatthew G. Knepley . fEnd   - The first face to exclude
34932f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
34942f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
34952f856554SMatthew G. Knepley 
34962f856554SMatthew G. Knepley   Output Parameters:
34972f856554SMatthew G. Knepley + Nface - The number of faces with field values
34982f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
34992f856554SMatthew G. Knepley - vol   - The cell volume
35002f856554SMatthew G. Knepley 
35012f856554SMatthew G. Knepley   Level: developer
35022f856554SMatthew G. Knepley 
35032f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
35042f856554SMatthew G. Knepley @*/
35052f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
35062f856554SMatthew G. Knepley {
35072f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
35082f856554SMatthew G. Knepley   DMLabel            ghostLabel;
35092f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
35102f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
35112f856554SMatthew G. Knepley   PetscErrorCode     ierr;
35122f856554SMatthew G. Knepley 
35132f856554SMatthew G. Knepley   PetscFunctionBegin;
35142f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35152f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
35162f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
35172f856554SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
35182f856554SMatthew G. Knepley   PetscValidPointer(vol, 7);
35192f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35202f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
35212f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
35222f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
35232f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
35242f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
35252f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
35262f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
35272f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
35282f856554SMatthew G. Knepley     const PetscInt        *cells;
35292f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
35302f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
35312f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
35322f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
35332f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
35342f856554SMatthew G. Knepley 
35352f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
35362f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
35372f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
35382f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
35392f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
35402f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
35412f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
35422f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
35432f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
35442f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
35452f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
35462f856554SMatthew G. Knepley     }
35472f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
35482f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
35492f856554SMatthew G. Knepley     ++iface;
35502f856554SMatthew G. Knepley   }
35512f856554SMatthew G. Knepley   *Nface = iface;
35522f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
35532f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
35542f856554SMatthew G. Knepley   PetscFunctionReturn(0);
35552f856554SMatthew G. Knepley }
35562f856554SMatthew G. Knepley 
35572f856554SMatthew G. Knepley /*@C
35582f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
35592f856554SMatthew G. Knepley 
35602f856554SMatthew G. Knepley   Input Parameters:
35612f856554SMatthew G. Knepley + dm     - The DM
35622f856554SMatthew G. Knepley . fStart - The first face to include
35632f856554SMatthew G. Knepley . fEnd   - The first face to exclude
35642f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
35652f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
35662f856554SMatthew G. Knepley 
35672f856554SMatthew G. Knepley   Output Parameters:
35682f856554SMatthew G. Knepley + Nface - The number of faces with field values
35692f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
35702f856554SMatthew G. Knepley - vol   - The cell volume
35712f856554SMatthew G. Knepley 
35722f856554SMatthew G. Knepley   Level: developer
35732f856554SMatthew G. Knepley 
35742f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
35752f856554SMatthew G. Knepley @*/
35762f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
35772f856554SMatthew G. Knepley {
35782f856554SMatthew G. Knepley   PetscErrorCode ierr;
35792f856554SMatthew G. Knepley 
35802f856554SMatthew G. Knepley   PetscFunctionBegin;
35812f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
35822f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
35832f856554SMatthew G. Knepley   PetscFunctionReturn(0);
35842f856554SMatthew G. Knepley }
35852f856554SMatthew G. Knepley 
3586a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3587a1cf66bbSMatthew G. Knepley {
3588a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3589a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3590a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3591a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3592a1cf66bbSMatthew G. Knepley 
3593a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3594a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3595a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3596a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3597a1cf66bbSMatthew G. Knepley   if (container) {
3598a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3599a1cf66bbSMatthew G. Knepley   } else {
3600a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3601a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3602a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3603a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3604a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3605a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3606a1cf66bbSMatthew G. Knepley   }
3607a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3608a1cf66bbSMatthew G. Knepley }
3609a1cf66bbSMatthew G. Knepley 
3610a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3611a1cf66bbSMatthew G. Knepley {
3612a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3613a1cf66bbSMatthew G. Knepley   *geom = NULL;
3614a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3615a1cf66bbSMatthew G. Knepley }
3616a1cf66bbSMatthew G. Knepley 
361792d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
361892d50984SMatthew G. Knepley {
361992d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
362092d50984SMatthew G. Knepley   const char      *name       = "Residual";
362192d50984SMatthew G. Knepley   DM               dmAux      = NULL;
362292d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
362392d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
362492d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
362592d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
362692d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
362792d50984SMatthew G. Knepley   DMField          coordField = NULL;
3628c0006e53SPatrick Farrell   Vec              locA;
3629c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
363092d50984SMatthew G. Knepley   IS               chunkIS;
363192d50984SMatthew G. Knepley   const PetscInt  *cells;
363292d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3633364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
363492d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
363592d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
363692d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
363792d50984SMatthew G. Knepley   PetscErrorCode   ierr;
363892d50984SMatthew G. Knepley 
363992d50984SMatthew G. Knepley   PetscFunctionBegin;
364092d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
364192d50984SMatthew G. Knepley   /* FEM+FVM */
364292d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
364392d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
364492d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
364592d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
364692d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
364792d50984SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
364892d50984SMatthew G. Knepley   if (locA) {
364992d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
365092d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
365192d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
365292d50984SMatthew G. Knepley   }
365392d50984SMatthew G. Knepley   /* 2: Get geometric data */
365492d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
365592d50984SMatthew G. Knepley     PetscObject  obj;
365692d50984SMatthew G. Knepley     PetscClassId id;
365792d50984SMatthew G. Knepley     PetscBool    fimp;
365892d50984SMatthew G. Knepley 
365992d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
366092d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
366192d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
366292d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
366392d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
3664364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
366592d50984SMatthew G. Knepley   }
366692d50984SMatthew G. Knepley   if (useFEM) {
366792d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
366892d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
366992d50984SMatthew G. Knepley     if (maxDegree <= 1) {
367092d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
367192d50984SMatthew G. Knepley       if (affineQuad) {
367292d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
367392d50984SMatthew G. Knepley       }
367492d50984SMatthew G. Knepley     } else {
367592d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
367692d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
367792d50984SMatthew G. Knepley         PetscObject  obj;
367892d50984SMatthew G. Knepley         PetscClassId id;
367992d50984SMatthew G. Knepley         PetscBool    fimp;
368092d50984SMatthew G. Knepley 
368192d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
368292d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
368392d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
368492d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
368592d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
368692d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
368792d50984SMatthew G. Knepley 
368892d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
368992d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
369092d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
369192d50984SMatthew G. Knepley         }
369292d50984SMatthew G. Knepley       }
369392d50984SMatthew G. Knepley     }
369492d50984SMatthew G. Knepley   }
369592d50984SMatthew G. Knepley   /* Loop over chunks */
369692d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
369792d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
369892d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
369992d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
370092d50984SMatthew G. Knepley   numChunks     = 1;
370192d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
370292d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
370392d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
3704c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
3705c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
3706c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
370792d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
3708c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
370992d50984SMatthew G. Knepley 
371092d50984SMatthew G. Knepley     /* Extract field coefficients */
371192d50984SMatthew G. Knepley     if (useFEM) {
371292d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
371392d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
371492d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
3715580bdb30SBarry Smith       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
371692d50984SMatthew G. Knepley     }
371792d50984SMatthew 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 */
371892d50984SMatthew G. Knepley     /* Loop over fields */
371992d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
372092d50984SMatthew G. Knepley       PetscObject  obj;
372192d50984SMatthew G. Knepley       PetscClassId id;
372292d50984SMatthew G. Knepley       PetscBool    fimp;
372392d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
372492d50984SMatthew G. Knepley 
372592d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
372692d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
372792d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
372892d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
372992d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
373092d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
373192d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
373292d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
373392d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
373492d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
373592d50984SMatthew G. Knepley 
373692d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
373792d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
373892d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
373992d50984SMatthew G. Knepley         blockSize = Nb;
374092d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
374192d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
374292d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
374392d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
374492d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
374592d50984SMatthew G. Knepley         offset    = numCells - Nr;
374692d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
374792d50984SMatthew 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) */
374892d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
37494bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
375092d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
37514bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
375292d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
375392d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
375492d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
375592d50984SMatthew G. Knepley 
375692d50984SMatthew G. Knepley         Ne = numFaces;
375792d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
375892d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
375992d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
3760ff1e0c32SBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
376192d50984SMatthew G. Knepley     }
376292d50984SMatthew G. Knepley     /* Loop over domain */
376392d50984SMatthew G. Knepley     if (useFEM) {
376492d50984SMatthew G. Knepley       /* Add elemVec to locX */
376592d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
376692d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
376792d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
376892d50984SMatthew G. Knepley 
376992d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
377092d50984SMatthew G. Knepley         if (ghostLabel) {
377192d50984SMatthew G. Knepley           PetscInt ghostVal;
377292d50984SMatthew G. Knepley 
377392d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
377492d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
377592d50984SMatthew G. Knepley         }
377692d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
377792d50984SMatthew G. Knepley       }
377892d50984SMatthew G. Knepley     }
377992d50984SMatthew G. Knepley     /* Handle time derivative */
378092d50984SMatthew G. Knepley     if (locX_t) {
378192d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
378292d50984SMatthew G. Knepley 
378392d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
378492d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
378592d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
378692d50984SMatthew G. Knepley         PetscFV      fv;
378792d50984SMatthew G. Knepley         PetscObject  obj;
378892d50984SMatthew G. Knepley         PetscClassId id;
378992d50984SMatthew G. Knepley         PetscInt     pdim, d;
379092d50984SMatthew G. Knepley 
379192d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
379292d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
379392d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
379492d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
379592d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
379692d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
379792d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
379892d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
379992d50984SMatthew G. Knepley 
380092d50984SMatthew G. Knepley           if (ghostLabel) {
380192d50984SMatthew G. Knepley             PetscInt ghostVal;
380292d50984SMatthew G. Knepley 
380392d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
380492d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
380592d50984SMatthew G. Knepley           }
380692d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
380792d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
380892d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
380992d50984SMatthew G. Knepley         }
381092d50984SMatthew G. Knepley       }
381192d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
381292d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
381392d50984SMatthew G. Knepley     }
381492d50984SMatthew G. Knepley     if (useFEM) {
381592d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
381692d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
381792d50984SMatthew G. Knepley     }
381892d50984SMatthew G. Knepley   }
381992d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
382092d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
382192d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
382292d50984SMatthew G. Knepley   if (useFEM) {
382392d50984SMatthew G. Knepley     if (maxDegree <= 1) {
382492d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
382592d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
382692d50984SMatthew G. Knepley     } else {
382792d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
382892d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
382992d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
383092d50984SMatthew G. Knepley       }
383192d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
383292d50984SMatthew G. Knepley     }
383392d50984SMatthew G. Knepley   }
383492d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
383592d50984SMatthew G. Knepley   PetscFunctionReturn(0);
383692d50984SMatthew G. Knepley }
383792d50984SMatthew G. Knepley 
3838a1cf66bbSMatthew G. Knepley /*
3839a1cf66bbSMatthew 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
3840a1cf66bbSMatthew G. Knepley 
3841a1cf66bbSMatthew G. Knepley   X   - The local solution vector
3842a1cf66bbSMatthew G. Knepley   X_t - The local solution time derviative vector, or NULL
3843a1cf66bbSMatthew G. Knepley */
3844a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
3845a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
3846a1cf66bbSMatthew G. Knepley {
3847a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
3848a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
3849a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
3850a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
3851a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
3852a1cf66bbSMatthew G. Knepley   Vec              A;
3853a1cf66bbSMatthew G. Knepley   DMField          coordField;
3854a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
3855a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
3856a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
3857a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
38589b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
3859a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
38609b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
3861a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
3862a1cf66bbSMatthew G. Knepley 
3863a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3864a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3865a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
3866a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
3867a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3868a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3869a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3870a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3871a1cf66bbSMatthew G. Knepley   if (dmAux) {
387292fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
3873a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
3874a1cf66bbSMatthew G. Knepley   }
3875a1cf66bbSMatthew G. Knepley   /* Get flags */
3876a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
3877a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3878a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
3879a1cf66bbSMatthew G. Knepley     PetscObject  disc;
3880a1cf66bbSMatthew G. Knepley     PetscClassId id;
3881a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
3882a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
3883a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
3884a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
3885a1cf66bbSMatthew G. Knepley   }
3886a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
3887a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
3888a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
3889a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
3890a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
3891a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
3892a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
3893a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
3894a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
3895a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
3896a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
3897a1cf66bbSMatthew 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 */
3898a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3899a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3900a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3901a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
3902a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3903a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
3904a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
3905a1cf66bbSMatthew G. Knepley     PetscFE         fe;
3906a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
3907a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
3908a1cf66bbSMatthew G. Knepley 
3909a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3910a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
3911a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
3912a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
3913a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
3914a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
3915a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
3916a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
3917a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
3918a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
3919a1cf66bbSMatthew G. Knepley   } else {
3920a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
3921a1cf66bbSMatthew G. Knepley     numChunks = 1;
3922a1cf66bbSMatthew G. Knepley   }
3923a1cf66bbSMatthew G. Knepley   /* Get work space */
3924a1cf66bbSMatthew 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;
3925a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
3926580bdb30SBarry Smith   ierr = PetscArrayzero(work, wsz);CHKERRQ(ierr);
3927a1cf66bbSMatthew G. Knepley   off      = 0;
3928a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3929a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3930a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
3931a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3932a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3933a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3934a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
3935a1cf66bbSMatthew G. Knepley   /* Setup geometry */
3936a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
39379b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
39389b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
3939a1cf66bbSMatthew G. Knepley   if (!qGeom) {
3940a1cf66bbSMatthew G. Knepley     PetscFE fe;
3941a1cf66bbSMatthew G. Knepley 
3942a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3943a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
3944a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
3945a1cf66bbSMatthew G. Knepley   }
3946a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3947a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
3948a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
3949a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
3950a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
3951a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
3952a1cf66bbSMatthew G. Knepley     PetscInt         c;
3953a1cf66bbSMatthew G. Knepley 
3954a1cf66bbSMatthew G. Knepley     /* Extract values */
3955a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3956a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3957a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
3958a1cf66bbSMatthew G. Knepley       PetscInt       i;
3959a1cf66bbSMatthew G. Knepley 
3960a1cf66bbSMatthew G. Knepley       if (X) {
3961a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3962a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
3963a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3964a1cf66bbSMatthew G. Knepley       }
3965a1cf66bbSMatthew G. Knepley       if (X_t) {
3966a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3967a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
3968a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3969a1cf66bbSMatthew G. Knepley       }
3970a1cf66bbSMatthew G. Knepley       if (dmAux) {
3971a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3972a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
3973a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3974a1cf66bbSMatthew G. Knepley       }
3975a1cf66bbSMatthew G. Knepley     }
3976a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3977a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
3978a1cf66bbSMatthew G. Knepley       PetscFE fe;
3979a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
3980a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
39814bee2e38SMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN,     fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
39824bee2e38SMatthew G. Knepley         if (hasPrec) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);}
39834bee2e38SMatthew G. Knepley         if (hasDyn)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);}
3984a1cf66bbSMatthew G. Knepley       }
3985a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
3986a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
3987a1cf66bbSMatthew G. Knepley         PetscFV  fv;
3988a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
3989a1cf66bbSMatthew G. Knepley 
3990a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
3991a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
3992a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
3993a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
3994a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
3995a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
3996a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
3997a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
3998a1cf66bbSMatthew G. Knepley           }
3999a1cf66bbSMatthew G. Knepley         }
4000a1cf66bbSMatthew G. Knepley       }
4001a1cf66bbSMatthew G. Knepley     }
4002a1cf66bbSMatthew G. Knepley     CHKMEMQ;
4003a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
4004a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
4005a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
4006a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
4007a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
4008a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
4009a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4010a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4011a1cf66bbSMatthew G. Knepley       }
4012a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
4013a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
4014a1cf66bbSMatthew G. Knepley     }
4015a1cf66bbSMatthew G. Knepley     CHKMEMQ;
4016a1cf66bbSMatthew G. Knepley   }
4017a1cf66bbSMatthew G. Knepley   /* Cleanup */
4018a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
4019a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
4020a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
4021a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
4022a1cf66bbSMatthew 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);
4023a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
4024a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
4025a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
4026a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
4027a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4028a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
4029a1cf66bbSMatthew G. Knepley   CHKMEMQ;
4030a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
4031a1cf66bbSMatthew G. Knepley }
40323e9753d6SMatthew G. Knepley 
40333e9753d6SMatthew G. Knepley /******** FEM Assembly Function ********/
40343e9753d6SMatthew G. Knepley 
40353e9753d6SMatthew G. Knepley static PetscErrorCode DMConvertPlex_Internal(DM dm, DM *plex, PetscBool copy)
40363e9753d6SMatthew G. Knepley {
40373e9753d6SMatthew G. Knepley   PetscBool      isPlex;
40383e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
40393e9753d6SMatthew G. Knepley 
40403e9753d6SMatthew G. Knepley   PetscFunctionBegin;
40413e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
40423e9753d6SMatthew G. Knepley   if (isPlex) {
40433e9753d6SMatthew G. Knepley     *plex = dm;
40443e9753d6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
40453e9753d6SMatthew G. Knepley   } else {
40463e9753d6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
40473e9753d6SMatthew G. Knepley     if (!*plex) {
40483e9753d6SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
40493e9753d6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
40503e9753d6SMatthew G. Knepley       if (copy) {
40513e9753d6SMatthew G. Knepley         const char *comps[] = {"A", "dmAux"};
40523e9753d6SMatthew G. Knepley         PetscObject obj;
40533e9753d6SMatthew G. Knepley         PetscInt    i;
40543e9753d6SMatthew G. Knepley 
40553e9753d6SMatthew G. Knepley         for (i = 0; i < 2; ++i) {
40563e9753d6SMatthew G. Knepley           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
40573e9753d6SMatthew G. Knepley           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
40583e9753d6SMatthew G. Knepley         }
40593e9753d6SMatthew G. Knepley       }
40603e9753d6SMatthew G. Knepley     } else {
40613e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
40623e9753d6SMatthew G. Knepley     }
40633e9753d6SMatthew G. Knepley   }
40643e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
40653e9753d6SMatthew G. Knepley }
40663e9753d6SMatthew G. Knepley 
40673e9753d6SMatthew G. Knepley /*@
40683e9753d6SMatthew G. Knepley   DMPlexGetGeometryFVM - Return precomputed geometric data
40693e9753d6SMatthew G. Knepley 
40703e9753d6SMatthew G. Knepley   Collective on DM
40713e9753d6SMatthew G. Knepley 
40723e9753d6SMatthew G. Knepley   Input Parameter:
40733e9753d6SMatthew G. Knepley . dm - The DM
40743e9753d6SMatthew G. Knepley 
40753e9753d6SMatthew G. Knepley   Output Parameters:
40763e9753d6SMatthew G. Knepley + facegeom - The values precomputed from face geometry
40773e9753d6SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
40783e9753d6SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
40793e9753d6SMatthew G. Knepley 
40803e9753d6SMatthew G. Knepley   Level: developer
40813e9753d6SMatthew G. Knepley 
40823e9753d6SMatthew G. Knepley .seealso: DMPlexTSSetRHSFunctionLocal()
40833e9753d6SMatthew G. Knepley @*/
40843e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
40853e9753d6SMatthew G. Knepley {
40863e9753d6SMatthew G. Knepley   DM             plex;
40873e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
40883e9753d6SMatthew G. Knepley 
40893e9753d6SMatthew G. Knepley   PetscFunctionBegin;
40903e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40913e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
40923e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
40933e9753d6SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
40943e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
40953e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
40963e9753d6SMatthew G. Knepley }
40973e9753d6SMatthew G. Knepley 
40983e9753d6SMatthew G. Knepley /*@
40993e9753d6SMatthew G. Knepley   DMPlexGetGradientDM - Return gradient data layout
41003e9753d6SMatthew G. Knepley 
41013e9753d6SMatthew G. Knepley   Collective on DM
41023e9753d6SMatthew G. Knepley 
41033e9753d6SMatthew G. Knepley   Input Parameters:
41043e9753d6SMatthew G. Knepley + dm - The DM
41053e9753d6SMatthew G. Knepley - fv - The PetscFV
41063e9753d6SMatthew G. Knepley 
41073e9753d6SMatthew G. Knepley   Output Parameter:
41083e9753d6SMatthew G. Knepley . dmGrad - The layout for gradient values
41093e9753d6SMatthew G. Knepley 
41103e9753d6SMatthew G. Knepley   Level: developer
41113e9753d6SMatthew G. Knepley 
41123e9753d6SMatthew G. Knepley .seealso: DMPlexSNESGetGeometryFVM()
41133e9753d6SMatthew G. Knepley @*/
41143e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
41153e9753d6SMatthew G. Knepley {
41163e9753d6SMatthew G. Knepley   DM             plex;
41173e9753d6SMatthew G. Knepley   PetscBool      computeGradients;
41183e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
41193e9753d6SMatthew G. Knepley 
41203e9753d6SMatthew G. Knepley   PetscFunctionBegin;
41213e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41223e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
41233e9753d6SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
41243e9753d6SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
41253e9753d6SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
41263e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
41273e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
41283e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
41293e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
41303e9753d6SMatthew G. Knepley }
41313e9753d6SMatthew G. Knepley 
41323e9753d6SMatthew G. Knepley static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
41333e9753d6SMatthew G. Knepley {
41343e9753d6SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
41353e9753d6SMatthew G. Knepley   DM               plex = NULL, plexA = NULL;
41363e9753d6SMatthew G. Knepley   DMEnclosureType  encAux;
41373e9753d6SMatthew G. Knepley   PetscDS          prob, probAux = NULL;
41383e9753d6SMatthew G. Knepley   PetscSection     section, sectionAux = NULL;
41393e9753d6SMatthew G. Knepley   Vec              locA = NULL;
41403e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
41413e9753d6SMatthew G. Knepley   PetscInt         v;
41423e9753d6SMatthew G. Knepley   PetscInt         totDim, totDimAux = 0;
41433e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
41443e9753d6SMatthew G. Knepley 
41453e9753d6SMatthew G. Knepley   PetscFunctionBegin;
41463e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
41473e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
41483e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
41493e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
41503e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
41513e9753d6SMatthew G. Knepley   if (locA) {
41523e9753d6SMatthew G. Knepley     DM dmAux;
41533e9753d6SMatthew G. Knepley 
41543e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
41553e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
41563e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
41573e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
41583e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
41593e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
41603e9753d6SMatthew G. Knepley   }
41613e9753d6SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
41623e9753d6SMatthew G. Knepley     PetscFEGeom    *fgeom;
41633e9753d6SMatthew G. Knepley     PetscInt        maxDegree;
41643e9753d6SMatthew G. Knepley     PetscQuadrature qGeom = NULL;
41653e9753d6SMatthew G. Knepley     IS              pointIS;
41663e9753d6SMatthew G. Knepley     const PetscInt *points;
41673e9753d6SMatthew G. Knepley     PetscInt        numFaces, face, Nq;
41683e9753d6SMatthew G. Knepley 
41693e9753d6SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
41703e9753d6SMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
41713e9753d6SMatthew G. Knepley     {
41723e9753d6SMatthew G. Knepley       IS isectIS;
41733e9753d6SMatthew G. Knepley 
41743e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
41753e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
41763e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
41773e9753d6SMatthew G. Knepley       pointIS = isectIS;
41783e9753d6SMatthew G. Knepley     }
41793e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr);
41803e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
41813e9753d6SMatthew G. Knepley     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
41823e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
41833e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
41843e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
41853e9753d6SMatthew G. Knepley     }
41863e9753d6SMatthew G. Knepley     if (!qGeom) {
41873e9753d6SMatthew G. Knepley       PetscFE fe;
41883e9753d6SMatthew G. Knepley 
41893e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
41903e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
41913e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
41923e9753d6SMatthew G. Knepley     }
41933e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
41943e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
41953e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
41963e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support, *cone;
41973e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
41983e9753d6SMatthew G. Knepley       PetscInt       i, coneSize, faceLoc;
41993e9753d6SMatthew G. Knepley 
42003e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
42013e9753d6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
42023e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
42033e9753d6SMatthew G. Knepley       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
42043e9753d6SMatthew G. Knepley       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
42053e9753d6SMatthew G. Knepley       fgeom->face[face][0] = faceLoc;
42063e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
42073e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
42083e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
42093e9753d6SMatthew G. Knepley       if (locX_t) {
42103e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
42113e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
42123e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
42133e9753d6SMatthew G. Knepley       }
42143e9753d6SMatthew G. Knepley       if (locA) {
42153e9753d6SMatthew G. Knepley         PetscInt subp;
42163e9753d6SMatthew G. Knepley 
42173e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
42183e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
42193e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
42203e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
42213e9753d6SMatthew G. Knepley       }
42223e9753d6SMatthew G. Knepley     }
42233e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemVec, numFaces*totDim);CHKERRQ(ierr);
42243e9753d6SMatthew G. Knepley     {
42253e9753d6SMatthew G. Knepley       PetscFE         fe;
42263e9753d6SMatthew G. Knepley       PetscInt        Nb;
42273e9753d6SMatthew G. Knepley       PetscFEGeom     *chunkGeom = NULL;
42283e9753d6SMatthew G. Knepley       /* Conforming batches */
42293e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
42303e9753d6SMatthew G. Knepley       /* Remainder */
42313e9753d6SMatthew G. Knepley       PetscInt        Nr, offset;
42323e9753d6SMatthew G. Knepley 
42333e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
42343e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
42353e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
42363e9753d6SMatthew G. Knepley       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
42373e9753d6SMatthew G. Knepley       blockSize = Nb;
42383e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
42393e9753d6SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
42403e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
42413e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
42423e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
42433e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
42443e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
42453e9753d6SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, field, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
42463e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
42473e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
42483e9753d6SMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, field, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
42493e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
42503e9753d6SMatthew G. Knepley     }
42513e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
42523e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
42533e9753d6SMatthew G. Knepley 
42543e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
42553e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
42563e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
42573e9753d6SMatthew G. Knepley     }
42583e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
42593e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
42603e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
42613e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
42623e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr);
42633e9753d6SMatthew G. Knepley   }
42643e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
42653e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
42663e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
42673e9753d6SMatthew G. Knepley }
42683e9753d6SMatthew G. Knepley 
42693e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, Vec locF)
42703e9753d6SMatthew G. Knepley {
42713e9753d6SMatthew G. Knepley   DMField        coordField;
42723e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
42733e9753d6SMatthew G. Knepley   IS             facetIS;
42743e9753d6SMatthew G. Knepley   PetscInt       dim;
42753e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
42763e9753d6SMatthew G. Knepley 
42773e9753d6SMatthew G. Knepley   PetscFunctionBegin;
42783e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42793e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
42803e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
42813e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
42823e9753d6SMatthew G. Knepley   ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
42833e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
42843e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
42853e9753d6SMatthew G. Knepley }
42863e9753d6SMatthew G. Knepley 
42873e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
42883e9753d6SMatthew G. Knepley {
42893e9753d6SMatthew G. Knepley   PetscDS        prob;
42903e9753d6SMatthew G. Knepley   PetscInt       numBd, bd;
42913e9753d6SMatthew G. Knepley   DMField        coordField = NULL;
42923e9753d6SMatthew G. Knepley   IS             facetIS    = NULL;
42933e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
42943e9753d6SMatthew G. Knepley   PetscInt       dim;
42953e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
42963e9753d6SMatthew G. Knepley 
42973e9753d6SMatthew G. Knepley   PetscFunctionBegin;
42983e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
42993e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
43003e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43013e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr);
43023e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
43033e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
43043e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
43053e9753d6SMatthew G. Knepley     const char             *bdLabel;
43063e9753d6SMatthew G. Knepley     DMLabel                 label;
43073e9753d6SMatthew G. Knepley     const PetscInt         *values;
43083e9753d6SMatthew G. Knepley     PetscInt                field, numValues;
43093e9753d6SMatthew G. Knepley     PetscObject             obj;
43103e9753d6SMatthew G. Knepley     PetscClassId            id;
43113e9753d6SMatthew G. Knepley 
43123e9753d6SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &field, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
43133e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
43143e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
43153e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
43163e9753d6SMatthew G. Knepley     if (!facetIS) {
43173e9753d6SMatthew G. Knepley       DMLabel  depthLabel;
43183e9753d6SMatthew G. Knepley       PetscInt dim;
43193e9753d6SMatthew G. Knepley 
43203e9753d6SMatthew G. Knepley       ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
43213e9753d6SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
43223e9753d6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr);
43233e9753d6SMatthew G. Knepley     }
43243e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
43253e9753d6SMatthew G. Knepley     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
43263e9753d6SMatthew G. Knepley     ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
43273e9753d6SMatthew G. Knepley   }
43283e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
43293e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
43303e9753d6SMatthew G. Knepley }
43313e9753d6SMatthew G. Knepley 
43323e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
43333e9753d6SMatthew G. Knepley {
43343e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
43353e9753d6SMatthew G. Knepley   const char      *name       = "Residual";
43363e9753d6SMatthew G. Knepley   DM               dmAux      = NULL;
43373e9753d6SMatthew G. Knepley   DM               dmGrad     = NULL;
43383e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
43393e9753d6SMatthew G. Knepley   PetscDS          prob       = NULL;
43403e9753d6SMatthew G. Knepley   PetscDS          probAux    = NULL;
43413e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
43423e9753d6SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
43433e9753d6SMatthew G. Knepley   PetscBool        useFVM     = PETSC_FALSE;
43443e9753d6SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
43453e9753d6SMatthew G. Knepley   PetscFV          fvm        = NULL;
43463e9753d6SMatthew G. Knepley   PetscFVCellGeom *cgeomFVM   = NULL;
43473e9753d6SMatthew G. Knepley   PetscFVFaceGeom *fgeomFVM   = NULL;
43483e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
43493e9753d6SMatthew G. Knepley   Vec              locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
43503e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a, *uL, *uR;
43513e9753d6SMatthew G. Knepley   IS               chunkIS;
43523e9753d6SMatthew G. Knepley   const PetscInt  *cells;
43533e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
43543e9753d6SMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
43553e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
43563e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
43573e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
43583e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
43593e9753d6SMatthew G. Knepley 
43603e9753d6SMatthew G. Knepley   PetscFunctionBegin;
43613e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
43623e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
43633e9753d6SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
43643e9753d6SMatthew G. Knepley   /* FEM+FVM */
43653e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
43663e9753d6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
43673e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
43683e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
43693e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
43703e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
43713e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
43723e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
43733e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
43743e9753d6SMatthew G. Knepley   if (locA) {
43753e9753d6SMatthew G. Knepley     PetscInt subcell;
43763e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
43773e9753d6SMatthew G. Knepley     ierr = DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);CHKERRQ(ierr);
43783e9753d6SMatthew G. Knepley     ierr = DMGetCellDS(dmAux, subcell, &probAux);CHKERRQ(ierr);
43793e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
43803e9753d6SMatthew G. Knepley   }
43813e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
43823e9753d6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
43833e9753d6SMatthew G. Knepley     PetscObject  obj;
43843e9753d6SMatthew G. Knepley     PetscClassId id;
43853e9753d6SMatthew G. Knepley     PetscBool    fimp;
43863e9753d6SMatthew G. Knepley 
43873e9753d6SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
43883e9753d6SMatthew G. Knepley     if (isImplicit != fimp) continue;
43893e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
43903e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
43913e9753d6SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
43923e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
43933e9753d6SMatthew G. Knepley   }
43943e9753d6SMatthew G. Knepley   if (useFEM) {
43953e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
43963e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
43973e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
43983e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
43993e9753d6SMatthew G. Knepley       if (affineQuad) {
44003e9753d6SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
44013e9753d6SMatthew G. Knepley       }
44023e9753d6SMatthew G. Knepley     } else {
44033e9753d6SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
44043e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
44053e9753d6SMatthew G. Knepley         PetscObject  obj;
44063e9753d6SMatthew G. Knepley         PetscClassId id;
44073e9753d6SMatthew G. Knepley         PetscBool    fimp;
44083e9753d6SMatthew G. Knepley 
44093e9753d6SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
44103e9753d6SMatthew G. Knepley         if (isImplicit != fimp) continue;
44113e9753d6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
44123e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
44133e9753d6SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
44143e9753d6SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
44153e9753d6SMatthew G. Knepley 
44163e9753d6SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
44173e9753d6SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
44183e9753d6SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
44193e9753d6SMatthew G. Knepley         }
44203e9753d6SMatthew G. Knepley       }
44213e9753d6SMatthew G. Knepley     }
44223e9753d6SMatthew G. Knepley   }
44233e9753d6SMatthew G. Knepley   if (useFVM) {
44243e9753d6SMatthew G. Knepley     ierr = DMPlexGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
44253e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
44263e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
44273e9753d6SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
44283e9753d6SMatthew G. Knepley     ierr = DMPlexGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
44293e9753d6SMatthew G. Knepley     if (dmGrad) {
44303e9753d6SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
44313e9753d6SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
44323e9753d6SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
44333e9753d6SMatthew G. Knepley       /* Communicate gradient values */
44343e9753d6SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
44353e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
44363e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
44373e9753d6SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
44383e9753d6SMatthew G. Knepley     }
44393e9753d6SMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
44403e9753d6SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
44413e9753d6SMatthew G. Knepley   }
44423e9753d6SMatthew G. Knepley   /* Loop over chunks */
44433e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
44443e9753d6SMatthew G. Knepley   numCells      = cEnd - cStart;
44453e9753d6SMatthew G. Knepley   numChunks     = 1;
44463e9753d6SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
44473e9753d6SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
44483e9753d6SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
44493e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
44503e9753d6SMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
44513e9753d6SMatthew G. Knepley     PetscReal       *vol;
44523e9753d6SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
44533e9753d6SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
44543e9753d6SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
44553e9753d6SMatthew G. Knepley 
44563e9753d6SMatthew G. Knepley     /* Extract field coefficients */
44573e9753d6SMatthew G. Knepley     if (useFEM) {
44583e9753d6SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
44593e9753d6SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
44603e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
44613e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
44623e9753d6SMatthew G. Knepley     }
44633e9753d6SMatthew G. Knepley     if (useFVM) {
44643e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
44653e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
44663e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
44673e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
44683e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxL, numFaces*totDim);CHKERRQ(ierr);
44693e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxR, numFaces*totDim);CHKERRQ(ierr);
44703e9753d6SMatthew G. Knepley     }
44713e9753d6SMatthew 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 */
44723e9753d6SMatthew G. Knepley     /* Loop over fields */
44733e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
44743e9753d6SMatthew G. Knepley       PetscObject  obj;
44753e9753d6SMatthew G. Knepley       PetscClassId id;
44763e9753d6SMatthew G. Knepley       PetscBool    fimp;
44773e9753d6SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
44783e9753d6SMatthew G. Knepley 
44793e9753d6SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
44803e9753d6SMatthew G. Knepley       if (isImplicit != fimp) continue;
44813e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
44823e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
44833e9753d6SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
44843e9753d6SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
44853e9753d6SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
44863e9753d6SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
44873e9753d6SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
44883e9753d6SMatthew G. Knepley         PetscInt        Nq, Nb;
44893e9753d6SMatthew G. Knepley 
44903e9753d6SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
44913e9753d6SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
44923e9753d6SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
44933e9753d6SMatthew G. Knepley         blockSize = Nb;
44943e9753d6SMatthew G. Knepley         batchSize = numBlocks * blockSize;
44953e9753d6SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
44963e9753d6SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
44973e9753d6SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
44983e9753d6SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
44993e9753d6SMatthew G. Knepley         offset    = numCells - Nr;
45003e9753d6SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
45013e9753d6SMatthew 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) */
45023e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
45033e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
45043e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
45053e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
45063e9753d6SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
45073e9753d6SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
45083e9753d6SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
45093e9753d6SMatthew G. Knepley 
45103e9753d6SMatthew G. Knepley         Ne = numFaces;
45113e9753d6SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
45123e9753d6SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
45133e9753d6SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
45143e9753d6SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
45153e9753d6SMatthew G. Knepley     }
45163e9753d6SMatthew G. Knepley     /* Loop over domain */
45173e9753d6SMatthew G. Knepley     if (useFEM) {
45183e9753d6SMatthew G. Knepley       /* Add elemVec to locX */
45193e9753d6SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
45203e9753d6SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
45213e9753d6SMatthew G. Knepley         const PetscInt cind = c - cStart;
45223e9753d6SMatthew G. Knepley 
45233e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
45243e9753d6SMatthew G. Knepley         if (ghostLabel) {
45253e9753d6SMatthew G. Knepley           PetscInt ghostVal;
45263e9753d6SMatthew G. Knepley 
45273e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
45283e9753d6SMatthew G. Knepley           if (ghostVal > 0) continue;
45293e9753d6SMatthew G. Knepley         }
45303e9753d6SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
45313e9753d6SMatthew G. Knepley       }
45323e9753d6SMatthew G. Knepley     }
45333e9753d6SMatthew G. Knepley     if (useFVM) {
45343e9753d6SMatthew G. Knepley       PetscScalar *fa;
45353e9753d6SMatthew G. Knepley       PetscInt     iface;
45363e9753d6SMatthew G. Knepley 
45373e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
45383e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
45393e9753d6SMatthew G. Knepley         PetscFV      fv;
45403e9753d6SMatthew G. Knepley         PetscObject  obj;
45413e9753d6SMatthew G. Knepley         PetscClassId id;
45423e9753d6SMatthew G. Knepley         PetscInt     foff, pdim;
45433e9753d6SMatthew G. Knepley 
45443e9753d6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
45453e9753d6SMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
45463e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
45473e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
45483e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
45493e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
45503e9753d6SMatthew G. Knepley         /* Accumulate fluxes to cells */
45513e9753d6SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
45523e9753d6SMatthew G. Knepley           const PetscInt *scells;
45533e9753d6SMatthew G. Knepley           PetscScalar    *fL = NULL, *fR = NULL;
45543e9753d6SMatthew G. Knepley           PetscInt        ghost, d, nsupp, nchild;
45553e9753d6SMatthew G. Knepley 
45563e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
45573e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
45583e9753d6SMatthew G. Knepley           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
45593e9753d6SMatthew G. Knepley           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
45603e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr);
45613e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr);
45623e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);}
45633e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr);
45643e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);}
45653e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
45663e9753d6SMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
45673e9753d6SMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
45683e9753d6SMatthew G. Knepley           }
45693e9753d6SMatthew G. Knepley           ++iface;
45703e9753d6SMatthew G. Knepley         }
45713e9753d6SMatthew G. Knepley       }
45723e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
45733e9753d6SMatthew G. Knepley     }
45743e9753d6SMatthew G. Knepley     /* Handle time derivative */
45753e9753d6SMatthew G. Knepley     if (locX_t) {
45763e9753d6SMatthew G. Knepley       PetscScalar *x_t, *fa;
45773e9753d6SMatthew G. Knepley 
45783e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
45793e9753d6SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
45803e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
45813e9753d6SMatthew G. Knepley         PetscFV      fv;
45823e9753d6SMatthew G. Knepley         PetscObject  obj;
45833e9753d6SMatthew G. Knepley         PetscClassId id;
45843e9753d6SMatthew G. Knepley         PetscInt     pdim, d;
45853e9753d6SMatthew G. Knepley 
45863e9753d6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
45873e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
45883e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
45893e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
45903e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
45913e9753d6SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
45923e9753d6SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
45933e9753d6SMatthew G. Knepley           PetscScalar   *u_t, *r;
45943e9753d6SMatthew G. Knepley 
45953e9753d6SMatthew G. Knepley           if (ghostLabel) {
45963e9753d6SMatthew G. Knepley             PetscInt ghostVal;
45973e9753d6SMatthew G. Knepley 
45983e9753d6SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
45993e9753d6SMatthew G. Knepley             if (ghostVal > 0) continue;
46003e9753d6SMatthew G. Knepley           }
46013e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
46023e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
46033e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
46043e9753d6SMatthew G. Knepley         }
46053e9753d6SMatthew G. Knepley       }
46063e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
46073e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
46083e9753d6SMatthew G. Knepley     }
46093e9753d6SMatthew G. Knepley     if (useFEM) {
46103e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
46113e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
46123e9753d6SMatthew G. Knepley     }
46133e9753d6SMatthew G. Knepley     if (useFVM) {
46143e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
46153e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
46163e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
46173e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
46183e9753d6SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
46193e9753d6SMatthew G. Knepley     }
46203e9753d6SMatthew G. Knepley   }
46213e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
46223e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
46233e9753d6SMatthew G. Knepley 
46243e9753d6SMatthew G. Knepley   if (useFEM) {
46253e9753d6SMatthew G. Knepley     ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);
46263e9753d6SMatthew G. Knepley 
46273e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
46283e9753d6SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
46293e9753d6SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
46303e9753d6SMatthew G. Knepley     } else {
46313e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
46323e9753d6SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
46333e9753d6SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
46343e9753d6SMatthew G. Knepley       }
46353e9753d6SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
46363e9753d6SMatthew G. Knepley     }
46373e9753d6SMatthew G. Knepley   }
46383e9753d6SMatthew G. Knepley 
46393e9753d6SMatthew G. Knepley   /* FEM */
46403e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
46413e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
46423e9753d6SMatthew G. Knepley   /* 3: Handle boundary values */
46433e9753d6SMatthew G. Knepley   /* 4: Loop over domain */
46443e9753d6SMatthew G. Knepley   /*   Extract coefficients */
46453e9753d6SMatthew G. Knepley   /* Loop over fields */
46463e9753d6SMatthew G. Knepley   /*   Set tiling for FE*/
46473e9753d6SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
46483e9753d6SMatthew G. Knepley   /*     Loop over subdomain */
46493e9753d6SMatthew G. Knepley   /*       Loop over quad points */
46503e9753d6SMatthew G. Knepley   /*         Transform coords to real space */
46513e9753d6SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
46523e9753d6SMatthew G. Knepley   /*         Evaluate residual at point */
46533e9753d6SMatthew G. Knepley   /*         Transform residual to real space */
46543e9753d6SMatthew G. Knepley   /*       Add residual to elemVec */
46553e9753d6SMatthew G. Knepley   /* Loop over domain */
46563e9753d6SMatthew G. Knepley   /*   Add elemVec to locX */
46573e9753d6SMatthew G. Knepley 
46583e9753d6SMatthew G. Knepley   /* FVM */
46593e9753d6SMatthew G. Knepley   /* Get geometric data */
46603e9753d6SMatthew G. Knepley   /* If using gradients */
46613e9753d6SMatthew G. Knepley   /*   Compute gradient data */
46623e9753d6SMatthew G. Knepley   /*   Loop over domain faces */
46633e9753d6SMatthew G. Knepley   /*     Count computational faces */
46643e9753d6SMatthew G. Knepley   /*     Reconstruct cell gradient */
46653e9753d6SMatthew G. Knepley   /*   Loop over domain cells */
46663e9753d6SMatthew G. Knepley   /*     Limit cell gradients */
46673e9753d6SMatthew G. Knepley   /* Handle boundary values */
46683e9753d6SMatthew G. Knepley   /* Loop over domain faces */
46693e9753d6SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
46703e9753d6SMatthew G. Knepley   /* Riemann solve over faces */
46713e9753d6SMatthew G. Knepley   /* Loop over domain faces */
46723e9753d6SMatthew G. Knepley   /*   Accumulate fluxes to cells */
46733e9753d6SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
46743e9753d6SMatthew G. Knepley   if (mesh->printFEM) {
46753e9753d6SMatthew G. Knepley     Vec         locFbc;
46763e9753d6SMatthew G. Knepley     PetscInt    pStart, pEnd, p, maxDof;
46773e9753d6SMatthew G. Knepley     PetscScalar *zeroes;
46783e9753d6SMatthew G. Knepley 
46793e9753d6SMatthew G. Knepley     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
46803e9753d6SMatthew G. Knepley     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
46813e9753d6SMatthew G. Knepley     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
46823e9753d6SMatthew G. Knepley     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
46833e9753d6SMatthew G. Knepley     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
46843e9753d6SMatthew G. Knepley     for (p = pStart; p < pEnd; p++) {
46853e9753d6SMatthew G. Knepley       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
46863e9753d6SMatthew G. Knepley     }
46873e9753d6SMatthew G. Knepley     ierr = PetscFree(zeroes);CHKERRQ(ierr);
46883e9753d6SMatthew G. Knepley     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
46893e9753d6SMatthew G. Knepley     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
46903e9753d6SMatthew G. Knepley   }
46913e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
46923e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
46933e9753d6SMatthew G. Knepley }
46943e9753d6SMatthew G. Knepley 
46953e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
46963e9753d6SMatthew G. Knepley {
46973e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
46983e9753d6SMatthew G. Knepley   const char      *name       = "Hybrid Residual";
46993e9753d6SMatthew G. Knepley   DM               dmAux      = NULL;
47003e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
47013e9753d6SMatthew G. Knepley   PetscDS          prob       = NULL;
47023e9753d6SMatthew G. Knepley   PetscDS          probAux    = NULL;
47033e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
47043e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
47053e9753d6SMatthew G. Knepley   Vec              locA;
47063e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a;
47073e9753d6SMatthew G. Knepley   PetscScalar     *elemVec;
47083e9753d6SMatthew G. Knepley   IS               chunkIS;
47093e9753d6SMatthew G. Knepley   const PetscInt  *cells;
47103e9753d6SMatthew G. Knepley   PetscInt        *faces;
47113e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
47123e9753d6SMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk;
47133e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
47143e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
47153e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
47163e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
47173e9753d6SMatthew G. Knepley 
47183e9753d6SMatthew G. Knepley   PetscFunctionBegin;
47193e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
47203e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
47213e9753d6SMatthew G. Knepley   /* FEM */
47223e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
47233e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
47243e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
47253e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
47263e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr);
47273e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
47283e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
47293e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
47303e9753d6SMatthew G. Knepley   if (locA) {
47313e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
47323e9753d6SMatthew G. Knepley     ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr);
47333e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
47343e9753d6SMatthew G. Knepley   }
47353e9753d6SMatthew G. Knepley   /* 2: Setup geometric data */
47363e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
47373e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
47383e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
47393e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
47403e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
47413e9753d6SMatthew G. Knepley       PetscFE fe;
47423e9753d6SMatthew G. Knepley 
47433e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
47443e9753d6SMatthew G. Knepley       if (fe) {
47453e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
47463e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
47473e9753d6SMatthew G. Knepley       }
47483e9753d6SMatthew G. Knepley     }
47493e9753d6SMatthew G. Knepley   }
47503e9753d6SMatthew G. Knepley   /* Loop over chunks */
47513e9753d6SMatthew G. Knepley   numCells      = cEnd - cStart;
47523e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
47533e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
47543e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
47553e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
47563e9753d6SMatthew G. Knepley   /* Extract field coefficients */
47573e9753d6SMatthew G. Knepley   /* NOTE This needs the end cap faces to have identical orientations */
47583e9753d6SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
47593e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
47603e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
47613e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
47623e9753d6SMatthew G. Knepley 
47633e9753d6SMatthew G. Knepley     ierr = PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
47643e9753d6SMatthew G. Knepley     /* Get faces */
47653e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
47663e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
47673e9753d6SMatthew G. Knepley       const PetscInt *cone;
47683e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
47693e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
47703e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
47713e9753d6SMatthew G. Knepley     }
47723e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
47733e9753d6SMatthew G. Knepley     /* Get geometric data */
47743e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
47753e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
47763e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
47773e9753d6SMatthew G. Knepley     } else {
47783e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
47793e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
47803e9753d6SMatthew G. Knepley       }
47813e9753d6SMatthew G. Knepley     }
47823e9753d6SMatthew G. Knepley     /* Loop over fields */
47833e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
47843e9753d6SMatthew G. Knepley       PetscFE         fe;
47853e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
47863e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL;
47873e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
47883e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
47893e9753d6SMatthew G. Knepley 
47903e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
47913e9753d6SMatthew G. Knepley       if (!fe) continue;
47923e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
47933e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
47943e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
47953e9753d6SMatthew G. Knepley       blockSize = Nb;
47963e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
47973e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
47983e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
47993e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
48003e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
48013e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
48023e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
48033e9753d6SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
48043e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
48053e9753d6SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
48063e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
48073e9753d6SMatthew G. Knepley     }
48083e9753d6SMatthew G. Knepley     /* Add elemVec to locX */
48093e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
48103e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
48113e9753d6SMatthew G. Knepley       const PetscInt cind = c - cStart;
48123e9753d6SMatthew G. Knepley 
48133e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
48143e9753d6SMatthew G. Knepley       if (ghostLabel) {
48153e9753d6SMatthew G. Knepley         PetscInt ghostVal;
48163e9753d6SMatthew G. Knepley 
48173e9753d6SMatthew G. Knepley         ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
48183e9753d6SMatthew G. Knepley         if (ghostVal > 0) continue;
48193e9753d6SMatthew G. Knepley       }
48203e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
48213e9753d6SMatthew G. Knepley     }
48223e9753d6SMatthew G. Knepley   }
48233e9753d6SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
48243e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
48253e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
48263e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
48273e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
48283e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
48293e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
48303e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
48313e9753d6SMatthew G. Knepley   } else {
48323e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
48333e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);}
48343e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
48353e9753d6SMatthew G. Knepley     }
48363e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
48373e9753d6SMatthew G. Knepley   }
48383e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
48393e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
48403e9753d6SMatthew G. Knepley }
48413e9753d6SMatthew G. Knepley 
48423e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, 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)
48433e9753d6SMatthew G. Knepley {
48443e9753d6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
48453e9753d6SMatthew G. Knepley   DM              plex = NULL, plexA = NULL, tdm;
48463e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
48473e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
48483e9753d6SMatthew G. Knepley   PetscSection    section, sectionAux = NULL;
48493e9753d6SMatthew G. Knepley   PetscSection    globalSection, subSection = NULL;
48503e9753d6SMatthew G. Knepley   Vec             locA = NULL, tv;
48513e9753d6SMatthew G. Knepley   PetscScalar    *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
48523e9753d6SMatthew G. Knepley   PetscInt        v;
48533e9753d6SMatthew G. Knepley   PetscInt        Nf, totDim, totDimAux = 0;
48543e9753d6SMatthew G. Knepley   PetscBool       isMatISP, transform;
48553e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
48563e9753d6SMatthew G. Knepley 
48573e9753d6SMatthew G. Knepley   PetscFunctionBegin;
48583e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
48593e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
48603e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
48613e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
48623e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
48633e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
48643e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
48653e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
48663e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
48673e9753d6SMatthew G. Knepley   if (locA) {
48683e9753d6SMatthew G. Knepley     DM dmAux;
48693e9753d6SMatthew G. Knepley 
48703e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
48713e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
48723e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
48733e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
48743e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
48753e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
48763e9753d6SMatthew G. Knepley   }
48773e9753d6SMatthew G. Knepley 
48783e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
48793e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
48803e9753d6SMatthew G. Knepley   if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);}
48813e9753d6SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
48823e9753d6SMatthew G. Knepley     PetscFEGeom    *fgeom;
48833e9753d6SMatthew G. Knepley     PetscInt        maxDegree;
48843e9753d6SMatthew G. Knepley     PetscQuadrature qGeom = NULL;
48853e9753d6SMatthew G. Knepley     IS              pointIS;
48863e9753d6SMatthew G. Knepley     const PetscInt *points;
48873e9753d6SMatthew G. Knepley     PetscInt        numFaces, face, Nq;
48883e9753d6SMatthew G. Knepley 
48893e9753d6SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
48903e9753d6SMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
48913e9753d6SMatthew G. Knepley     {
48923e9753d6SMatthew G. Knepley       IS isectIS;
48933e9753d6SMatthew G. Knepley 
48943e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
48953e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
48963e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
48973e9753d6SMatthew G. Knepley       pointIS = isectIS;
48983e9753d6SMatthew G. Knepley     }
48993e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
49003e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
49013e9753d6SMatthew 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);
49023e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
49033e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
49043e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
49053e9753d6SMatthew G. Knepley     }
49063e9753d6SMatthew G. Knepley     if (!qGeom) {
49073e9753d6SMatthew G. Knepley       PetscFE fe;
49083e9753d6SMatthew G. Knepley 
49093e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
49103e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
49113e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
49123e9753d6SMatthew G. Knepley     }
49133e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
49143e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
49153e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
49163e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support, *cone;
49173e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
49183e9753d6SMatthew G. Knepley       PetscInt       i, coneSize, faceLoc;
49193e9753d6SMatthew G. Knepley 
49203e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
49213e9753d6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
49223e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
49233e9753d6SMatthew G. Knepley       for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
49243e9753d6SMatthew G. Knepley       if (faceLoc == coneSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of support[0] %D", point, support[0]);
49253e9753d6SMatthew G. Knepley       fgeom->face[face][0] = faceLoc;
49263e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
49273e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
49283e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
49293e9753d6SMatthew G. Knepley       if (locX_t) {
49303e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
49313e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
49323e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
49333e9753d6SMatthew G. Knepley       }
49343e9753d6SMatthew G. Knepley       if (locA) {
49353e9753d6SMatthew G. Knepley         PetscInt subp;
49363e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
49373e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
49383e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
49393e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
49403e9753d6SMatthew G. Knepley       }
49413e9753d6SMatthew G. Knepley     }
49423e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemMat, numFaces*totDim*totDim);CHKERRQ(ierr);
49433e9753d6SMatthew G. Knepley     {
49443e9753d6SMatthew G. Knepley       PetscFE         fe;
49453e9753d6SMatthew G. Knepley       PetscInt        Nb;
49463e9753d6SMatthew G. Knepley       /* Conforming batches */
49473e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
49483e9753d6SMatthew G. Knepley       /* Remainder */
49493e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL;
49503e9753d6SMatthew G. Knepley       PetscInt        fieldJ, Nr, offset;
49513e9753d6SMatthew G. Knepley 
49523e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
49533e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
49543e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
49553e9753d6SMatthew G. Knepley       blockSize = Nb;
49563e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
49573e9753d6SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
49583e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
49593e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
49603e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
49613e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
49623e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
49633e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
49643e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
49653e9753d6SMatthew G. Knepley       }
49663e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
49673e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
49683e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, fieldI, fieldJ, 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);
49693e9753d6SMatthew G. Knepley       }
49703e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
49713e9753d6SMatthew G. Knepley     }
49723e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
49733e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
49743e9753d6SMatthew G. Knepley 
49753e9753d6SMatthew G. Knepley       /* Transform to global basis before insertion in Jacobian */
49763e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
49773e9753d6SMatthew G. Knepley       if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
49783e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
49793e9753d6SMatthew G. Knepley       if (!isMatISP) {
49803e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
49813e9753d6SMatthew G. Knepley       } else {
49823e9753d6SMatthew G. Knepley         Mat lJ;
49833e9753d6SMatthew G. Knepley 
49843e9753d6SMatthew G. Knepley         ierr = MatISGetLocalMat(JacP, &lJ);CHKERRQ(ierr);
49853e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
49863e9753d6SMatthew G. Knepley       }
49873e9753d6SMatthew G. Knepley     }
49883e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
49893e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
49903e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
49913e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
49923e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr);
49933e9753d6SMatthew G. Knepley   }
49943e9753d6SMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
49953e9753d6SMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
49963e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
49973e9753d6SMatthew G. Knepley }
49983e9753d6SMatthew G. Knepley 
49993e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP)
50003e9753d6SMatthew G. Knepley {
50013e9753d6SMatthew G. Knepley   DMField        coordField;
50023e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
50033e9753d6SMatthew G. Knepley   IS             facetIS;
50043e9753d6SMatthew G. Knepley   PetscInt       dim;
50053e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
50063e9753d6SMatthew G. Knepley 
50073e9753d6SMatthew G. Knepley   PetscFunctionBegin;
50083e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
50093e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
50103e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
50113e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
50123e9753d6SMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
50133e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
50143e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
50153e9753d6SMatthew G. Knepley }
50163e9753d6SMatthew G. Knepley 
50173e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
50183e9753d6SMatthew G. Knepley {
50193e9753d6SMatthew G. Knepley   PetscDS          prob;
50203e9753d6SMatthew G. Knepley   PetscInt         dim, numBd, bd;
50213e9753d6SMatthew G. Knepley   DMLabel          depthLabel;
50223e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
50233e9753d6SMatthew G. Knepley   IS               facetIS;
50243e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
50253e9753d6SMatthew G. Knepley 
50263e9753d6SMatthew G. Knepley   PetscFunctionBegin;
50273e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
50283e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
50293e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
50303e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
50313e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
50323e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
50333e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
50343e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
50353e9753d6SMatthew G. Knepley     const char             *bdLabel;
50363e9753d6SMatthew G. Knepley     DMLabel                 label;
50373e9753d6SMatthew G. Knepley     const PetscInt         *values;
50383e9753d6SMatthew G. Knepley     PetscInt                fieldI, numValues;
50393e9753d6SMatthew G. Knepley     PetscObject             obj;
50403e9753d6SMatthew G. Knepley     PetscClassId            id;
50413e9753d6SMatthew G. Knepley 
50423e9753d6SMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &type, NULL, &bdLabel, &fieldI, NULL, NULL, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
50433e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
50443e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
50453e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
50463e9753d6SMatthew G. Knepley     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
50473e9753d6SMatthew G. Knepley     ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
50483e9753d6SMatthew G. Knepley   }
50493e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
50503e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
50513e9753d6SMatthew G. Knepley }
50523e9753d6SMatthew G. Knepley 
50533e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
50543e9753d6SMatthew G. Knepley {
50553e9753d6SMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *) dm->data;
50563e9753d6SMatthew G. Knepley   const char     *name  = "Jacobian";
50573e9753d6SMatthew G. Knepley   DM              dmAux, plex, tdm;
50583e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
50593e9753d6SMatthew G. Knepley   Vec             A, tv;
50603e9753d6SMatthew G. Knepley   DMField         coordField;
50613e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
50623e9753d6SMatthew G. Knepley   PetscSection    section, globalSection, subSection, sectionAux;
50633e9753d6SMatthew G. Knepley   PetscScalar    *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
50643e9753d6SMatthew G. Knepley   const PetscInt *cells;
50653e9753d6SMatthew G. Knepley   PetscInt        Nf, fieldI, fieldJ;
50663e9753d6SMatthew G. Knepley   PetscInt        totDim, totDimAux, cStart, cEnd, numCells, c;
50673e9753d6SMatthew G. Knepley   PetscBool       isMatIS, isMatISP, hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform;
50683e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
50693e9753d6SMatthew G. Knepley 
50703e9753d6SMatthew G. Knepley   PetscFunctionBegin;
50713e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
50723e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
50733e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
50743e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
50753e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
50763e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
50773e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
50783e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
50793e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
50803e9753d6SMatthew G. Knepley   if (isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);}
50813e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
50823e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
50833e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
50843e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
50853e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
50863e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
50873e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
50883e9753d6SMatthew G. Knepley   /* user passed in the same matrix, avoid double contributions and
50893e9753d6SMatthew G. Knepley      only assemble the Jacobian */
50903e9753d6SMatthew G. Knepley   if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE;
50913e9753d6SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
50923e9753d6SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
50933e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
50943e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
50953e9753d6SMatthew G. Knepley   if (dmAux) {
50963e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
50973e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
50983e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plex, &sectionAux);CHKERRQ(ierr);
50993e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
51003e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
51013e9753d6SMatthew G. Knepley   }
51023e9753d6SMatthew 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);
51033e9753d6SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
51043e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
51053e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
51063e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
51073e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
51083e9753d6SMatthew G. Knepley     PetscScalar   *x = NULL,  *x_t = NULL;
51093e9753d6SMatthew G. Knepley     PetscInt       i;
51103e9753d6SMatthew G. Knepley 
51113e9753d6SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
51123e9753d6SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
51133e9753d6SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
51143e9753d6SMatthew G. Knepley     if (X_t) {
51153e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
51163e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
51173e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
51183e9753d6SMatthew G. Knepley     }
51193e9753d6SMatthew G. Knepley     if (dmAux) {
51203e9753d6SMatthew G. Knepley       PetscInt subcell;
51213e9753d6SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr);
51223e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
51233e9753d6SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
51243e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
51253e9753d6SMatthew G. Knepley     }
51263e9753d6SMatthew G. Knepley   }
51273e9753d6SMatthew G. Knepley   if (hasJac)  {ierr = PetscArrayzero(elemMat,  numCells*totDim*totDim);CHKERRQ(ierr);}
51283e9753d6SMatthew G. Knepley   if (hasPrec) {ierr = PetscArrayzero(elemMatP, numCells*totDim*totDim);CHKERRQ(ierr);}
51293e9753d6SMatthew G. Knepley   if (hasDyn)  {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);}
51303e9753d6SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
51313e9753d6SMatthew G. Knepley     PetscClassId    id;
51323e9753d6SMatthew G. Knepley     PetscFE         fe;
51333e9753d6SMatthew G. Knepley     PetscQuadrature qGeom = NULL;
51343e9753d6SMatthew G. Knepley     PetscInt        Nb;
51353e9753d6SMatthew G. Knepley     /* Conforming batches */
51363e9753d6SMatthew G. Knepley     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
51373e9753d6SMatthew G. Knepley     /* Remainder */
51383e9753d6SMatthew G. Knepley     PetscInt        Nr, offset, Nq;
51393e9753d6SMatthew G. Knepley     PetscInt        maxDegree;
51403e9753d6SMatthew G. Knepley     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
51413e9753d6SMatthew G. Knepley 
51423e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
51433e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
51443e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
51453e9753d6SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
51463e9753d6SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
51473e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
51483e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
51493e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
51503e9753d6SMatthew G. Knepley     }
51513e9753d6SMatthew G. Knepley     if (!qGeom) {
51523e9753d6SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
51533e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
51543e9753d6SMatthew G. Knepley     }
51553e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
51563e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
51573e9753d6SMatthew G. Knepley     blockSize = Nb;
51583e9753d6SMatthew G. Knepley     batchSize = numBlocks * blockSize;
51593e9753d6SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
51603e9753d6SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
51613e9753d6SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
51623e9753d6SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
51633e9753d6SMatthew G. Knepley     offset    = numCells - Nr;
51643e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
51653e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
51663e9753d6SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
51673e9753d6SMatthew G. Knepley       if (hasJac) {
51683e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
51693e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, 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);
51703e9753d6SMatthew G. Knepley       }
51713e9753d6SMatthew G. Knepley       if (hasPrec) {
51723e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
51733e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, 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);
51743e9753d6SMatthew G. Knepley       }
51753e9753d6SMatthew G. Knepley       if (hasDyn) {
51763e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
51773e9753d6SMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, 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);
51783e9753d6SMatthew G. Knepley       }
51793e9753d6SMatthew G. Knepley     }
51803e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
51813e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
51823e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
51833e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
51843e9753d6SMatthew G. Knepley   }
51853e9753d6SMatthew G. Knepley   /*   Add contribution from X_t */
51863e9753d6SMatthew G. Knepley   if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
51873e9753d6SMatthew G. Knepley   if (hasFV) {
51883e9753d6SMatthew G. Knepley     PetscClassId id;
51893e9753d6SMatthew G. Knepley     PetscFV      fv;
51903e9753d6SMatthew G. Knepley     PetscInt     offsetI, NcI, NbI = 1, fc, f;
51913e9753d6SMatthew G. Knepley 
51923e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
51933e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
51943e9753d6SMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
51953e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
51963e9753d6SMatthew G. Knepley       if (id != PETSCFV_CLASSID) continue;
51973e9753d6SMatthew G. Knepley       /* Put in the identity */
51983e9753d6SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
51993e9753d6SMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
52003e9753d6SMatthew G. Knepley         const PetscInt cind    = c - cStart;
52013e9753d6SMatthew G. Knepley         const PetscInt eOffset = cind*totDim*totDim;
52023e9753d6SMatthew G. Knepley         for (fc = 0; fc < NcI; ++fc) {
52033e9753d6SMatthew G. Knepley           for (f = 0; f < NbI; ++f) {
52043e9753d6SMatthew G. Knepley             const PetscInt i = offsetI + f*NcI+fc;
52053e9753d6SMatthew G. Knepley             if (hasPrec) {
52063e9753d6SMatthew G. Knepley               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
52073e9753d6SMatthew G. Knepley               elemMatP[eOffset+i*totDim+i] = 1.0;
52083e9753d6SMatthew G. Knepley             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
52093e9753d6SMatthew G. Knepley           }
52103e9753d6SMatthew G. Knepley         }
52113e9753d6SMatthew G. Knepley       }
52123e9753d6SMatthew G. Knepley     }
52133e9753d6SMatthew G. Knepley     /* No allocated space for FV stuff, so ignore the zero entries */
52143e9753d6SMatthew G. Knepley     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
52153e9753d6SMatthew G. Knepley   }
52163e9753d6SMatthew G. Knepley   /* Insert values into matrix */
52173e9753d6SMatthew G. Knepley   isMatIS = PETSC_FALSE;
52183e9753d6SMatthew G. Knepley   if (hasPrec && hasJac) {
52193e9753d6SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);
52203e9753d6SMatthew G. Knepley   }
52213e9753d6SMatthew G. Knepley   if (isMatIS && !subSection) {
52223e9753d6SMatthew G. Knepley     ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
52233e9753d6SMatthew G. Knepley   }
52243e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
52253e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
52263e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
52273e9753d6SMatthew G. Knepley 
52283e9753d6SMatthew G. Knepley     /* Transform to global basis before insertion in Jacobian */
52293e9753d6SMatthew G. Knepley     if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
52303e9753d6SMatthew G. Knepley     if (hasPrec) {
52313e9753d6SMatthew G. Knepley       if (hasJac) {
52323e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
52333e9753d6SMatthew G. Knepley         if (!isMatIS) {
52343e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52353e9753d6SMatthew G. Knepley         } else {
52363e9753d6SMatthew G. Knepley           Mat lJ;
52373e9753d6SMatthew G. Knepley 
52383e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
52393e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52403e9753d6SMatthew G. Knepley         }
52413e9753d6SMatthew G. Knepley       }
52423e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
52433e9753d6SMatthew G. Knepley       if (!isMatISP) {
52443e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52453e9753d6SMatthew G. Knepley       } else {
52463e9753d6SMatthew G. Knepley         Mat lJ;
52473e9753d6SMatthew G. Knepley 
52483e9753d6SMatthew G. Knepley         ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
52493e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52503e9753d6SMatthew G. Knepley       }
52513e9753d6SMatthew G. Knepley     } else {
52523e9753d6SMatthew G. Knepley       if (hasJac) {
52533e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
52543e9753d6SMatthew G. Knepley         if (!isMatISP) {
52553e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52563e9753d6SMatthew G. Knepley         } else {
52573e9753d6SMatthew G. Knepley           Mat lJ;
52583e9753d6SMatthew G. Knepley 
52593e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
52603e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(dm, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
52613e9753d6SMatthew G. Knepley         }
52623e9753d6SMatthew G. Knepley       }
52633e9753d6SMatthew G. Knepley     }
52643e9753d6SMatthew G. Knepley   }
52653e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
52663e9753d6SMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
52673e9753d6SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
52683e9753d6SMatthew G. Knepley   if (dmAux) {
52693e9753d6SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
52703e9753d6SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
52713e9753d6SMatthew G. Knepley   }
52723e9753d6SMatthew G. Knepley   /* Compute boundary integrals */
52733e9753d6SMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
52743e9753d6SMatthew G. Knepley   /* Assemble matrix */
52753e9753d6SMatthew G. Knepley   if (hasJac && hasPrec) {
52763e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
52773e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
52783e9753d6SMatthew G. Knepley   }
52793e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
52803e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
52813e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
52823e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
52833e9753d6SMatthew G. Knepley }
52843e9753d6SMatthew G. Knepley 
52853e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user)
52863e9753d6SMatthew G. Knepley {
52873e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
52883e9753d6SMatthew G. Knepley   const char      *name       = "Hybrid Jacobian";
52893e9753d6SMatthew G. Knepley   DM               dmAux      = NULL;
52903e9753d6SMatthew G. Knepley   DM               plex       = NULL;
52913e9753d6SMatthew G. Knepley   DM               plexA      = NULL;
52923e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
52933e9753d6SMatthew G. Knepley   PetscDS          prob       = NULL;
52943e9753d6SMatthew G. Knepley   PetscDS          probAux    = NULL;
52953e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
52963e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
52973e9753d6SMatthew G. Knepley   Vec              locA;
52983e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a = NULL;
52993e9753d6SMatthew G. Knepley   PetscScalar     *elemMat, *elemMatP;
53003e9753d6SMatthew G. Knepley   PetscSection     globalSection, subSection, sectionAux;
53013e9753d6SMatthew G. Knepley   IS               chunkIS;
53023e9753d6SMatthew G. Knepley   const PetscInt  *cells;
53033e9753d6SMatthew G. Knepley   PetscInt        *faces;
53043e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
53053e9753d6SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, totDim, totDimAux, numChunks, cellChunkSize, chunk;
53063e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
53073e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
53083e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
53093e9753d6SMatthew G. Knepley   PetscBool        isMatIS = PETSC_FALSE, isMatISP = PETSC_FALSE, hasBdJac, hasBdPrec;
53103e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
53113e9753d6SMatthew G. Knepley 
53123e9753d6SMatthew G. Knepley   PetscFunctionBegin;
53133e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
53143e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
53153e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
53163e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
53173e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
53183e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
53193e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
53203e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr);
53213e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
53223e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
53233e9753d6SMatthew G. Knepley   ierr = PetscDSHasBdJacobian(prob, &hasBdJac);CHKERRQ(ierr);
53243e9753d6SMatthew G. Knepley   ierr = PetscDSHasBdJacobianPreconditioner(prob, &hasBdPrec);CHKERRQ(ierr);
53253e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
53263e9753d6SMatthew G. Knepley   if (isMatISP) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);}
53273e9753d6SMatthew G. Knepley   if (hasBdPrec && hasBdJac) {ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatIS);CHKERRQ(ierr);}
53283e9753d6SMatthew G. Knepley   if (isMatIS && !subSection) {ierr = DMPlexGetSubdomainSection(plex, &subSection);CHKERRQ(ierr);}
53293e9753d6SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
53303e9753d6SMatthew G. Knepley   if (locA) {
53313e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
53323e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
53333e9753d6SMatthew G. Knepley     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
53343e9753d6SMatthew G. Knepley     ierr = DMGetCellDS(dmAux, cStart, &probAux);CHKERRQ(ierr);
53353e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
53363e9753d6SMatthew G. Knepley   }
53373e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
53383e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
53393e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
53403e9753d6SMatthew G. Knepley     PetscInt f;
53413e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
53423e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
53433e9753d6SMatthew G. Knepley       PetscFE fe;
53443e9753d6SMatthew G. Knepley 
53453e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fe);CHKERRQ(ierr);
53463e9753d6SMatthew G. Knepley       if (fe) {
53473e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
53483e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
53493e9753d6SMatthew G. Knepley       }
53503e9753d6SMatthew G. Knepley     }
53513e9753d6SMatthew G. Knepley   }
53523e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
53533e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
53543e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
53553e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
53563e9753d6SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
53573e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
53583e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
53593e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
53603e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
53613e9753d6SMatthew G. Knepley 
53623e9753d6SMatthew G. Knepley     if (hasBdJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
53633e9753d6SMatthew G. Knepley     if (hasBdPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
53643e9753d6SMatthew G. Knepley     /* Get faces */
53653e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
53663e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
53673e9753d6SMatthew G. Knepley       const PetscInt *cone;
53683e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(plex, cell, &cone);CHKERRQ(ierr);
53693e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
53703e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
53713e9753d6SMatthew G. Knepley     }
53723e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
53733e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
53743e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
53753e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
53763e9753d6SMatthew G. Knepley     } else {
53773e9753d6SMatthew G. Knepley       PetscInt f;
53783e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
53793e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
53803e9753d6SMatthew G. Knepley       }
53813e9753d6SMatthew G. Knepley     }
53823e9753d6SMatthew G. Knepley 
53833e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
53843e9753d6SMatthew G. Knepley       PetscFE         feI;
53853e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[fieldI];
53863e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
53873e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI];
53883e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
53893e9753d6SMatthew G. Knepley 
53903e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &feI);CHKERRQ(ierr);
53913e9753d6SMatthew G. Knepley       if (!feI) continue;
53923e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(feI, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
53933e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
53943e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(feI, &Nb);CHKERRQ(ierr);
53953e9753d6SMatthew G. Knepley       blockSize = Nb;
53963e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
53973e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(feI, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
53983e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
53993e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
54003e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
54013e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
54023e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
54033e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
54043e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
54053e9753d6SMatthew G. Knepley         PetscFE feJ;
54063e9753d6SMatthew G. Knepley 
54073e9753d6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldJ, (PetscObject *) &feJ);CHKERRQ(ierr);
54083e9753d6SMatthew G. Knepley         if (!feJ) continue;
54093e9753d6SMatthew G. Knepley         if (hasBdJac) {
54103e9753d6SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
54113e9753d6SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN, fieldI, fieldJ, 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);
54123e9753d6SMatthew G. Knepley         }
54133e9753d6SMatthew G. Knepley         if (hasBdPrec) {
54143e9753d6SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
54153e9753d6SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, 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);
54163e9753d6SMatthew G. Knepley         }
54173e9753d6SMatthew G. Knepley       }
54183e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
54193e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
54203e9753d6SMatthew G. Knepley     }
54213e9753d6SMatthew G. Knepley     /* Insert values into matrix */
54223e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
54233e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
54243e9753d6SMatthew G. Knepley       const PetscInt cind = c - cS;
54253e9753d6SMatthew G. Knepley 
54263e9753d6SMatthew G. Knepley       if (hasBdPrec) {
54273e9753d6SMatthew G. Knepley         if (hasBdJac) {
54283e9753d6SMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
54293e9753d6SMatthew G. Knepley           if (!isMatIS) {
54303e9753d6SMatthew G. Knepley             ierr = DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54313e9753d6SMatthew G. Knepley           } else {
54323e9753d6SMatthew G. Knepley             Mat lJ;
54333e9753d6SMatthew G. Knepley 
54343e9753d6SMatthew G. Knepley             ierr = MatISGetLocalMat(Jac,&lJ);CHKERRQ(ierr);
54353e9753d6SMatthew G. Knepley             ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54363e9753d6SMatthew G. Knepley           }
54373e9753d6SMatthew G. Knepley         }
54383e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
54393e9753d6SMatthew G. Knepley         if (!isMatISP) {
54403e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54413e9753d6SMatthew G. Knepley         } else {
54423e9753d6SMatthew G. Knepley           Mat lJ;
54433e9753d6SMatthew G. Knepley 
54443e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
54453e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54463e9753d6SMatthew G. Knepley         }
54473e9753d6SMatthew G. Knepley       } else if (hasBdJac) {
54483e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
54493e9753d6SMatthew G. Knepley         if (!isMatISP) {
54503e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54513e9753d6SMatthew G. Knepley         } else {
54523e9753d6SMatthew G. Knepley           Mat lJ;
54533e9753d6SMatthew G. Knepley 
54543e9753d6SMatthew G. Knepley           ierr = MatISGetLocalMat(JacP,&lJ);CHKERRQ(ierr);
54553e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, subSection, lJ, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
54563e9753d6SMatthew G. Knepley         }
54573e9753d6SMatthew G. Knepley       }
54583e9753d6SMatthew G. Knepley     }
54593e9753d6SMatthew G. Knepley   }
54603e9753d6SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
54613e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
54623e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
54633e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
54643e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
54653e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
54663e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
54673e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
54683e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
54693e9753d6SMatthew G. Knepley   } else {
54703e9753d6SMatthew G. Knepley     PetscInt f;
54713e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
54723e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);CHKERRQ(ierr);}
54733e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
54743e9753d6SMatthew G. Knepley     }
54753e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
54763e9753d6SMatthew G. Knepley   }
54773e9753d6SMatthew G. Knepley   if (dmAux) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
54783e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
54793e9753d6SMatthew G. Knepley   /* Assemble matrix */
54803e9753d6SMatthew G. Knepley   if (hasBdJac && hasBdPrec) {
54813e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54823e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54833e9753d6SMatthew G. Knepley   }
54843e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54853e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
54863e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
54873e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
54883e9753d6SMatthew G. Knepley }
5489