xref: /petsc/src/dm/impls/plex/plexfem.c (revision 2f856554da5058e6d321bf85f7e0f098625f6a9d)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3bfabdd78SPatrick Farrell #include <petscsnes.h>
4cb1e1211SMatthew G Knepley 
5e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h>
6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
7af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
8a0845e3aSMatthew G. Knepley 
9*2f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy)
10*2f856554SMatthew G. Knepley {
11*2f856554SMatthew G. Knepley   PetscBool      isPlex;
12*2f856554SMatthew G. Knepley   PetscErrorCode ierr;
13*2f856554SMatthew G. Knepley 
14*2f856554SMatthew G. Knepley   PetscFunctionBegin;
15*2f856554SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
16*2f856554SMatthew G. Knepley   if (isPlex) {
17*2f856554SMatthew G. Knepley     *plex = dm;
18*2f856554SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
19*2f856554SMatthew G. Knepley   } else {
20*2f856554SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
21*2f856554SMatthew G. Knepley     if (!*plex) {
22*2f856554SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
23*2f856554SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
24*2f856554SMatthew G. Knepley       if (copy) {
25*2f856554SMatthew G. Knepley         const char *comps[3] = {"A", "dmAux"};
26*2f856554SMatthew G. Knepley         PetscObject obj;
27*2f856554SMatthew G. Knepley         PetscInt    i;
28*2f856554SMatthew G. Knepley 
29*2f856554SMatthew G. Knepley         {
30*2f856554SMatthew G. Knepley           /* Run the subdomain hook (this will copy the DMSNES/DMTS) */
31*2f856554SMatthew G. Knepley           DMSubDomainHookLink link;
32*2f856554SMatthew G. Knepley           for (link = dm->subdomainhook; link; link = link->next) {
33*2f856554SMatthew G. Knepley             if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);}
34*2f856554SMatthew G. Knepley           }
35*2f856554SMatthew G. Knepley         }
36*2f856554SMatthew G. Knepley         for (i = 0; i < 3; i++) {
37*2f856554SMatthew G. Knepley           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
38*2f856554SMatthew G. Knepley           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
39*2f856554SMatthew G. Knepley         }
40*2f856554SMatthew G. Knepley       }
41*2f856554SMatthew G. Knepley     } else {
42*2f856554SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
43*2f856554SMatthew G. Knepley     }
44*2f856554SMatthew G. Knepley   }
45*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
46*2f856554SMatthew G. Knepley }
47*2f856554SMatthew G. Knepley 
489b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
499b6f715bSMatthew G. Knepley {
509b6f715bSMatthew G. Knepley   PetscFEGeom *geom = (PetscFEGeom *) ctx;
519b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
529b6f715bSMatthew G. Knepley 
539b6f715bSMatthew G. Knepley   PetscFunctionBegin;
549b6f715bSMatthew G. Knepley   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
559b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
569b6f715bSMatthew G. Knepley }
579b6f715bSMatthew G. Knepley 
589b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
599b6f715bSMatthew G. Knepley {
609b6f715bSMatthew G. Knepley   char            composeStr[33] = {0};
619b6f715bSMatthew G. Knepley   PetscObjectId   id;
629b6f715bSMatthew G. Knepley   PetscContainer  container;
639b6f715bSMatthew G. Knepley   PetscErrorCode  ierr;
649b6f715bSMatthew G. Knepley 
659b6f715bSMatthew G. Knepley   PetscFunctionBegin;
669b6f715bSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
679b6f715bSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr);
689b6f715bSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
699b6f715bSMatthew G. Knepley   if (container) {
709b6f715bSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
719b6f715bSMatthew G. Knepley   } else {
729b6f715bSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
739b6f715bSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
749b6f715bSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
759b6f715bSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
769b6f715bSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
779b6f715bSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
789b6f715bSMatthew G. Knepley   }
799b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
809b6f715bSMatthew G. Knepley }
819b6f715bSMatthew G. Knepley 
829b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
839b6f715bSMatthew G. Knepley {
849b6f715bSMatthew G. Knepley   PetscFunctionBegin;
859b6f715bSMatthew G. Knepley   *geom = NULL;
869b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
879b6f715bSMatthew G. Knepley }
889b6f715bSMatthew G. Knepley 
8946fa42a0SMatthew G. Knepley /*@
9046fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
9146fa42a0SMatthew G. Knepley 
9246fa42a0SMatthew G. Knepley   Not collective
9346fa42a0SMatthew G. Knepley 
9446fa42a0SMatthew G. Knepley   Input Arguments:
9546fa42a0SMatthew G. Knepley + dm   - the DM
9646fa42a0SMatthew G. Knepley - unit - The SI unit
9746fa42a0SMatthew G. Knepley 
9846fa42a0SMatthew G. Knepley   Output Argument:
9946fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
10046fa42a0SMatthew G. Knepley 
10146fa42a0SMatthew G. Knepley   Level: advanced
10246fa42a0SMatthew G. Knepley 
10346fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
10446fa42a0SMatthew G. Knepley @*/
105cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
106cb1e1211SMatthew G Knepley {
107cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
108cb1e1211SMatthew G Knepley 
109cb1e1211SMatthew G Knepley   PetscFunctionBegin;
110cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
111cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
112cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
113cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
114cb1e1211SMatthew G Knepley }
115cb1e1211SMatthew G Knepley 
11646fa42a0SMatthew G. Knepley /*@
11746fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
11846fa42a0SMatthew G. Knepley 
11946fa42a0SMatthew G. Knepley   Not collective
12046fa42a0SMatthew G. Knepley 
12146fa42a0SMatthew G. Knepley   Input Arguments:
12246fa42a0SMatthew G. Knepley + dm   - the DM
12346fa42a0SMatthew G. Knepley . unit - The SI unit
12446fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
12546fa42a0SMatthew G. Knepley 
12646fa42a0SMatthew G. Knepley   Level: advanced
12746fa42a0SMatthew G. Knepley 
12846fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
12946fa42a0SMatthew G. Knepley @*/
130cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
131cb1e1211SMatthew G Knepley {
132cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
133cb1e1211SMatthew G Knepley 
134cb1e1211SMatthew G Knepley   PetscFunctionBegin;
135cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
136cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
137cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
138cb1e1211SMatthew G Knepley }
139cb1e1211SMatthew G Knepley 
140d1828a1cSMatthew G. Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nf, PetscScalar *mode, void *ctx)
141026175e5SToby Isaac {
14212adca46SMatthew 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}}};
143026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
144ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
145026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
146026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
147026175e5SToby Isaac 
148ad917190SMatthew G. Knepley   PetscFunctionBegin;
149ad917190SMatthew G. Knepley   if (dim != dim2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %d does not match context dimension %d", dim, dim2);
150026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
151026175e5SToby Isaac   if (d < dim) {
15212adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
153ad917190SMatthew G. Knepley   } else {
154026175e5SToby Isaac     for (i = 0; i < dim; i++) {
155026175e5SToby Isaac       for (j = 0; j < dim; j++) {
15612adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
157026175e5SToby Isaac       }
158026175e5SToby Isaac     }
159026175e5SToby Isaac   }
160ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
161026175e5SToby Isaac }
162026175e5SToby Isaac 
163cc4e42d9SMartin Diehl /*@
16412adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
165cb1e1211SMatthew G Knepley 
166cb1e1211SMatthew G Knepley   Collective on DM
167cb1e1211SMatthew G Knepley 
168cb1e1211SMatthew G Knepley   Input Arguments:
169026175e5SToby Isaac . dm - the DM
170cb1e1211SMatthew G Knepley 
171cb1e1211SMatthew G Knepley   Output Argument:
172cb1e1211SMatthew G Knepley . sp - the null space
173cb1e1211SMatthew G Knepley 
17412adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
175cb1e1211SMatthew G Knepley 
176cb1e1211SMatthew G Knepley   Level: advanced
177cb1e1211SMatthew G Knepley 
17812adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
179cb1e1211SMatthew G Knepley @*/
180026175e5SToby Isaac PetscErrorCode DMPlexCreateRigidBody(DM dm, MatNullSpace *sp)
181cb1e1211SMatthew G Knepley {
182cb1e1211SMatthew G Knepley   MPI_Comm       comm;
183026175e5SToby Isaac   Vec            mode[6];
184026175e5SToby Isaac   PetscSection   section, globalSection;
185b247467aSMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, mmin, d, i, j;
186cb1e1211SMatthew G Knepley   PetscErrorCode ierr;
187cb1e1211SMatthew G Knepley 
188cb1e1211SMatthew G Knepley   PetscFunctionBegin;
189cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
190c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1919d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
192cb1e1211SMatthew G Knepley   if (dim == 1) {
193cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
194cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
195cb1e1211SMatthew G Knepley   }
196e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
197e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
198cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
199b247467aSMatthew G. Knepley   m    = (dim*(dim+1))/2;
200cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
201cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
202cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
203b247467aSMatthew G. Knepley   ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr);
204b247467aSMatthew G. Knepley   mmin = PetscMin(m, n);
205cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
206026175e5SToby Isaac   for (d = 0; d < m; d++) {
207330485fdSToby Isaac     PetscInt         ctx[2];
208d1828a1cSMatthew G. Knepley     PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
209026175e5SToby Isaac     void            *voidctx = (void *) (&ctx[0]);
210cb1e1211SMatthew G Knepley 
2119d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
212330485fdSToby Isaac     ctx[1] = d;
2130709b2feSToby Isaac     ierr = DMProjectFunction(dm, 0.0, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
214cb1e1211SMatthew G Knepley   }
215b247467aSMatthew G. Knepley   for (i = 0; i < PetscMin(dim, mmin); ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
216cb1e1211SMatthew G Knepley   /* Orthonormalize system */
217b247467aSMatthew G. Knepley   for (i = dim; i < mmin; ++i) {
218cb1e1211SMatthew G Knepley     PetscScalar dots[6];
219cb1e1211SMatthew G Knepley 
220cb1e1211SMatthew G Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
221cb1e1211SMatthew G Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
222cb1e1211SMatthew G Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
223cb1e1211SMatthew G Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
224cb1e1211SMatthew G Knepley   }
225b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
226b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
227cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
228cb1e1211SMatthew G Knepley }
229cb1e1211SMatthew G Knepley 
230cc4e42d9SMartin Diehl /*@
23112adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
23212adca46SMatthew G. Knepley 
23312adca46SMatthew G. Knepley   Collective on DM
23412adca46SMatthew G. Knepley 
23512adca46SMatthew G. Knepley   Input Arguments:
23612adca46SMatthew G. Knepley + dm    - the DM
23712adca46SMatthew G. Knepley . nb    - The number of bodies
23812adca46SMatthew G. Knepley . label - The DMLabel marking each domain
23912adca46SMatthew G. Knepley . nids  - The number of ids per body
24012adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
24112adca46SMatthew G. Knepley 
24212adca46SMatthew G. Knepley   Output Argument:
24312adca46SMatthew G. Knepley . sp - the null space
24412adca46SMatthew G. Knepley 
24512adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
24612adca46SMatthew G. Knepley 
24712adca46SMatthew G. Knepley   Level: advanced
24812adca46SMatthew G. Knepley 
24912adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
25012adca46SMatthew G. Knepley @*/
25112adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
25212adca46SMatthew G. Knepley {
25312adca46SMatthew G. Knepley   MPI_Comm       comm;
25412adca46SMatthew G. Knepley   PetscSection   section, globalSection;
25512adca46SMatthew G. Knepley   Vec           *mode;
25612adca46SMatthew G. Knepley   PetscScalar   *dots;
25712adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
25812adca46SMatthew G. Knepley   PetscErrorCode ierr;
25912adca46SMatthew G. Knepley 
26012adca46SMatthew G. Knepley   PetscFunctionBegin;
26112adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
26212adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
26312adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
264e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
265e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
26612adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
26712adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
26812adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
26912adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
27012adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
27112adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
27212adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
27312adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
27412adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
27512adca46SMatthew G. Knepley       PetscInt         ctx[2];
27612adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
27712adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
27812adca46SMatthew G. Knepley 
27912adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
28012adca46SMatthew G. Knepley       ctx[1] = d;
28112adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
28212adca46SMatthew G. Knepley       off   += nids[b];
28312adca46SMatthew G. Knepley     }
28412adca46SMatthew G. Knepley   }
28512adca46SMatthew G. Knepley   for (i = 0; i < dim; ++i) {ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);}
28612adca46SMatthew G. Knepley   /* Orthonormalize system */
28712adca46SMatthew G. Knepley   for (i = 0; i < m; ++i) {
28812adca46SMatthew G. Knepley     ierr = VecMDot(mode[i], i, mode, dots);CHKERRQ(ierr);
28912adca46SMatthew G. Knepley     for (j = 0; j < i; ++j) dots[j] *= -1.0;
29012adca46SMatthew G. Knepley     ierr = VecMAXPY(mode[i], i, dots, mode);CHKERRQ(ierr);
29112adca46SMatthew G. Knepley     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
29212adca46SMatthew G. Knepley   }
29312adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
29412adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
29512adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
29612adca46SMatthew G. Knepley   PetscFunctionReturn(0);
29712adca46SMatthew G. Knepley }
29812adca46SMatthew G. Knepley 
299b29cfa1cSToby Isaac /*@
300b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
301b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
302b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
303b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
304b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
305b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
306b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
307b29cfa1cSToby Isaac 
308b29cfa1cSToby Isaac   Input Parameters:
309b29cfa1cSToby Isaac + dm - the DMPlex object
310b29cfa1cSToby Isaac - height - the maximum projection height >= 0
311b29cfa1cSToby Isaac 
312b29cfa1cSToby Isaac   Level: advanced
313b29cfa1cSToby Isaac 
3144d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
315b29cfa1cSToby Isaac @*/
316b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
317b29cfa1cSToby Isaac {
318b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
319b29cfa1cSToby Isaac 
320b29cfa1cSToby Isaac   PetscFunctionBegin;
321b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
322b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
323b29cfa1cSToby Isaac   PetscFunctionReturn(0);
324b29cfa1cSToby Isaac }
325b29cfa1cSToby Isaac 
326b29cfa1cSToby Isaac /*@
327b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
328b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
329b29cfa1cSToby Isaac 
330b29cfa1cSToby Isaac   Input Parameters:
331b29cfa1cSToby Isaac . dm - the DMPlex object
332b29cfa1cSToby Isaac 
333b29cfa1cSToby Isaac   Output Parameters:
334b29cfa1cSToby Isaac . height - the maximum projection height
335b29cfa1cSToby Isaac 
336b29cfa1cSToby Isaac   Level: intermediate
337b29cfa1cSToby Isaac 
3384d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
339b29cfa1cSToby Isaac @*/
340b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
341b29cfa1cSToby Isaac {
342b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
343b29cfa1cSToby Isaac 
344b29cfa1cSToby Isaac   PetscFunctionBegin;
345b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
346b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
347b29cfa1cSToby Isaac   PetscFunctionReturn(0);
348b29cfa1cSToby Isaac }
349b29cfa1cSToby Isaac 
350b278463cSMatthew G. Knepley /*@C
351b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector
352b278463cSMatthew G. Knepley 
353b278463cSMatthew G. Knepley   Input Parameters:
354b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
355b278463cSMatthew G. Knepley . time   - The time
356b278463cSMatthew G. Knepley . field  - The field to constrain
3571c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3581c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
359b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
360b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
361b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
362b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
363b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
364b278463cSMatthew G. Knepley 
365b278463cSMatthew G. Knepley   Output Parameter:
366b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
367b278463cSMatthew G. Knepley 
368b278463cSMatthew G. Knepley   Level: developer
369b278463cSMatthew G. Knepley 
370b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
371b278463cSMatthew G. Knepley @*/
3721c531cf8SMatthew 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)
37355f2e967SMatthew G. Knepley {
3740163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
37555f2e967SMatthew G. Knepley   void            **ctxs;
376d7ddef95SMatthew G. Knepley   PetscInt          numFields;
377d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
378d7ddef95SMatthew G. Knepley 
379d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
380d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
381d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
382d7ddef95SMatthew G. Knepley   funcs[field] = func;
383d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
3841c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
385d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
386d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
387d7ddef95SMatthew G. Knepley }
388d7ddef95SMatthew G. Knepley 
389b278463cSMatthew G. Knepley /*@C
390b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector
391b278463cSMatthew G. Knepley 
392b278463cSMatthew G. Knepley   Input Parameters:
393b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
394b278463cSMatthew G. Knepley . time   - The time
395b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
396b278463cSMatthew G. Knepley . field  - The field to constrain
3971c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
3981c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
399b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
400b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
401b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
402b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
403b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
404b278463cSMatthew G. Knepley 
405b278463cSMatthew G. Knepley   Output Parameter:
406b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
407b278463cSMatthew G. Knepley 
408b278463cSMatthew G. Knepley   Level: developer
409b278463cSMatthew G. Knepley 
410b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary()
411b278463cSMatthew G. Knepley @*/
4121c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
413c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
414c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
415c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
41697b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
417b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
418b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
419c60e475cSMatthew G. Knepley {
420c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
421c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
422c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
42397b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
424c60e475cSMatthew G. Knepley   void            **ctxs;
425c60e475cSMatthew G. Knepley   PetscInt          numFields;
426c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
427c60e475cSMatthew G. Knepley 
428c60e475cSMatthew G. Knepley   PetscFunctionBegin;
429c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
430c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
431c60e475cSMatthew G. Knepley   funcs[field] = func;
432c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
4331c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
434c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
435c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
436c60e475cSMatthew G. Knepley }
437c60e475cSMatthew G. Knepley 
438b278463cSMatthew G. Knepley /*@C
439b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
440b278463cSMatthew G. Knepley 
441b278463cSMatthew G. Knepley   Input Parameters:
442b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
443b278463cSMatthew G. Knepley . time   - The time
444b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
445b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
446b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
447b278463cSMatthew G. Knepley . field  - The field to constrain
4481c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
4491c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
450b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
451b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
452b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
453b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
454b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
455b278463cSMatthew G. Knepley 
456b278463cSMatthew G. Knepley   Output Parameter:
457b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
458b278463cSMatthew G. Knepley 
459b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
460b278463cSMatthew G. Knepley 
461b278463cSMatthew G. Knepley   Level: developer
462b278463cSMatthew G. Knepley 
463b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
464b278463cSMatthew G. Knepley @*/
4651c531cf8SMatthew 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[],
466b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
467d7ddef95SMatthew G. Knepley {
46861f58d28SMatthew G. Knepley   PetscDS            prob;
469da97024aSMatthew G. Knepley   PetscSF            sf;
470d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
47120369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
472da97024aSMatthew G. Knepley   const PetscInt    *leaves;
473d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
474520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
475e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
476d7ddef95SMatthew G. Knepley 
477d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
478da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
479da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
480da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
481d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
482d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
48361f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
484d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
485d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
48620369375SToby Isaac   if (cellGeometry) {
487d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
488d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
48920369375SToby Isaac   }
490d7ddef95SMatthew G. Knepley   if (Grad) {
491c0a6632aSMatthew G. Knepley     PetscFV fv;
492c0a6632aSMatthew G. Knepley 
493c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
494d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
495d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
496d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
49769291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
498d7ddef95SMatthew G. Knepley   }
499d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
500d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
501d7ddef95SMatthew G. Knepley     IS              faceIS;
502d7ddef95SMatthew G. Knepley     const PetscInt *faces;
503d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
504d7ddef95SMatthew G. Knepley 
505d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
506d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
507d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
508d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
509d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
510d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
511640bce14SSatish Balay       PetscFVFaceGeom        *fg;
512d7ddef95SMatthew G. Knepley 
513d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
514da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
515da97024aSMatthew G. Knepley       if (loc >= 0) continue;
516d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
517d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
518d7ddef95SMatthew G. Knepley       if (Grad) {
519640bce14SSatish Balay         PetscFVCellGeom       *cg;
520640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
521d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
522d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
523d7ddef95SMatthew G. Knepley         PetscInt               d;
524d7ddef95SMatthew G. Knepley 
525d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
526d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
527d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
528520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
529d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
530d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
531e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
532e735a8a9SMatthew G. Knepley         if (ierru) {
533e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
534e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
535e735a8a9SMatthew G. Knepley           goto cleanup;
536e735a8a9SMatthew G. Knepley         }
537d7ddef95SMatthew G. Knepley       } else {
538640bce14SSatish Balay         PetscScalar       *xI;
539d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
540d7ddef95SMatthew G. Knepley 
541d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
542520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
543e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
544e735a8a9SMatthew G. Knepley         if (ierru) {
545e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
546e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
547e735a8a9SMatthew G. Knepley           goto cleanup;
548e735a8a9SMatthew G. Knepley         }
549d7ddef95SMatthew G. Knepley       }
550d7ddef95SMatthew G. Knepley     }
551d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
552d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
553d7ddef95SMatthew G. Knepley   }
554e735a8a9SMatthew G. Knepley   cleanup:
555d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
556d7ddef95SMatthew G. Knepley   if (Grad) {
55769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
558d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
559d7ddef95SMatthew G. Knepley   }
560e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
561d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
562e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
563d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
564d7ddef95SMatthew G. Knepley }
565d7ddef95SMatthew G. Knepley 
566f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
567d7ddef95SMatthew G. Knepley {
568e5e52638SMatthew G. Knepley   PetscDS        prob;
569d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
57055f2e967SMatthew G. Knepley   PetscErrorCode ierr;
57155f2e967SMatthew G. Knepley 
57255f2e967SMatthew G. Knepley   PetscFunctionBegin;
573e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
574e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
57555f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
576f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
577d7ddef95SMatthew G. Knepley     const char             *labelname;
578d7ddef95SMatthew G. Knepley     DMLabel                 label;
5791c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
5801c531cf8SMatthew G. Knepley     const PetscInt         *comps;
581d7ddef95SMatthew G. Knepley     PetscObject             obj;
582d7ddef95SMatthew G. Knepley     PetscClassId            id;
583a30ec4eaSSatish Balay     void                    (*func)(void);
584d7ddef95SMatthew G. Knepley     PetscInt                numids;
585d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
58655f2e967SMatthew G. Knepley     void                   *ctx;
58755f2e967SMatthew G. Knepley 
5881c531cf8SMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, NULL, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
589f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
590c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
59144a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
592d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
593d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
594c60e475cSMatthew G. Knepley       switch (type) {
595c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
596c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
597092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
5981c531cf8SMatthew 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);
599092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
600c60e475cSMatthew G. Knepley         break;
601c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
602c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
6031c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
604b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
605c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
60697b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
607c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
608c60e475cSMatthew G. Knepley         break;
609c60e475cSMatthew G. Knepley       default: break;
610c60e475cSMatthew G. Knepley       }
611d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
61243ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
6131c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
614b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
615d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
61655f2e967SMatthew G. Knepley   }
61755f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
61855f2e967SMatthew G. Knepley }
61955f2e967SMatthew G. Knepley 
620f1d73a7aSMatthew G. Knepley /*@
621f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
622f1d73a7aSMatthew G. Knepley 
623f1d73a7aSMatthew G. Knepley   Input Parameters:
624f1d73a7aSMatthew G. Knepley + dm - The DM
625f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
626f1d73a7aSMatthew G. Knepley . time - The time
627f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
628f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
629f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
630f1d73a7aSMatthew G. Knepley 
631f1d73a7aSMatthew G. Knepley   Output Parameters:
632f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
633f1d73a7aSMatthew G. Knepley 
634f1d73a7aSMatthew G. Knepley   Level: developer
635f1d73a7aSMatthew G. Knepley 
636f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
637f1d73a7aSMatthew G. Knepley @*/
638f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
639f1d73a7aSMatthew G. Knepley {
640f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
641f1d73a7aSMatthew G. Knepley 
642f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
643f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
644f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
645f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
646f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
647f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
648f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
649f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
650f1d73a7aSMatthew G. Knepley }
651f1d73a7aSMatthew G. Knepley 
6520709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
653cb1e1211SMatthew G Knepley {
654574a98acSMatthew G. Knepley   Vec              localX;
655574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
656574a98acSMatthew G. Knepley 
657574a98acSMatthew G. Knepley   PetscFunctionBegin;
658574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
6595d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
660574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
661574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
662574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
663574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
664574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
665574a98acSMatthew G. Knepley }
666574a98acSMatthew G. Knepley 
667574a98acSMatthew G. Knepley /*@C
668574a98acSMatthew G. Knepley   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
669574a98acSMatthew G. Knepley 
670574a98acSMatthew G. Knepley   Input Parameters:
671574a98acSMatthew G. Knepley + dm     - The DM
672574a98acSMatthew G. Knepley . time   - The time
673574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
674574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
675574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
676574a98acSMatthew G. Knepley 
677574a98acSMatthew G. Knepley   Output Parameter:
678574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
679574a98acSMatthew G. Knepley 
680574a98acSMatthew G. Knepley   Level: developer
681574a98acSMatthew G. Knepley 
682574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
683574a98acSMatthew G. Knepley @*/
684574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
685574a98acSMatthew G. Knepley {
6860f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
687cb1e1211SMatthew G Knepley   PetscSection     section;
688c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
68915496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
6907318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
691cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
6927318780aSToby Isaac   const PetscReal *quadWeights;
693aed3cbd0SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset;
694cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
695cb1e1211SMatthew G Knepley 
696cb1e1211SMatthew G Knepley   PetscFunctionBegin;
697c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6987318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
699e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
700cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
701cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
70215496722SMatthew G. Knepley     PetscObject  obj;
70315496722SMatthew G. Knepley     PetscClassId id;
704c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
705c5bbbd5bSMatthew G. Knepley 
70644a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
70715496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
70815496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
70915496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
71015496722SMatthew G. Knepley 
7110f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
7120f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
71315496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
71415496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
71515496722SMatthew G. Knepley 
71615496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
71715496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
71815496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
719c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
720cb1e1211SMatthew G Knepley   }
7219c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
722beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
7237318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
724aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
725aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
7269ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
7279ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
728cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
729a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
730cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
7319c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
732cb1e1211SMatthew G Knepley 
7337318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
734cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
735cb1e1211SMatthew G Knepley 
73615496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
73715496722SMatthew G. Knepley       PetscObject  obj;
73815496722SMatthew G. Knepley       PetscClassId id;
739c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
74015496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
741cb1e1211SMatthew G Knepley 
74244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
74315496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
74415496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
74515496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
74615496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
747cb1e1211SMatthew G Knepley       if (debug) {
748cb1e1211SMatthew G Knepley         char title[1024];
749cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
7504c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
751cb1e1211SMatthew G Knepley       }
7527318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
7537318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", detJ[q], c, q);
7547318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim * q], Nc, funcVal, ctx);
755e735a8a9SMatthew G. Knepley         if (ierr) {
756e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
757e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
758e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
7597318780aSToby Isaac           ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
760e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
761e735a8a9SMatthew G. Knepley         }
76215496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
76315496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
76415496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
76515496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
766beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
767beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
768beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
769cb1e1211SMatthew G Knepley         }
770cb1e1211SMatthew G Knepley       }
7719c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
772beaa55a6SMatthew G. Knepley       qc += Nc;
773cb1e1211SMatthew G Knepley     }
774cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
775cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
776cb1e1211SMatthew G Knepley     localDiff += elemDiff;
777cb1e1211SMatthew G Knepley   }
7787318780aSToby Isaac   ierr  = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
779b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
780cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
781cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
782cb1e1211SMatthew G Knepley }
783cb1e1211SMatthew G Knepley 
784b698f381SToby 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)
785cb1e1211SMatthew G Knepley {
7860f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
787cb1e1211SMatthew G Knepley   PetscSection     section;
78840e14135SMatthew G. Knepley   PetscQuadrature  quad;
78940e14135SMatthew G. Knepley   Vec              localX;
7909c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
7919c3cf19fSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
7927318780aSToby Isaac   PetscReal       *coords, *realSpaceDer, *J, *invJ, *detJ;
79340e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
7949c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset;
795cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
796cb1e1211SMatthew G Knepley 
797cb1e1211SMatthew G Knepley   PetscFunctionBegin;
798c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7997318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
800e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
80140e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
80240e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
80340e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
80440e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
805652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
8060f2d7e86SMatthew G. Knepley     PetscFE  fe;
80740e14135SMatthew G. Knepley     PetscInt Nc;
808652b88e8SMatthew G. Knepley 
80944a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
8100f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
8110f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
81240e14135SMatthew G. Knepley     numComponents += Nc;
813652b88e8SMatthew G. Knepley   }
8149c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
815beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
8164d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
8179c3cf19fSMatthew G. Knepley   ierr = PetscMalloc7(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*Nq,&realSpaceDer,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ,numComponents,&interpolant,Nq,&detJ);CHKERRQ(ierr);
81840e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
8199ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
8209ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
82140e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
82240e14135SMatthew G. Knepley     PetscScalar *x = NULL;
82340e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
8249c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
825652b88e8SMatthew G. Knepley 
8267318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
82740e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
82840e14135SMatthew G. Knepley 
8299c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
8300f2d7e86SMatthew G. Knepley       PetscFE          fe;
83151259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
83240e14135SMatthew G. Knepley       PetscReal       *basisDer;
8339c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
83440e14135SMatthew G. Knepley 
83544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
8360f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
8379c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
8380f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
83940e14135SMatthew G. Knepley       if (debug) {
84040e14135SMatthew G. Knepley         char title[1024];
84140e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
8429c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
843652b88e8SMatthew G. Knepley       }
8449c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
8457318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", detJ[q], c, q);
8467318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx);
847e735a8a9SMatthew G. Knepley         if (ierr) {
848e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
849e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
850e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
8519c3cf19fSMatthew G. Knepley           ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2);
852e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
853e735a8a9SMatthew G. Knepley         }
8549c3cf19fSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr);
8559c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
856beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
857beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d fieldDer %d diff %g\n", c, field, PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
858beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
85940e14135SMatthew G. Knepley         }
86040e14135SMatthew G. Knepley       }
8619c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
8629c3cf19fSMatthew G. Knepley       qc          += Nc;
86340e14135SMatthew G. Knepley     }
86440e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
86540e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
86640e14135SMatthew G. Knepley     localDiff += elemDiff;
86740e14135SMatthew G. Knepley   }
8689c3cf19fSMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr);
86940e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
870b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
87140e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
872cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
873cb1e1211SMatthew G Knepley }
874cb1e1211SMatthew G Knepley 
875c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
87673d901b8SMatthew G. Knepley {
8770f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
87873d901b8SMatthew G. Knepley   PetscSection     section;
87973d901b8SMatthew G. Knepley   PetscQuadrature  quad;
88073d901b8SMatthew G. Knepley   Vec              localX;
88115496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
8827318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
88373d901b8SMatthew G. Knepley   PetscReal       *localDiff;
88415496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
8859c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
88673d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
88773d901b8SMatthew G. Knepley 
88873d901b8SMatthew G. Knepley   PetscFunctionBegin;
889c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8907318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
891e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
89273d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
89373d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
894bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
89573d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
89673d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
89773d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
89815496722SMatthew G. Knepley     PetscObject  obj;
89915496722SMatthew G. Knepley     PetscClassId id;
90073d901b8SMatthew G. Knepley     PetscInt     Nc;
90173d901b8SMatthew G. Knepley 
90244a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
90315496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
90415496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
90515496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
90615496722SMatthew G. Knepley 
9070f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
9080f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
90915496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
91015496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
91115496722SMatthew G. Knepley 
91215496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
91315496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
91415496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
91573d901b8SMatthew G. Knepley     numComponents += Nc;
91673d901b8SMatthew G. Knepley   }
9179c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
918beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
9197318780aSToby Isaac   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
92073d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
9219ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
9229ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
92373d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
92473d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
9259c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
92673d901b8SMatthew G. Knepley 
9277318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
92873d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
92973d901b8SMatthew G. Knepley 
93015496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
93115496722SMatthew G. Knepley       PetscObject  obj;
93215496722SMatthew G. Knepley       PetscClassId id;
93373d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
93415496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
93573d901b8SMatthew G. Knepley 
93615496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
93715496722SMatthew G. Knepley 
93844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
93915496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
94015496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
94115496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
94215496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
94373d901b8SMatthew G. Knepley       if (debug) {
94473d901b8SMatthew G. Knepley         char title[1024];
94573d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
94615496722SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb*Nc, &x[fieldOffset]);CHKERRQ(ierr);
94773d901b8SMatthew G. Knepley       }
9487318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
9497318780aSToby Isaac         if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature point %D", detJ, c, q);
9507318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[coordDim*q], numFields, funcVal, ctx);
951e735a8a9SMatthew G. Knepley         if (ierr) {
952e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
953e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
954e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
9557318780aSToby Isaac           ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
956e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
957e735a8a9SMatthew G. Knepley         }
95815496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
95915496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
96015496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
96115496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
962beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
963beaa55a6SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %d field %d point %g %g %g diff %g\n", c, field, coordDim > 0 ? coords[0] : 0., coordDim > 1 ? coords[1] : 0., coordDim > 2 ? coords[2] : 0., PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
964beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
96573d901b8SMatthew G. Knepley         }
96673d901b8SMatthew G. Knepley       }
967beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
9689c3cf19fSMatthew G. Knepley       qc          += Nc;
96973d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
97073d901b8SMatthew G. Knepley     }
97173d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
97273d901b8SMatthew G. Knepley   }
97373d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
974b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
97573d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
9767318780aSToby Isaac   ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
97773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
97873d901b8SMatthew G. Knepley }
97973d901b8SMatthew G. Knepley 
980e729f68cSMatthew G. Knepley /*@C
981e729f68cSMatthew 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.
982e729f68cSMatthew G. Knepley 
983e729f68cSMatthew G. Knepley   Input Parameters:
984e729f68cSMatthew G. Knepley + dm    - The DM
9850163fd50SMatthew G. Knepley . time  - The time
986ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
987e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
988e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
989e729f68cSMatthew G. Knepley 
990e729f68cSMatthew G. Knepley   Output Parameter:
991e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
992e729f68cSMatthew G. Knepley 
993e729f68cSMatthew G. Knepley   Level: developer
994e729f68cSMatthew G. Knepley 
995b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
996e729f68cSMatthew G. Knepley @*/
9970163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
998e729f68cSMatthew G. Knepley {
999e729f68cSMatthew G. Knepley   PetscSection     section;
1000e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1001e729f68cSMatthew G. Knepley   Vec              localX;
1002e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
10037318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
1004e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
10059c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1006e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1007e729f68cSMatthew G. Knepley 
1008e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1009e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1010e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10117318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1012e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1013e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1014e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1015bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1016e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1017e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1018e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1019e729f68cSMatthew G. Knepley     PetscObject  obj;
1020e729f68cSMatthew G. Knepley     PetscClassId id;
1021e729f68cSMatthew G. Knepley     PetscInt     Nc;
1022e729f68cSMatthew G. Knepley 
102344a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1024e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1025e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1026e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1027e729f68cSMatthew G. Knepley 
1028e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1029e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1030e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1031e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1032e729f68cSMatthew G. Knepley 
1033e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1034e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1035e729f68cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1036e729f68cSMatthew G. Knepley     numComponents += Nc;
1037e729f68cSMatthew G. Knepley   }
10389c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1039beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
10407318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
1041e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1042e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1043e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1044e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1045e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
10466f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
10479c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1048e729f68cSMatthew G. Knepley 
10497318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
1050e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1051e729f68cSMatthew G. Knepley 
1052e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1053e729f68cSMatthew G. Knepley       PetscObject  obj;
1054e729f68cSMatthew G. Knepley       PetscClassId id;
1055e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1056e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1057e729f68cSMatthew G. Knepley 
105844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1059e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1060e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1061e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1062e729f68cSMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
106323f34ed2SToby Isaac       if (funcs[field]) {
10647318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
10657f79407eSBarry Smith           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], c, q);
1066274e8aaeSMatthew G. Knepley           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);
1067e735a8a9SMatthew G. Knepley           if (ierr) {
1068e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1069e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
10707f79407eSBarry Smith             ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
1071e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1072e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1073e735a8a9SMatthew G. Knepley           }
1074e729f68cSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1075e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1076e729f68cSMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1077e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1078beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1079beaa55a6SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
1080e729f68cSMatthew G. Knepley           }
1081e729f68cSMatthew G. Knepley         }
108223f34ed2SToby Isaac       }
1083beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
10849c3cf19fSMatthew G. Knepley       qc          += Nc;
1085e729f68cSMatthew G. Knepley     }
1086e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
108723f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1088e729f68cSMatthew G. Knepley   }
10897318780aSToby Isaac   ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
1090e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1091e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1092e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1093e729f68cSMatthew G. Knepley }
1094e729f68cSMatthew G. Knepley 
10951555c271SMatthew G. Knepley /*@C
10961555c271SMatthew 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.
10971555c271SMatthew G. Knepley 
10981555c271SMatthew G. Knepley   Input Parameters:
10991555c271SMatthew G. Knepley + dm - The DM
11001555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
11011555c271SMatthew G. Knepley 
11021555c271SMatthew G. Knepley   Output Parameter:
11031555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
11041555c271SMatthew G. Knepley 
110595452b02SPatrick Sanan   Notes:
110695452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
11071555c271SMatthew 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
11081555c271SMatthew G. Knepley 
11091555c271SMatthew G. Knepley   Level: developer
11101555c271SMatthew G. Knepley 
11111555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
11121555c271SMatthew G. Knepley @*/
11131555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
11141555c271SMatthew G. Knepley {
1115db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1116db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
11171555c271SMatthew G. Knepley   DM               dmC;
11181555c271SMatthew G. Knepley   PetscSection     section;
11191555c271SMatthew G. Knepley   PetscQuadrature  quad;
11201555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
11211555c271SMatthew G. Knepley   PetscReal       *coords, *detJ, *J, *invJ;
11221555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
11231555c271SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset;
11241555c271SMatthew G. Knepley   PetscErrorCode   ierr;
11251555c271SMatthew G. Knepley 
11261555c271SMatthew G. Knepley   PetscFunctionBegin;
11271555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
11281555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
11291555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11301555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1131e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
11321555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
11331555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
11341555c271SMatthew G. Knepley     PetscObject  obj;
11351555c271SMatthew G. Knepley     PetscClassId id;
11361555c271SMatthew G. Knepley     PetscInt     Nc;
11371555c271SMatthew G. Knepley 
113844a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
11391555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
11401555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
11411555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
11421555c271SMatthew G. Knepley 
11431555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
11441555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
11451555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
11461555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
11471555c271SMatthew G. Knepley 
11481555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
11491555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
11501555c271SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11511555c271SMatthew G. Knepley     numComponents += Nc;
11521555c271SMatthew G. Knepley   }
11531555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
11541555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
11551555c271SMatthew G. Knepley   ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J,coordDim*coordDim*Nq,&invJ);CHKERRQ(ierr);
11561555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
11571555c271SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11581555c271SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11591555c271SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
11601555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
11611555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
11621555c271SMatthew G. Knepley     PetscInt   *star = NULL;
11631555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
11641555c271SMatthew G. Knepley 
11651555c271SMatthew G. Knepley     ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
11661555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
11671555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
11681555c271SMatthew G. Knepley       const PetscInt cell = star[st];
11691555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
11701555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
11711555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
11721555c271SMatthew G. Knepley 
11731555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
11741555c271SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
11751555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
11761555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
11771555c271SMatthew G. Knepley         PetscObject  obj;
11781555c271SMatthew G. Knepley         PetscClassId id;
11791555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
11801555c271SMatthew G. Knepley 
11811555c271SMatthew G. Knepley         ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
118244a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
11831555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
11841555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
11851555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
11861555c271SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11871555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
11881555c271SMatthew G. Knepley           if (detJ[q] <= 0.0) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)detJ[q], cell, q);
11891555c271SMatthew G. Knepley           if (ierr) {
11901555c271SMatthew G. Knepley             PetscErrorCode ierr2;
11911555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
11921555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
11931555c271SMatthew G. Knepley             ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2);
11941555c271SMatthew G. Knepley             CHKERRQ(ierr);
11951555c271SMatthew G. Knepley           }
11961555c271SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);}
11971555c271SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
11981555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
11991555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
12001555c271SMatthew G. Knepley 
12011555c271SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q];
12021555c271SMatthew G. Knepley           }
12031555c271SMatthew G. Knepley           vol += quadWeights[q*qNc]*detJ[q];
12041555c271SMatthew G. Knepley         }
12051555c271SMatthew G. Knepley         fieldOffset += Nb;
12061555c271SMatthew G. Knepley         qc          += Nc;
12071555c271SMatthew G. Knepley       }
12081555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1209f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1210f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1211f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1212f8527842SMatthew G. Knepley         }
1213f8527842SMatthew G. Knepley       }
1214f8527842SMatthew G. Knepley       volsum += vol;
1215db1066baSMatthew G. Knepley       if (debug) {
12166d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
12171555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
12181555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
12191555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
12206d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
12211555c271SMatthew G. Knepley           }
12221555c271SMatthew G. Knepley         }
12231555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1224db1066baSMatthew G. Knepley       }
12251555c271SMatthew G. Knepley     }
12261555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
12271555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
12281555c271SMatthew G. Knepley     }
12291555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
12301555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
12311555c271SMatthew G. Knepley   }
12321555c271SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr);
12331555c271SMatthew G. Knepley   PetscFunctionReturn(0);
12341555c271SMatthew G. Knepley }
12351555c271SMatthew G. Knepley 
1236338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
123773d901b8SMatthew G. Knepley {
1238338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
123961aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
124073d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1241338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1242c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1243c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1244338f77d5SMatthew G. Knepley   /* DS */
1245338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1246338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1247338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1248338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1249338f77d5SMatthew G. Knepley   /* Geometry */
1250c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1251338f77d5SMatthew G. Knepley   DM                 dmGrad;
1252c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1253338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1254b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1255338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1256b7260050SToby Isaac   PetscInt           maxDegree;
1257c330f8ffSToby Isaac   DMField            coordField;
1258c330f8ffSToby Isaac   IS                 cellIS;
125973d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
126073d901b8SMatthew G. Knepley 
126173d901b8SMatthew G. Knepley   PetscFunctionBegin;
1262338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1263c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1264e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
126573d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1266338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1267b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1268b5a3613cSMatthew G. Knepley     PetscObject  obj;
1269b5a3613cSMatthew G. Knepley     PetscClassId id;
1270b5a3613cSMatthew G. Knepley 
1271b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1272b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1273338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1274338f77d5SMatthew G. Knepley   }
1275338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1276338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1277338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1278338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1279338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1280338f77d5SMatthew G. Knepley   /* Read DS information */
1281338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1282338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1283338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1284c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1285338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1286338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1287338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1288338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1289338f77d5SMatthew G. Knepley   if (dmAux) {
1290338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1291338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
1292e87a4003SBarry Smith     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
1293338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1294338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1295338f77d5SMatthew G. Knepley   }
1296338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1297338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1298338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1299338f77d5SMatthew G. Knepley   /* Read out geometry */
1300c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1301b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1302b7260050SToby Isaac   if (maxDegree <= 1) {
1303c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1304c330f8ffSToby Isaac     if (affineQuad) {
1305c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1306338f77d5SMatthew G. Knepley     }
1307b5a3613cSMatthew G. Knepley   }
1308b5a3613cSMatthew G. Knepley   if (useFVM) {
1309338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1310b5a3613cSMatthew G. Knepley     Vec       grad;
1311b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1312b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1313b5a3613cSMatthew G. Knepley 
1314338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1315338f77d5SMatthew G. Knepley       PetscObject  obj;
1316338f77d5SMatthew G. Knepley       PetscClassId id;
1317338f77d5SMatthew G. Knepley 
1318338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1319338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1320338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1321338f77d5SMatthew G. Knepley     }
1322338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1323338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1324b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1325338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1326338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1327b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1328b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1329b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1330b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1331338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1332b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1333b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1334b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1335b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1336b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1337b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1338338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1339b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1340b5a3613cSMatthew G. Knepley   }
1341338f77d5SMatthew G. Knepley   /* Read out data from inputs */
134273d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
134373d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
134473d901b8SMatthew G. Knepley     PetscInt     i;
134573d901b8SMatthew G. Knepley 
1346338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
13470f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1348338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
134973d901b8SMatthew G. Knepley     if (dmAux) {
1350338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
13510f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1352338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
135373d901b8SMatthew G. Knepley     }
135473d901b8SMatthew G. Knepley   }
1355338f77d5SMatthew G. Knepley   /* Do integration for each field */
135673d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1357c1f031eeSMatthew G. Knepley     PetscObject  obj;
1358c1f031eeSMatthew G. Knepley     PetscClassId id;
1359c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
136073d901b8SMatthew G. Knepley 
1361c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1362c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1363c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1364c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1365c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1366c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1367c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1368c1f031eeSMatthew G. Knepley 
13690f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1370c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
13719c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1372c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1373c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
137473d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
13750f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
137673d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
137773d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
137873d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
137973d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1380c330f8ffSToby Isaac       if (!affineQuad) {
1381c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1382c330f8ffSToby Isaac       }
1383c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
1384c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
1385c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1386c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1387c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1388c330f8ffSToby Isaac       if (!affineQuad) {
1389c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1390c330f8ffSToby Isaac       }
1391c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1392c1f031eeSMatthew G. Knepley       PetscInt       foff;
1393420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1394b69edc29SMatthew G. Knepley       PetscScalar    lint;
1395c1f031eeSMatthew G. Knepley 
1396c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1397c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1398c1f031eeSMatthew G. Knepley       if (obj_func) {
1399c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1400b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1401b5a3613cSMatthew G. Knepley 
1402b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1403338f77d5SMatthew 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);
1404338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
140573d901b8SMatthew G. Knepley         }
1406c1f031eeSMatthew G. Knepley       }
1407c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1408c1f031eeSMatthew G. Knepley   }
1409338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1410b5a3613cSMatthew G. Knepley   if (useFVM) {
1411b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1412b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1413b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1414b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1415b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1416b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1417b5a3613cSMatthew G. Knepley   }
141873d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1419338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1420338f77d5SMatthew G. Knepley   /* Cleanup */
1421f99c8401SToby Isaac   if (affineQuad) {
1422f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1423f99c8401SToby Isaac   }
1424f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1425c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1426338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1427338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1428338f77d5SMatthew G. Knepley }
1429338f77d5SMatthew G. Knepley 
1430338f77d5SMatthew G. Knepley /*@
1431338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1432338f77d5SMatthew G. Knepley 
1433338f77d5SMatthew G. Knepley   Input Parameters:
1434338f77d5SMatthew G. Knepley + dm - The mesh
1435338f77d5SMatthew G. Knepley . X  - Global input vector
1436338f77d5SMatthew G. Knepley - user - The user context
1437338f77d5SMatthew G. Knepley 
1438338f77d5SMatthew G. Knepley   Output Parameter:
1439338f77d5SMatthew G. Knepley . integral - Integral for each field
1440338f77d5SMatthew G. Knepley 
1441338f77d5SMatthew G. Knepley   Level: developer
1442338f77d5SMatthew G. Knepley 
1443338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1444338f77d5SMatthew G. Knepley @*/
1445b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1446338f77d5SMatthew G. Knepley {
1447338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1448b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1449338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1450338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1451338f77d5SMatthew G. Knepley 
1452338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1453338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1454338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1455338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1456338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1457338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1458338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1459338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1460338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1461338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1462338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1463338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1464338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1465338f77d5SMatthew G. Knepley   /* Sum up values */
1466338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1467338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1468338f77d5SMatthew G. Knepley 
1469338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1470b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
1471338f77d5SMatthew G. Knepley   }
1472b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
147373d901b8SMatthew G. Knepley   if (mesh->printFEM) {
1474338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
1475b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
147673d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
147773d901b8SMatthew G. Knepley   }
1478338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
1479338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1480338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1481338f77d5SMatthew G. Knepley }
1482338f77d5SMatthew G. Knepley 
1483338f77d5SMatthew G. Knepley /*@
1484338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
1485338f77d5SMatthew G. Knepley 
1486338f77d5SMatthew G. Knepley   Input Parameters:
1487338f77d5SMatthew G. Knepley + dm - The mesh
1488338f77d5SMatthew G. Knepley . X  - Global input vector
1489338f77d5SMatthew G. Knepley - user - The user context
1490338f77d5SMatthew G. Knepley 
1491338f77d5SMatthew G. Knepley   Output Parameter:
1492338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
1493338f77d5SMatthew G. Knepley 
1494338f77d5SMatthew G. Knepley   Level: developer
1495338f77d5SMatthew G. Knepley 
1496338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1497338f77d5SMatthew G. Knepley @*/
1498338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
1499338f77d5SMatthew G. Knepley {
1500338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1501338f77d5SMatthew G. Knepley   DM             dmF;
1502338f77d5SMatthew G. Knepley   PetscSection   sectionF;
1503338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
1504338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1505338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1506338f77d5SMatthew G. Knepley 
1507338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1508338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1509338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1510338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
1511338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1512338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1513338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1514338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1515338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1516338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1517338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1518338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1519338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1520338f77d5SMatthew G. Knepley   /* Put values in F*/
1521338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
1522e87a4003SBarry Smith   ierr = DMGetSection(dmF, &sectionF);CHKERRQ(ierr);
1523338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
1524338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1525338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1526338f77d5SMatthew G. Knepley     PetscInt       dof, off;
1527338f77d5SMatthew G. Knepley 
1528338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1529338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
1530338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
1531338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
1532338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
1533338f77d5SMatthew G. Knepley   }
1534338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
1535338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
1536c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
153773d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
153873d901b8SMatthew G. Knepley }
153973d901b8SMatthew G. Knepley 
15409b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
15419b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
154264c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
154364c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
154464c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
154564c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
154664c72086SMatthew G. Knepley {
15479b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
15489b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
15499b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
15509b6f715bSMatthew G. Knepley   Vec                locA = NULL;
15519b6f715bSMatthew G. Knepley   DMField            coordField;
15529b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
15539b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
15549b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
155564c72086SMatthew G. Knepley   const PetscScalar *constants;
15569b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
155764c72086SMatthew G. Knepley   PetscErrorCode     ierr;
155864c72086SMatthew G. Knepley 
155964c72086SMatthew G. Knepley   PetscFunctionBegin;
15609b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
15619b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
156264c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
156364c72086SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
156464c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
156564c72086SMatthew G. Knepley   /* Determine which discretizations we have */
15669b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
156764c72086SMatthew G. Knepley     PetscObject  obj;
156864c72086SMatthew G. Knepley     PetscClassId id;
156964c72086SMatthew G. Knepley 
15709b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
157164c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15729b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
157364c72086SMatthew G. Knepley   }
157464c72086SMatthew G. Knepley   /* Read DS information */
157564c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
157664c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
157764c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
157864c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
157964c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
158064c72086SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
15819b6f715bSMatthew G. Knepley   if (locA) {
15829b6f715bSMatthew G. Knepley     DM dmAux;
15839b6f715bSMatthew G. Knepley 
15849b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
15859b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
158664c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
158764c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
158864c72086SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
158964c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
159064c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
159164c72086SMatthew G. Knepley   }
15929b6f715bSMatthew G. Knepley   /* Integrate over points */
15939b6f715bSMatthew G. Knepley   {
15949b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
1595b7260050SToby Isaac     PetscInt        maxDegree;
15969b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
15979b6f715bSMatthew G. Knepley     const PetscInt *points;
15989b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
15999b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
160064c72086SMatthew G. Knepley 
16019b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
16029b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
16039b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
1604b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
160564c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
160664c72086SMatthew G. Knepley       PetscFE fe;
160764c72086SMatthew G. Knepley 
160864c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1609b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
16109b6f715bSMatthew G. Knepley       if (!qGeom) {
16119b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
16129b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
16139b6f715bSMatthew G. Knepley       }
16149b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
16159b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
16169b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
16179b6f715bSMatthew G. Knepley         const PetscInt point = points[face], *support, *cone;
16189b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
16199b6f715bSMatthew G. Knepley         PetscInt       i, coneSize, faceLoc;
16209b6f715bSMatthew G. Knepley 
16219b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
16229b6f715bSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
16239b6f715bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
16249b6f715bSMatthew G. Knepley         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
16259b6f715bSMatthew 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]);
16269b6f715bSMatthew G. Knepley         fgeom->face[face][0] = faceLoc;
16279b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
16289b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
16299b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
16309b6f715bSMatthew G. Knepley         if (locA) {
16319b6f715bSMatthew G. Knepley           PetscInt subp;
16329b6f715bSMatthew G. Knepley           ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);
16339b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
16349b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
16359b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
16369b6f715bSMatthew G. Knepley         }
16379b6f715bSMatthew G. Knepley       }
16389b6f715bSMatthew G. Knepley       /* Get blocking */
16399b6f715bSMatthew G. Knepley       {
16409b6f715bSMatthew G. Knepley         PetscQuadrature q;
16419b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
16429b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
16439b6f715bSMatthew G. Knepley 
164464c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
164564c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
164664c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
164764c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
164864c72086SMatthew G. Knepley         blockSize = Nb*Nq;
164964c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
16509b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
165164c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
16529b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
16539b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
165464c72086SMatthew G. Knepley         offset    = numFaces - Nr;
165564c72086SMatthew G. Knepley       }
16569b6f715bSMatthew G. Knepley       /* Do integration for each field */
16579b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
16589b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
16599b6f715bSMatthew G. Knepley         ierr = PetscFEIntegrateBd(fe, prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
16609b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
166164c72086SMatthew G. Knepley       }
16629b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
16639b6f715bSMatthew G. Knepley       ierr = PetscFEIntegrateBd(fe, prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
16649b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
166564c72086SMatthew G. Knepley       /* Cleanup data arrays */
16669b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
16679b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
16689b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
16699b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
167064c72086SMatthew G. Knepley     }
167164c72086SMatthew G. Knepley   }
16729b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
16739b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
16749b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
16759b6f715bSMatthew G. Knepley }
16769b6f715bSMatthew G. Knepley 
16779b6f715bSMatthew G. Knepley /*@
16789b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
16799b6f715bSMatthew G. Knepley 
16809b6f715bSMatthew G. Knepley   Input Parameters:
16819b6f715bSMatthew G. Knepley + dm      - The mesh
16829b6f715bSMatthew G. Knepley . X       - Global input vector
16839b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
16849b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
16859b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
16869b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
16879b6f715bSMatthew G. Knepley - user    - The user context
16889b6f715bSMatthew G. Knepley 
16899b6f715bSMatthew G. Knepley   Output Parameter:
16909b6f715bSMatthew G. Knepley . integral - Integral for each field
16919b6f715bSMatthew G. Knepley 
16929b6f715bSMatthew G. Knepley   Level: developer
16939b6f715bSMatthew G. Knepley 
16949b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
16959b6f715bSMatthew G. Knepley @*/
16969b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
16979b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
16989b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
16999b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
17009b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
17019b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
17029b6f715bSMatthew G. Knepley {
17039b6f715bSMatthew G. Knepley   Vec            locX;
17049b6f715bSMatthew G. Knepley   PetscSection   section;
17059b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
17069b6f715bSMatthew G. Knepley   IS             facetIS;
17079b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
17089b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
17099b6f715bSMatthew G. Knepley 
17109b6f715bSMatthew G. Knepley   PetscFunctionBegin;
17119b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17129b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
17139b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
17149b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
17159b6f715bSMatthew G. Knepley   PetscValidPointer(integral, 6);
17169b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
17179b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
17189b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
17199b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
17209b6f715bSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
17219b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
17229b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
17239b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
17249b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
17259b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
17269b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
17279b6f715bSMatthew G. Knepley   /* Loop over label values */
17289b6f715bSMatthew G. Knepley   ierr = PetscMemzero(integral, Nf * sizeof(PetscScalar));CHKERRQ(ierr);
17299b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
17309b6f715bSMatthew G. Knepley     IS           pointIS;
17319b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
17329b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
17339b6f715bSMatthew G. Knepley 
17349b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
17359b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
17369b6f715bSMatthew G. Knepley     {
17379b6f715bSMatthew G. Knepley       IS isectIS;
17389b6f715bSMatthew G. Knepley 
17399b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
17409b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
17419b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
17429b6f715bSMatthew G. Knepley       pointIS = isectIS;
17439b6f715bSMatthew G. Knepley     }
17449b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
17459b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
17469b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
17479b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
17489b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
17499b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
17509b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
17519b6f715bSMatthew G. Knepley   }
175264c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
17539b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
17549b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
175564c72086SMatthew G. Knepley   PetscFunctionReturn(0);
175664c72086SMatthew G. Knepley }
175764c72086SMatthew G. Knepley 
1758d69c5d34SMatthew G. Knepley /*@
175968132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
1760d69c5d34SMatthew G. Knepley 
1761d69c5d34SMatthew G. Knepley   Input Parameters:
1762d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
1763d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
1764d69c5d34SMatthew G. Knepley - user - The user context
1765d69c5d34SMatthew G. Knepley 
1766d69c5d34SMatthew G. Knepley   Output Parameter:
1767934789fcSMatthew G. Knepley . In  - The interpolation matrix
1768d69c5d34SMatthew G. Knepley 
1769d69c5d34SMatthew G. Knepley   Level: developer
1770d69c5d34SMatthew G. Knepley 
177168132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
1772d69c5d34SMatthew G. Knepley @*/
177368132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
1774d69c5d34SMatthew G. Knepley {
1775d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
1776d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
17772764a2aaSMatthew G. Knepley   PetscDS           prob;
1778d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
177997c42addSMatthew G. Knepley   PetscFV          *fvRef;
1780d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
1781d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
1782d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
17839ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
17840f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
1785d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
1786d69c5d34SMatthew G. Knepley 
1787d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
1788d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1789c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
1790e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
1791e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
1792e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
1793e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
1794d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
1795d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
17969ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
17979ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
17982764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
179997c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
1800d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
180197c42addSMatthew G. Knepley     PetscObject  obj;
180297c42addSMatthew G. Knepley     PetscClassId id;
1803aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
1804d69c5d34SMatthew G. Knepley 
180597c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
180697c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
180797c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
180897c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
180997c42addSMatthew G. Knepley 
18100f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
1811d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
18120f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
181397c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
181497c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
181597c42addSMatthew G. Knepley       PetscDualSpace Q;
181697c42addSMatthew G. Knepley 
181797c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
181897c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
181997c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
182097c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
182197c42addSMatthew G. Knepley     }
18229c3cf19fSMatthew G. Knepley     rTotDim += rNb;
1823d69c5d34SMatthew G. Knepley   }
18242764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
18250f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
18260f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
1827d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
1828d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
1829d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
1830d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
1831d69c5d34SMatthew G. Knepley     PetscReal       *points;
1832d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
1833d69c5d34SMatthew G. Knepley 
1834d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
183597c42addSMatthew G. Knepley     if (feRef[fieldI]) {
1836d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
18370f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
183897c42addSMatthew G. Knepley     } else {
183997c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
184097c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
184197c42addSMatthew G. Knepley     }
1842d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
1843d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
1844d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18459c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
1846d69c5d34SMatthew G. Knepley       npoints += Np;
1847d69c5d34SMatthew G. Knepley     }
1848d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
1849d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
1850d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18519c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
1852d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
1853d69c5d34SMatthew G. Knepley     }
1854d69c5d34SMatthew G. Knepley 
1855d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
185697c42addSMatthew G. Knepley       PetscObject  obj;
185797c42addSMatthew G. Knepley       PetscClassId id;
1858d69c5d34SMatthew G. Knepley       PetscReal   *B;
18599c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
1860d69c5d34SMatthew G. Knepley 
186197c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
186297c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
186397c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
186497c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
1865d69c5d34SMatthew G. Knepley 
1866d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
18670f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
18680f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
1869ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
1870ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
18719c3cf19fSMatthew 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);
18720f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
1873d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
1874d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
18759c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
18769c3cf19fSMatthew 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);
1877d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
187836a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
1879d172c84bSMatthew G. Knepley                 /*
1880d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
1881d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
1882d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
1883d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
1884d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
1885d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
1886d172c84bSMatthew G. Knepley                 */
18879c3cf19fSMatthew 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];
188836a6d9c0SMatthew G. Knepley               }
1889d69c5d34SMatthew G. Knepley             }
1890d69c5d34SMatthew G. Knepley           }
18910f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
1892ffe73a53SMatthew G. Knepley         }
189397c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
189497c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
189597c42addSMatthew G. Knepley 
189697c42addSMatthew G. Knepley         /* Evaluate constant function at points */
189797c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
189897c42addSMatthew G. Knepley         cpdim = 1;
189997c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
190097c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
190197c42addSMatthew 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);
190297c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
190397c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
19049c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
19059c3cf19fSMatthew 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);
190697c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
190797c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
1908458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
190997c42addSMatthew G. Knepley               }
191097c42addSMatthew G. Knepley             }
191197c42addSMatthew G. Knepley           }
191297c42addSMatthew G. Knepley         }
191397c42addSMatthew G. Knepley       }
1914d172c84bSMatthew G. Knepley       offsetJ += cpdim;
1915d69c5d34SMatthew G. Knepley     }
1916d172c84bSMatthew G. Knepley     offsetI += fpdim;
1917549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
1918d69c5d34SMatthew G. Knepley   }
19190f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
19207f5b169aSMatthew G. Knepley   /* Preallocate matrix */
19217f5b169aSMatthew G. Knepley   {
1922c094ef40SMatthew G. Knepley     Mat          preallocator;
1923c094ef40SMatthew G. Knepley     PetscScalar *vals;
1924c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
1925c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
19267f5b169aSMatthew G. Knepley 
1927c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
1928c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
1929c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
1930c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1931c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
1932c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
19337f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
19347f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
1935c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
19367f5b169aSMatthew G. Knepley     }
1937c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
1938c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1939c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1940c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
1941c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
19427f5b169aSMatthew G. Knepley   }
19437f5b169aSMatthew G. Knepley   /* Fill matrix */
19447f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
1945d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1946934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
1947d69c5d34SMatthew G. Knepley   }
1948549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
194997c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
1950549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
1951934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1952934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1953d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
1954825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
1955934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
1956825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
1957d69c5d34SMatthew G. Knepley   }
1958d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
1959d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
1960d69c5d34SMatthew G. Knepley }
19616c73c22cSMatthew G. Knepley 
1962bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
1963bd041c0cSMatthew G. Knepley {
1964bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
1965bd041c0cSMatthew G. Knepley }
1966bd041c0cSMatthew G. Knepley 
196768132eb9SMatthew G. Knepley /*@
196868132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
196968132eb9SMatthew G. Knepley 
197068132eb9SMatthew G. Knepley   Input Parameters:
197168132eb9SMatthew G. Knepley + dmf  - The fine mesh
197268132eb9SMatthew G. Knepley . dmc  - The coarse mesh
197368132eb9SMatthew G. Knepley - user - The user context
197468132eb9SMatthew G. Knepley 
197568132eb9SMatthew G. Knepley   Output Parameter:
197668132eb9SMatthew G. Knepley . In  - The interpolation matrix
197768132eb9SMatthew G. Knepley 
197868132eb9SMatthew G. Knepley   Level: developer
197968132eb9SMatthew G. Knepley 
198068132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
198168132eb9SMatthew G. Knepley @*/
198268132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
19834ef9d792SMatthew G. Knepley {
198464e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
198564e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
19864ef9d792SMatthew G. Knepley   PetscDS        prob;
19874ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
1988e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
19894ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
19904ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
19914ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
19924ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
19934ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
19944ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
19954ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
19964ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
19974ef9d792SMatthew G. Knepley 
19984ef9d792SMatthew G. Knepley   PetscFunctionBegin;
199977711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
20004ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
20014ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
20024ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
20034ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
20044ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
20054ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
2006e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2007e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
2008e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2009e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
20104ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
20114ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
20129c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
20134ef9d792SMatthew G. Knepley 
20144ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
20154ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
20164ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
20174ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
20184ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
20194ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
20204ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
20214ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2022e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
20234ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
20244ef9d792SMatthew G. Knepley     PetscObject      obj;
20254ef9d792SMatthew G. Knepley     PetscClassId     id;
2026c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
20274ef9d792SMatthew G. Knepley     PetscQuadrature  f;
202817f047d8SMatthew G. Knepley     const PetscReal *qpoints;
202917f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
20304ef9d792SMatthew G. Knepley 
20314ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
20324ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
20334ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
20344ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
20354ef9d792SMatthew G. Knepley 
20364ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
20374ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
20384ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
20394ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
20404ef9d792SMatthew G. Knepley 
20414ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
20424ef9d792SMatthew G. Knepley       Nc   = 1;
20434ef9d792SMatthew G. Knepley     }
20444ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
20454ef9d792SMatthew G. Knepley     /* For each fine grid cell */
20464ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
20474ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
20484ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
20494ef9d792SMatthew G. Knepley 
20506ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
20514ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
20529c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
20534ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
20544ef9d792SMatthew G. Knepley         Vec             pointVec;
20554ef9d792SMatthew G. Knepley         PetscScalar    *pV;
20563a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
20573a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
20589c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
20594ef9d792SMatthew G. Knepley 
20604ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
20614ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
20629c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
20634ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
20644ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
20654ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
20664ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2067c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2068c330f8ffSToby Isaac 
20694ef9d792SMatthew G. Knepley           /* Transform point to real space */
2070c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
20714ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
20724ef9d792SMatthew G. Knepley         }
20734ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
20744ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
20751555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
207662a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
20773a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
20784ef9d792SMatthew G. Knepley         /* Update preallocation info */
20793a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
20803a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
20819c3cf19fSMatthew G. Knepley         {
2082e8f14785SLisandro Dalcin           PetscHashIJKey key;
2083e8f14785SLisandro Dalcin           PetscBool      missing;
20844ef9d792SMatthew G. Knepley 
2085e8f14785SLisandro Dalcin           key.i = findices[i];
2086e8f14785SLisandro Dalcin           if (key.i >= 0) {
20874ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
20884ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
20893a93e3b7SToby Isaac               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
20904ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2091e8f14785SLisandro Dalcin                 key.j = cindices[c];
2092e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2093e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
20944ef9d792SMatthew G. Knepley                 if (missing) {
2095e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2096e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
20974ef9d792SMatthew G. Knepley                 }
20984ef9d792SMatthew G. Knepley               }
20993a93e3b7SToby Isaac               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
21004ef9d792SMatthew G. Knepley             }
21014ef9d792SMatthew G. Knepley           }
21028c543595SMatthew G. Knepley         }
21033a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
21044ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
21054ef9d792SMatthew G. Knepley       }
210646bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
21074ef9d792SMatthew G. Knepley     }
21084ef9d792SMatthew G. Knepley   }
2109e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
21104ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
21114ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
21124ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
21134ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
21144ef9d792SMatthew G. Knepley     PetscObject      obj;
21154ef9d792SMatthew G. Knepley     PetscClassId     id;
2116c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
21174ef9d792SMatthew G. Knepley     PetscQuadrature  f;
21184ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
21199c3cf19fSMatthew G. Knepley     PetscInt         Nc, qNc, Np, fpdim, i, d;
21204ef9d792SMatthew G. Knepley 
21214ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
21224ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
21234ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
21244ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
21254ef9d792SMatthew G. Knepley 
21264ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
21274ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
21284ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
21294ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
21304ef9d792SMatthew G. Knepley 
21314ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
21324ef9d792SMatthew G. Knepley       Nc   = 1;
213325ce1634SJed Brown     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %d",field);
21344ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
21354ef9d792SMatthew G. Knepley     /* For each fine grid cell */
21364ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
21374ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
21384ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
21394ef9d792SMatthew G. Knepley 
21406ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
21414ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
21429c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
21434ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
21444ef9d792SMatthew G. Knepley         Vec             pointVec;
21454ef9d792SMatthew G. Knepley         PetscScalar    *pV;
214612111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
21473a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
214817f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
21494ef9d792SMatthew G. Knepley 
21504ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
21514ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
21529c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
21539c3cf19fSMatthew 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);
21544ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
21554ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
21564ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
21574ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2158c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2159c330f8ffSToby Isaac 
21604ef9d792SMatthew G. Knepley           /* Transform point to real space */
2161c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
21624ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
21634ef9d792SMatthew G. Knepley         }
21644ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
21654ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
21661555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
216762a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
21684ef9d792SMatthew G. Knepley         /* Update preallocation info */
21693a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
21703a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
21714ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
21724ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2173826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2174c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2175826eb36dSMatthew G. Knepley 
21763a93e3b7SToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
21774ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
21783a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2179e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2180c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
21814ef9d792SMatthew G. Knepley 
21824ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
21834ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
21844ef9d792SMatthew G. Knepley             PetscReal *B;
21854ef9d792SMatthew G. Knepley 
21864ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
21874ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
21884ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
21899c3cf19fSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
21904ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
21914ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
21929c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c];
21934ef9d792SMatthew G. Knepley             }
21944ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
21954ef9d792SMatthew G. Knepley           } else {
21964ef9d792SMatthew G. Knepley             cpdim = 1;
21974ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
21989c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
21994ef9d792SMatthew G. Knepley             }
22004ef9d792SMatthew G. Knepley           }
22014ef9d792SMatthew G. Knepley           /* Update interpolator */
22029c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
22039c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
22049c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
22053a93e3b7SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
22064ef9d792SMatthew G. Knepley         }
22074ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
22083a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
22094ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
22104ef9d792SMatthew G. Knepley       }
221146bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
22124ef9d792SMatthew G. Knepley     }
22134ef9d792SMatthew G. Knepley   }
22144ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
22154ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
22164ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
22174ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
22184ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
221977711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
22204ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
22214ef9d792SMatthew G. Knepley }
22224ef9d792SMatthew G. Knepley 
222346fa42a0SMatthew G. Knepley /*@
2224bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2225bd041c0cSMatthew G. Knepley 
2226bd041c0cSMatthew G. Knepley   Input Parameters:
2227bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2228bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2229bd041c0cSMatthew G. Knepley - user - The user context
2230bd041c0cSMatthew G. Knepley 
2231bd041c0cSMatthew G. Knepley   Output Parameter:
2232bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2233bd041c0cSMatthew G. Knepley 
2234bd041c0cSMatthew G. Knepley   Level: developer
2235bd041c0cSMatthew G. Knepley 
2236bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2237bd041c0cSMatthew G. Knepley @*/
2238bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2239bd041c0cSMatthew G. Knepley {
2240bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2241bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2242bd041c0cSMatthew G. Knepley   PetscDS        prob;
2243bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2244e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2245bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2246bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2247bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2248bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2249bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2250bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2251bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2252bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2253bd041c0cSMatthew G. Knepley 
2254bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2255bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2256bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
2257bd041c0cSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
2258bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2259bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2260bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
2261e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2262e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
2263e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2264e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2265bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2266bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2267bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2268bd041c0cSMatthew G. Knepley 
2269bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2270bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2271bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2272bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2273bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2274bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2275bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2276bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2277e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2278bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2279bd041c0cSMatthew G. Knepley     PetscObject      obj;
2280bd041c0cSMatthew G. Knepley     PetscClassId     id;
2281bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2282bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2283bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2284bd041c0cSMatthew G. Knepley 
2285bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2286bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2287bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2288bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2289bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2290bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2291bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2292bd041c0cSMatthew G. Knepley       Vec                pointVec;
2293bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2294bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2295bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2296bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2297bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2298bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2299bd041c0cSMatthew G. Knepley 
2300bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2301bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2302bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2303bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2304bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2305bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2306bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2307c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2308c330f8ffSToby Isaac 
2309bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2310c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2311bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2312bd041c0cSMatthew G. Knepley       }
2313bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2314bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2315bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2316bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2317bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2318bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2319bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2320bd041c0cSMatthew G. Knepley       {
2321e8f14785SLisandro Dalcin         PetscHashIJKey key;
2322e8f14785SLisandro Dalcin         PetscBool      missing;
2323bd041c0cSMatthew G. Knepley 
2324bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2325e8f14785SLisandro Dalcin           key.i = findices[i];
2326e8f14785SLisandro Dalcin           if (key.i >= 0) {
2327bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2328bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2329bd041c0cSMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2330bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2331e8f14785SLisandro Dalcin                 key.j = cindices[c];
2332e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2333e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2334bd041c0cSMatthew G. Knepley                 if (missing) {
2335e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2336e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2337bd041c0cSMatthew G. Knepley                 }
2338bd041c0cSMatthew G. Knepley               }
2339bd041c0cSMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2340bd041c0cSMatthew G. Knepley             }
2341bd041c0cSMatthew G. Knepley           }
2342bd041c0cSMatthew G. Knepley         }
2343bd041c0cSMatthew G. Knepley       }
2344bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2345bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2346bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2347bd041c0cSMatthew G. Knepley     }
2348bd041c0cSMatthew G. Knepley   }
2349e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
2350bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2351bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2352bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2353bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2354bd041c0cSMatthew G. Knepley     PetscObject      obj;
2355bd041c0cSMatthew G. Knepley     PetscClassId     id;
2356bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2357bd041c0cSMatthew G. Knepley     PetscReal       *Bfine;
2358bd041c0cSMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2359bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2360bd041c0cSMatthew G. Knepley 
2361bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2362bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2363bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);}
2364bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2365bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2366bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2367bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2368bd041c0cSMatthew G. Knepley       Vec                pointVec;
2369bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2370bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2371bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2372bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2373bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2374bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2375bd041c0cSMatthew G. Knepley 
2376bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2377bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2378bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2379bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2380bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2381bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2382bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2383c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2384c330f8ffSToby Isaac 
2385bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2386c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2387bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2388bd041c0cSMatthew G. Knepley       }
2389bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2390bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2391bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2392bd041c0cSMatthew G. Knepley       /* Update matrix */
2393bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2394bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2395bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2396bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2397bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2398c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2399c330f8ffSToby Isaac 
2400bd041c0cSMatthew G. Knepley 
2401bd041c0cSMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2402bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2403bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2404bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2405c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
2406bd041c0cSMatthew G. Knepley 
2407bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2408bd041c0cSMatthew G. Knepley           PetscFE    fe = (PetscFE) obj;
2409bd041c0cSMatthew G. Knepley           PetscReal *B;
2410bd041c0cSMatthew G. Knepley 
2411bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2412bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2413bd041c0cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2414bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2415bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2416bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2417bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2418bd041c0cSMatthew 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;
2419bd041c0cSMatthew G. Knepley             }
2420bd041c0cSMatthew G. Knepley             /* Update interpolator */
2421bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2422bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2423bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2424bd041c0cSMatthew G. Knepley           }
2425bd041c0cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2426bd041c0cSMatthew G. Knepley         } else {
2427bd041c0cSMatthew G. Knepley           cpdim = 1;
2428bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2429bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2430bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2431bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2432bd041c0cSMatthew G. Knepley             }
2433bd041c0cSMatthew G. Knepley             /* Update interpolator */
2434bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2435bd041c0cSMatthew G. Knepley             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %d %d Nf: %d %d Nc: %d %d\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
2436bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2437bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2438bd041c0cSMatthew G. Knepley           }
2439bd041c0cSMatthew G. Knepley         }
2440bd041c0cSMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2441bd041c0cSMatthew G. Knepley       }
2442bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2443bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2444bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2445bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2446bd041c0cSMatthew G. Knepley     }
2447bd041c0cSMatthew G. Knepley   }
2448bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
2449bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
2450bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2451bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2452bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2453bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
2454bd041c0cSMatthew G. Knepley }
2455bd041c0cSMatthew G. Knepley 
2456bd041c0cSMatthew G. Knepley /*@
245746fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
245846fa42a0SMatthew G. Knepley 
245946fa42a0SMatthew G. Knepley   Input Parameters:
246046fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
246146fa42a0SMatthew G. Knepley - dmf  - The fine mesh
246246fa42a0SMatthew G. Knepley - user - The user context
246346fa42a0SMatthew G. Knepley 
246446fa42a0SMatthew G. Knepley   Output Parameter:
246546fa42a0SMatthew G. Knepley . sc   - The mapping
246646fa42a0SMatthew G. Knepley 
246746fa42a0SMatthew G. Knepley   Level: developer
246846fa42a0SMatthew G. Knepley 
246946fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
247046fa42a0SMatthew G. Knepley @*/
24717c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
24727c927364SMatthew G. Knepley {
2473e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
24747c927364SMatthew G. Knepley   PetscFE       *feRef;
247597c42addSMatthew G. Knepley   PetscFV       *fvRef;
24767c927364SMatthew G. Knepley   Vec            fv, cv;
24777c927364SMatthew G. Knepley   IS             fis, cis;
24787c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
24797c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
24800bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
24816f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
24827c927364SMatthew G. Knepley   PetscErrorCode ierr;
24837c927364SMatthew G. Knepley 
24847c927364SMatthew G. Knepley   PetscFunctionBegin;
248575a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2486c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
2487e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2488e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
2489e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2490e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
24917c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
24927c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
24939ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
24949ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2495e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
24966f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
24977c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
249897c42addSMatthew G. Knepley     PetscObject  obj;
249997c42addSMatthew G. Knepley     PetscClassId id;
2500aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
25017c927364SMatthew G. Knepley 
250297c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
250397c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
250497c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
250597c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
25066f3d3cbcSMatthew G. Knepley       PetscSpace sp;
25079b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
250897c42addSMatthew G. Knepley 
25097c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
25107c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
25117c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
25126f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
25139b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
25149b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
251597c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
251697c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
251797c42addSMatthew G. Knepley       PetscDualSpace Q;
251897c42addSMatthew G. Knepley 
251997c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
252097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
252197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
252297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
25236f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
252497c42addSMatthew G. Knepley     }
2525d172c84bSMatthew G. Knepley     fTotDim += fNb;
25267c927364SMatthew G. Knepley   }
2527e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
25287c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
25297c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
25307c927364SMatthew G. Knepley     PetscFE        feC;
253197c42addSMatthew G. Knepley     PetscFV        fvC;
25327c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
2533d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
25347c927364SMatthew G. Knepley 
253597c42addSMatthew G. Knepley     if (feRef[field]) {
2536e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
25377c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
25387c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
25397c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
2540d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
25417c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
25427c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
25437c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
254497c42addSMatthew G. Knepley     } else {
254597c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
254697c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
254797c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
254897c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
254997c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
255097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
255197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
255297c42addSMatthew G. Knepley     }
255397c42addSMatthew G. Knepley     if (NcF != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %d does not match coarse field %d", NcF, NcC);
25547c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
25557c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
2556d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
2557d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
255897c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
25597c927364SMatthew G. Knepley 
25607c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
2561d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
2562d172c84bSMatthew G. Knepley       if (NqcC != NcC) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcC, NcC);
256397c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
25647c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
25657c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
2566d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
25677c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
2568d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
25697c927364SMatthew G. Knepley 
25707c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
2571d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
2572d172c84bSMatthew G. Knepley         if (NqcF != NcF) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components", NqcF, NcF);
25737c927364SMatthew G. Knepley         if (NpC != NpF) continue;
25747c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
25757c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
2576d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
2577d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
2578d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
257997c42addSMatthew G. Knepley         found = PETSC_TRUE;
25807c927364SMatthew G. Knepley         break;
25817c927364SMatthew G. Knepley       }
258297c42addSMatthew G. Knepley       if (!found) {
258397c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
2584d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
2585d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
258697c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
258797c42addSMatthew G. Knepley       }
25887c927364SMatthew G. Knepley     }
2589d172c84bSMatthew G. Knepley     offsetC += cpdim;
2590d172c84bSMatthew G. Knepley     offsetF += fpdim;
25917c927364SMatthew G. Knepley   }
259297c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
25936f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
25947c927364SMatthew G. Knepley 
25957c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
25967c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
25970bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
25987c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
25997c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
2600aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
2601aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
26027c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
26037c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
26047c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
26057c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
26060bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
26077c927364SMatthew G. Knepley       if ((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]])) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Coarse dof %d maps to both %d and %d", cindices[cellCIndices[d]-startC], findices[cellCIndices[d]-startC], cellFIndices[cmap[d]]);
26087c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
26097c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
26107c927364SMatthew G. Knepley     }
26117c927364SMatthew G. Knepley   }
26127c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
26137c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
26147c927364SMatthew G. Knepley 
26157c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
26167c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
26179448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
26187c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
26197c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
26207c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
26217c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
262275a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2623cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2624cb1e1211SMatthew G Knepley }
2625a1cf66bbSMatthew G. Knepley 
2626*2f856554SMatthew G. Knepley /*@C
2627*2f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
2628*2f856554SMatthew G. Knepley 
2629*2f856554SMatthew G. Knepley   Input Parameters:
2630*2f856554SMatthew G. Knepley + dm     - The DM
2631*2f856554SMatthew G. Knepley . cellIS - The cells to include
2632*2f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
2633*2f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
2634*2f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
2635*2f856554SMatthew G. Knepley 
2636*2f856554SMatthew G. Knepley   Output Parameters:
2637*2f856554SMatthew G. Knepley + u   - The field coefficients
2638*2f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
2639*2f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
2640*2f856554SMatthew G. Knepley 
2641*2f856554SMatthew G. Knepley   Level: developer
2642*2f856554SMatthew G. Knepley 
2643*2f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
2644*2f856554SMatthew G. Knepley @*/
2645*2f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
2646*2f856554SMatthew G. Knepley {
2647*2f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
2648*2f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
2649*2f856554SMatthew G. Knepley   PetscDS         prob;
2650*2f856554SMatthew G. Knepley   const PetscInt *cells;
2651*2f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
2652*2f856554SMatthew G. Knepley   PetscErrorCode  ierr;
2653*2f856554SMatthew G. Knepley 
2654*2f856554SMatthew G. Knepley   PetscFunctionBegin;
2655*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2656*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
2657*2f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
2658*2f856554SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
2659*2f856554SMatthew G. Knepley   PetscValidPointer(u, 7);
2660*2f856554SMatthew G. Knepley   PetscValidPointer(u_t, 8);
2661*2f856554SMatthew G. Knepley   PetscValidPointer(a, 9);
2662*2f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
2663*2f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2664*2f856554SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
2665*2f856554SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr);
2666*2f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2667*2f856554SMatthew G. Knepley   if (locA) {
2668*2f856554SMatthew G. Knepley     DM      dmAux;
2669*2f856554SMatthew G. Knepley     PetscDS probAux;
2670*2f856554SMatthew G. Knepley 
2671*2f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2672*2f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
2673*2f856554SMatthew G. Knepley     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
2674*2f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2675*2f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2676*2f856554SMatthew G. Knepley   }
2677*2f856554SMatthew G. Knepley   numCells = cEnd - cStart;
2678*2f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
2679*2f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
2680*2f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
2681*2f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2682*2f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
2683*2f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
2684*2f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
2685*2f856554SMatthew G. Knepley     PetscInt       i;
2686*2f856554SMatthew G. Knepley 
2687*2f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
2688*2f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
2689*2f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
2690*2f856554SMatthew G. Knepley     if (locX_t) {
2691*2f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
2692*2f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
2693*2f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
2694*2f856554SMatthew G. Knepley     }
2695*2f856554SMatthew G. Knepley     if (locA) {
2696*2f856554SMatthew G. Knepley       PetscInt subcell;
2697*2f856554SMatthew G. Knepley       ierr = DMPlexGetAuxiliaryPoint(plex, plexA, cell, &subcell);CHKERRQ(ierr);
2698*2f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
2699*2f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
2700*2f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
2701*2f856554SMatthew G. Knepley     }
2702*2f856554SMatthew G. Knepley   }
2703*2f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
2704*2f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
2705*2f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
2706*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
2707*2f856554SMatthew G. Knepley }
2708*2f856554SMatthew G. Knepley 
2709*2f856554SMatthew G. Knepley /*@C
2710*2f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
2711*2f856554SMatthew G. Knepley 
2712*2f856554SMatthew G. Knepley   Input Parameters:
2713*2f856554SMatthew G. Knepley + dm     - The DM
2714*2f856554SMatthew G. Knepley . cellIS - The cells to include
2715*2f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
2716*2f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
2717*2f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
2718*2f856554SMatthew G. Knepley 
2719*2f856554SMatthew G. Knepley   Output Parameters:
2720*2f856554SMatthew G. Knepley + u   - The field coefficients
2721*2f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
2722*2f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
2723*2f856554SMatthew G. Knepley 
2724*2f856554SMatthew G. Knepley   Level: developer
2725*2f856554SMatthew G. Knepley 
2726*2f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
2727*2f856554SMatthew G. Knepley @*/
2728*2f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
2729*2f856554SMatthew G. Knepley {
2730*2f856554SMatthew G. Knepley   PetscErrorCode ierr;
2731*2f856554SMatthew G. Knepley 
2732*2f856554SMatthew G. Knepley   PetscFunctionBegin;
2733*2f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
2734*2f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
2735*2f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
2736*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
2737*2f856554SMatthew G. Knepley }
2738*2f856554SMatthew G. Knepley 
2739*2f856554SMatthew G. Knepley /*@C
2740*2f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
2741*2f856554SMatthew G. Knepley 
2742*2f856554SMatthew G. Knepley   Input Parameters:
2743*2f856554SMatthew G. Knepley + dm     - The DM
2744*2f856554SMatthew G. Knepley . fStart - The first face to include
2745*2f856554SMatthew G. Knepley . fEnd   - The first face to exclude
2746*2f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
2747*2f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
2748*2f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
2749*2f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
2750*2f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
2751*2f856554SMatthew G. Knepley 
2752*2f856554SMatthew G. Knepley   Output Parameters:
2753*2f856554SMatthew G. Knepley + Nface - The number of faces with field values
2754*2f856554SMatthew G. Knepley . uL - The field values at the left side of the face
2755*2f856554SMatthew G. Knepley - uR - The field values at the right side of the face
2756*2f856554SMatthew G. Knepley 
2757*2f856554SMatthew G. Knepley   Level: developer
2758*2f856554SMatthew G. Knepley 
2759*2f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
2760*2f856554SMatthew G. Knepley @*/
2761*2f856554SMatthew 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)
2762*2f856554SMatthew G. Knepley {
2763*2f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
2764*2f856554SMatthew G. Knepley   PetscSection       section;
2765*2f856554SMatthew G. Knepley   PetscDS            prob;
2766*2f856554SMatthew G. Knepley   DMLabel            ghostLabel;
2767*2f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
2768*2f856554SMatthew G. Knepley   PetscBool         *isFE;
2769*2f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
2770*2f856554SMatthew G. Knepley   PetscErrorCode     ierr;
2771*2f856554SMatthew G. Knepley 
2772*2f856554SMatthew G. Knepley   PetscFunctionBegin;
2773*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2774*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
2775*2f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
2776*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
2777*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
2778*2f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
2779*2f856554SMatthew G. Knepley   PetscValidPointer(uL, 9);
2780*2f856554SMatthew G. Knepley   PetscValidPointer(uR, 10);
2781*2f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2782*2f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2783*2f856554SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
2784*2f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2785*2f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
2786*2f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
2787*2f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2788*2f856554SMatthew G. Knepley     PetscObject  obj;
2789*2f856554SMatthew G. Knepley     PetscClassId id;
2790*2f856554SMatthew G. Knepley 
2791*2f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
2792*2f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2793*2f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2794*2f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2795*2f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
2796*2f856554SMatthew G. Knepley   }
2797*2f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2798*2f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
2799*2f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2800*2f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
2801*2f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2802*2f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
2803*2f856554SMatthew G. Knepley   if (locGrad) {
2804*2f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
2805*2f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
2806*2f856554SMatthew G. Knepley   }
2807*2f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
2808*2f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
2809*2f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
2810*2f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
2811*2f856554SMatthew G. Knepley     const PetscInt        *cells;
2812*2f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
2813*2f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
2814*2f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
2815*2f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
2816*2f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
2817*2f856554SMatthew G. Knepley 
2818*2f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
2819*2f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
2820*2f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
2821*2f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
2822*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
2823*2f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
2824*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
2825*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
2826*2f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2827*2f856554SMatthew G. Knepley       PetscInt off;
2828*2f856554SMatthew G. Knepley 
2829*2f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
2830*2f856554SMatthew G. Knepley       if (isFE[f]) {
2831*2f856554SMatthew G. Knepley         const PetscInt *cone;
2832*2f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
2833*2f856554SMatthew G. Knepley 
2834*2f856554SMatthew G. Knepley         xL = xR = NULL;
2835*2f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
2836*2f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
2837*2f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
2838*2f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
2839*2f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
2840*2f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
2841*2f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
2842*2f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
2843*2f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
2844*2f856554SMatthew G. Knepley         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]);
2845*2f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
2846*2f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
2847*2f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
2848*2f856554SMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
2849*2f856554SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
2850*2f856554SMatthew G. Knepley           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
2851*2f856554SMatthew G. Knepley         }
2852*2f856554SMatthew G. Knepley         else {
2853*2f856554SMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
2854*2f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
2855*2f856554SMatthew G. Knepley           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
2856*2f856554SMatthew G. Knepley         }
2857*2f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
2858*2f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
2859*2f856554SMatthew G. Knepley       } else {
2860*2f856554SMatthew G. Knepley         PetscFV  fv;
2861*2f856554SMatthew G. Knepley         PetscInt numComp, c;
2862*2f856554SMatthew G. Knepley 
2863*2f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
2864*2f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
2865*2f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
2866*2f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
2867*2f856554SMatthew G. Knepley         if (dmGrad) {
2868*2f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
2869*2f856554SMatthew G. Knepley 
2870*2f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
2871*2f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
2872*2f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
2873*2f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
2874*2f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
2875*2f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
2876*2f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
2877*2f856554SMatthew G. Knepley           }
2878*2f856554SMatthew G. Knepley         } else {
2879*2f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
2880*2f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
2881*2f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
2882*2f856554SMatthew G. Knepley           }
2883*2f856554SMatthew G. Knepley         }
2884*2f856554SMatthew G. Knepley       }
2885*2f856554SMatthew G. Knepley     }
2886*2f856554SMatthew G. Knepley     ++iface;
2887*2f856554SMatthew G. Knepley   }
2888*2f856554SMatthew G. Knepley   *Nface = iface;
2889*2f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
2890*2f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
2891*2f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
2892*2f856554SMatthew G. Knepley   if (locGrad) {
2893*2f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
2894*2f856554SMatthew G. Knepley   }
2895*2f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
2896*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
2897*2f856554SMatthew G. Knepley }
2898*2f856554SMatthew G. Knepley 
2899*2f856554SMatthew G. Knepley /*@C
2900*2f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
2901*2f856554SMatthew G. Knepley 
2902*2f856554SMatthew G. Knepley   Input Parameters:
2903*2f856554SMatthew G. Knepley + dm     - The DM
2904*2f856554SMatthew G. Knepley . fStart - The first face to include
2905*2f856554SMatthew G. Knepley . fEnd   - The first face to exclude
2906*2f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
2907*2f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
2908*2f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
2909*2f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
2910*2f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
2911*2f856554SMatthew G. Knepley 
2912*2f856554SMatthew G. Knepley   Output Parameters:
2913*2f856554SMatthew G. Knepley + Nface - The number of faces with field values
2914*2f856554SMatthew G. Knepley . uL - The field values at the left side of the face
2915*2f856554SMatthew G. Knepley - uR - The field values at the right side of the face
2916*2f856554SMatthew G. Knepley 
2917*2f856554SMatthew G. Knepley   Level: developer
2918*2f856554SMatthew G. Knepley 
2919*2f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
2920*2f856554SMatthew G. Knepley @*/
2921*2f856554SMatthew 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)
2922*2f856554SMatthew G. Knepley {
2923*2f856554SMatthew G. Knepley   PetscErrorCode ierr;
2924*2f856554SMatthew G. Knepley 
2925*2f856554SMatthew G. Knepley   PetscFunctionBegin;
2926*2f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
2927*2f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
2928*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
2929*2f856554SMatthew G. Knepley }
2930*2f856554SMatthew G. Knepley 
2931*2f856554SMatthew G. Knepley /*@C
2932*2f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
2933*2f856554SMatthew G. Knepley 
2934*2f856554SMatthew G. Knepley   Input Parameters:
2935*2f856554SMatthew G. Knepley + dm     - The DM
2936*2f856554SMatthew G. Knepley . fStart - The first face to include
2937*2f856554SMatthew G. Knepley . fEnd   - The first face to exclude
2938*2f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
2939*2f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
2940*2f856554SMatthew G. Knepley 
2941*2f856554SMatthew G. Knepley   Output Parameters:
2942*2f856554SMatthew G. Knepley + Nface - The number of faces with field values
2943*2f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
2944*2f856554SMatthew G. Knepley - vol   - The cell volume
2945*2f856554SMatthew G. Knepley 
2946*2f856554SMatthew G. Knepley   Level: developer
2947*2f856554SMatthew G. Knepley 
2948*2f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
2949*2f856554SMatthew G. Knepley @*/
2950*2f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
2951*2f856554SMatthew G. Knepley {
2952*2f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
2953*2f856554SMatthew G. Knepley   DMLabel            ghostLabel;
2954*2f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
2955*2f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
2956*2f856554SMatthew G. Knepley   PetscErrorCode     ierr;
2957*2f856554SMatthew G. Knepley 
2958*2f856554SMatthew G. Knepley   PetscFunctionBegin;
2959*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2960*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
2961*2f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
2962*2f856554SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
2963*2f856554SMatthew G. Knepley   PetscValidPointer(vol, 7);
2964*2f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2965*2f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
2966*2f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
2967*2f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
2968*2f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
2969*2f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
2970*2f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
2971*2f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
2972*2f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
2973*2f856554SMatthew G. Knepley     const PetscInt        *cells;
2974*2f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
2975*2f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
2976*2f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
2977*2f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
2978*2f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
2979*2f856554SMatthew G. Knepley 
2980*2f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
2981*2f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
2982*2f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
2983*2f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
2984*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
2985*2f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
2986*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
2987*2f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
2988*2f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
2989*2f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
2990*2f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
2991*2f856554SMatthew G. Knepley     }
2992*2f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
2993*2f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
2994*2f856554SMatthew G. Knepley     ++iface;
2995*2f856554SMatthew G. Knepley   }
2996*2f856554SMatthew G. Knepley   *Nface = iface;
2997*2f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
2998*2f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
2999*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
3000*2f856554SMatthew G. Knepley }
3001*2f856554SMatthew G. Knepley 
3002*2f856554SMatthew G. Knepley /*@C
3003*2f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
3004*2f856554SMatthew G. Knepley 
3005*2f856554SMatthew G. Knepley   Input Parameters:
3006*2f856554SMatthew G. Knepley + dm     - The DM
3007*2f856554SMatthew G. Knepley . fStart - The first face to include
3008*2f856554SMatthew G. Knepley . fEnd   - The first face to exclude
3009*2f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
3010*2f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
3011*2f856554SMatthew G. Knepley 
3012*2f856554SMatthew G. Knepley   Output Parameters:
3013*2f856554SMatthew G. Knepley + Nface - The number of faces with field values
3014*2f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
3015*2f856554SMatthew G. Knepley - vol   - The cell volume
3016*2f856554SMatthew G. Knepley 
3017*2f856554SMatthew G. Knepley   Level: developer
3018*2f856554SMatthew G. Knepley 
3019*2f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
3020*2f856554SMatthew G. Knepley @*/
3021*2f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
3022*2f856554SMatthew G. Knepley {
3023*2f856554SMatthew G. Knepley   PetscErrorCode ierr;
3024*2f856554SMatthew G. Knepley 
3025*2f856554SMatthew G. Knepley   PetscFunctionBegin;
3026*2f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
3027*2f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
3028*2f856554SMatthew G. Knepley   PetscFunctionReturn(0);
3029*2f856554SMatthew G. Knepley }
3030*2f856554SMatthew G. Knepley 
3031a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3032a1cf66bbSMatthew G. Knepley {
3033a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3034a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3035a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3036a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3037a1cf66bbSMatthew G. Knepley 
3038a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3039a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3040a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3041a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3042a1cf66bbSMatthew G. Knepley   if (container) {
3043a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3044a1cf66bbSMatthew G. Knepley   } else {
3045a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3046a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3047a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3048a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3049a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3050a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3051a1cf66bbSMatthew G. Knepley   }
3052a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3053a1cf66bbSMatthew G. Knepley }
3054a1cf66bbSMatthew G. Knepley 
3055a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3056a1cf66bbSMatthew G. Knepley {
3057a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3058a1cf66bbSMatthew G. Knepley   *geom = NULL;
3059a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3060a1cf66bbSMatthew G. Knepley }
3061a1cf66bbSMatthew G. Knepley 
306292d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
306392d50984SMatthew G. Knepley {
306492d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
306592d50984SMatthew G. Knepley   const char      *name       = "Residual";
306692d50984SMatthew G. Knepley   DM               dmAux      = NULL;
306792d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
306892d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
306992d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
307092d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
307192d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
307292d50984SMatthew G. Knepley   DMField          coordField = NULL;
3073c0006e53SPatrick Farrell   Vec              locA;
3074c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
307592d50984SMatthew G. Knepley   IS               chunkIS;
307692d50984SMatthew G. Knepley   const PetscInt  *cells;
307792d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3078364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
307992d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
308092d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
308192d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
308292d50984SMatthew G. Knepley   PetscErrorCode   ierr;
308392d50984SMatthew G. Knepley 
308492d50984SMatthew G. Knepley   PetscFunctionBegin;
308592d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
308692d50984SMatthew G. Knepley   /* FEM+FVM */
308792d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
308892d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
308992d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
309092d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
309192d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
309292d50984SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
309392d50984SMatthew G. Knepley   if (locA) {
309492d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
309592d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
309692d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
309792d50984SMatthew G. Knepley   }
309892d50984SMatthew G. Knepley   /* 2: Get geometric data */
309992d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
310092d50984SMatthew G. Knepley     PetscObject  obj;
310192d50984SMatthew G. Knepley     PetscClassId id;
310292d50984SMatthew G. Knepley     PetscBool    fimp;
310392d50984SMatthew G. Knepley 
310492d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
310592d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
310692d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
310792d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
310892d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
3109364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
311092d50984SMatthew G. Knepley   }
311192d50984SMatthew G. Knepley   if (useFEM) {
311292d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
311392d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
311492d50984SMatthew G. Knepley     if (maxDegree <= 1) {
311592d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
311692d50984SMatthew G. Knepley       if (affineQuad) {
311792d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
311892d50984SMatthew G. Knepley       }
311992d50984SMatthew G. Knepley     } else {
312092d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
312192d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
312292d50984SMatthew G. Knepley         PetscObject  obj;
312392d50984SMatthew G. Knepley         PetscClassId id;
312492d50984SMatthew G. Knepley         PetscBool    fimp;
312592d50984SMatthew G. Knepley 
312692d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
312792d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
312892d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
312992d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
313092d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
313192d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
313292d50984SMatthew G. Knepley 
313392d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
313492d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
313592d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
313692d50984SMatthew G. Knepley         }
313792d50984SMatthew G. Knepley       }
313892d50984SMatthew G. Knepley     }
313992d50984SMatthew G. Knepley   }
314092d50984SMatthew G. Knepley   /* Loop over chunks */
314192d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
314292d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
314392d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
314492d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
314592d50984SMatthew G. Knepley   numChunks     = 1;
314692d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
314792d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
314892d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
3149c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
3150c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
3151c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
315292d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
3153c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
315492d50984SMatthew G. Knepley 
315592d50984SMatthew G. Knepley     /* Extract field coefficients */
315692d50984SMatthew G. Knepley     if (useFEM) {
315792d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
315892d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
315992d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
316092d50984SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
316192d50984SMatthew G. Knepley     }
316292d50984SMatthew 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 */
316392d50984SMatthew G. Knepley     /* Loop over fields */
316492d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
316592d50984SMatthew G. Knepley       PetscObject  obj;
316692d50984SMatthew G. Knepley       PetscClassId id;
316792d50984SMatthew G. Knepley       PetscBool    fimp;
316892d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
316992d50984SMatthew G. Knepley 
317092d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
317192d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
317292d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
317392d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
317492d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
317592d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
317692d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
317792d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
317892d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
317992d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
318092d50984SMatthew G. Knepley 
318192d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
318292d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
318392d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
318492d50984SMatthew G. Knepley         blockSize = Nb;
318592d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
318692d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
318792d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
318892d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
318992d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
319092d50984SMatthew G. Knepley         offset    = numCells - Nr;
319192d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
319292d50984SMatthew 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) */
319392d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
319492d50984SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
319592d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
319692d50984SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
319792d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
319892d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
319992d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
320092d50984SMatthew G. Knepley 
320192d50984SMatthew G. Knepley         Ne = numFaces;
320292d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
320392d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
320492d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
320592d50984SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
320692d50984SMatthew G. Knepley     }
320792d50984SMatthew G. Knepley     /* Loop over domain */
320892d50984SMatthew G. Knepley     if (useFEM) {
320992d50984SMatthew G. Knepley       /* Add elemVec to locX */
321092d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
321192d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
321292d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
321392d50984SMatthew G. Knepley 
321492d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
321592d50984SMatthew G. Knepley         if (ghostLabel) {
321692d50984SMatthew G. Knepley           PetscInt ghostVal;
321792d50984SMatthew G. Knepley 
321892d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
321992d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
322092d50984SMatthew G. Knepley         }
322192d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
322292d50984SMatthew G. Knepley       }
322392d50984SMatthew G. Knepley     }
322492d50984SMatthew G. Knepley     /* Handle time derivative */
322592d50984SMatthew G. Knepley     if (locX_t) {
322692d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
322792d50984SMatthew G. Knepley 
322892d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
322992d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
323092d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
323192d50984SMatthew G. Knepley         PetscFV      fv;
323292d50984SMatthew G. Knepley         PetscObject  obj;
323392d50984SMatthew G. Knepley         PetscClassId id;
323492d50984SMatthew G. Knepley         PetscInt     pdim, d;
323592d50984SMatthew G. Knepley 
323692d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
323792d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
323892d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
323992d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
324092d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
324192d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
324292d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
324392d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
324492d50984SMatthew G. Knepley 
324592d50984SMatthew G. Knepley           if (ghostLabel) {
324692d50984SMatthew G. Knepley             PetscInt ghostVal;
324792d50984SMatthew G. Knepley 
324892d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
324992d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
325092d50984SMatthew G. Knepley           }
325192d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
325292d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
325392d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
325492d50984SMatthew G. Knepley         }
325592d50984SMatthew G. Knepley       }
325692d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
325792d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
325892d50984SMatthew G. Knepley     }
325992d50984SMatthew G. Knepley     if (useFEM) {
326092d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
326192d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
326292d50984SMatthew G. Knepley     }
326392d50984SMatthew G. Knepley   }
326492d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
326592d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
326692d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
326792d50984SMatthew G. Knepley   if (useFEM) {
326892d50984SMatthew G. Knepley     if (maxDegree <= 1) {
326992d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
327092d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
327192d50984SMatthew G. Knepley     } else {
327292d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
327392d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
327492d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
327592d50984SMatthew G. Knepley       }
327692d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
327792d50984SMatthew G. Knepley     }
327892d50984SMatthew G. Knepley   }
327992d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
328092d50984SMatthew G. Knepley   PetscFunctionReturn(0);
328192d50984SMatthew G. Knepley }
328292d50984SMatthew G. Knepley 
3283a1cf66bbSMatthew G. Knepley /*
3284a1cf66bbSMatthew 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
3285a1cf66bbSMatthew G. Knepley 
3286a1cf66bbSMatthew G. Knepley   X   - The local solution vector
3287a1cf66bbSMatthew G. Knepley   X_t - The local solution time derviative vector, or NULL
3288a1cf66bbSMatthew G. Knepley */
3289a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
3290a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
3291a1cf66bbSMatthew G. Knepley {
3292a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
3293a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
3294a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
3295a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
3296a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
3297a1cf66bbSMatthew G. Knepley   Vec              A;
3298a1cf66bbSMatthew G. Knepley   DMField          coordField;
3299a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
3300a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
3301a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
3302a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
33039b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
3304a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
33059b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
3306a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
3307a1cf66bbSMatthew G. Knepley 
3308a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3309a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3310a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
3311a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
3312a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3313a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3314a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3315a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3316a1cf66bbSMatthew G. Knepley   if (dmAux) {
3317a1cf66bbSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
3318a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
3319a1cf66bbSMatthew G. Knepley   }
3320a1cf66bbSMatthew G. Knepley   /* Get flags */
3321a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
3322a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3323a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
3324a1cf66bbSMatthew G. Knepley     PetscObject  disc;
3325a1cf66bbSMatthew G. Knepley     PetscClassId id;
3326a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
3327a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
3328a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
3329a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
3330a1cf66bbSMatthew G. Knepley   }
3331a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
3332a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
3333a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
3334a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
3335a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
3336a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
3337a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
3338a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
3339a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
3340a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
3341a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
3342a1cf66bbSMatthew 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 */
3343a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3344a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3345a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3346a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
3347a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3348a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
3349a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
3350a1cf66bbSMatthew G. Knepley     PetscFE         fe;
3351a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
3352a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
3353a1cf66bbSMatthew G. Knepley 
3354a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3355a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
3356a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
3357a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
3358a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
3359a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
3360a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
3361a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
3362a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
3363a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
3364a1cf66bbSMatthew G. Knepley   } else {
3365a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
3366a1cf66bbSMatthew G. Knepley     numChunks = 1;
3367a1cf66bbSMatthew G. Knepley   }
3368a1cf66bbSMatthew G. Knepley   /* Get work space */
3369a1cf66bbSMatthew 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;
3370a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
3371a1cf66bbSMatthew G. Knepley   ierr = PetscMemzero(work, wsz * sizeof(PetscScalar));CHKERRQ(ierr);
3372a1cf66bbSMatthew G. Knepley   off      = 0;
3373a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3374a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3375a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
3376a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3377a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3378a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3379a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
3380a1cf66bbSMatthew G. Knepley   /* Setup geometry */
3381a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
33829b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
33839b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
3384a1cf66bbSMatthew G. Knepley   if (!qGeom) {
3385a1cf66bbSMatthew G. Knepley     PetscFE fe;
3386a1cf66bbSMatthew G. Knepley 
3387a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3388a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
3389a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
3390a1cf66bbSMatthew G. Knepley   }
3391a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3392a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
3393a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
3394a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
3395a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
3396a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
3397a1cf66bbSMatthew G. Knepley     PetscInt         c;
3398a1cf66bbSMatthew G. Knepley 
3399a1cf66bbSMatthew G. Knepley     /* Extract values */
3400a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3401a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3402a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
3403a1cf66bbSMatthew G. Knepley       PetscInt       i;
3404a1cf66bbSMatthew G. Knepley 
3405a1cf66bbSMatthew G. Knepley       if (X) {
3406a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3407a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
3408a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3409a1cf66bbSMatthew G. Knepley       }
3410a1cf66bbSMatthew G. Knepley       if (X_t) {
3411a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3412a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
3413a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3414a1cf66bbSMatthew G. Knepley       }
3415a1cf66bbSMatthew G. Knepley       if (dmAux) {
3416a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3417a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
3418a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3419a1cf66bbSMatthew G. Knepley       }
3420a1cf66bbSMatthew G. Knepley     }
3421a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3422a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
3423a1cf66bbSMatthew G. Knepley       PetscFE fe;
3424a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
3425a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
3426a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN,     fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
3427a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_PRE, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);}
3428a1cf66bbSMatthew G. Knepley         if (hasDyn)  {ierr = PetscFEIntegrateJacobian(fe, prob, PETSCFE_JACOBIAN_DYN, fieldI, fieldJ, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);}
3429a1cf66bbSMatthew G. Knepley       }
3430a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
3431a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
3432a1cf66bbSMatthew G. Knepley         PetscFV  fv;
3433a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
3434a1cf66bbSMatthew G. Knepley 
3435a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
3436a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
3437a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
3438a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
3439a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
3440a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
3441a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
3442a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
3443a1cf66bbSMatthew G. Knepley           }
3444a1cf66bbSMatthew G. Knepley         }
3445a1cf66bbSMatthew G. Knepley       }
3446a1cf66bbSMatthew G. Knepley     }
3447a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3448a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
3449a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
3450a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
3451a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3452a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3453a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
3454a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3455a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3456a1cf66bbSMatthew G. Knepley       }
3457a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
3458a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
3459a1cf66bbSMatthew G. Knepley     }
3460a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3461a1cf66bbSMatthew G. Knepley   }
3462a1cf66bbSMatthew G. Knepley   /* Cleanup */
3463a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3464a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
3465a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
3466a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3467a1cf66bbSMatthew 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);
3468a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
3469a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
3470a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
3471a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
3472a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3473a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3474a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3475a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3476a1cf66bbSMatthew G. Knepley }
3477