xref: /petsc/src/dm/impls/plex/plexfem.c (revision b77f2eeeaa7e2d488a0f1f29507282de57d1b69a)
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 
139d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, 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   }
214b247467aSMatthew G. Knepley   for (i = 0; i < PetscMin(dim, mmin); ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
2153b2202bfSJacob Faibussowitsch   /* Orthonormalize system	*/
216b50a2c0aSJacob Faibussowitsch   for (i = 0; i < mmin; ++i) {
217*b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
218b50a2c0aSJacob Faibussowitsch 
219b50a2c0aSJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
220*b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], mmin-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
221b50a2c0aSJacob Faibussowitsch     for (j = i+1; j < mmin; ++j) {
222*b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
223*b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
224b50a2c0aSJacob Faibussowitsch     }
225cb1e1211SMatthew G Knepley    }
226b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
227b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
228cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
229cb1e1211SMatthew G Knepley }
230cb1e1211SMatthew G Knepley 
231cc4e42d9SMartin Diehl /*@
23212adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
23312adca46SMatthew G. Knepley 
234d083f849SBarry Smith   Collective on dm
23512adca46SMatthew G. Knepley 
23612adca46SMatthew G. Knepley   Input Arguments:
23712adca46SMatthew G. Knepley + dm    - the DM
23812adca46SMatthew G. Knepley . nb    - The number of bodies
23912adca46SMatthew G. Knepley . label - The DMLabel marking each domain
24012adca46SMatthew G. Knepley . nids  - The number of ids per body
24112adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
24212adca46SMatthew G. Knepley 
24312adca46SMatthew G. Knepley   Output Argument:
24412adca46SMatthew G. Knepley . sp - the null space
24512adca46SMatthew G. Knepley 
24612adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
24712adca46SMatthew G. Knepley 
24812adca46SMatthew G. Knepley   Level: advanced
24912adca46SMatthew G. Knepley 
25012adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
25112adca46SMatthew G. Knepley @*/
25212adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
25312adca46SMatthew G. Knepley {
25412adca46SMatthew G. Knepley   MPI_Comm       comm;
25512adca46SMatthew G. Knepley   PetscSection   section, globalSection;
25612adca46SMatthew G. Knepley   Vec           *mode;
25712adca46SMatthew G. Knepley   PetscScalar   *dots;
25812adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
25912adca46SMatthew G. Knepley   PetscErrorCode ierr;
26012adca46SMatthew G. Knepley 
26112adca46SMatthew G. Knepley   PetscFunctionBegin;
26212adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
26312adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26412adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
26592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
266e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
26712adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
26812adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
26912adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
27012adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
27112adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
27212adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
27312adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
27412adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
27512adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
27612adca46SMatthew G. Knepley       PetscInt         ctx[2];
27712adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
27812adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
27912adca46SMatthew G. Knepley 
28012adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
28112adca46SMatthew G. Knepley       ctx[1] = d;
28212adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
28312adca46SMatthew G. Knepley       off   += nids[b];
28412adca46SMatthew G. Knepley     }
28512adca46SMatthew G. Knepley   }
28612adca46SMatthew G. Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
2873b2202bfSJacob Faibussowitsch   /* Orthonormalize system	*/
2885a0e29b9SJacob Faibussowitsch   for (i = 0; i < m; ++i) {
289*b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
2905a0e29b9SJacob Faibussowitsch 
2915a0e29b9SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
292*b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], m-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
2935a0e29b9SJacob Faibussowitsch     for (j = i+1; j < m; ++j) {
294*b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
295*b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
2965a0e29b9SJacob Faibussowitsch     }
29712adca46SMatthew G. Knepley   }
29812adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
29912adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
30012adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
30112adca46SMatthew G. Knepley   PetscFunctionReturn(0);
30212adca46SMatthew G. Knepley }
30312adca46SMatthew G. Knepley 
304b29cfa1cSToby Isaac /*@
305b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
306b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
307b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
308b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
309b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
310b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
311b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
312b29cfa1cSToby Isaac 
313b29cfa1cSToby Isaac   Input Parameters:
314b29cfa1cSToby Isaac + dm - the DMPlex object
315b29cfa1cSToby Isaac - height - the maximum projection height >= 0
316b29cfa1cSToby Isaac 
317b29cfa1cSToby Isaac   Level: advanced
318b29cfa1cSToby Isaac 
3194d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
320b29cfa1cSToby Isaac @*/
321b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
322b29cfa1cSToby Isaac {
323b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
324b29cfa1cSToby Isaac 
325b29cfa1cSToby Isaac   PetscFunctionBegin;
326b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
327b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
328b29cfa1cSToby Isaac   PetscFunctionReturn(0);
329b29cfa1cSToby Isaac }
330b29cfa1cSToby Isaac 
331b29cfa1cSToby Isaac /*@
332b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
333b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
334b29cfa1cSToby Isaac 
335b29cfa1cSToby Isaac   Input Parameters:
336b29cfa1cSToby Isaac . dm - the DMPlex object
337b29cfa1cSToby Isaac 
338b29cfa1cSToby Isaac   Output Parameters:
339b29cfa1cSToby Isaac . height - the maximum projection height
340b29cfa1cSToby Isaac 
341b29cfa1cSToby Isaac   Level: intermediate
342b29cfa1cSToby Isaac 
3434d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
344b29cfa1cSToby Isaac @*/
345b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
346b29cfa1cSToby Isaac {
347b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
348b29cfa1cSToby Isaac 
349b29cfa1cSToby Isaac   PetscFunctionBegin;
350b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
352b29cfa1cSToby Isaac   PetscFunctionReturn(0);
353b29cfa1cSToby Isaac }
354b29cfa1cSToby Isaac 
355ca3d3a14SMatthew G. Knepley typedef struct {
356ca3d3a14SMatthew G. Knepley   PetscReal    alpha; /* The first Euler angle, and in 2D the only one */
357ca3d3a14SMatthew G. Knepley   PetscReal    beta;  /* The second Euler angle */
358ca3d3a14SMatthew G. Knepley   PetscReal    gamma; /* The third Euler angle */
359ca3d3a14SMatthew G. Knepley   PetscInt     dim;   /* The dimension of R */
360ca3d3a14SMatthew G. Knepley   PetscScalar *R;     /* The rotation matrix, transforming a vector in the local basis to the global basis */
361ca3d3a14SMatthew G. Knepley   PetscScalar *RT;    /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */
362ca3d3a14SMatthew G. Knepley } RotCtx;
363ca3d3a14SMatthew G. Knepley 
364ca3d3a14SMatthew G. Knepley /*
365ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
366ca3d3a14SMatthew 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:
367ca3d3a14SMatthew 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.
368ca3d3a14SMatthew 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.
369ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
370ca3d3a14SMatthew G. Knepley */
371ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx)
372ca3d3a14SMatthew G. Knepley {
373ca3d3a14SMatthew G. Knepley   RotCtx        *rc  = (RotCtx *) ctx;
374ca3d3a14SMatthew G. Knepley   PetscInt       dim = rc->dim;
375ca3d3a14SMatthew G. Knepley   PetscReal      c1, s1, c2, s2, c3, s3;
376ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
377ca3d3a14SMatthew G. Knepley 
378ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
379ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr);
380ca3d3a14SMatthew G. Knepley   switch (dim) {
381ca3d3a14SMatthew G. Knepley   case 2:
382ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
383ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1;rc->R[1] = s1;
384ca3d3a14SMatthew G. Knepley     rc->R[2] = -s1;rc->R[3] = c1;
385580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
386ca3d3a14SMatthew G. Knepley     DMPlex_Transpose2D_Internal(rc->RT);break;
387ca3d3a14SMatthew G. Knepley     break;
388ca3d3a14SMatthew G. Knepley   case 3:
389ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
390ca3d3a14SMatthew G. Knepley     c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta);
391ca3d3a14SMatthew G. Knepley     c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma);
392ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1*c3 - c2*s1*s3;rc->R[1] =  c3*s1    + c1*c2*s3;rc->R[2] = s2*s3;
393ca3d3a14SMatthew G. Knepley     rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] =  c1*c2*c3 - s1*s3;   rc->R[5] = c3*s2;
394ca3d3a14SMatthew G. Knepley     rc->R[6] =  s1*s2;           rc->R[7] = -c1*s2;              rc->R[8] = c2;
395580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
396ca3d3a14SMatthew G. Knepley     DMPlex_Transpose3D_Internal(rc->RT);break;
397ca3d3a14SMatthew G. Knepley     break;
398ca3d3a14SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
399ca3d3a14SMatthew G. Knepley   }
400ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
401ca3d3a14SMatthew G. Knepley }
402ca3d3a14SMatthew G. Knepley 
403ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx)
404ca3d3a14SMatthew G. Knepley {
405ca3d3a14SMatthew G. Knepley   RotCtx        *rc = (RotCtx *) ctx;
406ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
407ca3d3a14SMatthew G. Knepley 
408ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
409ca3d3a14SMatthew G. Knepley   ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr);
410ca3d3a14SMatthew G. Knepley   ierr = PetscFree(rc);CHKERRQ(ierr);
411ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
412ca3d3a14SMatthew G. Knepley }
413ca3d3a14SMatthew G. Knepley 
414ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx)
415ca3d3a14SMatthew G. Knepley {
416ca3d3a14SMatthew G. Knepley   RotCtx *rc = (RotCtx *) ctx;
417ca3d3a14SMatthew G. Knepley 
418ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
419ca3d3a14SMatthew G. Knepley   PetscValidPointer(ctx, 5);
420ca3d3a14SMatthew G. Knepley   if (l2g) {*A = rc->R;}
421ca3d3a14SMatthew G. Knepley   else     {*A = rc->RT;}
422ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
423ca3d3a14SMatthew G. Knepley }
424ca3d3a14SMatthew G. Knepley 
425ab6a9622SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApplyReal_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscReal *y, PetscReal *z, void *ctx)
426ab6a9622SMatthew G. Knepley {
427ec277c0fSMatthew G. Knepley   PetscErrorCode ierr;
428ec277c0fSMatthew G. Knepley 
429ec277c0fSMatthew G. Knepley   PetscFunctionBegin;
430ab6a9622SMatthew G. Knepley   #if defined(PETSC_USE_COMPLEX)
431ab6a9622SMatthew G. Knepley   switch (dim) {
432ab6a9622SMatthew G. Knepley     case 2:
433ab6a9622SMatthew G. Knepley     {
434ab6a9622SMatthew G. Knepley       PetscScalar yt[2], zt[2];
435ab6a9622SMatthew G. Knepley 
436ab6a9622SMatthew G. Knepley       yt[0] = y[0]; yt[1] = y[1];
437ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
438ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]);
439ab6a9622SMatthew G. Knepley     }
4409b4815dcSMatthew G. Knepley     break;
441ab6a9622SMatthew G. Knepley     case 3:
442ab6a9622SMatthew G. Knepley     {
443ab6a9622SMatthew G. Knepley       PetscScalar yt[3], zt[3];
444ab6a9622SMatthew G. Knepley 
445ab6a9622SMatthew G. Knepley       yt[0] = y[0]; yt[1] = y[1]; yt[2] = y[2];
446ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
447ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); z[2] = PetscRealPart(zt[2]);
448ab6a9622SMatthew G. Knepley     }
4499b4815dcSMatthew G. Knepley     break;
450ab6a9622SMatthew G. Knepley   }
451ab6a9622SMatthew G. Knepley   #else
452ab6a9622SMatthew G. Knepley   ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, y, z, ctx);CHKERRQ(ierr);
453ab6a9622SMatthew G. Knepley   #endif
454ec277c0fSMatthew G. Knepley   PetscFunctionReturn(0);
455ab6a9622SMatthew G. Knepley }
456ab6a9622SMatthew G. Knepley 
457ec277c0fSMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx)
458ca3d3a14SMatthew G. Knepley {
459ca3d3a14SMatthew G. Knepley   const PetscScalar *A;
460ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
461ca3d3a14SMatthew G. Knepley 
462ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
463ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr);
464ca3d3a14SMatthew G. Knepley   switch (dim) {
4651fba962aSMatthew G. Knepley   case 2: DMPlex_Mult2D_Internal(A, 1, y, z);break;
4661fba962aSMatthew G. Knepley   case 3: DMPlex_Mult3D_Internal(A, 1, y, z);break;
467ca3d3a14SMatthew G. Knepley   }
468ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
469ca3d3a14SMatthew G. Knepley }
470ca3d3a14SMatthew G. Knepley 
471ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a)
472ca3d3a14SMatthew G. Knepley {
473ca3d3a14SMatthew G. Knepley   PetscSection       ts;
474ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tva;
475ca3d3a14SMatthew G. Knepley   PetscInt           dof;
476ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
477ca3d3a14SMatthew G. Knepley 
478ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
47992fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
480ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
481ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
482ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, (void *) &tva);CHKERRQ(ierr);
483ca3d3a14SMatthew G. Knepley   if (l2g) {
484ca3d3a14SMatthew G. Knepley     switch (dof) {
4851fba962aSMatthew G. Knepley     case 4: DMPlex_Mult2D_Internal(tva, 1, a, a);break;
4861fba962aSMatthew G. Knepley     case 9: DMPlex_Mult3D_Internal(tva, 1, a, a);break;
487ca3d3a14SMatthew G. Knepley     }
488ca3d3a14SMatthew G. Knepley   } else {
489ca3d3a14SMatthew G. Knepley     switch (dof) {
4901fba962aSMatthew G. Knepley     case 4: DMPlex_MultTranspose2D_Internal(tva, 1, a, a);break;
4911fba962aSMatthew G. Knepley     case 9: DMPlex_MultTranspose3D_Internal(tva, 1, a, a);break;
492ca3d3a14SMatthew G. Knepley     }
493ca3d3a14SMatthew G. Knepley   }
494ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
495ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
496ca3d3a14SMatthew G. Knepley }
497ca3d3a14SMatthew G. Knepley 
498ca3d3a14SMatthew 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)
499ca3d3a14SMatthew G. Knepley {
500ca3d3a14SMatthew G. Knepley   PetscSection       s, ts;
501ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tvaf, *tvag;
502ca3d3a14SMatthew G. Knepley   PetscInt           fdof, gdof, fpdof, gpdof;
503ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
504ca3d3a14SMatthew G. Knepley 
505ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
50692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
50792fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
508ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr);
509ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr);
510ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr);
511ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr);
512ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
513ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, (void *) &tvaf);CHKERRQ(ierr);
514ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, (void *) &tvag);CHKERRQ(ierr);
515ca3d3a14SMatthew G. Knepley   if (l2g) {
516ca3d3a14SMatthew G. Knepley     switch (fdof) {
517ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break;
518ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break;
519ca3d3a14SMatthew G. Knepley     }
520ca3d3a14SMatthew G. Knepley     switch (gdof) {
521ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break;
522ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break;
523ca3d3a14SMatthew G. Knepley     }
524ca3d3a14SMatthew G. Knepley   } else {
525ca3d3a14SMatthew G. Knepley     switch (fdof) {
526ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break;
527ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break;
528ca3d3a14SMatthew G. Knepley     }
529ca3d3a14SMatthew G. Knepley     switch (gdof) {
530ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break;
531ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break;
532ca3d3a14SMatthew G. Knepley     }
533ca3d3a14SMatthew G. Knepley   }
534ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
535ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
536ca3d3a14SMatthew G. Knepley }
537ca3d3a14SMatthew G. Knepley 
538ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a)
539ca3d3a14SMatthew G. Knepley {
540ca3d3a14SMatthew G. Knepley   PetscSection    s;
541ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
542ca3d3a14SMatthew G. Knepley   IS              clPoints;
543ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
544ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
545ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, Np, cp, dof, d = 0;
546ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
547ca3d3a14SMatthew G. Knepley 
548ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
54992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
550ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
551ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
552ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
553ca3d3a14SMatthew G. Knepley     for (cp = 0; cp < Np*2; cp += 2) {
554ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr);
555ca3d3a14SMatthew G. Knepley       if (!dof) continue;
556ca3d3a14SMatthew G. Knepley       if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);}
557ca3d3a14SMatthew G. Knepley       d += dof;
558ca3d3a14SMatthew G. Knepley     }
559ca3d3a14SMatthew G. Knepley   }
560ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
561ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
562ca3d3a14SMatthew G. Knepley }
563ca3d3a14SMatthew G. Knepley 
564ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a)
565ca3d3a14SMatthew G. Knepley {
566ca3d3a14SMatthew G. Knepley   PetscSection    s;
567ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
568ca3d3a14SMatthew G. Knepley   IS              clPoints;
569ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
570ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
5718bdb3c71SMatthew G. Knepley   PetscInt        Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c = 0;
572ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
573ca3d3a14SMatthew G. Knepley 
574ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
57592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
576ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
577ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
578ca3d3a14SMatthew G. Knepley   for (f = 0, r = 0; f < Nf; ++f) {
579ca3d3a14SMatthew G. Knepley     for (cpf = 0; cpf < Np*2; cpf += 2) {
580ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr);
581ca3d3a14SMatthew G. Knepley       for (g = 0, c = 0; g < Nf; ++g) {
582ca3d3a14SMatthew G. Knepley         for (cpg = 0; cpg < Np*2; cpg += 2) {
583ca3d3a14SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr);
584ca3d3a14SMatthew G. Knepley           ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr);
585ca3d3a14SMatthew G. Knepley           c += gdof;
586ca3d3a14SMatthew G. Knepley         }
587ca3d3a14SMatthew G. Knepley       }
588ca3d3a14SMatthew G. Knepley       if (c != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda);
589ca3d3a14SMatthew G. Knepley       r += fdof;
590ca3d3a14SMatthew G. Knepley     }
591ca3d3a14SMatthew G. Knepley   }
592ca3d3a14SMatthew G. Knepley   if (r != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda);
593ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
594ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
595ca3d3a14SMatthew G. Knepley }
596ca3d3a14SMatthew G. Knepley 
597ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g)
598ca3d3a14SMatthew G. Knepley {
599ca3d3a14SMatthew G. Knepley   DM                 tdm;
600ca3d3a14SMatthew G. Knepley   Vec                tv;
601ca3d3a14SMatthew G. Knepley   PetscSection       ts, s;
602ca3d3a14SMatthew G. Knepley   const PetscScalar *ta;
603ca3d3a14SMatthew G. Knepley   PetscScalar       *a, *va;
604ca3d3a14SMatthew G. Knepley   PetscInt           pStart, pEnd, p, Nf, f;
605ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
606ca3d3a14SMatthew G. Knepley 
607ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
608ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
609ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
61092fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
61192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
612ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
613ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
614ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(lv, &a);CHKERRQ(ierr);
615ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
616ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
617ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
618ca3d3a14SMatthew G. Knepley       ierr = DMPlexPointLocalFieldRef(dm, p, f, a, (void *) &va);CHKERRQ(ierr);
619ca3d3a14SMatthew G. Knepley       ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr);
620ca3d3a14SMatthew G. Knepley     }
621ca3d3a14SMatthew G. Knepley   }
622ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr);
623ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
624ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
625ca3d3a14SMatthew G. Knepley }
626ca3d3a14SMatthew G. Knepley 
627ca3d3a14SMatthew G. Knepley /*@
628ca3d3a14SMatthew G. Knepley   DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis
629ca3d3a14SMatthew G. Knepley 
630ca3d3a14SMatthew G. Knepley   Input Parameters:
631ca3d3a14SMatthew G. Knepley + dm - The DM
632ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis
633ca3d3a14SMatthew G. Knepley 
634ca3d3a14SMatthew G. Knepley   Output Parameters:
635ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis
636ca3d3a14SMatthew G. Knepley 
637c0f8e1fdSMatthew 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.
638c0f8e1fdSMatthew G. Knepley 
639ca3d3a14SMatthew G. Knepley   Level: developer
640ca3d3a14SMatthew G. Knepley 
64192fd8e1eSJed Brown .seealso: DMPlexLocalToGlobalBasis(), DMGetLocalSection()
642ca3d3a14SMatthew G. Knepley @*/
643ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv)
644ca3d3a14SMatthew G. Knepley {
645ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
646ca3d3a14SMatthew G. Knepley 
647ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
648ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
649ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
650ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr);
651ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
652ca3d3a14SMatthew G. Knepley }
653ca3d3a14SMatthew G. Knepley 
654ca3d3a14SMatthew G. Knepley /*@
655ca3d3a14SMatthew G. Knepley   DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis
656ca3d3a14SMatthew G. Knepley 
657ca3d3a14SMatthew G. Knepley   Input Parameters:
658ca3d3a14SMatthew G. Knepley + dm - The DM
659ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis
660ca3d3a14SMatthew G. Knepley 
661ca3d3a14SMatthew G. Knepley   Output Parameters:
662ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis
663ca3d3a14SMatthew G. Knepley 
664c0f8e1fdSMatthew 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.
665c0f8e1fdSMatthew G. Knepley 
666ca3d3a14SMatthew G. Knepley   Level: developer
667ca3d3a14SMatthew G. Knepley 
66892fd8e1eSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMGetLocalSection()
669ca3d3a14SMatthew G. Knepley @*/
670ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv)
671ca3d3a14SMatthew G. Knepley {
672ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
673ca3d3a14SMatthew G. Knepley 
674ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
675ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
676ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
677ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr);
678ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
679ca3d3a14SMatthew G. Knepley }
680ca3d3a14SMatthew G. Knepley 
681ca3d3a14SMatthew G. Knepley /*@
682ca3d3a14SMatthew G. Knepley   DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions
683ca3d3a14SMatthew G. Knepley     and global solutions, to a local basis, appropriate for discretization integrals and assembly.
684ca3d3a14SMatthew G. Knepley 
685ca3d3a14SMatthew G. Knepley   Input Parameters:
686ca3d3a14SMatthew G. Knepley + dm    - The DM
687ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one
688ca3d3a14SMatthew G. Knepley . beta  - The second Euler angle
689ca3d3a14SMatthew G. Knepley . gamma - The third Euler angle
690ca3d3a14SMatthew G. Knepley 
691ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
692ca3d3a14SMatthew 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:
693ca3d3a14SMatthew 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.
694ca3d3a14SMatthew 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.
695ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
696ca3d3a14SMatthew G. Knepley 
697ca3d3a14SMatthew G. Knepley   Level: developer
698ca3d3a14SMatthew G. Knepley 
699ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()
700ca3d3a14SMatthew G. Knepley @*/
701ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma)
702ca3d3a14SMatthew G. Knepley {
703ca3d3a14SMatthew G. Knepley   RotCtx        *rc;
704ca3d3a14SMatthew G. Knepley   PetscInt       cdim;
705ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
706ca3d3a14SMatthew G. Knepley 
707ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
708ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr);
709ca3d3a14SMatthew G. Knepley   dm->transformCtx       = rc;
710ca3d3a14SMatthew G. Knepley   dm->transformSetUp     = DMPlexBasisTransformSetUp_Rotation_Internal;
711ca3d3a14SMatthew G. Knepley   dm->transformDestroy   = DMPlexBasisTransformDestroy_Rotation_Internal;
712ca3d3a14SMatthew G. Knepley   dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal;
713ca3d3a14SMatthew G. Knepley   rc->dim   = cdim;
714ca3d3a14SMatthew G. Knepley   rc->alpha = alpha;
715ca3d3a14SMatthew G. Knepley   rc->beta  = beta;
716ca3d3a14SMatthew G. Knepley   rc->gamma = gamma;
717ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr);
718ca3d3a14SMatthew G. Knepley   ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr);
719ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
720ca3d3a14SMatthew G. Knepley }
721ca3d3a14SMatthew G. Knepley 
722b278463cSMatthew G. Knepley /*@C
723b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector
724b278463cSMatthew G. Knepley 
725b278463cSMatthew G. Knepley   Input Parameters:
726b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
727b278463cSMatthew G. Knepley . time   - The time
728b278463cSMatthew G. Knepley . field  - The field to constrain
7291c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7301c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
731b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
732b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
733b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
734b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
735b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
736b278463cSMatthew G. Knepley 
737b278463cSMatthew G. Knepley   Output Parameter:
738b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
739b278463cSMatthew G. Knepley 
740b278463cSMatthew G. Knepley   Level: developer
741b278463cSMatthew G. Knepley 
742b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
743b278463cSMatthew G. Knepley @*/
7441c531cf8SMatthew 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)
74555f2e967SMatthew G. Knepley {
7460163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
74755f2e967SMatthew G. Knepley   void            **ctxs;
748d7ddef95SMatthew G. Knepley   PetscInt          numFields;
749d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
750d7ddef95SMatthew G. Knepley 
751d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
752d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
753d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
754d7ddef95SMatthew G. Knepley   funcs[field] = func;
755d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
7561c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
757d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
758d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
759d7ddef95SMatthew G. Knepley }
760d7ddef95SMatthew G. Knepley 
761b278463cSMatthew G. Knepley /*@C
762b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector
763b278463cSMatthew G. Knepley 
764b278463cSMatthew G. Knepley   Input Parameters:
765b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
766b278463cSMatthew G. Knepley . time   - The time
767b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
768b278463cSMatthew G. Knepley . field  - The field to constrain
7691c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7701c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
771b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
772b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
773b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
774b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
775b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
776b278463cSMatthew G. Knepley 
777b278463cSMatthew G. Knepley   Output Parameter:
778b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
779b278463cSMatthew G. Knepley 
780b278463cSMatthew G. Knepley   Level: developer
781b278463cSMatthew G. Knepley 
782b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary()
783b278463cSMatthew G. Knepley @*/
7841c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
785c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
786c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
787c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
78897b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
789b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
790b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
791c60e475cSMatthew G. Knepley {
792c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
793c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
794c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79597b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
796c60e475cSMatthew G. Knepley   void            **ctxs;
797c60e475cSMatthew G. Knepley   PetscInt          numFields;
798c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
799c60e475cSMatthew G. Knepley 
800c60e475cSMatthew G. Knepley   PetscFunctionBegin;
801c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
802c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
803c60e475cSMatthew G. Knepley   funcs[field] = func;
804c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
8051c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
806c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
807c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
808c60e475cSMatthew G. Knepley }
809c60e475cSMatthew G. Knepley 
810b278463cSMatthew G. Knepley /*@C
811b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
812b278463cSMatthew G. Knepley 
813b278463cSMatthew G. Knepley   Input Parameters:
814b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
815b278463cSMatthew G. Knepley . time   - The time
816b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
817b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
818b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
819b278463cSMatthew G. Knepley . field  - The field to constrain
8201c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
8211c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
822b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
823b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
824b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
825b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
826b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
827b278463cSMatthew G. Knepley 
828b278463cSMatthew G. Knepley   Output Parameter:
829b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
830b278463cSMatthew G. Knepley 
831b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
832b278463cSMatthew G. Knepley 
833b278463cSMatthew G. Knepley   Level: developer
834b278463cSMatthew G. Knepley 
835b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
836b278463cSMatthew G. Knepley @*/
8371c531cf8SMatthew 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[],
838b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
839d7ddef95SMatthew G. Knepley {
84061f58d28SMatthew G. Knepley   PetscDS            prob;
841da97024aSMatthew G. Knepley   PetscSF            sf;
842d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
84320369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
844da97024aSMatthew G. Knepley   const PetscInt    *leaves;
845d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
846520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
847e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
848d7ddef95SMatthew G. Knepley 
849d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
850da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
851da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
852da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
853d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
854d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
85561f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
856d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
857d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
85820369375SToby Isaac   if (cellGeometry) {
859d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
860d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
86120369375SToby Isaac   }
862d7ddef95SMatthew G. Knepley   if (Grad) {
863c0a6632aSMatthew G. Knepley     PetscFV fv;
864c0a6632aSMatthew G. Knepley 
865c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
866d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
867d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
868d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
86969291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
870d7ddef95SMatthew G. Knepley   }
871d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
872d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
873d7ddef95SMatthew G. Knepley     IS              faceIS;
874d7ddef95SMatthew G. Knepley     const PetscInt *faces;
875d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
876d7ddef95SMatthew G. Knepley 
877d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
878d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
879d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
880d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
881d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
882d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
883640bce14SSatish Balay       PetscFVFaceGeom        *fg;
884d7ddef95SMatthew G. Knepley 
885d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
886da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
887da97024aSMatthew G. Knepley       if (loc >= 0) continue;
888d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
889d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
890d7ddef95SMatthew G. Knepley       if (Grad) {
891640bce14SSatish Balay         PetscFVCellGeom       *cg;
892640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
893d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
894d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
895d7ddef95SMatthew G. Knepley         PetscInt               d;
896d7ddef95SMatthew G. Knepley 
897d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
898d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
899d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
900520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
901d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
902d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
903e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
904e735a8a9SMatthew G. Knepley         if (ierru) {
905e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
906e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
907e735a8a9SMatthew G. Knepley           goto cleanup;
908e735a8a9SMatthew G. Knepley         }
909d7ddef95SMatthew G. Knepley       } else {
910640bce14SSatish Balay         PetscScalar       *xI;
911d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
912d7ddef95SMatthew G. Knepley 
913d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
914520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
915e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
916e735a8a9SMatthew G. Knepley         if (ierru) {
917e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
918e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
919e735a8a9SMatthew G. Knepley           goto cleanup;
920e735a8a9SMatthew G. Knepley         }
921d7ddef95SMatthew G. Knepley       }
922d7ddef95SMatthew G. Knepley     }
923d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
924d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
925d7ddef95SMatthew G. Knepley   }
926e735a8a9SMatthew G. Knepley   cleanup:
927d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
928d7ddef95SMatthew G. Knepley   if (Grad) {
92969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
930d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
931d7ddef95SMatthew G. Knepley   }
932e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
933d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
934e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
935d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
936d7ddef95SMatthew G. Knepley }
937d7ddef95SMatthew G. Knepley 
938f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
939d7ddef95SMatthew G. Knepley {
940e5e52638SMatthew G. Knepley   PetscDS        prob;
941d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
94255f2e967SMatthew G. Knepley   PetscErrorCode ierr;
94355f2e967SMatthew G. Knepley 
94455f2e967SMatthew G. Knepley   PetscFunctionBegin;
945e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
946e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
94755f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
948f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
949648eda8cSMatthew G. Knepley     const char             *name, *labelname;
950d7ddef95SMatthew G. Knepley     DMLabel                 label;
9511c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
9521c531cf8SMatthew G. Knepley     const PetscInt         *comps;
953d7ddef95SMatthew G. Knepley     PetscObject             obj;
954d7ddef95SMatthew G. Knepley     PetscClassId            id;
955a30ec4eaSSatish Balay     void                    (*func)(void);
956d7ddef95SMatthew G. Knepley     PetscInt                numids;
957d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
95855f2e967SMatthew G. Knepley     void                   *ctx;
95955f2e967SMatthew G. Knepley 
960648eda8cSMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
961f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
962c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
963648eda8cSMatthew 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);
96444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
965d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
966d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
967c60e475cSMatthew G. Knepley       switch (type) {
968c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
969c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
970092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
9711c531cf8SMatthew 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);
972092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
973c60e475cSMatthew G. Knepley         break;
974c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
975c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
9761c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
977b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
978c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
97997b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
980c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
981c60e475cSMatthew G. Knepley         break;
982c60e475cSMatthew G. Knepley       default: break;
983c60e475cSMatthew G. Knepley       }
984d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
98543ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
9861c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
987b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
988ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
98955f2e967SMatthew G. Knepley   }
99055f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
99155f2e967SMatthew G. Knepley }
99255f2e967SMatthew G. Knepley 
993f1d73a7aSMatthew G. Knepley /*@
994f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
995f1d73a7aSMatthew G. Knepley 
996f1d73a7aSMatthew G. Knepley   Input Parameters:
997f1d73a7aSMatthew G. Knepley + dm - The DM
998f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
999f1d73a7aSMatthew G. Knepley . time - The time
1000f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
1001f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
1002f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
1003f1d73a7aSMatthew G. Knepley 
1004f1d73a7aSMatthew G. Knepley   Output Parameters:
1005f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
1006f1d73a7aSMatthew G. Knepley 
1007f1d73a7aSMatthew G. Knepley   Level: developer
1008f1d73a7aSMatthew G. Knepley 
1009f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
1010f1d73a7aSMatthew G. Knepley @*/
1011f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
1012f1d73a7aSMatthew G. Knepley {
1013f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
1014f1d73a7aSMatthew G. Knepley 
1015f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
1016f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1017f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
1018f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
1019f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
1020f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
1021f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
1022f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
1023f1d73a7aSMatthew G. Knepley }
1024f1d73a7aSMatthew G. Knepley 
10250709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
1026cb1e1211SMatthew G Knepley {
1027574a98acSMatthew G. Knepley   Vec              localX;
1028574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
1029574a98acSMatthew G. Knepley 
1030574a98acSMatthew G. Knepley   PetscFunctionBegin;
1031574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
10325d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
1033574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1034574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1035574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
1036574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1037574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
1038574a98acSMatthew G. Knepley }
1039574a98acSMatthew G. Knepley 
1040574a98acSMatthew G. Knepley /*@C
1041ca3d3a14SMatthew G. Knepley   DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
1042574a98acSMatthew G. Knepley 
1043d083f849SBarry Smith   Collective on dm
1044c0f8e1fdSMatthew G. Knepley 
1045574a98acSMatthew G. Knepley   Input Parameters:
1046574a98acSMatthew G. Knepley + dm     - The DM
1047574a98acSMatthew G. Knepley . time   - The time
1048574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
1049574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
1050574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
1051574a98acSMatthew G. Knepley 
1052574a98acSMatthew G. Knepley   Output Parameter:
1053574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
1054574a98acSMatthew G. Knepley 
1055574a98acSMatthew G. Knepley   Level: developer
1056574a98acSMatthew G. Knepley 
1057574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
1058574a98acSMatthew G. Knepley @*/
1059574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
1060574a98acSMatthew G. Knepley {
10610f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1062ca3d3a14SMatthew G. Knepley   DM               tdm;
1063ca3d3a14SMatthew G. Knepley   Vec              tv;
1064cb1e1211SMatthew G Knepley   PetscSection     section;
1065c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
10664bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
106715496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
10684bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
1069cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
10707318780aSToby Isaac   const PetscReal *quadWeights;
1071aed3cbd0SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1072ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1073cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1074cb1e1211SMatthew G Knepley 
1075cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1076c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10777318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
10782a4e142eSMatthew G. Knepley   fegeom.dimEmbed = coordDim;
107992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1080cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1081ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1082ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1083ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1084cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
108515496722SMatthew G. Knepley     PetscObject  obj;
108615496722SMatthew G. Knepley     PetscClassId id;
1087c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
1088c5bbbd5bSMatthew G. Knepley 
108944a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
109015496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
109115496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
109215496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
109315496722SMatthew G. Knepley 
10940f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10950f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
109615496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
109715496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
109815496722SMatthew G. Knepley 
109915496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
110015496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1101ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1102c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
1103cb1e1211SMatthew G Knepley   }
11049c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1105beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
11062a4e142eSMatthew 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);
1107aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1108aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
11099ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11109ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1111cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
1112a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
1113cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
11149c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1115cb1e1211SMatthew G Knepley 
11162a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1117cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1118cb1e1211SMatthew G Knepley 
111915496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
112015496722SMatthew G. Knepley       PetscObject  obj;
112115496722SMatthew G. Knepley       PetscClassId id;
1122c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
112315496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1124cb1e1211SMatthew G Knepley 
112544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
112615496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
112715496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
112815496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1129ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1130cb1e1211SMatthew G Knepley       if (debug) {
1131cb1e1211SMatthew G Knepley         char title[1024];
1132ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
11334c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1134cb1e1211SMatthew G Knepley       }
11357318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
11362a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
11372a4e142eSMatthew G. Knepley 
11382a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
11392a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
11402a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
11412a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1142ff1e0c32SBarry 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);
1143d3a7d86cSMatthew G. Knepley         if (transform) {
1144d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1145d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1146d3a7d86cSMatthew G. Knepley         } else {
1147d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1148d3a7d86cSMatthew G. Knepley         }
1149ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1150e735a8a9SMatthew G. Knepley         if (ierr) {
1151e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1152e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1153e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
11542a4e142eSMatthew G. Knepley           ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1155e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1156e735a8a9SMatthew G. Knepley         }
1157ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
11582a4e142eSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
115915496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1160ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
116115496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1162beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1163ff1e0c32SBarry Smith           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %D field %D,%D diff %g\n", c, field, fc, (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
11644bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1165cb1e1211SMatthew G Knepley         }
1166cb1e1211SMatthew G Knepley       }
11679c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
1168beaa55a6SMatthew G. Knepley       qc += Nc;
1169cb1e1211SMatthew G Knepley     }
1170cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1171ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
1172cb1e1211SMatthew G Knepley     localDiff += elemDiff;
1173cb1e1211SMatthew G Knepley   }
11742a4e142eSMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1175b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1176cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
1177cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1178cb1e1211SMatthew G Knepley }
1179cb1e1211SMatthew G Knepley 
1180b698f381SToby 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)
1181cb1e1211SMatthew G Knepley {
11820f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1183ca3d3a14SMatthew G. Knepley   DM               tdm;
1184cb1e1211SMatthew G Knepley   PetscSection     section;
118540e14135SMatthew G. Knepley   PetscQuadrature  quad;
1186ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
11879c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
11882a4e142eSMatthew G. Knepley   const PetscReal *quadWeights;
11894bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
11904bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
119140e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
11929c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1193ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1194cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1195cb1e1211SMatthew G Knepley 
1196cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1197c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11987318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
11994bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
120092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
120140e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
120240e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
120340e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
120440e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1205ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1206ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1207ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1208652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
12090f2d7e86SMatthew G. Knepley     PetscFE  fe;
121040e14135SMatthew G. Knepley     PetscInt Nc;
1211652b88e8SMatthew G. Knepley 
121244a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
12130f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12140f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
121540e14135SMatthew G. Knepley     numComponents += Nc;
1216652b88e8SMatthew G. Knepley   }
12172a4e142eSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1218beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
12194d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
12204bee2e38SMatthew 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);
122140e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
12229ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12239ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
122440e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
122540e14135SMatthew G. Knepley     PetscScalar *x = NULL;
122640e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
12279c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1228652b88e8SMatthew G. Knepley 
12294bee2e38SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
123040e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
123140e14135SMatthew G. Knepley 
12329c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
12330f2d7e86SMatthew G. Knepley       PetscFE          fe;
123451259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
12359c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
123640e14135SMatthew G. Knepley 
123744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
12380f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
12399c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
124040e14135SMatthew G. Knepley       if (debug) {
124140e14135SMatthew G. Knepley         char title[1024];
1242ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
12439c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1244652b88e8SMatthew G. Knepley       }
12459c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
12462a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
12472a4e142eSMatthew G. Knepley 
12482a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
12492a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
12502a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
12512a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1252ff1e0c32SBarry 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);
1253d3a7d86cSMatthew G. Knepley         if (transform) {
1254d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1255d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1256d3a7d86cSMatthew G. Knepley         } else {
1257d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1258d3a7d86cSMatthew G. Knepley         }
12594bee2e38SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, n, Nc, funcVal, ctx);
1260e735a8a9SMatthew G. Knepley         if (ierr) {
1261e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1262e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1263e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
12644bee2e38SMatthew G. Knepley           ierr2 = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr2);
1265e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1266e735a8a9SMatthew G. Knepley         }
1267ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
12682a4e142eSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);
12694bee2e38SMatthew G. Knepley         /* Overwrite with the dot product if the normal is given */
12704bee2e38SMatthew G. Knepley         if (n) {
12714bee2e38SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
12724bee2e38SMatthew G. Knepley             PetscScalar sum = 0.0;
12734bee2e38SMatthew G. Knepley             PetscInt    d;
12744bee2e38SMatthew G. Knepley             for (d = 0; d < dim; ++d) sum += interpolant[fc*dim+d]*n[d];
12754bee2e38SMatthew G. Knepley             interpolant[fc] = sum;
12764bee2e38SMatthew G. Knepley           }
12774bee2e38SMatthew G. Knepley         }
12789c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1279beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1280ff1e0c32SBarry 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);}
12814bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
128240e14135SMatthew G. Knepley         }
128340e14135SMatthew G. Knepley       }
12849c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
12859c3cf19fSMatthew G. Knepley       qc          += Nc;
128640e14135SMatthew G. Knepley     }
128740e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1288ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
128940e14135SMatthew G. Knepley     localDiff += elemDiff;
129040e14135SMatthew G. Knepley   }
12914bee2e38SMatthew G. Knepley   ierr  = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr);
129240e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1293b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
129440e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1295cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1296cb1e1211SMatthew G Knepley }
1297cb1e1211SMatthew G Knepley 
1298c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
129973d901b8SMatthew G. Knepley {
13000f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1301ca3d3a14SMatthew G. Knepley   DM               tdm;
130273d901b8SMatthew G. Knepley   PetscSection     section;
130373d901b8SMatthew G. Knepley   PetscQuadrature  quad;
1304ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
13054bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
130615496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
13074bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
130873d901b8SMatthew G. Knepley   PetscReal       *localDiff;
130915496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
13109c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1311ca3d3a14SMatthew G. Knepley   PetscBool        transform;
131273d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
131373d901b8SMatthew G. Knepley 
131473d901b8SMatthew G. Knepley   PetscFunctionBegin;
1315c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13167318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
131792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
131873d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
131973d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1320ca3d3a14SMatthew G. Knepley   ierr = VecSet(localX, 0.0);CHKERRQ(ierr);
132173d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
132273d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1323ca3d3a14SMatthew G. Knepley   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1324ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1325ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1326ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
132773d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
132815496722SMatthew G. Knepley     PetscObject  obj;
132915496722SMatthew G. Knepley     PetscClassId id;
133073d901b8SMatthew G. Knepley     PetscInt     Nc;
133173d901b8SMatthew G. Knepley 
133244a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
133315496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
133415496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
133515496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
133615496722SMatthew G. Knepley 
13370f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
13380f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
133915496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
134015496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
134115496722SMatthew G. Knepley 
134215496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
134315496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1344ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
134573d901b8SMatthew G. Knepley     numComponents += Nc;
134673d901b8SMatthew G. Knepley   }
13479c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1348beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
13492a4e142eSMatthew G. Knepley   ierr = PetscCalloc7(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*(Nq+1),&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
135073d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
13519ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
13529ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
135373d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
135473d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
13559c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
135673d901b8SMatthew G. Knepley 
13572a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
135873d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
135973d901b8SMatthew G. Knepley 
136015496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
136115496722SMatthew G. Knepley       PetscObject  obj;
136215496722SMatthew G. Knepley       PetscClassId id;
136373d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
136415496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
136573d901b8SMatthew G. Knepley 
136615496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
136715496722SMatthew G. Knepley 
136844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
136915496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
137015496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
137115496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1372ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
137373d901b8SMatthew G. Knepley       if (debug) {
137473d901b8SMatthew G. Knepley         char title[1024];
1375ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
1376ca3d3a14SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
137773d901b8SMatthew G. Knepley       }
13787318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
13792a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
13802a4e142eSMatthew G. Knepley 
13812a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
13822a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
13832a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
13842a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
1385ff1e0c32SBarry Smith         if (fegeom.detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature point %D", (double)fegeom.detJ[q], c, q);
1386d3a7d86cSMatthew G. Knepley         if (transform) {
1387d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1388d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1389d3a7d86cSMatthew G. Knepley         } else {
1390d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1391d3a7d86cSMatthew G. Knepley         }
13924bee2e38SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1393e735a8a9SMatthew G. Knepley         if (ierr) {
1394e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1395e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1396e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
13972a4e142eSMatthew G. Knepley           ierr2 = PetscFree7(localDiff,funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1398e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1399e735a8a9SMatthew G. Knepley         }
1400ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
14012a4e142eSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
140215496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1403ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
140415496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1405beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1406ff1e0c32SBarry Smith           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);}
14074bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
140873d901b8SMatthew G. Knepley         }
140973d901b8SMatthew G. Knepley       }
1410beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
14119c3cf19fSMatthew G. Knepley       qc          += Nc;
141273d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
1413ff1e0c32SBarry Smith       if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D field %D cum diff %g\n", c, field, (double)localDiff[field]);CHKERRQ(ierr);}
141473d901b8SMatthew G. Knepley     }
141573d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
141673d901b8SMatthew G. Knepley   }
141773d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1418b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
141973d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
14202a4e142eSMatthew G. Knepley   ierr = PetscFree7(localDiff,funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
142173d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
142273d901b8SMatthew G. Knepley }
142373d901b8SMatthew G. Knepley 
1424e729f68cSMatthew G. Knepley /*@C
1425e729f68cSMatthew 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.
1426e729f68cSMatthew G. Knepley 
1427d083f849SBarry Smith   Collective on dm
1428c0f8e1fdSMatthew G. Knepley 
1429e729f68cSMatthew G. Knepley   Input Parameters:
1430e729f68cSMatthew G. Knepley + dm    - The DM
14310163fd50SMatthew G. Knepley . time  - The time
1432ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1433e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1434e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1435e729f68cSMatthew G. Knepley 
1436e729f68cSMatthew G. Knepley   Output Parameter:
1437e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1438e729f68cSMatthew G. Knepley 
1439e729f68cSMatthew G. Knepley   Level: developer
1440e729f68cSMatthew G. Knepley 
1441b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1442e729f68cSMatthew G. Knepley @*/
14430163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1444e729f68cSMatthew G. Knepley {
1445e729f68cSMatthew G. Knepley   PetscSection     section;
1446e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1447e729f68cSMatthew G. Knepley   Vec              localX;
14484bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
1449e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
14504bee2e38SMatthew G. Knepley   PetscReal       *coords;
1451e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
14529c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1453e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1454e729f68cSMatthew G. Knepley 
1455e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1456e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1457e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
14587318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
145992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1460e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1461e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1462bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1463e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1464e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1465e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1466e729f68cSMatthew G. Knepley     PetscObject  obj;
1467e729f68cSMatthew G. Knepley     PetscClassId id;
1468e729f68cSMatthew G. Knepley     PetscInt     Nc;
1469e729f68cSMatthew G. Knepley 
147044a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1471e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1472e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1473e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1474e729f68cSMatthew G. Knepley 
1475e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1476e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1477e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1478e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1479e729f68cSMatthew G. Knepley 
1480e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1481e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1482ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1483e729f68cSMatthew G. Knepley     numComponents += Nc;
1484e729f68cSMatthew G. Knepley   }
14859c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1486beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
14872a4e142eSMatthew 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);
1488e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1489e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1490e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1491e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1492e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
14936f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
14949c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1495e729f68cSMatthew G. Knepley 
14962a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1497e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1498e729f68cSMatthew G. Knepley 
1499e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1500e729f68cSMatthew G. Knepley       PetscObject  obj;
1501e729f68cSMatthew G. Knepley       PetscClassId id;
1502e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1503e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1504e729f68cSMatthew G. Knepley 
150544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1506e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1507e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1508e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1509ff1e0c32SBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
151023f34ed2SToby Isaac       if (funcs[field]) {
15117318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
15122a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
15132a4e142eSMatthew G. Knepley 
15142a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
15152a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
15162a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
15172a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
15184bee2e38SMatthew 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);
1519274e8aaeSMatthew G. Knepley           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);
1520e735a8a9SMatthew G. Knepley           if (ierr) {
1521e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1522e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
15232a4e142eSMatthew G. Knepley             ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1524e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1525e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1526e735a8a9SMatthew G. Knepley           }
15272a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1528e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1529ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1530e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1531beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
15324bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1533e729f68cSMatthew G. Knepley           }
1534e729f68cSMatthew G. Knepley         }
153523f34ed2SToby Isaac       }
1536beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
15379c3cf19fSMatthew G. Knepley       qc          += Nc;
1538e729f68cSMatthew G. Knepley     }
1539e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
154023f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1541e729f68cSMatthew G. Knepley   }
15422a4e142eSMatthew G. Knepley   ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1543e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1544e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1545e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1546e729f68cSMatthew G. Knepley }
1547e729f68cSMatthew G. Knepley 
15481555c271SMatthew G. Knepley /*@C
15491555c271SMatthew 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.
15501555c271SMatthew G. Knepley 
1551d083f849SBarry Smith   Collective on dm
1552c0f8e1fdSMatthew G. Knepley 
15531555c271SMatthew G. Knepley   Input Parameters:
15541555c271SMatthew G. Knepley + dm - The DM
15551555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
15561555c271SMatthew G. Knepley 
15571555c271SMatthew G. Knepley   Output Parameter:
15581555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
15591555c271SMatthew G. Knepley 
156095452b02SPatrick Sanan   Notes:
156195452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
15621555c271SMatthew 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
15631555c271SMatthew G. Knepley 
15641555c271SMatthew G. Knepley   Level: developer
15651555c271SMatthew G. Knepley 
15661555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
15671555c271SMatthew G. Knepley @*/
15681555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
15691555c271SMatthew G. Knepley {
1570db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1571db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
15721555c271SMatthew G. Knepley   DM               dmC;
15731555c271SMatthew G. Knepley   PetscSection     section;
15741555c271SMatthew G. Knepley   PetscQuadrature  quad;
15751555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
15764bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
15774bee2e38SMatthew G. Knepley   PetscReal       *coords;
15781555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
15791555c271SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset;
15801555c271SMatthew G. Knepley   PetscErrorCode   ierr;
15811555c271SMatthew G. Knepley 
15821555c271SMatthew G. Knepley   PetscFunctionBegin;
15831555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
15841555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
15851555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15861555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
15874bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
158892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
15891555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
15901555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
15911555c271SMatthew G. Knepley     PetscObject  obj;
15921555c271SMatthew G. Knepley     PetscClassId id;
15931555c271SMatthew G. Knepley     PetscInt     Nc;
15941555c271SMatthew G. Knepley 
159544a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
15961555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15971555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
15981555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
15991555c271SMatthew G. Knepley 
16001555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
16011555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
16021555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
16031555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
16041555c271SMatthew G. Knepley 
16051555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
16061555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1607ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
16081555c271SMatthew G. Knepley     numComponents += Nc;
16091555c271SMatthew G. Knepley   }
16101555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
16111555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
16124bee2e38SMatthew 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);
16131555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
16141555c271SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
16151555c271SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
16161555c271SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
16171555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
16181555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
16191555c271SMatthew G. Knepley     PetscInt   *star = NULL;
16201555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
16211555c271SMatthew G. Knepley 
1622580bdb30SBarry Smith     ierr = PetscArrayzero(gradsum, coordDim*numComponents);CHKERRQ(ierr);
16231555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
16241555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
16251555c271SMatthew G. Knepley       const PetscInt cell = star[st];
16261555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
16271555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
16281555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
16291555c271SMatthew G. Knepley 
16301555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
16314bee2e38SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
16321555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
16331555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
16341555c271SMatthew G. Knepley         PetscObject  obj;
16351555c271SMatthew G. Knepley         PetscClassId id;
16361555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
16371555c271SMatthew G. Knepley 
1638580bdb30SBarry Smith         ierr = PetscArrayzero(grad, coordDim*numComponents);CHKERRQ(ierr);
163944a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
16401555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
16411555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
16421555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1643ff1e0c32SBarry Smith         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
16441555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
16452a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
16462a4e142eSMatthew G. Knepley 
16472a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
16482a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
16492a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
16502a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
16514bee2e38SMatthew 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);
16521555c271SMatthew G. Knepley           if (ierr) {
16531555c271SMatthew G. Knepley             PetscErrorCode ierr2;
16541555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
16551555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
16564bee2e38SMatthew G. Knepley             ierr2 = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
16571555c271SMatthew G. Knepley             CHKERRQ(ierr);
16581555c271SMatthew G. Knepley           }
16592a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1660ff1e0c32SBarry Smith           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
16611555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
16621555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
16631555c271SMatthew G. Knepley 
16644bee2e38SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*fegeom.detJ[q];
16651555c271SMatthew G. Knepley           }
16664bee2e38SMatthew G. Knepley           vol += quadWeights[q*qNc]*fegeom.detJ[q];
16671555c271SMatthew G. Knepley         }
16681555c271SMatthew G. Knepley         fieldOffset += Nb;
16691555c271SMatthew G. Knepley         qc          += Nc;
16701555c271SMatthew G. Knepley       }
16711555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1672f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1673f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1674f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1675f8527842SMatthew G. Knepley         }
1676f8527842SMatthew G. Knepley       }
1677f8527842SMatthew G. Knepley       volsum += vol;
1678db1066baSMatthew G. Knepley       if (debug) {
16796d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
16801555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
16811555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
16821555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
16836d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
16841555c271SMatthew G. Knepley           }
16851555c271SMatthew G. Knepley         }
16861555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1687db1066baSMatthew G. Knepley       }
16881555c271SMatthew G. Knepley     }
16891555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
16901555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
16911555c271SMatthew G. Knepley     }
16921555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
16931555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
16941555c271SMatthew G. Knepley   }
16954bee2e38SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
16961555c271SMatthew G. Knepley   PetscFunctionReturn(0);
16971555c271SMatthew G. Knepley }
16981555c271SMatthew G. Knepley 
1699338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
170073d901b8SMatthew G. Knepley {
1701338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
170261aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
170373d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1704338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1705c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1706c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1707338f77d5SMatthew G. Knepley   /* DS */
1708338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1709338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1710338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1711338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1712338f77d5SMatthew G. Knepley   /* Geometry */
1713c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1714338f77d5SMatthew G. Knepley   DM                 dmGrad;
1715c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1716338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1717b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1718338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1719b7260050SToby Isaac   PetscInt           maxDegree;
1720c330f8ffSToby Isaac   DMField            coordField;
1721c330f8ffSToby Isaac   IS                 cellIS;
172273d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
172373d901b8SMatthew G. Knepley 
172473d901b8SMatthew G. Knepley   PetscFunctionBegin;
1725338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1726c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
172792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
172873d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1729338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1730b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1731b5a3613cSMatthew G. Knepley     PetscObject  obj;
1732b5a3613cSMatthew G. Knepley     PetscClassId id;
1733b5a3613cSMatthew G. Knepley 
1734b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1735b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1736338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1737338f77d5SMatthew G. Knepley   }
1738338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1739338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1740338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1741338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1742338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1743338f77d5SMatthew G. Knepley   /* Read DS information */
1744338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1745338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1746338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1747c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1748338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1749338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1750338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1751338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1752338f77d5SMatthew G. Knepley   if (dmAux) {
1753338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1754338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
175592fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
1756338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1757338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1758338f77d5SMatthew G. Knepley   }
1759338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1760338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1761338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1762338f77d5SMatthew G. Knepley   /* Read out geometry */
1763c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1764b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1765b7260050SToby Isaac   if (maxDegree <= 1) {
1766c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1767c330f8ffSToby Isaac     if (affineQuad) {
1768c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1769338f77d5SMatthew G. Knepley     }
1770b5a3613cSMatthew G. Knepley   }
1771b5a3613cSMatthew G. Knepley   if (useFVM) {
1772338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1773b5a3613cSMatthew G. Knepley     Vec       grad;
1774b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1775b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1776b5a3613cSMatthew G. Knepley 
1777338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1778338f77d5SMatthew G. Knepley       PetscObject  obj;
1779338f77d5SMatthew G. Knepley       PetscClassId id;
1780338f77d5SMatthew G. Knepley 
1781338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1782338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1783338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1784338f77d5SMatthew G. Knepley     }
1785338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1786338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1787b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1788338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1789338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1790b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1791b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1792b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1793b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1794338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1795b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1796b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1797b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1798b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1799b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1800b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1801338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1802b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1803b5a3613cSMatthew G. Knepley   }
1804338f77d5SMatthew G. Knepley   /* Read out data from inputs */
180573d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
180673d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
180773d901b8SMatthew G. Knepley     PetscInt     i;
180873d901b8SMatthew G. Knepley 
1809338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
18100f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1811338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
181273d901b8SMatthew G. Knepley     if (dmAux) {
1813338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
18140f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1815338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
181673d901b8SMatthew G. Knepley     }
181773d901b8SMatthew G. Knepley   }
1818338f77d5SMatthew G. Knepley   /* Do integration for each field */
181973d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1820c1f031eeSMatthew G. Knepley     PetscObject  obj;
1821c1f031eeSMatthew G. Knepley     PetscClassId id;
1822c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
182373d901b8SMatthew G. Knepley 
1824c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1825c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1826c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1827c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1828c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1829c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1830c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1831c1f031eeSMatthew G. Knepley 
18320f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1833c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
18349c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1835c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1836c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
183773d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
18380f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
183973d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
184073d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
184173d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
184273d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1843c330f8ffSToby Isaac       if (!affineQuad) {
1844c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1845c330f8ffSToby Isaac       }
1846c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
18474bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
1848c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
18494bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1850c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1851c330f8ffSToby Isaac       if (!affineQuad) {
1852c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1853c330f8ffSToby Isaac       }
1854c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1855c1f031eeSMatthew G. Knepley       PetscInt       foff;
1856420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1857b69edc29SMatthew G. Knepley       PetscScalar    lint;
1858c1f031eeSMatthew G. Knepley 
1859c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1860c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1861c1f031eeSMatthew G. Knepley       if (obj_func) {
1862c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1863b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1864b5a3613cSMatthew G. Knepley 
1865b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1866338f77d5SMatthew 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);
1867338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
186873d901b8SMatthew G. Knepley         }
1869c1f031eeSMatthew G. Knepley       }
1870ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1871c1f031eeSMatthew G. Knepley   }
1872338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1873b5a3613cSMatthew G. Knepley   if (useFVM) {
1874b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1875b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1876b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1877b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1878b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1879b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1880b5a3613cSMatthew G. Knepley   }
188173d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1882338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1883338f77d5SMatthew G. Knepley   /* Cleanup */
1884f99c8401SToby Isaac   if (affineQuad) {
1885f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1886f99c8401SToby Isaac   }
1887f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1888c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1889338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1890338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1891338f77d5SMatthew G. Knepley }
1892338f77d5SMatthew G. Knepley 
1893338f77d5SMatthew G. Knepley /*@
1894338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1895338f77d5SMatthew G. Knepley 
1896338f77d5SMatthew G. Knepley   Input Parameters:
1897338f77d5SMatthew G. Knepley + dm - The mesh
1898338f77d5SMatthew G. Knepley . X  - Global input vector
1899338f77d5SMatthew G. Knepley - user - The user context
1900338f77d5SMatthew G. Knepley 
1901338f77d5SMatthew G. Knepley   Output Parameter:
1902338f77d5SMatthew G. Knepley . integral - Integral for each field
1903338f77d5SMatthew G. Knepley 
1904338f77d5SMatthew G. Knepley   Level: developer
1905338f77d5SMatthew G. Knepley 
1906338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1907338f77d5SMatthew G. Knepley @*/
1908b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1909338f77d5SMatthew G. Knepley {
1910338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1911b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1912338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1913338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1914338f77d5SMatthew G. Knepley 
1915338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1916338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1917338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1918338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1919338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1920338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1921338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1922338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1923338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1924338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1925338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1926338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1927338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1928338f77d5SMatthew G. Knepley   /* Sum up values */
1929338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1930338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1931338f77d5SMatthew G. Knepley 
1932338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1933b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
1934338f77d5SMatthew G. Knepley   }
1935b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
193673d901b8SMatthew G. Knepley   if (mesh->printFEM) {
1937338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
1938b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
193973d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
194073d901b8SMatthew G. Knepley   }
1941338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
1942338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1943338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1944338f77d5SMatthew G. Knepley }
1945338f77d5SMatthew G. Knepley 
1946338f77d5SMatthew G. Knepley /*@
1947338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
1948338f77d5SMatthew G. Knepley 
1949338f77d5SMatthew G. Knepley   Input Parameters:
1950338f77d5SMatthew G. Knepley + dm - The mesh
1951338f77d5SMatthew G. Knepley . X  - Global input vector
1952338f77d5SMatthew G. Knepley - user - The user context
1953338f77d5SMatthew G. Knepley 
1954338f77d5SMatthew G. Knepley   Output Parameter:
1955338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
1956338f77d5SMatthew G. Knepley 
1957338f77d5SMatthew G. Knepley   Level: developer
1958338f77d5SMatthew G. Knepley 
1959338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1960338f77d5SMatthew G. Knepley @*/
1961338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
1962338f77d5SMatthew G. Knepley {
1963338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1964338f77d5SMatthew G. Knepley   DM             dmF;
1965338f77d5SMatthew G. Knepley   PetscSection   sectionF;
1966338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
1967338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1968338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1969338f77d5SMatthew G. Knepley 
1970338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1971338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1972338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1973338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
1974338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1975338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1976338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1977338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1978338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1979338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1980338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1981338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1982338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1983338f77d5SMatthew G. Knepley   /* Put values in F*/
1984338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
198592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmF, &sectionF);CHKERRQ(ierr);
1986338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
1987338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1988338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1989338f77d5SMatthew G. Knepley     PetscInt       dof, off;
1990338f77d5SMatthew G. Knepley 
1991338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1992338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
1993338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
1994338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
1995338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
1996338f77d5SMatthew G. Knepley   }
1997338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
1998338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
1999c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
200073d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
200173d901b8SMatthew G. Knepley }
200273d901b8SMatthew G. Knepley 
20039b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
20049b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
200564c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
200664c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
200764c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
200864c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
200964c72086SMatthew G. Knepley {
20109b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
20119b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
20129b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
20139b6f715bSMatthew G. Knepley   Vec                locA = NULL;
20149b6f715bSMatthew G. Knepley   DMField            coordField;
20159b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
20169b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
20179b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
201864c72086SMatthew G. Knepley   const PetscScalar *constants;
20199b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
202064c72086SMatthew G. Knepley   PetscErrorCode     ierr;
202164c72086SMatthew G. Knepley 
202264c72086SMatthew G. Knepley   PetscFunctionBegin;
20239b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
20249b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
202564c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
202692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
202764c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
202864c72086SMatthew G. Knepley   /* Determine which discretizations we have */
20299b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
203064c72086SMatthew G. Knepley     PetscObject  obj;
203164c72086SMatthew G. Knepley     PetscClassId id;
203264c72086SMatthew G. Knepley 
20339b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
203464c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
20359b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
203664c72086SMatthew G. Knepley   }
203764c72086SMatthew G. Knepley   /* Read DS information */
203864c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
203964c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
204064c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
204164c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
204264c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
204364c72086SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
20449b6f715bSMatthew G. Knepley   if (locA) {
20459b6f715bSMatthew G. Knepley     DM dmAux;
20469b6f715bSMatthew G. Knepley 
20479b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
20489b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
204964c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
205064c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
205192fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
205264c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
205364c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
205464c72086SMatthew G. Knepley   }
20559b6f715bSMatthew G. Knepley   /* Integrate over points */
20569b6f715bSMatthew G. Knepley   {
20579b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
2058b7260050SToby Isaac     PetscInt        maxDegree;
20599b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
20609b6f715bSMatthew G. Knepley     const PetscInt *points;
20619b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
20629b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
206364c72086SMatthew G. Knepley 
20649b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
20659b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
20669b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
2067b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
206864c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
206964c72086SMatthew G. Knepley       PetscFE fe;
207064c72086SMatthew G. Knepley 
207164c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
2072b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
20739b6f715bSMatthew G. Knepley       if (!qGeom) {
20749b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
20759b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
20769b6f715bSMatthew G. Knepley       }
20779b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
20789b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
20799b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
20809b6f715bSMatthew G. Knepley         const PetscInt point = points[face], *support, *cone;
20819b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
20829b6f715bSMatthew G. Knepley         PetscInt       i, coneSize, faceLoc;
20839b6f715bSMatthew G. Knepley 
20849b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
20859b6f715bSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
20869b6f715bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
20879b6f715bSMatthew G. Knepley         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
20889b6f715bSMatthew 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]);
20899b6f715bSMatthew G. Knepley         fgeom->face[face][0] = faceLoc;
20909b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
20919b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
20929b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
20939b6f715bSMatthew G. Knepley         if (locA) {
20949b6f715bSMatthew G. Knepley           PetscInt subp;
20959b6f715bSMatthew G. Knepley           ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);
20969b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
20979b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
20989b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
20999b6f715bSMatthew G. Knepley         }
21009b6f715bSMatthew G. Knepley       }
21019b6f715bSMatthew G. Knepley       /* Get blocking */
21029b6f715bSMatthew G. Knepley       {
21039b6f715bSMatthew G. Knepley         PetscQuadrature q;
21049b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
21059b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
21069b6f715bSMatthew G. Knepley 
210764c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
210864c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
210964c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
211064c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
211164c72086SMatthew G. Knepley         blockSize = Nb*Nq;
211264c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
21139b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
211464c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
21159b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
21169b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
211764c72086SMatthew G. Knepley         offset    = numFaces - Nr;
211864c72086SMatthew G. Knepley       }
21199b6f715bSMatthew G. Knepley       /* Do integration for each field */
21209b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
21219b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
21224bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateBd(prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
21239b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
212464c72086SMatthew G. Knepley       }
21259b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
21264bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrateBd(prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
21279b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
212864c72086SMatthew G. Knepley       /* Cleanup data arrays */
21299b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
21309b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
21319b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
21329b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
213364c72086SMatthew G. Knepley     }
213464c72086SMatthew G. Knepley   }
21359b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
21369b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
21379b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
21389b6f715bSMatthew G. Knepley }
21399b6f715bSMatthew G. Knepley 
21409b6f715bSMatthew G. Knepley /*@
21419b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
21429b6f715bSMatthew G. Knepley 
21439b6f715bSMatthew G. Knepley   Input Parameters:
21449b6f715bSMatthew G. Knepley + dm      - The mesh
21459b6f715bSMatthew G. Knepley . X       - Global input vector
21469b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
21479b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
21489b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
21499b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
21509b6f715bSMatthew G. Knepley - user    - The user context
21519b6f715bSMatthew G. Knepley 
21529b6f715bSMatthew G. Knepley   Output Parameter:
21539b6f715bSMatthew G. Knepley . integral - Integral for each field
21549b6f715bSMatthew G. Knepley 
21559b6f715bSMatthew G. Knepley   Level: developer
21569b6f715bSMatthew G. Knepley 
21579b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
21589b6f715bSMatthew G. Knepley @*/
21599b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
21609b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
21619b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
21629b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
21639b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
21649b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
21659b6f715bSMatthew G. Knepley {
21669b6f715bSMatthew G. Knepley   Vec            locX;
21679b6f715bSMatthew G. Knepley   PetscSection   section;
21689b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
21699b6f715bSMatthew G. Knepley   IS             facetIS;
21709b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
21719b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
21729b6f715bSMatthew G. Knepley 
21739b6f715bSMatthew G. Knepley   PetscFunctionBegin;
21749b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21759b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
21769b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
21779b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
21789b6f715bSMatthew G. Knepley   PetscValidPointer(integral, 6);
21799b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
21809b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
21819b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
21829b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
218392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
21849b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
21859b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
21869b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
21879b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
21889b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
21899b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
21909b6f715bSMatthew G. Knepley   /* Loop over label values */
2191580bdb30SBarry Smith   ierr = PetscArrayzero(integral, Nf);CHKERRQ(ierr);
21929b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
21939b6f715bSMatthew G. Knepley     IS           pointIS;
21949b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
21959b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
21969b6f715bSMatthew G. Knepley 
21979b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
21989b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
21999b6f715bSMatthew G. Knepley     {
22009b6f715bSMatthew G. Knepley       IS isectIS;
22019b6f715bSMatthew G. Knepley 
22029b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
22039b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
22049b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22059b6f715bSMatthew G. Knepley       pointIS = isectIS;
22069b6f715bSMatthew G. Knepley     }
22079b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
22089b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
22099b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
22109b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
22119b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
22129b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
22139b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22149b6f715bSMatthew G. Knepley   }
221564c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
22169b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
22179b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
221864c72086SMatthew G. Knepley   PetscFunctionReturn(0);
221964c72086SMatthew G. Knepley }
222064c72086SMatthew G. Knepley 
2221d69c5d34SMatthew G. Knepley /*@
222268132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
2223d69c5d34SMatthew G. Knepley 
2224d69c5d34SMatthew G. Knepley   Input Parameters:
2225d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
2226d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
2227d69c5d34SMatthew G. Knepley - user - The user context
2228d69c5d34SMatthew G. Knepley 
2229d69c5d34SMatthew G. Knepley   Output Parameter:
2230934789fcSMatthew G. Knepley . In  - The interpolation matrix
2231d69c5d34SMatthew G. Knepley 
2232d69c5d34SMatthew G. Knepley   Level: developer
2233d69c5d34SMatthew G. Knepley 
223468132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2235d69c5d34SMatthew G. Knepley @*/
223668132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
2237d69c5d34SMatthew G. Knepley {
2238d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
2239d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
22402764a2aaSMatthew G. Knepley   PetscDS           prob;
2241d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
224297c42addSMatthew G. Knepley   PetscFV          *fvRef;
2243d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
2244d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
2245d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
22469ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
22470f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
2248d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
2249d69c5d34SMatthew G. Knepley 
2250d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
2251d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2252c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
225392fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2254e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
225592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2256e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
2257d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
2258d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
22599ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
22609ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
22612764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
226297c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
2263d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
226497c42addSMatthew G. Knepley     PetscObject  obj;
226597c42addSMatthew G. Knepley     PetscClassId id;
2266aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
2267d69c5d34SMatthew G. Knepley 
226897c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
226997c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
227097c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
227197c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
227297c42addSMatthew G. Knepley 
22730f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
2274d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
22750f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
227697c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
227797c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
227897c42addSMatthew G. Knepley       PetscDualSpace Q;
227997c42addSMatthew G. Knepley 
228097c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
228197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
228297c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
228397c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
228497c42addSMatthew G. Knepley     }
22859c3cf19fSMatthew G. Knepley     rTotDim += rNb;
2286d69c5d34SMatthew G. Knepley   }
22872764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
22880f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
2289580bdb30SBarry Smith   ierr = PetscArrayzero(elemMat, rTotDim*cTotDim);CHKERRQ(ierr);
2290d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
2291d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
2292d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
2293d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2294d69c5d34SMatthew G. Knepley     PetscReal       *points;
2295d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
2296d69c5d34SMatthew G. Knepley 
2297d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
229897c42addSMatthew G. Knepley     if (feRef[fieldI]) {
2299d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
23000f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
230197c42addSMatthew G. Knepley     } else {
230297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
230397c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
230497c42addSMatthew G. Knepley     }
2305d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
2306d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
2307d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23089c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
2309d69c5d34SMatthew G. Knepley       npoints += Np;
2310d69c5d34SMatthew G. Knepley     }
2311d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
2312d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
2313d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23149c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
2315d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
2316d69c5d34SMatthew G. Knepley     }
2317d69c5d34SMatthew G. Knepley 
2318d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
231997c42addSMatthew G. Knepley       PetscObject  obj;
232097c42addSMatthew G. Knepley       PetscClassId id;
2321d69c5d34SMatthew G. Knepley       PetscReal   *B;
23229c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
2323d69c5d34SMatthew G. Knepley 
232497c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
232597c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
232697c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
232797c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
2328d69c5d34SMatthew G. Knepley 
2329d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
23300f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
23310f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2332ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
2333ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
23349c3cf19fSMatthew 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);
23350f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
2336d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
2337d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23389c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
23399c3cf19fSMatthew 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);
2340d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
234136a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2342d172c84bSMatthew G. Knepley                 /*
2343d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
2344d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
2345d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
2346d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
2347d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
2348d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
2349d172c84bSMatthew G. Knepley                 */
23509c3cf19fSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += B[k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c];
235136a6d9c0SMatthew G. Knepley               }
2352d69c5d34SMatthew G. Knepley             }
2353d69c5d34SMatthew G. Knepley           }
23540f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2355ffe73a53SMatthew G. Knepley         }
235697c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
235797c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
235897c42addSMatthew G. Knepley 
235997c42addSMatthew G. Knepley         /* Evaluate constant function at points */
236097c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
236197c42addSMatthew G. Knepley         cpdim = 1;
236297c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
236397c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
2364ff1e0c32SBarry 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);
236597c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
236697c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
23679c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
23689c3cf19fSMatthew 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);
236997c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
237097c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2371458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
237297c42addSMatthew G. Knepley               }
237397c42addSMatthew G. Knepley             }
237497c42addSMatthew G. Knepley           }
237597c42addSMatthew G. Knepley         }
237697c42addSMatthew G. Knepley       }
2377d172c84bSMatthew G. Knepley       offsetJ += cpdim;
2378d69c5d34SMatthew G. Knepley     }
2379d172c84bSMatthew G. Knepley     offsetI += fpdim;
2380549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
2381d69c5d34SMatthew G. Knepley   }
23820f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
23837f5b169aSMatthew G. Knepley   /* Preallocate matrix */
23847f5b169aSMatthew G. Knepley   {
2385c094ef40SMatthew G. Knepley     Mat          preallocator;
2386c094ef40SMatthew G. Knepley     PetscScalar *vals;
2387c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
2388c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
23897f5b169aSMatthew G. Knepley 
2390c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
2391c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
2392c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
2393c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2394c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
2395c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
23967f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
23977f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
2398c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
23997f5b169aSMatthew G. Knepley     }
2400c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
2401c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2402c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2403c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
2404c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
24057f5b169aSMatthew G. Knepley   }
24067f5b169aSMatthew G. Knepley   /* Fill matrix */
24077f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
2408d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2409934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2410d69c5d34SMatthew G. Knepley   }
2411549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
241297c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
2413549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2414934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2415934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2416d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
2417825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
2418934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
2419825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
2420d69c5d34SMatthew G. Knepley   }
2421d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2422d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
2423d69c5d34SMatthew G. Knepley }
24246c73c22cSMatthew G. Knepley 
2425bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
2426bd041c0cSMatthew G. Knepley {
2427bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
2428bd041c0cSMatthew G. Knepley }
2429bd041c0cSMatthew G. Knepley 
243068132eb9SMatthew G. Knepley /*@
243168132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
243268132eb9SMatthew G. Knepley 
243368132eb9SMatthew G. Knepley   Input Parameters:
243468132eb9SMatthew G. Knepley + dmf  - The fine mesh
243568132eb9SMatthew G. Knepley . dmc  - The coarse mesh
243668132eb9SMatthew G. Knepley - user - The user context
243768132eb9SMatthew G. Knepley 
243868132eb9SMatthew G. Knepley   Output Parameter:
243968132eb9SMatthew G. Knepley . In  - The interpolation matrix
244068132eb9SMatthew G. Knepley 
244168132eb9SMatthew G. Knepley   Level: developer
244268132eb9SMatthew G. Knepley 
244368132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
244468132eb9SMatthew G. Knepley @*/
244568132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
24464ef9d792SMatthew G. Knepley {
244764e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
244864e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
24494ef9d792SMatthew G. Knepley   PetscDS        prob;
24504ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2451e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
24524ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
24534ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
24544ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
24554ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
24564ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
24574ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
24584ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
24594ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
24604ef9d792SMatthew G. Knepley 
24614ef9d792SMatthew G. Knepley   PetscFunctionBegin;
246277711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
24634ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
24644ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
24654bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
24664ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
24674ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
24684ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
246992fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2470e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
247192fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2472e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
24734ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
24744ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
24759c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
24764ef9d792SMatthew G. Knepley 
24774ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
24784ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
24794ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
24804ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
24814ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
24824ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
24834ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
24844ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2485e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
24864ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
24874ef9d792SMatthew G. Knepley     PetscObject      obj;
24884ef9d792SMatthew G. Knepley     PetscClassId     id;
2489c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
24904ef9d792SMatthew G. Knepley     PetscQuadrature  f;
249117f047d8SMatthew G. Knepley     const PetscReal *qpoints;
249217f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
24934ef9d792SMatthew G. Knepley 
24944ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
24954ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
24964ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
24974ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
24984ef9d792SMatthew G. Knepley 
24994ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
25004ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
25014ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
25024ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
25034ef9d792SMatthew G. Knepley 
25044ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
25054ef9d792SMatthew G. Knepley       Nc   = 1;
25064ef9d792SMatthew G. Knepley     }
25074ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
25084ef9d792SMatthew G. Knepley     /* For each fine grid cell */
25094ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
25104ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
25114ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
25124ef9d792SMatthew G. Knepley 
25136ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
25144ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2515ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
25164ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
25174ef9d792SMatthew G. Knepley         Vec             pointVec;
25184ef9d792SMatthew G. Knepley         PetscScalar    *pV;
25193a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
25203a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
25219c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
25224ef9d792SMatthew G. Knepley 
25234ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
25244ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
25259c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
25264ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
25274ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
25284ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
25294ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2530c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2531c330f8ffSToby Isaac 
25324ef9d792SMatthew G. Knepley           /* Transform point to real space */
2533c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
25344ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
25354ef9d792SMatthew G. Knepley         }
25364ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
25374ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
25381555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
253962a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
25403a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
25414ef9d792SMatthew G. Knepley         /* Update preallocation info */
25423a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
25433a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
25449c3cf19fSMatthew G. Knepley         {
2545e8f14785SLisandro Dalcin           PetscHashIJKey key;
2546e8f14785SLisandro Dalcin           PetscBool      missing;
25474ef9d792SMatthew G. Knepley 
2548e8f14785SLisandro Dalcin           key.i = findices[i];
2549e8f14785SLisandro Dalcin           if (key.i >= 0) {
25504ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
25514ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
25523a93e3b7SToby Isaac               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
25534ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2554e8f14785SLisandro Dalcin                 key.j = cindices[c];
2555e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2556e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
25574ef9d792SMatthew G. Knepley                 if (missing) {
2558e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2559e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
25604ef9d792SMatthew G. Knepley                 }
25614ef9d792SMatthew G. Knepley               }
25623a93e3b7SToby Isaac               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
25634ef9d792SMatthew G. Knepley             }
25644ef9d792SMatthew G. Knepley           }
25658c543595SMatthew G. Knepley         }
25663a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
25674ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
25684ef9d792SMatthew G. Knepley       }
256946bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
25704ef9d792SMatthew G. Knepley     }
25714ef9d792SMatthew G. Knepley   }
2572e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
25734ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
25744ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
25754ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
25764ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
25774ef9d792SMatthew G. Knepley     PetscObject      obj;
25784ef9d792SMatthew G. Knepley     PetscClassId     id;
2579c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
25804ef9d792SMatthew G. Knepley     PetscQuadrature  f;
25814ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
25829c3cf19fSMatthew G. Knepley     PetscInt         Nc, qNc, Np, fpdim, i, d;
25834ef9d792SMatthew G. Knepley 
25844ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
25854ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
25864ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
25874ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
25884ef9d792SMatthew G. Knepley 
25894ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
25904ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
25914ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
25924ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
25934ef9d792SMatthew G. Knepley 
25944ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
25954ef9d792SMatthew G. Knepley       Nc   = 1;
2596ff1e0c32SBarry Smith     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",field);
25974ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
25984ef9d792SMatthew G. Knepley     /* For each fine grid cell */
25994ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
26004ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
26014ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
26024ef9d792SMatthew G. Knepley 
26036ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
26044ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2605ff1e0c32SBarry Smith       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
26064ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
26074ef9d792SMatthew G. Knepley         Vec             pointVec;
26084ef9d792SMatthew G. Knepley         PetscScalar    *pV;
260912111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
26103a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
261117f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
26124ef9d792SMatthew G. Knepley 
26134ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
26144ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
26159c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
26169c3cf19fSMatthew 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);
26174ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
26184ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
26194ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
26204ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2621c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2622c330f8ffSToby Isaac 
26234ef9d792SMatthew G. Knepley           /* Transform point to real space */
2624c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
26254ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
26264ef9d792SMatthew G. Knepley         }
26274ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
26284ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
26291555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
263062a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
26314ef9d792SMatthew G. Knepley         /* Update preallocation info */
26323a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
26333a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
26344ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
26354ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2636826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2637c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2638826eb36dSMatthew G. Knepley 
26393a93e3b7SToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
26404ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
26413a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2642e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2643c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
26444ef9d792SMatthew G. Knepley 
26454ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
26464ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
26474ef9d792SMatthew G. Knepley             PetscReal *B;
26484ef9d792SMatthew G. Knepley 
26494ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
26504ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
26514ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2652580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
26534ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
26544ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
26559c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c];
26564ef9d792SMatthew G. Knepley             }
26574ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
26584ef9d792SMatthew G. Knepley           } else {
26594ef9d792SMatthew G. Knepley             cpdim = 1;
26604ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
26619c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
26624ef9d792SMatthew G. Knepley             }
26634ef9d792SMatthew G. Knepley           }
26644ef9d792SMatthew G. Knepley           /* Update interpolator */
26659c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
26669c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
26679c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
26683a93e3b7SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
26694ef9d792SMatthew G. Knepley         }
26704ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
26713a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
26724ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
26734ef9d792SMatthew G. Knepley       }
267446bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
26754ef9d792SMatthew G. Knepley     }
26764ef9d792SMatthew G. Knepley   }
26774ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
26784ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
26794ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
26804ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
26814ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
268277711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
26834ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
26844ef9d792SMatthew G. Knepley }
26854ef9d792SMatthew G. Knepley 
268646fa42a0SMatthew G. Knepley /*@
2687bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2688bd041c0cSMatthew G. Knepley 
2689bd041c0cSMatthew G. Knepley   Input Parameters:
2690bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2691bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2692bd041c0cSMatthew G. Knepley - user - The user context
2693bd041c0cSMatthew G. Knepley 
2694bd041c0cSMatthew G. Knepley   Output Parameter:
2695bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2696bd041c0cSMatthew G. Knepley 
2697bd041c0cSMatthew G. Knepley   Level: developer
2698bd041c0cSMatthew G. Knepley 
2699bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2700bd041c0cSMatthew G. Knepley @*/
2701bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2702bd041c0cSMatthew G. Knepley {
2703bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2704bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2705bd041c0cSMatthew G. Knepley   PetscDS        prob;
2706bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2707e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2708bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2709bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2710bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2711bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2712bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2713bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2714bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2715bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2716bd041c0cSMatthew G. Knepley 
2717bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2718bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2719bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
27204bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2721bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2722bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2723bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
272492fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2725e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
272692fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2727e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2728bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2729bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2730bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2731bd041c0cSMatthew G. Knepley 
2732bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2733bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2734bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2735bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2736bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2737bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2738bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2739bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2740e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2741bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2742bd041c0cSMatthew G. Knepley     PetscObject      obj;
2743bd041c0cSMatthew G. Knepley     PetscClassId     id;
2744bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2745bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2746bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2747bd041c0cSMatthew G. Knepley 
2748bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2749bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2750bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2751bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2752bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2753bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2754bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2755bd041c0cSMatthew G. Knepley       Vec                pointVec;
2756bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2757bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2758bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2759bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2760bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2761bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2762bd041c0cSMatthew G. Knepley 
2763bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2764bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2765bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2766bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2767bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2768bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2769bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2770c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2771c330f8ffSToby Isaac 
2772bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2773c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2774bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2775bd041c0cSMatthew G. Knepley       }
2776bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2777bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2778bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2779bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2780bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2781bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2782bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2783bd041c0cSMatthew G. Knepley       {
2784e8f14785SLisandro Dalcin         PetscHashIJKey key;
2785e8f14785SLisandro Dalcin         PetscBool      missing;
2786bd041c0cSMatthew G. Knepley 
2787bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2788e8f14785SLisandro Dalcin           key.i = findices[i];
2789e8f14785SLisandro Dalcin           if (key.i >= 0) {
2790bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2791bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2792bd041c0cSMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2793bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2794e8f14785SLisandro Dalcin                 key.j = cindices[c];
2795e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2796e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2797bd041c0cSMatthew G. Knepley                 if (missing) {
2798e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2799e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2800bd041c0cSMatthew G. Knepley                 }
2801bd041c0cSMatthew G. Knepley               }
2802bd041c0cSMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2803bd041c0cSMatthew G. Knepley             }
2804bd041c0cSMatthew G. Knepley           }
2805bd041c0cSMatthew G. Knepley         }
2806bd041c0cSMatthew G. Knepley       }
2807bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2808bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2809bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2810bd041c0cSMatthew G. Knepley     }
2811bd041c0cSMatthew G. Knepley   }
2812e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
2813bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2814bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2815bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2816bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2817bd041c0cSMatthew G. Knepley     PetscObject      obj;
2818bd041c0cSMatthew G. Knepley     PetscClassId     id;
2819bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2820bd041c0cSMatthew G. Knepley     PetscReal       *Bfine;
2821bd041c0cSMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2822bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2823bd041c0cSMatthew G. Knepley 
2824bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2825bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2826bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);}
2827bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2828bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2829bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2830bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2831bd041c0cSMatthew G. Knepley       Vec                pointVec;
2832bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2833bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2834bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2835bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2836bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2837bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2838bd041c0cSMatthew G. Knepley 
2839bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2840bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2841bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2842bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2843bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2844bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2845bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2846c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2847c330f8ffSToby Isaac 
2848bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2849c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2850bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2851bd041c0cSMatthew G. Knepley       }
2852bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2853bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2854bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2855bd041c0cSMatthew G. Knepley       /* Update matrix */
2856bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2857bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2858bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2859bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2860bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2861c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2862c330f8ffSToby Isaac 
2863bd041c0cSMatthew G. Knepley 
2864bd041c0cSMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2865bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2866bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2867bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2868c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
2869bd041c0cSMatthew G. Knepley 
2870bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2871bd041c0cSMatthew G. Knepley           PetscFE    fe = (PetscFE) obj;
2872bd041c0cSMatthew G. Knepley           PetscReal *B;
2873bd041c0cSMatthew G. Knepley 
2874bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2875bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2876bd041c0cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2877bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2878bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2879580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
2880bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2881bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*Bfine[(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ;
2882bd041c0cSMatthew G. Knepley             }
2883bd041c0cSMatthew G. Knepley             /* Update interpolator */
2884bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2885bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2886bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2887bd041c0cSMatthew G. Knepley           }
2888bd041c0cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2889bd041c0cSMatthew G. Knepley         } else {
2890bd041c0cSMatthew G. Knepley           cpdim = 1;
2891bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2892580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
2893bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2894bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2895bd041c0cSMatthew G. Knepley             }
2896bd041c0cSMatthew G. Knepley             /* Update interpolator */
2897bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2898ff1e0c32SBarry Smith             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %D %D Nf: %D %D Nc: %D %D\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
2899bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2900bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2901bd041c0cSMatthew G. Knepley           }
2902bd041c0cSMatthew G. Knepley         }
2903bd041c0cSMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2904bd041c0cSMatthew G. Knepley       }
2905bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2906bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2907bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2908bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2909bd041c0cSMatthew G. Knepley     }
2910bd041c0cSMatthew G. Knepley   }
2911bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
2912bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
2913bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2914bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2915bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2916bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
2917bd041c0cSMatthew G. Knepley }
2918bd041c0cSMatthew G. Knepley 
2919bd041c0cSMatthew G. Knepley /*@
292046fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
292146fa42a0SMatthew G. Knepley 
292246fa42a0SMatthew G. Knepley   Input Parameters:
292346fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
292446fa42a0SMatthew G. Knepley - dmf  - The fine mesh
292546fa42a0SMatthew G. Knepley - user - The user context
292646fa42a0SMatthew G. Knepley 
292746fa42a0SMatthew G. Knepley   Output Parameter:
292846fa42a0SMatthew G. Knepley . sc   - The mapping
292946fa42a0SMatthew G. Knepley 
293046fa42a0SMatthew G. Knepley   Level: developer
293146fa42a0SMatthew G. Knepley 
293246fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
293346fa42a0SMatthew G. Knepley @*/
29347c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
29357c927364SMatthew G. Knepley {
2936e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
29377c927364SMatthew G. Knepley   PetscFE       *feRef;
293897c42addSMatthew G. Knepley   PetscFV       *fvRef;
29397c927364SMatthew G. Knepley   Vec            fv, cv;
29407c927364SMatthew G. Knepley   IS             fis, cis;
29417c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
29427c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
29430bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
29446f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
29457c927364SMatthew G. Knepley   PetscErrorCode ierr;
29467c927364SMatthew G. Knepley 
29477c927364SMatthew G. Knepley   PetscFunctionBegin;
294875a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2949c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
295092fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2951e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
295292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2953e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
29547c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
29557c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
29569ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
29579ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2958e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
29596f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
29607c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
296197c42addSMatthew G. Knepley     PetscObject  obj;
296297c42addSMatthew G. Knepley     PetscClassId id;
2963aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
29647c927364SMatthew G. Knepley 
296597c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
296697c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
296797c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
296897c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
29696f3d3cbcSMatthew G. Knepley       PetscSpace sp;
29709b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
297197c42addSMatthew G. Knepley 
29727c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
29737c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
29747c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
29756f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
29769b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
29779b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
297897c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
297997c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
298097c42addSMatthew G. Knepley       PetscDualSpace Q;
298197c42addSMatthew G. Knepley 
298297c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
298397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
298497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
298597c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
29866f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
298797c42addSMatthew G. Knepley     }
2988d172c84bSMatthew G. Knepley     fTotDim += fNb;
29897c927364SMatthew G. Knepley   }
2990e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
29917c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
29927c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
29937c927364SMatthew G. Knepley     PetscFE        feC;
299497c42addSMatthew G. Knepley     PetscFV        fvC;
29957c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
2996d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
29977c927364SMatthew G. Knepley 
299897c42addSMatthew G. Knepley     if (feRef[field]) {
2999e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
30007c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
30017c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
30027c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
3003d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
30047c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
30057c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
30067c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
300797c42addSMatthew G. Knepley     } else {
300897c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
300997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
301097c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
301197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
301297c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
301397c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
301497c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
301597c42addSMatthew G. Knepley     }
3016ff1e0c32SBarry 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);
30177c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
30187c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
3019d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
3020d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
302197c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
30227c927364SMatthew G. Knepley 
30237c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
3024d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
3025ff1e0c32SBarry 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);
302697c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
30277c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
30287c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
3029d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
30307c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
3031d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
30327c927364SMatthew G. Knepley 
30337c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
3034d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
3035ff1e0c32SBarry 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);
30367c927364SMatthew G. Knepley         if (NpC != NpF) continue;
30377c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
30387c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
3039d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
3040d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
3041d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
304297c42addSMatthew G. Knepley         found = PETSC_TRUE;
30437c927364SMatthew G. Knepley         break;
30447c927364SMatthew G. Knepley       }
304597c42addSMatthew G. Knepley       if (!found) {
304697c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
3047d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
3048d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
304997c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
305097c42addSMatthew G. Knepley       }
30517c927364SMatthew G. Knepley     }
3052d172c84bSMatthew G. Knepley     offsetC += cpdim;
3053d172c84bSMatthew G. Knepley     offsetF += fpdim;
30547c927364SMatthew G. Knepley   }
305597c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
30566f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
30577c927364SMatthew G. Knepley 
30587c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
30597c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
30600bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
30617c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
30627c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
3063aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
3064aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
30657c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
30667c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
30677c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
30687c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
30690bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
3070ff1e0c32SBarry 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]]);
30717c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
30727c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
30737c927364SMatthew G. Knepley     }
30747c927364SMatthew G. Knepley   }
30757c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
30767c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
30777c927364SMatthew G. Knepley 
30787c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
30797c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
30809448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
30817c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
30827c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
30837c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
30847c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
308575a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3086cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
3087cb1e1211SMatthew G Knepley }
3088a1cf66bbSMatthew G. Knepley 
30892f856554SMatthew G. Knepley /*@C
30902f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
30912f856554SMatthew G. Knepley 
30922f856554SMatthew G. Knepley   Input Parameters:
30932f856554SMatthew G. Knepley + dm     - The DM
30942f856554SMatthew G. Knepley . cellIS - The cells to include
30952f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
30962f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
30972f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
30982f856554SMatthew G. Knepley 
30992f856554SMatthew G. Knepley   Output Parameters:
31002f856554SMatthew G. Knepley + u   - The field coefficients
31012f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
31022f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
31032f856554SMatthew G. Knepley 
31042f856554SMatthew G. Knepley   Level: developer
31052f856554SMatthew G. Knepley 
31062f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
31072f856554SMatthew G. Knepley @*/
31082f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
31092f856554SMatthew G. Knepley {
31102f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
31112f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
31122f856554SMatthew G. Knepley   PetscDS         prob;
31132f856554SMatthew G. Knepley   const PetscInt *cells;
31142f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
31152f856554SMatthew G. Knepley   PetscErrorCode  ierr;
31162f856554SMatthew G. Knepley 
31172f856554SMatthew G. Knepley   PetscFunctionBegin;
31182f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31192f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
31202f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
31212f856554SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
31222f856554SMatthew G. Knepley   PetscValidPointer(u, 7);
31232f856554SMatthew G. Knepley   PetscValidPointer(u_t, 8);
31242f856554SMatthew G. Knepley   PetscValidPointer(a, 9);
31252f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
31262f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
312792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
31282f856554SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr);
31292f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
31302f856554SMatthew G. Knepley   if (locA) {
31312f856554SMatthew G. Knepley     DM      dmAux;
31322f856554SMatthew G. Knepley     PetscDS probAux;
31332f856554SMatthew G. Knepley 
31342f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
31352f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
313692fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
31372f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
31382f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
31392f856554SMatthew G. Knepley   }
31402f856554SMatthew G. Knepley   numCells = cEnd - cStart;
31412f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
31422f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
31432f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
31442f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
31452f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
31462f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
31472f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
31482f856554SMatthew G. Knepley     PetscInt       i;
31492f856554SMatthew G. Knepley 
31502f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
31512f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
31522f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
31532f856554SMatthew G. Knepley     if (locX_t) {
31542f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
31552f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
31562f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
31572f856554SMatthew G. Knepley     }
31582f856554SMatthew G. Knepley     if (locA) {
31592f856554SMatthew G. Knepley       PetscInt subcell;
31602f856554SMatthew G. Knepley       ierr = DMPlexGetAuxiliaryPoint(plex, plexA, cell, &subcell);CHKERRQ(ierr);
31612f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
31622f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
31632f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
31642f856554SMatthew G. Knepley     }
31652f856554SMatthew G. Knepley   }
31662f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
31672f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
31682f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
31692f856554SMatthew G. Knepley   PetscFunctionReturn(0);
31702f856554SMatthew G. Knepley }
31712f856554SMatthew G. Knepley 
31722f856554SMatthew G. Knepley /*@C
31732f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
31742f856554SMatthew G. Knepley 
31752f856554SMatthew G. Knepley   Input Parameters:
31762f856554SMatthew G. Knepley + dm     - The DM
31772f856554SMatthew G. Knepley . cellIS - The cells to include
31782f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
31792f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
31802f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
31812f856554SMatthew G. Knepley 
31822f856554SMatthew G. Knepley   Output Parameters:
31832f856554SMatthew G. Knepley + u   - The field coefficients
31842f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
31852f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
31862f856554SMatthew G. Knepley 
31872f856554SMatthew G. Knepley   Level: developer
31882f856554SMatthew G. Knepley 
31892f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
31902f856554SMatthew G. Knepley @*/
31912f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
31922f856554SMatthew G. Knepley {
31932f856554SMatthew G. Knepley   PetscErrorCode ierr;
31942f856554SMatthew G. Knepley 
31952f856554SMatthew G. Knepley   PetscFunctionBegin;
31962f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
31972f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
31982f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
31992f856554SMatthew G. Knepley   PetscFunctionReturn(0);
32002f856554SMatthew G. Knepley }
32012f856554SMatthew G. Knepley 
32022f856554SMatthew G. Knepley /*@C
32032f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
32042f856554SMatthew G. Knepley 
32052f856554SMatthew G. Knepley   Input Parameters:
32062f856554SMatthew G. Knepley + dm     - The DM
32072f856554SMatthew G. Knepley . fStart - The first face to include
32082f856554SMatthew G. Knepley . fEnd   - The first face to exclude
32092f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
32102f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
32112f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
32122f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
32132f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
32142f856554SMatthew G. Knepley 
32152f856554SMatthew G. Knepley   Output Parameters:
32162f856554SMatthew G. Knepley + Nface - The number of faces with field values
32172f856554SMatthew G. Knepley . uL - The field values at the left side of the face
32182f856554SMatthew G. Knepley - uR - The field values at the right side of the face
32192f856554SMatthew G. Knepley 
32202f856554SMatthew G. Knepley   Level: developer
32212f856554SMatthew G. Knepley 
32222f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
32232f856554SMatthew G. Knepley @*/
32242f856554SMatthew 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)
32252f856554SMatthew G. Knepley {
32262f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
32272f856554SMatthew G. Knepley   PetscSection       section;
32282f856554SMatthew G. Knepley   PetscDS            prob;
32292f856554SMatthew G. Knepley   DMLabel            ghostLabel;
32302f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
32312f856554SMatthew G. Knepley   PetscBool         *isFE;
32322f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
32332f856554SMatthew G. Knepley   PetscErrorCode     ierr;
32342f856554SMatthew G. Knepley 
32352f856554SMatthew G. Knepley   PetscFunctionBegin;
32362f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32372f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
32382f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
32392f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
32402f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
32412f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
32422f856554SMatthew G. Knepley   PetscValidPointer(uL, 9);
32432f856554SMatthew G. Knepley   PetscValidPointer(uR, 10);
32442f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
32452f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
324692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
32472f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
32482f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
32492f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
32502f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
32512f856554SMatthew G. Knepley     PetscObject  obj;
32522f856554SMatthew G. Knepley     PetscClassId id;
32532f856554SMatthew G. Knepley 
32542f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
32552f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
32562f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
32572f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
32582f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
32592f856554SMatthew G. Knepley   }
32602f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
32612f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
32622f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
32632f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
32642f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
32652f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
32662f856554SMatthew G. Knepley   if (locGrad) {
32672f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
32682f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
32692f856554SMatthew G. Knepley   }
32702f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
32712f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
32722f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
32732f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
32742f856554SMatthew G. Knepley     const PetscInt        *cells;
32752f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
32762f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
32772f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
32782f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
32792f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
32802f856554SMatthew G. Knepley 
32812f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
32822f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
32832f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
32842f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
32852f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
32862f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
32872f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
32882f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
32892f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
32902f856554SMatthew G. Knepley       PetscInt off;
32912f856554SMatthew G. Knepley 
32922f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
32932f856554SMatthew G. Knepley       if (isFE[f]) {
32942f856554SMatthew G. Knepley         const PetscInt *cone;
32952f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
32962f856554SMatthew G. Knepley 
32972f856554SMatthew G. Knepley         xL = xR = NULL;
32982f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
32992f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
33002f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
33012f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
33022f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
33032f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
33042f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
33052f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
33062f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
3307ff1e0c32SBarry 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]);
33082f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
33092f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
33102f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
3311a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
3312a8f1f9e5SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
33132f856554SMatthew G. Knepley           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
33142f856554SMatthew G. Knepley         }
33152f856554SMatthew G. Knepley         else {
3316a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
33172f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
33182f856554SMatthew G. Knepley           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
33192f856554SMatthew G. Knepley         }
33202f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
33212f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
33222f856554SMatthew G. Knepley       } else {
33232f856554SMatthew G. Knepley         PetscFV  fv;
33242f856554SMatthew G. Knepley         PetscInt numComp, c;
33252f856554SMatthew G. Knepley 
33262f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
33272f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
33282f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
33292f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
33302f856554SMatthew G. Knepley         if (dmGrad) {
33312f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
33322f856554SMatthew G. Knepley 
33332f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
33342f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
33352f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
33362f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
33372f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
33382f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
33392f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
33402f856554SMatthew G. Knepley           }
33412f856554SMatthew G. Knepley         } else {
33422f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
33432f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
33442f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
33452f856554SMatthew G. Knepley           }
33462f856554SMatthew G. Knepley         }
33472f856554SMatthew G. Knepley       }
33482f856554SMatthew G. Knepley     }
33492f856554SMatthew G. Knepley     ++iface;
33502f856554SMatthew G. Knepley   }
33512f856554SMatthew G. Knepley   *Nface = iface;
33522f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
33532f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
33542f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
33552f856554SMatthew G. Knepley   if (locGrad) {
33562f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
33572f856554SMatthew G. Knepley   }
33582f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
33592f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33602f856554SMatthew G. Knepley }
33612f856554SMatthew G. Knepley 
33622f856554SMatthew G. Knepley /*@C
33632f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
33642f856554SMatthew G. Knepley 
33652f856554SMatthew G. Knepley   Input Parameters:
33662f856554SMatthew G. Knepley + dm     - The DM
33672f856554SMatthew G. Knepley . fStart - The first face to include
33682f856554SMatthew G. Knepley . fEnd   - The first face to exclude
33692f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
33702f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
33712f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
33722f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
33732f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
33742f856554SMatthew G. Knepley 
33752f856554SMatthew G. Knepley   Output Parameters:
33762f856554SMatthew G. Knepley + Nface - The number of faces with field values
33772f856554SMatthew G. Knepley . uL - The field values at the left side of the face
33782f856554SMatthew G. Knepley - uR - The field values at the right side of the face
33792f856554SMatthew G. Knepley 
33802f856554SMatthew G. Knepley   Level: developer
33812f856554SMatthew G. Knepley 
33822f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
33832f856554SMatthew G. Knepley @*/
33842f856554SMatthew 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)
33852f856554SMatthew G. Knepley {
33862f856554SMatthew G. Knepley   PetscErrorCode ierr;
33872f856554SMatthew G. Knepley 
33882f856554SMatthew G. Knepley   PetscFunctionBegin;
33892f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
33902f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
33912f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33922f856554SMatthew G. Knepley }
33932f856554SMatthew G. Knepley 
33942f856554SMatthew G. Knepley /*@C
33952f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
33962f856554SMatthew G. Knepley 
33972f856554SMatthew G. Knepley   Input Parameters:
33982f856554SMatthew G. Knepley + dm     - The DM
33992f856554SMatthew G. Knepley . fStart - The first face to include
34002f856554SMatthew G. Knepley . fEnd   - The first face to exclude
34012f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
34022f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
34032f856554SMatthew G. Knepley 
34042f856554SMatthew G. Knepley   Output Parameters:
34052f856554SMatthew G. Knepley + Nface - The number of faces with field values
34062f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
34072f856554SMatthew G. Knepley - vol   - The cell volume
34082f856554SMatthew G. Knepley 
34092f856554SMatthew G. Knepley   Level: developer
34102f856554SMatthew G. Knepley 
34112f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
34122f856554SMatthew G. Knepley @*/
34132f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
34142f856554SMatthew G. Knepley {
34152f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
34162f856554SMatthew G. Knepley   DMLabel            ghostLabel;
34172f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
34182f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
34192f856554SMatthew G. Knepley   PetscErrorCode     ierr;
34202f856554SMatthew G. Knepley 
34212f856554SMatthew G. Knepley   PetscFunctionBegin;
34222f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34232f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
34242f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
34252f856554SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
34262f856554SMatthew G. Knepley   PetscValidPointer(vol, 7);
34272f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34282f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
34292f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
34302f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
34312f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
34322f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
34332f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
34342f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
34352f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
34362f856554SMatthew G. Knepley     const PetscInt        *cells;
34372f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
34382f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
34392f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
34402f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
34412f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
34422f856554SMatthew G. Knepley 
34432f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
34442f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
34452f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
34462f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
34472f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
34482f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
34492f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
34502f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
34512f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
34522f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
34532f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
34542f856554SMatthew G. Knepley     }
34552f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
34562f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
34572f856554SMatthew G. Knepley     ++iface;
34582f856554SMatthew G. Knepley   }
34592f856554SMatthew G. Knepley   *Nface = iface;
34602f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
34612f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
34622f856554SMatthew G. Knepley   PetscFunctionReturn(0);
34632f856554SMatthew G. Knepley }
34642f856554SMatthew G. Knepley 
34652f856554SMatthew G. Knepley /*@C
34662f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
34672f856554SMatthew G. Knepley 
34682f856554SMatthew G. Knepley   Input Parameters:
34692f856554SMatthew G. Knepley + dm     - The DM
34702f856554SMatthew G. Knepley . fStart - The first face to include
34712f856554SMatthew G. Knepley . fEnd   - The first face to exclude
34722f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
34732f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
34742f856554SMatthew G. Knepley 
34752f856554SMatthew G. Knepley   Output Parameters:
34762f856554SMatthew G. Knepley + Nface - The number of faces with field values
34772f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
34782f856554SMatthew G. Knepley - vol   - The cell volume
34792f856554SMatthew G. Knepley 
34802f856554SMatthew G. Knepley   Level: developer
34812f856554SMatthew G. Knepley 
34822f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
34832f856554SMatthew G. Knepley @*/
34842f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
34852f856554SMatthew G. Knepley {
34862f856554SMatthew G. Knepley   PetscErrorCode ierr;
34872f856554SMatthew G. Knepley 
34882f856554SMatthew G. Knepley   PetscFunctionBegin;
34892f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
34902f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
34912f856554SMatthew G. Knepley   PetscFunctionReturn(0);
34922f856554SMatthew G. Knepley }
34932f856554SMatthew G. Knepley 
3494a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3495a1cf66bbSMatthew G. Knepley {
3496a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3497a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3498a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3499a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3500a1cf66bbSMatthew G. Knepley 
3501a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3502a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3503a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3504a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3505a1cf66bbSMatthew G. Knepley   if (container) {
3506a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3507a1cf66bbSMatthew G. Knepley   } else {
3508a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3509a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3510a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3511a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3512a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3513a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3514a1cf66bbSMatthew G. Knepley   }
3515a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3516a1cf66bbSMatthew G. Knepley }
3517a1cf66bbSMatthew G. Knepley 
3518a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3519a1cf66bbSMatthew G. Knepley {
3520a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3521a1cf66bbSMatthew G. Knepley   *geom = NULL;
3522a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3523a1cf66bbSMatthew G. Knepley }
3524a1cf66bbSMatthew G. Knepley 
352592d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
352692d50984SMatthew G. Knepley {
352792d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
352892d50984SMatthew G. Knepley   const char      *name       = "Residual";
352992d50984SMatthew G. Knepley   DM               dmAux      = NULL;
353092d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
353192d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
353292d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
353392d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
353492d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
353592d50984SMatthew G. Knepley   DMField          coordField = NULL;
3536c0006e53SPatrick Farrell   Vec              locA;
3537c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
353892d50984SMatthew G. Knepley   IS               chunkIS;
353992d50984SMatthew G. Knepley   const PetscInt  *cells;
354092d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3541364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
354292d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
354392d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
354492d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
354592d50984SMatthew G. Knepley   PetscErrorCode   ierr;
354692d50984SMatthew G. Knepley 
354792d50984SMatthew G. Knepley   PetscFunctionBegin;
354892d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
354992d50984SMatthew G. Knepley   /* FEM+FVM */
355092d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
355192d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
355292d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
355392d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
355492d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
355592d50984SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
355692d50984SMatthew G. Knepley   if (locA) {
355792d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
355892d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
355992d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
356092d50984SMatthew G. Knepley   }
356192d50984SMatthew G. Knepley   /* 2: Get geometric data */
356292d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
356392d50984SMatthew G. Knepley     PetscObject  obj;
356492d50984SMatthew G. Knepley     PetscClassId id;
356592d50984SMatthew G. Knepley     PetscBool    fimp;
356692d50984SMatthew G. Knepley 
356792d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
356892d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
356992d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
357092d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
357192d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
3572364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
357392d50984SMatthew G. Knepley   }
357492d50984SMatthew G. Knepley   if (useFEM) {
357592d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
357692d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
357792d50984SMatthew G. Knepley     if (maxDegree <= 1) {
357892d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
357992d50984SMatthew G. Knepley       if (affineQuad) {
358092d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
358192d50984SMatthew G. Knepley       }
358292d50984SMatthew G. Knepley     } else {
358392d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
358492d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
358592d50984SMatthew G. Knepley         PetscObject  obj;
358692d50984SMatthew G. Knepley         PetscClassId id;
358792d50984SMatthew G. Knepley         PetscBool    fimp;
358892d50984SMatthew G. Knepley 
358992d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
359092d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
359192d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
359292d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
359392d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
359492d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
359592d50984SMatthew G. Knepley 
359692d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
359792d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
359892d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
359992d50984SMatthew G. Knepley         }
360092d50984SMatthew G. Knepley       }
360192d50984SMatthew G. Knepley     }
360292d50984SMatthew G. Knepley   }
360392d50984SMatthew G. Knepley   /* Loop over chunks */
360492d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
360592d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
360692d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
360792d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
360892d50984SMatthew G. Knepley   numChunks     = 1;
360992d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
361092d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
361192d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
3612c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
3613c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
3614c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
361592d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
3616c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
361792d50984SMatthew G. Knepley 
361892d50984SMatthew G. Knepley     /* Extract field coefficients */
361992d50984SMatthew G. Knepley     if (useFEM) {
362092d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
362192d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
362292d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
3623580bdb30SBarry Smith       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
362492d50984SMatthew G. Knepley     }
362592d50984SMatthew 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 */
362692d50984SMatthew G. Knepley     /* Loop over fields */
362792d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
362892d50984SMatthew G. Knepley       PetscObject  obj;
362992d50984SMatthew G. Knepley       PetscClassId id;
363092d50984SMatthew G. Knepley       PetscBool    fimp;
363192d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
363292d50984SMatthew G. Knepley 
363392d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
363492d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
363592d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
363692d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
363792d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
363892d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
363992d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
364092d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
364192d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
364292d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
364392d50984SMatthew G. Knepley 
364492d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
364592d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
364692d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
364792d50984SMatthew G. Knepley         blockSize = Nb;
364892d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
364992d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
365092d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
365192d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
365292d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
365392d50984SMatthew G. Knepley         offset    = numCells - Nr;
365492d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
365592d50984SMatthew 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) */
365692d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
36574bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
365892d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
36594bee2e38SMatthew 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);
366092d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
366192d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
366292d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
366392d50984SMatthew G. Knepley 
366492d50984SMatthew G. Knepley         Ne = numFaces;
366592d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
366692d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
366792d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
3668ff1e0c32SBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
366992d50984SMatthew G. Knepley     }
367092d50984SMatthew G. Knepley     /* Loop over domain */
367192d50984SMatthew G. Knepley     if (useFEM) {
367292d50984SMatthew G. Knepley       /* Add elemVec to locX */
367392d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
367492d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
367592d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
367692d50984SMatthew G. Knepley 
367792d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
367892d50984SMatthew G. Knepley         if (ghostLabel) {
367992d50984SMatthew G. Knepley           PetscInt ghostVal;
368092d50984SMatthew G. Knepley 
368192d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
368292d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
368392d50984SMatthew G. Knepley         }
368492d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
368592d50984SMatthew G. Knepley       }
368692d50984SMatthew G. Knepley     }
368792d50984SMatthew G. Knepley     /* Handle time derivative */
368892d50984SMatthew G. Knepley     if (locX_t) {
368992d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
369092d50984SMatthew G. Knepley 
369192d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
369292d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
369392d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
369492d50984SMatthew G. Knepley         PetscFV      fv;
369592d50984SMatthew G. Knepley         PetscObject  obj;
369692d50984SMatthew G. Knepley         PetscClassId id;
369792d50984SMatthew G. Knepley         PetscInt     pdim, d;
369892d50984SMatthew G. Knepley 
369992d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
370092d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
370192d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
370292d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
370392d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
370492d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
370592d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
370692d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
370792d50984SMatthew G. Knepley 
370892d50984SMatthew G. Knepley           if (ghostLabel) {
370992d50984SMatthew G. Knepley             PetscInt ghostVal;
371092d50984SMatthew G. Knepley 
371192d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
371292d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
371392d50984SMatthew G. Knepley           }
371492d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
371592d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
371692d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
371792d50984SMatthew G. Knepley         }
371892d50984SMatthew G. Knepley       }
371992d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
372092d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
372192d50984SMatthew G. Knepley     }
372292d50984SMatthew G. Knepley     if (useFEM) {
372392d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
372492d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
372592d50984SMatthew G. Knepley     }
372692d50984SMatthew G. Knepley   }
372792d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
372892d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
372992d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
373092d50984SMatthew G. Knepley   if (useFEM) {
373192d50984SMatthew G. Knepley     if (maxDegree <= 1) {
373292d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
373392d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
373492d50984SMatthew G. Knepley     } else {
373592d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
373692d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
373792d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
373892d50984SMatthew G. Knepley       }
373992d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
374092d50984SMatthew G. Knepley     }
374192d50984SMatthew G. Knepley   }
374292d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
374392d50984SMatthew G. Knepley   PetscFunctionReturn(0);
374492d50984SMatthew G. Knepley }
374592d50984SMatthew G. Knepley 
3746a1cf66bbSMatthew G. Knepley /*
3747a1cf66bbSMatthew 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
3748a1cf66bbSMatthew G. Knepley 
3749a1cf66bbSMatthew G. Knepley   X   - The local solution vector
3750a1cf66bbSMatthew G. Knepley   X_t - The local solution time derviative vector, or NULL
3751a1cf66bbSMatthew G. Knepley */
3752a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
3753a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
3754a1cf66bbSMatthew G. Knepley {
3755a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
3756a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
3757a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
3758a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
3759a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
3760a1cf66bbSMatthew G. Knepley   Vec              A;
3761a1cf66bbSMatthew G. Knepley   DMField          coordField;
3762a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
3763a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
3764a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
3765a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
37669b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
3767a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
37689b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
3769a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
3770a1cf66bbSMatthew G. Knepley 
3771a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3772a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3773a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
3774a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
3775a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3776a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3777a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3778a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3779a1cf66bbSMatthew G. Knepley   if (dmAux) {
378092fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
3781a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
3782a1cf66bbSMatthew G. Knepley   }
3783a1cf66bbSMatthew G. Knepley   /* Get flags */
3784a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
3785a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3786a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
3787a1cf66bbSMatthew G. Knepley     PetscObject  disc;
3788a1cf66bbSMatthew G. Knepley     PetscClassId id;
3789a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
3790a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
3791a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
3792a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
3793a1cf66bbSMatthew G. Knepley   }
3794a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
3795a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
3796a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
3797a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
3798a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
3799a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
3800a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
3801a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
3802a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
3803a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
3804a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
3805a1cf66bbSMatthew 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 */
3806a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3807a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3808a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3809a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
3810a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3811a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
3812a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
3813a1cf66bbSMatthew G. Knepley     PetscFE         fe;
3814a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
3815a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
3816a1cf66bbSMatthew G. Knepley 
3817a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3818a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
3819a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
3820a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
3821a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
3822a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
3823a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
3824a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
3825a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
3826a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
3827a1cf66bbSMatthew G. Knepley   } else {
3828a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
3829a1cf66bbSMatthew G. Knepley     numChunks = 1;
3830a1cf66bbSMatthew G. Knepley   }
3831a1cf66bbSMatthew G. Knepley   /* Get work space */
3832a1cf66bbSMatthew 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;
3833a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
3834580bdb30SBarry Smith   ierr = PetscArrayzero(work, wsz);CHKERRQ(ierr);
3835a1cf66bbSMatthew G. Knepley   off      = 0;
3836a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3837a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3838a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
3839a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3840a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3841a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3842a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
3843a1cf66bbSMatthew G. Knepley   /* Setup geometry */
3844a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
38459b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
38469b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
3847a1cf66bbSMatthew G. Knepley   if (!qGeom) {
3848a1cf66bbSMatthew G. Knepley     PetscFE fe;
3849a1cf66bbSMatthew G. Knepley 
3850a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3851a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
3852a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
3853a1cf66bbSMatthew G. Knepley   }
3854a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3855a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
3856a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
3857a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
3858a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
3859a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
3860a1cf66bbSMatthew G. Knepley     PetscInt         c;
3861a1cf66bbSMatthew G. Knepley 
3862a1cf66bbSMatthew G. Knepley     /* Extract values */
3863a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3864a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3865a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
3866a1cf66bbSMatthew G. Knepley       PetscInt       i;
3867a1cf66bbSMatthew G. Knepley 
3868a1cf66bbSMatthew G. Knepley       if (X) {
3869a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3870a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
3871a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3872a1cf66bbSMatthew G. Knepley       }
3873a1cf66bbSMatthew G. Knepley       if (X_t) {
3874a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3875a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
3876a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3877a1cf66bbSMatthew G. Knepley       }
3878a1cf66bbSMatthew G. Knepley       if (dmAux) {
3879a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3880a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
3881a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3882a1cf66bbSMatthew G. Knepley       }
3883a1cf66bbSMatthew G. Knepley     }
3884a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3885a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
3886a1cf66bbSMatthew G. Knepley       PetscFE fe;
3887a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
3888a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
38894bee2e38SMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN,     fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
38904bee2e38SMatthew 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);}
38914bee2e38SMatthew 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);}
3892a1cf66bbSMatthew G. Knepley       }
3893a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
3894a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
3895a1cf66bbSMatthew G. Knepley         PetscFV  fv;
3896a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
3897a1cf66bbSMatthew G. Knepley 
3898a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
3899a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
3900a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
3901a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
3902a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
3903a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
3904a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
3905a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
3906a1cf66bbSMatthew G. Knepley           }
3907a1cf66bbSMatthew G. Knepley         }
3908a1cf66bbSMatthew G. Knepley       }
3909a1cf66bbSMatthew G. Knepley     }
3910a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3911a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
3912a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
3913a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
3914a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3915a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3916a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
3917a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3918a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3919a1cf66bbSMatthew G. Knepley       }
3920a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
3921a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
3922a1cf66bbSMatthew G. Knepley     }
3923a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3924a1cf66bbSMatthew G. Knepley   }
3925a1cf66bbSMatthew G. Knepley   /* Cleanup */
3926a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3927a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
3928a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
3929a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3930a1cf66bbSMatthew 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);
3931a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
3932a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
3933a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
3934a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
3935a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3936a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3937a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3938a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3939a1cf66bbSMatthew G. Knepley }
3940