xref: /petsc/src/dm/impls/plex/plexfem.c (revision 5f0b18bf3866bcc0fef3582e3e51025fad7fba6f)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2da97024aSMatthew G. Knepley #include <petscsf.h>
3cb1e1211SMatthew G Knepley 
48e3a2eefSMatthew G. Knepley #include <petscblaslapack.h>
5e8f14785SLisandro Dalcin #include <petsc/private/hashsetij.h>
6af0996ceSBarry Smith #include <petsc/private/petscfeimpl.h>
7af0996ceSBarry Smith #include <petsc/private/petscfvimpl.h>
8a0845e3aSMatthew G. Knepley 
9*5f0b18bfSMatthew G. Knepley PetscBool Clementcite = PETSC_FALSE;
10*5f0b18bfSMatthew G. Knepley const char ClementCitation[] = "@article{clement1975approximation,\n"
11*5f0b18bfSMatthew G. Knepley                                "  title   = {Approximation by finite element functions using local regularization},\n"
12*5f0b18bfSMatthew G. Knepley                                "  author  = {Philippe Cl{\\'e}ment},\n"
13*5f0b18bfSMatthew G. Knepley                                "  journal = {Revue fran{\\c{c}}aise d'automatique, informatique, recherche op{\\'e}rationnelle. Analyse num{\\'e}rique},\n"
14*5f0b18bfSMatthew G. Knepley                                "  volume  = {9},\n"
15*5f0b18bfSMatthew G. Knepley                                "  number  = {R2},\n"
16*5f0b18bfSMatthew G. Knepley                                "  pages   = {77--84},\n"
17*5f0b18bfSMatthew G. Knepley                                "  year    = {1975}\n}\n";
18*5f0b18bfSMatthew G. Knepley 
192f856554SMatthew G. Knepley static PetscErrorCode DMPlexConvertPlex(DM dm, DM *plex, PetscBool copy)
202f856554SMatthew G. Knepley {
212f856554SMatthew G. Knepley   PetscBool      isPlex;
222f856554SMatthew G. Knepley   PetscErrorCode ierr;
232f856554SMatthew G. Knepley 
242f856554SMatthew G. Knepley   PetscFunctionBegin;
252f856554SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
262f856554SMatthew G. Knepley   if (isPlex) {
272f856554SMatthew G. Knepley     *plex = dm;
282f856554SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
292f856554SMatthew G. Knepley   } else {
302f856554SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
312f856554SMatthew G. Knepley     if (!*plex) {
322f856554SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, plex);CHKERRQ(ierr);
332f856554SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
342f856554SMatthew G. Knepley       if (copy) {
352f856554SMatthew G. Knepley         DMSubDomainHookLink link;
369a2a23afSMatthew G. Knepley 
379a2a23afSMatthew G. Knepley         ierr = DMCopyAuxiliaryVec(dm, *plex);CHKERRQ(ierr);
389a2a23afSMatthew G. Knepley         /* Run the subdomain hook (this will copy the DMSNES/DMTS) */
392f856554SMatthew G. Knepley         for (link = dm->subdomainhook; link; link = link->next) {
402f856554SMatthew G. Knepley           if (link->ddhook) {ierr = (*link->ddhook)(dm, *plex, link->ctx);CHKERRQ(ierr);}
412f856554SMatthew G. Knepley         }
422f856554SMatthew G. Knepley       }
432f856554SMatthew G. Knepley     } else {
442f856554SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
452f856554SMatthew G. Knepley     }
462f856554SMatthew G. Knepley   }
472f856554SMatthew G. Knepley   PetscFunctionReturn(0);
482f856554SMatthew G. Knepley }
492f856554SMatthew G. Knepley 
509b6f715bSMatthew G. Knepley static PetscErrorCode PetscContainerUserDestroy_PetscFEGeom (void *ctx)
519b6f715bSMatthew G. Knepley {
529b6f715bSMatthew G. Knepley   PetscFEGeom *geom = (PetscFEGeom *) ctx;
539b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
549b6f715bSMatthew G. Knepley 
559b6f715bSMatthew G. Knepley   PetscFunctionBegin;
569b6f715bSMatthew G. Knepley   ierr = PetscFEGeomDestroy(&geom);CHKERRQ(ierr);
579b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
589b6f715bSMatthew G. Knepley }
599b6f715bSMatthew G. Knepley 
609b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
619b6f715bSMatthew G. Knepley {
629b6f715bSMatthew G. Knepley   char            composeStr[33] = {0};
639b6f715bSMatthew G. Knepley   PetscObjectId   id;
649b6f715bSMatthew G. Knepley   PetscContainer  container;
659b6f715bSMatthew G. Knepley   PetscErrorCode  ierr;
669b6f715bSMatthew G. Knepley 
679b6f715bSMatthew G. Knepley   PetscFunctionBegin;
689b6f715bSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
699b6f715bSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMPlexGetFEGeom_%x\n", id);CHKERRQ(ierr);
709b6f715bSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
719b6f715bSMatthew G. Knepley   if (container) {
729b6f715bSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
739b6f715bSMatthew G. Knepley   } else {
749b6f715bSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
759b6f715bSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
769b6f715bSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
779b6f715bSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
789b6f715bSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
799b6f715bSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
809b6f715bSMatthew G. Knepley   }
819b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
829b6f715bSMatthew G. Knepley }
839b6f715bSMatthew G. Knepley 
849b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
859b6f715bSMatthew G. Knepley {
869b6f715bSMatthew G. Knepley   PetscFunctionBegin;
879b6f715bSMatthew G. Knepley   *geom = NULL;
889b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
899b6f715bSMatthew G. Knepley }
909b6f715bSMatthew G. Knepley 
9146fa42a0SMatthew G. Knepley /*@
9246fa42a0SMatthew G. Knepley   DMPlexGetScale - Get the scale for the specified fundamental unit
9346fa42a0SMatthew G. Knepley 
9446fa42a0SMatthew G. Knepley   Not collective
9546fa42a0SMatthew G. Knepley 
964165533cSJose E. Roman   Input Parameters:
9746fa42a0SMatthew G. Knepley + dm   - the DM
9846fa42a0SMatthew G. Knepley - unit - The SI unit
9946fa42a0SMatthew G. Knepley 
1004165533cSJose E. Roman   Output Parameter:
10146fa42a0SMatthew G. Knepley . scale - The value used to scale all quantities with this unit
10246fa42a0SMatthew G. Knepley 
10346fa42a0SMatthew G. Knepley   Level: advanced
10446fa42a0SMatthew G. Knepley 
10546fa42a0SMatthew G. Knepley .seealso: DMPlexSetScale(), PetscUnit
10646fa42a0SMatthew G. Knepley @*/
107cb1e1211SMatthew G Knepley PetscErrorCode DMPlexGetScale(DM dm, PetscUnit unit, PetscReal *scale)
108cb1e1211SMatthew G Knepley {
109cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
110cb1e1211SMatthew G Knepley 
111cb1e1211SMatthew G Knepley   PetscFunctionBegin;
112cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
113cb1e1211SMatthew G Knepley   PetscValidPointer(scale, 3);
114cb1e1211SMatthew G Knepley   *scale = mesh->scale[unit];
115cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
116cb1e1211SMatthew G Knepley }
117cb1e1211SMatthew G Knepley 
11846fa42a0SMatthew G. Knepley /*@
11946fa42a0SMatthew G. Knepley   DMPlexSetScale - Set the scale for the specified fundamental unit
12046fa42a0SMatthew G. Knepley 
12146fa42a0SMatthew G. Knepley   Not collective
12246fa42a0SMatthew G. Knepley 
1234165533cSJose E. Roman   Input Parameters:
12446fa42a0SMatthew G. Knepley + dm   - the DM
12546fa42a0SMatthew G. Knepley . unit - The SI unit
12646fa42a0SMatthew G. Knepley - scale - The value used to scale all quantities with this unit
12746fa42a0SMatthew G. Knepley 
12846fa42a0SMatthew G. Knepley   Level: advanced
12946fa42a0SMatthew G. Knepley 
13046fa42a0SMatthew G. Knepley .seealso: DMPlexGetScale(), PetscUnit
13146fa42a0SMatthew G. Knepley @*/
132cb1e1211SMatthew G Knepley PetscErrorCode DMPlexSetScale(DM dm, PetscUnit unit, PetscReal scale)
133cb1e1211SMatthew G Knepley {
134cb1e1211SMatthew G Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
135cb1e1211SMatthew G Knepley 
136cb1e1211SMatthew G Knepley   PetscFunctionBegin;
137cb1e1211SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
138cb1e1211SMatthew G Knepley   mesh->scale[unit] = scale;
139cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
140cb1e1211SMatthew G Knepley }
141cb1e1211SMatthew G Knepley 
142327779f2SMatthew Knepley static PetscErrorCode DMPlexProjectRigidBody_Private(PetscInt dim, PetscReal t, const PetscReal X[], PetscInt Nc, PetscScalar *mode, void *ctx)
143026175e5SToby Isaac {
14412adca46SMatthew 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}}};
145026175e5SToby Isaac   PetscInt *ctxInt  = (PetscInt *) ctx;
146ad917190SMatthew G. Knepley   PetscInt  dim2    = ctxInt[0];
147026175e5SToby Isaac   PetscInt  d       = ctxInt[1];
148026175e5SToby Isaac   PetscInt  i, j, k = dim > 2 ? d - dim : d;
149026175e5SToby Isaac 
150ad917190SMatthew G. Knepley   PetscFunctionBegin;
1512c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dim != dim2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input dimension %D does not match context dimension %D", dim, dim2);
152026175e5SToby Isaac   for (i = 0; i < dim; i++) mode[i] = 0.;
153026175e5SToby Isaac   if (d < dim) {
15412adca46SMatthew G. Knepley     mode[d] = 1.; /* Translation along axis d */
155ad917190SMatthew G. Knepley   } else {
156026175e5SToby Isaac     for (i = 0; i < dim; i++) {
157026175e5SToby Isaac       for (j = 0; j < dim; j++) {
15812adca46SMatthew G. Knepley         mode[j] += eps[i][j][k]*X[i]; /* Rotation about axis d */
159026175e5SToby Isaac       }
160026175e5SToby Isaac     }
161026175e5SToby Isaac   }
162ad917190SMatthew G. Knepley   PetscFunctionReturn(0);
163026175e5SToby Isaac }
164026175e5SToby Isaac 
165cc4e42d9SMartin Diehl /*@
16612adca46SMatthew G. Knepley   DMPlexCreateRigidBody - For the default global section, create rigid body modes by function space interpolation
167cb1e1211SMatthew G Knepley 
168d083f849SBarry Smith   Collective on dm
169cb1e1211SMatthew G Knepley 
1704165533cSJose E. Roman   Input Parameters:
17156cf3b9cSMatthew G. Knepley + dm - the DM
17256cf3b9cSMatthew G. Knepley - field - The field number for the rigid body space, or 0 for the default
173cb1e1211SMatthew G Knepley 
1744165533cSJose E. Roman   Output Parameter:
175cb1e1211SMatthew G Knepley . sp - the null space
176cb1e1211SMatthew G Knepley 
17712adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
178cb1e1211SMatthew G Knepley 
179cb1e1211SMatthew G Knepley   Level: advanced
180cb1e1211SMatthew G Knepley 
18112adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate(), PCGAMG
182cb1e1211SMatthew G Knepley @*/
18356cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBody(DM dm, PetscInt field, MatNullSpace *sp)
184cb1e1211SMatthew G Knepley {
18556cf3b9cSMatthew G. Knepley   PetscErrorCode (**func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *);
186cb1e1211SMatthew G Knepley   MPI_Comm          comm;
187026175e5SToby Isaac   Vec               mode[6];
188026175e5SToby Isaac   PetscSection      section, globalSection;
18956cf3b9cSMatthew G. Knepley   PetscInt          dim, dimEmbed, Nf, n, m, mmin, d, i, j;
190cb1e1211SMatthew G Knepley   PetscErrorCode    ierr;
191cb1e1211SMatthew G Knepley 
192cb1e1211SMatthew G Knepley   PetscFunctionBegin;
193cb1e1211SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
194c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1959d8fbdeaSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
19656cf3b9cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1972c71b3e2SJacob Faibussowitsch   PetscCheckFalse(Nf && (field < 0 || field >= Nf),comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %D is not in [0, Nf)", field, Nf);
19856cf3b9cSMatthew G. Knepley   if (dim == 1 && Nf < 2) {
199cb1e1211SMatthew G Knepley     ierr = MatNullSpaceCreate(comm, PETSC_TRUE, 0, NULL, sp);CHKERRQ(ierr);
200cb1e1211SMatthew G Knepley     PetscFunctionReturn(0);
201cb1e1211SMatthew G Knepley   }
20292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
203e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
204cb1e1211SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
20556cf3b9cSMatthew G. Knepley   ierr = PetscCalloc1(Nf, &func);CHKERRQ(ierr);
206b247467aSMatthew G. Knepley   m    = (dim*(dim+1))/2;
207cb1e1211SMatthew G Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
208aef2e8d7SJed Brown   ierr = VecSetType(mode[0], dm->vectype);CHKERRQ(ierr);
209cb1e1211SMatthew G Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
210cb1e1211SMatthew G Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
211b247467aSMatthew G. Knepley   ierr = VecGetSize(mode[0], &n);CHKERRQ(ierr);
212b247467aSMatthew G. Knepley   mmin = PetscMin(m, n);
21356cf3b9cSMatthew G. Knepley   func[field] = DMPlexProjectRigidBody_Private;
214cb1e1211SMatthew G Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
215026175e5SToby Isaac   for (d = 0; d < m; d++) {
216330485fdSToby Isaac     PetscInt ctx[2];
217026175e5SToby Isaac     void    *voidctx = (void *) (&ctx[0]);
218cb1e1211SMatthew G Knepley 
2199d8fbdeaSMatthew G. Knepley     ctx[0] = dimEmbed;
220330485fdSToby Isaac     ctx[1] = d;
22156cf3b9cSMatthew G. Knepley     ierr = DMProjectFunction(dm, 0.0, func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
222cb1e1211SMatthew G Knepley   }
2233b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
224b50a2c0aSJacob Faibussowitsch   for (i = 0; i < mmin; ++i) {
225b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
226b50a2c0aSJacob Faibussowitsch 
2279019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
228b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], mmin-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
229b50a2c0aSJacob Faibussowitsch     for (j = i+1; j < mmin; ++j) {
230b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
231b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
232b50a2c0aSJacob Faibussowitsch     }
233cb1e1211SMatthew G Knepley   }
234b247467aSMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, mmin, mode, sp);CHKERRQ(ierr);
235b247467aSMatthew G. Knepley   for (i = 0; i < m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
23656cf3b9cSMatthew G. Knepley   ierr = PetscFree(func);CHKERRQ(ierr);
237cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
238cb1e1211SMatthew G Knepley }
239cb1e1211SMatthew G Knepley 
240cc4e42d9SMartin Diehl /*@
24112adca46SMatthew G. Knepley   DMPlexCreateRigidBodies - For the default global section, create rigid body modes by function space interpolation
24212adca46SMatthew G. Knepley 
243d083f849SBarry Smith   Collective on dm
24412adca46SMatthew G. Knepley 
2454165533cSJose E. Roman   Input Parameters:
24612adca46SMatthew G. Knepley + dm    - the DM
24712adca46SMatthew G. Knepley . nb    - The number of bodies
24812adca46SMatthew G. Knepley . label - The DMLabel marking each domain
24912adca46SMatthew G. Knepley . nids  - The number of ids per body
25012adca46SMatthew G. Knepley - ids   - An array of the label ids in sequence for each domain
25112adca46SMatthew G. Knepley 
2524165533cSJose E. Roman   Output Parameter:
25312adca46SMatthew G. Knepley . sp - the null space
25412adca46SMatthew G. Knepley 
25512adca46SMatthew G. Knepley   Note: This is necessary to provide a suitable coarse space for algebraic multigrid
25612adca46SMatthew G. Knepley 
25712adca46SMatthew G. Knepley   Level: advanced
25812adca46SMatthew G. Knepley 
25912adca46SMatthew G. Knepley .seealso: MatNullSpaceCreate()
26012adca46SMatthew G. Knepley @*/
26112adca46SMatthew G. Knepley PetscErrorCode DMPlexCreateRigidBodies(DM dm, PetscInt nb, DMLabel label, const PetscInt nids[], const PetscInt ids[], MatNullSpace *sp)
26212adca46SMatthew G. Knepley {
26312adca46SMatthew G. Knepley   MPI_Comm       comm;
26412adca46SMatthew G. Knepley   PetscSection   section, globalSection;
26512adca46SMatthew G. Knepley   Vec           *mode;
26612adca46SMatthew G. Knepley   PetscScalar   *dots;
26712adca46SMatthew G. Knepley   PetscInt       dim, dimEmbed, n, m, b, d, i, j, off;
26812adca46SMatthew G. Knepley   PetscErrorCode ierr;
26912adca46SMatthew G. Knepley 
27012adca46SMatthew G. Knepley   PetscFunctionBegin;
27112adca46SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
27212adca46SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
27312adca46SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
27492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
275e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
27612adca46SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &n);CHKERRQ(ierr);
27712adca46SMatthew G. Knepley   m    = nb * (dim*(dim+1))/2;
27812adca46SMatthew G. Knepley   ierr = PetscMalloc2(m, &mode, m, &dots);CHKERRQ(ierr);
27912adca46SMatthew G. Knepley   ierr = VecCreate(comm, &mode[0]);CHKERRQ(ierr);
28012adca46SMatthew G. Knepley   ierr = VecSetSizes(mode[0], n, PETSC_DETERMINE);CHKERRQ(ierr);
28112adca46SMatthew G. Knepley   ierr = VecSetUp(mode[0]);CHKERRQ(ierr);
28212adca46SMatthew G. Knepley   for (i = 1; i < m; ++i) {ierr = VecDuplicate(mode[0], &mode[i]);CHKERRQ(ierr);}
28312adca46SMatthew G. Knepley   for (b = 0, off = 0; b < nb; ++b) {
28412adca46SMatthew G. Knepley     for (d = 0; d < m/nb; ++d) {
28512adca46SMatthew G. Knepley       PetscInt         ctx[2];
28612adca46SMatthew G. Knepley       PetscErrorCode (*func)(PetscInt, PetscReal, const PetscReal *, PetscInt, PetscScalar *, void *) = DMPlexProjectRigidBody_Private;
28712adca46SMatthew G. Knepley       void            *voidctx = (void *) (&ctx[0]);
28812adca46SMatthew G. Knepley 
28912adca46SMatthew G. Knepley       ctx[0] = dimEmbed;
29012adca46SMatthew G. Knepley       ctx[1] = d;
29112adca46SMatthew G. Knepley       ierr = DMProjectFunctionLabel(dm, 0.0, label, nids[b], &ids[off], 0, NULL, &func, &voidctx, INSERT_VALUES, mode[d]);CHKERRQ(ierr);
29212adca46SMatthew G. Knepley       off   += nids[b];
29312adca46SMatthew G. Knepley     }
29412adca46SMatthew G. Knepley   }
2953b2202bfSJacob Faibussowitsch   /* Orthonormalize system */
296606c1a1cSJacob Faibussowitsch   for (i = 0; i < m; ++i) {
297b77f2eeeSJacob Faibussowitsch     PetscScalar dots[6];
2985a0e29b9SJacob Faibussowitsch 
2999019a6d7SJacob Faibussowitsch     ierr = VecNormalize(mode[i], NULL);CHKERRQ(ierr);
300b77f2eeeSJacob Faibussowitsch     ierr = VecMDot(mode[i], m-i-1, mode+i+1, dots+i+1);CHKERRQ(ierr);
3015a0e29b9SJacob Faibussowitsch     for (j = i+1; j < m; ++j) {
302b77f2eeeSJacob Faibussowitsch       dots[j] *= -1.0;
303b77f2eeeSJacob Faibussowitsch       ierr = VecAXPY(mode[j], dots[j], mode[i]);CHKERRQ(ierr);
3045a0e29b9SJacob Faibussowitsch     }
30512adca46SMatthew G. Knepley   }
30612adca46SMatthew G. Knepley   ierr = MatNullSpaceCreate(comm, PETSC_FALSE, m, mode, sp);CHKERRQ(ierr);
30712adca46SMatthew G. Knepley   for (i = 0; i< m; ++i) {ierr = VecDestroy(&mode[i]);CHKERRQ(ierr);}
30812adca46SMatthew G. Knepley   ierr = PetscFree2(mode, dots);CHKERRQ(ierr);
30912adca46SMatthew G. Knepley   PetscFunctionReturn(0);
31012adca46SMatthew G. Knepley }
31112adca46SMatthew G. Knepley 
312b29cfa1cSToby Isaac /*@
313b29cfa1cSToby Isaac   DMPlexSetMaxProjectionHeight - In DMPlexProjectXXXLocal() functions, the projected values of a basis function's dofs
314b29cfa1cSToby Isaac   are computed by associating the basis function with one of the mesh points in its transitively-closed support, and
315b29cfa1cSToby Isaac   evaluating the dual space basis of that point.  A basis function is associated with the point in its
316b29cfa1cSToby Isaac   transitively-closed support whose mesh height is highest (w.r.t. DAG height), but not greater than the maximum
317b29cfa1cSToby Isaac   projection height, which is set with this function.  By default, the maximum projection height is zero, which means
318b29cfa1cSToby Isaac   that only mesh cells are used to project basis functions.  A height of one, for example, evaluates a cell-interior
319b29cfa1cSToby Isaac   basis functions using its cells dual space basis, but all other basis functions with the dual space basis of a face.
320b29cfa1cSToby Isaac 
321b29cfa1cSToby Isaac   Input Parameters:
322b29cfa1cSToby Isaac + dm - the DMPlex object
323b29cfa1cSToby Isaac - height - the maximum projection height >= 0
324b29cfa1cSToby Isaac 
325b29cfa1cSToby Isaac   Level: advanced
326b29cfa1cSToby Isaac 
3274d6f44ffSToby Isaac .seealso: DMPlexGetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
328b29cfa1cSToby Isaac @*/
329b29cfa1cSToby Isaac PetscErrorCode DMPlexSetMaxProjectionHeight(DM dm, PetscInt height)
330b29cfa1cSToby Isaac {
331b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
332b29cfa1cSToby Isaac 
333b29cfa1cSToby Isaac   PetscFunctionBegin;
334b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335b29cfa1cSToby Isaac   plex->maxProjectionHeight = height;
336b29cfa1cSToby Isaac   PetscFunctionReturn(0);
337b29cfa1cSToby Isaac }
338b29cfa1cSToby Isaac 
339b29cfa1cSToby Isaac /*@
340b29cfa1cSToby Isaac   DMPlexGetMaxProjectionHeight - Get the maximum height (w.r.t. DAG) of mesh points used to evaluate dual bases in
341b29cfa1cSToby Isaac   DMPlexProjectXXXLocal() functions.
342b29cfa1cSToby Isaac 
343b29cfa1cSToby Isaac   Input Parameters:
344b29cfa1cSToby Isaac . dm - the DMPlex object
345b29cfa1cSToby Isaac 
346b29cfa1cSToby Isaac   Output Parameters:
347b29cfa1cSToby Isaac . height - the maximum projection height
348b29cfa1cSToby Isaac 
349b29cfa1cSToby Isaac   Level: intermediate
350b29cfa1cSToby Isaac 
3514d6f44ffSToby Isaac .seealso: DMPlexSetMaxProjectionHeight(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal()
352b29cfa1cSToby Isaac @*/
353b29cfa1cSToby Isaac PetscErrorCode DMPlexGetMaxProjectionHeight(DM dm, PetscInt *height)
354b29cfa1cSToby Isaac {
355b29cfa1cSToby Isaac   DM_Plex *plex = (DM_Plex *) dm->data;
356b29cfa1cSToby Isaac 
357b29cfa1cSToby Isaac   PetscFunctionBegin;
358b29cfa1cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
359b29cfa1cSToby Isaac   *height = plex->maxProjectionHeight;
360b29cfa1cSToby Isaac   PetscFunctionReturn(0);
361b29cfa1cSToby Isaac }
362b29cfa1cSToby Isaac 
363ca3d3a14SMatthew G. Knepley typedef struct {
364ca3d3a14SMatthew G. Knepley   PetscReal    alpha; /* The first Euler angle, and in 2D the only one */
365ca3d3a14SMatthew G. Knepley   PetscReal    beta;  /* The second Euler angle */
366ca3d3a14SMatthew G. Knepley   PetscReal    gamma; /* The third Euler angle */
367ca3d3a14SMatthew G. Knepley   PetscInt     dim;   /* The dimension of R */
368ca3d3a14SMatthew G. Knepley   PetscScalar *R;     /* The rotation matrix, transforming a vector in the local basis to the global basis */
369ca3d3a14SMatthew G. Knepley   PetscScalar *RT;    /* The transposed rotation matrix, transforming a vector in the global basis to the local basis */
370ca3d3a14SMatthew G. Knepley } RotCtx;
371ca3d3a14SMatthew G. Knepley 
372ca3d3a14SMatthew G. Knepley /*
373ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
374ca3d3a14SMatthew 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:
375ca3d3a14SMatthew 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.
376ca3d3a14SMatthew 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.
377ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
378ca3d3a14SMatthew G. Knepley */
379ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformSetUp_Rotation_Internal(DM dm, void *ctx)
380ca3d3a14SMatthew G. Knepley {
381ca3d3a14SMatthew G. Knepley   RotCtx        *rc  = (RotCtx *) ctx;
382ca3d3a14SMatthew G. Knepley   PetscInt       dim = rc->dim;
383ca3d3a14SMatthew G. Knepley   PetscReal      c1, s1, c2, s2, c3, s3;
384ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
385ca3d3a14SMatthew G. Knepley 
386ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
387ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(dim), &rc->R, PetscSqr(dim), &rc->RT);CHKERRQ(ierr);
388ca3d3a14SMatthew G. Knepley   switch (dim) {
389ca3d3a14SMatthew G. Knepley   case 2:
390ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
391ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1;rc->R[1] = s1;
392ca3d3a14SMatthew G. Knepley     rc->R[2] = -s1;rc->R[3] = c1;
393580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
394b458e8f1SJose E. Roman     DMPlex_Transpose2D_Internal(rc->RT);
395ca3d3a14SMatthew G. Knepley     break;
396ca3d3a14SMatthew G. Knepley   case 3:
397ca3d3a14SMatthew G. Knepley     c1 = PetscCosReal(rc->alpha);s1 = PetscSinReal(rc->alpha);
398ca3d3a14SMatthew G. Knepley     c2 = PetscCosReal(rc->beta); s2 = PetscSinReal(rc->beta);
399ca3d3a14SMatthew G. Knepley     c3 = PetscCosReal(rc->gamma);s3 = PetscSinReal(rc->gamma);
400ca3d3a14SMatthew G. Knepley     rc->R[0] =  c1*c3 - c2*s1*s3;rc->R[1] =  c3*s1    + c1*c2*s3;rc->R[2] = s2*s3;
401ca3d3a14SMatthew G. Knepley     rc->R[3] = -c1*s3 - c2*c3*s1;rc->R[4] =  c1*c2*c3 - s1*s3;   rc->R[5] = c3*s2;
402ca3d3a14SMatthew G. Knepley     rc->R[6] =  s1*s2;           rc->R[7] = -c1*s2;              rc->R[8] = c2;
403580bdb30SBarry Smith     ierr = PetscArraycpy(rc->RT, rc->R, PetscSqr(dim));CHKERRQ(ierr);
404b458e8f1SJose E. Roman     DMPlex_Transpose3D_Internal(rc->RT);
405ca3d3a14SMatthew G. Knepley     break;
40698921bdaSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
407ca3d3a14SMatthew G. Knepley   }
408ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
409ca3d3a14SMatthew G. Knepley }
410ca3d3a14SMatthew G. Knepley 
411ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformDestroy_Rotation_Internal(DM dm, void *ctx)
412ca3d3a14SMatthew G. Knepley {
413ca3d3a14SMatthew G. Knepley   RotCtx        *rc = (RotCtx *) ctx;
414ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
415ca3d3a14SMatthew G. Knepley 
416ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
417ca3d3a14SMatthew G. Knepley   ierr = PetscFree2(rc->R, rc->RT);CHKERRQ(ierr);
418ca3d3a14SMatthew G. Knepley   ierr = PetscFree(rc);CHKERRQ(ierr);
419ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
420ca3d3a14SMatthew G. Knepley }
421ca3d3a14SMatthew G. Knepley 
422ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformGetMatrix_Rotation_Internal(DM dm, const PetscReal x[], PetscBool l2g, const PetscScalar **A, void *ctx)
423ca3d3a14SMatthew G. Knepley {
424ca3d3a14SMatthew G. Knepley   RotCtx *rc = (RotCtx *) ctx;
425ca3d3a14SMatthew G. Knepley 
426ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
427ca3d3a14SMatthew G. Knepley   PetscValidPointer(ctx, 5);
428ca3d3a14SMatthew G. Knepley   if (l2g) {*A = rc->R;}
429ca3d3a14SMatthew G. Knepley   else     {*A = rc->RT;}
430ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
431ca3d3a14SMatthew G. Knepley }
432ca3d3a14SMatthew G. Knepley 
433ab6a9622SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApplyReal_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscReal *y, PetscReal *z, void *ctx)
434ab6a9622SMatthew G. Knepley {
435ec277c0fSMatthew G. Knepley   PetscErrorCode ierr;
436ec277c0fSMatthew G. Knepley 
437ec277c0fSMatthew G. Knepley   PetscFunctionBegin;
438ab6a9622SMatthew G. Knepley   #if defined(PETSC_USE_COMPLEX)
439ab6a9622SMatthew G. Knepley   switch (dim) {
440ab6a9622SMatthew G. Knepley     case 2:
441ab6a9622SMatthew G. Knepley     {
44227104ee2SJacob Faibussowitsch       PetscScalar yt[2] = {y[0], y[1]}, zt[2] = {0.0,0.0};
443ab6a9622SMatthew G. Knepley 
444ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
445ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]);
446ab6a9622SMatthew G. Knepley     }
4479b4815dcSMatthew G. Knepley     break;
448ab6a9622SMatthew G. Knepley     case 3:
449ab6a9622SMatthew G. Knepley     {
45027104ee2SJacob Faibussowitsch       PetscScalar yt[3] = {y[0], y[1], y[2]}, zt[3] = {0.0,0.0,0.0};
451ab6a9622SMatthew G. Knepley 
452ec277c0fSMatthew G. Knepley       ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, yt, zt, ctx);CHKERRQ(ierr);
453ab6a9622SMatthew G. Knepley       z[0] = PetscRealPart(zt[0]); z[1] = PetscRealPart(zt[1]); z[2] = PetscRealPart(zt[2]);
454ab6a9622SMatthew G. Knepley     }
4559b4815dcSMatthew G. Knepley     break;
456ab6a9622SMatthew G. Knepley   }
457ab6a9622SMatthew G. Knepley   #else
458ab6a9622SMatthew G. Knepley   ierr = DMPlexBasisTransformApply_Internal(dm, x, l2g, dim, y, z, ctx);CHKERRQ(ierr);
459ab6a9622SMatthew G. Knepley   #endif
460ec277c0fSMatthew G. Knepley   PetscFunctionReturn(0);
461ab6a9622SMatthew G. Knepley }
462ab6a9622SMatthew G. Knepley 
463ec277c0fSMatthew G. Knepley PetscErrorCode DMPlexBasisTransformApply_Internal(DM dm, const PetscReal x[], PetscBool l2g, PetscInt dim, const PetscScalar *y, PetscScalar *z, void *ctx)
464ca3d3a14SMatthew G. Knepley {
465ca3d3a14SMatthew G. Knepley   const PetscScalar *A;
466ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
467ca3d3a14SMatthew G. Knepley 
468ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
469ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformGetMatrix)(dm, x, l2g, &A, ctx);CHKERRQ(ierr);
470ca3d3a14SMatthew G. Knepley   switch (dim) {
4711fba962aSMatthew G. Knepley   case 2: DMPlex_Mult2D_Internal(A, 1, y, z);break;
4721fba962aSMatthew G. Knepley   case 3: DMPlex_Mult3D_Internal(A, 1, y, z);break;
473ca3d3a14SMatthew G. Knepley   }
474ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
475ca3d3a14SMatthew G. Knepley }
476ca3d3a14SMatthew G. Knepley 
477ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransformField_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscInt f, PetscBool l2g, PetscScalar *a)
478ca3d3a14SMatthew G. Knepley {
479ca3d3a14SMatthew G. Knepley   PetscSection       ts;
480ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tva;
481ca3d3a14SMatthew G. Knepley   PetscInt           dof;
482ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
483ca3d3a14SMatthew G. Knepley 
484ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
48592fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
486ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
487ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
48827104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, p, f, ta, &tva);CHKERRQ(ierr);
489ca3d3a14SMatthew G. Knepley   if (l2g) {
490ca3d3a14SMatthew G. Knepley     switch (dof) {
4911fba962aSMatthew G. Knepley     case 4: DMPlex_Mult2D_Internal(tva, 1, a, a);break;
4921fba962aSMatthew G. Knepley     case 9: DMPlex_Mult3D_Internal(tva, 1, a, a);break;
493ca3d3a14SMatthew G. Knepley     }
494ca3d3a14SMatthew G. Knepley   } else {
495ca3d3a14SMatthew G. Knepley     switch (dof) {
4961fba962aSMatthew G. Knepley     case 4: DMPlex_MultTranspose2D_Internal(tva, 1, a, a);break;
4971fba962aSMatthew G. Knepley     case 9: DMPlex_MultTranspose3D_Internal(tva, 1, a, a);break;
498ca3d3a14SMatthew G. Knepley     }
499ca3d3a14SMatthew G. Knepley   }
500ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
501ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
502ca3d3a14SMatthew G. Knepley }
503ca3d3a14SMatthew G. Knepley 
504ca3d3a14SMatthew 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)
505ca3d3a14SMatthew G. Knepley {
506ca3d3a14SMatthew G. Knepley   PetscSection       s, ts;
507ca3d3a14SMatthew G. Knepley   const PetscScalar *ta, *tvaf, *tvag;
508ca3d3a14SMatthew G. Knepley   PetscInt           fdof, gdof, fpdof, gpdof;
509ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
510ca3d3a14SMatthew G. Knepley 
511ca3d3a14SMatthew G. Knepley   PetscFunctionBeginHot;
51292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
51392fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
514ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pf, f, &fpdof);CHKERRQ(ierr);
515ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(s, pg, g, &gpdof);CHKERRQ(ierr);
516ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pf, f, &fdof);CHKERRQ(ierr);
517ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(ts, pg, g, &gdof);CHKERRQ(ierr);
518ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
51927104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, pf, f, ta, &tvaf);CHKERRQ(ierr);
52027104ee2SJacob Faibussowitsch   ierr = DMPlexPointLocalFieldRead(tdm, pg, g, ta, &tvag);CHKERRQ(ierr);
521ca3d3a14SMatthew G. Knepley   if (l2g) {
522ca3d3a14SMatthew G. Knepley     switch (fdof) {
523ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMult2D_Internal(tvaf, gpdof, lda, a, a);break;
524ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMult3D_Internal(tvaf, gpdof, lda, a, a);break;
525ca3d3a14SMatthew G. Knepley     }
526ca3d3a14SMatthew G. Knepley     switch (gdof) {
527ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTransposeLeft2D_Internal(tvag, fpdof, lda, a, a);break;
528ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTransposeLeft3D_Internal(tvag, fpdof, lda, a, a);break;
529ca3d3a14SMatthew G. Knepley     }
530ca3d3a14SMatthew G. Knepley   } else {
531ca3d3a14SMatthew G. Knepley     switch (fdof) {
532ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultTranspose2D_Internal(tvaf, gpdof, lda, a, a);break;
533ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultTranspose3D_Internal(tvaf, gpdof, lda, a, a);break;
534ca3d3a14SMatthew G. Knepley     }
535ca3d3a14SMatthew G. Knepley     switch (gdof) {
536ca3d3a14SMatthew G. Knepley     case 4: DMPlex_MatMultLeft2D_Internal(tvag, fpdof, lda, a, a);break;
537ca3d3a14SMatthew G. Knepley     case 9: DMPlex_MatMultLeft3D_Internal(tvag, fpdof, lda, a, a);break;
538ca3d3a14SMatthew G. Knepley     }
539ca3d3a14SMatthew G. Knepley   }
540ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
541ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
542ca3d3a14SMatthew G. Knepley }
543ca3d3a14SMatthew G. Knepley 
544ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPoint_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool fieldActive[], PetscBool l2g, PetscScalar *a)
545ca3d3a14SMatthew G. Knepley {
546ca3d3a14SMatthew G. Knepley   PetscSection    s;
547ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
548ca3d3a14SMatthew G. Knepley   IS              clPoints;
549ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
550ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
551ca3d3a14SMatthew G. Knepley   PetscInt        Nf, f, Np, cp, dof, d = 0;
552ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
553ca3d3a14SMatthew G. Knepley 
554ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
55592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
556ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
557ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
558ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
559ca3d3a14SMatthew G. Knepley     for (cp = 0; cp < Np*2; cp += 2) {
560ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cp], f, &dof);CHKERRQ(ierr);
561ca3d3a14SMatthew G. Knepley       if (!dof) continue;
562ca3d3a14SMatthew G. Knepley       if (fieldActive[f]) {ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, points[cp], f, l2g, &a[d]);CHKERRQ(ierr);}
563ca3d3a14SMatthew G. Knepley       d += dof;
564ca3d3a14SMatthew G. Knepley     }
565ca3d3a14SMatthew G. Knepley   }
566ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
567ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
568ca3d3a14SMatthew G. Knepley }
569ca3d3a14SMatthew G. Knepley 
570ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexBasisTransformPointTensor_Internal(DM dm, DM tdm, Vec tv, PetscInt p, PetscBool l2g, PetscInt lda, PetscScalar *a)
571ca3d3a14SMatthew G. Knepley {
572ca3d3a14SMatthew G. Knepley   PetscSection    s;
573ca3d3a14SMatthew G. Knepley   PetscSection    clSection;
574ca3d3a14SMatthew G. Knepley   IS              clPoints;
575ca3d3a14SMatthew G. Knepley   const PetscInt *clp;
576ca3d3a14SMatthew G. Knepley   PetscInt       *points = NULL;
5778bdb3c71SMatthew G. Knepley   PetscInt        Nf, f, g, Np, cpf, cpg, fdof, gdof, r, c = 0;
578ca3d3a14SMatthew G. Knepley   PetscErrorCode  ierr;
579ca3d3a14SMatthew G. Knepley 
580ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
58192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
582ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
583ca3d3a14SMatthew G. Knepley   ierr = DMPlexGetCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
584ca3d3a14SMatthew G. Knepley   for (f = 0, r = 0; f < Nf; ++f) {
585ca3d3a14SMatthew G. Knepley     for (cpf = 0; cpf < Np*2; cpf += 2) {
586ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, points[cpf], f, &fdof);CHKERRQ(ierr);
587ca3d3a14SMatthew G. Knepley       for (g = 0, c = 0; g < Nf; ++g) {
588ca3d3a14SMatthew G. Knepley         for (cpg = 0; cpg < Np*2; cpg += 2) {
589ca3d3a14SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(s, points[cpg], g, &gdof);CHKERRQ(ierr);
590ca3d3a14SMatthew G. Knepley           ierr = DMPlexBasisTransformFieldTensor_Internal(dm, tdm, tv, points[cpf], f, points[cpg], g, l2g, lda, &a[r*lda+c]);CHKERRQ(ierr);
591ca3d3a14SMatthew G. Knepley           c += gdof;
592ca3d3a14SMatthew G. Knepley         }
593ca3d3a14SMatthew G. Knepley       }
5942c71b3e2SJacob Faibussowitsch       PetscCheckFalse(c != lda,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of columns %D should be %D", c, lda);
595ca3d3a14SMatthew G. Knepley       r += fdof;
596ca3d3a14SMatthew G. Knepley     }
597ca3d3a14SMatthew G. Knepley   }
5982c71b3e2SJacob Faibussowitsch   PetscCheckFalse(r != lda,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of rows %D should be %D", c, lda);
599ca3d3a14SMatthew G. Knepley   ierr = DMPlexRestoreCompressedClosure(dm, s, p, &Np, &points, &clSection, &clPoints, &clp);CHKERRQ(ierr);
600ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
601ca3d3a14SMatthew G. Knepley }
602ca3d3a14SMatthew G. Knepley 
603ca3d3a14SMatthew G. Knepley static PetscErrorCode DMPlexBasisTransform_Internal(DM dm, Vec lv, PetscBool l2g)
604ca3d3a14SMatthew G. Knepley {
605ca3d3a14SMatthew G. Knepley   DM                 tdm;
606ca3d3a14SMatthew G. Knepley   Vec                tv;
607ca3d3a14SMatthew G. Knepley   PetscSection       ts, s;
608ca3d3a14SMatthew G. Knepley   const PetscScalar *ta;
609ca3d3a14SMatthew G. Knepley   PetscScalar       *a, *va;
610ca3d3a14SMatthew G. Knepley   PetscInt           pStart, pEnd, p, Nf, f;
611ca3d3a14SMatthew G. Knepley   PetscErrorCode     ierr;
612ca3d3a14SMatthew G. Knepley 
613ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
614ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
615ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
61692fd8e1eSJed Brown   ierr = DMGetLocalSection(tdm, &ts);CHKERRQ(ierr);
61792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
618ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
619ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
620ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(lv, &a);CHKERRQ(ierr);
621ca3d3a14SMatthew G. Knepley   ierr = VecGetArrayRead(tv, &ta);CHKERRQ(ierr);
622ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
623ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
62427104ee2SJacob Faibussowitsch       ierr = DMPlexPointLocalFieldRef(dm, p, f, a, &va);CHKERRQ(ierr);
625ca3d3a14SMatthew G. Knepley       ierr = DMPlexBasisTransformField_Internal(dm, tdm, tv, p, f, l2g, va);CHKERRQ(ierr);
626ca3d3a14SMatthew G. Knepley     }
627ca3d3a14SMatthew G. Knepley   }
628ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(lv, &a);CHKERRQ(ierr);
629ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArrayRead(tv, &ta);CHKERRQ(ierr);
630ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
631ca3d3a14SMatthew G. Knepley }
632ca3d3a14SMatthew G. Knepley 
633ca3d3a14SMatthew G. Knepley /*@
634ca3d3a14SMatthew G. Knepley   DMPlexGlobalToLocalBasis - Transform the values in the given local vector from the global basis to the local basis
635ca3d3a14SMatthew G. Knepley 
636ca3d3a14SMatthew G. Knepley   Input Parameters:
637ca3d3a14SMatthew G. Knepley + dm - The DM
638ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the global basis
639ca3d3a14SMatthew G. Knepley 
640ca3d3a14SMatthew G. Knepley   Output Parameters:
641ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the local basis
642ca3d3a14SMatthew G. Knepley 
643c0f8e1fdSMatthew G. Knepley   Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user will have a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal.
644c0f8e1fdSMatthew G. Knepley 
645ca3d3a14SMatthew G. Knepley   Level: developer
646ca3d3a14SMatthew G. Knepley 
647436bc73aSJed Brown .seealso: DMPlexLocalToGlobalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
648ca3d3a14SMatthew G. Knepley @*/
649ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexGlobalToLocalBasis(DM dm, Vec lv)
650ca3d3a14SMatthew G. Knepley {
651ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
652ca3d3a14SMatthew G. Knepley 
653ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
654ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
655ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
656ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_FALSE);CHKERRQ(ierr);
657ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
658ca3d3a14SMatthew G. Knepley }
659ca3d3a14SMatthew G. Knepley 
660ca3d3a14SMatthew G. Knepley /*@
661ca3d3a14SMatthew G. Knepley   DMPlexLocalToGlobalBasis - Transform the values in the given local vector from the local basis to the global basis
662ca3d3a14SMatthew G. Knepley 
663ca3d3a14SMatthew G. Knepley   Input Parameters:
664ca3d3a14SMatthew G. Knepley + dm - The DM
665ca3d3a14SMatthew G. Knepley - lv - A local vector with values in the local basis
666ca3d3a14SMatthew G. Knepley 
667ca3d3a14SMatthew G. Knepley   Output Parameters:
668ca3d3a14SMatthew G. Knepley . lv - A local vector with values in the global basis
669ca3d3a14SMatthew G. Knepley 
670c0f8e1fdSMatthew G. Knepley   Note: This method is only intended to be called inside DMGlobalToLocal(). It is unlikely that a user would want a local vector full of coefficients for the global basis unless they are reimplementing GlobalToLocal.
671c0f8e1fdSMatthew G. Knepley 
672ca3d3a14SMatthew G. Knepley   Level: developer
673ca3d3a14SMatthew G. Knepley 
674436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMGetLocalSection(), DMPlexCreateBasisRotation()
675ca3d3a14SMatthew G. Knepley @*/
676ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexLocalToGlobalBasis(DM dm, Vec lv)
677ca3d3a14SMatthew G. Knepley {
678ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
679ca3d3a14SMatthew G. Knepley 
680ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
681ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
682ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(lv, VEC_CLASSID, 2);
683ca3d3a14SMatthew G. Knepley   ierr = DMPlexBasisTransform_Internal(dm, lv, PETSC_TRUE);CHKERRQ(ierr);
684ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
685ca3d3a14SMatthew G. Knepley }
686ca3d3a14SMatthew G. Knepley 
687ca3d3a14SMatthew G. Knepley /*@
688ca3d3a14SMatthew G. Knepley   DMPlexCreateBasisRotation - Create an internal transformation from the global basis, used to specify boundary conditions
689ca3d3a14SMatthew G. Knepley     and global solutions, to a local basis, appropriate for discretization integrals and assembly.
690ca3d3a14SMatthew G. Knepley 
691ca3d3a14SMatthew G. Knepley   Input Parameters:
692ca3d3a14SMatthew G. Knepley + dm    - The DM
693ca3d3a14SMatthew G. Knepley . alpha - The first Euler angle, and in 2D the only one
694ca3d3a14SMatthew G. Knepley . beta  - The second Euler angle
695f0fc11ceSJed Brown - gamma - The third Euler angle
696ca3d3a14SMatthew G. Knepley 
697ca3d3a14SMatthew G. Knepley   Note: Following https://en.wikipedia.org/wiki/Euler_angles, we will specify Euler angles by extrinsic rotations, meaning that
698ca3d3a14SMatthew 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:
699ca3d3a14SMatthew 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.
700ca3d3a14SMatthew 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.
701ca3d3a14SMatthew G. Knepley   $ The XYZ system rotates a third time about the z axis by gamma.
702ca3d3a14SMatthew G. Knepley 
703ca3d3a14SMatthew G. Knepley   Level: developer
704ca3d3a14SMatthew G. Knepley 
705ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()
706ca3d3a14SMatthew G. Knepley @*/
707ca3d3a14SMatthew G. Knepley PetscErrorCode DMPlexCreateBasisRotation(DM dm, PetscReal alpha, PetscReal beta, PetscReal gamma)
708ca3d3a14SMatthew G. Knepley {
709ca3d3a14SMatthew G. Knepley   RotCtx        *rc;
710ca3d3a14SMatthew G. Knepley   PetscInt       cdim;
711ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
712ca3d3a14SMatthew G. Knepley 
713362febeeSStefano Zampini   PetscFunctionBegin;
714ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
715ca3d3a14SMatthew G. Knepley   ierr = PetscMalloc1(1, &rc);CHKERRQ(ierr);
716ca3d3a14SMatthew G. Knepley   dm->transformCtx       = rc;
717ca3d3a14SMatthew G. Knepley   dm->transformSetUp     = DMPlexBasisTransformSetUp_Rotation_Internal;
718ca3d3a14SMatthew G. Knepley   dm->transformDestroy   = DMPlexBasisTransformDestroy_Rotation_Internal;
719ca3d3a14SMatthew G. Knepley   dm->transformGetMatrix = DMPlexBasisTransformGetMatrix_Rotation_Internal;
720ca3d3a14SMatthew G. Knepley   rc->dim   = cdim;
721ca3d3a14SMatthew G. Knepley   rc->alpha = alpha;
722ca3d3a14SMatthew G. Knepley   rc->beta  = beta;
723ca3d3a14SMatthew G. Knepley   rc->gamma = gamma;
724ca3d3a14SMatthew G. Knepley   ierr = (*dm->transformSetUp)(dm, dm->transformCtx);CHKERRQ(ierr);
725ca3d3a14SMatthew G. Knepley   ierr = DMConstructBasisTransform_Internal(dm);CHKERRQ(ierr);
726ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
727ca3d3a14SMatthew G. Knepley }
728ca3d3a14SMatthew G. Knepley 
729b278463cSMatthew G. Knepley /*@C
730ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssential - Insert boundary values into a local vector using a function of the coordinates
731b278463cSMatthew G. Knepley 
732b278463cSMatthew G. Knepley   Input Parameters:
733b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
734b278463cSMatthew G. Knepley . time   - The time
735b278463cSMatthew G. Knepley . field  - The field to constrain
7361c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7371c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
738b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
739b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
740b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
741b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
742b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
743b278463cSMatthew G. Knepley 
744b278463cSMatthew G. Knepley   Output Parameter:
745b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
746b278463cSMatthew G. Knepley 
747b278463cSMatthew G. Knepley   Level: developer
748b278463cSMatthew G. Knepley 
749ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssentialField(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
750b278463cSMatthew G. Knepley @*/
7511c531cf8SMatthew 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)
75255f2e967SMatthew G. Knepley {
7530163fd50SMatthew G. Knepley   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
75455f2e967SMatthew G. Knepley   void            **ctxs;
755d7ddef95SMatthew G. Knepley   PetscInt          numFields;
756d7ddef95SMatthew G. Knepley   PetscErrorCode    ierr;
757d7ddef95SMatthew G. Knepley 
758d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
759d7ddef95SMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
760d7ddef95SMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
761d7ddef95SMatthew G. Knepley   funcs[field] = func;
762d7ddef95SMatthew G. Knepley   ctxs[field]  = ctx;
7631c531cf8SMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numids, ids, Nc, comps, funcs, ctxs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
764d7ddef95SMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
765d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
766d7ddef95SMatthew G. Knepley }
767d7ddef95SMatthew G. Knepley 
768b278463cSMatthew G. Knepley /*@C
769ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialField - Insert boundary values into a local vector using a function of the coordinates and field data
770b278463cSMatthew G. Knepley 
771b278463cSMatthew G. Knepley   Input Parameters:
772b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
773b278463cSMatthew G. Knepley . time   - The time
774b278463cSMatthew G. Knepley . locU   - A local vector with the input solution values
775b278463cSMatthew G. Knepley . field  - The field to constrain
7761c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
7771c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
778b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
779b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
780b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
781b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
782b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
783b278463cSMatthew G. Knepley 
784b278463cSMatthew G. Knepley   Output Parameter:
785b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
786b278463cSMatthew G. Knepley 
787b278463cSMatthew G. Knepley   Level: developer
788b278463cSMatthew G. Knepley 
789ece3a9fcSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialBdField(), DMAddBoundary()
790b278463cSMatthew G. Knepley @*/
7911c531cf8SMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
792c60e475cSMatthew G. Knepley                                                         void (*func)(PetscInt, PetscInt, PetscInt,
793c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
794c60e475cSMatthew G. Knepley                                                                      const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
79597b6e6e8SMatthew G. Knepley                                                                      PetscReal, const PetscReal[], PetscInt, const PetscScalar[],
796b278463cSMatthew G. Knepley                                                                      PetscScalar[]),
797b278463cSMatthew G. Knepley                                                         void *ctx, Vec locX)
798c60e475cSMatthew G. Knepley {
799c60e475cSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
800c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
801c60e475cSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
80297b6e6e8SMatthew G. Knepley                  PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
803c60e475cSMatthew G. Knepley   void            **ctxs;
804c60e475cSMatthew G. Knepley   PetscInt          numFields;
805c60e475cSMatthew G. Knepley   PetscErrorCode    ierr;
806c60e475cSMatthew G. Knepley 
807c60e475cSMatthew G. Knepley   PetscFunctionBegin;
808c60e475cSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
809c60e475cSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
810c60e475cSMatthew G. Knepley   funcs[field] = func;
811c60e475cSMatthew G. Knepley   ctxs[field]  = ctx;
8121c531cf8SMatthew G. Knepley   ierr = DMProjectFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
813c60e475cSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
814c60e475cSMatthew G. Knepley   PetscFunctionReturn(0);
815c60e475cSMatthew G. Knepley }
816c60e475cSMatthew G. Knepley 
817b278463cSMatthew G. Knepley /*@C
818ece3a9fcSMatthew G. Knepley   DMPlexInsertBoundaryValuesEssentialBdField - Insert boundary values into a local vector using a function of the coodinates and boundary field data
819ece3a9fcSMatthew G. Knepley 
820ece3a9fcSMatthew G. Knepley   Collective on dm
821ece3a9fcSMatthew G. Knepley 
822ece3a9fcSMatthew G. Knepley   Input Parameters:
823ece3a9fcSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
824ece3a9fcSMatthew G. Knepley . time   - The time
825ece3a9fcSMatthew G. Knepley . locU   - A local vector with the input solution values
826ece3a9fcSMatthew G. Knepley . field  - The field to constrain
827ece3a9fcSMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
828ece3a9fcSMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
829ece3a9fcSMatthew G. Knepley . label  - The DMLabel defining constrained points
830ece3a9fcSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
831ece3a9fcSMatthew G. Knepley . ids    - An array of ids for constrained points
832ece3a9fcSMatthew G. Knepley . func   - A pointwise function giving boundary values, the calling sequence is given in DMProjectBdFieldLabelLocal()
833ece3a9fcSMatthew G. Knepley - ctx    - An optional user context for bcFunc
834ece3a9fcSMatthew G. Knepley 
835ece3a9fcSMatthew G. Knepley   Output Parameter:
836ece3a9fcSMatthew G. Knepley . locX   - A local vector to receive the boundary values
837ece3a9fcSMatthew G. Knepley 
838ece3a9fcSMatthew G. Knepley   Level: developer
839ece3a9fcSMatthew G. Knepley 
840ece3a9fcSMatthew G. Knepley .seealso: DMProjectBdFieldLabelLocal(), DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
841ece3a9fcSMatthew G. Knepley @*/
842ece3a9fcSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValuesEssentialBdField(DM dm, PetscReal time, Vec locU, PetscInt field, PetscInt Nc, const PetscInt comps[], DMLabel label, PetscInt numids, const PetscInt ids[],
843ece3a9fcSMatthew G. Knepley                                                           void (*func)(PetscInt, PetscInt, PetscInt,
844ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
845ece3a9fcSMatthew G. Knepley                                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
846ece3a9fcSMatthew G. Knepley                                                                        PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[],
847ece3a9fcSMatthew G. Knepley                                                                        PetscScalar[]),
848ece3a9fcSMatthew G. Knepley                                                           void *ctx, Vec locX)
849ece3a9fcSMatthew G. Knepley {
850ece3a9fcSMatthew G. Knepley   void (**funcs)(PetscInt, PetscInt, PetscInt,
851ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
852ece3a9fcSMatthew G. Knepley                  const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
853ece3a9fcSMatthew G. Knepley                  PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
854ece3a9fcSMatthew G. Knepley   void            **ctxs;
855ece3a9fcSMatthew G. Knepley   PetscInt          numFields;
856ece3a9fcSMatthew G. Knepley   PetscErrorCode    ierr;
857ece3a9fcSMatthew G. Knepley 
858ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
859ece3a9fcSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
860ece3a9fcSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&funcs,numFields,&ctxs);CHKERRQ(ierr);
861ece3a9fcSMatthew G. Knepley   funcs[field] = func;
862ece3a9fcSMatthew G. Knepley   ctxs[field]  = ctx;
863ece3a9fcSMatthew G. Knepley   ierr = DMProjectBdFieldLabelLocal(dm, time, label, numids, ids, Nc, comps, locU, funcs, INSERT_BC_VALUES, locX);CHKERRQ(ierr);
864ece3a9fcSMatthew G. Knepley   ierr = PetscFree2(funcs,ctxs);CHKERRQ(ierr);
865ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
866ece3a9fcSMatthew G. Knepley }
867ece3a9fcSMatthew G. Knepley 
868ece3a9fcSMatthew G. Knepley /*@C
869b278463cSMatthew G. Knepley   DMPlexInsertBoundaryValuesRiemann - Insert boundary values into a local vector
870b278463cSMatthew G. Knepley 
871b278463cSMatthew G. Knepley   Input Parameters:
872b278463cSMatthew G. Knepley + dm     - The DM, with a PetscDS that matches the problem being constrained
873b278463cSMatthew G. Knepley . time   - The time
874b278463cSMatthew G. Knepley . faceGeometry - A vector with the FVM face geometry information
875b278463cSMatthew G. Knepley . cellGeometry - A vector with the FVM cell geometry information
876b278463cSMatthew G. Knepley . Grad         - A vector with the FVM cell gradient information
877b278463cSMatthew G. Knepley . field  - The field to constrain
8781c531cf8SMatthew G. Knepley . Nc     - The number of constrained field components, or 0 for all components
8791c531cf8SMatthew G. Knepley . comps  - An array of constrained component numbers, or NULL for all components
880b278463cSMatthew G. Knepley . label  - The DMLabel defining constrained points
881b278463cSMatthew G. Knepley . numids - The number of DMLabel ids for constrained points
882b278463cSMatthew G. Knepley . ids    - An array of ids for constrained points
883b278463cSMatthew G. Knepley . func   - A pointwise function giving boundary values
884b278463cSMatthew G. Knepley - ctx    - An optional user context for bcFunc
885b278463cSMatthew G. Knepley 
886b278463cSMatthew G. Knepley   Output Parameter:
887b278463cSMatthew G. Knepley . locX   - A local vector to receives the boundary values
888b278463cSMatthew G. Knepley 
889b278463cSMatthew G. Knepley   Note: This implementation currently ignores the numcomps/comps argument from DMAddBoundary()
890b278463cSMatthew G. Knepley 
891b278463cSMatthew G. Knepley   Level: developer
892b278463cSMatthew G. Knepley 
893b278463cSMatthew G. Knepley .seealso: DMPlexInsertBoundaryValuesEssential(), DMPlexInsertBoundaryValuesEssentialField(), DMAddBoundary()
894b278463cSMatthew G. Knepley @*/
8951c531cf8SMatthew 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[],
896b278463cSMatthew G. Knepley                                                  PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*), void *ctx, Vec locX)
897d7ddef95SMatthew G. Knepley {
89861f58d28SMatthew G. Knepley   PetscDS            prob;
899da97024aSMatthew G. Knepley   PetscSF            sf;
900d7ddef95SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad;
90120369375SToby Isaac   const PetscScalar *facegeom, *cellgeom = NULL, *grad;
902da97024aSMatthew G. Knepley   const PetscInt    *leaves;
903d7ddef95SMatthew G. Knepley   PetscScalar       *x, *fx;
904520b3818SMatthew G. Knepley   PetscInt           dim, nleaves, loc, fStart, fEnd, pdim, i;
905e735a8a9SMatthew G. Knepley   PetscErrorCode     ierr,ierru = 0;
906d7ddef95SMatthew G. Knepley 
907d7ddef95SMatthew G. Knepley   PetscFunctionBegin;
908da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
909da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
910da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
911d7ddef95SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
912d7ddef95SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
91361f58d28SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
914d7ddef95SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
915d7ddef95SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
91620369375SToby Isaac   if (cellGeometry) {
917d7ddef95SMatthew G. Knepley     ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
918d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
91920369375SToby Isaac   }
920d7ddef95SMatthew G. Knepley   if (Grad) {
921c0a6632aSMatthew G. Knepley     PetscFV fv;
922c0a6632aSMatthew G. Knepley 
923c0a6632aSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fv);CHKERRQ(ierr);
924d7ddef95SMatthew G. Knepley     ierr = VecGetDM(Grad, &dmGrad);CHKERRQ(ierr);
925d7ddef95SMatthew G. Knepley     ierr = VecGetArrayRead(Grad, &grad);CHKERRQ(ierr);
926d7ddef95SMatthew G. Knepley     ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
92769291d52SBarry Smith     ierr = DMGetWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
928d7ddef95SMatthew G. Knepley   }
929d7ddef95SMatthew G. Knepley   ierr = VecGetArray(locX, &x);CHKERRQ(ierr);
930d7ddef95SMatthew G. Knepley   for (i = 0; i < numids; ++i) {
931d7ddef95SMatthew G. Knepley     IS              faceIS;
932d7ddef95SMatthew G. Knepley     const PetscInt *faces;
933d7ddef95SMatthew G. Knepley     PetscInt        numFaces, f;
934d7ddef95SMatthew G. Knepley 
935d7ddef95SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, ids[i], &faceIS);CHKERRQ(ierr);
936d7ddef95SMatthew G. Knepley     if (!faceIS) continue; /* No points with that id on this process */
937d7ddef95SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
938d7ddef95SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
939d7ddef95SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
940d7ddef95SMatthew G. Knepley       const PetscInt         face = faces[f], *cells;
941640bce14SSatish Balay       PetscFVFaceGeom        *fg;
942d7ddef95SMatthew G. Knepley 
943d7ddef95SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue; /* Refinement adds non-faces to labels */
944da97024aSMatthew G. Knepley       ierr = PetscFindInt(face, nleaves, (PetscInt *) leaves, &loc);CHKERRQ(ierr);
945da97024aSMatthew G. Knepley       if (loc >= 0) continue;
946d7ddef95SMatthew G. Knepley       ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
947d7ddef95SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
948d7ddef95SMatthew G. Knepley       if (Grad) {
949640bce14SSatish Balay         PetscFVCellGeom       *cg;
950640bce14SSatish Balay         PetscScalar           *cx, *cgrad;
951d7ddef95SMatthew G. Knepley         PetscScalar           *xG;
952d7ddef95SMatthew G. Knepley         PetscReal              dx[3];
953d7ddef95SMatthew G. Knepley         PetscInt               d;
954d7ddef95SMatthew G. Knepley 
955d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cg);CHKERRQ(ierr);
956d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &cx);CHKERRQ(ierr);
957d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dmGrad, cells[0], grad, &cgrad);CHKERRQ(ierr);
958520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
959d7ddef95SMatthew G. Knepley         DMPlex_WaxpyD_Internal(dim, -1, cg->centroid, fg->centroid, dx);
960d7ddef95SMatthew G. Knepley         for (d = 0; d < pdim; ++d) fx[d] = cx[d] + DMPlex_DotD_Internal(dim, &cgrad[d*dim], dx);
961c3e24edfSBarry Smith         ierr = (*func)(time, fg->centroid, fg->normal, fx, xG, ctx);CHKERRQ(ierr);
962d7ddef95SMatthew G. Knepley       } else {
963640bce14SSatish Balay         PetscScalar       *xI;
964d7ddef95SMatthew G. Knepley         PetscScalar       *xG;
965d7ddef95SMatthew G. Knepley 
966d7ddef95SMatthew G. Knepley         ierr = DMPlexPointLocalRead(dm, cells[0], x, &xI);CHKERRQ(ierr);
967520b3818SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm, cells[1], field, x, &xG);CHKERRQ(ierr);
968e735a8a9SMatthew G. Knepley         ierru = (*func)(time, fg->centroid, fg->normal, xI, xG, ctx);
969e735a8a9SMatthew G. Knepley         if (ierru) {
970e735a8a9SMatthew G. Knepley           ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
971e735a8a9SMatthew G. Knepley           ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
972e735a8a9SMatthew G. Knepley           goto cleanup;
973e735a8a9SMatthew G. Knepley         }
974d7ddef95SMatthew G. Knepley       }
975d7ddef95SMatthew G. Knepley     }
976d7ddef95SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
977d7ddef95SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
978d7ddef95SMatthew G. Knepley   }
979e735a8a9SMatthew G. Knepley   cleanup:
980d7ddef95SMatthew G. Knepley   ierr = VecRestoreArray(locX, &x);CHKERRQ(ierr);
981d7ddef95SMatthew G. Knepley   if (Grad) {
98269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, pdim, MPIU_SCALAR, &fx);CHKERRQ(ierr);
983d7ddef95SMatthew G. Knepley     ierr = VecRestoreArrayRead(Grad, &grad);CHKERRQ(ierr);
984d7ddef95SMatthew G. Knepley   }
985e735a8a9SMatthew G. Knepley   if (cellGeometry) {ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);}
986d7ddef95SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
987e735a8a9SMatthew G. Knepley   CHKERRQ(ierru);
988d7ddef95SMatthew G. Knepley   PetscFunctionReturn(0);
989d7ddef95SMatthew G. Knepley }
990d7ddef95SMatthew G. Knepley 
9910c364540SMatthew G. Knepley static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
9920c364540SMatthew G. Knepley {
9930c364540SMatthew G. Knepley   PetscInt c;
9940c364540SMatthew G. Knepley   for (c = 0; c < Nc; ++c) u[c] = 0.0;
9950c364540SMatthew G. Knepley   return 0;
9960c364540SMatthew G. Knepley }
9970c364540SMatthew G. Knepley 
998f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
999d7ddef95SMatthew G. Knepley {
10000c364540SMatthew G. Knepley   PetscObject    isZero;
1001e5e52638SMatthew G. Knepley   PetscDS        prob;
1002d7ddef95SMatthew G. Knepley   PetscInt       numBd, b;
100355f2e967SMatthew G. Knepley   PetscErrorCode ierr;
100455f2e967SMatthew G. Knepley 
100555f2e967SMatthew G. Knepley   PetscFunctionBegin;
1006e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
1007e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
10080c364540SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
100955f2e967SMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
101045480ffeSMatthew G. Knepley     PetscWeakForm           wf;
1011f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
101245480ffeSMatthew G. Knepley     const char             *name;
1013d7ddef95SMatthew G. Knepley     DMLabel                 label;
10141c531cf8SMatthew G. Knepley     PetscInt                field, Nc;
10151c531cf8SMatthew G. Knepley     const PetscInt         *comps;
1016d7ddef95SMatthew G. Knepley     PetscObject             obj;
1017d7ddef95SMatthew G. Knepley     PetscClassId            id;
101845480ffeSMatthew G. Knepley     void                  (*bvfunc)(void);
1019d7ddef95SMatthew G. Knepley     PetscInt                numids;
1020d7ddef95SMatthew G. Knepley     const PetscInt         *ids;
102155f2e967SMatthew G. Knepley     void                   *ctx;
102255f2e967SMatthew G. Knepley 
102345480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, &bvfunc, NULL, &ctx);CHKERRQ(ierr);
1024f971fd6bSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
102544a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1026d7ddef95SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1027d7ddef95SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1028c60e475cSMatthew G. Knepley       switch (type) {
1029c60e475cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
1030c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
103145480ffeSMatthew G. Knepley         {
103245480ffeSMatthew G. Knepley           PetscSimplePointFunc func = (PetscSimplePointFunc) bvfunc;
103345480ffeSMatthew G. Knepley 
103445480ffeSMatthew G. Knepley           if (isZero) func = zero;
1035092e5057SToby Isaac           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
103645480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
1037092e5057SToby Isaac           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
103845480ffeSMatthew G. Knepley         }
1039c60e475cSMatthew G. Knepley         break;
1040c60e475cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
104145480ffeSMatthew G. Knepley         {
104245480ffeSMatthew G. Knepley           PetscPointFunc func = (PetscPointFunc) bvfunc;
104345480ffeSMatthew G. Knepley 
1044c60e475cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
104545480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
1046c60e475cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
104745480ffeSMatthew G. Knepley         }
1048c60e475cSMatthew G. Knepley         break;
1049c60e475cSMatthew G. Knepley       default: break;
1050c60e475cSMatthew G. Knepley       }
1051d7ddef95SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
105245480ffeSMatthew G. Knepley       {
105345480ffeSMatthew G. Knepley         PetscErrorCode (*func)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*) = (PetscErrorCode (*)(PetscReal,const PetscReal*,const PetscReal*,const PetscScalar*,PetscScalar*,void*)) bvfunc;
105445480ffeSMatthew G. Knepley 
105543ea7facSMatthew G. Knepley         if (!faceGeomFVM) continue;
105645480ffeSMatthew G. Knepley         ierr = DMPlexInsertBoundaryValuesRiemann(dm, time, faceGeomFVM, cellGeomFVM, gradFVM, field, Nc, comps, label, numids, ids, func, ctx, locX);CHKERRQ(ierr);
105745480ffeSMatthew G. Knepley       }
105898921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
105955f2e967SMatthew G. Knepley   }
106055f2e967SMatthew G. Knepley   PetscFunctionReturn(0);
106155f2e967SMatthew G. Knepley }
106255f2e967SMatthew G. Knepley 
106356cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues_Plex(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
106456cf3b9cSMatthew G. Knepley {
106556cf3b9cSMatthew G. Knepley   PetscObject    isZero;
106656cf3b9cSMatthew G. Knepley   PetscDS        prob;
106756cf3b9cSMatthew G. Knepley   PetscInt       numBd, b;
106856cf3b9cSMatthew G. Knepley   PetscErrorCode ierr;
106956cf3b9cSMatthew G. Knepley 
107056cf3b9cSMatthew G. Knepley   PetscFunctionBegin;
107156cf3b9cSMatthew G. Knepley   if (!locX) PetscFunctionReturn(0);
107256cf3b9cSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
107356cf3b9cSMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
107456cf3b9cSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) locX, "__Vec_bc_zero__", &isZero);CHKERRQ(ierr);
107556cf3b9cSMatthew G. Knepley   for (b = 0; b < numBd; ++b) {
107645480ffeSMatthew G. Knepley     PetscWeakForm           wf;
107756cf3b9cSMatthew G. Knepley     DMBoundaryConditionType type;
107845480ffeSMatthew G. Knepley     const char             *name;
107956cf3b9cSMatthew G. Knepley     DMLabel                 label;
108056cf3b9cSMatthew G. Knepley     PetscInt                field, Nc;
108156cf3b9cSMatthew G. Knepley     const PetscInt         *comps;
108256cf3b9cSMatthew G. Knepley     PetscObject             obj;
108356cf3b9cSMatthew G. Knepley     PetscClassId            id;
108456cf3b9cSMatthew G. Knepley     PetscInt                numids;
108556cf3b9cSMatthew G. Knepley     const PetscInt         *ids;
108645480ffeSMatthew G. Knepley     void                  (*bvfunc)(void);
108756cf3b9cSMatthew G. Knepley     void                   *ctx;
108856cf3b9cSMatthew G. Knepley 
108945480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, b, &wf, &type, &name, &label, &numids, &ids, &field, &Nc, &comps, NULL, &bvfunc, &ctx);CHKERRQ(ierr);
109056cf3b9cSMatthew G. Knepley     if (insertEssential != (type & DM_BC_ESSENTIAL)) continue;
109156cf3b9cSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
109256cf3b9cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
109356cf3b9cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
109456cf3b9cSMatthew G. Knepley       switch (type) {
109556cf3b9cSMatthew G. Knepley         /* for FEM, there is no insertion to be done for non-essential boundary conditions */
109656cf3b9cSMatthew G. Knepley       case DM_BC_ESSENTIAL:
109745480ffeSMatthew G. Knepley         {
109845480ffeSMatthew G. Knepley           PetscSimplePointFunc func_t = (PetscSimplePointFunc) bvfunc;
109945480ffeSMatthew G. Knepley 
110045480ffeSMatthew G. Knepley           if (isZero) func_t = zero;
110156cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
110245480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssential(dm, time, field, Nc, comps, label, numids, ids, func_t, ctx, locX);CHKERRQ(ierr);
110356cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
110445480ffeSMatthew G. Knepley         }
110556cf3b9cSMatthew G. Knepley         break;
110656cf3b9cSMatthew G. Knepley       case DM_BC_ESSENTIAL_FIELD:
110745480ffeSMatthew G. Knepley         {
110845480ffeSMatthew G. Knepley           PetscPointFunc func_t = (PetscPointFunc) bvfunc;
110945480ffeSMatthew G. Knepley 
111056cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelAddCells(dm,label);CHKERRQ(ierr);
111145480ffeSMatthew G. Knepley           ierr = DMPlexInsertBoundaryValuesEssentialField(dm, time, locX, field, Nc, comps, label, numids, ids, func_t, ctx, locX);CHKERRQ(ierr);
111256cf3b9cSMatthew G. Knepley           ierr = DMPlexLabelClearCells(dm,label);CHKERRQ(ierr);
111345480ffeSMatthew G. Knepley         }
111456cf3b9cSMatthew G. Knepley         break;
111556cf3b9cSMatthew G. Knepley       default: break;
111656cf3b9cSMatthew G. Knepley       }
111798921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
111856cf3b9cSMatthew G. Knepley   }
111956cf3b9cSMatthew G. Knepley   PetscFunctionReturn(0);
112056cf3b9cSMatthew G. Knepley }
112156cf3b9cSMatthew G. Knepley 
1122f1d73a7aSMatthew G. Knepley /*@
1123f1d73a7aSMatthew G. Knepley   DMPlexInsertBoundaryValues - Puts coefficients which represent boundary values into the local solution vector
1124f1d73a7aSMatthew G. Knepley 
1125ed808b8fSJed Brown   Not Collective
1126ed808b8fSJed Brown 
1127f1d73a7aSMatthew G. Knepley   Input Parameters:
1128f1d73a7aSMatthew G. Knepley + dm - The DM
1129f1d73a7aSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
1130f1d73a7aSMatthew G. Knepley . time - The time
1131f1d73a7aSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
1132f1d73a7aSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
1133f1d73a7aSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
1134f1d73a7aSMatthew G. Knepley 
1135f1d73a7aSMatthew G. Knepley   Output Parameters:
1136f1d73a7aSMatthew G. Knepley . locX - Solution updated with boundary values
1137f1d73a7aSMatthew G. Knepley 
1138ed808b8fSJed Brown   Level: intermediate
1139f1d73a7aSMatthew G. Knepley 
1140ed808b8fSJed Brown .seealso: DMProjectFunctionLabelLocal(), DMAddBoundary()
1141f1d73a7aSMatthew G. Knepley @*/
1142f1d73a7aSMatthew G. Knepley PetscErrorCode DMPlexInsertBoundaryValues(DM dm, PetscBool insertEssential, Vec locX, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
1143f1d73a7aSMatthew G. Knepley {
1144f1d73a7aSMatthew G. Knepley   PetscErrorCode ierr;
1145f1d73a7aSMatthew G. Knepley 
1146f1d73a7aSMatthew G. Knepley   PetscFunctionBegin;
1147f1d73a7aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1148064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(locX, VEC_CLASSID, 3);
1149064a246eSJacob Faibussowitsch   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 5);}
1150064a246eSJacob Faibussowitsch   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 6);}
1151064a246eSJacob Faibussowitsch   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 7);}
1152f1d73a7aSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
1153f1d73a7aSMatthew G. Knepley   PetscFunctionReturn(0);
1154f1d73a7aSMatthew G. Knepley }
1155f1d73a7aSMatthew G. Knepley 
115656cf3b9cSMatthew G. Knepley /*@
1157a5b23f4aSJose E. Roman   DMPlexInsertTimeDerivativeBoundaryValues - Puts coefficients which represent boundary values of the time derivative into the local solution vector
115856cf3b9cSMatthew G. Knepley 
115956cf3b9cSMatthew G. Knepley   Input Parameters:
116056cf3b9cSMatthew G. Knepley + dm - The DM
116156cf3b9cSMatthew G. Knepley . insertEssential - Should I insert essential (e.g. Dirichlet) or inessential (e.g. Neumann) boundary conditions
116256cf3b9cSMatthew G. Knepley . time - The time
116356cf3b9cSMatthew G. Knepley . faceGeomFVM - Face geometry data for FV discretizations
116456cf3b9cSMatthew G. Knepley . cellGeomFVM - Cell geometry data for FV discretizations
116556cf3b9cSMatthew G. Knepley - gradFVM - Gradient reconstruction data for FV discretizations
116656cf3b9cSMatthew G. Knepley 
116756cf3b9cSMatthew G. Knepley   Output Parameters:
116856cf3b9cSMatthew G. Knepley . locX_t - Solution updated with boundary values
116956cf3b9cSMatthew G. Knepley 
117056cf3b9cSMatthew G. Knepley   Level: developer
117156cf3b9cSMatthew G. Knepley 
117256cf3b9cSMatthew G. Knepley .seealso: DMProjectFunctionLabelLocal()
117356cf3b9cSMatthew G. Knepley @*/
117456cf3b9cSMatthew G. Knepley PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues(DM dm, PetscBool insertEssential, Vec locX_t, PetscReal time, Vec faceGeomFVM, Vec cellGeomFVM, Vec gradFVM)
117556cf3b9cSMatthew G. Knepley {
117656cf3b9cSMatthew G. Knepley   PetscErrorCode ierr;
117756cf3b9cSMatthew G. Knepley 
117856cf3b9cSMatthew G. Knepley   PetscFunctionBegin;
117956cf3b9cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1180064a246eSJacob Faibussowitsch   if (locX_t)      {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 3);}
1181064a246eSJacob Faibussowitsch   if (faceGeomFVM) {PetscValidHeaderSpecific(faceGeomFVM, VEC_CLASSID, 5);}
1182064a246eSJacob Faibussowitsch   if (cellGeomFVM) {PetscValidHeaderSpecific(cellGeomFVM, VEC_CLASSID, 6);}
1183064a246eSJacob Faibussowitsch   if (gradFVM)     {PetscValidHeaderSpecific(gradFVM, VEC_CLASSID, 7);}
118456cf3b9cSMatthew G. Knepley   ierr = PetscTryMethod(dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",(DM,PetscBool,Vec,PetscReal,Vec,Vec,Vec),(dm,insertEssential,locX_t,time,faceGeomFVM,cellGeomFVM,gradFVM));CHKERRQ(ierr);
118556cf3b9cSMatthew G. Knepley   PetscFunctionReturn(0);
118656cf3b9cSMatthew G. Knepley }
118756cf3b9cSMatthew G. Knepley 
11880709b2feSToby Isaac PetscErrorCode DMComputeL2Diff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
1189cb1e1211SMatthew G Knepley {
1190574a98acSMatthew G. Knepley   Vec              localX;
1191574a98acSMatthew G. Knepley   PetscErrorCode   ierr;
1192574a98acSMatthew G. Knepley 
1193574a98acSMatthew G. Knepley   PetscFunctionBegin;
1194574a98acSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
11955d42b983SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, localX, time, NULL, NULL, NULL);CHKERRQ(ierr);
1196574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1197574a98acSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1198574a98acSMatthew G. Knepley   ierr = DMPlexComputeL2DiffLocal(dm, time, funcs, ctxs, localX, diff);CHKERRQ(ierr);
1199574a98acSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1200574a98acSMatthew G. Knepley   PetscFunctionReturn(0);
1201574a98acSMatthew G. Knepley }
1202574a98acSMatthew G. Knepley 
1203574a98acSMatthew G. Knepley /*@C
1204ca3d3a14SMatthew G. Knepley   DMComputeL2DiffLocal - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
1205574a98acSMatthew G. Knepley 
1206d083f849SBarry Smith   Collective on dm
1207c0f8e1fdSMatthew G. Knepley 
1208574a98acSMatthew G. Knepley   Input Parameters:
1209574a98acSMatthew G. Knepley + dm     - The DM
1210574a98acSMatthew G. Knepley . time   - The time
1211574a98acSMatthew G. Knepley . funcs  - The functions to evaluate for each field component
1212574a98acSMatthew G. Knepley . ctxs   - Optional array of contexts to pass to each function, or NULL.
1213574a98acSMatthew G. Knepley - localX - The coefficient vector u_h, a local vector
1214574a98acSMatthew G. Knepley 
1215574a98acSMatthew G. Knepley   Output Parameter:
1216574a98acSMatthew G. Knepley . diff - The diff ||u - u_h||_2
1217574a98acSMatthew G. Knepley 
1218574a98acSMatthew G. Knepley   Level: developer
1219574a98acSMatthew G. Knepley 
1220574a98acSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
1221574a98acSMatthew G. Knepley @*/
1222574a98acSMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec localX, PetscReal *diff)
1223574a98acSMatthew G. Knepley {
12240f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1225ca3d3a14SMatthew G. Knepley   DM               tdm;
1226ca3d3a14SMatthew G. Knepley   Vec              tv;
1227cb1e1211SMatthew G Knepley   PetscSection     section;
1228c5bbbd5bSMatthew G. Knepley   PetscQuadrature  quad;
12294bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
123015496722SMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
12314bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
1232cb1e1211SMatthew G Knepley   PetscReal        localDiff = 0.0;
12337318780aSToby Isaac   const PetscReal *quadWeights;
1234412e9a14SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cellHeight, cStart, cEnd, c, field, fieldOffset;
1235ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1236cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1237cb1e1211SMatthew G Knepley 
1238cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1239c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
12407318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
12412a4e142eSMatthew G. Knepley   fegeom.dimEmbed = coordDim;
124292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1243cb1e1211SMatthew G Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1244ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1245ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1246ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1247cb1e1211SMatthew G Knepley   for (field = 0; field < numFields; ++field) {
124815496722SMatthew G. Knepley     PetscObject  obj;
124915496722SMatthew G. Knepley     PetscClassId id;
1250c5bbbd5bSMatthew G. Knepley     PetscInt     Nc;
1251c5bbbd5bSMatthew G. Knepley 
125244a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
125315496722SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
125415496722SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
125515496722SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
125615496722SMatthew G. Knepley 
12570f2d7e86SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
12580f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
125915496722SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
126015496722SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
126115496722SMatthew G. Knepley 
126215496722SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
126315496722SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
126498921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1265c5bbbd5bSMatthew G. Knepley     numComponents += Nc;
1266cb1e1211SMatthew G Knepley   }
12679c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
12682c71b3e2SJacob Faibussowitsch   PetscCheckFalse((qNc != 1) && (qNc != numComponents),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
12692a4e142eSMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
1270aed3cbd0SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
1271412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
1272cb1e1211SMatthew G Knepley   for (c = cStart; c < cEnd; ++c) {
1273a1e44745SMatthew G. Knepley     PetscScalar *x = NULL;
1274cb1e1211SMatthew G Knepley     PetscReal    elemDiff = 0.0;
12759c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1276cb1e1211SMatthew G Knepley 
12772a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1278cb1e1211SMatthew G Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1279cb1e1211SMatthew G Knepley 
128015496722SMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
128115496722SMatthew G. Knepley       PetscObject  obj;
128215496722SMatthew G. Knepley       PetscClassId id;
1283c110b1eeSGeoffrey Irving       void * const ctx = ctxs ? ctxs[field] : NULL;
128415496722SMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1285cb1e1211SMatthew G Knepley 
128644a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
128715496722SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
128815496722SMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
128915496722SMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
129098921bdaSJacob Faibussowitsch       else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1291cb1e1211SMatthew G Knepley       if (debug) {
1292cb1e1211SMatthew G Knepley         char title[1024];
1293ff1e0c32SBarry Smith         ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", field);CHKERRQ(ierr);
12944c848028SMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1295cb1e1211SMatthew G Knepley       }
12967318780aSToby Isaac       for (q = 0; q < Nq; ++q) {
12972a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
12982a4e142eSMatthew G. Knepley 
12992a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
13002a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
13012a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
13022a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
13032c71b3e2SJacob Faibussowitsch         PetscCheckFalse(fegeom.detJ[q] <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, point %D", (double)fegeom.detJ[q], c, q);
1304d3a7d86cSMatthew G. Knepley         if (transform) {
1305d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1306d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1307d3a7d86cSMatthew G. Knepley         } else {
1308d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1309d3a7d86cSMatthew G. Knepley         }
13102df84da0SMatthew G. Knepley         ierr = PetscArrayzero(funcVal,Nc);CHKERRQ(ierr);
1311ca3d3a14SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, Nc, funcVal, ctx);
1312e735a8a9SMatthew G. Knepley         if (ierr) {
1313e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1314e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1315e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
13162a4e142eSMatthew G. Knepley           ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1317e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1318e735a8a9SMatthew G. Knepley         }
1319ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
13202a4e142eSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
132115496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
13222df84da0SMatthew G. Knepley         else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %" PetscInt_FMT, field);
132315496722SMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1324beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
13252df84da0SMatthew G. Knepley           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %" PetscInt_FMT " field %" PetscInt_FMT ",%" PetscInt_FMT " point %g %g %g diff %g (%g, %g)\n", c, field, fc, (double)(coordDim > 0 ? coords[coordDim*q] : 0.), (double)(coordDim > 1 ? coords[coordDim*q+1] : 0.),(double)(coordDim > 2 ? coords[coordDim*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]), PetscRealPart(interpolant[fc]), PetscRealPart(funcVal[fc]));CHKERRQ(ierr);}
13264bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1327cb1e1211SMatthew G Knepley         }
1328cb1e1211SMatthew G Knepley       }
13299c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
1330beaa55a6SMatthew G. Knepley       qc += Nc;
1331cb1e1211SMatthew G Knepley     }
1332cb1e1211SMatthew G Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
13332df84da0SMatthew G. Knepley     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %" PetscInt_FMT " diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
1334cb1e1211SMatthew G Knepley     localDiff += elemDiff;
1335cb1e1211SMatthew G Knepley   }
13362a4e142eSMatthew G. Knepley   ierr  = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1337820f2d46SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1338cb1e1211SMatthew G Knepley   *diff = PetscSqrtReal(*diff);
1339cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1340cb1e1211SMatthew G Knepley }
1341cb1e1211SMatthew G Knepley 
1342b698f381SToby 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)
1343cb1e1211SMatthew G Knepley {
13440f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1345ca3d3a14SMatthew G. Knepley   DM               tdm;
1346cb1e1211SMatthew G Knepley   PetscSection     section;
134740e14135SMatthew G. Knepley   PetscQuadrature  quad;
1348ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
13499c3cf19fSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
13502a4e142eSMatthew G. Knepley   const PetscReal *quadWeights;
13514bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
13524bee2e38SMatthew G. Knepley   PetscReal       *coords, *gcoords;
135340e14135SMatthew G. Knepley   PetscReal        localDiff = 0.0;
1354485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, qNc = 0, Nq = 0, numFields, numComponents = 0, cStart, cEnd, c, field, fieldOffset;
1355ca3d3a14SMatthew G. Knepley   PetscBool        transform;
1356cb1e1211SMatthew G Knepley   PetscErrorCode   ierr;
1357cb1e1211SMatthew G Knepley 
1358cb1e1211SMatthew G Knepley   PetscFunctionBegin;
1359c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
13607318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
13614bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
136292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
136340e14135SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
136440e14135SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
136540e14135SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
136640e14135SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1367ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1368ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1369ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1370652b88e8SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
13710f2d7e86SMatthew G. Knepley     PetscFE  fe;
137240e14135SMatthew G. Knepley     PetscInt Nc;
1373652b88e8SMatthew G. Knepley 
137444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
13750f2d7e86SMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
13760f2d7e86SMatthew G. Knepley     ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
137740e14135SMatthew G. Knepley     numComponents += Nc;
1378652b88e8SMatthew G. Knepley   }
13792a4e142eSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights);CHKERRQ(ierr);
13802c71b3e2SJacob Faibussowitsch   PetscCheckFalse((qNc != 1) && (qNc != numComponents),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
13814d6f44ffSToby Isaac   /* ierr = DMProjectFunctionLocal(dm, fe, funcs, INSERT_BC_VALUES, localX);CHKERRQ(ierr); */
13824bee2e38SMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,coordDim*Nq,&coords,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ,numComponents*coordDim,&interpolant,Nq,&fegeom.detJ);CHKERRQ(ierr);
1383412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
138440e14135SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
138540e14135SMatthew G. Knepley     PetscScalar *x = NULL;
138640e14135SMatthew G. Knepley     PetscReal    elemDiff = 0.0;
13879c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1388652b88e8SMatthew G. Knepley 
13894bee2e38SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
139040e14135SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
139140e14135SMatthew G. Knepley 
13929c3cf19fSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
13930f2d7e86SMatthew G. Knepley       PetscFE          fe;
139451259fa3SMatthew G. Knepley       void * const     ctx = ctxs ? ctxs[field] : NULL;
13959c3cf19fSMatthew G. Knepley       PetscInt         Nb, Nc, q, fc;
139640e14135SMatthew G. Knepley 
139744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, (PetscObject *) &fe);CHKERRQ(ierr);
13980f2d7e86SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
13999c3cf19fSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
140040e14135SMatthew G. Knepley       if (debug) {
140140e14135SMatthew G. Knepley         char title[1024];
14022df84da0SMatthew G. Knepley         ierr = PetscSNPrintf(title, 1023, "Solution for Field %" PetscInt_FMT, field);CHKERRQ(ierr);
14039c3cf19fSMatthew G. Knepley         ierr = DMPrintCellVector(c, title, Nb, &x[fieldOffset]);CHKERRQ(ierr);
1404652b88e8SMatthew G. Knepley       }
14059c3cf19fSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
14062a4e142eSMatthew G. Knepley         PetscFEGeom qgeom;
14072a4e142eSMatthew G. Knepley 
14082a4e142eSMatthew G. Knepley         qgeom.dimEmbed = fegeom.dimEmbed;
14092a4e142eSMatthew G. Knepley         qgeom.J        = &fegeom.J[q*coordDim*coordDim];
14102a4e142eSMatthew G. Knepley         qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
14112a4e142eSMatthew G. Knepley         qgeom.detJ     = &fegeom.detJ[q];
14122df84da0SMatthew G. Knepley         PetscCheck(fegeom.detJ[q] > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %" PetscInt_FMT ", quadrature points %" PetscInt_FMT, (double)fegeom.detJ[q], c, q);
1413d3a7d86cSMatthew G. Knepley         if (transform) {
1414d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*Nq];
1415d3a7d86cSMatthew G. Knepley           ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[coordDim*q], PETSC_TRUE, coordDim, &coords[coordDim*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1416d3a7d86cSMatthew G. Knepley         } else {
1417d3a7d86cSMatthew G. Knepley           gcoords = &coords[coordDim*q];
1418d3a7d86cSMatthew G. Knepley         }
14192df84da0SMatthew G. Knepley         ierr = PetscArrayzero(funcVal,Nc);CHKERRQ(ierr);
14204bee2e38SMatthew G. Knepley         ierr = (*funcs[field])(coordDim, time, gcoords, n, Nc, funcVal, ctx);
1421e735a8a9SMatthew G. Knepley         if (ierr) {
1422e735a8a9SMatthew G. Knepley           PetscErrorCode ierr2;
1423e735a8a9SMatthew G. Knepley           ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr2);
1424e735a8a9SMatthew G. Knepley           ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
14254bee2e38SMatthew G. Knepley           ierr2 = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr2);
1426e735a8a9SMatthew G. Knepley           CHKERRQ(ierr);
1427e735a8a9SMatthew G. Knepley         }
1428ca3d3a14SMatthew G. Knepley         if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[coordDim*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
1429f9244615SMatthew G. Knepley         ierr = PetscFEInterpolateGradient_Static(fe, 1, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);
14304bee2e38SMatthew G. Knepley         /* Overwrite with the dot product if the normal is given */
14314bee2e38SMatthew G. Knepley         if (n) {
14324bee2e38SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
14334bee2e38SMatthew G. Knepley             PetscScalar sum = 0.0;
14344bee2e38SMatthew G. Knepley             PetscInt    d;
14354bee2e38SMatthew G. Knepley             for (d = 0; d < dim; ++d) sum += interpolant[fc*dim+d]*n[d];
14364bee2e38SMatthew G. Knepley             interpolant[fc] = sum;
14374bee2e38SMatthew G. Knepley           }
14384bee2e38SMatthew G. Knepley         }
14399c3cf19fSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1440beaa55a6SMatthew G. Knepley           const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1441ff1e0c32SBarry Smith           if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    elem %D fieldDer %D,%D diff %g\n", c, field, fc, (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
14424bee2e38SMatthew G. Knepley           elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
144340e14135SMatthew G. Knepley         }
144440e14135SMatthew G. Knepley       }
14459c3cf19fSMatthew G. Knepley       fieldOffset += Nb;
14469c3cf19fSMatthew G. Knepley       qc          += Nc;
144740e14135SMatthew G. Knepley     }
144840e14135SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1449ff1e0c32SBarry Smith     if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  elem %D diff %g\n", c, (double)elemDiff);CHKERRQ(ierr);}
145040e14135SMatthew G. Knepley     localDiff += elemDiff;
145140e14135SMatthew G. Knepley   }
14524bee2e38SMatthew G. Knepley   ierr  = PetscFree6(funcVal,coords,fegeom.J,fegeom.invJ,interpolant,fegeom.detJ);CHKERRQ(ierr);
145340e14135SMatthew G. Knepley   ierr  = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1454820f2d46SBarry Smith   ierr  = MPIU_Allreduce(&localDiff, diff, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
145540e14135SMatthew G. Knepley   *diff = PetscSqrtReal(*diff);
1456cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
1457cb1e1211SMatthew G Knepley }
1458cb1e1211SMatthew G Knepley 
1459c6eecec3SToby Isaac PetscErrorCode DMComputeL2FieldDiff_Plex(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
146073d901b8SMatthew G. Knepley {
14610f09c10fSMatthew G. Knepley   const PetscInt   debug = ((DM_Plex*)dm->data)->printL2;
1462ca3d3a14SMatthew G. Knepley   DM               tdm;
1463083401c6SMatthew G. Knepley   DMLabel          depthLabel;
146473d901b8SMatthew G. Knepley   PetscSection     section;
1465ca3d3a14SMatthew G. Knepley   Vec              localX, tv;
146673d901b8SMatthew G. Knepley   PetscReal       *localDiff;
1467083401c6SMatthew G. Knepley   PetscInt         dim, depth, dE, Nf, f, Nds, s;
1468ca3d3a14SMatthew G. Knepley   PetscBool        transform;
146973d901b8SMatthew G. Knepley   PetscErrorCode   ierr;
147073d901b8SMatthew G. Knepley 
147173d901b8SMatthew G. Knepley   PetscFunctionBegin;
1472c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1473083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
147492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
147573d901b8SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1476083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
1477083401c6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
1478083401c6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
1479083401c6SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1480083401c6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1481083401c6SMatthew G. Knepley   ierr = DMLabelGetNumValues(depthLabel, &depth);CHKERRQ(ierr);
1482083401c6SMatthew G. Knepley 
1483ca3d3a14SMatthew G. Knepley   ierr = VecSet(localX, 0.0);CHKERRQ(ierr);
148473d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
148573d901b8SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1486ca3d3a14SMatthew G. Knepley   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1487083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
1488083401c6SMatthew G. Knepley   ierr = PetscCalloc1(Nf, &localDiff);CHKERRQ(ierr);
1489083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
1490083401c6SMatthew G. Knepley     PetscDS          ds;
1491083401c6SMatthew G. Knepley     DMLabel          label;
1492083401c6SMatthew G. Knepley     IS               fieldIS, pointIS;
1493083401c6SMatthew G. Knepley     const PetscInt  *fields, *points = NULL;
1494083401c6SMatthew G. Knepley     PetscQuadrature  quad;
1495083401c6SMatthew G. Knepley     const PetscReal *quadPoints, *quadWeights;
1496083401c6SMatthew G. Knepley     PetscFEGeom      fegeom;
1497083401c6SMatthew G. Knepley     PetscReal       *coords, *gcoords;
1498083401c6SMatthew G. Knepley     PetscScalar     *funcVal, *interpolant;
14995fedec97SMatthew G. Knepley     PetscBool        isCohesive;
1500083401c6SMatthew G. Knepley     PetscInt         qNc, Nq, totNc, cStart = 0, cEnd, c, dsNf;
150173d901b8SMatthew G. Knepley 
1502083401c6SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
1503083401c6SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
15045fedec97SMatthew G. Knepley     ierr = PetscDSIsCohesive(ds, &isCohesive);CHKERRQ(ierr);
1505083401c6SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
1506083401c6SMatthew G. Knepley     ierr = PetscDSGetTotalComponents(ds, &totNc);CHKERRQ(ierr);
1507083401c6SMatthew G. Knepley     ierr = PetscDSGetQuadrature(ds, &quad);CHKERRQ(ierr);
15089c3cf19fSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
15092c71b3e2SJacob Faibussowitsch     PetscCheckFalse((qNc != 1) && (qNc != totNc),PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, totNc);
1510083401c6SMatthew G. Knepley     ierr = PetscCalloc6(totNc, &funcVal, totNc, &interpolant, dE*(Nq+1), &coords,Nq, &fegeom.detJ, dE*dE*Nq, &fegeom.J, dE*dE*Nq, &fegeom.invJ);CHKERRQ(ierr);
1511083401c6SMatthew G. Knepley     if (!label) {
1512412e9a14SMatthew G. Knepley       ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1513083401c6SMatthew G. Knepley     } else {
1514083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
1515083401c6SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &cEnd);CHKERRQ(ierr);
1516083401c6SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
1517083401c6SMatthew G. Knepley     }
151873d901b8SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
1519083401c6SMatthew G. Knepley       const PetscInt cell  = points ? points[c] : c;
152073d901b8SMatthew G. Knepley       PetscScalar    *x    = NULL;
15215fedec97SMatthew G. Knepley       const PetscInt *cone;
15225fedec97SMatthew G. Knepley       PetscInt        qc   = 0, fOff = 0, dep;
152373d901b8SMatthew G. Knepley 
1524083401c6SMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, cell, &dep);CHKERRQ(ierr);
1525083401c6SMatthew G. Knepley       if (dep != depth-1) continue;
15265fedec97SMatthew G. Knepley       if (isCohesive) {
152796959cd1SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
152896959cd1SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cone[0], quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
152996959cd1SMatthew G. Knepley       } else {
1530083401c6SMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
153196959cd1SMatthew G. Knepley       }
1532083401c6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
15335fedec97SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
153415496722SMatthew G. Knepley         PetscObject  obj;
153515496722SMatthew G. Knepley         PetscClassId id;
1536083401c6SMatthew G. Knepley         void * const ctx = ctxs ? ctxs[fields[f]] : NULL;
153715496722SMatthew G. Knepley         PetscInt     Nb, Nc, q, fc;
153815496722SMatthew G. Knepley         PetscReal    elemDiff = 0.0;
15395fedec97SMatthew G. Knepley         PetscBool    cohesive;
154015496722SMatthew G. Knepley 
15415fedec97SMatthew G. Knepley         ierr = PetscDSGetCohesive(ds, f, &cohesive);CHKERRQ(ierr);
15425fedec97SMatthew G. Knepley         if (isCohesive && !cohesive) continue;
1543083401c6SMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
154415496722SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
154515496722SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
154615496722SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
154798921bdaSJacob Faibussowitsch         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
154873d901b8SMatthew G. Knepley         if (debug) {
154973d901b8SMatthew G. Knepley           char title[1024];
1550083401c6SMatthew G. Knepley           ierr = PetscSNPrintf(title, 1023, "Solution for Field %D", fields[f]);CHKERRQ(ierr);
1551083401c6SMatthew G. Knepley           ierr = DMPrintCellVector(cell, title, Nb, &x[fOff]);CHKERRQ(ierr);
155273d901b8SMatthew G. Knepley         }
15537318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
15542a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
15552a4e142eSMatthew G. Knepley 
15562a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
1557083401c6SMatthew G. Knepley           qgeom.J        = &fegeom.J[q*dE*dE];
1558083401c6SMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*dE*dE];
15592a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
15602c71b3e2SJacob Faibussowitsch           PetscCheckFalse(fegeom.detJ[q] <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for cell %D, quadrature point %D", (double)fegeom.detJ[q], cell, q);
1561d3a7d86cSMatthew G. Knepley           if (transform) {
1562083401c6SMatthew G. Knepley             gcoords = &coords[dE*Nq];
1563083401c6SMatthew G. Knepley             ierr = DMPlexBasisTransformApplyReal_Internal(dm, &coords[dE*q], PETSC_TRUE, dE, &coords[dE*q], gcoords, dm->transformCtx);CHKERRQ(ierr);
1564d3a7d86cSMatthew G. Knepley           } else {
1565083401c6SMatthew G. Knepley             gcoords = &coords[dE*q];
1566d3a7d86cSMatthew G. Knepley           }
15672df84da0SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) funcVal[fc] = 0.;
1568083401c6SMatthew G. Knepley           ierr = (*funcs[fields[f]])(dE, time, gcoords, Nc, funcVal, ctx);
1569e735a8a9SMatthew G. Knepley           if (ierr) {
1570e735a8a9SMatthew G. Knepley             PetscErrorCode ierr2;
1571083401c6SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr2);
1572e735a8a9SMatthew G. Knepley             ierr2 = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr2);
1573083401c6SMatthew G. Knepley             ierr2 = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
1574e735a8a9SMatthew G. Knepley             CHKERRQ(ierr);
1575e735a8a9SMatthew G. Knepley           }
1576083401c6SMatthew G. Knepley           if (transform) {ierr = DMPlexBasisTransformApply_Internal(dm, &coords[dE*q], PETSC_FALSE, Nc, funcVal, funcVal, dm->transformCtx);CHKERRQ(ierr);}
157796959cd1SMatthew G. Knepley           /* Call once for each face, except for lagrange field */
1578083401c6SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fOff], &qgeom, q, interpolant);CHKERRQ(ierr);}
1579083401c6SMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fOff], q, interpolant);CHKERRQ(ierr);}
158098921bdaSJacob Faibussowitsch           else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", fields[f]);
158115496722SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1582beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
1583083401c6SMatthew G. Knepley             if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "    cell %D field %D,%D point %g %g %g diff %g\n", cell, fields[f], fc, (double)(dE > 0 ? coords[dE*q] : 0.), (double)(dE > 1 ? coords[dE*q+1] : 0.),(double)(dE > 2 ? coords[dE*q+2] : 0.), (double)(PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q]));CHKERRQ(ierr);}
15844bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
158573d901b8SMatthew G. Knepley           }
158673d901b8SMatthew G. Knepley         }
1587083401c6SMatthew G. Knepley         fOff += Nb;
15889c3cf19fSMatthew G. Knepley         qc   += Nc;
1589083401c6SMatthew G. Knepley         localDiff[fields[f]] += elemDiff;
1590083401c6SMatthew G. Knepley         if (debug) {ierr = PetscPrintf(PETSC_COMM_SELF, "  cell %D field %D cum diff %g\n", cell, fields[f], (double)localDiff[fields[f]]);CHKERRQ(ierr);}
159173d901b8SMatthew G. Knepley       }
1592083401c6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, localX, cell, NULL, &x);CHKERRQ(ierr);
1593083401c6SMatthew G. Knepley     }
1594083401c6SMatthew G. Knepley     if (label) {
1595083401c6SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
1596083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
1597083401c6SMatthew G. Knepley     }
1598083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
1599083401c6SMatthew G. Knepley     ierr = PetscFree6(funcVal, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ);CHKERRQ(ierr);
160073d901b8SMatthew G. Knepley   }
160173d901b8SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1602820f2d46SBarry Smith   ierr = MPIU_Allreduce(localDiff, diff, Nf, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)dm));CHKERRMPI(ierr);
1603083401c6SMatthew G. Knepley   ierr = PetscFree(localDiff);CHKERRQ(ierr);
1604083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) diff[f] = PetscSqrtReal(diff[f]);
160573d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
160673d901b8SMatthew G. Knepley }
160773d901b8SMatthew G. Knepley 
1608e729f68cSMatthew G. Knepley /*@C
1609e729f68cSMatthew 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.
1610e729f68cSMatthew G. Knepley 
1611d083f849SBarry Smith   Collective on dm
1612c0f8e1fdSMatthew G. Knepley 
1613e729f68cSMatthew G. Knepley   Input Parameters:
1614e729f68cSMatthew G. Knepley + dm    - The DM
16150163fd50SMatthew G. Knepley . time  - The time
1616ca3eba1bSToby Isaac . funcs - The functions to evaluate for each field component: NULL means that component does not contribute to error calculation
1617e729f68cSMatthew G. Knepley . ctxs  - Optional array of contexts to pass to each function, or NULL.
1618e729f68cSMatthew G. Knepley - X     - The coefficient vector u_h
1619e729f68cSMatthew G. Knepley 
1620e729f68cSMatthew G. Knepley   Output Parameter:
1621e729f68cSMatthew G. Knepley . D - A Vec which holds the difference ||u - u_h||_2 for each cell
1622e729f68cSMatthew G. Knepley 
1623e729f68cSMatthew G. Knepley   Level: developer
1624e729f68cSMatthew G. Knepley 
1625b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1626e729f68cSMatthew G. Knepley @*/
16270163fd50SMatthew G. Knepley PetscErrorCode DMPlexComputeL2DiffVec(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, Vec D)
1628e729f68cSMatthew G. Knepley {
1629e729f68cSMatthew G. Knepley   PetscSection     section;
1630e729f68cSMatthew G. Knepley   PetscQuadrature  quad;
1631e729f68cSMatthew G. Knepley   Vec              localX;
16324bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
1633e729f68cSMatthew G. Knepley   PetscScalar     *funcVal, *interpolant;
16344bee2e38SMatthew G. Knepley   PetscReal       *coords;
1635e729f68cSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1636485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, c, field, fieldOffset;
1637e729f68cSMatthew G. Knepley   PetscErrorCode   ierr;
1638e729f68cSMatthew G. Knepley 
1639e729f68cSMatthew G. Knepley   PetscFunctionBegin;
1640e729f68cSMatthew G. Knepley   ierr = VecSet(D, 0.0);CHKERRQ(ierr);
1641e729f68cSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16427318780aSToby Isaac   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
164392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1644e729f68cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
1645e729f68cSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
1646bdd6f66aSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, INSERT_BC_VALUES, localX);CHKERRQ(ierr);
1647e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1648e729f68cSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, localX);CHKERRQ(ierr);
1649e729f68cSMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
1650e729f68cSMatthew G. Knepley     PetscObject  obj;
1651e729f68cSMatthew G. Knepley     PetscClassId id;
1652e729f68cSMatthew G. Knepley     PetscInt     Nc;
1653e729f68cSMatthew G. Knepley 
165444a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1655e729f68cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1656e729f68cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1657e729f68cSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1658e729f68cSMatthew G. Knepley 
1659e729f68cSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1660e729f68cSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
1661e729f68cSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1662e729f68cSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1663e729f68cSMatthew G. Knepley 
1664e729f68cSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1665e729f68cSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
166698921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1667e729f68cSMatthew G. Knepley     numComponents += Nc;
1668e729f68cSMatthew G. Knepley   }
16699c3cf19fSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
16702c71b3e2SJacob Faibussowitsch   PetscCheckFalse((qNc != 1) && (qNc != numComponents),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
16712a4e142eSMatthew G. Knepley   ierr = PetscMalloc6(numComponents,&funcVal,numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
1672412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1673e729f68cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1674e729f68cSMatthew G. Knepley     PetscScalar *x = NULL;
16756f288a59SMatthew G. Knepley     PetscScalar  elemDiff = 0.0;
16769c3cf19fSMatthew G. Knepley     PetscInt     qc = 0;
1677e729f68cSMatthew G. Knepley 
16782a4e142eSMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1679e729f68cSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1680e729f68cSMatthew G. Knepley 
1681e729f68cSMatthew G. Knepley     for (field = 0, fieldOffset = 0; field < numFields; ++field) {
1682e729f68cSMatthew G. Knepley       PetscObject  obj;
1683e729f68cSMatthew G. Knepley       PetscClassId id;
1684e729f68cSMatthew G. Knepley       void * const ctx = ctxs ? ctxs[field] : NULL;
1685e729f68cSMatthew G. Knepley       PetscInt     Nb, Nc, q, fc;
1686e729f68cSMatthew G. Knepley 
168744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
1688e729f68cSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1689e729f68cSMatthew G. Knepley       if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1690e729f68cSMatthew G. Knepley       else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
169198921bdaSJacob Faibussowitsch       else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
169223f34ed2SToby Isaac       if (funcs[field]) {
16937318780aSToby Isaac         for (q = 0; q < Nq; ++q) {
16942a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
16952a4e142eSMatthew G. Knepley 
16962a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
16972a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
16982a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
16992a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
17002c71b3e2SJacob Faibussowitsch           PetscCheckFalse(fegeom.detJ[q] <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], c, q);
17012f613bf5SBarry Smith           ierr = (*funcs[field])(coordDim, time, &coords[q*coordDim], Nc, funcVal, ctx);CHKERRQ(ierr);
1702c3e24edfSBarry Smith #if defined(needs_fix_with_return_code_argument)
1703e735a8a9SMatthew G. Knepley           if (ierr) {
1704c3e24edfSBarry Smith             PetscErrorCode ierr;
1705c3e24edfSBarry Smith             ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
1706c3e24edfSBarry Smith             ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1707c3e24edfSBarry Smith             ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1708e735a8a9SMatthew G. Knepley           }
1709c3e24edfSBarry Smith #endif
17102a4e142eSMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
1711e729f68cSMatthew G. Knepley           else if (id == PETSCFV_CLASSID) {ierr = PetscFVInterpolate_Static((PetscFV) obj, &x[fieldOffset], q, interpolant);CHKERRQ(ierr);}
171298921bdaSJacob Faibussowitsch           else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
1713e729f68cSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
1714beaa55a6SMatthew G. Knepley             const PetscReal wt = quadWeights[q*qNc+(qNc == 1 ? 0 : qc+fc)];
17154bee2e38SMatthew G. Knepley             elemDiff += PetscSqr(PetscRealPart(interpolant[fc] - funcVal[fc]))*wt*fegeom.detJ[q];
1716e729f68cSMatthew G. Knepley           }
1717e729f68cSMatthew G. Knepley         }
171823f34ed2SToby Isaac       }
1719beaa55a6SMatthew G. Knepley       fieldOffset += Nb;
17209c3cf19fSMatthew G. Knepley       qc          += Nc;
1721e729f68cSMatthew G. Knepley     }
1722e729f68cSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, NULL, localX, c, NULL, &x);CHKERRQ(ierr);
172323f34ed2SToby Isaac     ierr = VecSetValue(D, c - cStart, elemDiff, INSERT_VALUES);CHKERRQ(ierr);
1724e729f68cSMatthew G. Knepley   }
17252a4e142eSMatthew G. Knepley   ierr = PetscFree6(funcVal,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
1726e729f68cSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
1727e729f68cSMatthew G. Knepley   ierr = VecSqrtAbs(D);CHKERRQ(ierr);
1728e729f68cSMatthew G. Knepley   PetscFunctionReturn(0);
1729e729f68cSMatthew G. Knepley }
1730e729f68cSMatthew G. Knepley 
1731*5f0b18bfSMatthew G. Knepley /*@
1732*5f0b18bfSMatthew G. Knepley   DMPlexComputeClementInterpolant - This function computes the L2 projection of the cellwise values of a function u onto P1, and stores it in a Vec.
1733*5f0b18bfSMatthew G. Knepley 
1734*5f0b18bfSMatthew G. Knepley   Collective on dm
1735*5f0b18bfSMatthew G. Knepley 
1736*5f0b18bfSMatthew G. Knepley   Input Parameters:
1737*5f0b18bfSMatthew G. Knepley + dm - The DM
1738*5f0b18bfSMatthew G. Knepley - locX  - The coefficient vector u_h
1739*5f0b18bfSMatthew G. Knepley 
1740*5f0b18bfSMatthew G. Knepley   Output Parameter:
1741*5f0b18bfSMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the function
1742*5f0b18bfSMatthew G. Knepley 
1743*5f0b18bfSMatthew G. Knepley   Notes:
1744*5f0b18bfSMatthew G. Knepley   u_h(v_i) = \sum_{T_i \in support(v_i)} |T_i| u_h(T_i) / \sum_{T_i \in support(v_i)} |T_i| where |T_i| is the cell volume
1745*5f0b18bfSMatthew G. Knepley 
1746*5f0b18bfSMatthew G. Knepley   Level: developer
1747*5f0b18bfSMatthew G. Knepley 
1748*5f0b18bfSMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
1749*5f0b18bfSMatthew G. Knepley @*/
1750*5f0b18bfSMatthew G. Knepley PetscErrorCode DMPlexComputeClementInterpolant(DM dm, Vec locX, Vec locC)
1751*5f0b18bfSMatthew G. Knepley {
1752*5f0b18bfSMatthew G. Knepley   PetscInt         debug = ((DM_Plex *) dm->data)->printFEM;
1753*5f0b18bfSMatthew G. Knepley   DM               dmc;
1754*5f0b18bfSMatthew G. Knepley   PetscQuadrature  quad;
1755*5f0b18bfSMatthew G. Knepley   PetscScalar     *interpolant, *valsum;
1756*5f0b18bfSMatthew G. Knepley   PetscFEGeom      fegeom;
1757*5f0b18bfSMatthew G. Knepley   PetscReal       *coords;
1758*5f0b18bfSMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1759*5f0b18bfSMatthew G. Knepley   PetscInt         dim, cdim, Nf, f, Nc = 0, Nq, qNc, cStart, cEnd, vStart, vEnd, v;
1760*5f0b18bfSMatthew G. Knepley   PetscErrorCode   ierr;
1761*5f0b18bfSMatthew G. Knepley 
1762*5f0b18bfSMatthew G. Knepley   PetscFunctionBegin;
1763*5f0b18bfSMatthew G. Knepley   ierr = PetscCitationsRegister(ClementCitation, &Clementcite);CHKERRQ(ierr);
1764*5f0b18bfSMatthew G. Knepley   ierr = VecGetDM(locC, &dmc);CHKERRQ(ierr);
1765*5f0b18bfSMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
1766*5f0b18bfSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1767*5f0b18bfSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
1768*5f0b18bfSMatthew G. Knepley   fegeom.dimEmbed = cdim;
1769*5f0b18bfSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1770*5f0b18bfSMatthew G. Knepley   PetscCheck(Nf > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fields is zero!");
1771*5f0b18bfSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
1772*5f0b18bfSMatthew G. Knepley     PetscObject  obj;
1773*5f0b18bfSMatthew G. Knepley     PetscClassId id;
1774*5f0b18bfSMatthew G. Knepley     PetscInt     fNc;
1775*5f0b18bfSMatthew G. Knepley 
1776*5f0b18bfSMatthew G. Knepley     ierr = DMGetField(dm, f, NULL, &obj);CHKERRQ(ierr);
1777*5f0b18bfSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1778*5f0b18bfSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
1779*5f0b18bfSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
1780*5f0b18bfSMatthew G. Knepley 
1781*5f0b18bfSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
1782*5f0b18bfSMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &fNc);CHKERRQ(ierr);
1783*5f0b18bfSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
1784*5f0b18bfSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
1785*5f0b18bfSMatthew G. Knepley 
1786*5f0b18bfSMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
1787*5f0b18bfSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &fNc);CHKERRQ(ierr);
1788*5f0b18bfSMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1789*5f0b18bfSMatthew G. Knepley     Nc += fNc;
1790*5f0b18bfSMatthew G. Knepley   }
1791*5f0b18bfSMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
1792*5f0b18bfSMatthew G. Knepley   PetscCheck(qNc == 1,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D > 1", qNc);
1793*5f0b18bfSMatthew G. Knepley   ierr = PetscMalloc6(Nc*2, &valsum, Nc, &interpolant, cdim*Nq, &coords, Nq, &fegeom.detJ, cdim*cdim*Nq, &fegeom.J, cdim*cdim*Nq, &fegeom.invJ);CHKERRQ(ierr);
1794*5f0b18bfSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1795*5f0b18bfSMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1796*5f0b18bfSMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
1797*5f0b18bfSMatthew G. Knepley     PetscScalar volsum = 0.0;
1798*5f0b18bfSMatthew G. Knepley     PetscInt   *star   = NULL;
1799*5f0b18bfSMatthew G. Knepley     PetscInt    starSize, st, fc;
1800*5f0b18bfSMatthew G. Knepley 
1801*5f0b18bfSMatthew G. Knepley     ierr = PetscArrayzero(valsum, Nc);CHKERRQ(ierr);
1802*5f0b18bfSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1803*5f0b18bfSMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
1804*5f0b18bfSMatthew G. Knepley       const PetscInt cell = star[st];
1805*5f0b18bfSMatthew G. Knepley       PetscScalar   *val  = &valsum[Nc];
1806*5f0b18bfSMatthew G. Knepley       PetscScalar   *x    = NULL;
1807*5f0b18bfSMatthew G. Knepley       PetscReal      vol  = 0.0;
1808*5f0b18bfSMatthew G. Knepley       PetscInt       foff = 0;
1809*5f0b18bfSMatthew G. Knepley 
1810*5f0b18bfSMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
1811*5f0b18bfSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
1812*5f0b18bfSMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1813*5f0b18bfSMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1814*5f0b18bfSMatthew G. Knepley         PetscObject  obj;
1815*5f0b18bfSMatthew G. Knepley         PetscClassId id;
1816*5f0b18bfSMatthew G. Knepley         PetscInt     Nb, fNc, q;
1817*5f0b18bfSMatthew G. Knepley 
1818*5f0b18bfSMatthew G. Knepley         ierr = PetscArrayzero(val, Nc);CHKERRQ(ierr);
1819*5f0b18bfSMatthew G. Knepley         ierr = DMGetField(dm, f, NULL, &obj);CHKERRQ(ierr);
1820*5f0b18bfSMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
1821*5f0b18bfSMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &fNc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
1822*5f0b18bfSMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &fNc);CHKERRQ(ierr);Nb = 1;}
1823*5f0b18bfSMatthew G. Knepley         else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1824*5f0b18bfSMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
1825*5f0b18bfSMatthew G. Knepley           const PetscReal wt = quadWeights[q]*fegeom.detJ[q];
1826*5f0b18bfSMatthew G. Knepley           PetscFEGeom     qgeom;
1827*5f0b18bfSMatthew G. Knepley 
1828*5f0b18bfSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
1829*5f0b18bfSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*cdim*cdim];
1830*5f0b18bfSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*cdim*cdim];
1831*5f0b18bfSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
1832*5f0b18bfSMatthew G. Knepley           PetscCheck(fegeom.detJ[q] > 0.0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double) fegeom.detJ[q], cell, q);
1833*5f0b18bfSMatthew G. Knepley           if (id == PETSCFE_CLASSID) {ierr = PetscFEInterpolate_Static((PetscFE) obj, &x[foff], &qgeom, q, interpolant);CHKERRQ(ierr);}
1834*5f0b18bfSMatthew G. Knepley           else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
1835*5f0b18bfSMatthew G. Knepley           for (fc = 0; fc < fNc; ++fc) val[foff+fc] += interpolant[fc]*wt;
1836*5f0b18bfSMatthew G. Knepley           vol += wt;
1837*5f0b18bfSMatthew G. Knepley         }
1838*5f0b18bfSMatthew G. Knepley         foff += Nb;
1839*5f0b18bfSMatthew G. Knepley       }
1840*5f0b18bfSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1841*5f0b18bfSMatthew G. Knepley       for (fc = 0; fc < Nc; ++fc) valsum[fc] += val[fc];
1842*5f0b18bfSMatthew G. Knepley       volsum += vol;
1843*5f0b18bfSMatthew G. Knepley       if (debug) {
1844*5f0b18bfSMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT " Cell %" PetscInt_FMT " value: [", v, cell);CHKERRQ(ierr);
1845*5f0b18bfSMatthew G. Knepley         for (fc = 0; fc < Nc; ++fc) {
1846*5f0b18bfSMatthew G. Knepley           if (fc) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
1847*5f0b18bfSMatthew G. Knepley           ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double) PetscRealPart(val[fc]));CHKERRQ(ierr);
1848*5f0b18bfSMatthew G. Knepley         }
1849*5f0b18bfSMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1850*5f0b18bfSMatthew G. Knepley       }
1851*5f0b18bfSMatthew G. Knepley     }
1852*5f0b18bfSMatthew G. Knepley     for (fc = 0; fc < Nc; ++fc) valsum[fc] /= volsum;
1853*5f0b18bfSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1854*5f0b18bfSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmc, NULL, locC, v, valsum, INSERT_VALUES);CHKERRQ(ierr);
1855*5f0b18bfSMatthew G. Knepley   }
1856*5f0b18bfSMatthew G. Knepley   ierr = PetscFree6(valsum, interpolant, coords, fegeom.detJ, fegeom.J, fegeom.invJ);CHKERRQ(ierr);
1857*5f0b18bfSMatthew G. Knepley   PetscFunctionReturn(0);
1858*5f0b18bfSMatthew G. Knepley }
1859*5f0b18bfSMatthew G. Knepley 
1860*5f0b18bfSMatthew G. Knepley /*@
18611555c271SMatthew 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.
18621555c271SMatthew G. Knepley 
1863d083f849SBarry Smith   Collective on dm
1864c0f8e1fdSMatthew G. Knepley 
18651555c271SMatthew G. Knepley   Input Parameters:
18661555c271SMatthew G. Knepley + dm - The DM
1867*5f0b18bfSMatthew G. Knepley - locX  - The coefficient vector u_h
18681555c271SMatthew G. Knepley 
18691555c271SMatthew G. Knepley   Output Parameter:
18701555c271SMatthew G. Knepley . locC - A Vec which holds the Clement interpolant of the gradient
18711555c271SMatthew G. Knepley 
187295452b02SPatrick Sanan   Notes:
18731555c271SMatthew 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
18741555c271SMatthew G. Knepley 
18751555c271SMatthew G. Knepley   Level: developer
18761555c271SMatthew G. Knepley 
18771555c271SMatthew G. Knepley .seealso: DMProjectFunction(), DMComputeL2Diff(), DMPlexComputeL2FieldDiff(), DMComputeL2GradientDiff()
18781555c271SMatthew G. Knepley @*/
18791555c271SMatthew G. Knepley PetscErrorCode DMPlexComputeGradientClementInterpolant(DM dm, Vec locX, Vec locC)
18801555c271SMatthew G. Knepley {
1881db1066baSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
1882db1066baSMatthew G. Knepley   PetscInt         debug = mesh->printFEM;
18831555c271SMatthew G. Knepley   DM               dmC;
18841555c271SMatthew G. Knepley   PetscQuadrature  quad;
18851555c271SMatthew G. Knepley   PetscScalar     *interpolant, *gradsum;
18864bee2e38SMatthew G. Knepley   PetscFEGeom      fegeom;
18874bee2e38SMatthew G. Knepley   PetscReal       *coords;
18881555c271SMatthew G. Knepley   const PetscReal *quadPoints, *quadWeights;
1889485ad865SMatthew G. Knepley   PetscInt         dim, coordDim, numFields, numComponents = 0, qNc, Nq, cStart, cEnd, vStart, vEnd, v, field, fieldOffset;
18901555c271SMatthew G. Knepley   PetscErrorCode   ierr;
18911555c271SMatthew G. Knepley 
18921555c271SMatthew G. Knepley   PetscFunctionBegin;
1893*5f0b18bfSMatthew G. Knepley   ierr = PetscCitationsRegister(ClementCitation, &Clementcite);CHKERRQ(ierr);
18941555c271SMatthew G. Knepley   ierr = VecGetDM(locC, &dmC);CHKERRQ(ierr);
18951555c271SMatthew G. Knepley   ierr = VecSet(locC, 0.0);CHKERRQ(ierr);
18961555c271SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
18971555c271SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &coordDim);CHKERRQ(ierr);
18984bee2e38SMatthew G. Knepley   fegeom.dimEmbed = coordDim;
18997a8154cbSJoe Wallwork   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
19002c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFields == 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fields is zero!");
19011555c271SMatthew G. Knepley   for (field = 0; field < numFields; ++field) {
19021555c271SMatthew G. Knepley     PetscObject  obj;
19031555c271SMatthew G. Knepley     PetscClassId id;
19041555c271SMatthew G. Knepley     PetscInt     Nc;
19051555c271SMatthew G. Knepley 
190644a7f3ddSMatthew G. Knepley     ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
19071555c271SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
19081555c271SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
19091555c271SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
19101555c271SMatthew G. Knepley 
19111555c271SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
19121555c271SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
19131555c271SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
19141555c271SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
19151555c271SMatthew G. Knepley 
19161555c271SMatthew G. Knepley       ierr = PetscFVGetQuadrature(fv, &quad);CHKERRQ(ierr);
19171555c271SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
191898921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
19191555c271SMatthew G. Knepley     numComponents += Nc;
19201555c271SMatthew G. Knepley   }
19211555c271SMatthew G. Knepley   ierr = PetscQuadratureGetData(quad, NULL, &qNc, &Nq, &quadPoints, &quadWeights);CHKERRQ(ierr);
19222c71b3e2SJacob Faibussowitsch   PetscCheckFalse((qNc != 1) && (qNc != numComponents),PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_SIZ, "Quadrature components %D != %D field components", qNc, numComponents);
19234bee2e38SMatthew G. Knepley   ierr = PetscMalloc6(coordDim*numComponents*2,&gradsum,coordDim*numComponents,&interpolant,coordDim*Nq,&coords,Nq,&fegeom.detJ,coordDim*coordDim*Nq,&fegeom.J,coordDim*coordDim*Nq,&fegeom.invJ);CHKERRQ(ierr);
19241555c271SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1925412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
19261555c271SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
19271555c271SMatthew G. Knepley     PetscScalar volsum = 0.0;
19281555c271SMatthew G. Knepley     PetscInt   *star   = NULL;
19291555c271SMatthew G. Knepley     PetscInt    starSize, st, d, fc;
19301555c271SMatthew G. Knepley 
1931580bdb30SBarry Smith     ierr = PetscArrayzero(gradsum, coordDim*numComponents);CHKERRQ(ierr);
19321555c271SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
19331555c271SMatthew G. Knepley     for (st = 0; st < starSize*2; st += 2) {
19341555c271SMatthew G. Knepley       const PetscInt cell = star[st];
19351555c271SMatthew G. Knepley       PetscScalar   *grad = &gradsum[coordDim*numComponents];
19361555c271SMatthew G. Knepley       PetscScalar   *x    = NULL;
19371555c271SMatthew G. Knepley       PetscReal      vol  = 0.0;
19381555c271SMatthew G. Knepley 
19391555c271SMatthew G. Knepley       if ((cell < cStart) || (cell >= cEnd)) continue;
19404bee2e38SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dm, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ);CHKERRQ(ierr);
19411555c271SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
19421555c271SMatthew G. Knepley       for (field = 0, fieldOffset = 0; field < numFields; ++field) {
19431555c271SMatthew G. Knepley         PetscObject  obj;
19441555c271SMatthew G. Knepley         PetscClassId id;
19451555c271SMatthew G. Knepley         PetscInt     Nb, Nc, q, qc = 0;
19461555c271SMatthew G. Knepley 
1947580bdb30SBarry Smith         ierr = PetscArrayzero(grad, coordDim*numComponents);CHKERRQ(ierr);
194844a7f3ddSMatthew G. Knepley         ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
19491555c271SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
19501555c271SMatthew G. Knepley         if (id == PETSCFE_CLASSID)      {ierr = PetscFEGetNumComponents((PetscFE) obj, &Nc);CHKERRQ(ierr);ierr = PetscFEGetDimension((PetscFE) obj, &Nb);CHKERRQ(ierr);}
19511555c271SMatthew G. Knepley         else if (id == PETSCFV_CLASSID) {ierr = PetscFVGetNumComponents((PetscFV) obj, &Nc);CHKERRQ(ierr);Nb = 1;}
195298921bdaSJacob Faibussowitsch         else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
19531555c271SMatthew G. Knepley         for (q = 0; q < Nq; ++q) {
19542a4e142eSMatthew G. Knepley           PetscFEGeom qgeom;
19552a4e142eSMatthew G. Knepley 
19562a4e142eSMatthew G. Knepley           qgeom.dimEmbed = fegeom.dimEmbed;
19572a4e142eSMatthew G. Knepley           qgeom.J        = &fegeom.J[q*coordDim*coordDim];
19582a4e142eSMatthew G. Knepley           qgeom.invJ     = &fegeom.invJ[q*coordDim*coordDim];
19592a4e142eSMatthew G. Knepley           qgeom.detJ     = &fegeom.detJ[q];
19602c71b3e2SJacob Faibussowitsch           PetscCheckFalse(fegeom.detJ[q] <= 0.0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid determinant %g for element %D, quadrature points %D", (double)fegeom.detJ[q], cell, q);
19611555c271SMatthew G. Knepley           if (ierr) {
19621555c271SMatthew G. Knepley             PetscErrorCode ierr2;
19631555c271SMatthew G. Knepley             ierr2 = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr2);
19641555c271SMatthew G. Knepley             ierr2 = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr2);
19654bee2e38SMatthew G. Knepley             ierr2 = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr2);
19661555c271SMatthew G. Knepley             CHKERRQ(ierr);
19671555c271SMatthew G. Knepley           }
1968f9244615SMatthew G. Knepley           if (id == PETSCFE_CLASSID)      {ierr = PetscFEInterpolateGradient_Static((PetscFE) obj, 1, &x[fieldOffset], &qgeom, q, interpolant);CHKERRQ(ierr);}
196998921bdaSJacob Faibussowitsch           else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", field);
19701555c271SMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
197153d2db2dSJoe Wallwork             const PetscReal wt = quadWeights[q*qNc+qc];
19721555c271SMatthew G. Knepley 
19734bee2e38SMatthew G. Knepley             for (d = 0; d < coordDim; ++d) grad[fc*coordDim+d] += interpolant[fc*dim+d]*wt*fegeom.detJ[q];
19741555c271SMatthew G. Knepley           }
19754bee2e38SMatthew G. Knepley           vol += quadWeights[q*qNc]*fegeom.detJ[q];
19761555c271SMatthew G. Knepley         }
19771555c271SMatthew G. Knepley         fieldOffset += Nb;
19781555c271SMatthew G. Knepley         qc          += Nc;
19791555c271SMatthew G. Knepley       }
19801555c271SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, locX, cell, NULL, &x);CHKERRQ(ierr);
1981f8527842SMatthew G. Knepley       for (fc = 0; fc < numComponents; ++fc) {
1982f8527842SMatthew G. Knepley         for (d = 0; d < coordDim; ++d) {
1983f8527842SMatthew G. Knepley           gradsum[fc*coordDim+d] += grad[fc*coordDim+d];
1984f8527842SMatthew G. Knepley         }
1985f8527842SMatthew G. Knepley       }
1986f8527842SMatthew G. Knepley       volsum += vol;
1987db1066baSMatthew G. Knepley       if (debug) {
1988bb48be70SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT " Cell %" PetscInt_FMT " gradient: [", v, cell);CHKERRQ(ierr);
19891555c271SMatthew G. Knepley         for (fc = 0; fc < numComponents; ++fc) {
19901555c271SMatthew G. Knepley           for (d = 0; d < coordDim; ++d) {
19911555c271SMatthew G. Knepley             if (fc || d > 0) {ierr = PetscPrintf(PETSC_COMM_SELF, ", ");CHKERRQ(ierr);}
19926d20ae03SJed Brown             ierr = PetscPrintf(PETSC_COMM_SELF, "%g", (double)PetscRealPart(grad[fc*coordDim+d]));CHKERRQ(ierr);
19931555c271SMatthew G. Knepley           }
19941555c271SMatthew G. Knepley         }
19951555c271SMatthew G. Knepley         ierr = PetscPrintf(PETSC_COMM_SELF, "]\n");CHKERRQ(ierr);
1996db1066baSMatthew G. Knepley       }
19971555c271SMatthew G. Knepley     }
19981555c271SMatthew G. Knepley     for (fc = 0; fc < numComponents; ++fc) {
19991555c271SMatthew G. Knepley       for (d = 0; d < coordDim; ++d) gradsum[fc*coordDim+d] /= volsum;
20001555c271SMatthew G. Knepley     }
20011555c271SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
20021555c271SMatthew G. Knepley     ierr = DMPlexVecSetClosure(dmC, NULL, locC, v, gradsum, INSERT_VALUES);CHKERRQ(ierr);
20031555c271SMatthew G. Knepley   }
20044bee2e38SMatthew G. Knepley   ierr = PetscFree6(gradsum,interpolant,coords,fegeom.detJ,fegeom.J,fegeom.invJ);CHKERRQ(ierr);
20051555c271SMatthew G. Knepley   PetscFunctionReturn(0);
20061555c271SMatthew G. Knepley }
20071555c271SMatthew G. Knepley 
2008338f77d5SMatthew G. Knepley static PetscErrorCode DMPlexComputeIntegral_Internal(DM dm, Vec X, PetscInt cStart, PetscInt cEnd, PetscScalar *cintegral, void *user)
200973d901b8SMatthew G. Knepley {
2010338f77d5SMatthew G. Knepley   DM                 dmAux = NULL;
201161aaff12SToby Isaac   PetscDS            prob,    probAux = NULL;
201273d901b8SMatthew G. Knepley   PetscSection       section, sectionAux;
2013338f77d5SMatthew G. Knepley   Vec                locX,    locA;
2014c330f8ffSToby Isaac   PetscInt           dim, numCells = cEnd - cStart, c, f;
2015c330f8ffSToby Isaac   PetscBool          useFVM = PETSC_FALSE;
2016338f77d5SMatthew G. Knepley   /* DS */
2017338f77d5SMatthew G. Knepley   PetscInt           Nf,    totDim,    *uOff, *uOff_x, numConstants;
2018338f77d5SMatthew G. Knepley   PetscInt           NfAux, totDimAux, *aOff;
2019338f77d5SMatthew G. Knepley   PetscScalar       *u, *a;
2020338f77d5SMatthew G. Knepley   const PetscScalar *constants;
2021338f77d5SMatthew G. Knepley   /* Geometry */
2022c330f8ffSToby Isaac   PetscFEGeom       *cgeomFEM;
2023338f77d5SMatthew G. Knepley   DM                 dmGrad;
2024c330f8ffSToby Isaac   PetscQuadrature    affineQuad = NULL;
2025338f77d5SMatthew G. Knepley   Vec                cellGeometryFVM = NULL, faceGeometryFVM = NULL, locGrad = NULL;
2026b5a3613cSMatthew G. Knepley   PetscFVCellGeom   *cgeomFVM;
2027338f77d5SMatthew G. Knepley   const PetscScalar *lgrad;
2028b7260050SToby Isaac   PetscInt           maxDegree;
2029c330f8ffSToby Isaac   DMField            coordField;
2030c330f8ffSToby Isaac   IS                 cellIS;
203173d901b8SMatthew G. Knepley   PetscErrorCode     ierr;
203273d901b8SMatthew G. Knepley 
203373d901b8SMatthew G. Knepley   PetscFunctionBegin;
2034338f77d5SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
2035c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
203692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
20377a8154cbSJoe Wallwork   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2038338f77d5SMatthew G. Knepley   /* Determine which discretizations we have */
2039b5a3613cSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2040b5a3613cSMatthew G. Knepley     PetscObject  obj;
2041b5a3613cSMatthew G. Knepley     PetscClassId id;
2042b5a3613cSMatthew G. Knepley 
2043b5a3613cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
2044b5a3613cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2045338f77d5SMatthew G. Knepley     if (id == PETSCFV_CLASSID) useFVM = PETSC_TRUE;
2046338f77d5SMatthew G. Knepley   }
2047338f77d5SMatthew G. Knepley   /* Get local solution with boundary values */
2048338f77d5SMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
2049338f77d5SMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
2050338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
2051338f77d5SMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
2052338f77d5SMatthew G. Knepley   /* Read DS information */
2053338f77d5SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
2054338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
2055338f77d5SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
2056c330f8ffSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF,numCells,cStart,1,&cellIS);CHKERRQ(ierr);
2057338f77d5SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
2058338f77d5SMatthew G. Knepley   /* Read Auxiliary DS information */
2059ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, 0, &locA);CHKERRQ(ierr);
20609a2a23afSMatthew G. Knepley   if (locA) {
20619a2a23afSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2062338f77d5SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
2063338f77d5SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
206492fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
2065338f77d5SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
2066338f77d5SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
2067338f77d5SMatthew G. Knepley   }
2068338f77d5SMatthew G. Knepley   /* Allocate data  arrays */
2069338f77d5SMatthew G. Knepley   ierr = PetscCalloc1(numCells*totDim, &u);CHKERRQ(ierr);
2070338f77d5SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
2071338f77d5SMatthew G. Knepley   /* Read out geometry */
2072c330f8ffSToby Isaac   ierr = DMGetCoordinateField(dm,&coordField);CHKERRQ(ierr);
2073b7260050SToby Isaac   ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
2074b7260050SToby Isaac   if (maxDegree <= 1) {
2075c330f8ffSToby Isaac     ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
2076c330f8ffSToby Isaac     if (affineQuad) {
2077c330f8ffSToby Isaac       ierr = DMFieldCreateFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2078338f77d5SMatthew G. Knepley     }
2079b5a3613cSMatthew G. Knepley   }
2080b5a3613cSMatthew G. Knepley   if (useFVM) {
2081338f77d5SMatthew G. Knepley     PetscFV   fv = NULL;
2082b5a3613cSMatthew G. Knepley     Vec       grad;
2083b5a3613cSMatthew G. Knepley     PetscInt  fStart, fEnd;
2084b5a3613cSMatthew G. Knepley     PetscBool compGrad;
2085b5a3613cSMatthew G. Knepley 
2086338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2087338f77d5SMatthew G. Knepley       PetscObject  obj;
2088338f77d5SMatthew G. Knepley       PetscClassId id;
2089338f77d5SMatthew G. Knepley 
2090338f77d5SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
2091338f77d5SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2092338f77d5SMatthew G. Knepley       if (id == PETSCFV_CLASSID) {fv = (PetscFV) obj; break;}
2093338f77d5SMatthew G. Knepley     }
2094338f77d5SMatthew G. Knepley     ierr = PetscFVGetComputeGradients(fv, &compGrad);CHKERRQ(ierr);
2095338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, PETSC_TRUE);CHKERRQ(ierr);
2096b5a3613cSMatthew G. Knepley     ierr = DMPlexComputeGeometryFVM(dm, &cellGeometryFVM, &faceGeometryFVM);CHKERRQ(ierr);
2097338f77d5SMatthew G. Knepley     ierr = DMPlexComputeGradientFVM(dm, fv, faceGeometryFVM, cellGeometryFVM, &dmGrad);CHKERRQ(ierr);
2098338f77d5SMatthew G. Knepley     ierr = PetscFVSetComputeGradients(fv, compGrad);CHKERRQ(ierr);
2099b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
2100b5a3613cSMatthew G. Knepley     /* Reconstruct and limit cell gradients */
2101b5a3613cSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
2102b5a3613cSMatthew G. Knepley     ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
2103338f77d5SMatthew G. Knepley     ierr = DMPlexReconstructGradients_Internal(dm, fv, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
2104b5a3613cSMatthew G. Knepley     /* Communicate gradient values */
2105b5a3613cSMatthew G. Knepley     ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
2106b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
2107b5a3613cSMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
2108b5a3613cSMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
2109b5a3613cSMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
2110338f77d5SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, 0.0, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
2111b5a3613cSMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
2112b5a3613cSMatthew G. Knepley   }
2113338f77d5SMatthew G. Knepley   /* Read out data from inputs */
211473d901b8SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
211573d901b8SMatthew G. Knepley     PetscScalar *x = NULL;
211673d901b8SMatthew G. Knepley     PetscInt     i;
211773d901b8SMatthew G. Knepley 
2118338f77d5SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
21190f2d7e86SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
2120338f77d5SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, locX, c, NULL, &x);CHKERRQ(ierr);
212173d901b8SMatthew G. Knepley     if (dmAux) {
2122338f77d5SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
21230f2d7e86SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
2124338f77d5SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, locA, c, NULL, &x);CHKERRQ(ierr);
212573d901b8SMatthew G. Knepley     }
212673d901b8SMatthew G. Knepley   }
2127338f77d5SMatthew G. Knepley   /* Do integration for each field */
212873d901b8SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2129c1f031eeSMatthew G. Knepley     PetscObject  obj;
2130c1f031eeSMatthew G. Knepley     PetscClassId id;
2131c1f031eeSMatthew G. Knepley     PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
213273d901b8SMatthew G. Knepley 
2133c1f031eeSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
2134c1f031eeSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2135c1f031eeSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
2136c1f031eeSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
2137c1f031eeSMatthew G. Knepley       PetscQuadrature q;
2138c330f8ffSToby Isaac       PetscFEGeom     *chunkGeom = NULL;
2139c1f031eeSMatthew G. Knepley       PetscInt        Nq, Nb;
2140c1f031eeSMatthew G. Knepley 
21410f2d7e86SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
2142c1f031eeSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
21439c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
2144c1f031eeSMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
2145c1f031eeSMatthew G. Knepley       blockSize = Nb*Nq;
214673d901b8SMatthew G. Knepley       batchSize = numBlocks * blockSize;
21470f2d7e86SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
214873d901b8SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
214973d901b8SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
215073d901b8SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
215173d901b8SMatthew G. Knepley       offset    = numCells - Nr;
2152c330f8ffSToby Isaac       if (!affineQuad) {
2153c330f8ffSToby Isaac         ierr = DMFieldCreateFEGeom(coordField,cellIS,q,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
2154c330f8ffSToby Isaac       }
2155c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
21564bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Ne, chunkGeom, u, probAux, a, cintegral);CHKERRQ(ierr);
2157c330f8ffSToby Isaac       ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
21584bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrate(prob, f, Nr, chunkGeom, &u[offset*totDim], probAux, &a[offset*totDimAux], &cintegral[offset*Nf]);CHKERRQ(ierr);
2159c330f8ffSToby Isaac       ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&chunkGeom);CHKERRQ(ierr);
2160c330f8ffSToby Isaac       if (!affineQuad) {
2161c330f8ffSToby Isaac         ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
2162c330f8ffSToby Isaac       }
2163c1f031eeSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
2164c1f031eeSMatthew G. Knepley       PetscInt       foff;
2165420e96edSMatthew G. Knepley       PetscPointFunc obj_func;
2166b69edc29SMatthew G. Knepley       PetscScalar    lint;
2167c1f031eeSMatthew G. Knepley 
2168c1f031eeSMatthew G. Knepley       ierr = PetscDSGetObjective(prob, f, &obj_func);CHKERRQ(ierr);
2169c1f031eeSMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, f, &foff);CHKERRQ(ierr);
2170c1f031eeSMatthew G. Knepley       if (obj_func) {
2171c1f031eeSMatthew G. Knepley         for (c = 0; c < numCells; ++c) {
2172b5a3613cSMatthew G. Knepley           PetscScalar *u_x;
2173b5a3613cSMatthew G. Knepley 
2174b5a3613cSMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, c, lgrad, &u_x);CHKERRQ(ierr);
2175338f77d5SMatthew 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);
2176338f77d5SMatthew G. Knepley           cintegral[c*Nf+f] += PetscRealPart(lint)*cgeomFVM[c].volume;
217773d901b8SMatthew G. Knepley         }
2178c1f031eeSMatthew G. Knepley       }
217998921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
2180c1f031eeSMatthew G. Knepley   }
2181338f77d5SMatthew G. Knepley   /* Cleanup data arrays */
2182b5a3613cSMatthew G. Knepley   if (useFVM) {
2183b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
2184b5a3613cSMatthew G. Knepley     ierr = VecRestoreArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
2185b5a3613cSMatthew G. Knepley     ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
2186b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&faceGeometryFVM);CHKERRQ(ierr);
2187b5a3613cSMatthew G. Knepley     ierr = VecDestroy(&cellGeometryFVM);CHKERRQ(ierr);
2188b5a3613cSMatthew G. Knepley     ierr = DMDestroy(&dmGrad);CHKERRQ(ierr);
2189b5a3613cSMatthew G. Knepley   }
219073d901b8SMatthew G. Knepley   if (dmAux) {ierr = PetscFree(a);CHKERRQ(ierr);}
2191338f77d5SMatthew G. Knepley   ierr = PetscFree(u);CHKERRQ(ierr);
2192338f77d5SMatthew G. Knepley   /* Cleanup */
2193f99c8401SToby Isaac   if (affineQuad) {
2194f99c8401SToby Isaac     ierr = PetscFEGeomDestroy(&cgeomFEM);CHKERRQ(ierr);
2195f99c8401SToby Isaac   }
2196f99c8401SToby Isaac   ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
2197c330f8ffSToby Isaac   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
2198338f77d5SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
2199338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
2200338f77d5SMatthew G. Knepley }
2201338f77d5SMatthew G. Knepley 
2202338f77d5SMatthew G. Knepley /*@
2203338f77d5SMatthew G. Knepley   DMPlexComputeIntegralFEM - Form the integral over the domain from the global input X using pointwise functions specified by the user
2204338f77d5SMatthew G. Knepley 
2205338f77d5SMatthew G. Knepley   Input Parameters:
2206338f77d5SMatthew G. Knepley + dm - The mesh
2207338f77d5SMatthew G. Knepley . X  - Global input vector
2208338f77d5SMatthew G. Knepley - user - The user context
2209338f77d5SMatthew G. Knepley 
2210338f77d5SMatthew G. Knepley   Output Parameter:
2211338f77d5SMatthew G. Knepley . integral - Integral for each field
2212338f77d5SMatthew G. Knepley 
2213338f77d5SMatthew G. Knepley   Level: developer
2214338f77d5SMatthew G. Knepley 
2215446c23c1SPierre Jolivet .seealso: DMPlexSNESComputeResidualFEM()
2216338f77d5SMatthew G. Knepley @*/
2217b8feb594SMatthew G. Knepley PetscErrorCode DMPlexComputeIntegralFEM(DM dm, Vec X, PetscScalar *integral, void *user)
2218338f77d5SMatthew G. Knepley {
2219338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
2220b8feb594SMatthew G. Knepley   PetscScalar   *cintegral, *lintegral;
2221412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
2222338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
2223338f77d5SMatthew G. Knepley 
2224338f77d5SMatthew G. Knepley   PetscFunctionBegin;
2225338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2226338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
2227338f77d5SMatthew G. Knepley   PetscValidPointer(integral, 3);
2228338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2229338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2230338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
2231412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2232338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
2233338f77d5SMatthew G. Knepley   ierr = PetscCalloc2(Nf, &lintegral, (cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
2234338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
2235338f77d5SMatthew G. Knepley   /* Sum up values */
2236338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
2237338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2238338f77d5SMatthew G. Knepley 
2239338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2240b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) lintegral[f] += cintegral[c*Nf+f];
2241338f77d5SMatthew G. Knepley   }
2242820f2d46SBarry Smith   ierr = MPIU_Allreduce(lintegral, integral, Nf, MPIU_SCALAR, MPIU_SUM, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
224373d901b8SMatthew G. Knepley   if (mesh->printFEM) {
2244338f77d5SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "Integral:");CHKERRQ(ierr);
2245b8feb594SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), " %g", (double) PetscRealPart(integral[f]));CHKERRQ(ierr);}
224673d901b8SMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "\n");CHKERRQ(ierr);
224773d901b8SMatthew G. Knepley   }
2248338f77d5SMatthew G. Knepley   ierr = PetscFree2(lintegral, cintegral);CHKERRQ(ierr);
2249338f77d5SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2250338f77d5SMatthew G. Knepley   PetscFunctionReturn(0);
2251338f77d5SMatthew G. Knepley }
2252338f77d5SMatthew G. Knepley 
2253338f77d5SMatthew G. Knepley /*@
2254338f77d5SMatthew G. Knepley   DMPlexComputeCellwiseIntegralFEM - Form the vector of cellwise integrals F from the global input X using pointwise functions specified by the user
2255338f77d5SMatthew G. Knepley 
2256338f77d5SMatthew G. Knepley   Input Parameters:
2257338f77d5SMatthew G. Knepley + dm - The mesh
2258338f77d5SMatthew G. Knepley . X  - Global input vector
2259338f77d5SMatthew G. Knepley - user - The user context
2260338f77d5SMatthew G. Knepley 
2261338f77d5SMatthew G. Knepley   Output Parameter:
2262338f77d5SMatthew G. Knepley . integral - Cellwise integrals for each field
2263338f77d5SMatthew G. Knepley 
2264338f77d5SMatthew G. Knepley   Level: developer
2265338f77d5SMatthew G. Knepley 
2266446c23c1SPierre Jolivet .seealso: DMPlexSNESComputeResidualFEM()
2267338f77d5SMatthew G. Knepley @*/
2268338f77d5SMatthew G. Knepley PetscErrorCode DMPlexComputeCellwiseIntegralFEM(DM dm, Vec X, Vec F, void *user)
2269338f77d5SMatthew G. Knepley {
2270338f77d5SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
2271338f77d5SMatthew G. Knepley   DM             dmF;
2272338f77d5SMatthew G. Knepley   PetscSection   sectionF;
2273338f77d5SMatthew G. Knepley   PetscScalar   *cintegral, *af;
2274412e9a14SMatthew G. Knepley   PetscInt       Nf, f, cellHeight, cStart, cEnd, cell;
2275338f77d5SMatthew G. Knepley   PetscErrorCode ierr;
2276338f77d5SMatthew G. Knepley 
2277338f77d5SMatthew G. Knepley   PetscFunctionBegin;
2278338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2279338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
2280338f77d5SMatthew G. Knepley   PetscValidHeaderSpecific(F, VEC_CLASSID, 3);
2281338f77d5SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
2282338f77d5SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2283338f77d5SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
2284412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
2285338f77d5SMatthew G. Knepley   /* TODO Introduce a loop over large chunks (right now this is a single chunk) */
2286338f77d5SMatthew G. Knepley   ierr = PetscCalloc1((cEnd-cStart)*Nf, &cintegral);CHKERRQ(ierr);
2287338f77d5SMatthew G. Knepley   ierr = DMPlexComputeIntegral_Internal(dm, X, cStart, cEnd, cintegral, user);CHKERRQ(ierr);
2288338f77d5SMatthew G. Knepley   /* Put values in F*/
2289338f77d5SMatthew G. Knepley   ierr = VecGetDM(F, &dmF);CHKERRQ(ierr);
229092fd8e1eSJed Brown   ierr = DMGetLocalSection(dmF, &sectionF);CHKERRQ(ierr);
2291338f77d5SMatthew G. Knepley   ierr = VecGetArray(F, &af);CHKERRQ(ierr);
2292338f77d5SMatthew G. Knepley   for (cell = cStart; cell < cEnd; ++cell) {
2293338f77d5SMatthew G. Knepley     const PetscInt c = cell - cStart;
2294338f77d5SMatthew G. Knepley     PetscInt       dof, off;
2295338f77d5SMatthew G. Knepley 
2296338f77d5SMatthew G. Knepley     if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, "Cell Integral", Nf, &cintegral[c*Nf]);CHKERRQ(ierr);}
2297338f77d5SMatthew G. Knepley     ierr = PetscSectionGetDof(sectionF, cell, &dof);CHKERRQ(ierr);
2298338f77d5SMatthew G. Knepley     ierr = PetscSectionGetOffset(sectionF, cell, &off);CHKERRQ(ierr);
22992c71b3e2SJacob Faibussowitsch     PetscCheckFalse(dof != Nf,PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of cell dofs %D != %D", dof, Nf);
2300338f77d5SMatthew G. Knepley     for (f = 0; f < Nf; ++f) af[off+f] = cintegral[c*Nf+f];
2301338f77d5SMatthew G. Knepley   }
2302338f77d5SMatthew G. Knepley   ierr = VecRestoreArray(F, &af);CHKERRQ(ierr);
2303338f77d5SMatthew G. Knepley   ierr = PetscFree(cintegral);CHKERRQ(ierr);
2304c1f031eeSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
230573d901b8SMatthew G. Knepley   PetscFunctionReturn(0);
230673d901b8SMatthew G. Knepley }
230773d901b8SMatthew G. Knepley 
23089b6f715bSMatthew G. Knepley static PetscErrorCode DMPlexComputeBdIntegral_Internal(DM dm, Vec locX, IS pointIS,
23099b6f715bSMatthew G. Knepley                                                        void (*func)(PetscInt, PetscInt, PetscInt,
231064c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
231164c72086SMatthew G. Knepley                                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
231264c72086SMatthew G. Knepley                                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
231364c72086SMatthew G. Knepley                                                        PetscScalar *fintegral, void *user)
231464c72086SMatthew G. Knepley {
23159b6f715bSMatthew G. Knepley   DM                 plex = NULL, plexA = NULL;
2316a6e0b375SMatthew G. Knepley   DMEnclosureType    encAux;
23179b6f715bSMatthew G. Knepley   PetscDS            prob, probAux = NULL;
23189b6f715bSMatthew G. Knepley   PetscSection       section, sectionAux = NULL;
23199b6f715bSMatthew G. Knepley   Vec                locA = NULL;
23209b6f715bSMatthew G. Knepley   DMField            coordField;
23219b6f715bSMatthew G. Knepley   PetscInt           Nf,        totDim,        *uOff, *uOff_x;
23229b6f715bSMatthew G. Knepley   PetscInt           NfAux = 0, totDimAux = 0, *aOff = NULL;
23239b6f715bSMatthew G. Knepley   PetscScalar       *u, *a = NULL;
232464c72086SMatthew G. Knepley   const PetscScalar *constants;
23259b6f715bSMatthew G. Knepley   PetscInt           numConstants, f;
232664c72086SMatthew G. Knepley   PetscErrorCode     ierr;
232764c72086SMatthew G. Knepley 
232864c72086SMatthew G. Knepley   PetscFunctionBegin;
23299b6f715bSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
23309b6f715bSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
233164c72086SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
233292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
233364c72086SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
233464c72086SMatthew G. Knepley   /* Determine which discretizations we have */
23359b6f715bSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
233664c72086SMatthew G. Knepley     PetscObject  obj;
233764c72086SMatthew G. Knepley     PetscClassId id;
233864c72086SMatthew G. Knepley 
23399b6f715bSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
234064c72086SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
23412c71b3e2SJacob Faibussowitsch     PetscCheckFalse(id == PETSCFV_CLASSID,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Not supported for FVM (field %D)", f);
234264c72086SMatthew G. Knepley   }
234364c72086SMatthew G. Knepley   /* Read DS information */
234464c72086SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
234564c72086SMatthew G. Knepley   ierr = PetscDSGetComponentOffsets(prob, &uOff);CHKERRQ(ierr);
234664c72086SMatthew G. Knepley   ierr = PetscDSGetComponentDerivativeOffsets(prob, &uOff_x);CHKERRQ(ierr);
234764c72086SMatthew G. Knepley   ierr = PetscDSGetConstants(prob, &numConstants, &constants);CHKERRQ(ierr);
234864c72086SMatthew G. Knepley   /* Read Auxiliary DS information */
2349ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, 0, &locA);CHKERRQ(ierr);
23509b6f715bSMatthew G. Knepley   if (locA) {
23519b6f715bSMatthew G. Knepley     DM dmAux;
23529b6f715bSMatthew G. Knepley 
23539b6f715bSMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
2354a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
23559b6f715bSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
235664c72086SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
235764c72086SMatthew G. Knepley     ierr = PetscDSGetNumFields(probAux, &NfAux);CHKERRQ(ierr);
235892fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
235964c72086SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
236064c72086SMatthew G. Knepley     ierr = PetscDSGetComponentOffsets(probAux, &aOff);CHKERRQ(ierr);
236164c72086SMatthew G. Knepley   }
23629b6f715bSMatthew G. Knepley   /* Integrate over points */
23639b6f715bSMatthew G. Knepley   {
23649b6f715bSMatthew G. Knepley     PetscFEGeom    *fgeom, *chunkGeom = NULL;
2365b7260050SToby Isaac     PetscInt        maxDegree;
23669b6f715bSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
23679b6f715bSMatthew G. Knepley     const PetscInt *points;
23689b6f715bSMatthew G. Knepley     PetscInt        numFaces, face, Nq, field;
23699b6f715bSMatthew G. Knepley     PetscInt        numChunks, chunkSize, chunk, Nr, offset;
237064c72086SMatthew G. Knepley 
23719b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
23729b6f715bSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
23739b6f715bSMatthew G. Knepley     ierr = PetscCalloc2(numFaces*totDim, &u, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
2374b7260050SToby Isaac     ierr = DMFieldGetDegree(coordField, pointIS, NULL, &maxDegree);CHKERRQ(ierr);
237564c72086SMatthew G. Knepley     for (field = 0; field < Nf; ++field) {
237664c72086SMatthew G. Knepley       PetscFE fe;
237764c72086SMatthew G. Knepley 
237864c72086SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fe);CHKERRQ(ierr);
2379b7260050SToby Isaac       if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, pointIS, &qGeom);CHKERRQ(ierr);}
23809b6f715bSMatthew G. Knepley       if (!qGeom) {
23819b6f715bSMatthew G. Knepley         ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
23829b6f715bSMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
23839b6f715bSMatthew G. Knepley       }
23849b6f715bSMatthew G. Knepley       ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
23859b6f715bSMatthew G. Knepley       ierr = DMPlexGetFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
23869b6f715bSMatthew G. Knepley       for (face = 0; face < numFaces; ++face) {
2387f15274beSMatthew Knepley         const PetscInt point = points[face], *support;
23889b6f715bSMatthew G. Knepley         PetscScalar    *x    = NULL;
2389f15274beSMatthew Knepley         PetscInt       i;
23909b6f715bSMatthew G. Knepley 
23919b6f715bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
23929b6f715bSMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
23939b6f715bSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
23949b6f715bSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
23959b6f715bSMatthew G. Knepley         if (locA) {
23969b6f715bSMatthew G. Knepley           PetscInt subp;
2397a6e0b375SMatthew G. Knepley           ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
23989b6f715bSMatthew G. Knepley           ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
23999b6f715bSMatthew G. Knepley           for (i = 0; i < totDimAux; ++i) a[f*totDimAux+i] = x[i];
24009b6f715bSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
24019b6f715bSMatthew G. Knepley         }
24029b6f715bSMatthew G. Knepley       }
24039b6f715bSMatthew G. Knepley       /* Get blocking */
24049b6f715bSMatthew G. Knepley       {
24059b6f715bSMatthew G. Knepley         PetscQuadrature q;
24069b6f715bSMatthew G. Knepley         PetscInt        numBatches, batchSize, numBlocks, blockSize;
24079b6f715bSMatthew G. Knepley         PetscInt        Nq, Nb;
24089b6f715bSMatthew G. Knepley 
240964c72086SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
241064c72086SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
241164c72086SMatthew G. Knepley         ierr = PetscQuadratureGetData(q, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
241264c72086SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
241364c72086SMatthew G. Knepley         blockSize = Nb*Nq;
241464c72086SMatthew G. Knepley         batchSize = numBlocks * blockSize;
24159b6f715bSMatthew G. Knepley         chunkSize = numBatches*batchSize;
241664c72086SMatthew G. Knepley         ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
24179b6f715bSMatthew G. Knepley         numChunks = numFaces / chunkSize;
24189b6f715bSMatthew G. Knepley         Nr        = numFaces % chunkSize;
241964c72086SMatthew G. Knepley         offset    = numFaces - Nr;
242064c72086SMatthew G. Knepley       }
24219b6f715bSMatthew G. Knepley       /* Do integration for each field */
24229b6f715bSMatthew G. Knepley       for (chunk = 0; chunk < numChunks; ++chunk) {
24239b6f715bSMatthew G. Knepley         ierr = PetscFEGeomGetChunk(fgeom, chunk*chunkSize, (chunk+1)*chunkSize, &chunkGeom);CHKERRQ(ierr);
24244bee2e38SMatthew G. Knepley         ierr = PetscFEIntegrateBd(prob, field, func, chunkSize, chunkGeom, u, probAux, a, fintegral);CHKERRQ(ierr);
24259b6f715bSMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
242664c72086SMatthew G. Knepley       }
24279b6f715bSMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
24284bee2e38SMatthew G. Knepley       ierr = PetscFEIntegrateBd(prob, field, func, Nr, chunkGeom, &u[offset*totDim], probAux, a ? &a[offset*totDimAux] : NULL, &fintegral[offset*Nf]);CHKERRQ(ierr);
24299b6f715bSMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, offset, numFaces, &chunkGeom);CHKERRQ(ierr);
243064c72086SMatthew G. Knepley       /* Cleanup data arrays */
24319b6f715bSMatthew G. Knepley       ierr = DMPlexRestoreFEGeom(coordField, pointIS, qGeom, PETSC_TRUE, &fgeom);CHKERRQ(ierr);
24329b6f715bSMatthew G. Knepley       ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
24339b6f715bSMatthew G. Knepley       ierr = PetscFree2(u, a);CHKERRQ(ierr);
24349b6f715bSMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
243564c72086SMatthew G. Knepley     }
243664c72086SMatthew G. Knepley   }
24379b6f715bSMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
24389b6f715bSMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
24399b6f715bSMatthew G. Knepley   PetscFunctionReturn(0);
24409b6f715bSMatthew G. Knepley }
24419b6f715bSMatthew G. Knepley 
24429b6f715bSMatthew G. Knepley /*@
24439b6f715bSMatthew G. Knepley   DMPlexComputeBdIntegral - Form the integral over the specified boundary from the global input X using pointwise functions specified by the user
24449b6f715bSMatthew G. Knepley 
24459b6f715bSMatthew G. Knepley   Input Parameters:
24469b6f715bSMatthew G. Knepley + dm      - The mesh
24479b6f715bSMatthew G. Knepley . X       - Global input vector
24489b6f715bSMatthew G. Knepley . label   - The boundary DMLabel
24499b6f715bSMatthew G. Knepley . numVals - The number of label values to use, or PETSC_DETERMINE for all values
24509b6f715bSMatthew G. Knepley . vals    - The label values to use, or PETSC_NULL for all values
245167b8a455SSatish Balay . func    - The function to integrate along the boundary
24529b6f715bSMatthew G. Knepley - user    - The user context
24539b6f715bSMatthew G. Knepley 
24549b6f715bSMatthew G. Knepley   Output Parameter:
24559b6f715bSMatthew G. Knepley . integral - Integral for each field
24569b6f715bSMatthew G. Knepley 
24579b6f715bSMatthew G. Knepley   Level: developer
24589b6f715bSMatthew G. Knepley 
24599b6f715bSMatthew G. Knepley .seealso: DMPlexComputeIntegralFEM(), DMPlexComputeBdResidualFEM()
24609b6f715bSMatthew G. Knepley @*/
24619b6f715bSMatthew G. Knepley PetscErrorCode DMPlexComputeBdIntegral(DM dm, Vec X, DMLabel label, PetscInt numVals, const PetscInt vals[],
24629b6f715bSMatthew G. Knepley                                        void (*func)(PetscInt, PetscInt, PetscInt,
24639b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
24649b6f715bSMatthew G. Knepley                                                     const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
24659b6f715bSMatthew G. Knepley                                                     PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
24669b6f715bSMatthew G. Knepley                                        PetscScalar *integral, void *user)
24679b6f715bSMatthew G. Knepley {
24689b6f715bSMatthew G. Knepley   Vec            locX;
24699b6f715bSMatthew G. Knepley   PetscSection   section;
24709b6f715bSMatthew G. Knepley   DMLabel        depthLabel;
24719b6f715bSMatthew G. Knepley   IS             facetIS;
24729b6f715bSMatthew G. Knepley   PetscInt       dim, Nf, f, v;
24739b6f715bSMatthew G. Knepley   PetscErrorCode ierr;
24749b6f715bSMatthew G. Knepley 
24759b6f715bSMatthew G. Knepley   PetscFunctionBegin;
24769b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24779b6f715bSMatthew G. Knepley   PetscValidHeaderSpecific(X, VEC_CLASSID, 2);
24789b6f715bSMatthew G. Knepley   PetscValidPointer(label, 3);
24799b6f715bSMatthew G. Knepley   if (vals) PetscValidPointer(vals, 5);
2480064a246eSJacob Faibussowitsch   PetscValidPointer(integral, 7);
24819b6f715bSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
24829b6f715bSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
24839b6f715bSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
24849b6f715bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
248592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
24869b6f715bSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
24879b6f715bSMatthew G. Knepley   /* Get local solution with boundary values */
24889b6f715bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &locX);CHKERRQ(ierr);
24899b6f715bSMatthew G. Knepley   ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locX, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);
24909b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalBegin(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
24919b6f715bSMatthew G. Knepley   ierr = DMGlobalToLocalEnd(dm, X, INSERT_VALUES, locX);CHKERRQ(ierr);
24929b6f715bSMatthew G. Knepley   /* Loop over label values */
2493580bdb30SBarry Smith   ierr = PetscArrayzero(integral, Nf);CHKERRQ(ierr);
24949b6f715bSMatthew G. Knepley   for (v = 0; v < numVals; ++v) {
24959b6f715bSMatthew G. Knepley     IS           pointIS;
24969b6f715bSMatthew G. Knepley     PetscInt     numFaces, face;
24979b6f715bSMatthew G. Knepley     PetscScalar *fintegral;
24989b6f715bSMatthew G. Knepley 
24999b6f715bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, vals[v], &pointIS);CHKERRQ(ierr);
25009b6f715bSMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
25019b6f715bSMatthew G. Knepley     {
25029b6f715bSMatthew G. Knepley       IS isectIS;
25039b6f715bSMatthew G. Knepley 
25049b6f715bSMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
25059b6f715bSMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS, pointIS, &isectIS);CHKERRQ(ierr);
25069b6f715bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
25079b6f715bSMatthew G. Knepley       pointIS = isectIS;
25089b6f715bSMatthew G. Knepley     }
25099b6f715bSMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
25109b6f715bSMatthew G. Knepley     ierr = PetscCalloc1(numFaces*Nf, &fintegral);CHKERRQ(ierr);
25119b6f715bSMatthew G. Knepley     ierr = DMPlexComputeBdIntegral_Internal(dm, locX, pointIS, func, fintegral, user);CHKERRQ(ierr);
25129b6f715bSMatthew G. Knepley     /* Sum point contributions into integral */
25139b6f715bSMatthew G. Knepley     for (f = 0; f < Nf; ++f) for (face = 0; face < numFaces; ++face) integral[f] += fintegral[face*Nf+f];
25149b6f715bSMatthew G. Knepley     ierr = PetscFree(fintegral);CHKERRQ(ierr);
25159b6f715bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
25169b6f715bSMatthew G. Knepley   }
251764c72086SMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &locX);CHKERRQ(ierr);
25189b6f715bSMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
25199b6f715bSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_IntegralFEM,dm,0,0,0);CHKERRQ(ierr);
252064c72086SMatthew G. Knepley   PetscFunctionReturn(0);
252164c72086SMatthew G. Knepley }
252264c72086SMatthew G. Knepley 
2523d69c5d34SMatthew G. Knepley /*@
2524cf51de39SMatthew G. Knepley   DMPlexComputeInterpolatorNested - Form the local portion of the interpolation matrix I from the coarse DM to a uniformly refined DM.
2525d69c5d34SMatthew G. Knepley 
2526d69c5d34SMatthew G. Knepley   Input Parameters:
2527cf51de39SMatthew G. Knepley + dmc  - The coarse mesh
2528cf51de39SMatthew G. Knepley . dmf  - The fine mesh
2529cf51de39SMatthew G. Knepley . isRefined - Flag indicating regular refinement, rather than the same topology
2530d69c5d34SMatthew G. Knepley - user - The user context
2531d69c5d34SMatthew G. Knepley 
2532d69c5d34SMatthew G. Knepley   Output Parameter:
2533934789fcSMatthew G. Knepley . In  - The interpolation matrix
2534d69c5d34SMatthew G. Knepley 
2535d69c5d34SMatthew G. Knepley   Level: developer
2536d69c5d34SMatthew G. Knepley 
253768132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
2538d69c5d34SMatthew G. Knepley @*/
2539cf51de39SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorNested(DM dmc, DM dmf, PetscBool isRefined, Mat In, void *user)
2540d69c5d34SMatthew G. Knepley {
2541d69c5d34SMatthew G. Knepley   DM_Plex          *mesh  = (DM_Plex *) dmc->data;
2542d69c5d34SMatthew G. Knepley   const char       *name  = "Interpolator";
2543d69c5d34SMatthew G. Knepley   PetscFE          *feRef;
254497c42addSMatthew G. Knepley   PetscFV          *fvRef;
2545d69c5d34SMatthew G. Knepley   PetscSection      fsection, fglobalSection;
2546d69c5d34SMatthew G. Knepley   PetscSection      csection, cglobalSection;
2547d69c5d34SMatthew G. Knepley   PetscScalar      *elemMat;
2548485ad865SMatthew G. Knepley   PetscInt          dim, Nf, f, fieldI, fieldJ, offsetI, offsetJ, cStart, cEnd, c;
25492ea9c922SToby Isaac   PetscInt          cTotDim=0, rTotDim = 0;
2550d69c5d34SMatthew G. Knepley   PetscErrorCode    ierr;
2551d69c5d34SMatthew G. Knepley 
2552d69c5d34SMatthew G. Knepley   PetscFunctionBegin;
2553d69c5d34SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2554c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
255592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2556e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
255792fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2558e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
2559d69c5d34SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
2560412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
256197c42addSMatthew G. Knepley   ierr = PetscCalloc2(Nf, &feRef, Nf, &fvRef);CHKERRQ(ierr);
2562d69c5d34SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
25632ea9c922SToby Isaac     PetscObject  obj, objc;
25642ea9c922SToby Isaac     PetscClassId id, idc;
25652ea9c922SToby Isaac     PetscInt     rNb = 0, Nc = 0, cNb = 0;
2566d69c5d34SMatthew G. Knepley 
25672ea9c922SToby Isaac     ierr = DMGetField(dmf, f, NULL, &obj);CHKERRQ(ierr);
256897c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
256997c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
257097c42addSMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
257197c42addSMatthew G. Knepley 
2572cf51de39SMatthew G. Knepley       if (isRefined) {
25730f2d7e86SMatthew G. Knepley         ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
2574cf51de39SMatthew G. Knepley       } else {
2575cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fe);CHKERRQ(ierr);
2576cf51de39SMatthew G. Knepley         feRef[f] = fe;
2577cf51de39SMatthew G. Knepley       }
2578d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &rNb);CHKERRQ(ierr);
25790f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
258097c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
258197c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
258297c42addSMatthew G. Knepley       PetscDualSpace Q;
258397c42addSMatthew G. Knepley 
2584cf51de39SMatthew G. Knepley       if (isRefined) {
258597c42addSMatthew G. Knepley         ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
2586cf51de39SMatthew G. Knepley       } else {
2587cf51de39SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) fv);CHKERRQ(ierr);
2588cf51de39SMatthew G. Knepley         fvRef[f] = fv;
2589cf51de39SMatthew G. Knepley       }
259097c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
259197c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &rNb);CHKERRQ(ierr);
25922ea9c922SToby Isaac       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
259397c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
259497c42addSMatthew G. Knepley     }
25952ea9c922SToby Isaac     ierr = DMGetField(dmc, f, NULL, &objc);CHKERRQ(ierr);
25962ea9c922SToby Isaac     ierr = PetscObjectGetClassId(objc, &idc);CHKERRQ(ierr);
25972ea9c922SToby Isaac     if (idc == PETSCFE_CLASSID) {
25982ea9c922SToby Isaac       PetscFE fe = (PetscFE) objc;
25992ea9c922SToby Isaac 
26002ea9c922SToby Isaac       ierr = PetscFEGetDimension(fe, &cNb);CHKERRQ(ierr);
26012ea9c922SToby Isaac     } else if (id == PETSCFV_CLASSID) {
26022ea9c922SToby Isaac       PetscFV        fv = (PetscFV) obj;
26032ea9c922SToby Isaac       PetscDualSpace Q;
26042ea9c922SToby Isaac 
26052ea9c922SToby Isaac       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
26062ea9c922SToby Isaac       ierr = PetscDualSpaceGetDimension(Q, &cNb);CHKERRQ(ierr);
2607d69c5d34SMatthew G. Knepley     }
26082ea9c922SToby Isaac     rTotDim += rNb;
26092ea9c922SToby Isaac     cTotDim += cNb;
26102ea9c922SToby Isaac   }
26110f2d7e86SMatthew G. Knepley   ierr = PetscMalloc1(rTotDim*cTotDim,&elemMat);CHKERRQ(ierr);
2612580bdb30SBarry Smith   ierr = PetscArrayzero(elemMat, rTotDim*cTotDim);CHKERRQ(ierr);
2613d69c5d34SMatthew G. Knepley   for (fieldI = 0, offsetI = 0; fieldI < Nf; ++fieldI) {
2614d69c5d34SMatthew G. Knepley     PetscDualSpace   Qref;
2615d69c5d34SMatthew G. Knepley     PetscQuadrature  f;
2616d69c5d34SMatthew G. Knepley     const PetscReal *qpoints, *qweights;
2617d69c5d34SMatthew G. Knepley     PetscReal       *points;
2618d69c5d34SMatthew G. Knepley     PetscInt         npoints = 0, Nc, Np, fpdim, i, k, p, d;
2619d69c5d34SMatthew G. Knepley 
2620d69c5d34SMatthew G. Knepley     /* Compose points from all dual basis functionals */
262197c42addSMatthew G. Knepley     if (feRef[fieldI]) {
2622d69c5d34SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[fieldI], &Qref);CHKERRQ(ierr);
26230f2d7e86SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[fieldI], &Nc);CHKERRQ(ierr);
262497c42addSMatthew G. Knepley     } else {
262597c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[fieldI], &Qref);CHKERRQ(ierr);
262697c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[fieldI], &Nc);CHKERRQ(ierr);
262797c42addSMatthew G. Knepley     }
2628d69c5d34SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Qref, &fpdim);CHKERRQ(ierr);
2629d69c5d34SMatthew G. Knepley     for (i = 0; i < fpdim; ++i) {
2630d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
26319c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, NULL, NULL);CHKERRQ(ierr);
2632d69c5d34SMatthew G. Knepley       npoints += Np;
2633d69c5d34SMatthew G. Knepley     }
2634d69c5d34SMatthew G. Knepley     ierr = PetscMalloc1(npoints*dim,&points);CHKERRQ(ierr);
2635d69c5d34SMatthew G. Knepley     for (i = 0, k = 0; i < fpdim; ++i) {
2636d69c5d34SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
26379c3cf19fSMatthew G. Knepley       ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
2638d69c5d34SMatthew G. Knepley       for (p = 0; p < Np; ++p, ++k) for (d = 0; d < dim; ++d) points[k*dim+d] = qpoints[p*dim+d];
2639d69c5d34SMatthew G. Knepley     }
2640d69c5d34SMatthew G. Knepley 
2641d69c5d34SMatthew G. Knepley     for (fieldJ = 0, offsetJ = 0; fieldJ < Nf; ++fieldJ) {
264297c42addSMatthew G. Knepley       PetscObject  obj;
264397c42addSMatthew G. Knepley       PetscClassId id;
26449c3cf19fSMatthew G. Knepley       PetscInt     NcJ = 0, cpdim = 0, j, qNc;
2645d69c5d34SMatthew G. Knepley 
26462ea9c922SToby Isaac       ierr = DMGetField(dmc, fieldJ, NULL, &obj);CHKERRQ(ierr);
264797c42addSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
264897c42addSMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
264997c42addSMatthew G. Knepley         PetscFE           fe = (PetscFE) obj;
2650ef0bb6c7SMatthew G. Knepley         PetscTabulation T  = NULL;
2651d69c5d34SMatthew G. Knepley 
2652d69c5d34SMatthew G. Knepley         /* Evaluate basis at points */
26530f2d7e86SMatthew G. Knepley         ierr = PetscFEGetNumComponents(fe, &NcJ);CHKERRQ(ierr);
26540f2d7e86SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2655ffe73a53SMatthew G. Knepley         /* For now, fields only interpolate themselves */
2656ffe73a53SMatthew G. Knepley         if (fieldI == fieldJ) {
26572c71b3e2SJacob Faibussowitsch           PetscCheckFalse(Nc != NcJ,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", Nc, NcJ);
2658ef0bb6c7SMatthew G. Knepley           ierr = PetscFECreateTabulation(fe, 1, npoints, points, 0, &T);CHKERRQ(ierr);
2659d69c5d34SMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
2660d69c5d34SMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
26619c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
26622c71b3e2SJacob Faibussowitsch             PetscCheckFalse(qNc != NcJ,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
2663d69c5d34SMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
266436a6d9c0SMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2665d172c84bSMatthew G. Knepley                 /*
2666d172c84bSMatthew G. Knepley                    cTotDim:            Total columns in element interpolation matrix, sum of number of dual basis functionals in each field
2667d172c84bSMatthew G. Knepley                    offsetI, offsetJ:   Offsets into the larger element interpolation matrix for different fields
2668d172c84bSMatthew G. Knepley                    fpdim, i, cpdim, j: Dofs for fine and coarse grids, correspond to dual space basis functionals
2669d172c84bSMatthew G. Knepley                    qNC, Nc, Ncj, c:    Number of components in this field
2670d172c84bSMatthew G. Knepley                    Np, p:              Number of quad points in the fine grid functional i
2671d172c84bSMatthew G. Knepley                    k:                  i*Np + p, overall point number for the interpolation
2672d172c84bSMatthew G. Knepley                 */
2673ef0bb6c7SMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += T->T[0][k*cpdim*NcJ+j*Nc+c]*qweights[p*qNc+c];
267436a6d9c0SMatthew G. Knepley               }
2675d69c5d34SMatthew G. Knepley             }
2676d69c5d34SMatthew G. Knepley           }
2677ef0bb6c7SMatthew G. Knepley           ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);CHKERRQ(ierr);
2678ffe73a53SMatthew G. Knepley         }
267997c42addSMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
268097c42addSMatthew G. Knepley         PetscFV        fv = (PetscFV) obj;
268197c42addSMatthew G. Knepley 
268297c42addSMatthew G. Knepley         /* Evaluate constant function at points */
268397c42addSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &NcJ);CHKERRQ(ierr);
268497c42addSMatthew G. Knepley         cpdim = 1;
268597c42addSMatthew G. Knepley         /* For now, fields only interpolate themselves */
268697c42addSMatthew G. Knepley         if (fieldI == fieldJ) {
26872c71b3e2SJacob Faibussowitsch           PetscCheckFalse(Nc != NcJ,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", Nc, NcJ);
268897c42addSMatthew G. Knepley           for (i = 0, k = 0; i < fpdim; ++i) {
268997c42addSMatthew G. Knepley             ierr = PetscDualSpaceGetFunctional(Qref, i, &f);CHKERRQ(ierr);
26909c3cf19fSMatthew G. Knepley             ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, NULL, &qweights);CHKERRQ(ierr);
26912c71b3e2SJacob Faibussowitsch             PetscCheckFalse(qNc != NcJ,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, NcJ);
269297c42addSMatthew G. Knepley             for (p = 0; p < Np; ++p, ++k) {
269397c42addSMatthew G. Knepley               for (j = 0; j < cpdim; ++j) {
2694458eb97cSMatthew G. Knepley                 for (c = 0; c < Nc; ++c) elemMat[(offsetI + i)*cTotDim + offsetJ + j] += 1.0*qweights[p*qNc+c];
269597c42addSMatthew G. Knepley               }
269697c42addSMatthew G. Knepley             }
269797c42addSMatthew G. Knepley           }
269897c42addSMatthew G. Knepley         }
269997c42addSMatthew G. Knepley       }
2700d172c84bSMatthew G. Knepley       offsetJ += cpdim;
2701d69c5d34SMatthew G. Knepley     }
2702d172c84bSMatthew G. Knepley     offsetI += fpdim;
2703549a8adaSMatthew G. Knepley     ierr = PetscFree(points);CHKERRQ(ierr);
2704d69c5d34SMatthew G. Knepley   }
27050f2d7e86SMatthew G. Knepley   if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(0, name, rTotDim, cTotDim, elemMat);CHKERRQ(ierr);}
27067f5b169aSMatthew G. Knepley   /* Preallocate matrix */
27077f5b169aSMatthew G. Knepley   {
2708c094ef40SMatthew G. Knepley     Mat          preallocator;
2709c094ef40SMatthew G. Knepley     PetscScalar *vals;
2710c094ef40SMatthew G. Knepley     PetscInt    *cellCIndices, *cellFIndices;
2711c094ef40SMatthew G. Knepley     PetscInt     locRows, locCols, cell;
27127f5b169aSMatthew G. Knepley 
2713c094ef40SMatthew G. Knepley     ierr = MatGetLocalSize(In, &locRows, &locCols);CHKERRQ(ierr);
2714c094ef40SMatthew G. Knepley     ierr = MatCreate(PetscObjectComm((PetscObject) In), &preallocator);CHKERRQ(ierr);
2715c094ef40SMatthew G. Knepley     ierr = MatSetType(preallocator, MATPREALLOCATOR);CHKERRQ(ierr);
2716c094ef40SMatthew G. Knepley     ierr = MatSetSizes(preallocator, locRows, locCols, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
2717c094ef40SMatthew G. Knepley     ierr = MatSetUp(preallocator);CHKERRQ(ierr);
2718c094ef40SMatthew G. Knepley     ierr = PetscCalloc3(rTotDim*cTotDim, &vals,cTotDim,&cellCIndices,rTotDim,&cellFIndices);CHKERRQ(ierr);
27197f5b169aSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
2720cf51de39SMatthew G. Knepley       if (isRefined) {
27217f5b169aSMatthew G. Knepley         ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, cell, cellCIndices, cellFIndices);CHKERRQ(ierr);
2722c094ef40SMatthew G. Knepley         ierr = MatSetValues(preallocator, rTotDim, cellFIndices, cTotDim, cellCIndices, vals, INSERT_VALUES);CHKERRQ(ierr);
2723cf51de39SMatthew G. Knepley       } else {
2724cf51de39SMatthew G. Knepley         ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, preallocator, cell, vals, INSERT_VALUES);CHKERRQ(ierr);
2725cf51de39SMatthew G. Knepley       }
27267f5b169aSMatthew G. Knepley     }
2727c094ef40SMatthew G. Knepley     ierr = PetscFree3(vals,cellCIndices,cellFIndices);CHKERRQ(ierr);
2728c094ef40SMatthew G. Knepley     ierr = MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2729c094ef40SMatthew G. Knepley     ierr = MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2730c094ef40SMatthew G. Knepley     ierr = MatPreallocatorPreallocate(preallocator, PETSC_TRUE, In);CHKERRQ(ierr);
2731c094ef40SMatthew G. Knepley     ierr = MatDestroy(&preallocator);CHKERRQ(ierr);
27327f5b169aSMatthew G. Knepley   }
27337f5b169aSMatthew G. Knepley   /* Fill matrix */
27347f5b169aSMatthew G. Knepley   ierr = MatZeroEntries(In);CHKERRQ(ierr);
2735d69c5d34SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2736cf51de39SMatthew G. Knepley     if (isRefined) {
2737934789fcSMatthew G. Knepley       ierr = DMPlexMatSetClosureRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2738cf51de39SMatthew G. Knepley     } else {
2739cf51de39SMatthew G. Knepley       ierr = DMPlexMatSetClosureGeneral(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, In, c, elemMat, INSERT_VALUES);CHKERRQ(ierr);
2740cf51de39SMatthew G. Knepley     }
2741d69c5d34SMatthew G. Knepley   }
2742549a8adaSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);}
274397c42addSMatthew G. Knepley   ierr = PetscFree2(feRef,fvRef);CHKERRQ(ierr);
2744549a8adaSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
2745934789fcSMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2746934789fcSMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
27479318fe57SMatthew G. Knepley   if (mesh->printFEM > 1) {
2748825f8a23SLisandro Dalcin     ierr = PetscPrintf(PetscObjectComm((PetscObject)In), "%s:\n", name);CHKERRQ(ierr);
2749934789fcSMatthew G. Knepley     ierr = MatChop(In, 1.0e-10);CHKERRQ(ierr);
2750825f8a23SLisandro Dalcin     ierr = MatView(In, NULL);CHKERRQ(ierr);
2751d69c5d34SMatthew G. Knepley   }
2752d69c5d34SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
2753d69c5d34SMatthew G. Knepley   PetscFunctionReturn(0);
2754d69c5d34SMatthew G. Knepley }
27556c73c22cSMatthew G. Knepley 
2756bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixNested(DM dmc, DM dmf, Mat mass, void *user)
2757bd041c0cSMatthew G. Knepley {
2758bd041c0cSMatthew G. Knepley   SETERRQ(PetscObjectComm((PetscObject) dmc), PETSC_ERR_SUP, "Laziness");
2759bd041c0cSMatthew G. Knepley }
2760bd041c0cSMatthew G. Knepley 
276168132eb9SMatthew G. Knepley /*@
276268132eb9SMatthew G. Knepley   DMPlexComputeInterpolatorGeneral - Form the local portion of the interpolation matrix I from the coarse DM to a non-nested fine DM.
276368132eb9SMatthew G. Knepley 
276468132eb9SMatthew G. Knepley   Input Parameters:
276568132eb9SMatthew G. Knepley + dmf  - The fine mesh
276668132eb9SMatthew G. Knepley . dmc  - The coarse mesh
276768132eb9SMatthew G. Knepley - user - The user context
276868132eb9SMatthew G. Knepley 
276968132eb9SMatthew G. Knepley   Output Parameter:
277068132eb9SMatthew G. Knepley . In  - The interpolation matrix
277168132eb9SMatthew G. Knepley 
277268132eb9SMatthew G. Knepley   Level: developer
277368132eb9SMatthew G. Knepley 
277468132eb9SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
277568132eb9SMatthew G. Knepley @*/
277668132eb9SMatthew G. Knepley PetscErrorCode DMPlexComputeInterpolatorGeneral(DM dmc, DM dmf, Mat In, void *user)
27774ef9d792SMatthew G. Knepley {
277864e98e1dSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
277964e98e1dSMatthew G. Knepley   const char    *name = "Interpolator";
27804ef9d792SMatthew G. Knepley   PetscDS        prob;
27814ef9d792SMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
2782e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
27834ef9d792SMatthew G. Knepley   PetscLayout    rLayout;
27844ef9d792SMatthew G. Knepley   PetscInt      *dnz, *onz;
27854ef9d792SMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
27864ef9d792SMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
27874ef9d792SMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
27884ef9d792SMatthew G. Knepley   PetscScalar   *elemMat;
27894ef9d792SMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
27904ef9d792SMatthew G. Knepley   PetscErrorCode ierr;
27914ef9d792SMatthew G. Knepley 
27924ef9d792SMatthew G. Knepley   PetscFunctionBegin;
279377711781SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
27944ef9d792SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
27954ef9d792SMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
27964bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
27974ef9d792SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
27984ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
27994ef9d792SMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
280092fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
2801e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
280292fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
2803e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
28044ef9d792SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
28054ef9d792SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
28069c3cf19fSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
28074ef9d792SMatthew G. Knepley 
28084ef9d792SMatthew G. Knepley   ierr = MatGetLocalSize(In, &locRows, NULL);CHKERRQ(ierr);
28094ef9d792SMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) In), &rLayout);CHKERRQ(ierr);
28104ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
28114ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
28124ef9d792SMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
28134ef9d792SMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
28144ef9d792SMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
28154ef9d792SMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
2816e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
28174ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
28184ef9d792SMatthew G. Knepley     PetscObject      obj;
28194ef9d792SMatthew G. Knepley     PetscClassId     id;
2820c0d7054bSMatthew G. Knepley     PetscDualSpace   Q = NULL;
28214ef9d792SMatthew G. Knepley     PetscQuadrature  f;
282217f047d8SMatthew G. Knepley     const PetscReal *qpoints;
282317f047d8SMatthew G. Knepley     PetscInt         Nc, Np, fpdim, i, d;
28244ef9d792SMatthew G. Knepley 
28254ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
28264ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
28274ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
28284ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
28294ef9d792SMatthew G. Knepley 
28304ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
28314ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
28324ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
28334ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
28344ef9d792SMatthew G. Knepley 
28354ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
28364ef9d792SMatthew G. Knepley       Nc   = 1;
28374ef9d792SMatthew G. Knepley     }
28384ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
28394ef9d792SMatthew G. Knepley     /* For each fine grid cell */
28404ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
28414ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
28424ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
28434ef9d792SMatthew G. Knepley 
284471f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
28454ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
28462c71b3e2SJacob Faibussowitsch       PetscCheckFalse(numFIndices != fpdim,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
28474ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
28484ef9d792SMatthew G. Knepley         Vec             pointVec;
28494ef9d792SMatthew G. Knepley         PetscScalar    *pV;
28503a93e3b7SToby Isaac         PetscSF         coarseCellSF = NULL;
28513a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
28529c3cf19fSMatthew G. Knepley         PetscInt        numCoarseCells, q, c;
28534ef9d792SMatthew G. Knepley 
28544ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
28554ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
28569c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, NULL, &Np, &qpoints, NULL);CHKERRQ(ierr);
28574ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
28584ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
28594ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
28604ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2861c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2862c330f8ffSToby Isaac 
28634ef9d792SMatthew G. Knepley           /* Transform point to real space */
2864c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
28654ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
28664ef9d792SMatthew G. Knepley         }
28674ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
28684ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
28691555c271SMatthew G. Knepley         /* OPT: Pack all quad points from fine cell */
287062a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
28713a93e3b7SToby Isaac         ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
28724ef9d792SMatthew G. Knepley         /* Update preallocation info */
28733a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
28742c71b3e2SJacob Faibussowitsch         PetscCheckFalse(numCoarseCells != Np,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
28759c3cf19fSMatthew G. Knepley         {
2876e8f14785SLisandro Dalcin           PetscHashIJKey key;
2877e8f14785SLisandro Dalcin           PetscBool      missing;
28784ef9d792SMatthew G. Knepley 
2879e8f14785SLisandro Dalcin           key.i = findices[i];
2880e8f14785SLisandro Dalcin           if (key.i >= 0) {
28814ef9d792SMatthew G. Knepley             /* Get indices for coarse elements */
28824ef9d792SMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
288371f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
28844ef9d792SMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
2885e8f14785SLisandro Dalcin                 key.j = cindices[c];
2886e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
2887e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
28884ef9d792SMatthew G. Knepley                 if (missing) {
2889e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
2890e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
28914ef9d792SMatthew G. Knepley                 }
28924ef9d792SMatthew G. Knepley               }
289371f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
28944ef9d792SMatthew G. Knepley             }
28954ef9d792SMatthew G. Knepley           }
28968c543595SMatthew G. Knepley         }
28973a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
28984ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
28994ef9d792SMatthew G. Knepley       }
290071f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
29014ef9d792SMatthew G. Knepley     }
29024ef9d792SMatthew G. Knepley   }
2903e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
29044ef9d792SMatthew G. Knepley   ierr = MatXAIJSetPreallocation(In, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
29054ef9d792SMatthew G. Knepley   ierr = MatSetOption(In, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
29064ef9d792SMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
29074ef9d792SMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
29084ef9d792SMatthew G. Knepley     PetscObject       obj;
29094ef9d792SMatthew G. Knepley     PetscClassId      id;
2910c0d7054bSMatthew G. Knepley     PetscDualSpace    Q = NULL;
2911ef0bb6c7SMatthew G. Knepley     PetscTabulation T = NULL;
29124ef9d792SMatthew G. Knepley     PetscQuadrature   f;
29134ef9d792SMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
29149c3cf19fSMatthew G. Knepley     PetscInt          Nc, qNc, Np, fpdim, i, d;
29154ef9d792SMatthew G. Knepley 
29164ef9d792SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
29174ef9d792SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
29184ef9d792SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
29194ef9d792SMatthew G. Knepley       PetscFE fe = (PetscFE) obj;
29204ef9d792SMatthew G. Knepley 
29214ef9d792SMatthew G. Knepley       ierr = PetscFEGetDualSpace(fe, &Q);CHKERRQ(ierr);
29224ef9d792SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
2923ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation(fe, 1, 1, x, 0, &T);CHKERRQ(ierr);
29244ef9d792SMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
29254ef9d792SMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
29264ef9d792SMatthew G. Knepley 
29274ef9d792SMatthew G. Knepley       ierr = PetscFVGetDualSpace(fv, &Q);CHKERRQ(ierr);
29284ef9d792SMatthew G. Knepley       Nc   = 1;
292998921bdaSJacob Faibussowitsch     } else SETERRQ(PetscObjectComm((PetscObject)dmc),PETSC_ERR_ARG_WRONG,"Unknown discretization type for field %D",field);
29304ef9d792SMatthew G. Knepley     ierr = PetscDualSpaceGetDimension(Q, &fpdim);CHKERRQ(ierr);
29314ef9d792SMatthew G. Knepley     /* For each fine grid cell */
29324ef9d792SMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
29334ef9d792SMatthew G. Knepley       PetscInt *findices,   *cindices;
29344ef9d792SMatthew G. Knepley       PetscInt  numFIndices, numCIndices;
29354ef9d792SMatthew G. Knepley 
293671f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
29374ef9d792SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
29382c71b3e2SJacob Faibussowitsch       PetscCheckFalse(numFIndices != fpdim,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of fine indices %D != %D dual basis vecs", numFIndices, fpdim);
29394ef9d792SMatthew G. Knepley       for (i = 0; i < fpdim; ++i) {
29404ef9d792SMatthew G. Knepley         Vec             pointVec;
29414ef9d792SMatthew G. Knepley         PetscScalar    *pV;
294212111d7cSToby Isaac         PetscSF         coarseCellSF = NULL;
29433a93e3b7SToby Isaac         const PetscSFNode *coarseCells;
294417f047d8SMatthew G. Knepley         PetscInt        numCoarseCells, cpdim, q, c, j;
29454ef9d792SMatthew G. Knepley 
29464ef9d792SMatthew G. Knepley         /* Get points from the dual basis functional quadrature */
29474ef9d792SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(Q, i, &f);CHKERRQ(ierr);
29489c3cf19fSMatthew G. Knepley         ierr = PetscQuadratureGetData(f, NULL, &qNc, &Np, &qpoints, &qweights);CHKERRQ(ierr);
29492c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qNc != Nc,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in quadrature %D does not match coarse field %D", qNc, Nc);
29504ef9d792SMatthew G. Knepley         ierr = VecCreateSeq(PETSC_COMM_SELF, Np*dim, &pointVec);CHKERRQ(ierr);
29514ef9d792SMatthew G. Knepley         ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
29524ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
29534ef9d792SMatthew G. Knepley         for (q = 0; q < Np; ++q) {
2954c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2955c330f8ffSToby Isaac 
29564ef9d792SMatthew G. Knepley           /* Transform point to real space */
2957c330f8ffSToby Isaac           CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
29584ef9d792SMatthew G. Knepley           for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
29594ef9d792SMatthew G. Knepley         }
29604ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
29614ef9d792SMatthew G. Knepley         /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
29621555c271SMatthew G. Knepley         /* OPT: Read this out from preallocation information */
296362a38674SMatthew G. Knepley         ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
29644ef9d792SMatthew G. Knepley         /* Update preallocation info */
29653a93e3b7SToby Isaac         ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
29662c71b3e2SJacob Faibussowitsch         PetscCheckFalse(numCoarseCells != Np,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
29674ef9d792SMatthew G. Knepley         ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
29684ef9d792SMatthew G. Knepley         for (ccell = 0; ccell < numCoarseCells; ++ccell) {
2969826eb36dSMatthew G. Knepley           PetscReal pVReal[3];
2970c330f8ffSToby Isaac           const PetscReal xi0[3] = {-1., -1., -1.};
2971826eb36dSMatthew G. Knepley 
297271f0bbf9SMatthew G. Knepley           ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
29734ef9d792SMatthew G. Knepley           /* Transform points from real space to coarse reference space */
29743a93e3b7SToby Isaac           ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
2975e2d86523SMatthew G. Knepley           for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
2976c330f8ffSToby Isaac           CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
29774ef9d792SMatthew G. Knepley 
29784ef9d792SMatthew G. Knepley           if (id == PETSCFE_CLASSID) {
29794ef9d792SMatthew G. Knepley             PetscFE fe = (PetscFE) obj;
29804ef9d792SMatthew G. Knepley 
29814ef9d792SMatthew G. Knepley             /* Evaluate coarse basis on contained point */
29824ef9d792SMatthew G. Knepley             ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
2983ef0bb6c7SMatthew G. Knepley             ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
2984580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
29854ef9d792SMatthew G. Knepley             /* Get elemMat entries by multiplying by weight */
29864ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
2987ef0bb6c7SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*qweights[ccell*qNc + c];
29884ef9d792SMatthew G. Knepley             }
29894ef9d792SMatthew G. Knepley           } else {
29904ef9d792SMatthew G. Knepley             cpdim = 1;
29914ef9d792SMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
29929c3cf19fSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*qweights[ccell*qNc + c];
29934ef9d792SMatthew G. Knepley             }
29944ef9d792SMatthew G. Knepley           }
29954ef9d792SMatthew G. Knepley           /* Update interpolator */
29969c3cf19fSMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
29972c71b3e2SJacob Faibussowitsch           PetscCheckFalse(numCIndices != cpdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
29989c3cf19fSMatthew G. Knepley           ierr = MatSetValues(In, 1, &findices[i], numCIndices, cindices, elemMat, INSERT_VALUES);CHKERRQ(ierr);
299971f0bbf9SMatthew G. Knepley           ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
30004ef9d792SMatthew G. Knepley         }
30014ef9d792SMatthew G. Knepley         ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
30023a93e3b7SToby Isaac         ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
30034ef9d792SMatthew G. Knepley         ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
30044ef9d792SMatthew G. Knepley       }
300571f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
30064ef9d792SMatthew G. Knepley     }
3007ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
30084ef9d792SMatthew G. Knepley   }
30094ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
30104ef9d792SMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
30114ef9d792SMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
30124ef9d792SMatthew G. Knepley   ierr = MatAssemblyBegin(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
30134ef9d792SMatthew G. Knepley   ierr = MatAssemblyEnd(In, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
301477711781SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InterpolatorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
30154ef9d792SMatthew G. Knepley   PetscFunctionReturn(0);
30164ef9d792SMatthew G. Knepley }
30174ef9d792SMatthew G. Knepley 
301846fa42a0SMatthew G. Knepley /*@
3019bd041c0cSMatthew G. Knepley   DMPlexComputeMassMatrixGeneral - Form the local portion of the mass matrix M from the coarse DM to a non-nested fine DM.
3020bd041c0cSMatthew G. Knepley 
3021bd041c0cSMatthew G. Knepley   Input Parameters:
3022bd041c0cSMatthew G. Knepley + dmf  - The fine mesh
3023bd041c0cSMatthew G. Knepley . dmc  - The coarse mesh
3024bd041c0cSMatthew G. Knepley - user - The user context
3025bd041c0cSMatthew G. Knepley 
3026bd041c0cSMatthew G. Knepley   Output Parameter:
3027bd041c0cSMatthew G. Knepley . mass  - The mass matrix
3028bd041c0cSMatthew G. Knepley 
3029bd041c0cSMatthew G. Knepley   Level: developer
3030bd041c0cSMatthew G. Knepley 
3031bd041c0cSMatthew G. Knepley .seealso: DMPlexComputeMassMatrixNested(), DMPlexComputeInterpolatorNested(), DMPlexComputeInterpolatorGeneral(), DMPlexComputeJacobianFEM()
3032bd041c0cSMatthew G. Knepley @*/
3033bd041c0cSMatthew G. Knepley PetscErrorCode DMPlexComputeMassMatrixGeneral(DM dmc, DM dmf, Mat mass, void *user)
3034bd041c0cSMatthew G. Knepley {
3035bd041c0cSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dmf->data;
3036bd041c0cSMatthew G. Knepley   const char    *name = "Mass Matrix";
3037bd041c0cSMatthew G. Knepley   PetscDS        prob;
3038bd041c0cSMatthew G. Knepley   PetscSection   fsection, csection, globalFSection, globalCSection;
3039e8f14785SLisandro Dalcin   PetscHSetIJ    ht;
3040bd041c0cSMatthew G. Knepley   PetscLayout    rLayout;
3041bd041c0cSMatthew G. Knepley   PetscInt      *dnz, *onz;
3042bd041c0cSMatthew G. Knepley   PetscInt       locRows, rStart, rEnd;
3043bd041c0cSMatthew G. Knepley   PetscReal     *x, *v0, *J, *invJ, detJ;
3044bd041c0cSMatthew G. Knepley   PetscReal     *v0c, *Jc, *invJc, detJc;
3045bd041c0cSMatthew G. Knepley   PetscScalar   *elemMat;
3046bd041c0cSMatthew G. Knepley   PetscInt       dim, Nf, field, totDim, cStart, cEnd, cell, ccell;
3047bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
3048bd041c0cSMatthew G. Knepley 
3049bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
3050bd041c0cSMatthew G. Knepley   ierr = DMGetCoordinateDim(dmc, &dim);CHKERRQ(ierr);
3051bd041c0cSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
30524bee2e38SMatthew G. Knepley   ierr = PetscDSGetWorkspace(prob, &x, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
3053bd041c0cSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
3054bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0,dim*dim,&J,dim*dim,&invJ);CHKERRQ(ierr);
3055bd041c0cSMatthew G. Knepley   ierr = PetscMalloc3(dim,&v0c,dim*dim,&Jc,dim*dim,&invJc);CHKERRQ(ierr);
305692fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
3057e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);
305892fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
3059e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);
3060bd041c0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmf, 0, &cStart, &cEnd);CHKERRQ(ierr);
3061bd041c0cSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3062bd041c0cSMatthew G. Knepley   ierr = PetscMalloc1(totDim, &elemMat);CHKERRQ(ierr);
3063bd041c0cSMatthew G. Knepley 
3064bd041c0cSMatthew G. Knepley   ierr = MatGetLocalSize(mass, &locRows, NULL);CHKERRQ(ierr);
3065bd041c0cSMatthew G. Knepley   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject) mass), &rLayout);CHKERRQ(ierr);
3066bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(rLayout, locRows);CHKERRQ(ierr);
3067bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(rLayout, 1);CHKERRQ(ierr);
3068bd041c0cSMatthew G. Knepley   ierr = PetscLayoutSetUp(rLayout);CHKERRQ(ierr);
3069bd041c0cSMatthew G. Knepley   ierr = PetscLayoutGetRange(rLayout, &rStart, &rEnd);CHKERRQ(ierr);
3070bd041c0cSMatthew G. Knepley   ierr = PetscLayoutDestroy(&rLayout);CHKERRQ(ierr);
3071bd041c0cSMatthew G. Knepley   ierr = PetscCalloc2(locRows,&dnz,locRows,&onz);CHKERRQ(ierr);
3072e8f14785SLisandro Dalcin   ierr = PetscHSetIJCreate(&ht);CHKERRQ(ierr);
3073bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
3074bd041c0cSMatthew G. Knepley     PetscObject      obj;
3075bd041c0cSMatthew G. Knepley     PetscClassId     id;
3076bd041c0cSMatthew G. Knepley     PetscQuadrature  quad;
3077bd041c0cSMatthew G. Knepley     const PetscReal *qpoints;
3078bd041c0cSMatthew G. Knepley     PetscInt         Nq, Nc, i, d;
3079bd041c0cSMatthew G. Knepley 
3080bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
3081bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3082bd041c0cSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);}
3083bd041c0cSMatthew G. Knepley     else                       {ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);}
3084bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, NULL);CHKERRQ(ierr);
3085bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
3086bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
3087bd041c0cSMatthew G. Knepley       Vec                pointVec;
3088bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
3089bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
3090bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
3091bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, q, c;
3092bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
3093bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
3094bd041c0cSMatthew G. Knepley 
309571f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3096bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
3097bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
3098bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
3099bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
3100bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
3101bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
3102c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3103c330f8ffSToby Isaac 
3104bd041c0cSMatthew G. Knepley         /* Transform point to real space */
3105c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
3106bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
3107bd041c0cSMatthew G. Knepley       }
3108bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
3109bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
3110bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
3111bd041c0cSMatthew G. Knepley       ierr = PetscSFViewFromOptions(coarseCellSF, NULL, "-interp_sf_view");CHKERRQ(ierr);
3112bd041c0cSMatthew G. Knepley       /* Update preallocation info */
3113bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
31142c71b3e2SJacob Faibussowitsch       PetscCheckFalse(numCoarseCells != Nq,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
3115bd041c0cSMatthew G. Knepley       {
3116e8f14785SLisandro Dalcin         PetscHashIJKey key;
3117e8f14785SLisandro Dalcin         PetscBool      missing;
3118bd041c0cSMatthew G. Knepley 
3119bd041c0cSMatthew G. Knepley         for (i = 0; i < numFIndices; ++i) {
3120e8f14785SLisandro Dalcin           key.i = findices[i];
3121e8f14785SLisandro Dalcin           if (key.i >= 0) {
3122bd041c0cSMatthew G. Knepley             /* Get indices for coarse elements */
3123bd041c0cSMatthew G. Knepley             for (ccell = 0; ccell < numCoarseCells; ++ccell) {
312471f0bbf9SMatthew G. Knepley               ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3125bd041c0cSMatthew G. Knepley               for (c = 0; c < numCIndices; ++c) {
3126e8f14785SLisandro Dalcin                 key.j = cindices[c];
3127e8f14785SLisandro Dalcin                 if (key.j < 0) continue;
3128e8f14785SLisandro Dalcin                 ierr = PetscHSetIJQueryAdd(ht, key, &missing);CHKERRQ(ierr);
3129bd041c0cSMatthew G. Knepley                 if (missing) {
3130e8f14785SLisandro Dalcin                   if ((key.j >= rStart) && (key.j < rEnd)) ++dnz[key.i-rStart];
3131e8f14785SLisandro Dalcin                   else                                     ++onz[key.i-rStart];
3132bd041c0cSMatthew G. Knepley                 }
3133bd041c0cSMatthew G. Knepley               }
313471f0bbf9SMatthew G. Knepley               ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3135bd041c0cSMatthew G. Knepley             }
3136bd041c0cSMatthew G. Knepley           }
3137bd041c0cSMatthew G. Knepley         }
3138bd041c0cSMatthew G. Knepley       }
3139bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
3140bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
314171f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3142bd041c0cSMatthew G. Knepley     }
3143bd041c0cSMatthew G. Knepley   }
3144e8f14785SLisandro Dalcin   ierr = PetscHSetIJDestroy(&ht);CHKERRQ(ierr);
3145bd041c0cSMatthew G. Knepley   ierr = MatXAIJSetPreallocation(mass, 1, dnz, onz, NULL, NULL);CHKERRQ(ierr);
3146bd041c0cSMatthew G. Knepley   ierr = MatSetOption(mass, MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr);
3147bd041c0cSMatthew G. Knepley   ierr = PetscFree2(dnz,onz);CHKERRQ(ierr);
3148bd041c0cSMatthew G. Knepley   for (field = 0; field < Nf; ++field) {
3149bd041c0cSMatthew G. Knepley     PetscObject       obj;
3150bd041c0cSMatthew G. Knepley     PetscClassId      id;
3151ef0bb6c7SMatthew G. Knepley     PetscTabulation T, Tfine;
3152bd041c0cSMatthew G. Knepley     PetscQuadrature   quad;
3153bd041c0cSMatthew G. Knepley     const PetscReal  *qpoints, *qweights;
3154bd041c0cSMatthew G. Knepley     PetscInt          Nq, Nc, i, d;
3155bd041c0cSMatthew G. Knepley 
3156bd041c0cSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
3157bd041c0cSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3158ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
3159ef0bb6c7SMatthew G. Knepley       ierr = PetscFEGetQuadrature((PetscFE) obj, &quad);CHKERRQ(ierr);
3160f9244615SMatthew G. Knepley       ierr = PetscFEGetCellTabulation((PetscFE) obj, 1, &Tfine);CHKERRQ(ierr);
3161ef0bb6c7SMatthew G. Knepley       ierr = PetscFECreateTabulation((PetscFE) obj, 1, 1, x, 0, &T);CHKERRQ(ierr);
3162ef0bb6c7SMatthew G. Knepley     } else {
3163ef0bb6c7SMatthew G. Knepley       ierr = PetscFVGetQuadrature((PetscFV) obj, &quad);CHKERRQ(ierr);
3164ef0bb6c7SMatthew G. Knepley     }
3165bd041c0cSMatthew G. Knepley     ierr = PetscQuadratureGetData(quad, NULL, &Nc, &Nq, &qpoints, &qweights);CHKERRQ(ierr);
3166bd041c0cSMatthew G. Knepley     /* For each fine grid cell */
3167bd041c0cSMatthew G. Knepley     for (cell = cStart; cell < cEnd; ++cell) {
3168bd041c0cSMatthew G. Knepley       Vec                pointVec;
3169bd041c0cSMatthew G. Knepley       PetscScalar       *pV;
3170bd041c0cSMatthew G. Knepley       PetscSF            coarseCellSF = NULL;
3171bd041c0cSMatthew G. Knepley       const PetscSFNode *coarseCells;
3172bd041c0cSMatthew G. Knepley       PetscInt           numCoarseCells, cpdim, q, c, j;
3173bd041c0cSMatthew G. Knepley       PetscInt          *findices,   *cindices;
3174bd041c0cSMatthew G. Knepley       PetscInt           numFIndices, numCIndices;
3175bd041c0cSMatthew G. Knepley 
317671f0bbf9SMatthew G. Knepley       ierr = DMPlexGetClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3177bd041c0cSMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFEM(dmf, cell, NULL, v0, J, invJ, &detJ);CHKERRQ(ierr);
3178bd041c0cSMatthew G. Knepley       /* Get points from the quadrature */
3179bd041c0cSMatthew G. Knepley       ierr = VecCreateSeq(PETSC_COMM_SELF, Nq*dim, &pointVec);CHKERRQ(ierr);
3180bd041c0cSMatthew G. Knepley       ierr = VecSetBlockSize(pointVec, dim);CHKERRQ(ierr);
3181bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
3182bd041c0cSMatthew G. Knepley       for (q = 0; q < Nq; ++q) {
3183c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3184c330f8ffSToby Isaac 
3185bd041c0cSMatthew G. Knepley         /* Transform point to real space */
3186c330f8ffSToby Isaac         CoordinatesRefToReal(dim, dim, xi0, v0, J, &qpoints[q*dim], x);
3187bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pV[q*dim+d] = x[d];
3188bd041c0cSMatthew G. Knepley       }
3189bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
3190bd041c0cSMatthew G. Knepley       /* Get set of coarse cells that overlap points (would like to group points by coarse cell) */
3191bd041c0cSMatthew G. Knepley       ierr = DMLocatePoints(dmc, pointVec, DM_POINTLOCATION_NEAREST, &coarseCellSF);CHKERRQ(ierr);
3192bd041c0cSMatthew G. Knepley       /* Update matrix */
3193bd041c0cSMatthew G. Knepley       ierr = PetscSFGetGraph(coarseCellSF, NULL, &numCoarseCells, NULL, &coarseCells);CHKERRQ(ierr);
31942c71b3e2SJacob Faibussowitsch       PetscCheckFalse(numCoarseCells != Nq,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Not all closure points located");
3195bd041c0cSMatthew G. Knepley       ierr = VecGetArray(pointVec, &pV);CHKERRQ(ierr);
3196bd041c0cSMatthew G. Knepley       for (ccell = 0; ccell < numCoarseCells; ++ccell) {
3197bd041c0cSMatthew G. Knepley         PetscReal pVReal[3];
3198c330f8ffSToby Isaac         const PetscReal xi0[3] = {-1., -1., -1.};
3199c330f8ffSToby Isaac 
320071f0bbf9SMatthew G. Knepley         ierr = DMPlexGetClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3201bd041c0cSMatthew G. Knepley         /* Transform points from real space to coarse reference space */
3202bd041c0cSMatthew G. Knepley         ierr = DMPlexComputeCellGeometryFEM(dmc, coarseCells[ccell].index, NULL, v0c, Jc, invJc, &detJc);CHKERRQ(ierr);
3203bd041c0cSMatthew G. Knepley         for (d = 0; d < dim; ++d) pVReal[d] = PetscRealPart(pV[ccell*dim+d]);
3204c330f8ffSToby Isaac         CoordinatesRealToRef(dim, dim, xi0, v0c, invJc, pVReal, x);
3205bd041c0cSMatthew G. Knepley 
3206bd041c0cSMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
3207bd041c0cSMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
3208bd041c0cSMatthew G. Knepley 
3209bd041c0cSMatthew G. Knepley           /* Evaluate coarse basis on contained point */
3210bd041c0cSMatthew G. Knepley           ierr = PetscFEGetDimension(fe, &cpdim);CHKERRQ(ierr);
3211ef0bb6c7SMatthew G. Knepley           ierr = PetscFEComputeTabulation(fe, 1, x, 0, T);CHKERRQ(ierr);
3212bd041c0cSMatthew G. Knepley           /* Get elemMat entries by multiplying by weight */
3213bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
3214580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
3215bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
3216ef0bb6c7SMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += T->T[0][j*Nc + c]*Tfine->T[0][(ccell*numFIndices + i)*Nc + c]*qweights[ccell*Nc + c]*detJ;
3217bd041c0cSMatthew G. Knepley             }
3218bd041c0cSMatthew G. Knepley             /* Update interpolator */
3219bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
32202c71b3e2SJacob Faibussowitsch             PetscCheckFalse(numCIndices != cpdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
3221bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
3222bd041c0cSMatthew G. Knepley           }
3223bd041c0cSMatthew G. Knepley         } else {
3224bd041c0cSMatthew G. Knepley           cpdim = 1;
3225bd041c0cSMatthew G. Knepley           for (i = 0; i < numFIndices; ++i) {
3226580bdb30SBarry Smith             ierr = PetscArrayzero(elemMat, cpdim);CHKERRQ(ierr);
3227bd041c0cSMatthew G. Knepley             for (j = 0; j < cpdim; ++j) {
3228bd041c0cSMatthew G. Knepley               for (c = 0; c < Nc; ++c) elemMat[j] += 1.0*1.0*qweights[ccell*Nc + c]*detJ;
3229bd041c0cSMatthew G. Knepley             }
3230bd041c0cSMatthew G. Knepley             /* Update interpolator */
3231bd041c0cSMatthew G. Knepley             if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, 1, numCIndices, elemMat);CHKERRQ(ierr);}
3232ff1e0c32SBarry Smith             ierr = PetscPrintf(PETSC_COMM_SELF, "Nq: %D %D Nf: %D %D Nc: %D %D\n", ccell, Nq, i, numFIndices, j, numCIndices);CHKERRQ(ierr);
32332c71b3e2SJacob Faibussowitsch             PetscCheckFalse(numCIndices != cpdim,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number of element matrix columns %D != %D", numCIndices, cpdim);
3234bd041c0cSMatthew G. Knepley             ierr = MatSetValues(mass, 1, &findices[i], numCIndices, cindices, elemMat, ADD_VALUES);CHKERRQ(ierr);
3235bd041c0cSMatthew G. Knepley           }
3236bd041c0cSMatthew G. Knepley         }
323771f0bbf9SMatthew G. Knepley         ierr = DMPlexRestoreClosureIndices(dmc, csection, globalCSection, coarseCells[ccell].index, PETSC_FALSE, &numCIndices, &cindices, NULL, NULL);CHKERRQ(ierr);
3238bd041c0cSMatthew G. Knepley       }
3239bd041c0cSMatthew G. Knepley       ierr = VecRestoreArray(pointVec, &pV);CHKERRQ(ierr);
3240bd041c0cSMatthew G. Knepley       ierr = PetscSFDestroy(&coarseCellSF);CHKERRQ(ierr);
3241bd041c0cSMatthew G. Knepley       ierr = VecDestroy(&pointVec);CHKERRQ(ierr);
324271f0bbf9SMatthew G. Knepley       ierr = DMPlexRestoreClosureIndices(dmf, fsection, globalFSection, cell, PETSC_FALSE, &numFIndices, &findices, NULL, NULL);CHKERRQ(ierr);
3243bd041c0cSMatthew G. Knepley     }
3244ef0bb6c7SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {ierr = PetscTabulationDestroy(&T);CHKERRQ(ierr);}
3245bd041c0cSMatthew G. Knepley   }
3246bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0,J,invJ);CHKERRQ(ierr);
3247bd041c0cSMatthew G. Knepley   ierr = PetscFree3(v0c,Jc,invJc);CHKERRQ(ierr);
3248bd041c0cSMatthew G. Knepley   ierr = PetscFree(elemMat);CHKERRQ(ierr);
3249bd041c0cSMatthew G. Knepley   ierr = MatAssemblyBegin(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3250bd041c0cSMatthew G. Knepley   ierr = MatAssemblyEnd(mass, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3251bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
3252bd041c0cSMatthew G. Knepley }
3253bd041c0cSMatthew G. Knepley 
3254bd041c0cSMatthew G. Knepley /*@
325546fa42a0SMatthew G. Knepley   DMPlexComputeInjectorFEM - Compute a mapping from coarse unknowns to fine unknowns
325646fa42a0SMatthew G. Knepley 
325746fa42a0SMatthew G. Knepley   Input Parameters:
325846fa42a0SMatthew G. Knepley + dmc  - The coarse mesh
325946fa42a0SMatthew G. Knepley - dmf  - The fine mesh
326046fa42a0SMatthew G. Knepley - user - The user context
326146fa42a0SMatthew G. Knepley 
326246fa42a0SMatthew G. Knepley   Output Parameter:
326346fa42a0SMatthew G. Knepley . sc   - The mapping
326446fa42a0SMatthew G. Knepley 
326546fa42a0SMatthew G. Knepley   Level: developer
326646fa42a0SMatthew G. Knepley 
326746fa42a0SMatthew G. Knepley .seealso: DMPlexComputeInterpolatorNested(), DMPlexComputeJacobianFEM()
326846fa42a0SMatthew G. Knepley @*/
32697c927364SMatthew G. Knepley PetscErrorCode DMPlexComputeInjectorFEM(DM dmc, DM dmf, VecScatter *sc, void *user)
32707c927364SMatthew G. Knepley {
3271e9d4ef1bSMatthew G. Knepley   PetscDS        prob;
32727c927364SMatthew G. Knepley   PetscFE       *feRef;
327397c42addSMatthew G. Knepley   PetscFV       *fvRef;
32747c927364SMatthew G. Knepley   Vec            fv, cv;
32757c927364SMatthew G. Knepley   IS             fis, cis;
32767c927364SMatthew G. Knepley   PetscSection   fsection, fglobalSection, csection, cglobalSection;
32777c927364SMatthew G. Knepley   PetscInt      *cmap, *cellCIndices, *cellFIndices, *cindices, *findices;
3278485ad865SMatthew G. Knepley   PetscInt       cTotDim, fTotDim = 0, Nf, f, field, cStart, cEnd, c, dim, d, startC, endC, offsetC, offsetF, m;
32796f3d3cbcSMatthew G. Knepley   PetscBool     *needAvg;
32807c927364SMatthew G. Knepley   PetscErrorCode ierr;
32817c927364SMatthew G. Knepley 
32827c927364SMatthew G. Knepley   PetscFunctionBegin;
328375a69067SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3284c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dmf, &dim);CHKERRQ(ierr);
328592fd8e1eSJed Brown   ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);
3286e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmf, &fglobalSection);CHKERRQ(ierr);
328792fd8e1eSJed Brown   ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);
3288e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmc, &cglobalSection);CHKERRQ(ierr);
32897c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &Nf);CHKERRQ(ierr);
3290412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dmc, 0, &cStart, &cEnd);CHKERRQ(ierr);
3291e9d4ef1bSMatthew G. Knepley   ierr = DMGetDS(dmc, &prob);CHKERRQ(ierr);
32926f3d3cbcSMatthew G. Knepley   ierr = PetscCalloc3(Nf,&feRef,Nf,&fvRef,Nf,&needAvg);CHKERRQ(ierr);
32937c927364SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
329497c42addSMatthew G. Knepley     PetscObject  obj;
329597c42addSMatthew G. Knepley     PetscClassId id;
3296aa7890ccSMatthew G. Knepley     PetscInt     fNb = 0, Nc = 0;
32977c927364SMatthew G. Knepley 
329897c42addSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
329997c42addSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
330097c42addSMatthew G. Knepley     if (id == PETSCFE_CLASSID) {
330197c42addSMatthew G. Knepley       PetscFE    fe = (PetscFE) obj;
33026f3d3cbcSMatthew G. Knepley       PetscSpace sp;
33039b2fc754SMatthew G. Knepley       PetscInt   maxDegree;
330497c42addSMatthew G. Knepley 
33057c927364SMatthew G. Knepley       ierr = PetscFERefine(fe, &feRef[f]);CHKERRQ(ierr);
33067c927364SMatthew G. Knepley       ierr = PetscFEGetDimension(feRef[f], &fNb);CHKERRQ(ierr);
33077c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &Nc);CHKERRQ(ierr);
33086f3d3cbcSMatthew G. Knepley       ierr = PetscFEGetBasisSpace(fe, &sp);CHKERRQ(ierr);
33099b2fc754SMatthew G. Knepley       ierr = PetscSpaceGetDegree(sp, NULL, &maxDegree);CHKERRQ(ierr);
33109b2fc754SMatthew G. Knepley       if (!maxDegree) needAvg[f] = PETSC_TRUE;
331197c42addSMatthew G. Knepley     } else if (id == PETSCFV_CLASSID) {
331297c42addSMatthew G. Knepley       PetscFV        fv = (PetscFV) obj;
331397c42addSMatthew G. Knepley       PetscDualSpace Q;
331497c42addSMatthew G. Knepley 
331597c42addSMatthew G. Knepley       ierr = PetscFVRefine(fv, &fvRef[f]);CHKERRQ(ierr);
331697c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[f], &Q);CHKERRQ(ierr);
331797c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(Q, &fNb);CHKERRQ(ierr);
331897c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
33196f3d3cbcSMatthew G. Knepley       needAvg[f] = PETSC_TRUE;
332097c42addSMatthew G. Knepley     }
3321d172c84bSMatthew G. Knepley     fTotDim += fNb;
33227c927364SMatthew G. Knepley   }
3323e9d4ef1bSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &cTotDim);CHKERRQ(ierr);
33247c927364SMatthew G. Knepley   ierr = PetscMalloc1(cTotDim,&cmap);CHKERRQ(ierr);
33257c927364SMatthew G. Knepley   for (field = 0, offsetC = 0, offsetF = 0; field < Nf; ++field) {
33267c927364SMatthew G. Knepley     PetscFE        feC;
332797c42addSMatthew G. Knepley     PetscFV        fvC;
33287c927364SMatthew G. Knepley     PetscDualSpace QF, QC;
3329d172c84bSMatthew G. Knepley     PetscInt       order = -1, NcF, NcC, fpdim, cpdim;
33307c927364SMatthew G. Knepley 
333197c42addSMatthew G. Knepley     if (feRef[field]) {
3332e9d4ef1bSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &feC);CHKERRQ(ierr);
33337c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feC, &NcC);CHKERRQ(ierr);
33347c927364SMatthew G. Knepley       ierr = PetscFEGetNumComponents(feRef[field], &NcF);CHKERRQ(ierr);
33357c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feRef[field], &QF);CHKERRQ(ierr);
3336d172c84bSMatthew G. Knepley       ierr = PetscDualSpaceGetOrder(QF, &order);CHKERRQ(ierr);
33377c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
33387c927364SMatthew G. Knepley       ierr = PetscFEGetDualSpace(feC, &QC);CHKERRQ(ierr);
33397c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
334097c42addSMatthew G. Knepley     } else {
334197c42addSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, field, (PetscObject *) &fvC);CHKERRQ(ierr);
334297c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvC, &NcC);CHKERRQ(ierr);
334397c42addSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fvRef[field], &NcF);CHKERRQ(ierr);
334497c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvRef[field], &QF);CHKERRQ(ierr);
334597c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QF, &fpdim);CHKERRQ(ierr);
334697c42addSMatthew G. Knepley       ierr = PetscFVGetDualSpace(fvC, &QC);CHKERRQ(ierr);
334797c42addSMatthew G. Knepley       ierr = PetscDualSpaceGetDimension(QC, &cpdim);CHKERRQ(ierr);
334897c42addSMatthew G. Knepley     }
33492c71b3e2SJacob Faibussowitsch     PetscCheckFalse(NcF != NcC,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of components in fine space field %D does not match coarse field %D", NcF, NcC);
33507c927364SMatthew G. Knepley     for (c = 0; c < cpdim; ++c) {
33517c927364SMatthew G. Knepley       PetscQuadrature  cfunc;
3352d172c84bSMatthew G. Knepley       const PetscReal *cqpoints, *cqweights;
3353d172c84bSMatthew G. Knepley       PetscInt         NqcC, NpC;
335497c42addSMatthew G. Knepley       PetscBool        found = PETSC_FALSE;
33557c927364SMatthew G. Knepley 
33567c927364SMatthew G. Knepley       ierr = PetscDualSpaceGetFunctional(QC, c, &cfunc);CHKERRQ(ierr);
3357d172c84bSMatthew G. Knepley       ierr = PetscQuadratureGetData(cfunc, NULL, &NqcC, &NpC, &cqpoints, &cqweights);CHKERRQ(ierr);
33582c71b3e2SJacob Faibussowitsch       PetscCheckFalse(NqcC != NcC,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcC, NcC);
33592c71b3e2SJacob Faibussowitsch       PetscCheckFalse(NpC != 1 && feRef[field],PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Do not know how to do injection for moments");
33607c927364SMatthew G. Knepley       for (f = 0; f < fpdim; ++f) {
33617c927364SMatthew G. Knepley         PetscQuadrature  ffunc;
3362d172c84bSMatthew G. Knepley         const PetscReal *fqpoints, *fqweights;
33637c927364SMatthew G. Knepley         PetscReal        sum = 0.0;
3364d172c84bSMatthew G. Knepley         PetscInt         NqcF, NpF;
33657c927364SMatthew G. Knepley 
33667c927364SMatthew G. Knepley         ierr = PetscDualSpaceGetFunctional(QF, f, &ffunc);CHKERRQ(ierr);
3367d172c84bSMatthew G. Knepley         ierr = PetscQuadratureGetData(ffunc, NULL, &NqcF, &NpF, &fqpoints, &fqweights);CHKERRQ(ierr);
33682c71b3e2SJacob Faibussowitsch         PetscCheckFalse(NqcF != NcF,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of quadrature components %D must match number of field components %D", NqcF, NcF);
33697c927364SMatthew G. Knepley         if (NpC != NpF) continue;
33707c927364SMatthew G. Knepley         for (d = 0; d < dim; ++d) sum += PetscAbsReal(cqpoints[d] - fqpoints[d]);
33717c927364SMatthew G. Knepley         if (sum > 1.0e-9) continue;
3372d172c84bSMatthew G. Knepley         for (d = 0; d < NcC; ++d) sum += PetscAbsReal(cqweights[d]*fqweights[d]);
3373d172c84bSMatthew G. Knepley         if (sum < 1.0e-9) continue;
3374d172c84bSMatthew G. Knepley         cmap[offsetC+c] = offsetF+f;
337597c42addSMatthew G. Knepley         found = PETSC_TRUE;
33767c927364SMatthew G. Knepley         break;
33777c927364SMatthew G. Knepley       }
337897c42addSMatthew G. Knepley       if (!found) {
337997c42addSMatthew G. Knepley         /* TODO We really want the average here, but some asshole put VecScatter in the interface */
3380d172c84bSMatthew G. Knepley         if (fvRef[field] || (feRef[field] && order == 0)) {
3381d172c84bSMatthew G. Knepley           cmap[offsetC+c] = offsetF+0;
338297c42addSMatthew G. Knepley         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate matching functional for injection");
338397c42addSMatthew G. Knepley       }
33847c927364SMatthew G. Knepley     }
3385d172c84bSMatthew G. Knepley     offsetC += cpdim;
3386d172c84bSMatthew G. Knepley     offsetF += fpdim;
33877c927364SMatthew G. Knepley   }
338897c42addSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {ierr = PetscFEDestroy(&feRef[f]);CHKERRQ(ierr);ierr = PetscFVDestroy(&fvRef[f]);CHKERRQ(ierr);}
33896f3d3cbcSMatthew G. Knepley   ierr = PetscFree3(feRef,fvRef,needAvg);CHKERRQ(ierr);
33907c927364SMatthew G. Knepley 
33917c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmf, &fv);CHKERRQ(ierr);
33927c927364SMatthew G. Knepley   ierr = DMGetGlobalVector(dmc, &cv);CHKERRQ(ierr);
33930bd915a7SMatthew G. Knepley   ierr = VecGetOwnershipRange(cv, &startC, &endC);CHKERRQ(ierr);
33947c927364SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(cglobalSection, &m);CHKERRQ(ierr);
33957c927364SMatthew G. Knepley   ierr = PetscMalloc2(cTotDim,&cellCIndices,fTotDim,&cellFIndices);CHKERRQ(ierr);
3396aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&cindices);CHKERRQ(ierr);
3397aa7da3c4SMatthew G. Knepley   ierr = PetscMalloc1(m,&findices);CHKERRQ(ierr);
33987c927364SMatthew G. Knepley   for (d = 0; d < m; ++d) cindices[d] = findices[d] = -1;
33997c927364SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
34007c927364SMatthew G. Knepley     ierr = DMPlexMatGetClosureIndicesRefined(dmf, fsection, fglobalSection, dmc, csection, cglobalSection, c, cellCIndices, cellFIndices);CHKERRQ(ierr);
34017c927364SMatthew G. Knepley     for (d = 0; d < cTotDim; ++d) {
34020bd915a7SMatthew G. Knepley       if ((cellCIndices[d] < startC) || (cellCIndices[d] >= endC)) continue;
34032c71b3e2SJacob Faibussowitsch       PetscCheckFalse((findices[cellCIndices[d]-startC] >= 0) && (findices[cellCIndices[d]-startC] != cellFIndices[cmap[d]]),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]]);
34047c927364SMatthew G. Knepley       cindices[cellCIndices[d]-startC] = cellCIndices[d];
34057c927364SMatthew G. Knepley       findices[cellCIndices[d]-startC] = cellFIndices[cmap[d]];
34067c927364SMatthew G. Knepley     }
34077c927364SMatthew G. Knepley   }
34087c927364SMatthew G. Knepley   ierr = PetscFree(cmap);CHKERRQ(ierr);
34097c927364SMatthew G. Knepley   ierr = PetscFree2(cellCIndices,cellFIndices);CHKERRQ(ierr);
34107c927364SMatthew G. Knepley 
34117c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, cindices, PETSC_OWN_POINTER, &cis);CHKERRQ(ierr);
34127c927364SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, m, findices, PETSC_OWN_POINTER, &fis);CHKERRQ(ierr);
34139448b7f1SJunchao Zhang   ierr = VecScatterCreate(cv, cis, fv, fis, sc);CHKERRQ(ierr);
34147c927364SMatthew G. Knepley   ierr = ISDestroy(&cis);CHKERRQ(ierr);
34157c927364SMatthew G. Knepley   ierr = ISDestroy(&fis);CHKERRQ(ierr);
34167c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmf, &fv);CHKERRQ(ierr);
34177c927364SMatthew G. Knepley   ierr = DMRestoreGlobalVector(dmc, &cv);CHKERRQ(ierr);
341875a69067SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_InjectorFEM,dmc,dmf,0,0);CHKERRQ(ierr);
3419cb1e1211SMatthew G Knepley   PetscFunctionReturn(0);
3420cb1e1211SMatthew G Knepley }
3421a1cf66bbSMatthew G. Knepley 
34222f856554SMatthew G. Knepley /*@C
34232f856554SMatthew G. Knepley   DMPlexGetCellFields - Retrieve the field values values for a chunk of cells
34242f856554SMatthew G. Knepley 
34252f856554SMatthew G. Knepley   Input Parameters:
34262f856554SMatthew G. Knepley + dm     - The DM
34272f856554SMatthew G. Knepley . cellIS - The cells to include
34282f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
34292f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
34302f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
34312f856554SMatthew G. Knepley 
34322f856554SMatthew G. Knepley   Output Parameters:
34332f856554SMatthew G. Knepley + u   - The field coefficients
34342f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
34352f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
34362f856554SMatthew G. Knepley 
34372f856554SMatthew G. Knepley   Level: developer
34382f856554SMatthew G. Knepley 
34392f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
34402f856554SMatthew G. Knepley @*/
34412f856554SMatthew G. Knepley PetscErrorCode DMPlexGetCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
34422f856554SMatthew G. Knepley {
34432f856554SMatthew G. Knepley   DM              plex, plexA = NULL;
3444a6e0b375SMatthew G. Knepley   DMEnclosureType encAux;
34452f856554SMatthew G. Knepley   PetscSection    section, sectionAux;
34462f856554SMatthew G. Knepley   PetscDS         prob;
34472f856554SMatthew G. Knepley   const PetscInt *cells;
34482f856554SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, totDim, totDimAux, c;
34492f856554SMatthew G. Knepley   PetscErrorCode  ierr;
34502f856554SMatthew G. Knepley 
34512f856554SMatthew G. Knepley   PetscFunctionBegin;
34522f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3453064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(locX, VEC_CLASSID, 3);
3454064a246eSJacob Faibussowitsch   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 4);}
3455064a246eSJacob Faibussowitsch   if (locA)   {PetscValidHeaderSpecific(locA, VEC_CLASSID, 5);}
3456064a246eSJacob Faibussowitsch   PetscValidPointer(u, 6);
3457064a246eSJacob Faibussowitsch   PetscValidPointer(u_t, 7);
3458064a246eSJacob Faibussowitsch   PetscValidPointer(a, 8);
34592f856554SMatthew G. Knepley   ierr = DMPlexConvertPlex(dm, &plex, PETSC_FALSE);CHKERRQ(ierr);
34602f856554SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
346192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
3462083401c6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
34632f856554SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
34642f856554SMatthew G. Knepley   if (locA) {
34652f856554SMatthew G. Knepley     DM      dmAux;
34662f856554SMatthew G. Knepley     PetscDS probAux;
34672f856554SMatthew G. Knepley 
34682f856554SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
3469a6e0b375SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
34702f856554SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux, &plexA, PETSC_FALSE);CHKERRQ(ierr);
347192fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
34722f856554SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
34732f856554SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
34742f856554SMatthew G. Knepley   }
34752f856554SMatthew G. Knepley   numCells = cEnd - cStart;
34762f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u);CHKERRQ(ierr);
34772f856554SMatthew G. Knepley   if (locX_t) {ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, u_t);CHKERRQ(ierr);} else {*u_t = NULL;}
34782f856554SMatthew G. Knepley   if (locA)   {ierr = DMGetWorkArray(dm, numCells*totDimAux, MPIU_SCALAR, a);CHKERRQ(ierr);} else {*a = NULL;}
34792f856554SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
34802f856554SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
34812f856554SMatthew G. Knepley     const PetscInt cind = c - cStart;
34822f856554SMatthew G. Knepley     PetscScalar   *x = NULL, *x_t = NULL, *ul = *u, *ul_t = *u_t, *al = *a;
34832f856554SMatthew G. Knepley     PetscInt       i;
34842f856554SMatthew G. Knepley 
34852f856554SMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
34862f856554SMatthew G. Knepley     for (i = 0; i < totDim; ++i) ul[cind*totDim+i] = x[i];
34872f856554SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, locX, cell, NULL, &x);CHKERRQ(ierr);
34882f856554SMatthew G. Knepley     if (locX_t) {
34892f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
34902f856554SMatthew G. Knepley       for (i = 0; i < totDim; ++i) ul_t[cind*totDim+i] = x_t[i];
34912f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX_t, cell, NULL, &x_t);CHKERRQ(ierr);
34922f856554SMatthew G. Knepley     }
34932f856554SMatthew G. Knepley     if (locA) {
34942f856554SMatthew G. Knepley       PetscInt subcell;
3495a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(plexA, dm, encAux, cell, &subcell);CHKERRQ(ierr);
34962f856554SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
34972f856554SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) al[cind*totDimAux+i] = x[i];
34982f856554SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subcell, NULL, &x);CHKERRQ(ierr);
34992f856554SMatthew G. Knepley     }
35002f856554SMatthew G. Knepley   }
35012f856554SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
35022f856554SMatthew G. Knepley   if (locA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
35032f856554SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
35042f856554SMatthew G. Knepley   PetscFunctionReturn(0);
35052f856554SMatthew G. Knepley }
35062f856554SMatthew G. Knepley 
35072f856554SMatthew G. Knepley /*@C
35082f856554SMatthew G. Knepley   DMPlexRestoreCellFields - Restore the field values values for a chunk of cells
35092f856554SMatthew G. Knepley 
35102f856554SMatthew G. Knepley   Input Parameters:
35112f856554SMatthew G. Knepley + dm     - The DM
35122f856554SMatthew G. Knepley . cellIS - The cells to include
35132f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
35142f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
35152f856554SMatthew G. Knepley - locA   - A local vector with auxiliary fields, or NULL
35162f856554SMatthew G. Knepley 
35172f856554SMatthew G. Knepley   Output Parameters:
35182f856554SMatthew G. Knepley + u   - The field coefficients
35192f856554SMatthew G. Knepley . u_t - The fields derivative coefficients
35202f856554SMatthew G. Knepley - a   - The auxiliary field coefficients
35212f856554SMatthew G. Knepley 
35222f856554SMatthew G. Knepley   Level: developer
35232f856554SMatthew G. Knepley 
35242f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
35252f856554SMatthew G. Knepley @*/
35262f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreCellFields(DM dm, IS cellIS, Vec locX, Vec locX_t, Vec locA, PetscScalar **u, PetscScalar **u_t, PetscScalar **a)
35272f856554SMatthew G. Knepley {
35282f856554SMatthew G. Knepley   PetscErrorCode ierr;
35292f856554SMatthew G. Knepley 
35302f856554SMatthew G. Knepley   PetscFunctionBegin;
35312f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u);CHKERRQ(ierr);
35322f856554SMatthew G. Knepley   if (locX_t) {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, u_t);CHKERRQ(ierr);}
35332f856554SMatthew G. Knepley   if (locA)   {ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, a);CHKERRQ(ierr);}
35342f856554SMatthew G. Knepley   PetscFunctionReturn(0);
35352f856554SMatthew G. Knepley }
35362f856554SMatthew G. Knepley 
3537faff4f7fSMatthew G. Knepley /*
3538faff4f7fSMatthew G. Knepley   Get the auxiliary field vectors for the negative side (s = 0) and positive side (s = 1) of the interfaace
3539faff4f7fSMatthew G. Knepley */
3540148442b3SMatthew G. Knepley static PetscErrorCode DMPlexGetHybridAuxFields(DM dm, DM dmAux[], PetscDS dsAux[], IS cellIS, Vec locA[], PetscScalar *a[])
35416528b96dSMatthew G. Knepley {
354204c51a94SMatthew G. Knepley   DM              plexA[2];
3543148442b3SMatthew G. Knepley   DMEnclosureType encAux[2];
354404c51a94SMatthew G. Knepley   PetscSection    sectionAux[2];
35456528b96dSMatthew G. Knepley   const PetscInt *cells;
3546148442b3SMatthew G. Knepley   PetscInt        cStart, cEnd, numCells, c, s, totDimAux[2];
35476528b96dSMatthew G. Knepley   PetscErrorCode  ierr;
35486528b96dSMatthew G. Knepley 
35496528b96dSMatthew G. Knepley   PetscFunctionBegin;
3550148442b3SMatthew G. Knepley   PetscValidPointer(locA, 5);
355104c51a94SMatthew G. Knepley   if (!locA[0] || !locA[1]) PetscFunctionReturn(0);
3552148442b3SMatthew G. Knepley   PetscValidPointer(dmAux, 2);
3553148442b3SMatthew G. Knepley   PetscValidPointer(dsAux, 3);
3554148442b3SMatthew G. Knepley   PetscValidPointer(a, 6);
35556528b96dSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
35566528b96dSMatthew G. Knepley   numCells = cEnd - cStart;
3557148442b3SMatthew G. Knepley   for (s = 0; s < 2; ++s) {
3558148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(dmAux[s], DM_CLASSID, 2);
3559148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(dsAux[s], PETSCDS_CLASSID, 3);
3560148442b3SMatthew G. Knepley     PetscValidHeaderSpecific(locA[s], VEC_CLASSID, 5);
3561148442b3SMatthew G. Knepley     ierr = DMPlexConvertPlex(dmAux[s], &plexA[s], PETSC_FALSE);CHKERRQ(ierr);
3562148442b3SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux[s], dm, &encAux[s]);CHKERRQ(ierr);
3563148442b3SMatthew G. Knepley     ierr = DMGetLocalSection(dmAux[s], &sectionAux[s]);CHKERRQ(ierr);
3564148442b3SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[s], &totDimAux[s]);CHKERRQ(ierr);
3565148442b3SMatthew G. Knepley     ierr = DMGetWorkArray(dmAux[s], numCells*totDimAux[s], MPIU_SCALAR, &a[s]);CHKERRQ(ierr);
356604c51a94SMatthew G. Knepley   }
3567148442b3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
35686528b96dSMatthew G. Knepley     const PetscInt  cell = cells ? cells[c] : c;
35696528b96dSMatthew G. Knepley     const PetscInt  cind = c - cStart;
35706528b96dSMatthew G. Knepley     const PetscInt *cone, *ornt;
35716528b96dSMatthew G. Knepley 
3572148442b3SMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
3573148442b3SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
35742c71b3e2SJacob Faibussowitsch     PetscCheckFalse(ornt[0],PETSC_COMM_SELF, PETSC_ERR_SUP, "Face %D in hybrid cell %D has orientation %D != 0", cone[0], cell, ornt[0]);
3575148442b3SMatthew G. Knepley     for (s = 0; s < 2; ++s) {
35765fedec97SMatthew G. Knepley       const PetscInt *support;
3577148442b3SMatthew G. Knepley       PetscScalar    *x = NULL, *al = a[s];
3578148442b3SMatthew G. Knepley       const PetscInt  tdA = totDimAux[s];
35795fedec97SMatthew G. Knepley       PetscInt        ssize, scell;
3580148442b3SMatthew G. Knepley       PetscInt        subface, Na, i;
35816528b96dSMatthew G. Knepley 
35825fedec97SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[s], &support);CHKERRQ(ierr);
35835fedec97SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[s], &ssize);CHKERRQ(ierr);
35842c71b3e2SJacob Faibussowitsch       PetscCheckFalse(ssize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[s], cell, ssize);
35855fedec97SMatthew G. Knepley       if      (support[0] == cell) scell = support[1];
35865fedec97SMatthew G. Knepley       else if (support[1] == cell) scell = support[0];
358798921bdaSJacob Faibussowitsch       else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[s], cell);
35885fedec97SMatthew G. Knepley 
35895fedec97SMatthew G. Knepley       ierr = DMGetEnclosurePoint(plexA[s], dm, encAux[s], scell, &subface);CHKERRQ(ierr);
3590148442b3SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexA[s], sectionAux[s], locA[s], subface, &Na, &x);CHKERRQ(ierr);
35916528b96dSMatthew G. Knepley       for (i = 0; i < Na; ++i) al[cind*tdA+i] = x[i];
3592148442b3SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexA[s], sectionAux[s], locA[s], subface, &Na, &x);CHKERRQ(ierr);
35936528b96dSMatthew G. Knepley     }
35946528b96dSMatthew G. Knepley   }
3595148442b3SMatthew G. Knepley   for (s = 0; s < 2; ++s) {ierr = DMDestroy(&plexA[s]);CHKERRQ(ierr);}
35966528b96dSMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
35976528b96dSMatthew G. Knepley   PetscFunctionReturn(0);
35986528b96dSMatthew G. Knepley }
35996528b96dSMatthew G. Knepley 
360004c51a94SMatthew G. Knepley static PetscErrorCode DMPlexRestoreHybridAuxFields(DM dmAux[], PetscDS dsAux[], IS cellIS, Vec locA[], PetscScalar *a[])
36016528b96dSMatthew G. Knepley {
36026528b96dSMatthew G. Knepley   PetscErrorCode ierr;
36036528b96dSMatthew G. Knepley 
36046528b96dSMatthew G. Knepley   PetscFunctionBegin;
360504c51a94SMatthew G. Knepley   if (!locA[0] || !locA[1]) PetscFunctionReturn(0);
360604c51a94SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmAux[0], 0, MPIU_SCALAR, &a[0]);CHKERRQ(ierr);
360704c51a94SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmAux[1], 0, MPIU_SCALAR, &a[1]);CHKERRQ(ierr);
36086528b96dSMatthew G. Knepley   PetscFunctionReturn(0);
36096528b96dSMatthew G. Knepley }
36106528b96dSMatthew G. Knepley 
36112f856554SMatthew G. Knepley /*@C
36122f856554SMatthew G. Knepley   DMPlexGetFaceFields - Retrieve the field values values for a chunk of faces
36132f856554SMatthew G. Knepley 
36142f856554SMatthew G. Knepley   Input Parameters:
36152f856554SMatthew G. Knepley + dm     - The DM
36162f856554SMatthew G. Knepley . fStart - The first face to include
36172f856554SMatthew G. Knepley . fEnd   - The first face to exclude
36182f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
36192f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
36202f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
36212f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
36222f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
36232f856554SMatthew G. Knepley 
36242f856554SMatthew G. Knepley   Output Parameters:
36252f856554SMatthew G. Knepley + Nface - The number of faces with field values
36262f856554SMatthew G. Knepley . uL - The field values at the left side of the face
36272f856554SMatthew G. Knepley - uR - The field values at the right side of the face
36282f856554SMatthew G. Knepley 
36292f856554SMatthew G. Knepley   Level: developer
36302f856554SMatthew G. Knepley 
36312f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
36322f856554SMatthew G. Knepley @*/
36332f856554SMatthew 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)
36342f856554SMatthew G. Knepley {
36352f856554SMatthew G. Knepley   DM                 dmFace, dmCell, dmGrad = NULL;
36362f856554SMatthew G. Knepley   PetscSection       section;
36372f856554SMatthew G. Knepley   PetscDS            prob;
36382f856554SMatthew G. Knepley   DMLabel            ghostLabel;
36392f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom, *x, *lgrad;
36402f856554SMatthew G. Knepley   PetscBool         *isFE;
36412f856554SMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, numFaces = fEnd - fStart, iface, face;
36422f856554SMatthew G. Knepley   PetscErrorCode     ierr;
36432f856554SMatthew G. Knepley 
36442f856554SMatthew G. Knepley   PetscFunctionBegin;
36452f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36462f856554SMatthew G. Knepley   PetscValidHeaderSpecific(locX, VEC_CLASSID, 4);
36472f856554SMatthew G. Knepley   if (locX_t) {PetscValidHeaderSpecific(locX_t, VEC_CLASSID, 5);}
36482f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 6);
36492f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 7);
36502f856554SMatthew G. Knepley   if (locGrad) {PetscValidHeaderSpecific(locGrad, VEC_CLASSID, 8);}
3651064a246eSJacob Faibussowitsch   PetscValidPointer(uL, 10);
3652064a246eSJacob Faibussowitsch   PetscValidPointer(uR, 11);
36532f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
36542f856554SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
365592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
36562f856554SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
36572f856554SMatthew G. Knepley   ierr = PetscDSGetTotalComponents(prob, &Nc);CHKERRQ(ierr);
36582f856554SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &isFE);CHKERRQ(ierr);
36592f856554SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
36602f856554SMatthew G. Knepley     PetscObject  obj;
36612f856554SMatthew G. Knepley     PetscClassId id;
36622f856554SMatthew G. Knepley 
36632f856554SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
36642f856554SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
36652f856554SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
36662f856554SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
36672f856554SMatthew G. Knepley     else                            {isFE[f] = PETSC_FALSE;}
36682f856554SMatthew G. Knepley   }
36692f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
36702f856554SMatthew G. Knepley   ierr = VecGetArrayRead(locX, &x);CHKERRQ(ierr);
36712f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
36722f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
36732f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
36742f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
36752f856554SMatthew G. Knepley   if (locGrad) {
36762f856554SMatthew G. Knepley     ierr = VecGetDM(locGrad, &dmGrad);CHKERRQ(ierr);
36772f856554SMatthew G. Knepley     ierr = VecGetArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
36782f856554SMatthew G. Knepley   }
36792f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uL);CHKERRQ(ierr);
36802f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*Nc, MPIU_SCALAR, uR);CHKERRQ(ierr);
36812f856554SMatthew G. Knepley   /* Right now just eat the extra work for FE (could make a cell loop) */
36822f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
36832f856554SMatthew G. Knepley     const PetscInt        *cells;
36842f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
36852f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
36862f856554SMatthew G. Knepley     PetscScalar           *xL, *xR, *gL, *gR;
36872f856554SMatthew G. Knepley     PetscScalar           *uLl = *uL, *uRl = *uR;
36882f856554SMatthew G. Knepley     PetscInt               ghost, nsupp, nchild;
36892f856554SMatthew G. Knepley 
36902f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
36912f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
36922f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
36932f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
36942f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
36952f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
36962f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
36972f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
36982f856554SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
36992f856554SMatthew G. Knepley       PetscInt off;
37002f856554SMatthew G. Knepley 
37012f856554SMatthew G. Knepley       ierr = PetscDSGetComponentOffset(prob, f, &off);CHKERRQ(ierr);
37022f856554SMatthew G. Knepley       if (isFE[f]) {
37032f856554SMatthew G. Knepley         const PetscInt *cone;
37042f856554SMatthew G. Knepley         PetscInt        comp, coneSizeL, coneSizeR, faceLocL, faceLocR, ldof, rdof, d;
37052f856554SMatthew G. Knepley 
37062f856554SMatthew G. Knepley         xL = xR = NULL;
37072f856554SMatthew G. Knepley         ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
37082f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
37092f856554SMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
37102f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[0], &cone);CHKERRQ(ierr);
37112f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[0], &coneSizeL);CHKERRQ(ierr);
37122f856554SMatthew G. Knepley         for (faceLocL = 0; faceLocL < coneSizeL; ++faceLocL) if (cone[faceLocL] == face) break;
37132f856554SMatthew G. Knepley         ierr = DMPlexGetCone(dm, cells[1], &cone);CHKERRQ(ierr);
37142f856554SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cells[1], &coneSizeR);CHKERRQ(ierr);
37152f856554SMatthew G. Knepley         for (faceLocR = 0; faceLocR < coneSizeR; ++faceLocR) if (cone[faceLocR] == face) break;
37162c71b3e2SJacob Faibussowitsch         PetscCheckFalse(faceLocL == coneSizeL && faceLocR == coneSizeR,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not find face %D in cone of cell %D or cell %D", face, cells[0], cells[1]);
37172f856554SMatthew G. Knepley         /* Check that FEM field has values in the right cell (sometimes its an FV ghost cell) */
37182f856554SMatthew G. Knepley         /* TODO: this is a hack that might not be right for nonconforming */
37192f856554SMatthew G. Knepley         if (faceLocL < coneSizeL) {
3720a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocL, xL, &uLl[iface*Nc+off]);CHKERRQ(ierr);
3721a8f1f9e5SMatthew G. Knepley           if (rdof == ldof && faceLocR < coneSizeR) {ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);}
37222f856554SMatthew G. Knepley           else              {for (d = 0; d < comp; ++d) uRl[iface*Nc+off+d] = uLl[iface*Nc+off+d];}
37232f856554SMatthew G. Knepley         }
37242f856554SMatthew G. Knepley         else {
3725a8f1f9e5SMatthew G. Knepley           ierr = PetscFEEvaluateFaceFields_Internal(prob, f, faceLocR, xR, &uRl[iface*Nc+off]);CHKERRQ(ierr);
37262f856554SMatthew G. Knepley           ierr = PetscSectionGetFieldComponents(section, f, &comp);CHKERRQ(ierr);
37272f856554SMatthew G. Knepley           for (d = 0; d < comp; ++d) uLl[iface*Nc+off+d] = uRl[iface*Nc+off+d];
37282f856554SMatthew G. Knepley         }
37292f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[0], &ldof, (PetscScalar **) &xL);CHKERRQ(ierr);
37302f856554SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, locX, cells[1], &rdof, (PetscScalar **) &xR);CHKERRQ(ierr);
37312f856554SMatthew G. Knepley       } else {
37322f856554SMatthew G. Knepley         PetscFV  fv;
37332f856554SMatthew G. Knepley         PetscInt numComp, c;
37342f856554SMatthew G. Knepley 
37352f856554SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, (PetscObject *) &fv);CHKERRQ(ierr);
37362f856554SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &numComp);CHKERRQ(ierr);
37372f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[0], f, x, &xL);CHKERRQ(ierr);
37382f856554SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRead(dm, cells[1], f, x, &xR);CHKERRQ(ierr);
37392f856554SMatthew G. Knepley         if (dmGrad) {
37402f856554SMatthew G. Knepley           PetscReal dxL[3], dxR[3];
37412f856554SMatthew G. Knepley 
37422f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[0], lgrad, &gL);CHKERRQ(ierr);
37432f856554SMatthew G. Knepley           ierr = DMPlexPointLocalRead(dmGrad, cells[1], lgrad, &gR);CHKERRQ(ierr);
37442f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgL->centroid, fg->centroid, dxL);
37452f856554SMatthew G. Knepley           DMPlex_WaxpyD_Internal(dim, -1, cgR->centroid, fg->centroid, dxR);
37462f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
37472f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c] + DMPlex_DotD_Internal(dim, &gL[c*dim], dxL);
37482f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c] + DMPlex_DotD_Internal(dim, &gR[c*dim], dxR);
37492f856554SMatthew G. Knepley           }
37502f856554SMatthew G. Knepley         } else {
37512f856554SMatthew G. Knepley           for (c = 0; c < numComp; ++c) {
37522f856554SMatthew G. Knepley             uLl[iface*Nc+off+c] = xL[c];
37532f856554SMatthew G. Knepley             uRl[iface*Nc+off+c] = xR[c];
37542f856554SMatthew G. Knepley           }
37552f856554SMatthew G. Knepley         }
37562f856554SMatthew G. Knepley       }
37572f856554SMatthew G. Knepley     }
37582f856554SMatthew G. Knepley     ++iface;
37592f856554SMatthew G. Knepley   }
37602f856554SMatthew G. Knepley   *Nface = iface;
37612f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(locX, &x);CHKERRQ(ierr);
37622f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
37632f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
37642f856554SMatthew G. Knepley   if (locGrad) {
37652f856554SMatthew G. Knepley     ierr = VecRestoreArrayRead(locGrad, &lgrad);CHKERRQ(ierr);
37662f856554SMatthew G. Knepley   }
37672f856554SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
37682f856554SMatthew G. Knepley   PetscFunctionReturn(0);
37692f856554SMatthew G. Knepley }
37702f856554SMatthew G. Knepley 
37712f856554SMatthew G. Knepley /*@C
37722f856554SMatthew G. Knepley   DMPlexRestoreFaceFields - Restore the field values values for a chunk of faces
37732f856554SMatthew G. Knepley 
37742f856554SMatthew G. Knepley   Input Parameters:
37752f856554SMatthew G. Knepley + dm     - The DM
37762f856554SMatthew G. Knepley . fStart - The first face to include
37772f856554SMatthew G. Knepley . fEnd   - The first face to exclude
37782f856554SMatthew G. Knepley . locX   - A local vector with the solution fields
37792f856554SMatthew G. Knepley . locX_t - A local vector with solution field time derivatives, or NULL
37802f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
37812f856554SMatthew G. Knepley . cellGeometry - A local vector with cell geometry
37822f856554SMatthew G. Knepley - locaGrad - A local vector with field gradients, or NULL
37832f856554SMatthew G. Knepley 
37842f856554SMatthew G. Knepley   Output Parameters:
37852f856554SMatthew G. Knepley + Nface - The number of faces with field values
37862f856554SMatthew G. Knepley . uL - The field values at the left side of the face
37872f856554SMatthew G. Knepley - uR - The field values at the right side of the face
37882f856554SMatthew G. Knepley 
37892f856554SMatthew G. Knepley   Level: developer
37902f856554SMatthew G. Knepley 
37912f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
37922f856554SMatthew G. Knepley @*/
37932f856554SMatthew 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)
37942f856554SMatthew G. Knepley {
37952f856554SMatthew G. Knepley   PetscErrorCode ierr;
37962f856554SMatthew G. Knepley 
37972f856554SMatthew G. Knepley   PetscFunctionBegin;
37982f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uL);CHKERRQ(ierr);
37992f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_SCALAR, uR);CHKERRQ(ierr);
38002f856554SMatthew G. Knepley   PetscFunctionReturn(0);
38012f856554SMatthew G. Knepley }
38022f856554SMatthew G. Knepley 
38032f856554SMatthew G. Knepley /*@C
38042f856554SMatthew G. Knepley   DMPlexGetFaceGeometry - Retrieve the geometric values for a chunk of faces
38052f856554SMatthew G. Knepley 
38062f856554SMatthew G. Knepley   Input Parameters:
38072f856554SMatthew G. Knepley + dm     - The DM
38082f856554SMatthew G. Knepley . fStart - The first face to include
38092f856554SMatthew G. Knepley . fEnd   - The first face to exclude
38102f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
38112f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
38122f856554SMatthew G. Knepley 
38132f856554SMatthew G. Knepley   Output Parameters:
38142f856554SMatthew G. Knepley + Nface - The number of faces with field values
38152f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
38162f856554SMatthew G. Knepley - vol   - The cell volume
38172f856554SMatthew G. Knepley 
38182f856554SMatthew G. Knepley   Level: developer
38192f856554SMatthew G. Knepley 
38202f856554SMatthew G. Knepley .seealso: DMPlexGetCellFields()
38212f856554SMatthew G. Knepley @*/
38222f856554SMatthew G. Knepley PetscErrorCode DMPlexGetFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
38232f856554SMatthew G. Knepley {
38242f856554SMatthew G. Knepley   DM                 dmFace, dmCell;
38252f856554SMatthew G. Knepley   DMLabel            ghostLabel;
38262f856554SMatthew G. Knepley   const PetscScalar *facegeom, *cellgeom;
38272f856554SMatthew G. Knepley   PetscInt           dim, numFaces = fEnd - fStart, iface, face;
38282f856554SMatthew G. Knepley   PetscErrorCode     ierr;
38292f856554SMatthew G. Knepley 
38302f856554SMatthew G. Knepley   PetscFunctionBegin;
38312f856554SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38322f856554SMatthew G. Knepley   PetscValidHeaderSpecific(faceGeometry, VEC_CLASSID, 4);
38332f856554SMatthew G. Knepley   PetscValidHeaderSpecific(cellGeometry, VEC_CLASSID, 5);
3834064a246eSJacob Faibussowitsch   PetscValidPointer(fgeom, 7);
3835064a246eSJacob Faibussowitsch   PetscValidPointer(vol, 8);
38362f856554SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
38372f856554SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
38382f856554SMatthew G. Knepley   ierr = VecGetDM(faceGeometry, &dmFace);CHKERRQ(ierr);
38392f856554SMatthew G. Knepley   ierr = VecGetArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
38402f856554SMatthew G. Knepley   ierr = VecGetDM(cellGeometry, &dmCell);CHKERRQ(ierr);
38412f856554SMatthew G. Knepley   ierr = VecGetArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
38422f856554SMatthew G. Knepley   ierr = PetscMalloc1(numFaces, fgeom);CHKERRQ(ierr);
38432f856554SMatthew G. Knepley   ierr = DMGetWorkArray(dm, numFaces*2, MPIU_SCALAR, vol);CHKERRQ(ierr);
38442f856554SMatthew G. Knepley   for (face = fStart, iface = 0; face < fEnd; ++face) {
38452f856554SMatthew G. Knepley     const PetscInt        *cells;
38462f856554SMatthew G. Knepley     PetscFVFaceGeom       *fg;
38472f856554SMatthew G. Knepley     PetscFVCellGeom       *cgL, *cgR;
38482f856554SMatthew G. Knepley     PetscFVFaceGeom       *fgeoml = *fgeom;
38492f856554SMatthew G. Knepley     PetscReal             *voll   = *vol;
38502f856554SMatthew G. Knepley     PetscInt               ghost, d, nchild, nsupp;
38512f856554SMatthew G. Knepley 
38522f856554SMatthew G. Knepley     ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
38532f856554SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
38542f856554SMatthew G. Knepley     ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
38552f856554SMatthew G. Knepley     if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
38562f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmFace, face, facegeom, &fg);CHKERRQ(ierr);
38572f856554SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, face, &cells);CHKERRQ(ierr);
38582f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[0], cellgeom, &cgL);CHKERRQ(ierr);
38592f856554SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dmCell, cells[1], cellgeom, &cgR);CHKERRQ(ierr);
38602f856554SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
38612f856554SMatthew G. Knepley       fgeoml[iface].centroid[d] = fg->centroid[d];
38622f856554SMatthew G. Knepley       fgeoml[iface].normal[d]   = fg->normal[d];
38632f856554SMatthew G. Knepley     }
38642f856554SMatthew G. Knepley     voll[iface*2+0] = cgL->volume;
38652f856554SMatthew G. Knepley     voll[iface*2+1] = cgR->volume;
38662f856554SMatthew G. Knepley     ++iface;
38672f856554SMatthew G. Knepley   }
38682f856554SMatthew G. Knepley   *Nface = iface;
38692f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(faceGeometry, &facegeom);CHKERRQ(ierr);
38702f856554SMatthew G. Knepley   ierr = VecRestoreArrayRead(cellGeometry, &cellgeom);CHKERRQ(ierr);
38712f856554SMatthew G. Knepley   PetscFunctionReturn(0);
38722f856554SMatthew G. Knepley }
38732f856554SMatthew G. Knepley 
38742f856554SMatthew G. Knepley /*@C
38752f856554SMatthew G. Knepley   DMPlexRestoreFaceGeometry - Restore the field values values for a chunk of faces
38762f856554SMatthew G. Knepley 
38772f856554SMatthew G. Knepley   Input Parameters:
38782f856554SMatthew G. Knepley + dm     - The DM
38792f856554SMatthew G. Knepley . fStart - The first face to include
38802f856554SMatthew G. Knepley . fEnd   - The first face to exclude
38812f856554SMatthew G. Knepley . faceGeometry - A local vector with face geometry
38822f856554SMatthew G. Knepley - cellGeometry - A local vector with cell geometry
38832f856554SMatthew G. Knepley 
38842f856554SMatthew G. Knepley   Output Parameters:
38852f856554SMatthew G. Knepley + Nface - The number of faces with field values
38862f856554SMatthew G. Knepley . fgeom - The extract the face centroid and normal
38872f856554SMatthew G. Knepley - vol   - The cell volume
38882f856554SMatthew G. Knepley 
38892f856554SMatthew G. Knepley   Level: developer
38902f856554SMatthew G. Knepley 
38912f856554SMatthew G. Knepley .seealso: DMPlexGetFaceFields()
38922f856554SMatthew G. Knepley @*/
38932f856554SMatthew G. Knepley PetscErrorCode DMPlexRestoreFaceGeometry(DM dm, PetscInt fStart, PetscInt fEnd, Vec faceGeometry, Vec cellGeometry, PetscInt *Nface, PetscFVFaceGeom **fgeom, PetscReal **vol)
38942f856554SMatthew G. Knepley {
38952f856554SMatthew G. Knepley   PetscErrorCode ierr;
38962f856554SMatthew G. Knepley 
38972f856554SMatthew G. Knepley   PetscFunctionBegin;
38982f856554SMatthew G. Knepley   ierr = PetscFree(*fgeom);CHKERRQ(ierr);
38992f856554SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, MPIU_REAL, vol);CHKERRQ(ierr);
39002f856554SMatthew G. Knepley   PetscFunctionReturn(0);
39012f856554SMatthew G. Knepley }
39022f856554SMatthew G. Knepley 
3903a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESGetFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3904a1cf66bbSMatthew G. Knepley {
3905a1cf66bbSMatthew G. Knepley   char            composeStr[33] = {0};
3906a1cf66bbSMatthew G. Knepley   PetscObjectId   id;
3907a1cf66bbSMatthew G. Knepley   PetscContainer  container;
3908a1cf66bbSMatthew G. Knepley   PetscErrorCode  ierr;
3909a1cf66bbSMatthew G. Knepley 
3910a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3911a1cf66bbSMatthew G. Knepley   ierr = PetscObjectGetId((PetscObject)quad,&id);CHKERRQ(ierr);
3912a1cf66bbSMatthew G. Knepley   ierr = PetscSNPrintf(composeStr, 32, "DMSNESGetFEGeom_%x\n", id);CHKERRQ(ierr);
3913a1cf66bbSMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) pointIS, composeStr, (PetscObject *) &container);CHKERRQ(ierr);
3914a1cf66bbSMatthew G. Knepley   if (container) {
3915a1cf66bbSMatthew G. Knepley     ierr = PetscContainerGetPointer(container, (void **) geom);CHKERRQ(ierr);
3916a1cf66bbSMatthew G. Knepley   } else {
3917a1cf66bbSMatthew G. Knepley     ierr = DMFieldCreateFEGeom(coordField, pointIS, quad, faceData, geom);CHKERRQ(ierr);
3918a1cf66bbSMatthew G. Knepley     ierr = PetscContainerCreate(PETSC_COMM_SELF,&container);CHKERRQ(ierr);
3919a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetPointer(container, (void *) *geom);CHKERRQ(ierr);
3920a1cf66bbSMatthew G. Knepley     ierr = PetscContainerSetUserDestroy(container, PetscContainerUserDestroy_PetscFEGeom);CHKERRQ(ierr);
3921a1cf66bbSMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) pointIS, composeStr, (PetscObject) container);CHKERRQ(ierr);
3922a1cf66bbSMatthew G. Knepley     ierr = PetscContainerDestroy(&container);CHKERRQ(ierr);
3923a1cf66bbSMatthew G. Knepley   }
3924a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3925a1cf66bbSMatthew G. Knepley }
3926a1cf66bbSMatthew G. Knepley 
3927a1cf66bbSMatthew G. Knepley PetscErrorCode DMSNESRestoreFEGeom(DMField coordField, IS pointIS, PetscQuadrature quad, PetscBool faceData, PetscFEGeom **geom)
3928a1cf66bbSMatthew G. Knepley {
3929a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
3930a1cf66bbSMatthew G. Knepley   *geom = NULL;
3931a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
3932a1cf66bbSMatthew G. Knepley }
3933a1cf66bbSMatthew G. Knepley 
393492d50984SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM dm, PetscSection section, IS cellIS, PetscReal t, Vec locX, Vec locX_t, Vec locF, void *user)
393592d50984SMatthew G. Knepley {
393692d50984SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
393792d50984SMatthew G. Knepley   const char      *name       = "Residual";
393892d50984SMatthew G. Knepley   DM               dmAux      = NULL;
393992d50984SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
394092d50984SMatthew G. Knepley   PetscDS          prob       = NULL;
394192d50984SMatthew G. Knepley   PetscDS          probAux    = NULL;
394292d50984SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
394392d50984SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || t == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
394492d50984SMatthew G. Knepley   DMField          coordField = NULL;
3945c0006e53SPatrick Farrell   Vec              locA;
3946c0006e53SPatrick Farrell   PetscScalar     *u = NULL, *u_t, *a, *uL = NULL, *uR = NULL;
394792d50984SMatthew G. Knepley   IS               chunkIS;
394892d50984SMatthew G. Knepley   const PetscInt  *cells;
394992d50984SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
3950364207b6SKarl Rupp   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, chunk, fStart, fEnd;
395192d50984SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
395206ad1575SMatthew G. Knepley   PetscFormKey key;
395392d50984SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
395492d50984SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
395592d50984SMatthew G. Knepley   PetscErrorCode   ierr;
395692d50984SMatthew G. Knepley 
395792d50984SMatthew G. Knepley   PetscFunctionBegin;
395892d50984SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
395992d50984SMatthew G. Knepley   /* FEM+FVM */
396092d50984SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
396192d50984SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
396292d50984SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
396392d50984SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
396492d50984SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
3965ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, 0, &locA);CHKERRQ(ierr);
396692d50984SMatthew G. Knepley   if (locA) {
396792d50984SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
396892d50984SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
396992d50984SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
397092d50984SMatthew G. Knepley   }
397192d50984SMatthew G. Knepley   /* 2: Get geometric data */
397292d50984SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
397392d50984SMatthew G. Knepley     PetscObject  obj;
397492d50984SMatthew G. Knepley     PetscClassId id;
397592d50984SMatthew G. Knepley     PetscBool    fimp;
397692d50984SMatthew G. Knepley 
397792d50984SMatthew G. Knepley     ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
397892d50984SMatthew G. Knepley     if (isImplicit != fimp) continue;
397992d50984SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
398092d50984SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
398192d50984SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
39822c71b3e2SJacob Faibussowitsch     PetscCheckFalse(id == PETSCFV_CLASSID,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Use of FVM with PCPATCH not yet implemented");
398392d50984SMatthew G. Knepley   }
398492d50984SMatthew G. Knepley   if (useFEM) {
398592d50984SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
398692d50984SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
398792d50984SMatthew G. Knepley     if (maxDegree <= 1) {
398892d50984SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
398992d50984SMatthew G. Knepley       if (affineQuad) {
399092d50984SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
399192d50984SMatthew G. Knepley       }
399292d50984SMatthew G. Knepley     } else {
399392d50984SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
399492d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
399592d50984SMatthew G. Knepley         PetscObject  obj;
399692d50984SMatthew G. Knepley         PetscClassId id;
399792d50984SMatthew G. Knepley         PetscBool    fimp;
399892d50984SMatthew G. Knepley 
399992d50984SMatthew G. Knepley         ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
400092d50984SMatthew G. Knepley         if (isImplicit != fimp) continue;
400192d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
400292d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
400392d50984SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
400492d50984SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
400592d50984SMatthew G. Knepley 
400692d50984SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
400792d50984SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
400892d50984SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
400992d50984SMatthew G. Knepley         }
401092d50984SMatthew G. Knepley       }
401192d50984SMatthew G. Knepley     }
401292d50984SMatthew G. Knepley   }
401392d50984SMatthew G. Knepley   /* Loop over chunks */
401492d50984SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
401592d50984SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
401692d50984SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
401792d50984SMatthew G. Knepley   numCells      = cEnd - cStart;
401892d50984SMatthew G. Knepley   numChunks     = 1;
401992d50984SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
402092d50984SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
40216528b96dSMatthew G. Knepley   key.label     = NULL;
40226528b96dSMatthew G. Knepley   key.value     = 0;
402306ad1575SMatthew G. Knepley   key.part      = 0;
402492d50984SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
4025c0006e53SPatrick Farrell     PetscScalar     *elemVec, *fluxL = NULL, *fluxR = NULL;
4026c0006e53SPatrick Farrell     PetscReal       *vol = NULL;
4027c0006e53SPatrick Farrell     PetscFVFaceGeom *fgeom = NULL;
402892d50984SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
4029c0006e53SPatrick Farrell     PetscInt         numFaces = 0;
403092d50984SMatthew G. Knepley 
403192d50984SMatthew G. Knepley     /* Extract field coefficients */
403292d50984SMatthew G. Knepley     if (useFEM) {
403392d50984SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
403492d50984SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
403592d50984SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
4036580bdb30SBarry Smith       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
403792d50984SMatthew G. Knepley     }
403892d50984SMatthew 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 */
403992d50984SMatthew G. Knepley     /* Loop over fields */
404092d50984SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
404192d50984SMatthew G. Knepley       PetscObject  obj;
404292d50984SMatthew G. Knepley       PetscClassId id;
404392d50984SMatthew G. Knepley       PetscBool    fimp;
404492d50984SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
404592d50984SMatthew G. Knepley 
40466528b96dSMatthew G. Knepley       key.field = f;
404792d50984SMatthew G. Knepley       ierr = PetscDSGetImplicit(prob, f, &fimp);CHKERRQ(ierr);
404892d50984SMatthew G. Knepley       if (isImplicit != fimp) continue;
404992d50984SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
405092d50984SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
405192d50984SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
405292d50984SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
405392d50984SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
405492d50984SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
405592d50984SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
405692d50984SMatthew G. Knepley         PetscInt        Nq, Nb;
405792d50984SMatthew G. Knepley 
405892d50984SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
405992d50984SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
406092d50984SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
406192d50984SMatthew G. Knepley         blockSize = Nb;
406292d50984SMatthew G. Knepley         batchSize = numBlocks * blockSize;
406392d50984SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
406492d50984SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
406592d50984SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
406692d50984SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
406792d50984SMatthew G. Knepley         offset    = numCells - Nr;
406892d50984SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
406992d50984SMatthew 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) */
407092d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
40716528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, key, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
407292d50984SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
40736528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(prob, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
407492d50984SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
407592d50984SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
407692d50984SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
407792d50984SMatthew G. Knepley 
407892d50984SMatthew G. Knepley         Ne = numFaces;
407992d50984SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
408092d50984SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
408192d50984SMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, prob, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
408298921bdaSJacob Faibussowitsch       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
408392d50984SMatthew G. Knepley     }
408492d50984SMatthew G. Knepley     /* Loop over domain */
408592d50984SMatthew G. Knepley     if (useFEM) {
408692d50984SMatthew G. Knepley       /* Add elemVec to locX */
408792d50984SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
408892d50984SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
408992d50984SMatthew G. Knepley         const PetscInt cind = c - cStart;
409092d50984SMatthew G. Knepley 
409192d50984SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
409292d50984SMatthew G. Knepley         if (ghostLabel) {
409392d50984SMatthew G. Knepley           PetscInt ghostVal;
409492d50984SMatthew G. Knepley 
409592d50984SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
409692d50984SMatthew G. Knepley           if (ghostVal > 0) continue;
409792d50984SMatthew G. Knepley         }
409892d50984SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
409992d50984SMatthew G. Knepley       }
410092d50984SMatthew G. Knepley     }
410192d50984SMatthew G. Knepley     /* Handle time derivative */
410292d50984SMatthew G. Knepley     if (locX_t) {
410392d50984SMatthew G. Knepley       PetscScalar *x_t, *fa;
410492d50984SMatthew G. Knepley 
410592d50984SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
410692d50984SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
410792d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
410892d50984SMatthew G. Knepley         PetscFV      fv;
410992d50984SMatthew G. Knepley         PetscObject  obj;
411092d50984SMatthew G. Knepley         PetscClassId id;
411192d50984SMatthew G. Knepley         PetscInt     pdim, d;
411292d50984SMatthew G. Knepley 
411392d50984SMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, f, &obj);CHKERRQ(ierr);
411492d50984SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
411592d50984SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
411692d50984SMatthew G. Knepley         fv   = (PetscFV) obj;
411792d50984SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
411892d50984SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
411992d50984SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
412092d50984SMatthew G. Knepley           PetscScalar   *u_t, *r;
412192d50984SMatthew G. Knepley 
412292d50984SMatthew G. Knepley           if (ghostLabel) {
412392d50984SMatthew G. Knepley             PetscInt ghostVal;
412492d50984SMatthew G. Knepley 
412592d50984SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
412692d50984SMatthew G. Knepley             if (ghostVal > 0) continue;
412792d50984SMatthew G. Knepley           }
412892d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
412992d50984SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
413092d50984SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
413192d50984SMatthew G. Knepley         }
413292d50984SMatthew G. Knepley       }
413392d50984SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
413492d50984SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
413592d50984SMatthew G. Knepley     }
413692d50984SMatthew G. Knepley     if (useFEM) {
413792d50984SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
413892d50984SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
413992d50984SMatthew G. Knepley     }
414092d50984SMatthew G. Knepley   }
414192d50984SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
414292d50984SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
414392d50984SMatthew G. Knepley   /* TODO Could include boundary residual here (see DMPlexComputeResidual_Internal) */
414492d50984SMatthew G. Knepley   if (useFEM) {
414592d50984SMatthew G. Knepley     if (maxDegree <= 1) {
414692d50984SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
414792d50984SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
414892d50984SMatthew G. Knepley     } else {
414992d50984SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
415092d50984SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
415192d50984SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
415292d50984SMatthew G. Knepley       }
415392d50984SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
415492d50984SMatthew G. Knepley     }
415592d50984SMatthew G. Knepley   }
415692d50984SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
415792d50984SMatthew G. Knepley   PetscFunctionReturn(0);
415892d50984SMatthew G. Knepley }
415992d50984SMatthew G. Knepley 
4160a1cf66bbSMatthew G. Knepley /*
4161a1cf66bbSMatthew 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
4162a1cf66bbSMatthew G. Knepley 
4163a1cf66bbSMatthew G. Knepley   X   - The local solution vector
4164a5b23f4aSJose E. Roman   X_t - The local solution time derivative vector, or NULL
4165a1cf66bbSMatthew G. Knepley */
4166a1cf66bbSMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM dm, PetscSection section, PetscSection globalSection, IS cellIS,
4167a1cf66bbSMatthew G. Knepley                                                     PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP, void *ctx)
4168a1cf66bbSMatthew G. Knepley {
4169a1cf66bbSMatthew G. Knepley   DM_Plex         *mesh  = (DM_Plex *) dm->data;
4170a1cf66bbSMatthew G. Knepley   const char      *name = "Jacobian", *nameP = "JacobianPre";
4171a1cf66bbSMatthew G. Knepley   DM               dmAux = NULL;
4172a1cf66bbSMatthew G. Knepley   PetscDS          prob,   probAux = NULL;
4173a1cf66bbSMatthew G. Knepley   PetscSection     sectionAux = NULL;
4174a1cf66bbSMatthew G. Knepley   Vec              A;
4175a1cf66bbSMatthew G. Knepley   DMField          coordField;
4176a1cf66bbSMatthew G. Knepley   PetscFEGeom     *cgeomFEM;
4177a1cf66bbSMatthew G. Knepley   PetscQuadrature  qGeom = NULL;
4178a1cf66bbSMatthew G. Knepley   Mat              J = Jac, JP = JacP;
4179a1cf66bbSMatthew G. Knepley   PetscScalar     *work, *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL, *elemMatP = NULL, *elemMatD = NULL;
4180e432b41dSStefano Zampini   PetscBool        hasJac, hasPrec, hasDyn, assembleJac, *isFE, hasFV = PETSC_FALSE;
4181a1cf66bbSMatthew G. Knepley   const PetscInt  *cells;
418206ad1575SMatthew G. Knepley   PetscFormKey key;
41839b2fc754SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, maxDegree, numCells, cStart, cEnd, numChunks, chunkSize, chunk, totDim, totDimAux = 0, sz, wsz, off = 0, offCell = 0;
4184a1cf66bbSMatthew G. Knepley   PetscErrorCode   ierr;
4185a1cf66bbSMatthew G. Knepley 
4186a1cf66bbSMatthew G. Knepley   PetscFunctionBegin;
4187a1cf66bbSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
4188a1cf66bbSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
4189a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
4190a1cf66bbSMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4191ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, NULL, 0, 0, &A);CHKERRQ(ierr);
41929a2a23afSMatthew G. Knepley   if (A) {
41939a2a23afSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
419492fd8e1eSJed Brown     ierr = DMGetLocalSection(dmAux, &sectionAux);CHKERRQ(ierr);
4195a1cf66bbSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
4196a1cf66bbSMatthew G. Knepley   }
4197a1cf66bbSMatthew G. Knepley   /* Get flags */
4198a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
4199a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
4200a1cf66bbSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
4201a1cf66bbSMatthew G. Knepley     PetscObject  disc;
4202a1cf66bbSMatthew G. Knepley     PetscClassId id;
4203a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &disc);CHKERRQ(ierr);
4204a1cf66bbSMatthew G. Knepley     ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
4205a1cf66bbSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[fieldI] = PETSC_TRUE;}
4206a1cf66bbSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; isFE[fieldI] = PETSC_FALSE;}
4207a1cf66bbSMatthew G. Knepley   }
4208a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
4209a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
4210a1cf66bbSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
4211a1cf66bbSMatthew G. Knepley   assembleJac = hasJac && hasPrec && (Jac != JacP) ? PETSC_TRUE : PETSC_FALSE;
4212a1cf66bbSMatthew G. Knepley   hasDyn      = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
4213a1cf66bbSMatthew 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 */
4214a1cf66bbSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
4215a1cf66bbSMatthew G. Knepley   if (probAux) {ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);}
4216a1cf66bbSMatthew G. Knepley   /* Compute batch sizes */
4217a1cf66bbSMatthew G. Knepley   if (isFE[0]) {
4218a1cf66bbSMatthew G. Knepley     PetscFE         fe;
4219a1cf66bbSMatthew G. Knepley     PetscQuadrature q;
4220a1cf66bbSMatthew G. Knepley     PetscInt        numQuadPoints, numBatches, batchSize, numBlocks, blockSize, Nb;
4221a1cf66bbSMatthew G. Knepley 
4222a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
4223a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &q);CHKERRQ(ierr);
4224a1cf66bbSMatthew G. Knepley     ierr = PetscQuadratureGetData(q, NULL, NULL, &numQuadPoints, NULL, NULL);CHKERRQ(ierr);
4225a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
4226a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
4227a1cf66bbSMatthew G. Knepley     blockSize = Nb*numQuadPoints;
4228a1cf66bbSMatthew G. Knepley     batchSize = numBlocks  * blockSize;
4229a1cf66bbSMatthew G. Knepley     chunkSize = numBatches * batchSize;
4230a1cf66bbSMatthew G. Knepley     numChunks = numCells / chunkSize + numCells % chunkSize;
4231a1cf66bbSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
4232a1cf66bbSMatthew G. Knepley   } else {
4233a1cf66bbSMatthew G. Knepley     chunkSize = numCells;
4234a1cf66bbSMatthew G. Knepley     numChunks = 1;
4235a1cf66bbSMatthew G. Knepley   }
4236a1cf66bbSMatthew G. Knepley   /* Get work space */
4237a1cf66bbSMatthew 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;
4238a1cf66bbSMatthew G. Knepley   ierr = DMGetWorkArray(dm, wsz, MPIU_SCALAR, &work);CHKERRQ(ierr);
4239580bdb30SBarry Smith   ierr = PetscArrayzero(work, wsz);CHKERRQ(ierr);
4240a1cf66bbSMatthew G. Knepley   off      = 0;
4241a1cf66bbSMatthew G. Knepley   u        = X       ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
4242a1cf66bbSMatthew G. Knepley   u_t      = X_t     ? (sz = chunkSize*totDim,        off += sz, work+off-sz) : NULL;
4243a1cf66bbSMatthew G. Knepley   a        = dmAux   ? (sz = chunkSize*totDimAux,     off += sz, work+off-sz) : NULL;
4244a1cf66bbSMatthew G. Knepley   elemMat  = hasJac  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
4245a1cf66bbSMatthew G. Knepley   elemMatP = hasPrec ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
4246a1cf66bbSMatthew G. Knepley   elemMatD = hasDyn  ? (sz = chunkSize*totDim*totDim, off += sz, work+off-sz) : NULL;
42472c71b3e2SJacob Faibussowitsch   PetscCheckFalse(off != wsz,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Error is workspace size %D should be %D", off, wsz);
4248a1cf66bbSMatthew G. Knepley   /* Setup geometry */
4249a1cf66bbSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
42509b2fc754SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
42519b2fc754SMatthew G. Knepley   if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField, cellIS, &qGeom);CHKERRQ(ierr);}
4252a1cf66bbSMatthew G. Knepley   if (!qGeom) {
4253a1cf66bbSMatthew G. Knepley     PetscFE fe;
4254a1cf66bbSMatthew G. Knepley 
4255a1cf66bbSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, 0, (PetscObject *) &fe);CHKERRQ(ierr);
4256a1cf66bbSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &qGeom);CHKERRQ(ierr);
4257a1cf66bbSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) qGeom);CHKERRQ(ierr);
4258a1cf66bbSMatthew G. Knepley   }
4259a1cf66bbSMatthew G. Knepley   ierr = DMSNESGetFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
4260a1cf66bbSMatthew G. Knepley   /* Compute volume integrals */
4261a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatZeroEntries(J);CHKERRQ(ierr);}
4262a1cf66bbSMatthew G. Knepley   ierr = MatZeroEntries(JP);CHKERRQ(ierr);
42636528b96dSMatthew G. Knepley   key.label = NULL;
42646528b96dSMatthew G. Knepley   key.value = 0;
426506ad1575SMatthew G. Knepley   key.part  = 0;
4266a1cf66bbSMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk, offCell += chunkSize) {
4267a1cf66bbSMatthew G. Knepley     const PetscInt   Ncell = PetscMin(chunkSize, numCells - offCell);
4268a1cf66bbSMatthew G. Knepley     PetscInt         c;
4269a1cf66bbSMatthew G. Knepley 
4270a1cf66bbSMatthew G. Knepley     /* Extract values */
4271a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
4272a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
4273a1cf66bbSMatthew G. Knepley       PetscScalar   *x = NULL,  *x_t = NULL;
4274a1cf66bbSMatthew G. Knepley       PetscInt       i;
4275a1cf66bbSMatthew G. Knepley 
4276a1cf66bbSMatthew G. Knepley       if (X) {
4277a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
4278a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u[c*totDim+i] = x[i];
4279a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
4280a1cf66bbSMatthew G. Knepley       }
4281a1cf66bbSMatthew G. Knepley       if (X_t) {
4282a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
4283a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[c*totDim+i] = x_t[i];
4284a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
4285a1cf66bbSMatthew G. Knepley       }
4286a1cf66bbSMatthew G. Knepley       if (dmAux) {
4287a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
4288a1cf66bbSMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[c*totDimAux+i] = x[i];
4289a1cf66bbSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dmAux, sectionAux, A, cell, NULL, &x);CHKERRQ(ierr);
4290a1cf66bbSMatthew G. Knepley       }
4291a1cf66bbSMatthew G. Knepley     }
4292a1cf66bbSMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
4293a1cf66bbSMatthew G. Knepley       PetscFE fe;
4294a1cf66bbSMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
4295a1cf66bbSMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
42966528b96dSMatthew G. Knepley         key.field = fieldI*Nf + fieldJ;
42976528b96dSMatthew G. Knepley         if (hasJac)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN,     key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);}
42986528b96dSMatthew G. Knepley         if (hasPrec) {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);}
42996528b96dSMatthew G. Knepley         if (hasDyn)  {ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ncell, cgeomFEM, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);}
4300a1cf66bbSMatthew G. Knepley       }
4301a1cf66bbSMatthew G. Knepley       /* For finite volume, add the identity */
4302a1cf66bbSMatthew G. Knepley       if (!isFE[fieldI]) {
4303a1cf66bbSMatthew G. Knepley         PetscFV  fv;
4304a1cf66bbSMatthew G. Knepley         PetscInt eOffset = 0, Nc, fc, foff;
4305a1cf66bbSMatthew G. Knepley 
4306a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(prob, fieldI, &foff);CHKERRQ(ierr);
4307a1cf66bbSMatthew G. Knepley         ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
4308a1cf66bbSMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &Nc);CHKERRQ(ierr);
4309a1cf66bbSMatthew G. Knepley         for (c = 0; c < chunkSize; ++c, eOffset += totDim*totDim) {
4310a1cf66bbSMatthew G. Knepley           for (fc = 0; fc < Nc; ++fc) {
4311a1cf66bbSMatthew G. Knepley             const PetscInt i = foff + fc;
4312a1cf66bbSMatthew G. Knepley             if (hasJac)  {elemMat [eOffset+i*totDim+i] = 1.0;}
4313a1cf66bbSMatthew G. Knepley             if (hasPrec) {elemMatP[eOffset+i*totDim+i] = 1.0;}
4314a1cf66bbSMatthew G. Knepley           }
4315a1cf66bbSMatthew G. Knepley         }
4316a1cf66bbSMatthew G. Knepley       }
4317a1cf66bbSMatthew G. Knepley     }
4318a1cf66bbSMatthew G. Knepley     /*   Add contribution from X_t */
4319a1cf66bbSMatthew G. Knepley     if (hasDyn) {for (c = 0; c < chunkSize*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
4320a1cf66bbSMatthew G. Knepley     /* Insert values into matrix */
4321a1cf66bbSMatthew G. Knepley     for (c = 0; c < Ncell; ++c) {
4322a1cf66bbSMatthew G. Knepley       const PetscInt cell = cells ? cells[c+offCell] : c+offCell;
4323a1cf66bbSMatthew G. Knepley       if (mesh->printFEM > 1) {
4324a1cf66bbSMatthew G. Knepley         if (hasJac)  {ierr = DMPrintCellMatrix(cell, name,  totDim, totDim, &elemMat[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4325a1cf66bbSMatthew G. Knepley         if (hasPrec) {ierr = DMPrintCellMatrix(cell, nameP, totDim, totDim, &elemMatP[(c-cStart)*totDim*totDim]);CHKERRQ(ierr);}
4326a1cf66bbSMatthew G. Knepley       }
4327a1cf66bbSMatthew G. Knepley       if (assembleJac) {ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);}
4328a1cf66bbSMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JP, cell, &elemMat[(c-cStart)*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
4329a1cf66bbSMatthew G. Knepley     }
4330a1cf66bbSMatthew G. Knepley   }
4331a1cf66bbSMatthew G. Knepley   /* Cleanup */
4332a1cf66bbSMatthew G. Knepley   ierr = DMSNESRestoreFEGeom(coordField, cellIS, qGeom, PETSC_FALSE, &cgeomFEM);CHKERRQ(ierr);
4333a1cf66bbSMatthew G. Knepley   ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
4334a1cf66bbSMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
4335a1cf66bbSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, Nf, MPIU_BOOL, &isFE);CHKERRQ(ierr);
4336a1cf66bbSMatthew 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);
4337a1cf66bbSMatthew G. Knepley   /* Compute boundary integrals */
4338a1cf66bbSMatthew G. Knepley   /* ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, ctx);CHKERRQ(ierr); */
4339a1cf66bbSMatthew G. Knepley   /* Assemble matrix */
4340a1cf66bbSMatthew G. Knepley   if (assembleJac) {ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);}
4341a1cf66bbSMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
4342a1cf66bbSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
4343a1cf66bbSMatthew G. Knepley   PetscFunctionReturn(0);
4344a1cf66bbSMatthew G. Knepley }
43453e9753d6SMatthew G. Knepley 
43463e9753d6SMatthew G. Knepley /******** FEM Assembly Function ********/
43473e9753d6SMatthew G. Knepley 
43483e9753d6SMatthew G. Knepley static PetscErrorCode DMConvertPlex_Internal(DM dm, DM *plex, PetscBool copy)
43493e9753d6SMatthew G. Knepley {
43503e9753d6SMatthew G. Knepley   PetscBool      isPlex;
43513e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
43523e9753d6SMatthew G. Knepley 
43533e9753d6SMatthew G. Knepley   PetscFunctionBegin;
43543e9753d6SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) dm, DMPLEX, &isPlex);CHKERRQ(ierr);
43553e9753d6SMatthew G. Knepley   if (isPlex) {
43563e9753d6SMatthew G. Knepley     *plex = dm;
43573e9753d6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
43583e9753d6SMatthew G. Knepley   } else {
43593e9753d6SMatthew G. Knepley     ierr = PetscObjectQuery((PetscObject) dm, "dm_plex", (PetscObject *) plex);CHKERRQ(ierr);
43603e9753d6SMatthew G. Knepley     if (!*plex) {
43613e9753d6SMatthew G. Knepley       ierr = DMConvert(dm,DMPLEX,plex);CHKERRQ(ierr);
43623e9753d6SMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) dm, "dm_plex", (PetscObject) *plex);CHKERRQ(ierr);
43633e9753d6SMatthew G. Knepley       if (copy) {
43649a2a23afSMatthew G. Knepley         ierr = DMCopyAuxiliaryVec(dm, *plex);CHKERRQ(ierr);
43653e9753d6SMatthew G. Knepley       }
43663e9753d6SMatthew G. Knepley     } else {
43673e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject) *plex);CHKERRQ(ierr);
43683e9753d6SMatthew G. Knepley     }
43693e9753d6SMatthew G. Knepley   }
43703e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
43713e9753d6SMatthew G. Knepley }
43723e9753d6SMatthew G. Knepley 
43733e9753d6SMatthew G. Knepley /*@
43743e9753d6SMatthew G. Knepley   DMPlexGetGeometryFVM - Return precomputed geometric data
43753e9753d6SMatthew G. Knepley 
43763e9753d6SMatthew G. Knepley   Collective on DM
43773e9753d6SMatthew G. Knepley 
43783e9753d6SMatthew G. Knepley   Input Parameter:
43793e9753d6SMatthew G. Knepley . dm - The DM
43803e9753d6SMatthew G. Knepley 
43813e9753d6SMatthew G. Knepley   Output Parameters:
43823e9753d6SMatthew G. Knepley + facegeom - The values precomputed from face geometry
43833e9753d6SMatthew G. Knepley . cellgeom - The values precomputed from cell geometry
43843e9753d6SMatthew G. Knepley - minRadius - The minimum radius over the mesh of an inscribed sphere in a cell
43853e9753d6SMatthew G. Knepley 
43863e9753d6SMatthew G. Knepley   Level: developer
43873e9753d6SMatthew G. Knepley 
4388446c23c1SPierre Jolivet .seealso: DMTSSetRHSFunctionLocal()
43893e9753d6SMatthew G. Knepley @*/
43903e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGeometryFVM(DM dm, Vec *facegeom, Vec *cellgeom, PetscReal *minRadius)
43913e9753d6SMatthew G. Knepley {
43923e9753d6SMatthew G. Knepley   DM             plex;
43933e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
43943e9753d6SMatthew G. Knepley 
43953e9753d6SMatthew G. Knepley   PetscFunctionBegin;
43963e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43973e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
43983e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, NULL, cellgeom, facegeom, NULL);CHKERRQ(ierr);
43993e9753d6SMatthew G. Knepley   if (minRadius) {ierr = DMPlexGetMinRadius(plex, minRadius);CHKERRQ(ierr);}
44003e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
44013e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
44023e9753d6SMatthew G. Knepley }
44033e9753d6SMatthew G. Knepley 
44043e9753d6SMatthew G. Knepley /*@
44053e9753d6SMatthew G. Knepley   DMPlexGetGradientDM - Return gradient data layout
44063e9753d6SMatthew G. Knepley 
44073e9753d6SMatthew G. Knepley   Collective on DM
44083e9753d6SMatthew G. Knepley 
44093e9753d6SMatthew G. Knepley   Input Parameters:
44103e9753d6SMatthew G. Knepley + dm - The DM
44113e9753d6SMatthew G. Knepley - fv - The PetscFV
44123e9753d6SMatthew G. Knepley 
44133e9753d6SMatthew G. Knepley   Output Parameter:
44143e9753d6SMatthew G. Knepley . dmGrad - The layout for gradient values
44153e9753d6SMatthew G. Knepley 
44163e9753d6SMatthew G. Knepley   Level: developer
44173e9753d6SMatthew G. Knepley 
4418446c23c1SPierre Jolivet .seealso: DMPlexGetGeometryFVM()
44193e9753d6SMatthew G. Knepley @*/
44203e9753d6SMatthew G. Knepley PetscErrorCode DMPlexGetGradientDM(DM dm, PetscFV fv, DM *dmGrad)
44213e9753d6SMatthew G. Knepley {
44223e9753d6SMatthew G. Knepley   DM             plex;
44233e9753d6SMatthew G. Knepley   PetscBool      computeGradients;
44243e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
44253e9753d6SMatthew G. Knepley 
44263e9753d6SMatthew G. Knepley   PetscFunctionBegin;
44273e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44283e9753d6SMatthew G. Knepley   PetscValidHeaderSpecific(fv,PETSCFV_CLASSID,2);
44293e9753d6SMatthew G. Knepley   PetscValidPointer(dmGrad,3);
44303e9753d6SMatthew G. Knepley   ierr = PetscFVGetComputeGradients(fv, &computeGradients);CHKERRQ(ierr);
44313e9753d6SMatthew G. Knepley   if (!computeGradients) {*dmGrad = NULL; PetscFunctionReturn(0);}
44323e9753d6SMatthew G. Knepley   ierr = DMConvertPlex_Internal(dm,&plex,PETSC_TRUE);CHKERRQ(ierr);
44333e9753d6SMatthew G. Knepley   ierr = DMPlexGetDataFVM(plex, fv, NULL, NULL, dmGrad);CHKERRQ(ierr);
44343e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
44353e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
44363e9753d6SMatthew G. Knepley }
44373e9753d6SMatthew G. Knepley 
44380c290148SMatthew G. Knepley static PetscErrorCode DMPlexComputeBdResidual_Single_Internal(DM dm, PetscReal t, PetscWeakForm wf, PetscFormKey key, Vec locX, Vec locX_t, Vec locF, DMField coordField, IS facetIS)
44393e9753d6SMatthew G. Knepley {
44403e9753d6SMatthew G. Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
44413e9753d6SMatthew G. Knepley   DM               plex = NULL, plexA = NULL;
44423e9753d6SMatthew G. Knepley   DMEnclosureType  encAux;
44433e9753d6SMatthew G. Knepley   PetscDS          prob, probAux = NULL;
44443e9753d6SMatthew G. Knepley   PetscSection     section, sectionAux = NULL;
44453e9753d6SMatthew G. Knepley   Vec              locA = NULL;
44463e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t = NULL, *a = NULL, *elemVec = NULL;
44473e9753d6SMatthew G. Knepley   PetscInt         totDim, totDimAux = 0;
44483e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
44493e9753d6SMatthew G. Knepley 
44503e9753d6SMatthew G. Knepley   PetscFunctionBegin;
44513e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
44523e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
44533e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
44543e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
4455ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, key.part, &locA);CHKERRQ(ierr);
44563e9753d6SMatthew G. Knepley   if (locA) {
44573e9753d6SMatthew G. Knepley     DM dmAux;
44583e9753d6SMatthew G. Knepley 
44593e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
44603e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
44613e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
44623e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
44633e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
44643e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
44653e9753d6SMatthew G. Knepley   }
44660c290148SMatthew G. Knepley   {
44673e9753d6SMatthew G. Knepley     PetscFEGeom     *fgeom;
44683e9753d6SMatthew G. Knepley     PetscInt         maxDegree;
44693e9753d6SMatthew G. Knepley     PetscQuadrature  qGeom = NULL;
44703e9753d6SMatthew G. Knepley     IS               pointIS;
44713e9753d6SMatthew G. Knepley     const PetscInt  *points;
44723e9753d6SMatthew G. Knepley     PetscInt         numFaces, face, Nq;
44733e9753d6SMatthew G. Knepley 
44740c290148SMatthew G. Knepley     ierr = DMLabelGetStratumIS(key.label, key.value, &pointIS);CHKERRQ(ierr);
44750c290148SMatthew G. Knepley     if (!pointIS) goto end; /* No points with that id on this process */
44763e9753d6SMatthew G. Knepley     {
44773e9753d6SMatthew G. Knepley       IS isectIS;
44783e9753d6SMatthew G. Knepley 
44793e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a priori if one is a superset of the other */
44803e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
44813e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
44823e9753d6SMatthew G. Knepley       pointIS = isectIS;
44833e9753d6SMatthew G. Knepley     }
44843e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS,&numFaces);CHKERRQ(ierr);
44853e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS,&points);CHKERRQ(ierr);
44863e9753d6SMatthew G. Knepley     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim, &elemVec, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
44873e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
44883e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
44893e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
44903e9753d6SMatthew G. Knepley     }
44913e9753d6SMatthew G. Knepley     if (!qGeom) {
44923e9753d6SMatthew G. Knepley       PetscFE fe;
44933e9753d6SMatthew G. Knepley 
44940c290148SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, key.field, (PetscObject *) &fe);CHKERRQ(ierr);
44953e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
44963e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
44973e9753d6SMatthew G. Knepley     }
44983e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
44993e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
45003e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
4501f15274beSMatthew Knepley       const PetscInt point = points[face], *support;
45023e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
4503f15274beSMatthew Knepley       PetscInt       i;
45043e9753d6SMatthew G. Knepley 
45053e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
45063e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
45073e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
45083e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
45093e9753d6SMatthew G. Knepley       if (locX_t) {
45103e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
45113e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
45123e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
45133e9753d6SMatthew G. Knepley       }
45143e9753d6SMatthew G. Knepley       if (locA) {
45153e9753d6SMatthew G. Knepley         PetscInt subp;
45163e9753d6SMatthew G. Knepley 
45173e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
45183e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
45193e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
45203e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
45213e9753d6SMatthew G. Knepley       }
45223e9753d6SMatthew G. Knepley     }
45233e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemVec, numFaces*totDim);CHKERRQ(ierr);
45243e9753d6SMatthew G. Knepley     {
45253e9753d6SMatthew G. Knepley       PetscFE         fe;
45263e9753d6SMatthew G. Knepley       PetscInt        Nb;
45273e9753d6SMatthew G. Knepley       PetscFEGeom     *chunkGeom = NULL;
45283e9753d6SMatthew G. Knepley       /* Conforming batches */
45293e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
45303e9753d6SMatthew G. Knepley       /* Remainder */
45313e9753d6SMatthew G. Knepley       PetscInt        Nr, offset;
45323e9753d6SMatthew G. Knepley 
45330c290148SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, key.field, (PetscObject *) &fe);CHKERRQ(ierr);
45343e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
45353e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
45363e9753d6SMatthew G. Knepley       /* TODO: documentation is unclear about what is going on with these numbers: how should Nb / Nq factor in ? */
45373e9753d6SMatthew G. Knepley       blockSize = Nb;
45383e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
45393e9753d6SMatthew G. Knepley       ierr =  PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
45403e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
45413e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
45423e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
45433e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
45443e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
454545480ffeSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, wf, key, Ne, chunkGeom, u, u_t, probAux, a, t, elemVec);CHKERRQ(ierr);
45463e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom, 0, offset, &chunkGeom);CHKERRQ(ierr);
45473e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
454845480ffeSMatthew G. Knepley       ierr = PetscFEIntegrateBdResidual(prob, wf, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, &elemVec[offset*totDim]);CHKERRQ(ierr);
45493e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
45503e9753d6SMatthew G. Knepley     }
45513e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
45523e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
45533e9753d6SMatthew G. Knepley 
45543e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(point, "BdResidual", totDim, &elemVec[face*totDim]);CHKERRQ(ierr);}
45553e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
45563e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(plex, NULL, locF, support[0], &elemVec[face*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
45573e9753d6SMatthew G. Knepley     }
45583e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
45593e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
45603e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
45613e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
45623e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemVec, a);CHKERRQ(ierr);
45633e9753d6SMatthew G. Knepley   }
45640c290148SMatthew G. Knepley   end:
45653e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
45663e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
45673e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
45683e9753d6SMatthew G. Knepley }
45693e9753d6SMatthew G. Knepley 
45700c290148SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidualSingle(DM dm, PetscReal t, PetscWeakForm wf, PetscFormKey key, Vec locX, Vec locX_t, Vec locF)
45713e9753d6SMatthew G. Knepley {
45723e9753d6SMatthew G. Knepley   DMField        coordField;
45733e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
45743e9753d6SMatthew G. Knepley   IS             facetIS;
45753e9753d6SMatthew G. Knepley   PetscInt       dim;
45763e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
45773e9753d6SMatthew G. Knepley 
45783e9753d6SMatthew G. Knepley   PetscFunctionBegin;
45793e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
45803e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
45813e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
45823e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
45830c290148SMatthew G. Knepley   ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, wf, key, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
45843e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
45853e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
45863e9753d6SMatthew G. Knepley }
45873e9753d6SMatthew G. Knepley 
45883e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdResidual_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
45893e9753d6SMatthew G. Knepley {
45903e9753d6SMatthew G. Knepley   PetscDS        prob;
45913e9753d6SMatthew G. Knepley   PetscInt       numBd, bd;
45923e9753d6SMatthew G. Knepley   DMField        coordField = NULL;
45933e9753d6SMatthew G. Knepley   IS             facetIS    = NULL;
45943e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
45953e9753d6SMatthew G. Knepley   PetscInt       dim;
45963e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
45973e9753d6SMatthew G. Knepley 
45983e9753d6SMatthew G. Knepley   PetscFunctionBegin;
45993e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
46003e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
46013e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
46023e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel,dim - 1,&facetIS);CHKERRQ(ierr);
46033e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
46043e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
460545480ffeSMatthew G. Knepley     PetscWeakForm           wf;
46063e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
46073e9753d6SMatthew G. Knepley     DMLabel                 label;
46083e9753d6SMatthew G. Knepley     const PetscInt         *values;
46090c290148SMatthew G. Knepley     PetscInt                field, numValues, v;
46103e9753d6SMatthew G. Knepley     PetscObject             obj;
46113e9753d6SMatthew G. Knepley     PetscClassId            id;
46120c290148SMatthew G. Knepley     PetscFormKey            key;
46133e9753d6SMatthew G. Knepley 
461445480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &wf, &type, NULL, &label, &numValues, &values, &field, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
46153e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, field, &obj);CHKERRQ(ierr);
46163e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
46173e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
46183e9753d6SMatthew G. Knepley     if (!facetIS) {
46193e9753d6SMatthew G. Knepley       DMLabel  depthLabel;
46203e9753d6SMatthew G. Knepley       PetscInt dim;
46213e9753d6SMatthew G. Knepley 
46223e9753d6SMatthew G. Knepley       ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
46233e9753d6SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
46243e9753d6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(depthLabel, dim - 1, &facetIS);CHKERRQ(ierr);
46253e9753d6SMatthew G. Knepley     }
46263e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
46270c290148SMatthew G. Knepley     for (v = 0; v < numValues; ++v) {
46280c290148SMatthew G. Knepley       key.label = label;
46290c290148SMatthew G. Knepley       key.value = values[v];
46300c290148SMatthew G. Knepley       key.field = field;
46310c290148SMatthew G. Knepley       key.part  = 0;
46320c290148SMatthew G. Knepley       ierr = DMPlexComputeBdResidual_Single_Internal(dm, t, wf, key, locX, locX_t, locF, coordField, facetIS);CHKERRQ(ierr);
46330c290148SMatthew G. Knepley     }
46343e9753d6SMatthew G. Knepley   }
46353e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
46363e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
46373e9753d6SMatthew G. Knepley }
46383e9753d6SMatthew G. Knepley 
463906ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
46403e9753d6SMatthew G. Knepley {
46413e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
46423e9753d6SMatthew G. Knepley   const char      *name       = "Residual";
46433e9753d6SMatthew G. Knepley   DM               dmAux      = NULL;
46443e9753d6SMatthew G. Knepley   DM               dmGrad     = NULL;
46453e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
46466528b96dSMatthew G. Knepley   PetscDS          ds         = NULL;
46476528b96dSMatthew G. Knepley   PetscDS          dsAux      = NULL;
46483e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
46493e9753d6SMatthew G. Knepley   PetscBool        useFEM     = PETSC_FALSE;
46503e9753d6SMatthew G. Knepley   PetscBool        useFVM     = PETSC_FALSE;
46513e9753d6SMatthew G. Knepley   PetscBool        isImplicit = (locX_t || time == PETSC_MIN_REAL) ? PETSC_TRUE : PETSC_FALSE;
46523e9753d6SMatthew G. Knepley   PetscFV          fvm        = NULL;
46533e9753d6SMatthew G. Knepley   PetscFVCellGeom *cgeomFVM   = NULL;
46543e9753d6SMatthew G. Knepley   PetscFVFaceGeom *fgeomFVM   = NULL;
46553e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
46563e9753d6SMatthew G. Knepley   Vec              locA, cellGeometryFVM = NULL, faceGeometryFVM = NULL, grad, locGrad = NULL;
46573e9753d6SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a, *uL, *uR;
46583e9753d6SMatthew G. Knepley   IS               chunkIS;
46593e9753d6SMatthew G. Knepley   const PetscInt  *cells;
46603e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
46613e9753d6SMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux, numChunks, cellChunkSize, faceChunkSize, chunk, fStart, fEnd;
46623e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
46633e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
46643e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
46653e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
46663e9753d6SMatthew G. Knepley 
46673e9753d6SMatthew G. Knepley   PetscFunctionBegin;
46683e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
46693e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
46703e9753d6SMatthew G. Knepley   /* TODO The FVM geometry is over-manipulated. Make the precalc functions return exactly what we need */
46713e9753d6SMatthew G. Knepley   /* FEM+FVM */
46723e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
46733e9753d6SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
46743e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
46753e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
46763e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
46776528b96dSMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &ds);CHKERRQ(ierr);
46786528b96dSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
46796528b96dSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
4680ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, key.part, &locA);CHKERRQ(ierr);
46813e9753d6SMatthew G. Knepley   if (locA) {
46823e9753d6SMatthew G. Knepley     PetscInt subcell;
46833e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
46843e9753d6SMatthew G. Knepley     ierr = DMGetEnclosurePoint(dmAux, dm, DM_ENC_UNKNOWN, cStart, &subcell);CHKERRQ(ierr);
46856528b96dSMatthew G. Knepley     ierr = DMGetCellDS(dmAux, subcell, &dsAux);CHKERRQ(ierr);
46866528b96dSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux, &totDimAux);CHKERRQ(ierr);
46873e9753d6SMatthew G. Knepley   }
46883e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
46893e9753d6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
46903e9753d6SMatthew G. Knepley     PetscObject  obj;
46913e9753d6SMatthew G. Knepley     PetscClassId id;
46923e9753d6SMatthew G. Knepley     PetscBool    fimp;
46933e9753d6SMatthew G. Knepley 
46946528b96dSMatthew G. Knepley     ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
46953e9753d6SMatthew G. Knepley     if (isImplicit != fimp) continue;
46966528b96dSMatthew G. Knepley     ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
46973e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
46983e9753d6SMatthew G. Knepley     if (id == PETSCFE_CLASSID) {useFEM = PETSC_TRUE;}
46993e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {useFVM = PETSC_TRUE; fvm = (PetscFV) obj;}
47003e9753d6SMatthew G. Knepley   }
47013e9753d6SMatthew G. Knepley   if (useFEM) {
47023e9753d6SMatthew G. Knepley     ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
47033e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
47043e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
47053e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&affineQuad);CHKERRQ(ierr);
47063e9753d6SMatthew G. Knepley       if (affineQuad) {
47073e9753d6SMatthew G. Knepley         ierr = DMSNESGetFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
47083e9753d6SMatthew G. Knepley       }
47093e9753d6SMatthew G. Knepley     } else {
47103e9753d6SMatthew G. Knepley       ierr = PetscCalloc2(Nf,&quads,Nf,&geoms);CHKERRQ(ierr);
47113e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
47123e9753d6SMatthew G. Knepley         PetscObject  obj;
47133e9753d6SMatthew G. Knepley         PetscClassId id;
47143e9753d6SMatthew G. Knepley         PetscBool    fimp;
47153e9753d6SMatthew G. Knepley 
47166528b96dSMatthew G. Knepley         ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
47173e9753d6SMatthew G. Knepley         if (isImplicit != fimp) continue;
47186528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
47193e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
47203e9753d6SMatthew G. Knepley         if (id == PETSCFE_CLASSID) {
47213e9753d6SMatthew G. Knepley           PetscFE fe = (PetscFE) obj;
47223e9753d6SMatthew G. Knepley 
47233e9753d6SMatthew G. Knepley           ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
47243e9753d6SMatthew G. Knepley           ierr = PetscObjectReference((PetscObject)quads[f]);CHKERRQ(ierr);
47253e9753d6SMatthew G. Knepley           ierr = DMSNESGetFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
47263e9753d6SMatthew G. Knepley         }
47273e9753d6SMatthew G. Knepley       }
47283e9753d6SMatthew G. Knepley     }
47293e9753d6SMatthew G. Knepley   }
47303e9753d6SMatthew G. Knepley   if (useFVM) {
47313e9753d6SMatthew G. Knepley     ierr = DMPlexGetGeometryFVM(dm, &faceGeometryFVM, &cellGeometryFVM, NULL);CHKERRQ(ierr);
47323e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(faceGeometryFVM, (const PetscScalar **) &fgeomFVM);CHKERRQ(ierr);
47333e9753d6SMatthew G. Knepley     ierr = VecGetArrayRead(cellGeometryFVM, (const PetscScalar **) &cgeomFVM);CHKERRQ(ierr);
47343e9753d6SMatthew G. Knepley     /* Reconstruct and limit cell gradients */
47353e9753d6SMatthew G. Knepley     ierr = DMPlexGetGradientDM(dm, fvm, &dmGrad);CHKERRQ(ierr);
47363e9753d6SMatthew G. Knepley     if (dmGrad) {
47373e9753d6SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
47383e9753d6SMatthew G. Knepley       ierr = DMGetGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
47393e9753d6SMatthew G. Knepley       ierr = DMPlexReconstructGradients_Internal(dm, fvm, fStart, fEnd, faceGeometryFVM, cellGeometryFVM, locX, grad);CHKERRQ(ierr);
47403e9753d6SMatthew G. Knepley       /* Communicate gradient values */
47413e9753d6SMatthew G. Knepley       ierr = DMGetLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);
47423e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalBegin(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
47433e9753d6SMatthew G. Knepley       ierr = DMGlobalToLocalEnd(dmGrad, grad, INSERT_VALUES, locGrad);CHKERRQ(ierr);
47443e9753d6SMatthew G. Knepley       ierr = DMRestoreGlobalVector(dmGrad, &grad);CHKERRQ(ierr);
47453e9753d6SMatthew G. Knepley     }
47463e9753d6SMatthew G. Knepley     /* Handle non-essential (e.g. outflow) boundary values */
47473e9753d6SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_FALSE, locX, time, faceGeometryFVM, cellGeometryFVM, locGrad);CHKERRQ(ierr);
47483e9753d6SMatthew G. Knepley   }
47493e9753d6SMatthew G. Knepley   /* Loop over chunks */
47503e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISCreate(PETSC_COMM_SELF, &chunkIS);CHKERRQ(ierr);}
47513e9753d6SMatthew G. Knepley   numCells      = cEnd - cStart;
47523e9753d6SMatthew G. Knepley   numChunks     = 1;
47533e9753d6SMatthew G. Knepley   cellChunkSize = numCells/numChunks;
47543e9753d6SMatthew G. Knepley   faceChunkSize = (fEnd - fStart)/numChunks;
47553e9753d6SMatthew G. Knepley   numChunks     = PetscMin(1,numCells);
47563e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
47573e9753d6SMatthew G. Knepley     PetscScalar     *elemVec, *fluxL, *fluxR;
47583e9753d6SMatthew G. Knepley     PetscReal       *vol;
47593e9753d6SMatthew G. Knepley     PetscFVFaceGeom *fgeom;
47603e9753d6SMatthew G. Knepley     PetscInt         cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
47613e9753d6SMatthew G. Knepley     PetscInt         fS = fStart+chunk*faceChunkSize, fE = PetscMin(fS+faceChunkSize, fEnd), numFaces = 0, face;
47623e9753d6SMatthew G. Knepley 
47633e9753d6SMatthew G. Knepley     /* Extract field coefficients */
47643e9753d6SMatthew G. Knepley     if (useFEM) {
47653e9753d6SMatthew G. Knepley       ierr = ISGetPointSubrange(chunkIS, cS, cE, cells);CHKERRQ(ierr);
47663e9753d6SMatthew G. Knepley       ierr = DMPlexGetCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
47673e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
47683e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(elemVec, numCells*totDim);CHKERRQ(ierr);
47693e9753d6SMatthew G. Knepley     }
47703e9753d6SMatthew G. Knepley     if (useFVM) {
47713e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
47723e9753d6SMatthew G. Knepley       ierr = DMPlexGetFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
47733e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
47743e9753d6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
47753e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxL, numFaces*totDim);CHKERRQ(ierr);
47763e9753d6SMatthew G. Knepley       ierr = PetscArrayzero(fluxR, numFaces*totDim);CHKERRQ(ierr);
47773e9753d6SMatthew G. Knepley     }
47783e9753d6SMatthew 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 */
47793e9753d6SMatthew G. Knepley     /* Loop over fields */
47803e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
47813e9753d6SMatthew G. Knepley       PetscObject  obj;
47823e9753d6SMatthew G. Knepley       PetscClassId id;
47833e9753d6SMatthew G. Knepley       PetscBool    fimp;
47843e9753d6SMatthew G. Knepley       PetscInt     numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset;
47853e9753d6SMatthew G. Knepley 
47866528b96dSMatthew G. Knepley       key.field = f;
47876528b96dSMatthew G. Knepley       ierr = PetscDSGetImplicit(ds, f, &fimp);CHKERRQ(ierr);
47883e9753d6SMatthew G. Knepley       if (isImplicit != fimp) continue;
47896528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
47903e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
47913e9753d6SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
47923e9753d6SMatthew G. Knepley         PetscFE         fe = (PetscFE) obj;
47933e9753d6SMatthew G. Knepley         PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
47943e9753d6SMatthew G. Knepley         PetscFEGeom    *chunkGeom = NULL;
47953e9753d6SMatthew G. Knepley         PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
47963e9753d6SMatthew G. Knepley         PetscInt        Nq, Nb;
47973e9753d6SMatthew G. Knepley 
47983e9753d6SMatthew G. Knepley         ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
47993e9753d6SMatthew G. Knepley         ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
48003e9753d6SMatthew G. Knepley         ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
48013e9753d6SMatthew G. Knepley         blockSize = Nb;
48023e9753d6SMatthew G. Knepley         batchSize = numBlocks * blockSize;
48033e9753d6SMatthew G. Knepley         ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
48043e9753d6SMatthew G. Knepley         numChunks = numCells / (numBatches*batchSize);
48053e9753d6SMatthew G. Knepley         Ne        = numChunks*numBatches*batchSize;
48063e9753d6SMatthew G. Knepley         Nr        = numCells % (numBatches*batchSize);
48073e9753d6SMatthew G. Knepley         offset    = numCells - Nr;
48083e9753d6SMatthew G. Knepley         /* Integrate FE residual to get elemVec (need fields at quadrature points) */
48093e9753d6SMatthew 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) */
48103e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
48116528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(ds, key, Ne, chunkGeom, u, u_t, dsAux, a, t, elemVec);CHKERRQ(ierr);
48123e9753d6SMatthew G. Knepley         ierr = PetscFEGeomGetChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
48136528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateResidual(ds, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux, &a[offset*totDimAux], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
48143e9753d6SMatthew G. Knepley         ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&chunkGeom);CHKERRQ(ierr);
48153e9753d6SMatthew G. Knepley       } else if (id == PETSCFV_CLASSID) {
48163e9753d6SMatthew G. Knepley         PetscFV fv = (PetscFV) obj;
48173e9753d6SMatthew G. Knepley 
48183e9753d6SMatthew G. Knepley         Ne = numFaces;
48193e9753d6SMatthew G. Knepley         /* Riemann solve over faces (need fields at face centroids) */
48203e9753d6SMatthew G. Knepley         /*   We need to evaluate FE fields at those coordinates */
48216528b96dSMatthew G. Knepley         ierr = PetscFVIntegrateRHSFunction(fv, ds, f, Ne, fgeom, vol, uL, uR, fluxL, fluxR);CHKERRQ(ierr);
482298921bdaSJacob Faibussowitsch       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
48233e9753d6SMatthew G. Knepley     }
48243e9753d6SMatthew G. Knepley     /* Loop over domain */
48253e9753d6SMatthew G. Knepley     if (useFEM) {
48263e9753d6SMatthew G. Knepley       /* Add elemVec to locX */
48273e9753d6SMatthew G. Knepley       for (c = cS; c < cE; ++c) {
48283e9753d6SMatthew G. Knepley         const PetscInt cell = cells ? cells[c] : c;
48293e9753d6SMatthew G. Knepley         const PetscInt cind = c - cStart;
48303e9753d6SMatthew G. Knepley 
48313e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
48323e9753d6SMatthew G. Knepley         if (ghostLabel) {
48333e9753d6SMatthew G. Knepley           PetscInt ghostVal;
48343e9753d6SMatthew G. Knepley 
48353e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
48363e9753d6SMatthew G. Knepley           if (ghostVal > 0) continue;
48373e9753d6SMatthew G. Knepley         }
48383e9753d6SMatthew G. Knepley         ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
48393e9753d6SMatthew G. Knepley       }
48403e9753d6SMatthew G. Knepley     }
48413e9753d6SMatthew G. Knepley     if (useFVM) {
48423e9753d6SMatthew G. Knepley       PetscScalar *fa;
48433e9753d6SMatthew G. Knepley       PetscInt     iface;
48443e9753d6SMatthew G. Knepley 
48453e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
48463e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
48473e9753d6SMatthew G. Knepley         PetscFV      fv;
48483e9753d6SMatthew G. Knepley         PetscObject  obj;
48493e9753d6SMatthew G. Knepley         PetscClassId id;
48503e9753d6SMatthew G. Knepley         PetscInt     foff, pdim;
48513e9753d6SMatthew G. Knepley 
48526528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
48536528b96dSMatthew G. Knepley         ierr = PetscDSGetFieldOffset(ds, f, &foff);CHKERRQ(ierr);
48543e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
48553e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
48563e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
48573e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
48583e9753d6SMatthew G. Knepley         /* Accumulate fluxes to cells */
48593e9753d6SMatthew G. Knepley         for (face = fS, iface = 0; face < fE; ++face) {
48603e9753d6SMatthew G. Knepley           const PetscInt *scells;
48613e9753d6SMatthew G. Knepley           PetscScalar    *fL = NULL, *fR = NULL;
48623e9753d6SMatthew G. Knepley           PetscInt        ghost, d, nsupp, nchild;
48633e9753d6SMatthew G. Knepley 
48643e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel, face, &ghost);CHKERRQ(ierr);
48653e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupportSize(dm, face, &nsupp);CHKERRQ(ierr);
48663e9753d6SMatthew G. Knepley           ierr = DMPlexGetTreeChildren(dm, face, &nchild, NULL);CHKERRQ(ierr);
48673e9753d6SMatthew G. Knepley           if (ghost >= 0 || nsupp > 2 || nchild > 0) continue;
48683e9753d6SMatthew G. Knepley           ierr = DMPlexGetSupport(dm, face, &scells);CHKERRQ(ierr);
48693e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[0],&ghost);CHKERRQ(ierr);
48703e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[0], f, fa, &fL);CHKERRQ(ierr);}
48713e9753d6SMatthew G. Knepley           ierr = DMLabelGetValue(ghostLabel,scells[1],&ghost);CHKERRQ(ierr);
48723e9753d6SMatthew G. Knepley           if (ghost <= 0) {ierr = DMPlexPointLocalFieldRef(dm, scells[1], f, fa, &fR);CHKERRQ(ierr);}
48733e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) {
48743e9753d6SMatthew G. Knepley             if (fL) fL[d] -= fluxL[iface*totDim+foff+d];
48753e9753d6SMatthew G. Knepley             if (fR) fR[d] += fluxR[iface*totDim+foff+d];
48763e9753d6SMatthew G. Knepley           }
48773e9753d6SMatthew G. Knepley           ++iface;
48783e9753d6SMatthew G. Knepley         }
48793e9753d6SMatthew G. Knepley       }
48803e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
48813e9753d6SMatthew G. Knepley     }
48823e9753d6SMatthew G. Knepley     /* Handle time derivative */
48833e9753d6SMatthew G. Knepley     if (locX_t) {
48843e9753d6SMatthew G. Knepley       PetscScalar *x_t, *fa;
48853e9753d6SMatthew G. Knepley 
48863e9753d6SMatthew G. Knepley       ierr = VecGetArray(locF, &fa);CHKERRQ(ierr);
48873e9753d6SMatthew G. Knepley       ierr = VecGetArray(locX_t, &x_t);CHKERRQ(ierr);
48883e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
48893e9753d6SMatthew G. Knepley         PetscFV      fv;
48903e9753d6SMatthew G. Knepley         PetscObject  obj;
48913e9753d6SMatthew G. Knepley         PetscClassId id;
48923e9753d6SMatthew G. Knepley         PetscInt     pdim, d;
48933e9753d6SMatthew G. Knepley 
48946528b96dSMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, f, &obj);CHKERRQ(ierr);
48953e9753d6SMatthew G. Knepley         ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
48963e9753d6SMatthew G. Knepley         if (id != PETSCFV_CLASSID) continue;
48973e9753d6SMatthew G. Knepley         fv   = (PetscFV) obj;
48983e9753d6SMatthew G. Knepley         ierr = PetscFVGetNumComponents(fv, &pdim);CHKERRQ(ierr);
48993e9753d6SMatthew G. Knepley         for (c = cS; c < cE; ++c) {
49003e9753d6SMatthew G. Knepley           const PetscInt cell = cells ? cells[c] : c;
49013e9753d6SMatthew G. Knepley           PetscScalar   *u_t, *r;
49023e9753d6SMatthew G. Knepley 
49033e9753d6SMatthew G. Knepley           if (ghostLabel) {
49043e9753d6SMatthew G. Knepley             PetscInt ghostVal;
49053e9753d6SMatthew G. Knepley 
49063e9753d6SMatthew G. Knepley             ierr = DMLabelGetValue(ghostLabel, cell, &ghostVal);CHKERRQ(ierr);
49073e9753d6SMatthew G. Knepley             if (ghostVal > 0) continue;
49083e9753d6SMatthew G. Knepley           }
49093e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRead(dm, cell, f, x_t, &u_t);CHKERRQ(ierr);
49103e9753d6SMatthew G. Knepley           ierr = DMPlexPointLocalFieldRef(dm, cell, f, fa, &r);CHKERRQ(ierr);
49113e9753d6SMatthew G. Knepley           for (d = 0; d < pdim; ++d) r[d] += u_t[d];
49123e9753d6SMatthew G. Knepley         }
49133e9753d6SMatthew G. Knepley       }
49143e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locX_t, &x_t);CHKERRQ(ierr);
49153e9753d6SMatthew G. Knepley       ierr = VecRestoreArray(locF, &fa);CHKERRQ(ierr);
49163e9753d6SMatthew G. Knepley     }
49173e9753d6SMatthew G. Knepley     if (useFEM) {
49183e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreCellFields(dm, chunkIS, locX, locX_t, locA, &u, &u_t, &a);CHKERRQ(ierr);
49193e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
49203e9753d6SMatthew G. Knepley     }
49213e9753d6SMatthew G. Knepley     if (useFVM) {
49223e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceFields(dm, fS, fE, locX, locX_t, faceGeometryFVM, cellGeometryFVM, locGrad, &numFaces, &uL, &uR);CHKERRQ(ierr);
49233e9753d6SMatthew G. Knepley       ierr = DMPlexRestoreFaceGeometry(dm, fS, fE, faceGeometryFVM, cellGeometryFVM, &numFaces, &fgeom, &vol);CHKERRQ(ierr);
49243e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxL);CHKERRQ(ierr);
49253e9753d6SMatthew G. Knepley       ierr = DMRestoreWorkArray(dm, numFaces*totDim, MPIU_SCALAR, &fluxR);CHKERRQ(ierr);
49263e9753d6SMatthew G. Knepley       if (dmGrad) {ierr = DMRestoreLocalVector(dmGrad, &locGrad);CHKERRQ(ierr);}
49273e9753d6SMatthew G. Knepley     }
49283e9753d6SMatthew G. Knepley   }
49293e9753d6SMatthew G. Knepley   if (useFEM) {ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);}
49303e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
49313e9753d6SMatthew G. Knepley 
49323e9753d6SMatthew G. Knepley   if (useFEM) {
49333e9753d6SMatthew G. Knepley     ierr = DMPlexComputeBdResidual_Internal(dm, locX, locX_t, t, locF, user);CHKERRQ(ierr);
49343e9753d6SMatthew G. Knepley 
49353e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
49363e9753d6SMatthew G. Knepley       ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
49373e9753d6SMatthew G. Knepley       ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
49383e9753d6SMatthew G. Knepley     } else {
49393e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
49403e9753d6SMatthew G. Knepley         ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);
49413e9753d6SMatthew G. Knepley         ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);
49423e9753d6SMatthew G. Knepley       }
49433e9753d6SMatthew G. Knepley       ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
49443e9753d6SMatthew G. Knepley     }
49453e9753d6SMatthew G. Knepley   }
49463e9753d6SMatthew G. Knepley 
49473e9753d6SMatthew G. Knepley   /* FEM */
49483e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
49493e9753d6SMatthew G. Knepley   /* 2: Get geometric data */
49503e9753d6SMatthew G. Knepley   /* 3: Handle boundary values */
49513e9753d6SMatthew G. Knepley   /* 4: Loop over domain */
49523e9753d6SMatthew G. Knepley   /*   Extract coefficients */
49533e9753d6SMatthew G. Knepley   /* Loop over fields */
49543e9753d6SMatthew G. Knepley   /*   Set tiling for FE*/
49553e9753d6SMatthew G. Knepley   /*   Integrate FE residual to get elemVec */
49563e9753d6SMatthew G. Knepley   /*     Loop over subdomain */
49573e9753d6SMatthew G. Knepley   /*       Loop over quad points */
49583e9753d6SMatthew G. Knepley   /*         Transform coords to real space */
49593e9753d6SMatthew G. Knepley   /*         Evaluate field and aux fields at point */
49603e9753d6SMatthew G. Knepley   /*         Evaluate residual at point */
49613e9753d6SMatthew G. Knepley   /*         Transform residual to real space */
49623e9753d6SMatthew G. Knepley   /*       Add residual to elemVec */
49633e9753d6SMatthew G. Knepley   /* Loop over domain */
49643e9753d6SMatthew G. Knepley   /*   Add elemVec to locX */
49653e9753d6SMatthew G. Knepley 
49663e9753d6SMatthew G. Knepley   /* FVM */
49673e9753d6SMatthew G. Knepley   /* Get geometric data */
49683e9753d6SMatthew G. Knepley   /* If using gradients */
49693e9753d6SMatthew G. Knepley   /*   Compute gradient data */
49703e9753d6SMatthew G. Knepley   /*   Loop over domain faces */
49713e9753d6SMatthew G. Knepley   /*     Count computational faces */
49723e9753d6SMatthew G. Knepley   /*     Reconstruct cell gradient */
49733e9753d6SMatthew G. Knepley   /*   Loop over domain cells */
49743e9753d6SMatthew G. Knepley   /*     Limit cell gradients */
49753e9753d6SMatthew G. Knepley   /* Handle boundary values */
49763e9753d6SMatthew G. Knepley   /* Loop over domain faces */
49773e9753d6SMatthew G. Knepley   /*   Read out field, centroid, normal, volume for each side of face */
49783e9753d6SMatthew G. Knepley   /* Riemann solve over faces */
49793e9753d6SMatthew G. Knepley   /* Loop over domain faces */
49803e9753d6SMatthew G. Knepley   /*   Accumulate fluxes to cells */
49813e9753d6SMatthew G. Knepley   /* TODO Change printFEM to printDisc here */
49823e9753d6SMatthew G. Knepley   if (mesh->printFEM) {
49833e9753d6SMatthew G. Knepley     Vec         locFbc;
49843e9753d6SMatthew G. Knepley     PetscInt    pStart, pEnd, p, maxDof;
49853e9753d6SMatthew G. Knepley     PetscScalar *zeroes;
49863e9753d6SMatthew G. Knepley 
49873e9753d6SMatthew G. Knepley     ierr = VecDuplicate(locF,&locFbc);CHKERRQ(ierr);
49883e9753d6SMatthew G. Knepley     ierr = VecCopy(locF,locFbc);CHKERRQ(ierr);
49893e9753d6SMatthew G. Knepley     ierr = PetscSectionGetChart(section,&pStart,&pEnd);CHKERRQ(ierr);
49903e9753d6SMatthew G. Knepley     ierr = PetscSectionGetMaxDof(section,&maxDof);CHKERRQ(ierr);
49913e9753d6SMatthew G. Knepley     ierr = PetscCalloc1(maxDof,&zeroes);CHKERRQ(ierr);
49923e9753d6SMatthew G. Knepley     for (p = pStart; p < pEnd; p++) {
49933e9753d6SMatthew G. Knepley       ierr = VecSetValuesSection(locFbc,section,p,zeroes,INSERT_BC_VALUES);CHKERRQ(ierr);
49943e9753d6SMatthew G. Knepley     }
49953e9753d6SMatthew G. Knepley     ierr = PetscFree(zeroes);CHKERRQ(ierr);
49963e9753d6SMatthew G. Knepley     ierr = DMPrintLocalVec(dm, name, mesh->printTol, locFbc);CHKERRQ(ierr);
49973e9753d6SMatthew G. Knepley     ierr = VecDestroy(&locFbc);CHKERRQ(ierr);
49983e9753d6SMatthew G. Knepley   }
49993e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
50003e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
50013e9753d6SMatthew G. Knepley }
50023e9753d6SMatthew G. Knepley 
50036528b96dSMatthew G. Knepley /*
50046528b96dSMatthew G. Knepley   1) Allow multiple kernels for BdResidual for hybrid DS
50056528b96dSMatthew G. Knepley 
50066528b96dSMatthew G. Knepley   DONE 2) Get out dsAux for either side at the same time as cohesive cell dsAux
50076528b96dSMatthew G. Knepley 
50086528b96dSMatthew G. Knepley   DONE 3) Change DMGetCellFields() to get different aux data a[] for each side
50096528b96dSMatthew G. Knepley      - I think I just need to replace a[] with the closure from each face
50106528b96dSMatthew G. Knepley 
50116528b96dSMatthew G. Knepley   4) Run both kernels for each non-hybrid field with correct dsAux, and then hybrid field as before
50126528b96dSMatthew G. Knepley */
501306ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeResidual_Hybrid_Internal(DM dm, PetscFormKey key[], IS cellIS, PetscReal time, Vec locX, Vec locX_t, PetscReal t, Vec locF, void *user)
50143e9753d6SMatthew G. Knepley {
50153e9753d6SMatthew G. Knepley   DM_Plex         *mesh       = (DM_Plex *) dm->data;
50163e9753d6SMatthew G. Knepley   const char      *name       = "Hybrid Residual";
501704c51a94SMatthew G. Knepley   DM               dmAux[3]   = {NULL, NULL, NULL};
50183e9753d6SMatthew G. Knepley   DMLabel          ghostLabel = NULL;
50196528b96dSMatthew G. Knepley   PetscDS          ds         = NULL;
50206528b96dSMatthew G. Knepley   PetscDS          dsAux[3]   = {NULL, NULL, NULL};
502104c51a94SMatthew G. Knepley   Vec              locA[3]    = {NULL, NULL, NULL};
50223e9753d6SMatthew G. Knepley   PetscSection     section    = NULL;
50233e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
50246528b96dSMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a[3];
50253e9753d6SMatthew G. Knepley   PetscScalar     *elemVec;
50263e9753d6SMatthew G. Knepley   IS               chunkIS;
50273e9753d6SMatthew G. Knepley   const PetscInt  *cells;
50283e9753d6SMatthew G. Knepley   PetscInt        *faces;
50293e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
50306528b96dSMatthew G. Knepley   PetscInt         Nf, f, totDim, totDimAux[3], numChunks, cellChunkSize, chunk;
50313e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
50323e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
50333e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
50343e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
50353e9753d6SMatthew G. Knepley 
50363e9753d6SMatthew G. Knepley   PetscFunctionBegin;
50375fedec97SMatthew G. Knepley   if ((key[0].label == key[1].label) && (key[0].value == key[1].value) && (key[0].part == key[1].part)) {
50385fedec97SMatthew G. Knepley     const char *name;
50395fedec97SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) key[0].label, &name);CHKERRQ(ierr);
504098921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Form keys for each side of a cohesive surface must be different (%s, %D, %D)", name, key[0].value, key[0].part);
50415fedec97SMatthew G. Knepley   }
50423e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
50433e9753d6SMatthew G. Knepley   /* TODO The places where we have to use isFE are probably the member functions for the PetscDisc class */
50443e9753d6SMatthew G. Knepley   /* FEM */
5045148442b3SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
50463e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
50473e9753d6SMatthew G. Knepley   /* 1: Get sizes from dm and dmAux */
50483e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
50493e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
50506528b96dSMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &ds);CHKERRQ(ierr);
50516528b96dSMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
50526528b96dSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
5053ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key[2].label, key[2].value, key[2].part, &locA[2]);CHKERRQ(ierr);
505404c51a94SMatthew G. Knepley   if (locA[2]) {
505504c51a94SMatthew G. Knepley     ierr = VecGetDM(locA[2], &dmAux[2]);CHKERRQ(ierr);
505604c51a94SMatthew G. Knepley     ierr = DMGetCellDS(dmAux[2], cStart, &dsAux[2]);CHKERRQ(ierr);
50576528b96dSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[2], &totDimAux[2]);CHKERRQ(ierr);
50586528b96dSMatthew G. Knepley     {
50596528b96dSMatthew G. Knepley       const PetscInt *cone;
50606528b96dSMatthew G. Knepley       PetscInt        c;
50616528b96dSMatthew G. Knepley 
50626528b96dSMatthew G. Knepley       ierr = DMPlexGetCone(dm, cStart, &cone);CHKERRQ(ierr);
50636528b96dSMatthew G. Knepley       for (c = 0; c < 2; ++c) {
50646528b96dSMatthew G. Knepley         const PetscInt *support;
50656528b96dSMatthew G. Knepley         PetscInt ssize, s;
50666528b96dSMatthew G. Knepley 
50676528b96dSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
50686528b96dSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, cone[c], &ssize);CHKERRQ(ierr);
50692c71b3e2SJacob Faibussowitsch         PetscCheckFalse(ssize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[c], cStart, ssize);
50706528b96dSMatthew G. Knepley         if      (support[0] == cStart) s = 1;
50716528b96dSMatthew G. Knepley         else if (support[1] == cStart) s = 0;
507298921bdaSJacob Faibussowitsch         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[c], cStart);
5073ac17215fSMatthew G. Knepley         ierr = DMGetAuxiliaryVec(dm, key[c].label, key[c].value, key[c].part, &locA[c]);CHKERRQ(ierr);
507404c51a94SMatthew G. Knepley         if (locA[c]) {ierr = VecGetDM(locA[c], &dmAux[c]);CHKERRQ(ierr);}
507504c51a94SMatthew G. Knepley         else         {dmAux[c] = dmAux[2];}
507604c51a94SMatthew G. Knepley         ierr = DMGetCellDS(dmAux[c], support[s], &dsAux[c]);CHKERRQ(ierr);
50776528b96dSMatthew G. Knepley         ierr = PetscDSGetTotalDimension(dsAux[c], &totDimAux[c]);CHKERRQ(ierr);
50786528b96dSMatthew G. Knepley       }
50796528b96dSMatthew G. Knepley     }
50803e9753d6SMatthew G. Knepley   }
50813e9753d6SMatthew G. Knepley   /* 2: Setup geometric data */
50823e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
50833e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
50843e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
50853e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf, &quads, Nf, &geoms);CHKERRQ(ierr);
50863e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
50873e9753d6SMatthew G. Knepley       PetscFE fe;
50883e9753d6SMatthew G. Knepley 
50896528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
50903e9753d6SMatthew G. Knepley       if (fe) {
50913e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
50923e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
50933e9753d6SMatthew G. Knepley       }
50943e9753d6SMatthew G. Knepley     }
50953e9753d6SMatthew G. Knepley   }
50963e9753d6SMatthew G. Knepley   /* Loop over chunks */
50973e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
50983e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
50993e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
51003e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
51013e9753d6SMatthew G. Knepley   /* Extract field coefficients */
51023e9753d6SMatthew G. Knepley   /* NOTE This needs the end cap faces to have identical orientations */
510304c51a94SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
5104148442b3SMatthew G. Knepley   ierr = DMPlexGetHybridAuxFields(dm, dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
51053e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, cellChunkSize*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
51063e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
51073e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
51083e9753d6SMatthew G. Knepley 
51093e9753d6SMatthew G. Knepley     ierr = PetscMemzero(elemVec, cellChunkSize*totDim * sizeof(PetscScalar));CHKERRQ(ierr);
51103e9753d6SMatthew G. Knepley     /* Get faces */
51113e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
51123e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
51133e9753d6SMatthew G. Knepley       const PetscInt *cone;
51143e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
51153e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
51163e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
51173e9753d6SMatthew G. Knepley     }
51183e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
51193e9753d6SMatthew G. Knepley     /* Get geometric data */
51203e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
51213e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
51223e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
51233e9753d6SMatthew G. Knepley     } else {
51243e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
51253e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
51263e9753d6SMatthew G. Knepley       }
51273e9753d6SMatthew G. Knepley     }
51283e9753d6SMatthew G. Knepley     /* Loop over fields */
51293e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
51303e9753d6SMatthew G. Knepley       PetscFE         fe;
51313e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[f];
5132148442b3SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
51333e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[f];
51343e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
51355fedec97SMatthew G. Knepley       PetscBool       isCohesiveField;
51363e9753d6SMatthew G. Knepley 
51376528b96dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
51383e9753d6SMatthew G. Knepley       if (!fe) continue;
51393e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
51403e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
51413e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
51423e9753d6SMatthew G. Knepley       blockSize = Nb;
51433e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
51443e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
51453e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
51463e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
51473e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
51483e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
5149148442b3SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
5150148442b3SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
51515fedec97SMatthew G. Knepley       ierr = PetscDSGetCohesive(ds, f, &isCohesiveField);CHKERRQ(ierr);
51525fedec97SMatthew G. Knepley       chunkGeom->isCohesive = remGeom->isCohesive = PETSC_TRUE;
51536528b96dSMatthew G. Knepley       key[0].field = f;
51546528b96dSMatthew G. Knepley       key[1].field = f;
51555fedec97SMatthew G. Knepley       key[2].field = f;
5156c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, elemVec);CHKERRQ(ierr);
5157c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[0], 0, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
51585fedec97SMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, elemVec);CHKERRQ(ierr);
5159c2b7495fSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[1], 1, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
51609ee2af8cSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[2], 2, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, elemVec);CHKERRQ(ierr);
51619ee2af8cSMatthew G. Knepley       ierr = PetscFEIntegrateHybridResidual(ds, key[2], 2, Nr, remGeom,  &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, &elemVec[offset*totDim]);CHKERRQ(ierr);
5162148442b3SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
5163148442b3SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
51643e9753d6SMatthew G. Knepley     }
51653e9753d6SMatthew G. Knepley     /* Add elemVec to locX */
51663e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
51673e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
51683e9753d6SMatthew G. Knepley       const PetscInt cind = c - cStart;
51693e9753d6SMatthew G. Knepley 
51703e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellVector(cell, name, totDim, &elemVec[cind*totDim]);CHKERRQ(ierr);}
51713e9753d6SMatthew G. Knepley       if (ghostLabel) {
51723e9753d6SMatthew G. Knepley         PetscInt ghostVal;
51733e9753d6SMatthew G. Knepley 
51743e9753d6SMatthew G. Knepley         ierr = DMLabelGetValue(ghostLabel,cell,&ghostVal);CHKERRQ(ierr);
51753e9753d6SMatthew G. Knepley         if (ghostVal > 0) continue;
51763e9753d6SMatthew G. Knepley       }
51773e9753d6SMatthew G. Knepley       ierr = DMPlexVecSetClosure(dm, section, locF, cell, &elemVec[cind*totDim], ADD_ALL_VALUES);CHKERRQ(ierr);
51783e9753d6SMatthew G. Knepley     }
51793e9753d6SMatthew G. Knepley   }
518004c51a94SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
518104c51a94SMatthew G. Knepley   ierr = DMPlexRestoreHybridAuxFields(dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
51823e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, numCells*totDim, MPIU_SCALAR, &elemVec);CHKERRQ(ierr);
51833e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
51843e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
51853e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
51863e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
51873e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
51883e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
51893e9753d6SMatthew G. Knepley   } else {
51903e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
51913e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE,&geoms[f]);CHKERRQ(ierr);}
51923e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
51933e9753d6SMatthew G. Knepley     }
51943e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
51953e9753d6SMatthew G. Knepley   }
51963e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_ResidualFEM,dm,0,0,0);CHKERRQ(ierr);
51973e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
51983e9753d6SMatthew G. Knepley }
51993e9753d6SMatthew G. Knepley 
520045480ffeSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Single_Internal(DM dm, PetscReal t, PetscWeakForm wf, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt fieldI, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP, DMField coordField, IS facetIS)
52013e9753d6SMatthew G. Knepley {
52023e9753d6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
52033e9753d6SMatthew G. Knepley   DM              plex = NULL, plexA = NULL, tdm;
52043e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
52053e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
52063e9753d6SMatthew G. Knepley   PetscSection    section, sectionAux = NULL;
5207e432b41dSStefano Zampini   PetscSection    globalSection;
52083e9753d6SMatthew G. Knepley   Vec             locA = NULL, tv;
52093e9753d6SMatthew G. Knepley   PetscScalar    *u = NULL, *u_t = NULL, *a = NULL, *elemMat = NULL;
52103e9753d6SMatthew G. Knepley   PetscInt        v;
52113e9753d6SMatthew G. Knepley   PetscInt        Nf, totDim, totDimAux = 0;
5212e432b41dSStefano Zampini   PetscBool       transform;
52133e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
52143e9753d6SMatthew G. Knepley 
52153e9753d6SMatthew G. Knepley   PetscFunctionBegin;
52163e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52173e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
52183e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
52193e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
52203e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
52213e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
52223e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
52233e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
5224ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, label, values[0], 0, &locA);CHKERRQ(ierr);
52253e9753d6SMatthew G. Knepley   if (locA) {
52263e9753d6SMatthew G. Knepley     DM dmAux;
52273e9753d6SMatthew G. Knepley 
52283e9753d6SMatthew G. Knepley     ierr = VecGetDM(locA, &dmAux);CHKERRQ(ierr);
52293e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
52303e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexA);CHKERRQ(ierr);
52313e9753d6SMatthew G. Knepley     ierr = DMGetDS(plexA, &probAux);CHKERRQ(ierr);
52323e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
52333e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plexA, &sectionAux);CHKERRQ(ierr);
52343e9753d6SMatthew G. Knepley   }
52353e9753d6SMatthew G. Knepley 
52363e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
52373e9753d6SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
52383e9753d6SMatthew G. Knepley     PetscFEGeom     *fgeom;
52393e9753d6SMatthew G. Knepley     PetscInt         maxDegree;
52403e9753d6SMatthew G. Knepley     PetscQuadrature  qGeom = NULL;
52413e9753d6SMatthew G. Knepley     IS               pointIS;
52423e9753d6SMatthew G. Knepley     const PetscInt  *points;
524306ad1575SMatthew G. Knepley     PetscFormKey key;
52443e9753d6SMatthew G. Knepley     PetscInt         numFaces, face, Nq;
52453e9753d6SMatthew G. Knepley 
524645480ffeSMatthew G. Knepley     key.label = label;
524745480ffeSMatthew G. Knepley     key.value = values[v];
524806ad1575SMatthew G. Knepley     key.part  = 0;
52493e9753d6SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
52503e9753d6SMatthew G. Knepley     if (!pointIS) continue; /* No points with that id on this process */
52513e9753d6SMatthew G. Knepley     {
52523e9753d6SMatthew G. Knepley       IS isectIS;
52533e9753d6SMatthew G. Knepley 
52543e9753d6SMatthew G. Knepley       /* TODO: Special cases of ISIntersect where it is quick to check a prior if one is a superset of the other */
52553e9753d6SMatthew G. Knepley       ierr = ISIntersect_Caching_Internal(facetIS,pointIS,&isectIS);CHKERRQ(ierr);
52563e9753d6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
52573e9753d6SMatthew G. Knepley       pointIS = isectIS;
52583e9753d6SMatthew G. Knepley     }
52593e9753d6SMatthew G. Knepley     ierr = ISGetLocalSize(pointIS, &numFaces);CHKERRQ(ierr);
52603e9753d6SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
52613e9753d6SMatthew G. Knepley     ierr = PetscMalloc4(numFaces*totDim, &u, locX_t ? numFaces*totDim : 0, &u_t, numFaces*totDim*totDim, &elemMat, locA ? numFaces*totDimAux : 0, &a);CHKERRQ(ierr);
52623e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,pointIS,NULL,&maxDegree);CHKERRQ(ierr);
52633e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
52643e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,pointIS,&qGeom);CHKERRQ(ierr);
52653e9753d6SMatthew G. Knepley     }
52663e9753d6SMatthew G. Knepley     if (!qGeom) {
52673e9753d6SMatthew G. Knepley       PetscFE fe;
52683e9753d6SMatthew G. Knepley 
52693e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
52703e9753d6SMatthew G. Knepley       ierr = PetscFEGetFaceQuadrature(fe, &qGeom);CHKERRQ(ierr);
52713e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
52723e9753d6SMatthew G. Knepley     }
52733e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
52743e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
52753e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
5276f15274beSMatthew Knepley       const PetscInt point = points[face], *support;
52773e9753d6SMatthew G. Knepley       PetscScalar   *x     = NULL;
5278f15274beSMatthew Knepley       PetscInt       i;
52793e9753d6SMatthew G. Knepley 
52803e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
52813e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
52823e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u[face*totDim+i] = x[i];
52833e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, locX, support[0], NULL, &x);CHKERRQ(ierr);
52843e9753d6SMatthew G. Knepley       if (locX_t) {
52853e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
52863e9753d6SMatthew G. Knepley         for (i = 0; i < totDim; ++i) u_t[face*totDim+i] = x[i];
52873e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plex, section, locX_t, support[0], NULL, &x);CHKERRQ(ierr);
52883e9753d6SMatthew G. Knepley       }
52893e9753d6SMatthew G. Knepley       if (locA) {
52903e9753d6SMatthew G. Knepley         PetscInt subp;
52913e9753d6SMatthew G. Knepley         ierr = DMGetEnclosurePoint(plexA, dm, encAux, support[0], &subp);CHKERRQ(ierr);
52923e9753d6SMatthew G. Knepley         ierr = DMPlexVecGetClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
52933e9753d6SMatthew G. Knepley         for (i = 0; i < totDimAux; ++i) a[face*totDimAux+i] = x[i];
52943e9753d6SMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(plexA, sectionAux, locA, subp, NULL, &x);CHKERRQ(ierr);
52953e9753d6SMatthew G. Knepley       }
52963e9753d6SMatthew G. Knepley     }
52973e9753d6SMatthew G. Knepley     ierr = PetscArrayzero(elemMat, numFaces*totDim*totDim);CHKERRQ(ierr);
52983e9753d6SMatthew G. Knepley     {
52993e9753d6SMatthew G. Knepley       PetscFE         fe;
53003e9753d6SMatthew G. Knepley       PetscInt        Nb;
53013e9753d6SMatthew G. Knepley       /* Conforming batches */
53023e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
53033e9753d6SMatthew G. Knepley       /* Remainder */
53043e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL;
53053e9753d6SMatthew G. Knepley       PetscInt        fieldJ, Nr, offset;
53063e9753d6SMatthew G. Knepley 
53073e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
53083e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
53093e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
53103e9753d6SMatthew G. Knepley       blockSize = Nb;
53113e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
53123e9753d6SMatthew G. Knepley       ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
53133e9753d6SMatthew G. Knepley       numChunks = numFaces / (numBatches*batchSize);
53143e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
53153e9753d6SMatthew G. Knepley       Nr        = numFaces % (numBatches*batchSize);
53163e9753d6SMatthew G. Knepley       offset    = numFaces - Nr;
53173e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,0,offset,&chunkGeom);CHKERRQ(ierr);
53183e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
531945480ffeSMatthew G. Knepley         key.field = fieldI*Nf+fieldJ;
532045480ffeSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, wf, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
53213e9753d6SMatthew G. Knepley       }
53223e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
53233e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
532445480ffeSMatthew G. Knepley         key.field = fieldI*Nf+fieldJ;
532545480ffeSMatthew G. Knepley         ierr = PetscFEIntegrateBdJacobian(prob, wf, key, Nr, chunkGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, a ? &a[offset*totDimAux] : NULL, t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
53263e9753d6SMatthew G. Knepley       }
53273e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(fgeom,offset,numFaces,&chunkGeom);CHKERRQ(ierr);
53283e9753d6SMatthew G. Knepley     }
53293e9753d6SMatthew G. Knepley     for (face = 0; face < numFaces; ++face) {
53303e9753d6SMatthew G. Knepley       const PetscInt point = points[face], *support;
53313e9753d6SMatthew G. Knepley 
53323e9753d6SMatthew G. Knepley       /* Transform to global basis before insertion in Jacobian */
53333e9753d6SMatthew G. Knepley       ierr = DMPlexGetSupport(plex, point, &support);CHKERRQ(ierr);
53343e9753d6SMatthew G. Knepley       if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, support[0], PETSC_TRUE, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
53353e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(point, "BdJacobian", totDim, totDim, &elemMat[face*totDim*totDim]);CHKERRQ(ierr);}
53363e9753d6SMatthew G. Knepley       ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, support[0], &elemMat[face*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
53373e9753d6SMatthew G. Knepley     }
53383e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,pointIS,qGeom,PETSC_TRUE,&fgeom);CHKERRQ(ierr);
53393e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
53403e9753d6SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
53413e9753d6SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
53423e9753d6SMatthew G. Knepley     ierr = PetscFree4(u, u_t, elemMat, a);CHKERRQ(ierr);
53433e9753d6SMatthew G. Knepley   }
53443e9753d6SMatthew G. Knepley   if (plex)  {ierr = DMDestroy(&plex);CHKERRQ(ierr);}
53453e9753d6SMatthew G. Knepley   if (plexA) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
53463e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
53473e9753d6SMatthew G. Knepley }
53483e9753d6SMatthew G. Knepley 
534945480ffeSMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobianSingle(DM dm, PetscReal t, PetscWeakForm wf, DMLabel label, PetscInt numValues, const PetscInt values[], PetscInt field, Vec locX, Vec locX_t, PetscReal X_tShift, Mat Jac, Mat JacP)
53503e9753d6SMatthew G. Knepley {
53513e9753d6SMatthew G. Knepley   DMField        coordField;
53523e9753d6SMatthew G. Knepley   DMLabel        depthLabel;
53533e9753d6SMatthew G. Knepley   IS             facetIS;
53543e9753d6SMatthew G. Knepley   PetscInt       dim;
53553e9753d6SMatthew G. Knepley   PetscErrorCode ierr;
53563e9753d6SMatthew G. Knepley 
53573e9753d6SMatthew G. Knepley   PetscFunctionBegin;
53583e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
53593e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
53603e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
53613e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
536245480ffeSMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, wf, label, numValues, values, field, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
53633e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
53643e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
53653e9753d6SMatthew G. Knepley }
53663e9753d6SMatthew G. Knepley 
53673e9753d6SMatthew G. Knepley PetscErrorCode DMPlexComputeBdJacobian_Internal(DM dm, Vec locX, Vec locX_t, PetscReal t, PetscReal X_tShift, Mat Jac, Mat JacP, void *user)
53683e9753d6SMatthew G. Knepley {
53693e9753d6SMatthew G. Knepley   PetscDS          prob;
53703e9753d6SMatthew G. Knepley   PetscInt         dim, numBd, bd;
53713e9753d6SMatthew G. Knepley   DMLabel          depthLabel;
53723e9753d6SMatthew G. Knepley   DMField          coordField = NULL;
53733e9753d6SMatthew G. Knepley   IS               facetIS;
53743e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
53753e9753d6SMatthew G. Knepley 
53763e9753d6SMatthew G. Knepley   PetscFunctionBegin;
53773e9753d6SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
53783e9753d6SMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
53793e9753d6SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
53803e9753d6SMatthew G. Knepley   ierr = DMLabelGetStratumIS(depthLabel, dim-1, &facetIS);CHKERRQ(ierr);
53813e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(prob, &numBd);CHKERRQ(ierr);
53823e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
53833e9753d6SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
538445480ffeSMatthew G. Knepley     PetscWeakForm           wf;
53853e9753d6SMatthew G. Knepley     DMBoundaryConditionType type;
53863e9753d6SMatthew G. Knepley     DMLabel                 label;
53873e9753d6SMatthew G. Knepley     const PetscInt         *values;
53883e9753d6SMatthew G. Knepley     PetscInt                fieldI, numValues;
53893e9753d6SMatthew G. Knepley     PetscObject             obj;
53903e9753d6SMatthew G. Knepley     PetscClassId            id;
53913e9753d6SMatthew G. Knepley 
539245480ffeSMatthew G. Knepley     ierr = PetscDSGetBoundary(prob, bd, &wf, &type, NULL, &label, &numValues, &values, &fieldI, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
53933e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, &obj);CHKERRQ(ierr);
53943e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
53953e9753d6SMatthew G. Knepley     if ((id != PETSCFE_CLASSID) || (type & DM_BC_ESSENTIAL)) continue;
539645480ffeSMatthew G. Knepley     ierr = DMPlexComputeBdJacobian_Single_Internal(dm, t, wf, label, numValues, values, fieldI, locX, locX_t, X_tShift, Jac, JacP, coordField, facetIS);CHKERRQ(ierr);
53973e9753d6SMatthew G. Knepley   }
53983e9753d6SMatthew G. Knepley   ierr = ISDestroy(&facetIS);CHKERRQ(ierr);
53993e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
54003e9753d6SMatthew G. Knepley }
54013e9753d6SMatthew G. Knepley 
540206ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Mat Jac, Mat JacP,void *user)
54033e9753d6SMatthew G. Knepley {
54043e9753d6SMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *) dm->data;
54053e9753d6SMatthew G. Knepley   const char     *name  = "Jacobian";
54069a2a23afSMatthew G. Knepley   DM              dmAux = NULL, plex, tdm;
54073e9753d6SMatthew G. Knepley   DMEnclosureType encAux;
54083e9753d6SMatthew G. Knepley   Vec             A, tv;
54093e9753d6SMatthew G. Knepley   DMField         coordField;
54103e9753d6SMatthew G. Knepley   PetscDS         prob, probAux = NULL;
5411e432b41dSStefano Zampini   PetscSection    section, globalSection, sectionAux;
54123e9753d6SMatthew G. Knepley   PetscScalar    *elemMat, *elemMatP, *elemMatD, *u, *u_t, *a = NULL;
54133e9753d6SMatthew G. Knepley   const PetscInt *cells;
54143e9753d6SMatthew G. Knepley   PetscInt        Nf, fieldI, fieldJ;
54153e9753d6SMatthew G. Knepley   PetscInt        totDim, totDimAux, cStart, cEnd, numCells, c;
5416e432b41dSStefano Zampini   PetscBool       hasJac, hasPrec, hasDyn, hasFV = PETSC_FALSE, transform;
54173e9753d6SMatthew G. Knepley   PetscErrorCode  ierr;
54183e9753d6SMatthew G. Knepley 
54193e9753d6SMatthew G. Knepley   PetscFunctionBegin;
54203e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
54213e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
54223e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
54233e9753d6SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
54243e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformDM_Internal(dm, &tdm);CHKERRQ(ierr);
54253e9753d6SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
54263e9753d6SMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
54273e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
54283e9753d6SMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
54293e9753d6SMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
54303e9753d6SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
54313e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobian(prob, &hasJac);CHKERRQ(ierr);
54323e9753d6SMatthew G. Knepley   ierr = PetscDSHasJacobianPreconditioner(prob, &hasPrec);CHKERRQ(ierr);
54333e9753d6SMatthew G. Knepley   /* user passed in the same matrix, avoid double contributions and
54343e9753d6SMatthew G. Knepley      only assemble the Jacobian */
54353e9753d6SMatthew G. Knepley   if (hasJac && Jac == JacP) hasPrec = PETSC_FALSE;
54363e9753d6SMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
54373e9753d6SMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
5438ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, key.part, &A);CHKERRQ(ierr);
54399a2a23afSMatthew G. Knepley   if (A) {
54409a2a23afSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
54413e9753d6SMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
54423e9753d6SMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plex);CHKERRQ(ierr);
54433e9753d6SMatthew G. Knepley     ierr = DMGetLocalSection(plex, &sectionAux);CHKERRQ(ierr);
54443e9753d6SMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
54453e9753d6SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
54463e9753d6SMatthew G. Knepley   }
54473e9753d6SMatthew G. Knepley   ierr = PetscMalloc5(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,hasJac ? numCells*totDim*totDim : 0,&elemMat,hasPrec ? numCells*totDim*totDim : 0, &elemMatP,hasDyn ? numCells*totDim*totDim : 0, &elemMatD);CHKERRQ(ierr);
54483e9753d6SMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
54493e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
54503e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
54513e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
54523e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
54533e9753d6SMatthew G. Knepley     PetscScalar   *x = NULL,  *x_t = NULL;
54543e9753d6SMatthew G. Knepley     PetscInt       i;
54553e9753d6SMatthew G. Knepley 
54563e9753d6SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
54573e9753d6SMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
54583e9753d6SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, section, X, cell, NULL, &x);CHKERRQ(ierr);
54593e9753d6SMatthew G. Knepley     if (X_t) {
54603e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
54613e9753d6SMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
54623e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
54633e9753d6SMatthew G. Knepley     }
54643e9753d6SMatthew G. Knepley     if (dmAux) {
54653e9753d6SMatthew G. Knepley       PetscInt subcell;
54663e9753d6SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr);
54673e9753d6SMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
54683e9753d6SMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
54693e9753d6SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
54703e9753d6SMatthew G. Knepley     }
54713e9753d6SMatthew G. Knepley   }
54723e9753d6SMatthew G. Knepley   if (hasJac)  {ierr = PetscArrayzero(elemMat,  numCells*totDim*totDim);CHKERRQ(ierr);}
54733e9753d6SMatthew G. Knepley   if (hasPrec) {ierr = PetscArrayzero(elemMatP, numCells*totDim*totDim);CHKERRQ(ierr);}
54743e9753d6SMatthew G. Knepley   if (hasDyn)  {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);}
54753e9753d6SMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
54763e9753d6SMatthew G. Knepley     PetscClassId    id;
54773e9753d6SMatthew G. Knepley     PetscFE         fe;
54783e9753d6SMatthew G. Knepley     PetscQuadrature qGeom = NULL;
54793e9753d6SMatthew G. Knepley     PetscInt        Nb;
54803e9753d6SMatthew G. Knepley     /* Conforming batches */
54813e9753d6SMatthew G. Knepley     PetscInt        numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
54823e9753d6SMatthew G. Knepley     /* Remainder */
54833e9753d6SMatthew G. Knepley     PetscInt        Nr, offset, Nq;
54843e9753d6SMatthew G. Knepley     PetscInt        maxDegree;
54853e9753d6SMatthew G. Knepley     PetscFEGeom     *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
54863e9753d6SMatthew G. Knepley 
54873e9753d6SMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
54883e9753d6SMatthew G. Knepley     ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
54893e9753d6SMatthew G. Knepley     if (id == PETSCFV_CLASSID) {hasFV = PETSC_TRUE; continue;}
54903e9753d6SMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
54913e9753d6SMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
54923e9753d6SMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
54933e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
54943e9753d6SMatthew G. Knepley       ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);
54953e9753d6SMatthew G. Knepley     }
54963e9753d6SMatthew G. Knepley     if (!qGeom) {
54973e9753d6SMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
54983e9753d6SMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
54993e9753d6SMatthew G. Knepley     }
55003e9753d6SMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
55013e9753d6SMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
55023e9753d6SMatthew G. Knepley     blockSize = Nb;
55033e9753d6SMatthew G. Knepley     batchSize = numBlocks * blockSize;
55043e9753d6SMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
55053e9753d6SMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
55063e9753d6SMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
55073e9753d6SMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
55083e9753d6SMatthew G. Knepley     offset    = numCells - Nr;
55093e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
55103e9753d6SMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
55113e9753d6SMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
55126528b96dSMatthew G. Knepley       key.field = fieldI*Nf+fieldJ;
55133e9753d6SMatthew G. Knepley       if (hasJac) {
55146528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
55156528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
55163e9753d6SMatthew G. Knepley       }
55173e9753d6SMatthew G. Knepley       if (hasPrec) {
55186528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatP);CHKERRQ(ierr);
55196528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_PRE, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
55203e9753d6SMatthew G. Knepley       }
55213e9753d6SMatthew G. Knepley       if (hasDyn) {
55226528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
55236528b96dSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
55243e9753d6SMatthew G. Knepley       }
55253e9753d6SMatthew G. Knepley     }
55263e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
55273e9753d6SMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
55283e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
55293e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
55303e9753d6SMatthew G. Knepley   }
55313e9753d6SMatthew G. Knepley   /*   Add contribution from X_t */
55323e9753d6SMatthew G. Knepley   if (hasDyn) {for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];}
55333e9753d6SMatthew G. Knepley   if (hasFV) {
55343e9753d6SMatthew G. Knepley     PetscClassId id;
55353e9753d6SMatthew G. Knepley     PetscFV      fv;
55363e9753d6SMatthew G. Knepley     PetscInt     offsetI, NcI, NbI = 1, fc, f;
55373e9753d6SMatthew G. Knepley 
55383e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
55393e9753d6SMatthew G. Knepley       ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fv);CHKERRQ(ierr);
55403e9753d6SMatthew G. Knepley       ierr = PetscDSGetFieldOffset(prob, fieldI, &offsetI);CHKERRQ(ierr);
55413e9753d6SMatthew G. Knepley       ierr = PetscObjectGetClassId((PetscObject) fv, &id);CHKERRQ(ierr);
55423e9753d6SMatthew G. Knepley       if (id != PETSCFV_CLASSID) continue;
55433e9753d6SMatthew G. Knepley       /* Put in the identity */
55443e9753d6SMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &NcI);CHKERRQ(ierr);
55453e9753d6SMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
55463e9753d6SMatthew G. Knepley         const PetscInt cind    = c - cStart;
55473e9753d6SMatthew G. Knepley         const PetscInt eOffset = cind*totDim*totDim;
55483e9753d6SMatthew G. Knepley         for (fc = 0; fc < NcI; ++fc) {
55493e9753d6SMatthew G. Knepley           for (f = 0; f < NbI; ++f) {
55503e9753d6SMatthew G. Knepley             const PetscInt i = offsetI + f*NcI+fc;
55513e9753d6SMatthew G. Knepley             if (hasPrec) {
55523e9753d6SMatthew G. Knepley               if (hasJac) {elemMat[eOffset+i*totDim+i] = 1.0;}
55533e9753d6SMatthew G. Knepley               elemMatP[eOffset+i*totDim+i] = 1.0;
55543e9753d6SMatthew G. Knepley             } else {elemMat[eOffset+i*totDim+i] = 1.0;}
55553e9753d6SMatthew G. Knepley           }
55563e9753d6SMatthew G. Knepley         }
55573e9753d6SMatthew G. Knepley       }
55583e9753d6SMatthew G. Knepley     }
55593e9753d6SMatthew G. Knepley     /* No allocated space for FV stuff, so ignore the zero entries */
55603e9753d6SMatthew G. Knepley     ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);CHKERRQ(ierr);
55613e9753d6SMatthew G. Knepley   }
55623e9753d6SMatthew G. Knepley   /* Insert values into matrix */
55633e9753d6SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
55643e9753d6SMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
55653e9753d6SMatthew G. Knepley     const PetscInt cind = c - cStart;
55663e9753d6SMatthew G. Knepley 
55673e9753d6SMatthew G. Knepley     /* Transform to global basis before insertion in Jacobian */
55683e9753d6SMatthew G. Knepley     if (transform) {ierr = DMPlexBasisTransformPointTensor_Internal(dm, tdm, tv, cell, PETSC_TRUE, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
55693e9753d6SMatthew G. Knepley     if (hasPrec) {
55703e9753d6SMatthew G. Knepley       if (hasJac) {
55713e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
55723e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
55733e9753d6SMatthew G. Knepley       }
55743e9753d6SMatthew G. Knepley       if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
55753e9753d6SMatthew G. Knepley       ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
55763e9753d6SMatthew G. Knepley     } else {
55773e9753d6SMatthew G. Knepley       if (hasJac) {
55783e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
55793e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(dm, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
55803e9753d6SMatthew G. Knepley       }
55813e9753d6SMatthew G. Knepley     }
55823e9753d6SMatthew G. Knepley   }
55833e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
55843e9753d6SMatthew G. Knepley   if (hasFV) {ierr = MatSetOption(JacP, MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);CHKERRQ(ierr);}
55853e9753d6SMatthew G. Knepley   ierr = PetscFree5(u,u_t,elemMat,elemMatP,elemMatD);CHKERRQ(ierr);
55863e9753d6SMatthew G. Knepley   if (dmAux) {
55873e9753d6SMatthew G. Knepley     ierr = PetscFree(a);CHKERRQ(ierr);
55883e9753d6SMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
55893e9753d6SMatthew G. Knepley   }
55903e9753d6SMatthew G. Knepley   /* Compute boundary integrals */
55913e9753d6SMatthew G. Knepley   ierr = DMPlexComputeBdJacobian_Internal(dm, X, X_t, t, X_tShift, Jac, JacP, user);CHKERRQ(ierr);
55923e9753d6SMatthew G. Knepley   /* Assemble matrix */
55933e9753d6SMatthew G. Knepley   if (hasJac && hasPrec) {
55943e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
55953e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
55963e9753d6SMatthew G. Knepley   }
55973e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
55983e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
55993e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
56003e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
56013e9753d6SMatthew G. Knepley }
56023e9753d6SMatthew G. Knepley 
560306ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Hybrid_Internal(DM dm, PetscFormKey key[], IS cellIS, PetscReal t, PetscReal X_tShift, Vec locX, Vec locX_t, Mat Jac, Mat JacP, void *user)
56043e9753d6SMatthew G. Knepley {
56053e9753d6SMatthew G. Knepley   DM_Plex         *mesh          = (DM_Plex *) dm->data;
56063e9753d6SMatthew G. Knepley   const char      *name          = "Hybrid Jacobian";
5607148442b3SMatthew G. Knepley   DM               dmAux[3]      = {NULL, NULL, NULL};
5608148442b3SMatthew G. Knepley   DMLabel          ghostLabel    = NULL;
56093e9753d6SMatthew G. Knepley   DM               plex          = NULL;
56103e9753d6SMatthew G. Knepley   DM               plexA         = NULL;
5611148442b3SMatthew G. Knepley   PetscDS          ds            = NULL;
5612148442b3SMatthew G. Knepley   PetscDS          dsAux[3]      = {NULL, NULL, NULL};
5613148442b3SMatthew G. Knepley   Vec              locA[3]       = {NULL, NULL, NULL};
56143e9753d6SMatthew G. Knepley   PetscSection     section       = NULL;
5615148442b3SMatthew G. Knepley   PetscSection     sectionAux[3] = {NULL, NULL, NULL};
56163e9753d6SMatthew G. Knepley   DMField          coordField    = NULL;
5617148442b3SMatthew G. Knepley   PetscScalar     *u = NULL, *u_t, *a[3];
56183e9753d6SMatthew G. Knepley   PetscScalar     *elemMat, *elemMatP;
5619e432b41dSStefano Zampini   PetscSection     globalSection;
56203e9753d6SMatthew G. Knepley   IS               chunkIS;
56213e9753d6SMatthew G. Knepley   const PetscInt  *cells;
56223e9753d6SMatthew G. Knepley   PetscInt        *faces;
56233e9753d6SMatthew G. Knepley   PetscInt         cStart, cEnd, numCells;
5624148442b3SMatthew G. Knepley   PetscInt         Nf, fieldI, fieldJ, totDim, totDimAux[3], numChunks, cellChunkSize, chunk;
56253e9753d6SMatthew G. Knepley   PetscInt         maxDegree = PETSC_MAX_INT;
56263e9753d6SMatthew G. Knepley   PetscQuadrature  affineQuad = NULL, *quads = NULL;
56273e9753d6SMatthew G. Knepley   PetscFEGeom     *affineGeom = NULL, **geoms = NULL;
5628e432b41dSStefano Zampini   PetscBool        hasBdJac, hasBdPrec;
56293e9753d6SMatthew G. Knepley   PetscErrorCode   ierr;
56303e9753d6SMatthew G. Knepley 
56313e9753d6SMatthew G. Knepley   PetscFunctionBegin;
56325fedec97SMatthew G. Knepley   if ((key[0].label == key[1].label) && (key[0].value == key[1].value) && (key[0].part == key[1].part)) {
56335fedec97SMatthew G. Knepley     const char *name;
56345fedec97SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) key[0].label, &name);CHKERRQ(ierr);
563598921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Form keys for each side of a cohesive surface must be different (%s, %D, %D)", name, key[0].value, key[0].part);
56365fedec97SMatthew G. Knepley   }
56373e9753d6SMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
56383e9753d6SMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
56393e9753d6SMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
56403e9753d6SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
56413e9753d6SMatthew G. Knepley   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
56423e9753d6SMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
56433e9753d6SMatthew G. Knepley   ierr = DMGetLabel(dm, "ghost", &ghostLabel);CHKERRQ(ierr);
5644148442b3SMatthew G. Knepley   ierr = DMGetCellDS(dm, cStart, &ds);CHKERRQ(ierr);
5645148442b3SMatthew G. Knepley   ierr = PetscDSGetNumFields(ds, &Nf);CHKERRQ(ierr);
5646148442b3SMatthew G. Knepley   ierr = PetscDSGetTotalDimension(ds, &totDim);CHKERRQ(ierr);
5647148442b3SMatthew G. Knepley   ierr = PetscDSHasBdJacobian(ds, &hasBdJac);CHKERRQ(ierr);
5648148442b3SMatthew G. Knepley   ierr = PetscDSHasBdJacobianPreconditioner(ds, &hasBdPrec);CHKERRQ(ierr);
5649ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key[2].label, key[2].value, key[2].part, &locA[2]);CHKERRQ(ierr);
5650148442b3SMatthew G. Knepley   if (locA[2]) {
5651148442b3SMatthew G. Knepley     ierr = VecGetDM(locA[2], &dmAux[2]);CHKERRQ(ierr);
5652148442b3SMatthew G. Knepley     ierr = DMConvert(dmAux[2], DMPLEX, &plexA);CHKERRQ(ierr);
5653148442b3SMatthew G. Knepley     ierr = DMGetSection(dmAux[2], &sectionAux[2]);CHKERRQ(ierr);
5654148442b3SMatthew G. Knepley     ierr = DMGetCellDS(dmAux[2], cStart, &dsAux[2]);CHKERRQ(ierr);
5655148442b3SMatthew G. Knepley     ierr = PetscDSGetTotalDimension(dsAux[2], &totDimAux[2]);CHKERRQ(ierr);
5656148442b3SMatthew G. Knepley     {
5657148442b3SMatthew G. Knepley       const PetscInt *cone;
5658148442b3SMatthew G. Knepley       PetscInt        c;
5659148442b3SMatthew G. Knepley 
5660148442b3SMatthew G. Knepley       ierr = DMPlexGetCone(dm, cStart, &cone);CHKERRQ(ierr);
5661148442b3SMatthew G. Knepley       for (c = 0; c < 2; ++c) {
5662148442b3SMatthew G. Knepley         const PetscInt *support;
5663148442b3SMatthew G. Knepley         PetscInt ssize, s;
5664148442b3SMatthew G. Knepley 
5665148442b3SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5666148442b3SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, cone[c], &ssize);CHKERRQ(ierr);
56672c71b3e2SJacob Faibussowitsch         PetscCheckFalse(ssize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D from cell %D has support size %D != 2", cone[c], cStart, ssize);
5668148442b3SMatthew G. Knepley         if      (support[0] == cStart) s = 1;
5669148442b3SMatthew G. Knepley         else if (support[1] == cStart) s = 0;
567098921bdaSJacob Faibussowitsch         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D does not have cell %D in its support", cone[c], cStart);
5671ac17215fSMatthew G. Knepley         ierr = DMGetAuxiliaryVec(dm, key[c].label, key[c].value, key[c].part, &locA[c]);CHKERRQ(ierr);
5672148442b3SMatthew G. Knepley         if (locA[c]) {ierr = VecGetDM(locA[c], &dmAux[c]);CHKERRQ(ierr);}
5673148442b3SMatthew G. Knepley         else         {dmAux[c] = dmAux[2];}
5674148442b3SMatthew G. Knepley         ierr = DMGetCellDS(dmAux[c], support[s], &dsAux[c]);CHKERRQ(ierr);
5675148442b3SMatthew G. Knepley         ierr = PetscDSGetTotalDimension(dsAux[c], &totDimAux[c]);CHKERRQ(ierr);
5676148442b3SMatthew G. Knepley       }
5677148442b3SMatthew G. Knepley     }
56783e9753d6SMatthew G. Knepley   }
56793e9753d6SMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
56803e9753d6SMatthew G. Knepley   ierr = DMFieldGetDegree(coordField, cellIS, NULL, &maxDegree);CHKERRQ(ierr);
56813e9753d6SMatthew G. Knepley   if (maxDegree > 1) {
56823e9753d6SMatthew G. Knepley     PetscInt f;
56833e9753d6SMatthew G. Knepley     ierr = PetscCalloc2(Nf, &quads, Nf, &geoms);CHKERRQ(ierr);
56843e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
56853e9753d6SMatthew G. Knepley       PetscFE fe;
56863e9753d6SMatthew G. Knepley 
5687148442b3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, f, (PetscObject *) &fe);CHKERRQ(ierr);
56883e9753d6SMatthew G. Knepley       if (fe) {
56893e9753d6SMatthew G. Knepley         ierr = PetscFEGetQuadrature(fe, &quads[f]);CHKERRQ(ierr);
56903e9753d6SMatthew G. Knepley         ierr = PetscObjectReference((PetscObject) quads[f]);CHKERRQ(ierr);
56913e9753d6SMatthew G. Knepley       }
56923e9753d6SMatthew G. Knepley     }
56933e9753d6SMatthew G. Knepley   }
56943e9753d6SMatthew G. Knepley   cellChunkSize = numCells;
56953e9753d6SMatthew G. Knepley   numChunks     = !numCells ? 0 : PetscCeilReal(((PetscReal) numCells)/cellChunkSize);
56963e9753d6SMatthew G. Knepley   ierr = PetscCalloc1(2*cellChunkSize, &faces);CHKERRQ(ierr);
56973e9753d6SMatthew G. Knepley   ierr = ISCreateGeneral(PETSC_COMM_SELF, cellChunkSize, faces, PETSC_USE_POINTER, &chunkIS);CHKERRQ(ierr);
5698148442b3SMatthew G. Knepley   ierr = DMPlexGetCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
5699148442b3SMatthew G. Knepley   ierr = DMPlexGetHybridAuxFields(dm, dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
57003e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
57013e9753d6SMatthew G. Knepley   ierr = DMGetWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
57023e9753d6SMatthew G. Knepley   for (chunk = 0; chunk < numChunks; ++chunk) {
57033e9753d6SMatthew G. Knepley     PetscInt cS = cStart+chunk*cellChunkSize, cE = PetscMin(cS+cellChunkSize, cEnd), numCells = cE - cS, c;
57043e9753d6SMatthew G. Knepley 
57053e9753d6SMatthew G. Knepley     if (hasBdJac)  {ierr = PetscMemzero(elemMat,  numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
57063e9753d6SMatthew G. Knepley     if (hasBdPrec) {ierr = PetscMemzero(elemMatP, numCells*totDim*totDim * sizeof(PetscScalar));CHKERRQ(ierr);}
57073e9753d6SMatthew G. Knepley     /* Get faces */
57083e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
57093e9753d6SMatthew G. Knepley       const PetscInt  cell = cells ? cells[c] : c;
57103e9753d6SMatthew G. Knepley       const PetscInt *cone;
57113e9753d6SMatthew G. Knepley       ierr = DMPlexGetCone(plex, cell, &cone);CHKERRQ(ierr);
57123e9753d6SMatthew G. Knepley       faces[(c-cS)*2+0] = cone[0];
57133e9753d6SMatthew G. Knepley       faces[(c-cS)*2+1] = cone[1];
57143e9753d6SMatthew G. Knepley     }
57153e9753d6SMatthew G. Knepley     ierr = ISGeneralSetIndices(chunkIS, cellChunkSize, faces, PETSC_USE_POINTER);CHKERRQ(ierr);
57163e9753d6SMatthew G. Knepley     if (maxDegree <= 1) {
57173e9753d6SMatthew G. Knepley       if (!affineQuad) {ierr = DMFieldCreateDefaultQuadrature(coordField, chunkIS, &affineQuad);CHKERRQ(ierr);}
57183e9753d6SMatthew G. Knepley       if (affineQuad)  {ierr = DMSNESGetFEGeom(coordField, chunkIS, affineQuad, PETSC_TRUE, &affineGeom);CHKERRQ(ierr);}
57193e9753d6SMatthew G. Knepley     } else {
57203e9753d6SMatthew G. Knepley       PetscInt f;
57213e9753d6SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
57223e9753d6SMatthew G. Knepley         if (quads[f]) {ierr = DMSNESGetFEGeom(coordField, chunkIS, quads[f], PETSC_TRUE, &geoms[f]);CHKERRQ(ierr);}
57233e9753d6SMatthew G. Knepley       }
57243e9753d6SMatthew G. Knepley     }
57253e9753d6SMatthew G. Knepley 
57263e9753d6SMatthew G. Knepley     for (fieldI = 0; fieldI < Nf; ++fieldI) {
57273e9753d6SMatthew G. Knepley       PetscFE         feI;
57283e9753d6SMatthew G. Knepley       PetscFEGeom    *geom = affineGeom ? affineGeom : geoms[fieldI];
57293e9753d6SMatthew G. Knepley       PetscFEGeom    *chunkGeom = NULL, *remGeom = NULL;
57303e9753d6SMatthew G. Knepley       PetscQuadrature quad = affineQuad ? affineQuad : quads[fieldI];
57313e9753d6SMatthew G. Knepley       PetscInt        numChunks, numBatches, batchSize, numBlocks, blockSize, Ne, Nr, offset, Nq, Nb;
57325fedec97SMatthew G. Knepley       PetscBool       isCohesiveField;
57333e9753d6SMatthew G. Knepley 
5734148442b3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(ds, fieldI, (PetscObject *) &feI);CHKERRQ(ierr);
57353e9753d6SMatthew G. Knepley       if (!feI) continue;
57363e9753d6SMatthew G. Knepley       ierr = PetscFEGetTileSizes(feI, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
57373e9753d6SMatthew G. Knepley       ierr = PetscQuadratureGetData(quad, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
57383e9753d6SMatthew G. Knepley       ierr = PetscFEGetDimension(feI, &Nb);CHKERRQ(ierr);
57393e9753d6SMatthew G. Knepley       blockSize = Nb;
57403e9753d6SMatthew G. Knepley       batchSize = numBlocks * blockSize;
57413e9753d6SMatthew G. Knepley       ierr      = PetscFESetTileSizes(feI, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
57423e9753d6SMatthew G. Knepley       numChunks = numCells / (numBatches*batchSize);
57433e9753d6SMatthew G. Knepley       Ne        = numChunks*numBatches*batchSize;
57443e9753d6SMatthew G. Knepley       Nr        = numCells % (numBatches*batchSize);
57453e9753d6SMatthew G. Knepley       offset    = numCells - Nr;
57463e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
57473e9753d6SMatthew G. Knepley       ierr = PetscFEGeomGetChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
57485fedec97SMatthew G. Knepley       ierr = PetscDSGetCohesive(ds, fieldI, &isCohesiveField);CHKERRQ(ierr);
57493e9753d6SMatthew G. Knepley       for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
57503e9753d6SMatthew G. Knepley         PetscFE feJ;
57513e9753d6SMatthew G. Knepley 
5752148442b3SMatthew G. Knepley         ierr = PetscDSGetDiscretization(ds, fieldJ, (PetscObject *) &feJ);CHKERRQ(ierr);
57533e9753d6SMatthew G. Knepley         if (!feJ) continue;
5754148442b3SMatthew G. Knepley         key[0].field = fieldI*Nf+fieldJ;
5755148442b3SMatthew G. Knepley         key[1].field = fieldI*Nf+fieldJ;
57565fedec97SMatthew G. Knepley         key[2].field = fieldI*Nf+fieldJ;
5757148442b3SMatthew G. Knepley         if (hasBdJac) {
57585fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, X_tShift, elemMat);CHKERRQ(ierr);
57595fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[0], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
57605fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, X_tShift, elemMat);CHKERRQ(ierr);
57615fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[1], 1, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
5762148442b3SMatthew G. Knepley         }
5763148442b3SMatthew G. Knepley         if (hasBdPrec) {
57645fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[0], 0, Ne, chunkGeom, u, u_t, dsAux[0], a[0], t, X_tShift, elemMatP);CHKERRQ(ierr);
57655fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[0], 0, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[0], &a[0][offset*totDimAux[0]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
57665fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[1], 1, Ne, chunkGeom, u, u_t, dsAux[1], a[1], t, X_tShift, elemMatP);CHKERRQ(ierr);
57675fedec97SMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[1], 1, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[1], &a[1][offset*totDimAux[1]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
5768148442b3SMatthew G. Knepley         }
57695fedec97SMatthew G. Knepley         if (hasBdJac) {
57709ee2af8cSMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[2], 2, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, X_tShift, elemMat);CHKERRQ(ierr);
57719ee2af8cSMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN, key[2], 2, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
5772148442b3SMatthew G. Knepley         }
57735fedec97SMatthew G. Knepley         if (hasBdPrec) {
57749ee2af8cSMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[2], 2, Ne, chunkGeom, u, u_t, dsAux[2], a[2], t, X_tShift, elemMatP);CHKERRQ(ierr);
57759ee2af8cSMatthew G. Knepley           ierr = PetscFEIntegrateHybridJacobian(ds, PETSCFE_JACOBIAN_PRE, key[2], 2, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, dsAux[2], &a[2][offset*totDimAux[2]], t, X_tShift, &elemMatP[offset*totDim*totDim]);CHKERRQ(ierr);
57763e9753d6SMatthew G. Knepley         }
57773e9753d6SMatthew G. Knepley       }
57783e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,offset,numCells,&remGeom);CHKERRQ(ierr);
57793e9753d6SMatthew G. Knepley       ierr = PetscFEGeomRestoreChunk(geom,0,offset,&chunkGeom);CHKERRQ(ierr);
57803e9753d6SMatthew G. Knepley     }
57813e9753d6SMatthew G. Knepley     /* Insert values into matrix */
57823e9753d6SMatthew G. Knepley     for (c = cS; c < cE; ++c) {
57833e9753d6SMatthew G. Knepley       const PetscInt cell = cells ? cells[c] : c;
57843e9753d6SMatthew G. Knepley       const PetscInt cind = c - cS;
57853e9753d6SMatthew G. Knepley 
57863e9753d6SMatthew G. Knepley       if (hasBdPrec) {
57873e9753d6SMatthew G. Knepley         if (hasBdJac) {
57883e9753d6SMatthew G. Knepley           if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
57893e9753d6SMatthew G. Knepley           ierr = DMPlexMatSetClosure(plex, section, globalSection, Jac, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57903e9753d6SMatthew G. Knepley         }
57913e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMatP[cind*totDim*totDim]);CHKERRQ(ierr);}
57923e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMatP[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57933e9753d6SMatthew G. Knepley       } else if (hasBdJac) {
57943e9753d6SMatthew G. Knepley         if (mesh->printFEM > 1) {ierr = DMPrintCellMatrix(cell, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);}
57953e9753d6SMatthew G. Knepley         ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, cell, &elemMat[cind*totDim*totDim], ADD_VALUES);CHKERRQ(ierr);
57963e9753d6SMatthew G. Knepley       }
57973e9753d6SMatthew G. Knepley     }
57983e9753d6SMatthew G. Knepley   }
5799148442b3SMatthew G. Knepley   ierr = DMPlexRestoreCellFields(dm, cellIS, locX, locX_t, locA[2], &u, &u_t, &a[2]);CHKERRQ(ierr);
5800148442b3SMatthew G. Knepley   ierr = DMPlexRestoreHybridAuxFields(dmAux, dsAux, cellIS, locA, a);CHKERRQ(ierr);
58013e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdJac  ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMat);CHKERRQ(ierr);
58023e9753d6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, hasBdPrec ? cellChunkSize*totDim*totDim : 0, MPIU_SCALAR, &elemMatP);CHKERRQ(ierr);
58033e9753d6SMatthew G. Knepley   ierr = PetscFree(faces);CHKERRQ(ierr);
58043e9753d6SMatthew G. Knepley   ierr = ISDestroy(&chunkIS);CHKERRQ(ierr);
58053e9753d6SMatthew G. Knepley   ierr = ISRestorePointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
58063e9753d6SMatthew G. Knepley   if (maxDegree <= 1) {
58073e9753d6SMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,affineQuad,PETSC_FALSE,&affineGeom);CHKERRQ(ierr);
58083e9753d6SMatthew G. Knepley     ierr = PetscQuadratureDestroy(&affineQuad);CHKERRQ(ierr);
58093e9753d6SMatthew G. Knepley   } else {
58103e9753d6SMatthew G. Knepley     PetscInt f;
58113e9753d6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
58123e9753d6SMatthew G. Knepley       if (geoms) {ierr = DMSNESRestoreFEGeom(coordField,cellIS,quads[f],PETSC_FALSE, &geoms[f]);CHKERRQ(ierr);}
58133e9753d6SMatthew G. Knepley       if (quads) {ierr = PetscQuadratureDestroy(&quads[f]);CHKERRQ(ierr);}
58143e9753d6SMatthew G. Knepley     }
58153e9753d6SMatthew G. Knepley     ierr = PetscFree2(quads,geoms);CHKERRQ(ierr);
58163e9753d6SMatthew G. Knepley   }
5817148442b3SMatthew G. Knepley   if (dmAux[2]) {ierr = DMDestroy(&plexA);CHKERRQ(ierr);}
58183e9753d6SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
58193e9753d6SMatthew G. Knepley   /* Assemble matrix */
58203e9753d6SMatthew G. Knepley   if (hasBdJac && hasBdPrec) {
58213e9753d6SMatthew G. Knepley     ierr = MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
58223e9753d6SMatthew G. Knepley     ierr = MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
58233e9753d6SMatthew G. Knepley   }
58243e9753d6SMatthew G. Knepley   ierr = MatAssemblyBegin(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
58253e9753d6SMatthew G. Knepley   ierr = MatAssemblyEnd(JacP, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
58263e9753d6SMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
58273e9753d6SMatthew G. Knepley   PetscFunctionReturn(0);
58283e9753d6SMatthew G. Knepley }
58298e3a2eefSMatthew G. Knepley 
58308e3a2eefSMatthew G. Knepley /*
58318e3a2eefSMatthew G. Knepley   DMPlexComputeJacobian_Action_Internal - Form the local portion of the Jacobian action Z = J(X) Y at the local solution X using pointwise functions specified by the user.
58328e3a2eefSMatthew G. Knepley 
58338e3a2eefSMatthew G. Knepley   Input Parameters:
58348e3a2eefSMatthew G. Knepley + dm     - The mesh
58358e3a2eefSMatthew G. Knepley . key    - The PetscWeakFormKey indcating where integration should happen
583606ad1575SMatthew G. Knepley . cellIS - The cells to integrate over
58378e3a2eefSMatthew G. Knepley . t      - The time
58388e3a2eefSMatthew G. Knepley . X_tShift - The multiplier for the Jacobian with repsect to X_t
58398e3a2eefSMatthew G. Knepley . X      - Local solution vector
58408e3a2eefSMatthew G. Knepley . X_t    - Time-derivative of the local solution vector
58418e3a2eefSMatthew G. Knepley . Y      - Local input vector
584206ad1575SMatthew G. Knepley - user   - the user context
58438e3a2eefSMatthew G. Knepley 
58448e3a2eefSMatthew G. Knepley   Output Parameter:
58458e3a2eefSMatthew G. Knepley . Z - Local output vector
58468e3a2eefSMatthew G. Knepley 
58478e3a2eefSMatthew G. Knepley   Note:
58488e3a2eefSMatthew G. Knepley   We form the residual one batch of elements at a time. This allows us to offload work onto an accelerator,
58498e3a2eefSMatthew G. Knepley   like a GPU, or vectorize on a multicore machine.
58508e3a2eefSMatthew G. Knepley */
585106ad1575SMatthew G. Knepley PetscErrorCode DMPlexComputeJacobian_Action_Internal(DM dm, PetscFormKey key, IS cellIS, PetscReal t, PetscReal X_tShift, Vec X, Vec X_t, Vec Y, Vec Z, void *user)
58528e3a2eefSMatthew G. Knepley {
58538e3a2eefSMatthew G. Knepley   DM_Plex        *mesh  = (DM_Plex *) dm->data;
58548e3a2eefSMatthew G. Knepley   const char     *name  = "Jacobian";
58558e3a2eefSMatthew G. Knepley   DM              dmAux = NULL, plex, plexAux = NULL;
58568e3a2eefSMatthew G. Knepley   DMEnclosureType encAux;
58578e3a2eefSMatthew G. Knepley   Vec             A;
58588e3a2eefSMatthew G. Knepley   DMField         coordField;
58598e3a2eefSMatthew G. Knepley   PetscDS         prob, probAux = NULL;
58608e3a2eefSMatthew G. Knepley   PetscQuadrature quad;
58618e3a2eefSMatthew G. Knepley   PetscSection    section, globalSection, sectionAux;
58628e3a2eefSMatthew G. Knepley   PetscScalar    *elemMat, *elemMatD, *u, *u_t, *a = NULL, *y, *z;
58638e3a2eefSMatthew G. Knepley   const PetscInt *cells;
58648e3a2eefSMatthew G. Knepley   PetscInt        Nf, fieldI, fieldJ;
58658e3a2eefSMatthew G. Knepley   PetscInt        totDim, totDimAux = 0, cStart, cEnd, numCells, c;
58668e3a2eefSMatthew G. Knepley   PetscBool       hasDyn;
58678e3a2eefSMatthew G. Knepley   PetscErrorCode  ierr;
58688e3a2eefSMatthew G. Knepley 
58698e3a2eefSMatthew G. Knepley   PetscFunctionBegin;
58708e3a2eefSMatthew G. Knepley   ierr = PetscLogEventBegin(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
58718e3a2eefSMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
58728e3a2eefSMatthew G. Knepley   if (!cellIS) {
58738e3a2eefSMatthew G. Knepley     PetscInt depth;
58748e3a2eefSMatthew G. Knepley 
58758e3a2eefSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
58768e3a2eefSMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &cellIS);CHKERRQ(ierr);
58778e3a2eefSMatthew G. Knepley     if (!cellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &cellIS);CHKERRQ(ierr);}
58788e3a2eefSMatthew G. Knepley   } else {
58798e3a2eefSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) cellIS);CHKERRQ(ierr);
58808e3a2eefSMatthew G. Knepley   }
58818e3a2eefSMatthew G. Knepley   ierr = ISGetLocalSize(cellIS, &numCells);CHKERRQ(ierr);
58828e3a2eefSMatthew G. Knepley   ierr = ISGetPointRange(cellIS, &cStart, &cEnd, &cells);CHKERRQ(ierr);
58838e3a2eefSMatthew G. Knepley   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
58848e3a2eefSMatthew G. Knepley   ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);
58858e3a2eefSMatthew G. Knepley   ierr = DMGetCellDS(dm, cells ? cells[cStart] : cStart, &prob);CHKERRQ(ierr);
58868e3a2eefSMatthew G. Knepley   ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr);
58878e3a2eefSMatthew G. Knepley   ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr);
58888e3a2eefSMatthew G. Knepley   ierr = PetscDSHasDynamicJacobian(prob, &hasDyn);CHKERRQ(ierr);
58898e3a2eefSMatthew G. Knepley   hasDyn = hasDyn && (X_tShift != 0.0) ? PETSC_TRUE : PETSC_FALSE;
5890ac17215fSMatthew G. Knepley   ierr = DMGetAuxiliaryVec(dm, key.label, key.value, key.part, &A);CHKERRQ(ierr);
58918e3a2eefSMatthew G. Knepley   if (A) {
58928e3a2eefSMatthew G. Knepley     ierr = VecGetDM(A, &dmAux);CHKERRQ(ierr);
58938e3a2eefSMatthew G. Knepley     ierr = DMGetEnclosureRelation(dmAux, dm, &encAux);CHKERRQ(ierr);
58948e3a2eefSMatthew G. Knepley     ierr = DMConvert(dmAux, DMPLEX, &plexAux);CHKERRQ(ierr);
58958e3a2eefSMatthew G. Knepley     ierr = DMGetLocalSection(plexAux, &sectionAux);CHKERRQ(ierr);
58968e3a2eefSMatthew G. Knepley     ierr = DMGetDS(dmAux, &probAux);CHKERRQ(ierr);
58978e3a2eefSMatthew G. Knepley     ierr = PetscDSGetTotalDimension(probAux, &totDimAux);CHKERRQ(ierr);
58988e3a2eefSMatthew G. Knepley   }
58998e3a2eefSMatthew G. Knepley   ierr = VecSet(Z, 0.0);CHKERRQ(ierr);
59008e3a2eefSMatthew G. Knepley   ierr = PetscMalloc6(numCells*totDim,&u,X_t ? numCells*totDim : 0,&u_t,numCells*totDim*totDim,&elemMat,hasDyn ? numCells*totDim*totDim : 0, &elemMatD,numCells*totDim,&y,totDim,&z);CHKERRQ(ierr);
59018e3a2eefSMatthew G. Knepley   if (dmAux) {ierr = PetscMalloc1(numCells*totDimAux, &a);CHKERRQ(ierr);}
59028e3a2eefSMatthew G. Knepley   ierr = DMGetCoordinateField(dm, &coordField);CHKERRQ(ierr);
59038e3a2eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
59048e3a2eefSMatthew G. Knepley     const PetscInt cell = cells ? cells[c] : c;
59058e3a2eefSMatthew G. Knepley     const PetscInt cind = c - cStart;
59068e3a2eefSMatthew G. Knepley     PetscScalar   *x = NULL,  *x_t = NULL;
59078e3a2eefSMatthew G. Knepley     PetscInt       i;
59088e3a2eefSMatthew G. Knepley 
59098e3a2eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, X, cell, NULL, &x);CHKERRQ(ierr);
59108e3a2eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) u[cind*totDim+i] = x[i];
59118e3a2eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, X, cell, NULL, &x);CHKERRQ(ierr);
59128e3a2eefSMatthew G. Knepley     if (X_t) {
59138e3a2eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plex, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
59148e3a2eefSMatthew G. Knepley       for (i = 0; i < totDim; ++i) u_t[cind*totDim+i] = x_t[i];
59158e3a2eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plex, section, X_t, cell, NULL, &x_t);CHKERRQ(ierr);
59168e3a2eefSMatthew G. Knepley     }
59178e3a2eefSMatthew G. Knepley     if (dmAux) {
59188e3a2eefSMatthew G. Knepley       PetscInt subcell;
59198e3a2eefSMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmAux, dm, encAux, cell, &subcell);CHKERRQ(ierr);
59208e3a2eefSMatthew G. Knepley       ierr = DMPlexVecGetClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
59218e3a2eefSMatthew G. Knepley       for (i = 0; i < totDimAux; ++i) a[cind*totDimAux+i] = x[i];
59228e3a2eefSMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(plexAux, sectionAux, A, subcell, NULL, &x);CHKERRQ(ierr);
59238e3a2eefSMatthew G. Knepley     }
59248e3a2eefSMatthew G. Knepley     ierr = DMPlexVecGetClosure(plex, section, Y, cell, NULL, &x);CHKERRQ(ierr);
59258e3a2eefSMatthew G. Knepley     for (i = 0; i < totDim; ++i) y[cind*totDim+i] = x[i];
59268e3a2eefSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(plex, section, Y, cell, NULL, &x);CHKERRQ(ierr);
59278e3a2eefSMatthew G. Knepley   }
59288e3a2eefSMatthew G. Knepley   ierr = PetscArrayzero(elemMat, numCells*totDim*totDim);CHKERRQ(ierr);
59298e3a2eefSMatthew G. Knepley   if (hasDyn)  {ierr = PetscArrayzero(elemMatD, numCells*totDim*totDim);CHKERRQ(ierr);}
59308e3a2eefSMatthew G. Knepley   for (fieldI = 0; fieldI < Nf; ++fieldI) {
59318e3a2eefSMatthew G. Knepley     PetscFE  fe;
59328e3a2eefSMatthew G. Knepley     PetscInt Nb;
59338e3a2eefSMatthew G. Knepley     /* Conforming batches */
59348e3a2eefSMatthew G. Knepley     PetscInt numChunks, numBatches, numBlocks, Ne, blockSize, batchSize;
59358e3a2eefSMatthew G. Knepley     /* Remainder */
59368e3a2eefSMatthew G. Knepley     PetscInt Nr, offset, Nq;
59378e3a2eefSMatthew G. Knepley     PetscQuadrature qGeom = NULL;
59388e3a2eefSMatthew G. Knepley     PetscInt    maxDegree;
59398e3a2eefSMatthew G. Knepley     PetscFEGeom *cgeomFEM, *chunkGeom = NULL, *remGeom = NULL;
59408e3a2eefSMatthew G. Knepley 
59418e3a2eefSMatthew G. Knepley     ierr = PetscDSGetDiscretization(prob, fieldI, (PetscObject *) &fe);CHKERRQ(ierr);
59428e3a2eefSMatthew G. Knepley     ierr = PetscFEGetQuadrature(fe, &quad);CHKERRQ(ierr);
59438e3a2eefSMatthew G. Knepley     ierr = PetscFEGetDimension(fe, &Nb);CHKERRQ(ierr);
59448e3a2eefSMatthew G. Knepley     ierr = PetscFEGetTileSizes(fe, NULL, &numBlocks, NULL, &numBatches);CHKERRQ(ierr);
59458e3a2eefSMatthew G. Knepley     ierr = DMFieldGetDegree(coordField,cellIS,NULL,&maxDegree);CHKERRQ(ierr);
59468e3a2eefSMatthew G. Knepley     if (maxDegree <= 1) {ierr = DMFieldCreateDefaultQuadrature(coordField,cellIS,&qGeom);CHKERRQ(ierr);}
59478e3a2eefSMatthew G. Knepley     if (!qGeom) {
59488e3a2eefSMatthew G. Knepley       ierr = PetscFEGetQuadrature(fe,&qGeom);CHKERRQ(ierr);
59498e3a2eefSMatthew G. Knepley       ierr = PetscObjectReference((PetscObject)qGeom);CHKERRQ(ierr);
59508e3a2eefSMatthew G. Knepley     }
59518e3a2eefSMatthew G. Knepley     ierr = PetscQuadratureGetData(qGeom, NULL, NULL, &Nq, NULL, NULL);CHKERRQ(ierr);
59528e3a2eefSMatthew G. Knepley     ierr = DMSNESGetFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
59538e3a2eefSMatthew G. Knepley     blockSize = Nb;
59548e3a2eefSMatthew G. Knepley     batchSize = numBlocks * blockSize;
59558e3a2eefSMatthew G. Knepley     ierr = PetscFESetTileSizes(fe, blockSize, numBlocks, batchSize, numBatches);CHKERRQ(ierr);
59568e3a2eefSMatthew G. Knepley     numChunks = numCells / (numBatches*batchSize);
59578e3a2eefSMatthew G. Knepley     Ne        = numChunks*numBatches*batchSize;
59588e3a2eefSMatthew G. Knepley     Nr        = numCells % (numBatches*batchSize);
59598e3a2eefSMatthew G. Knepley     offset    = numCells - Nr;
59608e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
59618e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomGetChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
59628e3a2eefSMatthew G. Knepley     for (fieldJ = 0; fieldJ < Nf; ++fieldJ) {
59638e3a2eefSMatthew G. Knepley       key.field = fieldI*Nf + fieldJ;
59648e3a2eefSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMat);CHKERRQ(ierr);
59658e3a2eefSMatthew G. Knepley       ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMat[offset*totDim*totDim]);CHKERRQ(ierr);
59668e3a2eefSMatthew G. Knepley       if (hasDyn) {
59678e3a2eefSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Ne, chunkGeom, u, u_t, probAux, a, t, X_tShift, elemMatD);CHKERRQ(ierr);
59688e3a2eefSMatthew G. Knepley         ierr = PetscFEIntegrateJacobian(prob, PETSCFE_JACOBIAN_DYN, key, Nr, remGeom, &u[offset*totDim], u_t ? &u_t[offset*totDim] : NULL, probAux, &a[offset*totDimAux], t, X_tShift, &elemMatD[offset*totDim*totDim]);CHKERRQ(ierr);
59698e3a2eefSMatthew G. Knepley       }
59708e3a2eefSMatthew G. Knepley     }
59718e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,offset,numCells,&remGeom);CHKERRQ(ierr);
59728e3a2eefSMatthew G. Knepley     ierr = PetscFEGeomRestoreChunk(cgeomFEM,0,offset,&chunkGeom);CHKERRQ(ierr);
59738e3a2eefSMatthew G. Knepley     ierr = DMSNESRestoreFEGeom(coordField,cellIS,qGeom,PETSC_FALSE,&cgeomFEM);CHKERRQ(ierr);
59748e3a2eefSMatthew G. Knepley     ierr = PetscQuadratureDestroy(&qGeom);CHKERRQ(ierr);
59758e3a2eefSMatthew G. Knepley   }
59768e3a2eefSMatthew G. Knepley   if (hasDyn) {
59778e3a2eefSMatthew G. Knepley     for (c = 0; c < numCells*totDim*totDim; ++c) elemMat[c] += X_tShift*elemMatD[c];
59788e3a2eefSMatthew G. Knepley   }
59798e3a2eefSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
59808e3a2eefSMatthew G. Knepley     const PetscInt     cell = cells ? cells[c] : c;
59818e3a2eefSMatthew G. Knepley     const PetscInt     cind = c - cStart;
59828e3a2eefSMatthew G. Knepley     const PetscBLASInt M = totDim, one = 1;
59838e3a2eefSMatthew G. Knepley     const PetscScalar  a = 1.0, b = 0.0;
59848e3a2eefSMatthew G. Knepley 
59858e3a2eefSMatthew G. Knepley     PetscStackCallBLAS("BLASgemv", BLASgemv_("N", &M, &M, &a, &elemMat[cind*totDim*totDim], &M, &y[cind*totDim], &one, &b, z, &one));
59868e3a2eefSMatthew G. Knepley     if (mesh->printFEM > 1) {
59878e3a2eefSMatthew G. Knepley       ierr = DMPrintCellMatrix(c, name, totDim, totDim, &elemMat[cind*totDim*totDim]);CHKERRQ(ierr);
59888e3a2eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Y",  totDim, &y[cind*totDim]);CHKERRQ(ierr);
59898e3a2eefSMatthew G. Knepley       ierr = DMPrintCellVector(c, "Z",  totDim, z);CHKERRQ(ierr);
59908e3a2eefSMatthew G. Knepley     }
59918e3a2eefSMatthew G. Knepley     ierr = DMPlexVecSetClosure(dm, section, Z, cell, z, ADD_VALUES);CHKERRQ(ierr);
59928e3a2eefSMatthew G. Knepley   }
59938e3a2eefSMatthew G. Knepley   ierr = PetscFree6(u,u_t,elemMat,elemMatD,y,z);CHKERRQ(ierr);
59948e3a2eefSMatthew G. Knepley   if (mesh->printFEM) {
59958e3a2eefSMatthew G. Knepley     ierr = PetscPrintf(PetscObjectComm((PetscObject)Z), "Z:\n");CHKERRQ(ierr);
59968e3a2eefSMatthew G. Knepley     ierr = VecView(Z, NULL);CHKERRQ(ierr);
59978e3a2eefSMatthew G. Knepley   }
59988e3a2eefSMatthew G. Knepley   ierr = PetscFree(a);CHKERRQ(ierr);
59998e3a2eefSMatthew G. Knepley   ierr = ISDestroy(&cellIS);CHKERRQ(ierr);
60008e3a2eefSMatthew G. Knepley   ierr = DMDestroy(&plexAux);CHKERRQ(ierr);
60018e3a2eefSMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
60028e3a2eefSMatthew G. Knepley   ierr = PetscLogEventEnd(DMPLEX_JacobianFEM,dm,0,0,0);CHKERRQ(ierr);
60038e3a2eefSMatthew G. Knepley   PetscFunctionReturn(0);
60048e3a2eefSMatthew G. Knepley }
6005