xref: /petsc/src/dm/impls/plex/plexfem.c (revision ca3d3a14de63d3f97e9c05ca58d776b1c45e4686)
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 
92f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy)
102f856554SMatthew G. Knepley {
112f856554SMatthew G. Knepley   PetscBool      isPlex;
122f856554SMatthew G. Knepley   PetscErrorCode ierr;
132f856554SMatthew G. Knepley 
142f856554SMatthew G. Knepley   PetscFunctionBegin;
152f856554SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
162f856554SMatthew G. Knepley   if (isPlex) {
172f856554SMatthew G. Knepley     *plex = dm;
182f856554SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
192f856554SMatthew G. Knepley   } else {
202f856554SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
212f856554SMatthew G. Knepley     if (!*plex) {
222f856554SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
232f856554SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
242f856554SMatthew G. Knepley       if (copy) {
252f856554SMatthew G. Knepley         const char *comps[3] = {"A", "dmAux"};
262f856554SMatthew G. Knepley         PetscObject obj;
272f856554SMatthew G. Knepley         PetscInt    i;
282f856554SMatthew G. Knepley 
292f856554SMatthew G. Knepley         {
302f856554SMatthew G. Knepley           /* Run the subdomain hook (this will copy the DMSNES/DMTS) */
312f856554SMatthew G. Knepley           DMSubDomainHookLink link;
322f856554SMatthew G. Knepley           for (link = dm->subdomainhook; link; link = link->next) {
332f856554SMatthew G. Knepley             if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);}
342f856554SMatthew G. Knepley           }
352f856554SMatthew G. Knepley         }
362f856554SMatthew G. Knepley         for (i = 0; i < 3; i++) {
372f856554SMatthew G. Knepley           ierr = PetscObjectQuery((PetscObject) dm, comps[i], &obj);CHKERRQ(ierr);
382f856554SMatthew G. Knepley           ierr = PetscObjectCompose((PetscObject) *plex, comps[i], obj);CHKERRQ(ierr);
392f856554SMatthew G. Knepley         }
402f856554SMatthew G. Knepley       }
412f856554SMatthew G. Knepley     } else {
422f856554SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
432f856554SMatthew G. Knepley     }
442f856554SMatthew G. Knepley   }
452f856554SMatthew G. Knepley   PetscFunctionReturn(0);
462f856554SMatthew G. Knepley }
472f856554SMatthew 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 
350*ca3d3a14SMatthew G. Knepley typedef struct {
351*ca3d3a14SMatthew G. Knepley   PetscReal    alpha; /* The first Euler angle, and in 2D the only one */
352*ca3d3a14SMatthew G. Knepley   PetscReal    beta;  /* The second Euler angle */
353*ca3d3a14SMatthew G. Knepley   PetscReal    gamma; /* The third Euler angle */
354*ca3d3a14SMatthew G. Knepley   PetscInt     dim;   /* The dimension of R */
355*ca3d3a14SMatthew G. Knepley   PetscScalar *R;     /* The rotation matrix, transforming a vector in the local basis to the global basis */
356*ca3d3a14SMatthew G. Knepley   PetscScalar *RT;    /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */
357*ca3d3a14SMatthew G. Knepley } RotCtx;
358*ca3d3a14SMatthew G. Knepley 
359*ca3d3a14SMatthew G. Knepley /*
360*ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
361*ca3d3a14SMatthew G. Knepley   we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows:
362*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis.
363*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis.
364*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
365*ca3d3a14SMatthew G. Knepley */
366*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx)
367*ca3d3a14SMatthew G. Knepley {
368*ca3d3a14SMatthew G. Knepley   RotCtx        *rc  = (RotCtx *) ctx;
369*ca3d3a14SMatthew G. Knepley   PetscInt       dim = rc->dim;
370*ca3d3a14SMatthew G. Knepley   PetscReal      c1, s1, c2, s2, c3, s3;
371*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
372*ca3d3a14SMatthew G. Knepley 
373*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
374*ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr);
375*ca3d3a14SMatthew G. Knepley   switch (dim) {
376*ca3d3a14SMatthew G. Knepley   case 2:
377*ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
378*ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1;rc->R[1] = s1;
379*ca3d3a14SMatthew G. Knepley     rc->R[2] = -s1;rc->R[3] = c1;
380*ca3d3a14SMatthew G. Knepley     ierr = PetscMemcpy(rc->RT, rc->R, PetscSqr(dim) * sizeof(PetscScalar));CHKERRQ(ierr);
381*ca3d3a14SMatthew G. Knepley     DMPlex_Transpose2D_Internal(rc->RT);break;
382*ca3d3a14SMatthew G. Knepley     break;
383*ca3d3a14SMatthew G. Knepley   case 3:
384*ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
385*ca3d3a14SMatthew G. Knepley     c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta);
386*ca3d3a14SMatthew G. Knepley     c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma);
387*ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1*c3 - c2*s1*s3;rc->R[1] =  c3*s1    + c1*c2*s3;rc->R[2] = s2*s3;
388*ca3d3a14SMatthew G. Knepley     rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] =  c1*c2*c3 - s1*s3;   rc->R[5] = c3*s2;
389*ca3d3a14SMatthew G. Knepley     rc->R[6] =  s1*s2;           rc->R[7] = -c1*s2;              rc->R[8] = c2;
390*ca3d3a14SMatthew G. Knepley     ierr = PetscMemcpy(rc->RT, rc->R, PetscSqr(dim) * sizeof(PetscScalar));CHKERRQ(ierr);
391*ca3d3a14SMatthew G. Knepley     DMPlex_Transpose3D_Internal(rc->RT);break;
392*ca3d3a14SMatthew G. Knepley     break;
393*ca3d3a14SMatthew G. Knepley   default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
394*ca3d3a14SMatthew G. Knepley   }
395*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
396*ca3d3a14SMatthew G. Knepley }
397*ca3d3a14SMatthew G. Knepley 
398*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx)
399*ca3d3a14SMatthew G. Knepley {
400*ca3d3a14SMatthew G. Knepley   RotCtx        *rc = (RotCtx *) ctx;
401*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
402*ca3d3a14SMatthew G. Knepley 
403*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
404*ca3d3a14SMatthew G. Knepley   ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr);
405*ca3d3a14SMatthew G. Knepley   ierr = PetscFree(rc);CHKERRQ(ierr);
406*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
407*ca3d3a14SMatthew G. Knepley }
408*ca3d3a14SMatthew G. Knepley 
409*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx)
410*ca3d3a14SMatthew G. Knepley {
411*ca3d3a14SMatthew G. Knepley   RotCtx *rc = (RotCtx *) ctx;
412*ca3d3a14SMatthew G. Knepley 
413*ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
414*ca3d3a14SMatthew G. Knepley   PetscValidPointer(ctx, 5);
415*ca3d3a14SMatthew G. Knepley   if (l2g) {*A = rc->R;}
416*ca3d3a14SMatthew G. Knepley   else     {*A = rc->RT;}
417*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
418*ca3d3a14SMatthew G. Knepley }
419*ca3d3a14SMatthew G. Knepley 
420*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx)
421*ca3d3a14SMatthew G. Knepley {
422*ca3d3a14SMatthew G. Knepley   const PetscScalar *A;
423*ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
424*ca3d3a14SMatthew G. Knepley 
425*ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
426*ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr);
427*ca3d3a14SMatthew G. Knepley   switch (dim) {
428*ca3d3a14SMatthew G. Knepley   case 2: DMPlex_Mult2D_Internal(A, y, z);break;
429*ca3d3a14SMatthew G. Knepley   case 3: DMPlex_Mult3D_Internal(A, y, z);break;
430*ca3d3a14SMatthew G. Knepley   }
431*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
432*ca3d3a14SMatthew G. Knepley }
433*ca3d3a14SMatthew G. Knepley 
434*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a)
435*ca3d3a14SMatthew G. Knepley {
436*ca3d3a14SMatthew G. Knepley   PetscSection       ts;
437*ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tva;
438*ca3d3a14SMatthew G. Knepley   PetscInt           dof;
439*ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
440*ca3d3a14SMatthew G. Knepley 
441*ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
442*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(tdm, &ts);CHKERRQ(ierr);
443*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
444*ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
445*ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, (void *) &tva);CHKERRQ(ierr);
446*ca3d3a14SMatthew G. Knepley   if (l2g) {
447*ca3d3a14SMatthew G. Knepley     switch (dof) {
448*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_Mult2D_Internal(tva, a, a);break;
449*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_Mult3D_Internal(tva, a, a);break;
450*ca3d3a14SMatthew G. Knepley     }
451*ca3d3a14SMatthew G. Knepley   } else {
452*ca3d3a14SMatthew G. Knepley     switch (dof) {
453*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MultTranspose2D_Internal(tva, a, a);break;
454*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MultTranspose3D_Internal(tva, a, a);break;
455*ca3d3a14SMatthew G. Knepley     }
456*ca3d3a14SMatthew G. Knepley   }
457*ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
458*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
459*ca3d3a14SMatthew G. Knepley }
460*ca3d3a14SMatthew G. Knepley 
461*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformFieldTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt pf, PetscInt f, PetscInt pg, PetscInt g, PetscBool l2g, PetscInt lda, PetscScalar *a)
462*ca3d3a14SMatthew G. Knepley {
463*ca3d3a14SMatthew G. Knepley   PetscSection       s, ts;
464*ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tvaf, *tvag;
465*ca3d3a14SMatthew G. Knepley   PetscInt           fdof, gdof, fpdof, gpdof;
466*ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
467*ca3d3a14SMatthew G. Knepley 
468*ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
469*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
470*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(tdm, &ts);CHKERRQ(ierr);
471*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr);
472*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr);
473*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr);
474*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr);
475*ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
476*ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, (void *) &tvaf);CHKERRQ(ierr);
477*ca3d3a14SMatthew G. Knepley   ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, (void *) &tvag);CHKERRQ(ierr);
478*ca3d3a14SMatthew G. Knepley   if (l2g) {
479*ca3d3a14SMatthew G. Knepley     switch (fdof) {
480*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break;
481*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break;
482*ca3d3a14SMatthew G. Knepley     }
483*ca3d3a14SMatthew G. Knepley     switch (gdof) {
484*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break;
485*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break;
486*ca3d3a14SMatthew G. Knepley     }
487*ca3d3a14SMatthew G. Knepley   } else {
488*ca3d3a14SMatthew G. Knepley     switch (fdof) {
489*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break;
490*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break;
491*ca3d3a14SMatthew G. Knepley     }
492*ca3d3a14SMatthew G. Knepley     switch (gdof) {
493*ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break;
494*ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break;
495*ca3d3a14SMatthew G. Knepley     }
496*ca3d3a14SMatthew G. Knepley   }
497*ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
498*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
499*ca3d3a14SMatthew G. Knepley }
500*ca3d3a14SMatthew G. Knepley 
501*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a)
502*ca3d3a14SMatthew G. Knepley {
503*ca3d3a14SMatthew G. Knepley   PetscSection    s;
504*ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
505*ca3d3a14SMatthew G. Knepley   IS              clPoints;
506*ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
507*ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
508*ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, Np, cp, dof, d = 0;
509*ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
510*ca3d3a14SMatthew G. Knepley 
511*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
512*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
513*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
514*ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
515*ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
516*ca3d3a14SMatthew G. Knepley     for (cp = 0; cp < Np*2; cp += 2) {
517*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr);
518*ca3d3a14SMatthew G. Knepley       if (!dof) continue;
519*ca3d3a14SMatthew G. Knepley       if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);}
520*ca3d3a14SMatthew G. Knepley       d += dof;
521*ca3d3a14SMatthew G. Knepley     }
522*ca3d3a14SMatthew G. Knepley   }
523*ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
524*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
525*ca3d3a14SMatthew G. Knepley }
526*ca3d3a14SMatthew G. Knepley 
527*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a)
528*ca3d3a14SMatthew G. Knepley {
529*ca3d3a14SMatthew G. Knepley   PetscSection    s;
530*ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
531*ca3d3a14SMatthew G. Knepley   IS              clPoints;
532*ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
533*ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
534*ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c;
535*ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
536*ca3d3a14SMatthew G. Knepley 
537*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
538*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
539*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
540*ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
541*ca3d3a14SMatthew G. Knepley   for (f = 0, r = 0; f < Nf; ++f) {
542*ca3d3a14SMatthew G. Knepley     for (cpf = 0; cpf < Np*2; cpf += 2) {
543*ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr);
544*ca3d3a14SMatthew G. Knepley       for (g = 0, c = 0; g < Nf; ++g) {
545*ca3d3a14SMatthew G. Knepley         for (cpg = 0; cpg < Np*2; cpg += 2) {
546*ca3d3a14SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr);
547*ca3d3a14SMatthew G. Knepley           ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr);
548*ca3d3a14SMatthew G. Knepley           c += gdof;
549*ca3d3a14SMatthew G. Knepley         }
550*ca3d3a14SMatthew G. Knepley       }
551*ca3d3a14SMatthew G. Knepley       if (c != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda);
552*ca3d3a14SMatthew G. Knepley       r += fdof;
553*ca3d3a14SMatthew G. Knepley     }
554*ca3d3a14SMatthew G. Knepley   }
555*ca3d3a14SMatthew G. Knepley   if (r != lda) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda);
556*ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
557*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
558*ca3d3a14SMatthew G. Knepley }
559*ca3d3a14SMatthew G. Knepley 
560*ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g)
561*ca3d3a14SMatthew G. Knepley {
562*ca3d3a14SMatthew G. Knepley   DM                 tdm;
563*ca3d3a14SMatthew G. Knepley   Vec                tv;
564*ca3d3a14SMatthew G. Knepley   PetscSection       ts, s;
565*ca3d3a14SMatthew G. Knepley   const PetscScalar *ta;
566*ca3d3a14SMatthew G. Knepley   PetscScalar       *a, *va;
567*ca3d3a14SMatthew G. Knepley   PetscInt           pStart, pEnd, p, Nf, f;
568*ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
569*ca3d3a14SMatthew G. Knepley 
570*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
571*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
572*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
573*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(tdm, &ts);CHKERRQ(ierr);
574*ca3d3a14SMatthew G. Knepley   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
575*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
576*ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
577*ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(lv, &a);CHKERRQ(ierr);
578*ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
579*ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
580*ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
581*ca3d3a14SMatthew G. Knepley       ierr = DMPlexPointLocalFieldRef(dm, p, f, a, (void *) &va);CHKERRQ(ierr);
582*ca3d3a14SMatthew G. Knepley       ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr);
583*ca3d3a14SMatthew G. Knepley     }
584*ca3d3a14SMatthew G. Knepley   }
585*ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr);
586*ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
587*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
588*ca3d3a14SMatthew G. Knepley }
589*ca3d3a14SMatthew G. Knepley 
590*ca3d3a14SMatthew G. Knepley /*@
591*ca3d3a14SMatthew G. Knepley   DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis
592*ca3d3a14SMatthew G. Knepley 
593*ca3d3a14SMatthew G. Knepley   Input Parameters:
594*ca3d3a14SMatthew G. Knepley + dm - The DM
595*ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis
596*ca3d3a14SMatthew G. Knepley 
597*ca3d3a14SMatthew G. Knepley   Output Parameters:
598*ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis
599*ca3d3a14SMatthew G. Knepley 
600*ca3d3a14SMatthew G. Knepley   Level: developer
601*ca3d3a14SMatthew G. Knepley 
602*ca3d3a14SMatthew G. Knepley .seealso: DMPlexLocalToGlobalBasis(), DMGetSection()
603*ca3d3a14SMatthew G. Knepley @*/
604*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv)
605*ca3d3a14SMatthew G. Knepley {
606*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
607*ca3d3a14SMatthew G. Knepley 
608*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
609*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
610*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
611*ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr);
612*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
613*ca3d3a14SMatthew G. Knepley }
614*ca3d3a14SMatthew G. Knepley 
615*ca3d3a14SMatthew G. Knepley /*@
616*ca3d3a14SMatthew G. Knepley   DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis
617*ca3d3a14SMatthew G. Knepley 
618*ca3d3a14SMatthew G. Knepley   Input Parameters:
619*ca3d3a14SMatthew G. Knepley + dm - The DM
620*ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis
621*ca3d3a14SMatthew G. Knepley 
622*ca3d3a14SMatthew G. Knepley   Output Parameters:
623*ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis
624*ca3d3a14SMatthew G. Knepley 
625*ca3d3a14SMatthew G. Knepley   Level: developer
626*ca3d3a14SMatthew G. Knepley 
627*ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMGetSection()
628*ca3d3a14SMatthew G. Knepley @*/
629*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv)
630*ca3d3a14SMatthew G. Knepley {
631*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
632*ca3d3a14SMatthew G. Knepley 
633*ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
634*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
635*ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
636*ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr);
637*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
638*ca3d3a14SMatthew G. Knepley }
639*ca3d3a14SMatthew G. Knepley 
640*ca3d3a14SMatthew G. Knepley /*@
641*ca3d3a14SMatthew G. Knepley   DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions
642*ca3d3a14SMatthew G. Knepley     and global solutions, to a local basis, appropriate for discretization integrals and assembly.
643*ca3d3a14SMatthew G. Knepley 
644*ca3d3a14SMatthew G. Knepley   Input Parameters:
645*ca3d3a14SMatthew G. Knepley + dm    - The DM
646*ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one
647*ca3d3a14SMatthew G. Knepley . beta  - The second Euler angle
648*ca3d3a14SMatthew G. Knepley . gamma - The third Euler angle
649*ca3d3a14SMatthew G. Knepley 
650*ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
651*ca3d3a14SMatthew G. Knepley   we rotate with respect to a fixed initial coordinate system, the local basis (x-y-z). The global basis (X-Y-Z) is reached as follows:
652*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates about the z axis by alpha. The X axis is now at angle alpha with respect to the x axis.
653*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates again about the x axis by beta. The Z axis is now at angle beta with respect to the z axis.
654*ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
655*ca3d3a14SMatthew G. Knepley 
656*ca3d3a14SMatthew G. Knepley   Level: developer
657*ca3d3a14SMatthew G. Knepley 
658*ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()
659*ca3d3a14SMatthew G. Knepley @*/
660*ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma)
661*ca3d3a14SMatthew G. Knepley {
662*ca3d3a14SMatthew G. Knepley   RotCtx        *rc;
663*ca3d3a14SMatthew G. Knepley   PetscInt       cdim;
664*ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
665*ca3d3a14SMatthew G. Knepley 
666*ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
667*ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr);
668*ca3d3a14SMatthew G. Knepley   dm->transformCtx       = rc;
669*ca3d3a14SMatthew G. Knepley   dm->transformSetUp     = DMPlexBasisTransformSetUp_Rotation_Internal;
670*ca3d3a14SMatthew G. Knepley   dm->transformDestroy   = DMPlexBasisTransformDestroy_Rotation_Internal;
671*ca3d3a14SMatthew G. Knepley   dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal;
672*ca3d3a14SMatthew G. Knepley   rc->dim   = cdim;
673*ca3d3a14SMatthew G. Knepley   rc->alpha = alpha;
674*ca3d3a14SMatthew G. Knepley   rc->beta  = beta;
675*ca3d3a14SMatthew G. Knepley   rc->gamma = gamma;
676*ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr);
677*ca3d3a14SMatthew G. Knepley   ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr);
678*ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
679*ca3d3a14SMatthew G. Knepley }
680*ca3d3a14SMatthew G. Knepley 
681b278463cSMatthew G. Knepley /*@C
682b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector
683b278463cSMatthew G. Knepley 
684b278463cSMatthew G. Knepley   Input Parameters:
685b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
686b278463cSMatthew G. Knepley . time   - The time
687b278463cSMatthew G. Knepley . field  - The field to constrain
6881c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
6891c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
690b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
691b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
692b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
693b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
694b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
695b278463cSMatthew G. Knepley 
696b278463cSMatthew G. Knepley   Output Parameter:
697b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
698b278463cSMatthew G. Knepley 
699b278463cSMatthew G. Knepley   Level: developer
700b278463cSMatthew G. Knepley 
701b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
702b278463cSMatthew G. Knepley @*/
7031c531cf8SMatthew 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)
70455f2e967SMatthew G. Knepley {
7050163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
70655f2e967SMatthew G. Knepley   void            **ctxs;
707d7ddef95SMatthew G. Knepley   PetscInt          numFields;
708d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
709d7ddef95SMatthew G. Knepley 
710d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
711d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
712d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
713d7ddef95SMatthew G. Knepley   funcs[field] = func;
714d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
7151c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
716d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
717d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
718d7ddef95SMatthew G. Knepley }
719d7ddef95SMatthew G. Knepley 
720b278463cSMatthew G. Knepley /*@C
721b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector
722b278463cSMatthew G. Knepley 
723b278463cSMatthew G. Knepley   Input Parameters:
724b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
725b278463cSMatthew G. Knepley . time   - The time
726b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
727b278463cSMatthew G. Knepley . field  - The field to constrain
7281c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7291c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
730b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
731b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
732b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
733b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
734b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
735b278463cSMatthew G. Knepley 
736b278463cSMatthew G. Knepley   Output Parameter:
737b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
738b278463cSMatthew G. Knepley 
739b278463cSMatthew G. Knepley   Level: developer
740b278463cSMatthew G. Knepley 
741b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMAddBoundary()
742b278463cSMatthew G. Knepley @*/
7431c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
744c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
745c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
746c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
74797b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
748b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
749b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
750c60e475cSMatthew G. Knepley {
751c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
752c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
753c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
75497b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
755c60e475cSMatthew G. Knepley   void            **ctxs;
756c60e475cSMatthew G. Knepley   PetscInt          numFields;
757c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
758c60e475cSMatthew G. Knepley 
759c60e475cSMatthew G. Knepley   PetscFunctionBegin;
760c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
761c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
762c60e475cSMatthew G. Knepley   funcs[field] = func;
763c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
7641c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
765c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
766c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
767c60e475cSMatthew G. Knepley }
768c60e475cSMatthew G. Knepley 
769b278463cSMatthew G. Knepley /*@C
770b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
771b278463cSMatthew G. Knepley 
772b278463cSMatthew G. Knepley   Input Parameters:
773b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
774b278463cSMatthew G. Knepley . time   - The time
775b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
776b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
777b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
778b278463cSMatthew G. Knepley . field  - The field to constrain
7791c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7801c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
781b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
782b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
783b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
784b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
785b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
786b278463cSMatthew G. Knepley 
787b278463cSMatthew G. Knepley   Output Parameter:
788b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
789b278463cSMatthew G. Knepley 
790b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
791b278463cSMatthew G. Knepley 
792b278463cSMatthew G. Knepley   Level: developer
793b278463cSMatthew G. Knepley 
794b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
795b278463cSMatthew G. Knepley @*/
7961c531cf8SMatthew 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[],
797b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
798d7ddef95SMatthew G. Knepley {
79961f58d28SMatthew G. Knepley   PetscDS            prob;
800da97024aSMatthew G. Knepley   PetscSF            sf;
801d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
80220369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
803da97024aSMatthew G. Knepley   const PetscInt    *leaves;
804d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
805520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
806e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr, ierru = 0;
807d7ddef95SMatthew G. Knepley 
808d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
809da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
810da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
811da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
812d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
813d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
81461f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
815d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
816d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
81720369375SToby Isaac   if (cellGeometry) {
818d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
819d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
82020369375SToby Isaac   }
821d7ddef95SMatthew G. Knepley   if (Grad) {
822c0a6632aSMatthew G. Knepley     PetscFV fv;
823c0a6632aSMatthew G. Knepley 
824c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
825d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
826d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
827d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
82869291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
829d7ddef95SMatthew G. Knepley   }
830d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
831d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
832d7ddef95SMatthew G. Knepley     IS              faceIS;
833d7ddef95SMatthew G. Knepley     const PetscInt *faces;
834d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
835d7ddef95SMatthew G. Knepley 
836d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
837d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
838d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
839d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
840d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
841d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
842640bce14SSatish Balay       PetscFVFaceGeom        *fg;
843d7ddef95SMatthew G. Knepley 
844d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
845da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
846da97024aSMatthew G. Knepley       if (loc >= 0) continue;
847d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
848d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
849d7ddef95SMatthew G. Knepley       if (Grad) {
850640bce14SSatish Balay         PetscFVCellGeom       *cg;
851640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
852d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
853d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
854d7ddef95SMatthew G. Knepley         PetscInt               d;
855d7ddef95SMatthew G. Knepley 
856d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
857d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
858d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
859520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
860d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
861d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
862e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);
863e735a8a9SMatthew G. Knepley         if (ierru) {
864e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
865e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
866e735a8a9SMatthew G. Knepley           goto cleanup;
867e735a8a9SMatthew G. Knepley         }
868d7ddef95SMatthew G. Knepley       } else {
869640bce14SSatish Balay         PetscScalar       *xI;
870d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
871d7ddef95SMatthew G. Knepley 
872d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
873520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
874e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
875e735a8a9SMatthew G. Knepley         if (ierru) {
876e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
877e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
878e735a8a9SMatthew G. Knepley           goto cleanup;
879e735a8a9SMatthew G. Knepley         }
880d7ddef95SMatthew G. Knepley       }
881d7ddef95SMatthew G. Knepley     }
882d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
883d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
884d7ddef95SMatthew G. Knepley   }
885e735a8a9SMatthew G. Knepley   cleanup:
886d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
887d7ddef95SMatthew G. Knepley   if (Grad) {
88869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
889d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
890d7ddef95SMatthew G. Knepley   }
891e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
892d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
893e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
894d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
895d7ddef95SMatthew G. Knepley }
896d7ddef95SMatthew G. Knepley 
897f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
898d7ddef95SMatthew G. Knepley {
899e5e52638SMatthew G. Knepley   PetscDS        prob;
900d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
90155f2e967SMatthew G. Knepley   PetscErrorCode ierr;
90255f2e967SMatthew G. Knepley 
90355f2e967SMatthew G. Knepley   PetscFunctionBegin;
904e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
905e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
90655f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
907f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
908648eda8cSMatthew G. Knepley     const char             *name, *labelname;
909d7ddef95SMatthew G. Knepley     DMLabel                 label;
9101c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
9111c531cf8SMatthew G. Knepley     const PetscInt         *comps;
912d7ddef95SMatthew G. Knepley     PetscObject             obj;
913d7ddef95SMatthew G. Knepley     PetscClassId            id;
914a30ec4eaSSatish Balay     void                    (*func)(void);
915d7ddef95SMatthew G. Knepley     PetscInt                numids;
916d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
91755f2e967SMatthew G. Knepley     void                   *ctx;
91855f2e967SMatthew G. Knepley 
919648eda8cSMatthew G. Knepley     ierr = DMGetBoundary(dm, b, &type, &name, &labelname, &field, &Nc, &comps, &func, &numids, &ids, &ctx);CHKERRQ(ierr);
920f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
921c58f1c22SToby Isaac     ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
922648eda8cSMatthew G. Knepley     if (!label) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "Label %s for boundary condition %s does not exist in the DM", labelname, name);
92344a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
924d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
925d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
926c60e475cSMatthew G. Knepley       switch (type) {
927c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
928c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
929092e5057SToby Isaac         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
9301c531cf8SMatthew 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);
931092e5057SToby Isaac         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
932c60e475cSMatthew G. Knepley         break;
933c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
934c60e475cSMatthew G. Knepley         ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
9351c531cf8SMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids,
936b278463cSMatthew G. Knepley                                                         (void (*)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
937c60e475cSMatthew G. Knepley                                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
93897b6e6e8SMatthew G. Knepley                                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[])) func, ctx, locX);CHKERRQ(ierr);
939c60e475cSMatthew G. Knepley         ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
940c60e475cSMatthew G. Knepley         break;
941c60e475cSMatthew G. Knepley       default: break;
942c60e475cSMatthew G. Knepley       }
943d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
94443ea7facSMatthew G. Knepley       if (!faceGeomFVM) continue;
9451c531cf8SMatthew G. Knepley       ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids,
946b278463cSMatthew G. Knepley                                                (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) func, ctx, locX);CHKERRQ(ierr);
947d7ddef95SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
94855f2e967SMatthew G. Knepley   }
94955f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
95055f2e967SMatthew G. Knepley }
95155f2e967SMatthew G. Knepley 
952f1d73a7aSMatthew G. Knepley /*@
953f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
954f1d73a7aSMatthew G. Knepley 
955f1d73a7aSMatthew G. Knepley   Input Parameters:
956f1d73a7aSMatthew G. Knepley + dm - The DM
957f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
958f1d73a7aSMatthew G. Knepley . time - The time
959f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
960f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
961f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
962f1d73a7aSMatthew G. Knepley 
963f1d73a7aSMatthew G. Knepley   Output Parameters:
964f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
965f1d73a7aSMatthew G. Knepley 
966f1d73a7aSMatthew G. Knepley   Level: developer
967f1d73a7aSMatthew G. Knepley 
968f1d73a7aSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
969f1d73a7aSMatthew G. Knepley @*/
970f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
971f1d73a7aSMatthew G. Knepley {
972f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
973f1d73a7aSMatthew G. Knepley 
974f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
975f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
976f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 2);
977f1d73a7aSMatthew G. Knepley   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 4);}
978f1d73a7aSMatthew G. Knepley   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 5);}
979f1d73a7aSMatthew G. Knepley   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 6);}
980f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
981f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
982f1d73a7aSMatthew G. Knepley }
983f1d73a7aSMatthew G. Knepley 
9840709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
985cb1e1211SMatthew G Knepley {
986574a98acSMatthew G. Knepley   Vec              localX;
987574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
988574a98acSMatthew G. Knepley 
989574a98acSMatthew G. Knepley   PetscFunctionBegin;
990574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
9915d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
992574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
993574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
994574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
995574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
996574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
997574a98acSMatthew G. Knepley }
998574a98acSMatthew G. Knepley 
999574a98acSMatthew G. Knepley /*@C
1000*ca3d3a14SMatthew G. Knepley   DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
1001574a98acSMatthew G. Knepley 
1002574a98acSMatthew G. Knepley   Input Parameters:
1003574a98acSMatthew G. Knepley + dm     - The DM
1004574a98acSMatthew G. Knepley . time   - The time
1005574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
1006574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
1007574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
1008574a98acSMatthew G. Knepley 
1009574a98acSMatthew G. Knepley   Output Parameter:
1010574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
1011574a98acSMatthew G. Knepley 
1012574a98acSMatthew G. Knepley   Level: developer
1013574a98acSMatthew G. Knepley 
1014574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
1015574a98acSMatthew G. Knepley @*/
1016574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
1017574a98acSMatthew G. Knepley {
10180f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1019*ca3d3a14SMatthew G. Knepley   DM               tdm;
1020*ca3d3a14SMatthew G. Knepley   Vec              tv;
1021cb1e1211SMatthew G Knepley   PetscSection     section;
1022c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
102315496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
1024*ca3d3a14SMatthew G. Knepley   PetscReal       *coords, *gcoords, *detJ, *J;
1025cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
10267318780aSToby Isaac   const PetscReal *quadWeights;
1027aed3cbd0SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1028*ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1029cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1030cb1e1211SMatthew G Knepley 
1031cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1032c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
10337318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1034e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1035cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1036*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1037*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1038*ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1039cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
104015496722SMatthew G. Knepley     PetscObject  obj;
104115496722SMatthew G. Knepley     PetscClassId id;
1042c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
1043c5bbbd5bSMatthew G. Knepley 
104444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
104515496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
104615496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
104715496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
104815496722SMatthew G. Knepley 
10490f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
10500f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
105115496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
105215496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
105315496722SMatthew G. Knepley 
105415496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
105515496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
105615496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1057c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
1058cb1e1211SMatthew G Knepley   }
10599c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
1060beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
10617318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
1062aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1063aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
10649ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10659ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1066cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
1067a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
1068cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
10699c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1070cb1e1211SMatthew G Knepley 
10717318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
1072cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1073cb1e1211SMatthew G Knepley 
107415496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
107515496722SMatthew G. Knepley       PetscObject  obj;
107615496722SMatthew G. Knepley       PetscClassId id;
1077c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
107815496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1079cb1e1211SMatthew G Knepley 
108044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
108115496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
108215496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
108315496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
108415496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1085cb1e1211SMatthew G Knepley       if (debug) {
1086cb1e1211SMatthew G Knepley         char title[1024];
1087cb1e1211SMatthew G Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
10884c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1089cb1e1211SMatthew G Knepley       }
10907318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
10917318780aSToby 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);
1092*ca3d3a14SMatthew G. Knepley         if (transform) {gcoords = &coords[coordDim*Nq];
1093*ca3d3a14SMatthew G. Knepley                         ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);}
1094*ca3d3a14SMatthew G. Knepley         else           {gcoords = &coords[coordDim*q];}
1095*ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1096e735a8a9SMatthew G. Knepley         if (ierr) {
1097e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1098e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1099e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
11007318780aSToby Isaac           ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
1101e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1102e735a8a9SMatthew G. Knepley         }
1103*ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
110415496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
110515496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
110615496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
110715496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1108beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1109beaa55a6SMatthew 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);}
1110beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
1111cb1e1211SMatthew G Knepley         }
1112cb1e1211SMatthew G Knepley       }
11139c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
1114beaa55a6SMatthew G. Knepley       qc += Nc;
1115cb1e1211SMatthew G Knepley     }
1116cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1117cb1e1211SMatthew G Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
1118cb1e1211SMatthew G Knepley     localDiff += elemDiff;
1119cb1e1211SMatthew G Knepley   }
11207318780aSToby Isaac   ierr  = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
1121b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1122cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
1123cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1124cb1e1211SMatthew G Knepley }
1125cb1e1211SMatthew G Knepley 
1126b698f381SToby 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)
1127cb1e1211SMatthew G Knepley {
11280f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1129*ca3d3a14SMatthew G. Knepley   DM               tdm;
1130cb1e1211SMatthew G Knepley   PetscSection     section;
113140e14135SMatthew G. Knepley   PetscQuadrature  quad;
1132*ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
11339c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
11349c3cf19fSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1135*ca3d3a14SMatthew G. Knepley   PetscReal       *coords, *gcoords, *realSpaceDer, *J, *invJ, *detJ;
113640e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
11379c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1138*ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1139cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1140cb1e1211SMatthew G Knepley 
1141cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1142c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
11437318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1144e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
114540e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
114640e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
114740e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
114840e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1149*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1150*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1151*ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1152652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
11530f2d7e86SMatthew G. Knepley     PetscFE  fe;
115440e14135SMatthew G. Knepley     PetscInt Nc;
1155652b88e8SMatthew G. Knepley 
115644a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
11570f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
11580f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
115940e14135SMatthew G. Knepley     numComponents += Nc;
1160652b88e8SMatthew G. Knepley   }
11619c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1162beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
11634d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
11649c3cf19fSMatthew 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);
116540e14135SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11669ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11679ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
116840e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
116940e14135SMatthew G. Knepley     PetscScalar *x = NULL;
117040e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
11719c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1172652b88e8SMatthew G. Knepley 
11737318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
117440e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
117540e14135SMatthew G. Knepley 
11769c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
11770f2d7e86SMatthew G. Knepley       PetscFE          fe;
117851259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
117940e14135SMatthew G. Knepley       PetscReal       *basisDer;
11809c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
118140e14135SMatthew G. Knepley 
118244a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
11830f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
11849c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
11850f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDefaultTabulation(fe, NULL, &basisDer, NULL);CHKERRQ(ierr);
118640e14135SMatthew G. Knepley       if (debug) {
118740e14135SMatthew G. Knepley         char title[1024];
118840e14135SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
11899c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1190652b88e8SMatthew G. Knepley       }
11919c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
11927318780aSToby 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);
1193*ca3d3a14SMatthew G. Knepley         if (transform) {gcoords = &coords[coordDim*Nq];
1194*ca3d3a14SMatthew G. Knepley                         ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);}
1195*ca3d3a14SMatthew G. Knepley         else           {gcoords = &coords[coordDim*q];}
11967318780aSToby Isaac         ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], n, numFields, funcVal, ctx);
1197e735a8a9SMatthew G. Knepley         if (ierr) {
1198e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1199e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1200e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
12019c3cf19fSMatthew G. Knepley           ierr2 = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr2);
1202e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1203e735a8a9SMatthew G. Knepley         }
1204*ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
12059c3cf19fSMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, &x[fieldOffset], coordDim, invJ, n, q, interpolant);CHKERRQ(ierr);
12069c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1207beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1208beaa55a6SMatthew 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);}
1209beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
121040e14135SMatthew G. Knepley         }
121140e14135SMatthew G. Knepley       }
12129c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
12139c3cf19fSMatthew G. Knepley       qc          += Nc;
121440e14135SMatthew G. Knepley     }
121540e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
121640e14135SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d diff %g\n", c, elemDiff);CHKERRQ(ierr);}
121740e14135SMatthew G. Knepley     localDiff += elemDiff;
121840e14135SMatthew G. Knepley   }
12199c3cf19fSMatthew G. Knepley   ierr  = PetscFree7(funcVal,coords,realSpaceDer,J,invJ,interpolant,detJ);CHKERRQ(ierr);
122040e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1221b2566f29SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
122240e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1223cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1224cb1e1211SMatthew G Knepley }
1225cb1e1211SMatthew G Knepley 
1226c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
122773d901b8SMatthew G. Knepley {
12280f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1229*ca3d3a14SMatthew G. Knepley   DM               tdm;
123073d901b8SMatthew G. Knepley   PetscSection     section;
123173d901b8SMatthew G. Knepley   PetscQuadrature  quad;
1232*ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
123315496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
1234*ca3d3a14SMatthew G. Knepley   PetscReal       *coords, *gcoords, *detJ, *J;
123573d901b8SMatthew G. Knepley   PetscReal       *localDiff;
123615496722SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
12379c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1238*ca3d3a14SMatthew G. Knepley   PetscBool        transform;
123973d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
124073d901b8SMatthew G. Knepley 
124173d901b8SMatthew G. Knepley   PetscFunctionBegin;
1242c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12437318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1244e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
124573d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
124673d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1247*ca3d3a14SMatthew G. Knepley   ierr = VecSet(localX, 0.0);CHKERRQ(ierr);
124873d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
124973d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1250*ca3d3a14SMatthew G. Knepley   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1251*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1252*ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1253*ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
125473d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
125515496722SMatthew G. Knepley     PetscObject  obj;
125615496722SMatthew G. Knepley     PetscClassId id;
125773d901b8SMatthew G. Knepley     PetscInt     Nc;
125873d901b8SMatthew G. Knepley 
125944a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
126015496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
126115496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
126215496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
126315496722SMatthew G. Knepley 
12640f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12650f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
126615496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
126715496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
126815496722SMatthew G. Knepley 
126915496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
127015496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
127115496722SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
127273d901b8SMatthew G. Knepley     numComponents += Nc;
127373d901b8SMatthew G. Knepley   }
12749c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1275beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
1276*ca3d3a14SMatthew G. Knepley   ierr = PetscCalloc6(numFields,&localDiff,numComponents,&funcVal,numComponents,&interpolant,coordDim*(Nq+1),&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
127773d901b8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
12789ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
12799ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
128073d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
128173d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
12829c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
128373d901b8SMatthew G. Knepley 
12847318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
128573d901b8SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
128673d901b8SMatthew G. Knepley 
128715496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
128815496722SMatthew G. Knepley       PetscObject  obj;
128915496722SMatthew G. Knepley       PetscClassId id;
129073d901b8SMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
129115496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
129273d901b8SMatthew G. Knepley 
129315496722SMatthew G. Knepley       PetscReal       elemDiff = 0.0;
129415496722SMatthew G. Knepley 
129544a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
129615496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
129715496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
129815496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
129915496722SMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
130073d901b8SMatthew G. Knepley       if (debug) {
130173d901b8SMatthew G. Knepley         char title[1024];
130273d901b8SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %d", field);CHKERRQ(ierr);
1303*ca3d3a14SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
130473d901b8SMatthew G. Knepley       }
13057318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
13067318780aSToby 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);
1307*ca3d3a14SMatthew G. Knepley         if (transform) {gcoords = &coords[coordDim*Nq];
1308*ca3d3a14SMatthew G. Knepley                         ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);}
1309*ca3d3a14SMatthew G. Knepley         else           {gcoords = &coords[coordDim*q];}
1310*ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, numFields, funcVal, ctx);
1311e735a8a9SMatthew G. Knepley         if (ierr) {
1312e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1313e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1314e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
13157318780aSToby Isaac           ierr2 = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
1316e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1317e735a8a9SMatthew G. Knepley         }
1318*ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
131915496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
132015496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
132115496722SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
132215496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1323beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1324*ca3d3a14SMatthew 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[coordDim*q] : 0., coordDim > 1 ? coords[coordDim*q+1] : 0., coordDim > 2 ? coords[coordDim*q+2] : 0., PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q]);CHKERRQ(ierr);}
1325beaa55a6SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
132673d901b8SMatthew G. Knepley         }
132773d901b8SMatthew G. Knepley       }
1328beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
13299c3cf19fSMatthew G. Knepley       qc          += Nc;
133073d901b8SMatthew G. Knepley       localDiff[field] += elemDiff;
1331*ca3d3a14SMatthew G. Knepley       if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %d field %d cum diff %g\n", c, field, localDiff[field]);CHKERRQ(ierr);}
133273d901b8SMatthew G. Knepley     }
133373d901b8SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
133473d901b8SMatthew G. Knepley   }
133573d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1336b2566f29SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, numFields, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
133773d901b8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) diff[field] = PetscSqrtReal(diff[field]);
13387318780aSToby Isaac   ierr = PetscFree6(localDiff,funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
133973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
134073d901b8SMatthew G. Knepley }
134173d901b8SMatthew G. Knepley 
1342e729f68cSMatthew G. Knepley /*@C
1343e729f68cSMatthew 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.
1344e729f68cSMatthew G. Knepley 
1345e729f68cSMatthew G. Knepley   Input Parameters:
1346e729f68cSMatthew G. Knepley + dm    - The DM
13470163fd50SMatthew G. Knepley . time  - The time
1348ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1349e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1350e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1351e729f68cSMatthew G. Knepley 
1352e729f68cSMatthew G. Knepley   Output Parameter:
1353e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1354e729f68cSMatthew G. Knepley 
1355e729f68cSMatthew G. Knepley   Level: developer
1356e729f68cSMatthew G. Knepley 
1357b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1358e729f68cSMatthew G. Knepley @*/
13590163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1360e729f68cSMatthew G. Knepley {
1361e729f68cSMatthew G. Knepley   PetscSection     section;
1362e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1363e729f68cSMatthew G. Knepley   Vec              localX;
1364e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
13657318780aSToby Isaac   PetscReal       *coords, *detJ, *J;
1366e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
13679c3cf19fSMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, c, field, fieldOffset;
1368e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1369e729f68cSMatthew G. Knepley 
1370e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1371e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1372e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13737318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1374e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1375e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1376e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1377bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1378e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1379e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1380e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1381e729f68cSMatthew G. Knepley     PetscObject  obj;
1382e729f68cSMatthew G. Knepley     PetscClassId id;
1383e729f68cSMatthew G. Knepley     PetscInt     Nc;
1384e729f68cSMatthew G. Knepley 
138544a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1386e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1387e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1388e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1389e729f68cSMatthew G. Knepley 
1390e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1391e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1392e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1393e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1394e729f68cSMatthew G. Knepley 
1395e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1396e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
1397e729f68cSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1398e729f68cSMatthew G. Knepley     numComponents += Nc;
1399e729f68cSMatthew G. Knepley   }
14009c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1401beaa55a6SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
14027318780aSToby Isaac   ierr = PetscMalloc5(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&detJ,coordDim*coordDim*Nq,&J);CHKERRQ(ierr);
1403e729f68cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1404e729f68cSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
1405e729f68cSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1406e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1407e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
14086f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
14099c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1410e729f68cSMatthew G. Knepley 
14117318780aSToby Isaac     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, J, NULL, detJ);CHKERRQ(ierr);
1412e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1413e729f68cSMatthew G. Knepley 
1414e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1415e729f68cSMatthew G. Knepley       PetscObject  obj;
1416e729f68cSMatthew G. Knepley       PetscClassId id;
1417e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1418e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1419e729f68cSMatthew G. Knepley 
142044a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1421e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1422e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1423e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
1424e729f68cSMatthew G. Knepley       else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
142523f34ed2SToby Isaac       if (funcs[field]) {
14267318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
14277f79407eSBarry 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);
1428274e8aaeSMatthew G. Knepley           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);
1429e735a8a9SMatthew G. Knepley           if (ierr) {
1430e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1431e735a8a9SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
14327f79407eSBarry Smith             ierr2 = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr2);
1433e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1434e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1435e735a8a9SMatthew G. Knepley           }
1436e729f68cSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1437e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
1438e729f68cSMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
1439e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1440beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1441beaa55a6SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*detJ[q];
1442e729f68cSMatthew G. Knepley           }
1443e729f68cSMatthew G. Knepley         }
144423f34ed2SToby Isaac       }
1445beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
14469c3cf19fSMatthew G. Knepley       qc          += Nc;
1447e729f68cSMatthew G. Knepley     }
1448e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
144923f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1450e729f68cSMatthew G. Knepley   }
14517318780aSToby Isaac   ierr = PetscFree5(funcVal,interpolant,coords,detJ,J);CHKERRQ(ierr);
1452e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1453e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1454e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1455e729f68cSMatthew G. Knepley }
1456e729f68cSMatthew G. Knepley 
14571555c271SMatthew G. Knepley /*@C
14581555c271SMatthew 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.
14591555c271SMatthew G. Knepley 
14601555c271SMatthew G. Knepley   Input Parameters:
14611555c271SMatthew G. Knepley + dm - The DM
14621555c271SMatthew G. Knepley - LocX  - The coefficient vector u_h
14631555c271SMatthew G. Knepley 
14641555c271SMatthew G. Knepley   Output Parameter:
14651555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
14661555c271SMatthew G. Knepley 
146795452b02SPatrick Sanan   Notes:
146895452b02SPatrick Sanan     Add citation to (Clement, 1975) and definition of the interpolant
14691555c271SMatthew 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
14701555c271SMatthew G. Knepley 
14711555c271SMatthew G. Knepley   Level: developer
14721555c271SMatthew G. Knepley 
14731555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
14741555c271SMatthew G. Knepley @*/
14751555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
14761555c271SMatthew G. Knepley {
1477db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1478db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
14791555c271SMatthew G. Knepley   DM               dmC;
14801555c271SMatthew G. Knepley   PetscSection     section;
14811555c271SMatthew G. Knepley   PetscQuadrature  quad;
14821555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
14831555c271SMatthew G. Knepley   PetscReal       *coords, *detJ, *J, *invJ;
14841555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
14851555c271SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, cEndInterior, vStart, vEnd, v, field, fieldOffset;
14861555c271SMatthew G. Knepley   PetscErrorCode   ierr;
14871555c271SMatthew G. Knepley 
14881555c271SMatthew G. Knepley   PetscFunctionBegin;
14891555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
14901555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
14911555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
14921555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
1493e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
14941555c271SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
14951555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
14961555c271SMatthew G. Knepley     PetscObject  obj;
14971555c271SMatthew G. Knepley     PetscClassId id;
14981555c271SMatthew G. Knepley     PetscInt     Nc;
14991555c271SMatthew G. Knepley 
150044a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
15011555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15021555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
15031555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
15041555c271SMatthew G. Knepley 
15051555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
15061555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
15071555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
15081555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
15091555c271SMatthew G. Knepley 
15101555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
15111555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
15121555c271SMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
15131555c271SMatthew G. Knepley     numComponents += Nc;
15141555c271SMatthew G. Knepley   }
15151555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
15161555c271SMatthew G. Knepley   if ((qNc != 1) && (qNc != numComponents)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
15171555c271SMatthew 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);
15181555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
15191555c271SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
15201555c271SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
15211555c271SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
15221555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
15231555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
15241555c271SMatthew G. Knepley     PetscInt   *star = NULL;
15251555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
15261555c271SMatthew G. Knepley 
15271555c271SMatthew G. Knepley     ierr = PetscMemzero(gradsum, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
15281555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
15291555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
15301555c271SMatthew G. Knepley       const PetscInt cell = star[st];
15311555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
15321555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
15331555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
15341555c271SMatthew G. Knepley 
15351555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
15361555c271SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, J, invJ, detJ);CHKERRQ(ierr);
15371555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
15381555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
15391555c271SMatthew G. Knepley         PetscObject  obj;
15401555c271SMatthew G. Knepley         PetscClassId id;
15411555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
15421555c271SMatthew G. Knepley 
15431555c271SMatthew G. Knepley         ierr = PetscMemzero(grad, coordDim*numComponents * sizeof(PetscScalar));CHKERRQ(ierr);
154444a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
15451555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
15461555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
15471555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
15481555c271SMatthew G. Knepley         else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
15491555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
15501555c271SMatthew 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);
15511555c271SMatthew G. Knepley           if (ierr) {
15521555c271SMatthew G. Knepley             PetscErrorCode ierr2;
15531555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
15541555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
15551555c271SMatthew G. Knepley             ierr2 = PetscFree4(interpolant,coords,detJ,J);CHKERRQ(ierr2);
15561555c271SMatthew G. Knepley             CHKERRQ(ierr);
15571555c271SMatthew G. Knepley           }
15581555c271SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, &x[fieldOffset], coordDim, invJ, NULL, q, interpolant);CHKERRQ(ierr);}
15591555c271SMatthew G. Knepley           else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", field);
15601555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
15611555c271SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+qc+fc];
15621555c271SMatthew G. Knepley 
15631555c271SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*detJ[q];
15641555c271SMatthew G. Knepley           }
15651555c271SMatthew G. Knepley           vol += quadWeights[q*qNc]*detJ[q];
15661555c271SMatthew G. Knepley         }
15671555c271SMatthew G. Knepley         fieldOffset += Nb;
15681555c271SMatthew G. Knepley         qc          += Nc;
15691555c271SMatthew G. Knepley       }
15701555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1571f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1572f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1573f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1574f8527842SMatthew G. Knepley         }
1575f8527842SMatthew G. Knepley       }
1576f8527842SMatthew G. Knepley       volsum += vol;
1577db1066baSMatthew G. Knepley       if (debug) {
15786d20ae03SJed Brown         ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D gradient: [", cell);CHKERRQ(ierr);
15791555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
15801555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
15811555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
15826d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
15831555c271SMatthew G. Knepley           }
15841555c271SMatthew G. Knepley         }
15851555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1586db1066baSMatthew G. Knepley       }
15871555c271SMatthew G. Knepley     }
15881555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
15891555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
15901555c271SMatthew G. Knepley     }
15911555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
15921555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
15931555c271SMatthew G. Knepley   }
15941555c271SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,detJ,J,invJ);CHKERRQ(ierr);
15951555c271SMatthew G. Knepley   PetscFunctionReturn(0);
15961555c271SMatthew G. Knepley }
15971555c271SMatthew G. Knepley 
1598338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
159973d901b8SMatthew G. Knepley {
1600338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
160161aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
160273d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
1603338f77d5SMatthew G. Knepley   Vec                locX,    locA;
1604c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
1605c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
1606338f77d5SMatthew G. Knepley   /* DS */
1607338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
1608338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
1609338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
1610338f77d5SMatthew G. Knepley   const PetscScalar *constants;
1611338f77d5SMatthew G. Knepley   /* Geometry */
1612c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
1613338f77d5SMatthew G. Knepley   DM                 dmGrad;
1614c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
1615338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
1616b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
1617338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
1618b7260050SToby Isaac   PetscInt           maxDegree;
1619c330f8ffSToby Isaac   DMField            coordField;
1620c330f8ffSToby Isaac   IS                 cellIS;
162173d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
162273d901b8SMatthew G. Knepley 
162373d901b8SMatthew G. Knepley   PetscFunctionBegin;
1624338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1625c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1626e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
162773d901b8SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
1628338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
1629b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1630b5a3613cSMatthew G. Knepley     PetscObject  obj;
1631b5a3613cSMatthew G. Knepley     PetscClassId id;
1632b5a3613cSMatthew G. Knepley 
1633b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1634b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1635338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
1636338f77d5SMatthew G. Knepley   }
1637338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
1638338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
1639338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
1640338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1641338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
1642338f77d5SMatthew G. Knepley   /* Read DS information */
1643338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
1644338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
1645338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
1646c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
1647338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
1648338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
1649338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
1650338f77d5SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
1651338f77d5SMatthew G. Knepley   if (dmAux) {
1652338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
1653338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
1654e87a4003SBarry Smith     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
1655338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
1656338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
1657338f77d5SMatthew G. Knepley   }
1658338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
1659338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
1660338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
1661338f77d5SMatthew G. Knepley   /* Read out geometry */
1662c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
1663b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
1664b7260050SToby Isaac   if (maxDegree <= 1) {
1665c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
1666c330f8ffSToby Isaac     if (affineQuad) {
1667c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1668338f77d5SMatthew G. Knepley     }
1669b5a3613cSMatthew G. Knepley   }
1670b5a3613cSMatthew G. Knepley   if (useFVM) {
1671338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
1672b5a3613cSMatthew G. Knepley     Vec       grad;
1673b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
1674b5a3613cSMatthew G. Knepley     PetscBool compGrad;
1675b5a3613cSMatthew G. Knepley 
1676338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
1677338f77d5SMatthew G. Knepley       PetscObject  obj;
1678338f77d5SMatthew G. Knepley       PetscClassId id;
1679338f77d5SMatthew G. Knepley 
1680338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1681338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1682338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
1683338f77d5SMatthew G. Knepley     }
1684338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
1685338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
1686b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
1687338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
1688338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
1689b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1690b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
1691b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1692b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1693338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
1694b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
1695b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1696b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1697b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
1698b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
1699b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
1700338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
1701b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1702b5a3613cSMatthew G. Knepley   }
1703338f77d5SMatthew G. Knepley   /* Read out data from inputs */
170473d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
170573d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
170673d901b8SMatthew G. Knepley     PetscInt     i;
170773d901b8SMatthew G. Knepley 
1708338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
17090f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
1710338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
171173d901b8SMatthew G. Knepley     if (dmAux) {
1712338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
17130f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
1714338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
171573d901b8SMatthew G. Knepley     }
171673d901b8SMatthew G. Knepley   }
1717338f77d5SMatthew G. Knepley   /* Do integration for each field */
171873d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1719c1f031eeSMatthew G. Knepley     PetscObject  obj;
1720c1f031eeSMatthew G. Knepley     PetscClassId id;
1721c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
172273d901b8SMatthew G. Knepley 
1723c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
1724c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1725c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1726c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
1727c1f031eeSMatthew G. Knepley       PetscQuadrature q;
1728c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
1729c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
1730c1f031eeSMatthew G. Knepley 
17310f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
1732c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
17339c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
1734c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
1735c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
173673d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
17370f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
173873d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
173973d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
174073d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
174173d901b8SMatthew G. Knepley       offset    = numCells - Nr;
1742c330f8ffSToby Isaac       if (!affineQuad) {
1743c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
1744c330f8ffSToby Isaac       }
1745c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
1746c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
1747c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1748c330f8ffSToby Isaac       ierr = PetscFEIntegrate(fe, prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
1749c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
1750c330f8ffSToby Isaac       if (!affineQuad) {
1751c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1752c330f8ffSToby Isaac       }
1753c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1754c1f031eeSMatthew G. Knepley       PetscInt       foff;
1755420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
1756b69edc29SMatthew G. Knepley       PetscScalar    lint;
1757c1f031eeSMatthew G. Knepley 
1758c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
1759c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
1760c1f031eeSMatthew G. Knepley       if (obj_func) {
1761c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
1762b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
1763b5a3613cSMatthew G. Knepley 
1764b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
1765338f77d5SMatthew 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);
1766338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
176773d901b8SMatthew G. Knepley         }
1768c1f031eeSMatthew G. Knepley       }
1769c1f031eeSMatthew G. Knepley     } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
1770c1f031eeSMatthew G. Knepley   }
1771338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
1772b5a3613cSMatthew G. Knepley   if (useFVM) {
1773b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
1774b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
1775b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
1776b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
1777b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
1778b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
1779b5a3613cSMatthew G. Knepley   }
178073d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
1781338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
1782338f77d5SMatthew G. Knepley   /* Cleanup */
1783f99c8401SToby Isaac   if (affineQuad) {
1784f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
1785f99c8401SToby Isaac   }
1786f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
1787c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
1788338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
1789338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1790338f77d5SMatthew G. Knepley }
1791338f77d5SMatthew G. Knepley 
1792338f77d5SMatthew G. Knepley /*@
1793338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
1794338f77d5SMatthew G. Knepley 
1795338f77d5SMatthew G. Knepley   Input Parameters:
1796338f77d5SMatthew G. Knepley + dm - The mesh
1797338f77d5SMatthew G. Knepley . X  - Global input vector
1798338f77d5SMatthew G. Knepley - user - The user context
1799338f77d5SMatthew G. Knepley 
1800338f77d5SMatthew G. Knepley   Output Parameter:
1801338f77d5SMatthew G. Knepley . integral - Integral for each field
1802338f77d5SMatthew G. Knepley 
1803338f77d5SMatthew G. Knepley   Level: developer
1804338f77d5SMatthew G. Knepley 
1805338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1806338f77d5SMatthew G. Knepley @*/
1807b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
1808338f77d5SMatthew G. Knepley {
1809338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1810b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
1811338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1812338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1813338f77d5SMatthew G. Knepley 
1814338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1815338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1816338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1817338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
1818338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1819338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1820338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1821338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1822338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1823338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1824338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1825338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1826338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1827338f77d5SMatthew G. Knepley   /* Sum up values */
1828338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1829338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1830338f77d5SMatthew G. Knepley 
1831338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1832b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
1833338f77d5SMatthew G. Knepley   }
1834b8feb594SMatthew G. Knepley   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
183573d901b8SMatthew G. Knepley   if (mesh->printFEM) {
1836338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
1837b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
183873d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
183973d901b8SMatthew G. Knepley   }
1840338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
1841338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1842338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
1843338f77d5SMatthew G. Knepley }
1844338f77d5SMatthew G. Knepley 
1845338f77d5SMatthew G. Knepley /*@
1846338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
1847338f77d5SMatthew G. Knepley 
1848338f77d5SMatthew G. Knepley   Input Parameters:
1849338f77d5SMatthew G. Knepley + dm - The mesh
1850338f77d5SMatthew G. Knepley . X  - Global input vector
1851338f77d5SMatthew G. Knepley - user - The user context
1852338f77d5SMatthew G. Knepley 
1853338f77d5SMatthew G. Knepley   Output Parameter:
1854338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
1855338f77d5SMatthew G. Knepley 
1856338f77d5SMatthew G. Knepley   Level: developer
1857338f77d5SMatthew G. Knepley 
1858338f77d5SMatthew G. Knepley .seealso: DMPlexComputeResidualFEM()
1859338f77d5SMatthew G. Knepley @*/
1860338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
1861338f77d5SMatthew G. Knepley {
1862338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1863338f77d5SMatthew G. Knepley   DM             dmF;
1864338f77d5SMatthew G. Knepley   PetscSection   sectionF;
1865338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
1866338f77d5SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cEndInterior[4], cell;
1867338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
1868338f77d5SMatthew G. Knepley 
1869338f77d5SMatthew G. Knepley   PetscFunctionBegin;
1870338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1871338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
1872338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
1873338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
1874338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1875338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1876338f77d5SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1877338f77d5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior[0], &cEndInterior[1], &cEndInterior[2], &cEndInterior[3]);CHKERRQ(ierr);
1878338f77d5SMatthew G. Knepley   cEnd = cEndInterior[cellHeight] < 0 ? cEnd : cEndInterior[cellHeight];
1879338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
1880338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
1881338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
1882338f77d5SMatthew G. Knepley   /* Put values in F*/
1883338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
1884e87a4003SBarry Smith   ierr = DMGetSection(dmF, &sectionF);CHKERRQ(ierr);
1885338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
1886338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
1887338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
1888338f77d5SMatthew G. Knepley     PetscInt       dof, off;
1889338f77d5SMatthew G. Knepley 
1890338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
1891338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
1892338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
1893338f77d5SMatthew G. Knepley     if (dof != Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
1894338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
1895338f77d5SMatthew G. Knepley   }
1896338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
1897338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
1898c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
189973d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
190073d901b8SMatthew G. Knepley }
190173d901b8SMatthew G. Knepley 
19029b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
19039b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
190464c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
190564c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
190664c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
190764c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
190864c72086SMatthew G. Knepley {
19099b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
19109b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
19119b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
19129b6f715bSMatthew G. Knepley   Vec                locA = NULL;
19139b6f715bSMatthew G. Knepley   DMField            coordField;
19149b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
19159b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
19169b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
191764c72086SMatthew G. Knepley   const PetscScalar *constants;
19189b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
191964c72086SMatthew G. Knepley   PetscErrorCode     ierr;
192064c72086SMatthew G. Knepley 
192164c72086SMatthew G. Knepley   PetscFunctionBegin;
19229b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
19239b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
192464c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
192564c72086SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
192664c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
192764c72086SMatthew G. Knepley   /* Determine which discretizations we have */
19289b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
192964c72086SMatthew G. Knepley     PetscObject  obj;
193064c72086SMatthew G. Knepley     PetscClassId id;
193164c72086SMatthew G. Knepley 
19329b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
193364c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
19349b6f715bSMatthew G. Knepley     if (id == PETSCFV_CLASSID) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
193564c72086SMatthew G. Knepley   }
193664c72086SMatthew G. Knepley   /* Read DS information */
193764c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
193864c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
193964c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
194064c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
194164c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
194264c72086SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
19439b6f715bSMatthew G. Knepley   if (locA) {
19449b6f715bSMatthew G. Knepley     DM dmAux;
19459b6f715bSMatthew G. Knepley 
19469b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
19479b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
194864c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
194964c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
195064c72086SMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
195164c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
195264c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
195364c72086SMatthew G. Knepley   }
19549b6f715bSMatthew G. Knepley   /* Integrate over points */
19559b6f715bSMatthew G. Knepley   {
19569b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
1957b7260050SToby Isaac     PetscInt        maxDegree;
19589b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
19599b6f715bSMatthew G. Knepley     const PetscInt *points;
19609b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
19619b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
196264c72086SMatthew G. Knepley 
19639b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
19649b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
19659b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
1966b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
196764c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
196864c72086SMatthew G. Knepley       PetscFE fe;
196964c72086SMatthew G. Knepley 
197064c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
1971b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
19729b6f715bSMatthew G. Knepley       if (!qGeom) {
19739b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
19749b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
19759b6f715bSMatthew G. Knepley       }
19769b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
19779b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
19789b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
19799b6f715bSMatthew G. Knepley         const PetscInt point = points[face], *support, *cone;
19809b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
19819b6f715bSMatthew G. Knepley         PetscInt       i, coneSize, faceLoc;
19829b6f715bSMatthew G. Knepley 
19839b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
19849b6f715bSMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, support[0], &coneSize);CHKERRQ(ierr);
19859b6f715bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, support[0], &cone);CHKERRQ(ierr);
19869b6f715bSMatthew G. Knepley         for (faceLoc = 0; faceLoc < coneSize; ++faceLoc) if (cone[faceLoc] == point) break;
19879b6f715bSMatthew 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]);
19889b6f715bSMatthew G. Knepley         fgeom->face[face][0] = faceLoc;
19899b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
19909b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
19919b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
19929b6f715bSMatthew G. Knepley         if (locA) {
19939b6f715bSMatthew G. Knepley           PetscInt subp;
19949b6f715bSMatthew G. Knepley           ierr = DMPlexGetSubpoint(plexA, support[0], &subp);CHKERRQ(ierr);
19959b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
19969b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
19979b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
19989b6f715bSMatthew G. Knepley         }
19999b6f715bSMatthew G. Knepley       }
20009b6f715bSMatthew G. Knepley       /* Get blocking */
20019b6f715bSMatthew G. Knepley       {
20029b6f715bSMatthew G. Knepley         PetscQuadrature q;
20039b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
20049b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
20059b6f715bSMatthew G. Knepley 
200664c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
200764c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
200864c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
200964c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
201064c72086SMatthew G. Knepley         blockSize = Nb*Nq;
201164c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
20129b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
201364c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
20149b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
20159b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
201664c72086SMatthew G. Knepley         offset    = numFaces - Nr;
201764c72086SMatthew G. Knepley       }
20189b6f715bSMatthew G. Knepley       /* Do integration for each field */
20199b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
20209b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
20219b6f715bSMatthew G. Knepley         ierr = PetscFEIntegrateBd(fe, prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
20229b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
202364c72086SMatthew G. Knepley       }
20249b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
20259b6f715bSMatthew G. Knepley       ierr = PetscFEIntegrateBd(fe, prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
20269b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
202764c72086SMatthew G. Knepley       /* Cleanup data arrays */
20289b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
20299b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
20309b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
20319b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
203264c72086SMatthew G. Knepley     }
203364c72086SMatthew G. Knepley   }
20349b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
20359b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
20369b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
20379b6f715bSMatthew G. Knepley }
20389b6f715bSMatthew G. Knepley 
20399b6f715bSMatthew G. Knepley /*@
20409b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
20419b6f715bSMatthew G. Knepley 
20429b6f715bSMatthew G. Knepley   Input Parameters:
20439b6f715bSMatthew G. Knepley + dm      - The mesh
20449b6f715bSMatthew G. Knepley . X       - Global input vector
20459b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
20469b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
20479b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
20489b6f715bSMatthew G. Knepley . func    = The function to integrate along the boundary
20499b6f715bSMatthew G. Knepley - user    - The user context
20509b6f715bSMatthew G. Knepley 
20519b6f715bSMatthew G. Knepley   Output Parameter:
20529b6f715bSMatthew G. Knepley . integral - Integral for each field
20539b6f715bSMatthew G. Knepley 
20549b6f715bSMatthew G. Knepley   Level: developer
20559b6f715bSMatthew G. Knepley 
20569b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
20579b6f715bSMatthew G. Knepley @*/
20589b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
20599b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
20609b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
20619b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
20629b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
20639b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
20649b6f715bSMatthew G. Knepley {
20659b6f715bSMatthew G. Knepley   Vec            locX;
20669b6f715bSMatthew G. Knepley   PetscSection   section;
20679b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
20689b6f715bSMatthew G. Knepley   IS             facetIS;
20699b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
20709b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
20719b6f715bSMatthew G. Knepley 
20729b6f715bSMatthew G. Knepley   PetscFunctionBegin;
20739b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20749b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
20759b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
20769b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
20779b6f715bSMatthew G. Knepley   PetscValidPointer(integral, 6);
20789b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
20799b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
20809b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
20819b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
20829b6f715bSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
20839b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
20849b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
20859b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
20869b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
20879b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
20889b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
20899b6f715bSMatthew G. Knepley   /* Loop over label values */
20909b6f715bSMatthew G. Knepley   ierr = PetscMemzero(integral, Nf * sizeof(PetscScalar));CHKERRQ(ierr);
20919b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
20929b6f715bSMatthew G. Knepley     IS           pointIS;
20939b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
20949b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
20959b6f715bSMatthew G. Knepley 
20969b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
20979b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
20989b6f715bSMatthew G. Knepley     {
20999b6f715bSMatthew G. Knepley       IS isectIS;
21009b6f715bSMatthew G. Knepley 
21019b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
21029b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
21039b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21049b6f715bSMatthew G. Knepley       pointIS = isectIS;
21059b6f715bSMatthew G. Knepley     }
21069b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
21079b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
21089b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
21099b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
21109b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
21119b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
21129b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21139b6f715bSMatthew G. Knepley   }
211464c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
21159b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
21169b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
211764c72086SMatthew G. Knepley   PetscFunctionReturn(0);
211864c72086SMatthew G. Knepley }
211964c72086SMatthew G. Knepley 
2120d69c5d34SMatthew G. Knepley /*@
212168132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to the uniformly refined DM.
2122d69c5d34SMatthew G. Knepley 
2123d69c5d34SMatthew G. Knepley   Input Parameters:
2124d69c5d34SMatthew G. Knepley + dmf  - The fine mesh
2125d69c5d34SMatthew G. Knepley . dmc  - The coarse mesh
2126d69c5d34SMatthew G. Knepley - user - The user context
2127d69c5d34SMatthew G. Knepley 
2128d69c5d34SMatthew G. Knepley   Output Parameter:
2129934789fcSMatthew G. Knepley . In  - The interpolation matrix
2130d69c5d34SMatthew G. Knepley 
2131d69c5d34SMatthew G. Knepley   Level: developer
2132d69c5d34SMatthew G. Knepley 
213368132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2134d69c5d34SMatthew G. Knepley @*/
213568132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, Mat In, void *user)
2136d69c5d34SMatthew G. Knepley {
2137d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
2138d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
21392764a2aaSMatthew G. Knepley   PetscDS           prob;
2140d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
214197c42addSMatthew G. Knepley   PetscFV          *fvRef;
2142d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
2143d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
2144d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
21459ac3fadcSMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, cEndInterior, c;
21460f2d7e86SMatthew G. Knepley   PetscInt          cTotDim, rTotDim = 0;
2147d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
2148d69c5d34SMatthew G. Knepley 
2149d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
2150d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2151c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
2152e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2153e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
2154e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2155e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
2156d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
2157d69c5d34SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
21589ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
21599ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
21602764a2aaSMatthew G. Knepley   ierr = DMGetDS(dmf, &prob);CHKERRQ(ierr);
216197c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf,&feRef,Nf,&fvRef);CHKERRQ(ierr);
2162d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
216397c42addSMatthew G. Knepley     PetscObject  obj;
216497c42addSMatthew G. Knepley     PetscClassId id;
2165aa7890ccSMatthew G. Knepley     PetscInt     rNb = 0, Nc = 0;
2166d69c5d34SMatthew G. Knepley 
216797c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
216897c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
216997c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
217097c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
217197c42addSMatthew G. Knepley 
21720f2d7e86SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
2173d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
21740f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
217597c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
217697c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
217797c42addSMatthew G. Knepley       PetscDualSpace Q;
217897c42addSMatthew G. Knepley 
217997c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
218097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
218197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
218297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
218397c42addSMatthew G. Knepley     }
21849c3cf19fSMatthew G. Knepley     rTotDim += rNb;
2185d69c5d34SMatthew G. Knepley   }
21862764a2aaSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
21870f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
21880f2d7e86SMatthew G. Knepley   ierr = PetscMemzero(elemMat, rTotDim*cTotDim * sizeof(PetscScalar));CHKERRQ(ierr);
2189d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
2190d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
2191d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
2192d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2193d69c5d34SMatthew G. Knepley     PetscReal       *points;
2194d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
2195d69c5d34SMatthew G. Knepley 
2196d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
219797c42addSMatthew G. Knepley     if (feRef[fieldI]) {
2198d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
21990f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
220097c42addSMatthew G. Knepley     } else {
220197c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
220297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
220397c42addSMatthew G. Knepley     }
2204d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
2205d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
2206d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
22079c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
2208d69c5d34SMatthew G. Knepley       npoints += Np;
2209d69c5d34SMatthew G. Knepley     }
2210d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
2211d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
2212d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
22139c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
2214d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
2215d69c5d34SMatthew G. Knepley     }
2216d69c5d34SMatthew G. Knepley 
2217d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
221897c42addSMatthew G. Knepley       PetscObject  obj;
221997c42addSMatthew G. Knepley       PetscClassId id;
2220d69c5d34SMatthew G. Knepley       PetscReal   *B;
22219c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
2222d69c5d34SMatthew G. Knepley 
222397c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldJ, &obj);CHKERRQ(ierr);
222497c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
222597c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
222697c42addSMatthew G. Knepley         PetscFE fe = (PetscFE) obj;
2227d69c5d34SMatthew G. Knepley 
2228d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
22290f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
22300f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2231ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
2232ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
22339c3cf19fSMatthew 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);
22340f2d7e86SMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);
2235d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
2236d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
22379c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
22389c3cf19fSMatthew 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);
2239d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
224036a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2241d172c84bSMatthew G. Knepley                 /*
2242d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
2243d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
2244d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
2245d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
2246d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
2247d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
2248d172c84bSMatthew G. Knepley                 */
22499c3cf19fSMatthew 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];
225036a6d9c0SMatthew G. Knepley               }
2251d69c5d34SMatthew G. Knepley             }
2252d69c5d34SMatthew G. Knepley           }
22530f2d7e86SMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, npoints, points, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2254ffe73a53SMatthew G. Knepley         }
225597c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
225697c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
225797c42addSMatthew G. Knepley 
225897c42addSMatthew G. Knepley         /* Evaluate constant function at points */
225997c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
226097c42addSMatthew G. Knepley         cpdim = 1;
226197c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
226297c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
226397c42addSMatthew 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);
226497c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
226597c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
22669c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
22679c3cf19fSMatthew 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);
226897c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
226997c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2270458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
227197c42addSMatthew G. Knepley               }
227297c42addSMatthew G. Knepley             }
227397c42addSMatthew G. Knepley           }
227497c42addSMatthew G. Knepley         }
227597c42addSMatthew G. Knepley       }
2276d172c84bSMatthew G. Knepley       offsetJ += cpdim;
2277d69c5d34SMatthew G. Knepley     }
2278d172c84bSMatthew G. Knepley     offsetI += fpdim;
2279549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
2280d69c5d34SMatthew G. Knepley   }
22810f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
22827f5b169aSMatthew G. Knepley   /* Preallocate matrix */
22837f5b169aSMatthew G. Knepley   {
2284c094ef40SMatthew G. Knepley     Mat          preallocator;
2285c094ef40SMatthew G. Knepley     PetscScalar *vals;
2286c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
2287c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
22887f5b169aSMatthew G. Knepley 
2289c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
2290c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
2291c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
2292c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2293c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
2294c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
22957f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
22967f5b169aSMatthew G. Knepley       ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
2297c094ef40SMatthew G. Knepley       ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
22987f5b169aSMatthew G. Knepley     }
2299c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
2300c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2301c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2302c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
2303c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
23047f5b169aSMatthew G. Knepley   }
23057f5b169aSMatthew G. Knepley   /* Fill matrix */
23067f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
2307d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2308934789fcSMatthew G. Knepley     ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2309d69c5d34SMatthew G. Knepley   }
2310549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
231197c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
2312549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2313934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2314934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2315d69c5d34SMatthew G. Knepley   if (mesh->printFEM) {
2316825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
2317934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
2318825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
2319d69c5d34SMatthew G. Knepley   }
2320d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2321d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
2322d69c5d34SMatthew G. Knepley }
23236c73c22cSMatthew G. Knepley 
2324bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
2325bd041c0cSMatthew G. Knepley {
2326bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
2327bd041c0cSMatthew G. Knepley }
2328bd041c0cSMatthew G. Knepley 
232968132eb9SMatthew G. Knepley /*@
233068132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
233168132eb9SMatthew G. Knepley 
233268132eb9SMatthew G. Knepley   Input Parameters:
233368132eb9SMatthew G. Knepley + dmf  - The fine mesh
233468132eb9SMatthew G. Knepley . dmc  - The coarse mesh
233568132eb9SMatthew G. Knepley - user - The user context
233668132eb9SMatthew G. Knepley 
233768132eb9SMatthew G. Knepley   Output Parameter:
233868132eb9SMatthew G. Knepley . In  - The interpolation matrix
233968132eb9SMatthew G. Knepley 
234068132eb9SMatthew G. Knepley   Level: developer
234168132eb9SMatthew G. Knepley 
234268132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
234368132eb9SMatthew G. Knepley @*/
234468132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
23454ef9d792SMatthew G. Knepley {
234664e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
234764e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
23484ef9d792SMatthew G. Knepley   PetscDS        prob;
23494ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2350e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
23514ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
23524ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
23534ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
23544ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
23554ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
23564ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
23574ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
23584ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
23594ef9d792SMatthew G. Knepley 
23604ef9d792SMatthew G. Knepley   PetscFunctionBegin;
236177711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
23624ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
23634ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
23644ef9d792SMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
23654ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
23664ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
23674ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
2368e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2369e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
2370e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2371e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
23724ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
23734ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
23749c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
23754ef9d792SMatthew G. Knepley 
23764ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
23774ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
23784ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
23794ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
23804ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
23814ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
23824ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
23834ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2384e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
23854ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
23864ef9d792SMatthew G. Knepley     PetscObject      obj;
23874ef9d792SMatthew G. Knepley     PetscClassId     id;
2388c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
23894ef9d792SMatthew G. Knepley     PetscQuadrature  f;
239017f047d8SMatthew G. Knepley     const PetscReal *qpoints;
239117f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
23924ef9d792SMatthew G. Knepley 
23934ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
23944ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
23954ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
23964ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
23974ef9d792SMatthew G. Knepley 
23984ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
23994ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
24004ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
24014ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
24024ef9d792SMatthew G. Knepley 
24034ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
24044ef9d792SMatthew G. Knepley       Nc   = 1;
24054ef9d792SMatthew G. Knepley     }
24064ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
24074ef9d792SMatthew G. Knepley     /* For each fine grid cell */
24084ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
24094ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
24104ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
24114ef9d792SMatthew G. Knepley 
24126ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
24134ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
24149c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
24154ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
24164ef9d792SMatthew G. Knepley         Vec             pointVec;
24174ef9d792SMatthew G. Knepley         PetscScalar    *pV;
24183a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
24193a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
24209c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
24214ef9d792SMatthew G. Knepley 
24224ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
24234ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
24249c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
24254ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
24264ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
24274ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
24284ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2429c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2430c330f8ffSToby Isaac 
24314ef9d792SMatthew G. Knepley           /* Transform point to real space */
2432c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
24334ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
24344ef9d792SMatthew G. Knepley         }
24354ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
24364ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
24371555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
243862a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
24393a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
24404ef9d792SMatthew G. Knepley         /* Update preallocation info */
24413a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
24423a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
24439c3cf19fSMatthew G. Knepley         {
2444e8f14785SLisandro Dalcin           PetscHashIJKey key;
2445e8f14785SLisandro Dalcin           PetscBool      missing;
24464ef9d792SMatthew G. Knepley 
2447e8f14785SLisandro Dalcin           key.i = findices[i];
2448e8f14785SLisandro Dalcin           if (key.i >= 0) {
24494ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
24504ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
24513a93e3b7SToby Isaac               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
24524ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2453e8f14785SLisandro Dalcin                 key.j = cindices[c];
2454e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2455e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
24564ef9d792SMatthew G. Knepley                 if (missing) {
2457e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2458e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
24594ef9d792SMatthew G. Knepley                 }
24604ef9d792SMatthew G. Knepley               }
24613a93e3b7SToby Isaac               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
24624ef9d792SMatthew G. Knepley             }
24634ef9d792SMatthew G. Knepley           }
24648c543595SMatthew G. Knepley         }
24653a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
24664ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
24674ef9d792SMatthew G. Knepley       }
246846bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
24694ef9d792SMatthew G. Knepley     }
24704ef9d792SMatthew G. Knepley   }
2471e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
24724ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
24734ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
24744ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
24754ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
24764ef9d792SMatthew G. Knepley     PetscObject      obj;
24774ef9d792SMatthew G. Knepley     PetscClassId     id;
2478c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
24794ef9d792SMatthew G. Knepley     PetscQuadrature  f;
24804ef9d792SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
24819c3cf19fSMatthew G. Knepley     PetscInt         Nc, qNc, Np, fpdim, i, d;
24824ef9d792SMatthew G. Knepley 
24834ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
24844ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
24854ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
24864ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
24874ef9d792SMatthew G. Knepley 
24884ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
24894ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
24904ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
24914ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
24924ef9d792SMatthew G. Knepley 
24934ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
24944ef9d792SMatthew G. Knepley       Nc   = 1;
249525ce1634SJed Brown     } else SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %d",field);
24964ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
24974ef9d792SMatthew G. Knepley     /* For each fine grid cell */
24984ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
24994ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
25004ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
25014ef9d792SMatthew G. Knepley 
25026ecaa68aSToby Isaac       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
25034ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
25049c3cf19fSMatthew G. Knepley       if (numFIndices != fpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %d != %d dual basis vecs", numFIndices, fpdim);
25054ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
25064ef9d792SMatthew G. Knepley         Vec             pointVec;
25074ef9d792SMatthew G. Knepley         PetscScalar    *pV;
250812111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
25093a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
251017f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
25114ef9d792SMatthew G. Knepley 
25124ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
25134ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
25149c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
25159c3cf19fSMatthew 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);
25164ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
25174ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
25184ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
25194ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2520c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2521c330f8ffSToby Isaac 
25224ef9d792SMatthew G. Knepley           /* Transform point to real space */
2523c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
25244ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
25254ef9d792SMatthew G. Knepley         }
25264ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
25274ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
25281555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
252962a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
25304ef9d792SMatthew G. Knepley         /* Update preallocation info */
25313a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
25323a93e3b7SToby Isaac         if (numCoarseCells != Np) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
25334ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
25344ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2535826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2536c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2537826eb36dSMatthew G. Knepley 
25383a93e3b7SToby Isaac           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
25394ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
25403a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2541e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2542c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
25434ef9d792SMatthew G. Knepley 
25444ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
25454ef9d792SMatthew G. Knepley             PetscFE    fe = (PetscFE) obj;
25464ef9d792SMatthew G. Knepley             PetscReal *B;
25474ef9d792SMatthew G. Knepley 
25484ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
25494ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
25504ef9d792SMatthew G. Knepley             ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
25519c3cf19fSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
25524ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
25534ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
25549c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += B[j*Nc + c]*qweights[ccell*qNc + c];
25554ef9d792SMatthew G. Knepley             }
25564ef9d792SMatthew G. Knepley             ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
25574ef9d792SMatthew G. Knepley           } else {
25584ef9d792SMatthew G. Knepley             cpdim = 1;
25594ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
25609c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
25614ef9d792SMatthew G. Knepley             }
25624ef9d792SMatthew G. Knepley           }
25634ef9d792SMatthew G. Knepley           /* Update interpolator */
25649c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
25659c3cf19fSMatthew G. Knepley           if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
25669c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
25673a93e3b7SToby Isaac           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
25684ef9d792SMatthew G. Knepley         }
25694ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
25703a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
25714ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
25724ef9d792SMatthew G. Knepley       }
257346bdb399SToby Isaac       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
25744ef9d792SMatthew G. Knepley     }
25754ef9d792SMatthew G. Knepley   }
25764ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
25774ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
25784ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
25794ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
25804ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
258177711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
25824ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
25834ef9d792SMatthew G. Knepley }
25844ef9d792SMatthew G. Knepley 
258546fa42a0SMatthew G. Knepley /*@
2586bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
2587bd041c0cSMatthew G. Knepley 
2588bd041c0cSMatthew G. Knepley   Input Parameters:
2589bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
2590bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
2591bd041c0cSMatthew G. Knepley - user - The user context
2592bd041c0cSMatthew G. Knepley 
2593bd041c0cSMatthew G. Knepley   Output Parameter:
2594bd041c0cSMatthew G. Knepley . mass  - The mass matrix
2595bd041c0cSMatthew G. Knepley 
2596bd041c0cSMatthew G. Knepley   Level: developer
2597bd041c0cSMatthew G. Knepley 
2598bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2599bd041c0cSMatthew G. Knepley @*/
2600bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
2601bd041c0cSMatthew G. Knepley {
2602bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
2603bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
2604bd041c0cSMatthew G. Knepley   PetscDS        prob;
2605bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2606e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
2607bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
2608bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
2609bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
2610bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
2611bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
2612bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
2613bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
2614bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
2615bd041c0cSMatthew G. Knepley 
2616bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
2617bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
2618bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
2619bd041c0cSMatthew G. Knepley   ierr = PetscDSGetRefCoordArrays(prob, &x, NULL);CHKERRQ(ierr);
2620bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
2621bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
2622bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
2623e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2624e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
2625e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2626e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
2627bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
2628bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2629bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
2630bd041c0cSMatthew G. Knepley 
2631bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
2632bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
2633bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
2634bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
2635bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
2636bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
2637bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
2638bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2639e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
2640bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2641bd041c0cSMatthew G. Knepley     PetscObject      obj;
2642bd041c0cSMatthew G. Knepley     PetscClassId     id;
2643bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2644bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
2645bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2646bd041c0cSMatthew G. Knepley 
2647bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2648bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2649bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
2650bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2651bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
2652bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2653bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2654bd041c0cSMatthew G. Knepley       Vec                pointVec;
2655bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2656bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2657bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2658bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
2659bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2660bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2661bd041c0cSMatthew G. Knepley 
2662bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2663bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2664bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2665bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2666bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2667bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2668bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2669c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2670c330f8ffSToby Isaac 
2671bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2672c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2673bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2674bd041c0cSMatthew G. Knepley       }
2675bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2676bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2677bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2678bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
2679bd041c0cSMatthew G. Knepley       /* Update preallocation info */
2680bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2681bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2682bd041c0cSMatthew G. Knepley       {
2683e8f14785SLisandro Dalcin         PetscHashIJKey key;
2684e8f14785SLisandro Dalcin         PetscBool      missing;
2685bd041c0cSMatthew G. Knepley 
2686bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
2687e8f14785SLisandro Dalcin           key.i = findices[i];
2688e8f14785SLisandro Dalcin           if (key.i >= 0) {
2689bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
2690bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2691bd041c0cSMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2692bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2693e8f14785SLisandro Dalcin                 key.j = cindices[c];
2694e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2695e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
2696bd041c0cSMatthew G. Knepley                 if (missing) {
2697e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2698e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
2699bd041c0cSMatthew G. Knepley                 }
2700bd041c0cSMatthew G. Knepley               }
2701bd041c0cSMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2702bd041c0cSMatthew G. Knepley             }
2703bd041c0cSMatthew G. Knepley           }
2704bd041c0cSMatthew G. Knepley         }
2705bd041c0cSMatthew G. Knepley       }
2706bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2707bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2708bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2709bd041c0cSMatthew G. Knepley     }
2710bd041c0cSMatthew G. Knepley   }
2711e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
2712bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
2713bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
2714bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
2715bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
2716bd041c0cSMatthew G. Knepley     PetscObject      obj;
2717bd041c0cSMatthew G. Knepley     PetscClassId     id;
2718bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
2719bd041c0cSMatthew G. Knepley     PetscReal       *Bfine;
2720bd041c0cSMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2721bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
2722bd041c0cSMatthew G. Knepley 
2723bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
2724bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2725bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);ierr = PetscFEGetDefaultTabulation((PetscFE) obj, &Bfine, NULL, NULL);CHKERRQ(ierr);}
2726bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
2727bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
2728bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
2729bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2730bd041c0cSMatthew G. Knepley       Vec                pointVec;
2731bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
2732bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
2733bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
2734bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
2735bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
2736bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
2737bd041c0cSMatthew G. Knepley 
2738bd041c0cSMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2739bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
2740bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
2741bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
2742bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
2743bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2744bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
2745c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2746c330f8ffSToby Isaac 
2747bd041c0cSMatthew G. Knepley         /* Transform point to real space */
2748c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
2749bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
2750bd041c0cSMatthew G. Knepley       }
2751bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2752bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
2753bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
2754bd041c0cSMatthew G. Knepley       /* Update matrix */
2755bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
2756bd041c0cSMatthew G. Knepley       if (numCoarseCells != Nq) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
2757bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
2758bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2759bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
2760c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
2761c330f8ffSToby Isaac 
2762bd041c0cSMatthew G. Knepley 
2763bd041c0cSMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2764bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
2765bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2766bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2767c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
2768bd041c0cSMatthew G. Knepley 
2769bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
2770bd041c0cSMatthew G. Knepley           PetscFE    fe = (PetscFE) obj;
2771bd041c0cSMatthew G. Knepley           PetscReal *B;
2772bd041c0cSMatthew G. Knepley 
2773bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
2774bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2775bd041c0cSMatthew G. Knepley           ierr = PetscFEGetTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);
2776bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
2777bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2778bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2779bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2780bd041c0cSMatthew 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;
2781bd041c0cSMatthew G. Knepley             }
2782bd041c0cSMatthew G. Knepley             /* Update interpolator */
2783bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2784bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2785bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2786bd041c0cSMatthew G. Knepley           }
2787bd041c0cSMatthew G. Knepley           ierr = PetscFERestoreTabulation(fe, 1, x, &B, NULL, NULL);CHKERRQ(ierr);CHKERRQ(ierr);
2788bd041c0cSMatthew G. Knepley         } else {
2789bd041c0cSMatthew G. Knepley           cpdim = 1;
2790bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
2791bd041c0cSMatthew G. Knepley             ierr = PetscMemzero(elemMat, cpdim * sizeof(PetscScalar));CHKERRQ(ierr);
2792bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2793bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
2794bd041c0cSMatthew G. Knepley             }
2795bd041c0cSMatthew G. Knepley             /* Update interpolator */
2796bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
2797bd041c0cSMatthew 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);
2798bd041c0cSMatthew G. Knepley             if (numCIndices != cpdim) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
2799bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
2800bd041c0cSMatthew G. Knepley           }
2801bd041c0cSMatthew G. Knepley         }
2802bd041c0cSMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, &numCIndices, &cindices, NULL);CHKERRQ(ierr);
2803bd041c0cSMatthew G. Knepley       }
2804bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
2805bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
2806bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
2807bd041c0cSMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, &numFIndices, &findices, NULL);CHKERRQ(ierr);
2808bd041c0cSMatthew G. Knepley     }
2809bd041c0cSMatthew G. Knepley   }
2810bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
2811bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
2812bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2813bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2814bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2815bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
2816bd041c0cSMatthew G. Knepley }
2817bd041c0cSMatthew G. Knepley 
2818bd041c0cSMatthew G. Knepley /*@
281946fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
282046fa42a0SMatthew G. Knepley 
282146fa42a0SMatthew G. Knepley   Input Parameters:
282246fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
282346fa42a0SMatthew G. Knepley - dmf  - The fine mesh
282446fa42a0SMatthew G. Knepley - user - The user context
282546fa42a0SMatthew G. Knepley 
282646fa42a0SMatthew G. Knepley   Output Parameter:
282746fa42a0SMatthew G. Knepley . sc   - The mapping
282846fa42a0SMatthew G. Knepley 
282946fa42a0SMatthew G. Knepley   Level: developer
283046fa42a0SMatthew G. Knepley 
283146fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
283246fa42a0SMatthew G. Knepley @*/
28337c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
28347c927364SMatthew G. Knepley {
2835e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
28367c927364SMatthew G. Knepley   PetscFE       *feRef;
283797c42addSMatthew G. Knepley   PetscFV       *fvRef;
28387c927364SMatthew G. Knepley   Vec            fv, cv;
28397c927364SMatthew G. Knepley   IS             fis, cis;
28407c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
28417c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
28420bd915a7SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, cEndInterior, c, dim, d, startC, endC, offsetC, offsetF, m;
28436f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
28447c927364SMatthew G. Knepley   PetscErrorCode ierr;
28457c927364SMatthew G. Knepley 
28467c927364SMatthew G. Knepley   PetscFunctionBegin;
284775a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2848c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
2849e87a4003SBarry Smith   ierr = DMGetSection(dmf, &fsection);CHKERRQ(ierr);
2850e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
2851e87a4003SBarry Smith   ierr = DMGetSection(dmc, &csection);CHKERRQ(ierr);
2852e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
28537c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
28547c927364SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
28559ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dmc, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
28569ac3fadcSMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
2857e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
28586f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
28597c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
286097c42addSMatthew G. Knepley     PetscObject  obj;
286197c42addSMatthew G. Knepley     PetscClassId id;
2862aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
28637c927364SMatthew G. Knepley 
286497c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
286597c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
286697c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
286797c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
28686f3d3cbcSMatthew G. Knepley       PetscSpace sp;
28699b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
287097c42addSMatthew G. Knepley 
28717c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
28727c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
28737c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
28746f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
28759b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
28769b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
287797c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
287897c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
287997c42addSMatthew G. Knepley       PetscDualSpace Q;
288097c42addSMatthew G. Knepley 
288197c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
288297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
288397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
288497c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
28856f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
288697c42addSMatthew G. Knepley     }
2887d172c84bSMatthew G. Knepley     fTotDim += fNb;
28887c927364SMatthew G. Knepley   }
2889e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
28907c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
28917c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
28927c927364SMatthew G. Knepley     PetscFE        feC;
289397c42addSMatthew G. Knepley     PetscFV        fvC;
28947c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
2895d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
28967c927364SMatthew G. Knepley 
289797c42addSMatthew G. Knepley     if (feRef[field]) {
2898e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
28997c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
29007c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
29017c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
2902d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
29037c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
29047c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
29057c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
290697c42addSMatthew G. Knepley     } else {
290797c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
290897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
290997c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
291097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
291197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
291297c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
291397c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
291497c42addSMatthew G. Knepley     }
291597c42addSMatthew 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);
29167c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
29177c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
2918d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
2919d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
292097c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
29217c927364SMatthew G. Knepley 
29227c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
2923d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
2924d172c84bSMatthew 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);
292597c42addSMatthew G. Knepley       if (NpC != 1 && feRef[field]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
29267c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
29277c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
2928d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
29297c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
2930d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
29317c927364SMatthew G. Knepley 
29327c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
2933d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
2934d172c84bSMatthew 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);
29357c927364SMatthew G. Knepley         if (NpC != NpF) continue;
29367c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
29377c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
2938d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
2939d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
2940d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
294197c42addSMatthew G. Knepley         found = PETSC_TRUE;
29427c927364SMatthew G. Knepley         break;
29437c927364SMatthew G. Knepley       }
294497c42addSMatthew G. Knepley       if (!found) {
294597c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
2946d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
2947d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
294897c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
294997c42addSMatthew G. Knepley       }
29507c927364SMatthew G. Knepley     }
2951d172c84bSMatthew G. Knepley     offsetC += cpdim;
2952d172c84bSMatthew G. Knepley     offsetF += fpdim;
29537c927364SMatthew G. Knepley   }
295497c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
29556f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
29567c927364SMatthew G. Knepley 
29577c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
29587c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
29590bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
29607c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
29617c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
2962aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
2963aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
29647c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
29657c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
29667c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
29677c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
29680bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
29697c927364SMatthew 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]]);
29707c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
29717c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
29727c927364SMatthew G. Knepley     }
29737c927364SMatthew G. Knepley   }
29747c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
29757c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
29767c927364SMatthew G. Knepley 
29777c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
29787c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
29799448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
29807c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
29817c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
29827c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
29837c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
298475a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2985cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
2986cb1e1211SMatthew G Knepley }
2987a1cf66bbSMatthew G. Knepley 
29882f856554SMatthew G. Knepley /*@C
29892f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
29902f856554SMatthew G. Knepley 
29912f856554SMatthew G. Knepley   Input Parameters:
29922f856554SMatthew G. Knepley + dm     - The DM
29932f856554SMatthew G. Knepley . cellIS - The cells to include
29942f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
29952f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
29962f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
29972f856554SMatthew G. Knepley 
29982f856554SMatthew G. Knepley   Output Parameters:
29992f856554SMatthew G. Knepley + u   - The field coefficients
30002f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
30012f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
30022f856554SMatthew G. Knepley 
30032f856554SMatthew G. Knepley   Level: developer
30042f856554SMatthew G. Knepley 
30052f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
30062f856554SMatthew G. Knepley @*/
30072f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
30082f856554SMatthew G. Knepley {
30092f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
30102f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
30112f856554SMatthew G. Knepley   PetscDS         prob;
30122f856554SMatthew G. Knepley   const PetscInt *cells;
30132f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
30142f856554SMatthew G. Knepley   PetscErrorCode  ierr;
30152f856554SMatthew G. Knepley 
30162f856554SMatthew G. Knepley   PetscFunctionBegin;
30172f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30182f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
30192f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
30202f856554SMatthew G. Knepley   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 6);}
30212f856554SMatthew G. Knepley   PetscValidPointer(u, 7);
30222f856554SMatthew G. Knepley   PetscValidPointer(u_t, 8);
30232f856554SMatthew G. Knepley   PetscValidPointer(a, 9);
30242f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
30252f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
30262f856554SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
30272f856554SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &prob);CHKERRQ(ierr);
30282f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
30292f856554SMatthew G. Knepley   if (locA) {
30302f856554SMatthew G. Knepley     DM      dmAux;
30312f856554SMatthew G. Knepley     PetscDS probAux;
30322f856554SMatthew G. Knepley 
30332f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
30342f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
30352f856554SMatthew G. Knepley     ierr = DMGetSection(dmAux, &sectionAux);CHKERRQ(ierr);
30362f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
30372f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
30382f856554SMatthew G. Knepley   }
30392f856554SMatthew G. Knepley   numCells = cEnd - cStart;
30402f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
30412f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
30422f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
30432f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
30442f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
30452f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
30462f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
30472f856554SMatthew G. Knepley     PetscInt       i;
30482f856554SMatthew G. Knepley 
30492f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
30502f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
30512f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
30522f856554SMatthew G. Knepley     if (locX_t) {
30532f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
30542f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
30552f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
30562f856554SMatthew G. Knepley     }
30572f856554SMatthew G. Knepley     if (locA) {
30582f856554SMatthew G. Knepley       PetscInt subcell;
30592f856554SMatthew G. Knepley       ierr = DMPlexGetAuxiliaryPoint(plex, plexA, cell, &subcell);CHKERRQ(ierr);
30602f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
30612f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
30622f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
30632f856554SMatthew G. Knepley     }
30642f856554SMatthew G. Knepley   }
30652f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
30662f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
30672f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
30682f856554SMatthew G. Knepley   PetscFunctionReturn(0);
30692f856554SMatthew G. Knepley }
30702f856554SMatthew G. Knepley 
30712f856554SMatthew G. Knepley /*@C
30722f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
30732f856554SMatthew G. Knepley 
30742f856554SMatthew G. Knepley   Input Parameters:
30752f856554SMatthew G. Knepley + dm     - The DM
30762f856554SMatthew G. Knepley . cellIS - The cells to include
30772f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
30782f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
30792f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
30802f856554SMatthew G. Knepley 
30812f856554SMatthew G. Knepley   Output Parameters:
30822f856554SMatthew G. Knepley + u   - The field coefficients
30832f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
30842f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
30852f856554SMatthew G. Knepley 
30862f856554SMatthew G. Knepley   Level: developer
30872f856554SMatthew G. Knepley 
30882f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
30892f856554SMatthew G. Knepley @*/
30902f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
30912f856554SMatthew G. Knepley {
30922f856554SMatthew G. Knepley   PetscErrorCode ierr;
30932f856554SMatthew G. Knepley 
30942f856554SMatthew G. Knepley   PetscFunctionBegin;
30952f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
30962f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
30972f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
30982f856554SMatthew G. Knepley   PetscFunctionReturn(0);
30992f856554SMatthew G. Knepley }
31002f856554SMatthew G. Knepley 
31012f856554SMatthew G. Knepley /*@C
31022f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
31032f856554SMatthew G. Knepley 
31042f856554SMatthew G. Knepley   Input Parameters:
31052f856554SMatthew G. Knepley + dm     - The DM
31062f856554SMatthew G. Knepley . fStart - The first face to include
31072f856554SMatthew G. Knepley . fEnd   - The first face to exclude
31082f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
31092f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
31102f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
31112f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
31122f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
31132f856554SMatthew G. Knepley 
31142f856554SMatthew G. Knepley   Output Parameters:
31152f856554SMatthew G. Knepley + Nface - The number of faces with field values
31162f856554SMatthew G. Knepley . uL - The field values at the left side of the face
31172f856554SMatthew G. Knepley - uR - The field values at the right side of the face
31182f856554SMatthew G. Knepley 
31192f856554SMatthew G. Knepley   Level: developer
31202f856554SMatthew G. Knepley 
31212f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
31222f856554SMatthew G. Knepley @*/
31232f856554SMatthew 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)
31242f856554SMatthew G. Knepley {
31252f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
31262f856554SMatthew G. Knepley   PetscSection       section;
31272f856554SMatthew G. Knepley   PetscDS            prob;
31282f856554SMatthew G. Knepley   DMLabel            ghostLabel;
31292f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
31302f856554SMatthew G. Knepley   PetscBool         *isFE;
31312f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
31322f856554SMatthew G. Knepley   PetscErrorCode     ierr;
31332f856554SMatthew G. Knepley 
31342f856554SMatthew G. Knepley   PetscFunctionBegin;
31352f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31362f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
31372f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
31382f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
31392f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
31402f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
31412f856554SMatthew G. Knepley   PetscValidPointer(uL, 9);
31422f856554SMatthew G. Knepley   PetscValidPointer(uR, 10);
31432f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
31442f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
31452f856554SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
31462f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
31472f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
31482f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
31492f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
31502f856554SMatthew G. Knepley     PetscObject  obj;
31512f856554SMatthew G. Knepley     PetscClassId id;
31522f856554SMatthew G. Knepley 
31532f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
31542f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
31552f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
31562f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
31572f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
31582f856554SMatthew G. Knepley   }
31592f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
31602f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
31612f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
31622f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
31632f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
31642f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
31652f856554SMatthew G. Knepley   if (locGrad) {
31662f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
31672f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
31682f856554SMatthew G. Knepley   }
31692f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
31702f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
31712f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
31722f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
31732f856554SMatthew G. Knepley     const PetscInt        *cells;
31742f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
31752f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
31762f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
31772f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
31782f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
31792f856554SMatthew G. Knepley 
31802f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
31812f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
31822f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
31832f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
31842f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
31852f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
31862f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
31872f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
31882f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
31892f856554SMatthew G. Knepley       PetscInt off;
31902f856554SMatthew G. Knepley 
31912f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
31922f856554SMatthew G. Knepley       if (isFE[f]) {
31932f856554SMatthew G. Knepley         const PetscInt *cone;
31942f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
31952f856554SMatthew G. Knepley 
31962f856554SMatthew G. Knepley         xL = xR = NULL;
31972f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
31982f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
31992f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
32002f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
32012f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
32022f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
32032f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
32042f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
32052f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
32062f856554SMatthew 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]);
32072f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
32082f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
32092f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
32102f856554SMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
32112f856554SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
32122f856554SMatthew G. Knepley           else              {for(d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
32132f856554SMatthew G. Knepley         }
32142f856554SMatthew G. Knepley         else {
32152f856554SMatthew G. Knepley           ierr = EvaluateFaceFields(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
32162f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
32172f856554SMatthew G. Knepley           for(d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
32182f856554SMatthew G. Knepley         }
32192f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
32202f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
32212f856554SMatthew G. Knepley       } else {
32222f856554SMatthew G. Knepley         PetscFV  fv;
32232f856554SMatthew G. Knepley         PetscInt numComp, c;
32242f856554SMatthew G. Knepley 
32252f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
32262f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
32272f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
32282f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
32292f856554SMatthew G. Knepley         if (dmGrad) {
32302f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
32312f856554SMatthew G. Knepley 
32322f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
32332f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
32342f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
32352f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
32362f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
32372f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
32382f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
32392f856554SMatthew G. Knepley           }
32402f856554SMatthew G. Knepley         } else {
32412f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
32422f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
32432f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
32442f856554SMatthew G. Knepley           }
32452f856554SMatthew G. Knepley         }
32462f856554SMatthew G. Knepley       }
32472f856554SMatthew G. Knepley     }
32482f856554SMatthew G. Knepley     ++iface;
32492f856554SMatthew G. Knepley   }
32502f856554SMatthew G. Knepley   *Nface = iface;
32512f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
32522f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
32532f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
32542f856554SMatthew G. Knepley   if (locGrad) {
32552f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
32562f856554SMatthew G. Knepley   }
32572f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
32582f856554SMatthew G. Knepley   PetscFunctionReturn(0);
32592f856554SMatthew G. Knepley }
32602f856554SMatthew G. Knepley 
32612f856554SMatthew G. Knepley /*@C
32622f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
32632f856554SMatthew G. Knepley 
32642f856554SMatthew G. Knepley   Input Parameters:
32652f856554SMatthew G. Knepley + dm     - The DM
32662f856554SMatthew G. Knepley . fStart - The first face to include
32672f856554SMatthew G. Knepley . fEnd   - The first face to exclude
32682f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
32692f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
32702f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
32712f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
32722f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
32732f856554SMatthew G. Knepley 
32742f856554SMatthew G. Knepley   Output Parameters:
32752f856554SMatthew G. Knepley + Nface - The number of faces with field values
32762f856554SMatthew G. Knepley . uL - The field values at the left side of the face
32772f856554SMatthew G. Knepley - uR - The field values at the right side of the face
32782f856554SMatthew G. Knepley 
32792f856554SMatthew G. Knepley   Level: developer
32802f856554SMatthew G. Knepley 
32812f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
32822f856554SMatthew G. Knepley @*/
32832f856554SMatthew 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)
32842f856554SMatthew G. Knepley {
32852f856554SMatthew G. Knepley   PetscErrorCode ierr;
32862f856554SMatthew G. Knepley 
32872f856554SMatthew G. Knepley   PetscFunctionBegin;
32882f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
32892f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
32902f856554SMatthew G. Knepley   PetscFunctionReturn(0);
32912f856554SMatthew G. Knepley }
32922f856554SMatthew G. Knepley 
32932f856554SMatthew G. Knepley /*@C
32942f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
32952f856554SMatthew G. Knepley 
32962f856554SMatthew G. Knepley   Input Parameters:
32972f856554SMatthew G. Knepley + dm     - The DM
32982f856554SMatthew G. Knepley . fStart - The first face to include
32992f856554SMatthew G. Knepley . fEnd   - The first face to exclude
33002f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
33012f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
33022f856554SMatthew G. Knepley 
33032f856554SMatthew G. Knepley   Output Parameters:
33042f856554SMatthew G. Knepley + Nface - The number of faces with field values
33052f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
33062f856554SMatthew G. Knepley - vol   - The cell volume
33072f856554SMatthew G. Knepley 
33082f856554SMatthew G. Knepley   Level: developer
33092f856554SMatthew G. Knepley 
33102f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
33112f856554SMatthew G. Knepley @*/
33122f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
33132f856554SMatthew G. Knepley {
33142f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
33152f856554SMatthew G. Knepley   DMLabel            ghostLabel;
33162f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
33172f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
33182f856554SMatthew G. Knepley   PetscErrorCode     ierr;
33192f856554SMatthew G. Knepley 
33202f856554SMatthew G. Knepley   PetscFunctionBegin;
33212f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33222f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
33232f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
33242f856554SMatthew G. Knepley   PetscValidPointer(fgeom, 6);
33252f856554SMatthew G. Knepley   PetscValidPointer(vol, 7);
33262f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33272f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
33282f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
33292f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
33302f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
33312f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
33322f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
33332f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
33342f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
33352f856554SMatthew G. Knepley     const PetscInt        *cells;
33362f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
33372f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
33382f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
33392f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
33402f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
33412f856554SMatthew G. Knepley 
33422f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
33432f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
33442f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
33452f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
33462f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
33472f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
33482f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
33492f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
33502f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
33512f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
33522f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
33532f856554SMatthew G. Knepley     }
33542f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
33552f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
33562f856554SMatthew G. Knepley     ++iface;
33572f856554SMatthew G. Knepley   }
33582f856554SMatthew G. Knepley   *Nface = iface;
33592f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
33602f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
33612f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33622f856554SMatthew G. Knepley }
33632f856554SMatthew G. Knepley 
33642f856554SMatthew G. Knepley /*@C
33652f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
33662f856554SMatthew G. Knepley 
33672f856554SMatthew G. Knepley   Input Parameters:
33682f856554SMatthew G. Knepley + dm     - The DM
33692f856554SMatthew G. Knepley . fStart - The first face to include
33702f856554SMatthew G. Knepley . fEnd   - The first face to exclude
33712f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
33722f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
33732f856554SMatthew G. Knepley 
33742f856554SMatthew G. Knepley   Output Parameters:
33752f856554SMatthew G. Knepley + Nface - The number of faces with field values
33762f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
33772f856554SMatthew G. Knepley - vol   - The cell volume
33782f856554SMatthew G. Knepley 
33792f856554SMatthew G. Knepley   Level: developer
33802f856554SMatthew G. Knepley 
33812f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
33822f856554SMatthew G. Knepley @*/
33832f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
33842f856554SMatthew G. Knepley {
33852f856554SMatthew G. Knepley   PetscErrorCode ierr;
33862f856554SMatthew G. Knepley 
33872f856554SMatthew G. Knepley   PetscFunctionBegin;
33882f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
33892f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
33902f856554SMatthew G. Knepley   PetscFunctionReturn(0);
33912f856554SMatthew G. Knepley }
33922f856554SMatthew G. Knepley 
3393a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3394a1cf66bbSMatthew G. Knepley {
3395a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3396a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3397a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3398a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3399a1cf66bbSMatthew G. Knepley 
3400a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3401a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3402a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3403a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3404a1cf66bbSMatthew G. Knepley   if (container) {
3405a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3406a1cf66bbSMatthew G. Knepley   } else {
3407a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3408a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3409a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3410a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3411a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3412a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3413a1cf66bbSMatthew G. Knepley   }
3414a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3415a1cf66bbSMatthew G. Knepley }
3416a1cf66bbSMatthew G. Knepley 
3417a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3418a1cf66bbSMatthew G. Knepley {
3419a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3420a1cf66bbSMatthew G. Knepley   *geom = NULL;
3421a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3422a1cf66bbSMatthew G. Knepley }
3423a1cf66bbSMatthew G. Knepley 
342492d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
342592d50984SMatthew G. Knepley {
342692d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
342792d50984SMatthew G. Knepley   const char      *name       = "Residual";
342892d50984SMatthew G. Knepley   DM               dmAux      = NULL;
342992d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
343092d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
343192d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
343292d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
343392d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
343492d50984SMatthew G. Knepley   DMField          coordField = NULL;
3435c0006e53SPatrick Farrell   Vec              locA;
3436c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
343792d50984SMatthew G. Knepley   IS               chunkIS;
343892d50984SMatthew G. Knepley   const PetscInt  *cells;
343992d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3440364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
344192d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
344292d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
344392d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
344492d50984SMatthew G. Knepley   PetscErrorCode   ierr;
344592d50984SMatthew G. Knepley 
344692d50984SMatthew G. Knepley   PetscFunctionBegin;
344792d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
344892d50984SMatthew G. Knepley   /* FEM+FVM */
344992d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
345092d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
345192d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
345292d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
345392d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
345492d50984SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &locA);CHKERRQ(ierr);
345592d50984SMatthew G. Knepley   if (locA) {
345692d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
345792d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
345892d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
345992d50984SMatthew G. Knepley   }
346092d50984SMatthew G. Knepley   /* 2: Get geometric data */
346192d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
346292d50984SMatthew G. Knepley     PetscObject  obj;
346392d50984SMatthew G. Knepley     PetscClassId id;
346492d50984SMatthew G. Knepley     PetscBool    fimp;
346592d50984SMatthew G. Knepley 
346692d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
346792d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
346892d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
346992d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
347092d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
3471364207b6SKarl Rupp     if (id == PETSCFV_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
347292d50984SMatthew G. Knepley   }
347392d50984SMatthew G. Knepley   if (useFEM) {
347492d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
347592d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
347692d50984SMatthew G. Knepley     if (maxDegree <= 1) {
347792d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
347892d50984SMatthew G. Knepley       if (affineQuad) {
347992d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
348092d50984SMatthew G. Knepley       }
348192d50984SMatthew G. Knepley     } else {
348292d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
348392d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
348492d50984SMatthew G. Knepley         PetscObject  obj;
348592d50984SMatthew G. Knepley         PetscClassId id;
348692d50984SMatthew G. Knepley         PetscBool    fimp;
348792d50984SMatthew G. Knepley 
348892d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
348992d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
349092d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
349192d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
349292d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
349392d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
349492d50984SMatthew G. Knepley 
349592d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
349692d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
349792d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
349892d50984SMatthew G. Knepley         }
349992d50984SMatthew G. Knepley       }
350092d50984SMatthew G. Knepley     }
350192d50984SMatthew G. Knepley   }
350292d50984SMatthew G. Knepley   /* Loop over chunks */
350392d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
350492d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
350592d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
350692d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
350792d50984SMatthew G. Knepley   numChunks     = 1;
350892d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
350992d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
351092d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
3511c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
3512c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
3513c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
351492d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
3515c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
351692d50984SMatthew G. Knepley 
351792d50984SMatthew G. Knepley     /* Extract field coefficients */
351892d50984SMatthew G. Knepley     if (useFEM) {
351992d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
352092d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
352192d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
352292d50984SMatthew G. Knepley       ierr = PetscMemzero(elemVec, numCells*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
352392d50984SMatthew G. Knepley     }
352492d50984SMatthew 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 */
352592d50984SMatthew G. Knepley     /* Loop over fields */
352692d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
352792d50984SMatthew G. Knepley       PetscObject  obj;
352892d50984SMatthew G. Knepley       PetscClassId id;
352992d50984SMatthew G. Knepley       PetscBool    fimp;
353092d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
353192d50984SMatthew G. Knepley 
353292d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
353392d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
353492d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
353592d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
353692d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
353792d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
353892d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
353992d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
354092d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
354192d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
354292d50984SMatthew G. Knepley 
354392d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
354492d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
354592d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
354692d50984SMatthew G. Knepley         blockSize = Nb;
354792d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
354892d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
354992d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
355092d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
355192d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
355292d50984SMatthew G. Knepley         offset    = numCells - Nr;
355392d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
355492d50984SMatthew 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) */
355592d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
355692d50984SMatthew G. Knepley         ierr = PetscFEIntegrateResidual(fe, prob, f, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
355792d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
355892d50984SMatthew 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);
355992d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
356092d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
356192d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
356292d50984SMatthew G. Knepley 
356392d50984SMatthew G. Knepley         Ne = numFaces;
356492d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
356592d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
356692d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
356792d50984SMatthew G. Knepley       } else SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
356892d50984SMatthew G. Knepley     }
356992d50984SMatthew G. Knepley     /* Loop over domain */
357092d50984SMatthew G. Knepley     if (useFEM) {
357192d50984SMatthew G. Knepley       /* Add elemVec to locX */
357292d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
357392d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
357492d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
357592d50984SMatthew G. Knepley 
357692d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
357792d50984SMatthew G. Knepley         if (ghostLabel) {
357892d50984SMatthew G. Knepley           PetscInt ghostVal;
357992d50984SMatthew G. Knepley 
358092d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
358192d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
358292d50984SMatthew G. Knepley         }
358392d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
358492d50984SMatthew G. Knepley       }
358592d50984SMatthew G. Knepley     }
358692d50984SMatthew G. Knepley     /* Handle time derivative */
358792d50984SMatthew G. Knepley     if (locX_t) {
358892d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
358992d50984SMatthew G. Knepley 
359092d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
359192d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
359292d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
359392d50984SMatthew G. Knepley         PetscFV      fv;
359492d50984SMatthew G. Knepley         PetscObject  obj;
359592d50984SMatthew G. Knepley         PetscClassId id;
359692d50984SMatthew G. Knepley         PetscInt     pdim, d;
359792d50984SMatthew G. Knepley 
359892d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
359992d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
360092d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
360192d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
360292d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
360392d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
360492d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
360592d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
360692d50984SMatthew G. Knepley 
360792d50984SMatthew G. Knepley           if (ghostLabel) {
360892d50984SMatthew G. Knepley             PetscInt ghostVal;
360992d50984SMatthew G. Knepley 
361092d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
361192d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
361292d50984SMatthew G. Knepley           }
361392d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
361492d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
361592d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
361692d50984SMatthew G. Knepley         }
361792d50984SMatthew G. Knepley       }
361892d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
361992d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
362092d50984SMatthew G. Knepley     }
362192d50984SMatthew G. Knepley     if (useFEM) {
362292d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
362392d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
362492d50984SMatthew G. Knepley     }
362592d50984SMatthew G. Knepley   }
362692d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
362792d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
362892d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
362992d50984SMatthew G. Knepley   if (useFEM) {
363092d50984SMatthew G. Knepley     if (maxDegree <= 1) {
363192d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
363292d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
363392d50984SMatthew G. Knepley     } else {
363492d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
363592d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
363692d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
363792d50984SMatthew G. Knepley       }
363892d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
363992d50984SMatthew G. Knepley     }
364092d50984SMatthew G. Knepley   }
364192d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
364292d50984SMatthew G. Knepley   PetscFunctionReturn(0);
364392d50984SMatthew G. Knepley }
364492d50984SMatthew G. Knepley 
3645a1cf66bbSMatthew G. Knepley /*
3646a1cf66bbSMatthew 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
3647a1cf66bbSMatthew G. Knepley 
3648a1cf66bbSMatthew G. Knepley   X   - The local solution vector
3649a1cf66bbSMatthew G. Knepley   X_t - The local solution time derviative vector, or NULL
3650a1cf66bbSMatthew G. Knepley */
3651a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
3652a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
3653a1cf66bbSMatthew G. Knepley {
3654a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
3655a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
3656a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
3657a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
3658a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
3659a1cf66bbSMatthew G. Knepley   Vec              A;
3660a1cf66bbSMatthew G. Knepley   DMField          coordField;
3661a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
3662a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
3663a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
3664a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
36659b2fc754SMatthew G. Knepley   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, isMatIS, isMatISP, *isFE, hasFV = PETSC_FALSE;
3666a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
36679b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
3668a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
3669a1cf66bbSMatthew G. Knepley 
3670a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3671a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3672a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
3673a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
3674a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3675a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
3676a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3677a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3678a1cf66bbSMatthew G. Knepley   if (dmAux) {
3679a1cf66bbSMatthew G. Knepley     ierr = DMGetDefaultSection(dmAux, &sectionAux);CHKERRQ(ierr);
3680a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
3681a1cf66bbSMatthew G. Knepley   }
3682a1cf66bbSMatthew G. Knepley   /* Get flags */
3683a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
3684a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3685a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
3686a1cf66bbSMatthew G. Knepley     PetscObject  disc;
3687a1cf66bbSMatthew G. Knepley     PetscClassId id;
3688a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
3689a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
3690a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
3691a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
3692a1cf66bbSMatthew G. Knepley   }
3693a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
3694a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
3695a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
3696a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
3697a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
3698a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) Jac,  MATIS, &isMatIS);CHKERRQ(ierr);
3699a1cf66bbSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) JacP, MATIS, &isMatISP);CHKERRQ(ierr);
3700a1cf66bbSMatthew G. Knepley   /* Setup input data and temp arrays (should be DMGetWorkArray) */
3701a1cf66bbSMatthew G. Knepley   if (isMatISP || isMatISP) {ierr = DMPlexGetSubdomainSection(dm, &globalSection);CHKERRQ(ierr);}
3702a1cf66bbSMatthew G. Knepley   if (isMatIS)  {ierr = MatISGetLocalMat(Jac,  &J);CHKERRQ(ierr);}
3703a1cf66bbSMatthew G. Knepley   if (isMatISP) {ierr = MatISGetLocalMat(JacP, &JP);CHKERRQ(ierr);}
3704a1cf66bbSMatthew 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 */
3705a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "dmAux", (PetscObject *) &dmAux);CHKERRQ(ierr);
3706a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) dm, "A", (PetscObject *) &A);CHKERRQ(ierr);
3707a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3708a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
3709a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3710a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
3711a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
3712a1cf66bbSMatthew G. Knepley     PetscFE         fe;
3713a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
3714a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
3715a1cf66bbSMatthew G. Knepley 
3716a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3717a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
3718a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
3719a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
3720a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
3721a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
3722a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
3723a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
3724a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
3725a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
3726a1cf66bbSMatthew G. Knepley   } else {
3727a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
3728a1cf66bbSMatthew G. Knepley     numChunks = 1;
3729a1cf66bbSMatthew G. Knepley   }
3730a1cf66bbSMatthew G. Knepley   /* Get work space */
3731a1cf66bbSMatthew 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;
3732a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
3733a1cf66bbSMatthew G. Knepley   ierr = PetscMemzero(work, wsz * sizeof(PetscScalar));CHKERRQ(ierr);
3734a1cf66bbSMatthew G. Knepley   off      = 0;
3735a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3736a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
3737a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
3738a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3739a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3740a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
3741a1cf66bbSMatthew G. Knepley   if (off != wsz) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
3742a1cf66bbSMatthew G. Knepley   /* Setup geometry */
3743a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
37449b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
37459b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
3746a1cf66bbSMatthew G. Knepley   if (!qGeom) {
3747a1cf66bbSMatthew G. Knepley     PetscFE fe;
3748a1cf66bbSMatthew G. Knepley 
3749a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
3750a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
3751a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
3752a1cf66bbSMatthew G. Knepley   }
3753a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3754a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
3755a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
3756a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
3757a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
3758a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
3759a1cf66bbSMatthew G. Knepley     PetscInt         c;
3760a1cf66bbSMatthew G. Knepley 
3761a1cf66bbSMatthew G. Knepley     /* Extract values */
3762a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3763a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3764a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
3765a1cf66bbSMatthew G. Knepley       PetscInt       i;
3766a1cf66bbSMatthew G. Knepley 
3767a1cf66bbSMatthew G. Knepley       if (X) {
3768a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3769a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
3770a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
3771a1cf66bbSMatthew G. Knepley       }
3772a1cf66bbSMatthew G. Knepley       if (X_t) {
3773a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3774a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
3775a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
3776a1cf66bbSMatthew G. Knepley       }
3777a1cf66bbSMatthew G. Knepley       if (dmAux) {
3778a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3779a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
3780a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
3781a1cf66bbSMatthew G. Knepley       }
3782a1cf66bbSMatthew G. Knepley     }
3783a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3784a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
3785a1cf66bbSMatthew G. Knepley       PetscFE fe;
3786a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
3787a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
3788a1cf66bbSMatthew 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);}
3789a1cf66bbSMatthew 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);}
3790a1cf66bbSMatthew 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);}
3791a1cf66bbSMatthew G. Knepley       }
3792a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
3793a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
3794a1cf66bbSMatthew G. Knepley         PetscFV  fv;
3795a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
3796a1cf66bbSMatthew G. Knepley 
3797a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
3798a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
3799a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
3800a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
3801a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
3802a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
3803a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
3804a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
3805a1cf66bbSMatthew G. Knepley           }
3806a1cf66bbSMatthew G. Knepley         }
3807a1cf66bbSMatthew G. Knepley       }
3808a1cf66bbSMatthew G. Knepley     }
3809a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3810a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
3811a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
3812a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
3813a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
3814a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
3815a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
3816a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3817a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
3818a1cf66bbSMatthew G. Knepley       }
3819a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
3820a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
3821a1cf66bbSMatthew G. Knepley     }
3822a1cf66bbSMatthew G. Knepley     CHKMEMQ;
3823a1cf66bbSMatthew G. Knepley   }
3824a1cf66bbSMatthew G. Knepley   /* Cleanup */
3825a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
3826a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
3827a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
3828a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
3829a1cf66bbSMatthew 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);
3830a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
3831a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
3832a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
3833a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
3834a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3835a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
3836a1cf66bbSMatthew G. Knepley   CHKMEMQ;
3837a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3838a1cf66bbSMatthew G. Knepley }
38398272889dSSatish Balay 
3840